Debian Codegraph @

src/plugins/imageformats/icns/qicnshandler.cpp

raw symbols ◀ ▶ SCIP
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 &&
363 (bound == 0 ||
364 // qBound can be used but requires checking the limits first
365 // this requires less operations
366 (ICNSBlockHeaderSize <= header.length && header.length <= bound));
367 }
368
369 static inline bool isIconCompressed(const ICNSEntry &icon)
370 {
371 return icon.dataFormat == ICNSEntry::PNG || icon.dataFormat == ICNSEntry::JP2;
372 }
373
374 static inline bool isMaskSuitable(const ICNSEntry &mask, const ICNSEntry &icon, ICNSEntry::Depth target)
375 {
376 return mask.variant == icon.variant && mask.depth == target
377 && mask.height == icon.height && mask.width == icon.width;
378 }
379
380 static inline QByteArray nameFromOSType(quint32 ostype)
381 {
382 const quint32 bytes = qToBigEndian(ostype);
383 return QByteArray((const char*)&bytes, 4);
384 }
385
386 static inline quint32 nameToOSType(const QByteArray &ostype)
387 {
388 if (ostype.size() != 4)
389 return 0;
390 return qFromBigEndian(*reinterpret_cast<const quint32*>(ostype.constData()));
391 }
392
393 static inline QByteArray nameForCompressedIcon(quint8 iconNumber)
394 {
395 const bool portable = iconNumber < 7;
396 const QByteArray base = portable ? QByteArrayLiteral("icp") : QByteArrayLiteral("ic");
397 if (!portable && iconNumber < 10)
398 return base + "0" + QByteArray::number(iconNumber);
399 return base + QByteArray::number(iconNumber);
400 }
401
402 static inline QVector<QRgb> getColorTable(ICNSEntry::Depth depth)
403 {
404 QVector<QRgb> table;
405 uint n = 1 << depth;
406 const QRgb *data;
407 switch (depth) {
408 case ICNSEntry::DepthMono:
409 data = ICNSColorTableMono;
410 break;
411 case ICNSEntry::Depth4bit:
412 data = ICNSColorTable4bit;
413 break;
414 case ICNSEntry::Depth8bit:
415 data = ICNSColorTable8bit;
416 break;
417 default:
418 Q_UNREACHABLE();
419 break;
420 }
421 table.resize(n);
422 memcpy(table.data(), data, sizeof(QRgb) * n);
423 return table;
424 }
425
426 static bool parseIconEntryData(ICNSEntry &icon, QIODevice *device)
427 {
428 const qint64 oldPos = device->pos();
429 if (oldPos != icon.dataOffset && !device->seek(icon.dataOffset))
430 return false;
431
432 const QByteArray magic = device->peek(12);
433 const bool isPNG = magic.startsWith(QByteArrayLiteral("\211PNG\r\n\032\n\000\000\000\r"));
434 const bool isJP2 = !isPNG && magic == QByteArrayLiteral("\000\000\000\014jP \r\n\207\n");
435 if (isPNG || isJP2) {
436 // TODO: Add parsing of png/jp2 headers to enable feature reporting by plugin?
437 icon.flags = ICNSEntry::IsIcon;
438 icon.dataFormat = isPNG? ICNSEntry::PNG : ICNSEntry::JP2;
439 }
440 if (oldPos != icon.dataOffset && !device->seek(oldPos))
441 return false;
442 return true;
443 }
444
445 static bool parseIconEntryInfo(ICNSEntry &icon)
446 {
447 const QString ostype = QString::fromLatin1(nameFromOSType(icon.ostype));
448 // Typical OSType naming: <junk><group><depth><mask>;
449 // For icons OSType should be strictly alphanumeric + '#' character for masks/mono.
450 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})$");
451 QRegularExpression regexp(ptrn);
452 QRegularExpressionMatch match = regexp.match(ostype);
453 if (!match.hasMatch()) {
454 qWarning("parseIconEntryInfo(): Failed, OSType doesn't match: \"%s\"", qPrintable(ostype));
455 return false;
456 }
457 const QString group = match.captured(QStringLiteral("group"));
458 const QString depth = match.captured(QStringLiteral("depth"));
459 const QString mask = match.captured(QStringLiteral("mask"));
460 // Icon group:
461 if (!group.isEmpty())
462 icon.group = ICNSEntry::Group(group.at(0).toLatin1());
463
464 // That's enough for compressed ones
465 if (isIconCompressed(icon))
466 return true;
467 // Icon depth:
468 if (!depth.isEmpty()) {
469 const uint depthUInt = depth.toUInt();
470 if (depthUInt > 32)
471 return false;
472 icon.depth = ICNSEntry::Depth(depthUInt);
473 }
474 // Try mono if depth not found
475 if (icon.depth == ICNSEntry::DepthUnknown)
476 icon.depth = ICNSEntry::DepthMono;
477 // Detect size:
478 const qreal bytespp = (qreal)icon.depth / 8;
479 const qreal r1 = qSqrt(icon.dataLength / bytespp);
480 const qreal r2 = qSqrt((icon.dataLength / bytespp) / 2);
481 const quint32 r1u = qRound(r1);
482 const quint32 r2u = qRound(r2);
483 const bool singleEntry = isPowOf2OrDividesBy16(r1u, r1);
484 const bool doubleSize = isPowOf2OrDividesBy16(r2u, r2);
485 if (singleEntry) {
486 icon.flags = mask.isEmpty() ? ICNSEntry::IsIcon : ICNSEntry::IsMask;
487 icon.dataFormat = ICNSEntry::RawIcon;
488 icon.width = r1u;
489 icon.height = r1u;
490 } else if (doubleSize) {
491 icon.flags = ICNSEntry::IconPlusMask;
492 icon.dataFormat = ICNSEntry::RawIcon;
493 icon.width = r2u;
494 icon.height = r2u;
495 } else if (icon.group == ICNSEntry::GroupMini) {
496 // Legacy 16x12 icons are an exception from the generic square formula
497 const bool doubleSize = icon.dataLength == 192 * bytespp * 2;
498 icon.flags = doubleSize ? ICNSEntry::IconPlusMask : ICNSEntry::IsIcon;
499 icon.dataFormat = ICNSEntry::RawIcon;
500 icon.width = 16;
501 icon.height = 12;
502 } else if (icon.depth == ICNSEntry::Depth32bit) {
503 // We have a formula mismatch in a 32bit icon there, probably RLE24
504 icon.dataFormat = ICNSEntry::RLE24;
505 icon.flags = mask.isEmpty() ? ICNSEntry::IsIcon : ICNSEntry::IsMask;
506 switch (icon.group) {
507 case ICNSEntry::GroupSmall:
508 icon.width = 16;
509 break;
510 case ICNSEntry::GroupLarge:
511 icon.width = 32;
512 break;
513 case ICNSEntry::GroupHuge:
514 icon.width = 48;
515 break;
516 case ICNSEntry::GroupThumbnail:
517 icon.width = 128;
518 break;
519 default:
520 qWarning("parseIconEntryInfo(): Failed, 32bit icon from an unknown group. OSType: \"%s\"",
521 qPrintable(ostype));
522 }
523 icon.height = icon.width;
524 }
525 // Sanity check
526 if (icon.width == 0 || icon.width > 4096)
527 return false;
528 return true;
529 }
530
531 static QImage readMask(const ICNSEntry &mask, QDataStream &stream)
532 {
533 if ((mask.flags & ICNSEntry::IsMask) == 0)
534 return QImage();
535 if (mask.depth != ICNSEntry::DepthMono && mask.depth != ICNSEntry::Depth8bit) {
536 qWarning("readMask(): Failed, unusual bit depth: %u OSType: \"%s\"",
537 mask.depth, nameFromOSType(mask.ostype).constData());
538 return QImage();
539 }
540 const bool isMono = mask.depth == ICNSEntry::DepthMono;
541 const bool doubleSize = mask.flags == ICNSEntry::IconPlusMask;
542 const quint32 imageDataSize = (mask.width * mask.height * mask.depth) / 8;
543 const qint64 pos = doubleSize ? (mask.dataOffset + imageDataSize) : mask.dataOffset;
544 const qint64 oldPos = stream.device()->pos();
545 if (!stream.device()->seek(pos))
546 return QImage();
547 QImage img(mask.width, mask.height, QImage::Format_RGB32);
548 quint8 byte = 0;
549 quint32 pixel = 0;
550 for (quint32 y = 0; y < mask.height; y++) {
551 QRgb *line = reinterpret_cast<QRgb *>(img.scanLine(y));
552 for (quint32 x = 0; x < mask.width; x++) {
553 if (pixel % (8 / mask.depth) == 0)
554 stream >> byte;
555 else if (isMono)
556 byte <<= 1;
557 const quint8 alpha = isMono ? (((byte >> 7) & 0x01) * 255) : byte;
558 line[x] = qRgb(alpha, alpha, alpha);
559 pixel++;
560 }
561 }
562 stream.device()->seek(oldPos);
563 return img;
564 }
565
566 template <ICNSEntry::Depth depth>
567 static QImage readLowDepthIcon(const ICNSEntry &icon, QDataStream &stream)
568 {
569 Q_STATIC_ASSERT(depth == ICNSEntry::DepthMono || depth == ICNSEntry::Depth4bit
570 || depth == ICNSEntry::Depth8bit);
571
572 const bool isMono = depth == ICNSEntry::DepthMono;
573 const QImage::Format format = isMono ? QImage::Format_Mono : QImage::Format_Indexed8;
574 const QVector<QRgb> colortable = getColorTable(depth);
575 if (colortable.isEmpty())
576 return QImage();
577 QImage img(icon.width, icon.height, format);
578 img.setColorTable(colortable);
579 quint32 pixel = 0;
580 quint8 byte = 0;
581 for (quint32 y = 0; y < icon.height; y++) {
582 for (quint32 x = 0; x < icon.width; x++) {
583 if (pixel % (8 / depth) == 0)
584 stream >> byte;
585 quint8 cindex;
586 switch (depth) {
587 case ICNSEntry::DepthMono:
588 cindex = (byte >> 7) & 0x01; // left 1 bit
589 byte <<= 1;
590 break;
591 case ICNSEntry::Depth4bit:
592 cindex = (byte >> 4) & 0x0F; // left 4 bits
593 byte <<= 4;
594 break;
595 default:
596 cindex = byte; // 8 bits
597 break;
598 }
599 img.setPixel(x, y, cindex);
600 pixel++;
601 }
602 }
603 return img;
604 }
605
606 static QImage read32bitIcon(const ICNSEntry &icon, QDataStream &stream)
607 {
608 QImage img = QImage(icon.width, icon.height, QImage::Format_RGB32);
609 if (icon.dataFormat != ICNSEntry::RLE24) {
610 for (quint32 y = 0; y < icon.height; y++) {
611 QRgb *line = reinterpret_cast<QRgb *>(img.scanLine(y));
612 for (quint32 x = 0; x < icon.width; x++) {
613 quint8 r, g, b, a;
614 stream >> r >> g >> b >> a;
615 line[x] = qRgb(r, g, b);
616 }
617 }
618 } else {
619 const quint32 estPxsNum = icon.width * icon.height;
620 const QByteArray &bytes = stream.device()->peek(4);
621 if (bytes.isEmpty())
622 return QImage();
623 // Zero-padding may be present:
624 if (qFromBigEndian<quint32>(*bytes.constData()) == 0)
625 stream.skipRawData(4);
626 for (quint8 colorNRun = 0; colorNRun < 3; colorNRun++) {
627 quint32 pixel = 0;
628 QRgb *line = 0;
629 while (pixel < estPxsNum && !stream.atEnd()) {
630 quint8 byte, value;
631 stream >> byte;
632 const bool bitIsClear = (byte & 0x80) == 0;
633 // If high bit is clear: run of different values; else: same value
634 quint8 runLength = bitIsClear ? ((0xFF & byte) + 1) : ((0xFF & byte) - 125);
635 // Length of the run for for different values: 1 <= len <= 128
636 // Length of the run for same values: 3 <= len <= 130
637 if (!bitIsClear)
638 stream >> value;
639 for (quint8 i = 0; i < runLength && pixel < estPxsNum; i++) {
640 if (bitIsClear)
641 stream >> value;
642 const quint32 y = pixel / icon.height;
643 const quint32 x = pixel - (icon.width * y);
644 if (pixel % icon.height == 0)
645 line = reinterpret_cast<QRgb *>(img.scanLine(y));
646 QRgb rgb = line[x];
647 const int r = (colorNRun == 0) ? value : qRed(rgb);
648 const int g = (colorNRun == 1) ? value : qGreen(rgb);
649 const int b = (colorNRun == 2) ? value : qBlue(rgb);
650 line[x] = qRgb(r, g, b);
651 pixel++;
652 }
653 }
654 }
655 }
656 return img;
657 }
658
659 QICNSHandler::QICNSHandler() :
660 m_currentIconIndex(0), m_state(ScanNotScanned)
661 {
662 }
663
664 bool QICNSHandler::canRead(QIODevice *device)
665 {
666 if (!device || !device->isReadable()) {
667 qWarning("QICNSHandler::canRead() called without a readable device");
668 return false;
669 }
670
671 if (device->peek(4) == QByteArrayLiteral("icns")) {
672 if (device->isSequential()) {
673 qWarning("QICNSHandler::canRead() called on a sequential device");
674 return false;
675 }
676 return true;
677 }
678
679 return false;
680 }
681
682 bool QICNSHandler::canRead() const
683 {
684 if (m_state == ScanNotScanned && !canRead(device()))
685 return false;
686
687 if (m_state != ScanError) {
688 setFormat(QByteArrayLiteral("icns"));
689 return true;
690 }
691
692 return false;
693 }
694
695 bool QICNSHandler::read(QImage *outImage)
696 {
697 QImage img;
698 if (!ensureScanned() || m_currentIconIndex >= m_icons.size()) {
699 qWarning("QICNSHandler::read(): The device wasn't parsed properly!");
700 return false;
701 }
702
703 const ICNSEntry &icon = m_icons.at(m_currentIconIndex);
704 QDataStream stream(device());
705 stream.setByteOrder(QDataStream::BigEndian);
706 if (!device()->seek(icon.dataOffset))
707 return false;
708
709 switch (icon.dataFormat) {
710 case ICNSEntry::RawIcon:
711 case ICNSEntry::RLE24:
712 if (qMin(icon.width, icon.height) == 0)
713 break;
714 switch (icon.depth) {
715 case ICNSEntry::DepthMono:
716 img = readLowDepthIcon<ICNSEntry::DepthMono>(icon, stream);
717 break;
718 case ICNSEntry::Depth4bit:
719 img = readLowDepthIcon<ICNSEntry::Depth4bit>(icon, stream);
720 break;
721 case ICNSEntry::Depth8bit:
722 img = readLowDepthIcon<ICNSEntry::Depth8bit>(icon, stream);
723 break;
724 case ICNSEntry::Depth32bit:
725 img = read32bitIcon(icon, stream);
726 break;
727 default:
728 qWarning("QICNSHandler::read(): Failed, unsupported icon bit depth: %u, OSType: \"%s\"",
729 icon.depth, nameFromOSType(icon.ostype).constData());
730 }
731 if (!img.isNull()) {
732 QImage alpha = readMask(getIconMask(icon), stream);
733 if (!alpha.isNull())
734 img.setAlphaChannel(alpha);
735 }
736 break;
737 default:
738 const char *format = 0;
739 if (icon.dataFormat == ICNSEntry::PNG)
740 format = "png";
741 else if (icon.dataFormat == ICNSEntry::JP2)
742 format = "jp2";
743 // Even if JP2 or PNG magic is not detected, try anyway for unknown formats
744 img = QImage::fromData(device()->read(icon.dataLength), format);
745 if (img.isNull()) {
746 if (format == 0)
747 format = "unknown";
748 qWarning("QICNSHandler::read(): Failed, compressed format \"%s\" is not supported " \
749 "by your Qt library or this file is corrupt. OSType: \"%s\"",
750 format, nameFromOSType(icon.ostype).constData());
751 }
752 }
753 *outImage = img;
754 return !img.isNull();
755 }
756
757 bool QICNSHandler::write(const QImage &image)
758 {
759 /*
760 Notes:
761 * Experimental implementation. Just for simple converting tasks / testing purposes.
762 * Min. size is 16x16, Max. size is 1024x1024.
763 * Performs downscale to a square image if width != height.
764 * Performs upscale to 16x16, if the image is smaller.
765 * Performs downscale to a nearest power of two if size is not a power of two.
766 * Currently uses non-hardcoded OSTypes.
767 */
768 QIODevice *device = this->device();
769 if (!device->isWritable() || image.isNull() || qMin(image.width(), image.height()) == 0)
770 return false;
771 const int minSize = qMin(image.width(), image.height());
772 const int oldSize = (minSize < 16) ? 16 : minSize;
773 // Calc power of two:
774 int size = oldSize;
775 uint pow = 0;
776 // Note: Values over 10 are reserved for retina icons.
777 while (pow < 10 && (size >>= 1))
778 pow++;
779 const int newSize = 1 << pow;
780 QImage img = image;
781 // Let's enforce resizing if size differs:
782 if (newSize != oldSize || qMax(image.width(), image.height()) != minSize)
783 img = img.scaled(newSize, newSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
784 // Construct OSType and headers:
785 const quint32 ostype = nameToOSType(nameForCompressedIcon(pow));
786 ICNSBlockHeader fileHeader;
787 fileHeader.ostype = ICNSBlockHeader::TypeIcns;
788 ICNSBlockHeader tocHeader;
789 tocHeader.ostype = ICNSBlockHeader::TypeToc;
790 ICNSBlockHeader iconEntry;
791 iconEntry.ostype = ostype;
792 QByteArray imageData;
793 QBuffer buffer(&imageData);
794 if (!buffer.open(QIODevice::WriteOnly) || !img.save(&buffer, "png"))
795 return false;
796 buffer.close();
797 iconEntry.length = ICNSBlockHeaderSize + imageData.size();
798 tocHeader.length = ICNSBlockHeaderSize * 2;
799 fileHeader.length = ICNSBlockHeaderSize + tocHeader.length + iconEntry.length;
800 if (!isBlockHeaderValid(iconEntry))
801 return false;
802
803 QDataStream stream(device);
804 // iconEntry is also a TOC entry
805 stream << fileHeader << tocHeader << iconEntry << iconEntry;
806 stream.writeRawData(imageData.constData(), imageData.size());
807 return stream.status() == QDataStream::Ok;
808 }
809
810 bool QICNSHandler::supportsOption(ImageOption option) const
811 {
812 return option == SubType;
813 }
814
815 QVariant QICNSHandler::option(ImageOption option) const
816 {
817 if (!supportsOption(option) || !ensureScanned())
818 return QVariant();
819
820 if (option == SubType) {
821 if (imageCount() > 0 && m_currentIconIndex <= imageCount()) {
822 const ICNSEntry &icon = m_icons.at(m_currentIconIndex);
823 if (icon.variant != 0)
824 return QByteArray(nameFromOSType(icon.variant) + '-' + nameFromOSType(icon.ostype));
825 return nameFromOSType(icon.ostype);
826 }
827 }
828
829 return QVariant();
830 }
831
832 int QICNSHandler::imageCount() const
833 {
834 if (!ensureScanned())
835 return 0;
836
837 return m_icons.size();
838 }
839
840 bool QICNSHandler::jumpToImage(int imageNumber)
841 {
842 if (imageNumber >= imageCount())
843 return false;
844
845 m_currentIconIndex = imageNumber;
846 return true;
847 }
848
849 bool QICNSHandler::jumpToNextImage()
850 {
851 return jumpToImage(m_currentIconIndex + 1);
852 }
853
854 bool QICNSHandler::ensureScanned() const
855 {
856 if (m_state == ScanNotScanned) {
857 QICNSHandler *that = const_cast<QICNSHandler *>(this);
858 that->m_state = that->scanDevice() ? ScanSuccess : ScanError;
859 }
860
861 return m_state == ScanSuccess;
862 }
863
864 bool QICNSHandler::addEntry(const ICNSBlockHeader &header, qint64 imgDataOffset, quint32 variant)
865 {
866 // Note: This function returns false only when a device positioning error occurred
867 ICNSEntry entry;
868 entry.ostype = header.ostype;
869 entry.variant = variant;
870 entry.dataOffset = imgDataOffset;
871 entry.dataLength = header.length - ICNSBlockHeaderSize;
872 // Check for known magic numbers:
873 if (!parseIconEntryData(entry, device()))
874 return false;
875 // Parse everything else and index this entry:
876 if (parseIconEntryInfo(entry)) {
877 if ((entry.flags & ICNSEntry::IsMask) != 0)
878 m_masks << entry;
879 if ((entry.flags & ICNSEntry::IsIcon) != 0)
880 m_icons << entry;
881 }
882 return true;
883 }
884
885 bool QICNSHandler::scanDevice()
886 {
887 if (m_state == ScanSuccess)
888 return true;
889
890 if (!device()->seek(0))
891 return false;
892
893 QDataStream stream(device());
894 stream.setByteOrder(QDataStream::BigEndian);
895
896 bool scanIsIncomplete = false;
897 qint64 filelength = device()->size();
898 ICNSBlockHeader blockHeader;
899 while (!stream.atEnd() || device()->pos() < filelength) {
900 stream >> blockHeader;
901 if (stream.status() != QDataStream::Ok)
902 return false;
903
904 const qint64 blockDataOffset = device()->pos();
•905 if (!isBlockHeaderValid(blockHeader, ICNSBlockHeaderSize - blockDataOffset + filelength)) {
906 qWarning("QICNSHandler::scanDevice(): Failed, bad header at pos %s. OSType \"%s\", length %u",
907 QByteArray::number(blockDataOffset).constData(),
908 nameFromOSType(blockHeader.ostype).constData(), blockHeader.length);
909 return false;
910 }
911 const quint64 blockDataLength = blockHeader.length - ICNSBlockHeaderSize;
912 const qint64 nextBlockOffset = blockDataOffset + blockDataLength;
913
914 switch (blockHeader.ostype) {
915 case ICNSBlockHeader::TypeIcns:
916 if (blockDataOffset != ICNSBlockHeaderSize) {
917 // Icns container definition should be in the beginning of the device.
918 // If we meet this block somewhere else, then just ignore it.
919 stream.skipRawData(blockDataLength);
920 break;
921 }
922 filelength = blockHeader.length;
923 if (device()->size() < blockHeader.length) {
924 qWarning("QICNSHandler::scanDevice(): Failed, file is incomplete.");
925 return false;
926 }
927 break;
928 case ICNSBlockHeader::TypeIcnv:
929 case ICNSBlockHeader::TypeClut:
930 // We don't have a good use for these blocks... yet.
931 stream.skipRawData(blockDataLength);
932 break;
933 case ICNSBlockHeader::TypeTile:
934 case ICNSBlockHeader::TypeOver:
935 case ICNSBlockHeader::TypeOpen:
936 case ICNSBlockHeader::TypeDrop:
937 case ICNSBlockHeader::TypeOdrp:
938 // Icns container seems to have an embedded icon variant container
939 // Let's start a scan for entries
940 while (!stream.atEnd() && device()->pos() < nextBlockOffset) {
941 ICNSBlockHeader icon;
942 stream >> icon;
943 if (stream.status() != QDataStream::Ok)
944 return false;
945 // Check for incorrect variant entry header and stop scan
946 quint64 remaining = blockDataLength - (device()->pos() - blockDataOffset);
947 if (!isBlockHeaderValid(icon, ICNSBlockHeaderSize + remaining))
948 break;
949 if (!addEntry(icon, device()->pos(), blockHeader.ostype))
950 return false;
951 if (stream.skipRawData(icon.length - ICNSBlockHeaderSize) < 0)
952 return false;
953 }
954 if (device()->pos() != nextBlockOffset) {
955 // Scan of this container didn't end where we expected.
956 // Let's generate some output about this incident:
957 qWarning("Scan of the icon variant container (\"%s\") failed at pos %s.\n" \
958 "Reason: Scan didn't reach the end of this container's block, " \
959 "delta: %s bytes. This file may be corrupted.",
960 nameFromOSType(blockHeader.ostype).constData(),
961 QByteArray::number(device()->pos()).constData(),
962 QByteArray::number(nextBlockOffset - device()->pos()).constData());
963 if (!device()->seek(nextBlockOffset))
964 return false;
965 }
966 break;
967 case ICNSBlockHeader::TypeToc: {
968 // Quick scan, table of contents
969 if (blockDataOffset != ICNSBlockHeaderSize * 2) {
970 // TOC should be the first block in the file after container definition.
971 // Ignore and go on with a deep scan.
972 stream.skipRawData(blockDataLength);
973 break;
974 }
975 // First image data offset:
976 qint64 imgDataOffset = blockDataOffset + blockHeader.length;
977 for (uint i = 0, count = blockDataLength / ICNSBlockHeaderSize; i < count; i++) {
978 ICNSBlockHeader tocEntry;
979 stream >> tocEntry;
980 if (!isBlockHeaderValid(tocEntry)) {
981 // TOC contains incorrect header, we should skip TOC since we can't trust it
982 qWarning("QICNSHandler::scanDevice(): Warning! Table of contents contains a bad " \
983 "entry! Stop at device pos: %s bytes. This file may be corrupted.",
984 QByteArray::number(device()->pos()).constData());
985 if (!device()->seek(nextBlockOffset))
986 return false;
987 break;
988 }
989 if (!addEntry(tocEntry, imgDataOffset))
990 return false;
991 imgDataOffset += tocEntry.length;
992 // If TOC covers all the blocks in the file, then quick scan is complete
993 if (imgDataOffset == filelength)
994 return true;
995 }
996 // Else just start a deep scan to salvage anything left after TOC's end
997 scanIsIncomplete = true;
998 break;
999 }
1000 default:
1001 // Deep scan, block by block
1002 if (scanIsIncomplete) {
1003 // Check if entry with this offset is added somewhere
1004 // But only if we have incomplete TOC, otherwise just try to add
1005 bool exists = false;
1006 for (int i = 0; i < m_icons.size() && !exists; i++)
1007 exists = m_icons.at(i).dataOffset == blockDataOffset;
1008 for (int i = 0; i < m_masks.size() && !exists; i++)
1009 exists = m_masks.at(i).dataOffset == blockDataOffset;
1010 if (!exists && !addEntry(blockHeader, blockDataOffset))
1011 return false;
1012 } else if (!addEntry(blockHeader, blockDataOffset)) {
1013 return false;
1014 }
1015 stream.skipRawData(blockDataLength);
1016 break;
1017 }
1018 }
1019 return (m_icons.size() > 0);
1020 }
1021
1022 const ICNSEntry &QICNSHandler::getIconMask(const ICNSEntry &icon) const
1023 {
1024 const bool is32bit = icon.depth == ICNSEntry::Depth32bit;
1025 ICNSEntry::Depth targetDepth = is32bit ? ICNSEntry::Depth8bit : ICNSEntry::DepthMono;
1026 for (int i = 0; i < m_masks.size(); i++) {
1027 const ICNSEntry &mask = m_masks.at(i);
1028 if (isMaskSuitable(mask, icon, targetDepth))
1029 return mask;
1030 }
1031 return icon;
1032 }
1033
1034 QT_END_NAMESPACE
1035
1036 #endif // QT_NO_DATASTREAM