forked from scantailor/scantailor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JpegMetadataLoader.cpp
275 lines (220 loc) · 6.3 KB
/
JpegMetadataLoader.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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/*
Scan Tailor - Interactive post-processing tool for scanned pages.
Copyright (C) 2007-2009 Joseph Artsimovich <joseph_a@mail.ru>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "JpegMetadataLoader.h"
#include "ImageMetadata.h"
#include "NonCopyable.h"
#include "Dpi.h"
#include "Dpm.h"
#include <QIODevice>
#include <QSize>
#include <QDebug>
#include <setjmp.h>
#include <string.h>
#include <assert.h>
extern "C" {
#include <jpeglib.h>
}
namespace
{
/*======================== JpegDecompressionHandle =======================*/
class JpegDecompressHandle
{
DECLARE_NON_COPYABLE(JpegDecompressHandle)
public:
JpegDecompressHandle(jpeg_error_mgr* err_mgr, jpeg_source_mgr* src_mgr);
~JpegDecompressHandle();
jpeg_decompress_struct* ptr() { return &m_info; }
jpeg_decompress_struct* operator->() { return &m_info; }
private:
jpeg_decompress_struct m_info;
};
JpegDecompressHandle::JpegDecompressHandle(
jpeg_error_mgr* err_mgr, jpeg_source_mgr* src_mgr)
{
m_info.err = err_mgr;
jpeg_create_decompress(&m_info);
m_info.src = src_mgr;
}
JpegDecompressHandle::~JpegDecompressHandle()
{
jpeg_destroy_decompress(&m_info);
}
/*============================ JpegSourceManager =========================*/
class JpegSourceManager : public jpeg_source_mgr
{
DECLARE_NON_COPYABLE(JpegSourceManager)
public:
JpegSourceManager(QIODevice& io_device);
private:
static void initSource(j_decompress_ptr cinfo);
static boolean fillInputBuffer(j_decompress_ptr cinfo);
boolean fillInputBufferImpl();
static void skipInputData(j_decompress_ptr cinfo, long num_bytes);
void skipInputDataImpl(long num_bytes);
static void termSource(j_decompress_ptr cinfo);
static JpegSourceManager* object(j_decompress_ptr cinfo);
QIODevice& m_rDevice;
JOCTET m_buf[4096];
};
JpegSourceManager::JpegSourceManager(QIODevice& io_device)
: m_rDevice(io_device)
{
init_source = &JpegSourceManager::initSource;
fill_input_buffer = &JpegSourceManager::fillInputBuffer;
skip_input_data = &JpegSourceManager::skipInputData;
resync_to_restart = &jpeg_resync_to_restart;
term_source = &JpegSourceManager::termSource;
bytes_in_buffer = 0;
next_input_byte = m_buf;
}
void
JpegSourceManager::initSource(j_decompress_ptr cinfo)
{
// No-op.
}
boolean
JpegSourceManager::fillInputBuffer(j_decompress_ptr cinfo)
{
return object(cinfo)->fillInputBufferImpl();
}
boolean
JpegSourceManager::fillInputBufferImpl()
{
qint64 const bytes_read = m_rDevice.read((char*)m_buf, sizeof(m_buf));
if (bytes_read > 0) {
bytes_in_buffer = bytes_read;
} else {
// Insert a fake EOI marker.
m_buf[0] = 0xFF;
m_buf[1] = JPEG_EOI;
bytes_in_buffer = 2;
}
next_input_byte = m_buf;
return 1;
}
void
JpegSourceManager::skipInputData(j_decompress_ptr cinfo, long num_bytes)
{
object(cinfo)->skipInputDataImpl(num_bytes);
}
void
JpegSourceManager::skipInputDataImpl(long num_bytes)
{
if (num_bytes <= 0) {
return;
}
while (num_bytes > (long)bytes_in_buffer) {
num_bytes -= (long)bytes_in_buffer;
fillInputBufferImpl();
}
next_input_byte += num_bytes;
bytes_in_buffer -= num_bytes;
}
void
JpegSourceManager::termSource(j_decompress_ptr cinfo)
{
// No-op.
}
JpegSourceManager*
JpegSourceManager::object(j_decompress_ptr cinfo)
{
return static_cast<JpegSourceManager*>(cinfo->src);
}
/*============================= JpegErrorManager ===========================*/
class JpegErrorManager : public jpeg_error_mgr
{
DECLARE_NON_COPYABLE(JpegErrorManager)
public:
JpegErrorManager();
jmp_buf& jmpBuf() { return m_jmpBuf; }
private:
static void errorExit(j_common_ptr cinfo);
static JpegErrorManager* object(j_common_ptr cinfo);
jmp_buf m_jmpBuf;
};
JpegErrorManager::JpegErrorManager()
{
jpeg_std_error(this);
error_exit = &JpegErrorManager::errorExit;
}
void
JpegErrorManager::errorExit(j_common_ptr cinfo)
{
longjmp(object(cinfo)->jmpBuf(), 1);
}
JpegErrorManager*
JpegErrorManager::object(j_common_ptr cinfo)
{
return static_cast<JpegErrorManager*>(cinfo->err);
}
} // anonymous namespace
/*============================= JpegMetadataLoader ==========================*/
void
JpegMetadataLoader::registerMyself()
{
static bool registered = false;
if (!registered) {
ImageMetadataLoader::registerLoader(
IntrusivePtr<ImageMetadataLoader>(new JpegMetadataLoader)
);
registered = true;
}
}
ImageMetadataLoader::Status
JpegMetadataLoader::loadMetadata(
QIODevice& io_device,
VirtualFunction1<void, ImageMetadata const&>& out)
{
if (!io_device.isReadable()) {
return GENERIC_ERROR;
}
static unsigned char const jpeg_signature[] = { 0xff, 0xd8, 0xff };
static int const sig_size = sizeof(jpeg_signature);
unsigned char signature[sig_size];
if (io_device.peek((char*)signature, sig_size) != sig_size) {
return FORMAT_NOT_RECOGNIZED;
}
if (memcmp(jpeg_signature, signature, sig_size) != 0) {
return FORMAT_NOT_RECOGNIZED;
}
JpegErrorManager err_mgr;
if (setjmp(err_mgr.jmpBuf())) {
// Returning from longjmp().
return GENERIC_ERROR;
}
JpegSourceManager src_mgr(io_device);
JpegDecompressHandle cinfo(&err_mgr, &src_mgr);
int const header_status = jpeg_read_header(cinfo.ptr(), 0);
if (header_status == JPEG_HEADER_TABLES_ONLY) {
return NO_IMAGES;
}
// The other possible value is JPEG_SUSPENDED, but we never suspend it.
assert(header_status == JPEG_HEADER_OK);
if (!jpeg_start_decompress(cinfo.ptr())) {
// libjpeg doesn't support all compression types.
return GENERIC_ERROR;
}
QSize const size(cinfo->image_width, cinfo->image_height);
Dpi dpi;
if (cinfo->density_unit == 1) {
// Dots per inch.
dpi = Dpi(cinfo->X_density, cinfo->Y_density);
} else if (cinfo->density_unit == 2) {
// Dots per centimeter.
dpi = Dpm(cinfo->X_density * 100, cinfo->Y_density * 100);
}
out(ImageMetadata(size, dpi));
return LOADED;
}