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