| 6 | #include <png.h> /* if this fails, you need to install libpng-devel */ |
| 7 |
| 8 |
| 9 | typedef struct png_source_struct { |
| 10 | struct cjpeg_source_struct pub; |
| 11 | png_structp png_ptr; |
| 12 | png_infop info_ptr; |
| 13 | JDIMENSION current_row; |
| 14 | } png_source_struct; |
| 15 |
| 16 |
| 17 | METHODDEF(void) |
| 18 | finish_input_png (j_compress_ptr cinfo, cjpeg_source_ptr sinfo); |
| 19 |
| 20 | METHODDEF(JDIMENSION) |
| 21 | get_pixel_rows_png (j_compress_ptr cinfo, cjpeg_source_ptr sinfo); |
| 22 |
| 23 | METHODDEF(void) |
| 24 | start_input_png (j_compress_ptr cinfo, cjpeg_source_ptr sinfo); |
| 25 |
| 26 |
| 27 | GLOBAL(cjpeg_source_ptr) |
| 28 | jinit_read_png(j_compress_ptr cinfo) |
| 29 | { |
| 30 | png_source_struct *source = (*cinfo->mem->alloc_small)((j_common_ptr) cinfo, JPOOL_IMAGE, sizeof(png_source_struct)); |
| 31 |
| 32 | memset(source, 0, sizeof(*source)); |
| 33 |
| 34 | /* Fill in method ptrs, except get_pixel_rows which start_input sets */ |
| 35 | source->pub.start_input = start_input_png; |
| 36 | source->pub.finish_input = finish_input_png; |
| 37 |
| 38 | return &source->pub; |
| 39 | } |
| 40 |
| 41 | METHODDEF(void) error_input_png(png_structp png_ptr, png_const_charp msg) { |
| 42 | j_compress_ptr cinfo = png_get_error_ptr(png_ptr); |
| 43 | ERREXITS(cinfo, JERR_PNG_ERROR, msg); |
| 44 | } |
| 45 |
| 46 | /* This is a small ICC profile for sRGB */ |
| 47 | static unsigned char tiny_srgb[] = {0,0,2,24,108,99,109,115,2,16,0,0,109,110, |
| 48 | 116,114,82,71,66,32,88,89,90,32,7,220,0,1,0,25,0,3,0,41,0,57,97,99,115,112,65, |
| 49 | 80,80,76,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,246,214,0,1,0,0,0, |
| 50 | 0,211,45,108,99,109,115,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 51 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,10,100,101,115,99,0,0,0,252,0,0,0,106, |
| 52 | 99,112,114,116,0,0,1,104,0,0,0,11,119,116,112,116,0,0,1,116,0,0,0,20,98,107, |
| 53 | 112,116,0,0,1,136,0,0,0,20,114,88,89,90,0,0,1,156,0,0,0,20,103,88,89,90,0,0,1, |
| 54 | 176,0,0,0,20,98,88,89,90,0,0,1,196,0,0,0,20,114,84,82,67,0,0,1,216,0,0,0,64,98, |
| 55 | 84,82,67,0,0,1,216,0,0,0,64,103,84,82,67,0,0,1,216,0,0,0,64,100,101,115,99,0,0, |
| 56 | 0,0,0,0,0,13,115,82,71,66,32,77,111,122,74,80,69,71,0,0,0,0,0,0,0,0,1,0,0,0,0, |
| 57 | 1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, |
| 58 | 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,116,101,120, |
| 59 | 116,0,0,0,0,80,68,0,0,88,89,90,32,0,0,0,0,0,0,246,214,0,1,0,0,0,0,211,45,88,89, |
| 60 | 90,32,0,0,0,0,0,0,3,22,0,0,3,51,0,0,2,164,88,89,90,32,0,0,0,0,0,0,111,162,0,0, |
| 61 | 56,245,0,0,3,144,88,89,90,32,0,0,0,0,0,0,98,153,0,0,183,133,0,0,24,218,88,89, |
| 62 | 90,32,0,0,0,0,0,0,36,160,0,0,15,132,0,0,182,207,99,117,114,118,0,0,0,0,0,0,0, |
| 63 | 26,0,0,0,203,1,201,3,99,5,146,8,107,11,246,16,63,21,81,27,52,33,241,41,144,50, |
| 64 | 24,59,146,70,5,81,119,93,237,107,112,122,5,137,177,154,124,172,105,191,125,211, |
| 65 | 195,233,48,255,255,}; |
| 66 |
| 67 | METHODDEF(void) |
| 68 | start_input_png (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 69 | { |
| 70 | png_source_struct *source = (png_source_struct *)sinfo; |
| 71 | png_uint_32 width, height; |
| 72 | int bit_depth, color_type; |
| 73 | int has_srgb_chunk; |
| 74 | double gamma; |
| 75 | png_bytep profile; |
| 76 | png_charp unused1; |
| 77 | int unused2; |
| 78 | png_uint_32 proflen; |
| 79 | int has_profile; |
| 80 | size_t datalen; |
| 81 | JOCTET *dataptr; |
| 82 | struct jpeg_marker_struct *marker; |
| 83 | png_size_t rowbytes; |
| 84 |
| 85 | source->png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, cinfo, error_input_png, NULL); |
| 86 | source->info_ptr = png_create_info_struct(source->png_ptr); |
| 87 |
| 88 | if (!source->png_ptr || !source->info_ptr) { |
| 89 | ERREXITS(cinfo, JERR_PNG_ERROR, "Can't create read/info_struct"); |
| 90 | return; |
| 91 | } |
| 92 |
| 93 | png_set_palette_to_rgb(source->png_ptr); |
| 94 | png_set_expand_gray_1_2_4_to_8(source->png_ptr); |
| 95 | png_set_strip_alpha(source->png_ptr); |
| 96 | png_set_interlace_handling(source->png_ptr); |
| 97 |
| 98 | png_init_io(source->png_ptr, source->pub.input_file); |
| 99 | png_read_info(source->png_ptr, source->info_ptr); |
| 100 |
| 101 | png_get_IHDR(source->png_ptr, source->info_ptr, &width, &height, |
| 102 | &bit_depth, &color_type, NULL, NULL, NULL); |
| 103 |
| 104 | if (width > 65535 || height > 65535) { |
| 105 | ERREXITS(cinfo, JERR_PNG_ERROR, "Image too large"); |
| 106 | return; |
| 107 | } |
| 108 |
| 109 | if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) { |
| 110 | cinfo->in_color_space = JCS_GRAYSCALE; |
| 111 | cinfo->input_components = 1; |
| 112 | } else { |
| 113 | cinfo->in_color_space = JCS_RGB; |
| 114 | cinfo->input_components = 3; |
| 115 | } |
| 116 |
| 117 | if (bit_depth == 16) { |
| 118 | png_set_strip_16(source->png_ptr); |
| 119 | } |
| 120 |
| 121 | cinfo->data_precision = 8; |
| 122 | cinfo->image_width = width; |
| 123 | cinfo->image_height = height; |
| 124 |
| 125 | has_srgb_chunk = png_get_valid(source->png_ptr, source->info_ptr, PNG_INFO_sRGB); |
| 126 |
| 127 | gamma = 0.45455; |
| 128 | if (!has_srgb_chunk) { |
| 129 | png_get_gAMA(source->png_ptr, source->info_ptr, &gamma); |
| 130 | } |
| 131 | cinfo->input_gamma = gamma; |
| 132 | sinfo->get_pixel_rows = get_pixel_rows_png; |
| 133 |
| 134 | source->pub.marker_list = NULL; |
| 135 | profile = NULL; |
| 136 | unused1 = NULL; |
| 137 | unused2 = 0; |
| 138 | proflen = 0; |
| 139 | has_profile = 0; |
| 140 |
| 141 | if (has_srgb_chunk) { |
| 142 | /* PNG can declare use of an sRGB profile without embedding an ICC file, but JPEG doesn't have such feature */ |
| 143 | has_profile = 1; |
| 144 | profile = tiny_srgb; |
| 145 | proflen = sizeof(tiny_srgb); |
| 146 | } else { |
| 147 | has_profile = png_get_iCCP(source->png_ptr, source->info_ptr, &unused1, &unused2, &profile, &proflen); /* your libpng is out of date if you get a warning here */ |
| 148 | } |
| 149 |
| 150 | if (has_profile && profile && proflen) { |
| 151 | if (proflen < 65535-14) { |
| 152 | datalen = proflen + 14; |
| 153 | dataptr = (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_IMAGE, datalen); |
| 154 | memcpy(dataptr, "ICC_PROFILE\0\x01\x01", 14); |
| 155 | memcpy(dataptr + 14, profile, proflen); |
| 156 | marker = (*cinfo->mem->alloc_small)((j_common_ptr)cinfo, JPOOL_IMAGE, sizeof(struct jpeg_marker_struct)); |
| 157 | marker->next = NULL; |
| 158 | marker->marker = JPEG_APP0+2; |
| 159 | marker->original_length = 0; |
| 160 | marker->data_length = datalen; |
| 161 | marker->data = dataptr; |
| 162 | source->pub.marker_list = marker; |
| 163 | } else { |
| 164 | WARNMS(cinfo, JERR_PNG_PROFILETOOLARGE); |
| 165 | } |
| 166 | } |
| 167 |
| 168 | png_read_update_info(source->png_ptr, source->info_ptr); |
| 169 |
| 170 | rowbytes = png_get_rowbytes(source->png_ptr, source->info_ptr); |
| 171 |
| 172 | source->pub.buffer = (*cinfo->mem->alloc_sarray)((j_common_ptr)cinfo, JPOOL_IMAGE, (JDIMENSION)rowbytes, 1); |
| 173 | source->pub.buffer_height = 1; |
| 174 | } |
| 175 |
| 176 | METHODDEF(JDIMENSION) |
| 177 | get_pixel_rows_png (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 178 | { |
| 179 | png_source_struct *source = (png_source_struct *)sinfo; |
| 180 |
| 181 | png_read_row(source->png_ptr, source->pub.buffer[0], NULL); |
| 182 | return 1; |
| 183 | } |
| 184 |
| 185 | METHODDEF(void) |
| 186 | finish_input_png (j_compress_ptr cinfo, cjpeg_source_ptr sinfo) |
| 187 | { |
| 188 | png_source_struct *source = (png_source_struct *)sinfo; |
| 189 |
| 190 | png_read_end(source->png_ptr, source->info_ptr); |
| 191 | png_destroy_read_struct(&source->png_ptr, &source->info_ptr, NULL); |
| 192 | } |
| 193 |
| 194 | #endif |
| 195 |