@@ -38,22 +38,25 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
3838 std::shared_ptr<heif_context> ctx (heif_context_alloc (),
3939 [](heif_context *c) { heif_context_free (c); });
4040 if (!ctx) {
41- throwCoderCreationException (env);
41+ std::string exception = " Can't create HEIF/AVIF decoder due to unknown reason" ;
42+ throwException (env, exception);
4243 return static_cast <jobject>(nullptr );
4344 }
4445
4546 auto result = heif_context_read_from_memory_without_copy (ctx.get (), srcBuffer.data (),
4647 srcBuffer.size (),
4748 nullptr );
4849 if (result.code != heif_error_Ok) {
49- throwCannotReadFileException (env);
50+ std::string exception = " Can't read heif file exception" ;
51+ throwException (env, exception);
5052 return static_cast <jobject>(nullptr );
5153 }
5254
5355 heif_image_handle *handle;
5456 result = heif_context_get_primary_image_handle (ctx.get (), &handle);
5557 if (result.code != heif_error_Ok) {
56- throwCannotReadFileException (env);
58+ std::string exception = " Acquiring an image from file has failed" ;
59+ throwException (env, exception);
5760 return static_cast <jobject>(nullptr );
5861 }
5962
@@ -85,7 +88,8 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
8588
8689 if (result.code != heif_error_Ok) {
8790 heif_image_handle_release (handle);
88- throwCantDecodeImageException (env);
91+ std::string exception = " Decoding an image has failed" ;
92+ throwException (env, exception);
8993 return static_cast <jobject>(nullptr );
9094 }
9195
@@ -133,15 +137,18 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
133137 if (!data) {
134138 heif_image_release (img);
135139 heif_image_handle_release (handle);
136- throwCannotReadFileException (env);
140+ std::string exception = " Interleaving planed has failed" ;
141+ throwException (env, exception);
137142 return nullptr ;
138143 }
139144 imageWidth = heif_image_get_width (img, heif_channel_interleaved);
140145 imageHeight = heif_image_get_height (img, heif_channel_interleaved);
141146 if (result.code != heif_error_Ok) {
142147 heif_image_release (img);
143148 heif_image_handle_release (handle);
144- throwInvalidScale (env, result.message );
149+ std::string cp (result.message );
150+ std::string exception = " HEIF wasn't able to scale image due to " + cp;
151+ throwException (env, exception);
145152 return static_cast <jobject>(nullptr );
146153 }
147154
@@ -159,7 +166,9 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
159166 if (result.code != heif_error_Ok) {
160167 heif_image_release (img);
161168 heif_image_handle_release (handle);
162- throwInvalidScale (env, result.message );
169+ std::string cp (result.message );
170+ std::string exception = " HEIF wasn't able to scale image due to " + cp;
171+ throwException (env, exception);
163172 return static_cast <jobject>(nullptr );
164173 }
165174
@@ -169,7 +178,8 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
169178 if (!data) {
170179 heif_image_release (scaledImg);
171180 heif_image_handle_release (handle);
172- throwCannotReadFileException (env);
181+ std::string exception = " Interleaving planed has failed" ;
182+ throwException (env, exception);
173183 return nullptr ;
174184 }
175185 imageWidth = heif_image_get_width (scaledImg, heif_channel_interleaved);
@@ -187,7 +197,8 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
187197 if (!data) {
188198 heif_image_release (img);
189199 heif_image_handle_release (handle);
190- throwCannotReadFileException (env);
200+ std::string exception = " Interleaving planed has failed" ;
201+ throwException (env, exception);
191202 return nullptr ;
192203 }
193204
@@ -303,11 +314,13 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
303314
304315 std::string imageConfig = useBitmapHalf16Floats ? " RGBA_F16" : " ARGB_8888" ;
305316
306- ReformatColorConfig (dstARGB, imageConfig, preferredColorConfig, bitDepth, imageWidth,
307- imageHeight, &stride, &useBitmapHalf16Floats);
317+ jobject hwBuffer = nullptr ;
318+
319+ ReformatColorConfig (env, dstARGB, imageConfig, preferredColorConfig, bitDepth, imageWidth,
320+ imageHeight, &stride, &useBitmapHalf16Floats, &hwBuffer);
308321
309322 return createBitmap (env, dstARGB, imageConfig, stride, imageWidth, imageHeight,
310- useBitmapHalf16Floats);
323+ useBitmapHalf16Floats, hwBuffer );
311324}
312325
313326extern " C"
@@ -322,4 +335,23 @@ Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_decodeImpl(JNIEnv *env, jobject
322335 reinterpret_cast <jbyte *>(srcBuffer.data ()));
323336 return decodeImplementationNative (env, thiz, srcBuffer, scaledWidth, scaledHeight,
324337 javaColorspace);
338+ }
339+ extern " C"
340+ JNIEXPORT jobject JNICALL
341+ Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_decodeByteBufferImpl (JNIEnv *env, jobject thiz,
342+ jobject byteBuffer,
343+ jint scaledWidth,
344+ jint scaledHeight,
345+ jint clrConfig) {
346+ auto bufferAddress = reinterpret_cast <uint8_t *>(env->GetDirectBufferAddress (byteBuffer));
347+ int length = (int ) env->GetDirectBufferCapacity (byteBuffer);
348+ if (!bufferAddress || length <= 0 ) {
349+ std::string errorString = " Only direct byte buffers are supported" ;
350+ throwException (env, errorString);
351+ return nullptr ;
352+ }
353+ std::vector<uint8_t > srcBuffer (length);
354+ std::copy (bufferAddress, bufferAddress + length, srcBuffer.begin ());
355+ return decodeImplementationNative (env, thiz, srcBuffer, scaledWidth, scaledHeight,
356+ clrConfig);
325357}
0 commit comments