fukasawa e60969
/*- pngtopng
fukasawa e60969
 *
fukasawa e60969
 * COPYRIGHT: Written by John Cunningham Bowler, 2011.
fukasawa e60969
 * To the extent possible under law, the author has waived all copyright and
fukasawa e60969
 * related or neighboring rights to this work.  This work is published from:
fukasawa e60969
 * United States.
fukasawa e60969
 *
fukasawa e60969
 * Read a PNG and write it out in a fixed format, using the 'simplified API'
fukasawa e60969
 * that was introduced in libpng-1.6.0.
fukasawa e60969
 *
fukasawa e60969
 * This sample code is just the code from the top of 'example.c' with some error
fukasawa e60969
 * handling added.  See example.c for more comments.
fukasawa e60969
 */
fukasawa e60969
#include <stddef.h></stddef.h>
fukasawa e60969
#include <stdlib.h></stdlib.h>
fukasawa e60969
#include <string.h></string.h>
fukasawa e60969
#include <stdio.h></stdio.h>
fukasawa e60969
fukasawa e60969
/* Normally use <png.h> here to get the installed libpng, but this is done to</png.h>
fukasawa e60969
 * ensure the code picks up the local libpng implementation:
fukasawa e60969
 */
fukasawa e60969
#include "../../png.h"
fukasawa e60969
#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && \
fukasawa e60969
    defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
fukasawa e60969
fukasawa e60969
int main(int argc, const char **argv)
fukasawa e60969
{
fukasawa e60969
   int result = 1;
fukasawa e60969
fukasawa e60969
   if (argc == 3)
fukasawa e60969
   {
fukasawa e60969
      png_image image;
fukasawa e60969
fukasawa e60969
      /* Only the image structure version number needs to be set. */
fukasawa e60969
      memset(&image, 0, sizeof image);
fukasawa e60969
      image.version = PNG_IMAGE_VERSION;
fukasawa e60969
fukasawa e60969
      if (png_image_begin_read_from_file(&image, argv[1]))
fukasawa e60969
      {
fukasawa e60969
         png_bytep buffer;
fukasawa e60969
fukasawa e60969
         /* Change this to try different formats!  If you set a colormap format
fukasawa e60969
          * then you must also supply a colormap below.
fukasawa e60969
          */
fukasawa e60969
         image.format = PNG_FORMAT_RGBA;
fukasawa e60969
fukasawa e60969
         buffer = malloc(PNG_IMAGE_SIZE(image));
fukasawa e60969
fukasawa e60969
         if (buffer != NULL)
fukasawa e60969
         {
fukasawa e60969
            if (png_image_finish_read(&image, NULL/*background*/, buffer,
fukasawa e60969
               0/*row_stride*/, NULL/*colormap for PNG_FORMAT_FLAG_COLORMAP */))
fukasawa e60969
            {
fukasawa e60969
               if (png_image_write_to_file(&image, argv[2],
fukasawa e60969
                  0/*convert_to_8bit*/, buffer, 0/*row_stride*/,
fukasawa e60969
                  NULL/*colormap*/))
fukasawa e60969
                  result = 0;
fukasawa e60969
fukasawa e60969
               else
fukasawa e60969
                  fprintf(stderr, "pngtopng: write %s: %s\n", argv[2],
fukasawa e60969
                      image.message);
fukasawa e60969
fukasawa e60969
               free(buffer);
fukasawa e60969
            }
fukasawa e60969
fukasawa e60969
            else
fukasawa e60969
            {
fukasawa e60969
               fprintf(stderr, "pngtopng: read %s: %s\n", argv[1],
fukasawa e60969
                   image.message);
fukasawa e60969
fukasawa e60969
               /* This is the only place where a 'free' is required; libpng does
fukasawa e60969
                * the cleanup on error and success, but in this case we couldn't
fukasawa e60969
                * complete the read because of running out of memory.
fukasawa e60969
                */
fukasawa e60969
               png_image_free(&image);
fukasawa e60969
            }
fukasawa e60969
         }
fukasawa e60969
fukasawa e60969
         else
fukasawa e60969
            fprintf(stderr, "pngtopng: out of memory: %lu bytes\n",
fukasawa e60969
               (unsigned long)PNG_IMAGE_SIZE(image));
fukasawa e60969
      }
fukasawa e60969
fukasawa e60969
      else
fukasawa e60969
         /* Failed to read the first argument: */
fukasawa e60969
         fprintf(stderr, "pngtopng: %s: %s\n", argv[1], image.message);
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   else
fukasawa e60969
      /* Wrong number of arguments */
fukasawa e60969
      fprintf(stderr, "pngtopng: usage: pngtopng input-file output-file\n");
fukasawa e60969
fukasawa e60969
   return result;
fukasawa e60969
}
fukasawa e60969
#endif /* READ && WRITE */