A little comment about a compilation matter: -lungif parameter is not usually correct...
[fbv.git] / jpeg.c
blob9dfeadc139caf8305a96f936a9d5d535a5b05923
1 /*
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.
19 #include "config.h"
20 #ifdef FBV_SUPPORT_JPEG
21 #include <stdio.h>
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <fcntl.h>
25 #include <jpeglib.h>
26 #include <setjmp.h>
27 #include <unistd.h>
28 #include <string.h>
29 #include "fbv.h"
31 struct r_jpeg_error_mgr
33 struct jpeg_error_mgr pub;
34 jmp_buf envbuffer;
38 int fh_jpeg_id(char *name)
40 int fd;
41 unsigned char id[10];
42 fd=open(name,O_RDONLY); if(fd==-1) return(0);
43 read(fd,id,10);
44 close(fd);
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);
47 return(0);
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;
64 unsigned char *bp;
65 int px,py,c;
66 FILE *fh;
67 JSAMPLE *lb;
69 ciptr=&cinfo;
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);
77 fclose(fh);
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;
91 if(c==3)
93 lb=(*ciptr->mem->alloc_small)((j_common_ptr) ciptr,JPOOL_PERMANENT,c*px);
94 bp=buffer;
95 while (ciptr->output_scanline < ciptr->output_height)
97 jpeg_read_scanlines(ciptr, &lb, 1);
98 memcpy(bp,lb,px*c);
99 bp+=px*c;
103 jpeg_finish_decompress(ciptr);
104 jpeg_destroy_decompress(ciptr);
105 fclose(fh);
106 return(FH_ERROR_OK);
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;
114 int px,py,c;
115 FILE *fh;
117 ciptr=&cinfo;
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);
126 fclose(fh);
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;
137 *x=px; *y=py;
138 jpeg_destroy_decompress(ciptr);
139 fclose(fh);
140 return(FH_ERROR_OK);
142 #endif