Skip to content

Commit b872a6e

Browse files
committed
deal exception appropriately
1 parent c4ce5bc commit b872a6e

5 files changed

Lines changed: 46 additions & 37 deletions

File tree

glideavif/src/main/cpp/avif_decoder.cpp

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
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

@@ -10,10 +9,10 @@
109
#include "avif_wrapper.hpp"
1110

1211
template<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

2928
template<typename T>
3029
void
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
}

glideavif/src/main/cpp/common.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@
44

55
#include "common.hpp"
66

7-
void throwException(JNIEnv *env, const char *mes) {
8-
jni_util::throwRuntimeException(env, mes);
9-
throw std::runtime_error(mes);
7+
void throwRuntimeException(JNIEnv *env, const char *what) {
8+
jclass classj = env->FindClass("java/lang/RuntimeException");
9+
if (classj == nullptr)
10+
return;
11+
12+
env->ThrowNew(classj, what);
13+
env->DeleteLocalRef(classj);
1014
}

glideavif/src/main/cpp/common.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44
#include <stdexcept>
55
#include <jni.h>
66
#include <android/log.h>
7-
#include <jni_util.hpp>
87

98
#define TAG "AvifDecoder"
109
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
1110

12-
void throwException(JNIEnv *env, const char *mes);
11+
void throwRuntimeException(JNIEnv *env, const char *what);
1312

1413
#endif

glideavif/src/main/cpp/my_bitmap.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,27 +65,27 @@ MyBitmap::MyBitmap(JNIEnv *env, int width, int height) {
6565
this->jbitmap = createBitmap(env, width, height);
6666

6767
if (AndroidBitmap_getInfo(env, jbitmap, &this->info) != ANDROID_BITMAP_RESULT_SUCCESS) {
68-
throwException(env, "get info from bitmap failed");
68+
throw std::runtime_error("get info from bitmap failed");
6969
}
7070

7171
if (this->info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
72-
throwException(env, "invalid bitmap format");
72+
throw std::runtime_error("invalid bitmap format");
7373
}
7474
}
7575

7676
void MyBitmap::Load(const std::vector<uint8_t> &rgbaList) {
7777
if (info.stride * info.height != rgbaList.size()) {
78-
throwException(env, "invalid array size");
78+
throw std::runtime_error("invalid array size");
7979
}
8080

8181
void *ptr;
8282
if (AndroidBitmap_lockPixels(env, jbitmap, &ptr) < 0) {
83-
throwException(env, "lock bitmap failed");
83+
throw std::runtime_error("lock bitmap failed");
8484
}
8585

8686
memcpy(ptr, (void *) rgbaList.data(), rgbaList.size());
8787

8888
if (AndroidBitmap_unlockPixels(env, jbitmap) < 0) {
89-
throwException(env, "unlock bitmap failed");
89+
throw std::runtime_error("unlock bitmap failed");
9090
}
9191
}

glideavif/src/main/cpp/my_bitmap.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
#include <vector>
55
#include <jni.h>
66
#include <android/bitmap.h>
7-
#include <string>
87

98
class MyBitmap {
109
public:

0 commit comments

Comments
 (0)