| 5 | #include <android/bitmap.h> |
| 6 | #include <libwebp/webp/decode.h> |
| 7 | #include <libwebp/webp/encode.h> |
| 8 | #include "utils.h" |
| 9 | #include "image.h" |
| 10 |
| 11 | jclass jclass_NullPointerException; |
| 12 | jclass jclass_RuntimeException; |
| 13 |
| 14 | jclass jclass_Options; |
| 15 | jfieldID jclass_Options_inJustDecodeBounds; |
| 16 | jfieldID jclass_Options_outHeight; |
| 17 | jfieldID jclass_Options_outWidth; |
| 18 |
| 19 | const uint32_t PGPhotoEnhanceHistogramBins = 256; |
| 20 | const uint32_t PGPhotoEnhanceSegments = 4; |
| 21 |
| 22 | jclass createGlobarRef(JNIEnv *env, jclass class) { |
| 23 | if (class) { |
| 24 | return (*env)->NewGlobalRef(env, class); |
| 25 | } |
| 26 | return 0; |
| 27 | } |
| 28 |
| 29 | jint imageOnJNILoad(JavaVM *vm, void *reserved, JNIEnv *env) { |
| 30 | jclass_NullPointerException = createGlobarRef(env, (*env)->FindClass(env, "java/lang/NullPointerException")); |
| 31 | if (jclass_NullPointerException == 0) { |
| 32 | return -1; |
| 33 | } |
| 34 | jclass_RuntimeException = createGlobarRef(env, (*env)->FindClass(env, "java/lang/RuntimeException")); |
| 35 | if (jclass_RuntimeException == 0) { |
| 36 | return -1; |
| 37 | } |
| 38 |
| 39 | jclass_Options = createGlobarRef(env, (*env)->FindClass(env, "android/graphics/BitmapFactory$Options")); |
| 40 | if (jclass_Options == 0) { |
| 41 | return -1; |
| 42 | } |
| 43 | jclass_Options_inJustDecodeBounds = (*env)->GetFieldID(env, jclass_Options, "inJustDecodeBounds", "Z"); |
| 44 | if (jclass_Options_inJustDecodeBounds == 0) { |
| 45 | return -1; |
| 46 | } |
| 47 | jclass_Options_outHeight = (*env)->GetFieldID(env, jclass_Options, "outHeight", "I"); |
| 48 | if (jclass_Options_outHeight == 0) { |
| 49 | return -1; |
| 50 | } |
| 51 | jclass_Options_outWidth = (*env)->GetFieldID(env, jclass_Options, "outWidth", "I"); |
| 52 | if (jclass_Options_outWidth == 0) { |
| 53 | return -1; |
| 54 | } |
| 55 |
| 56 | return JNI_VERSION_1_6; |
| 57 | } |
| 58 |
| 59 | static inline uint64_t get_colors (const uint8_t *p) { |
| 60 | return p[0] + (p[1] << 16) + ((uint64_t)p[2] << 32); |
| 61 | } |
| 62 |
| 63 | static void fastBlurMore(int imageWidth, int imageHeight, int imageStride, void *pixels, int radius) { |
| 64 | uint8_t *pix = (uint8_t *)pixels; |
| 65 | const int w = imageWidth; |
| 66 | const int h = imageHeight; |
| 67 | const int stride = imageStride; |
| 68 | const int r1 = radius + 1; |
| 69 | const int div = radius * 2 + 1; |
| 70 |
| 71 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || imageStride > imageWidth * 4) { |
| 72 | return; |
| 73 | } |
| 74 |
| 75 | uint64_t *rgb = malloc(imageWidth * imageHeight * sizeof(uint64_t)); |
| 76 | if (rgb == NULL) { |
| 77 | return; |
| 78 | } |
| 79 |
| 80 | int x, y, i; |
| 81 |
| 82 | int yw = 0; |
| 83 | const int we = w - r1; |
| 84 | for (y = 0; y < h; y++) { |
| 85 | uint64_t cur = get_colors (&pix[yw]); |
| 86 | uint64_t rgballsum = -radius * cur; |
| 87 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 88 |
| 89 | for (i = 1; i <= radius; i++) { |
| 90 | uint64_t cur = get_colors (&pix[yw + i * 4]); |
| 91 | rgbsum += cur * (r1 - i); |
| 92 | rgballsum += cur; |
| 93 | } |
| 94 |
| 95 | x = 0; |
| 96 |
| 97 | #define update(start, middle, end) \ |
| 98 | rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \ |
| 99 | rgballsum += get_colors (&pix[yw + (start) * 4]) - 2 * get_colors (&pix[yw + (middle) * 4]) + get_colors (&pix[yw + (end) * 4]); \ |
| 100 | rgbsum += rgballsum; \ |
| 101 | x++; \ |
| 102 |
| 103 | while (x < r1) { |
| 104 | update (0, x, x + r1); |
| 105 | } |
| 106 | while (x < we) { |
| 107 | update (x - r1, x, x + r1); |
| 108 | } |
| 109 | while (x < w) { |
| 110 | update (x - r1, x, w - 1); |
| 111 | } |
| 112 | #undef update |
| 113 |
| 114 | yw += stride; |
| 115 | } |
| 116 |
| 117 | const int he = h - r1; |
| 118 | for (x = 0; x < w; x++) { |
| 119 | uint64_t rgballsum = -radius * rgb[x]; |
| 120 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 121 | for (i = 1; i <= radius; i++) { |
| 122 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 123 | rgballsum += rgb[i * w + x]; |
| 124 | } |
| 125 |
| 126 | y = 0; |
| 127 | int yi = x * 4; |
| 128 |
| 129 | #define update(start, middle, end) \ |
| 130 | int64_t res = rgbsum >> 6; \ |
| 131 | pix[yi] = res; \ |
| 132 | pix[yi + 1] = res >> 16; \ |
| 133 | pix[yi + 2] = res >> 32; \ |
| 134 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 135 | rgbsum += rgballsum; \ |
| 136 | y++; \ |
| 137 | yi += stride; |
| 138 |
| 139 | while (y < r1) { |
| 140 | update (0, y, y + r1); |
| 141 | } |
| 142 | while (y < he) { |
| 143 | update (y - r1, y, y + r1); |
| 144 | } |
| 145 | while (y < h) { |
| 146 | update (y - r1, y, h - 1); |
| 147 | } |
| 148 | #undef update |
| 149 | } |
| 150 | } |
| 151 |
| 152 | static void fastBlur(int imageWidth, int imageHeight, int imageStride, void *pixels, int radius) { |
| 153 | uint8_t *pix = (uint8_t *)pixels; |
| 154 | if (pix == NULL) { |
| 155 | return; |
| 156 | } |
| 157 | const int w = imageWidth; |
| 158 | const int h = imageHeight; |
| 159 | const int stride = imageStride; |
| 160 | const int r1 = radius + 1; |
| 161 | const int div = radius * 2 + 1; |
| 162 | int shift; |
| 163 | if (radius == 1) { |
| 164 | shift = 2; |
| 165 | } else if (radius == 3) { |
| 166 | shift = 4; |
| 167 | } else if (radius == 7) { |
| 168 | shift = 6; |
| 169 | } else if (radius == 15) { |
| 170 | shift = 8; |
| 171 | } else { |
| 172 | return; |
| 173 | } |
| 174 |
| 175 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || imageStride > imageWidth * 4) { |
| 176 | return; |
| 177 | } |
| 178 |
| 179 | uint64_t *rgb = malloc(imageWidth * imageHeight * sizeof(uint64_t)); |
| 180 | if (rgb == NULL) { |
| 181 | return; |
| 182 | } |
| 183 |
| 184 | int x, y, i; |
| 185 |
| 186 | int yw = 0; |
| 187 | const int we = w - r1; |
| 188 | for (y = 0; y < h; y++) { |
| 189 | uint64_t cur = get_colors (&pix[yw]); |
| 190 | uint64_t rgballsum = -radius * cur; |
| 191 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 192 |
| 193 | for (i = 1; i <= radius; i++) { |
| 194 | uint64_t cur = get_colors (&pix[yw + i * 4]); |
| 195 | rgbsum += cur * (r1 - i); |
| 196 | rgballsum += cur; |
| 197 | } |
| 198 |
| 199 | x = 0; |
| 200 |
| 201 | #define update(start, middle, end) \ |
| 202 | rgb[y * w + x] = (rgbsum >> shift) & 0x00FF00FF00FF00FFLL; \ |
| 203 | rgballsum += get_colors (&pix[yw + (start) * 4]) - 2 * get_colors (&pix[yw + (middle) * 4]) + get_colors (&pix[yw + (end) * 4]); \ |
| 204 | rgbsum += rgballsum; \ |
| 205 | x++; \ |
| 206 |
| 207 | while (x < r1) { |
| 208 | update (0, x, x + r1); |
| 209 | } |
| 210 | while (x < we) { |
| 211 | update (x - r1, x, x + r1); |
| 212 | } |
| 213 | while (x < w) { |
| 214 | update (x - r1, x, w - 1); |
| 215 | } |
| 216 |
| 217 | #undef update |
| 218 |
| 219 | yw += stride; |
| 220 | } |
| 221 |
| 222 | const int he = h - r1; |
| 223 | for (x = 0; x < w; x++) { |
| 224 | uint64_t rgballsum = -radius * rgb[x]; |
| 225 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 226 | for (i = 1; i <= radius; i++) { |
| 227 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 228 | rgballsum += rgb[i * w + x]; |
| 229 | } |
| 230 |
| 231 | y = 0; |
| 232 | int yi = x * 4; |
| 233 |
| 234 | #define update(start, middle, end) \ |
| 235 | int64_t res = rgbsum >> shift; \ |
| 236 | pix[yi] = res; \ |
| 237 | pix[yi + 1] = res >> 16; \ |
| 238 | pix[yi + 2] = res >> 32; \ |
| 239 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 240 | rgbsum += rgballsum; \ |
| 241 | y++; \ |
| 242 | yi += stride; |
| 243 |
| 244 | while (y < r1) { |
| 245 | update (0, y, y + r1); |
| 246 | } |
| 247 | while (y < he) { |
| 248 | update (y - r1, y, y + r1); |
| 249 | } |
| 250 | while (y < h) { |
| 251 | update (y - r1, y, h - 1); |
| 252 | } |
| 253 | #undef update |
| 254 | } |
| 255 |
| 256 | free(rgb); |
| 257 | } |
| 258 |
| 259 | typedef struct my_error_mgr { |
| 260 | struct jpeg_error_mgr pub; |
| 261 | jmp_buf setjmp_buffer; |
| 262 | } *my_error_ptr; |
| 263 |
| 264 |
| 265 | METHODDEF(void) my_error_exit(j_common_ptr cinfo) { |
| 266 | my_error_ptr myerr = (my_error_ptr) cinfo->err; |
| 267 | (*cinfo->err->output_message) (cinfo); |
| 268 | longjmp(myerr->setjmp_buffer, 1); |
| 269 | } |
| 270 |
| 271 | JNIEXPORT void Java_org_telegram_messenger_Utilities_blurBitmap(JNIEnv *env, jclass class, jobject bitmap, int radius, int unpin, int width, int height, int stride) { |
| 272 | if (!bitmap) { |
| 273 | return; |
| 274 | } |
| 275 |
| 276 | if (!width || !height || !stride) { |
| 277 | return; |
| 278 | } |
| 279 |
| 280 | void *pixels = 0; |
| 281 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { |
| 282 | return; |
| 283 | } |
| 284 | if (radius <= 3) { |
| 285 | fastBlur(width, height, stride, pixels, radius); |
| 286 | } else { |
| 287 | fastBlurMore(width, height, stride, pixels, radius); |
| 288 | } |
| 289 | if (unpin) { |
| 290 | AndroidBitmap_unlockPixels(env, bitmap); |
| 291 | } |
| 292 | } |
| 293 |
| 294 | JNIEXPORT void Java_org_telegram_messenger_Utilities_calcCDT(JNIEnv *env, jclass class, jobject hsvBuffer, int width, int height, jobject buffer) { |
| 295 | float imageWidth = width; |
| 296 | float imageHeight = height; |
| 297 | float _clipLimit = 1.25f; |
| 298 |
| 299 | uint32_t totalSegments = PGPhotoEnhanceSegments * PGPhotoEnhanceSegments; |
| 300 | uint32_t tileArea = (uint32_t)(floorf(imageWidth / PGPhotoEnhanceSegments) * floorf(imageHeight / PGPhotoEnhanceSegments)); |
| 301 | uint32_t clipLimit = (uint32_t)max(1, _clipLimit * tileArea / (float) PGPhotoEnhanceHistogramBins); |
| 302 | float scale = 255.0f / (float) tileArea; |
| 303 |
| 304 |
| 305 | unsigned char *bytes = (*env)->GetDirectBufferAddress(env, hsvBuffer); |
| 306 |
| 307 | uint32_t **hist = calloc(totalSegments, sizeof(uint32_t *)); |
| 308 | uint32_t **cdfs = calloc(totalSegments, sizeof(uint32_t *)); |
| 309 | uint32_t *cdfsMin = calloc(totalSegments, sizeof(uint32_t)); |
| 310 | uint32_t *cdfsMax = calloc(totalSegments, sizeof(uint32_t)); |
| 311 |
| 312 | for (uint32_t a = 0; a < totalSegments; a++) { |
| 313 | hist[a] = calloc(PGPhotoEnhanceHistogramBins, sizeof(uint32_t)); |
| 314 | cdfs[a] = calloc(PGPhotoEnhanceHistogramBins, sizeof(uint32_t)); |
| 315 | } |
| 316 |
| 317 | float xMul = PGPhotoEnhanceSegments / imageWidth; |
| 318 | float yMul = PGPhotoEnhanceSegments / imageHeight; |
| 319 |
| 320 | for (uint32_t y = 0; y < imageHeight; y++) { |
| 321 | uint32_t yOffset = y * width * 4; |
| 322 | for (uint32_t x = 0; x < imageWidth; x++) { |
| 323 | uint32_t index = x * 4 + yOffset; |
| 324 |
| 325 | uint32_t tx = (uint32_t)(x * xMul); |
| 326 | uint32_t ty = (uint32_t)(y * yMul); |
| 327 | uint32_t t = ty * PGPhotoEnhanceSegments + tx; |
| 328 |
| 329 | hist[t][bytes[index + 2]]++; |
| 330 | } |
| 331 | } |
| 332 |
| 333 | for (uint32_t i = 0; i < totalSegments; i++) { |
| 334 | if (clipLimit > 0) { |
| 335 | uint32_t clipped = 0; |
| 336 | for (uint32_t j = 0; j < PGPhotoEnhanceHistogramBins; ++j) { |
| 337 | if (hist[i][j] > clipLimit) { |
| 338 | clipped += hist[i][j] - clipLimit; |
| 339 | hist[i][j] = clipLimit; |
| 340 | } |
| 341 | } |
| 342 |
| 343 | uint32_t redistBatch = clipped / PGPhotoEnhanceHistogramBins; |
| 344 | uint32_t residual = clipped - redistBatch * PGPhotoEnhanceHistogramBins; |
| 345 |
| 346 | for (uint32_t j = 0; j < PGPhotoEnhanceHistogramBins; ++j) { |
| 347 | hist[i][j] += redistBatch; |
| 348 | } |
| 349 |
| 350 | for (uint32_t j = 0; j < residual; ++j) { |
| 351 | hist[i][j]++; |
| 352 | } |
| 353 | } |
| 354 | memcpy(cdfs[i], hist[i], PGPhotoEnhanceHistogramBins * sizeof(uint32_t)); |
| 355 |
| 356 | uint32_t hMin = PGPhotoEnhanceHistogramBins - 1; |
| 357 | for (uint32_t j = 0; j < hMin; ++j) { |
| 358 | if (cdfs[j] != 0) { |
| 359 | hMin = j; |
| 360 | } |
| 361 | } |
| 362 |
| 363 | uint32_t cdf = 0; |
| 364 | for (uint32_t j = hMin; j < PGPhotoEnhanceHistogramBins; ++j) { |
| 365 | cdf += cdfs[i][j]; |
| 366 | cdfs[i][j] = (uint8_t) min(255, cdf * scale); |
| 367 | } |
| 368 |
| 369 | cdfsMin[i] = cdfs[i][hMin]; |
| 370 | cdfsMax[i] = cdfs[i][PGPhotoEnhanceHistogramBins - 1]; |
| 371 | } |
| 372 |
| 373 | uint32_t resultSize = 4 * PGPhotoEnhanceHistogramBins * totalSegments; |
| 374 | uint32_t resultBytesPerRow = 4 * PGPhotoEnhanceHistogramBins; |
| 375 |
| 376 | unsigned char *result = (*env)->GetDirectBufferAddress(env, buffer); |
| 377 | for (uint32_t tile = 0; tile < totalSegments; tile++) { |
| 378 | uint32_t yOffset = tile * resultBytesPerRow; |
| 379 | for (uint32_t i = 0; i < PGPhotoEnhanceHistogramBins; i++) { |
| 380 | uint32_t index = i * 4 + yOffset; |
| 381 | result[index] = (uint8_t)cdfs[tile][i]; |
| 382 | result[index + 1] = (uint8_t)cdfsMin[tile]; |
| 383 | result[index + 2] = (uint8_t)cdfsMax[tile]; |
| 384 | result[index + 3] = 255; |
| 385 | } |
| 386 | } |
| 387 |
| 388 | for (uint32_t a = 0; a < totalSegments; a++) { |
| 389 | free(hist[a]); |
| 390 | free(cdfs[a]); |
| 391 | } |
| 392 | free(hist); |
| 393 | free(cdfs); |
| 394 | free(cdfsMax); |
| 395 | free(cdfsMin); |
| 396 | } |
| 397 |
| 398 | JNIEXPORT int Java_org_telegram_messenger_Utilities_pinBitmap(JNIEnv *env, jclass class, jobject bitmap) { |
| 399 | if (bitmap == NULL) { |
| 400 | return 0; |
| 401 | } |
| 402 | unsigned char *pixels; |
| 403 | return AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0 ? 1 : 0; |
| 404 | } |
| 405 |
| 406 | JNIEXPORT void Java_org_telegram_messenger_Utilities_unpinBitmap(JNIEnv *env, jclass class, jobject bitmap) { |
| 407 | if (bitmap == NULL) { |
| 408 | return; |
| 409 | } |
| 410 | AndroidBitmap_unlockPixels(env, bitmap); |
| 411 | } |
| 412 |
| 413 | JNIEXPORT void Java_org_telegram_messenger_Utilities_loadBitmap(JNIEnv *env, jclass class, jstring path, jobject bitmap, int scale, int width, int height, int stride) { |
| 414 |
| 415 | AndroidBitmapInfo info; |
| 416 | int i; |
| 417 |
| 418 | if ((i = AndroidBitmap_getInfo(env, bitmap, &info)) >= 0) { |
| 419 | char *fileName = (*env)->GetStringUTFChars(env, path, NULL); |
| 420 | FILE *infile; |
| 421 |
| 422 | if ((infile = fopen(fileName, "rb"))) { |
| 423 | struct my_error_mgr jerr; |
| 424 | struct jpeg_decompress_struct cinfo; |
| 425 |
| 426 | cinfo.err = jpeg_std_error(&jerr.pub); |
| 427 | jerr.pub.error_exit = my_error_exit; |
| 428 |
| 429 | if (!setjmp(jerr.setjmp_buffer)) { |
| 430 | jpeg_create_decompress(&cinfo); |
| 431 | jpeg_stdio_src(&cinfo, infile); |
| 432 |
| 433 | jpeg_read_header(&cinfo, TRUE); |
| 434 |
| 435 | cinfo.scale_denom = scale; |
| 436 | cinfo.scale_num = 1; |
| 437 |
| 438 | jpeg_start_decompress(&cinfo); |
| 439 | int row_stride = cinfo.output_width * cinfo.output_components; |
| 440 | JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); |
| 441 |
| 442 | unsigned char *pixels; |
| 443 | if ((i = AndroidBitmap_lockPixels(env, bitmap, &pixels)) >= 0) { |
| 444 | int rowCount = min(cinfo.output_height, height); |
| 445 | int colCount = min(cinfo.output_width, width); |
| 446 |
| 447 | while (cinfo.output_scanline < rowCount) { |
| 448 | jpeg_read_scanlines(&cinfo, buffer, 1); |
| 449 |
| 450 | //if (info.format == ANDROID_BITMAP_FORMAT_RGBA_8888) { |
| 451 | if (cinfo.out_color_space == JCS_GRAYSCALE) { |
| 452 | for (i = 0; i < colCount; i++) { |
| 453 | float alpha = buffer[0][i] / 255.0f; |
| 454 | pixels[i * 4] *= alpha; |
| 455 | pixels[i * 4 + 1] *= alpha; |
| 456 | pixels[i * 4 + 2] *= alpha; |
| 457 | pixels[i * 4 + 3] = buffer[0][i]; |
| 458 | } |
| 459 | } else { |
| 460 | int c = 0; |
| 461 | for (i = 0; i < colCount; i++) { |
| 462 | pixels[i * 4] = buffer[0][i * 3]; |
| 463 | pixels[i * 4 + 1] = buffer[0][i * 3 + 1]; |
| 464 | pixels[i * 4 + 2] = buffer[0][i * 3 + 2]; |
| 465 | pixels[i * 4 + 3] = 255; |
| 466 | c += 4; |
| 467 | } |
| 468 | } |
| 469 | //} else if (info.format == ANDROID_BITMAP_FORMAT_RGB_565) { |
| 470 |
| 471 | //} |
| 472 |
| 473 | pixels += stride; |
| 474 | } |
| 475 |
| 476 | AndroidBitmap_unlockPixels(env, bitmap); |
| 477 | } else { |
| 478 | throwException(env, "AndroidBitmap_lockPixels() failed ! error=%d", i); |
| 479 | } |
| 480 |
| 481 | jpeg_finish_decompress(&cinfo); |
| 482 | } else { |
| 483 | throwException(env, "the JPEG code has signaled an error"); |
| 484 | } |
| 485 |
| 486 | jpeg_destroy_decompress(&cinfo); |
| 487 | fclose(infile); |
| 488 | } else { |
| 489 | throwException(env, "can't open %s", fileName); |
| 490 | } |
| 491 |
| 492 | (*env)->ReleaseStringUTFChars(env, path, fileName); |
| 493 | } else { |
| 494 | throwException(env, "AndroidBitmap_getInfo() failed ! error=%d", i); |
| 495 | } |
| 496 | } |
| 497 |
| 498 | JNIEXPORT jboolean Java_org_telegram_messenger_Utilities_loadWebpImage(JNIEnv *env, jclass class, jobject outputBitmap, jobject buffer, jint len, jobject options, jboolean unpin) { |
| 499 | if (!buffer) { |
| 500 | (*env)->ThrowNew(env, jclass_NullPointerException, "Input buffer can not be null"); |
| 501 | return 0; |
| 502 | } |
| 503 |
| 504 | jbyte *inputBuffer = (*env)->GetDirectBufferAddress(env, buffer); |
| 505 |
| 506 | int bitmapWidth = 0; |
| 507 | int bitmapHeight = 0; |
| 508 | if (!WebPGetInfo((uint8_t*)inputBuffer, len, &bitmapWidth, &bitmapHeight)) { |
| 509 | (*env)->ThrowNew(env, jclass_RuntimeException, "Invalid WebP format"); |
| 510 | return 0; |
| 511 | } |
| 512 |
| 513 | if (options && (*env)->GetBooleanField(env, options, jclass_Options_inJustDecodeBounds) == JNI_TRUE) { |
| 514 | (*env)->SetIntField(env, options, jclass_Options_outWidth, bitmapWidth); |
| 515 | (*env)->SetIntField(env, options, jclass_Options_outHeight, bitmapHeight); |
| 516 | return 1; |
| 517 | } |
| 518 |
| 519 | if (!outputBitmap) { |
| 520 | (*env)->ThrowNew(env, jclass_NullPointerException, "output bitmap can not be null"); |
| 521 | return 0; |
| 522 | } |
| 523 |
| 524 | AndroidBitmapInfo bitmapInfo; |
| 525 | if (AndroidBitmap_getInfo(env, outputBitmap, &bitmapInfo) != ANDROID_BITMAP_RESUT_SUCCESS) { |
| 526 | (*env)->ThrowNew(env, jclass_RuntimeException, "Failed to get Bitmap information"); |
| 527 | return 0; |
| 528 | } |
| 529 |
| 530 | void *bitmapPixels = 0; |
| 531 | if (AndroidBitmap_lockPixels(env, outputBitmap, &bitmapPixels) != ANDROID_BITMAP_RESUT_SUCCESS) { |
| 532 | (*env)->ThrowNew(env, jclass_RuntimeException, "Failed to lock Bitmap pixels"); |
| 533 | return 0; |
| 534 | } |
| 535 |
| 536 | if (!WebPDecodeRGBAInto((uint8_t*)inputBuffer, len, (uint8_t*)bitmapPixels, bitmapInfo.height * bitmapInfo.stride, bitmapInfo.stride)) { |
| 537 | AndroidBitmap_unlockPixels(env, outputBitmap); |
| 538 | (*env)->ThrowNew(env, jclass_RuntimeException, "Failed to decode webp image"); |
| 539 | return 0; |
| 540 | } |
| 541 |
| 542 | if (unpin && AndroidBitmap_unlockPixels(env, outputBitmap) != ANDROID_BITMAP_RESUT_SUCCESS) { |
| 543 | (*env)->ThrowNew(env, jclass_RuntimeException, "Failed to unlock Bitmap pixels"); |
| 544 | return 0; |
| 545 | } |
| 546 |
| 547 | return 1; |
| 548 | } |
| 549 |