1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtGui module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "private/qbmphandler_p.h" |
41 | |
42 | #ifndef QT_NO_IMAGEFORMAT_BMP |
43 | |
44 | #include <qimage.h> |
45 | #include <qlist.h> |
46 | #include <qvariant.h> |
47 | |
48 | QT_BEGIN_NAMESPACE |
49 | |
50 | static void swapPixel01(QImage *image) // 1-bpp: swap 0 and 1 pixels |
51 | { |
52 | qsizetype i; |
53 | if (image->depth() == 1 && image->colorCount() == 2) { |
54 | uint *p = (uint *)image->bits(); |
55 | qsizetype nbytes = static_cast<qsizetype>(image->sizeInBytes()); |
56 | for (i=0; i<nbytes/4; i++) { |
57 | *p = ~*p; |
58 | p++; |
59 | } |
60 | uchar *p2 = (uchar *)p; |
61 | for (i=0; i<(nbytes&3); i++) { |
62 | *p2 = ~*p2; |
63 | p2++; |
64 | } |
65 | QRgb t = image->color(0); // swap color 0 and 1 |
66 | image->setColor(0, image->color(1)); |
67 | image->setColor(1, t); |
68 | } |
69 | } |
70 | |
71 | /* |
72 | QImageIO::defineIOHandler("BMP", "^BM", 0, |
73 | read_bmp_image, write_bmp_image); |
74 | */ |
75 | |
76 | /***************************************************************************** |
77 | BMP (DIB) image read/write functions |
78 | *****************************************************************************/ |
79 | |
80 | const int BMP_FILEHDR_SIZE = 14; // size of BMP_FILEHDR data |
81 | |
82 | static QDataStream &operator>>(QDataStream &s, BMP_FILEHDR &bf) |
83 | { // read file header |
84 | s.readRawData(bf.bfType, 2); |
85 | s >> bf.bfSize >> bf.bfReserved1 >> bf.bfReserved2 >> bf.bfOffBits; |
86 | return s; |
87 | } |
88 | |
89 | static QDataStream &operator<<(QDataStream &s, const BMP_FILEHDR &bf) |
90 | { // write file header |
91 | s.writeRawData(bf.bfType, 2); |
92 | s << bf.bfSize << bf.bfReserved1 << bf.bfReserved2 << bf.bfOffBits; |
93 | return s; |
94 | } |
95 | |
96 | |
97 | const int BMP_OLD = 12; // old Windows/OS2 BMP size |
98 | const int BMP_WIN = 40; // Windows BMP v3 size |
99 | const int BMP_OS2 = 64; // new OS/2 BMP size |
100 | const int BMP_WIN4 = 108; // Windows BMP v4 size |
101 | const int BMP_WIN5 = 124; // Windows BMP v5 size |
102 | |
103 | const int BMP_RGB = 0; // no compression |
104 | const int BMP_RLE8 = 1; // run-length encoded, 8 bits |
105 | const int BMP_RLE4 = 2; // run-length encoded, 4 bits |
106 | const int BMP_BITFIELDS = 3; // RGB values encoded in data as bit-fields |
107 | |
108 | |
109 | static QDataStream &operator>>(QDataStream &s, BMP_INFOHDR &bi) |
110 | { |
111 | s >> bi.biSize; |
112 | if (bi.biSize == BMP_WIN || bi.biSize == BMP_OS2 || bi.biSize == BMP_WIN4 || bi.biSize == BMP_WIN5) { |
113 | s >> bi.biWidth >> bi.biHeight >> bi.biPlanes >> bi.biBitCount; |
114 | s >> bi.biCompression >> bi.biSizeImage; |
115 | s >> bi.biXPelsPerMeter >> bi.biYPelsPerMeter; |
116 | s >> bi.biClrUsed >> bi.biClrImportant; |
117 | if (bi.biSize >= BMP_WIN4) { |
118 | s >> bi.biRedMask >> bi.biGreenMask >> bi.biBlueMask >> bi.biAlphaMask; |
119 | s >> bi.biCSType; |
120 | for (int i = 0; i < 9; ++i) |
121 | s >> bi.biEndpoints[i]; |
122 | s >> bi.biGammaRed >> bi.biGammaGreen >> bi.biGammaBlue; |
123 | if (bi.biSize == BMP_WIN5) |
124 | s >> bi.biIntent >> bi.biProfileData >> bi.biProfileSize >> bi.biReserved; |
125 | } |
126 | } |
127 | else { // probably old Windows format |
128 | qint16 w, h; |
129 | s >> w >> h >> bi.biPlanes >> bi.biBitCount; |
130 | bi.biWidth = w; |
131 | bi.biHeight = h; |
132 | bi.biCompression = BMP_RGB; // no compression |
133 | bi.biSizeImage = 0; |
134 | bi.biXPelsPerMeter = bi.biYPelsPerMeter = 0; |
135 | bi.biClrUsed = bi.biClrImportant = 0; |
136 | } |
137 | return s; |
138 | } |
139 | |
140 | static QDataStream &operator<<(QDataStream &s, const BMP_INFOHDR &bi) |
141 | { |
142 | s << bi.biSize; |
143 | s << bi.biWidth << bi.biHeight; |
144 | s << bi.biPlanes; |
145 | s << bi.biBitCount; |
146 | s << bi.biCompression; |
147 | s << bi.biSizeImage; |
148 | s << bi.biXPelsPerMeter << bi.biYPelsPerMeter; |
149 | s << bi.biClrUsed << bi.biClrImportant; |
150 | return s; |
151 | } |
152 | |
153 | static int calc_shift(uint mask) |
154 | { |
155 | int result = 0; |
156 | while (mask && !(mask & 1)) { |
157 | result++; |
158 | mask >>= 1; |
159 | } |
160 | return result; |
161 | } |
162 | |
163 | static bool (QDataStream &s, BMP_FILEHDR &bf) |
164 | { |
165 | // read BMP file header |
166 | s >> bf; |
167 | if (s.status() != QDataStream::Ok) |
168 | return false; |
169 | |
170 | // check header |
171 | if (qstrncmp(bf.bfType,"BM" ,2) != 0) |
172 | return false; |
173 | |
174 | return true; |
175 | } |
176 | |
177 | static bool (QDataStream &s, BMP_INFOHDR &bi) |
178 | { |
179 | s >> bi; // read BMP info header |
180 | if (s.status() != QDataStream::Ok) |
181 | return false; |
182 | |
183 | int nbits = bi.biBitCount; |
184 | int comp = bi.biCompression; |
185 | if (!(nbits == 1 || nbits == 4 || nbits == 8 || nbits == 16 || nbits == 24 || nbits == 32) || |
186 | bi.biPlanes != 1 || comp > BMP_BITFIELDS) |
187 | return false; // weird BMP image |
188 | if (!(comp == BMP_RGB || (nbits == 4 && comp == BMP_RLE4) || |
189 | (nbits == 8 && comp == BMP_RLE8) || ((nbits == 16 || nbits == 32) && comp == BMP_BITFIELDS))) |
190 | return false; // weird compression type |
191 | if (bi.biHeight == INT_MIN) |
192 | return false; // out of range for positive int |
193 | if (bi.biWidth <= 0 || !bi.biHeight || quint64(bi.biWidth) * qAbs(bi.biHeight) > 16384 * 16384) |
194 | return false; |
195 | |
196 | return true; |
197 | } |
198 | |
199 | static bool read_dib_body(QDataStream &s, const BMP_INFOHDR &bi, qint64 offset, qint64 startpos, QImage &image) |
200 | { |
201 | QIODevice* d = s.device(); |
202 | if (d->atEnd()) // end of stream/file |
203 | return false; |
204 | #if 0 |
205 | qDebug("offset...........%lld" , offset); |
206 | qDebug("startpos.........%lld" , startpos); |
207 | qDebug("biSize...........%d" , bi.biSize); |
208 | qDebug("biWidth..........%d" , bi.biWidth); |
209 | qDebug("biHeight.........%d" , bi.biHeight); |
210 | qDebug("biPlanes.........%d" , bi.biPlanes); |
211 | qDebug("biBitCount.......%d" , bi.biBitCount); |
212 | qDebug("biCompression....%d" , bi.biCompression); |
213 | qDebug("biSizeImage......%d" , bi.biSizeImage); |
214 | qDebug("biXPelsPerMeter..%d" , bi.biXPelsPerMeter); |
215 | qDebug("biYPelsPerMeter..%d" , bi.biYPelsPerMeter); |
216 | qDebug("biClrUsed........%d" , bi.biClrUsed); |
217 | qDebug("biClrImportant...%d" , bi.biClrImportant); |
218 | #endif |
219 | int w = bi.biWidth, h = bi.biHeight, nbits = bi.biBitCount; |
220 | int t = bi.biSize, comp = bi.biCompression; |
221 | uint red_mask = 0; |
222 | uint green_mask = 0; |
223 | uint blue_mask = 0; |
224 | uint alpha_mask = 0; |
225 | int red_shift = 0; |
226 | int green_shift = 0; |
227 | int blue_shift = 0; |
228 | int alpha_shift = 0; |
229 | int red_scale = 0; |
230 | int green_scale = 0; |
231 | int blue_scale = 0; |
232 | int alpha_scale = 0; |
233 | |
234 | if (!d->isSequential()) |
235 | d->seek(startpos + BMP_FILEHDR_SIZE + bi.biSize); // goto start of colormap or masks |
236 | |
237 | if (bi.biSize >= BMP_WIN4) { |
238 | red_mask = bi.biRedMask; |
239 | green_mask = bi.biGreenMask; |
240 | blue_mask = bi.biBlueMask; |
241 | alpha_mask = bi.biAlphaMask; |
242 | } else if (comp == BMP_BITFIELDS && (nbits == 16 || nbits == 32)) { |
243 | if (d->read((char *)&red_mask, sizeof(red_mask)) != sizeof(red_mask)) |
244 | return false; |
245 | if (d->read((char *)&green_mask, sizeof(green_mask)) != sizeof(green_mask)) |
246 | return false; |
247 | if (d->read((char *)&blue_mask, sizeof(blue_mask)) != sizeof(blue_mask)) |
248 | return false; |
249 | } |
250 | |
251 | bool transp = (comp == BMP_BITFIELDS) && alpha_mask; |
252 | int ncols = 0; |
253 | int depth = 0; |
254 | QImage::Format format; |
255 | switch (nbits) { |
256 | case 32: |
257 | case 24: |
258 | case 16: |
259 | depth = 32; |
260 | format = transp ? QImage::Format_ARGB32 : QImage::Format_RGB32; |
261 | break; |
262 | case 8: |
263 | case 4: |
264 | depth = 8; |
265 | format = QImage::Format_Indexed8; |
266 | break; |
267 | case 1: |
268 | depth = 1; |
269 | format = QImage::Format_Mono; |
270 | break; |
271 | default: |
272 | return false; |
273 | break; |
274 | } |
275 | |
276 | if (depth != 32) { |
277 | ncols = bi.biClrUsed ? bi.biClrUsed : 1 << nbits; |
278 | if (ncols < 1 || ncols > 256) // sanity check - don't run out of mem if color table is broken |
279 | return false; |
280 | } |
281 | |
282 | if (bi.biHeight < 0) |
283 | h = -h; // support images with negative height |
284 | |
285 | if (!QImageIOHandler::allocateImage(QSize(w, h), format, &image)) |
286 | return false; |
287 | if (ncols > 0) { // read color table |
288 | image.setColorCount(ncols); |
289 | uchar rgb[4]; |
290 | int rgb_len = t == BMP_OLD ? 3 : 4; |
291 | for (int i=0; i<ncols; i++) { |
292 | if (d->read((char *)rgb, rgb_len) != rgb_len) |
293 | return false; |
294 | image.setColor(i, qRgb(rgb[2],rgb[1],rgb[0])); |
295 | if (d->atEnd()) // truncated file |
296 | return false; |
297 | } |
298 | } else if (comp == BMP_BITFIELDS && (nbits == 16 || nbits == 32)) { |
299 | red_shift = calc_shift(red_mask); |
300 | if (((red_mask >> red_shift) + 1) == 0) |
301 | return false; |
302 | red_scale = 256 / ((red_mask >> red_shift) + 1); |
303 | green_shift = calc_shift(green_mask); |
304 | if (((green_mask >> green_shift) + 1) == 0) |
305 | return false; |
306 | green_scale = 256 / ((green_mask >> green_shift) + 1); |
307 | blue_shift = calc_shift(blue_mask); |
308 | if (((blue_mask >> blue_shift) + 1) == 0) |
309 | return false; |
310 | blue_scale = 256 / ((blue_mask >> blue_shift) + 1); |
311 | alpha_shift = calc_shift(alpha_mask); |
312 | if (((alpha_mask >> alpha_shift) + 1) == 0) |
313 | return false; |
314 | alpha_scale = 256 / ((alpha_mask >> alpha_shift) + 1); |
315 | } else if (comp == BMP_RGB && (nbits == 24 || nbits == 32)) { |
316 | blue_mask = 0x000000ff; |
317 | green_mask = 0x0000ff00; |
318 | red_mask = 0x00ff0000; |
319 | blue_shift = 0; |
320 | green_shift = 8; |
321 | red_shift = 16; |
322 | blue_scale = green_scale = red_scale = 1; |
323 | } else if (comp == BMP_RGB && nbits == 16) { |
324 | blue_mask = 0x001f; |
325 | green_mask = 0x03e0; |
326 | red_mask = 0x7c00; |
327 | blue_shift = 0; |
328 | green_shift = 2; |
329 | red_shift = 7; |
330 | red_scale = 1; |
331 | green_scale = 1; |
332 | blue_scale = 8; |
333 | } |
334 | |
335 | image.setDotsPerMeterX(bi.biXPelsPerMeter); |
336 | image.setDotsPerMeterY(bi.biYPelsPerMeter); |
337 | |
338 | #if 0 |
339 | qDebug("Rmask: %08x Rshift: %08x Rscale:%08x" , red_mask, red_shift, red_scale); |
340 | qDebug("Gmask: %08x Gshift: %08x Gscale:%08x" , green_mask, green_shift, green_scale); |
341 | qDebug("Bmask: %08x Bshift: %08x Bscale:%08x" , blue_mask, blue_shift, blue_scale); |
342 | qDebug("Amask: %08x Ashift: %08x Ascale:%08x" , alpha_mask, alpha_shift, alpha_scale); |
343 | #endif |
344 | |
345 | // offset can be bogus, be careful |
346 | if (offset>=0 && startpos + offset > d->pos()) { |
347 | if (!d->isSequential()) |
348 | d->seek(startpos + offset); // start of image data |
349 | } |
350 | |
351 | int bpl = image.bytesPerLine(); |
352 | uchar *data = image.bits(); |
353 | |
354 | if (nbits == 1) { // 1 bit BMP image |
355 | while (--h >= 0) { |
356 | if (d->read((char*)(data + h*bpl), bpl) != bpl) |
357 | break; |
358 | } |
359 | if (ncols == 2 && qGray(image.color(0)) < qGray(image.color(1))) |
360 | swapPixel01(&image); // pixel 0 is white! |
361 | } |
362 | |
363 | else if (nbits == 4) { // 4 bit BMP image |
364 | int buflen = ((w+7)/8)*4; |
365 | uchar *buf = new uchar[buflen]; |
366 | if (comp == BMP_RLE4) { // run length compression |
367 | int x=0, y=0, c, i; |
368 | quint8 b; |
369 | uchar *p = data + (h-1)*bpl; |
370 | const uchar *endp = p + w; |
371 | while (y < h) { |
372 | if (!d->getChar((char *)&b)) |
373 | break; |
374 | if (b == 0) { // escape code |
375 | if (!d->getChar((char *)&b) || b == 1) { |
376 | y = h; // exit loop |
377 | } else switch (b) { |
378 | case 0: // end of line |
379 | x = 0; |
380 | y++; |
381 | p = data + (h-y-1)*bpl; |
382 | break; |
383 | case 2: // delta (jump) |
384 | { |
385 | quint8 tmp; |
386 | d->getChar((char *)&tmp); |
387 | x += tmp; |
388 | d->getChar((char *)&tmp); |
389 | y += tmp; |
390 | } |
391 | |
392 | // Protection |
393 | if ((uint)x >= (uint)w) |
394 | x = w-1; |
395 | if ((uint)y >= (uint)h) |
396 | y = h-1; |
397 | |
398 | p = data + (h-y-1)*bpl + x; |
399 | break; |
400 | default: // absolute mode |
401 | // Protection |
402 | if (p + b > endp) |
403 | b = endp-p; |
404 | |
405 | i = (c = b)/2; |
406 | while (i--) { |
407 | d->getChar((char *)&b); |
408 | *p++ = b >> 4; |
409 | *p++ = b & 0x0f; |
410 | } |
411 | if (c & 1) { |
412 | unsigned char tmp; |
413 | d->getChar((char *)&tmp); |
414 | *p++ = tmp >> 4; |
415 | } |
416 | if ((((c & 3) + 1) & 2) == 2) |
417 | d->getChar(nullptr); // align on word boundary |
418 | x += c; |
419 | } |
420 | } else { // encoded mode |
421 | // Protection |
422 | if (p + b > endp) |
423 | b = endp-p; |
424 | |
425 | i = (c = b)/2; |
426 | d->getChar((char *)&b); // 2 pixels to be repeated |
427 | while (i--) { |
428 | *p++ = b >> 4; |
429 | *p++ = b & 0x0f; |
430 | } |
431 | if (c & 1) |
432 | *p++ = b >> 4; |
433 | x += c; |
434 | } |
435 | } |
436 | } else if (comp == BMP_RGB) { // no compression |
437 | memset(data, 0, h*bpl); |
438 | while (--h >= 0) { |
439 | if (d->read((char*)buf,buflen) != buflen) |
440 | break; |
441 | uchar *p = data + h*bpl; |
442 | uchar *b = buf; |
443 | for (int i=0; i<w/2; i++) { // convert nibbles to bytes |
444 | *p++ = *b >> 4; |
445 | *p++ = *b++ & 0x0f; |
446 | } |
447 | if (w & 1) // the last nibble |
448 | *p = *b >> 4; |
449 | } |
450 | } |
451 | delete [] buf; |
452 | } |
453 | |
454 | else if (nbits == 8) { // 8 bit BMP image |
455 | if (comp == BMP_RLE8) { // run length compression |
456 | int x=0, y=0; |
457 | quint8 b; |
458 | uchar *p = data + (h-1)*bpl; |
459 | const uchar *endp = p + w; |
460 | while (y < h) { |
461 | if (!d->getChar((char *)&b)) |
462 | break; |
463 | if (b == 0) { // escape code |
464 | if (!d->getChar((char *)&b) || b == 1) { |
465 | y = h; // exit loop |
466 | } else switch (b) { |
467 | case 0: // end of line |
468 | x = 0; |
469 | y++; |
470 | p = data + (h-y-1)*bpl; |
471 | break; |
472 | case 2: // delta (jump) |
473 | { |
474 | quint8 tmp; |
475 | d->getChar((char *)&tmp); |
476 | x += tmp; |
477 | d->getChar((char *)&tmp); |
478 | y += tmp; |
479 | } |
480 | |
481 | // Protection |
482 | if ((uint)x >= (uint)w) |
483 | x = w-1; |
484 | if ((uint)y >= (uint)h) |
485 | y = h-1; |
486 | |
487 | p = data + (h-y-1)*bpl + x; |
488 | break; |
489 | default: // absolute mode |
490 | // Protection |
491 | if (p + b > endp) |
492 | b = endp-p; |
493 | |
494 | if (d->read((char *)p, b) != b) |
495 | return false; |
496 | if ((b & 1) == 1) |
497 | d->getChar(nullptr); // align on word boundary |
498 | x += b; |
499 | p += b; |
500 | } |
501 | } else { // encoded mode |
502 | // Protection |
503 | if (p + b > endp) |
504 | b = endp-p; |
505 | |
506 | char tmp; |
507 | d->getChar(&tmp); |
508 | memset(p, tmp, b); // repeat pixel |
509 | x += b; |
510 | p += b; |
511 | } |
512 | } |
513 | } else if (comp == BMP_RGB) { // uncompressed |
514 | while (--h >= 0) { |
515 | if (d->read((char *)data + h*bpl, bpl) != bpl) |
516 | break; |
517 | } |
518 | } |
519 | } |
520 | |
521 | else if (nbits == 16 || nbits == 24 || nbits == 32) { // 16,24,32 bit BMP image |
522 | QRgb *p; |
523 | QRgb *end; |
524 | uchar *buf24 = new uchar[bpl]; |
525 | int bpl24 = ((w*nbits+31)/32)*4; |
526 | uchar *b; |
527 | int c; |
528 | |
529 | while (--h >= 0) { |
530 | p = (QRgb *)(data + h*bpl); |
531 | end = p + w; |
532 | if (d->read((char *)buf24,bpl24) != bpl24) |
533 | break; |
534 | b = buf24; |
535 | while (p < end) { |
536 | c = *(uchar*)b | (*(uchar*)(b+1)<<8); |
537 | if (nbits > 16) |
538 | c |= *(uchar*)(b+2)<<16; |
539 | if (nbits > 24) |
540 | c |= *(uchar*)(b+3)<<24; |
541 | *p++ = qRgba(((c & red_mask) >> red_shift) * red_scale, |
542 | ((c & green_mask) >> green_shift) * green_scale, |
543 | ((c & blue_mask) >> blue_shift) * blue_scale, |
544 | transp ? ((c & alpha_mask) >> alpha_shift) * alpha_scale : 0xff); |
545 | b += nbits/8; |
546 | } |
547 | } |
548 | delete[] buf24; |
549 | } |
550 | |
551 | if (bi.biHeight < 0) { |
552 | // Flip the image |
553 | uchar *buf = new uchar[bpl]; |
554 | h = -bi.biHeight; |
555 | for (int y = 0; y < h/2; ++y) { |
556 | memcpy(buf, data + y*bpl, bpl); |
557 | memcpy(data + y*bpl, data + (h-y-1)*bpl, bpl); |
558 | memcpy(data + (h-y-1)*bpl, buf, bpl); |
559 | } |
560 | delete [] buf; |
561 | } |
562 | |
563 | return true; |
564 | } |
565 | |
566 | // this is also used in qmime_win.cpp |
567 | bool qt_write_dib(QDataStream &s, const QImage &image, int bpl, int bpl_bmp, int nbits) |
568 | { |
569 | QIODevice* d = s.device(); |
570 | if (!d->isWritable()) |
571 | return false; |
572 | |
573 | BMP_INFOHDR bi; |
574 | bi.biSize = BMP_WIN; // build info header |
575 | bi.biWidth = image.width(); |
576 | bi.biHeight = image.height(); |
577 | bi.biPlanes = 1; |
578 | bi.biBitCount = nbits; |
579 | bi.biCompression = BMP_RGB; |
580 | bi.biSizeImage = bpl_bmp*image.height(); |
581 | bi.biXPelsPerMeter = image.dotsPerMeterX() ? image.dotsPerMeterX() |
582 | : 2834; // 72 dpi default |
583 | bi.biYPelsPerMeter = image.dotsPerMeterY() ? image.dotsPerMeterY() : 2834; |
584 | bi.biClrUsed = image.colorCount(); |
585 | bi.biClrImportant = image.colorCount(); |
586 | s << bi; // write info header |
587 | if (s.status() != QDataStream::Ok) |
588 | return false; |
589 | |
590 | if (image.depth() != 32) { // write color table |
591 | uchar *color_table = new uchar[4*image.colorCount()]; |
592 | uchar *rgb = color_table; |
593 | QList<QRgb> c = image.colorTable(); |
594 | for (int i = 0; i < image.colorCount(); i++) { |
595 | *rgb++ = qBlue (c[i]); |
596 | *rgb++ = qGreen(c[i]); |
597 | *rgb++ = qRed (c[i]); |
598 | *rgb++ = 0; |
599 | } |
600 | if (d->write((char *)color_table, 4*image.colorCount()) == -1) { |
601 | delete [] color_table; |
602 | return false; |
603 | } |
604 | delete [] color_table; |
605 | } |
606 | |
607 | int y; |
608 | |
609 | if (nbits == 1 || nbits == 8) { // direct output |
610 | for (y=image.height()-1; y>=0; y--) { |
611 | if (d->write((const char*)image.constScanLine(y), bpl) == -1) |
612 | return false; |
613 | } |
614 | return true; |
615 | } |
616 | |
617 | uchar *buf = new uchar[bpl_bmp]; |
618 | uchar *b, *end; |
619 | const uchar *p; |
620 | |
621 | memset(buf, 0, bpl_bmp); |
622 | for (y=image.height()-1; y>=0; y--) { // write the image bits |
623 | if (nbits == 4) { // convert 8 -> 4 bits |
624 | p = image.constScanLine(y); |
625 | b = buf; |
626 | end = b + image.width()/2; |
627 | while (b < end) { |
628 | *b++ = (*p << 4) | (*(p+1) & 0x0f); |
629 | p += 2; |
630 | } |
631 | if (image.width() & 1) |
632 | *b = *p << 4; |
633 | } else { // 32 bits |
634 | const QRgb *p = (const QRgb *)image.constScanLine(y); |
635 | const QRgb *end = p + image.width(); |
636 | b = buf; |
637 | while (p < end) { |
638 | *b++ = qBlue(*p); |
639 | *b++ = qGreen(*p); |
640 | *b++ = qRed(*p); |
641 | p++; |
642 | } |
643 | } |
644 | if (bpl_bmp != d->write((char*)buf, bpl_bmp)) { |
645 | delete[] buf; |
646 | return false; |
647 | } |
648 | } |
649 | delete[] buf; |
650 | return true; |
651 | } |
652 | |
653 | // this is also used in qmime_win.cpp |
654 | bool qt_read_dib(QDataStream &s, QImage &image) |
655 | { |
656 | BMP_INFOHDR bi; |
657 | if (!read_dib_infoheader(s, bi)) |
658 | return false; |
659 | return read_dib_body(s, bi, -1, -BMP_FILEHDR_SIZE, image); |
660 | } |
661 | |
662 | QBmpHandler::QBmpHandler(InternalFormat fmt) : |
663 | m_format(fmt), state(Ready) |
664 | { |
665 | } |
666 | |
667 | QByteArray QBmpHandler::formatName() const |
668 | { |
669 | return m_format == BmpFormat ? "bmp" : "dib" ; |
670 | } |
671 | |
672 | bool QBmpHandler::readHeader() |
673 | { |
674 | state = Error; |
675 | |
676 | QIODevice *d = device(); |
677 | QDataStream s(d); |
678 | startpos = d->pos(); |
679 | |
680 | // Intel byte order |
681 | s.setByteOrder(QDataStream::LittleEndian); |
682 | |
683 | // read BMP file header |
684 | if (m_format == BmpFormat && !read_dib_fileheader(s, fileHeader)) |
685 | return false; |
686 | |
687 | // read BMP info header |
688 | if (!read_dib_infoheader(s, infoHeader)) |
689 | return false; |
690 | |
691 | state = ReadHeader; |
692 | return true; |
693 | } |
694 | |
695 | bool QBmpHandler::canRead() const |
696 | { |
697 | if (m_format == BmpFormat && state == Ready && !canRead(device())) |
698 | return false; |
699 | |
700 | if (state != Error) { |
701 | setFormat(formatName()); |
702 | return true; |
703 | } |
704 | |
705 | return false; |
706 | } |
707 | |
708 | bool QBmpHandler::canRead(QIODevice *device) |
709 | { |
710 | if (!device) { |
711 | qWarning("QBmpHandler::canRead() called with 0 pointer" ); |
712 | return false; |
713 | } |
714 | |
715 | char head[2]; |
716 | if (device->peek(head, sizeof(head)) != sizeof(head)) |
717 | return false; |
718 | |
719 | return (qstrncmp(head, "BM" , 2) == 0); |
720 | } |
721 | |
722 | bool QBmpHandler::read(QImage *image) |
723 | { |
724 | if (state == Error) |
725 | return false; |
726 | |
727 | if (!image) { |
728 | qWarning("QBmpHandler::read: cannot read into null pointer" ); |
729 | return false; |
730 | } |
731 | |
732 | if (state == Ready && !readHeader()) { |
733 | state = Error; |
734 | return false; |
735 | } |
736 | |
737 | QIODevice *d = device(); |
738 | QDataStream s(d); |
739 | |
740 | // Intel byte order |
741 | s.setByteOrder(QDataStream::LittleEndian); |
742 | |
743 | // read image |
744 | const bool readSuccess = m_format == BmpFormat ? |
745 | read_dib_body(s, infoHeader, fileHeader.bfOffBits, startpos, *image) : |
746 | read_dib_body(s, infoHeader, -1, startpos - BMP_FILEHDR_SIZE, *image); |
747 | if (!readSuccess) |
748 | return false; |
749 | |
750 | state = Ready; |
751 | return true; |
752 | } |
753 | |
754 | bool QBmpHandler::write(const QImage &img) |
755 | { |
756 | QImage image; |
757 | switch (img.format()) { |
758 | case QImage::Format_Mono: |
759 | case QImage::Format_Indexed8: |
760 | case QImage::Format_RGB32: |
761 | case QImage::Format_ARGB32: |
762 | image = img; |
763 | break; |
764 | case QImage::Format_MonoLSB: |
765 | image = img.convertToFormat(QImage::Format_Mono); |
766 | break; |
767 | case QImage::Format_Alpha8: |
768 | case QImage::Format_Grayscale8: |
769 | image = img.convertToFormat(QImage::Format_Indexed8); |
770 | break; |
771 | default: |
772 | if (img.hasAlphaChannel()) |
773 | image = img.convertToFormat(QImage::Format_ARGB32); |
774 | else |
775 | image = img.convertToFormat(QImage::Format_RGB32); |
776 | break; |
777 | } |
778 | |
779 | int nbits; |
780 | qsizetype bpl_bmp; |
781 | // Calculate a minimum bytes-per-line instead of using whatever value this QImage is using internally. |
782 | qsizetype bpl = ((image.width() * image.depth() + 31) >> 5) << 2; |
783 | |
784 | if (image.depth() == 8 && image.colorCount() <= 16) { |
785 | bpl_bmp = (((bpl+1)/2+3)/4)*4; |
786 | nbits = 4; |
787 | } else if (image.depth() == 32) { |
788 | bpl_bmp = ((image.width()*24+31)/32)*4; |
789 | nbits = 24; |
790 | } else { |
791 | bpl_bmp = bpl; |
792 | nbits = image.depth(); |
793 | } |
794 | if (qsizetype(int(bpl_bmp)) != bpl_bmp) |
795 | return false; |
796 | |
797 | if (m_format == DibFormat) { |
798 | QDataStream dibStream(device()); |
799 | dibStream.setByteOrder(QDataStream::LittleEndian); // Intel byte order |
800 | return qt_write_dib(dibStream, img, bpl, bpl_bmp, nbits); |
801 | } |
802 | |
803 | QIODevice *d = device(); |
804 | QDataStream s(d); |
805 | BMP_FILEHDR bf; |
806 | |
807 | // Intel byte order |
808 | s.setByteOrder(QDataStream::LittleEndian); |
809 | |
810 | // build file header |
811 | memcpy(bf.bfType, "BM" , 2); |
812 | |
813 | // write file header |
814 | bf.bfReserved1 = 0; |
815 | bf.bfReserved2 = 0; |
816 | bf.bfOffBits = BMP_FILEHDR_SIZE + BMP_WIN + image.colorCount() * 4; |
817 | bf.bfSize = bf.bfOffBits + bpl_bmp*image.height(); |
818 | if (qsizetype(bf.bfSize) != bf.bfOffBits + bpl_bmp*image.height()) |
819 | return false; |
820 | s << bf; |
821 | |
822 | // write image |
823 | return qt_write_dib(s, image, bpl, bpl_bmp, nbits); |
824 | } |
825 | |
826 | bool QBmpHandler::supportsOption(ImageOption option) const |
827 | { |
828 | return option == Size |
829 | || option == ImageFormat; |
830 | } |
831 | |
832 | QVariant QBmpHandler::option(ImageOption option) const |
833 | { |
834 | if (option == Size) { |
835 | if (state == Error) |
836 | return QVariant(); |
837 | if (state == Ready && !const_cast<QBmpHandler*>(this)->readHeader()) |
838 | return QVariant(); |
839 | return QSize(infoHeader.biWidth, infoHeader.biHeight); |
840 | } else if (option == ImageFormat) { |
841 | if (state == Error) |
842 | return QVariant(); |
843 | if (state == Ready && !const_cast<QBmpHandler*>(this)->readHeader()) |
844 | return QVariant(); |
845 | QImage::Format format; |
846 | switch (infoHeader.biBitCount) { |
847 | case 32: |
848 | case 24: |
849 | case 16: |
850 | if (infoHeader.biCompression == BMP_BITFIELDS && infoHeader.biSize >= BMP_WIN4 && infoHeader.biAlphaMask) |
851 | format = QImage::Format_ARGB32; |
852 | else |
853 | format = QImage::Format_RGB32; |
854 | break; |
855 | case 8: |
856 | case 4: |
857 | format = QImage::Format_Indexed8; |
858 | break; |
859 | default: |
860 | format = QImage::Format_Mono; |
861 | } |
862 | return format; |
863 | } |
864 | return QVariant(); |
865 | } |
866 | |
867 | void QBmpHandler::setOption(ImageOption option, const QVariant &value) |
868 | { |
869 | Q_UNUSED(option); |
870 | Q_UNUSED(value); |
871 | } |
872 | |
873 | QT_END_NAMESPACE |
874 | |
875 | #endif // QT_NO_IMAGEFORMAT_BMP |
876 | |