/* timepng.c * * Copyright (c) 2011 John Cunningham Bowler * * Last changed in libpng 1.5.7 [(PENDING RELEASE)] * * This code is released under the libpng license. * For conditions of distribution and use, see the disclaimer * and license in png.h * * Load an arbitrary number of PNG files (from the command line, or, if there * are no arguments on the command line, from stdin) then run a time test by * reading each file by row. The test does nothing with the read result and * does no transforms. The only output is a time as a floating point number of * seconds with 9 decimal digits. */ #include #include #include #include "png.h" static int read_png(FILE *fp) { png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0); png_infop info_ptr = NULL; png_bytep row = NULL, display = NULL; if (png_ptr == NULL) return 0; if (setjmp(png_jmpbuf(png_ptr))) { png_destroy_read_struct(&png_ptr, &info_ptr, NULL); if (row != NULL) free(row); if (display != NULL) free(display); return 0; } png_init_io(png_ptr, fp); info_ptr = png_create_info_struct(png_ptr); if (info_ptr == NULL) png_error(png_ptr, "OOM allocating info structure"); png_read_info(png_ptr, info_ptr); { png_size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr); row = malloc(rowbytes); display = malloc(rowbytes); if (row == NULL || display == NULL) png_error(png_ptr, "OOM allocating row buffers"); { png_uint_32 height = png_get_image_height(png_ptr, info_ptr); int passes = png_set_interlace_handling(png_ptr); int pass; png_start_read_image(png_ptr); for (pass = 0; pass < passes; ++pass) { png_uint_32 y = height; /* NOTE: this trashes the row each time; interlace handling won't * work, but this avoids memory thrashing for speed testing. */ while (y-- > 0) png_read_row(png_ptr, row, display); } } } /* Make sure to read to the end of the file: */ png_read_end(png_ptr, info_ptr); png_destroy_read_struct(&png_ptr, &info_ptr, NULL); free(row); free(display); return 1; } static int mytime(struct timespec *t) { /* Do the timing using clock_gettime and the per-process timer. */ if (!clock_gettime(CLOCK_PROCESS_CPUTIME_ID, t)) return 1; perror("CLOCK_PROCESS_CPUTIME_ID"); fprintf(stderr, "timepng: could not get the time\n"); return 0; } static int perform_one_test(FILE *fp, int nfiles) { int i; struct timespec before, after; /* Clear out all errors: */ rewind(fp); if (mytime(&before)) { for (i=0; i 1) { int i; for (i=1; i 0) ok = perform_one_test(fp, nfiles); else fprintf(stderr, "usage: timepng {files} or ls files | timepng\n"); } (void)fclose(fp); } else fprintf(stderr, "timepng: could not open temporary file\n"); /* Exit code 0 on success. */ return ok == 0; }