| 5 |
| #include <cstring> |
| 6 | #include <cmath> |
| 7 | #include <cstdint> |
| 8 | #include <mutex> |
| 9 | #include <unistd.h> |
| 10 | #include <android/bitmap.h> |
| 11 | #include <string> |
| 12 | //#include <mozjpeg/java/org_libjpegturbo_turbojpeg_TJ.h> |
| 13 | //#include <mozjpeg/jpeglib.h> |
| 14 | #include <tgnet/FileLog.h> |
| 15 | #include <vector> |
| 16 | #include <algorithm> |
| 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 | #define SQUARE(i) ((i)*(i)) |
| 658 |
| 659 | inline static void zeroClearInt(int *p, size_t count) { |
| 660 | memset(p, 0, sizeof(int) * count); |
| 661 | } |
| 662 |
| 663 | JNIEXPORT void Java_org_telegram_messenger_Utilities_stackBlurBitmap(JNIEnv *env, jclass clazz, jobject bitmap, jint radius) { |
| 664 | if (radius < 1) { |
| 665 | return; |
| 666 | } |
| 667 |
| 668 | AndroidBitmapInfo info; |
| 669 | if (AndroidBitmap_getInfo(env, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 670 | return; |
| 671 | } |
| 672 | if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { |
| 673 | return; |
| 674 | } |
| 675 |
| 676 | int w = info.width; |
| 677 | int h = info.height; |
| 678 | int stride = info.stride; |
| 679 |
| 680 | unsigned char *pixels = nullptr; |
| 681 | AndroidBitmap_lockPixels(env, bitmap, (void **) &pixels); |
| 682 | if (!pixels) { |
| 683 | return; |
| 684 | } |
| 685 | // Constants |
| 686 | //const int radius = (int)inradius; // Transform unsigned into signed for further operations |
| 687 | const int wm = w - 1; |
| 688 | const int hm = h - 1; |
| 689 | const int wh = w * h; |
| 690 | const int div = radius + radius + 1; |
| 691 | const int r1 = radius + 1; |
| 692 | const int divsum = SQUARE((div + 1) >> 1); |
| 693 |
| 694 | // Small buffers |
| 695 | int stack[div * 4]; |
| 696 | zeroClearInt(stack, div * 4); |
| 697 |
| 698 | int vmin[MAX(w, h)]; |
| 699 | zeroClearInt(vmin, MAX(w, h)); |
| 700 |
| 701 | // Large buffers |
| 702 | int *r = new int[wh]; |
| 703 | int *g = new int[wh]; |
| 704 | int *b = new int[wh]; |
| 705 | int *a = new int[wh]; |
| 706 | zeroClearInt(r, wh); |
| 707 | zeroClearInt(g, wh); |
| 708 | zeroClearInt(b, wh); |
| 709 | zeroClearInt(a, wh); |
| 710 |
| 711 | const size_t dvcount = 256 * divsum; |
| 712 | int *dv = new int[dvcount]; |
| 713 | int i; |
| 714 | for (i = 0; (size_t) i < dvcount; i++) { |
| 715 | dv[i] = (i / divsum); |
| 716 | } |
| 717 |
| 718 | // Variables |
| 719 | int x, y; |
| 720 | int *sir; |
| 721 | int routsum, goutsum, boutsum, aoutsum; |
| 722 | int rinsum, ginsum, binsum, ainsum; |
| 723 | int rsum, gsum, bsum, asum, p, yp; |
| 724 | int stackpointer; |
| 725 | int stackstart; |
| 726 | int rbs; |
| 727 |
| 728 | int yw = 0, yi = 0; |
| 729 | for (y = 0; y < h; y++) { |
| 730 | ainsum = aoutsum = asum = rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; |
| 731 |
| 732 | for (i = -radius; i <= radius; i++) { |
| 733 | sir = &stack[(i + radius) * 4]; |
| 734 | int offset = (y * stride + (MIN(wm, MAX(i, 0))) * 4); |
| 735 | sir[0] = pixels[offset]; |
| 736 | sir[1] = pixels[offset + 1]; |
| 737 | sir[2] = pixels[offset + 2]; |
| 738 | sir[3] = pixels[offset + 3]; |
| 739 |
| 740 | rbs = r1 - abs(i); |
| 741 | rsum += sir[0] * rbs; |
| 742 | gsum += sir[1] * rbs; |
| 743 | bsum += sir[2] * rbs; |
| 744 | asum += sir[3] * rbs; |
| 745 | if (i > 0) { |
| 746 | rinsum += sir[0]; |
| 747 | ginsum += sir[1]; |
| 748 | binsum += sir[2]; |
| 749 | ainsum += sir[3]; |
| 750 | } else { |
| 751 | routsum += sir[0]; |
| 752 | goutsum += sir[1]; |
| 753 | boutsum += sir[2]; |
| 754 | aoutsum += sir[3]; |
| 755 | } |
| 756 | } |
| 757 | stackpointer = radius; |
| 758 |
| 759 | for (x = 0; x < w; x++) { |
| 760 | r[yi] = dv[rsum]; |
| 761 | g[yi] = dv[gsum]; |
| 762 | b[yi] = dv[bsum]; |
| 763 | a[yi] = dv[asum]; |
| 764 |
| 765 | rsum -= routsum; |
| 766 | gsum -= goutsum; |
| 767 | bsum -= boutsum; |
| 768 | asum -= aoutsum; |
| 769 |
| 770 | stackstart = stackpointer - radius + div; |
| 771 | sir = &stack[(stackstart % div) * 4]; |
| 772 |
| 773 | routsum -= sir[0]; |
| 774 | goutsum -= sir[1]; |
| 775 | boutsum -= sir[2]; |
| 776 | aoutsum -= sir[3]; |
| 777 |
| 778 | if (y == 0) { |
| 779 | vmin[x] = MIN(x + radius + 1, wm); |
| 780 | } |
| 781 |
| 782 | int offset = (y * stride + vmin[x] * 4); |
| 783 | sir[0] = pixels[offset]; |
| 784 | sir[1] = pixels[offset + 1]; |
| 785 | sir[2] = pixels[offset + 2]; |
| 786 | sir[3] = pixels[offset + 3]; |
| 787 | rinsum += sir[0]; |
| 788 | ginsum += sir[1]; |
| 789 | binsum += sir[2]; |
| 790 | ainsum += sir[3]; |
| 791 |
| 792 | rsum += rinsum; |
| 793 | gsum += ginsum; |
| 794 | bsum += binsum; |
| 795 | asum += ainsum; |
| 796 |
| 797 | stackpointer = (stackpointer + 1) % div; |
| 798 | sir = &stack[(stackpointer % div) * 4]; |
| 799 |
| 800 | routsum += sir[0]; |
| 801 | goutsum += sir[1]; |
| 802 | boutsum += sir[2]; |
| 803 | aoutsum += sir[3]; |
| 804 |
| 805 | rinsum -= sir[0]; |
| 806 | ginsum -= sir[1]; |
| 807 | binsum -= sir[2]; |
| 808 | ainsum -= sir[3]; |
| 809 |
| 810 | yi++; |
| 811 | } |
| 812 | yw += w; |
| 813 | } |
| 814 |
| 815 | for (x = 0; x < w; x++) { |
| 816 | ainsum = aoutsum = asum = rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; |
| 817 | yp = -radius * w; |
| 818 | for (i = -radius; i <= radius; i++) { |
| 819 | yi = MAX(0, yp) + x; |
| 820 |
| 821 | sir = &stack[(i + radius) * 4]; |
| 822 |
| 823 | sir[0] = r[yi]; |
| 824 | sir[1] = g[yi]; |
| 825 | sir[2] = b[yi]; |
| 826 | sir[3] = a[yi]; |
| 827 |
| 828 | rbs = r1 - abs(i); |
| 829 |
| 830 | rsum += r[yi] * rbs; |
| 831 | gsum += g[yi] * rbs; |
| 832 | bsum += b[yi] * rbs; |
| 833 | asum += a[yi] * rbs; |
| 834 |
| 835 | if (i > 0) { |
| 836 | rinsum += sir[0]; |
| 837 | ginsum += sir[1]; |
| 838 | binsum += sir[2]; |
| 839 | ainsum += sir[3]; |
| 840 | } else { |
| 841 | routsum += sir[0]; |
| 842 | goutsum += sir[1]; |
| 843 | boutsum += sir[2]; |
| 844 | aoutsum += sir[3]; |
| 845 | } |
| 846 |
| 847 | if (i < hm) { |
| 848 | yp += w; |
| 849 | } |
| 850 | } |
| 851 | stackpointer = radius; |
| 852 | for (y = 0; y < h; y++) { |
| 853 | int offset = stride * y + x * 4; |
| 854 | pixels[offset] = dv[rsum]; |
| 855 | pixels[offset + 1] = dv[gsum]; |
| 856 | pixels[offset + 2] = dv[bsum]; |
| 857 | pixels[offset + 3] = dv[asum]; |
| 858 | rsum -= routsum; |
| 859 | gsum -= goutsum; |
| 860 | bsum -= boutsum; |
| 861 | asum -= aoutsum; |
| 862 |
| 863 | stackstart = stackpointer - radius + div; |
| 864 | sir = &stack[(stackstart % div) * 4]; |
| 865 |
| 866 | routsum -= sir[0]; |
| 867 | goutsum -= sir[1]; |
| 868 | boutsum -= sir[2]; |
| 869 | aoutsum -= sir[3]; |
| 870 |
| 871 | if (x == 0) { |
| 872 | vmin[y] = (MIN(y + r1, hm)) * w; |
| 873 | } |
| 874 | p = x + vmin[y]; |
| 875 |
| 876 | sir[0] = r[p]; |
| 877 | sir[1] = g[p]; |
| 878 | sir[2] = b[p]; |
| 879 | sir[3] = a[p]; |
| 880 |
| 881 | rinsum += sir[0]; |
| 882 | ginsum += sir[1]; |
| 883 | binsum += sir[2]; |
| 884 | ainsum += sir[3]; |
| 885 |
| 886 | rsum += rinsum; |
| 887 | gsum += ginsum; |
| 888 | bsum += binsum; |
| 889 | asum += ainsum; |
| 890 |
| 891 | stackpointer = (stackpointer + 1) % div; |
| 892 | sir = &stack[stackpointer * 4]; |
| 893 |
| 894 | routsum += sir[0]; |
| 895 | goutsum += sir[1]; |
| 896 | boutsum += sir[2]; |
| 897 | aoutsum += sir[3]; |
| 898 |
| 899 | rinsum -= sir[0]; |
| 900 | ginsum -= sir[1]; |
| 901 | binsum -= sir[2]; |
| 902 | ainsum -= sir[3]; |
| 903 |
| 904 | yi += w; |
| 905 | } |
| 906 | } |
| 907 |
| 908 | delete[] r; |
| 909 | delete[] g; |
| 910 | delete[] b; |
| 911 | delete[] a; |
| 912 | delete[] dv; |
| 913 | AndroidBitmap_unlockPixels(env, bitmap); |
| 914 | } |
| 915 |
| 916 | JNIEXPORT void Java_org_telegram_messenger_Utilities_drawDitheredGradient(JNIEnv *env, jclass clazz, jobject bitmap, jintArray colors, jint startX, jint startY, jint endX, jint endY) { |
| 917 | AndroidBitmapInfo info; |
| 918 | void *pixelsBuffer; |
| 919 | int reason; |
| 920 |
| 921 | if ((reason = AndroidBitmap_getInfo(env, bitmap, &info)) != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 922 | env->ThrowNew(jclass_RuntimeException, "AndroidBitmap_getInfo failed with a reason: " + reason); |
| 923 | return; |
| 924 | } |
| 925 |
| 926 | if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { |
| 927 | env->ThrowNew(jclass_RuntimeException, "Bitmap must be in ARGB_8888 format"); |
| 928 | return; |
| 929 | } |
| 930 |
| 931 | if ((reason = AndroidBitmap_lockPixels(env, bitmap, &pixelsBuffer)) != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 932 | env->ThrowNew(jclass_RuntimeException, "AndroidBitmap_lockPixels failed with a reason: " + reason); |
| 933 | return; |
| 934 | } |
| 935 |
| 936 | uint8_t i, j, n; |
| 937 |
| 938 | // gradient colors extracting |
| 939 | jint *colorsBuffer = env->GetIntArrayElements(colors, 0); |
| 940 | uint8_t *colorsComponents = (uint8_t *) colorsBuffer; |
| 941 | float colorsF[4][2]; |
| 942 | for (i = 0; i < 4; i++) { |
| 943 | // swap red and green channels |
| 944 | n = (uint8_t) (i == 0 ? 2 : (i == 2 ? 0 : i)); |
| 945 | for (j = 0; j < 2; j++) { |
| 946 | colorsF[n][j] = colorsComponents[j * 4 + i] / 255.F; |
| 947 | } |
| 948 | } |
| 949 | env->ReleaseIntArrayElements(colors, colorsBuffer, JNI_ABORT); |
| 950 |
| 951 | // gradient vector |
| 952 | const int32_t vx = endX - startX; |
| 953 | const int32_t vy = endY - startY; |
| 954 | const float vSquaredMag = vx * vx + vy * vy; |
| 955 |
| 956 | float noise, fraction, error, componentF; |
| 957 | float *pixelsComponentsF = new float[info.height * info.stride * 4]; |
| 958 | memset(pixelsComponentsF, 0, info.height * info.stride * 4 * sizeof(float)); |
| 959 | uint8_t * bitmapPixelsComponents = (uint8_t * ) |
| 960 | pixelsBuffer; |
| 961 |
| 962 | int32_t x, y; |
| 963 | int32_t offset; |
| 964 | int32_t position; |
| 965 | for (y = 0; y < info.height; y++) { |
| 966 | offset = y * info.stride; |
| 967 | for (x = 0; x < info.width; x++) { |
| 968 | // triangular probability density function dither noise |
| 969 | noise = (rand() - rand()) / 255.F / RAND_MAX; |
| 970 |
| 971 | // alpha channel |
| 972 | bitmapPixelsComponents[offset + x * 4 + 3] = 255; |
| 973 |
| 974 | for (i = 0; i < 3; i++) { |
| 975 | position = offset + x * 4 + i; |
| 976 | fraction = (vx * (x - startX) + vy * (y - startY)) / vSquaredMag; |
| 977 |
| 978 | // gradient interpolation and noise |
| 979 | pixelsComponentsF[position] += colorsF[i][0] + fraction * (colorsF[i][1] - colorsF[i][0]) + noise; |
| 980 |
| 981 | // clamp |
| 982 | if (pixelsComponentsF[position] > 1.F) { |
| 983 | pixelsComponentsF[position] = 1.F; |
| 984 | } else if (pixelsComponentsF[position] < 0.F) { |
| 985 | pixelsComponentsF[position] = 0.F; |
| 986 | } |
| 987 |
| 988 | // draw |
| 989 | componentF = roundf(pixelsComponentsF[position] * 255.F); |
| 990 | bitmapPixelsComponents[position] = (uint8_t) |
| 991 | componentF; |
| 992 |
| 993 | // floyd-steinberg dithering |
| 994 | error = pixelsComponentsF[position] - componentF / 255.F; |
| 995 | if (x + 1 < info.width) { |
| 996 | pixelsComponentsF[position + 4] += error * 7.F / 16.F; |
| 997 | if (y + 1 < info.height) { |
| 998 | pixelsComponentsF[position + info.height + 4] += error * 1.F / 16.F; |
| 999 | } |
| 1000 | } |
| 1001 | if (y + 1 < info.height) { |
| 1002 | pixelsComponentsF[position + info.height] += error * 5.F / 16.F; |
| 1003 | if (x - 1 >= 0) { |
| 1004 | pixelsComponentsF[position + info.height - 4] += error * 3.F / 16.F; |
| 1005 | } |
| 1006 | } |
| 1007 | } |
| 1008 | } |
| 1009 | } |
| 1010 |
| 1011 | delete[] pixelsComponentsF; |
| 1012 |
| 1013 | if ((reason = AndroidBitmap_unlockPixels(env, bitmap)) != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 1014 | env->ThrowNew(jclass_RuntimeException, "AndroidBitmap_unlockPixels failed with a reason: " + reason); |
| 1015 | return; |
| 1016 | } |
| 1017 | } |
| 1018 |
| 1019 | //JNIEXPORT jint Java_org_telegram_messenger_Utilities_saveProgressiveJpeg(JNIEnv *env, jclass clazz, jobject bitmap, jint width, jint height, jint stride, jint quality, jstring path) { |
| 1020 | // if (!bitmap || !path || !width || !height || !stride || stride != width * 4) { |
| 1021 | // return 0; |
| 1022 | // } |
| 1023 | // void *pixels = 0; |
| 1024 | // if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { |
| 1025 | // return 0; |
| 1026 | // } |
| 1027 | // if (pixels == NULL) { |
| 1028 | // return 0; |
| 1029 | // } |
| 1030 | // tjhandle handle = 0; |
| 1031 | // if ((handle = tjInitCompress()) == NULL) { |
| 1032 | // return 0; |
| 1033 | // } |
| 1034 | // const char *pathStr = env->GetStringUTFChars(path, 0); |
| 1035 | // if (pathStr == NULL) { |
| 1036 | // return 0; |
| 1037 | // } |
| 1038 | // std::string filePath = std::string(pathStr); |
| 1039 | // if (pathStr != 0) { |
| 1040 | // env->ReleaseStringUTFChars(path, pathStr); |
| 1041 | // } |
| 1042 | // |
| 1043 | // const char *enabledValue = "1"; |
| 1044 | // const char *disabledValue = "0"; |
| 1045 | // setenv("TJ_OPTIMIZE", enabledValue, 1); |
| 1046 | // setenv("TJ_ARITHMETIC", disabledValue, 1); |
| 1047 | // setenv("TJ_PROGRESSIVE", enabledValue, 1); |
| 1048 | // setenv("TJ_REVERT", enabledValue, 1); |
| 1049 | // |
| 1050 | // TJSAMP jpegSubsamp = TJSAMP::TJSAMP_420; |
| 1051 | // jint buffSize = (jint) tjBufSize(width, height, jpegSubsamp); |
| 1052 | // unsigned char *jpegBuf = new unsigned char[buffSize]; |
| 1053 | // unsigned char *srcBuf = (unsigned char *) pixels; |
| 1054 | // |
| 1055 | // int pf = org_libjpegturbo_turbojpeg_TJ_PF_RGBA; |
| 1056 | // |
| 1057 | // jsize actualPitch = width * tjPixelSize[pf]; |
| 1058 | // jsize arraySize = (height - 1) * actualPitch + (width) * tjPixelSize[pf]; |
| 1059 | // unsigned long jpegSize = tjBufSize(width, height, jpegSubsamp); |
| 1060 | // |
| 1061 | // if (tjCompress2(handle, srcBuf, width, stride, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, quality, TJFLAG_ACCURATEDCT | TJFLAG_PROGRESSIVE | TJFLAG_NOREALLOC) == 0) { |
| 1062 | // FILE *f = fopen(filePath.c_str(), "wb"); |
| 1063 | // if (f && fwrite(jpegBuf, sizeof(unsigned char), jpegSize, f) == jpegSize) { |
| 1064 | // fflush(f); |
| 1065 | // fsync(fileno(f)); |
| 1066 | // } else { |
| 1067 | // jpegSize = -1; |
| 1068 | // } |
| 1069 | // fclose(f); |
| 1070 | // } else { |
| 1071 | // jpegSize = -1; |
| 1072 | // } |
| 1073 | // delete[] jpegBuf; |
| 1074 | // tjDestroy(handle); |
| 1075 | // AndroidBitmap_unlockPixels(env, bitmap); |
| 1076 | // return jpegSize; |
| 1077 | // |
| 1078 | // /*struct jpeg_compress_struct cinfo; |
| 1079 | // struct jpeg_error_mgr jerr; |
| 1080 | // cinfo.err = jpeg_std_error(&jerr); |
| 1081 | // jpeg_create_compress(&cinfo); |
| 1082 | // |
| 1083 | // const char *pathStr = env->GetStringUTFChars(path, 0); |
| 1084 | // std::string filePath = std::string(pathStr); |
| 1085 | // if (pathStr != 0) { |
| 1086 | // env->ReleaseStringUTFChars(path, pathStr); |
| 1087 | // } |
| 1088 | // |
| 1089 | // uint8_t *outBuffer = NULL; |
| 1090 | // unsigned long outSize = 0; |
| 1091 | // jpeg_mem_dest(&cinfo, &outBuffer, &outSize); |
| 1092 | // unsigned char *srcBuf = (unsigned char *) pixels; |
| 1093 | // |
| 1094 | // cinfo.image_width = (uint32_t) width; |
| 1095 | // cinfo.image_height = (uint32_t) height; |
| 1096 | // cinfo.input_components = 4; |
| 1097 | // cinfo.in_color_space = JCS_EXT_RGBA; |
| 1098 | // jpeg_c_set_int_param(&cinfo, JINT_COMPRESS_PROFILE, JCP_FASTEST); |
| 1099 | // jpeg_set_defaults(&cinfo); |
| 1100 | // cinfo.arith_code = FALSE; |
| 1101 | // cinfo.dct_method = JDCT_ISLOW; |
| 1102 | // cinfo.optimize_coding = TRUE; |
| 1103 | // jpeg_set_quality(&cinfo, 78, 1); |
| 1104 | // jpeg_simple_progression(&cinfo); |
| 1105 | // jpeg_start_compress(&cinfo, 1); |
| 1106 | // |
| 1107 | // JSAMPROW rowPointer[1]; |
| 1108 | // while (cinfo.next_scanline < cinfo.image_height) { |
| 1109 | // rowPointer[0] = (JSAMPROW) (srcBuf + cinfo.next_scanline * stride); |
| 1110 | // jpeg_write_scanlines(&cinfo, rowPointer, 1); |
| 1111 | // } |
| 1112 | // |
| 1113 | // jpeg_finish_compress(&cinfo); |
| 1114 | // |
| 1115 | // FILE *f = fopen(filePath.c_str(), "wb"); |
| 1116 | // if (f && fwrite(outBuffer, sizeof(uint8_t), outSize, f) == outSize) { |
| 1117 | // fflush(f); |
| 1118 | // fsync(fileno(f)); |
| 1119 | // } |
| 1120 | // fclose(f); |
| 1121 | // |
| 1122 | // jpeg_destroy_compress(&cinfo); |
| 1123 | // return outSize;*/ |
| 1124 | //} |
| 1125 |
| 1126 | std::vector<std::pair<float, float>> gatherPositions(std::vector<std::pair<float, float>> list, int phase) { |
| 1127 | std::vector<std::pair<float, float>> result(4); |
| 1128 | for (int i = 0; i < 4; i++) { |
| 1129 | int pos = phase + i * 2; |
| 1130 | while (pos >= 8) { |
| 1131 | pos -= 8; |
| 1132 | } |
| 1133 | result[i] = list[pos]; |
| 1134 | result[i].second = 1.0f - result[i].second; |
| 1135 | } |
| 1136 | return result; |
| 1137 | } |
| 1138 |
| 1139 | thread_local static float *pixelCache = nullptr; |
| 1140 | thread_local static int pixelCacheSize = 0; |
| 1141 |
| 1142 | 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) { |
| 1143 | if (!bitmap) { |
| 1144 | return; |
| 1145 | } |
| 1146 |
| 1147 | if (!width || !height) { |
| 1148 | return; |
| 1149 | } |
| 1150 |
| 1151 | uint8_t *pixels = nullptr; |
| 1152 | if (AndroidBitmap_lockPixels(env, bitmap, (void **) &pixels) < 0) { |
| 1153 | return; |
| 1154 | } |
| 1155 |
| 1156 | std::vector<std::pair<float, float>> positions{ |
| 1157 | {0.80f, 0.10f}, |
| 1158 | {0.60f, 0.20f}, |
| 1159 | {0.35f, 0.25f}, |
| 1160 | {0.25f, 0.60f}, |
| 1161 | {0.20f, 0.90f}, |
| 1162 | {0.40f, 0.80f}, |
| 1163 | {0.65f, 0.75f}, |
| 1164 | {0.75f, 0.40f} |
| 1165 | }; |
| 1166 |
| 1167 | int32_t previousPhase = phase + 1; |
| 1168 | if (previousPhase > 7) { |
| 1169 | previousPhase = 0; |
| 1170 | } |
| 1171 | std::vector<std::pair<float, float>> previous = gatherPositions(positions, previousPhase); |
| 1172 | std::vector<std::pair<float, float>> current = gatherPositions(positions, phase); |
| 1173 |
| 1174 | auto colorsArray = (uint8_t *) env->GetIntArrayElements(colors, nullptr); |
| 1175 | float *newPixelCache = nullptr; |
| 1176 |
| 1177 | if (width * height != pixelCacheSize && pixelCache != nullptr) { |
| 1178 | delete[] pixelCache; |
| 1179 | pixelCache = nullptr; |
| 1180 | } |
| 1181 | pixelCacheSize = width * height; |
| 1182 |
| 1183 | if (pixelCache == nullptr) { |
| 1184 | newPixelCache = new float[width * height * 2]; |
| 1185 | } |
| 1186 | float directPixelY; |
| 1187 | float centerDistanceY; |
| 1188 | float centerDistanceY2; |
| 1189 | int32_t colorsCount = colorsArray[12] == 0 && colorsArray[13] == 0 && colorsArray[14] == 0 && colorsArray[15] == 0 ? 3 : 4; |
| 1190 |
| 1191 | for (int y = 0; y < height; y++) { |
| 1192 | if (pixelCache == nullptr) { |
| 1193 | directPixelY = (float) y / (float) height; |
| 1194 | centerDistanceY = directPixelY - 0.5f; |
| 1195 | centerDistanceY2 = centerDistanceY * centerDistanceY; |
| 1196 | } |
| 1197 | uint32_t offset = y * stride; |
| 1198 | for (int x = 0; x < width; x++) { |
| 1199 | float pixelX; |
| 1200 | float pixelY; |
| 1201 | if (pixelCache != nullptr) { |
| 1202 | pixelX = pixelCache[(y * width + x) * 2]; |
| 1203 | pixelY = pixelCache[(y * width + x) * 2 + 1]; |
| 1204 | } else { |
| 1205 | float directPixelX = (float) x / (float) width; |
| 1206 |
| 1207 | float centerDistanceX = directPixelX - 0.5f; |
| 1208 | float centerDistance = sqrtf(centerDistanceX * centerDistanceX + centerDistanceY2); |
| 1209 |
| 1210 | float swirlFactor = 0.35f * centerDistance; |
| 1211 | float theta = swirlFactor * swirlFactor * 0.8f * 8.0f; |
| 1212 | float sinTheta = sinf(theta); |
| 1213 | float cosTheta = cosf(theta); |
| 1214 |
| 1215 | pixelX = newPixelCache[(y * width + x) * 2] = std::max(0.0f, std::min(1.0f, 0.5f + centerDistanceX * cosTheta - centerDistanceY * sinTheta)); |
| 1216 | pixelY = newPixelCache[(y * width + x) * 2 + 1] = std::max(0.0f, std::min(1.0f, 0.5f + centerDistanceX * sinTheta + centerDistanceY * cosTheta)); |
| 1217 | } |
| 1218 |
| 1219 | float distanceSum = 0.0f; |
| 1220 |
| 1221 | float r = 0.0f; |
| 1222 | float g = 0.0f; |
| 1223 | float b = 0.0f; |
| 1224 |
| 1225 | for (int i = 0; i < colorsCount; i++) { |
| 1226 | float colorX = previous[i].first + (current[i].first - previous[i].first) * progress; |
| 1227 | float colorY = previous[i].second + (current[i].second - previous[i].second) * progress; |
| 1228 |
| 1229 | float distanceX = pixelX - colorX; |
| 1230 | float distanceY = pixelY - colorY; |
| 1231 |
| 1232 | float distance = std::max(0.0f, 0.9f - sqrtf(distanceX * distanceX + distanceY * distanceY)); |
| 1233 | distance = distance * distance * distance * distance; |
| 1234 | distanceSum += distance; |
| 1235 |
| 1236 | r = r + distance * ((float) colorsArray[i * 4] / 255.0f); |
| 1237 | g = g + distance * ((float) colorsArray[i * 4 + 1] / 255.0f); |
| 1238 | b = b + distance * ((float) colorsArray[i * 4 + 2] / 255.0f); |
| 1239 | } |
| 1240 |
| 1241 | pixels[offset + x * 4] = (uint8_t) (b / distanceSum * 255.0f); |
| 1242 | pixels[offset + x * 4 + 1] = (uint8_t) (g / distanceSum * 255.0f); |
| 1243 | pixels[offset + x * 4 + 2] = (uint8_t) (r / distanceSum * 255.0f); |
| 1244 | pixels[offset + x * 4 + 3] = 0xff; |
| 1245 | } |
| 1246 | } |
| 1247 | if (newPixelCache != nullptr) { |
| 1248 | delete [] pixelCache; |
| 1249 | pixelCache = newPixelCache; |
| 1250 | } |
| 1251 |
| 1252 | env->ReleaseIntArrayElements(colors, (jint *) colorsArray, JNI_ABORT); |
| 1253 |
| 1254 | if (unpin) { |
| 1255 | AndroidBitmap_unlockPixels(env, bitmap); |
| 1256 | } |
| 1257 | } |
| 1258 |
| 1259 | static inline uint32_t bitmapBytesPerPixel(int32_t format) { |
| 1260 | switch (format) { |
| 1261 | case ANDROID_BITMAP_FORMAT_A_8: |
| 1262 | return 1; |
| 1263 |
| 1264 | case ANDROID_BITMAP_FORMAT_RGB_565: |
| 1265 | case ANDROID_BITMAP_FORMAT_RGBA_4444: // deprecated since API 13 |
| 1266 | return 2; |
| 1267 |
| 1268 | case ANDROID_BITMAP_FORMAT_RGBA_8888: |
| 1269 | return 4; |
| 1270 |
| 1271 | case ANDROID_BITMAP_FORMAT_RGBA_F16: |
| 1272 | return 8; |
| 1273 |
| 1274 | case ANDROID_BITMAP_FORMAT_RGBA_1010102: |
| 1275 | return 4; |
| 1276 |
| 1277 | default: |
| 1278 | return 0; |
| 1279 | } |
| 1280 | } |
| 1281 |
| 1282 | /** |
| 1283 | * Copies pixel data from src to dst. |
| 1284 | * |
| 1285 | * Both bitmaps must have identical dimensions and pixel format. |
| 1286 | * Hardware-backed bitmaps are not supported. |
| 1287 | * Copying a bitmap to itself is a no-op and returns JNI_TRUE. |
| 1288 | * |
| 1289 | * @param src Source bitmap. |
| 1290 | * @param dst Destination bitmap. |
| 1291 | * @return JNI_TRUE on success, JNI_FALSE if bitmaps are incompatible or an error occurred. |
| 1292 | */ |
| 1293 | JNIEXPORT jboolean JNICALL |
| 1294 | Java_org_telegram_messenger_Utilities_copyBitmaps( |
| 1295 | JNIEnv *env, |
| 1296 | jclass /*clazz*/, |
| 1297 | jobject src, |
| 1298 | jobject dst) { |
| 1299 |
| 1300 | if (__builtin_expect(src == nullptr || dst == nullptr, 0)) { |
| 1301 | return JNI_FALSE; |
| 1302 | } |
| 1303 |
| 1304 | if (__builtin_expect(env->IsSameObject(src, dst), 0)) { |
| 1305 | return JNI_TRUE; |
| 1306 | } |
| 1307 |
| 1308 | AndroidBitmapInfo srcInfo{}; |
| 1309 | AndroidBitmapInfo dstInfo{}; |
| 1310 |
| 1311 | if (__builtin_expect( |
| 1312 | AndroidBitmap_getInfo(env, src, &srcInfo) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 1313 | AndroidBitmap_getInfo(env, dst, &dstInfo) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1314 | 0)) { |
| 1315 | return JNI_FALSE; |
| 1316 | } |
| 1317 |
| 1318 | if (__builtin_expect( |
| 1319 | (srcInfo.flags & ANDROID_BITMAP_FLAGS_IS_HARDWARE) != 0 || |
| 1320 | (dstInfo.flags & ANDROID_BITMAP_FLAGS_IS_HARDWARE) != 0, |
| 1321 | 0)) { |
| 1322 | return JNI_FALSE; |
| 1323 | } |
| 1324 |
| 1325 | if (__builtin_expect( |
| 1326 | srcInfo.width != dstInfo.width || |
| 1327 | srcInfo.height != dstInfo.height || |
| 1328 | srcInfo.format != dstInfo.format || |
| 1329 | srcInfo.width == 0 || |
| 1330 | srcInfo.height == 0, |
| 1331 | 0)) { |
| 1332 | return JNI_FALSE; |
| 1333 | } |
| 1334 |
| 1335 | const uint32_t bytesPerPixel = bitmapBytesPerPixel(srcInfo.format); |
| 1336 | if (__builtin_expect(bytesPerPixel == 0, 0)) { |
| 1337 | return JNI_FALSE; |
| 1338 | } |
| 1339 |
| 1340 | // size_t cast prevents width * bytesPerPixel overflow on 32-bit platforms |
| 1341 | const size_t rowBytes = static_cast<size_t>(srcInfo.width) * bytesPerPixel; |
| 1342 |
| 1343 | if (__builtin_expect( |
| 1344 | static_cast<size_t>(srcInfo.stride) < rowBytes || |
| 1345 | static_cast<size_t>(dstInfo.stride) < rowBytes, |
| 1346 | 0)) { |
| 1347 | return JNI_FALSE; |
| 1348 | } |
| 1349 |
| 1350 | void *srcPixels = nullptr; |
| 1351 | void *dstPixels = nullptr; |
| 1352 |
| 1353 | if (__builtin_expect( |
| 1354 | AndroidBitmap_lockPixels(env, src, &srcPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1355 | 0)) { |
| 1356 | return JNI_FALSE; |
| 1357 | } |
| 1358 |
| 1359 | if (__builtin_expect( |
| 1360 | AndroidBitmap_lockPixels(env, dst, &dstPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1361 | 0)) { |
| 1362 | AndroidBitmap_unlockPixels(env, src); |
| 1363 | return JNI_FALSE; |
| 1364 | } |
| 1365 |
| 1366 | const bool contiguous = |
| 1367 | static_cast<size_t>(srcInfo.stride) == rowBytes && |
| 1368 | static_cast<size_t>(dstInfo.stride) == rowBytes; |
| 1369 |
| 1370 | if (contiguous) { |
| 1371 | // size_t cast prevents rowBytes * height overflow on 32-bit platforms |
| 1372 | std::memcpy(dstPixels, srcPixels, rowBytes * static_cast<size_t>(srcInfo.height)); |
| 1373 | } else { |
| 1374 | auto *srcRow = static_cast<const uint8_t *>(srcPixels); |
| 1375 | auto *dstRow = static_cast<uint8_t *>(dstPixels); |
| 1376 |
| 1377 | for (uint32_t y = 0; y < srcInfo.height; ++y) { |
| 1378 | std::memcpy(dstRow, srcRow, rowBytes); |
| 1379 | srcRow += static_cast<ptrdiff_t>(srcInfo.stride); |
| 1380 | dstRow += static_cast<ptrdiff_t>(dstInfo.stride); |
| 1381 | } |
| 1382 | } |
| 1383 |
| 1384 | AndroidBitmap_unlockPixels(env, dst); |
| 1385 | AndroidBitmap_unlockPixels(env, src); |
| 1386 | return JNI_TRUE; |
| 1387 | } |
| 1388 |
| 1389 | // --------------------------------------------------------------------------- |
| 1390 | // Soft-Light blend — exact Android/Skia formula, simplified for α_dst = 1. |
| 1391 | // |
| 1392 | // General form (C values are pre-multiplied): |
| 1393 | // m = C_dst / α_dst |
| 1394 | // g = (16m² + 4m)*(m-1) + 7m if 4*C_dst <= α_dst (m <= 0.25) |
| 1395 | // = sqrt(m) - m otherwise |
| 1396 | // f = C_dst*(α_src + (2*C_src - α_src)*(1-m)) if 2*C_src <= α_src |
| 1397 | // = C_dst*α_src + α_dst*(2*C_src - α_src)*g otherwise |
| 1398 | // α_out = α_src + α_dst - α_src*α_dst |
| 1399 | // C_out = C_src/α_dst + C_dst/α_src + f |
| 1400 | // |
| 1401 | // Simplified for α_dst = 1 (guaranteed by caller). |
| 1402 | // Let cb = straight backdrop channel, cs = straight source channel, |
| 1403 | // a = α_src (color alpha, in [0,1]): |
| 1404 | // |
| 1405 | // m = cb |
| 1406 | // g = (16cb² + 4cb)*(cb-1) + 7cb if cb <= 0.25 |
| 1407 | // = sqrt(cb) - cb otherwise |
| 1408 | // f = cb*(a + (2*a*cs - a)*(1-cb)) if 2*a*cs <= a → cs <= 0.5 |
| 1409 | // = cb*a + (2*a*cs - a)*g otherwise |
| 1410 | // |
| 1411 | // result_straight = f/a (recover straight channel from pre-multiplied f) |
| 1412 | // |
| 1413 | // Both LUTs are built on the first call and reused across all subsequent calls. |
| 1414 | // --------------------------------------------------------------------------- |
| 1415 |
| 1416 | // g_sl_lut[cs_u8][cb_u8] -> soft-light result as uint8, for fully opaque color (a=1). |
| 1417 | // 256 * 256 = 64 KB — fits in L2 cache on modern ARM cores. |
| 1418 | static uint8_t g_sl_lut[256][256]; |
| 1419 |
| 1420 | // g_lerp_lut[alpha_u8][value_u8] -> floor(alpha * value / 255) |
| 1421 | // Used for branch-free integer lerp in the hot loop: |
| 1422 | // out = g_lerp_lut[alpha][blend] + g_lerp_lut[255 - alpha][cb] |
| 1423 | // 256 * 256 = 64 KB. |
| 1424 | static uint8_t g_lerp_lut[256][256]; |
| 1425 |
| 1426 | static std::once_flag g_lut_flag; |
| 1427 |
| 1428 | static void build_luts() { |
| 1429 | // lerp LUT: floor(a * v / 255) — intentional floor, not round. |
| 1430 | // This guarantees lerpA[x] + lerpInvA[x] <= 255 for any x and any alpha, |
| 1431 | // preventing uint8_t overflow when the two terms are summed in process_alpha. |
| 1432 | // |
| 1433 | // Proof: floor(a*x/255) + floor((255-a)*x/255) |
| 1434 | // <= a*x/255 + (255-a)*x/255 = x <= 255. |
| 1435 | for (int a = 0; a < 256; ++a) { |
| 1436 | for (int v = 0; v < 256; ++v) { |
| 1437 | g_lerp_lut[a][v] = static_cast<uint8_t>((a * v) / 255); |
| 1438 | } |
| 1439 | } |
| 1440 |
| 1441 | // Soft-light LUT for fully opaque color (α_src = 1, i.e. a = 1). |
| 1442 | // With a = 1: C_src = cs, so 2*C_src <= α_src becomes cs <= 0.5. |
| 1443 | // f = cb*(1 + (2*cs - 1)*(1-cb)) if cs <= 0.5 |
| 1444 | // = cb + (2*cs - 1)*g otherwise |
| 1445 | // result = f (already straight since a = 1) |
| 1446 | for (int cs_i = 0; cs_i < 256; ++cs_i) { |
| 1447 | const float cs = cs_i / 255.0f; |
| 1448 |
| 1449 | for (int cb_i = 0; cb_i < 256; ++cb_i) { |
| 1450 | const float cb = cb_i / 255.0f; |
| 1451 | float result; |
| 1452 |
| 1453 | if (cs <= 0.5f) { |
| 1454 | // f = cb * (α_src + (2*C_src - α_src)*(1 - m)) |
| 1455 | // = cb * (1 + (2*cs - 1)*(1 - cb)) |
| 1456 | result = cb * (1.0f + (2.0f * cs - 1.0f) * (1.0f - cb)); |
| 1457 | } else { |
| 1458 | // g = (16m² + 4m)*(m-1) + 7m, m = cb |
| 1459 | float g; |
| 1460 | if (cb <= 0.25f) { |
| 1461 | g = (16.0f * cb * cb + 4.0f * cb) * (cb - 1.0f) + 7.0f * cb; |
| 1462 | } else { |
| 1463 | g = sqrtf(cb) - cb; |
| 1464 | } |
| 1465 | // f = cb*α_src + α_dst*(2*C_src - α_src)*g |
| 1466 | // = cb + (2*cs - 1)*g (α_src = α_dst = 1) |
| 1467 | result = cb + (2.0f * cs - 1.0f) * g; |
| 1468 | } |
| 1469 |
| 1470 | // Clamp for float rounding safety. |
| 1471 | if (result < 0.0f) result = 0.0f; |
| 1472 | if (result > 1.0f) result = 1.0f; |
| 1473 |
| 1474 | g_sl_lut[cs_i][cb_i] = static_cast<uint8_t>(result * 255.0f + 0.5f); |
| 1475 | } |
| 1476 | } |
| 1477 | } |
| 1478 |
| 1479 | // --------------------------------------------------------------------------- |
| 1480 | // Three specialised hot loops, selected by colorA before entering the loop. |
| 1481 | // Alpha branching is lifted OUT of the loop — no branches inside iterations. |
| 1482 | // --------------------------------------------------------------------------- |
| 1483 |
| 1484 | // colorA == 0xFF: out[ch] = sl_lut[cs][cb] |
| 1485 | static void process_opaque( |
| 1486 | const uint8_t * __restrict__ inPx, |
| 1487 | uint8_t * __restrict__ outPx, |
| 1488 | uint32_t width, uint32_t height, |
| 1489 | uint32_t inStride, uint32_t outStride, |
| 1490 | uint8_t csR, uint8_t csG, uint8_t csB) |
| 1491 | { |
| 1492 | // LUT row pointers are fixed for a given color — load them once outside the loop. |
| 1493 | const uint8_t * __restrict__ slR = g_sl_lut[csR]; |
| 1494 | const uint8_t * __restrict__ slG = g_sl_lut[csG]; |
| 1495 | const uint8_t * __restrict__ slB = g_sl_lut[csB]; |
| 1496 |
| 1497 | for (uint32_t y = 0; y < height; ++y) { |
| 1498 | const uint8_t * __restrict__ src = inPx + y * inStride; |
| 1499 | uint8_t * __restrict__ dst = outPx + y * outStride; |
| 1500 | const uint8_t * const end = src + width * 4u; |
| 1501 |
| 1502 | while (src < end) { |
| 1503 | dst[0] = slR[src[0]]; |
| 1504 | dst[1] = slG[src[1]]; |
| 1505 | dst[2] = slB[src[2]]; |
| 1506 | dst[3] = 0xFF; |
| 1507 | src += 4; |
| 1508 | dst += 4; |
| 1509 | } |
| 1510 | } |
| 1511 | } |
| 1512 |
| 1513 | // colorA == 0x00: output is a copy of input with alpha forced to 0xFF. |
| 1514 | // (Input bitmap is guaranteed opaque, so the copy is a straight pixel copy.) |
| 1515 | static void process_transparent( |
| 1516 | const uint8_t * __restrict__ inPx, |
| 1517 | uint8_t * __restrict__ outPx, |
| 1518 | uint32_t width, uint32_t height, |
| 1519 | uint32_t inStride, uint32_t outStride) |
| 1520 | { |
| 1521 | if (inStride == width * 4u && outStride == width * 4u) { |
| 1522 | memcpy(outPx, inPx, width * height * 4u); |
| 1523 | } else { |
| 1524 | for (uint32_t y = 0; y < height; ++y) { |
| 1525 | memcpy(outPx + y * outStride, inPx + y * inStride, width * 4u); |
| 1526 | } |
| 1527 | } |
| 1528 | } |
| 1529 |
| 1530 | // 0 < colorA < 0xFF: |
| 1531 | // out[ch] = lerp_lut[colorA][sl[cs][cb]] + lerp_lut[255 - colorA][cb] |
| 1532 | // No floats, no branches inside the loop. |
| 1533 | static void process_alpha( |
| 1534 | const uint8_t * __restrict__ inPx, |
| 1535 | uint8_t * __restrict__ outPx, |
| 1536 | uint32_t width, uint32_t height, |
| 1537 | uint32_t inStride, uint32_t outStride, |
| 1538 | uint8_t csR, uint8_t csG, uint8_t csB, uint8_t colorA) |
| 1539 | { |
| 1540 | const uint8_t invA = static_cast<uint8_t>(255 - colorA); |
| 1541 |
| 1542 | const uint8_t * __restrict__ slR = g_sl_lut[csR]; |
| 1543 | const uint8_t * __restrict__ slG = g_sl_lut[csG]; |
| 1544 | const uint8_t * __restrict__ slB = g_sl_lut[csB]; |
| 1545 | const uint8_t * __restrict__ lerpA = g_lerp_lut[colorA]; |
| 1546 | const uint8_t * __restrict__ lerpInvA = g_lerp_lut[invA]; |
| 1547 |
| 1548 | for (uint32_t y = 0; y < height; ++y) { |
| 1549 | const uint8_t * __restrict__ src = inPx + y * inStride; |
| 1550 | uint8_t * __restrict__ dst = outPx + y * outStride; |
| 1551 | const uint8_t * const end = src + width * 4u; |
| 1552 |
| 1553 | while (src < end) { |
| 1554 | const uint8_t cbR = src[0]; |
| 1555 | const uint8_t cbG = src[1]; |
| 1556 | const uint8_t cbB = src[2]; |
| 1557 | dst[0] = lerpA[slR[cbR]] + lerpInvA[cbR]; |
| 1558 | dst[1] = lerpA[slG[cbG]] + lerpInvA[cbG]; |
| 1559 | dst[2] = lerpA[slB[cbB]] + lerpInvA[cbB]; |
| 1560 | dst[3] = 0xFF; |
| 1561 | src += 4; |
| 1562 | dst += 4; |
| 1563 | } |
| 1564 | } |
| 1565 | } |
| 1566 |
| 1567 | // --------------------------------------------------------------------------- |
| 1568 | // JNI entry point |
| 1569 | // |
| 1570 | // Kotlin: external fun applySoftLight(input: Bitmap, output: Bitmap, color: Int): Boolean [org.telegram.messenger.Utilities] |
| 1571 | // |
| 1572 | // color — Android packed ARGB (0xAARRGGBB), straight (non-premultiplied) alpha. |
| 1573 | // Returns true on success, false on error (size mismatch or unsupported format). |
| 1574 | // --------------------------------------------------------------------------- |
| 1575 | JNIEXPORT jboolean JNICALL |
| 1576 | Java_org_telegram_messenger_Utilities_applySoftLight( |
| 1577 | JNIEnv *env, |
| 1578 | jclass /*clazz*/, |
| 1579 | jobject inputBitmap, |
| 1580 | jobject outputBitmap, |
| 1581 | jint color) |
| 1582 | { |
| 1583 | std::call_once(g_lut_flag, build_luts); |
| 1584 |
| 1585 | if (__builtin_expect(env->IsSameObject(inputBitmap, outputBitmap), 0)) { |
| 1586 | return JNI_FALSE; |
| 1587 | } |
| 1588 |
| 1589 | AndroidBitmapInfo inInfo{}; |
| 1590 | AndroidBitmapInfo outInfo{}; |
| 1591 |
| 1592 | if (__builtin_expect( |
| 1593 | AndroidBitmap_getInfo(env, inputBitmap, &inInfo) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 1594 | AndroidBitmap_getInfo(env, outputBitmap, &outInfo) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1595 | 0)) { |
| 1596 | return JNI_FALSE; |
| 1597 | } |
| 1598 |
| 1599 | if (__builtin_expect( |
| 1600 | inInfo.width != outInfo.width || |
| 1601 | inInfo.height != outInfo.height || |
| 1602 | inInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || |
| 1603 | outInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || |
| 1604 | inInfo.width == 0 || |
| 1605 | inInfo.height == 0 || |
| 1606 | inInfo.stride < inInfo.width * 4u || |
| 1607 | outInfo.stride < outInfo.width * 4u, |
| 1608 | 0)) { |
| 1609 | return JNI_FALSE; |
| 1610 | } |
| 1611 |
| 1612 | void *inPixels = nullptr; |
| 1613 | void *outPixels = nullptr; |
| 1614 |
| 1615 | if (__builtin_expect( |
| 1616 | AndroidBitmap_lockPixels(env, inputBitmap, &inPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1617 | 0)) { |
| 1618 | return JNI_FALSE; |
| 1619 | } |
| 1620 | if (__builtin_expect( |
| 1621 | AndroidBitmap_lockPixels(env, outputBitmap, &outPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1622 | 0)) { |
| 1623 | AndroidBitmap_unlockPixels(env, inputBitmap); |
| 1624 | return JNI_FALSE; |
| 1625 | } |
| 1626 |
| 1627 | // Unpack Java color (0xAARRGGBB) into separate channels. |
| 1628 | const auto u = static_cast<uint32_t>(color); |
| 1629 | const uint8_t colorA = static_cast<uint8_t>(u >> 24); |
| 1630 | const uint8_t colorR = static_cast<uint8_t>(u >> 16); |
| 1631 | const uint8_t colorG = static_cast<uint8_t>(u >> 8); |
| 1632 | const uint8_t colorB = static_cast<uint8_t>(u); |
| 1633 |
| 1634 | const auto *src = static_cast<const uint8_t *>(inPixels); |
| 1635 | auto *dst = static_cast<uint8_t *>(outPixels); |
| 1636 | const uint32_t w = inInfo.width; |
| 1637 | const uint32_t h = inInfo.height; |
| 1638 | const uint32_t si = inInfo.stride; |
| 1639 | const uint32_t so = outInfo.stride; |
| 1640 |
| 1641 | // Dispatch before the loop so no alpha branching occurs inside it. |
| 1642 | if (colorA == 0xFF) { |
| 1643 | process_opaque(src, dst, w, h, si, so, colorR, colorG, colorB); |
| 1644 | } else if (colorA == 0x00) { |
| 1645 | process_transparent(src, dst, w, h, si, so); |
| 1646 | } else { |
| 1647 | process_alpha(src, dst, w, h, si, so, colorR, colorG, colorB, colorA); |
| 1648 | } |
| 1649 |
| 1650 | AndroidBitmap_unlockPixels(env, outputBitmap); |
| 1651 | AndroidBitmap_unlockPixels(env, inputBitmap); |
| 1652 | return JNI_TRUE; |
| 1653 | } |
| 1654 |
| 1655 | } |