-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjpeg.cpp
179 lines (157 loc) · 4.24 KB
/
jpeg.cpp
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <setjmp.h>
#include "jpeg.h"
#undef HAVE_STDLIB_H // -Werror complain
extern "C" {
#include <jpeglib.h>
}
#define MIN(a,b) ((a)>(b)?(b):(a))
struct r_jpeg_error_mgr
{
struct jpeg_error_mgr pub;
jmp_buf envbuffer;
};
int jpeg_id(const char *name)
{
// dbout("jpeg_id {\n");
int fd;
unsigned char id[10];
fd=open(name,O_RDONLY); if(fd==-1) return(0);
read(fd,id,10);
close(fd);
// dbout("jpeg_id }\n");
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)
{
// dbout("jpeg_cd_error_exit {\n");
struct r_jpeg_error_mgr *mptr;
mptr=(struct r_jpeg_error_mgr*) cinfo->err;
(*cinfo->err->output_message) (cinfo);
longjmp(mptr->envbuffer,1);
// dbout("jpeg_cd_error_exit }\n");
}
int jpeg_load(const char *filename, unsigned char **buffer, int* x, int* y)
{
//dbout("jpeg_load_local (%s/%d/%d) {\n",basename(filename),*x,*y);
struct jpeg_decompress_struct cinfo;
struct jpeg_decompress_struct *ciptr;
struct r_jpeg_error_mgr emgr;
unsigned char *bp;
int px,py,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)==1)
{
// FATAL ERROR - Free the object and return...
jpeg_destroy_decompress(ciptr);
fclose(fh);
// dbout("jpeg_load } - FATAL ERROR\n");
return(FH_ERROR_FORMAT);
}
jpeg_create_decompress(ciptr);
jpeg_stdio_src(ciptr,fh);
jpeg_read_header(ciptr,TRUE);
ciptr->out_color_space=JCS_RGB;
ciptr->dct_method=JDCT_FASTEST;
if(*x==(int)ciptr->image_width)
ciptr->scale_denom=1;
else if(abs(*x*2 - ciptr->image_width) < 2)
ciptr->scale_denom=2;
else if(abs(*x*4 - ciptr->image_width) < 4)
ciptr->scale_denom=4;
else if(abs(*x*8 - ciptr->image_width) < 8)
ciptr->scale_denom=8;
else
ciptr->scale_denom=1;
jpeg_start_decompress(ciptr);
px=ciptr->output_width; py=ciptr->output_height;
c=ciptr->output_components;
if(px > *x || py > *y)
{
// pic act larger, e.g. because of not responding jpeg server
free(*buffer);
*buffer = (unsigned char*) malloc(px*py*3);
*x = px;
*y = py;
}
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);
memmove(bp,lb,px*c);
bp+=px*c;
}
}
jpeg_finish_decompress(ciptr);
jpeg_destroy_decompress(ciptr);
fclose(fh);
//dbout("jpeg_load_local }\n");
return(FH_ERROR_OK);
}
int jpeg_getsize(const char *filename,int *x,int *y, int wanted_width, int wanted_height)
{
// dbout("jpeg_getsize {\n");
struct jpeg_decompress_struct cinfo;
struct jpeg_decompress_struct *ciptr;
struct r_jpeg_error_mgr emgr;
int px,py/*,c*/;
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)==1)
{
// FATAL ERROR - Free the object and return...
jpeg_destroy_decompress(ciptr);
fclose(fh);
// dbout("jpeg_getsize } - FATAL ERROR\n");
return(FH_ERROR_FORMAT);
}
jpeg_create_decompress(ciptr);
jpeg_stdio_src(ciptr,fh);
jpeg_read_header(ciptr,TRUE);
ciptr->out_color_space=JCS_RGB;
// should be more flexible...
if((int)ciptr->image_width/8 >= wanted_width ||
(int)ciptr->image_height/8 >= wanted_height)
ciptr->scale_denom=8;
else if((int)ciptr->image_width/4 >= wanted_width ||
(int)ciptr->image_height/4 >= wanted_height)
ciptr->scale_denom=4;
else if((int)ciptr->image_width/2 >= wanted_width ||
(int)ciptr->image_height/2 >= wanted_height)
ciptr->scale_denom=2;
else
ciptr->scale_denom=1;
jpeg_start_decompress(ciptr);
px=ciptr->output_width; py=ciptr->output_height;
// c=ciptr->output_components;
*x=px;
*y=py;
// jpeg_finish_decompress(ciptr);
jpeg_destroy_decompress(ciptr);
fclose(fh);
// dbout("jpeg_getsize }\n");
return(FH_ERROR_OK);
}