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.
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.
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).
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().
26 /* this is a way to get number of channels from color space
27 * Color code to channel number */
29 int CC2C[PNG_COLOR_MASK_PALETTE|PNG_COLOR_MASK_COLOR|PNG_COLOR_MASK_ALPHA];
31 #define PNG_BYTES_TO_CHECK 4
42 png_set_read_fn(png_structp read_ptr, voidp read_io_ptr, png_rw_ptr read_data_fn)
43 png_set_write_fn(png_structp write_ptr, voidp write_io_ptr, png_rw_ptr write_data_fn,
44 png_flush_ptr output_flush_fn);
45 voidp read_io_ptr = png_get_io_ptr(read_ptr);
46 voidp write_io_ptr = png_get_io_ptr(write_ptr);
53 wiol_read_data(png_structp png_ptr, png_bytep data, png_size_t length) {
54 io_glue *ig = (io_glue *)png_ptr->io_ptr;
55 int rc = ig->readcb(ig, data, length);
56 if (rc != length) png_error(png_ptr, "Read overflow error on an iolayer source.");
60 wiol_write_data(png_structp png_ptr, png_bytep data, png_size_t length) {
62 io_glue *ig = (io_glue *)png_ptr->io_ptr;
63 rc = ig->writecb(ig, data, length);
64 if (rc != length) png_error(png_ptr, "Write error on an iolayer source.");
68 wiol_flush_data(png_structp png_ptr) {
69 /* XXX : This needs to be added to the io layer */
74 check_if_png(char *file_name, FILE **fp) {
75 char buf[PNG_BYTES_TO_CHECK];
77 /* Open the prospective PNG file. */
78 if ((*fp = fopen(file_name, "rb")) != NULL) return 0;
80 /* Read in some of the signature bytes */
81 if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK) return 0;
83 /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature.
84 Return nonzero (true) if they match */
86 return(!png_sig_cmp((png_bytep)buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
92 i_writepng(i_img *im, int fd) {
97 volatile int cspace,channels;
99 int aspect_only, have_res;
101 char offunit[20] = "pixel";
103 mm_log((1,"i_writepng(0x%x,fd %d)\n",im,fd));
105 if ((fp = fdopen(fd,"w")) == NULL) {
106 mm_log((1,"can't fdopen.\n"));
113 channels=im->channels;
115 if (channels>2) { cspace=PNG_COLOR_TYPE_RGB; channels-=3; }
116 else { cspace=PNG_COLOR_TYPE_GRAY; channels--; }
118 if (channels) cspace|=PNG_COLOR_MASK_ALPHA;
119 mm_log((1,"cspace=%d\n",cspace));
121 channels=im->channels;
123 /* Create and initialize the png_struct with the desired error handler
124 * functions. If you want to use the default stderr and longjump method,
125 * you can supply NULL for the last three parameters. We also check that
126 * the library version is compatible with the one used at compile time,
127 * in case we are using dynamically linked libraries. REQUIRED.
130 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
132 if (png_ptr == NULL) {
137 /* Allocate/initialize the image information data. REQUIRED */
138 info_ptr = png_create_info_struct(png_ptr);
140 if (info_ptr == NULL) {
142 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
146 /* Set error handling. REQUIRED if you aren't supplying your own
147 * error hadnling functions in the png_create_write_struct() call.
149 if (setjmp(png_ptr->jmpbuf)) {
150 /* If we get here, we had a problem reading the file */
152 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
156 png_init_io(png_ptr, fp);
158 /* Set the image information here. Width and height are up to 2^31,
159 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
160 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
161 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
162 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
163 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
164 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
167 png_set_IHDR(png_ptr, info_ptr, width, height, 8, cspace,
168 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
171 if (i_tags_get_float(&im->tags, "i_xres", 0, &xres)) {
172 if (i_tags_get_float(&im->tags, "i_yres", 0, &yres))
173 ; /* nothing to do */
178 if (i_tags_get_float(&im->tags, "i_yres", 0, &yres))
185 i_tags_get_int(&im->tags, "i_aspect_only", 0, &aspect_only);
188 png_set_pHYs(png_ptr, info_ptr, xres + 0.5, yres + 0.5,
189 aspect_only ? PNG_RESOLUTION_UNKNOWN : PNG_RESOLUTION_METER);
192 png_write_info(png_ptr, info_ptr);
193 if (!im->virtual && im->type == i_direct_type && im->bits == i_8_bits) {
194 for (y = 0; y < height; y++)
195 png_write_row(png_ptr, (png_bytep) &(im->idata[channels*width*y]));
198 unsigned char *data = mymalloc(im->xsize * im->channels);
200 for (y = 0; y < height; y++) {
201 i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels);
202 png_write_row(png_ptr, (png_bytep)data);
208 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
212 png_write_end(png_ptr, info_ptr);
213 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
222 i_writepng_wiol(i_img *im, io_glue *ig) {
227 volatile int cspace,channels;
229 int aspect_only, have_res;
231 char offunit[20] = "pixel";
233 io_glue_commit_types(ig);
234 mm_log((1,"i_writepng(im %p ,ig %p)\n", im, ig));
239 channels=im->channels;
241 if (channels > 2) { cspace = PNG_COLOR_TYPE_RGB; channels-=3; }
242 else { cspace=PNG_COLOR_TYPE_GRAY; channels--; }
244 if (channels) cspace|=PNG_COLOR_MASK_ALPHA;
245 mm_log((1,"cspace=%d\n",cspace));
247 channels = im->channels;
249 /* Create and initialize the png_struct with the desired error handler
250 * functions. If you want to use the default stderr and longjump method,
251 * you can supply NULL for the last three parameters. We also check that
252 * the library version is compatible with the one used at compile time,
253 * in case we are using dynamically linked libraries. REQUIRED.
256 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
258 if (png_ptr == NULL) return 0;
261 /* Allocate/initialize the image information data. REQUIRED */
262 info_ptr = png_create_info_struct(png_ptr);
264 if (info_ptr == NULL) {
265 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
269 /* Set error handling. REQUIRED if you aren't supplying your own
270 * error hadnling functions in the png_create_write_struct() call.
272 if (setjmp(png_ptr->jmpbuf)) {
273 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
277 png_set_write_fn(png_ptr, (png_voidp) (ig), wiol_write_data, wiol_flush_data);
278 png_ptr->io_ptr = (png_voidp) ig;
280 /* Set the image information here. Width and height are up to 2^31,
281 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
282 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
283 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
284 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
285 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
286 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
289 png_set_IHDR(png_ptr, info_ptr, width, height, 8, cspace,
290 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
293 if (i_tags_get_float(&im->tags, "i_xres", 0, &xres)) {
294 if (i_tags_get_float(&im->tags, "i_yres", 0, &yres))
295 ; /* nothing to do */
300 if (i_tags_get_float(&im->tags, "i_yres", 0, &yres))
307 i_tags_get_int(&im->tags, "i_aspect_only", 0, &aspect_only);
310 png_set_pHYs(png_ptr, info_ptr, xres + 0.5, yres + 0.5,
311 aspect_only ? PNG_RESOLUTION_UNKNOWN : PNG_RESOLUTION_METER);
314 png_write_info(png_ptr, info_ptr);
316 if (!im->virtual && im->type == i_direct_type && im->bits == i_8_bits) {
317 for (y = 0; y < height; y++)
318 png_write_row(png_ptr, (png_bytep) &(im->idata[channels*width*y]));
321 unsigned char *data = mymalloc(im->xsize * im->channels);
323 for (y = 0; y < height; y++) {
324 i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels);
325 png_write_row(png_ptr, (png_bytep)data);
331 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
336 png_write_end(png_ptr, info_ptr);
338 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
345 static void get_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr);
348 i_readpng_wiol(io_glue *ig, int length) {
352 png_uint_32 width, height;
353 int bit_depth, color_type, interlace_type;
356 unsigned int sig_read;
360 io_glue_commit_types(ig);
361 mm_log((1,"i_readpng_wiol(ig %p, length %d)\n", ig, length));
363 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
364 png_set_read_fn(png_ptr, (png_voidp) (ig), wiol_read_data);
366 info_ptr = png_create_info_struct(png_ptr);
367 if (info_ptr == NULL) {
368 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
372 if (setjmp(png_ptr->jmpbuf)) {
373 mm_log((1,"i_readpng_wiol: error.\n"));
374 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
378 png_ptr->io_ptr = (png_voidp) ig;
379 png_set_sig_bytes(png_ptr, sig_read);
380 png_read_info(png_ptr, info_ptr);
381 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
384 "png_get_IHDR results: width %d, height %d, bit_depth %d, color_type %d, interlace_type %d\n",
385 width,height,bit_depth,color_type,interlace_type));
387 CC2C[PNG_COLOR_TYPE_GRAY]=1;
388 CC2C[PNG_COLOR_TYPE_PALETTE]=3;
389 CC2C[PNG_COLOR_TYPE_RGB]=3;
390 CC2C[PNG_COLOR_TYPE_RGB_ALPHA]=4;
391 CC2C[PNG_COLOR_TYPE_GRAY_ALPHA]=2;
392 channels = CC2C[color_type];
394 mm_log((1,"i_readpng_wiol: channels %d\n",channels));
396 png_set_strip_16(png_ptr);
397 png_set_packing(png_ptr);
398 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
399 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand(png_ptr);
401 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
403 mm_log((1, "image has transparency, adding alpha: channels = %d\n", channels));
404 png_set_expand(png_ptr);
407 number_passes = png_set_interlace_handling(png_ptr);
408 mm_log((1,"number of passes=%d\n",number_passes));
409 png_read_update_info(png_ptr, info_ptr);
411 im = i_img_empty_ch(NULL,width,height,channels);
413 for (pass = 0; pass < number_passes; pass++)
414 for (y = 0; y < height; y++) { png_read_row(png_ptr,(png_bytep) &(im->idata[channels*width*y]), NULL); }
416 png_read_end(png_ptr, info_ptr);
418 get_png_tags(im, png_ptr, info_ptr);
420 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
422 mm_log((1,"(0x%08X) <- i_readpng_scalar\n", im));
427 static void get_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr) {
428 png_uint_32 xres, yres;
430 if (png_get_pHYs(png_ptr, info_ptr, &xres, &yres, &unit_type)) {
431 mm_log((1,"pHYs (%d, %d) %d\n", xres, yres, unit_type));
432 if (unit_type == PNG_RESOLUTION_METER) {
433 i_tags_set_float(&im->tags, "i_xres", 0, xres * 0.0254);
434 i_tags_set_float(&im->tags, "i_yres", 0, xres * 0.0254);
437 i_tags_addn(&im->tags, "i_xres", 0, xres);
438 i_tags_addn(&im->tags, "i_yres", 0, yres);
439 i_tags_addn(&im->tags, "i_aspect_only", 0, 1);