| 5 |
| #include <cstring> |
| 6 | #include <cmath> |
| 7 | #include <unistd.h> |
| 8 | #include <android/bitmap.h> |
| 9 | #include <string> |
| 10 | #include <mozjpeg/java/org_libjpegturbo_turbojpeg_TJ.h> |
| 11 | #include <mozjpeg/jpeglib.h> |
| 12 | #include <tgnet/FileLog.h> |
| 13 | #include <vector> |
| 14 | #include <algorithm> |
| 15 | #include "libwebp/webp/decode.h" |
| 16 | #include "libwebp/webp/encode.h" |
| 17 | #include "mozjpeg/turbojpeg.h" |
| 18 | #include "c_utils.h" |
| 19 |
| 20 | extern "C" { |
| 21 | jclass jclass_NullPointerException; |
| 22 | jclass jclass_RuntimeException; |
| 23 |
| 24 | jclass jclass_Options; |
| 25 | jfieldID jclass_Options_inJustDecodeBounds; |
| 26 | jfieldID jclass_Options_outHeight; |
| 27 | jfieldID jclass_Options_outWidth; |
| 28 |
| 29 | jint imageOnJNILoad(JavaVM *vm, JNIEnv *env) { |
| 30 | DEBUG_REF("image.cpp nullpointerexception class"); |
| 31 | jclass_NullPointerException = (jclass) env->NewGlobalRef(env->FindClass("java/lang/NullPointerException")); |
| 32 | if (jclass_NullPointerException == 0) { |
| 33 | return JNI_FALSE; |
| 34 | } |
| 35 | DEBUG_REF("image.cpp runtimeexception class"); |
| 36 | jclass_RuntimeException = (jclass) env->NewGlobalRef(env->FindClass("java/lang/RuntimeException")); |
| 37 | if (jclass_RuntimeException == 0) { |
| 38 | return JNI_FALSE; |
| 39 | } |
| 40 | DEBUG_REF("image.cpp bitmapfactoryoptions class"); |
| 41 | jclass_Options = (jclass) env->NewGlobalRef(env->FindClass("android/graphics/BitmapFactory$Options")); |
| 42 | if (jclass_Options == 0) { |
| 43 | return JNI_FALSE; |
| 44 | } |
| 45 | jclass_Options_inJustDecodeBounds = env->GetFieldID(jclass_Options, "inJustDecodeBounds", "Z"); |
| 46 | if (jclass_Options_inJustDecodeBounds == 0) { |
| 47 | return JNI_FALSE; |
| 48 | } |
| 49 | jclass_Options_outHeight = env->GetFieldID(jclass_Options, "outHeight", "I"); |
| 50 | if (jclass_Options_outHeight == 0) { |
| 51 | return JNI_FALSE; |
| 52 | } |
| 53 | jclass_Options_outWidth = env->GetFieldID(jclass_Options, "outWidth", "I"); |
| 54 | if (jclass_Options_outWidth == 0) { |
| 55 | return JNI_FALSE; |
| 56 | } |
| 57 |
| 58 | return JNI_TRUE; |
| 59 | } |
| 60 |
| 61 | static inline uint64_t getColors(const uint8_t *p) { |
| 62 | return p[0] + (p[1] << 16) + ((uint64_t) p[2] << 32) + ((uint64_t) p[3] << 48); |
| 63 | } |
| 64 |
| 65 | static inline uint64_t getColors565(const uint8_t *p) { |
| 66 | uint16_t *ps = (uint16_t *) p; |
| 67 | return ((((ps[0] & 0xF800) >> 11) * 255) / 31) + (((((ps[0] & 0x07E0) >> 5) * 255) / 63) << 16) + ((uint64_t)(((ps[0] & 0x001F) * 255) / 31) << 32); |
| 68 | } |
| 69 |
| 70 | static void fastBlurMore(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) { |
| 71 | const int32_t r1 = radius + 1; |
| 72 | const int32_t div = radius * 2 + 1; |
| 73 |
| 74 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 4) { |
| 75 | return; |
| 76 | } |
| 77 |
| 78 | uint64_t *rgb = new uint64_t[w * h]; |
| 79 | if (rgb == NULL) { |
| 80 | return; |
| 81 | } |
| 82 |
| 83 | int32_t x, y, i; |
| 84 |
| 85 | int32_t yw = 0; |
| 86 | const int32_t we = w - r1; |
| 87 | for (y = 0; y < h; y++) { |
| 88 | uint64_t cur = getColors(&pix[yw]); |
| 89 | uint64_t rgballsum = -radius * cur; |
| 90 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 91 |
| 92 | for (i = 1; i <= radius; i++) { |
| 93 | cur = getColors(&pix[yw + i * 4]); |
| 94 | rgbsum += cur * (r1 - i); |
| 95 | rgballsum += cur; |
| 96 | } |
| 97 |
| 98 | x = 0; |
| 99 |
| 100 | #define update(start, middle, end) \ |
| 101 | rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \ |
| 102 | rgballsum += getColors(&pix[yw + (start) * 4]) - 2 * getColors(&pix[yw + (middle) * 4]) + getColors(&pix[yw + (end) * 4]); \ |
| 103 | rgbsum += rgballsum; \ |
| 104 | x++; \ |
| 105 |
| 106 | while (x < r1) { |
| 107 | update (0, x, x + r1) |
| 108 | } |
| 109 | while (x < we) { |
| 110 | update (x - r1, x, x + r1) |
| 111 | } |
| 112 | while (x < w) { |
| 113 | update (x - r1, x, w - 1) |
| 114 | } |
| 115 | #undef update |
| 116 |
| 117 | yw += stride; |
| 118 | } |
| 119 |
| 120 | const int32_t he = h - r1; |
| 121 | for (x = 0; x < w; x++) { |
| 122 | uint64_t rgballsum = -radius * rgb[x]; |
| 123 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 124 | for (i = 1; i <= radius; i++) { |
| 125 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 126 | rgballsum += rgb[i * w + x]; |
| 127 | } |
| 128 |
| 129 | y = 0; |
| 130 | int32_t yi = x * 4; |
| 131 |
| 132 | #define update(start, middle, end) \ |
| 133 | int64_t res = rgbsum >> 6; \ |
| 134 | pix[yi] = res; \ |
| 135 | pix[yi + 1] = res >> 16; \ |
| 136 | pix[yi + 2] = res >> 32; \ |
| 137 | pix[yi + 3] = res >> 48; \ |
| 138 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 139 | rgbsum += rgballsum; \ |
| 140 | y++; \ |
| 141 | yi += stride; |
| 142 |
| 143 | while (y < r1) { |
| 144 | update (0, y, y + r1) |
| 145 | } |
| 146 | while (y < he) { |
| 147 | update (y - r1, y, y + r1) |
| 148 | } |
| 149 | while (y < h) { |
| 150 | update (y - r1, y, h - 1) |
| 151 | } |
| 152 | #undef update |
| 153 | } |
| 154 |
| 155 | delete[] rgb; |
| 156 | } |
| 157 |
| 158 | static void fastBlur(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) { |
| 159 | if (pix == nullptr) { |
| 160 | return; |
| 161 | } |
| 162 | const int32_t r1 = radius + 1; |
| 163 | const int32_t div = radius * 2 + 1; |
| 164 | int32_t shift; |
| 165 | if (radius == 1) { |
| 166 | shift = 2; |
| 167 | } else if (radius == 3) { |
| 168 | shift = 4; |
| 169 | } else if (radius == 7) { |
| 170 | shift = 6; |
| 171 | } else if (radius == 15) { |
| 172 | shift = 8; |
| 173 | } else { |
| 174 | return; |
| 175 | } |
| 176 |
| 177 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 4) { |
| 178 | return; |
| 179 | } |
| 180 |
| 181 | uint64_t *rgb = new uint64_t[w * h]; |
| 182 | if (rgb == nullptr) { |
| 183 | return; |
| 184 | } |
| 185 |
| 186 | int32_t x, y, i; |
| 187 |
| 188 | int32_t yw = 0; |
| 189 | const int32_t we = w - r1; |
| 190 | for (y = 0; y < h; y++) { |
| 191 | uint64_t cur = getColors(&pix[yw]); |
| 192 | uint64_t rgballsum = -radius * cur; |
| 193 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 194 |
| 195 | for (i = 1; i <= radius; i++) { |
| 196 | cur = getColors(&pix[yw + i * 4]); |
| 197 | rgbsum += cur * (r1 - i); |
| 198 | rgballsum += cur; |
| 199 | } |
| 200 |
| 201 | x = 0; |
| 202 |
| 203 | #define update(start, middle, end) \ |
| 204 | rgb[y * w + x] = (rgbsum >> shift) & 0x00FF00FF00FF00FFLL; \ |
| 205 | rgballsum += getColors(&pix[yw + (start) * 4]) - 2 * getColors(&pix[yw + (middle) * 4]) + getColors(&pix[yw + (end) * 4]); \ |
| 206 | rgbsum += rgballsum; \ |
| 207 | x++; \ |
| 208 |
| 209 | while (x < r1) { |
| 210 | update (0, x, x + r1) |
| 211 | } |
| 212 | while (x < we) { |
| 213 | update (x - r1, x, x + r1) |
| 214 | } |
| 215 | while (x < w) { |
| 216 | update (x - r1, x, w - 1) |
| 217 | } |
| 218 |
| 219 | #undef update |
| 220 |
| 221 | yw += stride; |
| 222 | } |
| 223 |
| 224 | const int32_t he = h - r1; |
| 225 | for (x = 0; x < w; x++) { |
| 226 | uint64_t rgballsum = -radius * rgb[x]; |
| 227 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 228 | for (i = 1; i <= radius; i++) { |
| 229 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 230 | rgballsum += rgb[i * w + x]; |
| 231 | } |
| 232 |
| 233 | y = 0; |
| 234 | int32_t yi = x * 4; |
| 235 |
| 236 | #define update(start, middle, end) \ |
| 237 | int64_t res = rgbsum >> shift; \ |
| 238 | pix[yi] = res; \ |
| 239 | pix[yi + 1] = res >> 16; \ |
| 240 | pix[yi + 2] = res >> 32; \ |
| 241 | pix[yi + 3] = res >> 48; \ |
| 242 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 243 | rgbsum += rgballsum; \ |
| 244 | y++; \ |
| 245 | yi += stride; |
| 246 |
| 247 | while (y < r1) { |
| 248 | update (0, y, y + r1) |
| 249 | } |
| 250 | while (y < he) { |
| 251 | update (y - r1, y, y + r1) |
| 252 | } |
| 253 | while (y < h) { |
| 254 | update (y - r1, y, h - 1) |
| 255 | } |
| 256 | #undef update |
| 257 | } |
| 258 |
| 259 | delete[] rgb; |
| 260 | } |
| 261 |
| 262 | static void fastBlurMore565(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) { |
| 263 | const int32_t r1 = radius + 1; |
| 264 | const int32_t div = radius * 2 + 1; |
| 265 |
| 266 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 2) { |
| 267 | return; |
| 268 | } |
| 269 |
| 270 | uint64_t *rgb = new uint64_t[w * h]; |
| 271 | if (rgb == NULL) { |
| 272 | return; |
| 273 | } |
| 274 |
| 275 | int32_t x, y, i; |
| 276 |
| 277 | int32_t yw = 0; |
| 278 | const int32_t we = w - r1; |
| 279 | for (y = 0; y < h; y++) { |
| 280 | uint64_t cur = getColors565(&pix[yw]); |
| 281 | uint64_t rgballsum = -radius * cur; |
| 282 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 283 |
| 284 | for (i = 1; i <= radius; i++) { |
| 285 | cur = getColors565(&pix[yw + i * 2]); |
| 286 | rgbsum += cur * (r1 - i); |
| 287 | rgballsum += cur; |
| 288 | } |
| 289 |
| 290 | x = 0; |
| 291 |
| 292 | #define update(start, middle, end) \ |
| 293 | rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \ |
| 294 | rgballsum += getColors565(&pix[yw + (start) * 2]) - 2 * getColors565(&pix[yw + (middle) * 2]) + getColors565(&pix[yw + (end) * 2]); \ |
| 295 | rgbsum += rgballsum; \ |
| 296 | x++; \ |
| 297 |
| 298 | while (x < r1) { |
| 299 | update (0, x, x + r1) |
| 300 | } |
| 301 | while (x < we) { |
| 302 | update (x - r1, x, x + r1) |
| 303 | } |
| 304 | while (x < w) { |
| 305 | update (x - r1, x, w - 1) |
| 306 | } |
| 307 | #undef update |
| 308 |
| 309 | yw += stride; |
| 310 | } |
| 311 |
| 312 | const int32_t he = h - r1; |
| 313 | for (x = 0; x < w; x++) { |
| 314 | uint64_t rgballsum = -radius * rgb[x]; |
| 315 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 316 | for (i = 1; i <= radius; i++) { |
| 317 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 318 | rgballsum += rgb[i * w + x]; |
| 319 | } |
| 320 |
| 321 | y = 0; |
| 322 | int32_t yi = x * 2; |
| 323 |
| 324 | #define update(start, middle, end) \ |
| 325 | int64_t res = rgbsum >> 6; \ |
| 326 | pix[yi] = ((res >> 13) & 0xe0) | ((res >> 35) & 0x1f); \ |
| 327 | pix[yi + 1] = (res & 0xf8) | ((res >> 21) & 0x7); \ |
| 328 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 329 | rgbsum += rgballsum; \ |
| 330 | y++; \ |
| 331 | yi += stride; |
| 332 |
| 333 | while (y < r1) { |
| 334 | update (0, y, y + r1) |
| 335 | } |
| 336 | while (y < he) { |
| 337 | update (y - r1, y, y + r1) |
| 338 | } |
| 339 | while (y < h) { |
| 340 | update (y - r1, y, h - 1) |
| 341 | } |
| 342 | #undef update |
| 343 | } |
| 344 |
| 345 | delete[] rgb; |
| 346 | } |
| 347 |
| 348 | static void fastBlur565(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) { |
| 349 | if (pix == NULL) { |
| 350 | return; |
| 351 | } |
| 352 | const int32_t r1 = radius + 1; |
| 353 | const int32_t div = radius * 2 + 1; |
| 354 | int32_t shift; |
| 355 | if (radius == 1) { |
| 356 | shift = 2; |
| 357 | } else if (radius == 3) { |
| 358 | shift = 4; |
| 359 | } else if (radius == 7) { |
| 360 | shift = 6; |
| 361 | } else if (radius == 15) { |
| 362 | shift = 8; |
| 363 | } else { |
| 364 | return; |
| 365 | } |
| 366 |
| 367 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 2) { |
| 368 | return; |
| 369 | } |
| 370 |
| 371 | uint64_t *rgb = new uint64_t[w * h]; |
| 372 | if (rgb == NULL) { |
| 373 | return; |
| 374 | } |
| 375 |
| 376 | int32_t x, y, i; |
| 377 |
| 378 | int32_t yw = 0; |
| 379 | const int32_t we = w - r1; |
| 380 | for (y = 0; y < h; y++) { |
| 381 | uint64_t cur = getColors565(&pix[yw]); |
| 382 | uint64_t rgballsum = -radius * cur; |
| 383 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 384 |
| 385 | for (i = 1; i <= radius; i++) { |
| 386 | cur = getColors565(&pix[yw + i * 2]); |
| 387 | rgbsum += cur * (r1 - i); |
| 388 | rgballsum += cur; |
| 389 | } |
| 390 |
| 391 | x = 0; |
| 392 |
| 393 | #define update(start, middle, end) \ |
| 394 | rgb[y * w + x] = (rgbsum >> shift) & 0x00FF00FF00FF00FFLL; \ |
| 395 | rgballsum += getColors565(&pix[yw + (start) * 2]) - 2 * getColors565(&pix[yw + (middle) * 2]) + getColors565(&pix[yw + (end) * 2]); \ |
| 396 | rgbsum += rgballsum; \ |
| 397 | x++; |
| 398 |
| 399 | while (x < r1) { |
| 400 | update(0, x, x + r1) |
| 401 | } |
| 402 | while (x < we) { |
| 403 | update(x - r1, x, x + r1) |
| 404 | } |
| 405 | while (x < w) { |
| 406 | update(x - r1, x, w - 1) |
| 407 | } |
| 408 |
| 409 | #undef update |
| 410 |
| 411 | yw += stride; |
| 412 | } |
| 413 |
| 414 | const int32_t he = h - r1; |
| 415 | for (x = 0; x < w; x++) { |
| 416 | uint64_t rgballsum = -radius * rgb[x]; |
| 417 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 418 | for (i = 1; i <= radius; i++) { |
| 419 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 420 | rgballsum += rgb[i * w + x]; |
| 421 | } |
| 422 |
| 423 | y = 0; |
| 424 | int32_t yi = x * 2; |
| 425 |
| 426 | #define update(start, middle, end) \ |
| 427 | uint64_t res = rgbsum >> shift; \ |
| 428 | pix[yi] = ((res >> 13) & 0xe0) | ((res >> 35) & 0x1f); \ |
| 429 | pix[yi + 1] = (res & 0xf8) | ((res >> 21) & 0x7); \ |
| 430 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 431 | rgbsum += rgballsum; \ |
| 432 | y++; \ |
| 433 | yi += stride; |
| 434 |
| 435 | while (y < r1) { |
| 436 | update (0, y, y + r1) |
| 437 | } |
| 438 | while (y < he) { |
| 439 | update (y - r1, y, y + r1) |
| 440 | } |
| 441 | while (y < h) { |
| 442 | update (y - r1, y, h - 1) |
| 443 | } |
| 444 | #undef update |
| 445 | } |
| 446 |
| 447 | delete[] rgb; |
| 448 | } |
| 449 |
| 450 | JNIEXPORT int Java_org_telegram_messenger_Utilities_needInvert(JNIEnv *env, jclass clazz, jobject bitmap, jint unpin, jint width, jint height, jint stride) { |
| 451 | if (!bitmap) { |
| 452 | return 0; |
| 453 | } |
| 454 |
| 455 | if (!width || !height || !stride || stride != width * 4 || width * height > 150 * 150) { |
| 456 | return 0; |
| 457 | } |
| 458 |
| 459 | void *pixels = nullptr; |
| 460 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { |
| 461 | return 0; |
| 462 | } |
| 463 | if (pixels == nullptr) { |
| 464 | return 0; |
| 465 | } |
| 466 | uint8_t *pix = (uint8_t *) pixels; |
| 467 |
| 468 | int32_t hasAlpha = 0; |
| 469 | float matching = 0; |
| 470 | float total = 0; |
| 471 | for (int32_t y = 0; y < height; y++) { |
| 472 | for (int32_t x = 0; x < width; x++) { |
| 473 | int32_t index = y * stride + x * 4; |
| 474 | uint8_t a = pix[index + 3]; |
| 475 | float alpha = a / 255.0f; |
| 476 |
| 477 | uint8_t r = (uint8_t)(pix[index] * alpha); |
| 478 | uint8_t g = (uint8_t)(pix[index + 1] * alpha); |
| 479 | uint8_t b = (uint8_t)(pix[index + 2] * alpha); |
| 480 |
| 481 | uint8_t cmax = (r > g) ? r : g; |
| 482 | if (b > cmax) { |
| 483 | cmax = b; |
| 484 | } |
| 485 | uint8_t cmin = (r < g) ? r : g; |
| 486 | if (b < cmin) { |
| 487 | cmin = b; |
| 488 | } |
| 489 |
| 490 | float saturation; |
| 491 | float brightness = ((float) cmax) / 255.0f; |
| 492 | if (cmax != 0) { |
| 493 | saturation = ((float) (cmax - cmin)) / ((float) cmax); |
| 494 | } else { |
| 495 | saturation = 0; |
| 496 | } |
| 497 |
| 498 | if (alpha < 1.0) { |
| 499 | hasAlpha = 1; |
| 500 | } |
| 501 |
| 502 | if (alpha > 0.0) { |
| 503 | total += 1; |
| 504 | if (saturation < 0.1f && brightness < 0.25f) { |
| 505 | matching += 1; |
| 506 | } |
| 507 | } |
| 508 | } |
| 509 | } |
| 510 | if (unpin) { |
| 511 | AndroidBitmap_unlockPixels(env, bitmap); |
| 512 | } |
| 513 | return hasAlpha && matching / total > 0.85; |
| 514 | } |
| 515 |
| 516 | JNIEXPORT void Java_org_telegram_messenger_Utilities_blurBitmap(JNIEnv *env, jclass clazz, jobject bitmap, jint radius, jint unpin, jint width, jint height, jint stride) { |
| 517 | if (!bitmap) { |
| 518 | return; |
| 519 | } |
| 520 |
| 521 | if (!width || !height || !stride) { |
| 522 | return; |
| 523 | } |
| 524 |
| 525 | void *pixels = nullptr; |
| 526 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { |
| 527 | return; |
| 528 | } |
| 529 | if (stride == width * 2) { |
| 530 | if (radius <= 3) { |
| 531 | fastBlur565(width, height, stride, (uint8_t *) pixels, radius); |
| 532 | } else { |
| 533 | fastBlurMore565(width, height, stride, (uint8_t *) pixels, radius); |
| 534 | } |
| 535 | } else { |
| 536 | if (radius <= 3) { |
| 537 | fastBlur(width, height, stride, (uint8_t *) pixels, radius); |
| 538 | } else { |
| 539 | fastBlurMore(width, height, stride, (uint8_t *) pixels, radius); |
| 540 | } |
| 541 | } |
| 542 | if (unpin) { |
| 543 | AndroidBitmap_unlockPixels(env, bitmap); |
| 544 | } |
| 545 | } |
| 546 |
| 547 | const uint32_t PGPhotoEnhanceHistogramBins = 256; |
| 548 | const uint32_t PGPhotoEnhanceSegments = 4; |
| 549 |
| 550 | JNIEXPORT void Java_org_telegram_messenger_Utilities_calcCDT(JNIEnv *env, jclass clazz, jobject hsvBuffer, jint width, jint height, jobject buffer, jobject calcBuffer) { |
| 551 | float imageWidth = width; |
| 552 | float imageHeight = height; |
| 553 | float _clipLimit = 1.25f; |
| 554 |
| 555 | uint32_t totalSegments = PGPhotoEnhanceSegments * PGPhotoEnhanceSegments; |
| 556 | uint32_t tileArea = (uint32_t) (floorf(imageWidth / PGPhotoEnhanceSegments) * floorf(imageHeight / PGPhotoEnhanceSegments)); |
| 557 | uint32_t clipLimit = (uint32_t) MAX(1, _clipLimit * tileArea / (float) PGPhotoEnhanceHistogramBins); |
| 558 | float scale = 255.0f / (float) tileArea; |
| 559 |
| 560 | unsigned char *bytes = (unsigned char *) env->GetDirectBufferAddress(hsvBuffer); |
| 561 | uint32_t *calcBytes = (uint32_t *) env->GetDirectBufferAddress(calcBuffer); |
| 562 | unsigned char *result = (unsigned char *) env->GetDirectBufferAddress(buffer); |
| 563 |
| 564 | uint32_t *cdfsMin = calcBytes; |
| 565 | calcBytes += totalSegments; |
| 566 | uint32_t *cdfsMax = calcBytes; |
| 567 | calcBytes += totalSegments; |
| 568 | uint32_t *cdfs = calcBytes; |
| 569 | calcBytes += totalSegments * PGPhotoEnhanceHistogramBins; |
| 570 | uint32_t *hist = calcBytes; |
| 571 | memset(hist, 0, sizeof(uint32_t) * totalSegments * PGPhotoEnhanceHistogramBins); |
| 572 |
| 573 | float xMul = PGPhotoEnhanceSegments / imageWidth; |
| 574 | float yMul = PGPhotoEnhanceSegments / imageHeight; |
| 575 |
| 576 | uint32_t i, j; |
| 577 |
| 578 | for (i = 0; i < imageHeight; i++) { |
| 579 | uint32_t yOffset = i * width * 4; |
| 580 | for (j = 0; j < imageWidth; j++) { |
| 581 | uint32_t index = j * 4 + yOffset; |
| 582 |
| 583 | uint32_t tx = (uint32_t)(j * xMul); |
| 584 | uint32_t ty = (uint32_t)(i * yMul); |
| 585 | uint32_t t = ty * PGPhotoEnhanceSegments + tx; |
| 586 |
| 587 | hist[t * PGPhotoEnhanceHistogramBins + bytes[index + 2]]++; |
| 588 | } |
| 589 | } |
| 590 |
| 591 | for (i = 0; i < totalSegments; i++) { |
| 592 | if (clipLimit > 0) { |
| 593 | uint32_t clipped = 0; |
| 594 | for (j = 0; j < PGPhotoEnhanceHistogramBins; j++) { |
| 595 | if (hist[i * PGPhotoEnhanceHistogramBins + j] > clipLimit) { |
| 596 | clipped += hist[i * PGPhotoEnhanceHistogramBins + j] - clipLimit; |
| 597 | hist[i * PGPhotoEnhanceHistogramBins + j] = clipLimit; |
| 598 | } |
| 599 | } |
| 600 |
| 601 | uint32_t redistBatch = clipped / PGPhotoEnhanceHistogramBins; |
| 602 | uint32_t residual = clipped - redistBatch * PGPhotoEnhanceHistogramBins; |
| 603 |
| 604 | for (j = 0; j < PGPhotoEnhanceHistogramBins; j++) { |
| 605 | hist[i * PGPhotoEnhanceHistogramBins + j] += redistBatch; |
| 606 | if (j < residual) { |
| 607 | hist[i * PGPhotoEnhanceHistogramBins + j]++; |
| 608 | } |
| 609 | } |
| 610 | } |
| 611 | memcpy(cdfs + i * PGPhotoEnhanceHistogramBins, hist + i * PGPhotoEnhanceHistogramBins, PGPhotoEnhanceHistogramBins * sizeof(uint32_t)); |
| 612 |
| 613 | uint32_t hMin = PGPhotoEnhanceHistogramBins - 1; |
| 614 | for (j = 0; j < hMin; ++j) { |
| 615 | if (cdfs[i * PGPhotoEnhanceHistogramBins + j] != 0) { |
| 616 | hMin = j; |
| 617 | } |
| 618 | } |
| 619 |
| 620 | uint32_t cdf = 0; |
| 621 | for (j = hMin; j < PGPhotoEnhanceHistogramBins; j++) { |
| 622 | cdf += cdfs[i * PGPhotoEnhanceHistogramBins + j]; |
| 623 | cdfs[i * PGPhotoEnhanceHistogramBins + j] = (uint8_t) MIN(255, cdf * scale); |
| 624 | } |
| 625 |
| 626 | cdfsMin[i] = cdfs[i * PGPhotoEnhanceHistogramBins + hMin]; |
| 627 | cdfsMax[i] = cdfs[i * PGPhotoEnhanceHistogramBins + PGPhotoEnhanceHistogramBins - 1]; |
| 628 | } |
| 629 |
| 630 | for (j = 0; j < totalSegments; j++) { |
| 631 | uint32_t yOffset = j * PGPhotoEnhanceHistogramBins * 4; |
| 632 | for (i = 0; i < PGPhotoEnhanceHistogramBins; i++) { |
| 633 | uint32_t index = i * 4 + yOffset; |
| 634 | result[index] = (uint8_t) cdfs[j * PGPhotoEnhanceHistogramBins + i]; |
| 635 | result[index + 1] = (uint8_t) cdfsMin[j]; |
| 636 | result[index + 2] = (uint8_t) cdfsMax[j]; |
| 637 | result[index + 3] = 255; |
| 638 | } |
| 639 | } |
| 640 | } |
| 641 |
| 642 | JNIEXPORT jint Java_org_telegram_messenger_Utilities_pinBitmap(JNIEnv *env, jclass clazz, jobject bitmap) { |
| 643 | if (bitmap == nullptr) { |
| 644 | return 0; |
| 645 | } |
| 646 | void *pixels; |
| 647 | return AndroidBitmap_lockPixels(env, bitmap, &pixels) >= 0 ? 1 : 0; |
| 648 | } |
| 649 |
| 650 | JNIEXPORT void Java_org_telegram_messenger_Utilities_unpinBitmap(JNIEnv *env, jclass clazz, jobject bitmap) { |
| 651 | if (bitmap == nullptr) { |
| 652 | return; |
| 653 | } |
| 654 | AndroidBitmap_unlockPixels(env, bitmap); |
| 655 | } |
| 656 |
| 657 | JNIEXPORT jboolean Java_org_telegram_messenger_Utilities_loadWebpImage(JNIEnv *env, jclass clazz, jobject outputBitmap, jobject buffer, jint len, jobject options, jboolean unpin) { |
| 658 | if (!buffer) { |
| 659 | env->ThrowNew(jclass_NullPointerException, "Input buffer can not be null"); |
| 660 | return 0; |
| 661 | } |
| 662 |
| 663 | jbyte *inputBuffer = (jbyte *) env->GetDirectBufferAddress(buffer); |
| 664 |
| 665 | int32_t bitmapWidth = 0; |
| 666 | int32_t bitmapHeight = 0; |
| 667 | if (!WebPGetInfo((uint8_t *)inputBuffer, len, &bitmapWidth, &bitmapHeight)) { |
| 668 | env->ThrowNew(jclass_RuntimeException, "Invalid WebP format"); |
| 669 | return 0; |
| 670 | } |
| 671 |
| 672 | if (options && env->GetBooleanField(options, jclass_Options_inJustDecodeBounds) == JNI_TRUE) { |
| 673 | env->SetIntField(options, jclass_Options_outWidth, bitmapWidth); |
| 674 | env->SetIntField(options, jclass_Options_outHeight, bitmapHeight); |
| 675 | return 1; |
| 676 | } |
| 677 |
| 678 | if (!outputBitmap) { |
| 679 | env->ThrowNew(jclass_NullPointerException, "output bitmap can not be null"); |
| 680 | return 0; |
| 681 | } |
| 682 |
| 683 | AndroidBitmapInfo bitmapInfo; |
| 684 | if (AndroidBitmap_getInfo(env, outputBitmap, &bitmapInfo) != ANDROID_BITMAP_RESUT_SUCCESS) { |
| 685 | env->ThrowNew(jclass_RuntimeException, "Failed to get Bitmap information"); |
| 686 | return 0; |
| 687 | } |
| 688 |
| 689 | void *bitmapPixels = nullptr; |
| 690 | if (AndroidBitmap_lockPixels(env, outputBitmap, &bitmapPixels) != ANDROID_BITMAP_RESUT_SUCCESS) { |
| 691 | env->ThrowNew(jclass_RuntimeException, "Failed to lock Bitmap pixels"); |
| 692 | return 0; |
| 693 | } |
| 694 |
| 695 | if (!WebPDecodeRGBAInto((uint8_t *) inputBuffer, len, (uint8_t *) bitmapPixels, bitmapInfo.height * bitmapInfo.stride, bitmapInfo.stride)) { |
| 696 | AndroidBitmap_unlockPixels(env, outputBitmap); |
| 697 | env->ThrowNew(jclass_RuntimeException, "Failed to decode webp image"); |
| 698 | return 0; |
| 699 | } |
| 700 |
| 701 | if (unpin && AndroidBitmap_unlockPixels(env, outputBitmap) != ANDROID_BITMAP_RESUT_SUCCESS) { |
| 702 | env->ThrowNew(jclass_RuntimeException, "Failed to unlock Bitmap pixels"); |
| 703 | return 0; |
| 704 | } |
| 705 |
| 706 | return 1; |
| 707 | } |
| 708 |
| 709 | #define SQUARE(i) ((i)*(i)) |
| 710 |
| 711 | inline static void zeroClearInt(int *p, size_t count) { |
| 712 | memset(p, 0, sizeof(int) * count); |
| 713 | } |
| 714 |
| 715 | JNIEXPORT void Java_org_telegram_messenger_Utilities_stackBlurBitmap(JNIEnv *env, jclass clazz, jobject bitmap, jint radius) { |
| 716 | if (radius < 1) { |
| 717 | return; |
| 718 | } |
| 719 |
| 720 | AndroidBitmapInfo info; |
| 721 | if (AndroidBitmap_getInfo(env, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 722 | return; |
| 723 | } |
| 724 | if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { |
| 725 | return; |
| 726 | } |
| 727 |
| 728 | int w = info.width; |
| 729 | int h = info.height; |
| 730 | int stride = info.stride; |
| 731 |
| 732 | unsigned char *pixels = nullptr; |
| 733 | AndroidBitmap_lockPixels(env, bitmap, (void **) &pixels); |
| 734 | if (!pixels) { |
| 735 | return; |
| 736 | } |
| 737 | // Constants |
| 738 | //const int radius = (int)inradius; // Transform unsigned into signed for further operations |
| 739 | const int wm = w - 1; |
| 740 | const int hm = h - 1; |
| 741 | const int wh = w * h; |
| 742 | const int div = radius + radius + 1; |
| 743 | const int r1 = radius + 1; |
| 744 | const int divsum = SQUARE((div + 1) >> 1); |
| 745 |
| 746 | // Small buffers |
| 747 | int stack[div * 4]; |
| 748 | zeroClearInt(stack, div * 4); |
| 749 |
| 750 | int vmin[MAX(w, h)]; |
| 751 | zeroClearInt(vmin, MAX(w, h)); |
| 752 |
| 753 | // Large buffers |
| 754 | int *r = new int[wh]; |
| 755 | int *g = new int[wh]; |
| 756 | int *b = new int[wh]; |
| 757 | int *a = new int[wh]; |
| 758 | zeroClearInt(r, wh); |
| 759 | zeroClearInt(g, wh); |
| 760 | zeroClearInt(b, wh); |
| 761 | zeroClearInt(a, wh); |
| 762 |
| 763 | const size_t dvcount = 256 * divsum; |
| 764 | int *dv = new int[dvcount]; |
| 765 | int i; |
| 766 | for (i = 0; (size_t) i < dvcount; i++) { |
| 767 | dv[i] = (i / divsum); |
| 768 | } |
| 769 |
| 770 | // Variables |
| 771 | int x, y; |
| 772 | int *sir; |
| 773 | int routsum, goutsum, boutsum, aoutsum; |
| 774 | int rinsum, ginsum, binsum, ainsum; |
| 775 | int rsum, gsum, bsum, asum, p, yp; |
| 776 | int stackpointer; |
| 777 | int stackstart; |
| 778 | int rbs; |
| 779 |
| 780 | int yw = 0, yi = 0; |
| 781 | for (y = 0; y < h; y++) { |
| 782 | ainsum = aoutsum = asum = rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; |
| 783 |
| 784 | for (i = -radius; i <= radius; i++) { |
| 785 | sir = &stack[(i + radius) * 4]; |
| 786 | int offset = (y * stride + (MIN(wm, MAX(i, 0))) * 4); |
| 787 | sir[0] = pixels[offset]; |
| 788 | sir[1] = pixels[offset + 1]; |
| 789 | sir[2] = pixels[offset + 2]; |
| 790 | sir[3] = pixels[offset + 3]; |
| 791 |
| 792 | rbs = r1 - abs(i); |
| 793 | rsum += sir[0] * rbs; |
| 794 | gsum += sir[1] * rbs; |
| 795 | bsum += sir[2] * rbs; |
| 796 | asum += sir[3] * rbs; |
| 797 | if (i > 0) { |
| 798 | rinsum += sir[0]; |
| 799 | ginsum += sir[1]; |
| 800 | binsum += sir[2]; |
| 801 | ainsum += sir[3]; |
| 802 | } else { |
| 803 | routsum += sir[0]; |
| 804 | goutsum += sir[1]; |
| 805 | boutsum += sir[2]; |
| 806 | aoutsum += sir[3]; |
| 807 | } |
| 808 | } |
| 809 | stackpointer = radius; |
| 810 |
| 811 | for (x = 0; x < w; x++) { |
| 812 | r[yi] = dv[rsum]; |
| 813 | g[yi] = dv[gsum]; |
| 814 | b[yi] = dv[bsum]; |
| 815 | a[yi] = dv[asum]; |
| 816 |
| 817 | rsum -= routsum; |
| 818 | gsum -= goutsum; |
| 819 | bsum -= boutsum; |
| 820 | asum -= aoutsum; |
| 821 |
| 822 | stackstart = stackpointer - radius + div; |
| 823 | sir = &stack[(stackstart % div) * 4]; |
| 824 |
| 825 | routsum -= sir[0]; |
| 826 | goutsum -= sir[1]; |
| 827 | boutsum -= sir[2]; |
| 828 | aoutsum -= sir[3]; |
| 829 |
| 830 | if (y == 0) { |
| 831 | vmin[x] = MIN(x + radius + 1, wm); |
| 832 | } |
| 833 |
| 834 | int offset = (y * stride + vmin[x] * 4); |
| 835 | sir[0] = pixels[offset]; |
| 836 | sir[1] = pixels[offset + 1]; |
| 837 | sir[2] = pixels[offset + 2]; |
| 838 | sir[3] = pixels[offset + 3]; |
| 839 | rinsum += sir[0]; |
| 840 | ginsum += sir[1]; |
| 841 | binsum += sir[2]; |
| 842 | ainsum += sir[3]; |
| 843 |
| 844 | rsum += rinsum; |
| 845 | gsum += ginsum; |
| 846 | bsum += binsum; |
| 847 | asum += ainsum; |
| 848 |
| 849 | stackpointer = (stackpointer + 1) % div; |
| 850 | sir = &stack[(stackpointer % div) * 4]; |
| 851 |
| 852 | routsum += sir[0]; |
| 853 | goutsum += sir[1]; |
| 854 | boutsum += sir[2]; |
| 855 | aoutsum += sir[3]; |
| 856 |
| 857 | rinsum -= sir[0]; |
| 858 | ginsum -= sir[1]; |
| 859 | binsum -= sir[2]; |
| 860 | ainsum -= sir[3]; |
| 861 |
| 862 | yi++; |
| 863 | } |
| 864 | yw += w; |
| 865 | } |
| 866 |
| 867 | for (x = 0; x < w; x++) { |
| 868 | ainsum = aoutsum = asum = rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; |
| 869 | yp = -radius * w; |
| 870 | for (i = -radius; i <= radius; i++) { |
| 871 | yi = MAX(0, yp) + x; |
| 872 |
| 873 | sir = &stack[(i + radius) * 4]; |
| 874 |
| 875 | sir[0] = r[yi]; |
| 876 | sir[1] = g[yi]; |
| 877 | sir[2] = b[yi]; |
| 878 | sir[3] = a[yi]; |
| 879 |
| 880 | rbs = r1 - abs(i); |
| 881 |
| 882 | rsum += r[yi] * rbs; |
| 883 | gsum += g[yi] * rbs; |
| 884 | bsum += b[yi] * rbs; |
| 885 | asum += a[yi] * rbs; |
| 886 |
| 887 | if (i > 0) { |
| 888 | rinsum += sir[0]; |
| 889 | ginsum += sir[1]; |
| 890 | binsum += sir[2]; |
| 891 | ainsum += sir[3]; |
| 892 | } else { |
| 893 | routsum += sir[0]; |
| 894 | goutsum += sir[1]; |
| 895 | boutsum += sir[2]; |
| 896 | aoutsum += sir[3]; |
| 897 | } |
| 898 |
| 899 | if (i < hm) { |
| 900 | yp += w; |
| 901 | } |
| 902 | } |
| 903 | stackpointer = radius; |
| 904 | for (y = 0; y < h; y++) { |
| 905 | int offset = stride * y + x * 4; |
| 906 | pixels[offset] = dv[rsum]; |
| 907 | pixels[offset + 1] = dv[gsum]; |
| 908 | pixels[offset + 2] = dv[bsum]; |
| 909 | pixels[offset + 3] = dv[asum]; |
| 910 | rsum -= routsum; |
| 911 | gsum -= goutsum; |
| 912 | bsum -= boutsum; |
| 913 | asum -= aoutsum; |
| 914 |
| 915 | stackstart = stackpointer - radius + div; |
| 916 | sir = &stack[(stackstart % div) * 4]; |
| 917 |
| 918 | routsum -= sir[0]; |
| 919 | goutsum -= sir[1]; |
| 920 | boutsum -= sir[2]; |
| 921 | aoutsum -= sir[3]; |
| 922 |
| 923 | if (x == 0) { |
| 924 | vmin[y] = (MIN(y + r1, hm)) * w; |
| 925 | } |
| 926 | p = x + vmin[y]; |
| 927 |
| 928 | sir[0] = r[p]; |
| 929 | sir[1] = g[p]; |
| 930 | sir[2] = b[p]; |
| 931 | sir[3] = a[p]; |
| 932 |
| 933 | rinsum += sir[0]; |
| 934 | ginsum += sir[1]; |
| 935 | binsum += sir[2]; |
| 936 | ainsum += sir[3]; |
| 937 |
| 938 | rsum += rinsum; |
| 939 | gsum += ginsum; |
| 940 | bsum += binsum; |
| 941 | asum += ainsum; |
| 942 |
| 943 | stackpointer = (stackpointer + 1) % div; |
| 944 | sir = &stack[stackpointer * 4]; |
| 945 |
| 946 | routsum += sir[0]; |
| 947 | goutsum += sir[1]; |
| 948 | boutsum += sir[2]; |
| 949 | aoutsum += sir[3]; |
| 950 |
| 951 | rinsum -= sir[0]; |
| 952 | ginsum -= sir[1]; |
| 953 | binsum -= sir[2]; |
| 954 | ainsum -= sir[3]; |
| 955 |
| 956 | yi += w; |
| 957 | } |
| 958 | } |
| 959 |
| 960 | delete[] r; |
| 961 | delete[] g; |
| 962 | delete[] b; |
| 963 | delete[] a; |
| 964 | delete[] dv; |
| 965 | AndroidBitmap_unlockPixels(env, bitmap); |
| 966 | } |
| 967 |
| 968 | JNIEXPORT void Java_org_telegram_messenger_Utilities_drawDitheredGradient(JNIEnv *env, jclass clazz, jobject bitmap, jintArray colors, jint startX, jint startY, jint endX, jint endY) { |
| 969 | AndroidBitmapInfo info; |
| 970 | void *pixelsBuffer; |
| 971 | int reason; |
| 972 |
| 973 | if ((reason = AndroidBitmap_getInfo(env, bitmap, &info)) != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 974 | env->ThrowNew(jclass_RuntimeException, "AndroidBitmap_getInfo failed with a reason: " + reason); |
| 975 | return; |
| 976 | } |
| 977 |
| 978 | if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { |
| 979 | env->ThrowNew(jclass_RuntimeException, "Bitmap must be in ARGB_8888 format"); |
| 980 | return; |
| 981 | } |
| 982 |
| 983 | if ((reason = AndroidBitmap_lockPixels(env, bitmap, &pixelsBuffer)) != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 984 | env->ThrowNew(jclass_RuntimeException, "AndroidBitmap_lockPixels failed with a reason: " + reason); |
| 985 | return; |
| 986 | } |
| 987 |
| 988 | uint8_t i, j, n; |
| 989 |
| 990 | // gradient colors extracting |
| 991 | jint *colorsBuffer = env->GetIntArrayElements(colors, 0); |
| 992 | uint8_t *colorsComponents = (uint8_t *) colorsBuffer; |
| 993 | float colorsF[4][2]; |
| 994 | for (i = 0; i < 4; i++) { |
| 995 | // swap red and green channels |
| 996 | n = (uint8_t) (i == 0 ? 2 : (i == 2 ? 0 : i)); |
| 997 | for (j = 0; j < 2; j++) { |
| 998 | colorsF[n][j] = colorsComponents[j * 4 + i] / 255.F; |
| 999 | } |
| 1000 | } |
| 1001 | env->ReleaseIntArrayElements(colors, colorsBuffer, JNI_ABORT); |
| 1002 |
| 1003 | // gradient vector |
| 1004 | const int32_t vx = endX - startX; |
| 1005 | const int32_t vy = endY - startY; |
| 1006 | const float vSquaredMag = vx * vx + vy * vy; |
| 1007 |
| 1008 | float noise, fraction, error, componentF; |
| 1009 | float *pixelsComponentsF = new float[info.height * info.stride * 4]; |
| 1010 | memset(pixelsComponentsF, 0, info.height * info.stride * 4 * sizeof(float)); |
| 1011 | uint8_t * bitmapPixelsComponents = (uint8_t * ) |
| 1012 | pixelsBuffer; |
| 1013 |
| 1014 | int32_t x, y; |
| 1015 | int32_t offset; |
| 1016 | int32_t position; |
| 1017 | for (y = 0; y < info.height; y++) { |
| 1018 | offset = y * info.stride; |
| 1019 | for (x = 0; x < info.width; x++) { |
| 1020 | // triangular probability density function dither noise |
| 1021 | noise = (rand() - rand()) / 255.F / RAND_MAX; |
| 1022 |
| 1023 | // alpha channel |
| 1024 | bitmapPixelsComponents[offset + x * 4 + 3] = 255; |
| 1025 |
| 1026 | for (i = 0; i < 3; i++) { |
| 1027 | position = offset + x * 4 + i; |
| 1028 | fraction = (vx * (x - startX) + vy * (y - startY)) / vSquaredMag; |
| 1029 |
| 1030 | // gradient interpolation and noise |
| 1031 | pixelsComponentsF[position] += colorsF[i][0] + fraction * (colorsF[i][1] - colorsF[i][0]) + noise; |
| 1032 |
| 1033 | // clamp |
| 1034 | if (pixelsComponentsF[position] > 1.F) { |
| 1035 | pixelsComponentsF[position] = 1.F; |
| 1036 | } else if (pixelsComponentsF[position] < 0.F) { |
| 1037 | pixelsComponentsF[position] = 0.F; |
| 1038 | } |
| 1039 |
| 1040 | // draw |
| 1041 | componentF = roundf(pixelsComponentsF[position] * 255.F); |
| 1042 | bitmapPixelsComponents[position] = (uint8_t) |
| 1043 | componentF; |
| 1044 |
| 1045 | // floyd-steinberg dithering |
| 1046 | error = pixelsComponentsF[position] - componentF / 255.F; |
| 1047 | if (x + 1 < info.width) { |
| 1048 | pixelsComponentsF[position + 4] += error * 7.F / 16.F; |
| 1049 | if (y + 1 < info.height) { |
| 1050 | pixelsComponentsF[position + info.height + 4] += error * 1.F / 16.F; |
| 1051 | } |
| 1052 | } |
| 1053 | if (y + 1 < info.height) { |
| 1054 | pixelsComponentsF[position + info.height] += error * 5.F / 16.F; |
| 1055 | if (x - 1 >= 0) { |
| 1056 | pixelsComponentsF[position + info.height - 4] += error * 3.F / 16.F; |
| 1057 | } |
| 1058 | } |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 |
| 1063 | delete[] pixelsComponentsF; |
| 1064 |
| 1065 | if ((reason = AndroidBitmap_unlockPixels(env, bitmap)) != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 1066 | env->ThrowNew(jclass_RuntimeException, "AndroidBitmap_unlockPixels failed with a reason: " + reason); |
| 1067 | return; |
| 1068 | } |
| 1069 | } |
| 1070 |
| 1071 | JNIEXPORT jint Java_org_telegram_messenger_Utilities_saveProgressiveJpeg(JNIEnv *env, jclass clazz, jobject bitmap, jint width, jint height, jint stride, jint quality, jstring path) { |
| 1072 | if (!bitmap || !path || !width || !height || !stride || stride != width * 4) { |
| 1073 | return 0; |
| 1074 | } |
| 1075 | void *pixels = 0; |
| 1076 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { |
| 1077 | return 0; |
| 1078 | } |
| 1079 | if (pixels == NULL) { |
| 1080 | return 0; |
| 1081 | } |
| 1082 | tjhandle handle = 0; |
| 1083 | if ((handle = tjInitCompress()) == NULL) { |
| 1084 | return 0; |
| 1085 | } |
| 1086 | const char *pathStr = env->GetStringUTFChars(path, 0); |
| 1087 | std::string filePath = std::string(pathStr); |
| 1088 | if (pathStr != 0) { |
| 1089 | env->ReleaseStringUTFChars(path, pathStr); |
| 1090 | } |
| 1091 |
| 1092 | const char *enabledValue = "1"; |
| 1093 | const char *disabledValue = "0"; |
| 1094 | setenv("TJ_OPTIMIZE", enabledValue, 1); |
| 1095 | setenv("TJ_ARITHMETIC", disabledValue, 1); |
| 1096 | setenv("TJ_PROGRESSIVE", enabledValue, 1); |
| 1097 | setenv("TJ_REVERT", enabledValue, 1); |
| 1098 |
| 1099 | TJSAMP jpegSubsamp = TJSAMP::TJSAMP_420; |
| 1100 | jint buffSize = (jint) tjBufSize(width, height, jpegSubsamp); |
| 1101 | unsigned char *jpegBuf = new unsigned char[buffSize]; |
| 1102 | unsigned char *srcBuf = (unsigned char *) pixels; |
| 1103 |
| 1104 | int pf = org_libjpegturbo_turbojpeg_TJ_PF_RGBA; |
| 1105 |
| 1106 | jsize actualPitch = width * tjPixelSize[pf]; |
| 1107 | jsize arraySize = (height - 1) * actualPitch + (width) * tjPixelSize[pf]; |
| 1108 | unsigned long jpegSize = tjBufSize(width, height, jpegSubsamp); |
| 1109 |
| 1110 | if (tjCompress2(handle, srcBuf, width, stride, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, quality, TJFLAG_ACCURATEDCT | TJFLAG_PROGRESSIVE | TJFLAG_NOREALLOC) == 0) { |
| 1111 | FILE *f = fopen(filePath.c_str(), "wb"); |
| 1112 | if (f && fwrite(jpegBuf, sizeof(unsigned char), jpegSize, f) == jpegSize) { |
| 1113 | fflush(f); |
| 1114 | fsync(fileno(f)); |
| 1115 | } else { |
| 1116 | jpegSize = -1; |
| 1117 | } |
| 1118 | fclose(f); |
| 1119 | } else { |
| 1120 | jpegSize = -1; |
| 1121 | } |
| 1122 | delete[] jpegBuf; |
| 1123 | tjDestroy(handle); |
| 1124 | AndroidBitmap_unlockPixels(env, bitmap); |
| 1125 | return jpegSize; |
| 1126 |
| 1127 | /*struct jpeg_compress_struct cinfo; |
| 1128 | struct jpeg_error_mgr jerr; |
| 1129 | cinfo.err = jpeg_std_error(&jerr); |
| 1130 | jpeg_create_compress(&cinfo); |
| 1131 |
| 1132 | const char *pathStr = env->GetStringUTFChars(path, 0); |
| 1133 | std::string filePath = std::string(pathStr); |
| 1134 | if (pathStr != 0) { |
| 1135 | env->ReleaseStringUTFChars(path, pathStr); |
| 1136 | } |
| 1137 |
| 1138 | uint8_t *outBuffer = NULL; |
| 1139 | unsigned long outSize = 0; |
| 1140 | jpeg_mem_dest(&cinfo, &outBuffer, &outSize); |
| 1141 | unsigned char *srcBuf = (unsigned char *) pixels; |
| 1142 |
| 1143 | cinfo.image_width = (uint32_t) width; |
| 1144 | cinfo.image_height = (uint32_t) height; |
| 1145 | cinfo.input_components = 4; |
| 1146 | cinfo.in_color_space = JCS_EXT_RGBA; |
| 1147 | jpeg_c_set_int_param(&cinfo, JINT_COMPRESS_PROFILE, JCP_FASTEST); |
| 1148 | jpeg_set_defaults(&cinfo); |
| 1149 | cinfo.arith_code = FALSE; |
| 1150 | cinfo.dct_method = JDCT_ISLOW; |
| 1151 | cinfo.optimize_coding = TRUE; |
| 1152 | jpeg_set_quality(&cinfo, 78, 1); |
| 1153 | jpeg_simple_progression(&cinfo); |
| 1154 | jpeg_start_compress(&cinfo, 1); |
| 1155 |
| 1156 | JSAMPROW rowPointer[1]; |
| 1157 | while (cinfo.next_scanline < cinfo.image_height) { |
| 1158 | rowPointer[0] = (JSAMPROW) (srcBuf + cinfo.next_scanline * stride); |
| 1159 | jpeg_write_scanlines(&cinfo, rowPointer, 1); |
| 1160 | } |
| 1161 |
| 1162 | jpeg_finish_compress(&cinfo); |
| 1163 |
| 1164 | FILE *f = fopen(filePath.c_str(), "wb"); |
| 1165 | if (f && fwrite(outBuffer, sizeof(uint8_t), outSize, f) == outSize) { |
| 1166 | fflush(f); |
| 1167 | fsync(fileno(f)); |
| 1168 | } |
| 1169 | fclose(f); |
| 1170 |
| 1171 | jpeg_destroy_compress(&cinfo); |
| 1172 | return outSize;*/ |
| 1173 | } |
| 1174 |
| 1175 | std::vector<std::pair<float, float>> gatherPositions(std::vector<std::pair<float, float>> list, int phase) { |
| 1176 | std::vector<std::pair<float, float>> result(4); |
| 1177 | for (int i = 0; i < 4; i++) { |
| 1178 | int pos = phase + i * 2; |
| 1179 | while (pos >= 8) { |
| 1180 | pos -= 8; |
| 1181 | } |
| 1182 | result[i] = list[pos]; |
| 1183 | result[i].second = 1.0f - result[i].second; |
| 1184 | } |
| 1185 | return result; |
| 1186 | } |
| 1187 |
| 1188 | thread_local static float *pixelCache = nullptr; |
| 1189 |
| 1190 | JNIEXPORT void Java_org_telegram_messenger_Utilities_generateGradient(JNIEnv *env, jclass clazz, jobject bitmap, jboolean unpin, jint phase, jfloat progress, jint width, jint height, jint stride, jintArray colors) { |
| 1191 | if (!bitmap) { |
| 1192 | return; |
| 1193 | } |
| 1194 |
| 1195 | if (!width || !height) { |
| 1196 | return; |
| 1197 | } |
| 1198 |
| 1199 | uint8_t *pixels = nullptr; |
| 1200 | if (AndroidBitmap_lockPixels(env, bitmap, (void **) &pixels) < 0) { |
| 1201 | return; |
| 1202 | } |
| 1203 |
| 1204 | std::vector<std::pair<float, float>> positions{ |
| 1205 | {0.80f, 0.10f}, |
| 1206 | {0.60f, 0.20f}, |
| 1207 | {0.35f, 0.25f}, |
| 1208 | {0.25f, 0.60f}, |
| 1209 | {0.20f, 0.90f}, |
| 1210 | {0.40f, 0.80f}, |
| 1211 | {0.65f, 0.75f}, |
| 1212 | {0.75f, 0.40f} |
| 1213 | }; |
| 1214 |
| 1215 | int32_t previousPhase = phase + 1; |
| 1216 | if (previousPhase > 7) { |
| 1217 | previousPhase = 0; |
| 1218 | } |
| 1219 | std::vector<std::pair<float, float>> previous = gatherPositions(positions, previousPhase); |
| 1220 | std::vector<std::pair<float, float>> current = gatherPositions(positions, phase); |
| 1221 |
| 1222 | auto colorsArray = (uint8_t *) env->GetIntArrayElements(colors, nullptr); |
| 1223 | float *newPixelCache = nullptr; |
| 1224 | if (pixelCache == nullptr) { |
| 1225 | newPixelCache = new float[width * height * 2]; |
| 1226 | } |
| 1227 | float directPixelY; |
| 1228 | float centerDistanceY; |
| 1229 | float centerDistanceY2; |
| 1230 | int32_t colorsCount = colorsArray[12] == 0 && colorsArray[13] == 0 && colorsArray[14] == 0 && colorsArray[15] == 0 ? 3 : 4; |
| 1231 |
| 1232 | for (int y = 0; y < height; y++) { |
| 1233 | if (pixelCache == nullptr) { |
| 1234 | directPixelY = (float) y / (float) height; |
| 1235 | centerDistanceY = directPixelY - 0.5f; |
| 1236 | centerDistanceY2 = centerDistanceY * centerDistanceY; |
| 1237 | } |
| 1238 | uint32_t offset = y * stride; |
| 1239 | for (int x = 0; x < width; x++) { |
| 1240 | float pixelX; |
| 1241 | float pixelY; |
| 1242 | if (pixelCache != nullptr) { |
| 1243 | pixelX = pixelCache[(y * width + x) * 2]; |
| 1244 | pixelY = pixelCache[(y * width + x) * 2 + 1]; |
| 1245 | } else { |
| 1246 | float directPixelX = (float) x / (float) width; |
| 1247 |
| 1248 | float centerDistanceX = directPixelX - 0.5f; |
| 1249 | float centerDistance = sqrtf(centerDistanceX * centerDistanceX + centerDistanceY2); |
| 1250 |
| 1251 | float swirlFactor = 0.35f * centerDistance; |
| 1252 | float theta = swirlFactor * swirlFactor * 0.8f * 8.0f; |
| 1253 | float sinTheta = sinf(theta); |
| 1254 | float cosTheta = cosf(theta); |
| 1255 |
| 1256 | pixelX = newPixelCache[(y * width + x) * 2] = std::max(0.0f, std::min(1.0f, 0.5f + centerDistanceX * cosTheta - centerDistanceY * sinTheta)); |
| 1257 | pixelY = newPixelCache[(y * width + x) * 2 + 1] = std::max(0.0f, std::min(1.0f, 0.5f + centerDistanceX * sinTheta + centerDistanceY * cosTheta)); |
| 1258 | } |
| 1259 |
| 1260 | float distanceSum = 0.0f; |
| 1261 |
| 1262 | float r = 0.0f; |
| 1263 | float g = 0.0f; |
| 1264 | float b = 0.0f; |
| 1265 |
| 1266 | for (int i = 0; i < colorsCount; i++) { |
| 1267 | float colorX = previous[i].first + (current[i].first - previous[i].first) * progress; |
| 1268 | float colorY = previous[i].second + (current[i].second - previous[i].second) * progress; |
| 1269 |
| 1270 | float distanceX = pixelX - colorX; |
| 1271 | float distanceY = pixelY - colorY; |
| 1272 |
| 1273 | float distance = std::max(0.0f, 0.9f - sqrtf(distanceX * distanceX + distanceY * distanceY)); |
| 1274 | distance = distance * distance * distance * distance; |
| 1275 | distanceSum += distance; |
| 1276 |
| 1277 | r = r + distance * ((float) colorsArray[i * 4] / 255.0f); |
| 1278 | g = g + distance * ((float) colorsArray[i * 4 + 1] / 255.0f); |
| 1279 | b = b + distance * ((float) colorsArray[i * 4 + 2] / 255.0f); |
| 1280 | } |
| 1281 |
| 1282 | pixels[offset + x * 4] = (uint8_t) (b / distanceSum * 255.0f); |
| 1283 | pixels[offset + x * 4 + 1] = (uint8_t) (g / distanceSum * 255.0f); |
| 1284 | pixels[offset + x * 4 + 2] = (uint8_t) (r / distanceSum * 255.0f); |
| 1285 | pixels[offset + x * 4 + 3] = 0xff; |
| 1286 | } |
| 1287 | } |
| 1288 | if (newPixelCache != nullptr) { |
| 1289 | delete [] pixelCache; |
| 1290 | pixelCache = newPixelCache; |
| 1291 | } |
| 1292 |
| 1293 | env->ReleaseIntArrayElements(colors, (jint *) colorsArray, JNI_ABORT); |
| 1294 |
| 1295 | if (unpin) { |
| 1296 | AndroidBitmap_unlockPixels(env, bitmap); |
| 1297 | } |
| 1298 | } |
| 1299 |
| 1300 | } |