99#include " libyuv/convert_argb.h"
1010#include < vector>
1111#include " JniException.h"
12- #include " scaler .h"
12+ #include " SizeScaler .h"
1313#include < android/log.h>
1414#include < android/data_space.h>
1515#include < sys/system_properties.h>
3131
3232jobject decodeImplementationNative (JNIEnv *env, jobject thiz,
3333 std::vector<uint8_t > &srcBuffer, jint scaledWidth,
34- jint scaledHeight, jint javaColorspace ) {
34+ jint scaledHeight, jint javaColorSpace, jint javaScaleMode ) {
3535 PreferredColorConfig preferredColorConfig;
36- checkDecodePreconditions (env, javaColorspace, &preferredColorConfig);
36+ ScaleMode scaleMode;
37+ if (!checkDecodePreconditions (env, javaColorSpace, &preferredColorConfig, javaScaleMode,
38+ &scaleMode)) {
39+ return static_cast <jobject>(nullptr );
40+ }
3741
3842 std::shared_ptr<heif_context> ctx (heif_context_alloc (),
3943 [](heif_context *c) { heif_context_free (c); });
@@ -131,87 +135,13 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
131135 int stride;
132136 std::vector<uint8_t > initialData;
133137
134- if (scaledHeight > 0 && scaledWidth > 0 ) {
135- if (bitDepth == 8 ) {
136- auto data = heif_image_get_plane_readonly (img, heif_channel_interleaved, &stride);
137- if (!data) {
138- heif_image_release (img);
139- heif_image_handle_release (handle);
140- std::string exception = " Interleaving planed has failed" ;
141- throwException (env, exception);
142- return nullptr ;
143- }
144- imageWidth = heif_image_get_width (img, heif_channel_interleaved);
145- imageHeight = heif_image_get_height (img, heif_channel_interleaved);
146- if (result.code != heif_error_Ok) {
147- heif_image_release (img);
148- heif_image_handle_release (handle);
149- std::string cp (result.message );
150- std::string exception = " HEIF wasn't able to scale image due to " + cp;
151- throwException (env, exception);
152- return static_cast <jobject>(nullptr );
153- }
154-
155- // For unknown reason some images doesn't scale properly by libheif in 8-bit
156- initialData.resize (scaledWidth * 4 * scaledHeight);
157- libyuv::ARGBScale (data, stride, imageWidth, imageHeight, initialData.data (),
158- scaledWidth * 4 , scaledWidth, scaledHeight, libyuv::kFilterBox );
159- stride = scaledWidth * 4 ;
160- imageHeight = scaledHeight;
161- imageWidth = scaledWidth;
162- heif_image_release (img);
163- } else {
164- heif_image *scaledImg;
165- result = heif_image_scale_image (img, &scaledImg, scaledWidth, scaledHeight, nullptr );
166- if (result.code != heif_error_Ok) {
167- heif_image_release (img);
168- heif_image_handle_release (handle);
169- std::string cp (result.message );
170- std::string exception = " HEIF wasn't able to scale image due to " + cp;
171- throwException (env, exception);
172- return static_cast <jobject>(nullptr );
173- }
174-
175- heif_image_release (img);
176-
177- auto data = heif_image_get_plane_readonly (scaledImg, heif_channel_interleaved, &stride);
178- if (!data) {
179- heif_image_release (scaledImg);
180- heif_image_handle_release (handle);
181- std::string exception = " Interleaving planed has failed" ;
182- throwException (env, exception);
183- return nullptr ;
184- }
185- imageWidth = heif_image_get_width (scaledImg, heif_channel_interleaved);
186- imageHeight = heif_image_get_height (scaledImg, heif_channel_interleaved);
187- initialData.resize (stride * imageHeight);
188- coder::CopyUnalignedRGBA (reinterpret_cast <const uint8_t *>(data), stride,
189- reinterpret_cast <uint8_t *>(initialData.data ()), stride,
190- imageWidth,
191- imageHeight, useBitmapHalf16Floats ? 2 : 1 );
192- heif_image_release (scaledImg);
193- }
194-
195- } else {
196- auto data = heif_image_get_plane_readonly (img, heif_channel_interleaved, &stride);
197- if (!data) {
198- heif_image_release (img);
199- heif_image_handle_release (handle);
200- std::string exception = " Interleaving planed has failed" ;
201- throwException (env, exception);
202- return nullptr ;
203- }
204-
205- imageWidth = heif_image_get_width (img, heif_channel_interleaved);
206- imageHeight = heif_image_get_height (img, heif_channel_interleaved);
207- initialData.resize (stride * imageHeight);
208-
209- coder::CopyUnalignedRGBA (reinterpret_cast <const uint8_t *>(data), stride,
210- reinterpret_cast <uint8_t *>(initialData.data ()), stride,
211- imageWidth,
212- imageHeight, useBitmapHalf16Floats ? 2 : 1 );
213-
214- heif_image_release (img);
138+ imageWidth = heif_image_get_width (img, heif_channel_interleaved);
139+ imageHeight = heif_image_get_height (img, heif_channel_interleaved);
140+ auto scaleResult = RescaleImage (initialData, env, handle, img, &stride, useBitmapHalf16Floats,
141+ &imageWidth, &imageHeight, scaledWidth, scaledHeight,
142+ scaleMode);
143+ if (!scaleResult) {
144+ return static_cast <jobject >(nullptr );
215145 }
216146
217147 std::shared_ptr<uint8_t > dstARGB (static_cast <uint8_t *>(malloc (stride * imageHeight)),
@@ -328,21 +258,22 @@ JNIEXPORT jobject JNICALL
328258Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_decodeImpl (JNIEnv *env, jobject thiz,
329259 jbyteArray byte_array, jint scaledWidth,
330260 jint scaledHeight,
331- jint javaColorspace) {
261+ jint javaColorspace, jint scaleMode ) {
332262 auto totalLength = env->GetArrayLength (byte_array);
333263 std::vector<uint8_t > srcBuffer (totalLength);
334264 env->GetByteArrayRegion (byte_array, 0 , totalLength,
335265 reinterpret_cast <jbyte *>(srcBuffer.data ()));
336266 return decodeImplementationNative (env, thiz, srcBuffer, scaledWidth, scaledHeight,
337- javaColorspace);
267+ javaColorspace, scaleMode );
338268}
339269extern " C"
340270JNIEXPORT jobject JNICALL
341271Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_decodeByteBufferImpl (JNIEnv *env, jobject thiz,
342272 jobject byteBuffer,
343273 jint scaledWidth,
344274 jint scaledHeight,
345- jint clrConfig) {
275+ jint clrConfig,
276+ jint scaleMode) {
346277 auto bufferAddress = reinterpret_cast <uint8_t *>(env->GetDirectBufferAddress (byteBuffer));
347278 int length = (int ) env->GetDirectBufferCapacity (byteBuffer);
348279 if (!bufferAddress || length <= 0 ) {
@@ -353,5 +284,5 @@ Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_decodeByteBufferImpl(JNIEnv *en
353284 std::vector<uint8_t > srcBuffer (length);
354285 std::copy (bufferAddress, bufferAddress + length, srcBuffer.begin ());
355286 return decodeImplementationNative (env, thiz, srcBuffer, scaledWidth, scaledHeight,
356- clrConfig);
287+ clrConfig, scaleMode );
357288}
0 commit comments