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 char offunit[20] = "pixel";
78 io_glue_commit_types(ig);
79 mm_log((1,"i_writepng(im %p ,ig %p)\n", im, ig));
84 channels=im->channels;
86 if (channels > 2) { cspace = PNG_COLOR_TYPE_RGB; channels-=3; }
87 else { cspace=PNG_COLOR_TYPE_GRAY; channels--; }
89 if (channels) cspace|=PNG_COLOR_MASK_ALPHA;
90 mm_log((1,"cspace=%d\n",cspace));
92 channels = im->channels;
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.
101 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
103 if (png_ptr == NULL) return 0;
106 /* Allocate/initialize the image information data. REQUIRED */
107 info_ptr = png_create_info_struct(png_ptr);
109 if (info_ptr == NULL) {
110 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
114 /* Set error handling. REQUIRED if you aren't supplying your own
115 * error hadnling functions in the png_create_write_struct() call.
117 if (setjmp(png_ptr->jmpbuf)) {
118 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
122 png_set_write_fn(png_ptr, (png_voidp) (ig), wiol_write_data, wiol_flush_data);
123 png_ptr->io_ptr = (png_voidp) ig;
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
134 png_set_IHDR(png_ptr, info_ptr, width, height, 8, cspace,
135 PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
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 */
145 if (i_tags_get_float(&im->tags, "i_yres", 0, &yres))
152 i_tags_get_int(&im->tags, "i_aspect_only", 0, &aspect_only);
155 png_set_pHYs(png_ptr, info_ptr, xres + 0.5, yres + 0.5,
156 aspect_only ? PNG_RESOLUTION_UNKNOWN : PNG_RESOLUTION_METER);
159 png_write_info(png_ptr, info_ptr);
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]));
166 unsigned char *data = mymalloc(im->xsize * im->channels);
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);
175 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
180 png_write_end(png_ptr, info_ptr);
182 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
191 static void get_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr);
194 i_readpng_wiol(io_glue *ig, int length) {
198 png_uint_32 width, height;
199 int bit_depth, color_type, interlace_type;
202 unsigned int sig_read;
206 io_glue_commit_types(ig);
207 mm_log((1,"i_readpng_wiol(ig %p, length %d)\n", ig, length));
209 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,NULL,NULL,NULL);
210 png_set_read_fn(png_ptr, (png_voidp) (ig), wiol_read_data);
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);
218 if (setjmp(png_ptr->jmpbuf)) {
219 mm_log((1,"i_readpng_wiol: error.\n"));
220 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
224 png_ptr->io_ptr = (png_voidp) ig;
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);
230 "png_get_IHDR results: width %d, height %d, bit_depth %d, color_type %d, interlace_type %d\n",
231 width,height,bit_depth,color_type,interlace_type));
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;
238 channels = CC2C[color_type];
240 mm_log((1,"i_readpng_wiol: channels %d\n",channels));
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);
247 if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) {
249 mm_log((1, "image has transparency, adding alpha: channels = %d\n", channels));
250 png_set_expand(png_ptr);
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);
257 im = i_img_empty_ch(NULL,width,height,channels);
259 for (pass = 0; pass < number_passes; pass++)
260 for (y = 0; y < height; y++) { png_read_row(png_ptr,(png_bytep) &(im->idata[channels*width*y]), NULL); }
262 png_read_end(png_ptr, info_ptr);
264 get_png_tags(im, png_ptr, info_ptr);
266 png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL);
268 mm_log((1,"(0x%08X) <- i_readpng_scalar\n", im));
273 static void get_png_tags(i_img *im, png_structp png_ptr, png_infop info_ptr) {
274 png_uint_32 xres, yres;
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);
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);