NCEPLIBS-g2  3.4.5
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 #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 ENC_PNG
21 #elif defined LINUXF90
22  #define SUB_NAME ENC_PNG
23 #elif defined LINUXG95
24  #define SUB_NAME enc_png__
25 #elif defined HP || defined AIX
26  #define SUB_NAME enc_png
27 #elif defined SGI || defined LINUX || defined VPP5000 || defined APPLE
28  #define SUB_NAME enc_png_
29 #endif
30 
35 struct png_stream {
36 
37  unsigned char *stream_ptr;
38 /*
39 ** number of bytes written
40 */
41  g2int stream_len;
42 };
43 typedef struct png_stream png_stream;
45 void user_write_data(png_structp ,png_bytep , png_uint_32 );
46 void user_flush_data(png_structp );
47 
59 void user_write_data(png_structp png_ptr,png_bytep data, png_uint_32 length)
60 
61 {
62  unsigned char *ptr;
63  g2int offset;
64  png_stream *mem;
65 
66  mem=(png_stream *)png_get_io_ptr(png_ptr);
67  ptr=mem->stream_ptr;
68  offset=mem->stream_len;
69 /* printf("SAGwr %ld %ld %x\n",offset,length,ptr); */
70 /* for (j=offset,k=0;k<length;j++,k++) ptr[j]=data[k];*/
71  memcpy(ptr+offset,data,length);
72  mem->stream_len += length;
73 }
74 
83 void user_flush_data(png_structp png_ptr)
84 
85 {
86  int *do_nothing=NULL;
87 }
88 
102 int SUB_NAME(char *data,g2int *width,g2int *height,g2int *nbits,char *pngbuf)
103 {
104 
105  int color_type;
106  g2int j,bytes,pnglen,bit_depth;
107  png_structp png_ptr;
108  png_infop info_ptr;
109 /* png_bytep *row_pointers[*height]; */
110  png_bytep **row_pointers;
111  png_stream write_io_ptr;
112 
113 /*
114 ** create and initialize png_structs
115 */
116 
117  png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
118  NULL, NULL);
119  if (!png_ptr)
120  return (-1);
121 
122  info_ptr = png_create_info_struct(png_ptr);
123  if (!info_ptr)
124  {
125  png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
126  return (-2);
127  }
128 
129 /*
130 ** Set Error callback
131 */
132 
133  if (setjmp(png_jmpbuf(png_ptr)))
134  {
135  png_destroy_write_struct(&png_ptr, &info_ptr);
136  return (-3);
137  }
138 
139 /*
140 ** Initialize info for writing PNG stream to memory
141 */
142 
143  write_io_ptr.stream_ptr=(png_voidp)pngbuf;
144  write_io_ptr.stream_len=0;
145 
146 /*
147 ** Set new custom write functions
148 */
149 
150  png_set_write_fn(png_ptr,(png_voidp)&write_io_ptr,(png_rw_ptr)user_write_data,
151  (png_flush_ptr)user_flush_data);
152 /* png_init_io(png_ptr, fptr); */
153 /* png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); */
154 
155 /*
156 ** Set the image size, colortype, filter type, etc...
157 */
158 
159 /* printf("SAGTsettingIHDR %d %d %d\n",*width,*height,bit_depth); */
160  bit_depth=*nbits;
161  color_type=PNG_COLOR_TYPE_GRAY;
162  if (*nbits == 24 ) {
163  bit_depth=8;
164  color_type=PNG_COLOR_TYPE_RGB;
165  }
166  else if (*nbits == 32 ) {
167  bit_depth=8;
168  color_type=PNG_COLOR_TYPE_RGB_ALPHA;
169  }
170  png_set_IHDR(png_ptr, info_ptr, *width, *height,
171  bit_depth, color_type, PNG_INTERLACE_NONE,
172  PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
173 /*
174 ** Put image data into the PNG info structure
175 */
176 
177  /*bytes=bit_depth/8;*/
178  bytes=*nbits/8;
179  row_pointers=malloc((*height)*sizeof(png_bytep));
180  for (j=0;j<*height;j++) row_pointers[j]=(png_bytep *)(data+(j*(*width)*bytes));
181  png_set_rows(png_ptr, info_ptr, (png_bytepp)row_pointers);
182 
183 /*
184 ** Do the PNG encoding, and write out PNG stream
185 */
186  png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
187 
188 /*
189 ** Clean up
190 */
191 
192  png_destroy_write_struct(&png_ptr, &info_ptr);
193  free(row_pointers);
194  pnglen=write_io_ptr.stream_len;
195  return pnglen;
196 
197 }
198 
png_stream
struct png_stream png_stream
location to write PNG stream.
Definition: enc_png.c:43
user_write_data
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:59
SUB_NAME
int SUB_NAME(char *data, g2int *width, g2int *height, g2int *nbits, char *pngbuf)
create png_structs to write png stream into memory.
Definition: enc_png.c:102
user_flush_data
void user_flush_data(png_structp)
Dummy Custom flush function.
Definition: enc_png.c:83
g2int
long g2int
Long integer type.
Definition: enc_png.c:15
g2int
long g2int
Long Integer type.
Definition: dec_jpeg2000.c:18