NCEPLIBS-g2c 2.0.0
Loading...
Searching...
No Matches
decenc_png.c
Go to the documentation of this file.
1
6#include "grib2_int.h"
7#include <png.h>
8#include <stdio.h>
9#include <stdlib.h>
10#include <string.h>
11
15struct png_stream
16{
17 unsigned char *stream_ptr;
18 g2int stream_len;
19};
20
21typedef struct png_stream png_stream;
23void user_read_data(png_structp, png_bytep, png_uint_32);
24void user_write_data(png_structp, png_bytep, png_uint_32);
25void user_flush_data(png_structp);
26
37void
38user_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
39{
40 char *ptr;
41 g2int offset;
42 png_stream *mem;
43
44 mem = (png_stream *)png_get_io_ptr(png_ptr);
45 ptr = (void *)mem->stream_ptr;
46 offset = mem->stream_len;
47 memcpy(data, ptr + offset, length);
48 mem->stream_len += length;
49}
50
61void
62user_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
63{
64 unsigned char *ptr;
65 g2int offset;
66 png_stream *mem;
67
68 mem = (png_stream *)png_get_io_ptr(png_ptr);
69 ptr = mem->stream_ptr;
70 offset = mem->stream_len;
71 memcpy(ptr + offset, data, length);
72 mem->stream_len += length;
73}
74
82void
83user_flush_data(png_structp png_ptr)
84{
85}
86
99int
100dec_png(unsigned char *pngbuf, g2int *width, g2int *height,
101 unsigned char *cout)
102{
103 int interlace, color, compres, filter, bit_depth;
104 g2int j, k, n, bytes;
105 png_structp png_ptr;
106 png_infop info_ptr, end_info;
107 png_bytepp row_pointers;
108 png_stream read_io_ptr;
109 png_uint_32 h32, w32;
110
111 /* Check if stream is a valid PNG format. */
112 if (png_sig_cmp(pngbuf, 0, 8) != 0)
113 return -3;
114
115 /* Create and initialize png_structs. */
116 if (!(png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
117 NULL, NULL)))
118 return -1;
119
120 if (!(info_ptr = png_create_info_struct(png_ptr)))
121 {
122 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
123 return -2;
124 }
125
126 if (!(end_info = png_create_info_struct(png_ptr)))
127 {
128 png_destroy_read_struct(&png_ptr, (png_infopp)info_ptr, (png_infopp)NULL);
129 return -2;
130 }
131
132 /* Set Error callback. */
133 if (setjmp(png_jmpbuf(png_ptr)))
134 {
135 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
136 return -3;
137 }
138
139 /* Initialize info for reading PNG stream from memory. */
140 read_io_ptr.stream_ptr = (png_voidp)pngbuf;
141 read_io_ptr.stream_len = 0;
142
143 /* Set new custom read function. */
144 png_set_read_fn(png_ptr, (png_voidp)&read_io_ptr, (png_rw_ptr)user_read_data);
145
146 /* Read and decode PNG stream. */
147 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
148
149 /* Get pointer to each row of image data. */
150 row_pointers = png_get_rows(png_ptr, info_ptr);
151
152 /* Get image info, such as size, depth, colortype, etc... */
153 /*printf("SAGT:png %d %d %d\n", info_ptr->width, info_ptr->height, info_ptr->bit_depth);*/
154 // (void)png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *)width, (png_uint_32 *)height,
155 (void)png_get_IHDR(png_ptr, info_ptr, &w32, &h32,
156 &bit_depth, &color, &interlace, &compres, &filter);
157
158 *height = h32;
159 *width = w32;
160
161 /* Check if image was grayscale. */
162 /*
163 if (color != PNG_COLOR_TYPE_GRAY) {
164 fprintf(stderr, "dec_png: Grayscale image was expected. \n");
165 }
166 */
167 if (color == PNG_COLOR_TYPE_RGB)
168 bit_depth = 24;
169 else if (color == PNG_COLOR_TYPE_RGB_ALPHA)
170 bit_depth = 32;
171
172 /* Copy image data to output string */
173 n = 0;
174 bytes = (*width * bit_depth) / 8;
175 if ((*width * bit_depth) % 8 != 0)
176 {
177 bytes++;
178 }
179 for (j = 0; j < *height; j++)
180 {
181 for (k = 0; k < bytes; k++)
182 {
183 cout[n] = *(row_pointers[j] + k);
184 n++;
185 }
186 }
187
188 /* Clean up. */
189 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
190 return 0;
191}
192
206int
207enc_png(unsigned char *data, g2int width, g2int height, g2int nbits,
208 unsigned char *pngbuf)
209{
210 int color_type;
211 g2int j, bytes, pnglen, bit_depth;
212 png_structp png_ptr;
213 png_infop info_ptr;
214 png_bytep **row_pointers;
215 png_stream write_io_ptr;
216
217 /* Create and initialize png_structs. */
218 if (!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)))
219 return -1;
220
221 if (!(info_ptr = png_create_info_struct(png_ptr)))
222 {
223 png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
224 return -2;
225 }
226
227 /* Set Error callback. */
228 if (setjmp(png_jmpbuf(png_ptr)))
229 {
230 png_destroy_write_struct(&png_ptr, &info_ptr);
231 return -3;
232 }
233
234 /* Initialize info for writing PNG stream to memory. */
235 write_io_ptr.stream_ptr = (png_voidp)pngbuf;
236 write_io_ptr.stream_len = 0;
237
238 /* Set new custom write functions. */
239 png_set_write_fn(png_ptr, (png_voidp)&write_io_ptr, (png_rw_ptr)user_write_data,
240 (png_flush_ptr)user_flush_data);
241
242 /* Set the image size, colortype, filter type, etc... */
243 bit_depth = nbits;
244 color_type = PNG_COLOR_TYPE_GRAY;
245 if (nbits == 24)
246 {
247 bit_depth = 8;
248 color_type = PNG_COLOR_TYPE_RGB;
249 }
250 else if (nbits == 32)
251 {
252 bit_depth = 8;
253 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
254 }
255 png_set_IHDR(png_ptr, info_ptr, width, height,
256 bit_depth, color_type, PNG_INTERLACE_NONE,
257 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
258
259 /* Put image data into the PNG info structure. */
260 bytes = (width * nbits) / 8;
261 if ((width * nbits) % 8 != 0)
262 {
263 bytes++;
264 }
265
266 row_pointers = malloc(height * sizeof(png_bytep));
267 for (j = 0; j < height; j++)
268 row_pointers[j] = (png_bytep *)(data + (j * bytes));
269
270 png_set_rows(png_ptr, info_ptr, (png_bytepp)row_pointers);
271
272 /* Do the PNG encoding, and write out PNG stream. */
273 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
274
275 /* Clean up. */
276 png_destroy_write_struct(&png_ptr, &info_ptr);
277 free(row_pointers);
278 pnglen = write_io_ptr.stream_len;
279 return pnglen;
280}
int enc_png(unsigned char *data, g2int width, g2int height, g2int nbits, unsigned char *pngbuf)
Encode PNG.
Definition decenc_png.c:207
int dec_png(unsigned char *pngbuf, g2int *width, g2int *height, unsigned char *cout)
Decode PNG.
Definition decenc_png.c:100
struct png_stream png_stream
Typedef for PNG stream.
Definition decenc_png.c:21
void user_flush_data(png_structp)
Dummy Custom flush function.
Definition decenc_png.c:83
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 decenc_png.c:38
void user_write_data(png_structp, png_bytep, png_uint_32)
Custom write function used to that libpng will write to memory location instead of a file on disk.
Definition decenc_png.c:62
int64_t g2int
Long integer type.
Definition grib2.h:32
Header file with internal function prototypes NCEPLIBS-g2c library.