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