4 * Copyright (c) 2013,2016 John Cunningham Bowler
6 * This code is released under the libpng license.
7 * For conditions of distribution and use, see the disclaimer
10 * Load an arbitrary number of PNG files (from the command line, or, if there
11 * are no arguments on the command line, from stdin) then run a time test by
12 * reading each file by row or by image (possibly with transforms in the latter
13 * case). The only output is a time as a floating point number of seconds with
16 #define _POSIX_C_SOURCE 199309L /* for clock_gettime */
26 #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
30 /* Define the following to use this test against your installed libpng, rather
31 * than the one being built here:
33 #ifdef PNG_FREESTANDING_TESTS
36 # include "../../png.h"
39 /* The following is to support direct compilation of this file as C++ */
41 # define voidcast(type, value) static_cast<type>(value)
43 # define voidcast(type, value) (value)
44 #endif /* __cplusplus */
46 /* 'CLOCK_PROCESS_CPUTIME_ID' is one of the clock timers for clock_gettime. It
47 * need not be supported even when clock_gettime is available. It returns the
48 * 'CPU' time the process has consumed. 'CPU' time is assumed to include time
49 * when the CPU is actually blocked by a pending cache fill but not time
50 * waiting for page faults. The attempt is to get a measure of the actual time
51 * the implementation takes to read a PNG ignoring the potentially very large IO
54 #if defined (CLOCK_PROCESS_CPUTIME_ID) && defined(PNG_STDIO_SUPPORTED) &&\
55 defined(PNG_EASY_ACCESS_SUPPORTED) &&\
56 (PNG_LIBPNG_VER >= 10700 ? defined(PNG_READ_PNG_SUPPORTED) :\
57 defined (PNG_SEQUENTIAL_READ_SUPPORTED) &&\
58 defined(PNG_INFO_IMAGE_SUPPORTED))
66 static PNG_CALLBACK(void, read_and_copy,
67 (png_structp png_ptr, png_bytep buffer, size_t cb))
69 io_data *io = (io_data*)png_get_io_ptr(png_ptr);
71 if (fread(buffer, cb, 1, io->input) != 1)
72 png_error(png_ptr, strerror(errno));
74 if (fwrite(buffer, cb, 1, io->output) != 1)
76 perror("temporary file");
77 fprintf(stderr, "temporary file PNG write failed\n");
82 static void read_by_row(png_structp png_ptr, png_infop info_ptr,
83 FILE *write_ptr, FILE *read_ptr)
85 /* These don't get freed on error, this is fine; the program immediately
88 png_bytep row = NULL, display = NULL;
91 if (write_ptr != NULL)
93 /* Set up for a copy to the temporary file: */
94 io_copy.input = read_ptr;
95 io_copy.output = write_ptr;
96 png_set_read_fn(png_ptr, &io_copy, read_and_copy);
99 png_read_info(png_ptr, info_ptr);
102 size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
104 row = voidcast(png_bytep,malloc(rowbytes));
105 display = voidcast(png_bytep,malloc(rowbytes));
107 if (row == NULL || display == NULL)
108 png_error(png_ptr, "OOM allocating row buffers");
111 png_uint_32 height = png_get_image_height(png_ptr, info_ptr);
112 int passes = png_set_interlace_handling(png_ptr);
115 png_start_read_image(png_ptr);
117 for (pass = 0; pass < passes; ++pass)
119 png_uint_32 y = height;
121 /* NOTE: this trashes the row each time; interlace handling won't
122 * work, but this avoids memory thrashing for speed testing and is
123 * somewhat representative of an application that works row-by-row.
126 png_read_row(png_ptr, row, display);
131 /* Make sure to read to the end of the file: */
132 png_read_end(png_ptr, info_ptr);
139 static PNG_CALLBACK(void, no_warnings, (png_structp png_ptr,
140 png_const_charp warning))
146 static int read_png(FILE *fp, png_int_32 transforms, FILE *write_file)
148 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,
150 png_infop info_ptr = NULL;
155 if (setjmp(png_jmpbuf(png_ptr)))
157 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
161 # ifdef PNG_BENIGN_ERRORS_SUPPORTED
162 png_set_benign_errors(png_ptr, 1/*allowed*/);
164 png_init_io(png_ptr, fp);
166 info_ptr = png_create_info_struct(png_ptr);
168 if (info_ptr == NULL)
169 png_error(png_ptr, "OOM allocating info structure");
172 read_by_row(png_ptr, info_ptr, write_file, fp);
175 png_read_png(png_ptr, info_ptr, transforms, NULL/*params*/);
177 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
181 static int mytime(struct timespec *t)
183 /* Do the timing using clock_gettime and the per-process timer. */
184 if (!clock_gettime(CLOCK_PROCESS_CPUTIME_ID, t))
187 perror("CLOCK_PROCESS_CPUTIME_ID");
188 fprintf(stderr, "timepng: could not get the time\n");
192 static int perform_one_test(FILE *fp, int nfiles, png_int_32 transforms)
195 struct timespec before, after;
197 /* Clear out all errors: */
202 for (i=0; i<nfiles; ++i)
204 if (read_png(fp, transforms, NULL/*write*/))
208 perror("temporary file");
209 fprintf(stderr, "file %d: error reading PNG data\n", i);
216 perror("temporary file");
217 fprintf(stderr, "file %d: error from libpng\n", i);
228 /* Work out the time difference and print it - this is the only output,
229 * so flush it immediately.
231 unsigned long s = after.tv_sec - before.tv_sec;
232 long ns = after.tv_nsec - before.tv_nsec;
241 fprintf(stderr, "timepng: bad clock from kernel\n");
246 printf("%lu.%.9ld\n", s, ns);
250 fprintf(stderr, "timepng: error writing output\n");
254 /* Successful return */
262 static int add_one_file(FILE *fp, char *name)
264 FILE *ip = fopen(name, "rb");
268 /* Read the file using libpng; this detects errors and also deals with
269 * files which contain data beyond the end of the file.
274 if (fgetpos(fp, &pos))
276 /* Fatal error reading the start: */
277 perror("temporary file");
278 fprintf(stderr, "temporary file fgetpos error\n");
282 if (read_png(ip, -1/*by row*/, fp/*output*/))
287 fprintf(stderr, "%s: read error\n", name);
291 ok = 1; /* read ok */
295 fprintf(stderr, "%s: file not added\n", name);
299 /* An error in the output is fatal; exit immediately: */
302 perror("temporary file");
303 fprintf(stderr, "temporary file write error\n");
310 /* Did not read the file successfully, simply rewind the temporary
311 * file. This must happen after the ferror check above to avoid clearing
314 if (fsetpos(fp, &pos))
316 perror("temporary file");
317 fprintf(stderr, "temporary file fsetpos error\n");
324 /* file open error: */
326 fprintf(stderr, "%s: open failed\n", name);
329 return 0; /* file not added */
335 if (fp != NULL) fclose(fp);
339 " timepng --assemble <assembly> {files}\n"
340 " Read the files into <assembly>, output the count. Options are ignored.\n"
341 " timepng --dissemble <assembly> <count> [options]\n"
342 " Time <count> files from <assembly>, additional files may not be given.\n"
344 " Read the files into a temporary file and time the decode\n"
346 " --by-image: read by image with png_read_png\n"
347 " --<transform>: implies by-image, use PNG_TRANSFORM_<transform>\n"
348 " Otherwise: read by row using png_read_row (to a single row buffer)\n"
349 /* ISO C90 string length max 509 */);fprintf(stderr,
351 " PNG files to copy into the assembly and time. Invalid files are skipped\n"
352 " with appropriate error messages. If no files are given the list of files\n"
353 " is read from stdin with each file name terminated by a newline\n"
355 " For --assemble the output is the name of the assembly file followed by the\n"
356 " count of the files it contains; the arguments for --dissemble. Otherwise\n"
357 " the output is the total decode time in seconds.\n");
362 int main(int argc, char **argv)
367 int transforms = -1; /* by row */
368 const char *assembly = NULL;
371 if (argc > 2 && strcmp(argv[1], "--assemble") == 0)
373 /* Just build the test file, argv[2] is the file name. */
375 fp = fopen(assembly, "wb");
379 fprintf(stderr, "timepng --assemble %s: could not open for write\n",
388 else if (argc > 3 && strcmp(argv[1], "--dissemble") == 0)
390 fp = fopen(argv[2], "rb");
395 fprintf(stderr, "timepng --dissemble %s: could not open for read\n",
400 nfiles = atoi(argv[3]);
404 "timepng --dissemble <file> <count>: %s is not a count\n",
411 nfiles &= PNG_UINT_31_MAX;
419 else /* Else use a temporary file */
424 /* Experimental. Coverity says tmpfile() is insecure because it
425 * generates predictable names.
427 * It is possible to satisfy Coverity by using mkstemp(); however,
428 * any platform supporting mkstemp() undoubtedly has a secure tmpfile()
429 * implementation as well, and doesn't need the fix. Note that
430 * the fix won't work on platforms that don't support mkstemp().
432 * https://www.securecoding.cert.org/confluence/display/c/
433 * FIO21-C.+Do+not+create+temporary+files+in+shared+directories
434 * says that most historic implementations of tmpfile() provide
435 * only a limited number of possible temporary file names
436 * (usually 26) before file names are recycled. That article also
437 * provides a secure solution that unfortunately depends upon mkstemp().
439 char tmpfile[] = "timepng-XXXXXX";
442 filedes = mkstemp(tmpfile);
447 fp = fdopen(filedes,"w+");
448 /* Hide the filename immediately and ensure that the file does
449 * not exist after the program ends
451 (void) unlink(tmpfile);
458 fprintf(stderr, "timepng: could not open the temporary file\n");
459 exit(1); /* not a user error */
463 /* Handle the transforms: */
464 while (argc > 1 && argv[1][0] == '-' && argv[1][1] == '-')
466 const char *opt = *++argv + 2;
470 /* Transforms turn on the by-image processing and maybe set some
473 if (transforms == -1)
474 transforms = PNG_TRANSFORM_IDENTITY;
476 if (strcmp(opt, "by-image") == 0)
481 # define OPT(name) else if (strcmp(opt, #name) == 0)\
482 transforms |= PNG_TRANSFORM_ ## name
496 OPT(STRIP_FILLER_BEFORE);
497 OPT(STRIP_FILLER_AFTER);
504 fprintf(stderr, "timepng %s: unrecognized transform\n", opt);
509 /* Handle the files: */
510 if (argc > 1 && nfiles > 0)
511 usage(fp); /* Additional files not valid with --dissemble */
517 for (i=1; i<argc; ++i)
519 if (nfiles == INT_MAX)
521 fprintf(stderr, "%s: skipped, too many files\n", argv[i]);
525 else if (add_one_file(fp, argv[i]))
530 else if (nfiles == 0) /* Read from stdin without --dissemble */
532 char filename[FILENAME_MAX+1];
534 while (fgets(filename, FILENAME_MAX+1, stdin))
536 size_t len = strlen(filename);
538 if (filename[len-1] == '\n')
541 if (nfiles == INT_MAX)
543 fprintf(stderr, "%s: skipped, too many files\n", filename);
547 else if (add_one_file(fp, filename))
553 fprintf(stderr, "timepng: file name too long: ...%s\n",
562 fprintf(stderr, "timepng: stdin: read error\n");
567 /* Perform the test, or produce the --assemble output: */
572 if (assembly != NULL)
574 if (fflush(fp) && !ferror(fp) && fclose(fp))
577 fprintf(stderr, "%s: close failed\n", assembly);
582 printf("%s %d\n", assembly, nfiles);
584 ok = !ferror(stdout);
590 ok = perform_one_test(fp, nfiles, transforms);
602 /* Exit code 0 on success. */
605 #else /* !sufficient support */
606 int main(void) { return 77; }
607 #endif /* !sufficient support */