15 #if defined _UNDERSCORE
16 #define enc_png enc_png_
17 #elif defined _DOUBLEUNDERSCORE
18 #define enc_png enc_png__
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
41 mem
=(png_stream
*)png_get_io_ptr(png_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
61 int enc_png(char *data
,g2int
*width
,g2int
*height
,g2int
*nbits
,char *pngbuf
)
66 g2int j
,bytes
,bit_depth
;
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
,
80 info_ptr
= png_create_info_struct(png_ptr
);
83 png_destroy_write_struct(&png_ptr
,(png_infopp
)NULL
);
87 /* Set Error callback */
89 if (setjmp(png_jmpbuf(png_ptr
)))
91 png_destroy_write_struct(&png_ptr
, &info_ptr
);
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); */
111 color_type
=PNG_COLOR_TYPE_GRAY
;
114 color_type
=PNG_COLOR_TYPE_RGB
;
116 else if (*nbits
== 32 ) {
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;*/
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
);
138 png_destroy_write_struct(&png_ptr
, &info_ptr
);
140 pnglen
=write_io_ptr
.stream_len
;