1 |
1 |
2 #if 0 /* in case someone actually tries to compile this */ |
2 #if 0 /* in case someone actually tries to compile this */ |
3 |
3 |
4 /* example.c - an example of using libpng |
4 /* example.c - an example of using libpng |
5 * Last changed in libpng 1.2.37 [June 4, 2009] |
5 * Last changed in libpng 1.4.0 [January 3, 2010] |
6 * This file has been placed in the public domain by the authors. |
6 * This file has been placed in the public domain by the authors. |
7 * Maintained 1998-2009 Glenn Randers-Pehrson |
7 * Maintained 1998-2010 Glenn Randers-Pehrson |
8 * Maintained 1996, 1997 Andreas Dilger) |
8 * Maintained 1996, 1997 Andreas Dilger) |
9 * Written 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
9 * Written 1995, 1996 Guy Eric Schalnat, Group 42, Inc.) |
10 */ |
10 */ |
11 |
11 |
12 /* This is an example of how to use libpng to read and write PNG files. |
12 /* This is an example of how to use libpng to read and write PNG files. |
119 /* Allocate/initialize the memory for image information. REQUIRED. */ |
119 /* Allocate/initialize the memory for image information. REQUIRED. */ |
120 info_ptr = png_create_info_struct(png_ptr); |
120 info_ptr = png_create_info_struct(png_ptr); |
121 if (info_ptr == NULL) |
121 if (info_ptr == NULL) |
122 { |
122 { |
123 fclose(fp); |
123 fclose(fp); |
124 png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL); |
124 png_destroy_read_struct(&png_ptr, NULL, NULL); |
125 return (ERROR); |
125 return (ERROR); |
126 } |
126 } |
127 |
127 |
128 /* Set error handling if you are using the setjmp/longjmp method (this is |
128 /* Set error handling if you are using the setjmp/longjmp method (this is |
129 * the normal method of doing things with libpng). REQUIRED unless you |
129 * the normal method of doing things with libpng). REQUIRED unless you |
131 */ |
131 */ |
132 |
132 |
133 if (setjmp(png_jmpbuf(png_ptr))) |
133 if (setjmp(png_jmpbuf(png_ptr))) |
134 { |
134 { |
135 /* Free all of the memory associated with the png_ptr and info_ptr */ |
135 /* Free all of the memory associated with the png_ptr and info_ptr */ |
136 png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL); |
136 png_destroy_read_struct(&png_ptr, &info_ptr, NULL); |
137 fclose(fp); |
137 fclose(fp); |
138 /* If we get here, we had a problem reading the file */ |
138 /* If we get here, we had a problem reading the file */ |
139 return (ERROR); |
139 return (ERROR); |
140 } |
140 } |
141 |
141 |
162 * with one of the PNG_TRANSFORM_* bits (this presently excludes |
162 * with one of the PNG_TRANSFORM_* bits (this presently excludes |
163 * dithering, filling, setting background, and doing gamma |
163 * dithering, filling, setting background, and doing gamma |
164 * adjustment), then you can read the entire image (including |
164 * adjustment), then you can read the entire image (including |
165 * pixels) into the info structure with this call: |
165 * pixels) into the info structure with this call: |
166 */ |
166 */ |
167 png_read_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL); |
167 png_read_png(png_ptr, info_ptr, png_transforms, NULL); |
168 |
168 |
169 #else |
169 #else |
170 /* OK, you're doing it the hard way, with the lower-level functions */ |
170 /* OK, you're doing it the hard way, with the lower-level functions */ |
171 |
171 |
172 /* The call to png_read_info() gives us all of the information from the |
172 /* The call to png_read_info() gives us all of the information from the |
173 * PNG file before the first IDAT (image data chunk). REQUIRED |
173 * PNG file before the first IDAT (image data chunk). REQUIRED |
174 */ |
174 */ |
175 png_read_info(png_ptr, info_ptr); |
175 png_read_info(png_ptr, info_ptr); |
176 |
176 |
177 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, |
177 png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, |
178 &interlace_type, int_p_NULL, int_p_NULL); |
178 &interlace_type, NULL, NULL); |
179 |
179 |
180 /* Set up the data transformations you want. Note that these are all |
180 /* Set up the data transformations you want. Note that these are all |
181 * optional. Only call them if you want/need them. Many of the |
181 * optional. Only call them if you want/need them. Many of the |
182 * transformations only work on specific types of images, and many |
182 * transformations only work on specific types of images, and many |
183 * are mutually exclusive. |
183 * are mutually exclusive. |
284 { |
284 { |
285 /* An array of colors to which the image should be dithered */ |
285 /* An array of colors to which the image should be dithered */ |
286 png_color std_color_cube[MAX_SCREEN_COLORS]; |
286 png_color std_color_cube[MAX_SCREEN_COLORS]; |
287 |
287 |
288 png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS, |
288 png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS, |
289 MAX_SCREEN_COLORS, png_uint_16p_NULL, 0); |
289 MAX_SCREEN_COLORS, NULL, 0); |
290 } |
290 } |
291 /* This reduces the image to the palette supplied in the file */ |
291 /* This reduces the image to the palette supplied in the file */ |
292 else if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette)) |
292 else if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette)) |
293 { |
293 { |
294 png_uint_16p histogram = NULL; |
294 png_uint_16p histogram = NULL; |
363 for (pass = 0; pass < number_passes; pass++) |
363 for (pass = 0; pass < number_passes; pass++) |
364 { |
364 { |
365 #ifdef single /* Read the image a single row at a time */ |
365 #ifdef single /* Read the image a single row at a time */ |
366 for (y = 0; y < height; y++) |
366 for (y = 0; y < height; y++) |
367 { |
367 { |
368 png_read_rows(png_ptr, &row_pointers[y], png_bytepp_NULL, 1); |
368 png_read_rows(png_ptr, &row_pointers[y], NULL, 1); |
369 } |
369 } |
370 |
370 |
371 #else no_single /* Read the image several rows at a time */ |
371 #else no_single /* Read the image several rows at a time */ |
372 for (y = 0; y < height; y += number_of_rows) |
372 for (y = 0; y < height; y += number_of_rows) |
373 { |
373 { |
374 #ifdef sparkle /* Read the image using the "sparkle" effect. */ |
374 #ifdef sparkle /* Read the image using the "sparkle" effect. */ |
375 png_read_rows(png_ptr, &row_pointers[y], png_bytepp_NULL, |
375 png_read_rows(png_ptr, &row_pointers[y], NULL, |
376 number_of_rows); |
376 number_of_rows); |
377 #else no_sparkle /* Read the image using the "rectangle" effect */ |
377 #else no_sparkle /* Read the image using the "rectangle" effect */ |
378 png_read_rows(png_ptr, png_bytepp_NULL, &row_pointers[y], |
378 png_read_rows(png_ptr, NULL, &row_pointers[y], |
379 number_of_rows); |
379 number_of_rows); |
380 #endif no_sparkle /* Use only one of these two methods */ |
380 #endif no_sparkle /* Use only one of these two methods */ |
381 } |
381 } |
382 |
382 |
383 /* If you want to display the image after every pass, do so here */ |
383 /* If you want to display the image after every pass, do so here */ |
423 |
423 |
424 *info_ptr = png_create_info_struct(png_ptr); |
424 *info_ptr = png_create_info_struct(png_ptr); |
425 |
425 |
426 if (*info_ptr == NULL) |
426 if (*info_ptr == NULL) |
427 { |
427 { |
428 png_destroy_read_struct(png_ptr, info_ptr, png_infopp_NULL); |
428 png_destroy_read_struct(png_ptr, info_ptr, NULL); |
429 return (ERROR); |
429 return (ERROR); |
430 } |
430 } |
431 |
431 |
432 if (setjmp(png_jmpbuf((*png_ptr)))) |
432 if (setjmp(png_jmpbuf((*png_ptr)))) |
433 { |
433 { |
434 png_destroy_read_struct(png_ptr, info_ptr, png_infopp_NULL); |
434 png_destroy_read_struct(png_ptr, info_ptr, NULL); |
435 return (ERROR); |
435 return (ERROR); |
436 } |
436 } |
437 |
437 |
438 /* This one's new. You will need to provide all three |
438 /* This one's new. You will need to provide all three |
439 * function callbacks, even if you aren't using them all. |
439 * function callbacks, even if you aren't using them all. |
458 png_bytep buffer, png_uint_32 length) |
458 png_bytep buffer, png_uint_32 length) |
459 { |
459 { |
460 if (setjmp(png_jmpbuf((*png_ptr)))) |
460 if (setjmp(png_jmpbuf((*png_ptr)))) |
461 { |
461 { |
462 /* Free the png_ptr and info_ptr memory on error */ |
462 /* Free the png_ptr and info_ptr memory on error */ |
463 png_destroy_read_struct(png_ptr, info_ptr, png_infopp_NULL); |
463 png_destroy_read_struct(png_ptr, info_ptr, NULL); |
464 return (ERROR); |
464 return (ERROR); |
465 } |
465 } |
466 |
466 |
467 /* This one's new also. Simply give it chunks of data as |
467 /* This one's new also. Simply give it chunks of data as |
468 * they arrive from the data stream (in order, of course). |
468 * they arrive from the data stream (in order, of course). |
591 /* Allocate/initialize the image information data. REQUIRED */ |
591 /* Allocate/initialize the image information data. REQUIRED */ |
592 info_ptr = png_create_info_struct(png_ptr); |
592 info_ptr = png_create_info_struct(png_ptr); |
593 if (info_ptr == NULL) |
593 if (info_ptr == NULL) |
594 { |
594 { |
595 fclose(fp); |
595 fclose(fp); |
596 png_destroy_write_struct(&png_ptr, png_infopp_NULL); |
596 png_destroy_write_struct(&png_ptr, NULL); |
597 return (ERROR); |
597 return (ERROR); |
598 } |
598 } |
599 |
599 |
600 /* Set error handling. REQUIRED if you aren't supplying your own |
600 /* Set error handling. REQUIRED if you aren't supplying your own |
601 * error handling functions in the png_create_write_struct() call. |
601 * error handling functions in the png_create_write_struct() call. |
626 #ifdef hilevel |
626 #ifdef hilevel |
627 /* This is the easy way. Use it if you already have all the |
627 /* This is the easy way. Use it if you already have all the |
628 * image info living in the structure. You could "|" many |
628 * image info living in the structure. You could "|" many |
629 * PNG_TRANSFORM flags into the png_transforms integer here. |
629 * PNG_TRANSFORM flags into the png_transforms integer here. |
630 */ |
630 */ |
631 png_write_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL); |
631 png_write_png(png_ptr, info_ptr, png_transforms, NULL); |
632 |
632 |
633 #else |
633 #else |
634 /* This is the hard way */ |
634 /* This is the hard way */ |
635 |
635 |
636 /* Set the image information here. Width and height are up to 2^31, |
636 /* Set the image information here. Width and height are up to 2^31, |