15 #if defined _UNDERSCORE
16 #define dec_png dec_png_
17 #elif defined _DOUBLEUNDERSCORE
18 #define dec_png dec_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_read_data(png_structp
, png_bytep
, png_uint_32
);
30 void user_read_data(png_structp png_ptr
,png_bytep data
, png_uint_32 length
)
32 Custom read function used so that libpng will read a PNG stream
33 from memory instead of a file on disk.
40 mem
=(png_stream
*)png_get_io_ptr(png_ptr
);
41 ptr
=(void *)mem
->stream_ptr
;
42 offset
=mem
->stream_len
;
43 /* printf("SAGrd %ld %ld %x\n",offset,length,ptr); */
44 memcpy(data
,ptr
+offset
,length
);
45 mem
->stream_len
+= length
;
51 int dec_png(unsigned char *pngbuf
,g2int
*width
,g2int
*height
,char *cout
)
54 int interlace
,color
,compres
,filter
,bit_depth
;
55 g2int j
,k
,n
,bytes
,clen
;
57 png_infop info_ptr
,end_info
;
58 png_bytepp row_pointers
;
59 png_stream read_io_ptr
;
62 /* check if stream is a valid PNG format */
64 if ( png_sig_cmp(pngbuf
,0,8) != 0)
67 /* create and initialize png_structs */
69 png_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
, (png_voidp
)NULL
,
74 info_ptr
= png_create_info_struct(png_ptr
);
77 png_destroy_read_struct(&png_ptr
,(png_infopp
)NULL
,(png_infopp
)NULL
);
81 end_info
= png_create_info_struct(png_ptr
);
84 png_destroy_read_struct(&png_ptr
,(png_infopp
)info_ptr
,(png_infopp
)NULL
);
88 /* Set Error callback */
90 if (setjmp(png_jmpbuf(png_ptr
)))
92 png_destroy_read_struct(&png_ptr
, &info_ptr
,&end_info
);
96 /* Initialize info for reading PNG stream from memory */
98 read_io_ptr
.stream_ptr
=(png_voidp
)pngbuf
;
99 read_io_ptr
.stream_len
=0;
101 /* Set new custom read function */
103 png_set_read_fn(png_ptr
,(png_voidp
)&read_io_ptr
,(png_rw_ptr
)user_read_data
);
104 /* png_init_io(png_ptr, fptr); */
106 /* Read and decode PNG stream */
108 png_read_png(png_ptr
, info_ptr
, PNG_TRANSFORM_IDENTITY
, NULL
);
110 /* Get pointer to each row of image data */
112 row_pointers
= png_get_rows(png_ptr
, info_ptr
);
114 /* Get image info, such as size, depth, colortype, etc... */
116 /*printf("SAGT:png %d %d %d\n",info_ptr->width,info_ptr->height,info_ptr->bit_depth);*/
117 /* (void)png_get_IHDR(png_ptr, info_ptr, (png_uint_32 *)width, (png_uint_32 *)height,
118 &bit_depth, &color, &interlace, &compres, &filter);*/
119 (void)png_get_IHDR(png_ptr
, info_ptr
, &w32
, &h32
,
120 &bit_depth
, &color
, &interlace
, &compres
, &filter
);
125 /* Check if image was grayscale */
128 if (color != PNG_COLOR_TYPE_GRAY ) {
129 fprintf(stderr,"dec_png: Grayscale image was expected. \n");
132 if ( color
== PNG_COLOR_TYPE_RGB
) {
135 else if ( color
== PNG_COLOR_TYPE_RGB_ALPHA
) {
138 /* Copy image data to output string */
143 for (j
=0;j
<*height
;j
++) {
144 for (k
=0;k
<clen
;k
++) {
145 cout
[n
]=*(row_pointers
[j
]+k
);
152 png_destroy_read_struct(&png_ptr
, &info_ptr
, &end_info
);