updated top-level README and version_decl for V4.4.2 (#1795)
[WRF.git] / external / io_grib2 / g2lib / enc_png.c
blob7d2ef1d287bb998cb6991873d23d2b085f01d1f5
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <png.h>
5 #include "proto.h"
7 #ifdef __64BIT__
8 typedef int g2int;
9 #else
10 typedef long g2int;
11 #endif
13 struct png_stream {
14 unsigned char *stream_ptr; /* location to write PNG stream */
15 g2int stream_len; /* number of bytes written */
17 typedef struct png_stream png_stream;
19 void user_write_data(png_structp ,png_bytep , png_uint_32 );
20 void user_flush_data(png_structp );
22 void user_write_data(png_structp png_ptr,png_bytep data, png_uint_32 length)
24 Custom write function used to that libpng will write
25 to memory location instead of a file on disk
28 unsigned char *ptr;
29 g2int offset;
30 png_stream *mem;
32 mem=(png_stream *)png_get_io_ptr(png_ptr);
33 ptr=mem->stream_ptr;
34 offset=mem->stream_len;
35 /* printf("SAGwr %ld %ld %x\n",offset,length,ptr); */
36 /*for (j=offset,k=0;k<length;j++,k++) ptr[j]=data[k];*/
37 memcpy(ptr+offset,data,length);
38 mem->stream_len += length;
42 void user_flush_data(png_structp png_ptr)
44 Dummy Custom flush function
47 int *do_nothing=NULL;
51 int ENC_PNG(char *data,g2int *width,g2int *height,g2int *nbits,char *pngbuf)
54 int color_type;
55 g2int j,bytes,pnglen,bit_depth;
56 png_structp png_ptr;
57 png_infop info_ptr;
58 // png_bytep *row_pointers[*height];
59 png_bytep **row_pointers;
60 png_stream write_io_ptr;
62 /* create and initialize png_structs */
64 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
65 NULL, NULL);
66 if (!png_ptr)
67 return (-1);
69 info_ptr = png_create_info_struct(png_ptr);
70 if (!info_ptr)
72 png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
73 return (-2);
76 /* Set Error callback */
78 if (setjmp(png_jmpbuf(png_ptr)))
80 png_destroy_write_struct(&png_ptr, &info_ptr);
81 return (-3);
84 /* Initialize info for writing PNG stream to memory */
86 write_io_ptr.stream_ptr=(png_voidp)pngbuf;
87 write_io_ptr.stream_len=0;
89 /* Set new custom write functions */
91 png_set_write_fn(png_ptr,(voidp)&write_io_ptr,(png_rw_ptr)user_write_data,
92 (png_flush_ptr)user_flush_data);
93 /* png_init_io(png_ptr, fptr); */
94 /* png_set_compression_level(png_ptr, Z_BEST_COMPRESSION); */
96 /* Set the image size, colortype, filter type, etc... */
98 /* printf("SAGTsettingIHDR %d %d %d\n",*width,*height,bit_depth); */
99 bit_depth=*nbits;
100 color_type=PNG_COLOR_TYPE_GRAY;
101 if (*nbits == 24 ) {
102 bit_depth=8;
103 color_type=PNG_COLOR_TYPE_RGB;
105 else if (*nbits == 32 ) {
106 bit_depth=8;
107 color_type=PNG_COLOR_TYPE_RGB_ALPHA;
109 png_set_IHDR(png_ptr, info_ptr, *width, *height,
110 bit_depth, color_type, PNG_INTERLACE_NONE,
111 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
113 /* Put image data into the PNG info structure */
115 /*bytes=bit_depth/8;*/
116 bytes=*nbits/8;
117 row_pointers=malloc((*height)*sizeof(png_bytep));
118 for (j=0;j<*height;j++) row_pointers[j]=(png_bytep *)(data+(j*(*width)*bytes));
119 png_set_rows(png_ptr, info_ptr, (png_bytepp)row_pointers);
121 /* Do the PNG encoding, and write out PNG stream */
123 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
125 /* Clean up */
127 png_destroy_write_struct(&png_ptr, &info_ptr);
128 free(row_pointers);
129 pnglen=write_io_ptr.stream_len;
130 return pnglen;