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.
20 #ifdef FBV_SUPPORT_JPEG
22 #include <sys/types.h>
31 struct r_jpeg_error_mgr
33 struct jpeg_error_mgr pub
;
38 int fh_jpeg_id(char *name
)
42 fd
=open(name
,O_RDONLY
); if(fd
==-1) return(0);
45 if(id
[6]=='J' && id
[7]=='F' && id
[8]=='I' && id
[9]=='F') return(1);
46 if(id
[0]==0xff && id
[1]==0xd8 && id
[2]==0xff) return(1);
51 void jpeg_cb_error_exit(j_common_ptr cinfo
)
53 struct r_jpeg_error_mgr
*mptr
;
54 mptr
=(struct r_jpeg_error_mgr
*) cinfo
->err
;
55 (*cinfo
->err
->output_message
) (cinfo
);
56 longjmp(mptr
->envbuffer
,1);
59 int fh_jpeg_load(char *filename
,unsigned char *buffer
, unsigned char ** alpha
, int x
,int y
)
61 struct jpeg_decompress_struct cinfo
;
62 struct jpeg_decompress_struct
*ciptr
;
63 struct r_jpeg_error_mgr emgr
;
70 if(!(fh
=fopen(filename
,"rb"))) return(FH_ERROR_FILE
);
71 ciptr
->err
=jpeg_std_error(&emgr
.pub
);
72 emgr
.pub
.error_exit
=jpeg_cb_error_exit
;
73 if(setjmp(emgr
.envbuffer
)==1)
75 // FATAL ERROR - Free the object and return...
76 jpeg_destroy_decompress(ciptr
);
78 return(FH_ERROR_FORMAT
);
81 jpeg_create_decompress(ciptr
);
82 jpeg_stdio_src(ciptr
,fh
);
83 jpeg_read_header(ciptr
,TRUE
);
84 ciptr
->out_color_space
=JCS_RGB
;
85 jpeg_start_decompress(ciptr
);
87 px
=ciptr
->output_width
; py
=ciptr
->output_height
;
88 c
=ciptr
->output_components
;
93 lb
=(*ciptr
->mem
->alloc_small
)((j_common_ptr
) ciptr
,JPOOL_PERMANENT
,c
*px
);
95 while (ciptr
->output_scanline
< ciptr
->output_height
)
97 jpeg_read_scanlines(ciptr
, &lb
, 1);
103 jpeg_finish_decompress(ciptr
);
104 jpeg_destroy_decompress(ciptr
);
109 int fh_jpeg_getsize(char *filename
,int *x
,int *y
)
111 struct jpeg_decompress_struct cinfo
;
112 struct jpeg_decompress_struct
*ciptr
;
113 struct r_jpeg_error_mgr emgr
;
118 if(!(fh
=fopen(filename
,"rb"))) return(FH_ERROR_FILE
);
120 ciptr
->err
=jpeg_std_error(&emgr
.pub
);
121 emgr
.pub
.error_exit
=jpeg_cb_error_exit
;
122 if(setjmp(emgr
.envbuffer
)==1)
124 // FATAL ERROR - Free the object and return...
125 jpeg_destroy_decompress(ciptr
);
127 return(FH_ERROR_FORMAT
);
130 jpeg_create_decompress(ciptr
);
131 jpeg_stdio_src(ciptr
,fh
);
132 jpeg_read_header(ciptr
,TRUE
);
133 ciptr
->out_color_space
=JCS_RGB
;
134 jpeg_start_decompress(ciptr
);
135 px
=ciptr
->output_width
; py
=ciptr
->output_height
;
136 c
=ciptr
->output_components
;
138 jpeg_destroy_decompress(ciptr
);