NCEPLIBS-g2  3.4.8
enc_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 typedef int g2int;
18 struct png_stream {
19 
20  unsigned char *stream_ptr;
21 /*
22 ** number of bytes written
23 */
24  g2int stream_len;
25 };
26 typedef struct png_stream png_stream;
39 static void
40 user_write_data(png_structp png_ptr,png_bytep data, png_uint_32 length)
41 {
42  unsigned char *ptr;
43  g2int offset;
44  png_stream *mem;
45 
46  mem = (png_stream *)png_get_io_ptr(png_ptr);
47  ptr = mem->stream_ptr;
48  offset = mem->stream_len;
49  memcpy(ptr + offset, data, length);
50  mem->stream_len += length;
51 }
52 
61 static void
62 user_flush_data(png_structp png_ptr)
63 {
64 }
65 
78 int
79 enc_png(char *data, g2int *width, g2int *height, g2int *nbits, char *pngbuf)
80 {
81 
82  int color_type;
83  g2int j, bytes, pnglen, bit_depth;
84  png_structp png_ptr;
85  png_infop info_ptr;
86  png_bytep **row_pointers;
87  png_stream write_io_ptr;
88 
89  /* create and initialize png_structs. */
90  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
91  NULL, NULL);
92  if (!png_ptr)
93  return -1;
94 
95  info_ptr = png_create_info_struct(png_ptr);
96  if (!info_ptr)
97  {
98  png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
99  return -2;
100  }
101 
102  /* Set Error callback. */
103  if (setjmp(png_jmpbuf(png_ptr)))
104  {
105  png_destroy_write_struct(&png_ptr, &info_ptr);
106  return -3;
107  }
108 
109  /* Initialize info for writing PNG stream to memory. */
110  write_io_ptr.stream_ptr = (png_voidp)pngbuf;
111  write_io_ptr.stream_len = 0;
112 
113  /* Set new custom write functions. */
114  png_set_write_fn(png_ptr, (png_voidp)&write_io_ptr, (png_rw_ptr)user_write_data,
115  (png_flush_ptr)user_flush_data);
116 
117  /* Set the image size, colortype, filter type, etc... */
118  bit_depth = *nbits;
119  color_type = PNG_COLOR_TYPE_GRAY;
120  if (*nbits == 24)
121  {
122  bit_depth = 8;
123  color_type = PNG_COLOR_TYPE_RGB;
124  }
125  else if (*nbits == 32)
126  {
127  bit_depth = 8;
128  color_type = PNG_COLOR_TYPE_RGB_ALPHA;
129  }
130  png_set_IHDR(png_ptr, info_ptr, *width, *height,
131  bit_depth, color_type, PNG_INTERLACE_NONE,
132  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
133 
134  /* Put image data into the PNG info structure. */
135  bytes = *nbits / 8;
136  row_pointers = malloc((*height) * sizeof(png_bytep));
137  for (j = 0; j < *height; j++)
138  row_pointers[j] = (png_bytep *)(data+(j * (*width) * bytes));
139  png_set_rows(png_ptr, info_ptr, (png_bytepp)row_pointers);
140 
141  /* Do the PNG encoding, and write out PNG stream. */
142  png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
143 
144  /* Clean up. */
145  png_destroy_write_struct(&png_ptr, &info_ptr);
146  free(row_pointers);
147  pnglen = write_io_ptr.stream_len;
148  return pnglen;
149 
150 }
151 
int g2int
Integer type.
Definition: dec_png.c:12
int enc_png(char *data, g2int *width, g2int *height, g2int *nbits, char *pngbuf)
create png_structs to write png stream into memory.
Definition: enc_png.c:79
struct png_stream png_stream
location to write PNG stream.
Definition: enc_png.c:26
static void user_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
Custom write function used to that libpng will write to memory location instead of a file on disk.
Definition: enc_png.c:40
int g2int
Long integer type.
Definition: enc_png.c:12
static void user_flush_data(png_structp png_ptr)
Dummy Custom flush function.
Definition: enc_png.c:62
integer j
loop iterator