readpng.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* readpng.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. #include <stdlib.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
  21. # include <config.h>
  22. #endif
  23. /* Define the following to use this test against your installed libpng, rather
  24. * than the one being built here:
  25. */
  26. #ifdef PNG_FREESTANDING_TESTS
  27. # include <png.h>
  28. #else
  29. # include "../../png.h"
  30. #endif
  31. static int
  32. read_png(FILE *fp)
  33. {
  34. png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0);
  35. png_infop info_ptr = NULL;
  36. png_bytep row = NULL, display = NULL;
  37. if (png_ptr == NULL)
  38. return 0;
  39. if (setjmp(png_jmpbuf(png_ptr)))
  40. {
  41. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  42. if (row != NULL) free(row);
  43. if (display != NULL) free(display);
  44. return 0;
  45. }
  46. png_init_io(png_ptr, fp);
  47. info_ptr = png_create_info_struct(png_ptr);
  48. if (info_ptr == NULL)
  49. png_error(png_ptr, "OOM allocating info structure");
  50. png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, NULL, 0);
  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. int
  82. main(void)
  83. {
  84. /* Exit code 0 on success. */
  85. return !read_png(stdin);
  86. }