timepng.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /* timepng.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. #define _POSIX_C_SOURCE 199309L /* for clock_gettime */
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <time.h>
  22. #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
  23. # include <config.h>
  24. #endif
  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. static int read_png(FILE *fp)
  34. {
  35. png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0);
  36. png_infop info_ptr = NULL;
  37. png_bytep row = NULL, display = NULL;
  38. if (png_ptr == NULL)
  39. return 0;
  40. if (setjmp(png_jmpbuf(png_ptr)))
  41. {
  42. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  43. if (row != NULL) free(row);
  44. if (display != NULL) free(display);
  45. return 0;
  46. }
  47. png_init_io(png_ptr, fp);
  48. info_ptr = png_create_info_struct(png_ptr);
  49. if (info_ptr == NULL)
  50. png_error(png_ptr, "OOM allocating info structure");
  51. png_read_info(png_ptr, info_ptr);
  52. {
  53. png_size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
  54. row = malloc(rowbytes);
  55. display = malloc(rowbytes);
  56. if (row == NULL || display == NULL)
  57. png_error(png_ptr, "OOM allocating row buffers");
  58. {
  59. png_uint_32 height = png_get_image_height(png_ptr, info_ptr);
  60. int passes = png_set_interlace_handling(png_ptr);
  61. int pass;
  62. png_start_read_image(png_ptr);
  63. for (pass = 0; pass < passes; ++pass)
  64. {
  65. png_uint_32 y = height;
  66. /* NOTE: this trashes the row each time; interlace handling won't
  67. * work, but this avoids memory thrashing for speed testing.
  68. */
  69. while (y-- > 0)
  70. png_read_row(png_ptr, row, display);
  71. }
  72. }
  73. }
  74. /* Make sure to read to the end of the file: */
  75. png_read_end(png_ptr, info_ptr);
  76. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  77. free(row);
  78. free(display);
  79. return 1;
  80. }
  81. static int mytime(struct timespec *t)
  82. {
  83. /* Do the timing using clock_gettime and the per-process timer. */
  84. if (!clock_gettime(CLOCK_PROCESS_CPUTIME_ID, t))
  85. return 1;
  86. perror("CLOCK_PROCESS_CPUTIME_ID");
  87. fprintf(stderr, "timepng: could not get the time\n");
  88. return 0;
  89. }
  90. static int perform_one_test(FILE *fp, int nfiles)
  91. {
  92. int i;
  93. struct timespec before, after;
  94. /* Clear out all errors: */
  95. rewind(fp);
  96. if (mytime(&before))
  97. {
  98. for (i=0; i<nfiles; ++i)
  99. {
  100. if (read_png(fp))
  101. {
  102. if (ferror(fp))
  103. {
  104. perror("temporary file");
  105. fprintf(stderr, "file %d: error reading PNG data\n", i);
  106. return 0;
  107. }
  108. }
  109. else
  110. {
  111. perror("temporary file");
  112. fprintf(stderr, "file %d: error from libpng\n", i);
  113. return 0;
  114. }
  115. }
  116. }
  117. else
  118. return 0;
  119. if (mytime(&after))
  120. {
  121. /* Work out the time difference and print it - this is the only output,
  122. * so flush it immediately.
  123. */
  124. unsigned long s = after.tv_sec - before.tv_sec;
  125. long ns = after.tv_nsec - before.tv_nsec;
  126. if (ns < 0)
  127. {
  128. --s;
  129. ns += 1000000000;
  130. if (ns < 0)
  131. {
  132. fprintf(stderr, "timepng: bad clock from kernel\n");
  133. return 0;
  134. }
  135. }
  136. printf("%lu.%.9ld\n", s, ns);
  137. fflush(stdout);
  138. if (ferror(stdout))
  139. {
  140. fprintf(stderr, "timepng: error writing output\n");
  141. return 0;
  142. }
  143. /* Successful return */
  144. return 1;
  145. }
  146. else
  147. return 0;
  148. }
  149. static int add_one_file(FILE *fp, char *name)
  150. {
  151. FILE *ip = fopen(name, "rb");
  152. if (ip != NULL)
  153. {
  154. int ch;
  155. for (;;)
  156. {
  157. ch = getc(ip);
  158. if (ch == EOF) break;
  159. putc(ch, fp);
  160. }
  161. if (ferror(ip))
  162. {
  163. perror(name);
  164. fprintf(stderr, "%s: read error\n", name);
  165. return 0;
  166. }
  167. (void)fclose(ip);
  168. if (ferror(fp))
  169. {
  170. perror("temporary file");
  171. fprintf(stderr, "temporary file write error\n");
  172. return 0;
  173. }
  174. }
  175. else
  176. {
  177. perror(name);
  178. fprintf(stderr, "%s: open failed\n", name);
  179. return 0;
  180. }
  181. return 1;
  182. }
  183. int main(int argc, char **argv)
  184. {
  185. int ok = 0;
  186. FILE *fp = tmpfile();
  187. if (fp != NULL)
  188. {
  189. int err = 0;
  190. int nfiles = 0;
  191. if (argc > 1)
  192. {
  193. int i;
  194. for (i=1; i<argc; ++i)
  195. {
  196. if (add_one_file(fp, argv[i]))
  197. ++nfiles;
  198. else
  199. {
  200. err = 1;
  201. break;
  202. }
  203. }
  204. }
  205. else
  206. {
  207. char filename[FILENAME_MAX+1];
  208. while (fgets(filename, FILENAME_MAX+1, stdin))
  209. {
  210. size_t len = strlen(filename);
  211. if (filename[len-1] == '\n')
  212. {
  213. filename[len-1] = 0;
  214. if (add_one_file(fp, filename))
  215. ++nfiles;
  216. else
  217. {
  218. err = 1;
  219. break;
  220. }
  221. }
  222. else
  223. {
  224. fprintf(stderr, "timepng: truncated file name ...%s\n",
  225. filename+len-32);
  226. err = 1;
  227. break;
  228. }
  229. }
  230. if (ferror(stdin))
  231. {
  232. fprintf(stderr, "timepng: stdin: read error\n");
  233. err = 1;
  234. }
  235. }
  236. if (!err)
  237. {
  238. if (nfiles > 0)
  239. ok = perform_one_test(fp, nfiles);
  240. else
  241. fprintf(stderr, "usage: timepng {files} or ls files | timepng\n");
  242. }
  243. (void)fclose(fp);
  244. }
  245. else
  246. fprintf(stderr, "timepng: could not open temporary file\n");
  247. /* Exit code 0 on success. */
  248. return ok == 0;
  249. }