Blame example.c

Packit 0ba690
Packit 0ba690
#if 0 /* in case someone actually tries to compile this */
Packit 0ba690
Packit 0ba690
/* example.c - an example of using libpng
Packit 0ba690
 * Last changed in libpng 1.2.37 [June 4, 2009]
Packit 0ba690
 * This file has been placed in the public domain by the authors.
Packit 0ba690
 * Maintained 1998-2009 Glenn Randers-Pehrson
Packit 0ba690
 * Maintained 1996, 1997 Andreas Dilger)
Packit 0ba690
 * Written 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
Packit 0ba690
 */
Packit 0ba690
Packit 0ba690
/* This is an example of how to use libpng to read and write PNG files.
Packit 0ba690
 * The file libpng.txt is much more verbose then this.  If you have not
Packit 0ba690
 * read it, do so first.  This was designed to be a starting point of an
Packit 0ba690
 * implementation.  This is not officially part of libpng, is hereby placed
Packit 0ba690
 * in the public domain, and therefore does not require a copyright notice.
Packit 0ba690
 *
Packit 0ba690
 * This file does not currently compile, because it is missing certain
Packit 0ba690
 * parts, like allocating memory to hold an image.  You will have to
Packit 0ba690
 * supply these parts to get it to compile.  For an example of a minimal
Packit 0ba690
 * working PNG reader/writer, see pngtest.c, included in this distribution;
Packit 0ba690
 * see also the programs in the contrib directory.
Packit 0ba690
 */
Packit 0ba690
Packit 0ba690
#include "png.h"
Packit 0ba690
Packit 0ba690
 /* The png_jmpbuf() macro, used in error handling, became available in
Packit 0ba690
  * libpng version 1.0.6.  If you want to be able to run your code with older
Packit 0ba690
  * versions of libpng, you must define the macro yourself (but only if it
Packit 0ba690
  * is not already defined by libpng!).
Packit 0ba690
  */
Packit 0ba690
Packit 0ba690
#ifndef png_jmpbuf
Packit 0ba690
#  define png_jmpbuf(png_ptr) ((png_ptr)->jmpbuf)
Packit 0ba690
#endif
Packit 0ba690
Packit 0ba690
/* Check to see if a file is a PNG file using png_sig_cmp().  png_sig_cmp()
Packit 0ba690
 * returns zero if the image is a PNG and nonzero if it isn't a PNG.
Packit 0ba690
 *
Packit 0ba690
 * The function check_if_png() shown here, but not used, returns nonzero (true)
Packit 0ba690
 * if the file can be opened and is a PNG, 0 (false) otherwise.
Packit 0ba690
 *
Packit 0ba690
 * If this call is successful, and you are going to keep the file open,
Packit 0ba690
 * you should call png_set_sig_bytes(png_ptr, PNG_BYTES_TO_CHECK); once
Packit 0ba690
 * you have created the png_ptr, so that libpng knows your application
Packit 0ba690
 * has read that many bytes from the start of the file.  Make sure you
Packit 0ba690
 * don't call png_set_sig_bytes() with more than 8 bytes read or give it
Packit 0ba690
 * an incorrect number of bytes read, or you will either have read too
Packit 0ba690
 * many bytes (your fault), or you are telling libpng to read the wrong
Packit 0ba690
 * number of magic bytes (also your fault).
Packit 0ba690
 *
Packit 0ba690
 * Many applications already read the first 2 or 4 bytes from the start
Packit 0ba690
 * of the image to determine the file type, so it would be easiest just
Packit 0ba690
 * to pass the bytes to png_sig_cmp() or even skip that if you know
Packit 0ba690
 * you have a PNG file, and call png_set_sig_bytes().
Packit 0ba690
 */
Packit 0ba690
#define PNG_BYTES_TO_CHECK 4
Packit 0ba690
int check_if_png(char *file_name, FILE **fp)
Packit 0ba690
{
Packit 0ba690
   char buf[PNG_BYTES_TO_CHECK];
Packit 0ba690
Packit 0ba690
   /* Open the prospective PNG file. */
Packit 0ba690
   if ((*fp = fopen(file_name, "rb")) == NULL)
Packit 0ba690
      return 0;
Packit 0ba690
Packit 0ba690
   /* Read in some of the signature bytes */
Packit 0ba690
   if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK)
Packit 0ba690
      return 0;
Packit 0ba690
Packit 0ba690
   /* Compare the first PNG_BYTES_TO_CHECK bytes of the signature.
Packit 0ba690
      Return nonzero (true) if they match */
