@@ -95,6 +95,8 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
9595 std::copy (reinterpret_cast <uint8_t *>(addr),
9696 reinterpret_cast <uint8_t *>(addr) + info.height * info.stride , sourceData.data ());
9797
98+ AndroidBitmap_unlockPixels (env, bitmap);
99+
98100 heif_image *image;
99101 heif_chroma chroma = heif_chroma_interleaved_RGBA;
100102 if (info.format == ANDROID_BITMAP_FORMAT_RGBA_F16 &&
@@ -104,7 +106,6 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
104106 result = heif_image_create ((int ) info.width , (int ) info.height , heif_colorspace_RGB,
105107 chroma, &image);
106108 if (result.code != heif_error_Ok) {
107- AndroidBitmap_unlockPixels (env, bitmap);
108109 throwCantEncodeImageException (env, result.message );
109110 return static_cast <jbyteArray>(nullptr );
110111 }
@@ -123,15 +124,15 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
123124 result = heif_image_add_plane (image, heif_channel_interleaved, (int ) info.width ,
124125 (int ) info.height , bitDepth);
125126 if (result.code != heif_error_Ok) {
126- AndroidBitmap_unlockPixels (env, bitmap);
127127 throwCantEncodeImageException (env, result.message );
128128 return static_cast <jbyteArray>(nullptr );
129129 }
130130 int stride;
131131 uint8_t *imgData = heif_image_get_plane (image, heif_channel_interleaved, &stride);
132132 if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888) {
133- libyuv::ARGBCopy (sourceData.data (), (int ) info.stride , imgData,
134- stride, (int ) info.width , (int ) info.height );
133+ libyuv::ARGBCopy (sourceData.data (), (int ) info.stride ,
134+ imgData,
135+ stride, (int ) info.width , (int ) info.height );
135136 } else if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) {
136137 libyuv::RGB565ToARGB (sourceData.data (), (int ) info.stride , imgData,
137138 stride, (int ) info.width , (int ) info.height );
@@ -178,21 +179,22 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
178179 for (int y = 0 ; y < info.height ; ++y) {
179180
180181 auto srcPtr = reinterpret_cast <uint16_t *>(srcData);
181- auto dstPtr = reinterpret_cast <uint64_t *>(data64Ptr);
182+ auto dstPtr = reinterpret_cast <uint16_t *>(data64Ptr);
182183
183184 for (int x = 0 ; x < info.width ; ++x) {
184185 auto alpha = half_to_float (srcPtr[3 ]);
185- tmpR = (uint16_t ) (half_to_float (srcPtr[0 ]) / scale / (alpha != 0 ? alpha : 1 ) );
186- tmpG = (uint16_t ) (half_to_float (srcPtr[1 ]) / scale / (alpha != 0 ? alpha : 1 ) );
187- tmpB = (uint16_t ) (half_to_float (srcPtr[2 ]) / scale / (alpha != 0 ? alpha : 1 ) );
186+ tmpR = (uint16_t ) (half_to_float (srcPtr[0 ]) / scale);
187+ tmpG = (uint16_t ) (half_to_float (srcPtr[1 ]) / scale);
188+ tmpB = (uint16_t ) (half_to_float (srcPtr[2 ]) / scale);
188189 tmpA = (uint16_t ) (alpha / scale);
189- uint64_t color =
190- ((uint64_t ) tmpA & 0xffff ) << 48 | ((uint64_t ) tmpB & 0xffff ) << 32 |
191- ((uint64_t ) tmpG & 0xffff ) << 16 | ((uint64_t ) tmpR & 0xffff );
192- dstPtr[0 ] = color;
190+
191+ dstPtr[0 ] = tmpR;
192+ dstPtr[1 ] = tmpG;
193+ dstPtr[2 ] = tmpB;
194+ dstPtr[3 ] = tmpA;
193195
194196 srcPtr += 4 ;
195- dstPtr += 1 ;
197+ dstPtr += 4 ;
196198 }
197199
198200 srcData += info.stride ;
@@ -242,7 +244,6 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
242244 dstARGB.reset ();
243245 }
244246 }
245- AndroidBitmap_unlockPixels (env, bitmap);
246247
247248 heif_image_handle *handle;
248249 std::shared_ptr<heif_encoding_options> options (heif_encoding_options_alloc (),
@@ -251,6 +252,8 @@ jbyteArray encodeBitmap(JNIEnv *env, jobject thiz,
251252 });
252253 options->version = 5 ;
253254 options->image_orientation = heif_orientation_normal;
255+ options->output_nclx_profile = nullptr ;
256+ options->save_two_colr_boxes_when_ICC_and_nclx_available = false ;
254257
255258 result = heif_context_encode_image (ctx.get (), image, encoder.get (), options.get (), &handle);
256259 options.reset ();
0 commit comments