1 |
|
---|
2 | /* readpng.c
|
---|
3 | *
|
---|
4 | * Copyright (c) 2013 John Cunningham Bowler
|
---|
5 | *
|
---|
6 | * This code is released under the libpng license.
|
---|
7 | * For conditions of distribution and use, see the disclaimer
|
---|
8 | * and license in png.h
|
---|
9 | *
|
---|
10 | * Load an arbitrary number of PNG files (from the command line, or, if there
|
---|
11 | * are no arguments on the command line, from stdin) then run a time test by
|
---|
12 | * reading each file by row. The test does nothing with the read result and
|
---|
13 | * does no transforms. The only output is a time as a floating point number of
|
---|
14 | * seconds with 9 decimal digits.
|
---|
15 | */
|
---|
16 | #include <stdlib.h>
|
---|
17 | #include <stdio.h>
|
---|
18 | #include <string.h>
|
---|
19 |
|
---|
20 | #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
|
---|
21 | # include <config.h>
|
---|
22 | #endif
|
---|
23 |
|
---|
24 | /* Define the following to use this test against your installed libpng, rather
|
---|
25 | * than the one being built here:
|
---|
26 | */
|
---|
27 | #ifdef PNG_FREESTANDING_TESTS
|
---|
28 | # include <png.h>
|
---|
29 | #else
|
---|
30 | # include "../../png.h"
|
---|
31 | #endif
|
---|
32 |
|
---|
33 | static int
|
---|
34 | read_png(FILE *fp)
|
---|
35 | {
|
---|
36 | png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0);
|
---|
37 | png_infop info_ptr = NULL;
|
---|
38 | png_bytep row = NULL, display = NULL;
|
---|
39 |
|
---|
40 | if (png_ptr == NULL)
|
---|
41 | return 0;
|
---|
42 |
|
---|
43 | if (setjmp(png_jmpbuf(png_ptr)))
|
---|
44 | {
|
---|
45 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
---|
46 | if (row != NULL) free(row);
|
---|
47 | if (display != NULL) free(display);
|
---|
48 | return 0;
|
---|
49 | }
|
---|
50 |
|
---|
51 | png_init_io(png_ptr, fp);
|
---|
52 |
|
---|
53 | info_ptr = png_create_info_struct(png_ptr);
|
---|
54 | if (info_ptr == NULL)
|
---|
55 | png_error(png_ptr, "OOM allocating info structure");
|
---|
56 |
|
---|
57 | png_set_keep_unknown_chunks(png_ptr, PNG_HANDLE_CHUNK_ALWAYS, NULL, 0);
|
---|
58 |
|
---|
59 | png_read_info(png_ptr, info_ptr);
|
---|
60 |
|
---|
61 | {
|
---|
62 | size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
|
---|
63 |
|
---|
64 | /* Failure to initialize these is harmless */
|
---|
65 | row = malloc(rowbytes);
|
---|
66 | display = malloc(rowbytes);
|
---|
67 |
|
---|
68 | if (row == NULL || display == NULL)
|
---|
69 | png_error(png_ptr, "OOM allocating row buffers");
|
---|
70 |
|
---|
71 | {
|
---|
72 | png_uint_32 height = png_get_image_height(png_ptr, info_ptr);
|
---|
73 | # ifdef PNG_READ_INTERLACING_SUPPORTED
|
---|
74 | int passes = png_set_interlace_handling(png_ptr);
|
---|
75 | # else /* !READ_INTERLACING */
|
---|
76 | int passes = png_get_interlace_type(png_ptr, info_ptr) ==
|
---|
77 | PNG_INTERLACE_ADAM7 ? PNG_INTERLACE_ADAM7_PASSES : 1;
|
---|
78 | # endif /* !READ_INTERLACING */
|
---|
79 | int pass;
|
---|
80 |
|
---|
81 | png_start_read_image(png_ptr);
|
---|
82 |
|
---|
83 | for (pass = 0; pass < passes; ++pass)
|
---|
84 | {
|
---|
85 | png_uint_32 y = height;
|
---|
86 |
|
---|
87 | # ifndef PNG_READ_INTERLACING_SUPPORTED
|
---|
88 | if (passes == PNG_INTERLACE_ADAM7_PASSES)
|
---|
89 | y = PNG_PASS_ROWS(y, pass);
|
---|
90 | # endif /* READ_INTERLACING */
|
---|
91 |
|
---|
92 | /* NOTE: this trashes the row each time; interlace handling won't
|
---|
93 | * work, but this avoids memory thrashing for speed testing.
|
---|
94 | */
|
---|
95 | while (y-- > 0)
|
---|
96 | png_read_row(png_ptr, row, display);
|
---|
97 | }
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | /* Make sure to read to the end of the file: */
|
---|
102 | png_read_end(png_ptr, info_ptr);
|
---|
103 | png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
|
---|
104 | free(row);
|
---|
105 | free(display);
|
---|
106 | return 1;
|
---|
107 | }
|
---|
108 |
|
---|
109 | int
|
---|
110 | main(void)
|
---|
111 | {
|
---|
112 | /* Exit code 0 on success. */
|
---|
113 | return !read_png(stdin);
|
---|
114 | }
|
---|