| 5 | #include "utils.h" |
| 6 |
| 7 | typedef struct my_error_mgr { |
| 8 | struct jpeg_error_mgr pub; |
| 9 | jmp_buf setjmp_buffer; |
| 10 | } *my_error_ptr; |
| 11 |
| 12 |
| 13 | METHODDEF(void) my_error_exit(j_common_ptr cinfo) { |
| 14 | my_error_ptr myerr = (my_error_ptr) cinfo->err; |
| 15 | (*cinfo->err->output_message) (cinfo); |
| 16 | longjmp(myerr->setjmp_buffer, 1); |
| 17 | } |
| 18 |
| 19 | JNIEXPORT void Java_org_telegram_messenger_Utilities_loadBitmap(JNIEnv *env, jclass class, jstring path, jintArray bitmap, int scale, int format, int width, int height) { |
| 20 |
| 21 | int i; |
| 22 |
| 23 | char *fileName = (*env)->GetStringUTFChars(env, path, NULL); |
| 24 | FILE *infile; |
| 25 |
| 26 | if ((infile = fopen(fileName, "rb"))) { |
| 27 | struct my_error_mgr jerr; |
| 28 | struct jpeg_decompress_struct cinfo; |
| 29 |
| 30 | cinfo.err = jpeg_std_error(&jerr.pub); |
| 31 | jerr.pub.error_exit = my_error_exit; |
| 32 |
| 33 | if (!setjmp(jerr.setjmp_buffer)) { |
| 34 | unsigned char *bitmapBuf = (*env)->GetPrimitiveArrayCritical(env, bitmap, 0); |
| 35 | if (bitmapBuf) { |
| 36 | jpeg_create_decompress(&cinfo); |
| 37 | jpeg_stdio_src(&cinfo, infile); |
| 38 |
| 39 | jpeg_read_header(&cinfo, TRUE); |
| 40 |
| 41 | cinfo.scale_denom = scale; |
| 42 | cinfo.scale_num = 1; |
| 43 |
| 44 | jpeg_start_decompress(&cinfo); |
| 45 | int row_stride = cinfo.output_width * cinfo.output_components; |
| 46 | JSAMPARRAY buffer = (*cinfo.mem->alloc_sarray) ((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); |
| 47 | int stride = width; |
| 48 | if (format == 0) { |
| 49 | stride *= 4; |
| 50 | } else if (format == 1) { |
| 51 | stride *= 2; |
| 52 | } |
| 53 |
| 54 | unsigned char *pixels = bitmapBuf; |
| 55 |
| 56 | int rowCount = min(cinfo.output_height, height); |
| 57 | int colCount = min(cinfo.output_width, width); |
| 58 |
| 59 | while (cinfo.output_scanline < rowCount) { |
| 60 | jpeg_read_scanlines(&cinfo, buffer, 1); |
| 61 |
| 62 | if (format == 0) { |
| 63 | if (cinfo.out_color_space == JCS_GRAYSCALE) { |
| 64 | for (i = 0; i < colCount; i++) { |
| 65 | float alpha = buffer[0][i] / 255.0f; |
| 66 | pixels[i * 4] *= alpha; |
| 67 | pixels[i * 4 + 1] *= alpha; |
| 68 | pixels[i * 4 + 2] *= alpha; |
| 69 | pixels[i * 4 + 3] = buffer[0][i]; |
| 70 | } |
| 71 | } else { |
| 72 | int c = 0; |
| 73 | for (i = 0; i < colCount; i++) { |
| 74 | pixels[i * 4] = buffer[0][i * 3 + 2]; |
| 75 | pixels[i * 4 + 1] = buffer[0][i * 3 + 1]; |
| 76 | pixels[i * 4 + 2] = buffer[0][i * 3]; |
| 77 | pixels[i * 4 + 3] = 255; |
| 78 | c += 4; |
| 79 | } |
| 80 | } |
| 81 | } else if (format == 1) { |
| 82 |
| 83 | } |
| 84 |
| 85 | pixels += stride; |
| 86 | } |
| 87 | (*env)->ReleasePrimitiveArrayCritical(env, bitmap, bitmapBuf, 0); |
| 88 | jpeg_finish_decompress(&cinfo); |
| 89 | } else { |
| 90 | throwException(env, "can't get bitmap buff"); |
| 91 | } |
| 92 | } else { |
| 93 | throwException(env, "the JPEG code has signaled an error"); |
| 94 | } |
| 95 |
| 96 | jpeg_destroy_decompress(&cinfo); |
| 97 | fclose(infile); |
| 98 | } else { |
| 99 | throwException(env, "can't open %s", fileName); |
| 100 | } |
| 101 |
| 102 | (*env)->ReleaseStringUTFChars(env, path, fileName); |
| 103 | } |
| 104 |