-
Notifications
You must be signed in to change notification settings - Fork 40
/
jpeg.c
123 lines (107 loc) · 2.62 KB
/
jpeg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#include "config.h"
#ifdef FBV_SUPPORT_JPEG
#include "fbv.h"
#include <stdio.h>
#include <string.h>
#include <jpeglib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <setjmp.h>
#include <unistd.h>
struct r_jpeg_error_mgr
{
struct jpeg_error_mgr pub;
jmp_buf envbuffer;
};
int fh_jpeg_id(char *name)
{
int fd;
unsigned char id[10];
fd = open(name,O_RDONLY); if(fd==-1) return(0);
read(fd,id,10);
close(fd);
if(id[6]=='J' && id[7]=='F' && id[8]=='I' && id[9]=='F') return(1);
if(id[0]==0xff && id[1]==0xd8 && id[2]==0xff) return(1);
return(0);
}
void jpeg_cb_error_exit(j_common_ptr cinfo)
{
struct r_jpeg_error_mgr *mptr;
mptr = (struct r_jpeg_error_mgr*) cinfo->err;
(*cinfo->err->output_message) (cinfo);
longjmp(mptr->envbuffer, 1);
}
int fh_jpeg_load(char *filename, unsigned char *buffer, unsigned char ** alpha, int x, int y)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_decompress_struct *ciptr;
struct r_jpeg_error_mgr emgr;
unsigned char *bp;
int px, c;
FILE *fh;
JSAMPLE *lb;
ciptr = &cinfo;
if(!(fh=fopen(filename,"rb"))) return(FH_ERROR_FILE);
ciptr->err = jpeg_std_error(&emgr.pub);
emgr.pub.error_exit = jpeg_cb_error_exit;
if(setjmp(emgr.envbuffer))
{
jpeg_destroy_decompress(ciptr);
fclose(fh);
return(FH_ERROR_FORMAT);
}
jpeg_create_decompress(ciptr);
jpeg_stdio_src(ciptr, fh);
jpeg_read_header(ciptr, TRUE);
ciptr->out_color_space = JCS_RGB;
jpeg_start_decompress(ciptr);
px = ciptr->output_width;
c = ciptr->output_components;
if(c==3)
{
lb = (JSAMPLE*)(*ciptr->mem->alloc_small)((j_common_ptr)ciptr, JPOOL_PERMANENT, c*px);
bp = buffer;
while (ciptr->output_scanline < ciptr->output_height)
{
jpeg_read_scanlines(ciptr, &lb, 1);
memcpy(bp, lb, px*c);
bp += px*c;
}
}
jpeg_finish_decompress(ciptr);
jpeg_destroy_decompress(ciptr);
fclose(fh);
return(FH_ERROR_OK);
}
int fh_jpeg_getsize(char *filename, int *x, int *y)
{
struct jpeg_decompress_struct cinfo;
struct jpeg_decompress_struct *ciptr;
struct r_jpeg_error_mgr emgr;
int px, py;
FILE *fh;
ciptr = &cinfo;
if(!(fh=fopen(filename,"rb"))) return(FH_ERROR_FILE);
ciptr->err = jpeg_std_error(&emgr.pub);
emgr.pub.error_exit = jpeg_cb_error_exit;
if(setjmp(emgr.envbuffer))
{
jpeg_destroy_decompress(ciptr);
fclose(fh);
return(FH_ERROR_FORMAT);
}
jpeg_create_decompress(ciptr);
jpeg_stdio_src(ciptr, fh);
jpeg_read_header(ciptr, TRUE);
ciptr->out_color_space = JCS_RGB;
jpeg_start_decompress(ciptr);
px = ciptr->output_width;
py = ciptr->output_height;
*x = px;
*y = py;
jpeg_destroy_decompress(ciptr);
fclose(fh);
return(FH_ERROR_OK);
}
#endif /*FBV_SUPPORT_JPEG*/