ungrib build
[WPS.git] / ungrib / src / ngl / g2 / dec_png.c
blob75dee08dfe8ad7afde321e2ad61c8dc92bd2df5c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #ifdef USE_PNG
5 #include <png.h>
6 #include <zlib.h>
7 #endif /* USE_PNG */
9 #ifdef __64BIT__
10 typedef int g2int;
11 #else
12 typedef long g2int;
13 #endif
15 #if defined _UNDERSCORE
16 #define dec_png dec_png_
17 #elif defined _DOUBLEUNDERSCORE
18 #define dec_png dec_png__
19 #endif
21 #ifdef USE_PNG
22 struct png_stream {
23 unsigned char *stream_ptr; /* location to write PNG stream */
24 g2int stream_len; /* number of bytes written */
26 typedef struct png_stream png_stream;
28 void user_read_data(png_structp , png_bytep , png_uint_32 );
30 void user_read_data(png_structp png_ptr,png_bytep data, png_uint_32 length)
32 Custom read function used so that libpng will read a PNG stream
33 from memory instead of a file on disk.
36 char *ptr;
37 g2int offset;
38 png_stream *mem;
40 mem=(png_stream *)png_get_io_ptr(png_ptr);
41 ptr=(void *)mem->stream_ptr;
42 offset=mem->stream_len;
43 /* printf("SAGrd %ld %ld %x\n",offset,length,ptr); */
44 memcpy(data,ptr+offset,length);
45 mem->stream_len += length;
47 #endif /* USE_PNG */
51 int dec_png(unsigned char *pngbuf,g2int *width,g2int *height,char *cout)
53 #ifdef USE_PNG
54 int interlace,color,compres,filter,bit_depth;
55 g2int j,k,n,bytes,clen;
56 png_structp png_ptr;
57 png_infop info_ptr,end_info;
58 png_bytepp row_pointers;
59 png_stream read_io_ptr;
60 png_uint_32 h32, w32;
62 /* check if stream is a valid PNG format */
64 if ( png_sig_cmp(pngbuf,0,8) != 0)
65 return (-3);
67 /* create and initialize png_structs */
69 png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
70 NULL, NULL);
71 if (!png_ptr)
72 return (-1);
74 info_ptr = png_create_info_struct(png_ptr);
75 if (!info_ptr)
77 png_destroy_read_struct(&png_ptr,(png_infopp)NULL,(png_infopp)NULL);
78 return (-2);
81 end_info = png_create_info_struct(png_ptr);
82 if (!end_info)
84 png_destroy_read_struct(&png_ptr,(png_infopp)info_ptr,(png_infopp)NULL);
85 return (-2);
88 /* Set Error callback */
90 if (setjmp(png_jmpbuf(png_ptr)))
92 png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
93 return (-3);
96 /* Initialize info for reading PNG stream from memory */
98 read_io_ptr.stream_ptr=(png_voidp)pngbuf;
99 read_io_ptr.stream_len=0;
101 /* Set new custom read function */
103 png_set_read_fn(png_ptr,(png_voidp)&read_io_ptr,(png_rw_ptr)user_read_data);
104 /* png_init_io(png_ptr, fptr); */
106 /* Read and decode PNG stream */
108 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
110 /* Get pointer to each row of image data */
112 row_pointers = png_get_rows(png_ptr, info_ptr);
114 /* Get image info, such as size, depth, colortype, etc... */
116 /*printf("SAGT:png %d %d %d\n",info_ptr->width,info_ptr->height,info_ptr->bit_depth);*/
117 /* (void)png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *)width, (png_uint_32 *)height,
118 &bit_depth, &color, &interlace, &compres, &filter);*/
119 (void)png_get_IHDR(png_ptr, info_ptr, &w32, &h32,
120 &bit_depth, &color, &interlace, &compres, &filter);
122 *height = h32;
123 *width = w32;
125 /* Check if image was grayscale */
128 if (color != PNG_COLOR_TYPE_GRAY ) {
129 fprintf(stderr,"dec_png: Grayscale image was expected. \n");
132 if ( color == PNG_COLOR_TYPE_RGB ) {
133 bit_depth=24;
135 else if ( color == PNG_COLOR_TYPE_RGB_ALPHA ) {
136 bit_depth=32;
138 /* Copy image data to output string */
140 n=0;
141 bytes=bit_depth/8;
142 clen=(*width)*bytes;
143 for (j=0;j<*height;j++) {
144 for (k=0;k<clen;k++) {
145 cout[n]=*(row_pointers[j]+k);
146 n++;
150 /* Clean up */
152 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
153 #endif /* USE_PNG */
154 return 0;