| 5 | #include <android/bitmap.h> |
| 6 | #include "utils.h" |
| 7 |
| 8 | static inline uint64_t get_colors (const uint8_t *p) { |
| 9 | return p[0] + (p[1] << 16) + ((uint64_t)p[2] << 32); |
| 10 | } |
| 11 |
| 12 | static void fastBlurMore(int imageWidth, int imageHeight, int imageStride, void *pixels, int radius) { |
| 13 | uint8_t *pix = (uint8_t *)pixels; |
| 14 | const int w = imageWidth; |
| 15 | const int h = imageHeight; |
| 16 | const int stride = imageStride; |
| 17 | const int r1 = radius + 1; |
| 18 | const int div = radius * 2 + 1; |
| 19 |
| 20 | if (radius > 15 || div >= w || div >= h || w * h > 90 * 90 || imageStride > imageWidth * 4) { |
| 21 | return; |
| 22 | } |
| 23 |
| 24 | uint64_t *rgb = malloc(imageWidth * imageHeight * sizeof(uint64_t)); |
| 25 | if (rgb == NULL) { |
| 26 | return; |
| 27 | } |
| 28 |
| 29 | int x, y, i; |
| 30 |
| 31 | int yw = 0; |
| 32 | const int we = w - r1; |
| 33 | for (y = 0; y < h; y++) { |
| 34 | uint64_t cur = get_colors (&pix[yw]); |
| 35 | uint64_t rgballsum = -radius * cur; |
| 36 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 37 |
| 38 | for (i = 1; i <= radius; i++) { |
| 39 | uint64_t cur = get_colors (&pix[yw + i * 4]); |
| 40 | rgbsum += cur * (r1 - i); |
| 41 | rgballsum += cur; |
| 42 | } |
| 43 |
| 44 | x = 0; |
| 45 |
| 46 | #define update(start, middle, end) \ |
| 47 | rgb[y * w + x] = (rgbsum >> 6) & 0x00FF00FF00FF00FF; \ |
| 48 | rgballsum += get_colors (&pix[yw + (start) * 4]) - 2 * get_colors (&pix[yw + (middle) * 4]) + get_colors (&pix[yw + (end) * 4]); \ |
| 49 | rgbsum += rgballsum; \ |
| 50 | x++; \ |
| 51 |
| 52 | while (x < r1) { |
| 53 | update (0, x, x + r1); |
| 54 | } |
| 55 | while (x < we) { |
| 56 | update (x - r1, x, x + r1); |
| 57 | } |
| 58 | while (x < w) { |
| 59 | update (x - r1, x, w - 1); |
| 60 | } |
| 61 | #undef update |
| 62 |
| 63 | yw += stride; |
| 64 | } |
| 65 |
| 66 | const int he = h - r1; |
| 67 | for (x = 0; x < w; x++) { |
| 68 | uint64_t rgballsum = -radius * rgb[x]; |
| 69 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 70 | for (i = 1; i <= radius; i++) { |
| 71 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 72 | rgballsum += rgb[i * w + x]; |
| 73 | } |
| 74 |
| 75 | y = 0; |
| 76 | int yi = x * 4; |
| 77 |
| 78 | #define update(start, middle, end) \ |
| 79 | int64_t res = rgbsum >> 6; \ |
| 80 | pix[yi] = res; \ |
| 81 | pix[yi + 1] = res >> 16; \ |
| 82 | pix[yi + 2] = res >> 32; \ |
| 83 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 84 | rgbsum += rgballsum; \ |
| 85 | y++; \ |
| 86 | yi += stride; |
| 87 |
| 88 | while (y < r1) { |
| 89 | update (0, y, y + r1); |
| 90 | } |
| 91 | while (y < he) { |
| 92 | update (y - r1, y, y + r1); |
| 93 | } |
| 94 | while (y < h) { |
| 95 | update (y - r1, y, h - 1); |
| 96 | } |
| 97 | #undef update |
| 98 | } |
| 99 | } |
| 100 |
| 101 | static void fastBlur(int imageWidth, int imageHeight, int imageStride, void *pixels, int radius) { |
| 102 | uint8_t *pix = (uint8_t *)pixels; |
| 103 | const int w = imageWidth; |
| 104 | const int h = imageHeight; |
| 105 | const int stride = imageStride; |
| 106 | const int r1 = radius + 1; |
| 107 | const int div = radius * 2 + 1; |
| 108 |
| 109 | if (radius > 15 || div >= w || div >= h || w * h > 90 * 90 || imageStride > imageWidth * 4) { |
| 110 | return; |
| 111 | } |
| 112 |
| 113 | uint64_t *rgb = malloc(imageWidth * imageHeight * sizeof(uint64_t)); |
| 114 | if (rgb == NULL) { |
| 115 | return; |
| 116 | } |
| 117 |
| 118 | int x, y, i; |
| 119 |
| 120 | int yw = 0; |
| 121 | const int we = w - r1; |
| 122 | for (y = 0; y < h; y++) { |
| 123 | uint64_t cur = get_colors (&pix[yw]); |
| 124 | uint64_t rgballsum = -radius * cur; |
| 125 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 126 |
| 127 | for (i = 1; i <= radius; i++) { |
| 128 | uint64_t cur = get_colors (&pix[yw + i * 4]); |
| 129 | rgbsum += cur * (r1 - i); |
| 130 | rgballsum += cur; |
| 131 | } |
| 132 |
| 133 | x = 0; |
| 134 |
| 135 | #define update(start, middle, end) \ |
| 136 | rgb[y * w + x] = (rgbsum >> 4) & 0x00FF00FF00FF00FFLL; \ |
| 137 | rgballsum += get_colors (&pix[yw + (start) * 4]) - 2 * get_colors (&pix[yw + (middle) * 4]) + get_colors (&pix[yw + (end) * 4]); \ |
| 138 | rgbsum += rgballsum; \ |
| 139 | x++; \ |
| 140 |
| 141 | while (x < r1) { |
| 142 | update (0, x, x + r1); |
| 143 | } |
| 144 | while (x < we) { |
| 145 | update (x - r1, x, x + r1); |
| 146 | } |
| 147 | while (x < w) { |
| 148 | update (x - r1, x, w - 1); |
| 149 | } |
| 150 |
| 151 | #undef update |
| 152 |
| 153 | yw += stride; |
| 154 | } |
| 155 |
| 156 | const int he = h - r1; |
| 157 | for (x = 0; x < w; x++) { |
| 158 | uint64_t rgballsum = -radius * rgb[x]; |
| 159 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 160 | for (i = 1; i <= radius; i++) { |
| 161 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 162 | rgballsum += rgb[i * w + x]; |
| 163 | } |
| 164 |
| 165 | y = 0; |
| 166 | int yi = x * 4; |
| 167 |
| 168 | #define update(start, middle, end) \ |
| 169 | int64_t res = rgbsum >> 4; \ |
| 170 | pix[yi] = res; \ |
| 171 | pix[yi + 1] = res >> 16; \ |
| 172 | pix[yi + 2] = res >> 32; \ |
| 173 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 174 | rgbsum += rgballsum; \ |
| 175 | y++; \ |
| 176 | yi += stride; |
| 177 |
| 178 | while (y < r1) { |
| 179 | update (0, y, y + r1); |
| 180 | } |
| 181 | while (y < he) { |
| 182 | update (y - r1, y, y + r1); |
| 183 | } |
| 184 | while (y < h) { |
| 185 | update (y - r1, y, h - 1); |
| 186 | } |
| 187 | #undef update |
| 188 | } |
| 189 |
| 190 | free(rgb); |
| 191 | } |
| 192 |
| 193 | typedef struct my_error_mgr { |
| 194 | struct jpeg_error_mgr pub; |
| 195 | jmp_buf setjmp_buffer; |
| 196 | } *my_error_ptr; |
| 197 |
| 198 |
| 199 | METHODDEF(void) my_error_exit(j_common_ptr cinfo) { |
| 200 | my_error_ptr myerr = (my_error_ptr) cinfo->err; |
| 201 | (*cinfo->err->output_message) (cinfo); |
| 202 | longjmp(myerr->setjmp_buffer, 1); |
| 203 | } |
| 204 |
| 205 | JNIEXPORT void Java_org_telegram_messenger_Utilities_blurBitmap(JNIEnv *env, jclass class, jobject bitmap, int radius) { |
| 206 | if (!bitmap) { |
| 207 | return; |
| 208 | } |
| 209 |
| 210 | AndroidBitmapInfo info; |
| 211 |
| 212 | if (AndroidBitmap_getInfo(env, bitmap, &info) < 0) { |
| 213 | return; |
| 214 | } |
| 215 |
| 216 | if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888 || !info.width || !info.height || !info.stride) { |
| 217 | return; |
| 218 | } |
| 219 |
| 220 | void *pixels = 0; |
| 221 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { |
| 222 | return; |
| 223 | } |
| 224 | if (radius <= 3) { |
| 225 | fastBlur(info.width, info.height, info.stride, pixels, radius); |
| 226 | } else { |
| 227 | fastBlurMore(info.width, info.height, info.stride, pixels, radius); |
| 228 | } |
| 229 | AndroidBitmap_unlockPixels(env, bitmap); |
| 230 | } |
| 231 |
| 232 | JNIEXPORT void Java_org_telegram_messenger_Utilities_loadBitmap(JNIEnv *env, jclass class, jstring path, jintArray bitmap, int scale, int format, int width, int height) { |
| 233 |
| 234 | int i; |
| 235 |
| 236 | char *fileName = (*env)->GetStringUTFChars(env, path, NULL); |
| 237 | FILE *infile; |
| 238 |
| 239 | if ((infile = fopen(fileName, "rb"))) { |
| 240 | struct my_error_mgr jerr; |
| 241 | struct jpeg_decompress_struct cinfo; |
| 242 |
| 243 | cinfo.err = jpeg_std_error(&jerr.pub); |
| 244 | jerr.pub.error_exit = my_error_exit; |
| 245 |
| 246 | if (!setjmp(jerr.setjmp_buffer)) { |
| 247 | unsigned char *bitmapBuf = (*env)->GetPrimitiveArrayCritical(env, bitmap, 0); |
| 248 | if (bitmapBuf) { |
| 249 | jpeg_create_decompress(&cinfo); |
| 250 | jpeg_stdio_src(&cinfo, infile); |
| 251 |
| 252 | jpeg_read_header(&cinfo, TRUE); |
| 253 |
| 254 | cinfo.scale_denom = scale; |
| 255 | cinfo.scale_num = 1; |
| 256 |
| 257 | jpeg_start_decompress(&cinfo); |
| 258 | int row_stride = cinfo.output_width * cinfo.output_components; |
| 259 | JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); |
| 260 | int stride = width; |
| 261 | if (format == 0) { |
| 262 | stride *= 4; |
| 263 | } else if (format == 1) { |
| 264 | stride *= 2; |
| 265 | } |
| 266 |
| 267 | unsigned char *pixels = bitmapBuf; |
| 268 |
| 269 | int rowCount = min(cinfo.output_height, height); |
| 270 | int colCount = min(cinfo.output_width, width); |
| 271 |
| 272 | while (cinfo.output_scanline < rowCount) { |
| 273 | jpeg_read_scanlines(&cinfo, buffer, 1); |
| 274 |
| 275 | if (format == 0) { |
| 276 | if (cinfo.out_color_space == JCS_GRAYSCALE) { |
| 277 | for (i = 0; i < colCount; i++) { |
| 278 | float alpha = buffer[0][i] / 255.0f; |
| 279 | pixels[i * 4] *= alpha; |
| 280 | pixels[i * 4 + 1] *= alpha; |
| 281 | pixels[i * 4 + 2] *= alpha; |
| 282 | pixels[i * 4 + 3] = buffer[0][i]; |
| 283 | } |
| 284 | } else { |
| 285 | int c = 0; |
| 286 | for (i = 0; i < colCount; i++) { |
| 287 | pixels[i * 4] = buffer[0][i * 3 + 2]; |
| 288 | pixels[i * 4 + 1] = buffer[0][i * 3 + 1]; |
| 289 | pixels[i * 4 + 2] = buffer[0][i * 3]; |
| 290 | pixels[i * 4 + 3] = 255; |
| 291 | c += 4; |
| 292 | } |
| 293 | } |
| 294 | } else if (format == 1) { |
| 295 |
| 296 | } |
| 297 |
| 298 | pixels += stride; |
| 299 | } |
| 300 | (*env)->ReleasePrimitiveArrayCritical(env, bitmap, bitmapBuf, 0); |
| 301 | jpeg_finish_decompress(&cinfo); |
| 302 | } else { |
| 303 | throwException(env, "can't get bitmap buff"); |
| 304 | } |
| 305 | } else { |
| 306 | throwException(env, "the JPEG code has signaled an error"); |
| 307 | } |
| 308 |
| 309 | jpeg_destroy_decompress(&cinfo); |
| 310 | fclose(infile); |
| 311 | } else { |
| 312 | throwException(env, "can't open %s", fileName); |
| 313 | } |
| 314 |
| 315 | (*env)->ReleaseStringUTFChars(env, path, fileName); |
| 316 | } |
| 317 |