-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
qpsdhandler.cpp
336 lines (305 loc) · 9.93 KB
/
qpsdhandler.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
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
/*
Copyright (c) 2012 Ronie Martinez ([email protected])
All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library 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 Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA
*/
#include "qpsdhandler.h"
#ifdef QT_DEBUG
#include <QDebug>
#include <QElapsedTimer>
#endif
QPsdHandler::QPsdHandler()
{
}
QPsdHandler::~QPsdHandler()
{
}
bool QPsdHandler::canRead() const
{
if (canRead(device())) {
QByteArray bytes = device()->peek(6);
QDataStream input(bytes);
input.setByteOrder(QDataStream::BigEndian);
quint32 signature;
quint16 version;
input >> signature >> version;
if (version == 1)
setFormat("psd");
else if (version == 2)
setFormat("psb");
else return false;
return true;
}
return false;
}
bool QPsdHandler::read(QImage *image)
{
QDataStream input(device());
quint32 signature, height, width;
quint16 version, channels, depth, colorMode, compression;
input.setByteOrder(QDataStream::BigEndian);
input >> signature;
if (!isValidSignature(signature)) {
return false;
}
input >> version;
if (!isValidVersion(version)) {
return false;
}
input.skipRawData(6); //reserved bytes should be 6-byte in size
input >> channels;
if (!isChannelCountSupported(channels)) {
return false;
}
input >> height;
if (!isValidWidthOrHeight(version, height)) {
return false;
}
input >> width;
if (!isValidWidthOrHeight(version, width)) {
return false;
}
input >> depth;
if (!isSupportedDepth(depth)) {
return false;
}
input >> colorMode;
if (!isSupportedColorMode(colorMode)) {
return false;
}
QByteArray colorData = readColorData(input);
skipImageResources(input);
skipLayerAndMaskSection(input);
input >> compression;
quint64 totalBytesPerChannel = (quint64)width * height * depth / 8;
quint64 size = (quint64)channels * totalBytesPerChannel;
QByteArray imageData;
switch (compression) {
case RLE:
// The RLE-compressed data is preceeded by a 2-byte(psd) or 4-byte(psb)
// data count for each row in the data
if (format() == "psd")
input.skipRawData(height * channels * 2);
else if (format() == "psb")
input.skipRawData(height * channels * 4);
case RAW:
imageData = readImageData(input, (Compression)compression, size);
break;
default:
return false;
break;
}
if (input.status() != QDataStream::Ok)
return false;
if ((quint64)imageData.size() != size)
return false;
#ifdef QT_DEBUG
qDebug() << "format: " << format()
<< "\ncolor mode: " << colorMode
<< "\ndepth: " << depth
<< "\nchannels: " << channels
<< "\ncompression: " << compression
<< "\nwidth: " << width
<< "\nheight: " << height
<< "\ntotalBytesPerChannel: " << totalBytesPerChannel
<< "\nimage data: " << imageData.size();
#endif
switch (colorMode) {
case BITMAP:
{
QImage result = processBitmap(imageData, width, height);
if (result.isNull())
return false;
else {
*image = result;
return true;
}
}
break;
case GRAYSCALE:
{
switch (depth) {
case 8:
if (1 == channels) {
*image = processGrayscale8(imageData, width, height);
return true;
} else {
//excess channels other than Gray are considered alphas
*image = processGrayscale8WithAlpha(imageData, width, height, totalBytesPerChannel);
return true;
}
break;
case 16:
if (1 == channels) {
*image = processGrayscale16(imageData, width, height);
return true;
} else {
//excess channels other than Gray are considered alphas
*image = processGrayscale16WithAlpha(imageData, width, height, totalBytesPerChannel);
return true;
}
break;
case 32: // FIXME: 32 bpc (HDR)... requires tonemapping
default:
return false;
break;
}
}
break;
case INDEXED:
if (8 == depth && 1 == channels) {
*image = processIndexed(colorData, imageData, width, height);
return true;
} else {
return false;
}
break;
case RGB:
{
switch (depth) {
case 8:
if (3 == channels) {
*image = processRGB8(imageData, width, height, totalBytesPerChannel);
return true;
} else {
//excess channels other than RGB are considered alphas
*image = processRGB8WithAlpha(imageData, width, height, totalBytesPerChannel);
return true;
}
break;
case 16:
if (3 == channels) {
*image = processRGB16(imageData, width, height, totalBytesPerChannel);
return true;
} else {
//excess channels other than RGB are considered alphas
*image = processRGB16WithAlpha(imageData, width, height, totalBytesPerChannel);
return true;
}
break;
case 32: //FIXME: 32 bpc (HDR)... requires tonemapping
default:
return false;
break;
}
}
break;
/* Mixed CMYK and Multichannel logic due to similarities*/
case CMYK:
case MULTICHANNEL:
{
// Reference: http://help.adobe.com/en_US/photoshop/cs/using/WSfd1234e1c4b69f30ea53e41001031ab64-73eea.html#WSfd1234e1c4b69f30ea53e41001031ab64-73e5a
// Converting a CMYK image to Multichannel mode creates cyan, magenta, yellow, and black spot channels.
// Converting an RGB image to Multichannel mode creates cyan, magenta, and yellow spot channels.
switch (depth) {
case 8:
if (3 == channels) {
*image = processCMY8(imageData, width, height, totalBytesPerChannel);
return true;
} else if (4 == channels) {
*image = processCMYK8(imageData, width, height, totalBytesPerChannel);
return true;
} else {
*image = processCMYK8WithAlpha(imageData, width, height, totalBytesPerChannel);
return true;
}
break;
case 16:
if (4 == channels) {
*image = processCMYK16(imageData, width, height, totalBytesPerChannel);
return true;
} else {
*image = processCMYK16WithAlpha(imageData, width, height, totalBytesPerChannel);
return true;
}
break;
default:
return false;
break;
}
}
break;
case DUOTONE:
// Duotone images: color data contains the duotone specification
// (the format of which is not documented). Other applications that
// read Photoshop files can treat a duotone image as a gray image,
// and just preserve the contents of the duotone information when
// reading and writing the file.
// TODO: find a way to actually get the duotone, tritone, and quadtone colors
if (8 == depth && 1 == channels) {
*image = processDuotone(imageData, width, height);
return true;
} else {
return false;
}
break;
case LAB:
{
switch (depth) {
case 8:
if (3 == channels) {
*image = processLAB8(imageData, width, height, totalBytesPerChannel);
return true;
} else {
*image = processLAB8WithAlpha(imageData, width, height, totalBytesPerChannel);
return true;
}
break;
case 16:
if (3 == channels) {
*image = processLAB16(imageData, width, height, totalBytesPerChannel);
return true;
} else {
*image = processLAB16WithAlpha(imageData, width, height, totalBytesPerChannel);
return true;
}
break;
default:
return false;
break;
}
}
break;
default:
return false;
break;
}
return input.status() == QDataStream::Ok;
}
bool QPsdHandler::canRead(QIODevice *device)
{
return device->peek(4) == "8BPS";
}
QVariant QPsdHandler::option(ImageOption option) const
{
if (option == Size) {
QByteArray bytes = device()->peek(26);
QDataStream input(bytes);
quint32 signature, height, width;
quint16 version, channels, depth, colorMode;
input.setByteOrder(QDataStream::BigEndian);
input >> signature >> version ;
input.skipRawData(6); //reserved bytes should be 6-byte in size
input >> channels >> height >> width >> depth >> colorMode;
if (input.status() == QDataStream::Ok && signature == 0x38425053 &&
(version == 1 || version == 2))
return QSize(width, height);
}
return QVariant();
}
bool QPsdHandler::supportsOption(ImageOption option) const
{
return option == Size;
}