1 /*****************************************************************************
2  * RRDtool 1.2.30  Copyright by Tobi Oetiker, 1997-2009
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(FILE * fd,long * width,long * height)11 PngSize(FILE *fd, long *width, long *height)
12 {
13   png_structp png_read_ptr =
14     png_create_read_struct(PNG_LIBPNG_VER_STRING,
15 			   (png_voidp)NULL,
16 				/* we would need to point to error handlers
17 				   here to do it properly */
18 			   (png_error_ptr)NULL, (png_error_ptr)NULL);
19 
20   png_infop info_ptr = png_create_info_struct(png_read_ptr);
21 
22   (*width)=0;
23   (*height)=0;
24 
25 /* this is to make compile on aix work since they seem to define jmpbuf
26    to be _jmpbuf which breaks compilation */
27 
28 
29 #ifndef png_jmpbuf
30 #ifdef PNG_SETJMP_SUPPORTED
31 #  define png_jmpbuf(png_ptr)   ((png_ptr)->PNG_jmpbuf)
32 #else
33 #ifdef jmpbuf
34 #undef jmpbuf
35 #endif
36 #  define png_jmpbuf(png_ptr)   ((png_ptr)->jmpbuf)
37 #endif
38 #endif
39 
40   if (setjmp(png_jmpbuf(png_read_ptr))){
41     png_destroy_read_struct(&png_read_ptr, &info_ptr, (png_infopp)NULL);
42     return 0;
43   }
44 
45   png_init_io(png_read_ptr,fd);
46   png_read_info(png_read_ptr, info_ptr);
47   (*width)=png_get_image_width(png_read_ptr, info_ptr);
48   (*height)=png_get_image_height(png_read_ptr, info_ptr);
49 
50   png_destroy_read_struct(&png_read_ptr, &info_ptr, NULL);
51   if (*width >0 && *height >0)
52     return 1;
53   else
54     return 0;
55 }
56 
57 
58 
59