| 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 <limits.h> |
| 13 | #include "libyuv/scale_argb.h" |
| 14 | #include <tgnet/FileLog.h> |
| 15 | #include <vector> |
| 16 | #include <algorithm> |
| 17 | #include "c_utils.h" |
| 18 |
| 19 | extern "C" { |
| 20 |
| 21 | static inline uint64_t getColors(const uint8_t *p) { |
| 22 | return p[0] + (p[1] << 16) + ((uint64_t) p[2] << 32) + ((uint64_t) p[3] << 48); |
| 23 | } |
| 24 |
| 25 | static inline uint64_t getColors565(const uint8_t *p) { |
| 26 | uint16_t *ps = (uint16_t *) p; |
| 27 | return ((((ps[0] & 0xF800) >> 11) * 255) / 31) + (((((ps[0] & 0x07E0) >> 5) * 255) / 63) << 16) + ((uint64_t)(((ps[0] & 0x001F) * 255) / 31) << 32); |
| 28 | } |
| 29 |
| 30 | static void fastBlurMore(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) { |
| 31 | const int32_t r1 = radius + 1; |
| 32 | const int32_t div = radius * 2 + 1; |
| 33 |
| 34 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 4) { |
| 35 | return; |
| 36 | } |
| 37 |
| 38 | uint64_t *rgb = new uint64_t[w * h]; |
| 39 | if (rgb == NULL) { |
| 40 | return; |
| 41 | } |
| 42 |
| 43 | int32_t x, y, i; |
| 44 |
| 45 | int32_t yw = 0; |
| 46 | const int32_t we = w - r1; |
| 47 | for (y = 0; y < h; y++) { |
| 48 | uint64_t cur = getColors(&pix[yw]); |
| 49 | uint64_t rgballsum = -radius * cur; |
| 50 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 51 |
| 52 | for (i = 1; i <= radius; i++) { |
| 53 | cur = getColors(&pix[yw + i * 4]); |
| 54 | rgbsum += cur * (r1 - i); |
| 55 | rgballsum += cur; |
| 56 | } |
| 57 |
| 58 | x = 0; |
| 59 |
| 60 | #define update(start, middle, end) \ |
| 61 | rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \ |
| 62 | rgballsum += getColors(&pix[yw + (start) * 4]) - 2 * getColors(&pix[yw + (middle) * 4]) + getColors(&pix[yw + (end) * 4]); \ |
| 63 | rgbsum += rgballsum; \ |
| 64 | x++; \ |
| 65 |
| 66 | while (x < r1) { |
| 67 | update (0, x, x + r1) |
| 68 | } |
| 69 | while (x < we) { |
| 70 | update (x - r1, x, x + r1) |
| 71 | } |
| 72 | while (x < w) { |
| 73 | update (x - r1, x, w - 1) |
| 74 | } |
| 75 | #undef update |
| 76 |
| 77 | yw += stride; |
| 78 | } |
| 79 |
| 80 | const int32_t he = h - r1; |
| 81 | for (x = 0; x < w; x++) { |
| 82 | uint64_t rgballsum = -radius * rgb[x]; |
| 83 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 84 | for (i = 1; i <= radius; i++) { |
| 85 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 86 | rgballsum += rgb[i * w + x]; |
| 87 | } |
| 88 |
| 89 | y = 0; |
| 90 | int32_t yi = x * 4; |
| 91 |
| 92 | #define update(start, middle, end) \ |
| 93 | int64_t res = rgbsum >> 6; \ |
| 94 | pix[yi] = res; \ |
| 95 | pix[yi + 1] = res >> 16; \ |
| 96 | pix[yi + 2] = res >> 32; \ |
| 97 | pix[yi + 3] = res >> 48; \ |
| 98 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 99 | rgbsum += rgballsum; \ |
| 100 | y++; \ |
| 101 | yi += stride; |
| 102 |
| 103 | while (y < r1) { |
| 104 | update (0, y, y + r1) |
| 105 | } |
| 106 | while (y < he) { |
| 107 | update (y - r1, y, y + r1) |
| 108 | } |
| 109 | while (y < h) { |
| 110 | update (y - r1, y, h - 1) |
| 111 | } |
| 112 | #undef update |
| 113 | } |
| 114 |
| 115 | delete[] rgb; |
| 116 | } |
| 117 |
| 118 | static void fastBlur(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) { |
| 119 | if (pix == nullptr) { |
| 120 | return; |
| 121 | } |
| 122 | const int32_t r1 = radius + 1; |
| 123 | const int32_t div = radius * 2 + 1; |
| 124 | int32_t shift; |
| 125 | if (radius == 1) { |
| 126 | shift = 2; |
| 127 | } else if (radius == 3) { |
| 128 | shift = 4; |
| 129 | } else if (radius == 7) { |
| 130 | shift = 6; |
| 131 | } else if (radius == 15) { |
| 132 | shift = 8; |
| 133 | } else { |
| 134 | return; |
| 135 | } |
| 136 |
| 137 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 4) { |
| 138 | return; |
| 139 | } |
| 140 |
| 141 | uint64_t *rgb = new uint64_t[w * h]; |
| 142 | if (rgb == nullptr) { |
| 143 | return; |
| 144 | } |
| 145 |
| 146 | int32_t x, y, i; |
| 147 |
| 148 | int32_t yw = 0; |
| 149 | const int32_t we = w - r1; |
| 150 | for (y = 0; y < h; y++) { |
| 151 | uint64_t cur = getColors(&pix[yw]); |
| 152 | uint64_t rgballsum = -radius * cur; |
| 153 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 154 |
| 155 | for (i = 1; i <= radius; i++) { |
| 156 | cur = getColors(&pix[yw + i * 4]); |
| 157 | rgbsum += cur * (r1 - i); |
| 158 | rgballsum += cur; |
| 159 | } |
| 160 |
| 161 | x = 0; |
| 162 |
| 163 | #define update(start, middle, end) \ |
| 164 | rgb[y * w + x] = (rgbsum >> shift) & 0x00FF00FF00FF00FFLL; \ |
| 165 | rgballsum += getColors(&pix[yw + (start) * 4]) - 2 * getColors(&pix[yw + (middle) * 4]) + getColors(&pix[yw + (end) * 4]); \ |
| 166 | rgbsum += rgballsum; \ |
| 167 | x++; \ |
| 168 |
| 169 | while (x < r1) { |
| 170 | update (0, x, x + r1) |
| 171 | } |
| 172 | while (x < we) { |
| 173 | update (x - r1, x, x + r1) |
| 174 | } |
| 175 | while (x < w) { |
| 176 | update (x - r1, x, w - 1) |
| 177 | } |
| 178 |
| 179 | #undef update |
| 180 |
| 181 | yw += stride; |
| 182 | } |
| 183 |
| 184 | const int32_t he = h - r1; |
| 185 | for (x = 0; x < w; x++) { |
| 186 | uint64_t rgballsum = -radius * rgb[x]; |
| 187 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 188 | for (i = 1; i <= radius; i++) { |
| 189 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 190 | rgballsum += rgb[i * w + x]; |
| 191 | } |
| 192 |
| 193 | y = 0; |
| 194 | int32_t yi = x * 4; |
| 195 |
| 196 | #define update(start, middle, end) \ |
| 197 | int64_t res = rgbsum >> shift; \ |
| 198 | pix[yi] = res; \ |
| 199 | pix[yi + 1] = res >> 16; \ |
| 200 | pix[yi + 2] = res >> 32; \ |
| 201 | pix[yi + 3] = res >> 48; \ |
| 202 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 203 | rgbsum += rgballsum; \ |
| 204 | y++; \ |
| 205 | yi += stride; |
| 206 |
| 207 | while (y < r1) { |
| 208 | update (0, y, y + r1) |
| 209 | } |
| 210 | while (y < he) { |
| 211 | update (y - r1, y, y + r1) |
| 212 | } |
| 213 | while (y < h) { |
| 214 | update (y - r1, y, h - 1) |
| 215 | } |
| 216 | #undef update |
| 217 | } |
| 218 |
| 219 | delete[] rgb; |
| 220 | } |
| 221 |
| 222 | static void fastBlurMore565(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) { |
| 223 | const int32_t r1 = radius + 1; |
| 224 | const int32_t div = radius * 2 + 1; |
| 225 |
| 226 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 2) { |
| 227 | return; |
| 228 | } |
| 229 |
| 230 | uint64_t *rgb = new uint64_t[w * h]; |
| 231 | if (rgb == NULL) { |
| 232 | return; |
| 233 | } |
| 234 |
| 235 | int32_t x, y, i; |
| 236 |
| 237 | int32_t yw = 0; |
| 238 | const int32_t we = w - r1; |
| 239 | for (y = 0; y < h; y++) { |
| 240 | uint64_t cur = getColors565(&pix[yw]); |
| 241 | uint64_t rgballsum = -radius * cur; |
| 242 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 243 |
| 244 | for (i = 1; i <= radius; i++) { |
| 245 | cur = getColors565(&pix[yw + i * 2]); |
| 246 | rgbsum += cur * (r1 - i); |
| 247 | rgballsum += cur; |
| 248 | } |
| 249 |
| 250 | x = 0; |
| 251 |
| 252 | #define update(start, middle, end) \ |
| 253 | rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \ |
| 254 | rgballsum += getColors565(&pix[yw + (start) * 2]) - 2 * getColors565(&pix[yw + (middle) * 2]) + getColors565(&pix[yw + (end) * 2]); \ |
| 255 | rgbsum += rgballsum; \ |
| 256 | x++; \ |
| 257 |
| 258 | while (x < r1) { |
| 259 | update (0, x, x + r1) |
| 260 | } |
| 261 | while (x < we) { |
| 262 | update (x - r1, x, x + r1) |
| 263 | } |
| 264 | while (x < w) { |
| 265 | update (x - r1, x, w - 1) |
| 266 | } |
| 267 | #undef update |
| 268 |
| 269 | yw += stride; |
| 270 | } |
| 271 |
| 272 | const int32_t he = h - r1; |
| 273 | for (x = 0; x < w; x++) { |
| 274 | uint64_t rgballsum = -radius * rgb[x]; |
| 275 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 276 | for (i = 1; i <= radius; i++) { |
| 277 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 278 | rgballsum += rgb[i * w + x]; |
| 279 | } |
| 280 |
| 281 | y = 0; |
| 282 | int32_t yi = x * 2; |
| 283 |
| 284 | #define update(start, middle, end) \ |
| 285 | int64_t res = rgbsum >> 6; \ |
| 286 | pix[yi] = ((res >> 13) & 0xe0) | ((res >> 35) & 0x1f); \ |
| 287 | pix[yi + 1] = (res & 0xf8) | ((res >> 21) & 0x7); \ |
| 288 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 289 | rgbsum += rgballsum; \ |
| 290 | y++; \ |
| 291 | yi += stride; |
| 292 |
| 293 | while (y < r1) { |
| 294 | update (0, y, y + r1) |
| 295 | } |
| 296 | while (y < he) { |
| 297 | update (y - r1, y, y + r1) |
| 298 | } |
| 299 | while (y < h) { |
| 300 | update (y - r1, y, h - 1) |
| 301 | } |
| 302 | #undef update |
| 303 | } |
| 304 |
| 305 | delete[] rgb; |
| 306 | } |
| 307 |
| 308 | static void fastBlur565(int32_t w, int32_t h, int32_t stride, uint8_t *pix, int32_t radius) { |
| 309 | if (pix == NULL) { |
| 310 | return; |
| 311 | } |
| 312 | const int32_t r1 = radius + 1; |
| 313 | const int32_t div = radius * 2 + 1; |
| 314 | int32_t shift; |
| 315 | if (radius == 1) { |
| 316 | shift = 2; |
| 317 | } else if (radius == 3) { |
| 318 | shift = 4; |
| 319 | } else if (radius == 7) { |
| 320 | shift = 6; |
| 321 | } else if (radius == 15) { |
| 322 | shift = 8; |
| 323 | } else { |
| 324 | return; |
| 325 | } |
| 326 |
| 327 | if (radius > 15 || div >= w || div >= h || w * h > 150 * 150 || stride > w * 2) { |
| 328 | return; |
| 329 | } |
| 330 |
| 331 | uint64_t *rgb = new uint64_t[w * h]; |
| 332 | if (rgb == NULL) { |
| 333 | return; |
| 334 | } |
| 335 |
| 336 | int32_t x, y, i; |
| 337 |
| 338 | int32_t yw = 0; |
| 339 | const int32_t we = w - r1; |
| 340 | for (y = 0; y < h; y++) { |
| 341 | uint64_t cur = getColors565(&pix[yw]); |
| 342 | uint64_t rgballsum = -radius * cur; |
| 343 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 344 |
| 345 | for (i = 1; i <= radius; i++) { |
| 346 | cur = getColors565(&pix[yw + i * 2]); |
| 347 | rgbsum += cur * (r1 - i); |
| 348 | rgballsum += cur; |
| 349 | } |
| 350 |
| 351 | x = 0; |
| 352 |
| 353 | #define update(start, middle, end) \ |
| 354 | rgb[y * w + x] = (rgbsum >> shift) & 0x00FF00FF00FF00FFLL; \ |
| 355 | rgballsum += getColors565(&pix[yw + (start) * 2]) - 2 * getColors565(&pix[yw + (middle) * 2]) + getColors565(&pix[yw + (end) * 2]); \ |
| 356 | rgbsum += rgballsum; \ |
| 357 | x++; |
| 358 |
| 359 | while (x < r1) { |
| 360 | update(0, x, x + r1) |
| 361 | } |
| 362 | while (x < we) { |
| 363 | update(x - r1, x, x + r1) |
| 364 | } |
| 365 | while (x < w) { |
| 366 | update(x - r1, x, w - 1) |
| 367 | } |
| 368 |
| 369 | #undef update |
| 370 |
| 371 | yw += stride; |
| 372 | } |
| 373 |
| 374 | const int32_t he = h - r1; |
| 375 | for (x = 0; x < w; x++) { |
| 376 | uint64_t rgballsum = -radius * rgb[x]; |
| 377 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 378 | for (i = 1; i <= radius; i++) { |
| 379 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 380 | rgballsum += rgb[i * w + x]; |
| 381 | } |
| 382 |
| 383 | y = 0; |
| 384 | int32_t yi = x * 2; |
| 385 |
| 386 | #define update(start, middle, end) \ |
| 387 | uint64_t res = rgbsum >> shift; \ |
| 388 | pix[yi] = ((res >> 13) & 0xe0) | ((res >> 35) & 0x1f); \ |
| 389 | pix[yi + 1] = (res & 0xf8) | ((res >> 21) & 0x7); \ |
| 390 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 391 | rgbsum += rgballsum; \ |
| 392 | y++; \ |
| 393 | yi += stride; |
| 394 |
| 395 | while (y < r1) { |
| 396 | update (0, y, y + r1) |
| 397 | } |
| 398 | while (y < he) { |
| 399 | update (y - r1, y, y + r1) |
| 400 | } |
| 401 | while (y < h) { |
| 402 | update (y - r1, y, h - 1) |
| 403 | } |
| 404 | #undef update |
| 405 | } |
| 406 |
| 407 | delete[] rgb; |
| 408 | } |
| 409 |
| 410 | JNIEXPORT int Java_org_telegram_messenger_Utilities_needInvert(JNIEnv *env, jclass clazz, jobject bitmap) { |
| 411 | if (!bitmap) { |
| 412 | return 0; |
| 413 | } |
| 414 |
| 415 | AndroidBitmapInfo info{}; |
| 416 | if (__builtin_expect(AndroidBitmap_getInfo(env, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS, 0)) { |
| 417 | return 0; |
| 418 | } |
| 419 | uint32_t width = info.width; |
| 420 | uint32_t height = info.height; |
| 421 | uint32_t stride = info.stride; |
| 422 |
| 423 | if (!width || !height || !stride || stride != width * 4 || width * height > 150 * 150) { |
| 424 | return 0; |
| 425 | } |
| 426 |
| 427 | void *pixels = nullptr; |
| 428 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { |
| 429 | return 0; |
| 430 | } |
| 431 | if (pixels == nullptr) { |
| 432 | return 0; |
| 433 | } |
| 434 | uint8_t *pix = (uint8_t *) pixels; |
| 435 |
| 436 | int32_t hasAlpha = 0; |
| 437 | float matching = 0; |
| 438 | float total = 0; |
| 439 | for (int32_t y = 0; y < height; y++) { |
| 440 | for (int32_t x = 0; x < width; x++) { |
| 441 | int32_t index = y * stride + x * 4; |
| 442 | uint8_t a = pix[index + 3]; |
| 443 | float alpha = a / 255.0f; |
| 444 |
| 445 | uint8_t r = (uint8_t)(pix[index] * alpha); |
| 446 | uint8_t g = (uint8_t)(pix[index + 1] * alpha); |
| 447 | uint8_t b = (uint8_t)(pix[index + 2] * alpha); |
| 448 |
| 449 | uint8_t cmax = (r > g) ? r : g; |
| 450 | if (b > cmax) { |
| 451 | cmax = b; |
| 452 | } |
| 453 | uint8_t cmin = (r < g) ? r : g; |
| 454 | if (b < cmin) { |
| 455 | cmin = b; |
| 456 | } |
| 457 |
| 458 | float saturation; |
| 459 | float brightness = ((float) cmax) / 255.0f; |
| 460 | if (cmax != 0) { |
| 461 | saturation = ((float) (cmax - cmin)) / ((float) cmax); |
| 462 | } else { |
| 463 | saturation = 0; |
| 464 | } |
| 465 |
| 466 | if (alpha < 1.0) { |
| 467 | hasAlpha = 1; |
| 468 | } |
| 469 |
| 470 | if (alpha > 0.0) { |
| 471 | total += 1; |
| 472 | if (saturation < 0.1f && brightness < 0.25f) { |
| 473 | matching += 1; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | } |
| 478 | AndroidBitmap_unlockPixels(env, bitmap); |
| 479 | return hasAlpha && matching / total > 0.85; |
| 480 | } |
| 481 |
| 482 | JNIEXPORT void Java_org_telegram_messenger_Utilities_blurBitmap(JNIEnv *env, jclass clazz, jobject bitmap, jint radius) { |
| 483 | if (!bitmap) { |
| 484 | return; |
| 485 | } |
| 486 |
| 487 | AndroidBitmapInfo info{}; |
| 488 | if (__builtin_expect(AndroidBitmap_getInfo(env, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS, 0)) { |
| 489 | return; |
| 490 | } |
| 491 | uint32_t width = info.width; |
| 492 | uint32_t height = info.height; |
| 493 | uint32_t stride = info.stride; |
| 494 |
| 495 | if (!width || !height || !stride) { |
| 496 | return; |
| 497 | } |
| 498 |
| 499 | void *pixels = nullptr; |
| 500 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { |
| 501 | return; |
| 502 | } |
| 503 | if (stride == width * 2) { |
| 504 | if (radius <= 3) { |
| 505 | fastBlur565(width, height, stride, (uint8_t *) pixels, radius); |
| 506 | } else { |
| 507 | fastBlurMore565(width, height, stride, (uint8_t *) pixels, radius); |
| 508 | } |
| 509 | } else { |
| 510 | if (radius <= 3) { |
| 511 | fastBlur(width, height, stride, (uint8_t *) pixels, radius); |
| 512 | } else { |
| 513 | fastBlurMore(width, height, stride, (uint8_t *) pixels, radius); |
| 514 | } |
| 515 | } |
| 516 | AndroidBitmap_unlockPixels(env, bitmap); |
| 517 | } |
| 518 |
| 519 | const uint32_t PGPhotoEnhanceHistogramBins = 256; |
| 520 | const uint32_t PGPhotoEnhanceSegments = 4; |
| 521 |
| 522 | JNIEXPORT void Java_org_telegram_messenger_Utilities_calcCDT(JNIEnv *env, jclass clazz, jobject hsvBuffer, jint width, jint height, jobject buffer, jobject calcBuffer) { |
| 523 | float imageWidth = width; |
| 524 | float imageHeight = height; |
| 525 | float _clipLimit = 1.25f; |
| 526 |
| 527 | uint32_t totalSegments = PGPhotoEnhanceSegments * PGPhotoEnhanceSegments; |
| 528 | uint32_t tileArea = (uint32_t) (floorf(imageWidth / PGPhotoEnhanceSegments) * floorf(imageHeight / PGPhotoEnhanceSegments)); |
| 529 | uint32_t clipLimit = (uint32_t) MAX(1, _clipLimit * tileArea / (float) PGPhotoEnhanceHistogramBins); |
| 530 | float scale = 255.0f / (float) tileArea; |
| 531 |
| 532 | unsigned char *bytes = (unsigned char *) env->GetDirectBufferAddress(hsvBuffer); |
| 533 | uint32_t *calcBytes = (uint32_t *) env->GetDirectBufferAddress(calcBuffer); |
| 534 | unsigned char *result = (unsigned char *) env->GetDirectBufferAddress(buffer); |
| 535 |
| 536 | uint32_t *cdfsMin = calcBytes; |
| 537 | calcBytes += totalSegments; |
| 538 | uint32_t *cdfsMax = calcBytes; |
| 539 | calcBytes += totalSegments; |
| 540 | uint32_t *cdfs = calcBytes; |
| 541 | calcBytes += totalSegments * PGPhotoEnhanceHistogramBins; |
| 542 | uint32_t *hist = calcBytes; |
| 543 | memset(hist, 0, sizeof(uint32_t) * totalSegments * PGPhotoEnhanceHistogramBins); |
| 544 |
| 545 | float xMul = PGPhotoEnhanceSegments / imageWidth; |
| 546 | float yMul = PGPhotoEnhanceSegments / imageHeight; |
| 547 |
| 548 | uint32_t i, j; |
| 549 |
| 550 | for (i = 0; i < imageHeight; i++) { |
| 551 | uint32_t yOffset = i * width * 4; |
| 552 | for (j = 0; j < imageWidth; j++) { |
| 553 | uint32_t index = j * 4 + yOffset; |
| 554 |
| 555 | uint32_t tx = (uint32_t)(j * xMul); |
| 556 | uint32_t ty = (uint32_t)(i * yMul); |
| 557 | uint32_t t = ty * PGPhotoEnhanceSegments + tx; |
| 558 |
| 559 | hist[t * PGPhotoEnhanceHistogramBins + bytes[index + 2]]++; |
| 560 | } |
| 561 | } |
| 562 |
| 563 | for (i = 0; i < totalSegments; i++) { |
| 564 | if (clipLimit > 0) { |
| 565 | uint32_t clipped = 0; |
| 566 | for (j = 0; j < PGPhotoEnhanceHistogramBins; j++) { |
| 567 | if (hist[i * PGPhotoEnhanceHistogramBins + j] > clipLimit) { |
| 568 | clipped += hist[i * PGPhotoEnhanceHistogramBins + j] - clipLimit; |
| 569 | hist[i * PGPhotoEnhanceHistogramBins + j] = clipLimit; |
| 570 | } |
| 571 | } |
| 572 |
| 573 | uint32_t redistBatch = clipped / PGPhotoEnhanceHistogramBins; |
| 574 | uint32_t residual = clipped - redistBatch * PGPhotoEnhanceHistogramBins; |
| 575 |
| 576 | for (j = 0; j < PGPhotoEnhanceHistogramBins; j++) { |
| 577 | hist[i * PGPhotoEnhanceHistogramBins + j] += redistBatch; |
| 578 | if (j < residual) { |
| 579 | hist[i * PGPhotoEnhanceHistogramBins + j]++; |
| 580 | } |
| 581 | } |
| 582 | } |
| 583 | memcpy(cdfs + i * PGPhotoEnhanceHistogramBins, hist + i * PGPhotoEnhanceHistogramBins, PGPhotoEnhanceHistogramBins * sizeof(uint32_t)); |
| 584 |
| 585 | uint32_t hMin = PGPhotoEnhanceHistogramBins - 1; |
| 586 | for (j = 0; j < hMin; ++j) { |
| 587 | if (cdfs[i * PGPhotoEnhanceHistogramBins + j] != 0) { |
| 588 | hMin = j; |
| 589 | } |
| 590 | } |
| 591 |
| 592 | uint32_t cdf = 0; |
| 593 | for (j = hMin; j < PGPhotoEnhanceHistogramBins; j++) { |
| 594 | cdf += cdfs[i * PGPhotoEnhanceHistogramBins + j]; |
| 595 | cdfs[i * PGPhotoEnhanceHistogramBins + j] = (uint8_t) MIN(255, cdf * scale); |
| 596 | } |
| 597 |
| 598 | cdfsMin[i] = cdfs[i * PGPhotoEnhanceHistogramBins + hMin]; |
| 599 | cdfsMax[i] = cdfs[i * PGPhotoEnhanceHistogramBins + PGPhotoEnhanceHistogramBins - 1]; |
| 600 | } |
| 601 |
| 602 | for (j = 0; j < totalSegments; j++) { |
| 603 | uint32_t yOffset = j * PGPhotoEnhanceHistogramBins * 4; |
| 604 | for (i = 0; i < PGPhotoEnhanceHistogramBins; i++) { |
| 605 | uint32_t index = i * 4 + yOffset; |
| 606 | result[index] = (uint8_t) cdfs[j * PGPhotoEnhanceHistogramBins + i]; |
| 607 | result[index + 1] = (uint8_t) cdfsMin[j]; |
| 608 | result[index + 2] = (uint8_t) cdfsMax[j]; |
| 609 | result[index + 3] = 255; |
| 610 | } |
| 611 | } |
| 612 | } |
| 613 |
| 614 | #define SQUARE(i) ((i)*(i)) |
| 615 |
| 616 | inline static void zeroClearInt(int *p, size_t count) { |
| 617 | memset(p, 0, sizeof(int) * count); |
| 618 | } |
| 619 |
| 620 | JNIEXPORT void Java_org_telegram_messenger_Utilities_stackBlurBitmap(JNIEnv *env, jclass clazz, jobject bitmap, jint radius) { |
| 621 | if (radius < 1) { |
| 622 | return; |
| 623 | } |
| 624 |
| 625 | AndroidBitmapInfo info; |
| 626 | if (AndroidBitmap_getInfo(env, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 627 | return; |
| 628 | } |
| 629 | if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { |
| 630 | return; |
| 631 | } |
| 632 |
| 633 | int w = info.width; |
| 634 | int h = info.height; |
| 635 | int stride = info.stride; |
| 636 |
| 637 | unsigned char *pixels = nullptr; |
| 638 | AndroidBitmap_lockPixels(env, bitmap, (void **) &pixels); |
| 639 | if (!pixels) { |
| 640 | return; |
| 641 | } |
| 642 | // Constants |
| 643 | //const int radius = (int)inradius; // Transform unsigned into signed for further operations |
| 644 | const int wm = w - 1; |
| 645 | const int hm = h - 1; |
| 646 | const int wh = w * h; |
| 647 | const int div = radius + radius + 1; |
| 648 | const int r1 = radius + 1; |
| 649 | const int divsum = SQUARE((div + 1) >> 1); |
| 650 |
| 651 | // Small buffers |
| 652 | int stack[div * 4]; |
| 653 | zeroClearInt(stack, div * 4); |
| 654 |
| 655 | int vmin[MAX(w, h)]; |
| 656 | zeroClearInt(vmin, MAX(w, h)); |
| 657 |
| 658 | // Large buffers |
| 659 | int *r = new int[wh]; |
| 660 | int *g = new int[wh]; |
| 661 | int *b = new int[wh]; |
| 662 | int *a = new int[wh]; |
| 663 | zeroClearInt(r, wh); |
| 664 | zeroClearInt(g, wh); |
| 665 | zeroClearInt(b, wh); |
| 666 | zeroClearInt(a, wh); |
| 667 |
| 668 | const size_t dvcount = 256 * divsum; |
| 669 | int *dv = new int[dvcount]; |
| 670 | int i; |
| 671 | for (i = 0; (size_t) i < dvcount; i++) { |
| 672 | dv[i] = (i / divsum); |
| 673 | } |
| 674 |
| 675 | // Variables |
| 676 | int x, y; |
| 677 | int *sir; |
| 678 | int routsum, goutsum, boutsum, aoutsum; |
| 679 | int rinsum, ginsum, binsum, ainsum; |
| 680 | int rsum, gsum, bsum, asum, p, yp; |
| 681 | int stackpointer; |
| 682 | int stackstart; |
| 683 | int rbs; |
| 684 |
| 685 | int yw = 0, yi = 0; |
| 686 | for (y = 0; y < h; y++) { |
| 687 | ainsum = aoutsum = asum = rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; |
| 688 |
| 689 | for (i = -radius; i <= radius; i++) { |
| 690 | sir = &stack[(i + radius) * 4]; |
| 691 | int offset = (y * stride + (MIN(wm, MAX(i, 0))) * 4); |
| 692 | sir[0] = pixels[offset]; |
| 693 | sir[1] = pixels[offset + 1]; |
| 694 | sir[2] = pixels[offset + 2]; |
| 695 | sir[3] = pixels[offset + 3]; |
| 696 |
| 697 | rbs = r1 - abs(i); |
| 698 | rsum += sir[0] * rbs; |
| 699 | gsum += sir[1] * rbs; |
| 700 | bsum += sir[2] * rbs; |
| 701 | asum += sir[3] * rbs; |
| 702 | if (i > 0) { |
| 703 | rinsum += sir[0]; |
| 704 | ginsum += sir[1]; |
| 705 | binsum += sir[2]; |
| 706 | ainsum += sir[3]; |
| 707 | } else { |
| 708 | routsum += sir[0]; |
| 709 | goutsum += sir[1]; |
| 710 | boutsum += sir[2]; |
| 711 | aoutsum += sir[3]; |
| 712 | } |
| 713 | } |
| 714 | stackpointer = radius; |
| 715 |
| 716 | for (x = 0; x < w; x++) { |
| 717 | r[yi] = dv[rsum]; |
| 718 | g[yi] = dv[gsum]; |
| 719 | b[yi] = dv[bsum]; |
| 720 | a[yi] = dv[asum]; |
| 721 |
| 722 | rsum -= routsum; |
| 723 | gsum -= goutsum; |
| 724 | bsum -= boutsum; |
| 725 | asum -= aoutsum; |
| 726 |
| 727 | stackstart = stackpointer - radius + div; |
| 728 | sir = &stack[(stackstart % div) * 4]; |
| 729 |
| 730 | routsum -= sir[0]; |
| 731 | goutsum -= sir[1]; |
| 732 | boutsum -= sir[2]; |
| 733 | aoutsum -= sir[3]; |
| 734 |
| 735 | if (y == 0) { |
| 736 | vmin[x] = MIN(x + radius + 1, wm); |
| 737 | } |
| 738 |
| 739 | int offset = (y * stride + vmin[x] * 4); |
| 740 | sir[0] = pixels[offset]; |
| 741 | sir[1] = pixels[offset + 1]; |
| 742 | sir[2] = pixels[offset + 2]; |
| 743 | sir[3] = pixels[offset + 3]; |
| 744 | rinsum += sir[0]; |
| 745 | ginsum += sir[1]; |
| 746 | binsum += sir[2]; |
| 747 | ainsum += sir[3]; |
| 748 |
| 749 | rsum += rinsum; |
| 750 | gsum += ginsum; |
| 751 | bsum += binsum; |
| 752 | asum += ainsum; |
| 753 |
| 754 | stackpointer = (stackpointer + 1) % div; |
| 755 | sir = &stack[(stackpointer % div) * 4]; |
| 756 |
| 757 | routsum += sir[0]; |
| 758 | goutsum += sir[1]; |
| 759 | boutsum += sir[2]; |
| 760 | aoutsum += sir[3]; |
| 761 |
| 762 | rinsum -= sir[0]; |
| 763 | ginsum -= sir[1]; |
| 764 | binsum -= sir[2]; |
| 765 | ainsum -= sir[3]; |
| 766 |
| 767 | yi++; |
| 768 | } |
| 769 | yw += w; |
| 770 | } |
| 771 |
| 772 | for (x = 0; x < w; x++) { |
| 773 | ainsum = aoutsum = asum = rinsum = ginsum = binsum = routsum = goutsum = boutsum = rsum = gsum = bsum = 0; |
| 774 | yp = -radius * w; |
| 775 | for (i = -radius; i <= radius; i++) { |
| 776 | yi = MAX(0, yp) + x; |
| 777 |
| 778 | sir = &stack[(i + radius) * 4]; |
| 779 |
| 780 | sir[0] = r[yi]; |
| 781 | sir[1] = g[yi]; |
| 782 | sir[2] = b[yi]; |
| 783 | sir[3] = a[yi]; |
| 784 |
| 785 | rbs = r1 - abs(i); |
| 786 |
| 787 | rsum += r[yi] * rbs; |
| 788 | gsum += g[yi] * rbs; |
| 789 | bsum += b[yi] * rbs; |
| 790 | asum += a[yi] * rbs; |
| 791 |
| 792 | if (i > 0) { |
| 793 | rinsum += sir[0]; |
| 794 | ginsum += sir[1]; |
| 795 | binsum += sir[2]; |
| 796 | ainsum += sir[3]; |
| 797 | } else { |
| 798 | routsum += sir[0]; |
| 799 | goutsum += sir[1]; |
| 800 | boutsum += sir[2]; |
| 801 | aoutsum += sir[3]; |
| 802 | } |
| 803 |
| 804 | if (i < hm) { |
| 805 | yp += w; |
| 806 | } |
| 807 | } |
| 808 | stackpointer = radius; |
| 809 | for (y = 0; y < h; y++) { |
| 810 | int offset = stride * y + x * 4; |
| 811 | pixels[offset] = dv[rsum]; |
| 812 | pixels[offset + 1] = dv[gsum]; |
| 813 | pixels[offset + 2] = dv[bsum]; |
| 814 | pixels[offset + 3] = dv[asum]; |
| 815 | rsum -= routsum; |
| 816 | gsum -= goutsum; |
| 817 | bsum -= boutsum; |
| 818 | asum -= aoutsum; |
| 819 |
| 820 | stackstart = stackpointer - radius + div; |
| 821 | sir = &stack[(stackstart % div) * 4]; |
| 822 |
| 823 | routsum -= sir[0]; |
| 824 | goutsum -= sir[1]; |
| 825 | boutsum -= sir[2]; |
| 826 | aoutsum -= sir[3]; |
| 827 |
| 828 | if (x == 0) { |
| 829 | vmin[y] = (MIN(y + r1, hm)) * w; |
| 830 | } |
| 831 | p = x + vmin[y]; |
| 832 |
| 833 | sir[0] = r[p]; |
| 834 | sir[1] = g[p]; |
| 835 | sir[2] = b[p]; |
| 836 | sir[3] = a[p]; |
| 837 |
| 838 | rinsum += sir[0]; |
| 839 | ginsum += sir[1]; |
| 840 | binsum += sir[2]; |
| 841 | ainsum += sir[3]; |
| 842 |
| 843 | rsum += rinsum; |
| 844 | gsum += ginsum; |
| 845 | bsum += binsum; |
| 846 | asum += ainsum; |
| 847 |
| 848 | stackpointer = (stackpointer + 1) % div; |
| 849 | sir = &stack[stackpointer * 4]; |
| 850 |
| 851 | routsum += sir[0]; |
| 852 | goutsum += sir[1]; |
| 853 | boutsum += sir[2]; |
| 854 | aoutsum += sir[3]; |
| 855 |
| 856 | rinsum -= sir[0]; |
| 857 | ginsum -= sir[1]; |
| 858 | binsum -= sir[2]; |
| 859 | ainsum -= sir[3]; |
| 860 |
| 861 | yi += w; |
| 862 | } |
| 863 | } |
| 864 |
| 865 | delete[] r; |
| 866 | delete[] g; |
| 867 | delete[] b; |
| 868 | delete[] a; |
| 869 | delete[] dv; |
| 870 | AndroidBitmap_unlockPixels(env, bitmap); |
| 871 | } |
| 872 |
| 873 | JNIEXPORT jboolean JNICALL Java_org_telegram_messenger_Utilities_drawDitheredGradient(JNIEnv *env, jclass clazz, jobject bitmap, jintArray colors, jint startX, jint startY, jint endX, jint endY) { |
| 874 | AndroidBitmapInfo info; |
| 875 | void *pixelsBuffer; |
| 876 | int reason; |
| 877 |
| 878 | if ((reason = AndroidBitmap_getInfo(env, bitmap, &info)) != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 879 | return JNI_FALSE; |
| 880 | } |
| 881 |
| 882 | if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { |
| 883 | return JNI_FALSE; |
| 884 | } |
| 885 |
| 886 | if ((reason = AndroidBitmap_lockPixels(env, bitmap, &pixelsBuffer)) != ANDROID_BITMAP_RESULT_SUCCESS) { |
| 887 | return JNI_FALSE; |
| 888 | } |
| 889 |
| 890 | uint8_t i, j, n; |
| 891 |
| 892 | // gradient colors extracting |
| 893 | jint *colorsBuffer = env->GetIntArrayElements(colors, 0); |
| 894 | uint8_t *colorsComponents = (uint8_t *) colorsBuffer; |
| 895 | float colorsF[4][2]; |
| 896 | for (i = 0; i < 4; i++) { |
| 897 | // swap red and green channels |
| 898 | n = (uint8_t) (i == 0 ? 2 : (i == 2 ? 0 : i)); |
| 899 | for (j = 0; j < 2; j++) { |
| 900 | colorsF[n][j] = colorsComponents[j * 4 + i] / 255.F; |
| 901 | } |
| 902 | } |
| 903 | env->ReleaseIntArrayElements(colors, colorsBuffer, JNI_ABORT); |
| 904 |
| 905 | // gradient vector |
| 906 | const int32_t vx = endX - startX; |
| 907 | const int32_t vy = endY - startY; |
| 908 | const float vSquaredMag = vx * vx + vy * vy; |
| 909 |
| 910 | float noise, fraction, error, componentF; |
| 911 | float *pixelsComponentsF = new float[info.height * info.stride * 4]; |
| 912 | memset(pixelsComponentsF, 0, info.height * info.stride * 4 * sizeof(float)); |
| 913 | uint8_t * bitmapPixelsComponents = (uint8_t * ) |
| 914 | pixelsBuffer; |
| 915 |
| 916 | int32_t x, y; |
| 917 | int32_t offset; |
| 918 | int32_t position; |
| 919 | for (y = 0; y < info.height; y++) { |
| 920 | offset = y * info.stride; |
| 921 | for (x = 0; x < info.width; x++) { |
| 922 | // triangular probability density function dither noise |
| 923 | noise = (rand() - rand()) / 255.F / RAND_MAX; |
| 924 |
| 925 | // alpha channel |
| 926 | bitmapPixelsComponents[offset + x * 4 + 3] = 255; |
| 927 |
| 928 | for (i = 0; i < 3; i++) { |
| 929 | position = offset + x * 4 + i; |
| 930 | fraction = (vx * (x - startX) + vy * (y - startY)) / vSquaredMag; |
| 931 |
| 932 | // gradient interpolation and noise |
| 933 | pixelsComponentsF[position] += colorsF[i][0] + fraction * (colorsF[i][1] - colorsF[i][0]) + noise; |
| 934 |
| 935 | // clamp |
| 936 | if (pixelsComponentsF[position] > 1.F) { |
| 937 | pixelsComponentsF[position] = 1.F; |
| 938 | } else if (pixelsComponentsF[position] < 0.F) { |
| 939 | pixelsComponentsF[position] = 0.F; |
| 940 | } |
| 941 |
| 942 | // draw |
| 943 | componentF = roundf(pixelsComponentsF[position] * 255.F); |
| 944 | bitmapPixelsComponents[position] = (uint8_t) |
| 945 | componentF; |
| 946 |
| 947 | // floyd-steinberg dithering |
| 948 | error = pixelsComponentsF[position] - componentF / 255.F; |
| 949 | if (x + 1 < info.width) { |
| 950 | pixelsComponentsF[position + 4] += error * 7.F / 16.F; |
| 951 | if (y + 1 < info.height) { |
| 952 | pixelsComponentsF[position + info.height + 4] += error * 1.F / 16.F; |
| 953 | } |
| 954 | } |
| 955 | if (y + 1 < info.height) { |
| 956 | pixelsComponentsF[position + info.height] += error * 5.F / 16.F; |
| 957 | if (x - 1 >= 0) { |
| 958 | pixelsComponentsF[position + info.height - 4] += error * 3.F / 16.F; |
| 959 | } |
| 960 | } |
| 961 | } |
| 962 | } |
| 963 | } |
| 964 |
| 965 | delete[] pixelsComponentsF; |
| 966 |
| 967 | AndroidBitmap_unlockPixels(env, bitmap); |
| 968 | return JNI_TRUE; |
| 969 | } |
| 970 |
| 971 | //JNIEXPORT jint Java_org_telegram_messenger_Utilities_saveProgressiveJpeg(JNIEnv *env, jclass clazz, jobject bitmap, jint width, jint height, jint stride, jint quality, jstring path) { |
| 972 | // if (!bitmap || !path || !width || !height || !stride || stride != width * 4) { |
| 973 | // return 0; |
| 974 | // } |
| 975 | // void *pixels = 0; |
| 976 | // if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { |
| 977 | // return 0; |
| 978 | // } |
| 979 | // if (pixels == NULL) { |
| 980 | // return 0; |
| 981 | // } |
| 982 | // tjhandle handle = 0; |
| 983 | // if ((handle = tjInitCompress()) == NULL) { |
| 984 | // return 0; |
| 985 | // } |
| 986 | // const char *pathStr = env->GetStringUTFChars(path, 0); |
| 987 | // if (pathStr == NULL) { |
| 988 | // return 0; |
| 989 | // } |
| 990 | // std::string filePath = std::string(pathStr); |
| 991 | // if (pathStr != 0) { |
| 992 | // env->ReleaseStringUTFChars(path, pathStr); |
| 993 | // } |
| 994 | // |
| 995 | // const char *enabledValue = "1"; |
| 996 | // const char *disabledValue = "0"; |
| 997 | // setenv("TJ_OPTIMIZE", enabledValue, 1); |
| 998 | // setenv("TJ_ARITHMETIC", disabledValue, 1); |
| 999 | // setenv("TJ_PROGRESSIVE", enabledValue, 1); |
| 1000 | // setenv("TJ_REVERT", enabledValue, 1); |
| 1001 | // |
| 1002 | // TJSAMP jpegSubsamp = TJSAMP::TJSAMP_420; |
| 1003 | // jint buffSize = (jint) tjBufSize(width, height, jpegSubsamp); |
| 1004 | // unsigned char *jpegBuf = new unsigned char[buffSize]; |
| 1005 | // unsigned char *srcBuf = (unsigned char *) pixels; |
| 1006 | // |
| 1007 | // int pf = org_libjpegturbo_turbojpeg_TJ_PF_RGBA; |
| 1008 | // |
| 1009 | // jsize actualPitch = width * tjPixelSize[pf]; |
| 1010 | // jsize arraySize = (height - 1) * actualPitch + (width) * tjPixelSize[pf]; |
| 1011 | // unsigned long jpegSize = tjBufSize(width, height, jpegSubsamp); |
| 1012 | // |
| 1013 | // if (tjCompress2(handle, srcBuf, width, stride, height, pf, &jpegBuf, &jpegSize, jpegSubsamp, quality, TJFLAG_ACCURATEDCT | TJFLAG_PROGRESSIVE | TJFLAG_NOREALLOC) == 0) { |
| 1014 | // FILE *f = fopen(filePath.c_str(), "wb"); |
| 1015 | // if (f && fwrite(jpegBuf, sizeof(unsigned char), jpegSize, f) == jpegSize) { |
| 1016 | // fflush(f); |
| 1017 | // fsync(fileno(f)); |
| 1018 | // } else { |
| 1019 | // jpegSize = -1; |
| 1020 | // } |
| 1021 | // fclose(f); |
| 1022 | // } else { |
| 1023 | // jpegSize = -1; |
| 1024 | // } |
| 1025 | // delete[] jpegBuf; |
| 1026 | // tjDestroy(handle); |
| 1027 | // AndroidBitmap_unlockPixels(env, bitmap); |
| 1028 | // return jpegSize; |
| 1029 | // |
| 1030 | // /*struct jpeg_compress_struct cinfo; |
| 1031 | // struct jpeg_error_mgr jerr; |
| 1032 | // cinfo.err = jpeg_std_error(&jerr); |
| 1033 | // jpeg_create_compress(&cinfo); |
| 1034 | // |
| 1035 | // const char *pathStr = env->GetStringUTFChars(path, 0); |
| 1036 | // std::string filePath = std::string(pathStr); |
| 1037 | // if (pathStr != 0) { |
| 1038 | // env->ReleaseStringUTFChars(path, pathStr); |
| 1039 | // } |
| 1040 | // |
| 1041 | // uint8_t *outBuffer = NULL; |
| 1042 | // unsigned long outSize = 0; |
| 1043 | // jpeg_mem_dest(&cinfo, &outBuffer, &outSize); |
| 1044 | // unsigned char *srcBuf = (unsigned char *) pixels; |
| 1045 | // |
| 1046 | // cinfo.image_width = (uint32_t) width; |
| 1047 | // cinfo.image_height = (uint32_t) height; |
| 1048 | // cinfo.input_components = 4; |
| 1049 | // cinfo.in_color_space = JCS_EXT_RGBA; |
| 1050 | // jpeg_c_set_int_param(&cinfo, JINT_COMPRESS_PROFILE, JCP_FASTEST); |
| 1051 | // jpeg_set_defaults(&cinfo); |
| 1052 | // cinfo.arith_code = FALSE; |
| 1053 | // cinfo.dct_method = JDCT_ISLOW; |
| 1054 | // cinfo.optimize_coding = TRUE; |
| 1055 | // jpeg_set_quality(&cinfo, 78, 1); |
| 1056 | // jpeg_simple_progression(&cinfo); |
| 1057 | // jpeg_start_compress(&cinfo, 1); |
| 1058 | // |
| 1059 | // JSAMPROW rowPointer[1]; |
| 1060 | // while (cinfo.next_scanline < cinfo.image_height) { |
| 1061 | // rowPointer[0] = (JSAMPROW) (srcBuf + cinfo.next_scanline * stride); |
| 1062 | // jpeg_write_scanlines(&cinfo, rowPointer, 1); |
| 1063 | // } |
| 1064 | // |
| 1065 | // jpeg_finish_compress(&cinfo); |
| 1066 | // |
| 1067 | // FILE *f = fopen(filePath.c_str(), "wb"); |
| 1068 | // if (f && fwrite(outBuffer, sizeof(uint8_t), outSize, f) == outSize) { |
| 1069 | // fflush(f); |
| 1070 | // fsync(fileno(f)); |
| 1071 | // } |
| 1072 | // fclose(f); |
| 1073 | // |
| 1074 | // jpeg_destroy_compress(&cinfo); |
| 1075 | // return outSize;*/ |
| 1076 | //} |
| 1077 |
| 1078 | std::vector<std::pair<float, float>> gatherPositions(std::vector<std::pair<float, float>> list, int phase) { |
| 1079 | std::vector<std::pair<float, float>> result(4); |
| 1080 | for (int i = 0; i < 4; i++) { |
| 1081 | int pos = phase + i * 2; |
| 1082 | while (pos >= 8) { |
| 1083 | pos -= 8; |
| 1084 | } |
| 1085 | result[i] = list[pos]; |
| 1086 | result[i].second = 1.0f - result[i].second; |
| 1087 | } |
| 1088 | return result; |
| 1089 | } |
| 1090 |
| 1091 | thread_local static float *pixelCache = nullptr; |
| 1092 | thread_local static int pixelCacheSize = 0; |
| 1093 |
| 1094 | JNIEXPORT void Java_org_telegram_messenger_Utilities_generateGradient(JNIEnv *env, jclass clazz, jobject bitmap, jint phase, jfloat progress, jintArray colors) { |
| 1095 | if (!bitmap) { |
| 1096 | return; |
| 1097 | } |
| 1098 |
| 1099 | AndroidBitmapInfo info{}; |
| 1100 | if (__builtin_expect(AndroidBitmap_getInfo(env, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS, 0)) { |
| 1101 | return; |
| 1102 | } |
| 1103 | uint32_t width = info.width; |
| 1104 | uint32_t height = info.height; |
| 1105 | uint32_t stride = info.stride; |
| 1106 |
| 1107 | if (!width || !height) { |
| 1108 | return; |
| 1109 | } |
| 1110 |
| 1111 | uint8_t *pixels = nullptr; |
| 1112 | if (AndroidBitmap_lockPixels(env, bitmap, (void **) &pixels) < 0) { |
| 1113 | return; |
| 1114 | } |
| 1115 |
| 1116 | std::vector<std::pair<float, float>> positions{ |
| 1117 | {0.80f, 0.10f}, |
| 1118 | {0.60f, 0.20f}, |
| 1119 | {0.35f, 0.25f}, |
| 1120 | {0.25f, 0.60f}, |
| 1121 | {0.20f, 0.90f}, |
| 1122 | {0.40f, 0.80f}, |
| 1123 | {0.65f, 0.75f}, |
| 1124 | {0.75f, 0.40f} |
| 1125 | }; |
| 1126 |
| 1127 | int32_t previousPhase = phase + 1; |
| 1128 | if (previousPhase > 7) { |
| 1129 | previousPhase = 0; |
| 1130 | } |
| 1131 | std::vector<std::pair<float, float>> previous = gatherPositions(positions, previousPhase); |
| 1132 | std::vector<std::pair<float, float>> current = gatherPositions(positions, phase); |
| 1133 |
| 1134 | auto colorsArray = (uint8_t *) env->GetIntArrayElements(colors, nullptr); |
| 1135 | float *newPixelCache = nullptr; |
| 1136 |
| 1137 | if (width * height != pixelCacheSize && pixelCache != nullptr) { |
| 1138 | delete[] pixelCache; |
| 1139 | pixelCache = nullptr; |
| 1140 | } |
| 1141 | pixelCacheSize = width * height; |
| 1142 |
| 1143 | if (pixelCache == nullptr) { |
| 1144 | newPixelCache = new float[width * height * 2]; |
| 1145 | } |
| 1146 | float directPixelY; |
| 1147 | float centerDistanceY; |
| 1148 | float centerDistanceY2; |
| 1149 | int32_t colorsCount = colorsArray[12] == 0 && colorsArray[13] == 0 && colorsArray[14] == 0 && colorsArray[15] == 0 ? 3 : 4; |
| 1150 |
| 1151 | for (int y = 0; y < height; y++) { |
| 1152 | if (pixelCache == nullptr) { |
| 1153 | directPixelY = (float) y / (float) height; |
| 1154 | centerDistanceY = directPixelY - 0.5f; |
| 1155 | centerDistanceY2 = centerDistanceY * centerDistanceY; |
| 1156 | } |
| 1157 | uint32_t offset = y * stride; |
| 1158 | for (int x = 0; x < width; x++) { |
| 1159 | float pixelX; |
| 1160 | float pixelY; |
| 1161 | if (pixelCache != nullptr) { |
| 1162 | pixelX = pixelCache[(y * width + x) * 2]; |
| 1163 | pixelY = pixelCache[(y * width + x) * 2 + 1]; |
| 1164 | } else { |
| 1165 | float directPixelX = (float) x / (float) width; |
| 1166 |
| 1167 | float centerDistanceX = directPixelX - 0.5f; |
| 1168 | float centerDistance = sqrtf(centerDistanceX * centerDistanceX + centerDistanceY2); |
| 1169 |
| 1170 | float swirlFactor = 0.35f * centerDistance; |
| 1171 | float theta = swirlFactor * swirlFactor * 0.8f * 8.0f; |
| 1172 | float sinTheta = sinf(theta); |
| 1173 | float cosTheta = cosf(theta); |
| 1174 |
| 1175 | pixelX = newPixelCache[(y * width + x) * 2] = std::max(0.0f, std::min(1.0f, 0.5f + centerDistanceX * cosTheta - centerDistanceY * sinTheta)); |
| 1176 | pixelY = newPixelCache[(y * width + x) * 2 + 1] = std::max(0.0f, std::min(1.0f, 0.5f + centerDistanceX * sinTheta + centerDistanceY * cosTheta)); |
| 1177 | } |
| 1178 |
| 1179 | float distanceSum = 0.0f; |
| 1180 |
| 1181 | float r = 0.0f; |
| 1182 | float g = 0.0f; |
| 1183 | float b = 0.0f; |
| 1184 |
| 1185 | for (int i = 0; i < colorsCount; i++) { |
| 1186 | float colorX = previous[i].first + (current[i].first - previous[i].first) * progress; |
| 1187 | float colorY = previous[i].second + (current[i].second - previous[i].second) * progress; |
| 1188 |
| 1189 | float distanceX = pixelX - colorX; |
| 1190 | float distanceY = pixelY - colorY; |
| 1191 |
| 1192 | float distance = std::max(0.0f, 0.9f - sqrtf(distanceX * distanceX + distanceY * distanceY)); |
| 1193 | distance = distance * distance * distance * distance; |
| 1194 | distanceSum += distance; |
| 1195 |
| 1196 | r = r + distance * ((float) colorsArray[i * 4] / 255.0f); |
| 1197 | g = g + distance * ((float) colorsArray[i * 4 + 1] / 255.0f); |
| 1198 | b = b + distance * ((float) colorsArray[i * 4 + 2] / 255.0f); |
| 1199 | } |
| 1200 |
| 1201 | pixels[offset + x * 4] = (uint8_t) (b / distanceSum * 255.0f); |
| 1202 | pixels[offset + x * 4 + 1] = (uint8_t) (g / distanceSum * 255.0f); |
| 1203 | pixels[offset + x * 4 + 2] = (uint8_t) (r / distanceSum * 255.0f); |
| 1204 | pixels[offset + x * 4 + 3] = 0xff; |
| 1205 | } |
| 1206 | } |
| 1207 | if (newPixelCache != nullptr) { |
| 1208 | delete [] pixelCache; |
| 1209 | pixelCache = newPixelCache; |
| 1210 | } |
| 1211 |
| 1212 | env->ReleaseIntArrayElements(colors, (jint *) colorsArray, JNI_ABORT); |
| 1213 |
| 1214 | AndroidBitmap_unlockPixels(env, bitmap); |
| 1215 | } |
| 1216 |
| 1217 | // ---- QR share backdrop: a low-res picture of a 3-stop gradient (linear or radial), upscaled by the GPU as a plain |
| 1218 | // bitmap (no gradient shader to compile or run per pixel). During a recolour wave the new scene grows in a circle over |
| 1219 | // the old one; the wave's glowing ring goes to a second (transparent) bitmap drawn over the pattern. |
| 1220 |
| 1221 | namespace { |
| 1222 |
| 1223 | struct QrScene { |
| 1224 | int type; // 0 linear, 1 radial |
| 1225 | float px, py, dx, dy; // linear: origin + unit direction; radial: centre |
| 1226 | float scale; // 1 / length (linear) or 1 / radius (radial) |
| 1227 | float mid; // the middle stop |
| 1228 | float c[3][3]; // the stops' colours, 0..255 (r, g, b) |
| 1229 | }; |
| 1230 |
| 1231 | inline void qrSceneColor(const QrScene &s, float x, float y, float *out) { |
| 1232 | float t; |
| 1233 | if (s.type == 1) { |
| 1234 | const float ox = x - s.px, oy = y - s.py; |
| 1235 | t = sqrtf(ox * ox + oy * oy) * s.scale; |
| 1236 | } else { |
| 1237 | t = ((x - s.px) * s.dx + (y - s.py) * s.dy) * s.scale; |
| 1238 | } |
| 1239 | t = t < 0.0f ? 0.0f : (t > 1.0f ? 1.0f : t); |
| 1240 | const float *a, *b; |
| 1241 | float k; |
| 1242 | if (t < s.mid) { |
| 1243 | a = s.c[0]; b = s.c[1]; k = s.mid > 0.0f ? t / s.mid : 1.0f; |
| 1244 | } else { |
| 1245 | a = s.c[1]; b = s.c[2]; k = s.mid < 1.0f ? (t - s.mid) / (1.0f - s.mid) : 1.0f; |
| 1246 | } |
| 1247 | out[0] = a[0] + (b[0] - a[0]) * k; |
| 1248 | out[1] = a[1] + (b[1] - a[1]) * k; |
| 1249 | out[2] = a[2] + (b[2] - a[2]) * k; |
| 1250 | } |
| 1251 |
| 1252 | inline void qrReadScene(QrScene &s, const float *g, const jint *colors) { |
| 1253 | s.type = (int) g[0]; |
| 1254 | s.px = g[1]; s.py = g[2]; s.dx = g[3]; s.dy = g[4]; s.scale = g[5]; s.mid = g[6]; |
| 1255 | for (int i = 0; i < 3; i++) { |
| 1256 | const uint32_t c = (uint32_t) colors[i]; |
| 1257 | s.c[i][0] = (float) ((c >> 16) & 0xFF); |
| 1258 | s.c[i][1] = (float) ((c >> 8) & 0xFF); |
| 1259 | s.c[i][2] = (float) (c & 0xFF); |
| 1260 | } |
| 1261 | } |
| 1262 |
| 1263 | // the ring's alpha at distance d: piecewise linear through 6 (position, alpha) stops, 0 outside |
| 1264 | inline float qrRingAlpha(const float *pos, const float *alpha, float d) { |
| 1265 | if (d <= pos[0] || d >= pos[5]) return 0.0f; |
| 1266 | for (int i = 1; i < 6; i++) { |
| 1267 | if (d <= pos[i]) { |
| 1268 | const float span = pos[i] - pos[i - 1]; |
| 1269 | const float k = span > 0.0f ? (d - pos[i - 1]) / span : 1.0f; |
| 1270 | return alpha[i - 1] + (alpha[i] - alpha[i - 1]) * k; |
| 1271 | } |
| 1272 | } |
| 1273 | return 0.0f; |
| 1274 | } |
| 1275 |
| 1276 | inline uint8_t qrByte(float v) { |
| 1277 | v += 0.5f; |
| 1278 | return (uint8_t) (v < 0.0f ? 0.0f : (v > 255.0f ? 255.0f : v)); |
| 1279 | } |
| 1280 |
| 1281 | } |
| 1282 |
| 1283 | /** |
| 1284 | * bitmap: the backdrop, opaque; texel (i, j) is the view point (i * cell, j * cell). |
| 1285 | * geom: 7 floats per scene (type, px, py, dx, dy, scale, mid); colors: 3 per scene, then the ring's colour. |
| 1286 | * count 2 = a wave: scene 1 inside the circle wave[0..2] (x, y, r) over scene 0, its ring into ringBitmap |
| 1287 | * (premultiplied) from wave[3..8] (positions) and wave[9..14] (alphas, 0..1). |
| 1288 | */ |
| 1289 | extern "C" JNIEXPORT void Java_org_telegram_messenger_Utilities_generateQrBackdrop(JNIEnv *env, jclass clazz, jobject bitmap, jobject ringBitmap, jfloat cell, jint count, jfloatArray geom, jintArray colors, jfloatArray wave) { |
| 1290 | if (!bitmap || count < 1) { |
| 1291 | return; |
| 1292 | } |
| 1293 | AndroidBitmapInfo info{}; |
| 1294 | if (AndroidBitmap_getInfo(env, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS || info.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || !info.width || !info.height) { |
| 1295 | return; |
| 1296 | } |
| 1297 | const bool waving = count > 1 && wave != nullptr; |
| 1298 | // the arrays hold what count asks for: 7 floats per scene, 3 colours per scene (+ the ring's), 15 wave floats |
| 1299 | if (geom == nullptr || colors == nullptr || env->GetArrayLength(geom) < (count > 1 ? 14 : 7) |
| 1300 | || env->GetArrayLength(colors) < (waving ? 7 : 3) || (waving && env->GetArrayLength(wave) < 15)) { |
| 1301 | return; |
| 1302 | } |
| 1303 | AndroidBitmapInfo ringInfo{}; |
| 1304 | const bool ring = waving && ringBitmap != nullptr && AndroidBitmap_getInfo(env, ringBitmap, &ringInfo) == ANDROID_BITMAP_RESULT_SUCCESS |
| 1305 | && ringInfo.format == ANDROID_BITMAP_FORMAT_RGBA_8888 && ringInfo.width == info.width && ringInfo.height == info.height; |
| 1306 |
| 1307 | jfloat *g = env->GetFloatArrayElements(geom, nullptr); |
| 1308 | jint *cs = env->GetIntArrayElements(colors, nullptr); |
| 1309 | jfloat *wv = waving ? env->GetFloatArrayElements(wave, nullptr) : nullptr; |
| 1310 |
| 1311 | QrScene a{}, b{}; |
| 1312 | qrReadScene(a, g, cs); |
| 1313 | if (waving) qrReadScene(b, g + 7, cs + 3); |
| 1314 | const uint32_t rc = waving ? (uint32_t) cs[6] : 0; |
| 1315 | const float rr = (float) ((rc >> 16) & 0xFF), rg = (float) ((rc >> 8) & 0xFF), rb = (float) (rc & 0xFF); |
| 1316 |
| 1317 | uint8_t *px = nullptr, *rpx = nullptr; |
| 1318 | if (AndroidBitmap_lockPixels(env, bitmap, (void **) &px) >= 0) { |
| 1319 | if (ring && AndroidBitmap_lockPixels(env, ringBitmap, (void **) &rpx) < 0) rpx = nullptr; |
| 1320 | const float h4 = cell * 0.25f; |
| 1321 | float c0[3], c1[3]; |
| 1322 | for (uint32_t j = 0; j < info.height; j++) { |
| 1323 | const float y = (float) j * cell; |
| 1324 | uint8_t *row = px + j * info.stride; |
| 1325 | uint8_t *ringRow = rpx ? rpx + j * ringInfo.stride : nullptr; |
| 1326 | for (uint32_t i = 0; i < info.width; i++) { |
| 1327 | const float x = (float) i * cell; |
| 1328 | qrSceneColor(a, x, y, c0); |
| 1329 | float ra = 0.0f; |
| 1330 | if (waving) { |
| 1331 | const float ox = x - wv[0], oy = y - wv[1]; |
| 1332 | const float d = sqrtf(ox * ox + oy * oy); |
| 1333 | // the circle's edge covers one texel (a pixel-exact edge would alias at this resolution) |
| 1334 | float k = (wv[2] - d) / cell + 0.5f; |
| 1335 | k = k < 0.0f ? 0.0f : (k > 1.0f ? 1.0f : k); |
| 1336 | if (k > 0.0f) { |
| 1337 | qrSceneColor(b, x, y, c1); |
| 1338 | c0[0] += (c1[0] - c0[0]) * k; |
| 1339 | c0[1] += (c1[1] - c0[1]) * k; |
| 1340 | c0[2] += (c1[2] - c0[2]) * k; |
| 1341 | } |
| 1342 | if (ringRow) { |
| 1343 | // the ring's thin rim is narrower than a texel: averaged over the texel across the radius |
| 1344 | ra = 0.25f * (qrRingAlpha(wv + 3, wv + 9, d - 1.5f * h4) + qrRingAlpha(wv + 3, wv + 9, d - 0.5f * h4) |
| 1345 | + qrRingAlpha(wv + 3, wv + 9, d + 0.5f * h4) + qrRingAlpha(wv + 3, wv + 9, d + 1.5f * h4)); |
| 1346 | } |
| 1347 | } |
| 1348 | uint8_t *o = row + i * 4; |
| 1349 | o[0] = qrByte(c0[0]); |
| 1350 | o[1] = qrByte(c0[1]); |
| 1351 | o[2] = qrByte(c0[2]); |
| 1352 | o[3] = 0xFF; |
| 1353 | if (ringRow) { |
| 1354 | uint8_t *q = ringRow + i * 4; |
| 1355 | q[0] = qrByte(rr * ra); |
| 1356 | q[1] = qrByte(rg * ra); |
| 1357 | q[2] = qrByte(rb * ra); |
| 1358 | q[3] = qrByte(255.0f * ra); |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | if (rpx) AndroidBitmap_unlockPixels(env, ringBitmap); |
| 1363 | AndroidBitmap_unlockPixels(env, bitmap); |
| 1364 | } |
| 1365 |
| 1366 | if (wv) env->ReleaseFloatArrayElements(wave, wv, JNI_ABORT); |
| 1367 | env->ReleaseIntArrayElements(colors, cs, JNI_ABORT); |
| 1368 | env->ReleaseFloatArrayElements(geom, g, JNI_ABORT); |
| 1369 | } |
| 1370 |
| 1371 | static inline uint32_t bitmapBytesPerPixel(int32_t format) { |
| 1372 | switch (format) { |
| 1373 | case ANDROID_BITMAP_FORMAT_A_8: |
| 1374 | return 1; |
| 1375 |
| 1376 | case ANDROID_BITMAP_FORMAT_RGB_565: |
| 1377 | case ANDROID_BITMAP_FORMAT_RGBA_4444: // deprecated since API 13 |
| 1378 | return 2; |
| 1379 |
| 1380 | case ANDROID_BITMAP_FORMAT_RGBA_8888: |
| 1381 | return 4; |
| 1382 |
| 1383 | case ANDROID_BITMAP_FORMAT_RGBA_F16: |
| 1384 | return 8; |
| 1385 |
| 1386 | case ANDROID_BITMAP_FORMAT_RGBA_1010102: |
| 1387 | return 4; |
| 1388 |
| 1389 | default: |
| 1390 | return 0; |
| 1391 | } |
| 1392 | } |
| 1393 |
| 1394 | /** |
| 1395 | * Copies pixel data from src to dst. |
| 1396 | * |
| 1397 | * Both bitmaps must have identical dimensions and pixel format. |
| 1398 | * Hardware-backed bitmaps are not supported. |
| 1399 | * Copying a bitmap to itself is a no-op and returns JNI_TRUE. |
| 1400 | * |
| 1401 | * @param src Source bitmap. |
| 1402 | * @param dst Destination bitmap. |
| 1403 | * @return JNI_TRUE on success, JNI_FALSE if bitmaps are incompatible or an error occurred. |
| 1404 | */ |
| 1405 | JNIEXPORT jboolean JNICALL |
| 1406 | Java_org_telegram_messenger_Utilities_copyBitmaps( |
| 1407 | JNIEnv *env, |
| 1408 | jclass /*clazz*/, |
| 1409 | jobject src, |
| 1410 | jobject dst) { |
| 1411 |
| 1412 | if (__builtin_expect(src == nullptr || dst == nullptr, 0)) { |
| 1413 | return JNI_FALSE; |
| 1414 | } |
| 1415 |
| 1416 | if (__builtin_expect(env->IsSameObject(src, dst), 0)) { |
| 1417 | return JNI_TRUE; |
| 1418 | } |
| 1419 |
| 1420 | AndroidBitmapInfo srcInfo{}; |
| 1421 | AndroidBitmapInfo dstInfo{}; |
| 1422 |
| 1423 | if (__builtin_expect( |
| 1424 | AndroidBitmap_getInfo(env, src, &srcInfo) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 1425 | AndroidBitmap_getInfo(env, dst, &dstInfo) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1426 | 0)) { |
| 1427 | return JNI_FALSE; |
| 1428 | } |
| 1429 |
| 1430 | if (__builtin_expect( |
| 1431 | (srcInfo.flags & ANDROID_BITMAP_FLAGS_IS_HARDWARE) != 0 || |
| 1432 | (dstInfo.flags & ANDROID_BITMAP_FLAGS_IS_HARDWARE) != 0, |
| 1433 | 0)) { |
| 1434 | return JNI_FALSE; |
| 1435 | } |
| 1436 |
| 1437 | if (__builtin_expect( |
| 1438 | srcInfo.width != dstInfo.width || |
| 1439 | srcInfo.height != dstInfo.height || |
| 1440 | srcInfo.format != dstInfo.format || |
| 1441 | srcInfo.width == 0 || |
| 1442 | srcInfo.height == 0, |
| 1443 | 0)) { |
| 1444 | return JNI_FALSE; |
| 1445 | } |
| 1446 |
| 1447 | const uint32_t bytesPerPixel = bitmapBytesPerPixel(srcInfo.format); |
| 1448 | if (__builtin_expect(bytesPerPixel == 0, 0)) { |
| 1449 | return JNI_FALSE; |
| 1450 | } |
| 1451 |
| 1452 | // size_t cast prevents width * bytesPerPixel overflow on 32-bit platforms |
| 1453 | const size_t rowBytes = static_cast<size_t>(srcInfo.width) * bytesPerPixel; |
| 1454 |
| 1455 | if (__builtin_expect( |
| 1456 | static_cast<size_t>(srcInfo.stride) < rowBytes || |
| 1457 | static_cast<size_t>(dstInfo.stride) < rowBytes, |
| 1458 | 0)) { |
| 1459 | return JNI_FALSE; |
| 1460 | } |
| 1461 |
| 1462 | void *srcPixels = nullptr; |
| 1463 | void *dstPixels = nullptr; |
| 1464 |
| 1465 | if (__builtin_expect( |
| 1466 | AndroidBitmap_lockPixels(env, src, &srcPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1467 | 0)) { |
| 1468 | return JNI_FALSE; |
| 1469 | } |
| 1470 |
| 1471 | if (__builtin_expect( |
| 1472 | AndroidBitmap_lockPixels(env, dst, &dstPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1473 | 0)) { |
| 1474 | AndroidBitmap_unlockPixels(env, src); |
| 1475 | return JNI_FALSE; |
| 1476 | } |
| 1477 |
| 1478 | const bool contiguous = |
| 1479 | static_cast<size_t>(srcInfo.stride) == rowBytes && |
| 1480 | static_cast<size_t>(dstInfo.stride) == rowBytes; |
| 1481 |
| 1482 | if (contiguous) { |
| 1483 | // size_t cast prevents rowBytes * height overflow on 32-bit platforms |
| 1484 | std::memcpy(dstPixels, srcPixels, rowBytes * static_cast<size_t>(srcInfo.height)); |
| 1485 | } else { |
| 1486 | auto *srcRow = static_cast<const uint8_t *>(srcPixels); |
| 1487 | auto *dstRow = static_cast<uint8_t *>(dstPixels); |
| 1488 |
| 1489 | for (uint32_t y = 0; y < srcInfo.height; ++y) { |
| 1490 | std::memcpy(dstRow, srcRow, rowBytes); |
| 1491 | srcRow += static_cast<ptrdiff_t>(srcInfo.stride); |
| 1492 | dstRow += static_cast<ptrdiff_t>(dstInfo.stride); |
| 1493 | } |
| 1494 | } |
| 1495 |
| 1496 | AndroidBitmap_unlockPixels(env, dst); |
| 1497 | AndroidBitmap_unlockPixels(env, src); |
| 1498 | return JNI_TRUE; |
| 1499 | } |
| 1500 |
| 1501 | // --------------------------------------------------------------------------- |
| 1502 | // Soft-Light blend — exact Android/Skia formula, simplified for α_dst = 1. |
| 1503 | // |
| 1504 | // General form (C values are pre-multiplied): |
| 1505 | // m = C_dst / α_dst |
| 1506 | // g = (16m² + 4m)*(m-1) + 7m if 4*C_dst <= α_dst (m <= 0.25) |
| 1507 | // = sqrt(m) - m otherwise |
| 1508 | // f = C_dst*(α_src + (2*C_src - α_src)*(1-m)) if 2*C_src <= α_src |
| 1509 | // = C_dst*α_src + α_dst*(2*C_src - α_src)*g otherwise |
| 1510 | // α_out = α_src + α_dst - α_src*α_dst |
| 1511 | // C_out = C_src/α_dst + C_dst/α_src + f |
| 1512 | // |
| 1513 | // Simplified for α_dst = 1 (guaranteed by caller). |
| 1514 | // Let cb = straight backdrop channel, cs = straight source channel, |
| 1515 | // a = α_src (color alpha, in [0,1]): |
| 1516 | // |
| 1517 | // m = cb |
| 1518 | // g = (16cb² + 4cb)*(cb-1) + 7cb if cb <= 0.25 |
| 1519 | // = sqrt(cb) - cb otherwise |
| 1520 | // f = cb*(a + (2*a*cs - a)*(1-cb)) if 2*a*cs <= a → cs <= 0.5 |
| 1521 | // = cb*a + (2*a*cs - a)*g otherwise |
| 1522 | // |
| 1523 | // result_straight = f/a (recover straight channel from pre-multiplied f) |
| 1524 | // |
| 1525 | // Both LUTs are built on the first call and reused across all subsequent calls. |
| 1526 | // --------------------------------------------------------------------------- |
| 1527 |
| 1528 | // g_sl_lut[cs_u8][cb_u8] -> soft-light result as uint8, for fully opaque color (a=1). |
| 1529 | // 256 * 256 = 64 KB — fits in L2 cache on modern ARM cores. |
| 1530 | static uint8_t g_sl_lut[256][256]; |
| 1531 |
| 1532 | // g_lerp_lut[alpha_u8][value_u8] -> floor(alpha * value / 255) |
| 1533 | // Used for branch-free integer lerp in the hot loop: |
| 1534 | // out = g_lerp_lut[alpha][blend] + g_lerp_lut[255 - alpha][cb] |
| 1535 | // 256 * 256 = 64 KB. |
| 1536 | static uint8_t g_lerp_lut[256][256]; |
| 1537 |
| 1538 | static std::once_flag g_lut_flag; |
| 1539 |
| 1540 | static void build_luts() { |
| 1541 | // lerp LUT: floor(a * v / 255) — intentional floor, not round. |
| 1542 | // This guarantees lerpA[x] + lerpInvA[x] <= 255 for any x and any alpha, |
| 1543 | // preventing uint8_t overflow when the two terms are summed in process_alpha. |
| 1544 | // |
| 1545 | // Proof: floor(a*x/255) + floor((255-a)*x/255) |
| 1546 | // <= a*x/255 + (255-a)*x/255 = x <= 255. |
| 1547 | for (int a = 0; a < 256; ++a) { |
| 1548 | for (int v = 0; v < 256; ++v) { |
| 1549 | g_lerp_lut[a][v] = static_cast<uint8_t>((a * v) / 255); |
| 1550 | } |
| 1551 | } |
| 1552 |
| 1553 | // Soft-light LUT for fully opaque color (α_src = 1, i.e. a = 1). |
| 1554 | // With a = 1: C_src = cs, so 2*C_src <= α_src becomes cs <= 0.5. |
| 1555 | // f = cb*(1 + (2*cs - 1)*(1-cb)) if cs <= 0.5 |
| 1556 | // = cb + (2*cs - 1)*g otherwise |
| 1557 | // result = f (already straight since a = 1) |
| 1558 | for (int cs_i = 0; cs_i < 256; ++cs_i) { |
| 1559 | const float cs = cs_i / 255.0f; |
| 1560 |
| 1561 | for (int cb_i = 0; cb_i < 256; ++cb_i) { |
| 1562 | const float cb = cb_i / 255.0f; |
| 1563 | float result; |
| 1564 |
| 1565 | if (cs <= 0.5f) { |
| 1566 | // f = cb * (α_src + (2*C_src - α_src)*(1 - m)) |
| 1567 | // = cb * (1 + (2*cs - 1)*(1 - cb)) |
| 1568 | result = cb * (1.0f + (2.0f * cs - 1.0f) * (1.0f - cb)); |
| 1569 | } else { |
| 1570 | // g = (16m² + 4m)*(m-1) + 7m, m = cb |
| 1571 | float g; |
| 1572 | if (cb <= 0.25f) { |
| 1573 | g = (16.0f * cb * cb + 4.0f * cb) * (cb - 1.0f) + 7.0f * cb; |
| 1574 | } else { |
| 1575 | g = sqrtf(cb) - cb; |
| 1576 | } |
| 1577 | // f = cb*α_src + α_dst*(2*C_src - α_src)*g |
| 1578 | // = cb + (2*cs - 1)*g (α_src = α_dst = 1) |
| 1579 | result = cb + (2.0f * cs - 1.0f) * g; |
| 1580 | } |
| 1581 |
| 1582 | // Clamp for float rounding safety. |
| 1583 | if (result < 0.0f) result = 0.0f; |
| 1584 | if (result > 1.0f) result = 1.0f; |
| 1585 |
| 1586 | g_sl_lut[cs_i][cb_i] = static_cast<uint8_t>(result * 255.0f + 0.5f); |
| 1587 | } |
| 1588 | } |
| 1589 | } |
| 1590 |
| 1591 | // --------------------------------------------------------------------------- |
| 1592 | // Three specialised hot loops, selected by colorA before entering the loop. |
| 1593 | // Alpha branching is lifted OUT of the loop — no branches inside iterations. |
| 1594 | // --------------------------------------------------------------------------- |
| 1595 |
| 1596 | // colorA == 0xFF: out[ch] = sl_lut[cs][cb] |
| 1597 | static void process_opaque( |
| 1598 | const uint8_t * __restrict__ inPx, |
| 1599 | uint8_t * __restrict__ outPx, |
| 1600 | uint32_t width, uint32_t height, |
| 1601 | uint32_t inStride, uint32_t outStride, |
| 1602 | uint8_t csR, uint8_t csG, uint8_t csB) |
| 1603 | { |
| 1604 | // LUT row pointers are fixed for a given color — load them once outside the loop. |
| 1605 | const uint8_t * __restrict__ slR = g_sl_lut[csR]; |
| 1606 | const uint8_t * __restrict__ slG = g_sl_lut[csG]; |
| 1607 | const uint8_t * __restrict__ slB = g_sl_lut[csB]; |
| 1608 |
| 1609 | for (uint32_t y = 0; y < height; ++y) { |
| 1610 | const uint8_t * __restrict__ src = inPx + y * inStride; |
| 1611 | uint8_t * __restrict__ dst = outPx + y * outStride; |
| 1612 | const uint8_t * const end = src + width * 4u; |
| 1613 |
| 1614 | while (src < end) { |
| 1615 | dst[0] = slR[src[0]]; |
| 1616 | dst[1] = slG[src[1]]; |
| 1617 | dst[2] = slB[src[2]]; |
| 1618 | dst[3] = 0xFF; |
| 1619 | src += 4; |
| 1620 | dst += 4; |
| 1621 | } |
| 1622 | } |
| 1623 | } |
| 1624 |
| 1625 | // colorA == 0x00: output is a copy of input with alpha forced to 0xFF. |
| 1626 | // (Input bitmap is guaranteed opaque, so the copy is a straight pixel copy.) |
| 1627 | static void process_transparent( |
| 1628 | const uint8_t * __restrict__ inPx, |
| 1629 | uint8_t * __restrict__ outPx, |
| 1630 | uint32_t width, uint32_t height, |
| 1631 | uint32_t inStride, uint32_t outStride) |
| 1632 | { |
| 1633 | if (inStride == width * 4u && outStride == width * 4u) { |
| 1634 | memcpy(outPx, inPx, width * height * 4u); |
| 1635 | } else { |
| 1636 | for (uint32_t y = 0; y < height; ++y) { |
| 1637 | memcpy(outPx + y * outStride, inPx + y * inStride, width * 4u); |
| 1638 | } |
| 1639 | } |
| 1640 | } |
| 1641 |
| 1642 | // 0 < colorA < 0xFF: |
| 1643 | // out[ch] = lerp_lut[colorA][sl[cs][cb]] + lerp_lut[255 - colorA][cb] |
| 1644 | // No floats, no branches inside the loop. |
| 1645 | static void process_alpha( |
| 1646 | const uint8_t * __restrict__ inPx, |
| 1647 | uint8_t * __restrict__ outPx, |
| 1648 | uint32_t width, uint32_t height, |
| 1649 | uint32_t inStride, uint32_t outStride, |
| 1650 | uint8_t csR, uint8_t csG, uint8_t csB, uint8_t colorA) |
| 1651 | { |
| 1652 | const uint8_t invA = static_cast<uint8_t>(255 - colorA); |
| 1653 |
| 1654 | const uint8_t * __restrict__ slR = g_sl_lut[csR]; |
| 1655 | const uint8_t * __restrict__ slG = g_sl_lut[csG]; |
| 1656 | const uint8_t * __restrict__ slB = g_sl_lut[csB]; |
| 1657 | const uint8_t * __restrict__ lerpA = g_lerp_lut[colorA]; |
| 1658 | const uint8_t * __restrict__ lerpInvA = g_lerp_lut[invA]; |
| 1659 |
| 1660 | for (uint32_t y = 0; y < height; ++y) { |
| 1661 | const uint8_t * __restrict__ src = inPx + y * inStride; |
| 1662 | uint8_t * __restrict__ dst = outPx + y * outStride; |
| 1663 | const uint8_t * const end = src + width * 4u; |
| 1664 |
| 1665 | while (src < end) { |
| 1666 | const uint8_t cbR = src[0]; |
| 1667 | const uint8_t cbG = src[1]; |
| 1668 | const uint8_t cbB = src[2]; |
| 1669 | dst[0] = lerpA[slR[cbR]] + lerpInvA[cbR]; |
| 1670 | dst[1] = lerpA[slG[cbG]] + lerpInvA[cbG]; |
| 1671 | dst[2] = lerpA[slB[cbB]] + lerpInvA[cbB]; |
| 1672 | dst[3] = 0xFF; |
| 1673 | src += 4; |
| 1674 | dst += 4; |
| 1675 | } |
| 1676 | } |
| 1677 | } |
| 1678 |
| 1679 | // --------------------------------------------------------------------------- |
| 1680 | // JNI entry point |
| 1681 | // |
| 1682 | // Kotlin: external fun applySoftLight(input: Bitmap, output: Bitmap, color: Int): Boolean [org.telegram.messenger.Utilities] |
| 1683 | // |
| 1684 | // color — Android packed ARGB (0xAARRGGBB), straight (non-premultiplied) alpha. |
| 1685 | // Returns true on success, false on error (size mismatch or unsupported format). |
| 1686 | // --------------------------------------------------------------------------- |
| 1687 | JNIEXPORT jboolean JNICALL |
| 1688 | Java_org_telegram_messenger_Utilities_applySoftLight( |
| 1689 | JNIEnv *env, |
| 1690 | jclass /*clazz*/, |
| 1691 | jobject inputBitmap, |
| 1692 | jobject outputBitmap, |
| 1693 | jint color) |
| 1694 | { |
| 1695 | std::call_once(g_lut_flag, build_luts); |
| 1696 |
| 1697 | if (__builtin_expect(env->IsSameObject(inputBitmap, outputBitmap), 0)) { |
| 1698 | return JNI_FALSE; |
| 1699 | } |
| 1700 |
| 1701 | AndroidBitmapInfo inInfo{}; |
| 1702 | AndroidBitmapInfo outInfo{}; |
| 1703 |
| 1704 | if (__builtin_expect( |
| 1705 | AndroidBitmap_getInfo(env, inputBitmap, &inInfo) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 1706 | AndroidBitmap_getInfo(env, outputBitmap, &outInfo) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1707 | 0)) { |
| 1708 | return JNI_FALSE; |
| 1709 | } |
| 1710 |
| 1711 | if (__builtin_expect( |
| 1712 | inInfo.width != outInfo.width || |
| 1713 | inInfo.height != outInfo.height || |
| 1714 | inInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || |
| 1715 | outInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || |
| 1716 | inInfo.width == 0 || |
| 1717 | inInfo.height == 0 || |
| 1718 | inInfo.stride < inInfo.width * 4u || |
| 1719 | outInfo.stride < outInfo.width * 4u, |
| 1720 | 0)) { |
| 1721 | return JNI_FALSE; |
| 1722 | } |
| 1723 |
| 1724 | void *inPixels = nullptr; |
| 1725 | void *outPixels = nullptr; |
| 1726 |
| 1727 | if (__builtin_expect( |
| 1728 | AndroidBitmap_lockPixels(env, inputBitmap, &inPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1729 | 0)) { |
| 1730 | return JNI_FALSE; |
| 1731 | } |
| 1732 | if (__builtin_expect( |
| 1733 | AndroidBitmap_lockPixels(env, outputBitmap, &outPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1734 | 0)) { |
| 1735 | AndroidBitmap_unlockPixels(env, inputBitmap); |
| 1736 | return JNI_FALSE; |
| 1737 | } |
| 1738 |
| 1739 | // Unpack Java color (0xAARRGGBB) into separate channels. |
| 1740 | const auto u = static_cast<uint32_t>(color); |
| 1741 | const uint8_t colorA = static_cast<uint8_t>(u >> 24); |
| 1742 | const uint8_t colorR = static_cast<uint8_t>(u >> 16); |
| 1743 | const uint8_t colorG = static_cast<uint8_t>(u >> 8); |
| 1744 | const uint8_t colorB = static_cast<uint8_t>(u); |
| 1745 |
| 1746 | const auto *src = static_cast<const uint8_t *>(inPixels); |
| 1747 | auto *dst = static_cast<uint8_t *>(outPixels); |
| 1748 | const uint32_t w = inInfo.width; |
| 1749 | const uint32_t h = inInfo.height; |
| 1750 | const uint32_t si = inInfo.stride; |
| 1751 | const uint32_t so = outInfo.stride; |
| 1752 |
| 1753 | // Dispatch before the loop so no alpha branching occurs inside it. |
| 1754 | if (colorA == 0xFF) { |
| 1755 | process_opaque(src, dst, w, h, si, so, colorR, colorG, colorB); |
| 1756 | } else if (colorA == 0x00) { |
| 1757 | process_transparent(src, dst, w, h, si, so); |
| 1758 | } else { |
| 1759 | process_alpha(src, dst, w, h, si, so, colorR, colorG, colorB, colorA); |
| 1760 | } |
| 1761 |
| 1762 | AndroidBitmap_unlockPixels(env, outputBitmap); |
| 1763 | AndroidBitmap_unlockPixels(env, inputBitmap); |
| 1764 | return JNI_TRUE; |
| 1765 | } |
| 1766 |
| 1767 |
| 1768 |
| 1769 | // --------------------------------------------------------------------------- |
| 1770 | // Alpha-invert effect. |
| 1771 | // |
| 1772 | // For every pixel: |
| 1773 | // a = alpha channel of the input pixel |
| 1774 | // v = 255 - (a * intensity / 255) (clamped to [0, 255]) |
| 1775 | // output pixel = RGBA(0, 0, 0, v) |
| 1776 | // |
| 1777 | // Input : RGBA_8888 (alpha is byte 3) or ALPHA_8 (single byte per pixel) |
| 1778 | // Output : RGBA_8888, same dimensions as input |
| 1779 | // |
| 1780 | // The output has RGB = 0, which satisfies RGB <= A for any v, so the result is |
| 1781 | // simultaneously valid as straight and as premultiplied RGBA — no conversion |
| 1782 | // is needed either way. |
| 1783 | // |
| 1784 | // The output pixel depends only on `a` and `intensity`, so a 256-entry LUT of |
| 1785 | // fully packed uint32 pixels is built once per call (256 iterations — |
| 1786 | // negligible next to millions of pixels). The hot loop then reduces to one |
| 1787 | // lookup plus one 32-bit store per pixel. |
| 1788 | // --------------------------------------------------------------------------- |
| 1789 |
| 1790 | // Builds lut[a] = the fully packed output pixel RGBA(0, 0, 0, v), |
| 1791 | // where v = clamp(255 - (a * intensity / 255), 0, 255). |
| 1792 | // |
| 1793 | // Storing the packed 32-bit pixel (rather than just v) means the hot loop is a |
| 1794 | // single lookup followed by a single 32-bit store, with no per-pixel packing. |
| 1795 | // |
| 1796 | // RGBA_8888 memory order is R, G, B, A; on little-endian (every Android ABI) |
| 1797 | // the packed word is (A << 24) | (B << 16) | (G << 8) | R. With R = G = B = 0 |
| 1798 | // this collapses to v << 24. |
| 1799 | static void build_intensity_lut(uint32_t lut[256], int32_t intensity) { |
| 1800 | for (int32_t a = 0; a < 256; ++a) { |
| 1801 | // int64 keeps the product safe even for out-of-range intensity values. |
| 1802 | int64_t v = 255 - (static_cast<int64_t>(a) * intensity) / 255; |
| 1803 | if (v < 0) v = 0; |
| 1804 | if (v > 255) v = 255; |
| 1805 |
| 1806 | lut[a] = static_cast<uint32_t>(v) << 24; |
| 1807 | } |
| 1808 | } |
| 1809 |
| 1810 | // --------------------------------------------------------------------------- |
| 1811 | // Hot loops. One per input format, selected before entering the loop. |
| 1812 | // |
| 1813 | // Each iteration is a single LUT lookup followed by a single 32-bit store. |
| 1814 | // Output is RGBA_8888, whose base pointer is 4-byte aligned by |
| 1815 | // AndroidBitmap_lockPixels and whose stride is a multiple of 4 (validated by |
| 1816 | // the caller), so the uint32_t store is legal on every row. |
| 1817 | // --------------------------------------------------------------------------- |
| 1818 |
| 1819 | // Input RGBA_8888: alpha lives in byte 3 of every 4-byte pixel. |
| 1820 | static void process_rgba8888( |
| 1821 | const uint8_t * __restrict__ inPx, |
| 1822 | uint8_t * __restrict__ outPx, |
| 1823 | uint32_t width, uint32_t height, |
| 1824 | uint32_t inStride, uint32_t outStride, |
| 1825 | const uint32_t * __restrict__ lut) |
| 1826 | { |
| 1827 | for (uint32_t y = 0; y < height; ++y) { |
| 1828 | const uint8_t * __restrict__ src = inPx + y * inStride; |
| 1829 | auto * __restrict__ dst = |
| 1830 | reinterpret_cast<uint32_t *>(outPx + y * outStride); |
| 1831 |
| 1832 | for (uint32_t x = 0; x < width; ++x) { |
| 1833 | dst[x] = lut[src[3]]; // byte 3 = alpha |
| 1834 | src += 4; |
| 1835 | } |
| 1836 | } |
| 1837 | } |
| 1838 |
| 1839 | // Input ALPHA_8: one alpha byte per pixel, no other channels. |
| 1840 | static void process_alpha8( |
| 1841 | const uint8_t * __restrict__ inPx, |
| 1842 | uint8_t * __restrict__ outPx, |
| 1843 | uint32_t width, uint32_t height, |
| 1844 | uint32_t inStride, uint32_t outStride, |
| 1845 | const uint32_t * __restrict__ lut) |
| 1846 | { |
| 1847 | for (uint32_t y = 0; y < height; ++y) { |
| 1848 | const uint8_t * __restrict__ src = inPx + y * inStride; |
| 1849 | auto * __restrict__ dst = |
| 1850 | reinterpret_cast<uint32_t *>(outPx + y * outStride); |
| 1851 |
| 1852 | for (uint32_t x = 0; x < width; ++x) { |
| 1853 | dst[x] = lut[src[x]]; |
| 1854 | } |
| 1855 | } |
| 1856 | } |
| 1857 |
| 1858 | // --------------------------------------------------------------------------- |
| 1859 | // JNI entry point |
| 1860 | // |
| 1861 | // Kotlin: external fun applyAlphaInvert( |
| 1862 | // input: Bitmap, output: Bitmap, intensity: Int): Boolean |
| 1863 | // [org.telegram.messenger.Utilities] |
| 1864 | // |
| 1865 | // input — RGBA_8888 or ALPHA_8 |
| 1866 | // output — RGBA_8888, same dimensions as input |
| 1867 | // intensity — alpha scaling factor in [0, 255]; 255 means "use alpha as-is" |
| 1868 | // |
| 1869 | // Returns true on success, false on error (size mismatch, unsupported format, |
| 1870 | // aliasing between the two bitmaps, or a failed lock). |
| 1871 | // --------------------------------------------------------------------------- |
| 1872 | extern "C" |
| 1873 | JNIEXPORT jboolean JNICALL |
| 1874 | Java_org_telegram_messenger_Utilities_applyAlphaInvert( |
| 1875 | JNIEnv *env, |
| 1876 | jclass /*clazz*/, |
| 1877 | jobject inputBitmap, |
| 1878 | jobject outputBitmap, |
| 1879 | jint intensity) |
| 1880 | { |
| 1881 | if (__builtin_expect(env->IsSameObject(inputBitmap, outputBitmap), 0)) { |
| 1882 | return JNI_FALSE; |
| 1883 | } |
| 1884 |
| 1885 | AndroidBitmapInfo inInfo{}; |
| 1886 | AndroidBitmapInfo outInfo{}; |
| 1887 |
| 1888 | if (__builtin_expect( |
| 1889 | AndroidBitmap_getInfo(env, inputBitmap, &inInfo) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 1890 | AndroidBitmap_getInfo(env, outputBitmap, &outInfo) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1891 | 0)) { |
| 1892 | return JNI_FALSE; |
| 1893 | } |
| 1894 |
| 1895 | // Input may be RGBA_8888 (4 bytes/px) or ALPHA_8 (1 byte/px). |
| 1896 | const bool inIsRgba = (inInfo.format == ANDROID_BITMAP_FORMAT_RGBA_8888); |
| 1897 | const bool inIsA8 = (inInfo.format == ANDROID_BITMAP_FORMAT_A_8); |
| 1898 | const uint32_t inBpp = inIsRgba ? 4u : 1u; |
| 1899 |
| 1900 | if (__builtin_expect( |
| 1901 | (!inIsRgba && !inIsA8) || |
| 1902 | outInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || |
| 1903 | inInfo.width != outInfo.width || |
| 1904 | inInfo.height != outInfo.height || |
| 1905 | inInfo.width == 0 || |
| 1906 | inInfo.height == 0 || |
| 1907 | inInfo.stride < inInfo.width * inBpp || |
| 1908 | outInfo.stride < outInfo.width * 4u, |
| 1909 | 0)) { |
| 1910 | return JNI_FALSE; |
| 1911 | } |
| 1912 |
| 1913 | void *inPixels = nullptr; |
| 1914 | void *outPixels = nullptr; |
| 1915 |
| 1916 | if (__builtin_expect( |
| 1917 | AndroidBitmap_lockPixels(env, inputBitmap, &inPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1918 | 0)) { |
| 1919 | return JNI_FALSE; |
| 1920 | } |
| 1921 | if (__builtin_expect( |
| 1922 | AndroidBitmap_lockPixels(env, outputBitmap, &outPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1923 | 0)) { |
| 1924 | AndroidBitmap_unlockPixels(env, inputBitmap); |
| 1925 | return JNI_FALSE; |
| 1926 | } |
| 1927 |
| 1928 | // 1 KB LUT of packed output pixels — built per call because it depends on `intensity`. |
| 1929 | uint32_t lut[256]; |
| 1930 | build_intensity_lut(lut, intensity); |
| 1931 |
| 1932 | const auto *src = static_cast<const uint8_t *>(inPixels); |
| 1933 | auto *dst = static_cast<uint8_t *>(outPixels); |
| 1934 | const uint32_t w = inInfo.width; |
| 1935 | const uint32_t h = inInfo.height; |
| 1936 | const uint32_t si = inInfo.stride; |
| 1937 | const uint32_t so = outInfo.stride; |
| 1938 |
| 1939 | // Dispatch before the loop so no format branching occurs inside it. |
| 1940 | if (inIsRgba) { |
| 1941 | process_rgba8888(src, dst, w, h, si, so, lut); |
| 1942 | } else { |
| 1943 | process_alpha8(src, dst, w, h, si, so, lut); |
| 1944 | } |
| 1945 |
| 1946 | AndroidBitmap_unlockPixels(env, outputBitmap); |
| 1947 | AndroidBitmap_unlockPixels(env, inputBitmap); |
| 1948 | return JNI_TRUE; |
| 1949 | } |
| 1950 |
| 1951 |
| 1952 |
| 1953 | extern "C" |
| 1954 | JNIEXPORT jboolean JNICALL |
| 1955 | Java_org_telegram_messenger_Utilities_nLibyuvARGBSaleBitmap( |
| 1956 | JNIEnv* env, |
| 1957 | jclass, |
| 1958 | jobject inputBitmap, |
| 1959 | jobject outputBitmap, |
| 1960 | jint filterMode |
| 1961 | ) { |
| 1962 | if (__builtin_expect(inputBitmap == nullptr || outputBitmap == nullptr, 0)) { |
| 1963 | return JNI_FALSE; |
| 1964 | } |
| 1965 |
| 1966 | AndroidBitmapInfo inInfo{}; |
| 1967 | AndroidBitmapInfo outInfo{}; |
| 1968 |
| 1969 | if (__builtin_expect( |
| 1970 | AndroidBitmap_getInfo(env, inputBitmap, &inInfo) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 1971 | AndroidBitmap_getInfo(env, outputBitmap, &outInfo) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 1972 | 0)) { |
| 1973 | return JNI_FALSE; |
| 1974 | } |
| 1975 |
| 1976 | if (__builtin_expect( |
| 1977 | inInfo.width == 0 || inInfo.height == 0 || |
| 1978 | outInfo.width == 0 || outInfo.height == 0, |
| 1979 | 0)) { |
| 1980 | return JNI_FALSE; |
| 1981 | } |
| 1982 |
| 1983 | if (__builtin_expect( |
| 1984 | inInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || |
| 1985 | outInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888, |
| 1986 | 0)) { |
| 1987 | return JNI_FALSE; |
| 1988 | } |
| 1989 |
| 1990 | if (__builtin_expect( |
| 1991 | inInfo.width > INT_MAX / 4 || |
| 1992 | outInfo.width > INT_MAX / 4 || |
| 1993 | inInfo.height > INT_MAX || |
| 1994 | outInfo.height > INT_MAX || |
| 1995 | inInfo.stride > INT_MAX || |
| 1996 | outInfo.stride > INT_MAX, |
| 1997 | 0)) { |
| 1998 | return JNI_FALSE; |
| 1999 | } |
| 2000 |
| 2001 | if (__builtin_expect( |
| 2002 | inInfo.stride < inInfo.width * 4 || |
| 2003 | outInfo.stride < outInfo.width * 4, |
| 2004 | 0)) { |
| 2005 | return JNI_FALSE; |
| 2006 | } |
| 2007 |
| 2008 | libyuv::FilterMode mode; |
| 2009 |
| 2010 | switch (filterMode) { |
| 2011 | case 0: mode = libyuv::kFilterNone; break; |
| 2012 | case 1: mode = libyuv::kFilterLinear; break; |
| 2013 | case 2: mode = libyuv::kFilterBilinear; break; |
| 2014 | case 3: mode = libyuv::kFilterBox; break; |
| 2015 | default: return JNI_FALSE; |
| 2016 | } |
| 2017 |
| 2018 | void* inPixels = nullptr; |
| 2019 | void* outPixels = nullptr; |
| 2020 |
| 2021 | if (__builtin_expect( |
| 2022 | AndroidBitmap_lockPixels(env, inputBitmap, &inPixels) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 2023 | inPixels == nullptr, |
| 2024 | 0)) { |
| 2025 | return JNI_FALSE; |
| 2026 | } |
| 2027 |
| 2028 | if (__builtin_expect( |
| 2029 | AndroidBitmap_lockPixels(env, outputBitmap, &outPixels) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 2030 | outPixels == nullptr, |
| 2031 | 0)) { |
| 2032 | AndroidBitmap_unlockPixels(env, inputBitmap); |
| 2033 | return JNI_FALSE; |
| 2034 | } |
| 2035 |
| 2036 | const int scaleResult = libyuv::ARGBScale( |
| 2037 | static_cast<const uint8_t*>(inPixels), |
| 2038 | static_cast<int>(inInfo.stride), |
| 2039 | static_cast<int>(inInfo.width), |
| 2040 | static_cast<int>(inInfo.height), |
| 2041 | static_cast<uint8_t*>(outPixels), |
| 2042 | static_cast<int>(outInfo.stride), |
| 2043 | static_cast<int>(outInfo.width), |
| 2044 | static_cast<int>(outInfo.height), |
| 2045 | mode |
| 2046 | ); |
| 2047 |
| 2048 | AndroidBitmap_unlockPixels(env, outputBitmap); |
| 2049 | AndroidBitmap_unlockPixels(env, inputBitmap); |
| 2050 |
| 2051 | return __builtin_expect(scaleResult == 0, 1) ? JNI_TRUE : JNI_FALSE; |
| 2052 | } |
| 2053 |
| 2054 |
| 2055 | extern "C" |
| 2056 | JNIEXPORT jint JNICALL |
| 2057 | Java_org_telegram_messenger_Utilities_averageBitmapColor( |
| 2058 | JNIEnv* env, |
| 2059 | jclass, |
| 2060 | jobject bitmap, |
| 2061 | jint left, |
| 2062 | jint top, |
| 2063 | jint right, |
| 2064 | jint bottom |
| 2065 | ) { |
| 2066 | if (__builtin_expect(bitmap == nullptr, 0)) { |
| 2067 | return 0; |
| 2068 | } |
| 2069 |
| 2070 | if (__builtin_expect(left < 0 || top < 0 || right <= left || bottom <= top, 0)) { |
| 2071 | return 0; |
| 2072 | } |
| 2073 |
| 2074 | AndroidBitmapInfo info{}; |
| 2075 |
| 2076 | if (__builtin_expect( |
| 2077 | AndroidBitmap_getInfo(env, bitmap, &info) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 2078 | 0)) { |
| 2079 | return 0; |
| 2080 | } |
| 2081 |
| 2082 | if (__builtin_expect( |
| 2083 | info.width == 0 || info.height == 0 || |
| 2084 | info.format != ANDROID_BITMAP_FORMAT_RGBA_8888, |
| 2085 | 0)) { |
| 2086 | return 0; |
| 2087 | } |
| 2088 |
| 2089 | if (__builtin_expect( |
| 2090 | info.width > INT_MAX / 4 || |
| 2091 | info.height > INT_MAX || |
| 2092 | info.stride > INT_MAX, |
| 2093 | 0)) { |
| 2094 | return 0; |
| 2095 | } |
| 2096 |
| 2097 | if (__builtin_expect(info.stride < info.width * 4, 0)) { |
| 2098 | return 0; |
| 2099 | } |
| 2100 |
| 2101 | if (__builtin_expect( |
| 2102 | right > static_cast<jint>(info.width) || |
| 2103 | bottom > static_cast<jint>(info.height), |
| 2104 | 0)) { |
| 2105 | return 0; |
| 2106 | } |
| 2107 |
| 2108 | void* pixels = nullptr; |
| 2109 |
| 2110 | if (__builtin_expect( |
| 2111 | AndroidBitmap_lockPixels(env, bitmap, &pixels) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 2112 | pixels == nullptr, |
| 2113 | 0)) { |
| 2114 | return 0; |
| 2115 | } |
| 2116 |
| 2117 | uint64_t sumR = 0; |
| 2118 | uint64_t sumG = 0; |
| 2119 | uint64_t sumB = 0; |
| 2120 | uint64_t sumA = 0; |
| 2121 |
| 2122 | const uint8_t* base = static_cast<const uint8_t*>(pixels); |
| 2123 | const int stride = static_cast<int>(info.stride); |
| 2124 |
| 2125 | for (int y = top; y < bottom; y++) { |
| 2126 | const uint8_t* row = base + static_cast<size_t>(y) * stride + static_cast<size_t>(left) * 4; |
| 2127 |
| 2128 | for (int x = left; x < right; x++) { |
| 2129 | sumR += row[0]; |
| 2130 | sumG += row[1]; |
| 2131 | sumB += row[2]; |
| 2132 | sumA += row[3]; |
| 2133 | row += 4; |
| 2134 | } |
| 2135 | } |
| 2136 |
| 2137 | AndroidBitmap_unlockPixels(env, bitmap); |
| 2138 |
| 2139 | const uint64_t count = static_cast<uint64_t>(right - left) * static_cast<uint64_t>(bottom - top); |
| 2140 |
| 2141 | const uint32_t avgR = static_cast<uint32_t>(sumR / count); |
| 2142 | const uint32_t avgG = static_cast<uint32_t>(sumG / count); |
| 2143 | const uint32_t avgB = static_cast<uint32_t>(sumB / count); |
| 2144 | const uint32_t avgA = static_cast<uint32_t>(sumA / count); |
| 2145 |
| 2146 | return static_cast<jint>( |
| 2147 | (avgA << 24) | |
| 2148 | (avgR << 16) | |
| 2149 | (avgG << 8) | |
| 2150 | avgB |
| 2151 | ); |
| 2152 | } |
| 2153 |
| 2154 | // --------------------------------------------------------------------------- |
| 2155 | // ALPHA_8 -> RGBA_8888 expansion. |
| 2156 | // |
| 2157 | // For every pixel: |
| 2158 | // a = the source alpha byte |
| 2159 | // output pixel = RGBA(0, 0, 0, a) — black, alpha taken from the source |
| 2160 | // |
| 2161 | // src : ALPHA_8 (1 byte per pixel) |
| 2162 | // dst : RGBA_8888 (4 bytes per pixel), same dimensions as src |
| 2163 | // |
| 2164 | // RGB = 0 satisfies RGB <= A for any a, so the result is simultaneously valid |
| 2165 | // as straight and as premultiplied RGBA — no conversion is needed either way. |
| 2166 | // |
| 2167 | // No LUT is needed here: on little-endian (every Android ABI) the RGBA_8888 |
| 2168 | // memory order R, G, B, A packs into a word as (A << 24) | (B << 16) | |
| 2169 | // (G << 8) | R, which with R = G = B = 0 collapses to a << 24. A shift is |
| 2170 | // cheaper than a table lookup, so the hot loop is a byte load, a shift, and a |
| 2171 | // 32-bit store. |
| 2172 | // --------------------------------------------------------------------------- |
| 2173 |
| 2174 | static void expand_alpha8_to_rgba8888( |
| 2175 | const uint8_t * __restrict__ srcPx, |
| 2176 | uint8_t * __restrict__ dstPx, |
| 2177 | uint32_t width, uint32_t height, |
| 2178 | uint32_t srcStride, uint32_t dstStride) |
| 2179 | { |
| 2180 | for (uint32_t y = 0; y < height; ++y) { |
| 2181 | const uint8_t * __restrict__ src = srcPx + y * srcStride; |
| 2182 | auto * __restrict__ dst = |
| 2183 | reinterpret_cast<uint32_t *>(dstPx + y * dstStride); |
| 2184 |
| 2185 | for (uint32_t x = 0; x < width; ++x) { |
| 2186 | dst[x] = static_cast<uint32_t>(src[x]) << 24; // RGBA(0, 0, 0, a) |
| 2187 | } |
| 2188 | } |
| 2189 | } |
| 2190 |
| 2191 | // --------------------------------------------------------------------------- |
| 2192 | // JNI entry point |
| 2193 | // |
| 2194 | // Kotlin: external fun expandAlphaToBlack(src: Bitmap, dst: Bitmap): Boolean |
| 2195 | // [org.telegram.messenger.Utilities] |
| 2196 | // |
| 2197 | // src — ALPHA_8 |
| 2198 | // dst — RGBA_8888, same dimensions as src |
| 2199 | // |
| 2200 | // Returns true on success, false on error (size mismatch, unsupported format, |
| 2201 | // aliasing between the two bitmaps, or a failed lock). |
| 2202 | // --------------------------------------------------------------------------- |
| 2203 | extern "C" |
| 2204 | JNIEXPORT jboolean JNICALL |
| 2205 | Java_org_telegram_messenger_Utilities_expandAlphaToBlack( |
| 2206 | JNIEnv *env, |
| 2207 | jclass /*clazz*/, |
| 2208 | jobject srcBitmap, |
| 2209 | jobject dstBitmap) |
| 2210 | { |
| 2211 | if (__builtin_expect(env->IsSameObject(srcBitmap, dstBitmap), 0)) { |
| 2212 | return JNI_FALSE; |
| 2213 | } |
| 2214 |
| 2215 | AndroidBitmapInfo srcInfo{}; |
| 2216 | AndroidBitmapInfo dstInfo{}; |
| 2217 |
| 2218 | if (__builtin_expect( |
| 2219 | AndroidBitmap_getInfo(env, srcBitmap, &srcInfo) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 2220 | AndroidBitmap_getInfo(env, dstBitmap, &dstInfo) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 2221 | 0)) { |
| 2222 | return JNI_FALSE; |
| 2223 | } |
| 2224 |
| 2225 | if (__builtin_expect( |
| 2226 | srcInfo.format != ANDROID_BITMAP_FORMAT_A_8 || |
| 2227 | dstInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || |
| 2228 | srcInfo.width != dstInfo.width || |
| 2229 | srcInfo.height != dstInfo.height || |
| 2230 | srcInfo.width == 0 || |
| 2231 | srcInfo.height == 0 || |
| 2232 | srcInfo.stride < srcInfo.width || |
| 2233 | dstInfo.stride < dstInfo.width * 4u, |
| 2234 | 0)) { |
| 2235 | return JNI_FALSE; |
| 2236 | } |
| 2237 |
| 2238 | void *srcPixels = nullptr; |
| 2239 | void *dstPixels = nullptr; |
| 2240 |
| 2241 | if (__builtin_expect( |
| 2242 | AndroidBitmap_lockPixels(env, srcBitmap, &srcPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 2243 | 0)) { |
| 2244 | return JNI_FALSE; |
| 2245 | } |
| 2246 | if (__builtin_expect( |
| 2247 | AndroidBitmap_lockPixels(env, dstBitmap, &dstPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 2248 | 0)) { |
| 2249 | AndroidBitmap_unlockPixels(env, srcBitmap); |
| 2250 | return JNI_FALSE; |
| 2251 | } |
| 2252 |
| 2253 | expand_alpha8_to_rgba8888( |
| 2254 | static_cast<const uint8_t *>(srcPixels), |
| 2255 | static_cast<uint8_t *>(dstPixels), |
| 2256 | srcInfo.width, srcInfo.height, |
| 2257 | srcInfo.stride, dstInfo.stride); |
| 2258 |
| 2259 | AndroidBitmap_unlockPixels(env, dstBitmap); |
| 2260 | AndroidBitmap_unlockPixels(env, srcBitmap); |
| 2261 | return JNI_TRUE; |
| 2262 | } |
| 2263 |
| 2264 | // --------------------------------------------------------------------------- |
| 2265 | // RGBA_8888 -> ALPHA_8: extract the alpha channel. |
| 2266 | // |
| 2267 | // For every pixel: |
| 2268 | // dst = alpha byte of the source pixel |
| 2269 | // |
| 2270 | // src : RGBA_8888 (4 bytes per pixel; alpha is the high byte on little-endian) |
| 2271 | // dst : ALPHA_8 (1 byte per pixel), same dimensions as src |
| 2272 | // |
| 2273 | // The hot loop reads each pixel as one 32-bit word and stores its high byte. |
| 2274 | // Reading a whole word and shifting is friendlier to the load unit than a |
| 2275 | // strided byte read of src[3], and lets the compiler vectorise the pass. |
| 2276 | // --------------------------------------------------------------------------- |
| 2277 |
| 2278 | // Contiguous fast path: both bitmaps have no row padding, so they are flat |
| 2279 | // buffers and the whole image is one pass over width*height pixels. |
| 2280 | static void extract_alpha_contiguous( |
| 2281 | const uint8_t * __restrict__ srcPx, |
| 2282 | uint8_t * __restrict__ dstPx, |
| 2283 | uint32_t width, uint32_t height) |
| 2284 | { |
| 2285 | const auto * __restrict__ src = reinterpret_cast<const uint32_t *>(srcPx); |
| 2286 | uint8_t * __restrict__ dst = dstPx; |
| 2287 | const uint32_t count = width * height; |
| 2288 |
| 2289 | for (uint32_t i = 0; i < count; ++i) { |
| 2290 | dst[i] = static_cast<uint8_t>(src[i] >> 24); // high byte = alpha |
| 2291 | } |
| 2292 | } |
| 2293 |
| 2294 | // General path: row-by-row, honouring each bitmap's stride. |
| 2295 | static void extract_alpha_strided( |
| 2296 | const uint8_t * __restrict__ srcPx, |
| 2297 | uint8_t * __restrict__ dstPx, |
| 2298 | uint32_t width, uint32_t height, |
| 2299 | uint32_t srcStride, uint32_t dstStride) |
| 2300 | { |
| 2301 | for (uint32_t y = 0; y < height; ++y) { |
| 2302 | const auto * __restrict__ src = |
| 2303 | reinterpret_cast<const uint32_t *>(srcPx + y * srcStride); |
| 2304 | uint8_t * __restrict__ dst = dstPx + y * dstStride; |
| 2305 |
| 2306 | for (uint32_t x = 0; x < width; ++x) { |
| 2307 | dst[x] = static_cast<uint8_t>(src[x] >> 24); // high byte = alpha |
| 2308 | } |
| 2309 | } |
| 2310 | } |
| 2311 |
| 2312 | // --------------------------------------------------------------------------- |
| 2313 | // JNI entry point |
| 2314 | // |
| 2315 | // Kotlin: external fun extractAlpha(src: Bitmap, dst: Bitmap): Boolean |
| 2316 | // [org.telegram.messenger.Utilities] |
| 2317 | // |
| 2318 | // src — RGBA_8888 |
| 2319 | // dst — ALPHA_8, same dimensions as src |
| 2320 | // |
| 2321 | // Returns true on success, false on error (size mismatch, unsupported format, |
| 2322 | // aliasing between the two bitmaps, or a failed lock). |
| 2323 | // --------------------------------------------------------------------------- |
| 2324 | extern "C" |
| 2325 | JNIEXPORT jboolean JNICALL |
| 2326 | Java_org_telegram_messenger_Utilities_extractAlpha( |
| 2327 | JNIEnv *env, |
| 2328 | jclass /*clazz*/, |
| 2329 | jobject srcBitmap, |
| 2330 | jobject dstBitmap) |
| 2331 | { |
| 2332 | if (__builtin_expect(env->IsSameObject(srcBitmap, dstBitmap), 0)) { |
| 2333 | return JNI_FALSE; |
| 2334 | } |
| 2335 |
| 2336 | AndroidBitmapInfo srcInfo{}; |
| 2337 | AndroidBitmapInfo dstInfo{}; |
| 2338 |
| 2339 | if (__builtin_expect( |
| 2340 | AndroidBitmap_getInfo(env, srcBitmap, &srcInfo) != ANDROID_BITMAP_RESULT_SUCCESS || |
| 2341 | AndroidBitmap_getInfo(env, dstBitmap, &dstInfo) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 2342 | 0)) { |
| 2343 | return JNI_FALSE; |
| 2344 | } |
| 2345 |
| 2346 | if (__builtin_expect( |
| 2347 | srcInfo.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || |
| 2348 | dstInfo.format != ANDROID_BITMAP_FORMAT_A_8 || |
| 2349 | srcInfo.width != dstInfo.width || |
| 2350 | srcInfo.height != dstInfo.height || |
| 2351 | srcInfo.width == 0 || |
| 2352 | srcInfo.height == 0 || |
| 2353 | srcInfo.stride < srcInfo.width * 4u || |
| 2354 | dstInfo.stride < dstInfo.width, |
| 2355 | 0)) { |
| 2356 | return JNI_FALSE; |
| 2357 | } |
| 2358 |
| 2359 | void *srcPixels = nullptr; |
| 2360 | void *dstPixels = nullptr; |
| 2361 |
| 2362 | if (__builtin_expect( |
| 2363 | AndroidBitmap_lockPixels(env, srcBitmap, &srcPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 2364 | 0)) { |
| 2365 | return JNI_FALSE; |
| 2366 | } |
| 2367 | if (__builtin_expect( |
| 2368 | AndroidBitmap_lockPixels(env, dstBitmap, &dstPixels) != ANDROID_BITMAP_RESULT_SUCCESS, |
| 2369 | 0)) { |
| 2370 | AndroidBitmap_unlockPixels(env, srcBitmap); |
| 2371 | return JNI_FALSE; |
| 2372 | } |
| 2373 |
| 2374 | // Contiguous when neither bitmap has row padding. The two formats have |
| 2375 | // different bytes-per-pixel, so each has its own "no padding" test. |
| 2376 | if (srcInfo.stride == srcInfo.width * 4u && dstInfo.stride == dstInfo.width) { |
| 2377 | extract_alpha_contiguous( |
| 2378 | static_cast<const uint8_t *>(srcPixels), |
| 2379 | static_cast<uint8_t *>(dstPixels), |
| 2380 | srcInfo.width, srcInfo.height); |
| 2381 | } else { |
| 2382 | extract_alpha_strided( |
| 2383 | static_cast<const uint8_t *>(srcPixels), |
| 2384 | static_cast<uint8_t *>(dstPixels), |
| 2385 | srcInfo.width, srcInfo.height, |
| 2386 | srcInfo.stride, dstInfo.stride); |
| 2387 | } |
| 2388 |
| 2389 | AndroidBitmap_unlockPixels(env, dstBitmap); |
| 2390 | AndroidBitmap_unlockPixels(env, srcBitmap); |
| 2391 | return JNI_TRUE; |
| 2392 | } |
| 2393 |
| 2394 | } |
| 2395 |
| 2396 |
| 2397 |
| 2398 |
| 2399 |
| 2400 | // --------------------------------------------------------------------------- |
| 2401 | // Fills an ARGB_8888 bitmap with the reply-line stripe pattern: |
| 2402 | // |
| 2403 | // - color1 background |
| 2404 | // - color2 diagonal bars (45°, sharp pixel edges) |
| 2405 | // - optionally color3 diagonal bars placed right after color2's |
| 2406 | // |
| 2407 | // The bitmap height IS the vertical period; a BitmapShader with |
| 2408 | // TileMode.REPEAT tiles it on the Java side. |
| 2409 | // |
| 2410 | // All bar colors are pre-composited by the caller via |
| 2411 | // ColorUtils.compositeColors(barColor, backgroundColor), so this |
| 2412 | // function writes flat pixel values — no alpha blending. |
| 2413 | // --------------------------------------------------------------------------- |
| 2414 |
| 2415 | #include <jni.h> |
| 2416 | #include <android/bitmap.h> |
| 2417 | #include <cstdint> |
| 2418 | #include <cstring> |
| 2419 |
| 2420 | // Maximum supported tile period (pixels). dp(19) ≈ 95 px at 5× density; |
| 2421 | // 256 is well above any realistic value and keeps the ref column on the stack. |
| 2422 | static constexpr uint32_t MAX_PERIOD = 256; |
| 2423 |
| 2424 | // --------------------------------------------------------------------------- |
| 2425 | // Converts a Java ARGB color int to the native RGBA_8888 pixel value. |
| 2426 | // |
| 2427 | // Java color int (0xAARRGGBB): A31-24 R23-16 G15-8 B7-0 |
| 2428 | // RGBA_8888 memory (little-endian byte order R, G, B, A) read as uint32_t: |
| 2429 | // R7-0 G15-8 B23-16 A31-24 → 0xAABBGGRR |
| 2430 | // |
| 2431 | // A and G are already in the right positions; swap R and B. |
| 2432 | // --------------------------------------------------------------------------- |
| 2433 | static inline uint32_t argb_to_native(uint32_t argb) { |
| 2434 | return (argb & 0xFF00FF00u) | // A and G stay |
| 2435 | ((argb >> 16) & 0xFFu) | // R → bits 7-0 |
| 2436 | ((argb & 0xFFu) << 16); // B → bits 23-16 |
| 2437 | } |
| 2438 |
| 2439 | // --------------------------------------------------------------------------- |
| 2440 | // Premultiplies a native RGBA pixel (R in low byte, A in high byte on LE). |
| 2441 | // |
| 2442 | // Android ARGB_8888 bitmaps are premultiplied by default; writing |
| 2443 | // straight-alpha pixels causes the renderer to mis-interpret RGB, |
| 2444 | // distorting colors whenever A < 255. |
| 2445 | // --------------------------------------------------------------------------- |
| 2446 | static inline uint32_t premultiply(uint32_t rgba) { |
| 2447 | const uint32_t a = rgba >> 24; |
| 2448 | if (a == 0xFF) return rgba; |
| 2449 | if (a == 0x00) return 0; |
| 2450 | const uint32_t r = ( rgba & 0xFFu) * a / 255u; |
| 2451 | const uint32_t g = ((rgba >> 8) & 0xFFu) * a / 255u; |
| 2452 | const uint32_t b = ((rgba >> 16) & 0xFFu) * a / 255u; |
| 2453 | return r | (g << 8) | (b << 16) | (a << 24); |
| 2454 | } |
| 2455 |
| 2456 | // --------------------------------------------------------------------------- |
| 2457 | // Per-byte average of two RGBA pixels (SWAR trick, no per-channel extract). |
| 2458 | // Computes floor((p + q) / 2) for each of the four bytes independently. |
| 2459 | // The 0x7F7F7F7F mask prevents carry leaking between adjacent bytes. |
| 2460 | // --------------------------------------------------------------------------- |
| 2461 | static inline uint32_t avg_color(uint32_t p, uint32_t q) { |
| 2462 | return (p & q) + (((p ^ q) >> 1) & 0x7F7F7F7Fu); |
| 2463 | } |
| 2464 |
| 2465 | // --------------------------------------------------------------------------- |
| 2466 | // Core pixel loop. |
| 2467 | // |
| 2468 | // The pattern within one period (= height): |
| 2469 | // [0, gapSize) → color1 (background) |
| 2470 | // [gapSize, gapSize + barHeight) → color2 (first bar) |
| 2471 | // [gapSize + barHeight, period) → color3 (second bar, if present) |
| 2472 | // |
| 2473 | // The 45° skew is applied per-column: column x reads from the reference |
| 2474 | // column at (y + x) % period. Since width ≤ period (checked by the |
| 2475 | // caller), y + x < 2·period, so a single compare-and-subtract replaces |
| 2476 | // the modulo. |
| 2477 | // --------------------------------------------------------------------------- |
| 2478 | static void draw_reply_line_pattern( |
| 2479 | uint8_t * __restrict__ pixels, |
| 2480 | uint32_t width, uint32_t height, uint32_t stride, |
| 2481 | uint32_t c1, uint32_t c2, uint32_t c3, |
| 2482 | uint32_t barHeight, bool hasColor3) |
| 2483 | { |
| 2484 | const uint32_t period = height; |
| 2485 | const uint32_t barsTotal = hasColor3 ? 2u * barHeight : barHeight; |
| 2486 | const uint32_t gapSize = period > barsTotal ? period - barsTotal : 0u; |
| 2487 | const uint32_t bar2End = gapSize + barHeight; |
| 2488 |
| 2489 | // Build a reference column (color for each y at x = 0). |
| 2490 | // At every boundary between two color regions the last pixel of the |
| 2491 | // outgoing region is replaced with the average of the two colors, |
| 2492 | // giving a 1-pixel anti-aliased transition on the 45° diagonal. |
| 2493 | uint32_t ref[MAX_PERIOD]; |
| 2494 | for (uint32_t i = 0; i < period; ++i) { |
| 2495 | uint32_t ci, cn; |
| 2496 |
| 2497 | if (i < gapSize) ci = c1; |
| 2498 | else if (i < bar2End) ci = c2; |
| 2499 | else ci = c3; |
| 2500 |
| 2501 | const uint32_t ni = (i + 1u < period) ? i + 1u : 0u; |
| 2502 | if (ni < gapSize) cn = c1; |
| 2503 | else if (ni < bar2End) cn = c2; |
| 2504 | else cn = c3; |
| 2505 |
| 2506 | ref[i] = (ci != cn) ? avg_color(ci, cn) : ci; |
| 2507 | } |
| 2508 |
| 2509 | // Fill each pixel: the 45° skew means column x reads from |
| 2510 | // ref[(y + x) % period]. Instead of a per-pixel branch we split |
| 2511 | // each row into at most two contiguous memcpy runs. |
| 2512 | for (uint32_t y = 0; y < height; ++y) { |
| 2513 | auto * __restrict__ row = |
| 2514 | reinterpret_cast<uint32_t *>(pixels + y * stride); |
| 2515 | const uint32_t split = period - y; |
| 2516 | if (split >= width) { |
| 2517 | memcpy(row, ref + y, width * sizeof(uint32_t)); |
| 2518 | } else { |
| 2519 | memcpy(row, ref + y, split * sizeof(uint32_t)); |
| 2520 | memcpy(row + split, ref, (width - split) * sizeof(uint32_t)); |
| 2521 | } |
| 2522 | } |
| 2523 | } |
| 2524 |
| 2525 | // --------------------------------------------------------------------------- |
| 2526 | // JNI entry point |
| 2527 | // |
| 2528 | // Java: public static native boolean drawReplyLinePattern( |
| 2529 | // Bitmap bitmap, int color1, int color2, int color3, |
| 2530 | // int barHeight, boolean hasColor3); |
| 2531 | // [org.telegram.messenger.Utilities] |
| 2532 | // |
| 2533 | // bitmap — mutable ARGB_8888, width × period |
| 2534 | // color1 — background (Java ARGB int, raw) |
| 2535 | // color2 — first bar color (Java ARGB int, composited over color1) |
| 2536 | // color3 — second bar color (Java ARGB int, composited over color1; |
| 2537 | // ignored when hasColor3 == false) |
| 2538 | // barHeight — height of each bar in pixels (> 0) |
| 2539 | // hasColor3 — whether the second bar is drawn |
| 2540 | // |
| 2541 | // Returns true on success, false on any validation or locking error. |
| 2542 | // --------------------------------------------------------------------------- |
| 2543 | extern "C" |
| 2544 | JNIEXPORT jboolean JNICALL |
| 2545 | Java_org_telegram_messenger_Utilities_drawReplyLinePattern( |
| 2546 | JNIEnv *env, |
| 2547 | jclass /*clazz*/, |
| 2548 | jobject bitmap, |
| 2549 | jint color1, |
| 2550 | jint color2, |
| 2551 | jint color3, |
| 2552 | jint barHeight, |
| 2553 | jboolean hasColor3) |
| 2554 | { |
| 2555 | if (__builtin_expect(!bitmap, 0)) { |
| 2556 | return JNI_FALSE; |
| 2557 | } |
| 2558 |
| 2559 | AndroidBitmapInfo info{}; |
| 2560 | if (__builtin_expect( |
| 2561 | AndroidBitmap_getInfo(env, bitmap, &info) |
| 2562 | != ANDROID_BITMAP_RESULT_SUCCESS, 0)) { |
| 2563 | return JNI_FALSE; |
| 2564 | } |
| 2565 |
| 2566 | if (__builtin_expect( |
| 2567 | info.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || |
| 2568 | info.width == 0 || |
| 2569 | info.height == 0 || |
| 2570 | info.height > MAX_PERIOD || |
| 2571 | info.width > info.height || |
| 2572 | info.stride < info.width * 4u || |
| 2573 | barHeight <= 0 || |
| 2574 | static_cast<uint32_t>(barHeight) > info.height, |
| 2575 | 0)) { |
| 2576 | return JNI_FALSE; |
| 2577 | } |
| 2578 |
| 2579 | void *pixels = nullptr; |
| 2580 | if (__builtin_expect( |
| 2581 | AndroidBitmap_lockPixels(env, bitmap, &pixels) |
| 2582 | != ANDROID_BITMAP_RESULT_SUCCESS, 0)) { |
| 2583 | return JNI_FALSE; |
| 2584 | } |
| 2585 |
| 2586 | draw_reply_line_pattern( |
| 2587 | static_cast<uint8_t *>(pixels), |
| 2588 | info.width, info.height, info.stride, |
| 2589 | premultiply(argb_to_native(static_cast<uint32_t>(color1))), |
| 2590 | premultiply(argb_to_native(static_cast<uint32_t>(color2))), |
| 2591 | hasColor3 ? premultiply(argb_to_native(static_cast<uint32_t>(color3))) : 0u, |
| 2592 | static_cast<uint32_t>(barHeight), |
| 2593 | hasColor3); |
| 2594 |
| 2595 | AndroidBitmap_unlockPixels(env, bitmap); |
| 2596 | return JNI_TRUE; |
| 2597 | } |