prepare for the release of rrdtool-1.3rc7
[rrdtool.git] / src / pngsize.c
1 /*****************************************************************************
2  * RRDtool 1.3rc7  Copyright by Tobi Oetiker, 1997-2008
3  *****************************************************************************
4  * pngsize.c  determine the size of a PNG image
5  *****************************************************************************/
6
7 #include <png.h>
8 #include "rrd_tool.h"
9
10 int PngSize(
11     FILE * fd,
12     long *width,
13     long *height)
14 {
15     png_structp png_read_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
16                                                       (png_voidp) NULL,
17                                                       /* we would need to point to error handlers
18                                                          here to do it properly */
19                                                       (png_error_ptr) NULL,
20                                                       (png_error_ptr) NULL);
21
22     png_infop info_ptr = png_create_info_struct(png_read_ptr);
23
24     (*width) = 0;
25     (*height) = 0;
26
27 /* this is to make compile on aix work since they seem to define jmpbuf
28    to be _jmpbuf which breaks compilation */
29
30 #ifdef jmpbuf
31 #undef jmpbuf
32 #endif
33
34 #ifndef png_jmpbuf
35 #  define png_jmpbuf(png_ptr)   ((png_ptr)->jmpbuf)
36 #endif
37
38     if (setjmp(png_jmpbuf(png_read_ptr))) {
39         png_destroy_read_struct(&png_read_ptr, &info_ptr, (png_infopp) NULL);
40         return 0;
41     }
42
43     png_init_io(png_read_ptr, fd);
44     png_read_info(png_read_ptr, info_ptr);
45     (*width) = png_get_image_width(png_read_ptr, info_ptr);
46     (*height) = png_get_image_height(png_read_ptr, info_ptr);
47
48     png_destroy_read_struct(&png_read_ptr, &info_ptr, NULL);
49     if (*width > 0 && *height > 0)
50         return 1;
51     else
52         return 0;
53 }