Debian Codegraph @

src/plugins/imageformats/icns/qicnshandler.cpp

raw symbols ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
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 #include "qicnshandler_p.h"
42
43 #include <QtCore/qmath.h>
44 #include <QtCore/qendian.h>
45 #include <QtCore/qregularexpression.h>
46 #include <QtCore/qbuffer.h>
47 #include <QtGui/qimage.h>
48
49 #ifndef QT_NO_DATASTREAM
50
51 QT_BEGIN_NAMESPACE
52
53 static const quint8 ICNSBlockHeaderSize = 8;
54
55 static const QRgb ICNSColorTableMono[] = {
56 qRgb(0xFF, 0xFF, 0xFF),
57 qRgb(0x00, 0x00, 0x00)
58 };
59 Q_STATIC_ASSERT(sizeof(ICNSColorTableMono) / sizeof(ICNSColorTableMono[0]) == (1 << ICNSEntry::DepthMono));
60
61 static const QRgb ICNSColorTable4bit[] = {
62 qRgb(0xFF, 0xFF, 0xFF),
63 qRgb(0xFC, 0xF3, 0x05),
64 qRgb(0xFF, 0x64, 0x02),
65 qRgb(0xDD, 0x08, 0x06),
66 qRgb(0xF2, 0x08, 0x84),
67 qRgb(0x46, 0x00, 0xA5),
68 qRgb(0x00, 0x00, 0xD4),
69 qRgb(0x02, 0xAB, 0xEA),
70 qRgb(0x1F, 0xB7, 0x14),
71 qRgb(0x00, 0x64, 0x11),
72 qRgb(0x56, 0x2C, 0x05),
73 qRgb(0x90, 0x71, 0x3A),
74 qRgb(0xC0, 0xC0, 0xC0),
75 qRgb(0x80, 0x80, 0x80),
76 qRgb(0x40, 0x40, 0x40),
77 qRgb(0x00, 0x00, 0x00)
78 };
79 Q_STATIC_ASSERT(sizeof(ICNSColorTable4bit) / sizeof(ICNSColorTable4bit[0]) == (1 << ICNSEntry::Depth4bit));
80
81 static const QRgb ICNSColorTable8bit[] = {
82 qRgb(0xFF, 0xFF, 0xFF),
83 qRgb(0xFF, 0xFF, 0xCC),
84 qRgb(0xFF, 0xFF, 0x99),
85 qRgb(0xFF, 0xFF, 0x66),
86 qRgb(0xFF, 0xFF, 0x33),
87 qRgb(0xFF, 0xFF, 0x00),
88 qRgb(0xFF, 0xCC, 0xFF),
89 qRgb(0xFF, 0xCC, 0xCC),
90 qRgb(0xFF, 0xCC, 0x99),
91 qRgb(0xFF, 0xCC, 0x66),
92 qRgb(0xFF, 0xCC, 0x33),
93 qRgb(0xFF, 0xCC, 0x00),
94 qRgb(0xFF, 0x99, 0xFF),
95 qRgb(0xFF, 0x99, 0xCC),
96 qRgb(0xFF, 0x99, 0x99),
97 qRgb(0xFF, 0x99, 0x66),
98 qRgb(0xFF, 0x99, 0x33),
99 qRgb(0xFF, 0x99, 0x00),
100 qRgb(0xFF, 0x66, 0xFF),
101 qRgb(0xFF, 0x66, 0xCC),
102 qRgb(0xFF, 0x66, 0x99),
103 qRgb(0xFF, 0x66, 0x66),
104 qRgb(0xFF, 0x66, 0x33),
105 qRgb(0xFF, 0x66, 0x00),
106 qRgb(0xFF, 0x33, 0xFF),
107 qRgb(0xFF, 0x33, 0xCC),
108 qRgb(0xFF, 0x33, 0x99),
109 qRgb(0xFF, 0x33, 0x66),
110 qRgb(0xFF, 0x33, 0x33),
111 qRgb(0xFF, 0x33, 0x00),
112 qRgb(0xFF, 0x00, 0xFF),
113 qRgb(0xFF, 0x00, 0xCC),
114 qRgb(0xFF, 0x00, 0x99),
115 qRgb(0xFF, 0x00, 0x66),
116 qRgb(0xFF, 0x00, 0x33),
117 qRgb(0xFF, 0x00, 0x00),
118 qRgb(0xCC, 0xFF, 0xFF),
119 qRgb(0xCC, 0xFF, 0xCC),
120 qRgb(0xCC, 0xFF, 0x99),
121 qRgb(0xCC, 0xFF, 0x66),
122 qRgb(0xCC, 0xFF, 0x33),
123 qRgb(0xCC, 0xFF, 0x00),
124 qRgb(0xCC, 0xCC, 0xFF),
125 qRgb(0xCC, 0xCC, 0xCC),
126 qRgb(0xCC, 0xCC, 0x99),
127 qRgb(0xCC, 0xCC, 0x66),
128 qRgb(0xCC, 0xCC, 0x33),
129 qRgb(0xCC, 0xCC, 0x00),
130 qRgb(0xCC, 0x99, 0xFF),
131 qRgb(0xCC, 0x99, 0xCC),
132 qRgb(0xCC, 0x99, 0x99),
133 qRgb(0xCC, 0x99, 0x66),
134 qRgb(0xCC, 0x99, 0x33),
135 qRgb(0xCC, 0x99, 0x00),
136 qRgb(0xCC, 0x66, 0xFF),
137 qRgb(0xCC, 0x66, 0xCC),
138 qRgb(0xCC, 0x66, 0x99),
139 qRgb(0xCC, 0x66, 0x66),
140 qRgb(0xCC, 0x66, 0x33),
141 qRgb(0xCC, 0x66, 0x00),
142 qRgb(0xCC, 0x33, 0xFF),
143 qRgb(0xCC, 0x33, 0xCC),
144 qRgb(0xCC, 0x33, 0x99),
145 qRgb(0xCC, 0x33, 0x66),
146 qRgb(0xCC, 0x33, 0x33),
147 qRgb(0xCC, 0x33, 0x00),
148 qRgb(0xCC, 0x00, 0xFF),
149 qRgb(0xCC, 0x00, 0xCC),
150 qRgb(0xCC, 0x00, 0x99),
151 qRgb(0xCC, 0x00, 0x66),
152 qRgb(0xCC, 0x00, 0x33),
153 qRgb(0xCC, 0x00, 0x00),
154 qRgb(0x99, 0xFF, 0xFF),
155 qRgb(0x99, 0xFF, 0xCC),
156 qRgb(0x99, 0xFF, 0x99),
157 qRgb(0x99, 0xFF, 0x66),
158 qRgb(0x99, 0xFF, 0x33),
159 qRgb(0x99, 0xFF, 0x00),
160 qRgb(0x99, 0xCC, 0xFF),
161 qRgb(0x99, 0xCC, 0xCC),
162 qRgb(0x99, 0xCC, 0x99),
163 qRgb(0x99, 0xCC, 0x66),
164 qRgb(0x99, 0xCC, 0x33),
165 qRgb(0x99, 0xCC, 0x00),
166 qRgb(0x99, 0x99, 0xFF),
167 qRgb(0x99, 0x99, 0xCC),
168 qRgb(0x99, 0x99, 0x99),
169 qRgb(0x99, 0x99, 0x66),
170 qRgb(0x99, 0x99, 0x33),
171 qRgb(0x99, 0x99, 0x00),
172 qRgb(0x99, 0x66, 0xFF),
173 qRgb(0x99, 0x66, 0xCC),
174 qRgb(0x99, 0x66, 0x99),
175 qRgb(0x99, 0x66, 0x66),
176 qRgb(0x99, 0x66, 0x33),
177 qRgb(0x99, 0x66, 0x00),
178 qRgb(0x99, 0x33, 0xFF),
179 qRgb(0x99, 0x33, 0xCC),
180 qRgb(0x99, 0x33, 0x99),
181 qRgb(0x99, 0x33, 0x66),
182 qRgb(0x99, 0x33, 0x33),
183 qRgb(0x99, 0x33, 0x00),
184 qRgb(0x99, 0x00, 0xFF),
185 qRgb(0x99, 0x00, 0xCC),
186 qRgb(0x99, 0x00, 0x99),
187 qRgb(0x99, 0x00, 0x66),
188 qRgb(0x99, 0x00, 0x33),
189 qRgb(0x99, 0x00, 0x00),
190 qRgb(0x66, 0xFF, 0xFF),
191 qRgb(0x66, 0xFF, 0xCC),
192 qRgb(0x66, 0xFF, 0x99),
193 qRgb(0x66, 0xFF, 0x66),
194 qRgb(0x66, 0xFF, 0x33),
195 qRgb(0x66, 0xFF, 0x00),
196 qRgb(0x66, 0xCC, 0xFF),
197 qRgb(0x66, 0xCC, 0xCC),
198 qRgb(0x66, 0xCC, 0x99),
199 qRgb(0x66, 0xCC, 0x66),
200 qRgb(0x66, 0xCC, 0x33),
201 qRgb(0x66, 0xCC, 0x00),
202 qRgb(0x66, 0x99, 0xFF),
203 qRgb(0x66, 0x99, 0xCC),
204 qRgb(0x66, 0x99, 0x99),
205 qRgb(0x66, 0x99, 0x66),
206 qRgb(0x66, 0x99, 0x33),
207 qRgb(0x66, 0x99, 0x00),
208 qRgb(0x66, 0x66, 0xFF),
209 qRgb(0x66, 0x66, 0xCC),
210 qRgb(0x66, 0x66, 0x99),
211 qRgb(0x66, 0x66, 0x66),
212 qRgb(0x66, 0x66, 0x33),
213 qRgb(0x66, 0x66, 0x00),
214 qRgb(0x66, 0x33, 0xFF),
215 qRgb(0x66, 0x33, 0xCC),
216 qRgb(0x66, 0x33, 0x99),
217 qRgb(0x66, 0x33, 0x66),
218 qRgb(0x66, 0x33, 0x33),
219 qRgb(0x66, 0x33, 0x00),
220 qRgb(0x66, 0x00, 0xFF),
221 qRgb(0x66, 0x00, 0xCC),
222 qRgb(0x66, 0x00, 0x99),
223 qRgb(0x66, 0x00, 0x66),
224 qRgb(0x66, 0x00, 0x33),
225 qRgb(0x66, 0x00, 0x00),
226 qRgb(0x33, 0xFF, 0xFF),
227 qRgb(0x33, 0xFF, 0xCC),
228 qRgb(0x33, 0xFF, 0x99),
229 qRgb(0x33, 0xFF, 0x66),
230 qRgb(0x33, 0xFF, 0x33),
231 qRgb(0x33, 0xFF, 0x00),
232 qRgb(0x33, 0xCC, 0xFF),
233 qRgb(0x33, 0xCC, 0xCC),
234 qRgb(0x33, 0xCC, 0x99),
235 qRgb(0x33, 0xCC, 0x66),
236 qRgb(0x33, 0xCC, 0x33),
237 qRgb(0x33, 0xCC, 0x00),
238 qRgb(0x33, 0x99, 0xFF),
239 qRgb(0x33, 0x99, 0xCC),
240 qRgb(0x33, 0x99, 0x99),
241 qRgb(0x33, 0x99, 0x66),
242 qRgb(0x33, 0x99, 0x33),
243 qRgb(0x33, 0x99, 0x00),
244 qRgb(0x33, 0x66, 0xFF),
245 qRgb(0x33, 0x66, 0xCC),
246 qRgb(0x33, 0x66, 0x99),
247 qRgb(0x33, 0x66, 0x66),
248 qRgb(0x33, 0x66, 0x33),
249 qRgb(0x33, 0x66, 0x00),
250 qRgb(0x33, 0x33, 0xFF),
251 qRgb(0x33, 0x33, 0xCC),
252 qRgb(0x33, 0x33, 0x99),
253 qRgb(0x33, 0x33, 0x66),
254 qRgb(0x33, 0x33, 0x33),
255 qRgb(0x33, 0x33, 0x00),
256 qRgb(0x33, 0x00, 0xFF),
257 qRgb(0x33, 0x00, 0xCC),
258 qRgb(0x33, 0x00, 0x99),
259 qRgb(0x33, 0x00, 0x66),
260 qRgb(0x33, 0x00, 0x33),
261 qRgb(0x33, 0x00, 0x00),
262 qRgb(0x00, 0xFF, 0xFF),
263 qRgb(0x00, 0xFF, 0xCC),
264 qRgb(0x00, 0xFF, 0x99),
265 qRgb(0x00, 0xFF, 0x66),
266 qRgb(0x00, 0xFF, 0x33),
267 qRgb(0x00, 0xFF, 0x00),
268 qRgb(0x00, 0xCC, 0xFF),
269 qRgb(0x00, 0xCC, 0xCC),
270 qRgb(0x00, 0xCC, 0x99),
271 qRgb(0x00, 0xCC, 0x66),
272 qRgb(0x00, 0xCC, 0x33),
273 qRgb(0x00, 0xCC, 0x00),
274 qRgb(0x00, 0x99, 0xFF),
275 qRgb(0x00, 0x99, 0xCC),
276 qRgb(0x00, 0x99, 0x99),
277 qRgb(0x00, 0x99, 0x66),
278 qRgb(0x00, 0x99, 0x33),
279 qRgb(0x00, 0x99, 0x00),
280 qRgb(0x00, 0x66, 0xFF),
281 qRgb(0x00, 0x66, 0xCC),
282 qRgb(0x00, 0x66, 0x99),
283 qRgb(0x00, 0x66, 0x66),
284 qRgb(0x00, 0x66, 0x33),
285 qRgb(0x00, 0x66, 0x00),
286 qRgb(0x00, 0x33, 0xFF),
287 qRgb(0x00, 0x33, 0xCC),
288 qRgb(0x00, 0x33, 0x99),
289 qRgb(0x00, 0x33, 0x66),
290 qRgb(0x00, 0x33, 0x33),
291 qRgb(0x00, 0x33, 0x00),
292 qRgb(0x00, 0x00, 0xFF),
293 qRgb(0x00, 0x00, 0xCC),
294 qRgb(0x00, 0x00, 0x99),
295 qRgb(0x00, 0x00, 0x66),
296 qRgb(0x00, 0x00, 0x33),
297 qRgb(0xEE, 0x00, 0x00),
298 qRgb(0xDD, 0x00, 0x00),
299 qRgb(0xBB, 0x00, 0x00),
300 qRgb(0xAA, 0x00, 0x00),
301 qRgb(0x88, 0x00, 0x00),
302 qRgb(0x77, 0x00, 0x00),
303 qRgb(0x55, 0x00, 0x00),
304 qRgb(0x44, 0x00, 0x00),
305 qRgb(0x22, 0x00, 0x00),
306 qRgb(0x11, 0x00, 0x00),
307 qRgb(0x00, 0xEE, 0x00),
308 qRgb(0x00, 0xDD, 0x00),
309 qRgb(0x00, 0xBB, 0x00),
310 qRgb(0x00, 0xAA, 0x00),
311 qRgb(0x00, 0x88, 0x00),
312 qRgb(0x00, 0x77, 0x00),
313 qRgb(0x00, 0x55, 0x00),
314 qRgb(0x00, 0x44, 0x00),
315 qRgb(0x00, 0x22, 0x00),
316 qRgb(0x00, 0x11, 0x00),
317 qRgb(0x00, 0x00, 0xEE),
318 qRgb(0x00, 0x00, 0xDD),
319 qRgb(0x00, 0x00, 0xBB),
320 qRgb(0x00, 0x00, 0xAA),
321 qRgb(0x00, 0x00, 0x88),
322 qRgb(0x00, 0x00, 0x77),
323 qRgb(0x00, 0x00, 0x55),
324 qRgb(0x00, 0x00, 0x44),
325 qRgb(0x00, 0x00, 0x22),
326 qRgb(0x00, 0x00, 0x11),
327 qRgb(0xEE, 0xEE, 0xEE),
328 qRgb(0xDD, 0xDD, 0xDD),
329 qRgb(0xBB, 0xBB, 0xBB),
330 qRgb(0xAA, 0xAA, 0xAA),
331 qRgb(0x88, 0x88, 0x88),
332 qRgb(0x77, 0x77, 0x77),
333 qRgb(0x55, 0x55, 0x55),
334 qRgb(0x44, 0x44, 0x44),
335 qRgb(0x22, 0x22, 0x22),
336 qRgb(0x11, 0x11, 0x11),
337 qRgb(0x00, 0x00, 0x00)
338 };
339 Q_STATIC_ASSERT(sizeof(ICNSColorTable8bit) / sizeof(ICNSColorTable8bit[0]) == (1 << ICNSEntry::Depth8bit));
340
341 static inline QDataStream &operator>>(QDataStream &in, ICNSBlockHeader &p)
342 {
343 in >> p.ostype;
344 in >> p.length;
345 return in;
346 }
347
348 static inline QDataStream &operator<<(QDataStream &out, const ICNSBlockHeader &p)
349 {
350 out << p.ostype;
351 out << p.length;
352 return out;
353 }
354
355 static inline bool isPowOf2OrDividesBy16(quint32 u, qreal r)
356 {
357 return u == r && ((u % 16 == 0) || (r >= 16 && (u & (u - 1)) == 0));
358 }
359
360 static inline bool isBlockHeaderValid(const ICNSBlockHeader &header, quint64 bound = 0)
361 {
362 return header.ostype != 0 && (bound == 0
363 || qBound(quint64(ICNSBlockHeaderSize), quint64(header.length), bound) == header.length);
364 }
365
366 static inline bool isIconCompressed(const ICNSEntry &icon)
367 {
368 return icon.dataFormat == ICNSEntry::PNG || icon.dataFormat == ICNSEntry::JP2;
369 }
370
371 static inline bool isMaskSuitable(const ICNSEntry &mask, const ICNSEntry &icon, ICNSEntry::Depth target)
372 {
373 return mask.variant == icon.variant && mask.depth == target
374 && mask.height == icon.height && mask.width == icon.width;
375 }
376
377 static inline QByteArray nameFromOSType(quint32 ostype)
378 {
379 const quint32 bytes = qToBigEndian(ostype);
380 return QByteArray((const char*)&bytes, 4);
381 }
382
383 static inline quint32 nameToOSType(const QByteArray &ostype)
384 {
385 if (ostype.size() != 4)
386 return 0;
387 return qFromBigEndian(*reinterpret_cast<const quint32*>(ostype.constData()));
388 }
389
390 static inline QByteArray nameForCompressedIcon(quint8 iconNumber)
391 {
392 const bool portable = iconNumber < 7;
393 const QByteArray base = portable ? QByteArrayLiteral("icp") : QByteArrayLiteral("ic");
394 if (!portable && iconNumber < 10)
395 return base + "0" + QByteArray::number(iconNumber);
396 return base + QByteArray::number(iconNumber);
397 }
398
399 static inline QVector<QRgb> getColorTable(ICNSEntry::Depth depth)
400 {
401 QVector<QRgb> table;
402 uint n = 1 << depth;
403 const QRgb *data;
404 switch (depth) {
405 case ICNSEntry::DepthMono:
406 data = ICNSColorTableMono;
407 break;
408 case ICNSEntry::Depth4bit:
409 data = ICNSColorTable4bit;
410 break;
411 case ICNSEntry::Depth8bit:
412 data = ICNSColorTable8bit;
413 break;
414 default:
415 Q_UNREACHABLE();
416 break;
417 }
418 table.resize(n);
419 memcpy(table.data(), data, sizeof(QRgb) * n);
420 return table;
421 }
422
423 static bool parseIconEntryData(ICNSEntry &icon, QIODevice *device)
424 {
425 const qint64 oldPos = device->pos();
426 if (oldPos != icon.dataOffset && !device->seek(icon.dataOffset))
427 return false;
428
429 const QByteArray magic = device->peek(12);
430 const bool isPNG = magic.startsWith(QByteArrayLiteral("\211PNG\r\n\032\n\000\000\000\r"));
431 const bool isJP2 = !isPNG && magic == QByteArrayLiteral("\000\000\000\014jP \r\n\207\n");
432 if (isPNG || isJP2) {
433 // TODO: Add parsing of png/jp2 headers to enable feature reporting by plugin?
434 icon.flags = ICNSEntry::IsIcon;
435 icon.dataFormat = isPNG? ICNSEntry::PNG : ICNSEntry::JP2;
436 }
437 if (oldPos != icon.dataOffset && !device->seek(oldPos))
438 return false;
439 return true;
440 }
441
442 static bool parseIconEntryInfo(ICNSEntry &icon)
443 {
444 const QString ostype = QString::fromLatin1(nameFromOSType(icon.ostype));
445 // Typical OSType naming: <junk><group><depth><mask>;
446 // For icons OSType should be strictly alphanumeric + '#' character for masks/mono.
447 const QString ptrn = QStringLiteral("^(?<junk>[a-z|A-Z]{0,4})(?<group>[a-z|A-Z]{1})(?<depth>[\\d]{0,2})(?<mask>[#mk]{0,2})$");
448 QRegularExpression regexp(ptrn);
449 QRegularExpressionMatch match = regexp.match(ostype);
450 if (!match.hasMatch()) {
451 qWarning("parseIconEntryInfo(): Failed, OSType doesn't match: \"%s\"", qPrintable(ostype));
452 return false;
453 }
454 const QString group = match.captured(QStringLiteral("group"));
455 const QString depth = match.captured(QStringLiteral("depth"));
456 const QString mask = match.captured(QStringLiteral("mask"));
457 // Icon group:
458 if (!group.isEmpty())
459 icon.group = ICNSEntry::Group(group.at(0).toLatin1());
460
461 // That's enough for compressed ones
462 if (isIconCompressed(icon))
463 return true;
464 // Icon depth:
465 if (!depth.isEmpty()) {
466 const uint depthUInt = depth.toUInt();
467 if (depthUInt > 32)
468 return false;
469 icon.depth = ICNSEntry::Depth(depthUInt);
470 }
471 // Try mono if depth not found
472 if (icon.depth == ICNSEntry::DepthUnknown)
473 icon.depth = ICNSEntry::DepthMono;
474 // Detect size:
475 const qreal bytespp = (qreal)icon.depth / 8;
476 const qreal r1 = qSqrt(icon.dataLength / bytespp);
477 const qreal r2 = qSqrt((icon.dataLength / bytespp) / 2);
478 const quint32 r1u = qRound(r1);
479 const quint32 r2u = qRound(r2);
480 const bool singleEntry = isPowOf2OrDividesBy16(r1u, r1);
481 const bool doubleSize = isPowOf2OrDividesBy16(r2u, r2);
482 if (singleEntry) {
483 icon.flags = mask.isEmpty() ? ICNSEntry::IsIcon : ICNSEntry::IsMask;
484 icon.dataFormat = ICNSEntry::RawIcon;
485 icon.width = r1u;
486 icon.height = r1u;
487 } else if (doubleSize) {
488 icon.flags = ICNSEntry::IconPlusMask;
489 icon.dataFormat = ICNSEntry::RawIcon;
490 icon.width = r2u;
491 icon.height = r2u;
492 } else if (icon.group == ICNSEntry::GroupMini) {
493 // Legacy 16x12 icons are an exception from the generic square formula
494 const bool doubleSize = icon.dataLength == 192 * bytespp * 2;
495 icon.flags = doubleSize ? ICNSEntry::IconPlusMask : ICNSEntry::IsIcon;
496 icon.dataFormat = ICNSEntry::RawIcon;
497 icon.width = 16;
498 icon.height = 12;
499 } else if (icon.depth == ICNSEntry::Depth32bit) {
500 // We have a formula mismatch in a 32bit icon there, probably RLE24
501 icon.dataFormat = ICNSEntry::RLE24;
502 icon.flags = mask.isEmpty() ? ICNSEntry::IsIcon : ICNSEntry::IsMask;
503 switch (icon.group) {
504 case ICNSEntry::GroupSmall:
505 icon.width = 16;
506 break;
507 case ICNSEntry::GroupLarge:
508 icon.width = 32;
509 break;
510 case ICNSEntry::GroupHuge:
511 icon.width = 48;
512 break;
513 case ICNSEntry::GroupThumbnail:
514 icon.width = 128;
515 break;
516 default:
517 qWarning("parseIconEntryInfo(): Failed, 32bit icon from an unknown group. OSType: \"%s\"",
518 qPrintable(ostype));
519 }
520 icon.height = icon.width;
521 }
522 // Sanity check
523 if (icon.width == 0 || icon.width > 4096)
524 return false;
525 return true;
526 }
527
528 static QImage readMask(const ICNSEntry &mask, QDataStream &stream)
529 {
530 if ((mask.flags & ICNSEntry::IsMask) == 0)
531 return QImage();
532 if (mask.depth != ICNSEntry::DepthMono && mask.depth != ICNSEntry::Depth8bit) {
533 qWarning("readMask(): Failed, unusual bit depth: %u OSType: \"%s\"",
534 mask.depth, nameFromOSType(mask.ostype).constData());
535 return QImage();
536 }
537 const bool isMono = mask.depth == ICNSEntry::DepthMono;
538 const bool doubleSize = mask.flags == ICNSEntry::IconPlusMask;
539 const quint32 imageDataSize = (mask.width * mask.height * mask.depth) / 8;
540 const qint64 pos = doubleSize ? (mask.dataOffset + imageDataSize) : mask.dataOffset;
541 const qint64 oldPos = stream.device()->pos();
542 if (!stream.device()->seek(pos))
543 return QImage();
544 QImage img(mask.width, mask.height, QImage::Format_RGB32);
545 quint8 byte = 0;
546 quint32 pixel = 0;
547 for (quint32 y = 0; y < mask.height; y++) {
548 QRgb *line = reinterpret_cast<QRgb *>(img.scanLine(y));
549 for (quint32 x = 0; x < mask.width; x++) {
550 if (pixel % (8 / mask.depth) == 0)
551 stream >> byte;
552 else if (isMono)
553 byte <<= 1;
554 const quint8 alpha = isMono ? (((byte >> 7) & 0x01) * 255) : byte;
555 line[x] = qRgb(alpha, alpha, alpha);
556 pixel++;
557 }
558 }
559 stream.device()->seek(oldPos);
560 return img;
561 }
562
563 template <ICNSEntry::Depth depth>
564 static QImage readLowDepthIcon(const ICNSEntry &icon, QDataStream &stream)
565 {
566 Q_STATIC_ASSERT(depth == ICNSEntry::DepthMono || depth == ICNSEntry::Depth4bit
567 || depth == ICNSEntry::Depth8bit);
568
569 const bool isMono = depth == ICNSEntry::DepthMono;
570 const QImage::Format format = isMono ? QImage::Format_Mono : QImage::Format_Indexed8;
571 const QVector<QRgb> colortable = getColorTable(depth);
572 if (colortable.isEmpty())
573 return QImage();
574 QImage img(icon.width, icon.height, format);
575 img.setColorTable(colortable);
576 quint32 pixel = 0;
577 quint8 byte = 0;
578 for (quint32 y = 0; y < icon.height; y++) {
579 for (quint32 x = 0; x < icon.width; x++) {
580 if (pixel % (8 / depth) == 0)
581 stream >> byte;
582 quint8 cindex;
583 switch (depth) {
584 case ICNSEntry::DepthMono:
585 cindex = (byte >> 7) & 0x01; // left 1 bit
586 byte <<= 1;
587 break;
588 case ICNSEntry::Depth4bit:
589 cindex = (byte >> 4) & 0x0F; // left 4 bits
590 byte <<= 4;
591 break;
592 default:
593 cindex = byte; // 8 bits
594 break;
595 }
596 img.setPixel(x, y, cindex);
597 pixel++;
598 }
599 }
600 return img;
601 }
602
603 static QImage read32bitIcon(const ICNSEntry &icon, QDataStream &stream)
604 {
605 QImage img = QImage(icon.width, icon.height, QImage::Format_RGB32);
606 if (icon.dataFormat != ICNSEntry::RLE24) {
607 for (quint32 y = 0; y < icon.height; y++) {
608 QRgb *line = reinterpret_cast<QRgb *>(img.scanLine(y));
609 for (quint32 x = 0; x < icon.width; x++) {
610 quint8 r, g, b, a;
611 stream >> r >> g >> b >> a;
612 line[x] = qRgb(r, g, b);
613 }
614 }
615 } else {
616 const quint32 estPxsNum = icon.width * icon.height;
617 const QByteArray &bytes = stream.device()->peek(4);
618 if (bytes.isEmpty())
619 return QImage();
620 // Zero-padding may be present:
621 if (qFromBigEndian<quint32>(*bytes.constData()) == 0)
622 stream.skipRawData(4);
623 for (quint8 colorNRun = 0; colorNRun < 3; colorNRun++) {
624 quint32 pixel = 0;
625 QRgb *line = 0;
626 while (pixel < estPxsNum && !stream.atEnd()) {
627 quint8 byte, value;
628 stream >> byte;
629 const bool bitIsClear = (byte & 0x80) == 0;
630 // If high bit is clear: run of different values; else: same value
631 quint8 runLength = bitIsClear ? ((0xFF & byte) + 1) : ((0xFF & byte) - 125);
632 // Length of the run for for different values: 1 <= len <= 128
633 // Length of the run for same values: 3 <= len <= 130
634 if (!bitIsClear)
635 stream >> value;
636 for (quint8 i = 0; i < runLength && pixel < estPxsNum; i++) {
637 if (bitIsClear)
638 stream >> value;
639 const quint32 y = pixel / icon.height;
640 const quint32 x = pixel - (icon.width * y);
641 if (pixel % icon.height == 0)
642 line = reinterpret_cast<QRgb *>(img.scanLine(y));
643 QRgb rgb = line[x];
644 const int r = (colorNRun == 0) ? value : qRed(rgb);
645 const int g = (colorNRun == 1) ? value : qGreen(rgb);
646 const int b = (colorNRun == 2) ? value : qBlue(rgb);
647 line[x] = qRgb(r, g, b);
648 pixel++;
649 }
650 }
651 }
652 }
653 return img;
654 }
655
656 QICNSHandler::QICNSHandler() :
657 m_currentIconIndex(0), m_state(ScanNotScanned)
658 {
659 }
660
661 bool QICNSHandler::canRead(QIODevice *device)
662 {
663 if (!device || !device->isReadable()) {
664 qWarning("QICNSHandler::canRead() called without a readable device");
665 return false;
666 }
667
668 if (device->peek(4) == QByteArrayLiteral("icns")) {
669 if (device->isSequential()) {
670 qWarning("QICNSHandler::canRead() called on a sequential device");
671 return false;
672 }
673 return true;
674 }
675
676 return false;
677 }
678
679 bool QICNSHandler::canRead() const
680 {
681 if (m_state == ScanNotScanned && !canRead(device()))
682 return false;
683
684 if (m_state != ScanError) {
685 setFormat(QByteArrayLiteral("icns"));
686 return true;
687 }
688
689 return false;
690 }
691
692 bool QICNSHandler::read(QImage *outImage)
693 {
694 QImage img;
695 if (!ensureScanned() || m_currentIconIndex >= m_icons.size()) {
696 qWarning("QICNSHandler::read(): The device wasn't parsed properly!");
697 return false;
698 }
699
700 const ICNSEntry &icon = m_icons.at(m_currentIconIndex);
701 QDataStream stream(device());
702 stream.setByteOrder(QDataStream::BigEndian);
703 if (!device()->seek(icon.dataOffset))
704 return false;
705
706 switch (icon.dataFormat) {
707 case ICNSEntry::RawIcon:
708 case ICNSEntry::RLE24:
709 if (qMin(icon.width, icon.height) == 0)
710 break;
711 switch (icon.depth) {
712 case ICNSEntry::DepthMono:
713 img = readLowDepthIcon<ICNSEntry::DepthMono>(icon, stream);
714 break;
715 case ICNSEntry::Depth4bit:
716 img = readLowDepthIcon<ICNSEntry::Depth4bit>(icon, stream);
717 break;
718 case ICNSEntry::Depth8bit:
719 img = readLowDepthIcon<ICNSEntry::Depth8bit>(icon, stream);
720 break;
721 case ICNSEntry::Depth32bit:
722 img = read32bitIcon(icon, stream);
723 break;
724 default:
725 qWarning("QICNSHandler::read(): Failed, unsupported icon bit depth: %u, OSType: \"%s\"",
726 icon.depth, nameFromOSType(icon.ostype).constData());
727 }
728 if (!img.isNull()) {
729 QImage alpha = readMask(getIconMask(icon), stream);
730 if (!alpha.isNull())
731 img.setAlphaChannel(alpha);
732 }
733 break;
734 default:
735 const char *format = 0;
736 if (icon.dataFormat == ICNSEntry::PNG)
737 format = "png";
738 else if (icon.dataFormat == ICNSEntry::JP2)
739 format = "jp2";
740 // Even if JP2 or PNG magic is not detected, try anyway for unknown formats
741 img = QImage::fromData(device()->read(icon.dataLength), format);
742 if (img.isNull()) {
743 if (format == 0)
744 format = "unknown";
745 qWarning("QICNSHandler::read(): Failed, compressed format \"%s\" is not supported " \
746 "by your Qt library or this file is corrupt. OSType: \"%s\"",
747 format, nameFromOSType(icon.ostype).constData());
748 }
749 }
750 *outImage = img;
751 return !img.isNull();
752 }
753
754 bool QICNSHandler::write(const QImage &image)
755 {
756 /*
757 Notes:
758 * Experimental implementation. Just for simple converting tasks / testing purposes.
759 * Min. size is 16x16, Max. size is 1024x1024.
760 * Performs downscale to a square image if width != height.
761 * Performs upscale to 16x16, if the image is smaller.
762 * Performs downscale to a nearest power of two if size is not a power of two.
763 * Currently uses non-hardcoded OSTypes.
764 */
765 QIODevice *device = this->device();
766 if (!device->isWritable() || image.isNull() || qMin(image.width(), image.height()) == 0)
767 return false;
768 const int minSize = qMin(image.width(), image.height());
769 const int oldSize = (minSize < 16) ? 16 : minSize;
770 // Calc power of two:
771 int size = oldSize;
772 uint pow = 0;
773 // Note: Values over 10 are reserved for retina icons.
774 while (pow < 10 && (size >>= 1))
775 pow++;
776 const int newSize = 1 << pow;
777 QImage img = image;
778 // Let's enforce resizing if size differs:
779 if (newSize != oldSize || qMax(image.width(), image.height()) != minSize)
780 img = img.scaled(newSize, newSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
781 // Construct OSType and headers:
782 const quint32 ostype = nameToOSType(nameForCompressedIcon(pow));
783 ICNSBlockHeader fileHeader;
784 fileHeader.ostype = ICNSBlockHeader::TypeIcns;
785 ICNSBlockHeader tocHeader;
786 tocHeader.ostype = ICNSBlockHeader::TypeToc;
787 ICNSBlockHeader iconEntry;
788 iconEntry.ostype = ostype;
789 QByteArray imageData;
790 QBuffer buffer(&imageData);
791 if (!buffer.open(QIODevice::WriteOnly) || !img.save(&buffer, "png"))
792 return false;
793 buffer.close();
794 iconEntry.length = ICNSBlockHeaderSize + imageData.size();
795 tocHeader.length = ICNSBlockHeaderSize * 2;
796 fileHeader.length = ICNSBlockHeaderSize + tocHeader.length + iconEntry.length;
797 if (!isBlockHeaderValid(iconEntry))
798 return false;
799
800 QDataStream stream(device);
801 // iconEntry is also a TOC entry
802 stream << fileHeader << tocHeader << iconEntry << iconEntry;
803 stream.writeRawData(imageData.constData(), imageData.size());
804 return stream.status() == QDataStream::Ok;
805 }
806
807 bool QICNSHandler::supportsOption(ImageOption option) const
808 {
809 return option == SubType;
810 }
811
812 QVariant QICNSHandler::option(ImageOption option) const
813 {
814 if (!supportsOption(option) || !ensureScanned())
815 return QVariant();
816
817 if (option == SubType) {
818 if (imageCount() > 0 && m_currentIconIndex <= imageCount()) {
819 const ICNSEntry &icon = m_icons.at(m_currentIconIndex);
820 if (icon.variant != 0)
821 return QByteArray(nameFromOSType(icon.variant) + '-' + nameFromOSType(icon.ostype));
822 return nameFromOSType(icon.ostype);
823 }
824 }
825
826 return QVariant();
827 }
828
829 int QICNSHandler::imageCount() const
830 {
831 if (!ensureScanned())
832 return 0;
833
834 return m_icons.size();
835 }
836
837 bool QICNSHandler::jumpToImage(int imageNumber)
838 {
839 if (imageNumber >= imageCount())
840 return false;
841
842 m_currentIconIndex = imageNumber;
843 return true;
844 }
845
846 bool QICNSHandler::jumpToNextImage()
847 {
848 return jumpToImage(m_currentIconIndex + 1);
849 }
850
851 bool QICNSHandler::ensureScanned() const
852 {
853 if (m_state == ScanNotScanned) {
854 QICNSHandler *that = const_cast<QICNSHandler *>(this);
855 that->m_state = that->scanDevice() ? ScanSuccess : ScanError;
856 }
857
858 return m_state == ScanSuccess;
859 }
860
861 bool QICNSHandler::addEntry(const ICNSBlockHeader &header, qint64 imgDataOffset, quint32 variant)
862 {
863 // Note: This function returns false only when a device positioning error occurred
864 ICNSEntry entry;
865 entry.ostype = header.ostype;
866 entry.variant = variant;
867 entry.dataOffset = imgDataOffset;
868 entry.dataLength = header.length - ICNSBlockHeaderSize;
869 // Check for known magic numbers:
870 if (!parseIconEntryData(entry, device()))
871 return false;
872 // Parse everything else and index this entry:
873 if (parseIconEntryInfo(entry)) {
874 if ((entry.flags & ICNSEntry::IsMask) != 0)
875 m_masks << entry;
876 if ((entry.flags & ICNSEntry::IsIcon) != 0)
877 m_icons << entry;
878 }
879 return true;
880 }
881
882 bool QICNSHandler::scanDevice()
883 {
884 if (m_state == ScanSuccess)
885 return true;
886
887 if (!device()->seek(0))
888 return false;
889
890 QDataStream stream(device());
891 stream.setByteOrder(QDataStream::BigEndian);
892
893 bool scanIsIncomplete = false;
894 qint64 filelength = device()->size();
895 ICNSBlockHeader blockHeader;
896 while (!stream.atEnd() || device()->pos() < filelength) {
897 stream >> blockHeader;
898 if (stream.status() != QDataStream::Ok)
899 return false;
900
901 const qint64 blockDataOffset = device()->pos();
902 if (!isBlockHeaderValid(blockHeader, ICNSBlockHeaderSize + filelength - blockDataOffset)) {
903 qWarning("QICNSHandler::scanDevice(): Failed, bad header at pos %s. OSType \"%s\", length %u",
904 QByteArray::number(blockDataOffset).constData(),
905 nameFromOSType(blockHeader.ostype).constData(), blockHeader.length);
906 return false;
907 }
908 const quint64 blockDataLength = blockHeader.length - ICNSBlockHeaderSize;
909 const qint64 nextBlockOffset = blockDataOffset + blockDataLength;
910
911 switch (blockHeader.ostype) {
912 case ICNSBlockHeader::TypeIcns:
913 if (blockDataOffset != ICNSBlockHeaderSize) {
914 // Icns container definition should be in the beginning of the device.
915 // If we meet this block somewhere else, then just ignore it.
916 stream.skipRawData(blockDataLength);
917 break;
918 }
919 filelength = blockHeader.length;
920 if (device()->size() < blockHeader.length) {
921 qWarning("QICNSHandler::scanDevice(): Failed, file is incomplete.");
922 return false;
923 }
924 break;
925 case ICNSBlockHeader::TypeIcnv:
926 case ICNSBlockHeader::TypeClut:
927 // We don't have a good use for these blocks... yet.
928 stream.skipRawData(blockDataLength);
929 break;
930 case ICNSBlockHeader::TypeTile:
931 case ICNSBlockHeader::TypeOver:
932 case ICNSBlockHeader::TypeOpen:
933 case ICNSBlockHeader::TypeDrop:
934 case ICNSBlockHeader::TypeOdrp:
935 // Icns container seems to have an embedded icon variant container
936 // Let's start a scan for entries
937 while (!stream.atEnd() && device()->pos() < nextBlockOffset) {
938 ICNSBlockHeader icon;
939 stream >> icon;
940 if (stream.status() != QDataStream::Ok)
941 return false;
942 // Check for incorrect variant entry header and stop scan
943 quint64 remaining = blockDataLength - (device()->pos() - blockDataOffset);
944 if (!isBlockHeaderValid(icon, ICNSBlockHeaderSize + remaining))
945 break;
946 if (!addEntry(icon, device()->pos(), blockHeader.ostype))
947 return false;
948 if (stream.skipRawData(icon.length - ICNSBlockHeaderSize) < 0)
949 return false;
950 }
951 if (device()->pos() != nextBlockOffset) {
952 // Scan of this container didn't end where we expected.
953 // Let's generate some output about this incident:
954 qWarning("Scan of the icon variant container (\"%s\") failed at pos %s.\n" \
955 "Reason: Scan didn't reach the end of this container's block, " \
956 "delta: %s bytes. This file may be corrupted.",
957 nameFromOSType(blockHeader.ostype).constData(),
958 QByteArray::number(device()->pos()).constData(),
959 QByteArray::number(nextBlockOffset - device()->pos()).constData());
960 if (!device()->seek(nextBlockOffset))
961 return false;
962 }
963 break;
964 case ICNSBlockHeader::TypeToc: {
965 // Quick scan, table of contents
966 if (blockDataOffset != ICNSBlockHeaderSize * 2) {
967 // TOC should be the first block in the file after container definition.
968 // Ignore and go on with a deep scan.
969 stream.skipRawData(blockDataLength);
970 break;
971 }
972 // First image data offset:
973 qint64 imgDataOffset = blockDataOffset + blockHeader.length;
974 for (uint i = 0, count = blockDataLength / ICNSBlockHeaderSize; i < count; i++) {
975 ICNSBlockHeader tocEntry;
976 stream >> tocEntry;
977 if (!isBlockHeaderValid(tocEntry)) {
978 // TOC contains incorrect header, we should skip TOC since we can't trust it
979 qWarning("QICNSHandler::scanDevice(): Warning! Table of contents contains a bad " \
980 "entry! Stop at device pos: %s bytes. This file may be corrupted.",
981 QByteArray::number(device()->pos()).constData());
982 if (!device()->seek(nextBlockOffset))
983 return false;
984 break;
985 }
986 if (!addEntry(tocEntry, imgDataOffset))
987 return false;
988 imgDataOffset += tocEntry.length;
989 // If TOC covers all the blocks in the file, then quick scan is complete
990 if (imgDataOffset == filelength)
991 return true;
992 }
993 // Else just start a deep scan to salvage anything left after TOC's end
994 scanIsIncomplete = true;
995 break;
996 }
997 default:
998 // Deep scan, block by block
999 if (scanIsIncomplete) {
1000 // Check if entry with this offset is added somewhere
1001 // But only if we have incomplete TOC, otherwise just try to add
1002 bool exists = false;
1003 for (int i = 0; i < m_icons.size() && !exists; i++)
1004 exists = m_icons.at(i).dataOffset == blockDataOffset;
1005 for (int i = 0; i < m_masks.size() && !exists; i++)
1006 exists = m_masks.at(i).dataOffset == blockDataOffset;
1007 if (!exists && !addEntry(blockHeader, blockDataOffset))
1008 return false;
1009 } else if (!addEntry(blockHeader, blockDataOffset)) {
1010 return false;
1011 }
1012 stream.skipRawData(blockDataLength);
1013 break;
1014 }
1015 }
1016 return (m_icons.size() > 0);
1017 }
1018
1019 const ICNSEntry &QICNSHandler::getIconMask(const ICNSEntry &icon) const
1020 {
1021 const bool is32bit = icon.depth == ICNSEntry::Depth32bit;
1022 ICNSEntry::Depth targetDepth = is32bit ? ICNSEntry::Depth8bit : ICNSEntry::DepthMono;
1023 for (int i = 0; i < m_masks.size(); i++) {
1024 const ICNSEntry &mask = m_masks.at(i);
1025 if (isMaskSuitable(mask, icon, targetDepth))
1026 return mask;
1027 }
1028 return icon;
1029 }
1030
1031 QT_END_NAMESPACE
1032
1033 #endif // QT_NO_DATASTREAM