9
9
#include " libyuv/convert_argb.h"
10
10
#include < vector>
11
11
#include " JniException.h"
12
- #include " scaler .h"
12
+ #include " SizeScaler .h"
13
13
#include < android/log.h>
14
14
#include < android/data_space.h>
15
15
#include < sys/system_properties.h>
31
31
32
32
jobject decodeImplementationNative (JNIEnv *env, jobject thiz,
33
33
std::vector<uint8_t > &srcBuffer, jint scaledWidth,
34
- jint scaledHeight, jint javaColorspace ) {
34
+ jint scaledHeight, jint javaColorSpace, jint javaScaleMode ) {
35
35
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
+ }
37
41
38
42
std::shared_ptr<heif_context> ctx (heif_context_alloc (),
39
43
[](heif_context *c) { heif_context_free (c); });
@@ -131,87 +135,13 @@ jobject decodeImplementationNative(JNIEnv *env, jobject thiz,
131
135
int stride;
132
136
std::vector<uint8_t > initialData;
133
137
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 );
215
145
}
216
146
217
147
std::shared_ptr<uint8_t > dstARGB (static_cast <uint8_t *>(malloc (stride * imageHeight)),
@@ -328,21 +258,22 @@ JNIEXPORT jobject JNICALL
328
258
Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_decodeImpl (JNIEnv *env, jobject thiz,
329
259
jbyteArray byte_array, jint scaledWidth,
330
260
jint scaledHeight,
331
- jint javaColorspace) {
261
+ jint javaColorspace, jint scaleMode ) {
332
262
auto totalLength = env->GetArrayLength (byte_array);
333
263
std::vector<uint8_t > srcBuffer (totalLength);
334
264
env->GetByteArrayRegion (byte_array, 0 , totalLength,
335
265
reinterpret_cast <jbyte *>(srcBuffer.data ()));
336
266
return decodeImplementationNative (env, thiz, srcBuffer, scaledWidth, scaledHeight,
337
- javaColorspace);
267
+ javaColorspace, scaleMode );
338
268
}
339
269
extern " C"
340
270
JNIEXPORT jobject JNICALL
341
271
Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_decodeByteBufferImpl (JNIEnv *env, jobject thiz,
342
272
jobject byteBuffer,
343
273
jint scaledWidth,
344
274
jint scaledHeight,
345
- jint clrConfig) {
275
+ jint clrConfig,
276
+ jint scaleMode) {
346
277
auto bufferAddress = reinterpret_cast <uint8_t *>(env->GetDirectBufferAddress (byteBuffer));
347
278
int length = (int ) env->GetDirectBufferCapacity (byteBuffer);
348
279
if (!bufferAddress || length <= 0 ) {
@@ -353,5 +284,5 @@ Java_com_radzivon_bartoshyk_avif_coder_HeifCoder_decodeByteBufferImpl(JNIEnv *en
353
284
std::vector<uint8_t > srcBuffer (length);
354
285
std::copy (bufferAddress, bufferAddress + length, srcBuffer.begin ());
355
286
return decodeImplementationNative (env, thiz, srcBuffer, scaledWidth, scaledHeight,
356
- clrConfig);
287
+ clrConfig, scaleMode );
357
288
}
0 commit comments