timepng.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. /* timepng.c
  2. *
  3. * Copyright (c) 2013,2016 John Cunningham Bowler
  4. *
  5. * Last changed in libpng 1.6.22 [May 26, 2016]
  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 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
  15. * 9 decimal digits.
  16. */
  17. #define _POSIX_C_SOURCE 199309L /* for clock_gettime */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <limits.h>
  23. #include <time.h>
  24. #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
  25. # include <config.h>
  26. #endif
  27. /* Define the following to use this test against your installed libpng, rather
  28. * than the one being built here:
  29. */
  30. #ifdef PNG_FREESTANDING_TESTS
  31. # include <png.h>
  32. #else
  33. # include "../../png.h"
  34. #endif
  35. /* The following is to support direct compilation of this file as C++ */
  36. #ifdef __cplusplus
  37. # define voidcast(type, value) static_cast<type>(value)
  38. #else
  39. # define voidcast(type, value) (value)
  40. #endif /* __cplusplus */
  41. /* 'CLOCK_PROCESS_CPUTIME_ID' is one of the clock timers for clock_gettime. It
  42. * need not be supported even when clock_gettime is available. It returns the
  43. * 'CPU' time the process has consumed. 'CPU' time is assumed to include time
  44. * when the CPU is actually blocked by a pending cache fill but not time
  45. * waiting for page faults. The attempt is to get a measure of the actual time
  46. * the implementation takes to read a PNG ignoring the potentially very large IO
  47. * overhead.
  48. */
  49. #if defined (CLOCK_PROCESS_CPUTIME_ID) && defined(PNG_STDIO_SUPPORTED) &&\
  50. defined(PNG_EASY_ACCESS_SUPPORTED) &&\
  51. (PNG_LIBPNG_VER >= 10700 ? defined(PNG_READ_PNG_SUPPORTED) :\
  52. defined (PNG_SEQUENTIAL_READ_SUPPORTED) &&\
  53. defined(PNG_INFO_IMAGE_SUPPORTED))
  54. typedef struct
  55. {
  56. FILE *input;
  57. FILE *output;
  58. } io_data;
  59. static PNG_CALLBACK(void, read_and_copy,
  60. (png_structp png_ptr, png_bytep buffer, size_t cb))
  61. {
  62. io_data *io = (io_data*)png_get_io_ptr(png_ptr);
  63. if (fread(buffer, cb, 1, io->input) != 1)
  64. png_error(png_ptr, strerror(errno));
  65. if (fwrite(buffer, cb, 1, io->output) != 1)
  66. {
  67. perror("temporary file");
  68. fprintf(stderr, "temporary file PNG write failed\n");
  69. exit(1);
  70. }
  71. }
  72. static void read_by_row(png_structp png_ptr, png_infop info_ptr,
  73. FILE *write_ptr, FILE *read_ptr)
  74. {
  75. /* These don't get freed on error, this is fine; the program immediately
  76. * exits.
  77. */
  78. png_bytep row = NULL, display = NULL;
  79. io_data io_copy;
  80. if (write_ptr != NULL)
  81. {
  82. /* Set up for a copy to the temporary file: */
  83. io_copy.input = read_ptr;
  84. io_copy.output = write_ptr;
  85. png_set_read_fn(png_ptr, &io_copy, read_and_copy);
  86. }
  87. png_read_info(png_ptr, info_ptr);
  88. {
  89. size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
  90. row = voidcast(png_bytep,malloc(rowbytes));
  91. display = voidcast(png_bytep,malloc(rowbytes));
  92. if (row == NULL || display == NULL)
  93. png_error(png_ptr, "OOM allocating row buffers");
  94. {
  95. png_uint_32 height = png_get_image_height(png_ptr, info_ptr);
  96. int passes = png_set_interlace_handling(png_ptr);
  97. int pass;
  98. png_start_read_image(png_ptr);
  99. for (pass = 0; pass < passes; ++pass)
  100. {
  101. png_uint_32 y = height;
  102. /* NOTE: this trashes the row each time; interlace handling won't
  103. * work, but this avoids memory thrashing for speed testing and is
  104. * somewhat representative of an application that works row-by-row.
  105. */
  106. while (y-- > 0)
  107. png_read_row(png_ptr, row, display);
  108. }
  109. }
  110. }
  111. /* Make sure to read to the end of the file: */
  112. png_read_end(png_ptr, info_ptr);
  113. /* Free this up: */
  114. free(row);
  115. free(display);
  116. }
  117. static PNG_CALLBACK(void, no_warnings, (png_structp png_ptr,
  118. png_const_charp warning))
  119. {
  120. (void)png_ptr;
  121. (void)warning;
  122. }
  123. static int read_png(FILE *fp, png_int_32 transforms, FILE *write_file)
  124. {
  125. png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,
  126. no_warnings);
  127. png_infop info_ptr = NULL;
  128. if (png_ptr == NULL)
  129. return 0;
  130. if (setjmp(png_jmpbuf(png_ptr)))
  131. {
  132. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  133. return 0;
  134. }
  135. # ifdef PNG_BENIGN_ERRORS_SUPPORTED
  136. png_set_benign_errors(png_ptr, 1/*allowed*/);
  137. # endif
  138. png_init_io(png_ptr, fp);
  139. info_ptr = png_create_info_struct(png_ptr);
  140. if (info_ptr == NULL)
  141. png_error(png_ptr, "OOM allocating info structure");
  142. if (transforms < 0)
  143. read_by_row(png_ptr, info_ptr, write_file, fp);
  144. else
  145. png_read_png(png_ptr, info_ptr, transforms, NULL/*params*/);
  146. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  147. return 1;
  148. }
  149. static int mytime(struct timespec *t)
  150. {
  151. /* Do the timing using clock_gettime and the per-process timer. */
  152. if (!clock_gettime(CLOCK_PROCESS_CPUTIME_ID, t))
  153. return 1;
  154. perror("CLOCK_PROCESS_CPUTIME_ID");
  155. fprintf(stderr, "timepng: could not get the time\n");
  156. return 0;
  157. }
  158. static int perform_one_test(FILE *fp, int nfiles, png_int_32 transforms)
  159. {
  160. int i;
  161. struct timespec before, after;
  162. /* Clear out all errors: */
  163. rewind(fp);
  164. if (mytime(&before))
  165. {
  166. for (i=0; i<nfiles; ++i)
  167. {
  168. if (read_png(fp, transforms, NULL/*write*/))
  169. {
  170. if (ferror(fp))
  171. {
  172. perror("temporary file");
  173. fprintf(stderr, "file %d: error reading PNG data\n", i);
  174. return 0;
  175. }
  176. }
  177. else
  178. {
  179. perror("temporary file");
  180. fprintf(stderr, "file %d: error from libpng\n", i);
  181. return 0;
  182. }
  183. }
  184. }
  185. else
  186. return 0;
  187. if (mytime(&after))
  188. {
  189. /* Work out the time difference and print it - this is the only output,
  190. * so flush it immediately.
  191. */
  192. unsigned long s = after.tv_sec - before.tv_sec;
  193. long ns = after.tv_nsec - before.tv_nsec;
  194. if (ns < 0)
  195. {
  196. --s;
  197. ns += 1000000000;
  198. if (ns < 0)
  199. {
  200. fprintf(stderr, "timepng: bad clock from kernel\n");
  201. return 0;
  202. }
  203. }
  204. printf("%lu.%.9ld\n", s, ns);
  205. fflush(stdout);
  206. if (ferror(stdout))
  207. {
  208. fprintf(stderr, "timepng: error writing output\n");
  209. return 0;
  210. }
  211. /* Successful return */
  212. return 1;
  213. }
  214. else
  215. return 0;
  216. }
  217. static int add_one_file(FILE *fp, char *name)
  218. {
  219. FILE *ip = fopen(name, "rb");
  220. if (ip != NULL)
  221. {
  222. /* Read the file using libpng; this detects errors and also deals with
  223. * files which contain data beyond the end of the file.
  224. */
  225. int ok = 0;
  226. fpos_t pos;
  227. if (fgetpos(fp, &pos))
  228. {
  229. /* Fatal error reading the start: */
  230. perror("temporary file");
  231. fprintf(stderr, "temporary file fgetpos error\n");
  232. exit(1);
  233. }
  234. if (read_png(ip, -1/*by row*/, fp/*output*/))
  235. {
  236. if (ferror(ip))
  237. {
  238. perror(name);
  239. fprintf(stderr, "%s: read error\n", name);
  240. }
  241. else
  242. ok = 1; /* read ok */
  243. }
  244. else
  245. fprintf(stderr, "%s: file not added\n", name);
  246. (void)fclose(ip);
  247. /* An error in the output is fatal; exit immediately: */
  248. if (ferror(fp))
  249. {
  250. perror("temporary file");
  251. fprintf(stderr, "temporary file write error\n");
  252. exit(1);
  253. }
  254. if (ok)
  255. return 1;
  256. /* Did not read the file successfully, simply rewind the temporary
  257. * file. This must happen after the ferror check above to avoid clearing
  258. * the error.
  259. */
  260. if (fsetpos(fp, &pos))
  261. {
  262. perror("temporary file");
  263. fprintf(stderr, "temporary file fsetpos error\n");
  264. exit(1);
  265. }
  266. }
  267. else
  268. {
  269. /* file open error: */
  270. perror(name);
  271. fprintf(stderr, "%s: open failed\n", name);
  272. }
  273. return 0; /* file not added */
  274. }
  275. static void
  276. usage(FILE *fp)
  277. {
  278. if (fp != NULL) fclose(fp);
  279. fprintf(stderr,
  280. "Usage:\n"
  281. " timepng --assemble <assembly> {files}\n"
  282. " Read the files into <assembly>, output the count. Options are ignored.\n"
  283. " timepng --dissemble <assembly> <count> [options]\n"
  284. " Time <count> files from <assembly>, additional files may not be given.\n"
  285. " Otherwise:\n"
  286. " Read the files into a temporary file and time the decode\n"
  287. "Transforms:\n"
  288. " --by-image: read by image with png_read_png\n"
  289. " --<transform>: implies by-image, use PNG_TRANSFORM_<transform>\n"
  290. " Otherwise: read by row using png_read_row (to a single row buffer)\n"
  291. /* ISO C90 string length max 509 */);fprintf(stderr,
  292. "{files}:\n"
  293. " PNG files to copy into the assembly and time. Invalid files are skipped\n"
  294. " with appropriate error messages. If no files are given the list of files\n"
  295. " is read from stdin with each file name terminated by a newline\n"
  296. "Output:\n"
  297. " For --assemble the output is the name of the assembly file followed by the\n"
  298. " count of the files it contains; the arguments for --dissemble. Otherwise\n"
  299. " the output is the total decode time in seconds.\n");
  300. exit(99);
  301. }
  302. int main(int argc, char **argv)
  303. {
  304. int ok = 0;
  305. int err = 0;
  306. int nfiles = 0;
  307. int transforms = -1; /* by row */
  308. const char *assembly = NULL;
  309. FILE *fp;
  310. if (argc > 2 && strcmp(argv[1], "--assemble") == 0)
  311. {
  312. /* Just build the test file, argv[2] is the file name. */
  313. assembly = argv[2];
  314. fp = fopen(assembly, "wb");
  315. if (fp == NULL)
  316. {
  317. perror(assembly);
  318. fprintf(stderr, "timepng --assemble %s: could not open for write\n",
  319. assembly);
  320. usage(NULL);
  321. }
  322. argv += 2;
  323. argc -= 2;
  324. }
  325. else if (argc > 3 && strcmp(argv[1], "--dissemble") == 0)
  326. {
  327. fp = fopen(argv[2], "rb");
  328. if (fp == NULL)
  329. {
  330. perror(argv[2]);
  331. fprintf(stderr, "timepng --dissemble %s: could not open for read\n",
  332. argv[2]);
  333. usage(NULL);
  334. }
  335. nfiles = atoi(argv[3]);
  336. if (nfiles <= 0)
  337. {
  338. fprintf(stderr,
  339. "timepng --dissemble <file> <count>: %s is not a count\n",
  340. argv[3]);
  341. exit(99);
  342. }
  343. #ifdef __COVERITY__
  344. else
  345. {
  346. nfiles &= PNG_UINT_31_MAX;
  347. }
  348. #endif
  349. argv += 3;
  350. argc -= 3;
  351. }
  352. else /* Else use a temporary file */
  353. {
  354. #ifndef __COVERITY__
  355. fp = tmpfile();
  356. #else
  357. /* Experimental. Coverity says tmpfile() is insecure because it
  358. * generates predictable names.
  359. *
  360. * It is possible to satisfy Coverity by using mkstemp(); however,
  361. * any platform supporting mkstemp() undoubtedly has a secure tmpfile()
  362. * implementation as well, and doesn't need the fix. Note that
  363. * the fix won't work on platforms that don't support mkstemp().
  364. *
  365. * https://www.securecoding.cert.org/confluence/display/c/
  366. * FIO21-C.+Do+not+create+temporary+files+in+shared+directories
  367. * says that most historic implementations of tmpfile() provide
  368. * only a limited number of possible temporary file names
  369. * (usually 26) before file names are recycled. That article also
  370. * provides a secure solution that unfortunately depends upon mkstemp().
  371. */
  372. char tmpfile[] = "timepng-XXXXXX";
  373. int filedes;
  374. umask(0177);
  375. filedes = mkstemp(tmpfile);
  376. if (filedes < 0)
  377. fp = NULL;
  378. else
  379. {
  380. fp = fdopen(filedes,"w+");
  381. /* Hide the filename immediately and ensure that the file does
  382. * not exist after the program ends
  383. */
  384. (void) unlink(tmpfile);
  385. }
  386. #endif
  387. if (fp == NULL)
  388. {
  389. perror("tmpfile");
  390. fprintf(stderr, "timepng: could not open the temporary file\n");
  391. exit(1); /* not a user error */
  392. }
  393. }
  394. /* Handle the transforms: */
  395. while (argc > 1 && argv[1][0] == '-' && argv[1][1] == '-')
  396. {
  397. const char *opt = *++argv + 2;
  398. --argc;
  399. /* Transforms turn on the by-image processing and maybe set some
  400. * transforms:
  401. */
  402. if (transforms == -1)
  403. transforms = PNG_TRANSFORM_IDENTITY;
  404. if (strcmp(opt, "by-image") == 0)
  405. {
  406. /* handled above */
  407. }
  408. # define OPT(name) else if (strcmp(opt, #name) == 0)\
  409. transforms |= PNG_TRANSFORM_ ## name
  410. OPT(STRIP_16);
  411. OPT(STRIP_ALPHA);
  412. OPT(PACKING);
  413. OPT(PACKSWAP);
  414. OPT(EXPAND);
  415. OPT(INVERT_MONO);
  416. OPT(SHIFT);
  417. OPT(BGR);
  418. OPT(SWAP_ALPHA);
  419. OPT(SWAP_ENDIAN);
  420. OPT(INVERT_ALPHA);
  421. OPT(STRIP_FILLER);
  422. OPT(STRIP_FILLER_BEFORE);
  423. OPT(STRIP_FILLER_AFTER);
  424. OPT(GRAY_TO_RGB);
  425. OPT(EXPAND_16);
  426. OPT(SCALE_16);
  427. else
  428. {
  429. fprintf(stderr, "timepng %s: unrecognized transform\n", opt);
  430. usage(fp);
  431. }
  432. }
  433. /* Handle the files: */
  434. if (argc > 1 && nfiles > 0)
  435. usage(fp); /* Additional files not valid with --dissemble */
  436. else if (argc > 1)
  437. {
  438. int i;
  439. for (i=1; i<argc; ++i)
  440. {
  441. if (nfiles == INT_MAX)
  442. {
  443. fprintf(stderr, "%s: skipped, too many files\n", argv[i]);
  444. break;
  445. }
  446. else if (add_one_file(fp, argv[i]))
  447. ++nfiles;
  448. }
  449. }
  450. else if (nfiles == 0) /* Read from stdin withoout --dissemble */
  451. {
  452. char filename[FILENAME_MAX+1];
  453. while (fgets(filename, FILENAME_MAX+1, stdin))
  454. {
  455. size_t len = strlen(filename);
  456. if (filename[len-1] == '\n')
  457. {
  458. filename[len-1] = 0;
  459. if (nfiles == INT_MAX)
  460. {
  461. fprintf(stderr, "%s: skipped, too many files\n", filename);
  462. break;
  463. }
  464. else if (add_one_file(fp, filename))
  465. ++nfiles;
  466. }
  467. else
  468. {
  469. fprintf(stderr, "timepng: file name too long: ...%s\n",
  470. filename+len-32);
  471. err = 1;
  472. break;
  473. }
  474. }
  475. if (ferror(stdin))
  476. {
  477. fprintf(stderr, "timepng: stdin: read error\n");
  478. err = 1;
  479. }
  480. }
  481. /* Perform the test, or produce the --assemble output: */
  482. if (!err)
  483. {
  484. if (nfiles > 0)
  485. {
  486. if (assembly != NULL)
  487. {
  488. if (fflush(fp) && !ferror(fp) && fclose(fp))
  489. {
  490. perror(assembly);
  491. fprintf(stderr, "%s: close failed\n", assembly);
  492. }
  493. else
  494. {
  495. printf("%s %d\n", assembly, nfiles);
  496. fflush(stdout);
  497. ok = !ferror(stdout);
  498. }
  499. }
  500. else
  501. {
  502. ok = perform_one_test(fp, nfiles, transforms);
  503. (void)fclose(fp);
  504. }
  505. }
  506. else
  507. usage(fp);
  508. }
  509. else
  510. (void)fclose(fp);
  511. /* Exit code 0 on success. */
  512. return ok == 0;
  513. }
  514. #else /* !sufficient support */
  515. int main(void) { return 77; }
  516. #endif /* !sufficient support */