Created a tag for the 2012 HWRF baseline tests.
[WPS-merge.git] / hwrf-baseline-20120103-1354 / ungrib / src / ngl / g2 / enc_png.c
blob8b80fd403270a21228758154d404cf894a7adbcc
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #ifdef USE_PNG
5 #include <png.h>
6 #include <zlib.h>
7 #endif /* USE_PNG */
9 #ifdef __64BIT__
10 typedef int g2int;
11 #else
12 typedef long g2int;
13 #endif
15 #if defined _UNDERSCORE
16 #define enc_png enc_png_
17 #elif defined _DOUBLEUNDERSCORE
18 #define enc_png enc_png__
19 #endif
21 #ifdef USE_PNG
22 struct png_stream {
23 unsigned char *stream_ptr; /* location to write PNG stream */
24 g2int stream_len; /* number of bytes written */
26 typedef struct png_stream png_stream;
28 void user_write_data(png_structp ,png_bytep , png_uint_32 );
29 void user_flush_data(png_structp );
31 void user_write_data(png_structp png_ptr,png_bytep data, png_uint_32 length)
33 Custom write function used to that libpng will write
34 to memory location instead of a file on disk
37 unsigned char *ptr;
38 g2int offset;
39 png_stream *mem;
41 mem=(png_stream *)png_get_io_ptr(png_ptr);
42 ptr=mem->stream_ptr;
43 offset=mem->stream_len;
44 /* printf("SAGwr %ld %ld %x\n",offset,length,ptr); */
45 /*for (j=offset,k=0;k<length;j++,k++) ptr[j]=data[k];*/
46 memcpy(ptr+offset,data,length);
47 mem->stream_len += length;
51 void user_flush_data(png_structp png_ptr)
53 Dummy Custom flush function
56 int *do_nothing=NULL;
58 #endif /* USE_PNG */
61 int enc_png(char *data,g2int *width,g2int *height,g2int *nbits,char *pngbuf)
63 g2int pnglen;
64 #ifdef USE_PNG
65 int color_type;
66 g2int j,bytes,bit_depth;
67 png_structp png_ptr;
68 png_infop info_ptr;
69 /* png_bytep *row_pointers[*height]; */
70 png_bytep **row_pointers;
71 png_stream write_io_ptr;
73 /* create and initialize png_structs */
75 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
76 NULL, NULL);
77 if (!png_ptr)
78 return (-1);
80 info_ptr = png_create_info_struct(png_ptr);
81 if (!info_ptr)
83 png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
84 return (-2);
87 /* Set Error callback */
89 if (setjmp(png_jmpbuf(png_ptr)))
91 png_destroy_write_struct(&png_ptr, &info_ptr);
92 return (-3);
95 /* Initialize info for writing PNG stream to memory */
97 write_io_ptr.stream_ptr=(png_voidp)pngbuf;
98 write_io_ptr.stream_len=0;
100 /* Set new custom write functions */
102 png_set_write_fn(png_ptr,(voidp)&write_io_ptr,(png_rw_ptr)user_write_data,
103 (png_flush_ptr)user_flush_data);
104 /* png_init_io(png_ptr, fptr); */
105 /* png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); */
107 /* Set the image size, colortype, filter type, etc... */
109 /* printf("SAGTsettingIHDR %d %d %d\n",*width,*height,bit_depth); */
110 bit_depth=*nbits;
111 color_type=PNG_COLOR_TYPE_GRAY;
112 if (*nbits == 24 ) {
113 bit_depth=8;
114 color_type=PNG_COLOR_TYPE_RGB;
116 else if (*nbits == 32 ) {
117 bit_depth=8;
118 color_type=PNG_COLOR_TYPE_RGB_ALPHA;
120 png_set_IHDR(png_ptr, info_ptr, *width, *height,
121 bit_depth, color_type, PNG_INTERLACE_NONE,
122 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
124 /* Put image data into the PNG info structure */
126 /*bytes=bit_depth/8;*/
127 bytes=*nbits/8;
128 row_pointers=malloc((*height)*sizeof(png_bytep));
129 for (j=0;j<*height;j++) row_pointers[j]=(png_bytep *)(data+(j*(*width)*bytes));
130 png_set_rows(png_ptr, info_ptr, (png_bytepp)row_pointers);
132 /* Do the PNG encoding, and write out PNG stream */
134 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
136 /* Clean up */
138 png_destroy_write_struct(&png_ptr, &info_ptr);
139 free(row_pointers);
140 pnglen=write_io_ptr.stream_len;
141 #endif /* USE_PNG */
142 return pnglen;