-
Notifications
You must be signed in to change notification settings - Fork 1
/
airscan-jpeg.c
225 lines (183 loc) · 5.86 KB
/
airscan-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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* AirScan (a.k.a. eSCL) backend for SANE
*
* Copyright (C) 2019 and up by Alexander Pevzner ([email protected])
* See LICENSE for license terms and conditions
*
* JPEG image decoder
*/
#include "airscan.h"
#include <jpeglib.h>
#include <setjmp.h>
#include <string.h>
/* JPEG image decoder
*/
typedef struct {
image_decoder decoder; /* Base class */
struct jpeg_decompress_struct cinfo; /* libjpeg decoder */
struct jpeg_error_mgr jerr; /* libjpeg error manager */
jmp_buf jmpb; /* For longjmp from libjpeg */
char errbuf[ /* Error buffer */
JMSG_LENGTH_MAX + 16];
JDIMENSION num_lines; /* Num of lines left to read */
} image_decoder_jpeg;
/* Free JPEG decoder
*/
static void
image_decoder_jpeg_free (image_decoder *decoder)
{
image_decoder_jpeg *jpeg = (image_decoder_jpeg*) decoder;
jpeg_destroy_decompress(&jpeg->cinfo);
mem_free(jpeg);
}
/* Begin JPEG decoding
*/
static error
image_decoder_jpeg_begin (image_decoder *decoder, const void *data,
size_t size)
{
image_decoder_jpeg *jpeg = (image_decoder_jpeg*) decoder;
int rc;
if (!setjmp(jpeg->jmpb)) {
jpeg_mem_src(&jpeg->cinfo, (unsigned char*) data, size);
rc = jpeg_read_header(&jpeg->cinfo, true);
if (rc != JPEG_HEADER_OK) {
jpeg_abort((j_common_ptr) &jpeg->cinfo);
return ERROR("JPEG: invalid header");
}
if (jpeg->cinfo.num_components != 1) {
jpeg->cinfo.out_color_space = JCS_RGB;
}
jpeg_start_decompress(&jpeg->cinfo);
jpeg->num_lines = jpeg->cinfo.image_height;
return NULL;
}
return ERROR(jpeg->errbuf);
}
/* Reset JPEG decoder
*/
static void
image_decoder_jpeg_reset (image_decoder *decoder)
{
image_decoder_jpeg *jpeg = (image_decoder_jpeg*) decoder;
jpeg_abort((j_common_ptr) &jpeg->cinfo);
}
/* Get bytes count per pixel
*/
static int
image_decoder_jpeg_get_bytes_per_pixel (image_decoder *decoder)
{
image_decoder_jpeg *jpeg = (image_decoder_jpeg*) decoder;
return jpeg->cinfo.num_components;
}
/* Get image parameters
*/
static void
image_decoder_jpeg_get_params (image_decoder *decoder, SANE_Parameters *params)
{
image_decoder_jpeg *jpeg = (image_decoder_jpeg*) decoder;
params->last_frame = SANE_TRUE;
params->pixels_per_line = jpeg->cinfo.image_width;
params->lines = jpeg->cinfo.image_height;
params->depth = 8;
if (jpeg->cinfo.num_components == 1) {
params->format = SANE_FRAME_GRAY;
params->bytes_per_line = params->pixels_per_line;
} else {
params->format = SANE_FRAME_RGB;
params->bytes_per_line = params->pixels_per_line * 3;
}
}
/* Set clipping window
*/
static error
image_decoder_jpeg_set_window (image_decoder *decoder, image_window *win)
{
image_decoder_jpeg *jpeg = (image_decoder_jpeg*) decoder;
/* Note, image clipping cannot be supported on rather
* old libjpeg version (i.e., on Ubuntu 16.04, because
* jpeg_crop_scanline() and jpeg_skip_scanlines() functions
* are missed. The safe default is to update window to
* match the entire image dimensions.
*/
#if 1
win->x_off = win->y_off = 0;
win->wid = jpeg->cinfo.image_width;
win->hei = jpeg->cinfo.image_height;
return NULL;
#else
JDIMENSION x_off = win->x_off;
JDIMENSION wid = win->wid;
if (!setjmp(jpeg->jmpb)) {
jpeg_crop_scanline(&jpeg->cinfo, &x_off, &wid);
if (win->y_off > 0) {
jpeg_skip_scanlines(&jpeg->cinfo, win->y_off);
}
jpeg->num_lines = win->hei;
win->x_off = x_off;
win->wid = wid;
return NULL;
}
return ERROR(jpeg->errbuf);
#endif
}
/* Read next line of image
*/
static error
image_decoder_jpeg_read_line (image_decoder *decoder, void *buffer)
{
image_decoder_jpeg *jpeg = (image_decoder_jpeg*) decoder;
JSAMPROW lines[1] = {buffer};
if (!jpeg->num_lines) {
return ERROR("JPEG: end of file");
}
if (!setjmp(jpeg->jmpb)) {
if (jpeg_read_scanlines(&jpeg->cinfo, lines, 1) == 0) {
return ERROR(jpeg->errbuf);
}
jpeg->num_lines --;
return NULL;
}
return ERROR(jpeg->errbuf);
}
/* "Output error message" callback for JPEG decoder
*/
static void
image_decoder_jpeg_output_message (j_common_ptr cinfo)
{
image_decoder_jpeg *jpeg = OUTER_STRUCT(cinfo, image_decoder_jpeg, cinfo);
memcpy(jpeg->errbuf, "JPEG: ", 6);
(*cinfo->err->format_message)(cinfo, jpeg->errbuf + 6);
}
/* error_exit callback for JPEG decoder. The default callback
* terminates a program, which is not good for us
*/
static void
image_decoder_jpeg_error_exit (j_common_ptr cinfo)
{
image_decoder_jpeg *jpeg = OUTER_STRUCT(cinfo, image_decoder_jpeg, cinfo);
image_decoder_jpeg_output_message(cinfo);
jpeg_abort(cinfo);
longjmp(jpeg->jmpb, 1);
}
/* Create JPEG image decoder
*/
image_decoder*
image_decoder_jpeg_new (void)
{
image_decoder_jpeg *jpeg = mem_new(image_decoder_jpeg, 1);
jpeg->decoder.content_type = "image/jpeg";
jpeg->decoder.free = image_decoder_jpeg_free;
jpeg->decoder.begin = image_decoder_jpeg_begin;
jpeg->decoder.reset = image_decoder_jpeg_reset;
jpeg->decoder.get_bytes_per_pixel = image_decoder_jpeg_get_bytes_per_pixel;
jpeg->decoder.get_params = image_decoder_jpeg_get_params;
jpeg->decoder.set_window = image_decoder_jpeg_set_window;
jpeg->decoder.read_line = image_decoder_jpeg_read_line;
jpeg->cinfo.err = jpeg_std_error(&jpeg->jerr);
jpeg->jerr.output_message = image_decoder_jpeg_output_message;
jpeg->jerr.error_exit = image_decoder_jpeg_error_exit;
jpeg_create_decompress(&jpeg->cinfo);
return &jpeg->decoder;
}
/* vim:ts=8:sw=4:et
*/