Check for SYS/GL during library init. Reason is that
[AROS.git] / workbench / libs / png / contrib / libtests / timepng.c
blob7c937971cfd265fd31878640fa3ecb2da98081b5
1 /* timepng.c
3 * Copyright (c) 2013 John Cunningham Bowler
5 * Last changed in libpng 1.6.1 [March 28, 2013]
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
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.
17 #define _POSIX_C_SOURCE 199309L /* for clock_gettime */
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
23 #include <time.h>
25 #if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
26 # include <config.h>
27 #endif
29 /* Define the following to use this test against your installed libpng, rather
30 * than the one being built here:
32 #ifdef PNG_FREESTANDING_TESTS
33 # include <png.h>
34 #else
35 # include "../../png.h"
36 #endif
38 static int read_png(FILE *fp)
40 png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,0,0,0);
41 png_infop info_ptr = NULL;
42 png_bytep row = NULL, display = NULL;
44 if (png_ptr == NULL)
45 return 0;
47 if (setjmp(png_jmpbuf(png_ptr)))
49 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
50 if (row != NULL) free(row);
51 if (display != NULL) free(display);
52 return 0;
55 png_init_io(png_ptr, fp);
57 info_ptr = png_create_info_struct(png_ptr);
58 if (info_ptr == NULL)
59 png_error(png_ptr, "OOM allocating info structure");
61 png_read_info(png_ptr, info_ptr);
64 png_size_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
66 row = malloc(rowbytes);
67 display = malloc(rowbytes);
69 if (row == NULL || display == NULL)
70 png_error(png_ptr, "OOM allocating row buffers");
73 png_uint_32 height = png_get_image_height(png_ptr, info_ptr);
74 int passes = png_set_interlace_handling(png_ptr);
75 int pass;
77 png_start_read_image(png_ptr);
79 for (pass = 0; pass < passes; ++pass)
81 png_uint_32 y = height;
83 /* NOTE: this trashes the row each time; interlace handling won't
84 * work, but this avoids memory thrashing for speed testing.
86 while (y-- > 0)
87 png_read_row(png_ptr, row, display);
92 /* Make sure to read to the end of the file: */
93 png_read_end(png_ptr, info_ptr);
94 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
95 free(row);
96 free(display);
97 return 1;
100 static int mytime(struct timespec *t)
102 /* Do the timing using clock_gettime and the per-process timer. */
103 if (!clock_gettime(CLOCK_PROCESS_CPUTIME_ID, t))
104 return 1;
106 perror("CLOCK_PROCESS_CPUTIME_ID");
107 fprintf(stderr, "timepng: could not get the time\n");
108 return 0;
111 static int perform_one_test(FILE *fp, int nfiles)
113 int i;
114 struct timespec before, after;
116 /* Clear out all errors: */
117 rewind(fp);
119 if (mytime(&before))
121 for (i=0; i<nfiles; ++i)
123 if (read_png(fp))
125 if (ferror(fp))
127 perror("temporary file");
128 fprintf(stderr, "file %d: error reading PNG data\n", i);
129 return 0;
133 else
135 perror("temporary file");
136 fprintf(stderr, "file %d: error from libpng\n", i);
137 return 0;
142 else
143 return 0;
145 if (mytime(&after))
147 /* Work out the time difference and print it - this is the only output,
148 * so flush it immediately.
150 unsigned long s = after.tv_sec - before.tv_sec;
151 long ns = after.tv_nsec - before.tv_nsec;
153 if (ns < 0)
155 --s;
156 ns += 1000000000;
158 if (ns < 0)
160 fprintf(stderr, "timepng: bad clock from kernel\n");
161 return 0;
165 printf("%lu.%.9ld\n", s, ns);
166 fflush(stdout);
167 if (ferror(stdout))
169 fprintf(stderr, "timepng: error writing output\n");
170 return 0;
173 /* Successful return */
174 return 1;
177 else
178 return 0;
181 static int add_one_file(FILE *fp, char *name)
183 FILE *ip = fopen(name, "rb");
185 if (ip != NULL)
187 int ch;
188 for (;;)
190 ch = getc(ip);
191 if (ch == EOF) break;
192 putc(ch, fp);
195 if (ferror(ip))
197 perror(name);
198 fprintf(stderr, "%s: read error\n", name);
199 return 0;
202 (void)fclose(ip);
204 if (ferror(fp))
206 perror("temporary file");
207 fprintf(stderr, "temporary file write error\n");
208 return 0;
212 else
214 perror(name);
215 fprintf(stderr, "%s: open failed\n", name);
216 return 0;
219 return 1;
222 int main(int argc, char **argv)
224 int ok = 0;
225 FILE *fp = tmpfile();
227 if (fp != NULL)
229 int err = 0;
230 int nfiles = 0;
232 if (argc > 1)
234 int i;
236 for (i=1; i<argc; ++i)
238 if (add_one_file(fp, argv[i]))
239 ++nfiles;
241 else
243 err = 1;
244 break;
249 else
251 char filename[FILENAME_MAX+1];
253 while (fgets(filename, FILENAME_MAX+1, stdin))
255 size_t len = strlen(filename);
257 if (filename[len-1] == '\n')
259 filename[len-1] = 0;
260 if (add_one_file(fp, filename))
261 ++nfiles;
263 else
265 err = 1;
266 break;
270 else
272 fprintf(stderr, "timepng: truncated file name ...%s\n",
273 filename+len-32);
274 err = 1;
275 break;
279 if (ferror(stdin))
281 fprintf(stderr, "timepng: stdin: read error\n");
282 err = 1;
286 if (!err)
288 if (nfiles > 0)
289 ok = perform_one_test(fp, nfiles);
291 else
292 fprintf(stderr, "usage: timepng {files} or ls files | timepng\n");
295 (void)fclose(fp);
298 else
299 fprintf(stderr, "timepng: could not open temporary file\n");
301 /* Exit code 0 on success. */
302 return ok == 0;