Glenn Randers-Pehrson | 08a3343 | 1998-03-07 06:06:55 -0600 | [diff] [blame] | 1 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 2 | #if 0 /* in case someone actually tries to compile this */ |
| 3 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 4 | /* example.c - an example of using libpng */ |
| 5 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 6 | /* This is an example of how to use libpng to read and write PNG files. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 7 | * The file libpng.txt is much more verbose then this. If you have not |
| 8 | * read it, do so first. This was designed to be a starting point of an |
| 9 | * implementation. This is not officially part of libpng, and therefore |
| 10 | * does not require a copyright notice. |
| 11 | * |
| 12 | * This file does not currently compile, because it is missing certain |
| 13 | * parts, like allocating memory to hold an image. You will have to |
| 14 | * supply these parts to get it to compile. For an example of a minimal |
| 15 | * working PNG reader/writer, see pngtest.c, included in this distribution. |
| 16 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 17 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 18 | #include "png.h" |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 19 | |
Glenn Randers-Pehrson | c944229 | 1999-01-06 21:50:16 -0600 | [diff] [blame] | 20 | /* Check to see if a file is a PNG file using png_sig_cmp(). png_sig_cmp() |
| 21 | * returns zero if the image is a PNG and nonzero if it isn't a PNG. |
| 22 | * |
| 23 | * The function check_if_png() shown here, but not used, returns nonzero (true) |
| 24 | * if the file can be opened and is a PNG, 0 (false) otherwise. |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 25 | * |
| 26 | * If this call is successful, and you are going to keep the file open, |
| 27 | * you should call png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); once |
| 28 | * you have created the png_ptr, so that libpng knows your application |
| 29 | * has read that many bytes from the start of the file. Make sure you |
| 30 | * don't call png_set_sig_bytes() with more than 8 bytes read or give it |
| 31 | * an incorrect number of bytes read, or you will either have read too |
| 32 | * many bytes (your fault), or you are telling libpng to read the wrong |
| 33 | * number of magic bytes (also your fault). |
| 34 | * |
| 35 | * Many applications already read the first 2 or 4 bytes from the start |
| 36 | * of the image to determine the file type, so it would be easiest just |
Glenn Randers-Pehrson | 0f7202f | 1998-03-08 18:52:15 -0600 | [diff] [blame] | 37 | * to pass the bytes to png_sig_cmp() or even skip that if you know |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 38 | * you have a PNG file, and call png_set_sig_bytes(). |
| 39 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 40 | #define PNG_BYTES_TO_CHECK 4 |
| 41 | int check_if_png(char *file_name, FILE **fp) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 42 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 43 | char buf[PNG_BYTES_TO_CHECK]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 44 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 45 | /* Open the prospective PNG file. */ |
| 46 | if ((*fp = fopen(file_name, "rb")) != NULL); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 47 | return 0; |
| 48 | |
Glenn Randers-Pehrson | c944229 | 1999-01-06 21:50:16 -0600 | [diff] [blame] | 49 | /* Read in some of the signature bytes */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 50 | if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK) |
| 51 | return 0; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 52 | |
Glenn Randers-Pehrson | c944229 | 1999-01-06 21:50:16 -0600 | [diff] [blame] | 53 | /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. |
| 54 | Return nonzero (true) if they match */ |
| 55 | |
| 56 | return(!png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK)); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 57 | } |
| 58 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 59 | /* Read a PNG file. You may want to return an error code if the read |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 60 | * fails (depending upon the failure). There are two "prototypes" given |
| 61 | * here - one where we are given the filename, and we need to open the |
| 62 | * file, and the other where we are given an open file (possibly with |
| 63 | * some or all of the magic bytes read - see comments above). |
| 64 | */ |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 65 | #ifdef open_file /* prototype 1 */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 66 | void read_png(char *file_name) /* We need to open the file */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 67 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 68 | png_structp png_ptr; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 69 | png_infop info_ptr; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 70 | unsigned int sig_read = 0; |
| 71 | png_uint_32 width, height; |
| 72 | int bit_depth, color_type, interlace_type; |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 73 | FILE *fp; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 74 | |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 75 | if ((fp = fopen(file_name, "rb")) == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 76 | return; |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 77 | #else no_open_file /* prototype 2 */ |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 78 | void read_png(FILE *fp, unsigned int sig_read) /* file is already open */ |
| 79 | { |
| 80 | png_structp png_ptr; |
| 81 | png_infop info_ptr; |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 82 | png_uint_32 width, height; |
| 83 | int bit_depth, color_type, interlace_type; |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 84 | #endif no_open_file /* only use one prototype! */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 85 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 86 | /* Create and initialize the png_struct with the desired error handler |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 87 | * functions. If you want to use the default stderr and longjump method, |
| 88 | * you can supply NULL for the last three parameters. We also supply the |
| 89 | * the compiler header file version, so that we know if the application |
| 90 | * was compiled with a compatible version of the library. REQUIRED |
| 91 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 92 | png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 93 | png_voidp user_error_ptr, user_error_fn, user_warning_fn); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 94 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 95 | if (png_ptr == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 96 | { |
| 97 | fclose(fp); |
| 98 | return; |
| 99 | } |
| 100 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 101 | /* Allocate/initialize the memory for image information. REQUIRED. */ |
Glenn Randers-Pehrson | 0f7202f | 1998-03-08 18:52:15 -0600 | [diff] [blame] | 102 | info_ptr = png_create_info_struct(png_ptr); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 103 | if (info_ptr == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 104 | { |
| 105 | fclose(fp); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 106 | png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 107 | return; |
| 108 | } |
| 109 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 110 | /* Set error handling if you are using the setjmp/longjmp method (this is |
| 111 | * the normal method of doing things with libpng). REQUIRED unless you |
| 112 | * set up your own error handlers in the png_create_read_struct() earlier. |
| 113 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 114 | if (setjmp(png_ptr->jmpbuf)) |
| 115 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 116 | /* Free all of the memory associated with the png_ptr and info_ptr */ |
| 117 | png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 118 | fclose(fp); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 119 | /* If we get here, we had a problem reading the file */ |
| 120 | return; |
| 121 | } |
| 122 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 123 | /* One of the following I/O initialization methods is REQUIRED */ |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 124 | #ifdef streams /* PNG file I/O method 1 */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 125 | /* Set up the input control if you are using standard C streams */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 126 | png_init_io(png_ptr, fp); |
| 127 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 128 | #else no_streams /* PNG file I/O method 2 */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 129 | /* If you are using replacement read functions, instead of calling |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 130 | * png_init_io() here you would call: |
| 131 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 132 | png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn); |
| 133 | /* where user_io_ptr is a structure you want available to the callbacks */ |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 134 | #endif no_streams /* Use only one I/O method! */ |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 135 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 136 | /* If we have already read some of the signature */ |
Glenn Randers-Pehrson | 0f7202f | 1998-03-08 18:52:15 -0600 | [diff] [blame] | 137 | png_set_sig_bytes(png_ptr, sig_read); |
Andreas Dilger | 02ad0ef | 1997-01-17 01:34:35 -0600 | [diff] [blame] | 138 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 139 | /* The call to png_read_info() gives us all of the information from the |
| 140 | * PNG file before the first IDAT (image data chunk). REQUIRED |
| 141 | */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 142 | png_read_info(png_ptr, info_ptr); |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 143 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 144 | png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, |
| 145 | &interlace_type, NULL, NULL); |
| 146 | |
| 147 | /**** Set up the data transformations you want. Note that these are all |
| 148 | **** optional. Only call them if you want/need them. Many of the |
| 149 | **** transformations only work on specific types of images, and many |
| 150 | **** are mutually exclusive. |
| 151 | ****/ |
| 152 | |
| 153 | /* tell libpng to strip 16 bit/color files down to 8 bits/color */ |
| 154 | png_set_strip_16(png_ptr); |
| 155 | |
Glenn Randers-Pehrson | 4393a9a | 1999-09-17 12:27:26 -0500 | [diff] [blame] | 156 | /* Strip alpha bytes from the input data without combining with the |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 157 | * background (not recommended). |
| 158 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 159 | png_set_strip_alpha(png_ptr); |
| 160 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 161 | /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 162 | * byte into separate bytes (useful for paletted and grayscale images). |
| 163 | */ |
| 164 | png_set_packing(png_ptr); |
| 165 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 166 | /* Change the order of packed pixels to least significant bit first |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 167 | * (not useful if you are using png_set_packing). */ |
| 168 | png_set_packswap(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 169 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 170 | /* Expand paletted colors into true RGB triplets */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 171 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 172 | png_set_expand(png_ptr); |
| 173 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 174 | /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 175 | if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 176 | png_set_expand(png_ptr); |
| 177 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 178 | /* Expand paletted or RGB images with transparency to full alpha channels |
| 179 | * so the data will be available as RGBA quartets. |
| 180 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 181 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 182 | png_set_expand(png_ptr); |
| 183 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 184 | /* Set the background color to draw transparent and alpha images over. |
| 185 | * It is possible to set the red, green, and blue components directly |
| 186 | * for paletted images instead of supplying a palette index. Note that |
| 187 | * even if the PNG file supplies a background, you are not required to |
| 188 | * use it - you should use the (solid) application background if it has one. |
| 189 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 190 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 191 | png_color_16 my_background, *image_background; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 192 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 193 | if (png_get_bKGD(png_ptr, info_ptr, &image_background)) |
| 194 | png_set_background(png_ptr, image_background, |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 195 | PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 196 | else |
| 197 | png_set_background(png_ptr, &my_background, |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 198 | PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 199 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 200 | /* Some suggestions as to how to get a screen gamma value */ |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 201 | |
Glenn Randers-Pehrson | bcfd15d | 1999-10-01 14:22:25 -0500 | [diff] [blame] | 202 | /* Note that screen gamma is the display_exponent, which includes |
| 203 | * the CRT_exponent and any correction for viewing conditions */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 204 | if (/* We have a user-defined screen gamma value */) |
| 205 | { |
| 206 | screen_gamma = user-defined screen_gamma; |
| 207 | } |
| 208 | /* This is one way that applications share the same screen gamma value */ |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 209 | else if ((gamma_str = getenv("SCREEN_GAMMA")) != NULL) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 210 | { |
| 211 | screen_gamma = atof(gamma_str); |
| 212 | } |
| 213 | /* If we don't have another value */ |
| 214 | else |
| 215 | { |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 216 | screen_gamma = 2.2; /* A good guess for a PC monitors in a dimly |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 217 | lit room */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 218 | screen_gamma = 1.7 or 1.0; /* A good guess for Mac systems */ |
| 219 | } |
| 220 | |
| 221 | /* Tell libpng to handle the gamma conversion for you. The second call |
| 222 | * is a good guess for PC generated images, but it should be configurable |
| 223 | * by the user at run time by the user. It is strongly suggested that |
| 224 | * your application support gamma correction. |
| 225 | */ |
Glenn Randers-Pehrson | c4a2ae6 | 1998-01-16 22:06:18 -0600 | [diff] [blame] | 226 | |
| 227 | int intent; |
| 228 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 229 | if (png_get_sRGB(png_ptr, info_ptr, &intent)) |
Glenn Randers-Pehrson | 6d8f3b0 | 1999-10-23 08:39:18 -0500 | [diff] [blame] | 230 | png_set_sRGB(png_ptr, info_ptr, intent); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 231 | else |
| 232 | { |
| 233 | double image_gamma; |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 234 | if (png_get_gAMA(png_ptr, info_ptr, &image_gamma)) |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 235 | png_set_gamma(png_ptr, screen_gamma, image_gamma); |
| 236 | else |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 237 | png_set_gamma(png_ptr, screen_gamma, 0.45455); |
| 238 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 239 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 240 | /* Dither RGB files down to 8 bit palette or reduce palettes |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 241 | * to the number of colors available on your screen. |
| 242 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 243 | if (color_type & PNG_COLOR_MASK_COLOR) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 244 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 245 | png_uint_32 num_palette; |
| 246 | png_colorp palette; |
| 247 | |
| 248 | /* This reduces the image to the application supplied palette */ |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 249 | if (/* we have our own palette */) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 250 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 251 | /* An array of colors to which the image should be dithered */ |
| 252 | png_color std_color_cube[MAX_SCREEN_COLORS]; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 253 | |
| 254 | png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 255 | MAX_SCREEN_COLORS, NULL, 0); |
| 256 | } |
| 257 | /* This reduces the image to the palette supplied in the file */ |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 258 | else if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette)) |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 259 | { |
| 260 | png_color16p histogram; |
| 261 | |
| 262 | png_get_hIST(png_ptr, info_ptr, &histogram); |
| 263 | |
| 264 | png_set_dither(png_ptr, palette, num_palette, |
| 265 | max_screen_colors, histogram, 0); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 266 | } |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 267 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 268 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 269 | /* invert monocrome files to have 0 as white and 1 as black */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 270 | png_set_invert_mono(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 271 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 272 | /* If you want to shift the pixel values from the range [0,255] or |
| 273 | * [0,65535] to the original [0,7] or [0,31], or whatever range the |
| 274 | * colors were originally in: |
| 275 | */ |
| 276 | if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT)) |
| 277 | { |
| 278 | png_color8p sig_bit; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 279 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 280 | png_get_sBIT(png_ptr, info_ptr, &sig_bit); |
| 281 | png_set_shift(png_ptr, sig_bit); |
| 282 | } |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 283 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 284 | /* flip the RGB pixels to BGR (or RGBA to BGRA) */ |
| 285 | png_set_bgr(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 286 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 287 | /* swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */ |
| 288 | png_set_swap_alpha(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 289 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 290 | /* swap bytes of 16 bit files to least significant byte first */ |
| 291 | png_set_swap(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 292 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 293 | /* Add filler (or alpha) byte (before/after each RGB triplet) */ |
| 294 | png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER); |
| 295 | |
| 296 | /* Turn on interlace handling. REQUIRED if you are not using |
| 297 | * png_read_image(). To see how to handle interlacing passes, |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 298 | * see the png_read_row() method below: |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 299 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 300 | number_passes = png_set_interlace_handling(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 301 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 302 | /* Optional call to gamma correct and add the background to the palette |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 303 | * and update info structure. REQUIRED if you are expecting libpng to |
| 304 | * update the palette for you (ie you selected such a transform above). |
| 305 | */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 306 | png_read_update_info(png_ptr, info_ptr); |
| 307 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 308 | /* Allocate the memory to hold the image using the fields of info_ptr. */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 309 | |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 310 | /* The easiest way to read the image: */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 311 | png_bytep row_pointers[height]; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 312 | |
| 313 | for (row = 0; row < height; row++) |
| 314 | { |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 315 | row_pointers[row] = malloc(png_get_rowbytes(png_ptr, info_ptr)); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 316 | } |
| 317 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 318 | /* Now it's time to read the image. One of these methods is REQUIRED */ |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 319 | #ifdef entire /* Read the entire image in one go */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 320 | png_read_image(png_ptr, row_pointers); |
| 321 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 322 | #else no_entire /* Read the image one or more scanlines at a time */ |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 323 | /* The other way to read images - deal with interlacing: */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 324 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 325 | for (pass = 0; pass < number_passes; pass++) |
| 326 | { |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 327 | #ifdef single /* Read the image a single row at a time */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 328 | for (y = 0; y < height; y++) |
| 329 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 330 | png_read_rows(png_ptr, &row_pointers[y], NULL, 1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 331 | } |
| 332 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 333 | #else no_single /* Read the image several rows at a time */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 334 | for (y = 0; y < height; y += number_of_rows) |
| 335 | { |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 336 | #ifdef sparkle /* Read the image using the "sparkle" effect. */ |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 337 | png_read_rows(png_ptr, &row_pointers[y], NULL, number_of_rows); |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 338 | |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 339 | png_read_rows(png_ptr, NULL, row_pointers[y], number_of_rows); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 340 | #else no_sparkle /* Read the image using the "rectangle" effect */ |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 341 | png_read_rows(png_ptr, NULL, &row_pointers[y], number_of_rows); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 342 | #endif no_sparkle /* use only one of these two methods */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 343 | } |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 344 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 345 | /* if you want to display the image after every pass, do |
| 346 | so here */ |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 347 | #endif no_single /* use only one of these two methods */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 348 | } |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 349 | #endif no_entire /* use only one of these two methods */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 350 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 351 | /* read rest of file, and get additional chunks in info_ptr - REQUIRED */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 352 | png_read_end(png_ptr, info_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 353 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 354 | /* clean up after the read, and free any memory allocated - REQUIRED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 355 | png_destroy_read_struct(&png_ptr, &info_ptr, (png_infopp)NULL); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 356 | |
| 357 | /* close the file */ |
| 358 | fclose(fp); |
| 359 | |
| 360 | /* that's it */ |
| 361 | return; |
| 362 | } |
| 363 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 364 | /* progressively read a file */ |
| 365 | |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 366 | int |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 367 | initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 368 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 369 | /* Create and initialize the png_struct with the desired error handler |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 370 | * functions. If you want to use the default stderr and longjump method, |
| 371 | * you can supply NULL for the last three parameters. We also check that |
| 372 | * the library version is compatible in case we are using dynamically |
| 373 | * linked libraries. |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 374 | */ |
| 375 | *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 376 | png_voidp user_error_ptr, user_error_fn, user_warning_fn); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 377 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 378 | if (*png_ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 379 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 380 | *info_ptr = NULL; |
| 381 | return ERROR; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 382 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 383 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 384 | *info_ptr = png_create_info_struct(png_ptr); |
| 385 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 386 | if (*info_ptr == NULL) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 387 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 388 | png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL); |
| 389 | return ERROR; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 390 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 391 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 392 | if (setjmp((*png_ptr)->jmpbuf)) |
| 393 | { |
| 394 | png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL); |
| 395 | return ERROR; |
| 396 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 397 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 398 | /* This one's new. You will need to provide all three |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 399 | * function callbacks, even if you aren't using them all. |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 400 | * If you aren't using all functions, you can specify NULL |
| 401 | * parameters. Even when all three functions are NULL, |
| 402 | * you need to call png_set_progressive_read_fn(). |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 403 | * These functions shouldn't be dependent on global or |
| 404 | * static variables if you are decoding several images |
| 405 | * simultaneously. You should store stream specific data |
| 406 | * in a separate struct, given as the second parameter, |
| 407 | * and retrieve the pointer from inside the callbacks using |
| 408 | * the function png_get_progressive_ptr(png_ptr). |
| 409 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 410 | png_set_progressive_read_fn(*png_ptr, (void *)stream_data, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 411 | info_callback, row_callback, end_callback); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 412 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 413 | return OK; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | int |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 417 | process_data(png_structp *png_ptr, png_infop *info_ptr, |
| 418 | png_bytep buffer, png_uint_32 length) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 419 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 420 | if (setjmp((*png_ptr)->jmpbuf)) |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 421 | { |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 422 | /* Free the png_ptr and info_ptr memory on error */ |
| 423 | png_destroy_read_struct(png_ptr, info_ptr, (png_infopp)NULL); |
| 424 | return ERROR; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 425 | } |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 426 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 427 | /* This one's new also. Simply give it chunks of data as |
| 428 | * they arrive from the data stream (in order, of course). |
| 429 | * On Segmented machines, don't give it any more than 64K. |
| 430 | * The library seems to run fine with sizes of 4K, although |
| 431 | * you can give it much less if necessary (I assume you can |
| 432 | * give it chunks of 1 byte, but I haven't tried with less |
| 433 | * than 256 bytes yet). When this function returns, you may |
| 434 | * want to display any rows that were generated in the row |
| 435 | * callback, if you aren't already displaying them there. |
| 436 | */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 437 | png_process_data(*png_ptr, *info_ptr, buffer, length); |
| 438 | return OK; |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | info_callback(png_structp png_ptr, png_infop info) |
| 442 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 443 | /* do any setup here, including setting any of the transformations |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 444 | * mentioned in the Reading PNG files section. For now, you _must_ |
| 445 | * call either png_start_read_image() or png_read_update_info() |
| 446 | * after all the transformations are set (even if you don't set |
| 447 | * any). You may start getting rows before png_process_data() |
| 448 | * returns, so this is your last chance to prepare for that. |
| 449 | */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | row_callback(png_structp png_ptr, png_bytep new_row, |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 453 | png_uint_32 row_num, int pass) |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 454 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 455 | /* this function is called for every row in the image. If the |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 456 | * image is interlacing, and you turned on the interlace handler, |
| 457 | * this function will be called for every row in every pass. |
| 458 | * Some of these rows will not be changed from the previous pass. |
| 459 | * When the row is not changed, the new_row variable will be NULL. |
| 460 | * The rows and passes are called in order, so you don't really |
| 461 | * need the row_num and pass, but I'm supplying them because it |
| 462 | * may make your life easier. |
| 463 | * |
| 464 | * For the non-NULL rows of interlaced images, you must call |
| 465 | * png_progressive_combine_row() passing in the row and the |
| 466 | * old row. You can call this function for NULL rows (it will |
| 467 | * just return) and for non-interlaced images (it just does the |
| 468 | * memcpy for you) if it will make the code easier. Thus, you |
| 469 | * can just do this for all cases: |
| 470 | */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 471 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 472 | png_progressive_combine_row(png_ptr, old_row, new_row); |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 473 | |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 474 | /* where old_row is what was displayed for previous rows. Note |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 475 | * that the first pass (pass == 0 really) will completely cover |
| 476 | * the old row, so the rows do not have to be initialized. After |
| 477 | * the first pass (and only for interlaced images), you will have |
| 478 | * to pass the current row, and the function will combine the |
| 479 | * old row and the new row. |
| 480 | */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 481 | } |
| 482 | |
| 483 | end_callback(png_structp png_ptr, png_infop info) |
| 484 | { |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 485 | /* this function is called when the whole image has been read, |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 486 | * including any chunks after the image (up to and including |
| 487 | * the IEND). You will usually have the same info chunk as you |
| 488 | * had in the header, although some data may have been added |
| 489 | * to the comments and time fields. |
| 490 | * |
| 491 | * Most people won't do much here, perhaps setting a flag that |
| 492 | * marks the image as finished. |
| 493 | */ |
Guy Schalnat | 6d76471 | 1995-12-19 03:22:19 -0600 | [diff] [blame] | 494 | } |
| 495 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 496 | /* write a png file */ |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 497 | void write_png(char *file_name /* , ... other image information ... */) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 498 | { |
| 499 | FILE *fp; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 500 | png_structp png_ptr; |
| 501 | png_infop info_ptr; |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 502 | |
| 503 | /* open the file */ |
| 504 | fp = fopen(file_name, "wb"); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 505 | if (fp == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 506 | return; |
| 507 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 508 | /* Create and initialize the png_struct with the desired error handler |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 509 | * functions. If you want to use the default stderr and longjump method, |
| 510 | * you can supply NULL for the last three parameters. We also check that |
| 511 | * the library version is compatible with the one used at compile time, |
| 512 | * in case we are using dynamically linked libraries. REQUIRED. |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 513 | */ |
| 514 | png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, |
Glenn Randers-Pehrson | d0dce40 | 1998-05-09 10:02:29 -0500 | [diff] [blame] | 515 | png_voidp user_error_ptr, user_error_fn, user_warning_fn); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 516 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 517 | if (png_ptr == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 518 | { |
| 519 | fclose(fp); |
| 520 | return; |
| 521 | } |
| 522 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 523 | /* Allocate/initialize the image information data. REQUIRED */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 524 | info_ptr = png_create_info_struct(png_ptr); |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 525 | if (info_ptr == NULL) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 526 | { |
| 527 | fclose(fp); |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 528 | png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 529 | return; |
| 530 | } |
| 531 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 532 | /* Set error handling. REQUIRED if you aren't supplying your own |
| 533 | * error hadnling functions in the png_create_write_struct() call. |
| 534 | */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 535 | if (setjmp(png_ptr->jmpbuf)) |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 536 | { |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 537 | /* If we get here, we had a problem reading the file */ |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 538 | fclose(fp); |
| 539 | png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 540 | return; |
| 541 | } |
| 542 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 543 | /* One of the following I/O initialization functions is REQUIRED */ |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 544 | #ifdef streams /* I/O initialization method 1 */ |
Guy Schalnat | 69b1448 | 1996-01-10 02:56:49 -0600 | [diff] [blame] | 545 | /* set up the output control if you are using standard C streams */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 546 | png_init_io(png_ptr, fp); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 547 | #else no_streams /* I/O initialization method 2 */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 548 | /* If you are using replacement read functions, instead of calling |
| 549 | * png_init_io() here you would call */ |
| 550 | png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn, |
| 551 | user_IO_flush_function); |
| 552 | /* where user_io_ptr is a structure you want available to the callbacks */ |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 553 | #endif no_streams /* only use one initialization method */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 554 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 555 | /* Set the image information here. Width and height are up to 2^31, |
| 556 | * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on |
| 557 | * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY, |
| 558 | * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, |
| 559 | * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or |
| 560 | * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST |
| 561 | * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED |
| 562 | */ |
| 563 | png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, PNG_COLOR_TYPE_???, |
| 564 | PNG_INTERLACE_????, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 565 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 566 | /* set the palette if there is one. REQUIRED for indexed-color images */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 567 | palette = (png_colorp)png_malloc(png_ptr, 256 * sizeof (png_color)); |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 568 | /* ... set palette colors ... */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 569 | png_set_PLTE(png_ptr, info_ptr, palette, 256); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 570 | |
| 571 | /* optional significant bit chunk */ |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 572 | /* if we are dealing with a grayscale image then */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 573 | sig_bit.gray = true_bit_depth; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 574 | /* otherwise, if we are dealing with a color image then */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 575 | sig_bit.red = true_red_bit_depth; |
| 576 | sig_bit.green = true_green_bit_depth; |
| 577 | sig_bit.blue = true_blue_bit_depth; |
Guy Schalnat | b2e01bd | 1996-01-26 01:38:47 -0600 | [diff] [blame] | 578 | /* if the image has an alpha channel then */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 579 | sig_bit.alpha = true_alpha_bit_depth; |
| 580 | png_set_sBIT(png_ptr, info_ptr, sig_bit); |
| 581 | |
Glenn Randers-Pehrson | 5c6aeb2 | 1998-12-29 11:47:59 -0600 | [diff] [blame] | 582 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 583 | /* Optional gamma chunk is strongly suggested if you have any guess |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 584 | * as to the correct gamma of the image. |
| 585 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 586 | png_set_gAMA(png_ptr, info_ptr, gamma); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 587 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 588 | /* Optionally write comments into the image */ |
| 589 | text_ptr[0].key = "Title"; |
| 590 | text_ptr[0].text = "Mona Lisa"; |
| 591 | text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE; |
| 592 | text_ptr[1].key = "Author"; |
| 593 | text_ptr[1].text = "Leonardo DaVinci"; |
| 594 | text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE; |
| 595 | text_ptr[2].key = "Description"; |
| 596 | text_ptr[2].text = "<long text>"; |
| 597 | text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt; |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 598 | png_set_text(png_ptr, info_ptr, text_ptr, 3); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 599 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 600 | /* other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs, */ |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 601 | /* note that if sRGB is present the cHRM chunk must be ignored |
| 602 | * on read and must be written in accordance with the sRGB profile */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 603 | |
| 604 | /* Write the file header information. REQUIRED */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 605 | png_write_info(png_ptr, info_ptr); |
| 606 | |
Glenn Randers-Pehrson | 5379b24 | 1999-11-27 10:22:33 -0600 | [diff] [blame^] | 607 | /* If you want, you can write the info in two steps, in case you need to |
| 608 | * write your private chunk ahead of PLTE: |
| 609 | * |
| 610 | * png_write_info_before_PLTE(write_ptr, write_info_ptr); |
| 611 | * write_my_chunk(); |
| 612 | * png_write_info(png_ptr, info_ptr); |
| 613 | */ |
| 614 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 615 | /* Once we write out the header, the compression type on the text |
| 616 | * chunks gets changed to PNG_TEXT_COMPRESSION_NONE_WR or |
| 617 | * PNG_TEXT_COMPRESSION_zTXt_WR, so it doesn't get written out again |
| 618 | * at the end. |
| 619 | */ |
| 620 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 621 | /* set up the transformations you want. Note that these are |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 622 | * all optional. Only call them if you want them. |
| 623 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 624 | |
| 625 | /* invert monocrome pixels */ |
Glenn Randers-Pehrson | 46f61e2 | 1998-01-30 21:45:12 -0600 | [diff] [blame] | 626 | png_set_invert_mono(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 627 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 628 | /* Shift the pixels up to a legal bit depth and fill in |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 629 | * as appropriate to correctly scale the image. |
| 630 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 631 | png_set_shift(png_ptr, &sig_bit); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 632 | |
| 633 | /* pack pixels into bytes */ |
| 634 | png_set_packing(png_ptr); |
| 635 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 636 | /* swap location of alpha bytes from ARGB to RGBA */ |
| 637 | png_set_swap_alpha(png_ptr); |
| 638 | |
| 639 | /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into |
Glenn Randers-Pehrson | b6ce43d | 1998-01-01 07:13:13 -0600 | [diff] [blame] | 640 | * RGB (4 channels -> 3 channels). The second parameter is not used. |
| 641 | */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 642 | png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); |
| 643 | |
| 644 | /* flip BGR pixels to RGB */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 645 | png_set_bgr(png_ptr); |
| 646 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 647 | /* swap bytes of 16-bit files to most significant byte first */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 648 | png_set_swap(png_ptr); |
| 649 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 650 | /* swap bits of 1, 2, 4 bit packed pixel formats */ |
| 651 | png_set_packswap(png_ptr); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 652 | |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 653 | /* turn on interlace handling if you are not using png_write_image() */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 654 | if (interlacing) |
| 655 | number_passes = png_set_interlace_handling(png_ptr); |
| 656 | else |
| 657 | number_passes = 1; |
| 658 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 659 | /* The easiest way to write the image (you may have a different memory |
| 660 | * layout, however, so choose what fits your needs best). You need to |
| 661 | * use the first method if you aren't handling interlacing yourself. |
| 662 | */ |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 663 | png_uint_32 k, height, width; |
| 664 | png_byte image[height][width]; |
| 665 | png_bytep row_pointers[height]; |
| 666 | for (k = 0; k < height; k++) |
| 667 | row_pointers[k] = image + k*width; |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 668 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 669 | /* One of the following output methods is REQUIRED */ |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 670 | #ifdef entire /* write out the entire image data in one call */ |
Guy Schalnat | 51f0eb4 | 1995-09-26 05:22:39 -0500 | [diff] [blame] | 671 | png_write_image(png_ptr, row_pointers); |
| 672 | |
| 673 | /* the other way to write the image - deal with interlacing */ |
| 674 | |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 675 | #else no_entire /* write out the image data by one or more scanlines */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 676 | /* The number of passes is either 1 for non-interlaced images, |
| 677 | * or 7 for interlaced images. |
| 678 | */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 679 | for (pass = 0; pass < number_passes; pass++) |
| 680 | { |
| 681 | /* Write a few rows at a time. */ |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 682 | png_write_rows(png_ptr, &row_pointers[first_row], number_of_rows); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 683 | |
| 684 | /* If you are only writing one row at a time, this works */ |
| 685 | for (y = 0; y < height; y++) |
| 686 | { |
Glenn Randers-Pehrson | 8686fff | 1998-05-21 09:27:50 -0500 | [diff] [blame] | 687 | png_write_rows(png_ptr, &row_pointers[y], 1); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 688 | } |
| 689 | } |
Glenn Randers-Pehrson | cbe52d8 | 1998-02-28 07:00:24 -0600 | [diff] [blame] | 690 | #endif no_entire /* use only one output method */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 691 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 692 | /* You can write optional chunks like tEXt, zTXt, and tIME at the end |
| 693 | * as well. |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 694 | */ |
| 695 | |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 696 | /* It is REQUIRED to call this to finish writing the rest of the file */ |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 697 | png_write_end(png_ptr, info_ptr); |
| 698 | |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 699 | /* if you malloced the palette, free it here */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 700 | free(info_ptr->palette); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 701 | |
Guy Schalnat | e5a3779 | 1996-06-05 15:50:50 -0500 | [diff] [blame] | 702 | /* if you allocated any text comments, free them here */ |
| 703 | |
| 704 | /* clean up after the write, and free any memory allocated */ |
Andreas Dilger | 47a0c42 | 1997-05-16 02:46:07 -0500 | [diff] [blame] | 705 | png_destroy_write_struct(&png_ptr, (png_infopp)NULL); |
Guy Schalnat | 0d58058 | 1995-07-20 02:43:20 -0500 | [diff] [blame] | 706 | |
| 707 | /* close the file */ |
| 708 | fclose(fp); |
| 709 | |
| 710 | /* that's it */ |
| 711 | return; |
| 712 | } |
| 713 | |
Glenn Randers-Pehrson | 860ab2b | 1999-10-14 07:43:10 -0500 | [diff] [blame] | 714 | #endif /* if 0 */ |