1c2c66affSColin Finck 2c2c66affSColin Finck #if 0 /* in case someone actually tries to compile this */ 3c2c66affSColin Finck 4c2c66affSColin Finck /* example.c - an example of using libpng 5b61b1815SThomas Faber * 69f1e0532SThomas Faber * Maintained 2018 Cosmin Truta 7c2c66affSColin Finck * Maintained 1998-2016 Glenn Randers-Pehrson 8b61b1815SThomas Faber * Maintained 1996-1997 Andreas Dilger 9b61b1815SThomas Faber * Written 1995-1996 Guy Eric Schalnat, Group 42, Inc. 10b61b1815SThomas Faber * 11c2c66affSColin Finck * To the extent possible under law, the authors have waived 12c2c66affSColin Finck * all copyright and related or neighboring rights to this file. 139f1e0532SThomas Faber * This work is published from: United States, Canada. 14c2c66affSColin Finck */ 15c2c66affSColin Finck 16c2c66affSColin Finck /* This is an example of how to use libpng to read and write PNG files. 17b61b1815SThomas Faber * The file libpng-manual.txt is much more verbose then this. If you have 18b61b1815SThomas Faber * not read it, do so first. This was designed to be a starting point of an 19c2c66affSColin Finck * implementation. This is not officially part of libpng, is hereby placed 20c2c66affSColin Finck * in the public domain, and therefore does not require a copyright notice. 21c2c66affSColin Finck * 22c2c66affSColin Finck * This file does not currently compile, because it is missing certain 23c2c66affSColin Finck * parts, like allocating memory to hold an image. You will have to 24c2c66affSColin Finck * supply these parts to get it to compile. For an example of a minimal 25c2c66affSColin Finck * working PNG reader/writer, see pngtest.c, included in this distribution; 26c2c66affSColin Finck * see also the programs in the contrib directory. 27c2c66affSColin Finck */ 28c2c66affSColin Finck 29b61b1815SThomas Faber /* The simple, but restricted approach to reading a PNG file or data stream 30b61b1815SThomas Faber * requires just two function calls, as in the following complete program. 31b61b1815SThomas Faber * Writing a file needs just one function call, so long as the data has an 32c2c66affSColin Finck * appropriate layout. 33c2c66affSColin Finck * 34c2c66affSColin Finck * The following code reads PNG image data from a file and writes it, in a 35b61b1815SThomas Faber * potentially new format, to a new file. While this code will compile, there 36b61b1815SThomas Faber * is minimal (insufficient) error checking. For a more realistic version, 37b61b1815SThomas Faber * see contrib/examples/pngtopng.c 38c2c66affSColin Finck */ 39b61b1815SThomas Faber 40c2c66affSColin Finck #include <stddef.h> 41c2c66affSColin Finck #include <stdlib.h> 42c2c66affSColin Finck #include <string.h> 43c2c66affSColin Finck #include <stdio.h> 44c2c66affSColin Finck #include <png.h> 45c2c66affSColin Finck #include <zlib.h> 46c2c66affSColin Finck 47c2c66affSColin Finck int main(int argc, const char **argv) 48c2c66affSColin Finck { 49c2c66affSColin Finck if (argc == 3) 50c2c66affSColin Finck { 51c2c66affSColin Finck png_image image; /* The control structure used by libpng */ 52c2c66affSColin Finck 53c2c66affSColin Finck /* Initialize the 'png_image' structure. */ 54c2c66affSColin Finck memset(&image, 0, (sizeof image)); 55c2c66affSColin Finck image.version = PNG_IMAGE_VERSION; 56c2c66affSColin Finck 57c2c66affSColin Finck /* The first argument is the file to read: */ 58c2c66affSColin Finck if (png_image_begin_read_from_file(&image, argv[1]) != 0) 59c2c66affSColin Finck { 60c2c66affSColin Finck png_bytep buffer; 61c2c66affSColin Finck 62c2c66affSColin Finck /* Set the format in which to read the PNG file; this code chooses a 63c2c66affSColin Finck * simple sRGB format with a non-associated alpha channel, adequate to 64c2c66affSColin Finck * store most images. 65c2c66affSColin Finck */ 66c2c66affSColin Finck image.format = PNG_FORMAT_RGBA; 67c2c66affSColin Finck 68c2c66affSColin Finck /* Now allocate enough memory to hold the image in this format; the 69c2c66affSColin Finck * PNG_IMAGE_SIZE macro uses the information about the image (width, 70c2c66affSColin Finck * height and format) stored in 'image'. 71c2c66affSColin Finck */ 72c2c66affSColin Finck buffer = malloc(PNG_IMAGE_SIZE(image)); 73c2c66affSColin Finck 74b61b1815SThomas Faber /* If enough memory was available, read the image in the desired 75b61b1815SThomas Faber * format, then write the result out to the new file. 'background' is 76b61b1815SThomas Faber * not necessary when reading the image, because the alpha channel is 77c2c66affSColin Finck * preserved; if it were to be removed, for example if we requested 78c2c66affSColin Finck * PNG_FORMAT_RGB, then either a solid background color would have to 79b61b1815SThomas Faber * be supplied, or the output buffer would have to be initialized to 80b61b1815SThomas Faber * the actual background of the image. 81c2c66affSColin Finck * 82c2c66affSColin Finck * The fourth argument to png_image_finish_read is the 'row_stride' - 83c2c66affSColin Finck * this is the number of components allocated for the image in each 84c2c66affSColin Finck * row. It has to be at least as big as the value returned by 85c2c66affSColin Finck * PNG_IMAGE_ROW_STRIDE, but if you just allocate space for the 86b61b1815SThomas Faber * default, minimum size, using PNG_IMAGE_SIZE as above, you can pass 87c2c66affSColin Finck * zero. 88c2c66affSColin Finck * 89c2c66affSColin Finck * The final argument is a pointer to a buffer for the colormap; 90b61b1815SThomas Faber * colormaps have exactly the same format as a row of image pixels 91b61b1815SThomas Faber * (so you choose what format to make the colormap by setting 92c2c66affSColin Finck * image.format). A colormap is only returned if 93c2c66affSColin Finck * PNG_FORMAT_FLAG_COLORMAP is also set in image.format, so in this 94c2c66affSColin Finck * case NULL is passed as the final argument. If you do want to force 95b61b1815SThomas Faber * all images into an index/color-mapped format, then you can use: 96c2c66affSColin Finck * 97c2c66affSColin Finck * PNG_IMAGE_COLORMAP_SIZE(image) 98c2c66affSColin Finck * 99c2c66affSColin Finck * to find the maximum size of the colormap in bytes. 100c2c66affSColin Finck */ 101c2c66affSColin Finck if (buffer != NULL && 102c2c66affSColin Finck png_image_finish_read(&image, NULL/*background*/, buffer, 103c2c66affSColin Finck 0/*row_stride*/, NULL/*colormap*/) != 0) 104c2c66affSColin Finck { 105c2c66affSColin Finck /* Now write the image out to the second argument. In the write 106c2c66affSColin Finck * call 'convert_to_8bit' allows 16-bit data to be squashed down to 107c2c66affSColin Finck * 8 bits; this isn't necessary here because the original read was 108c2c66affSColin Finck * to the 8-bit format. 109c2c66affSColin Finck */ 110c2c66affSColin Finck if (png_image_write_to_file(&image, argv[2], 0/*convert_to_8bit*/, 111c2c66affSColin Finck buffer, 0/*row_stride*/, NULL/*colormap*/) != 0) 112c2c66affSColin Finck { 113c2c66affSColin Finck /* The image has been written successfully. */ 114c2c66affSColin Finck exit(0); 115c2c66affSColin Finck } 116c2c66affSColin Finck } 117c2c66affSColin Finck else 118c2c66affSColin Finck { 119c2c66affSColin Finck /* Calling png_image_free is optional unless the simplified API was 120b61b1815SThomas Faber * not run to completion. In this case, if there wasn't enough 121b61b1815SThomas Faber * memory for 'buffer', we didn't complete the read, so we must 122b61b1815SThomas Faber * free the image: 123c2c66affSColin Finck */ 124c2c66affSColin Finck if (buffer == NULL) 125c2c66affSColin Finck png_image_free(&image); 126c2c66affSColin Finck else 127c2c66affSColin Finck free(buffer); 128c2c66affSColin Finck } 129*c5febe93SThomas Faber } 130c2c66affSColin Finck 131c2c66affSColin Finck /* Something went wrong reading or writing the image. libpng stores a 132c2c66affSColin Finck * textual message in the 'png_image' structure: 133c2c66affSColin Finck */ 134c2c66affSColin Finck fprintf(stderr, "pngtopng: error: %s\n", image.message); 135c2c66affSColin Finck exit(1); 136c2c66affSColin Finck } 137c2c66affSColin Finck 138c2c66affSColin Finck fprintf(stderr, "pngtopng: usage: pngtopng input-file output-file\n"); 139b61b1815SThomas Faber exit(2); 140c2c66affSColin Finck } 141c2c66affSColin Finck 142c2c66affSColin Finck /* That's it ;-) Of course you probably want to do more with PNG files than 143c2c66affSColin Finck * just converting them all to 32-bit RGBA PNG files; you can do that between 144c2c66affSColin Finck * the call to png_image_finish_read and png_image_write_to_file. You can also 145b61b1815SThomas Faber * ask for the image data to be presented in a number of different formats. 146b61b1815SThomas Faber * You do this by simply changing the 'format' parameter set before allocating 147b61b1815SThomas Faber * the buffer. 148c2c66affSColin Finck * 149c2c66affSColin Finck * The format parameter consists of five flags that define various aspects of 150b61b1815SThomas Faber * the image. You can simply add these together to get the format, or you can 151b61b1815SThomas Faber * use one of the predefined macros from png.h (as above): 152c2c66affSColin Finck * 153b61b1815SThomas Faber * PNG_FORMAT_FLAG_COLOR: if set, the image will have three color components 154b61b1815SThomas Faber * per pixel (red, green and blue); if not set, the image will just have one 155c2c66affSColin Finck * luminance (grayscale) component. 156c2c66affSColin Finck * 157b61b1815SThomas Faber * PNG_FORMAT_FLAG_ALPHA: if set, each pixel in the image will have an 158b61b1815SThomas Faber * additional alpha value; a linear value that describes the degree the 159b61b1815SThomas Faber * image pixel covers (overwrites) the contents of the existing pixel on the 160b61b1815SThomas Faber * display. 161c2c66affSColin Finck * 162b61b1815SThomas Faber * PNG_FORMAT_FLAG_LINEAR: if set, the components of each pixel will be 163b61b1815SThomas Faber * returned as a series of 16-bit linear values; if not set, the components 164b61b1815SThomas Faber * will be returned as a series of 8-bit values encoded according to the 165b61b1815SThomas Faber * sRGB standard. The 8-bit format is the normal format for images intended 166b61b1815SThomas Faber * for direct display, because almost all display devices do the inverse of 167b61b1815SThomas Faber * the sRGB transformation to the data they receive. The 16-bit format is 168b61b1815SThomas Faber * more common for scientific data and image data that must be further 169b61b1815SThomas Faber * processed; because it is linear, simple math can be done on the component 170b61b1815SThomas Faber * values. Regardless of the setting of this flag, the alpha channel is 171b61b1815SThomas Faber * always linear, although it will be 8 bits or 16 bits wide as specified by 172b61b1815SThomas Faber * the flag. 173c2c66affSColin Finck * 174b61b1815SThomas Faber * PNG_FORMAT_FLAG_BGR: if set, the components of a color pixel will be 175b61b1815SThomas Faber * returned in the order blue, then green, then red. If not set, the pixel 176b61b1815SThomas Faber * components are in the order red, then green, then blue. 177c2c66affSColin Finck * 178b61b1815SThomas Faber * PNG_FORMAT_FLAG_AFIRST: if set, the alpha channel (if present) precedes the 179b61b1815SThomas Faber * color or grayscale components. If not set, the alpha channel follows the 180c2c66affSColin Finck * components. 181c2c66affSColin Finck * 182c2c66affSColin Finck * You do not have to read directly from a file. You can read from memory or, 183c2c66affSColin Finck * on systems that support it, from a <stdio.h> FILE*. This is controlled by 184b61b1815SThomas Faber * the particular png_image_read_from_ function you call at the start. 185b61b1815SThomas Faber * Likewise, on write, you can write to a FILE* if your system supports it. 186b61b1815SThomas Faber * Check the macro PNG_STDIO_SUPPORTED to see if stdio support has been 187b61b1815SThomas Faber * included in your libpng build. 188c2c66affSColin Finck * 189b61b1815SThomas Faber * If you read 16-bit (PNG_FORMAT_FLAG_LINEAR) data, you may need to write it 190b61b1815SThomas Faber * in the 8-bit format for display. You do this by setting the convert_to_8bit 191c2c66affSColin Finck * flag to 'true'. 192c2c66affSColin Finck * 193c2c66affSColin Finck * Don't repeatedly convert between the 8-bit and 16-bit forms. There is 194b61b1815SThomas Faber * significant data loss when 16-bit data is converted to the 8-bit encoding, 195b61b1815SThomas Faber * and the current libpng implementation of conversion to 16-bit is also 196c2c66affSColin Finck * significantly lossy. The latter will be fixed in the future, but the former 197c2c66affSColin Finck * is unavoidable - the 8-bit format just doesn't have enough resolution. 198c2c66affSColin Finck */ 199c2c66affSColin Finck 200c2c66affSColin Finck /* If your program needs more information from the PNG data it reads, or if you 201c2c66affSColin Finck * need to do more complex transformations, or minimize transformations, on the 202c2c66affSColin Finck * data you read, then you must use one of the several lower level libpng 203c2c66affSColin Finck * interfaces. 204c2c66affSColin Finck * 205c2c66affSColin Finck * All these interfaces require that you do your own error handling - your 206b61b1815SThomas Faber * program must be able to arrange for control to return to your own code, any 207b61b1815SThomas Faber * time libpng encounters a problem. There are several ways to do this, but 208b61b1815SThomas Faber * the standard way is to use the <setjmp.h> interface to establish a return 209b61b1815SThomas Faber * point within your own code. You must do this if you do not use the 210c2c66affSColin Finck * simplified interface (above). 211c2c66affSColin Finck * 212c2c66affSColin Finck * The first step is to include the header files you need, including the libpng 213c2c66affSColin Finck * header file. Include any standard headers and feature test macros your 214c2c66affSColin Finck * program requires before including png.h: 215c2c66affSColin Finck */ 216c2c66affSColin Finck #include <png.h> 217c2c66affSColin Finck 218c2c66affSColin Finck /* The png_jmpbuf() macro, used in error handling, became available in 219c2c66affSColin Finck * libpng version 1.0.6. If you want to be able to run your code with older 220c2c66affSColin Finck * versions of libpng, you must define the macro yourself (but only if it 221b61b1815SThomas Faber * is not already defined by libpng!) 222c2c66affSColin Finck */ 223c2c66affSColin Finck 224c2c66affSColin Finck #ifndef png_jmpbuf 225c2c66affSColin Finck # define png_jmpbuf(png_ptr) ((png_ptr)->png_jmpbuf) 226c2c66affSColin Finck #endif 227c2c66affSColin Finck 228c2c66affSColin Finck /* Check to see if a file is a PNG file using png_sig_cmp(). png_sig_cmp() 229b61b1815SThomas Faber * returns zero if the image is a PNG, and nonzero otherwise. 230c2c66affSColin Finck * 231c2c66affSColin Finck * The function check_if_png() shown here, but not used, returns nonzero (true) 232b61b1815SThomas Faber * if the file can be opened and is a PNG, and 0 (false) otherwise. 233c2c66affSColin Finck * 234c2c66affSColin Finck * If this call is successful, and you are going to keep the file open, 235c2c66affSColin Finck * you should call png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); once 236c2c66affSColin Finck * you have created the png_ptr, so that libpng knows your application 237c2c66affSColin Finck * has read that many bytes from the start of the file. Make sure you 238c2c66affSColin Finck * don't call png_set_sig_bytes() with more than 8 bytes read or give it 239c2c66affSColin Finck * an incorrect number of bytes read, or you will either have read too 240c2c66affSColin Finck * many bytes (your fault), or you are telling libpng to read the wrong 241c2c66affSColin Finck * number of magic bytes (also your fault). 242c2c66affSColin Finck * 243c2c66affSColin Finck * Many applications already read the first 2 or 4 bytes from the start 244c2c66affSColin Finck * of the image to determine the file type, so it would be easiest just 245b61b1815SThomas Faber * to pass the bytes to png_sig_cmp(), or even skip that if you know 246c2c66affSColin Finck * you have a PNG file, and call png_set_sig_bytes(). 247c2c66affSColin Finck */ 248c2c66affSColin Finck #define PNG_BYTES_TO_CHECK 4 249c2c66affSColin Finck int check_if_png(char *file_name, FILE **fp) 250c2c66affSColin Finck { 251c2c66affSColin Finck char buf[PNG_BYTES_TO_CHECK]; 252c2c66affSColin Finck 253c2c66affSColin Finck /* Open the prospective PNG file. */ 254c2c66affSColin Finck if ((*fp = fopen(file_name, "rb")) == NULL) 255c2c66affSColin Finck return 0; 256c2c66affSColin Finck 257b61b1815SThomas Faber /* Read in some of the signature bytes. */ 258c2c66affSColin Finck if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK) 259c2c66affSColin Finck return 0; 260c2c66affSColin Finck 261c2c66affSColin Finck /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature. 262b61b1815SThomas Faber * Return nonzero (true) if they match. 263b61b1815SThomas Faber */ 2649f1e0532SThomas Faber return(!png_sig_cmp(buf, 0, PNG_BYTES_TO_CHECK)); 265c2c66affSColin Finck } 266c2c66affSColin Finck 267c2c66affSColin Finck /* Read a PNG file. You may want to return an error code if the read 268c2c66affSColin Finck * fails (depending upon the failure). There are two "prototypes" given 269c2c66affSColin Finck * here - one where we are given the filename, and we need to open the 270c2c66affSColin Finck * file, and the other where we are given an open file (possibly with 271c2c66affSColin Finck * some or all of the magic bytes read - see comments above). 272c2c66affSColin Finck */ 273c2c66affSColin Finck #ifdef open_file /* prototype 1 */ 274c2c66affSColin Finck void read_png(char *file_name) /* We need to open the file */ 275c2c66affSColin Finck { 276c2c66affSColin Finck png_structp png_ptr; 277c2c66affSColin Finck png_infop info_ptr; 278c2c66affSColin Finck int sig_read = 0; 279c2c66affSColin Finck png_uint_32 width, height; 280c2c66affSColin Finck int bit_depth, color_type, interlace_type; 281c2c66affSColin Finck FILE *fp; 282c2c66affSColin Finck 283c2c66affSColin Finck if ((fp = fopen(file_name, "rb")) == NULL) 284c2c66affSColin Finck return (ERROR); 285c2c66affSColin Finck 286c2c66affSColin Finck #else no_open_file /* prototype 2 */ 287c2c66affSColin Finck void read_png(FILE *fp, int sig_read) /* File is already open */ 288c2c66affSColin Finck { 289c2c66affSColin Finck png_structp png_ptr; 290c2c66affSColin Finck png_infop info_ptr; 291c2c66affSColin Finck png_uint_32 width, height; 292c2c66affSColin Finck int bit_depth, color_type, interlace_type; 293c2c66affSColin Finck #endif no_open_file /* Only use one prototype! */ 294c2c66affSColin Finck 295c2c66affSColin Finck /* Create and initialize the png_struct with the desired error handler 296c2c66affSColin Finck * functions. If you want to use the default stderr and longjump method, 297c2c66affSColin Finck * you can supply NULL for the last three parameters. We also supply the 298c2c66affSColin Finck * the compiler header file version, so that we know if the application 299b61b1815SThomas Faber * was compiled with a compatible version of the library. REQUIRED. 300c2c66affSColin Finck */ 301c2c66affSColin Finck png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 302c2c66affSColin Finck png_voidp user_error_ptr, user_error_fn, user_warning_fn); 303c2c66affSColin Finck 304c2c66affSColin Finck if (png_ptr == NULL) 305c2c66affSColin Finck { 306c2c66affSColin Finck fclose(fp); 307c2c66affSColin Finck return (ERROR); 308c2c66affSColin Finck } 309c2c66affSColin Finck 310c2c66affSColin Finck /* Allocate/initialize the memory for image information. REQUIRED. */ 311c2c66affSColin Finck info_ptr = png_create_info_struct(png_ptr); 312c2c66affSColin Finck if (info_ptr == NULL) 313c2c66affSColin Finck { 314c2c66affSColin Finck fclose(fp); 315c2c66affSColin Finck png_destroy_read_struct(&png_ptr, NULL, NULL); 316c2c66affSColin Finck return (ERROR); 317c2c66affSColin Finck } 318c2c66affSColin Finck 319c2c66affSColin Finck /* Set error handling if you are using the setjmp/longjmp method (this is 320c2c66affSColin Finck * the normal method of doing things with libpng). REQUIRED unless you 321c2c66affSColin Finck * set up your own error handlers in the png_create_read_struct() earlier. 322c2c66affSColin Finck */ 323c2c66affSColin Finck if (setjmp(png_jmpbuf(png_ptr))) 324c2c66affSColin Finck { 325b61b1815SThomas Faber /* Free all of the memory associated with the png_ptr and info_ptr. */ 326c2c66affSColin Finck png_destroy_read_struct(&png_ptr, &info_ptr, NULL); 327c2c66affSColin Finck fclose(fp); 328b61b1815SThomas Faber /* If we get here, we had a problem reading the file. */ 329c2c66affSColin Finck return (ERROR); 330c2c66affSColin Finck } 331c2c66affSColin Finck 332b61b1815SThomas Faber /* One of the following I/O initialization methods is REQUIRED. */ 333c2c66affSColin Finck #ifdef streams /* PNG file I/O method 1 */ 334b61b1815SThomas Faber /* Set up the input control if you are using standard C streams. */ 335c2c66affSColin Finck png_init_io(png_ptr, fp); 336c2c66affSColin Finck 337c2c66affSColin Finck #else no_streams /* PNG file I/O method 2 */ 338c2c66affSColin Finck /* If you are using replacement read functions, instead of calling 339b61b1815SThomas Faber * png_init_io(), you would call: 340c2c66affSColin Finck */ 341c2c66affSColin Finck png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn); 342b61b1815SThomas Faber /* where user_io_ptr is a structure you want available to the callbacks. */ 343c2c66affSColin Finck #endif no_streams /* Use only one I/O method! */ 344c2c66affSColin Finck 345c2c66affSColin Finck /* If we have already read some of the signature */ 346c2c66affSColin Finck png_set_sig_bytes(png_ptr, sig_read); 347c2c66affSColin Finck 348c2c66affSColin Finck #ifdef hilevel 349b61b1815SThomas Faber /* If you have enough memory to read in the entire image at once, 350c2c66affSColin Finck * and you need to specify only transforms that can be controlled 351c2c66affSColin Finck * with one of the PNG_TRANSFORM_* bits (this presently excludes 352c2c66affSColin Finck * quantizing, filling, setting background, and doing gamma 353c2c66affSColin Finck * adjustment), then you can read the entire image (including 354c2c66affSColin Finck * pixels) into the info structure with this call: 355c2c66affSColin Finck */ 356c2c66affSColin Finck png_read_png(png_ptr, info_ptr, png_transforms, NULL); 357c2c66affSColin Finck 358c2c66affSColin Finck #else 359b61b1815SThomas Faber /* OK, you're doing it the hard way, with the lower-level functions. */ 360c2c66affSColin Finck 361c2c66affSColin Finck /* The call to png_read_info() gives us all of the information from the 362b61b1815SThomas Faber * PNG file before the first IDAT (image data chunk). REQUIRED. 363c2c66affSColin Finck */ 364c2c66affSColin Finck png_read_info(png_ptr, info_ptr); 365c2c66affSColin Finck 366c2c66affSColin Finck png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, 367c2c66affSColin Finck &interlace_type, NULL, NULL); 368c2c66affSColin Finck 369c2c66affSColin Finck /* Set up the data transformations you want. Note that these are all 370c2c66affSColin Finck * optional. Only call them if you want/need them. Many of the 371c2c66affSColin Finck * transformations only work on specific types of images, and many 372c2c66affSColin Finck * are mutually exclusive. 373c2c66affSColin Finck */ 374c2c66affSColin Finck 375c2c66affSColin Finck /* Tell libpng to strip 16 bits/color files down to 8 bits/color. 376c2c66affSColin Finck * Use accurate scaling if it's available, otherwise just chop off the 377c2c66affSColin Finck * low byte. 378c2c66affSColin Finck */ 379c2c66affSColin Finck #ifdef PNG_READ_SCALE_16_TO_8_SUPPORTED 380c2c66affSColin Finck png_set_scale_16(png_ptr); 381c2c66affSColin Finck #else 382c2c66affSColin Finck png_set_strip_16(png_ptr); 383c2c66affSColin Finck #endif 384c2c66affSColin Finck 385c2c66affSColin Finck /* Strip alpha bytes from the input data without combining with the 386c2c66affSColin Finck * background (not recommended). 387c2c66affSColin Finck */ 388c2c66affSColin Finck png_set_strip_alpha(png_ptr); 389c2c66affSColin Finck 390b61b1815SThomas Faber /* Extract multiple pixels with bit depths of 1, 2 or 4 from a single 391c2c66affSColin Finck * byte into separate bytes (useful for paletted and grayscale images). 392c2c66affSColin Finck */ 393c2c66affSColin Finck png_set_packing(png_ptr); 394c2c66affSColin Finck 395c2c66affSColin Finck /* Change the order of packed pixels to least significant bit first 396b61b1815SThomas Faber * (not useful if you are using png_set_packing). 397b61b1815SThomas Faber */ 398c2c66affSColin Finck png_set_packswap(png_ptr); 399c2c66affSColin Finck 400b61b1815SThomas Faber /* Expand paletted colors into true RGB triplets. */ 401c2c66affSColin Finck if (color_type == PNG_COLOR_TYPE_PALETTE) 402c2c66affSColin Finck png_set_palette_to_rgb(png_ptr); 403c2c66affSColin Finck 404b61b1815SThomas Faber /* Expand grayscale images to the full 8 bits from 1, 2 or 4 bits/pixel. */ 405c2c66affSColin Finck if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) 406c2c66affSColin Finck png_set_expand_gray_1_2_4_to_8(png_ptr); 407c2c66affSColin Finck 408c2c66affSColin Finck /* Expand paletted or RGB images with transparency to full alpha channels 409c2c66affSColin Finck * so the data will be available as RGBA quartets. 410c2c66affSColin Finck */ 411c2c66affSColin Finck if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS) != 0) 412c2c66affSColin Finck png_set_tRNS_to_alpha(png_ptr); 413c2c66affSColin Finck 414c2c66affSColin Finck /* Set the background color to draw transparent and alpha images over. 415b61b1815SThomas Faber * It is possible to set the red, green and blue components directly 416b61b1815SThomas Faber * for paletted images, instead of supplying a palette index. Note that, 417c2c66affSColin Finck * even if the PNG file supplies a background, you are not required to 418c2c66affSColin Finck * use it - you should use the (solid) application background if it has one. 419c2c66affSColin Finck */ 420c2c66affSColin Finck png_color_16 my_background, *image_background; 421c2c66affSColin Finck 422c2c66affSColin Finck if (png_get_bKGD(png_ptr, info_ptr, &image_background) != 0) 423c2c66affSColin Finck png_set_background(png_ptr, image_background, 424c2c66affSColin Finck PNG_BACKGROUND_GAMMA_FILE, 1, 1.0); 425c2c66affSColin Finck else 426c2c66affSColin Finck png_set_background(png_ptr, &my_background, 427c2c66affSColin Finck PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0); 428c2c66affSColin Finck 429b61b1815SThomas Faber /* Some suggestions as to how to get a screen gamma value. 430c2c66affSColin Finck * 431c2c66affSColin Finck * Note that screen gamma is the display_exponent, which includes 432b61b1815SThomas Faber * the CRT_exponent and any correction for viewing conditions. 433c2c66affSColin Finck */ 434c2c66affSColin Finck if (/* We have a user-defined screen gamma value */) 435c2c66affSColin Finck screen_gamma = user-defined screen_gamma; 436b61b1815SThomas Faber /* This is one way that applications share the same screen gamma value. */ 437c2c66affSColin Finck else if ((gamma_str = getenv("SCREEN_GAMMA")) != NULL) 438c2c66affSColin Finck screen_gamma = atof(gamma_str); 439c2c66affSColin Finck /* If we don't have another value */ 440c2c66affSColin Finck else 441c2c66affSColin Finck { 442c2c66affSColin Finck screen_gamma = PNG_DEFAULT_sRGB; /* A good guess for a PC monitor 443c2c66affSColin Finck in a dimly lit room */ 444b61b1815SThomas Faber screen_gamma = PNG_GAMMA_MAC_18 or 1.0; /* Good guesses for Mac 445b61b1815SThomas Faber systems */ 446c2c66affSColin Finck } 447c2c66affSColin Finck 448c2c66affSColin Finck /* Tell libpng to handle the gamma conversion for you. The final call 449c2c66affSColin Finck * is a good guess for PC generated images, but it should be configurable 450b61b1815SThomas Faber * by the user at run time. Gamma correction support in your application 451b61b1815SThomas Faber * is strongly recommended. 452c2c66affSColin Finck */ 453c2c66affSColin Finck 454c2c66affSColin Finck int intent; 455c2c66affSColin Finck 456c2c66affSColin Finck if (png_get_sRGB(png_ptr, info_ptr, &intent) != 0) 457c2c66affSColin Finck png_set_gamma(png_ptr, screen_gamma, PNG_DEFAULT_sRGB); 458c2c66affSColin Finck else 459c2c66affSColin Finck { 460c2c66affSColin Finck double image_gamma; 461c2c66affSColin Finck if (png_get_gAMA(png_ptr, info_ptr, &image_gamma) != 0) 462c2c66affSColin Finck png_set_gamma(png_ptr, screen_gamma, image_gamma); 463c2c66affSColin Finck else 464c2c66affSColin Finck png_set_gamma(png_ptr, screen_gamma, 0.45455); 465c2c66affSColin Finck } 466c2c66affSColin Finck 467c2c66affSColin Finck #ifdef PNG_READ_QUANTIZE_SUPPORTED 468b61b1815SThomas Faber /* Quantize RGB files down to 8-bit palette, or reduce palettes 469c2c66affSColin Finck * to the number of colors available on your screen. 470c2c66affSColin Finck */ 471c2c66affSColin Finck if ((color_type & PNG_COLOR_MASK_COLOR) != 0) 472c2c66affSColin Finck { 473c2c66affSColin Finck int num_palette; 474c2c66affSColin Finck png_colorp palette; 475c2c66affSColin Finck 476b61b1815SThomas Faber /* This reduces the image to the application-supplied palette. */ 477c2c66affSColin Finck if (/* We have our own palette */) 478c2c66affSColin Finck { 479b61b1815SThomas Faber /* An array of colors to which the image should be quantized. */ 480c2c66affSColin Finck png_color std_color_cube[MAX_SCREEN_COLORS]; 481c2c66affSColin Finck png_set_quantize(png_ptr, std_color_cube, MAX_SCREEN_COLORS, 482c2c66affSColin Finck MAX_SCREEN_COLORS, NULL, 0); 483c2c66affSColin Finck } 484b61b1815SThomas Faber /* This reduces the image to the palette supplied in the file. */ 485c2c66affSColin Finck else if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette) != 0) 486c2c66affSColin Finck { 487c2c66affSColin Finck png_uint_16p histogram = NULL; 488c2c66affSColin Finck png_get_hIST(png_ptr, info_ptr, &histogram); 489c2c66affSColin Finck png_set_quantize(png_ptr, palette, num_palette, 490c2c66affSColin Finck max_screen_colors, histogram, 0); 491c2c66affSColin Finck } 492c2c66affSColin Finck } 493c2c66affSColin Finck #endif /* READ_QUANTIZE */ 494c2c66affSColin Finck 495b61b1815SThomas Faber /* Invert monochrome files to have 0 as white and 1 as black. */ 496c2c66affSColin Finck png_set_invert_mono(png_ptr); 497c2c66affSColin Finck 498c2c66affSColin Finck /* If you want to shift the pixel values from the range [0,255] or 499c2c66affSColin Finck * [0,65535] to the original [0,7] or [0,31], or whatever range the 500c2c66affSColin Finck * colors were originally in: 501c2c66affSColin Finck */ 502c2c66affSColin Finck if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT) != 0) 503c2c66affSColin Finck { 504c2c66affSColin Finck png_color_8p sig_bit_p; 505c2c66affSColin Finck png_get_sBIT(png_ptr, info_ptr, &sig_bit_p); 506c2c66affSColin Finck png_set_shift(png_ptr, sig_bit_p); 507c2c66affSColin Finck } 508c2c66affSColin Finck 509b61b1815SThomas Faber /* Flip the RGB pixels to BGR (or RGBA to BGRA). */ 510c2c66affSColin Finck if ((color_type & PNG_COLOR_MASK_COLOR) != 0) 511c2c66affSColin Finck png_set_bgr(png_ptr); 512c2c66affSColin Finck 513b61b1815SThomas Faber /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR). */ 514c2c66affSColin Finck png_set_swap_alpha(png_ptr); 515c2c66affSColin Finck 516b61b1815SThomas Faber /* Swap bytes of 16-bit files to least significant byte first. */ 517c2c66affSColin Finck png_set_swap(png_ptr); 518c2c66affSColin Finck 519b61b1815SThomas Faber /* Add filler (or alpha) byte (before/after each RGB triplet). */ 520c2c66affSColin Finck png_set_filler(png_ptr, 0xffff, PNG_FILLER_AFTER); 521c2c66affSColin Finck 522c2c66affSColin Finck #ifdef PNG_READ_INTERLACING_SUPPORTED 523c2c66affSColin Finck /* Turn on interlace handling. REQUIRED if you are not using 524c2c66affSColin Finck * png_read_image(). To see how to handle interlacing passes, 525c2c66affSColin Finck * see the png_read_row() method below: 526c2c66affSColin Finck */ 527c2c66affSColin Finck number_passes = png_set_interlace_handling(png_ptr); 528c2c66affSColin Finck #else /* !READ_INTERLACING */ 529c2c66affSColin Finck number_passes = 1; 530c2c66affSColin Finck #endif /* READ_INTERLACING */ 531c2c66affSColin Finck 532c2c66affSColin Finck /* Optional call to gamma correct and add the background to the palette 533c2c66affSColin Finck * and update info structure. REQUIRED if you are expecting libpng to 534b61b1815SThomas Faber * update the palette for you (i.e. you selected such a transform above). 535c2c66affSColin Finck */ 536c2c66affSColin Finck png_read_update_info(png_ptr, info_ptr); 537c2c66affSColin Finck 538c2c66affSColin Finck /* Allocate the memory to hold the image using the fields of info_ptr. */ 539c2c66affSColin Finck png_bytep row_pointers[height]; 540c2c66affSColin Finck for (row = 0; row < height; row++) 541b61b1815SThomas Faber row_pointers[row] = NULL; /* Clear the pointer array */ 542c2c66affSColin Finck for (row = 0; row < height; row++) 543c2c66affSColin Finck row_pointers[row] = png_malloc(png_ptr, png_get_rowbytes(png_ptr, 544c2c66affSColin Finck info_ptr)); 545c2c66affSColin Finck 546b61b1815SThomas Faber /* Now it's time to read the image. One of these methods is REQUIRED. */ 547c2c66affSColin Finck #ifdef entire /* Read the entire image in one go */ 548c2c66affSColin Finck png_read_image(png_ptr, row_pointers); 549c2c66affSColin Finck 550c2c66affSColin Finck #else no_entire /* Read the image one or more scanlines at a time */ 551c2c66affSColin Finck /* The other way to read images - deal with interlacing: */ 552c2c66affSColin Finck for (pass = 0; pass < number_passes; pass++) 553c2c66affSColin Finck { 554c2c66affSColin Finck #ifdef single /* Read the image a single row at a time */ 555c2c66affSColin Finck for (y = 0; y < height; y++) 556c2c66affSColin Finck png_read_rows(png_ptr, &row_pointers[y], NULL, 1); 557c2c66affSColin Finck 558c2c66affSColin Finck #else no_single /* Read the image several rows at a time */ 559c2c66affSColin Finck for (y = 0; y < height; y += number_of_rows) 560c2c66affSColin Finck { 561c2c66affSColin Finck #ifdef sparkle /* Read the image using the "sparkle" effect. */ 562c2c66affSColin Finck png_read_rows(png_ptr, &row_pointers[y], NULL, 563c2c66affSColin Finck number_of_rows); 564c2c66affSColin Finck #else no_sparkle /* Read the image using the "rectangle" effect */ 565c2c66affSColin Finck png_read_rows(png_ptr, NULL, &row_pointers[y], 566c2c66affSColin Finck number_of_rows); 567c2c66affSColin Finck #endif no_sparkle /* Use only one of these two methods */ 568c2c66affSColin Finck } 569c2c66affSColin Finck 570b61b1815SThomas Faber /* If you want to display the image after every pass, do so here. */ 571c2c66affSColin Finck #endif no_single /* Use only one of these two methods */ 572c2c66affSColin Finck } 573c2c66affSColin Finck #endif no_entire /* Use only one of these two methods */ 574c2c66affSColin Finck 575b61b1815SThomas Faber /* Read rest of file, and get additional chunks in info_ptr. REQUIRED. */ 576c2c66affSColin Finck png_read_end(png_ptr, info_ptr); 577c2c66affSColin Finck #endif hilevel 578c2c66affSColin Finck 579b61b1815SThomas Faber /* At this point you have read the entire image. */ 580c2c66affSColin Finck 581b61b1815SThomas Faber /* Clean up after the read, and free any memory allocated. REQUIRED. */ 582c2c66affSColin Finck png_destroy_read_struct(&png_ptr, &info_ptr, NULL); 583c2c66affSColin Finck 584b61b1815SThomas Faber /* Close the file. */ 585c2c66affSColin Finck fclose(fp); 586c2c66affSColin Finck 587b61b1815SThomas Faber /* That's it! */ 588c2c66affSColin Finck return (OK); 589c2c66affSColin Finck } 590c2c66affSColin Finck 591c2c66affSColin Finck /* Progressively read a file */ 592c2c66affSColin Finck 593c2c66affSColin Finck int 594c2c66affSColin Finck initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr) 595c2c66affSColin Finck { 596c2c66affSColin Finck /* Create and initialize the png_struct with the desired error handler 597c2c66affSColin Finck * functions. If you want to use the default stderr and longjump method, 598c2c66affSColin Finck * you can supply NULL for the last three parameters. We also check that 599b61b1815SThomas Faber * the library version is compatible, in case we are using dynamically 600c2c66affSColin Finck * linked libraries. 601c2c66affSColin Finck */ 602c2c66affSColin Finck *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, 603c2c66affSColin Finck png_voidp user_error_ptr, user_error_fn, user_warning_fn); 604c2c66affSColin Finck if (*png_ptr == NULL) 605c2c66affSColin Finck { 606c2c66affSColin Finck *info_ptr = NULL; 607c2c66affSColin Finck return (ERROR); 608c2c66affSColin Finck } 609c2c66affSColin Finck *info_ptr = png_create_info_struct(png_ptr); 610c2c66affSColin Finck if (*info_ptr == NULL) 611c2c66affSColin Finck { 612c2c66affSColin Finck png_destroy_read_struct(png_ptr, info_ptr, NULL); 613c2c66affSColin Finck return (ERROR); 614c2c66affSColin Finck } 615c2c66affSColin Finck if (setjmp(png_jmpbuf((*png_ptr)))) 616c2c66affSColin Finck { 617c2c66affSColin Finck png_destroy_read_struct(png_ptr, info_ptr, NULL); 618c2c66affSColin Finck return (ERROR); 619c2c66affSColin Finck } 620c2c66affSColin Finck 621b61b1815SThomas Faber /* You will need to provide all three function callbacks, 622b61b1815SThomas Faber * even if you aren't using all of them. 623c2c66affSColin Finck * If you aren't using all functions, you can specify NULL 624c2c66affSColin Finck * parameters. Even when all three functions are NULL, 625c2c66affSColin Finck * you need to call png_set_progressive_read_fn(). 626c2c66affSColin Finck * These functions shouldn't be dependent on global or 627c2c66affSColin Finck * static variables if you are decoding several images 628c2c66affSColin Finck * simultaneously. You should store stream specific data 629c2c66affSColin Finck * in a separate struct, given as the second parameter, 630c2c66affSColin Finck * and retrieve the pointer from inside the callbacks using 631c2c66affSColin Finck * the function png_get_progressive_ptr(png_ptr). 632c2c66affSColin Finck */ 633c2c66affSColin Finck png_set_progressive_read_fn(*png_ptr, (void *)stream_data, 634c2c66affSColin Finck info_callback, row_callback, end_callback); 635c2c66affSColin Finck return (OK); 636c2c66affSColin Finck } 637c2c66affSColin Finck 638c2c66affSColin Finck int 639c2c66affSColin Finck process_data(png_structp *png_ptr, png_infop *info_ptr, 640c2c66affSColin Finck png_bytep buffer, png_uint_32 length) 641c2c66affSColin Finck { 642c2c66affSColin Finck if (setjmp(png_jmpbuf((*png_ptr)))) 643c2c66affSColin Finck { 644b61b1815SThomas Faber /* Free the png_ptr and info_ptr memory on error. */ 645c2c66affSColin Finck png_destroy_read_struct(png_ptr, info_ptr, NULL); 646c2c66affSColin Finck return (ERROR); 647c2c66affSColin Finck } 648c2c66affSColin Finck 649b61b1815SThomas Faber /* Give chunks of data as they arrive from the data stream 650b61b1815SThomas Faber * (in order, of course). 651c2c66affSColin Finck * On segmented machines, don't give it any more than 64K. 652c2c66affSColin Finck * The library seems to run fine with sizes of 4K, although 653b61b1815SThomas Faber * you can give it much less if necessary. (I assume you can 654c2c66affSColin Finck * give it chunks of 1 byte, but I haven't tried with less 655b61b1815SThomas Faber * than 256 bytes yet.) When this function returns, you may 656c2c66affSColin Finck * want to display any rows that were generated in the row 657c2c66affSColin Finck * callback, if you aren't already displaying them there. 658c2c66affSColin Finck */ 659c2c66affSColin Finck png_process_data(*png_ptr, *info_ptr, buffer, length); 660c2c66affSColin Finck return (OK); 661c2c66affSColin Finck } 662c2c66affSColin Finck 663c2c66affSColin Finck info_callback(png_structp png_ptr, png_infop info) 664c2c66affSColin Finck { 665c2c66affSColin Finck /* Do any setup here, including setting any of the transformations 666c2c66affSColin Finck * mentioned in the Reading PNG files section. For now, you _must_ 667c2c66affSColin Finck * call either png_start_read_image() or png_read_update_info() 668c2c66affSColin Finck * after all the transformations are set (even if you don't set 669c2c66affSColin Finck * any). You may start getting rows before png_process_data() 670c2c66affSColin Finck * returns, so this is your last chance to prepare for that. 671c2c66affSColin Finck */ 672c2c66affSColin Finck } 673c2c66affSColin Finck 674c2c66affSColin Finck row_callback(png_structp png_ptr, png_bytep new_row, 675c2c66affSColin Finck png_uint_32 row_num, int pass) 676c2c66affSColin Finck { 677b61b1815SThomas Faber /* This function is called for every row in the image. If the 678c2c66affSColin Finck * image is interlaced, and you turned on the interlace handler, 679c2c66affSColin Finck * this function will be called for every row in every pass. 680c2c66affSColin Finck * 681c2c66affSColin Finck * In this function you will receive a pointer to new row data from 682c2c66affSColin Finck * libpng called new_row that is to replace a corresponding row (of 683c2c66affSColin Finck * the same data format) in a buffer allocated by your application. 684c2c66affSColin Finck * 685c2c66affSColin Finck * The new row data pointer "new_row" may be NULL, indicating there is 686c2c66affSColin Finck * no new data to be replaced (in cases of interlace loading). 687c2c66affSColin Finck * 688b61b1815SThomas Faber * If new_row is not NULL, then you need to call 689b61b1815SThomas Faber * png_progressive_combine_row(), to replace the corresponding row as 690c2c66affSColin Finck * shown below: 691c2c66affSColin Finck */ 692c2c66affSColin Finck 693b61b1815SThomas Faber /* Get pointer to corresponding row in our PNG read buffer. */ 694c2c66affSColin Finck png_bytep old_row = ((png_bytep *)our_data)[row_num]; 695c2c66affSColin Finck 696c2c66affSColin Finck #ifdef PNG_READ_INTERLACING_SUPPORTED 697b61b1815SThomas Faber /* If both rows are allocated, then copy the new row 698c2c66affSColin Finck * data to the corresponding row data. 699c2c66affSColin Finck */ 700b61b1815SThomas Faber if (old_row != NULL && new_row != NULL) 701c2c66affSColin Finck png_progressive_combine_row(png_ptr, old_row, new_row); 702c2c66affSColin Finck 703b61b1815SThomas Faber /* The rows and passes are called in order, so you don't really 704c2c66affSColin Finck * need the row_num and pass, but I'm supplying them because it 705c2c66affSColin Finck * may make your life easier. 706c2c66affSColin Finck * 707c2c66affSColin Finck * For the non-NULL rows of interlaced images, you must call 708c2c66affSColin Finck * png_progressive_combine_row() passing in the new row and the 709c2c66affSColin Finck * old row, as demonstrated above. You can call this function for 710c2c66affSColin Finck * NULL rows (it will just return) and for non-interlaced images 711c2c66affSColin Finck * (it just does the memcpy for you) if it will make the code 712c2c66affSColin Finck * easier. Thus, you can just do this for all cases: 713c2c66affSColin Finck */ 714c2c66affSColin Finck png_progressive_combine_row(png_ptr, old_row, new_row); 715c2c66affSColin Finck 716c2c66affSColin Finck /* where old_row is what was displayed for previous rows. Note 717c2c66affSColin Finck * that the first pass (pass == 0 really) will completely cover 718c2c66affSColin Finck * the old row, so the rows do not have to be initialized. After 719c2c66affSColin Finck * the first pass (and only for interlaced images), you will have 720c2c66affSColin Finck * to pass the current row as new_row, and the function will combine 721c2c66affSColin Finck * the old row and the new row. 722c2c66affSColin Finck */ 723c2c66affSColin Finck #endif /* READ_INTERLACING */ 724c2c66affSColin Finck } 725c2c66affSColin Finck 726c2c66affSColin Finck end_callback(png_structp png_ptr, png_infop info) 727c2c66affSColin Finck { 728c2c66affSColin Finck /* This function is called when the whole image has been read, 729c2c66affSColin Finck * including any chunks after the image (up to and including 730c2c66affSColin Finck * the IEND). You will usually have the same info chunk as you 731c2c66affSColin Finck * had in the header, although some data may have been added 732c2c66affSColin Finck * to the comments and time fields. 733c2c66affSColin Finck * 734c2c66affSColin Finck * Most people won't do much here, perhaps setting a flag that 735c2c66affSColin Finck * marks the image as finished. 736c2c66affSColin Finck */ 737c2c66affSColin Finck } 738c2c66affSColin Finck 739c2c66affSColin Finck /* Write a png file */ 740c2c66affSColin Finck void write_png(char *file_name /* , ... other image information ... */) 741c2c66affSColin Finck { 742c2c66affSColin Finck FILE *fp; 743c2c66affSColin Finck png_structp png_ptr; 744c2c66affSColin Finck png_infop info_ptr; 745c2c66affSColin Finck png_colorp palette; 746c2c66affSColin Finck 747c2c66affSColin Finck /* Open the file */ 748c2c66affSColin Finck fp = fopen(file_name, "wb"); 749c2c66affSColin Finck if (fp == NULL) 750c2c66affSColin Finck return (ERROR); 751c2c66affSColin Finck 752c2c66affSColin Finck /* Create and initialize the png_struct with the desired error handler 753c2c66affSColin Finck * functions. If you want to use the default stderr and longjump method, 754c2c66affSColin Finck * you can supply NULL for the last three parameters. We also check that 755c2c66affSColin Finck * the library version is compatible with the one used at compile time, 756c2c66affSColin Finck * in case we are using dynamically linked libraries. REQUIRED. 757c2c66affSColin Finck */ 758c2c66affSColin Finck png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 759c2c66affSColin Finck png_voidp user_error_ptr, user_error_fn, user_warning_fn); 760c2c66affSColin Finck if (png_ptr == NULL) 761c2c66affSColin Finck { 762c2c66affSColin Finck fclose(fp); 763c2c66affSColin Finck return (ERROR); 764c2c66affSColin Finck } 765c2c66affSColin Finck 766b61b1815SThomas Faber /* Allocate/initialize the image information data. REQUIRED. */ 767c2c66affSColin Finck info_ptr = png_create_info_struct(png_ptr); 768c2c66affSColin Finck if (info_ptr == NULL) 769c2c66affSColin Finck { 770c2c66affSColin Finck fclose(fp); 771c2c66affSColin Finck png_destroy_write_struct(&png_ptr, NULL); 772c2c66affSColin Finck return (ERROR); 773c2c66affSColin Finck } 774c2c66affSColin Finck 775b61b1815SThomas Faber /* Set up error handling. REQUIRED if you aren't supplying your own 776c2c66affSColin Finck * error handling functions in the png_create_write_struct() call. 777c2c66affSColin Finck */ 778c2c66affSColin Finck if (setjmp(png_jmpbuf(png_ptr))) 779c2c66affSColin Finck { 780b61b1815SThomas Faber /* If we get here, we had a problem writing the file. */ 781c2c66affSColin Finck fclose(fp); 782c2c66affSColin Finck png_destroy_write_struct(&png_ptr, &info_ptr); 783c2c66affSColin Finck return (ERROR); 784c2c66affSColin Finck } 785c2c66affSColin Finck 786b61b1815SThomas Faber /* One of the following I/O initialization functions is REQUIRED. */ 787c2c66affSColin Finck 788c2c66affSColin Finck #ifdef streams /* I/O initialization method 1 */ 789b61b1815SThomas Faber /* Set up the output control if you are using standard C streams. */ 790c2c66affSColin Finck png_init_io(png_ptr, fp); 791c2c66affSColin Finck 792c2c66affSColin Finck #else no_streams /* I/O initialization method 2 */ 793c2c66affSColin Finck /* If you are using replacement write functions, instead of calling 794b61b1815SThomas Faber * png_init_io(), you would call: 795c2c66affSColin Finck */ 796c2c66affSColin Finck png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn, 797c2c66affSColin Finck user_IO_flush_function); 798b61b1815SThomas Faber /* where user_io_ptr is a structure you want available to the callbacks. */ 799c2c66affSColin Finck #endif no_streams /* Only use one initialization method */ 800c2c66affSColin Finck 801c2c66affSColin Finck #ifdef hilevel 802c2c66affSColin Finck /* This is the easy way. Use it if you already have all the 803c2c66affSColin Finck * image info living in the structure. You could "|" many 804c2c66affSColin Finck * PNG_TRANSFORM flags into the png_transforms integer here. 805c2c66affSColin Finck */ 806c2c66affSColin Finck png_write_png(png_ptr, info_ptr, png_transforms, NULL); 807c2c66affSColin Finck 808c2c66affSColin Finck #else 809b61b1815SThomas Faber /* This is the hard way. */ 810c2c66affSColin Finck 811c2c66affSColin Finck /* Set the image information here. Width and height are up to 2^31, 812b61b1815SThomas Faber * bit_depth is one of 1, 2, 4, 8 or 16, but valid values also depend on 813c2c66affSColin Finck * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY, 814c2c66affSColin Finck * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB, 815c2c66affSColin Finck * or PNG_COLOR_TYPE_RGB_ALPHA. interlace is either PNG_INTERLACE_NONE or 816c2c66affSColin Finck * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST 817b61b1815SThomas Faber * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. 818b61b1815SThomas Faber * REQUIRED. 819c2c66affSColin Finck */ 820b61b1815SThomas Faber png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, 821b61b1815SThomas Faber PNG_COLOR_TYPE_???, PNG_INTERLACE_????, 822b61b1815SThomas Faber PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE); 823c2c66affSColin Finck 824b61b1815SThomas Faber /* Set the palette if there is one. REQUIRED for indexed-color images. */ 825b61b1815SThomas Faber palette = (png_colorp)png_malloc(png_ptr, 826b61b1815SThomas Faber PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))); 827c2c66affSColin Finck /* ... Set palette colors ... */ 828c2c66affSColin Finck png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH); 829b61b1815SThomas Faber /* You must not free palette here, because png_set_PLTE only makes a link 830b61b1815SThomas Faber * to the palette that you allocated. Wait until you are about to destroy 831c2c66affSColin Finck * the png structure. 832c2c66affSColin Finck */ 833c2c66affSColin Finck 834b61b1815SThomas Faber /* Optional significant bit (sBIT) chunk. */ 835c2c66affSColin Finck png_color_8 sig_bit; 836c2c66affSColin Finck 837c2c66affSColin Finck /* If we are dealing with a grayscale image then */ 838c2c66affSColin Finck sig_bit.gray = true_bit_depth; 839c2c66affSColin Finck 840c2c66affSColin Finck /* Otherwise, if we are dealing with a color image then */ 841c2c66affSColin Finck sig_bit.red = true_red_bit_depth; 842c2c66affSColin Finck sig_bit.green = true_green_bit_depth; 843c2c66affSColin Finck sig_bit.blue = true_blue_bit_depth; 844c2c66affSColin Finck 845c2c66affSColin Finck /* If the image has an alpha channel then */ 846c2c66affSColin Finck sig_bit.alpha = true_alpha_bit_depth; 847c2c66affSColin Finck 848c2c66affSColin Finck png_set_sBIT(png_ptr, info_ptr, &sig_bit); 849c2c66affSColin Finck 850c2c66affSColin Finck /* Optional gamma chunk is strongly suggested if you have any guess 851c2c66affSColin Finck * as to the correct gamma of the image. 852c2c66affSColin Finck */ 853c2c66affSColin Finck png_set_gAMA(png_ptr, info_ptr, gamma); 854c2c66affSColin Finck 855b61b1815SThomas Faber /* Optionally write comments into the image. */ 856c2c66affSColin Finck { 857c2c66affSColin Finck png_text text_ptr[3]; 858c2c66affSColin Finck 859c2c66affSColin Finck char key0[] = "Title"; 860c2c66affSColin Finck char text0[] = "Mona Lisa"; 861c2c66affSColin Finck text_ptr[0].key = key0; 862c2c66affSColin Finck text_ptr[0].text = text0; 863c2c66affSColin Finck text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE; 864c2c66affSColin Finck text_ptr[0].itxt_length = 0; 865c2c66affSColin Finck text_ptr[0].lang = NULL; 866c2c66affSColin Finck text_ptr[0].lang_key = NULL; 867c2c66affSColin Finck 868c2c66affSColin Finck char key1[] = "Author"; 869c2c66affSColin Finck char text1[] = "Leonardo DaVinci"; 870c2c66affSColin Finck text_ptr[1].key = key1; 871c2c66affSColin Finck text_ptr[1].text = text1; 872c2c66affSColin Finck text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE; 873c2c66affSColin Finck text_ptr[1].itxt_length = 0; 874c2c66affSColin Finck text_ptr[1].lang = NULL; 875c2c66affSColin Finck text_ptr[1].lang_key = NULL; 876c2c66affSColin Finck 877c2c66affSColin Finck char key2[] = "Description"; 878c2c66affSColin Finck char text2[] = "<long text>"; 879c2c66affSColin Finck text_ptr[2].key = key2; 880c2c66affSColin Finck text_ptr[2].text = text2; 881c2c66affSColin Finck text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt; 882c2c66affSColin Finck text_ptr[2].itxt_length = 0; 883c2c66affSColin Finck text_ptr[2].lang = NULL; 884c2c66affSColin Finck text_ptr[2].lang_key = NULL; 885c2c66affSColin Finck 886c2c66affSColin Finck png_set_text(write_ptr, write_info_ptr, text_ptr, 3); 887c2c66affSColin Finck } 888c2c66affSColin Finck 889b61b1815SThomas Faber /* Other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs. */ 890c2c66affSColin Finck 891b61b1815SThomas Faber /* Note that if sRGB is present, the gAMA and cHRM chunks must be ignored 892c2c66affSColin Finck * on read and, if your application chooses to write them, they must 893b61b1815SThomas Faber * be written in accordance with the sRGB profile. 894c2c66affSColin Finck */ 895c2c66affSColin Finck 896b61b1815SThomas Faber /* Write the file header information. REQUIRED. */ 897c2c66affSColin Finck png_write_info(png_ptr, info_ptr); 898c2c66affSColin Finck 899c2c66affSColin Finck /* If you want, you can write the info in two steps, in case you need to 900c2c66affSColin Finck * write your private chunk ahead of PLTE: 901c2c66affSColin Finck * 902c2c66affSColin Finck * png_write_info_before_PLTE(write_ptr, write_info_ptr); 903c2c66affSColin Finck * write_my_chunk(); 904c2c66affSColin Finck * png_write_info(png_ptr, info_ptr); 905c2c66affSColin Finck * 906c2c66affSColin Finck * However, given the level of known- and unknown-chunk support in 1.2.0 907c2c66affSColin Finck * and up, this should no longer be necessary. 908c2c66affSColin Finck */ 909c2c66affSColin Finck 910c2c66affSColin Finck /* Once we write out the header, the compression type on the text 911c2c66affSColin Finck * chunk gets changed to PNG_TEXT_COMPRESSION_NONE_WR or 912c2c66affSColin Finck * PNG_TEXT_COMPRESSION_zTXt_WR, so it doesn't get written out again 913c2c66affSColin Finck * at the end. 914c2c66affSColin Finck */ 915c2c66affSColin Finck 916c2c66affSColin Finck /* Set up the transformations you want. Note that these are 917c2c66affSColin Finck * all optional. Only call them if you want them. 918c2c66affSColin Finck */ 919c2c66affSColin Finck 920b61b1815SThomas Faber /* Invert monochrome pixels. */ 921c2c66affSColin Finck png_set_invert_mono(png_ptr); 922c2c66affSColin Finck 923c2c66affSColin Finck /* Shift the pixels up to a legal bit depth and fill in 924c2c66affSColin Finck * as appropriate to correctly scale the image. 925c2c66affSColin Finck */ 926c2c66affSColin Finck png_set_shift(png_ptr, &sig_bit); 927c2c66affSColin Finck 928b61b1815SThomas Faber /* Pack pixels into bytes. */ 929c2c66affSColin Finck png_set_packing(png_ptr); 930c2c66affSColin Finck 931b61b1815SThomas Faber /* Swap location of alpha bytes from ARGB to RGBA. */ 932c2c66affSColin Finck png_set_swap_alpha(png_ptr); 933c2c66affSColin Finck 934c2c66affSColin Finck /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into 935c2c66affSColin Finck * RGB (4 channels -> 3 channels). The second parameter is not used. 936c2c66affSColin Finck */ 937c2c66affSColin Finck png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE); 938c2c66affSColin Finck 939b61b1815SThomas Faber /* Flip BGR pixels to RGB. */ 940c2c66affSColin Finck png_set_bgr(png_ptr); 941c2c66affSColin Finck 942b61b1815SThomas Faber /* Swap bytes of 16-bit files to most significant byte first. */ 943c2c66affSColin Finck png_set_swap(png_ptr); 944c2c66affSColin Finck 945b61b1815SThomas Faber /* Swap bits of 1-bit, 2-bit, 4-bit packed pixel formats. */ 946c2c66affSColin Finck png_set_packswap(png_ptr); 947c2c66affSColin Finck 948b61b1815SThomas Faber /* Turn on interlace handling if you are not using png_write_image(). */ 949c2c66affSColin Finck if (interlacing != 0) 950c2c66affSColin Finck number_passes = png_set_interlace_handling(png_ptr); 951c2c66affSColin Finck else 952c2c66affSColin Finck number_passes = 1; 953c2c66affSColin Finck 954c2c66affSColin Finck /* The easiest way to write the image (you may have a different memory 955c2c66affSColin Finck * layout, however, so choose what fits your needs best). You need to 956c2c66affSColin Finck * use the first method if you aren't handling interlacing yourself. 957c2c66affSColin Finck */ 958c2c66affSColin Finck png_uint_32 k, height, width; 959c2c66affSColin Finck 960b61b1815SThomas Faber /* In this example, "image" is a one-dimensional array of bytes. */ 961c2c66affSColin Finck 962b61b1815SThomas Faber /* Guard against integer overflow. */ 963b61b1815SThomas Faber if (height > PNG_SIZE_MAX / (width * bytes_per_pixel)) 964b61b1815SThomas Faber png_error(png_ptr, "Image data buffer would be too large"); 965b61b1815SThomas Faber 966c2c66affSColin Finck png_byte image[height * width * bytes_per_pixel]; 967c2c66affSColin Finck png_bytep row_pointers[height]; 968c2c66affSColin Finck 969c2c66affSColin Finck if (height > PNG_UINT_32_MAX / (sizeof (png_bytep))) 970c2c66affSColin Finck png_error(png_ptr, "Image is too tall to process in memory"); 971c2c66affSColin Finck 972b61b1815SThomas Faber /* Set up pointers into your "image" byte array. */ 973c2c66affSColin Finck for (k = 0; k < height; k++) 974c2c66affSColin Finck row_pointers[k] = image + k * width * bytes_per_pixel; 975c2c66affSColin Finck 976b61b1815SThomas Faber /* One of the following output methods is REQUIRED. */ 977c2c66affSColin Finck 978c2c66affSColin Finck #ifdef entire /* Write out the entire image data in one call */ 979c2c66affSColin Finck png_write_image(png_ptr, row_pointers); 980c2c66affSColin Finck 981b61b1815SThomas Faber /* The other way to write the image - deal with interlacing. */ 982c2c66affSColin Finck 983c2c66affSColin Finck #else no_entire /* Write out the image data by one or more scanlines */ 984c2c66affSColin Finck 985c2c66affSColin Finck /* The number of passes is either 1 for non-interlaced images, 986c2c66affSColin Finck * or 7 for interlaced images. 987c2c66affSColin Finck */ 988c2c66affSColin Finck for (pass = 0; pass < number_passes; pass++) 989c2c66affSColin Finck { 990c2c66affSColin Finck /* Write a few rows at a time. */ 991c2c66affSColin Finck png_write_rows(png_ptr, &row_pointers[first_row], number_of_rows); 992c2c66affSColin Finck 993b61b1815SThomas Faber /* If you are only writing one row at a time, this works. */ 994c2c66affSColin Finck for (y = 0; y < height; y++) 995c2c66affSColin Finck png_write_rows(png_ptr, &row_pointers[y], 1); 996c2c66affSColin Finck } 997c2c66affSColin Finck #endif no_entire /* Use only one output method */ 998c2c66affSColin Finck 999c2c66affSColin Finck /* You can write optional chunks like tEXt, zTXt, and tIME at the end 1000b61b1815SThomas Faber * as well. Shouldn't be necessary in 1.2.0 and up, as all the public 1001b61b1815SThomas Faber * chunks are supported, and you can use png_set_unknown_chunks() to 1002c2c66affSColin Finck * register unknown chunks into the info structure to be written out. 1003c2c66affSColin Finck */ 1004c2c66affSColin Finck 1005b61b1815SThomas Faber /* It is REQUIRED to call this to finish writing the rest of the file. */ 1006c2c66affSColin Finck png_write_end(png_ptr, info_ptr); 1007c2c66affSColin Finck #endif hilevel 1008c2c66affSColin Finck 1009b61b1815SThomas Faber /* If you png_malloced a palette, free it here. 1010b61b1815SThomas Faber * (Don't free info_ptr->palette, as shown in versions 1.0.5m and earlier of 1011b61b1815SThomas Faber * this example; if libpng mallocs info_ptr->palette, libpng will free it). 1012b61b1815SThomas Faber * If you allocated it with malloc() instead of png_malloc(), use free() 1013b61b1815SThomas Faber * instead of png_free(). 1014c2c66affSColin Finck */ 1015c2c66affSColin Finck png_free(png_ptr, palette); 1016c2c66affSColin Finck palette = NULL; 1017c2c66affSColin Finck 1018c2c66affSColin Finck /* Similarly, if you png_malloced any data that you passed in with 1019c2c66affSColin Finck * png_set_something(), such as a hist or trans array, free it here, 1020c2c66affSColin Finck * when you can be sure that libpng is through with it. 1021c2c66affSColin Finck */ 1022c2c66affSColin Finck png_free(png_ptr, trans); 1023c2c66affSColin Finck trans = NULL; 1024b61b1815SThomas Faber 1025b61b1815SThomas Faber /* Whenever you use png_free(), it is a good idea to set the pointer to 1026c2c66affSColin Finck * NULL in case your application inadvertently tries to png_free() it 1027b61b1815SThomas Faber * again. When png_free() sees a NULL it returns without action, avoiding 1028b61b1815SThomas Faber * the double-free problem. 1029c2c66affSColin Finck */ 1030c2c66affSColin Finck 1031b61b1815SThomas Faber /* Clean up after the write, and free any allocated memory. */ 1032c2c66affSColin Finck png_destroy_write_struct(&png_ptr, &info_ptr); 1033c2c66affSColin Finck 1034b61b1815SThomas Faber /* Close the file. */ 1035c2c66affSColin Finck fclose(fp); 1036c2c66affSColin Finck 1037b61b1815SThomas Faber /* That's it! */ 1038c2c66affSColin Finck return (OK); 1039c2c66affSColin Finck } 1040c2c66affSColin Finck 1041c2c66affSColin Finck #endif /* if 0 */ 1042