11#include < jni.h>
22#include < string>
33#include < avif/avif.h>
4- #include < jni_util.hpp>
54#include < libyuv/convert_argb.h>
65#include < algorithm>
76
109#include " avif_wrapper.hpp"
1110
1211template <typename T>
13- void copyGrayscalePixels (JNIEnv *env, avifImage *im, std::vector<uint8_t > &rgbaList) {
12+ void copyGrayscalePixels (avifImage *im, std::vector<uint8_t > &rgbaList) {
1413 uint32_t w = im->width * sizeof (T) / sizeof (uint8_t );
1514 if (im->yuvRowBytes [0 ] < w) {
16- throwException (env, " invalid yuv bytes" );
15+ throw std::runtime_error ( " invalid yuv bytes" );
1716 }
1817
1918 auto array = (uint32_t *) rgbaList.data ();
@@ -28,10 +27,10 @@ void copyGrayscalePixels(JNIEnv *env, avifImage *im, std::vector<uint8_t> &rgbaL
2827
2928template <typename T>
3029void
31- copyAlphaPixels (JNIEnv *env, avifImage *im, bool limitedRange, std::vector<uint8_t > &rgbaList) {
30+ copyAlphaPixels (avifImage *im, bool limitedRange, std::vector<uint8_t > &rgbaList) {
3231 uint32_t w = im->width * sizeof (T) / sizeof (uint8_t );
3332 if (im->alphaRowBytes < w) {
34- throwException (env, " invalid alpha bytes" );
33+ throw std::runtime_error ( " invalid alpha bytes" );
3534 }
3635
3736 for (int i = 0 ; i < im->height ; ++i) {
@@ -46,7 +45,7 @@ copyAlphaPixels(JNIEnv *env, avifImage *im, bool limitedRange, std::vector<uint8
4645 }
4746}
4847
49- bool isGrayscale (avifImage *im) {
48+ inline bool isGrayscale (avifImage *im) {
5049 return !(im->yuvRowBytes [1 ] && im->yuvRowBytes [2 ]);
5150}
5251
@@ -109,12 +108,12 @@ jobject decodeAvif(JNIEnv *env, const uint8_t *sourceData, int sourceDataLength)
109108 avifDecoderPtr decoder (avifDecoderCreate ());
110109 auto result = avifDecoderParse (decoder.get (), &raw);
111110 if (result != AVIF_RESULT_OK ) {
112- throwException (env, " parse failed" );
111+ throw std::runtime_error ( " avifDecoderParse failed" );
113112 }
114113
115114 result = avifDecoderNextImage (decoder.get ());
116115 if (result != AVIF_RESULT_OK ) {
117- throwException (env, " decode failed" );
116+ throw std::runtime_error ( " avifDecoderNextImage failed" );
118117 }
119118
120119 avifImage *im = decoder->image ;
@@ -127,39 +126,41 @@ jobject decodeAvif(JNIEnv *env, const uint8_t *sourceData, int sourceDataLength)
127126 // transferCharacteristicsとmatrixCoefficientsの組み合わせによっては駄目な可能性がある
128127 switch (im->depth ) {
129128 case 8 :
130- copyGrayscalePixels<uint8_t >(env, im, rgbaList);
129+ copyGrayscalePixels<uint8_t >(im, rgbaList);
131130 return ;
132131 case 10 :
133132 case 12 :
134- copyGrayscalePixels<uint16_t >(env, im, rgbaList);
133+ copyGrayscalePixels<uint16_t >(im, rgbaList);
135134 return ;
136- default :
137- throwException (env, " unknown color depth" );
135+ default : {
136+ throw std::runtime_error (" unknown color depth" );
137+ }
138138 }
139139 }
140140
141141 if (!im->nclx .fullRangeFlag && im->depth == 8 ) {
142142 // libyuv は8bitかつlimited rangeにのみ対応
143143 if (isGrayscale (im)) {
144- auto result = libyuv::I400ToARGB (im->yuvPlanes [0 ], im->yuvRowBytes [0 ],
145- rgbaList.data (), im->width * 4 ,
146- im->width , im->height );
147- if (result != 0 ) {
148- throwException (env, " convert yuv to rgb with libyuv failed" );
144+ auto result1 = libyuv::I400ToARGB (
145+ im->yuvPlanes [0 ], im->yuvRowBytes [0 ],
146+ rgbaList.data (), im->width * 4 ,
147+ im->width , im->height );
148+ if (result1 != 0 ) {
149+ throw std::runtime_error (" convert yuv to rgb with libyuv failed" );
149150 }
150151 return ;
151152 }
152153
153154 auto convertYUV = getYUVConvertFunc (im);
154155 if (convertYUV) {
155- auto result = convertYUV (
156+ auto result1 = convertYUV (
156157 im->yuvPlanes [0 ], im->yuvRowBytes [0 ],
157158 im->yuvPlanes [1 ], im->yuvRowBytes [1 ],
158159 im->yuvPlanes [2 ], im->yuvRowBytes [2 ],
159160 rgbaList.data (), im->width * 4 ,
160161 im->width , im->height );
161- if (result != 0 ) {
162- throwException (env, " convert 8bpc limited yuv to rgb with libyuv failed" );
162+ if (result1 != 0 ) {
163+ throw std::runtime_error ( " convert 8bpc limited yuv to rgb with libyuv failed" );
163164 }
164165 return ;
165166 }
@@ -173,7 +174,7 @@ jobject decodeAvif(JNIEnv *env, const uint8_t *sourceData, int sourceDataLength)
173174 avifRGBImageAllocatePixels (rgb.get ());
174175 avifImageYUVToRGB (im, rgb.get ());
175176 if (rgbaList.size () != rgb->rowBytes * rgb->height ) {
176- throwException (env, " invalid size" );
177+ throw std::runtime_error ( " invalid size" );
177178 }
178179 memcpy (rgbaList.data (), rgb->pixels , rgb->rowBytes * rgb->height );
179180
@@ -200,14 +201,15 @@ jobject decodeAvif(JNIEnv *env, const uint8_t *sourceData, int sourceDataLength)
200201 }
201202 switch (im->depth ) {
202203 case 8 :
203- copyAlphaPixels<uint8_t >(env, im, limitedRange, rgbaList);
204+ copyAlphaPixels<uint8_t >(im, limitedRange, rgbaList);
204205 break ;
205206 case 10 :
206207 case 12 :
207- copyAlphaPixels<uint16_t >(env, im, limitedRange, rgbaList);
208+ copyAlphaPixels<uint16_t >(im, limitedRange, rgbaList);
208209 break ;
209- default :
210- throwException (env, " unknown color depth" );
210+ default : {
211+ throw std::runtime_error (" unknown color depth" );
212+ }
211213 }
212214 }
213215
@@ -226,12 +228,17 @@ Java_jp_co_link_1u_library_glideavif_AvifDecoderFromByteBuffer_decodeAvif(
226228 try {
227229 auto buffer = env->GetDirectBufferAddress (sourceData);
228230 if (buffer == nullptr ) {
229- throwException (env, " allocation failed " );
231+ throw std::runtime_error ( " buffer must be DirectByteBuffer " );
230232 }
231233
232234 return decodeAvif (env, (const uint8_t *) buffer, sourceDataLength);
233235 }
234236 catch (const std::exception &e) {
237+ throwRuntimeException (env, e.what ());
238+ return nullptr ;
239+ }
240+ catch (...) {
241+ throwRuntimeException (env, " unknown error occurred" );
235242 return nullptr ;
236243 }
237244}
0 commit comments