| 5 | #include <libwebp/webp/decode.h> |
| 6 | #include <libwebp/webp/encode.h> |
| 7 | #include "utils.h" |
| 8 | #include "image.h" |
| 9 |
| 10 | jclass jclass_NullPointerException; |
| 11 | jclass jclass_RuntimeException; |
| 12 |
| 13 | jclass jclass_Options; |
| 14 | jfieldID jclass_Options_inJustDecodeBounds; |
| 15 | jfieldID jclass_Options_outHeight; |
| 16 | jfieldID jclass_Options_outWidth; |
| 17 |
| 18 | const uint32_t PGPhotoEnhanceHistogramBins = 256; |
| 19 | const uint32_t PGPhotoEnhanceSegments = 4; |
| 20 |
| 21 | jclass createGlobarRef(JNIEnv *env, jclass class) { |
| 22 | if (class) { |
| 23 | return (*env)->NewGlobalRef(env, class); |
| 24 | } |
| 25 | return 0; |
| 26 | } |
| 27 |
| 28 | jint imageOnJNILoad(JavaVM *vm, void *reserved, JNIEnv *env) { |
| 29 | jclass_NullPointerException = createGlobarRef(env, (*env)->FindClass(env, "java/lang/NullPointerException")); |
| 30 | if (jclass_NullPointerException == 0) { |
| 31 | return -1; |
| 32 | } |
| 33 | jclass_RuntimeException = createGlobarRef(env, (*env)->FindClass(env, "java/lang/RuntimeException")); |
| 34 | if (jclass_RuntimeException == 0) { |
| 35 | return -1; |
| 36 | } |
| 37 |
| 38 | jclass_Options = createGlobarRef(env, (*env)->FindClass(env, "android/graphics/BitmapFactory$Options")); |
| 39 | if (jclass_Options == 0) { |
| 40 | return -1; |
| 41 | } |
| 42 | jclass_Options_inJustDecodeBounds = (*env)->GetFieldID(env, jclass_Options, "inJustDecodeBounds", "Z"); |
| 43 | if (jclass_Options_inJustDecodeBounds == 0) { |
| 44 | return -1; |
| 45 | } |
| 46 | jclass_Options_outHeight = (*env)->GetFieldID(env, jclass_Options, "outHeight", "I"); |
| 47 | if (jclass_Options_outHeight == 0) { |
| 48 | return -1; |
| 49 | } |
| 50 | jclass_Options_outWidth = (*env)->GetFieldID(env, jclass_Options, "outWidth", "I"); |
| 51 | if (jclass_Options_outWidth == 0) { |
| 52 | return -1; |
| 53 | } |
| 54 |
| 55 | return JNI_VERSION_1_6; |
| 56 | } |
| 57 |
| 58 | static inline uint64_t get_colors (const uint8_t *p) { |
| 59 | return p[0] + (p[1] << 16) + ((uint64_t)p[2] << 32); |
| 60 | } |
| 61 |
| 62 | static void fastBlurMore(int imageWidth, int imageHeight, int imageStride, void *pixels, int radius) { |
| 63 | uint8_t *pix = (uint8_t *)pixels; |
| 64 | const int w = imageWidth; |
| 65 | const int h = imageHeight; |
| 66 | const int stride = imageStride; |
| 67 | const int r1 = radius + 1; |
| 68 | const int div = radius * 2 + 1; |
| 69 |
| 70 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || imageStride > imageWidth * 4) { |
| 71 | return; |
| 72 | } |
| 73 |
| 74 | uint64_t *rgb = malloc(imageWidth * imageHeight * sizeof(uint64_t)); |
| 75 | if (rgb == NULL) { |
| 76 | return; |
| 77 | } |
| 78 |
| 79 | int x, y, i; |
| 80 |
| 81 | int yw = 0; |
| 82 | const int we = w - r1; |
| 83 | for (y = 0; y < h; y++) { |
| 84 | uint64_t cur = get_colors (&pix[yw]); |
| 85 | uint64_t rgballsum = -radius * cur; |
| 86 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 87 |
| 88 | for (i = 1; i <= radius; i++) { |
| 89 | uint64_t cur = get_colors (&pix[yw + i * 4]); |
| 90 | rgbsum += cur * (r1 - i); |
| 91 | rgballsum += cur; |
| 92 | } |
| 93 |
| 94 | x = 0; |
| 95 |
| 96 | #define update(start, middle, end) \ |
| 97 | rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \ |
| 98 | rgballsum += get_colors (&pix[yw + (start) * 4]) - 2 * get_colors (&pix[yw + (middle) * 4]) + get_colors (&pix[yw + (end) * 4]); \ |
| 99 | rgbsum += rgballsum; \ |
| 100 | x++; \ |
| 101 |
| 102 | while (x < r1) { |
| 103 | update (0, x, x + r1); |
| 104 | } |
| 105 | while (x < we) { |
| 106 | update (x - r1, x, x + r1); |
| 107 | } |
| 108 | while (x < w) { |
| 109 | update (x - r1, x, w - 1); |
| 110 | } |
| 111 | #undef update |
| 112 |
| 113 | yw += stride; |
| 114 | } |
| 115 |
| 116 | const int he = h - r1; |
| 117 | for (x = 0; x < w; x++) { |
| 118 | uint64_t rgballsum = -radius * rgb[x]; |
| 119 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 120 | for (i = 1; i <= radius; i++) { |
| 121 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 122 | rgballsum += rgb[i * w + x]; |
| 123 | } |
| 124 |
| 125 | y = 0; |
| 126 | int yi = x * 4; |
| 127 |
| 128 | #define update(start, middle, end) \ |
| 129 | int64_t res = rgbsum >> 6; \ |
| 130 | pix[yi] = res; \ |
| 131 | pix[yi + 1] = res >> 16; \ |
| 132 | pix[yi + 2] = res >> 32; \ |
| 133 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 134 | rgbsum += rgballsum; \ |
| 135 | y++; \ |
| 136 | yi += stride; |
| 137 |
| 138 | while (y < r1) { |
| 139 | update (0, y, y + r1); |
| 140 | } |
| 141 | while (y < he) { |
| 142 | update (y - r1, y, y + r1); |
| 143 | } |
| 144 | while (y < h) { |
| 145 | update (y - r1, y, h - 1); |
| 146 | } |
| 147 | #undef update |
| 148 | } |
| 149 | } |
| 150 |
| 151 | static void fastBlur(int imageWidth, int imageHeight, int imageStride, void *pixels, int radius) { |
| 152 | uint8_t *pix = (uint8_t *)pixels; |
| 153 | if (pix == NULL) { |
| 154 | return; |
| 155 | } |
| 156 | const int w = imageWidth; |
| 157 | const int h = imageHeight; |
| 158 | const int stride = imageStride; |
| 159 | const int r1 = radius + 1; |
| 160 | const int div = radius * 2 + 1; |
| 161 | int shift; |
| 162 | if (radius == 1) { |
| 163 | shift = 2; |
| 164 | } else if (radius == 3) { |
| 165 | shift = 4; |
| 166 | } else if (radius == 7) { |
| 167 | shift = 6; |
| 168 | } else if (radius == 15) { |
| 169 | shift = 8; |
| 170 | } else { |
| 171 | return; |
| 172 | } |
| 173 |
| 174 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || imageStride > imageWidth * 4) { |
| 175 | return; |
| 176 | } |
| 177 |
| 178 | uint64_t *rgb = malloc(imageWidth * imageHeight * sizeof(uint64_t)); |
| 179 | if (rgb == NULL) { |
| 180 | return; |
| 181 | } |
| 182 |
| 183 | int x, y, i; |
| 184 |
| 185 | int yw = 0; |
| 186 | const int we = w - r1; |
| 187 | for (y = 0; y < h; y++) { |
| 188 | uint64_t cur = get_colors (&pix[yw]); |
| 189 | uint64_t rgballsum = -radius * cur; |
| 190 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 191 |
| 192 | for (i = 1; i <= radius; i++) { |
| 193 | uint64_t cur = get_colors (&pix[yw + i * 4]); |
| 194 | rgbsum += cur * (r1 - i); |
| 195 | rgballsum += cur; |
| 196 | } |
| 197 |
| 198 | x = 0; |
| 199 |
| 200 | #define update(start, middle, end) \ |
| 201 | rgb[y * w + x] = (rgbsum >> shift) & 0x00FF00FF00FF00FFLL; \ |
| 202 | rgballsum += get_colors (&pix[yw + (start) * 4]) - 2 * get_colors (&pix[yw + (middle) * 4]) + get_colors (&pix[yw + (end) * 4]); \ |
| 203 | rgbsum += rgballsum; \ |
| 204 | x++; \ |
| 205 |
| 206 | while (x < r1) { |
| 207 | update (0, x, x + r1); |
| 208 | } |
| 209 | while (x < we) { |
| 210 | update (x - r1, x, x + r1); |
| 211 | } |
| 212 | while (x < w) { |
| 213 | update (x - r1, x, w - 1); |
| 214 | } |
| 215 |
| 216 | #undef update |
| 217 |
| 218 | yw += stride; |
| 219 | } |
| 220 |
| 221 | const int he = h - r1; |
| 222 | for (x = 0; x < w; x++) { |
| 223 | uint64_t rgballsum = -radius * rgb[x]; |
| 224 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 225 | for (i = 1; i <= radius; i++) { |
| 226 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 227 | rgballsum += rgb[i * w + x]; |
| 228 | } |
| 229 |
| 230 | y = 0; |
| 231 | int yi = x * 4; |
| 232 |
| 233 | #define update(start, middle, end) \ |
| 234 | int64_t res = rgbsum >> shift; \ |
| 235 | pix[yi] = res; \ |
| 236 | pix[yi + 1] = res >> 16; \ |
| 237 | pix[yi + 2] = res >> 32; \ |
| 238 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 239 | rgbsum += rgballsum; \ |
| 240 | y++; \ |
| 241 | yi += stride; |
| 242 |
| 243 | while (y < r1) { |
| 244 | update (0, y, y + r1); |
| 245 | } |
| 246 | while (y < he) { |
| 247 | update (y - r1, y, y + r1); |
| 248 | } |
| 249 | while (y < h) { |
| 250 | update (y - r1, y, h - 1); |
| 251 | } |
| 252 | #undef update |
| 253 | } |
| 254 |
| 255 | free(rgb); |
| 256 | } |
| 257 |
| 258 | JNIEXPORT void Java_org_telegram_messenger_Utilities_blurBitmap(JNIEnv *env, jclass class, jobject bitmap, int radius, int unpin, int width, int height, int stride) { |
| 259 | if (!bitmap) { |
| 260 | return; |
| 261 | } |
| 262 |
| 263 | if (!width || !height || !stride) { |
| 264 | return; |
| 265 | } |
| 266 |
| 267 | void *pixels = 0; |
| 268 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { |
| 269 | return; |
| 270 | } |
| 271 | if (radius <= 3) { |
| 272 | fastBlur(width, height, stride, pixels, radius); |
| 273 | } else { |
| 274 | fastBlurMore(width, height, stride, pixels, radius); |
| 275 | } |
| 276 | if (unpin) { |
| 277 | AndroidBitmap_unlockPixels(env, bitmap); |
| 278 | } |
| 279 | } |
| 280 |
| 281 | JNIEXPORT void Java_org_telegram_messenger_Utilities_calcCDT(JNIEnv *env, jclass class, jobject hsvBuffer, int width, int height, jobject buffer) { |
| 282 | float imageWidth = width; |
| 283 | float imageHeight = height; |
| 284 | float _clipLimit = 1.25f; |
| 285 |
| 286 | uint32_t totalSegments = PGPhotoEnhanceSegments * PGPhotoEnhanceSegments; |
| 287 | uint32_t tileArea = (uint32_t)(floorf(imageWidth / PGPhotoEnhanceSegments) * floorf(imageHeight / PGPhotoEnhanceSegments)); |
| 288 | uint32_t clipLimit = (uint32_t)max(1, _clipLimit * tileArea / (float) PGPhotoEnhanceHistogramBins); |
| 289 | float scale = 255.0f / (float) tileArea; |
| 290 |
| 291 |
| 292 | unsigned char *bytes = (*env)->GetDirectBufferAddress(env, hsvBuffer); |
| 293 |
| 294 | uint32_t **hist = calloc(totalSegments, sizeof(uint32_t *)); |
| 295 | uint32_t **cdfs = calloc(totalSegments, sizeof(uint32_t *)); |
| 296 | uint32_t *cdfsMin = calloc(totalSegments, sizeof(uint32_t)); |
| 297 | uint32_t *cdfsMax = calloc(totalSegments, sizeof(uint32_t)); |
| 298 |
| 299 | for (uint32_t a = 0; a < totalSegments; a++) { |
| 300 | hist[a] = calloc(PGPhotoEnhanceHistogramBins, sizeof(uint32_t)); |
| 301 | cdfs[a] = calloc(PGPhotoEnhanceHistogramBins, sizeof(uint32_t)); |
| 302 | } |
| 303 |
| 304 | float xMul = PGPhotoEnhanceSegments / imageWidth; |
| 305 | float yMul = PGPhotoEnhanceSegments / imageHeight; |
| 306 |
| 307 | for (uint32_t y = 0; y < imageHeight; y++) { |
| 308 | uint32_t yOffset = y * width * 4; |
| 309 | for (uint32_t x = 0; x < imageWidth; x++) { |
| 310 | uint32_t index = x * 4 + yOffset; |
| 311 |
| 312 | uint32_t tx = (uint32_t)(x * xMul); |
| 313 | uint32_t ty = (uint32_t)(y * yMul); |
| 314 | uint32_t t = ty * PGPhotoEnhanceSegments + tx; |
| 315 |
| 316 | hist[t][bytes[index + 2]]++; |
| 317 | } |
| 318 | } |
| 319 |
| 320 | for (uint32_t i = 0; i < totalSegments; i++) { |
| 321 | if (clipLimit > 0) { |
| 322 | uint32_t clipped = 0; |
| 323 | for (uint32_t j = 0; j < PGPhotoEnhanceHistogramBins; ++j) { |
| 324 | if (hist[i][j] > clipLimit) { |
| 325 | clipped += hist[i][j] - clipLimit; |
| 326 | hist[i][j] = clipLimit; |
| 327 | } |
| 328 | } |
| 329 |
| 330 | uint32_t redistBatch = clipped / PGPhotoEnhanceHistogramBins; |
| 331 | uint32_t residual = clipped - redistBatch * PGPhotoEnhanceHistogramBins; |
| 332 |
| 333 | for (uint32_t j = 0; j < PGPhotoEnhanceHistogramBins; ++j) { |
| 334 | hist[i][j] += redistBatch; |
| 335 | } |
| 336 |
| 337 | for (uint32_t j = 0; j < residual; ++j) { |
| 338 | hist[i][j]++; |
| 339 | } |
| 340 | } |
| 341 | memcpy(cdfs[i], hist[i], PGPhotoEnhanceHistogramBins * sizeof(uint32_t)); |
| 342 |
| 343 | uint32_t hMin = PGPhotoEnhanceHistogramBins - 1; |
| 344 | for (uint32_t j = 0; j < hMin; ++j) { |
| 345 | if (cdfs[j] != 0) { |
| 346 | hMin = j; |
| 347 | } |
| 348 | } |
| 349 |
| 350 | uint32_t cdf = 0; |
| 351 | for (uint32_t j = hMin; j < PGPhotoEnhanceHistogramBins; ++j) { |
| 352 | cdf += cdfs[i][j]; |
| 353 | cdfs[i][j] = (uint8_t) min(255, cdf * scale); |
| 354 | } |
| 355 |
| 356 | cdfsMin[i] = cdfs[i][hMin]; |
| 357 | cdfsMax[i] = cdfs[i][PGPhotoEnhanceHistogramBins - 1]; |
| 358 | } |
| 359 |
| 360 | uint32_t resultSize = 4 * PGPhotoEnhanceHistogramBins * totalSegments; |
| 361 | uint32_t resultBytesPerRow = 4 * PGPhotoEnhanceHistogramBins; |
| 362 |
| 363 | unsigned char *result = (*env)->GetDirectBufferAddress(env, buffer); |
| 364 | for (uint32_t tile = 0; tile < totalSegments; tile++) { |
| 365 | uint32_t yOffset = tile * resultBytesPerRow; |
| 366 | for (uint32_t i = 0; i < PGPhotoEnhanceHistogramBins; i++) { |
| 367 | uint32_t index = i * 4 + yOffset; |
| 368 | result[index] = (uint8_t)cdfs[tile][i]; |
| 369 | result[index + 1] = (uint8_t)cdfsMin[tile]; |
| 370 | result[index + 2] = (uint8_t)cdfsMax[tile]; |
| 371 | result[index + 3] = 255; |
| 372 | } |
| 373 | } |
| 374 |
| 375 | for (uint32_t a = 0; a < totalSegments; a++) { |
| 376 | free(hist[a]); |
| 377 | free(cdfs[a]); |
| 378 | } |
| 379 | free(hist); |
| 380 | free(cdfs); |
| 381 | free(cdfsMax); |
| 382 | free(cdfsMin); |
| 383 | } |
| 384 |
| 385 | JNIEXPORT int Java_org_telegram_messenger_Utilities_pinBitmap(JNIEnv *env, jclass class, jobject bitmap) { |
| 386 | if (bitmap == NULL) { |
| 387 | return 0; |
| 388 | } |
| 389 | unsigned char *pixels; |
| 390 | return AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0 ? 1 : 0; |
| 391 | } |
| 392 |
| 393 | JNIEXPORT void Java_org_telegram_messenger_Utilities_unpinBitmap(JNIEnv *env, jclass class, jobject bitmap) { |
| 394 | if (bitmap == NULL) { |
| 395 | return; |
| 396 | } |
| 397 | AndroidBitmap_unlockPixels(env, bitmap); |
| 398 | } |
| 399 |
| 400 | JNIEXPORT jboolean Java_org_telegram_messenger_Utilities_loadWebpImage(JNIEnv *env, jclass class, jobject outputBitmap, jobject buffer, jint len, jobject options, jboolean unpin) { |
| 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 1; |
| 419 | } |
| 420 |
| 421 | if (!outputBitmap) { |
| 422 | (*env)->ThrowNew(env, jclass_NullPointerException, "output bitmap can not be null"); |
| 423 | return 0; |
| 424 | } |
| 425 |
| 426 | AndroidBitmapInfo bitmapInfo; |
| 427 | if (AndroidBitmap_getInfo(env, outputBitmap, &bitmapInfo) != ANDROID_BITMAP_RESUT_SUCCESS) { |
| 428 | (*env)->ThrowNew(env, jclass_RuntimeException, "Failed to get Bitmap information"); |
| 429 | return 0; |
| 430 | } |
| 431 |
| 432 | void *bitmapPixels = 0; |
| 433 | if (AndroidBitmap_lockPixels(env, outputBitmap, &bitmapPixels) != ANDROID_BITMAP_RESUT_SUCCESS) { |
| 434 | (*env)->ThrowNew(env, jclass_RuntimeException, "Failed to lock Bitmap pixels"); |
| 435 | return 0; |
| 436 | } |
| 437 |
| 438 | if (!WebPDecodeRGBAInto((uint8_t*)inputBuffer, len, (uint8_t*)bitmapPixels, bitmapInfo.height * bitmapInfo.stride, bitmapInfo.stride)) { |
| 439 | AndroidBitmap_unlockPixels(env, outputBitmap); |
| 440 | (*env)->ThrowNew(env, jclass_RuntimeException, "Failed to decode webp image"); |
| 441 | return 0; |
| 442 | } |
| 443 |
| 444 | if (unpin && AndroidBitmap_unlockPixels(env, outputBitmap) != ANDROID_BITMAP_RESUT_SUCCESS) { |
| 445 | (*env)->ThrowNew(env, jclass_RuntimeException, "Failed to unlock Bitmap pixels"); |
| 446 | return 0; |
| 447 | } |
| 448 |
| 449 | return 1; |
| 450 | } |
| 451 |