1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Copyright (C) 2016 Alex Char.
5** Contact: https://www.qt.io/licensing/
6**
7** This file is part of the plugins of the Qt Toolkit.
8**
9** $QT_BEGIN_LICENSE:LGPL$
10** Commercial License Usage
11** Licensees holding valid commercial Qt licenses may use this file in
12** accordance with the commercial license agreement provided with the
13** Software or, alternatively, in accordance with the terms contained in
14** a written agreement between you and The Qt Company. For licensing terms
15** and conditions see https://www.qt.io/terms-conditions. For further
16** information use the contact form at https://www.qt.io/contact-us.
17**
18** GNU Lesser General Public License Usage
19** Alternatively, this file may be used under the terms of the GNU Lesser
20** General Public License version 3 as published by the Free Software
21** Foundation and appearing in the file LICENSE.LGPL3 included in the
22** packaging of this file. Please review the following information to
23** ensure the GNU Lesser General Public License version 3 requirements
24** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
25**
26** GNU General Public License Usage
27** Alternatively, this file may be used under the terms of the GNU
28** General Public License version 2.0 or (at your option) the GNU General
29** Public license version 3 or any later version approved by the KDE Free
30** Qt Foundation. The licenses are as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
32** included in the packaging of this file. Please review the following
33** information to ensure the GNU General Public License requirements will
34** be met: https://www.gnu.org/licenses/gpl-2.0.html and
35** https://www.gnu.org/licenses/gpl-3.0.html.
36**
37** $QT_END_LICENSE$
38**
39****************************************************************************/
40
41#ifndef QICNSHANDLER_P_H
42#define QICNSHANDLER_P_H
43
44#include <QtGui/qimageiohandler.h>
45#include <QtCore/qvector.h>
46
47#ifndef QT_NO_DATASTREAM
48
49#define MAKEOSTYPE(c0,c1,c2,c3) (((quint8)c0 << 24) | ((quint8)c1 << 16) | ((quint8)c2 << 8) | (quint8)c3)
50
51QT_BEGIN_NAMESPACE
52
53struct ICNSBlockHeader
54{
55 enum OS {
56 TypeIcns = MAKEOSTYPE('i', 'c', 'n', 's'), // Icns container magic
57 TypeToc = MAKEOSTYPE('T', 'O', 'C', ' '), // Table of contents
58 TypeIcnv = MAKEOSTYPE('i', 'c', 'n', 'V'), // Icon Composer version
59 // Legacy:
60 TypeClut = MAKEOSTYPE('c', 'l', 'u', 't'), // Color look-up table (pre-OS X resources)
61 TypeTile = MAKEOSTYPE('t', 'i', 'l', 'e'), // Container (icon variants)
62 TypeOver = MAKEOSTYPE('o', 'v', 'e', 'r'), // Container (icon variants)
63 TypeOpen = MAKEOSTYPE('o', 'p', 'e', 'n'), // Container (icon variants)
64 TypeDrop = MAKEOSTYPE('d', 'r', 'o', 'p'), // Container (icon variants)
65 TypeOdrp = MAKEOSTYPE('o', 'd', 'r', 'p'), // Container (icon variants)
66 };
67
68 quint32 ostype;
69 quint32 length;
70};
71
72struct ICNSEntry
73{
74 enum Group {
75 GroupUnknown = 0, // Default for invalid ones
76 GroupMini = 'm', // "mini" (16x12)
77 GroupSmall = 's', // "small" (16x16)
78 GroupLarge = 'l', // "large" (32x32)
79 GroupHuge = 'h', // "huge" (48x48)
80 GroupThumbnail = 't', // "thumbnail" (128x128)
81 GroupPortable = 'p', // "portable"? (Speculation, used for png/jp2)
82 GroupCompressed = 'c', // "compressed"? (Speculation, used for png/jp2)
83 // Legacy icons:
84 GroupICON = 'N', // "ICON" (32x32)
85 };
86 enum Depth {
87 DepthUnknown = 0, // Default for invalid or compressed ones
88 DepthMono = 1,
89 Depth4bit = 4,
90 Depth8bit = 8,
91 Depth32bit = 32
92 };
93 enum Flags {
94 Unknown = 0x0, // Default for invalid ones
95 IsIcon = 0x1, // Contains a raw icon without alpha or compressed icon
96 IsMask = 0x2, // Contains alpha mask
97 IconPlusMask = IsIcon | IsMask // Contains raw icon and mask combined in one entry (double size)
98 };
99 enum Format {
100 FormatUnknown = 0, // Default for invalid or undetermined ones
101 RawIcon, // Raw legacy icon, uncompressed
102 RLE24, // Raw 32bit icon, data is compressed
103 PNG, // Compressed icon in PNG format
104 JP2 // Compressed icon in JPEG2000 format
105 };
106
107 quint32 ostype; // Real OSType
108 quint32 variant; // Virtual OSType: a parent container, zero if parent is icns root
109 Group group; // ASCII character number
110 quint32 width; // For uncompressed icons only, zero for compressed ones for now
111 quint32 height; // For uncompressed icons only, zero for compressed ones fow now
112 Depth depth; // Color depth
113 Flags flags; // Flags to determine the type of entry
114 Format dataFormat; // Format of the image data
115 quint32 dataLength; // Length of the image data in bytes
116 qint64 dataOffset; // Offset from the initial position of the file/device
117
118 ICNSEntry() :
119 ostype(0), variant(0), group(GroupUnknown), width(0), height(0), depth(DepthUnknown),
120 flags(Unknown), dataFormat(FormatUnknown), dataLength(0), dataOffset(0)
121 {
122 }
123};
124Q_DECLARE_TYPEINFO(ICNSEntry, Q_MOVABLE_TYPE);
125
126class QICNSHandler : public QImageIOHandler
127{
128public:
129 QICNSHandler();
130
131 bool canRead() const override;
132 bool read(QImage *image) override;
133 bool write(const QImage &image) override;
134
135 bool supportsOption(ImageOption option) const override;
136 QVariant option(ImageOption option) const override;
137
138 int imageCount() const override;
139 bool jumpToImage(int imageNumber) override;
140 bool jumpToNextImage() override;
141
142 static bool canRead(QIODevice *device);
143
144private:
145 bool ensureScanned() const;
146 bool scanDevice();
147 bool addEntry(const ICNSBlockHeader &header, qint64 imgDataOffset, quint32 variant = 0);
148 const ICNSEntry &getIconMask(const ICNSEntry &icon) const;
149
150private:
151 enum ScanState {
152 ScanError = -1,
153 ScanNotScanned = 0,
154 ScanSuccess = 1,
155 };
156
157 int m_currentIconIndex;
158 QVector<ICNSEntry> m_icons;
159 QVector<ICNSEntry> m_masks;
160 ScanState m_state;
161};
162
163QT_END_NAMESPACE
164
165#endif // QT_NO_DATASTREAM
166
167#endif /* QICNSHANDLER_P_H */
168

source code of qtimageformats/src/plugins/imageformats/icns/qicnshandler_p.h