readpng.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. /* Failure to initialize these is harmless */
  55. row = malloc(rowbytes);
  56. display = malloc(rowbytes);
  57. if (row == NULL || display == NULL)
  58. png_error(png_ptr, "OOM allocating row buffers");
  59. {
  60. png_uint_32 height = png_get_image_height(png_ptr, info_ptr);
  61. # ifdef PNG_READ_INTERLACING_SUPPORTED
  62. int passes = png_set_interlace_handling(png_ptr);
  63. # else /* !READ_INTERLACING */
  64. int passes = png_get_interlace_type(png_ptr, info_ptr) ==
  65. PNG_INTERLACE_ADAM7 ? PNG_INTERLACE_ADAM7_PASSES : 1;
  66. # endif /* !READ_INTERLACING */
  67. int pass;
  68. png_start_read_image(png_ptr);
  69. for (pass = 0; pass < passes; ++pass)
  70. {
  71. png_uint_32 y = height;
  72. # ifndef PNG_READ_INTERLACING_SUPPORTED
  73. if (passes == PNG_INTERLACE_ADAM7_PASSES)
  74. y = PNG_PASS_ROWS(y, pass);
  75. # endif /* READ_INTERLACING */
  76. /* NOTE: this trashes the row each time; interlace handling won't
  77. * work, but this avoids memory thrashing for speed testing.
  78. */
  79. while (y-- > 0)
  80. png_read_row(png_ptr, row, display);
  81. }
  82. }
  83. }
  84. /* Make sure to read to the end of the file: */
  85. png_read_end(png_ptr, info_ptr);
  86. png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
  87. free(row);
  88. free(display);
  89. return 1;
  90. }
  91. int
  92. main(void)
  93. {
  94. /* Exit code 0 on success. */
  95. return !read_png(stdin);
  96. }