Packit 0ba690
Packit 0ba690
   return(!png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
/* Read a PNG file.  You may want to return an error code if the read
Packit 0ba690
 * fails (depending upon the failure).  There are two "prototypes" given
Packit 0ba690
 * here - one where we are given the filename, and we need to open the
Packit 0ba690
 * file, and the other where we are given an open file (possibly with
Packit 0ba690
 * some or all of the magic bytes read - see comments above).
Packit 0ba690
 */
Packit 0ba690
#ifdef open_file /* prototype 1 */
Packit 0ba690
void read_png(char *file_name)  /* We need to open the file */
Packit 0ba690
{
Packit 0ba690
   png_structp png_ptr;
Packit 0ba690
   png_infop info_ptr;
Packit 0ba690
   unsigned int sig_read = 0;
Packit 0ba690
   png_uint_32 width, height;
Packit 0ba690
   int bit_depth, color_type, interlace_type;
Packit 0ba690
   FILE *fp;
Packit 0ba690
Packit 0ba690
   if ((fp = fopen(file_name, "rb")) == NULL)
Packit 0ba690
      return (ERROR);
Packit 0ba690
Packit 0ba690
#else no_open_file /* prototype 2 */
Packit 0ba690
void read_png(FILE *fp, unsigned int sig_read)  /* File is already open */
Packit 0ba690
{
Packit 0ba690
   png_structp png_ptr;
Packit 0ba690
   png_infop info_ptr;
Packit 0ba690
   png_uint_32 width, height;
Packit 0ba690
   int bit_depth, color_type, interlace_type;
Packit 0ba690
#endif no_open_file /* Only use one prototype! */
Packit 0ba690
Packit 0ba690
   /* Create and initialize the png_struct with the desired error handler
Packit 0ba690
    * functions.  If you want to use the default stderr and longjump method,
Packit 0ba690
    * you can supply NULL for the last three parameters.  We also supply the
Packit 0ba690
    * the compiler header file version, so that we know if the application
Packit 0ba690
    * was compiled with a compatible version of the library.  REQUIRED
Packit 0ba690
    */
Packit 0ba690
   png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
Packit 0ba690
      png_voidp user_error_ptr, user_error_fn, user_warning_fn);
Packit 0ba690
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
   {
Packit 0ba690
      fclose(fp);
Packit 0ba690
      return (ERROR);
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* Allocate/initialize the memory for image information.  REQUIRED. */
Packit 0ba690
   info_ptr = png_create_info_struct(png_ptr);
Packit 0ba690
   if (info_ptr == NULL)
Packit 0ba690
   {
Packit 0ba690
      fclose(fp);
Packit 0ba690
      png_destroy_read_struct(&png_ptr, png_infopp_NULL, png_infopp_NULL);
Packit 0ba690
      return (ERROR);
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* Set error handling if you are using the setjmp/longjmp method (this is
Packit 0ba690
    * the normal method of doing things with libpng).  REQUIRED unless you
Packit 0ba690
    * set up your own error handlers in the png_create_read_struct() earlier.
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   if (setjmp(png_jmpbuf(png_ptr)))
Packit 0ba690
   {
Packit 0ba690
      /* Free all of the memory associated with the png_ptr and info_ptr */
Packit 0ba690
      png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
Packit 0ba690
      fclose(fp);
Packit 0ba690
      /* If we get here, we had a problem reading the file */
Packit 0ba690
      return (ERROR);
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* One of the following I/O initialization methods is REQUIRED */
Packit 0ba690
#ifdef streams /* PNG file I/O method 1 */
Packit 0ba690
   /* Set up the input control if you are using standard C streams */
Packit 0ba690
   png_init_io(png_ptr, fp);
Packit 0ba690
Packit 0ba690
#else no_streams /* PNG file I/O method 2 */
Packit 0ba690
   /* If you are using replacement read functions, instead of calling
Packit 0ba690
    * png_init_io() here you would call:
Packit 0ba690
    */
Packit 0ba690
   png_set_read_fn(png_ptr, (void *)user_io_ptr, user_read_fn);
Packit 0ba690
   /* where user_io_ptr is a structure you want available to the callbacks */
Packit 0ba690
#endif no_streams /* Use only one I/O method! */
Packit 0ba690
Packit 0ba690
   /* If we have already read some of the signature */
Packit 0ba690
   png_set_sig_bytes(png_ptr, sig_read);
Packit 0ba690
Packit 0ba690
#ifdef hilevel
Packit 0ba690
   /*
Packit 0ba690
    * If you have enough memory to read in the entire image at once,
Packit 0ba690
    * and you need to specify only transforms that can be controlled
Packit 0ba690
    * with one of the PNG_TRANSFORM_* bits (this presently excludes
Packit 0ba690
    * dithering, filling, setting background, and doing gamma
Packit 0ba690
    * adjustment), then you can read the entire image (including
Packit 0ba690
    * pixels) into the info structure with this call:
Packit 0ba690
    */
Packit 0ba690
   png_read_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL);
Packit 0ba690
Packit 0ba690
#else
Packit 0ba690
   /* OK, you're doing it the hard way, with the lower-level functions */
Packit 0ba690
Packit 0ba690
   /* The call to png_read_info() gives us all of the information from the
Packit 0ba690
    * PNG file before the first IDAT (image data chunk).  REQUIRED
Packit 0ba690
    */
Packit 0ba690
   png_read_info(png_ptr, info_ptr);
Packit 0ba690
Packit 0ba690
   png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type,
Packit 0ba690
       &interlace_type, int_p_NULL, int_p_NULL);
Packit 0ba690
Packit 0ba690
   /* Set up the data transformations you want.  Note that these are all
Packit 0ba690
    * optional.  Only call them if you want/need them.  Many of the
Packit 0ba690
    * transformations only work on specific types of images, and many
Packit 0ba690
    * are mutually exclusive.
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   /* Tell libpng to strip 16 bit/color files down to 8 bits/color */
Packit 0ba690
   png_set_strip_16(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Strip alpha bytes from the input data without combining with the
Packit 0ba690
    * background (not recommended).
Packit 0ba690
    */
Packit 0ba690
   png_set_strip_alpha(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Extract multiple pixels with bit depths of 1, 2, and 4 from a single
Packit 0ba690
    * byte into separate bytes (useful for paletted and grayscale images).
Packit 0ba690
    */
Packit 0ba690
   png_set_packing(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Change the order of packed pixels to least significant bit first
Packit 0ba690
    * (not useful if you are using png_set_packing). */
Packit 0ba690
   png_set_packswap(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Expand paletted colors into true RGB triplets */
Packit 0ba690
   if (color_type == PNG_COLOR_TYPE_PALETTE)
Packit 0ba690
      png_set_palette_to_rgb(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Expand grayscale images to the full 8 bits from 1, 2, or 4 bits/pixel */
Packit 0ba690
   if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)
Packit 0ba690
      png_set_expand_gray_1_2_4_to_8(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Expand paletted or RGB images with transparency to full alpha channels
Packit 0ba690
    * so the data will be available as RGBA quartets.
Packit 0ba690
    */
Packit 0ba690
   if (png_get_valid(png_ptr, info_ptr, PNG_INFO_tRNS))
Packit 0ba690
      png_set_tRNS_to_alpha(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Set the background color to draw transparent and alpha images over.
Packit 0ba690
    * It is possible to set the red, green, and blue components directly
Packit 0ba690
    * for paletted images instead of supplying a palette index.  Note that
Packit 0ba690
    * even if the PNG file supplies a background, you are not required to
Packit 0ba690
    * use it - you should use the (solid) application background if it has one.
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   png_color_16 my_background, *image_background;
Packit 0ba690
Packit 0ba690
   if (png_get_bKGD(png_ptr, info_ptr, &image_background))
Packit 0ba690
      png_set_background(png_ptr, image_background,
Packit 0ba690
                         PNG_BACKGROUND_GAMMA_FILE, 1, 1.0);
Packit 0ba690
   else
Packit 0ba690
      png_set_background(png_ptr, &my_background,
Packit 0ba690
                         PNG_BACKGROUND_GAMMA_SCREEN, 0, 1.0);
Packit 0ba690
Packit 0ba690
   /* Some suggestions as to how to get a screen gamma value
Packit 0ba690
    *
Packit 0ba690
    * Note that screen gamma is the display_exponent, which includes
Packit 0ba690
    * the CRT_exponent and any correction for viewing conditions
Packit 0ba690
    */
Packit 0ba690
   if (/* We have a user-defined screen gamma value */)
Packit 0ba690
   {
Packit 0ba690
      screen_gamma = user-defined screen_gamma;
Packit 0ba690
   }
Packit 0ba690
   /* This is one way that applications share the same screen gamma value */
Packit 0ba690
   else if ((gamma_str = getenv("SCREEN_GAMMA")) != NULL)
Packit 0ba690
   {
Packit 0ba690
      screen_gamma = atof(gamma_str);
Packit 0ba690
   }
Packit 0ba690
   /* If we don't have another value */
Packit 0ba690
   else
Packit 0ba690
   {
Packit 0ba690
      screen_gamma = 2.2;  /* A good guess for a PC monitor in a dimly
Packit 0ba690
                              lit room */
Packit 0ba690
      screen_gamma = 1.7 or 1.0;  /* A good guess for Mac systems */
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* Tell libpng to handle the gamma conversion for you.  The final call
Packit 0ba690
    * is a good guess for PC generated images, but it should be configurable
Packit 0ba690
    * by the user at run time by the user.  It is strongly suggested that
Packit 0ba690
    * your application support gamma correction.
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   int intent;
Packit 0ba690
Packit 0ba690
   if (png_get_sRGB(png_ptr, info_ptr, &intent))
Packit 0ba690
      png_set_gamma(png_ptr, screen_gamma, 0.45455);
Packit 0ba690
   else
Packit 0ba690
   {
Packit 0ba690
      double image_gamma;
Packit 0ba690
      if (png_get_gAMA(png_ptr, info_ptr, &image_gamma))
Packit 0ba690
         png_set_gamma(png_ptr, screen_gamma, image_gamma);
Packit 0ba690
      else
Packit 0ba690
         png_set_gamma(png_ptr, screen_gamma, 0.45455);
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* Dither RGB files down to 8 bit palette or reduce palettes
Packit 0ba690
    * to the number of colors available on your screen.
Packit 0ba690
    */
Packit 0ba690
   if (color_type & PNG_COLOR_MASK_COLOR)
Packit 0ba690
   {
Packit 0ba690
      int num_palette;
Packit 0ba690
      png_colorp palette;
Packit 0ba690
Packit 0ba690
      /* This reduces the image to the application supplied palette */
Packit 0ba690
      if (/* We have our own palette */)
Packit 0ba690
      {
Packit 0ba690
         /* An array of colors to which the image should be dithered */
Packit 0ba690
         png_color std_color_cube[MAX_SCREEN_COLORS];
Packit 0ba690
Packit 0ba690
         png_set_dither(png_ptr, std_color_cube, MAX_SCREEN_COLORS,
Packit 0ba690
            MAX_SCREEN_COLORS, png_uint_16p_NULL, 0);
Packit 0ba690
      }
Packit 0ba690
      /* This reduces the image to the palette supplied in the file */
Packit 0ba690
      else if (png_get_PLTE(png_ptr, info_ptr, &palette, &num_palette))
Packit 0ba690
      {
Packit 0ba690
         png_uint_16p histogram = NULL;
Packit 0ba690
Packit 0ba690
         png_get_hIST(png_ptr, info_ptr, &histogram);
Packit 0ba690
Packit 0ba690
         png_set_dither(png_ptr, palette, num_palette,
Packit 0ba690
                        max_screen_colors, histogram, 0);
Packit 0ba690
      }
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* Invert monochrome files to have 0 as white and 1 as black */
Packit 0ba690
   png_set_invert_mono(png_ptr);
Packit 0ba690
Packit 0ba690
   /* If you want to shift the pixel values from the range [0,255] or
Packit 0ba690
    * [0,65535] to the original [0,7] or [0,31], or whatever range the
Packit 0ba690
    * colors were originally in:
Packit 0ba690
    */
Packit 0ba690
   if (png_get_valid(png_ptr, info_ptr, PNG_INFO_sBIT))
Packit 0ba690
   {
Packit 0ba690
      png_color_8p sig_bit_p;
Packit 0ba690
Packit 0ba690
      png_get_sBIT(png_ptr, info_ptr, &sig_bit_p);
Packit 0ba690
      png_set_shift(png_ptr, sig_bit_p);
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* Flip the RGB pixels to BGR (or RGBA to BGRA) */
Packit 0ba690
   if (color_type & PNG_COLOR_MASK_COLOR)
Packit 0ba690
      png_set_bgr(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Swap the RGBA or GA data to ARGB or AG (or BGRA to ABGR) */
Packit 0ba690
   png_set_swap_alpha(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Swap bytes of 16 bit files to least significant byte first */
Packit 0ba690
   png_set_swap(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Add filler (or alpha) byte (before/after each RGB triplet) */
Packit 0ba690
   png_set_filler(png_ptr, 0xff, PNG_FILLER_AFTER);
Packit 0ba690
Packit 0ba690
   /* Turn on interlace handling.  REQUIRED if you are not using
Packit 0ba690
    * png_read_image().  To see how to handle interlacing passes,
Packit 0ba690
    * see the png_read_row() method below:
Packit 0ba690
    */
Packit 0ba690
   number_passes = png_set_interlace_handling(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Optional call to gamma correct and add the background to the palette
Packit 0ba690
    * and update info structure.  REQUIRED if you are expecting libpng to
Packit 0ba690
    * update the palette for you (ie you selected such a transform above).
Packit 0ba690
    */
Packit 0ba690
   png_read_update_info(png_ptr, info_ptr);
Packit 0ba690
Packit 0ba690
   /* Allocate the memory to hold the image using the fields of info_ptr. */
Packit 0ba690
Packit 0ba690
   /* The easiest way to read the image: */
Packit 0ba690
   png_bytep row_pointers[height];
Packit 0ba690
Packit 0ba690
   /* Clear the pointer array */
Packit 0ba690
   for (row = 0; row < height; row++)
Packit 0ba690
      row_pointers[row] = NULL;
Packit 0ba690
Packit 0ba690
   for (row = 0; row < height; row++)
Packit 0ba690
      row_pointers[row] = png_malloc(png_ptr, png_get_rowbytes(png_ptr,
Packit 0ba690
         info_ptr));
Packit 0ba690
Packit 0ba690
   /* Now it's time to read the image.  One of these methods is REQUIRED */
Packit 0ba690
#ifdef entire /* Read the entire image in one go */
Packit 0ba690
   png_read_image(png_ptr, row_pointers);
Packit 0ba690
Packit 0ba690
#else no_entire /* Read the image one or more scanlines at a time */
Packit 0ba690
   /* The other way to read images - deal with interlacing: */
Packit 0ba690
Packit 0ba690
   for (pass = 0; pass < number_passes; pass++)
Packit 0ba690
   {
Packit 0ba690
#ifdef single /* Read the image a single row at a time */
Packit 0ba690
      for (y = 0; y < height; y++)
Packit 0ba690
      {
Packit 0ba690
         png_read_rows(png_ptr, &row_pointers[y], png_bytepp_NULL, 1);
Packit 0ba690
      }
Packit 0ba690
Packit 0ba690
#else no_single /* Read the image several rows at a time */
Packit 0ba690
      for (y = 0; y < height; y += number_of_rows)
Packit 0ba690
      {
Packit 0ba690
#ifdef sparkle /* Read the image using the "sparkle" effect. */
Packit 0ba690
         png_read_rows(png_ptr, &row_pointers[y], png_bytepp_NULL,
Packit 0ba690
            number_of_rows);
Packit 0ba690
#else no_sparkle /* Read the image using the "rectangle" effect */
Packit 0ba690
         png_read_rows(png_ptr, png_bytepp_NULL, &row_pointers[y],
Packit 0ba690
            number_of_rows);
Packit 0ba690
#endif no_sparkle /* Use only one of these two methods */
Packit 0ba690
      }
Packit 0ba690
Packit 0ba690
      /* If you want to display the image after every pass, do so here */
Packit 0ba690
#endif no_single /* Use only one of these two methods */
Packit 0ba690
   }
Packit 0ba690
#endif no_entire /* Use only one of these two methods */
Packit 0ba690
Packit 0ba690
   /* Read rest of file, and get additional chunks in info_ptr - REQUIRED */
Packit 0ba690
   png_read_end(png_ptr, info_ptr);
Packit 0ba690
#endif hilevel
Packit 0ba690
Packit 0ba690
   /* At this point you have read the entire image */
Packit 0ba690
Packit 0ba690
   /* Clean up after the read, and free any memory allocated - REQUIRED */
Packit 0ba690
   png_destroy_read_struct(&png_ptr, &info_ptr, png_infopp_NULL);
Packit 0ba690
Packit 0ba690
   /* Close the file */
Packit 0ba690
   fclose(fp);
Packit 0ba690
Packit 0ba690
   /* That's it */
Packit 0ba690
   return (OK);
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
/* Progressively read a file */
Packit 0ba690
Packit 0ba690
int
Packit 0ba690
initialize_png_reader(png_structp *png_ptr, png_infop *info_ptr)
Packit 0ba690
{
Packit 0ba690
   /* Create and initialize the png_struct with the desired error handler
Packit 0ba690
    * functions.  If you want to use the default stderr and longjump method,
Packit 0ba690
    * you can supply NULL for the last three parameters.  We also check that
Packit 0ba690
    * the library version is compatible in case we are using dynamically
Packit 0ba690
    * linked libraries.
Packit 0ba690
    */
Packit 0ba690
   *png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
Packit 0ba690
       png_voidp user_error_ptr, user_error_fn, user_warning_fn);
Packit 0ba690
Packit 0ba690
   if (*png_ptr == NULL)
Packit 0ba690
   {
Packit 0ba690
      *info_ptr = NULL;
Packit 0ba690
      return (ERROR);
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   *info_ptr = png_create_info_struct(png_ptr);
Packit 0ba690
Packit 0ba690
   if (*info_ptr == NULL)
Packit 0ba690
   {
Packit 0ba690
      png_destroy_read_struct(png_ptr, info_ptr, png_infopp_NULL);
Packit 0ba690
      return (ERROR);
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   if (setjmp(png_jmpbuf((*png_ptr))))
Packit 0ba690
   {
Packit 0ba690
      png_destroy_read_struct(png_ptr, info_ptr, png_infopp_NULL);
Packit 0ba690
      return (ERROR);
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* This one's new.  You will need to provide all three
Packit 0ba690
    * function callbacks, even if you aren't using them all.
Packit 0ba690
    * If you aren't using all functions, you can specify NULL
Packit 0ba690
    * parameters.  Even when all three functions are NULL,
Packit 0ba690
    * you need to call png_set_progressive_read_fn().
Packit 0ba690
    * These functions shouldn't be dependent on global or
Packit 0ba690
    * static variables if you are decoding several images
Packit 0ba690
    * simultaneously.  You should store stream specific data
Packit 0ba690
    * in a separate struct, given as the second parameter,
Packit 0ba690
    * and retrieve the pointer from inside the callbacks using
Packit 0ba690
    * the function png_get_progressive_ptr(png_ptr).
Packit 0ba690
    */
Packit 0ba690
   png_set_progressive_read_fn(*png_ptr, (void *)stream_data,
Packit 0ba690
      info_callback, row_callback, end_callback);
Packit 0ba690
Packit 0ba690
   return (OK);
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
int
Packit 0ba690
process_data(png_structp *png_ptr, png_infop *info_ptr,
Packit 0ba690
   png_bytep buffer, png_uint_32 length)
Packit 0ba690
{
Packit 0ba690
   if (setjmp(png_jmpbuf((*png_ptr))))
Packit 0ba690
   {
Packit 0ba690
      /* Free the png_ptr and info_ptr memory on error */
Packit 0ba690
      png_destroy_read_struct(png_ptr, info_ptr, png_infopp_NULL);
Packit 0ba690
      return (ERROR);
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* This one's new also.  Simply give it chunks of data as
Packit 0ba690
    * they arrive from the data stream (in order, of course).
Packit 0ba690
    * On segmented machines, don't give it any more than 64K.
Packit 0ba690
    * The library seems to run fine with sizes of 4K, although
Packit 0ba690
    * you can give it much less if necessary (I assume you can
Packit 0ba690
    * give it chunks of 1 byte, but I haven't tried with less
Packit 0ba690
    * than 256 bytes yet).  When this function returns, you may
Packit 0ba690
    * want to display any rows that were generated in the row
Packit 0ba690
    * callback, if you aren't already displaying them there.
Packit 0ba690
    */
Packit 0ba690
   png_process_data(*png_ptr, *info_ptr, buffer, length);
Packit 0ba690
   return (OK);
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
info_callback(png_structp png_ptr, png_infop info)
Packit 0ba690
{
Packit 0ba690
   /* Do any setup here, including setting any of the transformations
Packit 0ba690
    * mentioned in the Reading PNG files section.  For now, you _must_
Packit 0ba690
    * call either png_start_read_image() or png_read_update_info()
Packit 0ba690
    * after all the transformations are set (even if you don't set
Packit 0ba690
    * any).  You may start getting rows before png_process_data()
Packit 0ba690
    * returns, so this is your last chance to prepare for that.
Packit 0ba690
    */
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
row_callback(png_structp png_ptr, png_bytep new_row,
Packit 0ba690
   png_uint_32 row_num, int pass)
Packit 0ba690
{
Packit 0ba690
   /*
Packit 0ba690
    * This function is called for every row in the image.  If the
Packit 0ba690
    * image is interlaced, and you turned on the interlace handler,
Packit 0ba690
    * this function will be called for every row in every pass.
Packit 0ba690
    *
Packit 0ba690
    * In this function you will receive a pointer to new row data from
Packit 0ba690
    * libpng called new_row that is to replace a corresponding row (of
Packit 0ba690
    * the same data format) in a buffer allocated by your application.
Packit 0ba690
    *
Packit 0ba690
    * The new row data pointer "new_row" may be NULL, indicating there is
Packit 0ba690
    * no new data to be replaced (in cases of interlace loading).
Packit 0ba690
    *
Packit 0ba690
    * If new_row is not NULL then you need to call
Packit 0ba690
    * png_progressive_combine_row() to replace the corresponding row as
Packit 0ba690
    * shown below:
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   /* Get pointer to corresponding row in our
Packit 0ba690
    * PNG read buffer.
Packit 0ba690
    */
Packit 0ba690
   png_bytep old_row = ((png_bytep *)our_data)[row_num];
Packit 0ba690
Packit 0ba690
   /* If both rows are allocated then copy the new row
Packit 0ba690
    * data to the corresponding row data.
Packit 0ba690
    */
Packit 0ba690
   if ((old_row != NULL) && (new_row != NULL))
Packit 0ba690
   png_progressive_combine_row(png_ptr, old_row, new_row);
Packit 0ba690
Packit 0ba690
   /*
Packit 0ba690
    * The rows and passes are called in order, so you don't really
Packit 0ba690
    * need the row_num and pass, but I'm supplying them because it
Packit 0ba690
    * may make your life easier.
Packit 0ba690
    *
Packit 0ba690
    * For the non-NULL rows of interlaced images, you must call
Packit 0ba690
    * png_progressive_combine_row() passing in the new row and the
Packit 0ba690
    * old row, as demonstrated above.  You can call this function for
Packit 0ba690
    * NULL rows (it will just return) and for non-interlaced images
Packit 0ba690
    * (it just does the png_memcpy for you) if it will make the code
Packit 0ba690
    * easier.  Thus, you can just do this for all cases:
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   png_progressive_combine_row(png_ptr, old_row, new_row);
Packit 0ba690
Packit 0ba690
   /* where old_row is what was displayed for previous rows.  Note
Packit 0ba690
    * that the first pass (pass == 0 really) will completely cover
Packit 0ba690
    * the old row, so the rows do not have to be initialized.  After
Packit 0ba690
    * the first pass (and only for interlaced images), you will have
Packit 0ba690
    * to pass the current row as new_row, and the function will combine
Packit 0ba690
    * the old row and the new row.
Packit 0ba690
    */
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
end_callback(png_structp png_ptr, png_infop info)
Packit 0ba690
{
Packit 0ba690
   /* This function is called when the whole image has been read,
Packit 0ba690
    * including any chunks after the image (up to and including
Packit 0ba690
    * the IEND).  You will usually have the same info chunk as you
Packit 0ba690
    * had in the header, although some data may have been added
Packit 0ba690
    * to the comments and time fields.
Packit 0ba690
    *
Packit 0ba690
    * Most people won't do much here, perhaps setting a flag that
Packit 0ba690
    * marks the image as finished.
Packit 0ba690
    */
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
/* Write a png file */
Packit 0ba690
void write_png(char *file_name /* , ... other image information ... */)
Packit 0ba690
{
Packit 0ba690
   FILE *fp;
Packit 0ba690
   png_structp png_ptr;
Packit 0ba690
   png_infop info_ptr;
Packit 0ba690
   png_colorp palette;
Packit 0ba690
Packit 0ba690
   /* Open the file */
Packit 0ba690
   fp = fopen(file_name, "wb");
Packit 0ba690
   if (fp == NULL)
Packit 0ba690
      return (ERROR);
Packit 0ba690
Packit 0ba690
   /* Create and initialize the png_struct with the desired error handler
Packit 0ba690
    * functions.  If you want to use the default stderr and longjump method,
Packit 0ba690
    * you can supply NULL for the last three parameters.  We also check that
Packit 0ba690
    * the library version is compatible with the one used at compile time,
Packit 0ba690
    * in case we are using dynamically linked libraries.  REQUIRED.
Packit 0ba690
    */
Packit 0ba690
   png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING,
Packit 0ba690
      png_voidp user_error_ptr, user_error_fn, user_warning_fn);
Packit 0ba690
Packit 0ba690
   if (png_ptr == NULL)
Packit 0ba690
   {
Packit 0ba690
      fclose(fp);
Packit 0ba690
      return (ERROR);
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* Allocate/initialize the image information data.  REQUIRED */
Packit 0ba690
   info_ptr = png_create_info_struct(png_ptr);
Packit 0ba690
   if (info_ptr == NULL)
Packit 0ba690
   {
Packit 0ba690
      fclose(fp);
Packit 0ba690
      png_destroy_write_struct(&png_ptr,  png_infopp_NULL);
Packit 0ba690
      return (ERROR);
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* Set error handling.  REQUIRED if you aren't supplying your own
Packit 0ba690
    * error handling functions in the png_create_write_struct() call.
Packit 0ba690
    */
Packit 0ba690
   if (setjmp(png_jmpbuf(png_ptr)))
Packit 0ba690
   {
Packit 0ba690
      /* If we get here, we had a problem writing the file */
Packit 0ba690
      fclose(fp);
Packit 0ba690
      png_destroy_write_struct(&png_ptr, &info_ptr);
Packit 0ba690
      return (ERROR);
Packit 0ba690
   }
Packit 0ba690
Packit 0ba690
   /* One of the following I/O initialization functions is REQUIRED */
Packit 0ba690
Packit 0ba690
#ifdef streams /* I/O initialization method 1 */
Packit 0ba690
   /* Set up the output control if you are using standard C streams */
Packit 0ba690
   png_init_io(png_ptr, fp);
Packit 0ba690
Packit 0ba690
#else no_streams /* I/O initialization method 2 */
Packit 0ba690
   /* If you are using replacement write functions, instead of calling
Packit 0ba690
    * png_init_io() here you would call
Packit 0ba690
    */
Packit 0ba690
   png_set_write_fn(png_ptr, (void *)user_io_ptr, user_write_fn,
Packit 0ba690
      user_IO_flush_function);
Packit 0ba690
   /* where user_io_ptr is a structure you want available to the callbacks */
Packit 0ba690
#endif no_streams /* Only use one initialization method */
Packit 0ba690
Packit 0ba690
#ifdef hilevel
Packit 0ba690
   /* This is the easy way.  Use it if you already have all the
Packit 0ba690
    * image info living in the structure.  You could "|" many
Packit 0ba690
    * PNG_TRANSFORM flags into the png_transforms integer here.
Packit 0ba690
    */
Packit 0ba690
   png_write_png(png_ptr, info_ptr, png_transforms, png_voidp_NULL);
Packit 0ba690
Packit 0ba690
#else
Packit 0ba690
   /* This is the hard way */
Packit 0ba690
Packit 0ba690
   /* Set the image information here.  Width and height are up to 2^31,
Packit 0ba690
    * bit_depth is one of 1, 2, 4, 8, or 16, but valid values also depend on
Packit 0ba690
    * the color_type selected. color_type is one of PNG_COLOR_TYPE_GRAY,
Packit 0ba690
    * PNG_COLOR_TYPE_GRAY_ALPHA, PNG_COLOR_TYPE_PALETTE, PNG_COLOR_TYPE_RGB,
Packit 0ba690
    * or PNG_COLOR_TYPE_RGB_ALPHA.  interlace is either PNG_INTERLACE_NONE or
Packit 0ba690
    * PNG_INTERLACE_ADAM7, and the compression_type and filter_type MUST
Packit 0ba690
    * currently be PNG_COMPRESSION_TYPE_BASE and PNG_FILTER_TYPE_BASE. REQUIRED
Packit 0ba690
    */
Packit 0ba690
   png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, PNG_COLOR_TYPE_???,
Packit 0ba690
      PNG_INTERLACE_????, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
Packit 0ba690
Packit 0ba690
   /* Set the palette if there is one.  REQUIRED for indexed-color images */
Packit 0ba690
   palette = (png_colorp)png_malloc(png_ptr, PNG_MAX_PALETTE_LENGTH
Packit 0ba690
             * png_sizeof(png_color));
Packit 0ba690
   /* ... Set palette colors ... */
Packit 0ba690
   png_set_PLTE(png_ptr, info_ptr, palette, PNG_MAX_PALETTE_LENGTH);
Packit 0ba690
   /* You must not free palette here, because png_set_PLTE only makes a link to
Packit 0ba690
    * the palette that you malloced.  Wait until you are about to destroy
Packit 0ba690
    * the png structure.
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   /* Optional significant bit (sBIT) chunk */
Packit 0ba690
   png_color_8 sig_bit;
Packit 0ba690
   /* If we are dealing with a grayscale image then */
Packit 0ba690
   sig_bit.gray = true_bit_depth;
Packit 0ba690
   /* Otherwise, if we are dealing with a color image then */
Packit 0ba690
   sig_bit.red = true_red_bit_depth;
Packit 0ba690
   sig_bit.green = true_green_bit_depth;
Packit 0ba690
   sig_bit.blue = true_blue_bit_depth;
Packit 0ba690
   /* If the image has an alpha channel then */
Packit 0ba690
   sig_bit.alpha = true_alpha_bit_depth;
Packit 0ba690
   png_set_sBIT(png_ptr, info_ptr, &sig_bit);
Packit 0ba690
Packit 0ba690
Packit 0ba690
   /* Optional gamma chunk is strongly suggested if you have any guess
Packit 0ba690
    * as to the correct gamma of the image.
Packit 0ba690
    */
Packit 0ba690
   png_set_gAMA(png_ptr, info_ptr, gamma);
Packit 0ba690
Packit 0ba690
   /* Optionally write comments into the image */
Packit 0ba690
   text_ptr[0].key = "Title";
Packit 0ba690
   text_ptr[0].text = "Mona Lisa";
Packit 0ba690
   text_ptr[0].compression = PNG_TEXT_COMPRESSION_NONE;
Packit 0ba690
   text_ptr[1].key = "Author";
Packit 0ba690
   text_ptr[1].text = "Leonardo DaVinci";
Packit 0ba690
   text_ptr[1].compression = PNG_TEXT_COMPRESSION_NONE;
Packit 0ba690
   text_ptr[2].key = "Description";
Packit 0ba690
   text_ptr[2].text = "<long text>";
Packit 0ba690
   text_ptr[2].compression = PNG_TEXT_COMPRESSION_zTXt;
Packit 0ba690
#ifdef PNG_iTXt_SUPPORTED
Packit 0ba690
   text_ptr[0].lang = NULL;
Packit 0ba690
   text_ptr[1].lang = NULL;
Packit 0ba690
   text_ptr[2].lang = NULL;
Packit 0ba690
#endif
Packit 0ba690
   png_set_text(png_ptr, info_ptr, text_ptr, 3);
Packit 0ba690
Packit 0ba690
   /* Other optional chunks like cHRM, bKGD, tRNS, tIME, oFFs, pHYs */
Packit 0ba690
Packit 0ba690
   /* Note that if sRGB is present the gAMA and cHRM chunks must be ignored
Packit 0ba690
    * on read and, if your application chooses to write them, they must
Packit 0ba690
    * be written in accordance with the sRGB profile
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   /* Write the file header information.  REQUIRED */
Packit 0ba690
   png_write_info(png_ptr, info_ptr);
Packit 0ba690
Packit 0ba690
   /* If you want, you can write the info in two steps, in case you need to
Packit 0ba690
    * write your private chunk ahead of PLTE:
Packit 0ba690
    *
Packit 0ba690
    *   png_write_info_before_PLTE(write_ptr, write_info_ptr);
Packit 0ba690
    *   write_my_chunk();
Packit 0ba690
    *   png_write_info(png_ptr, info_ptr);
Packit 0ba690
    *
Packit 0ba690
    * However, given the level of known- and unknown-chunk support in 1.2.0
Packit 0ba690
    * and up, this should no longer be necessary.
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   /* Once we write out the header, the compression type on the text
Packit 0ba690
    * chunks gets changed to PNG_TEXT_COMPRESSION_NONE_WR or
Packit 0ba690
    * PNG_TEXT_COMPRESSION_zTXt_WR, so it doesn't get written out again
Packit 0ba690
    * at the end.
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   /* Set up the transformations you want.  Note that these are
Packit 0ba690
    * all optional.  Only call them if you want them.
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   /* Invert monochrome pixels */
Packit 0ba690
   png_set_invert_mono(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Shift the pixels up to a legal bit depth and fill in
Packit 0ba690
    * as appropriate to correctly scale the image.
Packit 0ba690
    */
Packit 0ba690
   png_set_shift(png_ptr, &sig_bit);
Packit 0ba690
Packit 0ba690
   /* Pack pixels into bytes */
Packit 0ba690
   png_set_packing(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Swap location of alpha bytes from ARGB to RGBA */
Packit 0ba690
   png_set_swap_alpha(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Get rid of filler (OR ALPHA) bytes, pack XRGB/RGBX/ARGB/RGBA into
Packit 0ba690
    * RGB (4 channels -> 3 channels). The second parameter is not used.
Packit 0ba690
    */
Packit 0ba690
   png_set_filler(png_ptr, 0, PNG_FILLER_BEFORE);
Packit 0ba690
Packit 0ba690
   /* Flip BGR pixels to RGB */
Packit 0ba690
   png_set_bgr(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Swap bytes of 16-bit files to most significant byte first */
Packit 0ba690
   png_set_swap(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Swap bits of 1, 2, 4 bit packed pixel formats */
Packit 0ba690
   png_set_packswap(png_ptr);
Packit 0ba690
Packit 0ba690
   /* Turn on interlace handling if you are not using png_write_image() */
Packit 0ba690
   if (interlacing)
Packit 0ba690
      number_passes = png_set_interlace_handling(png_ptr);
Packit 0ba690
   else
Packit 0ba690
      number_passes = 1;
Packit 0ba690
Packit 0ba690
   /* The easiest way to write the image (you may have a different memory
Packit 0ba690
    * layout, however, so choose what fits your needs best).  You need to
Packit 0ba690
    * use the first method if you aren't handling interlacing yourself.
Packit 0ba690
    */
Packit 0ba690
   png_uint_32 k, height, width;
Packit 0ba690
   png_byte image[height][width*bytes_per_pixel];
Packit 0ba690
   png_bytep row_pointers[height];
Packit 0ba690
Packit 0ba690
   if (height > PNG_UINT_32_MAX/png_sizeof(png_bytep))
Packit 0ba690
     png_error (png_ptr, "Image is too tall to process in memory");
Packit 0ba690
Packit 0ba690
   for (k = 0; k < height; k++)
Packit 0ba690
     row_pointers[k] = image + k*width*bytes_per_pixel;
Packit 0ba690
Packit 0ba690
   /* One of the following output methods is REQUIRED */
Packit 0ba690
Packit 0ba690
#ifdef entire /* Write out the entire image data in one call */
Packit 0ba690
   png_write_image(png_ptr, row_pointers);
Packit 0ba690
Packit 0ba690
   /* The other way to write the image - deal with interlacing */
Packit 0ba690
Packit 0ba690
#else no_entire /* Write out the image data by one or more scanlines */
Packit 0ba690
Packit 0ba690
   /* The number of passes is either 1 for non-interlaced images,
Packit 0ba690
    * or 7 for interlaced images.
Packit 0ba690
    */
Packit 0ba690
   for (pass = 0; pass < number_passes; pass++)
Packit 0ba690
   {
Packit 0ba690
      /* Write a few rows at a time. */
Packit 0ba690
      png_write_rows(png_ptr, &row_pointers[first_row], number_of_rows);
Packit 0ba690
Packit 0ba690
      /* If you are only writing one row at a time, this works */
Packit 0ba690
      for (y = 0; y < height; y++)
Packit 0ba690
         png_write_rows(png_ptr, &row_pointers[y], 1);
Packit 0ba690
   }
Packit 0ba690
#endif no_entire /* Use only one output method */
Packit 0ba690
Packit 0ba690
   /* You can write optional chunks like tEXt, zTXt, and tIME at the end
Packit 0ba690
    * as well.  Shouldn't be necessary in 1.2.0 and up as all the public
Packit 0ba690
    * chunks are supported and you can use png_set_unknown_chunks() to
Packit 0ba690
    * register unknown chunks into the info structure to be written out.
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   /* It is REQUIRED to call this to finish writing the rest of the file */
Packit 0ba690
   png_write_end(png_ptr, info_ptr);
Packit 0ba690
#endif hilevel
Packit 0ba690
Packit 0ba690
   /* If you png_malloced a palette, free it here (don't free info_ptr->palette,
Packit 0ba690
    * as recommended in versions 1.0.5m and earlier of this example; if
Packit 0ba690
    * libpng mallocs info_ptr->palette, libpng will free it).  If you
Packit 0ba690
    * allocated it with malloc() instead of png_malloc(), use free() instead
Packit 0ba690
    * of png_free().
Packit 0ba690
    */
Packit 0ba690
   png_free(png_ptr, palette);
Packit 0ba690
   palette = NULL;
Packit 0ba690
Packit 0ba690
   /* Similarly, if you png_malloced any data that you passed in with
Packit 0ba690
    * png_set_something(), such as a hist or trans array, free it here,
Packit 0ba690
    * when you can be sure that libpng is through with it.
Packit 0ba690
    */
Packit 0ba690
   png_free(png_ptr, trans);
Packit 0ba690
   trans = NULL;
Packit 0ba690
   /* Whenever you use png_free() it is a good idea to set the pointer to
Packit 0ba690
    * NULL in case your application inadvertently tries to png_free() it
Packit 0ba690
    * again.  When png_free() sees a NULL it returns without action, thus
Packit 0ba690
    * avoiding the double-free security problem.
Packit 0ba690
    */
Packit 0ba690
Packit 0ba690
   /* Clean up after the write, and free any memory allocated */
Packit 0ba690
   png_destroy_write_struct(&png_ptr, &info_ptr);
Packit 0ba690
Packit 0ba690
   /* Close the file */
Packit 0ba690
   fclose(fp);
Packit 0ba690
Packit 0ba690
   /* That's it */
Packit 0ba690
   return (OK);
Packit 0ba690
}
Packit 0ba690
Packit 0ba690
#endif /* if 0 */