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 static int CC2C[PNG_COLOR_MASK_PALETTE|PNG_COLOR_MASK_COLOR|PNG_COLOR_MASK_ALPHA];
31 #define PNG_BYTES_TO_CHECK 4
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.");
43 wiol_write_data(png_structp png_ptr, png_bytep data, png_size_t length) {
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.");
51 wiol_flush_data(png_structp png_ptr) {
52 /* XXX : This needs to be added to the io layer */
56 /* Check function demo
59 check_if_png(char *file_name, FILE **fp) {
60 char buf[PNG_BYTES_TO_CHECK];
61 if ((*fp = fopen(file_name, "rb")) != NULL) return 0;
62 if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK) return 0;
63 return(!png_sig_cmp((png_bytep)buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
68 i_writepng_wiol(i_img *im, io_glue *ig) {
72 volatile int cspace,channels;
74 int aspect_only, have_res;
76 io_glue_commit_types(ig);
77 mm_log((1,"i_writepng(im %p ,ig %p)\n", im, ig));
82 channels=im->channels;
84 if (channels > 2) { cspace = PNG_COLOR_TYPE_RGB; channels-=3; }
85 else { cspace=PNG_COLOR_TYPE_GRAY; channels--; }
87 if (channels) cspace|=PNG_COLOR_MASK_ALPHA;
88 mm_log((1,"cspace=%d\n",cspace));
90 channels = im->channels;
92 /* Create and initialize the png_struct with the desired error handler
93 * functions. If you want to use the default stderr and longjump method,
94 * you can supply NULL for the last three parameters. We also check that
95 * the library version is compatible with the one used at compile time,
96 * in case we are using dynamically linked libraries. REQUIRED.
99 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
101 if (png_ptr == NULL) return 0;
104 /* Allocate/initialize the image information data. REQUIRED */
105 info_ptr = png_create_info_struct(png_ptr);
107 if (info_ptr == NULL) {
108 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
112 /* Set error handling. REQUIRED if you aren't supplying your own
113 * error hadnling functions in the png_create_write_struct() call.
115 if (setjmp(png_ptr->jmpbuf)) {
116 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
120 png_set_write_fn(png_ptr, (png_voidp) (ig), wiol_write_data, wiol_flush_data);
121 png_ptr->io_ptr = (png_voidp) ig;
123 /* Set the image information here. Width and height are up to 2^31,
124 * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
125 * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
126 * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
127 * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or
128 * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
129 * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
132 png_set_IHDR(png_ptr, info_ptr, width, height, 8, cspace,
133 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
136 if (i_tags_get_float(&im->tags, "i_xres", 0, &xres)) {
137 if (i_tags_get_float(&im->tags, "i_yres", 0, &yres))
138 ; /* nothing to do */
143 if (i_tags_get_float(&im->tags, "i_yres", 0, &yres))
150 i_tags_get_int(&im->tags, "i_aspect_only", 0, &aspect_only);
153 png_set_pHYs(png_ptr, info_ptr, xres + 0.5, yres + 0.5,
154 aspect_only ? PNG_RESOLUTION_UNKNOWN : PNG_RESOLUTION_METER);
157 png_write_info(png_ptr, info_ptr);
159 if (!im->virtual && im->type == i_direct_type && im->bits == i_8_bits) {
160 for (y = 0; y < height; y++)
161 png_write_row(png_ptr, (png_bytep) &(im->idata[channels*width*y]));
164 unsigned char *data = mymalloc(im->xsize * im->channels);
166 for (y = 0; y < height; y++) {
167 i_gsamp(im, 0, im->xsize, y, data, NULL, im->channels);
168 png_write_row(png_ptr, (png_bytep)data);
173 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
178 png_write_end(png_ptr, info_ptr);
180 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
189 static void get_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr);
192 i_readpng_wiol(io_glue *ig, int length) {
196 png_uint_32 width, height;
197 int bit_depth, color_type, interlace_type;
200 unsigned int sig_read;
204 io_glue_commit_types(ig);
205 mm_log((1,"i_readpng_wiol(ig %p, length %d)\n", ig, length));
207 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
208 png_set_read_fn(png_ptr, (png_voidp) (ig), wiol_read_data);
210 info_ptr = png_create_info_struct(png_ptr);
211 if (info_ptr == NULL) {
212 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
216 if (setjmp(png_ptr->jmpbuf)) {
217 mm_log((1,"i_readpng_wiol: error.\n"));
218 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
222 png_ptr->io_ptr = (png_voidp) ig;
223 png_set_sig_bytes(png_ptr, sig_read);
224 png_read_info(png_ptr, info_ptr);
225 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, &interlace_type, NULL, NULL);
228 "png_get_IHDR results: width %d, height %d, bit_depth %d, color_type %d, interlace_type %d\n",
229 width,height,bit_depth,color_type,interlace_type));
231 CC2C[PNG_COLOR_TYPE_GRAY]=1;
232 CC2C[PNG_COLOR_TYPE_PALETTE]=3;
233 CC2C[PNG_COLOR_TYPE_RGB]=3;
234 CC2C[PNG_COLOR_TYPE_RGB_ALPHA]=4;
235 CC2C[PNG_COLOR_TYPE_GRAY_ALPHA]=2;
236 channels = CC2C[color_type];
238 mm_log((1,"i_readpng_wiol: channels %d\n",channels));
240 png_set_strip_16(png_ptr);
241 png_set_packing(png_ptr);
242 if (color_type == PNG_COLOR_TYPE_PALETTE) png_set_expand(png_ptr);
243 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) png_set_expand(png_ptr);
245 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
247 mm_log((1, "image has transparency, adding alpha: channels = %d\n", channels));
248 png_set_expand(png_ptr);
251 number_passes = png_set_interlace_handling(png_ptr);
252 mm_log((1,"number of passes=%d\n",number_passes));
253 png_read_update_info(png_ptr, info_ptr);
255 im = i_img_empty_ch(NULL,width,height,channels);
257 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
261 for (pass = 0; pass < number_passes; pass++)
262 for (y = 0; y < height; y++) { png_read_row(png_ptr,(png_bytep) &(im->idata[channels*width*y]), NULL); }
264 png_read_end(png_ptr, info_ptr);
266 get_png_tags(im, png_ptr, info_ptr);
268 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
270 mm_log((1,"(0x%08X) <- i_readpng_scalar\n", im));
275 static void get_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr) {
276 png_uint_32 xres, yres;
279 i_tags_add(&im->tags, "i_format", 0, "png", -1, 0);
280 if (png_get_pHYs(png_ptr, info_ptr, &xres, &yres, &unit_type)) {
281 mm_log((1,"pHYs (%d, %d) %d\n", xres, yres, unit_type));
282 if (unit_type == PNG_RESOLUTION_METER) {
283 i_tags_set_float2(&im->tags, "i_xres", 0, xres * 0.0254, 5);
284 i_tags_set_float2(&im->tags, "i_yres", 0, yres * 0.0254, 5);
287 i_tags_addn(&im->tags, "i_xres", 0, xres);
288 i_tags_addn(&im->tags, "i_yres", 0, yres);
289 i_tags_addn(&im->tags, "i_aspect_only", 0, 1);