Update the g2lib to NCEP's latest version (g2lib-1.2.2)
[WPS.git] / ungrib / src / ngl / g2 / enc_png.c
blob04e0f9c14118e813067c77f7e1821396f8e8490d
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #ifdef USE_PNG
5 #include <png.h>
6 #endif /* USE_PNG */
8 #ifdef __64BIT__
9 typedef int g2int;
10 #else
11 typedef long g2int;
12 #endif
14 #if defined _UNDERSCORE
15 #define enc_png enc_png_
16 #elif defined _DOUBLEUNDERSCORE
17 #define enc_png enc_png__
18 #endif
20 #ifdef USE_PNG
21 struct png_stream {
22 unsigned char *stream_ptr; /* location to write PNG stream */
23 g2int stream_len; /* number of bytes written */
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 );
30 void user_write_data(png_structp png_ptr,png_bytep data, png_uint_32 length)
32 Custom write function used to that libpng will write
33 to memory location instead of a file on disk
36 unsigned char *ptr;
37 g2int offset;
38 png_stream *mem;
40 mem=(png_stream *)png_get_io_ptr(png_ptr);
41 ptr=mem->stream_ptr;
42 offset=mem->stream_len;
43 /* printf("SAGwr %ld %ld %x\n",offset,length,ptr); */
44 /*for (j=offset,k=0;k<length;j++,k++) ptr[j]=data[k];*/
45 memcpy(ptr+offset,data,length);
46 mem->stream_len += length;
50 void user_flush_data(png_structp png_ptr)
52 Dummy Custom flush function
55 int *do_nothing=NULL;
57 #endif /* USE_PNG */
60 int enc_png(char *data,g2int *width,g2int *height,g2int *nbits,char *pngbuf)
62 g2int pnglen;
63 #ifdef USE_PNG
64 int color_type;
65 g2int j,bytes,bit_depth;
66 png_structp png_ptr;
67 png_infop info_ptr;
68 /* png_bytep *row_pointers[*height]; */
69 png_bytep **row_pointers;
70 png_stream write_io_ptr;
72 /* create and initialize png_structs */
74 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
75 NULL, NULL);
76 if (!png_ptr)
77 return (-1);
79 info_ptr = png_create_info_struct(png_ptr);
80 if (!info_ptr)
82 png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
83 return (-2);
86 /* Set Error callback */
88 if (setjmp(png_jmpbuf(png_ptr)))
90 png_destroy_write_struct(&png_ptr, &info_ptr);
91 return (-3);
94 /* Initialize info for writing PNG stream to memory */
96 write_io_ptr.stream_ptr=(png_voidp)pngbuf;
97 write_io_ptr.stream_len=0;
99 /* Set new custom write functions */
101 png_set_write_fn(png_ptr,(voidp)&write_io_ptr,(png_rw_ptr)user_write_data,
102 (png_flush_ptr)user_flush_data);
103 /* png_init_io(png_ptr, fptr); */
104 /* png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); */
106 /* Set the image size, colortype, filter type, etc... */
108 /* printf("SAGTsettingIHDR %d %d %d\n",*width,*height,bit_depth); */
109 bit_depth=*nbits;
110 color_type=PNG_COLOR_TYPE_GRAY;
111 if (*nbits == 24 ) {
112 bit_depth=8;
113 color_type=PNG_COLOR_TYPE_RGB;
115 else if (*nbits == 32 ) {
116 bit_depth=8;
117 color_type=PNG_COLOR_TYPE_RGB_ALPHA;
119 png_set_IHDR(png_ptr, info_ptr, *width, *height,
120 bit_depth, color_type, PNG_INTERLACE_NONE,
121 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
123 /* Put image data into the PNG info structure */
125 /*bytes=bit_depth/8;*/
126 bytes=*nbits/8;
127 row_pointers=malloc((*height)*sizeof(png_bytep));
128 for (j=0;j<*height;j++) row_pointers[j]=(png_bytep *)(data+(j*(*width)*bytes));
129 png_set_rows(png_ptr, info_ptr, (png_bytepp)row_pointers);
131 /* Do the PNG encoding, and write out PNG stream */
133 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
135 /* Clean up */
137 png_destroy_write_struct(&png_ptr, &info_ptr);
138 free(row_pointers);
139 pnglen=write_io_ptr.stream_len;
140 #endif /* USE_PNG */
141 return pnglen;