fukasawa e60969
/* readpng.c
fukasawa e60969
 *
fukasawa e60969
 * Copyright (c) 2013 John Cunningham Bowler
fukasawa e60969
 *
fukasawa e60969
 * Last changed in libpng 1.6.1 [March 28, 2013]
fukasawa e60969
 *
fukasawa e60969
 * This code is released under the libpng license.
fukasawa e60969
 * For conditions of distribution and use, see the disclaimer
fukasawa e60969
 * and license in png.h
fukasawa e60969
 *
fukasawa e60969
 * Load an arbitrary number of PNG files (from the command line, or, if there
fukasawa e60969
 * are no arguments on the command line, from stdin) then run a time test by
fukasawa e60969
 * reading each file by row.  The test does nothing with the read result and
fukasawa e60969
 * does no transforms.  The only output is a time as a floating point number of
fukasawa e60969
 * seconds with 9 decimal digits.
fukasawa e60969
 */
fukasawa e60969
#include <stdlib.h></stdlib.h>
fukasawa e60969
#include <stdio.h></stdio.h>
fukasawa e60969
#include <string.h></string.h>
fukasawa e60969
fukasawa e60969
#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
fukasawa e60969
#  include <config.h></config.h>
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
/* Define the following to use this test against your installed libpng, rather
fukasawa e60969
 * than the one being built here:
fukasawa e60969
 */
fukasawa e60969
#ifdef PNG_FREESTANDING_TESTS
fukasawa e60969
#  include <png.h></png.h>
fukasawa e60969
#else
fukasawa e60969
#  include "../../png.h"
fukasawa e60969
#endif
fukasawa e60969
fukasawa e60969
static int
fukasawa e60969
read_png(FILE *fp)
fukasawa e60969
{
fukasawa e60969
   png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0);
fukasawa e60969
   png_infop info_ptr = NULL;
fukasawa e60969
   png_bytep row = NULL, display = NULL;
fukasawa e60969
fukasawa e60969
   if (png_ptr == NULL)
fukasawa e60969
      return 0;
fukasawa e60969
fukasawa e60969
   if (setjmp(png_jmpbuf(png_ptr)))
fukasawa e60969
   {
fukasawa e60969
      png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fukasawa e60969
      if (row != NULL) free(row);
fukasawa e60969
      if (display != NULL) free(display);
fukasawa e60969
      return 0;
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   png_init_io(png_ptr, fp);
fukasawa e60969
fukasawa e60969
   info_ptr = png_create_info_struct(png_ptr);
fukasawa e60969
   if (info_ptr == NULL)
fukasawa e60969
      png_error(png_ptr, "OOM allocating info structure");
fukasawa e60969
fukasawa e60969
   png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, NULL, 0);
fukasawa e60969
fukasawa e60969
   png_read_info(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
   {
fukasawa e60969
      png_size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
fukasawa e60969
fukasawa e60969
      /* Failure to initialize these is harmless */
fukasawa e60969
      row = malloc(rowbytes);
fukasawa e60969
      display = malloc(rowbytes);
fukasawa e60969
fukasawa e60969
      if (row == NULL || display == NULL)
fukasawa e60969
         png_error(png_ptr, "OOM allocating row buffers");
fukasawa e60969
fukasawa e60969
      {
fukasawa e60969
         png_uint_32 height = png_get_image_height(png_ptr, info_ptr);
fukasawa e60969
#        ifdef PNG_READ_INTERLACING_SUPPORTED
fukasawa e60969
            int passes = png_set_interlace_handling(png_ptr);
fukasawa e60969
#        else /* !READ_INTERLACING */
fukasawa e60969
            int passes = png_get_interlace_type(png_ptr, info_ptr) ==
fukasawa e60969
               PNG_INTERLACE_ADAM7 ? PNG_INTERLACE_ADAM7_PASSES : 1;
fukasawa e60969
#        endif /* !READ_INTERLACING */
fukasawa e60969
         int pass;
fukasawa e60969
fukasawa e60969
         png_start_read_image(png_ptr);
fukasawa e60969
fukasawa e60969
         for (pass = 0; pass < passes; ++pass)
fukasawa e60969
         {
fukasawa e60969
            png_uint_32 y = height;
fukasawa e60969
fukasawa e60969
#           ifndef PNG_READ_INTERLACING_SUPPORTED
fukasawa e60969
               if (passes == PNG_INTERLACE_ADAM7_PASSES)
fukasawa e60969
                  y = PNG_PASS_ROWS(y, pass);
fukasawa e60969
#           endif /* READ_INTERLACING */
fukasawa e60969
fukasawa e60969
            /* NOTE: this trashes the row each time; interlace handling won't
fukasawa e60969
             * work, but this avoids memory thrashing for speed testing.
fukasawa e60969
             */
fukasawa e60969
            while (y-- > 0)
fukasawa e60969
               png_read_row(png_ptr, row, display);
fukasawa e60969
         }
fukasawa e60969
      }
fukasawa e60969
   }
fukasawa e60969
fukasawa e60969
   /* Make sure to read to the end of the file: */
fukasawa e60969
   png_read_end(png_ptr, info_ptr);
fukasawa e60969
   png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fukasawa e60969
   free(row);
fukasawa e60969
   free(display);
fukasawa e60969
   return 1;
fukasawa e60969
}
fukasawa e60969
fukasawa e60969
int
fukasawa e60969
main(void)
fukasawa e60969
{
fukasawa e60969
   /* Exit code 0 on success. */
fukasawa e60969
   return !read_png(stdin);
fukasawa e60969
}