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