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