2 fbv -- simple image viewer for the linux framebuffer
3 Copyright (C) 2000, 2001, 2003 Mateusz Golicz
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #ifdef FBV_SUPPORT_PNG
24 #include <sys/types.h>
31 #define PNG_BYTES_TO_CHECK 4
33 #define min(x,y) ((x) < (y) ? (x) : (y))
36 int fh_png_id(char *name
)
40 fd
=open(name
,O_RDONLY
); if(fd
==-1) return(0);
43 if(id
[1]=='P' && id
[2]=='N' && id
[3]=='G') return(1);
48 int fh_png_load(char *name
,unsigned char *buffer
, unsigned char ** alpha
,int x
,int y
)
52 png_uint_32 width
, height
;
54 int bit_depth
, color_type
, interlace_type
;
55 int number_passes
,pass
, trans
= 0;
61 if(!(fh
=fopen(name
,"rb"))) return(FH_ERROR_FILE
);
63 png_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
,NULL
,NULL
,NULL
);
64 if (png_ptr
== NULL
) return(FH_ERROR_FORMAT
);
65 info_ptr
= png_create_info_struct(png_ptr
);
68 png_destroy_read_struct(&png_ptr
, (png_infopp
)NULL
, (png_infopp
)NULL
);
69 fclose(fh
); return(FH_ERROR_FORMAT
);
72 if (setjmp(png_ptr
->jmpbuf
))
74 png_destroy_read_struct(&png_ptr
, &info_ptr
, (png_infopp
)NULL
);
76 fclose(fh
); return(FH_ERROR_FORMAT
);
79 png_init_io(png_ptr
,fh
);
81 png_read_info(png_ptr
, info_ptr
);
82 png_get_IHDR(png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,&interlace_type
, NULL
, NULL
);
83 if (color_type
== PNG_COLOR_TYPE_PALETTE
) png_set_expand(png_ptr
);
84 if (bit_depth
< 8) png_set_packing(png_ptr
);
85 if (color_type
== PNG_COLOR_TYPE_GRAY
|| color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
) png_set_gray_to_rgb(png_ptr
);
86 if (png_get_valid(png_ptr
, info_ptr
, PNG_INFO_tRNS
))
89 png_set_tRNS_to_alpha(png_ptr
);
92 if(bit_depth
== 16) png_set_strip_16(png_ptr
);
93 number_passes
= png_set_interlace_handling(png_ptr
);
94 png_read_update_info(png_ptr
,info_ptr
);
96 if (color_type
== PNG_COLOR_TYPE_GRAY_ALPHA
|| color_type
== PNG_COLOR_TYPE_RGB_ALPHA
|| trans
)
98 unsigned char * alpha_buffer
= (unsigned char*) malloc(width
* height
);
101 rp
= (char*) malloc(width
* 4);
102 rptr
[0] = (png_bytep
) rp
;
104 *alpha
= alpha_buffer
;
106 for (pass
= 0; pass
< number_passes
; pass
++)
111 for(i
=0; i
<height
; i
++)
114 unsigned char *trp
= rp
;
116 png_read_rows(png_ptr
, rptr
, NULL
, 1);
118 for(n
= 0; n
< width
; n
++, fbptr
+= 3, trp
+= 4)
120 memcpy(fbptr
, trp
, 3);
129 for (pass
= 0; pass
< number_passes
; pass
++)
132 for(i
=0; i
<height
; i
++, fbptr
+= width
*3)
134 rptr
[0] = (png_bytep
) fbptr
;
135 png_read_rows(png_ptr
, rptr
, NULL
, 1);
139 png_read_end(png_ptr
, info_ptr
);
140 png_destroy_read_struct(&png_ptr
, &info_ptr
, (png_infopp
)NULL
);
144 int fh_png_getsize(char *name
,int *x
,int *y
)
148 png_uint_32 width
, height
;
149 int bit_depth
, color_type
, interlace_type
;
153 if(!(fh
=fopen(name
,"rb"))) return(FH_ERROR_FILE
);
155 png_ptr
= png_create_read_struct(PNG_LIBPNG_VER_STRING
,NULL
,NULL
,NULL
);
156 if (png_ptr
== NULL
) return(FH_ERROR_FORMAT
);
157 info_ptr
= png_create_info_struct(png_ptr
);
158 if (info_ptr
== NULL
)
160 png_destroy_read_struct(&png_ptr
, (png_infopp
)NULL
, (png_infopp
)NULL
);
161 fclose(fh
); return(FH_ERROR_FORMAT
);
164 if (setjmp(png_ptr
->jmpbuf
))
166 png_destroy_read_struct(&png_ptr
, &info_ptr
, (png_infopp
)NULL
);
168 fclose(fh
); return(FH_ERROR_FORMAT
);
171 png_init_io(png_ptr
,fh
);
172 png_read_info(png_ptr
, info_ptr
);
173 png_get_IHDR(png_ptr
, info_ptr
, &width
, &height
, &bit_depth
, &color_type
,&interlace_type
, NULL
, NULL
);
174 png_destroy_read_struct(&png_ptr
, &info_ptr
, (png_infopp
)NULL
);