31 32 #define SIG_BYTES 8 33 34 void PNGAPI 35 my_png_read_stream(png_structp png_ptr, png_bytep data, png_size_t length) 36 { 37 png_uint_32 check; 38 39 SplashStream * stream = (SplashStream*)png_get_io_ptr(png_ptr); 40 check = stream->read(stream, data, length); 41 if (check != length) 42 png_error(png_ptr, "Read Error"); 43 } 44 45 int 46 SplashDecodePng(Splash * splash, png_rw_ptr read_func, void *io_ptr) 47 { 48 int stride; 49 ImageFormat srcFormat; 50 png_uint_32 i, rowbytes; 51 png_bytepp row_pointers = NULL; 52 png_bytep image_data = NULL; 53 int success = 0; 54 double gamma; 55 56 png_structp png_ptr = NULL; 57 png_infop info_ptr = NULL; 58 59 png_uint_32 width, height; 60 int bit_depth, color_type; 61 62 ImageRect srcRect, dstRect; 63 64 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 65 if (!png_ptr) { 66 goto done; 67 } 68 69 info_ptr = png_create_info_struct(png_ptr); 70 if (!info_ptr) { 71 goto done; 72 } 73 74 #ifdef __APPLE__ 75 /* use setjmp/longjmp versions that do not save/restore the signal mask */ 76 if (_setjmp(png_set_longjmp_fn(png_ptr, _longjmp, sizeof(jmp_buf)))) { 77 #else 78 if (setjmp(png_jmpbuf(png_ptr))) { 79 #endif 80 goto done; 81 } 82 83 png_set_read_fn(png_ptr, io_ptr, read_func); 84 85 png_set_sig_bytes(png_ptr, SIG_BYTES); /* we already read the 8 signature bytes */ 86 87 png_read_info(png_ptr, info_ptr); /* read all PNG info up to image data */ 88 89 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 90 NULL, NULL, NULL); 91 92 /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits, 93 * transparency chunks to full alpha channel; strip 16-bit-per-sample 94 * images to 8 bits per sample; and convert grayscale to RGB[A] 95 * this may be sub-optimal but this simplifies implementation */ 96 97 png_set_expand(png_ptr); 98 png_set_tRNS_to_alpha(png_ptr); 99 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); 100 png_set_strip_16(png_ptr); 101 png_set_gray_to_rgb(png_ptr); | 31 32 #define SIG_BYTES 8 33 34 void PNGAPI 35 my_png_read_stream(png_structp png_ptr, png_bytep data, png_size_t length) 36 { 37 png_uint_32 check; 38 39 SplashStream * stream = (SplashStream*)png_get_io_ptr(png_ptr); 40 check = stream->read(stream, data, length); 41 if (check != length) 42 png_error(png_ptr, "Read Error"); 43 } 44 45 int 46 SplashDecodePng(Splash * splash, png_rw_ptr read_func, void *io_ptr) 47 { 48 int stride; 49 ImageFormat srcFormat; 50 png_uint_32 i, rowbytes; 51 png_bytepp row_pointers; 52 png_bytep image_data; 53 int success = 0; 54 double gamma; 55 56 png_structp png_ptr = NULL; 57 png_infop info_ptr = NULL; 58 59 png_uint_32 width, height; 60 int bit_depth, color_type; 61 62 ImageRect srcRect, dstRect; 63 64 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); 65 if (!png_ptr) { 66 row_pointers = NULL; 67 image_data = NULL; 68 goto done; 69 } 70 71 info_ptr = png_create_info_struct(png_ptr); 72 if (!info_ptr) { 73 row_pointers = NULL; 74 image_data = NULL; 75 goto done; 76 } 77 78 #ifdef __APPLE__ 79 /* use setjmp/longjmp versions that do not save/restore the signal mask */ 80 if (_setjmp(png_set_longjmp_fn(png_ptr, _longjmp, sizeof(jmp_buf)))) { 81 #else 82 if (setjmp(png_jmpbuf(png_ptr))) { 83 #endif 84 row_pointers = NULL; 85 image_data = NULL; 86 goto done; 87 } 88 89 row_pointers = NULL; 90 image_data = NULL; 91 92 png_set_read_fn(png_ptr, io_ptr, read_func); 93 94 png_set_sig_bytes(png_ptr, SIG_BYTES); /* we already read the 8 signature bytes */ 95 96 png_read_info(png_ptr, info_ptr); /* read all PNG info up to image data */ 97 98 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 99 NULL, NULL, NULL); 100 101 /* expand palette images to RGB, low-bit-depth grayscale images to 8 bits, 102 * transparency chunks to full alpha channel; strip 16-bit-per-sample 103 * images to 8 bits per sample; and convert grayscale to RGB[A] 104 * this may be sub-optimal but this simplifies implementation */ 105 106 png_set_expand(png_ptr); 107 png_set_tRNS_to_alpha(png_ptr); 108 png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); 109 png_set_strip_16(png_ptr); 110 png_set_gray_to_rgb(png_ptr); |