| 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 getColors(const uint8_t *p) { |
| 59 | return p[0] + (p[1] << 16) + ((uint64_t) p[2] << 32); |
| 60 | } |
| 61 |
| 62 | static inline uint64_t getColors565(const uint8_t *p) { |
| 63 | uint16_t *ps = (uint16_t *) p; |
| 64 | return ((((ps[0] & 0xF800) >> 11) * 255) / 31) + (((((ps[0] & 0x07E0) >> 5) * 255) / 63) << 16) + ((uint64_t) (((ps[0] & 0x001F) * 255) / 31) << 32); |
| 65 | } |
| 66 |
| 67 | static void fastBlurMore(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) { |
| 68 | const int32_t r1 = radius + 1; |
| 69 | const int32_t div = radius * 2 + 1; |
| 70 |
| 71 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 4) { |
| 72 | return; |
| 73 | } |
| 74 |
| 75 | uint64_t *rgb = malloc(w * h * sizeof(uint64_t)); |
| 76 | if (rgb == NULL) { |
| 77 | return; |
| 78 | } |
| 79 |
| 80 | int32_t x, y, i; |
| 81 |
| 82 | int32_t yw = 0; |
| 83 | const int32_t we = w - r1; |
| 84 | for (y = 0; y < h; y++) { |
| 85 | uint64_t cur = getColors(&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 | cur = getColors(&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 += getColors(&pix[yw + (start) * 4]) - 2 * getColors(&pix[yw + (middle) * 4]) + getColors(&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 int32_t 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 | int32_t 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(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) { |
| 153 | if (pix == NULL) { |
| 154 | return; |
| 155 | } |
| 156 | const int32_t r1 = radius + 1; |
| 157 | const int32_t div = radius * 2 + 1; |
| 158 | int32_t shift; |
| 159 | if (radius == 1) { |
| 160 | shift = 2; |
| 161 | } else if (radius == 3) { |
| 162 | shift = 4; |
| 163 | } else if (radius == 7) { |
| 164 | shift = 6; |
| 165 | } else if (radius == 15) { |
| 166 | shift = 8; |
| 167 | } else { |
| 168 | return; |
| 169 | } |
| 170 |
| 171 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 4) { |
| 172 | return; |
| 173 | } |
| 174 |
| 175 | uint64_t *rgb = malloc(w * h * sizeof(uint64_t)); |
| 176 | if (rgb == NULL) { |
| 177 | return; |
| 178 | } |
| 179 |
| 180 | int32_t x, y, i; |
| 181 |
| 182 | int32_t yw = 0; |
| 183 | const int32_t we = w - r1; |
| 184 | for (y = 0; y < h; y++) { |
| 185 | uint64_t cur = getColors(&pix[yw]); |
| 186 | uint64_t rgballsum = -radius * cur; |
| 187 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 188 |
| 189 | for (i = 1; i <= radius; i++) { |
| 190 | cur = getColors(&pix[yw + i * 4]); |
| 191 | rgbsum += cur * (r1 - i); |
| 192 | rgballsum += cur; |
| 193 | } |
| 194 |
| 195 | x = 0; |
| 196 |
| 197 | #define update(start, middle, end) \ |
| 198 | rgb[y * w + x] = (rgbsum >> shift) & 0x00FF00FF00FF00FFLL; \ |
| 199 | rgballsum += getColors(&pix[yw + (start) * 4]) - 2 * getColors(&pix[yw + (middle) * 4]) + getColors(&pix[yw + (end) * 4]); \ |
| 200 | rgbsum += rgballsum; \ |
| 201 | x++; \ |
| 202 |
| 203 | while (x < r1) { |
| 204 | update (0, x, x + r1); |
| 205 | } |
| 206 | while (x < we) { |
| 207 | update (x - r1, x, x + r1); |
| 208 | } |
| 209 | while (x < w) { |
| 210 | update (x - r1, x, w - 1); |
| 211 | } |
| 212 |
| 213 | #undef update |
| 214 |
| 215 | yw += stride; |
| 216 | } |
| 217 |
| 218 | const int32_t he = h - r1; |
| 219 | for (x = 0; x < w; x++) { |
| 220 | uint64_t rgballsum = -radius * rgb[x]; |
| 221 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 222 | for (i = 1; i <= radius; i++) { |
| 223 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 224 | rgballsum += rgb[i * w + x]; |
| 225 | } |
| 226 |
| 227 | y = 0; |
| 228 | int32_t yi = x * 4; |
| 229 |
| 230 | #define update(start, middle, end) \ |
| 231 | int64_t res = rgbsum >> shift; \ |
| 232 | pix[yi] = res; \ |
| 233 | pix[yi + 1] = res >> 16; \ |
| 234 | pix[yi + 2] = res >> 32; \ |
| 235 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 236 | rgbsum += rgballsum; \ |
| 237 | y++; \ |
| 238 | yi += stride; |
| 239 |
| 240 | while (y < r1) { |
| 241 | update (0, y, y + r1); |
| 242 | } |
| 243 | while (y < he) { |
| 244 | update (y - r1, y, y + r1); |
| 245 | } |
| 246 | while (y < h) { |
| 247 | update (y - r1, y, h - 1); |
| 248 | } |
| 249 | #undef update |
| 250 | } |
| 251 |
| 252 | free(rgb); |
| 253 | } |
| 254 |
| 255 | static void fastBlurMore565(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) { |
| 256 | const int32_t r1 = radius + 1; |
| 257 | const int32_t div = radius * 2 + 1; |
| 258 |
| 259 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 2) { |
| 260 | return; |
| 261 | } |
| 262 |
| 263 | uint64_t *rgb = malloc(w * h * sizeof(uint64_t)); |
| 264 | if (rgb == NULL) { |
| 265 | return; |
| 266 | } |
| 267 |
| 268 | int32_t x, y, i; |
| 269 |
| 270 | int32_t yw = 0; |
| 271 | const int32_t we = w - r1; |
| 272 | for (y = 0; y < h; y++) { |
| 273 | uint64_t cur = getColors565(&pix[yw]); |
| 274 | uint64_t rgballsum = -radius * cur; |
| 275 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 276 |
| 277 | for (i = 1; i <= radius; i++) { |
| 278 | cur = getColors565(&pix[yw + i * 2]); |
| 279 | rgbsum += cur * (r1 - i); |
| 280 | rgballsum += cur; |
| 281 | } |
| 282 |
| 283 | x = 0; |
| 284 |
| 285 | #define update(start, middle, end) \ |
| 286 | rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \ |
| 287 | rgballsum += getColors565(&pix[yw + (start) * 2]) - 2 * getColors565(&pix[yw + (middle) * 2]) + getColors565(&pix[yw + (end) * 2]); \ |
| 288 | rgbsum += rgballsum; \ |
| 289 | x++; \ |
| 290 |
| 291 | while (x < r1) { |
| 292 | update (0, x, x + r1); |
| 293 | } |
| 294 | while (x < we) { |
| 295 | update (x - r1, x, x + r1); |
| 296 | } |
| 297 | while (x < w) { |
| 298 | update (x - r1, x, w - 1); |
| 299 | } |
| 300 | #undef update |
| 301 |
| 302 | yw += stride; |
| 303 | } |
| 304 |
| 305 | const int32_t he = h - r1; |
| 306 | for (x = 0; x < w; x++) { |
| 307 | uint64_t rgballsum = -radius * rgb[x]; |
| 308 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 309 | for (i = 1; i <= radius; i++) { |
| 310 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 311 | rgballsum += rgb[i * w + x]; |
| 312 | } |
| 313 |
| 314 | y = 0; |
| 315 | int32_t yi = x * 2; |
| 316 |
| 317 | #define update(start, middle, end) \ |
| 318 | int64_t res = rgbsum >> 6; \ |
| 319 | pix[yi] = ((res >> 13) & 0xe0) | ((res >> 35) & 0x1f); \ |
| 320 | pix[yi + 1] = (res & 0xf8) | ((res >> 21) & 0x7); \ |
| 321 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 322 | rgbsum += rgballsum; \ |
| 323 | y++; \ |
| 324 | yi += stride; |
| 325 |
| 326 | while (y < r1) { |
| 327 | update (0, y, y + r1); |
| 328 | } |
| 329 | while (y < he) { |
| 330 | update (y - r1, y, y + r1); |
| 331 | } |
| 332 | while (y < h) { |
| 333 | update (y - r1, y, h - 1); |
| 334 | } |
| 335 | #undef update |
| 336 | } |
| 337 | } |
| 338 |
| 339 | static void fastBlur565(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) { |
| 340 | if (pix == NULL) { |
| 341 | return; |
| 342 | } |
| 343 | const int32_t r1 = radius + 1; |
| 344 | const int32_t div = radius * 2 + 1; |
| 345 | int32_t shift; |
| 346 | if (radius == 1) { |
| 347 | shift = 2; |
| 348 | } else if (radius == 3) { |
| 349 | shift = 4; |
| 350 | } else if (radius == 7) { |
| 351 | shift = 6; |
| 352 | } else if (radius == 15) { |
| 353 | shift = 8; |
| 354 | } else { |
| 355 | return; |
| 356 | } |
| 357 |
| 358 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 2) { |
| 359 | return; |
| 360 | } |
| 361 |
| 362 | uint64_t *rgb = malloc(w * h * sizeof(uint64_t)); |
| 363 | if (rgb == NULL) { |
| 364 | return; |
| 365 | } |
| 366 |
| 367 | int32_t x, y, i; |
| 368 |
| 369 | int32_t yw = 0; |
| 370 | const int32_t we = w - r1; |
| 371 | for (y = 0; y < h; y++) { |
| 372 | uint64_t cur = getColors565(&pix[yw]); |
| 373 | uint64_t rgballsum = -radius * cur; |
| 374 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 375 |
| 376 | for (i = 1; i <= radius; i++) { |
| 377 | cur = getColors565(&pix[yw + i * 2]); |
| 378 | rgbsum += cur * (r1 - i); |
| 379 | rgballsum += cur; |
| 380 | } |
| 381 |
| 382 | x = 0; |
| 383 |
| 384 | #define update(start, middle, end) \ |
| 385 | rgb[y * w + x] = (rgbsum >> shift) & 0x00FF00FF00FF00FFLL; \ |
| 386 | rgballsum += getColors565(&pix[yw + (start) * 2]) - 2 * getColors565(&pix[yw + (middle) * 2]) + getColors565(&pix[yw + (end) * 2]); \ |
| 387 | rgbsum += rgballsum; \ |
| 388 | x++; |
| 389 |
| 390 | while (x < r1) { |
| 391 | update(0, x, x + r1); |
| 392 | } |
| 393 | while (x < we) { |
| 394 | update(x - r1, x, x + r1); |
| 395 | } |
| 396 | while (x < w) { |
| 397 | update(x - r1, x, w - 1); |
| 398 | } |
| 399 |
| 400 | #undef update |
| 401 |
| 402 | yw += stride; |
| 403 | } |
| 404 |
| 405 | const int32_t he = h - r1; |
| 406 | for (x = 0; x < w; x++) { |
| 407 | uint64_t rgballsum = -radius * rgb[x]; |
| 408 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 409 | for (i = 1; i <= radius; i++) { |
| 410 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 411 | rgballsum += rgb[i * w + x]; |
| 412 | } |
| 413 |
| 414 | y = 0; |
| 415 | int32_t yi = x * 2; |
| 416 |
| 417 | #define update(start, middle, end) \ |
| 418 | uint64_t res = rgbsum >> shift; \ |
| 419 | pix[yi] = ((res >> 13) & 0xe0) | ((res >> 35) & 0x1f); \ |
| 420 | pix[yi + 1] = (res & 0xf8) | ((res >> 21) & 0x7); \ |
| 421 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 422 | rgbsum += rgballsum; \ |
| 423 | y++; \ |
| 424 | yi += stride; |
| 425 |
| 426 | while (y < r1) { |
| 427 | update (0, y, y + r1); |
| 428 | } |
| 429 | while (y < he) { |
| 430 | update (y - r1, y, y + r1); |
| 431 | } |
| 432 | while (y < h) { |
| 433 | update (y - r1, y, h - 1); |
| 434 | } |
| 435 | #undef update |
| 436 | } |
| 437 |
| 438 | free(rgb); |
| 439 | } |
| 440 |
| 441 | JNIEXPORT void Java_org_telegram_messenger_Utilities_blurBitmap(JNIEnv *env, jclass class, jobject bitmap, jint radius, jint unpin, jint width, jint height, jint stride) { |
| 442 | if (!bitmap) { |
| 443 | return; |
| 444 | } |
| 445 |
| 446 | if (!width || !height || !stride) { |
| 447 | return; |
| 448 | } |
| 449 |
| 450 | void *pixels = 0; |
| 451 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { |
| 452 | return; |
| 453 | } |
| 454 | if (stride == width * 2) { |
| 455 | if (radius <= 3) { |
| 456 | fastBlur565(width, height, stride, pixels, radius); |
| 457 | } else { |
| 458 | fastBlurMore565(width, height, stride, pixels, radius); |
| 459 | } |
| 460 | } else { |
| 461 | if (radius <= 3) { |
| 462 | fastBlur(width, height, stride, pixels, radius); |
| 463 | } else { |
| 464 | fastBlurMore(width, height, stride, pixels, radius); |
| 465 | } |
| 466 | } |
| 467 | if (unpin) { |
| 468 | AndroidBitmap_unlockPixels(env, bitmap); |
| 469 | } |
| 470 | } |
| 471 |
| 472 | JNIEXPORT void Java_org_telegram_messenger_Utilities_calcCDT(JNIEnv *env, jclass class, jobject hsvBuffer, jint width, jint height, jobject buffer) { |
| 473 | float imageWidth = width; |
| 474 | float imageHeight = height; |
| 475 | float _clipLimit = 1.25f; |
| 476 |
| 477 | uint32_t totalSegments = PGPhotoEnhanceSegments * PGPhotoEnhanceSegments; |
| 478 | uint32_t tileArea = (uint32_t) (floorf(imageWidth / PGPhotoEnhanceSegments) * floorf(imageHeight / PGPhotoEnhanceSegments)); |
| 479 | uint32_t clipLimit = (uint32_t) max(1, _clipLimit * tileArea / (float) PGPhotoEnhanceHistogramBins); |
| 480 | float scale = 255.0f / (float) tileArea; |
| 481 |
| 482 | unsigned char *bytes = (*env)->GetDirectBufferAddress(env, hsvBuffer); |
| 483 |
| 484 | uint32_t **hist = calloc(totalSegments, sizeof(uint32_t *)); |
| 485 | uint32_t **cdfs = calloc(totalSegments, sizeof(uint32_t *)); |
| 486 | uint32_t *cdfsMin = calloc(totalSegments, sizeof(uint32_t)); |
| 487 | uint32_t *cdfsMax = calloc(totalSegments, sizeof(uint32_t)); |
| 488 |
| 489 | for (uint32_t a = 0; a < totalSegments; a++) { |
| 490 | hist[a] = calloc(PGPhotoEnhanceHistogramBins, sizeof(uint32_t)); |
| 491 | cdfs[a] = calloc(PGPhotoEnhanceHistogramBins, sizeof(uint32_t)); |
| 492 | } |
| 493 |
| 494 | float xMul = PGPhotoEnhanceSegments / imageWidth; |
| 495 | float yMul = PGPhotoEnhanceSegments / imageHeight; |
| 496 |
| 497 | for (uint32_t y = 0; y < imageHeight; y++) { |
| 498 | uint32_t yOffset = y * width * 4; |
| 499 | for (uint32_t x = 0; x < imageWidth; x++) { |
| 500 | uint32_t index = x * 4 + yOffset; |
| 501 |
| 502 | uint32_t tx = (uint32_t)(x * xMul); |
| 503 | uint32_t ty = (uint32_t)(y * yMul); |
| 504 | uint32_t t = ty * PGPhotoEnhanceSegments + tx; |
| 505 |
| 506 | hist[t][bytes[index + 2]]++; |
| 507 | } |
| 508 | } |
| 509 |
| 510 | for (uint32_t i = 0; i < totalSegments; i++) { |
| 511 | if (clipLimit > 0) { |
| 512 | uint32_t clipped = 0; |
| 513 | for (uint32_t j = 0; j < PGPhotoEnhanceHistogramBins; ++j) { |
| 514 | if (hist[i][j] > clipLimit) { |
| 515 | clipped += hist[i][j] - clipLimit; |
| 516 | hist[i][j] = clipLimit; |
| 517 | } |
| 518 | } |
| 519 |
| 520 | uint32_t redistBatch = clipped / PGPhotoEnhanceHistogramBins; |
| 521 | uint32_t residual = clipped - redistBatch * PGPhotoEnhanceHistogramBins; |
| 522 |
| 523 | for (uint32_t j = 0; j < PGPhotoEnhanceHistogramBins; ++j) { |
| 524 | hist[i][j] += redistBatch; |
| 525 | } |
| 526 |
| 527 | for (uint32_t j = 0; j < residual; ++j) { |
| 528 | hist[i][j]++; |
| 529 | } |
| 530 | } |
| 531 | memcpy(cdfs[i], hist[i], PGPhotoEnhanceHistogramBins * sizeof(uint32_t)); |
| 532 |
| 533 | uint32_t hMin = PGPhotoEnhanceHistogramBins - 1; |
| 534 | for (uint32_t j = 0; j < hMin; ++j) { |
| 535 | if (cdfs[j] != 0) { |
| 536 | hMin = j; |
| 537 | } |
| 538 | } |
| 539 |
| 540 | uint32_t cdf = 0; |
| 541 | for (uint32_t j = hMin; j < PGPhotoEnhanceHistogramBins; ++j) { |
| 542 | cdf += cdfs[i][j]; |
| 543 | cdfs[i][j] = (uint8_t) min(255, cdf * scale); |
| 544 | } |
| 545 |
| 546 | cdfsMin[i] = cdfs[i][hMin]; |
| 547 | cdfsMax[i] = cdfs[i][PGPhotoEnhanceHistogramBins - 1]; |
| 548 | } |
| 549 |
| 550 | uint32_t resultSize = 4 * PGPhotoEnhanceHistogramBins * totalSegments; |
| 551 | uint32_t resultBytesPerRow = 4 * PGPhotoEnhanceHistogramBins; |
| 552 |
| 553 | unsigned char *result = (*env)->GetDirectBufferAddress(env, buffer); |
| 554 | for (uint32_t tile = 0; tile < totalSegments; tile++) { |
| 555 | uint32_t yOffset = tile * resultBytesPerRow; |
| 556 | for (uint32_t i = 0; i < PGPhotoEnhanceHistogramBins; i++) { |
| 557 | uint32_t index = i * 4 + yOffset; |
| 558 | result[index] = (uint8_t)cdfs[tile][i]; |
| 559 | result[index + 1] = (uint8_t)cdfsMin[tile]; |
| 560 | result[index + 2] = (uint8_t)cdfsMax[tile]; |
| 561 | result[index + 3] = 255; |
| 562 | } |
| 563 | } |
| 564 |
| 565 | for (uint32_t a = 0; a < totalSegments; a++) { |
| 566 | free(hist[a]); |
| 567 | free(cdfs[a]); |
| 568 | } |
| 569 | free(hist); |
| 570 | free(cdfs); |
| 571 | free(cdfsMax); |
| 572 | free(cdfsMin); |
| 573 | } |
| 574 |
| 575 | JNIEXPORT jint Java_org_telegram_messenger_Utilities_pinBitmap(JNIEnv *env, jclass class, jobject bitmap) { |
| 576 | if (bitmap == NULL) { |
| 577 | return 0; |
| 578 | } |
| 579 | unsigned char *pixels; |
| 580 | return AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0 ? 1 : 0; |
| 581 | } |
| 582 |
| 583 | JNIEXPORT void Java_org_telegram_messenger_Utilities_unpinBitmap(JNIEnv *env, jclass class, jobject bitmap) { |
| 584 | if (bitmap == NULL) { |
| 585 | return; |
| 586 | } |
| 587 | AndroidBitmap_unlockPixels(env, bitmap); |
| 588 | } |
| 589 |
| 590 | JNIEXPORT jboolean Java_org_telegram_messenger_Utilities_loadWebpImage(JNIEnv *env, jclass class, jobject outputBitmap, jobject buffer, jint len, jobject options, jboolean unpin) { |
| 591 | if (!buffer) { |
| 592 | (*env)->ThrowNew(env, jclass_NullPointerException, "Input buffer can not be null"); |
| 593 | return 0; |
| 594 | } |
| 595 |
| 596 | jbyte *inputBuffer = (*env)->GetDirectBufferAddress(env, buffer); |
| 597 |
| 598 | int32_t bitmapWidth = 0; |
| 599 | int32_t bitmapHeight = 0; |
| 600 | if (!WebPGetInfo((uint8_t*)inputBuffer, len, &bitmapWidth, &bitmapHeight)) { |
| 601 | (*env)->ThrowNew(env, jclass_RuntimeException, "Invalid WebP format"); |
| 602 | return 0; |
| 603 | } |
| 604 |
| 605 | if (options && (*env)->GetBooleanField(env, options, jclass_Options_inJustDecodeBounds) == JNI_TRUE) { |
| 606 | (*env)->SetIntField(env, options, jclass_Options_outWidth, bitmapWidth); |
| 607 | (*env)->SetIntField(env, options, jclass_Options_outHeight, bitmapHeight); |
| 608 | return 1; |
| 609 | } |
| 610 |
| 611 | if (!outputBitmap) { |
| 612 | (*env)->ThrowNew(env, jclass_NullPointerException, "output bitmap can not be null"); |
| 613 | return 0; |
| 614 | } |
| 615 |
| 616 | AndroidBitmapInfo bitmapInfo; |
| 617 | if (AndroidBitmap_getInfo(env, outputBitmap, &bitmapInfo) != ANDROID_BITMAP_RESUT_SUCCESS) { |
| 618 | (*env)->ThrowNew(env, jclass_RuntimeException, "Failed to get Bitmap information"); |
| 619 | return 0; |
| 620 | } |
| 621 |
| 622 | void *bitmapPixels = 0; |
| 623 | if (AndroidBitmap_lockPixels(env, outputBitmap, &bitmapPixels) != ANDROID_BITMAP_RESUT_SUCCESS) { |
| 624 | (*env)->ThrowNew(env, jclass_RuntimeException, "Failed to lock Bitmap pixels"); |
| 625 | return 0; |
| 626 | } |
| 627 |
| 628 | if (!WebPDecodeRGBAInto((uint8_t*)inputBuffer, len, (uint8_t*)bitmapPixels, bitmapInfo.height * bitmapInfo.stride, bitmapInfo.stride)) { |
| 629 | AndroidBitmap_unlockPixels(env, outputBitmap); |
| 630 | (*env)->ThrowNew(env, jclass_RuntimeException, "Failed to decode webp image"); |
| 631 | return 0; |
| 632 | } |
| 633 |
| 634 | if (unpin && AndroidBitmap_unlockPixels(env, outputBitmap) != ANDROID_BITMAP_RESUT_SUCCESS) { |
| 635 | (*env)->ThrowNew(env, jclass_RuntimeException, "Failed to unlock Bitmap pixels"); |
| 636 | return 0; |
| 637 | } |
| 638 |
| 639 | return 1; |
| 640 | } |
| 641 |