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