NCEPLIBS-g2  3.4.5
dec_png.c
Go to the documentation of this file.
1 
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <string.h>
10 #include <png.h>
11 
12 #ifdef __64BIT__
13  typedef int g2int;
14 #else
15  typedef long g2int;
16 #endif
17 
18 #if defined CRAY90
19  #include <fortran.h>
20  #define SUB_NAME DEC_PNG
21 #elif defined LINUXF90
22  #define SUB_NAME DEC_PNG
23 #elif defined LINUXG95
24  #define SUB_NAME dec_png__
25 #elif defined HP || defined AIX
26  #define SUB_NAME dec_png
27 #elif defined SGI || defined LINUX || defined VPP5000 || defined APPLE
28  #define SUB_NAME dec_png_
29 #endif
30 
35 struct png_stream {
36  unsigned char *stream_ptr;
37  g2int stream_len;
38 };
39 typedef struct png_stream png_stream;
41 void user_read_data(png_structp , png_bytep , png_uint_32 );
42 
43 void user_read_data(png_structp png_ptr,png_bytep data, png_uint_32 length)
44 
45 
50 {
51  char *ptr;
52  g2int offset;
53  png_stream *mem;
54 
55  mem=(png_stream *)png_get_io_ptr(png_ptr);
56  ptr=(void *)mem->stream_ptr;
57  offset=mem->stream_len;
58 /* printf("SAGrd %ld %ld %x\n",offset,length,ptr); */
59  memcpy(data,ptr+offset,length);
60  mem->stream_len += length;
61 }
62 
76 int SUB_NAME(unsigned char *pngbuf,g2int *width,g2int *height,char *cout)
77 {
78  int interlace,color,compres,filter,bit_depth;
79  g2int j,k,n,bytes,clen;
80  png_structp png_ptr;
81  png_infop info_ptr,end_info;
82  png_bytepp row_pointers;
83  png_stream read_io_ptr;
84  png_uint_32 h32, w32;
85 
86 /*
87 ** check if stream is a valid PNG format
88 */
89 
90  if ( png_sig_cmp(pngbuf,0,8) != 0)
91  return (-3);
92 
93 /*
94 ** create and initialize png_structs
95 */
96 
97  png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
98  NULL, NULL);
99  if (!png_ptr)
100  return (-1);
101 
102  info_ptr = png_create_info_struct(png_ptr);
103  if (!info_ptr)
104  {
105  png_destroy_read_struct(&png_ptr,(png_infopp)NULL,(png_infopp)NULL);
106  return (-2);
107  }
108 
109  end_info = png_create_info_struct(png_ptr);
110  if (!end_info)
111  {
112  png_destroy_read_struct(&png_ptr,(png_infopp)info_ptr,(png_infopp)NULL);
113  return (-2);
114  }
115 
116 /*
117 ** Set Error callback
118 */
119 
120  if (setjmp(png_jmpbuf(png_ptr)))
121  {
122  png_destroy_read_struct(&png_ptr, &info_ptr,&end_info);
123  return (-3);
124  }
125 
126 /*
127 ** Initialize info for reading PNG stream from memory
128 */
129 
130  read_io_ptr.stream_ptr=(png_voidp)pngbuf;
131  read_io_ptr.stream_len=0;
132 
133 /*
134 ** Set new custom read function
135 */
136 
137  png_set_read_fn(png_ptr,(png_voidp)&read_io_ptr,(png_rw_ptr)user_read_data);
138 
139 /* png_init_io(png_ptr, fptr); */
140 
141 /*
142 ** Read and decode PNG stream
143 */
144 
145  png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
146 
147 /*
148 ** Get pointer to each row of image data
149 */
150 
151  row_pointers = png_get_rows(png_ptr, info_ptr);
152 
153 /*
154 ** Get image info, such as size, depth, colortype, etc...
155 */
156 
157 /* printf("SAGT:png %d %d %d\n",info_ptr->width,info_ptr->height,info_ptr->bit_depth);*/
158 /* (void)png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *)width, (png_uint_32 *)height,
159  &bit_depth, &color, &interlace, &compres, &filter);*/
160  (void)png_get_IHDR(png_ptr, info_ptr, &w32, &h32,
161  &bit_depth, &color, &interlace, &compres, &filter);
162 
163  *height = h32;
164  *width = w32;
165 
166 /*
167 ** Check if image was grayscale
168 */
169 
170 /*
171  if (color != PNG_COLOR_TYPE_GRAY ) {
172  fprintf(stderr,"dec_png: Grayscale image was expected. \n");
173  }
174 */
175  if ( color == PNG_COLOR_TYPE_RGB ) {
176  bit_depth=24;
177  }
178  else if ( color == PNG_COLOR_TYPE_RGB_ALPHA ) {
179  bit_depth=32;
180  }
181 /*
182 ** Copy image data to output string
183 */
184 
185  n=0;
186  bytes=bit_depth/8;
187  clen=(*width)*bytes;
188  for (j=0;j<*height;j++) {
189  for (k=0;k<clen;k++) {
190  cout[n]=*(row_pointers[j]+k);
191  n++;
192  }
193  }
194 
195 /*
196 ** Clean up
197 */
198 
199  png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
200  return 0;
201 
202 }
203 
SUB_NAME
int SUB_NAME(unsigned char *pngbuf, g2int *width, g2int *height, char *cout)
create png_structs to write png stream into memory.
Definition: dec_png.c:76
user_read_data
void user_read_data(png_structp, png_bytep, png_uint_32)
Custom read function used so that libpng will read a PNG stream from memory instead of a file on disk...
Definition: dec_png.c:43
png_stream
struct png_stream png_stream
location to write PNG stream
Definition: dec_png.c:39
g2int
long g2int
Long Integer type.
Definition: dec_png.c:15
g2int
long g2int
Long Integer type.
Definition: dec_jpeg2000.c:18