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
32 mem
=(png_stream
*)png_get_io_ptr(png_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
51 int ENC_PNG(char *data
,g2int
*width
,g2int
*height
,g2int
*nbits
,char *pngbuf
)
55 g2int j
,bytes
,pnglen
,bit_depth
;
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
,
69 info_ptr
= png_create_info_struct(png_ptr
);
72 png_destroy_write_struct(&png_ptr
,(png_infopp
)NULL
);
76 /* Set Error callback */
78 if (setjmp(png_jmpbuf(png_ptr
)))
80 png_destroy_write_struct(&png_ptr
, &info_ptr
);
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); */
100 color_type
=PNG_COLOR_TYPE_GRAY
;
103 color_type
=PNG_COLOR_TYPE_RGB
;
105 else if (*nbits
== 32 ) {
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;*/
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
);
127 png_destroy_write_struct(&png_ptr
, &info_ptr
);
129 pnglen
=write_io_ptr
.stream_len
;