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