NCEPLIBS-g2c  1.6.4
enc_png.c
Go to the documentation of this file.
1 
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <png.h>
10 #include "grib2.h"
11 
12 #ifndef USE_PNG
13 void dummy(void) {}
14 #else /* USE_PNG */
15 
19 struct png_stream
20 {
21  unsigned char *stream_ptr;
22  g2int stream_len;
23 };
24 
25 typedef struct png_stream png_stream;
27 void user_write_data(png_structp, png_bytep, png_uint_32);
28 void user_flush_data(png_structp);
29 
40 void
41 user_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
42 {
43  unsigned char *ptr;
44  g2int offset;
45  png_stream *mem;
46 
47  mem = (png_stream *)png_get_io_ptr(png_ptr);
48  ptr = mem->stream_ptr;
49  offset = mem->stream_len;
50  memcpy(ptr + offset, data, length);
51  mem->stream_len += length;
52 }
53 
61 void user_flush_data(png_structp png_ptr)
62 {
63  int *do_nothing;
64  do_nothing = NULL;
65 }
66 
80 int
81 enc_png(char *data, g2int width, g2int height, g2int nbits, char *pngbuf)
82 {
83  int color_type;
84  g2int j, bytes, pnglen, bit_depth;
85  png_structp png_ptr;
86  png_infop info_ptr;
87  png_bytep **row_pointers;
88  png_stream write_io_ptr;
89 
90  /* Create and initialize png_structs. */
91  if (!(png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL)))
92  return (-1);
93 
94  if (!(info_ptr = png_create_info_struct(png_ptr)))
95  {
96  png_destroy_write_struct(&png_ptr, (png_infopp)NULL);
97  return (-2);
98  }
99 
100  /* Set Error callback. */
101  if (setjmp(png_jmpbuf(png_ptr)))
102  {
103  png_destroy_write_struct(&png_ptr, &info_ptr);
104  return (-3);
105  }
106 
107  /* Initialize info for writing PNG stream to memory. */
108  write_io_ptr.stream_ptr = (png_voidp)pngbuf;
109  write_io_ptr.stream_len = 0;
110 
111  /* Set new custom write functions. */
112  png_set_write_fn(png_ptr, (png_voidp)&write_io_ptr, (png_rw_ptr)user_write_data,
113  (png_flush_ptr)user_flush_data);
114 
115  /* Set the image size, colortype, filter type, etc... */
116  bit_depth = nbits;
117  color_type = PNG_COLOR_TYPE_GRAY;
118  if (nbits == 24)
119  {
120  bit_depth = 8;
121  color_type = PNG_COLOR_TYPE_RGB;
122  }
123  else if (nbits == 32)
124  {
125  bit_depth = 8;
126  color_type = PNG_COLOR_TYPE_RGB_ALPHA;
127  }
128  png_set_IHDR(png_ptr, info_ptr, width, height,
129  bit_depth, color_type, PNG_INTERLACE_NONE,
130  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
131 
132  /* Put image data into the PNG info structure. */
133  bytes = nbits / 8;
134  row_pointers = malloc(height * sizeof(png_bytep));
135  for (j = 0; j < height; j++)
136  row_pointers[j] = (png_bytep *)(data + (j * width * bytes));
137  png_set_rows(png_ptr, info_ptr, (png_bytepp)row_pointers);
138 
139  /* Do the PNG encoding, and write out PNG stream. */
140  png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
141 
142  /* Clean up. */
143  png_destroy_write_struct(&png_ptr, &info_ptr);
144  free(row_pointers);
145  pnglen = write_io_ptr.stream_len;
146  return pnglen;
147 }
148 
149 #endif /* USE_PNG */
int enc_png(char *data, g2int width, g2int height, g2int nbits, char *pngbuf)
Encode PNG.
Definition: enc_png.c:81
struct png_stream png_stream
Typedef for PNG stream.
Definition: enc_png.c:25
void user_flush_data(png_structp)
Dummy Custom flush function.
Definition: enc_png.c:61
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: enc_png.c:41
Header file for NCEPLIBS-g2c library.
int64_t g2int
Long integer type.
Definition: grib2.h:20