1 /*
2 Copyright (c) 2012-2019 Ronie Martinez (ronmarti18@gmail.com)
3 All rights reserved.
4 
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9 
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied
12 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
13 PURPOSE.  See the GNU Lesser General Public License for more
14 details.
15 
16 You should have received a copy of the GNU Lesser General Public
17 License along with this library; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 02110-1301  USA
20 */
21 
22 #ifndef QPSDHANDLER_H
23 #define QPSDHANDLER_H
24 
25 #include <QImageIOHandler>
26 #include <QImage>
27 #include <QColor>
28 #include <QVariant>
29 #include <qmath.h>
30 
31 class QPsdHandler : public QImageIOHandler
32 {
33 public:
34     QPsdHandler();
35     ~QPsdHandler();
36 
37     bool canRead() const;
38     bool read(QImage *image);
39     //bool write(const QImage &image);
40 
41     static bool canRead(QIODevice *device);
42 
43     QVariant option(ImageOption option) const;
44     //void setOption(ImageOption option, const QVariant &value);
45     bool supportsOption(ImageOption option) const;
46 private:
47     bool isValidSignature(quint32 signature);
48     bool isValidVersion(quint16 version);
49     bool isChannelCountSupported(quint16 channel);
50     bool isValidWidthOrHeight(quint16 version, quint32 value);
51     bool isSupportedDepth(quint16 depth);
52     bool isSupportedColorMode(quint16 colorMode);
53     QByteArray readColorData(QDataStream& input);
54     void skipImageResources(QDataStream& input);
55     void skipLayerAndMaskSection(QDataStream& input);
56     enum Compression {
57         RAW = 0,
58         RLE = 1,
59         ZIP_WITHOUT_PREDICTION = 2,
60         ZIP_WITH_PREDICTION = 3
61     };
62     QByteArray readImageData(QDataStream& input, Compression compression, quint64 size=0);
63     enum ColorMode {
64         BITMAP = 0,
65         GRAYSCALE = 1,
66         INDEXED = 2,
67         RGB = 3,
68         CMYK = 4,
69         MULTICHANNEL = 7,
70         DUOTONE = 8,
71         LAB = 9,
72     };
73     QImage processBitmap(QByteArray& imageData, quint32 width, quint32 height);
74     QImage processGrayscale8(QByteArray& imageData, quint32 width, quint32 height);
75     QImage processGrayscale8WithAlpha(QByteArray& imageData, quint32 width, quint32 height,
76                                      quint64 totalBytesPerChannel);
77     QImage processGrayscale16(QByteArray& imageData, quint32 width, quint32 height);
78     QImage processGrayscale16WithAlpha(QByteArray& imageData, quint32 width, quint32 height,
79                                        quint64 totalBytesPerChannel);
80     QImage processIndexed(QByteArray& colorData, QByteArray& imageData, quint32 width,
81                           quint32 height);
82     QImage processRGB8(QByteArray& imageData, quint32 width, quint32 height,
83                        quint64 totalBytesPerChannel);
84     QImage processRGB16(QByteArray& imageData, quint32 width, quint32 height,
85                         quint64 totalBytesPerChannel);
86     QImage processRGB8WithAlpha(QByteArray& imageData, quint32 width, quint32 height,
87                                 quint64 totalBytesPerChannel);
88     QImage processRGB16WithAlpha(QByteArray& imageData, quint32 width, quint32 height,
89                                  quint64 totalBytesPerChannel);
90     QImage processCMY8(QByteArray& imageData, quint32 width, quint32 height,
91                        quint64 totalBytesPerChannel);
92     QImage processCMYK8(QByteArray& imageData, quint32 width, quint32 height,
93                         quint64 totalBytesPerChannel);
94     QImage processCMYK8WithAlpha(QByteArray& imageData, quint32 width, quint32 height,
95                                  quint64 totalBytesPerChannel);
96     QImage processCMYK16(QByteArray& imageData, quint32 width, quint32 height,
97                          quint64 totalBytesPerChannel);
98     QImage processCMYK16WithAlpha(QByteArray& imageData, quint32 width, quint32 height,
99                                   quint64 totalBytesPerChannel);
100     QImage processDuotone(QByteArray& imageData, quint32 width, quint32 height);
101     QImage processLAB8(QByteArray& imageData, quint32 width, quint32 height,
102                        quint64 totalBytesPerChannel);
103     QImage processLAB8WithAlpha(QByteArray& imageData, quint32 width, quint32 height,
104                                 quint64 totalBytesPerChannel);
105     QImage processLAB16(QByteArray& imageData, quint32 width, quint32 height,
106                         quint64 totalBytesPerChannel);
107     QImage processLAB16WithAlpha(QByteArray& imageData, quint32 width, quint32 height,
108                                  quint64 totalBytesPerChannel);
109 };
110 
111 #endif // QPSDHANDLER_H
112