4 /* Check to see if a file is a PNG file using png_sig_cmp(). png_sig_cmp()
5 * returns zero if the image is a PNG and nonzero if it isn't a PNG.
7 * The function check_if_png() shown here, but not used, returns nonzero (true)
8 * if the file can be opened and is a PNG, 0 (false) otherwise.
10 * If this call is successful, and you are going to keep the file open,
11 * you should call png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); once
12 * you have created the png_ptr, so that libpng knows your application
13 * has read that many bytes from the start of the file. Make sure you
14 * don't call png_set_sig_bytes() with more than 8 bytes read or give it
15 * an incorrect number of bytes read, or you will either have read too
16 * many bytes (your fault), or you are telling libpng to read the wrong
17 * number of magic bytes (also your fault).
19 * Many applications already read the first 2 or 4 bytes from the start
20 * of the image to determine the file type, so it would be easiest just
21 * to pass the bytes to png_sig_cmp() or even skip that if you know
22 * you have a PNG file, and call png_set_sig_bytes().
25 /* this is a way to get number of channels from color space
26 * Color code to channel number */
28 int CC2C[PNG_COLOR_MASK_PALETTE|PNG_COLOR_MASK_COLOR|PNG_COLOR_MASK_ALPHA];
30 #define PNG_BYTES_TO_CHECK 4
41 png_set_read_fn(png_structp read_ptr, voidp read_io_ptr, png_rw_ptr read_data_fn)
42 png_set_write_fn(png_structp write_ptr, voidp write_io_ptr, png_rw_ptr write_data_fn,
43 png_flush_ptr output_flush_fn);
44 voidp read_io_ptr = png_get_io_ptr(read_ptr);
45 voidp write_io_ptr = png_get_io_ptr(write_ptr);
48 struct png_scalar_info {
55 struct png_wiol_info {
62 user_read_data(png_structp png_ptr,png_bytep data, png_size_t length) {
63 struct png_scalar_info *sci=(struct png_scalar_info *)png_ptr->io_ptr;
65 /* fprintf(stderr,"user_read_data: cpos %d/%d (%d)\n",sci->cpos,sci->length,length); */
67 if (sci->cpos+(ssize_t)length > sci->length ) { png_error(png_ptr, "Read overflow error on a scalar."); }
69 memcpy(data, sci->data+sci->cpos , length);
75 user_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length) {
76 FIXME: implement these
80 user_flush_data(png_structp png_ptr) {
81 FIXME: implement these
87 scalar_png_init_io(png_structp png_ptr,struct png_scalar_info *sci) {
88 png_ptr->io_ptr = (png_voidp)sci;
97 check_if_png(char *file_name, FILE **fp) {
98 char buf[PNG_BYTES_TO_CHECK];
100 /* Open the prospective PNG file. */
101 if ((*fp = fopen(file_name, "rb")) != NULL) return 0;
103 /* Read in some of the signature bytes */
104 if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK) return 0;
106 /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature.
107 Return nonzero (true) if they match */
109 return(!png_sig_cmp((png_bytep)buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
112 /* Read a PNG file. You may want to return an error code if the read
113 * fails (depending upon the failure). There are two "prototypes" given
114 * here - one where we are given the filename, and we need to open the
115 * file, and the other where we are given an open file (possibly with
116 * some or all of the magic bytes read - see comments above).
124 png_uint_32 width, height, y;
125 int bit_depth, color_type, interlace_type;
130 unsigned int sig_read;
134 if ((fp = fdopen(fd,"r")) == NULL) {
135 mm_log((1,"can't fdopen.\n"));
139 mm_log((1,"i_readpng(fd %d)\n",fd));
141 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
143 if (png_ptr == NULL) {
148 /* Allocate/initialize the memory for image information. REQUIRED. */
149 info_ptr = png_create_info_struct(png_ptr);
150 if (info_ptr == NULL) {
152 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
156 /* Set error handling if you are using the setjmp/longjmp method (this is
157 * the normal method of doing things with libpng). REQUIRED unless you
158 * set up your own error handlers in the png_create_read_struct() earlier.
161 if (setjmp(png_ptr->jmpbuf)) {
162 /* Free all of the memory associated with the png_ptr and info_ptr */
163 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
165 /* If we get here, we had a problem reading the file */
169 /* Set up the input control if you are using standard C streams */
170 png_init_io(png_ptr, fp);
171 /* If we have already read some of the signature */
172 png_set_sig_bytes(png_ptr, sig_read);
174 /* The call to png_read_info() gives us all of the information from the
175 * PNG file before the first IDAT (image data chunk). REQUIRED
178 png_read_info(png_ptr, info_ptr);
179 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
180 &interlace_type, NULL, NULL);
183 "png_get_IHDR results: width %d, height %d, bit_depth %d,color_type %d,interlace_type %d\n",
184 width,height,bit_depth,color_type,interlace_type));
186 CC2C[PNG_COLOR_TYPE_GRAY]=1;
187 CC2C[PNG_COLOR_TYPE_PALETTE]=3;
188 CC2C[PNG_COLOR_TYPE_RGB]=3;
189 CC2C[PNG_COLOR_TYPE_RGB_ALPHA]=4;
190 CC2C[PNG_COLOR_TYPE_GRAY_ALPHA]=2;
192 channels=CC2C[color_type];
194 mm_log((1,"channels %d\n",channels));
196 im=i_img_empty_ch(NULL,width,height,channels);
198 /**** Set up the data transformations you want. Note that these are all
199 **** optional. Only call them if you want/need them. Many of the
200 **** transformations only work on specific types of images, and many
201 **** are mutually exclusive.
204 /* tell libpng to strip 16 bit/color files down to 8 bits/color */
205 png_set_strip_16(png_ptr);
207 /* Strip alpha bytes from the input data without combining with the
208 * background (not recommended).
210 /* png_set_strip_alpha(png_ptr); */
212 /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
213 * byte into separate bytes (useful for paletted and grayscale images).
216 png_set_packing(png_ptr);
218 /* Expand paletted colors into true RGB triplets */
219 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
221 /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
222 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand(png_ptr);
224 /* Expand paletted or RGB images with transparency to full alpha channels
225 * so the data will be available as RGBA quartets.
227 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr);
229 number_passes = png_set_interlace_handling(png_ptr);
231 mm_log((1,"number of passes=%d\n",number_passes));
233 png_read_update_info(png_ptr, info_ptr);
234 for (pass = 0; pass < number_passes; pass++)
235 for (y = 0; y < height; y++) { png_read_row(png_ptr,(png_bytep) &(im->data[channels*width*y]), NULL); }
236 /* read rest of file, and get additional chunks in info_ptr - REQUIRED */
238 png_read_end(png_ptr, info_ptr);
239 /* clean up after the read, and free any memory allocated - REQUIRED */
240 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
250 i_writepng(i_img *im,int fd) {
255 volatile int cspace,channels;
257 mm_log((1,"i_writepng(0x%x,fd %d)\n",im,fd));
259 if ((fp = fdopen(fd,"w")) == NULL) {
260 mm_log((1,"can't fdopen.\n"));
267 channels=im->channels;
269 if (channels>2) { cspace=PNG_COLOR_TYPE_RGB; channels-=3; }
270 else { cspace=PNG_COLOR_TYPE_GRAY; channels--; }
272 if (channels) cspace|=PNG_COLOR_MASK_ALPHA;
273 mm_log((1,"cspace=%d\n",cspace));
275 channels=im->channels;
277 /* Create and initialize the png_struct with the desired error handler
278 * functions. If you want to use the default stderr and longjump method,
279 * you can supply NULL for the last three parameters. We also check that
280 * the library version is compatible with the one used at compile time,
281 * in case we are using dynamically linked libraries. REQUIRED.
284 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
286 if (png_ptr == NULL) {
291 /* Allocate/initialize the image information data. REQUIRED */
292 info_ptr = png_create_info_struct(png_ptr);
294 if (info_ptr == NULL) {
296 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
300 /* Set error handling. REQUIRED if you aren't supplying your own
301 * error hadnling functions in the png_create_write_struct() call.
303 if (setjmp(png_ptr->jmpbuf)) {
304 /* If we get here, we had a problem reading the file */
306 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
310 png_init_io(png_ptr, fp);
312 /* Set the image information here. Width and height are up to 2^31,
313 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
314 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
315 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
316 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
317 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
318 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
321 png_set_IHDR(png_ptr, info_ptr, width, height, 8, cspace,
322 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
324 png_write_info(png_ptr, info_ptr);
325 for (y = 0; y < height; y++) png_write_row(png_ptr, (png_bytep) &(im->data[channels*width*y]));
326 png_write_end(png_ptr, info_ptr);
327 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
337 i_readpng_scalar(char *data, int length) {
341 png_uint_32 width, height, y;
342 int bit_depth, color_type, interlace_type;
345 unsigned int sig_read;
347 struct png_scalar_info sci;
354 mm_log((1,"i_readpng_scalar(char 0x%08X, length %d)\n",data,length));
356 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
357 png_set_read_fn(png_ptr, (void*) (&sci), user_read_data);
359 info_ptr = png_create_info_struct(png_ptr);
360 if (info_ptr == NULL) {
361 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
365 if (setjmp(png_ptr->jmpbuf)) {
366 mm_log((1,"i_readpng_scalar: error.\n"));
367 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
371 scalar_png_init_io(png_ptr, &sci);
372 png_set_sig_bytes(png_ptr, sig_read);
373 png_read_info(png_ptr, info_ptr);
374 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
377 "png_get_IHDR results: width %d, height %d, bit_depth %d,color_type %d,interlace_type %d\n",
378 width,height,bit_depth,color_type,interlace_type));
380 CC2C[PNG_COLOR_TYPE_GRAY]=1;
381 CC2C[PNG_COLOR_TYPE_PALETTE]=3;
382 CC2C[PNG_COLOR_TYPE_RGB]=3;
383 CC2C[PNG_COLOR_TYPE_RGB_ALPHA]=4;
384 CC2C[PNG_COLOR_TYPE_GRAY_ALPHA]=2;
385 channels=CC2C[color_type];
386 mm_log((1,"channels %d\n",channels));
387 im=i_img_empty_ch(NULL,width,height,channels);
388 png_set_strip_16(png_ptr);
389 png_set_packing(png_ptr);
390 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
391 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand(png_ptr);
392 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr);
393 number_passes = png_set_interlace_handling(png_ptr);
394 mm_log((1,"number of passes=%d\n",number_passes));
395 png_read_update_info(png_ptr, info_ptr);
396 mm_log((1,"made it to here 1\n"));
397 for (pass = 0; pass < number_passes; pass++)
398 for (y = 0; y < height; y++) { png_read_row(png_ptr,(png_bytep) &(im->data[channels*width*y]), NULL); }
399 mm_log((1,"made it to here 2\n"));
400 png_read_end(png_ptr, info_ptr);
401 mm_log((1,"made it to here 3\n"));
402 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
403 mm_log((1,"made it to here 4\n"));
404 mm_log((1,"(0x%08X) <- i_readpng_scalar\n",im));
412 /* i_readpng_wiol(io_glue *ig, int length) { */
414 /* png_structp png_ptr; */
415 /* png_infop info_ptr; */
416 /* png_uint_32 width, height; */
417 /* int bit_depth, color_type, interlace_type; */
418 /* int number_passes,y; */
419 /* int channels,pass; */
420 /* unsigned int sig_read; */
422 /* struct png_wiol_info wi; */
425 /* wi.length = length; */
429 /* mm_log((1,"i_readpng_wiol(char 0x%p, length %d)\n", data, length)); */
431 /* png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL); */
432 /* png_set_read_fn(png_ptr, (void*) (&wi), user_read_data); */
434 /* info_ptr = png_create_info_struct(png_ptr); */
435 /* if (info_ptr == NULL) { */
436 /* png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); */
440 /* if (setjmp(png_ptr->jmpbuf)) { */
441 /* mm_log((1,"i_readpng_wiol: error.\n")); */
442 /* png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); */
446 /* scalar_png_init_io(png_ptr, &sci); */
447 /* png_set_sig_bytes(png_ptr, sig_read); */
448 /* png_read_info(png_ptr, info_ptr); */
449 /* png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL); */
452 /* "png_get_IHDR results: width %d, height %d, bit_depth %d,color_type %d,interlace_type %d\n", */
453 /* width,height,bit_depth,color_type,interlace_type)); */
455 /* CC2C[PNG_COLOR_TYPE_GRAY]=1; */
456 /* CC2C[PNG_COLOR_TYPE_PALETTE]=3; */
457 /* CC2C[PNG_COLOR_TYPE_RGB]=3; */
458 /* CC2C[PNG_COLOR_TYPE_RGB_ALPHA]=4; */
459 /* CC2C[PNG_COLOR_TYPE_GRAY_ALPHA]=2; */
460 /* channels = CC2C[color_type]; */
462 /* mm_log((1,"i_readpng_wiol: channels %d\n",channels)); */
464 /* im = i_img_empty_ch(NULL,width,height,channels); */
465 /* png_set_strip_16(png_ptr); */
466 /* png_set_packing(png_ptr); */
467 /* if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr); */
468 /* if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand(png_ptr); */
469 /* if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) png_set_expand(png_ptr); */
470 /* number_passes = png_set_interlace_handling(png_ptr); */
471 /* mm_log((1,"number of passes=%d\n",number_passes)); */
472 /* png_read_update_info(png_ptr, info_ptr); */
473 /* mm_log((1,"made it to here 1\n")); */
474 /* for (pass = 0; pass < number_passes; pass++) */
475 /* for (y = 0; y < height; y++) { png_read_row(png_ptr,(png_bytep) &(im->data[channels*width*y]), NULL); } */
476 /* mm_log((1,"made it to here 2\n")); */
477 /* png_read_end(png_ptr, info_ptr); */
478 /* mm_log((1,"made it to here 3\n")); */
479 /* png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); */
480 /* mm_log((1,"made it to here 4\n")); */
481 /* mm_log((1,"(0x%08X) <- i_readpng_scalar\n",im)); */