1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the QtGui module of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:LGPL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in
|
---|
14 | ** a written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Lesser General Public License Usage
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
---|
18 | ** General Public License version 2.1 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
---|
20 | ** packaging of this file. Please review the following information to
|
---|
21 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
---|
22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
---|
23 | **
|
---|
24 | ** In addition, as a special exception, Nokia gives you certain additional
|
---|
25 | ** rights. These rights are described in the Nokia Qt LGPL Exception
|
---|
26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
|
---|
27 | **
|
---|
28 | ** GNU General Public License Usage
|
---|
29 | ** Alternatively, this file may be used under the terms of the GNU
|
---|
30 | ** General Public License version 3.0 as published by the Free Software
|
---|
31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
---|
32 | ** packaging of this file. Please review the following information to
|
---|
33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
---|
34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
---|
35 | **
|
---|
36 | ** If you have questions regarding the use of this file, please contact
|
---|
37 | ** Nokia at [email protected].
|
---|
38 | ** $QT_END_LICENSE$
|
---|
39 | **
|
---|
40 | ****************************************************************************/
|
---|
41 |
|
---|
42 | #include "private/qbmphandler_p.h"
|
---|
43 |
|
---|
44 | #ifndef QT_NO_IMAGEFORMAT_BMP
|
---|
45 |
|
---|
46 | #include <qimage.h>
|
---|
47 | #include <qvariant.h>
|
---|
48 | #include <qvector.h>
|
---|
49 |
|
---|
50 | QT_BEGIN_NAMESPACE
|
---|
51 |
|
---|
52 | static void swapPixel01(QImage *image) // 1-bpp: swap 0 and 1 pixels
|
---|
53 | {
|
---|
54 | int i;
|
---|
55 | if (image->depth() == 1 && image->colorCount() == 2) {
|
---|
56 | register uint *p = (uint *)image->bits();
|
---|
57 | int nbytes = image->byteCount();
|
---|
58 | for (i=0; i<nbytes/4; i++) {
|
---|
59 | *p = ~*p;
|
---|
60 | p++;
|
---|
61 | }
|
---|
62 | uchar *p2 = (uchar *)p;
|
---|
63 | for (i=0; i<(nbytes&3); i++) {
|
---|
64 | *p2 = ~*p2;
|
---|
65 | p2++;
|
---|
66 | }
|
---|
67 | QRgb t = image->color(0); // swap color 0 and 1
|
---|
68 | image->setColor(0, image->color(1));
|
---|
69 | image->setColor(1, t);
|
---|
70 | }
|
---|
71 | }
|
---|
72 |
|
---|
73 | /*
|
---|
74 | QImageIO::defineIOHandler("BMP", "^BM", 0,
|
---|
75 | read_bmp_image, write_bmp_image);
|
---|
76 | */
|
---|
77 |
|
---|
78 | /*****************************************************************************
|
---|
79 | BMP (DIB) image read/write functions
|
---|
80 | *****************************************************************************/
|
---|
81 |
|
---|
82 | const int BMP_FILEHDR_SIZE = 14; // size of BMP_FILEHDR data
|
---|
83 |
|
---|
84 | static QDataStream &operator>>(QDataStream &s, BMP_FILEHDR &bf)
|
---|
85 | { // read file header
|
---|
86 | s.readRawData(bf.bfType, 2);
|
---|
87 | s >> bf.bfSize >> bf.bfReserved1 >> bf.bfReserved2 >> bf.bfOffBits;
|
---|
88 | return s;
|
---|
89 | }
|
---|
90 |
|
---|
91 | static QDataStream &operator<<(QDataStream &s, const BMP_FILEHDR &bf)
|
---|
92 | { // write file header
|
---|
93 | s.writeRawData(bf.bfType, 2);
|
---|
94 | s << bf.bfSize << bf.bfReserved1 << bf.bfReserved2 << bf.bfOffBits;
|
---|
95 | return s;
|
---|
96 | }
|
---|
97 |
|
---|
98 |
|
---|
99 | const int BMP_OLD = 12; // old Windows/OS2 BMP size
|
---|
100 | const int BMP_WIN = 40; // new Windows BMP size
|
---|
101 | const int BMP_OS2 = 64; // new OS/2 BMP 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) {
|
---|
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 | }
|
---|
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 | }
|
---|
128 | return s;
|
---|
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;
|
---|
142 | }
|
---|
143 |
|
---|
144 | static int calc_shift(int mask)
|
---|
145 | {
|
---|
146 | int result = 0;
|
---|
147 | while (mask && !(mask & 1)) {
|
---|
148 | result++;
|
---|
149 | mask >>= 1;
|
---|
150 | }
|
---|
151 | return result;
|
---|
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)
|
---|
159 | return false;
|
---|
160 |
|
---|
161 | // check header
|
---|
162 | if (qstrncmp(bf.bfType,"BM",2) != 0)
|
---|
163 | return false;
|
---|
164 |
|
---|
165 | return true;
|
---|
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)
|
---|
172 | return false;
|
---|
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) ||
|
---|
177 | bi.biPlanes != 1 || comp > BMP_BITFIELDS)
|
---|
178 | return false; // weird BMP image
|
---|
179 | if (!(comp == BMP_RGB || (nbits == 4 && comp == BMP_RLE4) ||
|
---|
180 | (nbits == 8 && comp == BMP_RLE8) || ((nbits == 16 || nbits == 32) && comp == BMP_BITFIELDS)))
|
---|
181 | return false; // weird compression type
|
---|
182 |
|
---|
183 | return true;
|
---|
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
|
---|
190 | return false;
|
---|
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 | int red_mask = 0;
|
---|
209 | int green_mask = 0;
|
---|
210 | int blue_mask = 0;
|
---|
211 | int red_shift = 0;
|
---|
212 | int green_shift = 0;
|
---|
213 | int blue_shift = 0;
|
---|
214 | int red_scale = 0;
|
---|
215 | int green_scale = 0;
|
---|
216 | int blue_scale = 0;
|
---|
217 |
|
---|
218 | int ncols = 0;
|
---|
219 | int depth = 0;
|
---|
220 | QImage::Format format;
|
---|
221 | switch (nbits) {
|
---|
222 | case 32:
|
---|
223 | case 24:
|
---|
224 | case 16:
|
---|
225 | depth = 32;
|
---|
226 | format = QImage::Format_RGB32;
|
---|
227 | break;
|
---|
228 | case 8:
|
---|
229 | case 4:
|
---|
230 | depth = 8;
|
---|
231 | format = QImage::Format_Indexed8;
|
---|
232 | break;
|
---|
233 | default:
|
---|
234 | depth = 1;
|
---|
235 | format = QImage::Format_Mono;
|
---|
236 | }
|
---|
237 |
|
---|
238 | if (bi.biHeight < 0)
|
---|
239 | h = -h; // support images with negative height
|
---|
240 |
|
---|
241 | if (image.size() != QSize(w, h) || image.format() != format) {
|
---|
242 | image = QImage(w, h, format);
|
---|
243 | if (image.isNull()) // could not create image
|
---|
244 | return false;
|
---|
245 | }
|
---|
246 |
|
---|
247 | if (depth != 32) {
|
---|
248 | ncols = bi.biClrUsed ? bi.biClrUsed : 1 << nbits;
|
---|
249 | if (ncols > 256) // sanity check - don't run out of mem if color table is broken
|
---|
250 | return false;
|
---|
251 | image.setColorCount(ncols);
|
---|
252 | }
|
---|
253 |
|
---|
254 | image.setDotsPerMeterX(bi.biXPelsPerMeter);
|
---|
255 | image.setDotsPerMeterY(bi.biYPelsPerMeter);
|
---|
256 |
|
---|
257 | if (!d->isSequential())
|
---|
258 | d->seek(startpos + BMP_FILEHDR_SIZE + bi.biSize); // goto start of colormap
|
---|
259 |
|
---|
260 | if (ncols > 0) { // read color table
|
---|
261 | uchar rgb[4];
|
---|
262 | int rgb_len = t == BMP_OLD ? 3 : 4;
|
---|
263 | for (int i=0; i<ncols; i++) {
|
---|
264 | if (d->read((char *)rgb, rgb_len) != rgb_len)
|
---|
265 | return false;
|
---|
266 | image.setColor(i, qRgb(rgb[2],rgb[1],rgb[0]));
|
---|
267 | if (d->atEnd()) // truncated file
|
---|
268 | return false;
|
---|
269 | }
|
---|
270 | } else if (comp == BMP_BITFIELDS && (nbits == 16 || nbits == 32)) {
|
---|
271 | if (d->read((char *)&red_mask, sizeof(red_mask)) != sizeof(red_mask))
|
---|
272 | return false;
|
---|
273 | if (d->read((char *)&green_mask, sizeof(green_mask)) != sizeof(green_mask))
|
---|
274 | return false;
|
---|
275 | if (d->read((char *)&blue_mask, sizeof(blue_mask)) != sizeof(blue_mask))
|
---|
276 | return false;
|
---|
277 | red_shift = calc_shift(red_mask);
|
---|
278 | red_scale = 256 / ((red_mask >> red_shift) + 1);
|
---|
279 | green_shift = calc_shift(green_mask);
|
---|
280 | green_scale = 256 / ((green_mask >> green_shift) + 1);
|
---|
281 | blue_shift = calc_shift(blue_mask);
|
---|
282 | blue_scale = 256 / ((blue_mask >> blue_shift) + 1);
|
---|
283 | } else if (comp == BMP_RGB && (nbits == 24 || nbits == 32)) {
|
---|
284 | blue_mask = 0x000000ff;
|
---|
285 | green_mask = 0x0000ff00;
|
---|
286 | red_mask = 0x00ff0000;
|
---|
287 | blue_shift = 0;
|
---|
288 | green_shift = 8;
|
---|
289 | red_shift = 16;
|
---|
290 | blue_scale = green_scale = red_scale = 1;
|
---|
291 | } else if (comp == BMP_RGB && nbits == 16) {
|
---|
292 | blue_mask = 0x001f;
|
---|
293 | green_mask = 0x03e0;
|
---|
294 | red_mask = 0x7c00;
|
---|
295 | blue_shift = 0;
|
---|
296 | green_shift = 2;
|
---|
297 | red_shift = 7;
|
---|
298 | red_scale = 1;
|
---|
299 | green_scale = 1;
|
---|
300 | blue_scale = 8;
|
---|
301 | }
|
---|
302 |
|
---|
303 | // offset can be bogus, be careful
|
---|
304 | if (offset>=0 && startpos + offset > d->pos()) {
|
---|
305 | if (!d->isSequential())
|
---|
306 | d->seek(startpos + offset); // start of image data
|
---|
307 | }
|
---|
308 |
|
---|
309 | int bpl = image.bytesPerLine();
|
---|
310 | uchar *data = image.bits();
|
---|
311 |
|
---|
312 | if (nbits == 1) { // 1 bit BMP image
|
---|
313 | while (--h >= 0) {
|
---|
314 | if (d->read((char*)(data + h*bpl), bpl) != bpl)
|
---|
315 | break;
|
---|
316 | }
|
---|
317 | if (ncols == 2 && qGray(image.color(0)) < qGray(image.color(1)))
|
---|
318 | swapPixel01(&image); // pixel 0 is white!
|
---|
319 | }
|
---|
320 |
|
---|
321 | else if (nbits == 4) { // 4 bit BMP image
|
---|
322 | int buflen = ((w+7)/8)*4;
|
---|
323 | uchar *buf = new uchar[buflen];
|
---|
324 | if (comp == BMP_RLE4) { // run length compression
|
---|
325 | int x=0, y=0, c, i;
|
---|
326 | quint8 b;
|
---|
327 | register uchar *p = data + (h-1)*bpl;
|
---|
328 | const uchar *endp = p + w;
|
---|
329 | while (y < h) {
|
---|
330 | if (!d->getChar((char *)&b))
|
---|
331 | break;
|
---|
332 | if (b == 0) { // escape code
|
---|
333 | if (!d->getChar((char *)&b) || b == 1) {
|
---|
334 | y = h; // exit loop
|
---|
335 | } else switch (b) {
|
---|
336 | case 0: // end of line
|
---|
337 | x = 0;
|
---|
338 | y++;
|
---|
339 | p = data + (h-y-1)*bpl;
|
---|
340 | break;
|
---|
341 | case 2: // delta (jump)
|
---|
342 | {
|
---|
343 | quint8 tmp;
|
---|
344 | d->getChar((char *)&tmp);
|
---|
345 | x += tmp;
|
---|
346 | d->getChar((char *)&tmp);
|
---|
347 | y += tmp;
|
---|
348 | }
|
---|
349 |
|
---|
350 | // Protection
|
---|
351 | if ((uint)x >= (uint)w)
|
---|
352 | x = w-1;
|
---|
353 | if ((uint)y >= (uint)h)
|
---|
354 | y = h-1;
|
---|
355 |
|
---|
356 | p = data + (h-y-1)*bpl + x;
|
---|
357 | break;
|
---|
358 | default: // absolute mode
|
---|
359 | // Protection
|
---|
360 | if (p + b > endp)
|
---|
361 | b = endp-p;
|
---|
362 |
|
---|
363 | i = (c = b)/2;
|
---|
364 | while (i--) {
|
---|
365 | d->getChar((char *)&b);
|
---|
366 | *p++ = b >> 4;
|
---|
367 | *p++ = b & 0x0f;
|
---|
368 | }
|
---|
369 | if (c & 1) {
|
---|
370 | unsigned char tmp;
|
---|
371 | d->getChar((char *)&tmp);
|
---|
372 | *p++ = tmp >> 4;
|
---|
373 | }
|
---|
374 | if ((((c & 3) + 1) & 2) == 2)
|
---|
375 | d->getChar(0); // align on word boundary
|
---|
376 | x += c;
|
---|
377 | }
|
---|
378 | } else { // encoded mode
|
---|
379 | // Protection
|
---|
380 | if (p + b > endp)
|
---|
381 | b = endp-p;
|
---|
382 |
|
---|
383 | i = (c = b)/2;
|
---|
384 | d->getChar((char *)&b); // 2 pixels to be repeated
|
---|
385 | while (i--) {
|
---|
386 | *p++ = b >> 4;
|
---|
387 | *p++ = b & 0x0f;
|
---|
388 | }
|
---|
389 | if (c & 1)
|
---|
390 | *p++ = b >> 4;
|
---|
391 | x += c;
|
---|
392 | }
|
---|
393 | }
|
---|
394 | } else if (comp == BMP_RGB) { // no compression
|
---|
395 | memset(data, 0, h*bpl);
|
---|
396 | while (--h >= 0) {
|
---|
397 | if (d->read((char*)buf,buflen) != buflen)
|
---|
398 | break;
|
---|
399 | register uchar *p = data + h*bpl;
|
---|
400 | uchar *b = buf;
|
---|
401 | for (int i=0; i<w/2; i++) { // convert nibbles to bytes
|
---|
402 | *p++ = *b >> 4;
|
---|
403 | *p++ = *b++ & 0x0f;
|
---|
404 | }
|
---|
405 | if (w & 1) // the last nibble
|
---|
406 | *p = *b >> 4;
|
---|
407 | }
|
---|
408 | }
|
---|
409 | delete [] buf;
|
---|
410 | }
|
---|
411 |
|
---|
412 | else if (nbits == 8) { // 8 bit BMP image
|
---|
413 | if (comp == BMP_RLE8) { // run length compression
|
---|
414 | int x=0, y=0;
|
---|
415 | quint8 b;
|
---|
416 | register uchar *p = data + (h-1)*bpl;
|
---|
417 | const uchar *endp = p + w;
|
---|
418 | while (y < h) {
|
---|
419 | if (!d->getChar((char *)&b))
|
---|
420 | break;
|
---|
421 | if (b == 0) { // escape code
|
---|
422 | if (!d->getChar((char *)&b) || b == 1) {
|
---|
423 | y = h; // exit loop
|
---|
424 | } else switch (b) {
|
---|
425 | case 0: // end of line
|
---|
426 | x = 0;
|
---|
427 | y++;
|
---|
428 | p = data + (h-y-1)*bpl;
|
---|
429 | break;
|
---|
430 | case 2: // delta (jump)
|
---|
431 | // Protection
|
---|
432 | if ((uint)x >= (uint)w)
|
---|
433 | x = w-1;
|
---|
434 | if ((uint)y >= (uint)h)
|
---|
435 | y = h-1;
|
---|
436 |
|
---|
437 | {
|
---|
438 | quint8 tmp;
|
---|
439 | d->getChar((char *)&tmp);
|
---|
440 | x += tmp;
|
---|
441 | d->getChar((char *)&tmp);
|
---|
442 | y += tmp;
|
---|
443 | }
|
---|
444 | p = data + (h-y-1)*bpl + x;
|
---|
445 | break;
|
---|
446 | default: // absolute mode
|
---|
447 | // Protection
|
---|
448 | if (p + b > endp)
|
---|
449 | b = endp-p;
|
---|
450 |
|
---|
451 | if (d->read((char *)p, b) != b)
|
---|
452 | return false;
|
---|
453 | if ((b & 1) == 1)
|
---|
454 | d->getChar(0); // align on word boundary
|
---|
455 | x += b;
|
---|
456 | p += b;
|
---|
457 | }
|
---|
458 | } else { // encoded mode
|
---|
459 | // Protection
|
---|
460 | if (p + b > endp)
|
---|
461 | b = endp-p;
|
---|
462 |
|
---|
463 | char tmp;
|
---|
464 | d->getChar(&tmp);
|
---|
465 | memset(p, tmp, b); // repeat pixel
|
---|
466 | x += b;
|
---|
467 | p += b;
|
---|
468 | }
|
---|
469 | }
|
---|
470 | } else if (comp == BMP_RGB) { // uncompressed
|
---|
471 | while (--h >= 0) {
|
---|
472 | if (d->read((char *)data + h*bpl, bpl) != bpl)
|
---|
473 | break;
|
---|
474 | }
|
---|
475 | }
|
---|
476 | }
|
---|
477 |
|
---|
478 | else if (nbits == 16 || nbits == 24 || nbits == 32) { // 16,24,32 bit BMP image
|
---|
479 | register QRgb *p;
|
---|
480 | QRgb *end;
|
---|
481 | uchar *buf24 = new uchar[bpl];
|
---|
482 | int bpl24 = ((w*nbits+31)/32)*4;
|
---|
483 | uchar *b;
|
---|
484 | int c;
|
---|
485 |
|
---|
486 | while (--h >= 0) {
|
---|
487 | p = (QRgb *)(data + h*bpl);
|
---|
488 | end = p + w;
|
---|
489 | if (d->read((char *)buf24,bpl24) != bpl24)
|
---|
490 | break;
|
---|
491 | b = buf24;
|
---|
492 | while (p < end) {
|
---|
493 | c = *(uchar*)b | (*(uchar*)(b+1)<<8);
|
---|
494 | if (nbits != 16)
|
---|
495 | c |= *(uchar*)(b+2)<<16;
|
---|
496 | *p++ = qRgb(((c & red_mask) >> red_shift) * red_scale,
|
---|
497 | ((c & green_mask) >> green_shift) * green_scale,
|
---|
498 | ((c & blue_mask) >> blue_shift) * blue_scale);
|
---|
499 | b += nbits/8;
|
---|
500 | }
|
---|
501 | }
|
---|
502 | delete[] buf24;
|
---|
503 | }
|
---|
504 |
|
---|
505 | if (bi.biHeight < 0) {
|
---|
506 | // Flip the image
|
---|
507 | uchar *buf = new uchar[bpl];
|
---|
508 | h = -bi.biHeight;
|
---|
509 | for (int y = 0; y < h/2; ++y) {
|
---|
510 | memcpy(buf, data + y*bpl, bpl);
|
---|
511 | memcpy(data + y*bpl, data + (h-y-1)*bpl, bpl);
|
---|
512 | memcpy(data + (h-y-1)*bpl, buf, bpl);
|
---|
513 | }
|
---|
514 | delete [] buf;
|
---|
515 | }
|
---|
516 |
|
---|
517 | return true;
|
---|
518 | }
|
---|
519 |
|
---|
520 | // this is also used in qmime_win.cpp
|
---|
521 | bool qt_write_dib(QDataStream &s, QImage image)
|
---|
522 | {
|
---|
523 | int nbits;
|
---|
524 | int bpl_bmp;
|
---|
525 | int bpl = image.bytesPerLine();
|
---|
526 |
|
---|
527 | QIODevice* d = s.device();
|
---|
528 | if (!d->isWritable())
|
---|
529 | return false;
|
---|
530 |
|
---|
531 | if (image.depth() == 8 && image.colorCount() <= 16) {
|
---|
532 | bpl_bmp = (((bpl+1)/2+3)/4)*4;
|
---|
533 | nbits = 4;
|
---|
534 | } else if (image.depth() == 32) {
|
---|
535 | bpl_bmp = ((image.width()*24+31)/32)*4;
|
---|
536 | nbits = 24;
|
---|
537 | #ifdef Q_WS_QWS
|
---|
538 | } else if (image.depth() == 1 || image.depth() == 8) {
|
---|
539 | // Qt for Embedded Linux doesn't word align.
|
---|
540 | bpl_bmp = ((image.width()*image.depth()+31)/32)*4;
|
---|
541 | nbits = image.depth();
|
---|
542 | #endif
|
---|
543 | } else {
|
---|
544 | bpl_bmp = bpl;
|
---|
545 | nbits = image.depth();
|
---|
546 | }
|
---|
547 |
|
---|
548 | BMP_INFOHDR bi;
|
---|
549 | bi.biSize = BMP_WIN; // build info header
|
---|
550 | bi.biWidth = image.width();
|
---|
551 | bi.biHeight = image.height();
|
---|
552 | bi.biPlanes = 1;
|
---|
553 | bi.biBitCount = nbits;
|
---|
554 | bi.biCompression = BMP_RGB;
|
---|
555 | bi.biSizeImage = bpl_bmp*image.height();
|
---|
556 | bi.biXPelsPerMeter = image.dotsPerMeterX() ? image.dotsPerMeterX()
|
---|
557 | : 2834; // 72 dpi default
|
---|
558 | bi.biYPelsPerMeter = image.dotsPerMeterY() ? image.dotsPerMeterY() : 2834;
|
---|
559 | bi.biClrUsed = image.colorCount();
|
---|
560 | bi.biClrImportant = image.colorCount();
|
---|
561 | s << bi; // write info header
|
---|
562 | if (s.status() != QDataStream::Ok)
|
---|
563 | return false;
|
---|
564 |
|
---|
565 | if (image.depth() != 32) { // write color table
|
---|
566 | uchar *color_table = new uchar[4*image.colorCount()];
|
---|
567 | uchar *rgb = color_table;
|
---|
568 | QVector<QRgb> c = image.colorTable();
|
---|
569 | for (int i=0; i<image.colorCount(); i++) {
|
---|
570 | *rgb++ = qBlue (c[i]);
|
---|
571 | *rgb++ = qGreen(c[i]);
|
---|
572 | *rgb++ = qRed (c[i]);
|
---|
573 | *rgb++ = 0;
|
---|
574 | }
|
---|
575 | if (d->write((char *)color_table, 4*image.colorCount()) == -1) {
|
---|
576 | delete [] color_table;
|
---|
577 | return false;
|
---|
578 | }
|
---|
579 | delete [] color_table;
|
---|
580 | }
|
---|
581 |
|
---|
582 | if (image.format() == QImage::Format_MonoLSB)
|
---|
583 | image = image.convertToFormat(QImage::Format_Mono);
|
---|
584 |
|
---|
585 | int y;
|
---|
586 |
|
---|
587 | if (nbits == 1 || nbits == 8) { // direct output
|
---|
588 | #ifdef Q_WS_QWS
|
---|
589 | // Qt for Embedded Linux doesn't word align.
|
---|
590 | int pad = bpl_bmp - bpl;
|
---|
591 | char padding[4];
|
---|
592 | #endif
|
---|
593 | for (y=image.height()-1; y>=0; y--) {
|
---|
594 | if (d->write((char*)image.scanLine(y), bpl) == -1)
|
---|
595 | return false;
|
---|
596 | #ifdef Q_WS_QWS
|
---|
597 | if (d->write(padding, pad) == -1)
|
---|
598 | return false;
|
---|
599 | #endif
|
---|
600 | }
|
---|
601 | return true;
|
---|
602 | }
|
---|
603 |
|
---|
604 | uchar *buf = new uchar[bpl_bmp];
|
---|
605 | uchar *b, *end;
|
---|
606 | register uchar *p;
|
---|
607 |
|
---|
608 | memset(buf, 0, bpl_bmp);
|
---|
609 | for (y=image.height()-1; y>=0; y--) { // write the image bits
|
---|
610 | if (nbits == 4) { // convert 8 -> 4 bits
|
---|
611 | p = image.scanLine(y);
|
---|
612 | b = buf;
|
---|
613 | end = b + image.width()/2;
|
---|
614 | while (b < end) {
|
---|
615 | *b++ = (*p << 4) | (*(p+1) & 0x0f);
|
---|
616 | p += 2;
|
---|
617 | }
|
---|
618 | if (image.width() & 1)
|
---|
619 | *b = *p << 4;
|
---|
620 | } else { // 32 bits
|
---|
621 | QRgb *p = (QRgb *)image.scanLine(y);
|
---|
622 | QRgb *end = p + image.width();
|
---|
623 | b = buf;
|
---|
624 | while (p < end) {
|
---|
625 | *b++ = qBlue(*p);
|
---|
626 | *b++ = qGreen(*p);
|
---|
627 | *b++ = qRed(*p);
|
---|
628 | p++;
|
---|
629 | }
|
---|
630 | }
|
---|
631 | if (bpl_bmp != d->write((char*)buf, bpl_bmp)) {
|
---|
632 | delete[] buf;
|
---|
633 | return false;
|
---|
634 | }
|
---|
635 | }
|
---|
636 | delete[] buf;
|
---|
637 | return true;
|
---|
638 | }
|
---|
639 |
|
---|
640 | // this is also used in qmime_win.cpp
|
---|
641 | bool qt_read_dib(QDataStream &s, QImage &image)
|
---|
642 | {
|
---|
643 | BMP_INFOHDR bi;
|
---|
644 | if (!read_dib_infoheader(s, bi))
|
---|
645 | return false;
|
---|
646 | return read_dib_body(s, bi, -1, -BMP_FILEHDR_SIZE, image);
|
---|
647 | }
|
---|
648 |
|
---|
649 | QBmpHandler::QBmpHandler()
|
---|
650 | : state(Ready)
|
---|
651 | {
|
---|
652 | }
|
---|
653 |
|
---|
654 | bool QBmpHandler::readHeader()
|
---|
655 | {
|
---|
656 | state = Error;
|
---|
657 |
|
---|
658 | QIODevice *d = device();
|
---|
659 | QDataStream s(d);
|
---|
660 | startpos = d->pos();
|
---|
661 |
|
---|
662 | // Intel byte order
|
---|
663 | s.setByteOrder(QDataStream::LittleEndian);
|
---|
664 |
|
---|
665 | // read BMP file header
|
---|
666 | if (!read_dib_fileheader(s, fileHeader))
|
---|
667 | return false;
|
---|
668 |
|
---|
669 | // read BMP info header
|
---|
670 | if (!read_dib_infoheader(s, infoHeader))
|
---|
671 | return false;
|
---|
672 |
|
---|
673 | state = ReadHeader;
|
---|
674 | return true;
|
---|
675 | }
|
---|
676 |
|
---|
677 | bool QBmpHandler::canRead() const
|
---|
678 | {
|
---|
679 | if (state == Ready && !canRead(device()))
|
---|
680 | return false;
|
---|
681 |
|
---|
682 | if (state != Error) {
|
---|
683 | setFormat("bmp");
|
---|
684 | return true;
|
---|
685 | }
|
---|
686 |
|
---|
687 | return false;
|
---|
688 | }
|
---|
689 |
|
---|
690 | bool QBmpHandler::canRead(QIODevice *device)
|
---|
691 | {
|
---|
692 | if (!device) {
|
---|
693 | qWarning("QBmpHandler::canRead() called with 0 pointer");
|
---|
694 | return false;
|
---|
695 | }
|
---|
696 |
|
---|
697 | char head[2];
|
---|
698 | if (device->peek(head, sizeof(head)) != sizeof(head))
|
---|
699 | return false;
|
---|
700 |
|
---|
701 | return (qstrncmp(head, "BM", 2) == 0);
|
---|
702 | }
|
---|
703 |
|
---|
704 | bool QBmpHandler::read(QImage *image)
|
---|
705 | {
|
---|
706 | if (state == Error)
|
---|
707 | return false;
|
---|
708 |
|
---|
709 | if (!image) {
|
---|
710 | qWarning("QBmpHandler::read: cannot read into null pointer");
|
---|
711 | return false;
|
---|
712 | }
|
---|
713 |
|
---|
714 | if (state == Ready && !readHeader()) {
|
---|
715 | state = Error;
|
---|
716 | return false;
|
---|
717 | }
|
---|
718 |
|
---|
719 | QIODevice *d = device();
|
---|
720 | QDataStream s(d);
|
---|
721 |
|
---|
722 | // Intel byte order
|
---|
723 | s.setByteOrder(QDataStream::LittleEndian);
|
---|
724 |
|
---|
725 | // read image
|
---|
726 | if (!read_dib_body(s, infoHeader, fileHeader.bfOffBits, startpos, *image))
|
---|
727 | return false;
|
---|
728 |
|
---|
729 | state = Ready;
|
---|
730 | return true;
|
---|
731 | }
|
---|
732 |
|
---|
733 | bool QBmpHandler::write(const QImage &img)
|
---|
734 | {
|
---|
735 | QImage image;
|
---|
736 | switch (img.format()) {
|
---|
737 | case QImage::Format_ARGB8565_Premultiplied:
|
---|
738 | case QImage::Format_ARGB8555_Premultiplied:
|
---|
739 | case QImage::Format_ARGB6666_Premultiplied:
|
---|
740 | case QImage::Format_ARGB4444_Premultiplied:
|
---|
741 | image = img.convertToFormat(QImage::Format_ARGB32);
|
---|
742 | break;
|
---|
743 | case QImage::Format_RGB16:
|
---|
744 | case QImage::Format_RGB888:
|
---|
745 | case QImage::Format_RGB666:
|
---|
746 | case QImage::Format_RGB555:
|
---|
747 | case QImage::Format_RGB444:
|
---|
748 | image = img.convertToFormat(QImage::Format_RGB32);
|
---|
749 | break;
|
---|
750 | default:
|
---|
751 | image = img;
|
---|
752 | }
|
---|
753 |
|
---|
754 | QIODevice *d = device();
|
---|
755 | QDataStream s(d);
|
---|
756 | BMP_FILEHDR bf;
|
---|
757 | int bpl_bmp;
|
---|
758 | int bpl = image.bytesPerLine();
|
---|
759 |
|
---|
760 | // Code partially repeated in qt_write_dib
|
---|
761 | if (image.depth() == 8 && image.colorCount() <= 16) {
|
---|
762 | bpl_bmp = (((bpl+1)/2+3)/4)*4;
|
---|
763 | } else if (image.depth() == 32) {
|
---|
764 | bpl_bmp = ((image.width()*24+31)/32)*4;
|
---|
765 | } else {
|
---|
766 | bpl_bmp = bpl;
|
---|
767 | }
|
---|
768 |
|
---|
769 | // Intel byte order
|
---|
770 | s.setByteOrder(QDataStream::LittleEndian);
|
---|
771 |
|
---|
772 | // build file header
|
---|
773 | memcpy(bf.bfType, "BM", 2);
|
---|
774 |
|
---|
775 | // write file header
|
---|
776 | bf.bfReserved1 = 0;
|
---|
777 | bf.bfReserved2 = 0;
|
---|
778 | bf.bfOffBits = BMP_FILEHDR_SIZE + BMP_WIN + image.colorCount() * 4;
|
---|
779 | bf.bfSize = bf.bfOffBits + bpl_bmp*image.height();
|
---|
780 | s << bf;
|
---|
781 |
|
---|
782 | // write image
|
---|
783 | return qt_write_dib(s, image);
|
---|
784 | }
|
---|
785 |
|
---|
786 | bool QBmpHandler::supportsOption(ImageOption option) const
|
---|
787 | {
|
---|
788 | return option == Size
|
---|
789 | || option == ImageFormat;
|
---|
790 | }
|
---|
791 |
|
---|
792 | QVariant QBmpHandler::option(ImageOption option) const
|
---|
793 | {
|
---|
794 | if (option == Size) {
|
---|
795 | if (state == Error)
|
---|
796 | return QVariant();
|
---|
797 | if (state == Ready && !const_cast<QBmpHandler*>(this)->readHeader())
|
---|
798 | return QVariant();
|
---|
799 | return QSize(infoHeader.biWidth, infoHeader.biHeight);
|
---|
800 | } else if (option == ImageFormat) {
|
---|
801 | if (state == Error)
|
---|
802 | return QVariant();
|
---|
803 | if (state == Ready && !const_cast<QBmpHandler*>(this)->readHeader())
|
---|
804 | return QVariant();
|
---|
805 | QImage::Format format;
|
---|
806 | switch (infoHeader.biBitCount) {
|
---|
807 | case 32:
|
---|
808 | case 24:
|
---|
809 | case 16:
|
---|
810 | format = QImage::Format_RGB32;
|
---|
811 | break;
|
---|
812 | case 8:
|
---|
813 | case 4:
|
---|
814 | format = QImage::Format_Indexed8;
|
---|
815 | break;
|
---|
816 | default:
|
---|
817 | format = QImage::Format_Mono;
|
---|
818 | }
|
---|
819 | return format;
|
---|
820 | }
|
---|
821 | return QVariant();
|
---|
822 | }
|
---|
823 |
|
---|
824 | void QBmpHandler::setOption(ImageOption option, const QVariant &value)
|
---|
825 | {
|
---|
826 | Q_UNUSED(option);
|
---|
827 | Q_UNUSED(value);
|
---|
828 | }
|
---|
829 |
|
---|
830 | QByteArray QBmpHandler::name() const
|
---|
831 | {
|
---|
832 | return "bmp";
|
---|
833 | }
|
---|
834 |
|
---|
835 | QT_END_NAMESPACE
|
---|
836 |
|
---|
837 | #endif // QT_NO_IMAGEFORMAT_BMP
|
---|