]>
Commit | Line | Data |
---|---|---|
790923a4 | 1 | #include "iolayer.h" |
02d1d628 AMH |
2 | #include "image.h" |
3 | #include "png.h" | |
4 | ||
5 | /* Check to see if a file is a PNG file using png_sig_cmp(). png_sig_cmp() | |
6 | * returns zero if the image is a PNG and nonzero if it isn't a PNG. | |
7 | * | |
8 | * The function check_if_png() shown here, but not used, returns nonzero (true) | |
9 | * if the file can be opened and is a PNG, 0 (false) otherwise. | |
10 | * | |
11 | * If this call is successful, and you are going to keep the file open, | |
12 | * you should call png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); once | |
13 | * you have created the png_ptr, so that libpng knows your application | |
14 | * has read that many bytes from the start of the file. Make sure you | |
15 | * don't call png_set_sig_bytes() with more than 8 bytes read or give it | |
16 | * an incorrect number of bytes read, or you will either have read too | |
17 | * many bytes (your fault), or you are telling libpng to read the wrong | |
18 | * number of magic bytes (also your fault). | |
19 | * | |
20 | * Many applications already read the first 2 or 4 bytes from the start | |
21 | * of the image to determine the file type, so it would be easiest just | |
22 | * to pass the bytes to png_sig_cmp() or even skip that if you know | |
23 | * you have a PNG file, and call png_set_sig_bytes(). | |
24 | */ | |
25 | ||
26 | /* this is a way to get number of channels from color space | |
27 | * Color code to channel number */ | |
28 | ||
b33c08f8 | 29 | static int CC2C[PNG_COLOR_MASK_PALETTE|PNG_COLOR_MASK_COLOR|PNG_COLOR_MASK_ALPHA]; |
02d1d628 AMH |
30 | |
31 | #define PNG_BYTES_TO_CHECK 4 | |
32 | ||
33 | ||
34 | ||
02d1d628 | 35 | static void |
790923a4 AMH |
36 | wiol_read_data(png_structp png_ptr, png_bytep data, png_size_t length) { |
37 | io_glue *ig = (io_glue *)png_ptr->io_ptr; | |
38 | int rc = ig->readcb(ig, data, length); | |
39 | if (rc != length) png_error(png_ptr, "Read overflow error on an iolayer source."); | |
02d1d628 AMH |
40 | } |
41 | ||
02d1d628 | 42 | static void |
790923a4 AMH |
43 | wiol_write_data(png_structp png_ptr, png_bytep data, png_size_t length) { |
44 | int rc; | |
45 | io_glue *ig = (io_glue *)png_ptr->io_ptr; | |
46 | rc = ig->writecb(ig, data, length); | |
47 | if (rc != length) png_error(png_ptr, "Write error on an iolayer source."); | |
02d1d628 AMH |
48 | } |
49 | ||
50 | static void | |
790923a4 AMH |
51 | wiol_flush_data(png_structp png_ptr) { |
52 | /* XXX : This needs to be added to the io layer */ | |
02d1d628 | 53 | } |
02d1d628 AMH |
54 | |
55 | ||
15dc61b6 AMH |
56 | /* Check function demo |
57 | ||
02d1d628 AMH |
58 | int |
59 | check_if_png(char *file_name, FILE **fp) { | |
790923a4 | 60 | char buf[PNG_BYTES_TO_CHECK]; |
790923a4 | 61 | if ((*fp = fopen(file_name, "rb")) != NULL) return 0; |
790923a4 | 62 | if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK) return 0; |
790923a4 | 63 | return(!png_sig_cmp((png_bytep)buf, (png_size_t)0, PNG_BYTES_TO_CHECK)); |
02d1d628 | 64 | } |
15dc61b6 | 65 | */ |
02d1d628 | 66 | |
02d1d628 | 67 | undef_int |
790923a4 | 68 | i_writepng_wiol(i_img *im, io_glue *ig) { |
02d1d628 AMH |
69 | png_structp png_ptr; |
70 | png_infop info_ptr; | |
71 | int width,height,y; | |
72 | volatile int cspace,channels; | |
faa9b3e7 TC |
73 | double xres, yres; |
74 | int aspect_only, have_res; | |
75 | double offx, offy; | |
76 | char offunit[20] = "pixel"; | |
02d1d628 | 77 | |
790923a4 AMH |
78 | io_glue_commit_types(ig); |
79 | mm_log((1,"i_writepng(im %p ,ig %p)\n", im, ig)); | |
02d1d628 | 80 | |
790923a4 AMH |
81 | height = im->ysize; |
82 | width = im->xsize; | |
02d1d628 AMH |
83 | |
84 | channels=im->channels; | |
85 | ||
790923a4 | 86 | if (channels > 2) { cspace = PNG_COLOR_TYPE_RGB; channels-=3; } |
02d1d628 AMH |
87 | else { cspace=PNG_COLOR_TYPE_GRAY; channels--; } |
88 | ||
89 | if (channels) cspace|=PNG_COLOR_MASK_ALPHA; | |
90 | mm_log((1,"cspace=%d\n",cspace)); | |
91 | ||
790923a4 | 92 | channels = im->channels; |
02d1d628 AMH |
93 | |
94 | /* Create and initialize the png_struct with the desired error handler | |
95 | * functions. If you want to use the default stderr and longjump method, | |
96 | * you can supply NULL for the last three parameters. We also check that | |
97 | * the library version is compatible with the one used at compile time, | |
98 | * in case we are using dynamically linked libraries. REQUIRED. | |
99 | */ | |
100 | ||
101 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL); | |
102 | ||
790923a4 AMH |
103 | if (png_ptr == NULL) return 0; |
104 | ||
02d1d628 AMH |
105 | |
106 | /* Allocate/initialize the image information data. REQUIRED */ | |
107 | info_ptr = png_create_info_struct(png_ptr); | |
108 | ||
109 | if (info_ptr == NULL) { | |
02d1d628 | 110 | png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
790923a4 | 111 | return 0; |
02d1d628 AMH |
112 | } |
113 | ||
114 | /* Set error handling. REQUIRED if you aren't supplying your own | |
115 | * error hadnling functions in the png_create_write_struct() call. | |
116 | */ | |
117 | if (setjmp(png_ptr->jmpbuf)) { | |
02d1d628 AMH |
118 | png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
119 | return(0); | |
120 | } | |
121 | ||
790923a4 AMH |
122 | png_set_write_fn(png_ptr, (png_voidp) (ig), wiol_write_data, wiol_flush_data); |
123 | png_ptr->io_ptr = (png_voidp) ig; | |
02d1d628 AMH |
124 | |
125 | /* Set the image information here. Width and height are up to 2^31, | |
126 | * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on | |
127 | * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY, | |
128 | * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, | |
129 | * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or | |
130 | * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST | |
131 | * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED | |
132 | */ | |
133 | ||
134 | png_set_IHDR(png_ptr, info_ptr, width, height, 8, cspace, | |
135 | PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); | |
136 | ||
faa9b3e7 TC |
137 | have_res = 1; |
138 | if (i_tags_get_float(&im->tags, "i_xres", 0, &xres)) { | |
139 | if (i_tags_get_float(&im->tags, "i_yres", 0, &yres)) | |
140 | ; /* nothing to do */ | |
141 | else | |
142 | yres = xres; | |
143 | } | |
144 | else { | |
145 | if (i_tags_get_float(&im->tags, "i_yres", 0, &yres)) | |
146 | xres = yres; | |
147 | else | |
148 | have_res = 0; | |
149 | } | |
150 | if (have_res) { | |
151 | aspect_only = 0; | |
152 | i_tags_get_int(&im->tags, "i_aspect_only", 0, &aspect_only); | |
153 | xres /= 0.0254; | |
154 | yres /= 0.0254; | |
155 | png_set_pHYs(png_ptr, info_ptr, xres + 0.5, yres + 0.5, | |
156 | aspect_only ? PNG_RESOLUTION_UNKNOWN : PNG_RESOLUTION_METER); | |
157 | } | |
158 | ||
02d1d628 | 159 | png_write_info(png_ptr, info_ptr); |
790923a4 | 160 | |
faa9b3e7 TC |
161 | if (!im->virtual && im->type == i_direct_type && im->bits == i_8_bits) { |
162 | for (y = 0; y < height; y++) | |
163 | png_write_row(png_ptr, (png_bytep) &(im->idata[channels*width*y])); | |
164 | } | |
165 | else { | |
166 | unsigned char *data = mymalloc(im->xsize * im->channels); | |
167 | if (data) { | |
168 | for (y = 0; y < height; y++) { | |
169 | i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels); | |
170 | png_write_row(png_ptr, (png_bytep)data); | |
171 | } | |
172 | myfree(data); | |
173 | } | |
174 | else { | |
faa9b3e7 TC |
175 | png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
176 | return 0; | |
177 | } | |
178 | } | |
790923a4 | 179 | |
02d1d628 | 180 | png_write_end(png_ptr, info_ptr); |
790923a4 | 181 | |
02d1d628 AMH |
182 | png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
183 | ||
10461f9a TC |
184 | ig->closecb(ig); |
185 | ||
02d1d628 AMH |
186 | return(1); |
187 | } | |
188 | ||
189 | ||
190 | ||
faa9b3e7 | 191 | static void get_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr); |
02d1d628 AMH |
192 | |
193 | i_img* | |
790923a4 | 194 | i_readpng_wiol(io_glue *ig, int length) { |
02d1d628 AMH |
195 | i_img *im; |
196 | png_structp png_ptr; | |
197 | png_infop info_ptr; | |
790923a4 | 198 | png_uint_32 width, height; |
02d1d628 | 199 | int bit_depth, color_type, interlace_type; |
790923a4 AMH |
200 | int number_passes,y; |
201 | int channels,pass; | |
02d1d628 AMH |
202 | unsigned int sig_read; |
203 | ||
790923a4 | 204 | sig_read = 0; |
02d1d628 | 205 | |
790923a4 AMH |
206 | io_glue_commit_types(ig); |
207 | mm_log((1,"i_readpng_wiol(ig %p, length %d)\n", ig, length)); | |
02d1d628 AMH |
208 | |
209 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL); | |
790923a4 | 210 | png_set_read_fn(png_ptr, (png_voidp) (ig), wiol_read_data); |
02d1d628 AMH |
211 | |
212 | info_ptr = png_create_info_struct(png_ptr); | |
213 | if (info_ptr == NULL) { | |
214 | png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); | |
215 | return NULL; | |
216 | } | |
217 | ||
218 | if (setjmp(png_ptr->jmpbuf)) { | |
790923a4 | 219 | mm_log((1,"i_readpng_wiol: error.\n")); |
02d1d628 AMH |
220 | png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
221 | return NULL; | |
222 | } | |
790923a4 AMH |
223 | |
224 | png_ptr->io_ptr = (png_voidp) ig; | |
02d1d628 AMH |
225 | png_set_sig_bytes(png_ptr, sig_read); |
226 | png_read_info(png_ptr, info_ptr); | |
227 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL); | |
228 | ||
229 | mm_log((1, | |
790923a4 | 230 | "png_get_IHDR results: width %d, height %d, bit_depth %d, color_type %d, interlace_type %d\n", |
02d1d628 AMH |
231 | width,height,bit_depth,color_type,interlace_type)); |
232 | ||
233 | CC2C[PNG_COLOR_TYPE_GRAY]=1; | |
234 | CC2C[PNG_COLOR_TYPE_PALETTE]=3; | |
235 | CC2C[PNG_COLOR_TYPE_RGB]=3; | |
236 | CC2C[PNG_COLOR_TYPE_RGB_ALPHA]=4; | |
237 | CC2C[PNG_COLOR_TYPE_GRAY_ALPHA]=2; | |
790923a4 AMH |
238 | channels = CC2C[color_type]; |
239 | ||
240 | mm_log((1,"i_readpng_wiol: channels %d\n",channels)); | |
241 | ||
02d1d628 AMH |
242 | png_set_strip_16(png_ptr); |
243 | png_set_packing(png_ptr); | |
244 | if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr); | |
245 | if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand(png_ptr); | |
6829f5f6 AMH |
246 | |
247 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) { | |
248 | channels++; | |
249 | mm_log((1, "image has transparency, adding alpha: channels = %d\n", channels)); | |
250 | png_set_expand(png_ptr); | |
251 | } | |
790923a4 | 252 | |
02d1d628 AMH |
253 | number_passes = png_set_interlace_handling(png_ptr); |
254 | mm_log((1,"number of passes=%d\n",number_passes)); | |
255 | png_read_update_info(png_ptr, info_ptr); | |
790923a4 AMH |
256 | |
257 | im = i_img_empty_ch(NULL,width,height,channels); | |
258 | ||
02d1d628 | 259 | for (pass = 0; pass < number_passes; pass++) |
faa9b3e7 | 260 | for (y = 0; y < height; y++) { png_read_row(png_ptr,(png_bytep) &(im->idata[channels*width*y]), NULL); } |
02d1d628 | 261 | |
790923a4 | 262 | png_read_end(png_ptr, info_ptr); |
02d1d628 | 263 | |
faa9b3e7 TC |
264 | get_png_tags(im, png_ptr, info_ptr); |
265 | ||
790923a4 | 266 | png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
02d1d628 | 267 | |
790923a4 | 268 | mm_log((1,"(0x%08X) <- i_readpng_scalar\n", im)); |
02d1d628 | 269 | |
790923a4 AMH |
270 | return im; |
271 | } | |
faa9b3e7 TC |
272 | |
273 | static void get_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr) { | |
274 | png_uint_32 xres, yres; | |
275 | int unit_type; | |
276 | if (png_get_pHYs(png_ptr, info_ptr, &xres, &yres, &unit_type)) { | |
277 | mm_log((1,"pHYs (%d, %d) %d\n", xres, yres, unit_type)); | |
278 | if (unit_type == PNG_RESOLUTION_METER) { | |
279 | i_tags_set_float(&im->tags, "i_xres", 0, xres * 0.0254); | |
280 | i_tags_set_float(&im->tags, "i_yres", 0, xres * 0.0254); | |
281 | } | |
282 | else { | |
283 | i_tags_addn(&im->tags, "i_xres", 0, xres); | |
284 | i_tags_addn(&im->tags, "i_yres", 0, yres); | |
285 | i_tags_addn(&im->tags, "i_aspect_only", 0, 1); | |
286 | } | |
287 | } | |
288 | } |