14 #if defined _UNDERSCORE
15 #define enc_png enc_png_
16 #elif defined _DOUBLEUNDERSCORE
17 #define enc_png enc_png__
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
40 mem
=(png_stream
*)png_get_io_ptr(png_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
60 int enc_png(char *data
,g2int
*width
,g2int
*height
,g2int
*nbits
,char *pngbuf
)
65 g2int j
,bytes
,bit_depth
;
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
,
79 info_ptr
= png_create_info_struct(png_ptr
);
82 png_destroy_write_struct(&png_ptr
,(png_infopp
)NULL
);
86 /* Set Error callback */
88 if (setjmp(png_jmpbuf(png_ptr
)))
90 png_destroy_write_struct(&png_ptr
, &info_ptr
);
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); */
110 color_type
=PNG_COLOR_TYPE_GRAY
;
113 color_type
=PNG_COLOR_TYPE_RGB
;
115 else if (*nbits
== 32 ) {
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;*/
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
);
137 png_destroy_write_struct(&png_ptr
, &info_ptr
);
139 pnglen
=write_io_ptr
.stream_len
;