3 * Copyright (c) 2013,2016 John Cunningham Bowler
5 * Last changed in libpng 1.6.22 [May 26, 2016]
7 * This code is released under the libpng license.
8 * For conditions of distribution and use, see the disclaimer
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 or by image (possibly with transforms in the latter
14 * case). The only output is a time as a floating point number of seconds with
17 #define _POSIX_C_SOURCE 199309L /* for clock_gettime */
27 #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
31 /* Define the following to use this test against your installed libpng, rather
32 * than the one being built here:
34 #ifdef PNG_FREESTANDING_TESTS
37 # include "../../png.h"
40 /* The following is to support direct compilation of this file as C++ */
42 # define voidcast(type, value) static_cast<type>(value)
44 # define voidcast(type, value) (value)
45 #endif /* __cplusplus */
47 /* 'CLOCK_PROCESS_CPUTIME_ID' is one of the clock timers for clock_gettime. It
48 * need not be supported even when clock_gettime is available. It returns the
49 * 'CPU' time the process has consumed. 'CPU' time is assumed to include time
50 * when the CPU is actually blocked by a pending cache fill but not time
51 * waiting for page faults. The attempt is to get a measure of the actual time
52 * the implementation takes to read a PNG ignoring the potentially very large IO
55 #if defined (CLOCK_PROCESS_CPUTIME_ID) && defined(PNG_STDIO_SUPPORTED) &&\
56 defined(PNG_EASY_ACCESS_SUPPORTED) &&\
57 (PNG_LIBPNG_VER >= 10700 ? defined(PNG_READ_PNG_SUPPORTED) :\
58 defined (PNG_SEQUENTIAL_READ_SUPPORTED) &&\
59 defined(PNG_INFO_IMAGE_SUPPORTED))
67 static PNG_CALLBACK(void, read_and_copy
,
68 (png_structp png_ptr
, png_bytep buffer
, size_t cb
))
70 io_data
*io
= (io_data
*)png_get_io_ptr(png_ptr
);
72 if (fread(buffer
, cb
, 1, io
->input
) != 1)
73 png_error(png_ptr
, strerror(errno
));
75 if (fwrite(buffer
, cb
, 1, io
->output
) != 1)
77 perror("temporary file");
78 fprintf(stderr
, "temporary file PNG write failed\n");
83 static void read_by_row(png_structp png_ptr
, png_infop info_ptr
,
84 FILE *write_ptr
, FILE *read_ptr
)
86 /* These don't get freed on error, this is fine; the program immediately
89 png_bytep row
= NULL
, display
= NULL
;
92 if (write_ptr
!= NULL
)
94 /* Set up for a copy to the temporary file: */
95 io_copy
.input
= read_ptr
;
96 io_copy
.output
= write_ptr
;
97 png_set_read_fn(png_ptr
, &io_copy
, read_and_copy
);
100 png_read_info(png_ptr
, info_ptr
);
103 size_t rowbytes
= png_get_rowbytes(png_ptr
, info_ptr
);
105 row
= voidcast(png_bytep
,malloc(rowbytes
));
106 display
= voidcast(png_bytep
,malloc(rowbytes
));
108 if (row
== NULL
|| display
== NULL
)
109 png_error(png_ptr
, "OOM allocating row buffers");
112 png_uint_32 height
= png_get_image_height(png_ptr
, info_ptr
);
113 int passes
= png_set_interlace_handling(png_ptr
);
116 png_start_read_image(png_ptr
);
118 for (pass
= 0; pass
< passes
; ++pass
)
120 png_uint_32 y
= height
;
122 /* NOTE: this trashes the row each time; interlace handling won't
123 * work, but this avoids memory thrashing for speed testing and is
124 * somewhat representative of an application that works row-by-row.
127 png_read_row(png_ptr
, row
, display
);
132 /* Make sure to read to the end of the file: */
133 png_read_end(png_ptr
, info_ptr
);
140 static PNG_CALLBACK(void, no_warnings
, (png_structp png_ptr
,
141 png_const_charp warning
))
147 static int read_png(FILE *fp
, png_int_32 transforms
, FILE *write_file
)
149 png_structp png_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
,0,0,
151 png_infop info_ptr
= NULL
;
156 if (setjmp(png_jmpbuf(png_ptr
)))
158 png_destroy_read_struct(&png_ptr
, &info_ptr
, NULL
);
162 # ifdef PNG_BENIGN_ERRORS_SUPPORTED
163 png_set_benign_errors(png_ptr
, 1/*allowed*/);
165 png_init_io(png_ptr
, fp
);
167 info_ptr
= png_create_info_struct(png_ptr
);
169 if (info_ptr
== NULL
)
170 png_error(png_ptr
, "OOM allocating info structure");
173 read_by_row(png_ptr
, info_ptr
, write_file
, fp
);
176 png_read_png(png_ptr
, info_ptr
, transforms
, NULL
/*params*/);
178 png_destroy_read_struct(&png_ptr
, &info_ptr
, NULL
);
182 static int mytime(struct timespec
*t
)
184 /* Do the timing using clock_gettime and the per-process timer. */
185 if (!clock_gettime(CLOCK_PROCESS_CPUTIME_ID
, t
))
188 perror("CLOCK_PROCESS_CPUTIME_ID");
189 fprintf(stderr
, "timepng: could not get the time\n");
193 static int perform_one_test(FILE *fp
, int nfiles
, png_int_32 transforms
)
196 struct timespec before
, after
;
198 /* Clear out all errors: */
203 for (i
=0; i
<nfiles
; ++i
)
205 if (read_png(fp
, transforms
, NULL
/*write*/))
209 perror("temporary file");
210 fprintf(stderr
, "file %d: error reading PNG data\n", i
);
217 perror("temporary file");
218 fprintf(stderr
, "file %d: error from libpng\n", i
);
229 /* Work out the time difference and print it - this is the only output,
230 * so flush it immediately.
232 unsigned long s
= after
.tv_sec
- before
.tv_sec
;
233 long ns
= after
.tv_nsec
- before
.tv_nsec
;
242 fprintf(stderr
, "timepng: bad clock from kernel\n");
247 printf("%lu.%.9ld\n", s
, ns
);
251 fprintf(stderr
, "timepng: error writing output\n");
255 /* Successful return */
263 static int add_one_file(FILE *fp
, char *name
)
265 FILE *ip
= fopen(name
, "rb");
269 /* Read the file using libpng; this detects errors and also deals with
270 * files which contain data beyond the end of the file.
275 if (fgetpos(fp
, &pos
))
277 /* Fatal error reading the start: */
278 perror("temporary file");
279 fprintf(stderr
, "temporary file fgetpos error\n");
283 if (read_png(ip
, -1/*by row*/, fp
/*output*/))
288 fprintf(stderr
, "%s: read error\n", name
);
292 ok
= 1; /* read ok */
296 fprintf(stderr
, "%s: file not added\n", name
);
300 /* An error in the output is fatal; exit immediately: */
303 perror("temporary file");
304 fprintf(stderr
, "temporary file write error\n");
311 /* Did not read the file successfully, simply rewind the temporary
312 * file. This must happen after the ferror check above to avoid clearing
315 if (fsetpos(fp
, &pos
))
317 perror("temporary file");
318 fprintf(stderr
, "temporary file fsetpos error\n");
325 /* file open error: */
327 fprintf(stderr
, "%s: open failed\n", name
);
330 return 0; /* file not added */
336 if (fp
!= NULL
) fclose(fp
);
340 " timepng --assemble <assembly> {files}\n"
341 " Read the files into <assembly>, output the count. Options are ignored.\n"
342 " timepng --dissemble <assembly> <count> [options]\n"
343 " Time <count> files from <assembly>, additional files may not be given.\n"
345 " Read the files into a temporary file and time the decode\n"
347 " --by-image: read by image with png_read_png\n"
348 " --<transform>: implies by-image, use PNG_TRANSFORM_<transform>\n"
349 " Otherwise: read by row using png_read_row (to a single row buffer)\n"
350 /* ISO C90 string length max 509 */);fprintf(stderr
,
352 " PNG files to copy into the assembly and time. Invalid files are skipped\n"
353 " with appropriate error messages. If no files are given the list of files\n"
354 " is read from stdin with each file name terminated by a newline\n"
356 " For --assemble the output is the name of the assembly file followed by the\n"
357 " count of the files it contains; the arguments for --dissemble. Otherwise\n"
358 " the output is the total decode time in seconds.\n");
363 int main(int argc
, char **argv
)
368 int transforms
= -1; /* by row */
369 const char *assembly
= NULL
;
372 if (argc
> 2 && strcmp(argv
[1], "--assemble") == 0)
374 /* Just build the test file, argv[2] is the file name. */
376 fp
= fopen(assembly
, "wb");
380 fprintf(stderr
, "timepng --assemble %s: could not open for write\n",
389 else if (argc
> 3 && strcmp(argv
[1], "--dissemble") == 0)
391 fp
= fopen(argv
[2], "rb");
396 fprintf(stderr
, "timepng --dissemble %s: could not open for read\n",
401 nfiles
= atoi(argv
[3]);
405 "timepng --dissemble <file> <count>: %s is not a count\n",
412 nfiles
&= PNG_UINT_31_MAX
;
420 else /* Else use a temporary file */
425 /* Experimental. Coverity says tmpfile() is insecure because it
426 * generates predictable names.
428 * It is possible to satisfy Coverity by using mkstemp(); however,
429 * any platform supporting mkstemp() undoubtedly has a secure tmpfile()
430 * implementation as well, and doesn't need the fix. Note that
431 * the fix won't work on platforms that don't support mkstemp().
433 * https://www.securecoding.cert.org/confluence/display/c/
434 * FIO21-C.+Do+not+create+temporary+files+in+shared+directories
435 * says that most historic implementations of tmpfile() provide
436 * only a limited number of possible temporary file names
437 * (usually 26) before file names are recycled. That article also
438 * provides a secure solution that unfortunately depends upon mkstemp().
440 char tmpfile
[] = "timepng-XXXXXX";
443 filedes
= mkstemp(tmpfile
);
448 fp
= fdopen(filedes
,"w+");
449 /* Hide the filename immediately and ensure that the file does
450 * not exist after the program ends
452 (void) unlink(tmpfile
);
459 fprintf(stderr
, "timepng: could not open the temporary file\n");
460 exit(1); /* not a user error */
464 /* Handle the transforms: */
465 while (argc
> 1 && argv
[1][0] == '-' && argv
[1][1] == '-')
467 const char *opt
= *++argv
+ 2;
471 /* Transforms turn on the by-image processing and maybe set some
474 if (transforms
== -1)
475 transforms
= PNG_TRANSFORM_IDENTITY
;
477 if (strcmp(opt
, "by-image") == 0)
482 # define OPT(name) else if (strcmp(opt, #name) == 0)\
483 transforms |= PNG_TRANSFORM_ ## name
497 OPT(STRIP_FILLER_BEFORE
);
498 OPT(STRIP_FILLER_AFTER
);
505 fprintf(stderr
, "timepng %s: unrecognized transform\n", opt
);
510 /* Handle the files: */
511 if (argc
> 1 && nfiles
> 0)
512 usage(fp
); /* Additional files not valid with --dissemble */
518 for (i
=1; i
<argc
; ++i
)
520 if (nfiles
== INT_MAX
)
522 fprintf(stderr
, "%s: skipped, too many files\n", argv
[i
]);
526 else if (add_one_file(fp
, argv
[i
]))
531 else if (nfiles
== 0) /* Read from stdin withoout --dissemble */
533 char filename
[FILENAME_MAX
+1];
535 while (fgets(filename
, FILENAME_MAX
+1, stdin
))
537 size_t len
= strlen(filename
);
539 if (filename
[len
-1] == '\n')
542 if (nfiles
== INT_MAX
)
544 fprintf(stderr
, "%s: skipped, too many files\n", filename
);
548 else if (add_one_file(fp
, filename
))
554 fprintf(stderr
, "timepng: file name too long: ...%s\n",
563 fprintf(stderr
, "timepng: stdin: read error\n");
568 /* Perform the test, or produce the --assemble output: */
573 if (assembly
!= NULL
)
575 if (fflush(fp
) && !ferror(fp
) && fclose(fp
))
578 fprintf(stderr
, "%s: close failed\n", assembly
);
583 printf("%s %d\n", assembly
, nfiles
);
585 ok
= !ferror(stdout
);
591 ok
= perform_one_test(fp
, nfiles
, transforms
);
603 /* Exit code 0 on success. */
606 #else /* !sufficient support */
607 int main(void) { return 77; }
608 #endif /* !sufficient support */