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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
|
/****************************************************************************
**
** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the plugins of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:LGPL$
** No Commercial Usage
** This file contains pre-release code and may not be distributed.
** You may use this file in accordance with the terms and conditions
** contained in the either Technology Preview License Agreement or the
** Beta Release License Agreement.
**
** GNU Lesser General Public License Usage
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** In addition, as a special exception, Nokia gives you certain
** additional rights. These rights are described in the Nokia Qt LGPL
** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
** package.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3.0 as published by the Free Software
** Foundation and appearing in the file LICENSE.GPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU General Public License version 3.0 requirements will be
** met: http://www.gnu.org/copyleft/gpl.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
** $QT_END_LICENSE$
**
****************************************************************************/
#include "qtiffhandler.h"
#include <qvariant.h>
#include <qdebug.h>
#include <qimage.h>
#include <qglobal.h>
extern "C" {
#include "tiffio.h"
}
QT_BEGIN_NAMESPACE
tsize_t qtiffReadProc(thandle_t fd, tdata_t buf, tsize_t size)
{
QIODevice* device = static_cast<QTiffHandler*>(fd)->device();
return device->isReadable() ? device->read(static_cast<char *>(buf), size) : -1;
}
tsize_t qtiffWriteProc(thandle_t fd, tdata_t buf, tsize_t size)
{
return static_cast<QTiffHandler*>(fd)->device()->write(static_cast<char *>(buf), size);
}
toff_t qtiffSeekProc(thandle_t fd, toff_t off, int whence)
{
QIODevice *device = static_cast<QTiffHandler*>(fd)->device();
switch (whence) {
case SEEK_SET:
device->seek(off);
break;
case SEEK_CUR:
device->seek(device->pos() + off);
break;
case SEEK_END:
device->seek(device->size() + off);
break;
}
return device->pos();
}
int qtiffCloseProc(thandle_t /*fd*/)
{
return 0;
}
toff_t qtiffSizeProc(thandle_t fd)
{
return static_cast<QTiffHandler*>(fd)->device()->size();
}
int qtiffMapProc(thandle_t /*fd*/, tdata_t* /*pbase*/, toff_t* /*psize*/)
{
return 0;
}
void qtiffUnmapProc(thandle_t /*fd*/, tdata_t /*base*/, toff_t /*size*/)
{
}
QTiffHandler::QTiffHandler() : QImageIOHandler()
{
compression = NoCompression;
}
bool QTiffHandler::canRead() const
{
if (canRead(device())) {
setFormat("tiff");
return true;
}
return false;
}
bool QTiffHandler::canRead(QIODevice *device)
{
if (!device) {
qWarning("QTiffHandler::canRead() called with no device");
return false;
}
// current implementation uses TIFFClientOpen which needs to be
// able to seek, so sequential devices are not supported
QByteArray header = device->peek(4);
return header == QByteArray::fromRawData("\x49\x49\x2A\x00", 4)
|| header == QByteArray::fromRawData("\x4D\x4D\x00\x2A", 4);
}
bool QTiffHandler::read(QImage *image)
{
if (!canRead())
return false;
TIFF *tiff = TIFFClientOpen("foo",
"r",
this,
qtiffReadProc,
qtiffWriteProc,
qtiffSeekProc,
qtiffCloseProc,
qtiffSizeProc,
qtiffMapProc,
qtiffUnmapProc);
if (tiff) {
uint32 width = 0;
uint32 height = 0;
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height);
if (image->size() != QSize(width, height) || image->format() != QImage::Format_ARGB32)
*image = QImage(width, height, QImage::Format_ARGB32);
if (!image->isNull()) {
if (TIFFReadRGBAImageOriented(tiff, width, height, reinterpret_cast<uint32 *>(image->bits()), ORIENTATION_TOPLEFT, 0)) {
uint16 resUnit = RESUNIT_NONE;
float resX = 0;
float resY = 0;
TIFFGetField(tiff, TIFFTAG_RESOLUTIONUNIT, &resUnit);
TIFFGetField(tiff, TIFFTAG_XRESOLUTION, &resX);
TIFFGetField(tiff, TIFFTAG_YRESOLUTION, &resY);
switch(resUnit) {
case RESUNIT_CENTIMETER:
image->setDotsPerMeterX(qRound(resX * 100));
image->setDotsPerMeterY(qRound(resY * 100));
break;
case RESUNIT_INCH:
image->setDotsPerMeterX(qRound(resX * (100 / 2.54)));
image->setDotsPerMeterY(qRound(resY * (100 / 2.54)));
break;
default:
// do nothing as defaults have already
// been set within the QImage class
break;
}
for (uint32 y=0; y<height; ++y)
convert32BitOrder(image->scanLine(y), width);
} else {
*image = QImage();
}
}
TIFFClose(tiff);
}
if (image->isNull())
return false;
return true;
}
bool QTiffHandler::write(const QImage &image)
{
if (!device()->isWritable())
return false;
TIFF *tiff = TIFFClientOpen("foo",
"w",
this,
qtiffReadProc,
qtiffWriteProc,
qtiffSeekProc,
qtiffCloseProc,
qtiffSizeProc,
qtiffMapProc,
qtiffUnmapProc);
if (tiff) {
int width = image.width();
int height = image.height();
int depth = 32;
if (!TIFFSetField(tiff, TIFFTAG_IMAGEWIDTH, width)
|| !TIFFSetField(tiff, TIFFTAG_IMAGELENGTH, height)
|| !TIFFSetField(tiff, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB)
|| !TIFFSetField(tiff, TIFFTAG_COMPRESSION, compression == NoCompression ? COMPRESSION_NONE : COMPRESSION_LZW)
|| !TIFFSetField(tiff, TIFFTAG_SAMPLESPERPIXEL, depth/8)
|| !TIFFSetField(tiff, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG)
|| !TIFFSetField(tiff, TIFFTAG_BITSPERSAMPLE, 8)) {
TIFFClose(tiff);
return false;
}
// set the resolution
bool resolutionSet = false;
const int dotPerMeterX = image.dotsPerMeterX();
const int dotPerMeterY = image.dotsPerMeterY();
if ((dotPerMeterX % 100) == 0
&& (dotPerMeterY % 100) == 0) {
resolutionSet = TIFFSetField(tiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_CENTIMETER)
&& TIFFSetField(tiff, TIFFTAG_XRESOLUTION, dotPerMeterX/100.0)
&& TIFFSetField(tiff, TIFFTAG_YRESOLUTION, dotPerMeterY/100.0);
} else {
resolutionSet = TIFFSetField(tiff, TIFFTAG_RESOLUTIONUNIT, RESUNIT_INCH)
&& TIFFSetField(tiff, TIFFTAG_XRESOLUTION, static_cast<float>(image.logicalDpiX()))
&& TIFFSetField(tiff, TIFFTAG_YRESOLUTION, static_cast<float>(image.logicalDpiY()));
}
if (!resolutionSet) {
TIFFClose(tiff);
return false;
}
// try to do the ARGB32 conversion in chunks no greater than 16 MB
int chunks = (width * height * 4 / (1024 * 1024 * 16)) + 1;
int chunkHeight = qMax(height / chunks, 1);
int y = 0;
while (y < height) {
QImage chunk = image.copy(0, y, width, qMin(chunkHeight, height - y)).convertToFormat(QImage::Format_ARGB32);
int chunkStart = y;
int chunkEnd = y + chunk.height();
while (y < chunkEnd) {
if (QSysInfo::ByteOrder == QSysInfo::LittleEndian)
convert32BitOrder(chunk.scanLine(y - chunkStart), width);
else
convert32BitOrderBigEndian(chunk.scanLine(y - chunkStart), width);
if (TIFFWriteScanline(tiff, reinterpret_cast<uint32 *>(chunk.scanLine(y - chunkStart)), y) != 1) {
TIFFClose(tiff);
return false;
}
++y;
}
}
TIFFClose(tiff);
} else {
return false;
}
return true;
}
QByteArray QTiffHandler::name() const
{
return "tiff";
}
QVariant QTiffHandler::option(ImageOption option) const
{
if (option == Size && canRead()) {
QSize imageSize;
qint64 pos = device()->pos();
TIFF *tiff = TIFFClientOpen("foo",
"r",
const_cast<QTiffHandler*>(this),
qtiffReadProc,
qtiffWriteProc,
qtiffSeekProc,
qtiffCloseProc,
qtiffSizeProc,
qtiffMapProc,
qtiffUnmapProc);
if (tiff) {
uint32 width = 0;
uint32 height = 0;
TIFFGetField(tiff, TIFFTAG_IMAGEWIDTH, &width);
TIFFGetField(tiff, TIFFTAG_IMAGELENGTH, &height);
imageSize = QSize(width, height);
}
device()->seek(pos);
if (imageSize.isValid())
return imageSize;
} else if (option == CompressionRatio) {
return compression;
} else if (option == ImageFormat) {
return QImage::Format_ARGB32;
}
return QVariant();
}
void QTiffHandler::setOption(ImageOption option, const QVariant &value)
{
if (option == CompressionRatio && value.type() == QVariant::Int)
compression = value.toInt();
}
bool QTiffHandler::supportsOption(ImageOption option) const
{
return option == CompressionRatio
|| option == Size
|| option == ImageFormat;
}
void QTiffHandler::convert32BitOrder(void *buffer, int width)
{
uint32 *target = reinterpret_cast<uint32 *>(buffer);
for (int32 x=0; x<width; ++x) {
uint32 p = target[x];
// convert between ARGB and ABGR
target[x] = (p & 0xff000000)
| ((p & 0x00ff0000) >> 16)
| (p & 0x0000ff00)
| ((p & 0x000000ff) << 16);
}
}
void QTiffHandler::convert32BitOrderBigEndian(void *buffer, int width)
{
uint32 *target = reinterpret_cast<uint32 *>(buffer);
for (int32 x=0; x<width; ++x) {
uint32 p = target[x];
target[x] = (p & 0xff000000) >> 24
| (p & 0x00ff0000) << 8
| (p & 0x0000ff00) << 8
| (p & 0x000000ff) << 8;
}
}
QT_END_NAMESPACE
|