Home | History | Annotate | Download | only in libtests
      1 /* readpng.c
      2  *
      3  * Copyright (c) 2013 John Cunningham Bowler
      4  *
      5  * Last changed in libpng 1.6.1 [March 28, 2013]
      6  *
      7  * This code is released under the libpng license.
      8  * For conditions of distribution and use, see the disclaimer
      9  * and license in png.h
     10  *
     11  * Load an arbitrary number of PNG files (from the command line, or, if there
     12  * are no arguments on the command line, from stdin) then run a time test by
     13  * reading each file by row.  The test does nothing with the read result and
     14  * does no transforms.  The only output is a time as a floating point number of
     15  * seconds with 9 decimal digits.
     16  */
     17 #include <stdlib.h>
     18 #include <stdio.h>
     19 #include <string.h>
     20 
     21 #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
     22 #  include <config.h>
     23 #endif
     24 
     25 /* Define the following to use this test against your installed libpng, rather
     26  * than the one being built here:
     27  */
     28 #ifdef PNG_FREESTANDING_TESTS
     29 #  include <png.h>
     30 #else
     31 #  include "../../png.h"
     32 #endif
     33 
     34 static int
     35 read_png(FILE *fp)
     36 {
     37    png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0);
     38    png_infop info_ptr = NULL;
     39    png_bytep row = NULL, display = NULL;
     40 
     41    if (png_ptr == NULL)
     42       return 0;
     43 
     44    if (setjmp(png_jmpbuf(png_ptr)))
     45    {
     46       png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
     47       if (row != NULL) free(row);
     48       if (display != NULL) free(display);
     49       return 0;
     50    }
     51 
     52    png_init_io(png_ptr, fp);
     53 
     54    info_ptr = png_create_info_struct(png_ptr);
     55    if (info_ptr == NULL)
     56       png_error(png_ptr, "OOM allocating info structure");
     57 
     58    png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, NULL, 0);
     59 
     60    png_read_info(png_ptr, info_ptr);
     61 
     62    {
     63       png_size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
     64 
     65       /* Failure to initialize these is harmless */
     66       row = malloc(rowbytes);
     67       display = malloc(rowbytes);
     68 
     69       if (row == NULL || display == NULL)
     70          png_error(png_ptr, "OOM allocating row buffers");
     71 
     72       {
     73          png_uint_32 height = png_get_image_height(png_ptr, info_ptr);
     74 #        ifdef PNG_READ_INTERLACING_SUPPORTED
     75             int passes = png_set_interlace_handling(png_ptr);
     76 #        else /* !READ_INTERLACING */
     77             int passes = png_get_interlace_type(png_ptr, info_ptr) ==
     78                PNG_INTERLACE_ADAM7 ? PNG_INTERLACE_ADAM7_PASSES : 1;
     79 #        endif /* !READ_INTERLACING */
     80          int pass;
     81 
     82          png_start_read_image(png_ptr);
     83 
     84          for (pass = 0; pass < passes; ++pass)
     85          {
     86             png_uint_32 y = height;
     87 
     88 #           ifndef PNG_READ_INTERLACING_SUPPORTED
     89                if (passes == PNG_INTERLACE_ADAM7_PASSES)
     90                   y = PNG_PASS_ROWS(y, pass);
     91 #           endif /* READ_INTERLACING */
     92 
     93             /* NOTE: this trashes the row each time; interlace handling won't
     94              * work, but this avoids memory thrashing for speed testing.
     95              */
     96             while (y-- > 0)
     97                png_read_row(png_ptr, row, display);
     98          }
     99       }
    100    }
    101 
    102    /* Make sure to read to the end of the file: */
    103    png_read_end(png_ptr, info_ptr);
    104    png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
    105    free(row);
    106    free(display);
    107    return 1;
    108 }
    109 
    110 int
    111 main(void)
    112 {
    113    /* Exit code 0 on success. */
    114    return !read_png(stdin);
    115 }
    116