| 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 fastBlur(int imageWidth, int imageHeight, int imageStride, void *pixels) { |
| 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 radius = 3; |
| 18 | const int r1 = radius + 1; |
| 19 | const int div = radius * 2 + 1; |
| 20 |
| 21 | if (radius > 15 || div >= w || div >= h) { |
| 22 | return; |
| 23 | } |
| 24 |
| 25 | uint64_t rgb[imageStride * imageHeight]; |
| 26 |
| 27 | int x, y, i; |
| 28 |
| 29 | int yw = 0; |
| 30 | const int we = w - r1; |
| 31 | for (y = 0; y < h; y++) { |
| 32 | uint64_t cur = get_colors (&pix[yw]); |
| 33 | uint64_t rgballsum = -radius * cur; |
| 34 | uint64_t rgbsum = cur * ((r1 * (r1 + 1)) >> 1); |
| 35 |
| 36 | for (i = 1; i <= radius; i++) { |
| 37 | uint64_t cur = get_colors (&pix[yw + i * 4]); |
| 38 | rgbsum += cur * (r1 - i); |
| 39 | rgballsum += cur; |
| 40 | } |
| 41 |
| 42 | x = 0; |
| 43 |
| 44 | #define update(start, middle, end) \ |
| 45 | rgb[y * w + x] = (rgbsum >> 4) & 0x00FF00FF00FF00FFLL; \ |
| 46 | rgballsum += get_colors (&pix[yw + (start) * 4]) - 2 * get_colors (&pix[yw + (middle) * 4]) + get_colors (&pix[yw + (end) * 4]); \ |
| 47 | rgbsum += rgballsum; \ |
| 48 | x++; \ |
| 49 |
| 50 | while (x < r1) { |
| 51 | update (0, x, x + r1); |
| 52 | } |
| 53 | while (x < we) { |
| 54 | update (x - r1, x, x + r1); |
| 55 | } |
| 56 | while (x < w) { |
| 57 | update (x - r1, x, w - 1); |
| 58 | } |
| 59 |
| 60 | #undef update |
| 61 |
| 62 | yw += stride; |
| 63 | } |
| 64 |
| 65 | const int he = h - r1; |
| 66 | for (x = 0; x < w; x++) { |
| 67 | uint64_t rgballsum = -radius * rgb[x]; |
| 68 | uint64_t rgbsum = rgb[x] * ((r1 * (r1 + 1)) >> 1); |
| 69 | for (i = 1; i <= radius; i++) { |
| 70 | rgbsum += rgb[i * w + x] * (r1 - i); |
| 71 | rgballsum += rgb[i * w + x]; |
| 72 | } |
| 73 |
| 74 | y = 0; |
| 75 | int yi = x * 4; |
| 76 |
| 77 | #define update(start, middle, end) \ |
| 78 | int64_t res = rgbsum >> 4; \ |
| 79 | pix[yi] = res; \ |
| 80 | pix[yi + 1] = res >> 16; \ |
| 81 | pix[yi + 2] = res >> 32; \ |
| 82 | rgballsum += rgb[x + (start) * w] - 2 * rgb[x + (middle) * w] + rgb[x + (end) * w]; \ |
| 83 | rgbsum += rgballsum; \ |
| 84 | y++; \ |
| 85 | yi += stride; |
| 86 |
| 87 | while (y < r1) { |
| 88 | update (0, y, y + r1); |
| 89 | } |
| 90 | while (y < he) { |
| 91 | update (y - r1, y, y + r1); |
| 92 | } |
| 93 | while (y < h) { |
| 94 | update (y - r1, y, h - 1); |
| 95 | } |
| 96 | #undef update |
| 97 | } |
| 98 | } |
| 99 |
| 100 | typedef struct my_error_mgr { |
| 101 | struct jpeg_error_mgr pub; |
| 102 | jmp_buf setjmp_buffer; |
| 103 | } *my_error_ptr; |
| 104 |
| 105 |
| 106 | METHODDEF(void) my_error_exit(j_common_ptr cinfo) { |
| 107 | my_error_ptr myerr = (my_error_ptr) cinfo->err; |
| 108 | (*cinfo->err->output_message) (cinfo); |
| 109 | longjmp(myerr->setjmp_buffer, 1); |
| 110 | } |
| 111 |
| 112 | JNIEXPORT void Java_org_telegram_messenger_Utilities_blurBitmap(JNIEnv *env, jclass class, jobject bitmap, int width, int height, int stride) { |
| 113 | void *pixels = 0; |
| 114 | if (AndroidBitmap_lockPixels(env, bitmap, &pixels) < 0) { |
| 115 | return; |
| 116 | } |
| 117 | fastBlur(width, height, stride, pixels); |
| 118 | AndroidBitmap_unlockPixels(env, bitmap); |
| 119 | } |
| 120 |
| 121 | JNIEXPORT void Java_org_telegram_messenger_Utilities_loadBitmap(JNIEnv *env, jclass class, jstring path, jintArray bitmap, int scale, int format, int width, int height) { |
| 122 |
| 123 | int i; |
| 124 |
| 125 | char *fileName = (*env)->GetStringUTFChars(env, path, NULL); |
| 126 | FILE *infile; |
| 127 |
| 128 | if ((infile = fopen(fileName, "rb"))) { |
| 129 | struct my_error_mgr jerr; |
| 130 | struct jpeg_decompress_struct cinfo; |
| 131 |
| 132 | cinfo.err = jpeg_std_error(&jerr.pub); |
| 133 | jerr.pub.error_exit = my_error_exit; |
| 134 |
| 135 | if (!setjmp(jerr.setjmp_buffer)) { |
| 136 | unsigned char *bitmapBuf = (*env)->GetPrimitiveArrayCritical(env, bitmap, 0); |
| 137 | if (bitmapBuf) { |
| 138 | jpeg_create_decompress(&cinfo); |
| 139 | jpeg_stdio_src(&cinfo, infile); |
| 140 |
| 141 | jpeg_read_header(&cinfo, TRUE); |
| 142 |
| 143 | cinfo.scale_denom = scale; |
| 144 | cinfo.scale_num = 1; |
| 145 |
| 146 | jpeg_start_decompress(&cinfo); |
| 147 | int row_stride = cinfo.output_width * cinfo.output_components; |
| 148 | JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); |
| 149 | int stride = width; |
| 150 | if (format == 0) { |
| 151 | stride *= 4; |
| 152 | } else if (format == 1) { |
| 153 | stride *= 2; |
| 154 | } |
| 155 |
| 156 | unsigned char *pixels = bitmapBuf; |
| 157 |
| 158 | int rowCount = min(cinfo.output_height, height); |
| 159 | int colCount = min(cinfo.output_width, width); |
| 160 |
| 161 | while (cinfo.output_scanline < rowCount) { |
| 162 | jpeg_read_scanlines(&cinfo, buffer, 1); |
| 163 |
| 164 | if (format == 0) { |
| 165 | if (cinfo.out_color_space == JCS_GRAYSCALE) { |
| 166 | for (i = 0; i < colCount; i++) { |
| 167 | float alpha = buffer[0][i] / 255.0f; |
| 168 | pixels[i * 4] *= alpha; |
| 169 | pixels[i * 4 + 1] *= alpha; |
| 170 | pixels[i * 4 + 2] *= alpha; |
| 171 | pixels[i * 4 + 3] = buffer[0][i]; |
| 172 | } |
| 173 | } else { |
| 174 | int c = 0; |
| 175 | for (i = 0; i < colCount; i++) { |
| 176 | pixels[i * 4] = buffer[0][i * 3 + 2]; |
| 177 | pixels[i * 4 + 1] = buffer[0][i * 3 + 1]; |
| 178 | pixels[i * 4 + 2] = buffer[0][i * 3]; |
| 179 | pixels[i * 4 + 3] = 255; |
| 180 | c += 4; |
| 181 | } |
| 182 | } |
| 183 | } else if (format == 1) { |
| 184 |
| 185 | } |
| 186 |
| 187 | pixels += stride; |
| 188 | } |
| 189 | (*env)->ReleasePrimitiveArrayCritical(env, bitmap, bitmapBuf, 0); |
| 190 | jpeg_finish_decompress(&cinfo); |
| 191 | } else { |
| 192 | throwException(env, "can't get bitmap buff"); |
| 193 | } |
| 194 | } else { |
| 195 | throwException(env, "the JPEG code has signaled an error"); |
| 196 | } |
| 197 |
| 198 | jpeg_destroy_decompress(&cinfo); |
| 199 | fclose(infile); |
| 200 | } else { |
| 201 | throwException(env, "can't open %s", fileName); |
| 202 | } |
| 203 |
| 204 | (*env)->ReleaseStringUTFChars(env, path, fileName); |
| 205 | } |
| 206 |