17 unsigned char *stream_ptr;
42 ptr = mem->stream_ptr;
43 offset = mem->stream_len;
44 memcpy(ptr + offset, data, length);
45 mem->stream_len += length;
78 g2int j, bytes, pnglen, bit_depth;
81 png_bytep **row_pointers;
85 png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
90 info_ptr = png_create_info_struct(png_ptr);
93 png_destroy_write_struct(&png_ptr,(png_infopp)NULL);
98 if (setjmp(png_jmpbuf(png_ptr)))
100 png_destroy_write_struct(&png_ptr, &info_ptr);
105 write_io_ptr.stream_ptr = (png_voidp)pngbuf;
106 write_io_ptr.stream_len = 0;
109 png_set_write_fn(png_ptr, (png_voidp)&write_io_ptr, (png_rw_ptr)
user_write_data,
114 color_type = PNG_COLOR_TYPE_GRAY;
118 color_type = PNG_COLOR_TYPE_RGB;
120 else if (*nbits == 32)
123 color_type = PNG_COLOR_TYPE_RGB_ALPHA;
125 png_set_IHDR(png_ptr, info_ptr, *width, *height,
126 bit_depth, color_type, PNG_INTERLACE_NONE,
127 PNG_COMPRESSION_TYPE_DEFAULT, PNG_FILTER_TYPE_DEFAULT);
131 row_pointers = malloc((*height) *
sizeof(png_bytep));
132 for (
j = 0;
j < *height;
j++)
133 row_pointers[
j] = (png_bytep *)(data+(
j * (*width) * bytes));
134 png_set_rows(png_ptr, info_ptr, (png_bytepp)row_pointers);
137 png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
140 png_destroy_write_struct(&png_ptr, &info_ptr);
142 pnglen = write_io_ptr.stream_len;
165 ptr = (
void *)mem->stream_ptr;
166 offset = mem->stream_len;
167 memcpy(data, ptr + offset, length);
168 mem->stream_len += length;
190 int interlace, color, compres, filter, bit_depth;
191 g2int j, k, n, bytes, clen;
193 png_infop info_ptr, end_info;
194 png_bytepp row_pointers;
196 png_uint_32 h32, w32;
199 if (png_sig_cmp(pngbuf, 0, 8) != 0)
203 if (!(png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, (png_voidp)NULL,
207 if (!(info_ptr = png_create_info_struct(png_ptr)))
209 png_destroy_read_struct(&png_ptr, (png_infopp)NULL, (png_infopp)NULL);
213 if (!(end_info = png_create_info_struct(png_ptr)))
215 png_destroy_read_struct(&png_ptr, (png_infopp)info_ptr, (png_infopp)NULL);
220 if (setjmp(png_jmpbuf(png_ptr)))
222 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
227 read_io_ptr.stream_ptr = (png_voidp)pngbuf;
228 read_io_ptr.stream_len = 0;
231 png_set_read_fn(png_ptr, (png_voidp)&read_io_ptr, (png_rw_ptr)
user_read_data);
234 png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_IDENTITY, NULL);
237 row_pointers = png_get_rows(png_ptr, info_ptr);
240 (void)png_get_IHDR(png_ptr, info_ptr, &w32, &h32,
241 &bit_depth, &color, &interlace, &compres, &filter);
247 if ( color == PNG_COLOR_TYPE_RGB ) {
250 else if ( color == PNG_COLOR_TYPE_RGB_ALPHA ) {
256 bytes = bit_depth / 8;
257 clen = *width * bytes;
258 for (
j = 0;
j < *height;
j++) {
259 for (k = 0; k < clen; k++) {
260 cout[n] = *(row_pointers[
j]+k);
266 png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
int enc_png(char *data, g2int *width, g2int *height, g2int *nbits, char *pngbuf)
create png_structs to write png stream into memory.
int dec_png(unsigned char *pngbuf, g2int *width, g2int *height, char *cout)
Decode some PNG compressed data.
struct png_stream png_stream
location to write PNG stream
static void user_write_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
Custom write function used to that libpng will write to memory location instead of a file on disk.
static void user_read_data(png_structp png_ptr, png_bytep data, png_uint_32 length)
Custom read function used so that libpng will read a PNG stream from memory instead of a file on disk...
static void user_flush_data(png_structp png_ptr)
Dummy Custom flush function.