source: trunk/src/gui/image/qimage.cpp

Last change on this file was 846, checked in by Dmitry A. Kuminov, 14 years ago

trunk: Merged in qt 4.7.2 sources from branches/vendor/nokia/qt.

File size: 203.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4** All rights reserved.
5** Contact: Nokia Corporation (qt-info@nokia.com)
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 qt-info@nokia.com.
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qimage.h"
43#include "qdatastream.h"
44#include "qbuffer.h"
45#include "qmap.h"
46#include "qmatrix.h"
47#include "qtransform.h"
48#include "qimagereader.h"
49#include "qimagewriter.h"
50#include "qstringlist.h"
51#include "qvariant.h"
52#include "qimagepixmapcleanuphooks_p.h"
53#include <ctype.h>
54#include <stdlib.h>
55#include <limits.h>
56#include <math.h>
57#include <private/qdrawhelper_p.h>
58#include <private/qmemrotate_p.h>
59#include <private/qpixmapdata_p.h>
60#include <private/qimagescale_p.h>
61#include <private/qsimd_p.h>
62
63#include <qhash.h>
64
65#include <private/qpaintengine_raster_p.h>
66
67#include <private/qimage_p.h>
68
69QT_BEGIN_NAMESPACE
70
71static inline bool checkPixelSize(const QImage::Format format)
72{
73 switch (format) {
74 case QImage::Format_ARGB8565_Premultiplied:
75 return (sizeof(qargb8565) == 3);
76 case QImage::Format_RGB666:
77 return (sizeof(qrgb666) == 3);
78 case QImage::Format_ARGB6666_Premultiplied:
79 return (sizeof(qargb6666) == 3);
80 case QImage::Format_RGB555:
81 return (sizeof(qrgb555) == 2);
82 case QImage::Format_ARGB8555_Premultiplied:
83 return (sizeof(qargb8555) == 3);
84 case QImage::Format_RGB888:
85 return (sizeof(qrgb888) == 3);
86 case QImage::Format_RGB444:
87 return (sizeof(qrgb444) == 2);
88 case QImage::Format_ARGB4444_Premultiplied:
89 return (sizeof(qargb4444) == 2);
90 default:
91 return true;
92 }
93}
94
95#if defined(Q_CC_DEC) && defined(__alpha) && (__DECCXX_VER-0 >= 50190001)
96#pragma message disable narrowptr
97#endif
98
99
100#define QIMAGE_SANITYCHECK_MEMORY(image) \
101 if ((image).isNull()) { \
102 qWarning("QImage: out of memory, returning null image"); \
103 return QImage(); \
104 }
105
106
107static QImage rotated90(const QImage &src);
108static QImage rotated180(const QImage &src);
109static QImage rotated270(const QImage &src);
110
111// ### Qt 5: remove
112Q_GUI_EXPORT qint64 qt_image_id(const QImage &image)
113{
114 return image.cacheKey();
115}
116
117const QVector<QRgb> *qt_image_colortable(const QImage &image)
118{
119 return &image.d->colortable;
120}
121
122Q_GUI_EXPORT extern int qt_defaultDpiX();
123Q_GUI_EXPORT extern int qt_defaultDpiY();
124
125QBasicAtomicInt qimage_serial_number = Q_BASIC_ATOMIC_INITIALIZER(1);
126
127QImageData::QImageData()
128 : ref(0), width(0), height(0), depth(0), nbytes(0), data(0),
129#ifdef QT3_SUPPORT
130 jumptable(0),
131#endif
132 format(QImage::Format_ARGB32), bytes_per_line(0),
133 ser_no(qimage_serial_number.fetchAndAddRelaxed(1)),
134 detach_no(0),
135 dpmx(qt_defaultDpiX() * 100 / qreal(2.54)),
136 dpmy(qt_defaultDpiY() * 100 / qreal(2.54)),
137 offset(0, 0), own_data(true), ro_data(false), has_alpha_clut(false),
138 is_cached(false), paintEngine(0)
139{
140}
141
142static int depthForFormat(QImage::Format format)
143{
144 int depth = 0;
145 switch(format) {
146 case QImage::Format_Invalid:
147 case QImage::NImageFormats:
148 Q_ASSERT(false);
149 case QImage::Format_Mono:
150 case QImage::Format_MonoLSB:
151 depth = 1;
152 break;
153 case QImage::Format_Indexed8:
154 depth = 8;
155 break;
156 case QImage::Format_RGB32:
157 case QImage::Format_ARGB32:
158 case QImage::Format_ARGB32_Premultiplied:
159 depth = 32;
160 break;
161 case QImage::Format_RGB555:
162 case QImage::Format_RGB16:
163 case QImage::Format_RGB444:
164 case QImage::Format_ARGB4444_Premultiplied:
165 depth = 16;
166 break;
167 case QImage::Format_RGB666:
168 case QImage::Format_ARGB6666_Premultiplied:
169 case QImage::Format_ARGB8565_Premultiplied:
170 case QImage::Format_ARGB8555_Premultiplied:
171 case QImage::Format_RGB888:
172 depth = 24;
173 break;
174 }
175 return depth;
176}
177
178/*! \fn QImageData * QImageData::create(const QSize &size, QImage::Format format, int numColors)
179
180 \internal
181
182 Creates a new image data.
183 Returns 0 if invalid parameters are give or anything else failed.
184*/
185QImageData * QImageData::create(const QSize &size, QImage::Format format, int numColors)
186{
187 if (!size.isValid() || numColors < 0 || format == QImage::Format_Invalid)
188 return 0; // invalid parameter(s)
189
190 if (!checkPixelSize(format)) {
191 qWarning("QImageData::create(): Invalid pixel size for format %i",
192 format);
193 return 0;
194 }
195
196 uint width = size.width();
197 uint height = size.height();
198 uint depth = depthForFormat(format);
199
200 switch (format) {
201 case QImage::Format_Mono:
202 case QImage::Format_MonoLSB:
203 numColors = 2;
204 break;
205 case QImage::Format_Indexed8:
206 numColors = qBound(0, numColors, 256);
207 break;
208 default:
209 numColors = 0;
210 break;
211 }
212
213 const int bytes_per_line = ((width * depth + 31) >> 5) << 2; // bytes per scanline (must be multiple of 4)
214
215 // sanity check for potential overflows
216 if (INT_MAX/depth < width
217 || bytes_per_line <= 0
218 || height <= 0
219 || INT_MAX/uint(bytes_per_line) < height
220 || INT_MAX/sizeof(uchar *) < uint(height))
221 return 0;
222
223 QScopedPointer<QImageData> d(new QImageData);
224 d->colortable.resize(numColors);
225 if (depth == 1) {
226 d->colortable[0] = QColor(Qt::black).rgba();
227 d->colortable[1] = QColor(Qt::white).rgba();
228 } else {
229 for (int i = 0; i < numColors; ++i)
230 d->colortable[i] = 0;
231 }
232
233 d->width = width;
234 d->height = height;
235 d->depth = depth;
236 d->format = format;
237 d->has_alpha_clut = false;
238 d->is_cached = false;
239
240 d->bytes_per_line = bytes_per_line;
241
242 d->nbytes = d->bytes_per_line*height;
243 d->data = (uchar *)malloc(d->nbytes);
244
245 if (!d->data) {
246 return 0;
247 }
248
249 d->ref.ref();
250 return d.take();
251
252}
253
254QImageData::~QImageData()
255{
256 if (is_cached)
257 QImagePixmapCleanupHooks::executeImageHooks((((qint64) ser_no) << 32) | ((qint64) detach_no));
258 delete paintEngine;
259 if (data && own_data)
260 free(data);
261#ifdef QT3_SUPPORT
262 if (jumptable)
263 free(jumptable);
264 jumptable = 0;
265#endif
266 data = 0;
267}
268
269
270bool QImageData::checkForAlphaPixels() const
271{
272 bool has_alpha_pixels = false;
273
274 switch (format) {
275
276 case QImage::Format_Mono:
277 case QImage::Format_MonoLSB:
278 case QImage::Format_Indexed8:
279 has_alpha_pixels = has_alpha_clut;
280 break;
281
282 case QImage::Format_ARGB32:
283 case QImage::Format_ARGB32_Premultiplied: {
284 uchar *bits = data;
285 for (int y=0; y<height && !has_alpha_pixels; ++y) {
286 for (int x=0; x<width; ++x)
287 has_alpha_pixels |= (((uint *)bits)[x] & 0xff000000) != 0xff000000;
288 bits += bytes_per_line;
289 }
290 } break;
291
292 case QImage::Format_ARGB8555_Premultiplied:
293 case QImage::Format_ARGB8565_Premultiplied: {
294 uchar *bits = data;
295 uchar *end_bits = data + bytes_per_line;
296
297 for (int y=0; y<height && !has_alpha_pixels; ++y) {
298 while (bits < end_bits) {
299 has_alpha_pixels |= bits[0] != 0;
300 bits += 3;
301 }
302 bits = end_bits;
303 end_bits += bytes_per_line;
304 }
305 } break;
306
307 case QImage::Format_ARGB6666_Premultiplied: {
308 uchar *bits = data;
309 uchar *end_bits = data + bytes_per_line;
310
311 for (int y=0; y<height && !has_alpha_pixels; ++y) {
312 while (bits < end_bits) {
313 has_alpha_pixels |= (bits[0] & 0xfc) != 0;
314 bits += 3;
315 }
316 bits = end_bits;
317 end_bits += bytes_per_line;
318 }
319 } break;
320
321 case QImage::Format_ARGB4444_Premultiplied: {
322 uchar *bits = data;
323 uchar *end_bits = data + bytes_per_line;
324
325 for (int y=0; y<height && !has_alpha_pixels; ++y) {
326 while (bits < end_bits) {
327 has_alpha_pixels |= (bits[0] & 0xf0) != 0;
328 bits += 2;
329 }
330 bits = end_bits;
331 end_bits += bytes_per_line;
332 }
333 } break;
334
335 default:
336 break;
337 }
338
339 return has_alpha_pixels;
340}
341
342/*!
343 \class QImage
344
345 \ingroup painting
346 \ingroup shared
347
348 \reentrant
349
350 \brief The QImage class provides a hardware-independent image
351 representation that allows direct access to the pixel data, and
352 can be used as a paint device.
353
354 Qt provides four classes for handling image data: QImage, QPixmap,
355 QBitmap and QPicture. QImage is designed and optimized for I/O,
356 and for direct pixel access and manipulation, while QPixmap is
357 designed and optimized for showing images on screen. QBitmap is
358 only a convenience class that inherits QPixmap, ensuring a
359 depth of 1. Finally, the QPicture class is a paint device that
360 records and replays QPainter commands.
361
362 Because QImage is a QPaintDevice subclass, QPainter can be used to
363 draw directly onto images. When using QPainter on a QImage, the
364 painting can be performed in another thread than the current GUI
365 thread.
366
367 The QImage class supports several image formats described by the
368 \l Format enum. These include monochrome, 8-bit, 32-bit and
369 alpha-blended images which are available in all versions of Qt
370 4.x.
371
372 QImage provides a collection of functions that can be used to
373 obtain a variety of information about the image. There are also
374 several functions that enables transformation of the image.
375
376 QImage objects can be passed around by value since the QImage
377 class uses \l{Implicit Data Sharing}{implicit data
378 sharing}. QImage objects can also be streamed and compared.
379
380 \note If you would like to load QImage objects in a static build of Qt,
381 refer to the \l{How To Create Qt Plugins#Static Plugins}{Plugin HowTo}.
382
383 \warning Painting on a QImage with the format
384 QImage::Format_Indexed8 is not supported.
385
386 \tableofcontents
387
388 \section1 Reading and Writing Image Files
389
390 QImage provides several ways of loading an image file: The file
391 can be loaded when constructing the QImage object, or by using the
392 load() or loadFromData() functions later on. QImage also provides
393 the static fromData() function, constructing a QImage from the
394 given data. When loading an image, the file name can either refer
395 to an actual file on disk or to one of the application's embedded
396 resources. See \l{The Qt Resource System} overview for details
397 on how to embed images and other resource files in the
398 application's executable.
399
400 Simply call the save() function to save a QImage object.
401
402 The complete list of supported file formats are available through
403 the QImageReader::supportedImageFormats() and
404 QImageWriter::supportedImageFormats() functions. New file formats
405 can be added as plugins. By default, Qt supports the following
406 formats:
407
408 \table
409 \header \o Format \o Description \o Qt's support
410 \row \o BMP \o Windows Bitmap \o Read/write
411 \row \o GIF \o Graphic Interchange Format (optional) \o Read
412 \row \o JPG \o Joint Photographic Experts Group \o Read/write
413 \row \o JPEG \o Joint Photographic Experts Group \o Read/write
414 \row \o PNG \o Portable Network Graphics \o Read/write
415 \row \o PBM \o Portable Bitmap \o Read
416 \row \o PGM \o Portable Graymap \o Read
417 \row \o PPM \o Portable Pixmap \o Read/write
418 \row \o TIFF \o Tagged Image File Format \o Read/write
419 \row \o XBM \o X11 Bitmap \o Read/write
420 \row \o XPM \o X11 Pixmap \o Read/write
421 \endtable
422
423 \section1 Image Information
424
425 QImage provides a collection of functions that can be used to
426 obtain a variety of information about the image:
427
428 \table
429 \header
430 \o \o Available Functions
431
432 \row
433 \o Geometry
434 \o
435
436 The size(), width(), height(), dotsPerMeterX(), and
437 dotsPerMeterY() functions provide information about the image size
438 and aspect ratio.
439
440 The rect() function returns the image's enclosing rectangle. The
441 valid() function tells if a given pair of coordinates is within
442 this rectangle. The offset() function returns the number of pixels
443 by which the image is intended to be offset by when positioned
444 relative to other images, which also can be manipulated using the
445 setOffset() function.
446
447 \row
448 \o Colors
449 \o
450
451 The color of a pixel can be retrieved by passing its coordinates
452 to the pixel() function. The pixel() function returns the color
453 as a QRgb value indepedent of the image's format.
454
455 In case of monochrome and 8-bit images, the colorCount() and
456 colorTable() functions provide information about the color
457 components used to store the image data: The colorTable() function
458 returns the image's entire color table. To obtain a single entry,
459 use the pixelIndex() function to retrieve the pixel index for a
460 given pair of coordinates, then use the color() function to
461 retrieve the color. Note that if you create an 8-bit image
462 manually, you have to set a valid color table on the image as
463 well.
464
465 The hasAlphaChannel() function tells if the image's format
466 respects the alpha channel, or not. The allGray() and
467 isGrayscale() functions tell whether an image's colors are all
468 shades of gray.
469
470 See also the \l {QImage#Pixel Manipulation}{Pixel Manipulation}
471 and \l {QImage#Image Transformations}{Image Transformations}
472 sections.
473
474 \row
475 \o Text
476 \o
477
478 The text() function returns the image text associated with the
479 given text key. An image's text keys can be retrieved using the
480 textKeys() function. Use the setText() function to alter an
481 image's text.
482
483 \row
484 \o Low-level information
485 \o
486
487 The depth() function returns the depth of the image. The supported
488 depths are 1 (monochrome), 8, 16, 24 and 32 bits. The
489 bitPlaneCount() function tells how many of those bits that are
490 used. For more information see the
491 \l {QImage#Image Formats}{Image Formats} section.
492
493 The format(), bytesPerLine(), and byteCount() functions provide
494 low-level information about the data stored in the image.
495
496 The cacheKey() function returns a number that uniquely
497 identifies the contents of this QImage object.
498 \endtable
499
500 \section1 Pixel Manipulation
501
502 The functions used to manipulate an image's pixels depend on the
503 image format. The reason is that monochrome and 8-bit images are
504 index-based and use a color lookup table, while 32-bit images
505 store ARGB values directly. For more information on image formats,
506 see the \l {Image Formats} section.
507
508 In case of a 32-bit image, the setPixel() function can be used to
509 alter the color of the pixel at the given coordinates to any other
510 color specified as an ARGB quadruplet. To make a suitable QRgb
511 value, use the qRgb() (adding a default alpha component to the
512 given RGB values, i.e. creating an opaque color) or qRgba()
513 function. For example:
514
515 \table
516 \row
517 \o \inlineimage qimage-32bit_scaled.png
518 \o
519 \snippet doc/src/snippets/code/src_gui_image_qimage.cpp 0
520 \header
521 \o {2,1}32-bit
522 \endtable
523
524 In case of a 8-bit and monchrome images, the pixel value is only
525 an index from the image's color table. So the setPixel() function
526 can only be used to alter the color of the pixel at the given
527 coordinates to a predefined color from the image's color table,
528 i.e. it can only change the pixel's index value. To alter or add a
529 color to an image's color table, use the setColor() function.
530
531 An entry in the color table is an ARGB quadruplet encoded as an
532 QRgb value. Use the qRgb() and qRgba() functions to make a
533 suitable QRgb value for use with the setColor() function. For
534 example:
535
536 \table
537 \row
538 \o \inlineimage qimage-8bit_scaled.png
539 \o
540 \snippet doc/src/snippets/code/src_gui_image_qimage.cpp 1
541 \header
542 \o {2,1} 8-bit
543 \endtable
544
545 QImage also provide the scanLine() function which returns a
546 pointer to the pixel data at the scanline with the given index,
547 and the bits() function which returns a pointer to the first pixel
548 data (this is equivalent to \c scanLine(0)).
549
550 \section1 Image Formats
551
552 Each pixel stored in a QImage is represented by an integer. The
553 size of the integer varies depending on the format. QImage
554 supports several image formats described by the \l Format
555 enum.
556
557 Monochrome images are stored using 1-bit indexes into a color table
558 with at most two colors. There are two different types of
559 monochrome images: big endian (MSB first) or little endian (LSB
560 first) bit order.
561
562 8-bit images are stored using 8-bit indexes into a color table,
563 i.e. they have a single byte per pixel. The color table is a
564 QVector<QRgb>, and the QRgb typedef is equivalent to an unsigned
565 int containing an ARGB quadruplet on the format 0xAARRGGBB.
566
567 32-bit images have no color table; instead, each pixel contains an
568 QRgb value. There are three different types of 32-bit images
569 storing RGB (i.e. 0xffRRGGBB), ARGB and premultiplied ARGB
570 values respectively. In the premultiplied format the red, green,
571 and blue channels are multiplied by the alpha component divided by
572 255.
573
574 An image's format can be retrieved using the format()
575 function. Use the convertToFormat() functions to convert an image
576 into another format. The allGray() and isGrayscale() functions
577 tell whether a color image can safely be converted to a grayscale
578 image.
579
580 \section1 Image Transformations
581
582 QImage supports a number of functions for creating a new image
583 that is a transformed version of the original: The
584 createAlphaMask() function builds and returns a 1-bpp mask from
585 the alpha buffer in this image, and the createHeuristicMask()
586 function creates and returns a 1-bpp heuristic mask for this
587 image. The latter function works by selecting a color from one of
588 the corners, then chipping away pixels of that color starting at
589 all the edges.
590
591 The mirrored() function returns a mirror of the image in the
592 desired direction, the scaled() returns a copy of the image scaled
593 to a rectangle of the desired measures, and the rgbSwapped() function
594 constructs a BGR image from a RGB image.
595
596 The scaledToWidth() and scaledToHeight() functions return scaled
597 copies of the image.
598
599 The transformed() function returns a copy of the image that is
600 transformed with the given transformation matrix and
601 transformation mode: Internally, the transformation matrix is
602 adjusted to compensate for unwanted translation,
603 i.e. transformed() returns the smallest image containing all
604 transformed points of the original image. The static trueMatrix()
605 function returns the actual matrix used for transforming the
606 image.
607
608 There are also functions for changing attributes of an image
609 in-place:
610
611 \table
612 \header \o Function \o Description
613 \row
614 \o setDotsPerMeterX()
615 \o Defines the aspect ratio by setting the number of pixels that fit
616 horizontally in a physical meter.
617 \row
618 \o setDotsPerMeterY()
619 \o Defines the aspect ratio by setting the number of pixels that fit
620 vertically in a physical meter.
621 \row
622 \o fill()
623 \o Fills the entire image with the given pixel value.
624 \row
625 \o invertPixels()
626 \o Inverts all pixel values in the image using the given InvertMode value.
627 \row
628 \o setColorTable()
629 \o Sets the color table used to translate color indexes. Only
630 monochrome and 8-bit formats.
631 \row
632 \o setColorCount()
633 \o Resizes the color table. Only monochrome and 8-bit formats.
634
635 \endtable
636
637 \section1 Legal Information
638
639 For smooth scaling, the transformed() functions use code based on
640 smooth scaling algorithm by Daniel M. Duley.
641
642 \legalese
643 Copyright (C) 2004, 2005 Daniel M. Duley
644
645 Redistribution and use in source and binary forms, with or without
646 modification, are permitted provided that the following conditions
647 are met:
648
649 1. Redistributions of source code must retain the above copyright
650 notice, this list of conditions and the following disclaimer.
651 2. Redistributions in binary form must reproduce the above copyright
652 notice, this list of conditions and the following disclaimer in the
653 documentation and/or other materials provided with the distribution.
654
655 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
656 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
657 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
658 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
659 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
660 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
661 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
662 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
663 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
664 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
665 \endlegalese
666
667 \sa QImageReader, QImageWriter, QPixmap, QSvgRenderer, {Image Composition Example},
668 {Image Viewer Example}, {Scribble Example}, {Pixelator Example}
669*/
670
671/*!
672 \enum QImage::Endian
673 \compat
674
675 This enum type is used to describe the endianness of the CPU and
676 graphics hardware. It is provided here for compatibility with earlier versions of Qt.
677
678 Use the \l Format enum instead. The \l Format enum specify the
679 endianess for monchrome formats, but for other formats the
680 endianess is not relevant.
681
682 \value IgnoreEndian Endianness does not matter. Useful for some
683 operations that are independent of endianness.
684 \value BigEndian Most significant bit first or network byte order, as on SPARC, PowerPC, and Motorola CPUs.
685 \value LittleEndian Least significant bit first or little endian byte order, as on Intel x86.
686*/
687
688/*!
689 \enum QImage::InvertMode
690
691 This enum type is used to describe how pixel values should be
692 inverted in the invertPixels() function.
693
694 \value InvertRgb Invert only the RGB values and leave the alpha
695 channel unchanged.
696
697 \value InvertRgba Invert all channels, including the alpha channel.
698
699 \sa invertPixels()
700*/
701
702/*!
703 \enum QImage::Format
704
705 The following image formats are available in Qt. Values greater
706 than QImage::Format_RGB16 were added in Qt 4.4. See the notes
707 after the table.
708
709 \value Format_Invalid The image is invalid.
710 \value Format_Mono The image is stored using 1-bit per pixel. Bytes are
711 packed with the most significant bit (MSB) first.
712 \value Format_MonoLSB The image is stored using 1-bit per pixel. Bytes are
713 packed with the less significant bit (LSB) first.
714
715 \value Format_Indexed8 The image is stored using 8-bit indexes
716 into a colormap.
717
718 \value Format_RGB32 The image is stored using a 32-bit RGB format (0xffRRGGBB).
719
720 \value Format_ARGB32 The image is stored using a 32-bit ARGB
721 format (0xAARRGGBB).
722
723 \value Format_ARGB32_Premultiplied The image is stored using a premultiplied 32-bit
724 ARGB format (0xAARRGGBB), i.e. the red,
725 green, and blue channels are multiplied
726 by the alpha component divided by 255. (If RR, GG, or BB
727 has a higher value than the alpha channel, the results are
728 undefined.) Certain operations (such as image composition
729 using alpha blending) are faster using premultiplied ARGB32
730 than with plain ARGB32.
731
732 \value Format_RGB16 The image is stored using a 16-bit RGB format (5-6-5).
733
734 \value Format_ARGB8565_Premultiplied The image is stored using a
735 premultiplied 24-bit ARGB format (8-5-6-5).
736 \value Format_RGB666 The image is stored using a 24-bit RGB format (6-6-6).
737 The unused most significant bits is always zero.
738 \value Format_ARGB6666_Premultiplied The image is stored using a
739 premultiplied 24-bit ARGB format (6-6-6-6).
740 \value Format_RGB555 The image is stored using a 16-bit RGB format (5-5-5).
741 The unused most significant bit is always zero.
742 \value Format_ARGB8555_Premultiplied The image is stored using a
743 premultiplied 24-bit ARGB format (8-5-5-5).
744 \value Format_RGB888 The image is stored using a 24-bit RGB format (8-8-8).
745 \value Format_RGB444 The image is stored using a 16-bit RGB format (4-4-4).
746 The unused bits are always zero.
747 \value Format_ARGB4444_Premultiplied The image is stored using a
748 premultiplied 16-bit ARGB format (4-4-4-4).
749
750 \note Drawing into a QImage with QImage::Format_Indexed8 is not
751 supported.
752
753 \note Do not render into ARGB32 images using QPainter. Using
754 QImage::Format_ARGB32_Premultiplied is significantly faster.
755
756 \sa format(), convertToFormat()
757*/
758
759/*****************************************************************************
760 QImage member functions
761 *****************************************************************************/
762
763// table to flip bits
764static const uchar bitflip[256] = {
765 /*
766 open OUT, "| fmt";
767 for $i (0..255) {
768 print OUT (($i >> 7) & 0x01) | (($i >> 5) & 0x02) |
769 (($i >> 3) & 0x04) | (($i >> 1) & 0x08) |
770 (($i << 7) & 0x80) | (($i << 5) & 0x40) |
771 (($i << 3) & 0x20) | (($i << 1) & 0x10), ", ";
772 }
773 close OUT;
774 */
775 0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
776 8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
777 4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
778 12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
779 2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
780 10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
781 6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
782 14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
783 1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
784 9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
785 5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
786 13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
787 3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
788 11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
789 7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
790 15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
791};
792
793const uchar *qt_get_bitflip_array() // called from QPixmap code
794{
795 return bitflip;
796}
797
798#if defined(QT3_SUPPORT)
799static QImage::Format formatFor(int depth, QImage::Endian bitOrder)
800{
801 QImage::Format format;
802 if (depth == 1) {
803 format = bitOrder == QImage::BigEndian ? QImage::Format_Mono : QImage::Format_MonoLSB;
804 } else if (depth == 8) {
805 format = QImage::Format_Indexed8;
806 } else if (depth == 32) {
807 format = QImage::Format_RGB32;
808 } else if (depth == 24) {
809 format = QImage::Format_RGB888;
810 } else if (depth == 16) {
811 format = QImage::Format_RGB16;
812 } else {
813 qWarning("QImage: Depth %d not supported", depth);
814 format = QImage::Format_Invalid;
815 }
816 return format;
817}
818#endif
819
820/*!
821 Constructs a null image.
822
823 \sa isNull()
824*/
825
826QImage::QImage()
827 : QPaintDevice()
828{
829 d = 0;
830}
831
832/*!
833 Constructs an image with the given \a width, \a height and \a
834 format.
835
836 \warning This will create a QImage with uninitialized data. Call
837 fill() to fill the image with an appropriate pixel value before
838 drawing onto it with QPainter.
839*/
840QImage::QImage(int width, int height, Format format)
841 : QPaintDevice()
842{
843 d = QImageData::create(QSize(width, height), format, 0);
844}
845
846/*!
847 Constructs an image with the given \a size and \a format.
848
849 \warning This will create a QImage with uninitialized data. Call
850 fill() to fill the image with an appropriate pixel value before
851 drawing onto it with QPainter.
852*/
853QImage::QImage(const QSize &size, Format format)
854 : QPaintDevice()
855{
856 d = QImageData::create(size, format, 0);
857}
858
859
860
861QImageData *QImageData::create(uchar *data, int width, int height, int bpl, QImage::Format format, bool readOnly)
862{
863 QImageData *d = 0;
864
865 if (format == QImage::Format_Invalid)
866 return d;
867
868 if (!checkPixelSize(format)) {
869 qWarning("QImageData::create(): Invalid pixel size for format %i",
870 format);
871 return 0;
872 }
873
874 const int depth = depthForFormat(format);
875 const int calc_bytes_per_line = ((width * depth + 31)/32) * 4;
876 const int min_bytes_per_line = (width * depth + 7)/8;
877
878 if (bpl <= 0)
879 bpl = calc_bytes_per_line;
880
881 if (width <= 0 || height <= 0 || !data
882 || INT_MAX/sizeof(uchar *) < uint(height)
883 || INT_MAX/uint(depth) < uint(width)
884 || bpl <= 0
885 || height <= 0
886 || bpl < min_bytes_per_line
887 || INT_MAX/uint(bpl) < uint(height))
888 return d; // invalid parameter(s)
889
890 d = new QImageData;
891 d->ref.ref();
892
893 d->own_data = false;
894 d->ro_data = readOnly;
895 d->data = data;
896 d->width = width;
897 d->height = height;
898 d->depth = depth;
899 d->format = format;
900
901 d->bytes_per_line = bpl;
902 d->nbytes = d->bytes_per_line * height;
903
904 return d;
905}
906
907/*!
908 Constructs an image with the given \a width, \a height and \a
909 format, that uses an existing memory buffer, \a data. The \a width
910 and \a height must be specified in pixels, \a data must be 32-bit aligned,
911 and each scanline of data in the image must also be 32-bit aligned.
912
913 The buffer must remain valid throughout the life of the
914 QImage. The image does not delete the buffer at destruction.
915
916 If \a format is an indexed color format, the image color table is
917 initially empty and must be sufficiently expanded with
918 setColorCount() or setColorTable() before the image is used.
919*/
920QImage::QImage(uchar* data, int width, int height, Format format)
921 : QPaintDevice()
922{
923 d = QImageData::create(data, width, height, 0, format, false);
924}
925
926/*!
927 Constructs an image with the given \a width, \a height and \a
928 format, that uses an existing read-only memory buffer, \a
929 data. The \a width and \a height must be specified in pixels, \a
930 data must be 32-bit aligned, and each scanline of data in the
931 image must also be 32-bit aligned.
932
933 The buffer must remain valid throughout the life of the QImage and
934 all copies that have not been modified or otherwise detached from
935 the original buffer. The image does not delete the buffer at
936 destruction.
937
938 If \a format is an indexed color format, the image color table is
939 initially empty and must be sufficiently expanded with
940 setColorCount() or setColorTable() before the image is used.
941
942 Unlike the similar QImage constructor that takes a non-const data buffer,
943 this version will never alter the contents of the buffer. For example,
944 calling QImage::bits() will return a deep copy of the image, rather than
945 the buffer passed to the constructor. This allows for the efficiency of
946 constructing a QImage from raw data, without the possibility of the raw
947 data being changed.
948*/
949QImage::QImage(const uchar* data, int width, int height, Format format)
950 : QPaintDevice()
951{
952 d = QImageData::create(const_cast<uchar*>(data), width, height, 0, format, true);
953}
954
955/*!
956 Constructs an image with the given \a width, \a height and \a
957 format, that uses an existing memory buffer, \a data. The \a width
958 and \a height must be specified in pixels. \a bytesPerLine
959 specifies the number of bytes per line (stride).
960
961 The buffer must remain valid throughout the life of the
962 QImage. The image does not delete the buffer at destruction.
963
964 If \a format is an indexed color format, the image color table is
965 initially empty and must be sufficiently expanded with
966 setColorCount() or setColorTable() before the image is used.
967*/
968QImage::QImage(uchar *data, int width, int height, int bytesPerLine, Format format)
969 :QPaintDevice()
970{
971 d = QImageData::create(data, width, height, bytesPerLine, format, false);
972}
973
974
975/*!
976 Constructs an image with the given \a width, \a height and \a
977 format, that uses an existing memory buffer, \a data. The \a width
978 and \a height must be specified in pixels. \a bytesPerLine
979 specifies the number of bytes per line (stride).
980
981 The buffer must remain valid throughout the life of the
982 QImage. The image does not delete the buffer at destruction.
983
984 If \a format is an indexed color format, the image color table is
985 initially empty and must be sufficiently expanded with
986 setColorCount() or setColorTable() before the image is used.
987
988 Unlike the similar QImage constructor that takes a non-const data buffer,
989 this version will never alter the contents of the buffer. For example,
990 calling QImage::bits() will return a deep copy of the image, rather than
991 the buffer passed to the constructor. This allows for the efficiency of
992 constructing a QImage from raw data, without the possibility of the raw
993 data being changed.
994*/
995
996QImage::QImage(const uchar *data, int width, int height, int bytesPerLine, Format format)
997 :QPaintDevice()
998{
999 d = QImageData::create(const_cast<uchar*>(data), width, height, bytesPerLine, format, true);
1000}
1001
1002/*!
1003 Constructs an image and tries to load the image from the file with
1004 the given \a fileName.
1005
1006 The loader attempts to read the image using the specified \a
1007 format. If the \a format is not specified (which is the default),
1008 the loader probes the file for a header to guess the file format.
1009
1010 If the loading of the image failed, this object is a null image.
1011
1012 The file name can either refer to an actual file on disk or to one
1013 of the application's embedded resources. See the
1014 \l{resources.html}{Resource System} overview for details on how to
1015 embed images and other resource files in the application's
1016 executable.
1017
1018 \sa isNull(), {QImage#Reading and Writing Image Files}{Reading and Writing Image Files}
1019*/
1020
1021QImage::QImage(const QString &fileName, const char *format)
1022 : QPaintDevice()
1023{
1024 d = 0;
1025 load(fileName, format);
1026}
1027
1028/*!
1029 Constructs an image and tries to load the image from the file with
1030 the given \a fileName.
1031
1032 The loader attempts to read the image using the specified \a
1033 format. If the \a format is not specified (which is the default),
1034 the loader probes the file for a header to guess the file format.
1035
1036 If the loading of the image failed, this object is a null image.
1037
1038 The file name can either refer to an actual file on disk or to one
1039 of the application's embedded resources. See the
1040 \l{resources.html}{Resource System} overview for details on how to
1041 embed images and other resource files in the application's
1042 executable.
1043
1044 You can disable this constructor by defining \c
1045 QT_NO_CAST_FROM_ASCII when you compile your applications. This can
1046 be useful, for example, if you want to ensure that all
1047 user-visible strings go through QObject::tr().
1048
1049 \sa QString::fromAscii(), isNull(), {QImage#Reading and Writing
1050 Image Files}{Reading and Writing Image Files}
1051*/
1052#ifndef QT_NO_CAST_FROM_ASCII
1053QImage::QImage(const char *fileName, const char *format)
1054 : QPaintDevice()
1055{
1056 // ### Qt 5: if you remove the QImage(const QByteArray &) QT3_SUPPORT
1057 // constructor, remove this constructor as well. The constructor here
1058 // exists so that QImage("foo.png") compiles without ambiguity.
1059 d = 0;
1060 load(QString::fromAscii(fileName), format);
1061}
1062#endif
1063
1064#ifndef QT_NO_IMAGEFORMAT_XPM
1065extern bool qt_read_xpm_image_or_array(QIODevice *device, const char * const *source, QImage &image);
1066
1067/*!
1068 Constructs an image from the given \a xpm image.
1069
1070 Make sure that the image is a valid XPM image. Errors are silently
1071 ignored.
1072
1073 Note that it's possible to squeeze the XPM variable a little bit
1074 by using an unusual declaration:
1075
1076 \snippet doc/src/snippets/code/src_gui_image_qimage.cpp 2
1077
1078 The extra \c const makes the entire definition read-only, which is
1079 slightly more efficient (e.g., when the code is in a shared
1080 library) and able to be stored in ROM with the application.
1081*/
1082
1083QImage::QImage(const char * const xpm[])
1084 : QPaintDevice()
1085{
1086 d = 0;
1087 if (!xpm)
1088 return;
1089 if (!qt_read_xpm_image_or_array(0, xpm, *this))
1090 // Issue: Warning because the constructor may be ambigious
1091 qWarning("QImage::QImage(), XPM is not supported");
1092}
1093#endif // QT_NO_IMAGEFORMAT_XPM
1094
1095/*!
1096 \fn QImage::QImage(const QByteArray &data)
1097
1098 Use the static fromData() function instead.
1099
1100 \oldcode
1101 QByteArray data;
1102 ...
1103 QImage image(data);
1104 \newcode
1105 QByteArray data;
1106 ...
1107 QImage image = QImage::fromData(data);
1108 \endcode
1109*/
1110
1111
1112/*!
1113 Constructs a shallow copy of the given \a image.
1114
1115 For more information about shallow copies, see the \l {Implicit
1116 Data Sharing} documentation.
1117
1118 \sa copy()
1119*/
1120
1121QImage::QImage(const QImage &image)
1122 : QPaintDevice()
1123{
1124 if (image.paintingActive()) {
1125 d = 0;
1126 operator=(image.copy());
1127 } else {
1128 d = image.d;
1129 if (d)
1130 d->ref.ref();
1131 }
1132}
1133
1134#ifdef QT3_SUPPORT
1135/*!
1136 \fn QImage::QImage(int width, int height, int depth, int numColors, Endian bitOrder)
1137
1138 Constructs an image with the given \a width, \a height, \a depth,
1139 \a numColors colors and \a bitOrder.
1140
1141 Use the constructor that accepts a width, a height and a format
1142 (i.e. specifying the depth and bit order), in combination with the
1143 setColorCount() function, instead.
1144
1145 \oldcode
1146 QImage image(width, height, depth, numColors);
1147 \newcode
1148 QImage image(width, height, format);
1149
1150 // For 8 bit images the default number of colors is 256. If
1151 // another number of colors is required it can be specified
1152 // using the setColorCount() function.
1153 image.setColorCount(numColors);
1154 \endcode
1155*/
1156
1157QImage::QImage(int w, int h, int depth, int colorCount, Endian bitOrder)
1158 : QPaintDevice()
1159{
1160 d = QImageData::create(QSize(w, h), formatFor(depth, bitOrder), colorCount);
1161}
1162
1163/*!
1164 Constructs an image with the given \a size, \a depth, \a numColors
1165 and \a bitOrder.
1166
1167 Use the constructor that accepts a size and a format
1168 (i.e. specifying the depth and bit order), in combination with the
1169 setColorCount() function, instead.
1170
1171 \oldcode
1172 QSize mySize(width, height);
1173 QImage image(mySize, depth, numColors);
1174 \newcode
1175 QSize mySize(width, height);
1176 QImage image(mySize, format);
1177
1178 // For 8 bit images the default number of colors is 256. If
1179 // another number of colors is required it can be specified
1180 // using the setColorCount() function.
1181 image.setColorCount(numColors);
1182 \endcode
1183*/
1184QImage::QImage(const QSize& size, int depth, int numColors, Endian bitOrder)
1185 : QPaintDevice()
1186{
1187 d = QImageData::create(size, formatFor(depth, bitOrder), numColors);
1188}
1189
1190/*!
1191 \fn QImage::QImage(uchar* data, int width, int height, int depth, const QRgb* colortable, int numColors, Endian bitOrder)
1192
1193 Constructs an image with the given \a width, \a height, depth, \a
1194 colortable, \a numColors and \a bitOrder, that uses an existing
1195 memory buffer, \a data.
1196
1197 Use the constructor that accepts a uchar pointer, a width, a
1198 height and a format (i.e. specifying the depth and bit order), in
1199 combination with the setColorTable() function, instead.
1200
1201 \oldcode
1202 uchar *myData;
1203 QRgb *myColorTable;
1204
1205 QImage image(myData, width, height, depth,
1206 myColorTable, numColors, IgnoreEndian);
1207 \newcode
1208 uchar *myData;
1209 QVector<QRgb> myColorTable;
1210
1211 QImage image(myData, width, height, format);
1212 image.setColorTable(myColorTable);
1213 \endcode
1214*/
1215QImage::QImage(uchar* data, int w, int h, int depth, const QRgb* colortable, int numColors, Endian bitOrder)
1216 : QPaintDevice()
1217{
1218 d = 0;
1219 Format f = formatFor(depth, bitOrder);
1220 if (f == Format_Invalid)
1221 return;
1222
1223 const int bytes_per_line = ((w*depth+31)/32)*4; // bytes per scanline
1224 if (w <= 0 || h <= 0 || numColors < 0 || !data
1225 || INT_MAX/sizeof(uchar *) < uint(h)
1226 || INT_MAX/uint(depth) < uint(w)
1227 || bytes_per_line <= 0
1228 || INT_MAX/uint(bytes_per_line) < uint(h))
1229 return; // invalid parameter(s)
1230 d = new QImageData;
1231 d->ref.ref();
1232
1233 d->own_data = false;
1234 d->data = data;
1235 d->width = w;
1236 d->height = h;
1237 d->depth = depth;
1238 d->format = f;
1239 if (depth == 32)
1240 numColors = 0;
1241
1242 d->bytes_per_line = bytes_per_line;
1243 d->nbytes = d->bytes_per_line * h;
1244 if (colortable) {
1245 d->colortable.resize(numColors);
1246 for (int i = 0; i < numColors; ++i)
1247 d->colortable[i] = colortable[i];
1248 } else if (numColors) {
1249 setColorCount(numColors);
1250 }
1251}
1252
1253#ifdef Q_WS_QWS
1254
1255/*!
1256 \fn QImage::QImage(uchar* data, int width, int height, int depth, int bytesPerLine, const QRgb* colortable, int numColors, Endian bitOrder)
1257
1258 Constructs an image with the given \a width, \a height, \a depth,
1259 \a bytesPerLine, \a colortable, \a numColors and \a bitOrder, that
1260 uses an existing memory buffer, \a data. The image does not delete
1261 the buffer at destruction.
1262
1263 \warning This constructor is only available in Qt for Embedded Linux.
1264
1265 The data has to be 32-bit aligned, and each scanline of data in the image
1266 must also be 32-bit aligned, so it's no longer possible to specify a custom
1267 \a bytesPerLine value.
1268*/
1269QImage::QImage(uchar* data, int w, int h, int depth, int bpl, const QRgb* colortable, int numColors, Endian bitOrder)
1270 : QPaintDevice()
1271{
1272 d = 0;
1273 Format f = formatFor(depth, bitOrder);
1274 if (f == Format_Invalid)
1275 return;
1276 if (!data || w <= 0 || h <= 0 || depth <= 0 || numColors < 0
1277 || INT_MAX/sizeof(uchar *) < uint(h)
1278 || INT_MAX/uint(depth) < uint(w)
1279 || bpl <= 0
1280 || INT_MAX/uint(bpl) < uint(h))
1281 return; // invalid parameter(s)
1282
1283 d = new QImageData;
1284 d->ref.ref();
1285 d->own_data = false;
1286 d->data = data;
1287 d->width = w;
1288 d->height = h;
1289 d->depth = depth;
1290 d->format = f;
1291 if (depth == 32)
1292 numColors = 0;
1293 d->bytes_per_line = bpl;
1294 d->nbytes = d->bytes_per_line * h;
1295 if (colortable) {
1296 d->colortable.resize(numColors);
1297 for (int i = 0; i < numColors; ++i)
1298 d->colortable[i] = colortable[i];
1299 } else if (numColors) {
1300 setColorCount(numColors);
1301 }
1302}
1303#endif // Q_WS_QWS
1304#endif // QT3_SUPPORT
1305
1306/*!
1307 Destroys the image and cleans up.
1308*/
1309
1310QImage::~QImage()
1311{
1312 if (d && !d->ref.deref())
1313 delete d;
1314}
1315
1316/*!
1317 Assigns a shallow copy of the given \a image to this image and
1318 returns a reference to this image.
1319
1320 For more information about shallow copies, see the \l {Implicit
1321 Data Sharing} documentation.
1322
1323 \sa copy(), QImage()
1324*/
1325
1326QImage &QImage::operator=(const QImage &image)
1327{
1328 if (image.paintingActive()) {
1329 operator=(image.copy());
1330 } else {
1331 if (image.d)
1332 image.d->ref.ref();
1333 if (d && !d->ref.deref())
1334 delete d;
1335 d = image.d;
1336 }
1337 return *this;
1338}
1339
1340/*!
1341 \internal
1342*/
1343int QImage::devType() const
1344{
1345 return QInternal::Image;
1346}
1347
1348/*!
1349 Returns the image as a QVariant.
1350*/
1351QImage::operator QVariant() const
1352{
1353 return QVariant(QVariant::Image, this);
1354}
1355
1356/*!
1357 \internal
1358
1359 If multiple images share common data, this image makes a copy of
1360 the data and detaches itself from the sharing mechanism, making
1361 sure that this image is the only one referring to the data.
1362
1363 Nothing is done if there is just a single reference.
1364
1365 \sa copy(), isDetached(), {Implicit Data Sharing}
1366*/
1367void QImage::detach()
1368{
1369 if (d) {
1370 if (d->is_cached && d->ref == 1)
1371 QImagePixmapCleanupHooks::executeImageHooks(cacheKey());
1372
1373 if (d->ref != 1 || d->ro_data)
1374 *this = copy();
1375
1376 if (d)
1377 ++d->detach_no;
1378 }
1379}
1380
1381
1382/*!
1383 \fn QImage QImage::copy(int x, int y, int width, int height) const
1384 \overload
1385
1386 The returned image is copied from the position (\a x, \a y) in
1387 this image, and will always have the given \a width and \a height.
1388 In areas beyond this image, pixels are set to 0.
1389
1390*/
1391
1392/*!
1393 \fn QImage QImage::copy(const QRect& rectangle) const
1394
1395 Returns a sub-area of the image as a new image.
1396
1397 The returned image is copied from the position (\a
1398 {rectangle}.x(), \a{rectangle}.y()) in this image, and will always
1399 have the size of the given \a rectangle.
1400
1401 In areas beyond this image, pixels are set to 0. For 32-bit RGB
1402 images, this means black; for 32-bit ARGB images, this means
1403 transparent black; for 8-bit images, this means the color with
1404 index 0 in the color table which can be anything; for 1-bit
1405 images, this means Qt::color0.
1406
1407 If the given \a rectangle is a null rectangle the entire image is
1408 copied.
1409
1410 \sa QImage()
1411*/
1412QImage QImage::copy(const QRect& r) const
1413{
1414 if (!d)
1415 return QImage();
1416
1417 if (r.isNull()) {
1418 QImage image(d->width, d->height, d->format);
1419 if (image.isNull())
1420 return image;
1421
1422 // Qt for Embedded Linux can create images with non-default bpl
1423 // make sure we don't crash.
1424 if (image.d->nbytes != d->nbytes) {
1425 int bpl = image.bytesPerLine();
1426 for (int i = 0; i < height(); i++)
1427 memcpy(image.scanLine(i), scanLine(i), bpl);
1428 } else
1429 memcpy(image.bits(), bits(), d->nbytes);
1430 image.d->colortable = d->colortable;
1431 image.d->dpmx = d->dpmx;
1432 image.d->dpmy = d->dpmy;
1433 image.d->offset = d->offset;
1434 image.d->has_alpha_clut = d->has_alpha_clut;
1435#ifndef QT_NO_IMAGE_TEXT
1436 image.d->text = d->text;
1437#endif
1438 return image;
1439 }
1440
1441 int x = r.x();
1442 int y = r.y();
1443 int w = r.width();
1444 int h = r.height();
1445
1446 int dx = 0;
1447 int dy = 0;
1448 if (w <= 0 || h <= 0)
1449 return QImage();
1450
1451 QImage image(w, h, d->format);
1452 if (image.isNull())
1453 return image;
1454
1455 if (x < 0 || y < 0 || x + w > d->width || y + h > d->height) {
1456 // bitBlt will not cover entire image - clear it.
1457 image.fill(0);
1458 if (x < 0) {
1459 dx = -x;
1460 x = 0;
1461 }
1462 if (y < 0) {
1463 dy = -y;
1464 y = 0;
1465 }
1466 }
1467
1468 image.d->colortable = d->colortable;
1469
1470 int pixels_to_copy = qMax(w - dx, 0);
1471 if (x > d->width)
1472 pixels_to_copy = 0;
1473 else if (pixels_to_copy > d->width - x)
1474 pixels_to_copy = d->width - x;
1475 int lines_to_copy = qMax(h - dy, 0);
1476 if (y > d->height)
1477 lines_to_copy = 0;
1478 else if (lines_to_copy > d->height - y)
1479 lines_to_copy = d->height - y;
1480
1481 bool byteAligned = true;
1482 if (d->format == Format_Mono || d->format == Format_MonoLSB)
1483 byteAligned = !(dx & 7) && !(x & 7) && !(pixels_to_copy & 7);
1484
1485 if (byteAligned) {
1486 const uchar *src = d->data + ((x * d->depth) >> 3) + y * d->bytes_per_line;
1487 uchar *dest = image.d->data + ((dx * d->depth) >> 3) + dy * image.d->bytes_per_line;
1488 const int bytes_to_copy = (pixels_to_copy * d->depth) >> 3;
1489 for (int i = 0; i < lines_to_copy; ++i) {
1490 memcpy(dest, src, bytes_to_copy);
1491 src += d->bytes_per_line;
1492 dest += image.d->bytes_per_line;
1493 }
1494 } else if (d->format == Format_Mono) {
1495 const uchar *src = d->data + y * d->bytes_per_line;
1496 uchar *dest = image.d->data + dy * image.d->bytes_per_line;
1497 for (int i = 0; i < lines_to_copy; ++i) {
1498 for (int j = 0; j < pixels_to_copy; ++j) {
1499 if (src[(x + j) >> 3] & (0x80 >> ((x + j) & 7)))
1500 dest[(dx + j) >> 3] |= (0x80 >> ((dx + j) & 7));
1501 else
1502 dest[(dx + j) >> 3] &= ~(0x80 >> ((dx + j) & 7));
1503 }
1504 src += d->bytes_per_line;
1505 dest += image.d->bytes_per_line;
1506 }
1507 } else { // Format_MonoLSB
1508 Q_ASSERT(d->format == Format_MonoLSB);
1509 const uchar *src = d->data + y * d->bytes_per_line;
1510 uchar *dest = image.d->data + dy * image.d->bytes_per_line;
1511 for (int i = 0; i < lines_to_copy; ++i) {
1512 for (int j = 0; j < pixels_to_copy; ++j) {
1513 if (src[(x + j) >> 3] & (0x1 << ((x + j) & 7)))
1514 dest[(dx + j) >> 3] |= (0x1 << ((dx + j) & 7));
1515 else
1516 dest[(dx + j) >> 3] &= ~(0x1 << ((dx + j) & 7));
1517 }
1518 src += d->bytes_per_line;
1519 dest += image.d->bytes_per_line;
1520 }
1521 }
1522
1523 image.d->dpmx = dotsPerMeterX();
1524 image.d->dpmy = dotsPerMeterY();
1525 image.d->offset = offset();
1526 image.d->has_alpha_clut = d->has_alpha_clut;
1527#ifndef QT_NO_IMAGE_TEXT
1528 image.d->text = d->text;
1529#endif
1530 return image;
1531}
1532
1533
1534/*!
1535 \fn bool QImage::isNull() const
1536
1537 Returns true if it is a null image, otherwise returns false.
1538
1539 A null image has all parameters set to zero and no allocated data.
1540*/
1541bool QImage::isNull() const
1542{
1543 return !d;
1544}
1545
1546/*!
1547 \fn int QImage::width() const
1548
1549 Returns the width of the image.
1550
1551 \sa {QImage#Image Information}{Image Information}
1552*/
1553int QImage::width() const
1554{
1555 return d ? d->width : 0;
1556}
1557
1558/*!
1559 \fn int QImage::height() const
1560
1561 Returns the height of the image.
1562
1563 \sa {QImage#Image Information}{Image Information}
1564*/
1565int QImage::height() const
1566{
1567 return d ? d->height : 0;
1568}
1569
1570/*!
1571 \fn QSize QImage::size() const
1572
1573 Returns the size of the image, i.e. its width() and height().
1574
1575 \sa {QImage#Image Information}{Image Information}
1576*/
1577QSize QImage::size() const
1578{
1579 return d ? QSize(d->width, d->height) : QSize(0, 0);
1580}
1581
1582/*!
1583 \fn QRect QImage::rect() const
1584
1585 Returns the enclosing rectangle (0, 0, width(), height()) of the
1586 image.
1587
1588 \sa {QImage#Image Information}{Image Information}
1589*/
1590QRect QImage::rect() const
1591{
1592 return d ? QRect(0, 0, d->width, d->height) : QRect();
1593}
1594
1595/*!
1596 Returns the depth of the image.
1597
1598 The image depth is the number of bits used to store a single
1599 pixel, also called bits per pixel (bpp).
1600
1601 The supported depths are 1, 8, 16, 24 and 32.
1602
1603 \sa bitPlaneCount(), convertToFormat(), {QImage#Image Formats}{Image Formats},
1604 {QImage#Image Information}{Image Information}
1605
1606*/
1607int QImage::depth() const
1608{
1609 return d ? d->depth : 0;
1610}
1611
1612/*!
1613 \obsolete
1614 \fn int QImage::numColors() const
1615
1616 Returns the size of the color table for the image.
1617
1618 \sa setColorCount()
1619*/
1620int QImage::numColors() const
1621{
1622 return d ? d->colortable.size() : 0;
1623}
1624
1625/*!
1626 \since 4.6
1627 \fn int QImage::colorCount() const
1628
1629 Returns the size of the color table for the image.
1630
1631 Notice that colorCount() returns 0 for 32-bpp images because these
1632 images do not use color tables, but instead encode pixel values as
1633 ARGB quadruplets.
1634
1635 \sa setColorCount(), {QImage#Image Information}{Image Information}
1636*/
1637int QImage::colorCount() const
1638{
1639 return d ? d->colortable.size() : 0;
1640}
1641
1642
1643#ifdef QT3_SUPPORT
1644/*!
1645 \fn QImage::Endian QImage::bitOrder() const
1646
1647 Returns the bit order for the image. If it is a 1-bpp image, this
1648 function returns either QImage::BigEndian or
1649 QImage::LittleEndian. Otherwise, this function returns
1650 QImage::IgnoreEndian.
1651
1652 Use the format() function instead for the monochrome formats. For
1653 non-monochrome formats the bit order is irrelevant.
1654*/
1655
1656/*!
1657 Returns a pointer to the scanline pointer table. This is the
1658 beginning of the data block for the image.
1659 Returns 0 in case of an error.
1660
1661 Use the bits() or scanLine() function instead.
1662*/
1663uchar **QImage::jumpTable()
1664{
1665 if (!d)
1666 return 0;
1667 detach();
1668
1669 // in case detach() ran out of memory..
1670 if (!d)
1671 return 0;
1672
1673 if (!d->jumptable) {
1674 d->jumptable = (uchar **)malloc(d->height*sizeof(uchar *));
1675 if (!d->jumptable)
1676 return 0;
1677 uchar *data = d->data;
1678 int height = d->height;
1679 uchar **p = d->jumptable;
1680 while (height--) {
1681 *p++ = data;
1682 data += d->bytes_per_line;
1683 }
1684 }
1685 return d->jumptable;
1686}
1687
1688/*!
1689 \overload
1690*/
1691const uchar * const *QImage::jumpTable() const
1692{
1693 if (!d)
1694 return 0;
1695 if (!d->jumptable) {
1696 d->jumptable = (uchar **)malloc(d->height*sizeof(uchar *));
1697 if (!d->jumptable)
1698 return 0;
1699 uchar *data = d->data;
1700 int height = d->height;
1701 uchar **p = d->jumptable;
1702 while (height--) {
1703 *p++ = data;
1704 data += d->bytes_per_line;
1705 }
1706 }
1707 return d->jumptable;
1708}
1709#endif
1710
1711/*!
1712 Sets the color table used to translate color indexes to QRgb
1713 values, to the specified \a colors.
1714
1715 When the image is used, the color table must be large enough to
1716 have entries for all the pixel/index values present in the image,
1717 otherwise the results are undefined.
1718
1719 \sa colorTable(), setColor(), {QImage#Image Transformations}{Image
1720 Transformations}
1721*/
1722void QImage::setColorTable(const QVector<QRgb> colors)
1723{
1724 if (!d)
1725 return;
1726 detach();
1727
1728 // In case detach() ran out of memory
1729 if (!d)
1730 return;
1731
1732 d->colortable = colors;
1733 d->has_alpha_clut = false;
1734 for (int i = 0; i < d->colortable.size(); ++i) {
1735 if (qAlpha(d->colortable.at(i)) != 255) {
1736 d->has_alpha_clut = true;
1737 break;
1738 }
1739 }
1740}
1741
1742/*!
1743 Returns a list of the colors contained in the image's color table,
1744 or an empty list if the image does not have a color table
1745
1746 \sa setColorTable(), colorCount(), color()
1747*/
1748QVector<QRgb> QImage::colorTable() const
1749{
1750 return d ? d->colortable : QVector<QRgb>();
1751}
1752
1753
1754/*!
1755 \obsolete
1756 Returns the number of bytes occupied by the image data.
1757
1758 \sa byteCount()
1759*/
1760int QImage::numBytes() const
1761{
1762 return d ? d->nbytes : 0;
1763}
1764
1765/*!
1766 \since 4.6
1767 Returns the number of bytes occupied by the image data.
1768
1769 \sa bytesPerLine(), bits(), {QImage#Image Information}{Image
1770 Information}
1771*/
1772int QImage::byteCount() const
1773{
1774 return d ? d->nbytes : 0;
1775}
1776
1777/*!
1778 Returns the number of bytes per image scanline.
1779
1780 This is equivalent to byteCount() / height().
1781
1782 \sa scanLine()
1783*/
1784int QImage::bytesPerLine() const
1785{
1786 return (d && d->height) ? d->nbytes / d->height : 0;
1787}
1788
1789
1790/*!
1791 Returns the color in the color table at index \a i. The first
1792 color is at index 0.
1793
1794 The colors in an image's color table are specified as ARGB
1795 quadruplets (QRgb). Use the qAlpha(), qRed(), qGreen(), and
1796 qBlue() functions to get the color value components.
1797
1798 \sa setColor(), pixelIndex(), {QImage#Pixel Manipulation}{Pixel
1799 Manipulation}
1800*/
1801QRgb QImage::color(int i) const
1802{
1803 Q_ASSERT(i < colorCount());
1804 return d ? d->colortable.at(i) : QRgb(uint(-1));
1805}
1806
1807/*!
1808 \fn void QImage::setColor(int index, QRgb colorValue)
1809
1810 Sets the color at the given \a index in the color table, to the
1811 given to \a colorValue. The color value is an ARGB quadruplet.
1812
1813 If \a index is outside the current size of the color table, it is
1814 expanded with setColorCount().
1815
1816 \sa color(), colorCount(), setColorTable(), {QImage#Pixel Manipulation}{Pixel
1817 Manipulation}
1818*/
1819void QImage::setColor(int i, QRgb c)
1820{
1821 if (!d)
1822 return;
1823 if (i < 0 || d->depth > 8 || i >= 1<<d->depth) {
1824 qWarning("QImage::setColor: Index out of bound %d", i);
1825 return;
1826 }
1827 detach();
1828
1829 // In case detach() run out of memory
1830 if (!d)
1831 return;
1832
1833 if (i >= d->colortable.size())
1834 setColorCount(i+1);
1835 d->colortable[i] = c;
1836 d->has_alpha_clut |= (qAlpha(c) != 255);
1837}
1838
1839/*!
1840 Returns a pointer to the pixel data at the scanline with index \a
1841 i. The first scanline is at index 0.
1842
1843 The scanline data is aligned on a 32-bit boundary.
1844
1845 \warning If you are accessing 32-bpp image data, cast the returned
1846 pointer to \c{QRgb*} (QRgb has a 32-bit size) and use it to
1847 read/write the pixel value. You cannot use the \c{uchar*} pointer
1848 directly, because the pixel format depends on the byte order on
1849 the underlying platform. Use qRed(), qGreen(), qBlue(), and
1850 qAlpha() to access the pixels.
1851
1852 \sa bytesPerLine(), bits(), {QImage#Pixel Manipulation}{Pixel
1853 Manipulation}, constScanLine()
1854*/
1855uchar *QImage::scanLine(int i)
1856{
1857 if (!d)
1858 return 0;
1859
1860 detach();
1861
1862 // In case detach() ran out of memory
1863 if (!d)
1864 return 0;
1865
1866 return d->data + i * d->bytes_per_line;
1867}
1868
1869/*!
1870 \overload
1871*/
1872const uchar *QImage::scanLine(int i) const
1873{
1874 if (!d)
1875 return 0;
1876
1877 Q_ASSERT(i >= 0 && i < height());
1878 return d->data + i * d->bytes_per_line;
1879}
1880
1881
1882/*!
1883 Returns a pointer to the pixel data at the scanline with index \a
1884 i. The first scanline is at index 0.
1885
1886 The scanline data is aligned on a 32-bit boundary.
1887
1888 Note that QImage uses \l{Implicit Data Sharing} {implicit data
1889 sharing}, but this function does \e not perform a deep copy of the
1890 shared pixel data, because the returned data is const.
1891
1892 \sa scanLine(), constBits()
1893 \since 4.7
1894*/
1895const uchar *QImage::constScanLine(int i) const
1896{
1897 if (!d)
1898 return 0;
1899
1900 Q_ASSERT(i >= 0 && i < height());
1901 return d->data + i * d->bytes_per_line;
1902}
1903
1904/*!
1905 Returns a pointer to the first pixel data. This is equivalent to
1906 scanLine(0).
1907
1908 Note that QImage uses \l{Implicit Data Sharing} {implicit data
1909 sharing}. This function performs a deep copy of the shared pixel
1910 data, thus ensuring that this QImage is the only one using the
1911 current return value.
1912
1913 \sa scanLine(), byteCount(), constBits()
1914*/
1915uchar *QImage::bits()
1916{
1917 if (!d)
1918 return 0;
1919 detach();
1920
1921 // In case detach ran out of memory...
1922 if (!d)
1923 return 0;
1924
1925 return d->data;
1926}
1927
1928/*!
1929 \overload
1930
1931 Note that QImage uses \l{Implicit Data Sharing} {implicit data
1932 sharing}, but this function does \e not perform a deep copy of the
1933 shared pixel data, because the returned data is const.
1934*/
1935const uchar *QImage::bits() const
1936{
1937 return d ? d->data : 0;
1938}
1939
1940
1941/*!
1942 Returns a pointer to the first pixel data.
1943
1944 Note that QImage uses \l{Implicit Data Sharing} {implicit data
1945 sharing}, but this function does \e not perform a deep copy of the
1946 shared pixel data, because the returned data is const.
1947
1948 \sa bits(), constScanLine()
1949 \since 4.7
1950*/
1951const uchar *QImage::constBits() const
1952{
1953 return d ? d->data : 0;
1954}
1955
1956/*!
1957 \fn void QImage::reset()
1958
1959 Resets all image parameters and deallocates the image data.
1960
1961 Assign a null image instead.
1962
1963 \oldcode
1964 QImage image;
1965 image.reset();
1966 \newcode
1967 QImage image;
1968 image = QImage();
1969 \endcode
1970*/
1971
1972/*!
1973 \fn void QImage::fill(uint pixelValue)
1974
1975 Fills the entire image with the given \a pixelValue.
1976
1977 If the depth of this image is 1, only the lowest bit is used. If
1978 you say fill(0), fill(2), etc., the image is filled with 0s. If
1979 you say fill(1), fill(3), etc., the image is filled with 1s. If
1980 the depth is 8, the lowest 8 bits are used and if the depth is 16
1981 the lowest 16 bits are used.
1982
1983 Note: QImage::pixel() returns the color of the pixel at the given
1984 coordinates while QColor::pixel() returns the pixel value of the
1985 underlying window system (essentially an index value), so normally
1986 you will want to use QImage::pixel() to use a color from an
1987 existing image or QColor::rgb() to use a specific color.
1988
1989 \sa depth(), {QImage#Image Transformations}{Image Transformations}
1990*/
1991
1992void QImage::fill(uint pixel)
1993{
1994 if (!d)
1995 return;
1996
1997 detach();
1998
1999 // In case detach() ran out of memory
2000 if (!d)
2001 return;
2002
2003 if (d->depth == 1 || d->depth == 8) {
2004 int w = d->width;
2005 if (d->depth == 1) {
2006 if (pixel & 1)
2007 pixel = 0xffffffff;
2008 else
2009 pixel = 0;
2010 w = (w + 7) / 8;
2011 } else {
2012 pixel &= 0xff;
2013 }
2014 qt_rectfill<quint8>(d->data, pixel, 0, 0,
2015 w, d->height, d->bytes_per_line);
2016 return;
2017 } else if (d->depth == 16) {
2018 qt_rectfill<quint16>(reinterpret_cast<quint16*>(d->data), pixel,
2019 0, 0, d->width, d->height, d->bytes_per_line);
2020 return;
2021 } else if (d->depth == 24) {
2022 qt_rectfill<quint24>(reinterpret_cast<quint24*>(d->data), pixel,
2023 0, 0, d->width, d->height, d->bytes_per_line);
2024 return;
2025 }
2026
2027 if (d->format == Format_RGB32)
2028 pixel |= 0xff000000;
2029
2030 qt_rectfill<uint>(reinterpret_cast<uint*>(d->data), pixel,
2031 0, 0, d->width, d->height, d->bytes_per_line);
2032}
2033
2034/*!
2035 Inverts all pixel values in the image.
2036
2037 The given invert \a mode only have a meaning when the image's
2038 depth is 32. The default \a mode is InvertRgb, which leaves the
2039 alpha channel unchanged. If the \a mode is InvertRgba, the alpha
2040 bits are also inverted.
2041
2042 Inverting an 8-bit image means to replace all pixels using color
2043 index \e i with a pixel using color index 255 minus \e i. The same
2044 is the case for a 1-bit image. Note that the color table is \e not
2045 changed.
2046
2047 \sa {QImage#Image Transformations}{Image Transformations}
2048*/
2049
2050void QImage::invertPixels(InvertMode mode)
2051{
2052 if (!d)
2053 return;
2054
2055 detach();
2056
2057 // In case detach() ran out of memory
2058 if (!d)
2059 return;
2060
2061 if (depth() != 32) {
2062 // number of used bytes pr line
2063 int bpl = (d->width * d->depth + 7) / 8;
2064 int pad = d->bytes_per_line - bpl;
2065 uchar *sl = d->data;
2066 for (int y=0; y<d->height; ++y) {
2067 for (int x=0; x<bpl; ++x)
2068 *sl++ ^= 0xff;
2069 sl += pad;
2070 }
2071 } else {
2072 quint32 *p = (quint32*)d->data;
2073 quint32 *end = (quint32*)(d->data + d->nbytes);
2074 uint xorbits = (mode == InvertRgba) ? 0xffffffff : 0x00ffffff;
2075 while (p < end)
2076 *p++ ^= xorbits;
2077 }
2078}
2079
2080/*!
2081 \fn void QImage::invertPixels(bool invertAlpha)
2082
2083 Use the invertPixels() function that takes a QImage::InvertMode
2084 parameter instead.
2085*/
2086
2087/*! \fn QImage::Endian QImage::systemByteOrder()
2088
2089 Determines the host computer byte order. Returns
2090 QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first).
2091
2092 This function is no longer relevant for QImage. Use QSysInfo
2093 instead.
2094*/
2095
2096// Windows defines these
2097#if defined(write)
2098# undef write
2099#endif
2100#if defined(close)
2101# undef close
2102#endif
2103#if defined(read)
2104# undef read
2105#endif
2106
2107/*!
2108 \obsolete
2109 Resizes the color table to contain \a numColors entries.
2110
2111 \sa setColorCount()
2112*/
2113
2114void QImage::setNumColors(int numColors)
2115{
2116 setColorCount(numColors);
2117}
2118
2119/*!
2120 \since 4.6
2121 Resizes the color table to contain \a colorCount entries.
2122
2123 If the color table is expanded, all the extra colors will be set to
2124 transparent (i.e qRgba(0, 0, 0, 0)).
2125
2126 When the image is used, the color table must be large enough to
2127 have entries for all the pixel/index values present in the image,
2128 otherwise the results are undefined.
2129
2130 \sa colorCount(), colorTable(), setColor(), {QImage#Image
2131 Transformations}{Image Transformations}
2132*/
2133
2134void QImage::setColorCount(int colorCount)
2135{
2136 if (!d) {
2137 qWarning("QImage::setColorCount: null image");
2138 return;
2139 }
2140
2141 detach();
2142
2143 // In case detach() ran out of memory
2144 if (!d)
2145 return;
2146
2147 if (colorCount == d->colortable.size())
2148 return;
2149 if (colorCount <= 0) { // use no color table
2150 d->colortable = QVector<QRgb>();
2151 return;
2152 }
2153 int nc = d->colortable.size();
2154 d->colortable.resize(colorCount);
2155 for (int i = nc; i < colorCount; ++i)
2156 d->colortable[i] = 0;
2157}
2158
2159/*!
2160 Returns the format of the image.
2161
2162 \sa {QImage#Image Formats}{Image Formats}
2163*/
2164QImage::Format QImage::format() const
2165{
2166 return d ? d->format : Format_Invalid;
2167}
2168
2169
2170#ifdef QT3_SUPPORT
2171/*!
2172 Returns true if alpha buffer mode is enabled; otherwise returns
2173 false.
2174
2175 Use the hasAlphaChannel() function instead.
2176
2177*/
2178bool QImage::hasAlphaBuffer() const
2179{
2180 if (!d)
2181 return false;
2182
2183 switch (d->format) {
2184 case Format_ARGB32:
2185 case Format_ARGB32_Premultiplied:
2186 case Format_ARGB8565_Premultiplied:
2187 case Format_ARGB8555_Premultiplied:
2188 case Format_ARGB6666_Premultiplied:
2189 case Format_ARGB4444_Premultiplied:
2190 return true;
2191 default:
2192 return false;
2193 }
2194}
2195
2196/*!
2197 Enables alpha buffer mode if \a enable is true, otherwise disables
2198 it. The alpha buffer is used to set a mask when a QImage is
2199 translated to a QPixmap.
2200
2201 If a monochrome or indexed 8-bit image has alpha channels in their
2202 color tables they will automatically detect that they have an
2203 alpha channel, so this function is not required. To force alpha
2204 channels on 32-bit images, use the convertToFormat() function.
2205*/
2206
2207void QImage::setAlphaBuffer(bool enable)
2208{
2209 if (!d
2210 || d->format == Format_Mono
2211 || d->format == Format_MonoLSB
2212 || d->format == Format_Indexed8)
2213 return;
2214 if (enable && (d->format == Format_ARGB32 ||
2215 d->format == Format_ARGB32_Premultiplied ||
2216 d->format == Format_ARGB8565_Premultiplied ||
2217 d->format == Format_ARGB6666_Premultiplied ||
2218 d->format == Format_ARGB8555_Premultiplied ||
2219 d->format == Format_ARGB4444_Premultiplied))
2220 {
2221 return;
2222 }
2223 if (!enable && (d->format == Format_RGB32 ||
2224 d->format == Format_RGB555 ||
2225 d->format == Format_RGB666 ||
2226 d->format == Format_RGB888 ||
2227 d->format == Format_RGB444))
2228 {
2229 return;
2230 }
2231 detach();
2232 d->format = (enable ? Format_ARGB32 : Format_RGB32);
2233}
2234
2235
2236/*!
2237 \fn bool QImage::create(int width, int height, int depth, int numColors, Endian bitOrder)
2238
2239 Sets the image \a width, \a height, \a depth, its number of colors
2240 (in \a numColors), and bit order. Returns true if successful, or
2241 false if the parameters are incorrect or if memory cannot be
2242 allocated.
2243
2244 The \a width and \a height is limited to 32767. \a depth must be
2245 1, 8, or 32. If \a depth is 1, \a bitOrder must be set to
2246 either QImage::LittleEndian or QImage::BigEndian. For other depths
2247 \a bitOrder must be QImage::IgnoreEndian.
2248
2249 This function allocates a color table and a buffer for the image
2250 data. The image data is not initialized. The image buffer is
2251 allocated as a single block that consists of a table of scanLine()
2252 pointers (jumpTable()) and the image data (bits()).
2253
2254 Use a QImage constructor instead.
2255*/
2256bool QImage::create(int width, int height, int depth, int numColors, Endian bitOrder)
2257{
2258 if (d && !d->ref.deref())
2259 delete d;
2260 d = QImageData::create(QSize(width, height), formatFor(depth, bitOrder), numColors);
2261 return true;
2262}
2263
2264/*!
2265 \fn bool QImage::create(const QSize& size, int depth, int numColors, Endian bitOrder)
2266 \overload
2267
2268 The width and height are specified in the \a size argument.
2269
2270 Use a QImage constructor instead.
2271*/
2272bool QImage::create(const QSize& size, int depth, int numColors, QImage::Endian bitOrder)
2273{
2274 if (d && !d->ref.deref())
2275 delete d;
2276 d = QImageData::create(size, formatFor(depth, bitOrder), numColors);
2277 return true;
2278}
2279#endif // QT3_SUPPORT
2280
2281/*****************************************************************************
2282 Internal routines for converting image depth.
2283 *****************************************************************************/
2284
2285typedef void (*Image_Converter)(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags);
2286
2287typedef bool (*InPlace_Image_Converter)(QImageData *data, Qt::ImageConversionFlags);
2288
2289static void convert_ARGB_to_ARGB_PM(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
2290{
2291 Q_ASSERT(src->format == QImage::Format_ARGB32);
2292 Q_ASSERT(dest->format == QImage::Format_ARGB32_Premultiplied);
2293 Q_ASSERT(src->width == dest->width);
2294 Q_ASSERT(src->height == dest->height);
2295
2296 const int src_pad = (src->bytes_per_line >> 2) - src->width;
2297 const int dest_pad = (dest->bytes_per_line >> 2) - dest->width;
2298 const QRgb *src_data = (QRgb *) src->data;
2299 QRgb *dest_data = (QRgb *) dest->data;
2300
2301 for (int i = 0; i < src->height; ++i) {
2302 const QRgb *end = src_data + src->width;
2303 while (src_data < end) {
2304 *dest_data = PREMUL(*src_data);
2305 ++src_data;
2306 ++dest_data;
2307 }
2308 src_data += src_pad;
2309 dest_data += dest_pad;
2310 }
2311}
2312
2313static bool convert_ARGB_to_ARGB_PM_inplace(QImageData *data, Qt::ImageConversionFlags)
2314{
2315 Q_ASSERT(data->format == QImage::Format_ARGB32);
2316
2317 const int pad = (data->bytes_per_line >> 2) - data->width;
2318 QRgb *rgb_data = (QRgb *) data->data;
2319
2320 for (int i = 0; i < data->height; ++i) {
2321 const QRgb *end = rgb_data + data->width;
2322 while (rgb_data < end) {
2323 *rgb_data = PREMUL(*rgb_data);
2324 ++rgb_data;
2325 }
2326 rgb_data += pad;
2327 }
2328 data->format = QImage::Format_ARGB32_Premultiplied;
2329 return true;
2330}
2331
2332static bool convert_indexed8_to_ARGB_PM_inplace(QImageData *data, Qt::ImageConversionFlags)
2333{
2334 Q_ASSERT(data->format == QImage::Format_Indexed8);
2335 const int depth = 32;
2336
2337 const int dst_bytes_per_line = ((data->width * depth + 31) >> 5) << 2;
2338 const int nbytes = dst_bytes_per_line * data->height;
2339 uchar *const newData = (uchar *)realloc(data->data, nbytes);
2340 if (!newData)
2341 return false;
2342
2343 data->data = newData;
2344
2345 // start converting from the end because the end image is bigger than the source
2346 uchar *src_data = newData + data->nbytes; // end of src
2347 quint32 *dest_data = (quint32 *) (newData + nbytes); // end of dest > end of src
2348 const int width = data->width;
2349 const int src_pad = data->bytes_per_line - width;
2350 const int dest_pad = (dst_bytes_per_line >> 2) - width;
2351 if (data->colortable.size() == 0) {
2352 data->colortable.resize(256);
2353 for (int i = 0; i < 256; ++i)
2354 data->colortable[i] = qRgb(i, i, i);
2355 } else {
2356 for (int i = 0; i < data->colortable.size(); ++i)
2357 data->colortable[i] = PREMUL(data->colortable.at(i));
2358
2359 // Fill the rest of the table in case src_data > colortable.size()
2360 const int oldSize = data->colortable.size();
2361 const QRgb lastColor = data->colortable.at(oldSize - 1);
2362 data->colortable.insert(oldSize, 256 - oldSize, lastColor);
2363 }
2364
2365 for (int i = 0; i < data->height; ++i) {
2366 src_data -= src_pad;
2367 dest_data -= dest_pad;
2368 for (int pixI = 0; pixI < width; ++pixI) {
2369 --src_data;
2370 --dest_data;
2371 *dest_data = data->colortable.at(*src_data);
2372 }
2373 }
2374
2375 data->colortable = QVector<QRgb>();
2376 data->format = QImage::Format_ARGB32_Premultiplied;
2377 data->bytes_per_line = dst_bytes_per_line;
2378 data->depth = depth;
2379 data->nbytes = nbytes;
2380
2381 return true;
2382}
2383
2384static bool convert_indexed8_to_RGB_inplace(QImageData *data, Qt::ImageConversionFlags)
2385{
2386 Q_ASSERT(data->format == QImage::Format_Indexed8);
2387 const int depth = 32;
2388
2389 const int dst_bytes_per_line = ((data->width * depth + 31) >> 5) << 2;
2390 const int nbytes = dst_bytes_per_line * data->height;
2391 uchar *const newData = (uchar *)realloc(data->data, nbytes);
2392 if (!newData)
2393 return false;
2394
2395 data->data = newData;
2396
2397 // start converting from the end because the end image is bigger than the source
2398 uchar *src_data = newData + data->nbytes;
2399 quint32 *dest_data = (quint32 *) (newData + nbytes);
2400 const int width = data->width;
2401 const int src_pad = data->bytes_per_line - width;
2402 const int dest_pad = (dst_bytes_per_line >> 2) - width;
2403 if (data->colortable.size() == 0) {
2404 data->colortable.resize(256);
2405 for (int i = 0; i < 256; ++i)
2406 data->colortable[i] = qRgb(i, i, i);
2407 } else {
2408 // Fill the rest of the table in case src_data > colortable.size()
2409 const int oldSize = data->colortable.size();
2410 const QRgb lastColor = data->colortable.at(oldSize - 1);
2411 data->colortable.insert(oldSize, 256 - oldSize, lastColor);
2412 }
2413
2414 for (int i = 0; i < data->height; ++i) {
2415 src_data -= src_pad;
2416 dest_data -= dest_pad;
2417 for (int pixI = 0; pixI < width; ++pixI) {
2418 --src_data;
2419 --dest_data;
2420 *dest_data = (quint32) data->colortable.at(*src_data);
2421 }
2422 }
2423
2424 data->colortable = QVector<QRgb>();
2425 data->format = QImage::Format_RGB32;
2426 data->bytes_per_line = dst_bytes_per_line;
2427 data->depth = depth;
2428 data->nbytes = nbytes;
2429
2430 return true;
2431}
2432
2433static bool convert_indexed8_to_RGB16_inplace(QImageData *data, Qt::ImageConversionFlags)
2434{
2435 Q_ASSERT(data->format == QImage::Format_Indexed8);
2436 const int depth = 16;
2437
2438 const int dst_bytes_per_line = ((data->width * depth + 31) >> 5) << 2;
2439 const int nbytes = dst_bytes_per_line * data->height;
2440 uchar *const newData = (uchar *)realloc(data->data, nbytes);
2441 if (!newData)
2442 return false;
2443
2444 data->data = newData;
2445
2446 // start converting from the end because the end image is bigger than the source
2447 uchar *src_data = newData + data->nbytes;
2448 quint16 *dest_data = (quint16 *) (newData + nbytes);
2449 const int width = data->width;
2450 const int src_pad = data->bytes_per_line - width;
2451 const int dest_pad = (dst_bytes_per_line >> 1) - width;
2452
2453 quint16 colorTableRGB16[256];
2454 if (data->colortable.isEmpty()) {
2455 for (int i = 0; i < 256; ++i)
2456 colorTableRGB16[i] = qt_colorConvert<quint16, quint32>(qRgb(i, i, i), 0);
2457 } else {
2458 // 1) convert the existing colors to RGB16
2459 const int tableSize = data->colortable.size();
2460 for (int i = 0; i < tableSize; ++i)
2461 colorTableRGB16[i] = qt_colorConvert<quint16, quint32>(data->colortable.at(i), 0);
2462 data->colortable = QVector<QRgb>();
2463
2464 // 2) fill the rest of the table in case src_data > colortable.size()
2465 const quint16 lastColor = colorTableRGB16[tableSize - 1];
2466 for (int i = tableSize; i < 256; ++i)
2467 colorTableRGB16[i] = lastColor;
2468 }
2469
2470 for (int i = 0; i < data->height; ++i) {
2471 src_data -= src_pad;
2472 dest_data -= dest_pad;
2473 for (int pixI = 0; pixI < width; ++pixI) {
2474 --src_data;
2475 --dest_data;
2476 *dest_data = colorTableRGB16[*src_data];
2477 }
2478 }
2479
2480 data->format = QImage::Format_RGB16;
2481 data->bytes_per_line = dst_bytes_per_line;
2482 data->depth = depth;
2483 data->nbytes = nbytes;
2484
2485 return true;
2486}
2487
2488static bool convert_RGB_to_RGB16_inplace(QImageData *data, Qt::ImageConversionFlags)
2489{
2490 Q_ASSERT(data->format == QImage::Format_RGB32);
2491 const int depth = 16;
2492
2493 const int dst_bytes_per_line = ((data->width * depth + 31) >> 5) << 2;
2494 const int src_bytes_per_line = data->bytes_per_line;
2495 quint32 *src_data = (quint32 *) data->data;
2496 quint16 *dst_data = (quint16 *) data->data;
2497
2498 for (int i = 0; i < data->height; ++i) {
2499 qt_memconvert(dst_data, src_data, data->width);
2500 src_data = (quint32 *) (((char*)src_data) + src_bytes_per_line);
2501 dst_data = (quint16 *) (((char*)dst_data) + dst_bytes_per_line);
2502 }
2503 data->format = QImage::Format_RGB16;
2504 data->bytes_per_line = dst_bytes_per_line;
2505 data->depth = depth;
2506 data->nbytes = dst_bytes_per_line * data->height;
2507 uchar *const newData = (uchar *)realloc(data->data, data->nbytes);
2508 if (newData) {
2509 data->data = newData;
2510 return true;
2511 } else {
2512 return false;
2513 }
2514}
2515
2516static void convert_ARGB_PM_to_ARGB(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
2517{
2518 Q_ASSERT(src->format == QImage::Format_ARGB32_Premultiplied);
2519 Q_ASSERT(dest->format == QImage::Format_ARGB32);
2520 Q_ASSERT(src->width == dest->width);
2521 Q_ASSERT(src->height == dest->height);
2522
2523 const int src_pad = (src->bytes_per_line >> 2) - src->width;
2524 const int dest_pad = (dest->bytes_per_line >> 2) - dest->width;
2525 const QRgb *src_data = (QRgb *) src->data;
2526 QRgb *dest_data = (QRgb *) dest->data;
2527
2528 for (int i = 0; i < src->height; ++i) {
2529 const QRgb *end = src_data + src->width;
2530 while (src_data < end) {
2531 *dest_data = INV_PREMUL(*src_data);
2532 ++src_data;
2533 ++dest_data;
2534 }
2535 src_data += src_pad;
2536 dest_data += dest_pad;
2537 }
2538}
2539
2540static void convert_ARGB_PM_to_RGB(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
2541{
2542 Q_ASSERT(src->format == QImage::Format_ARGB32_Premultiplied);
2543 Q_ASSERT(dest->format == QImage::Format_RGB32);
2544 Q_ASSERT(src->width == dest->width);
2545 Q_ASSERT(src->height == dest->height);
2546
2547 const int src_pad = (src->bytes_per_line >> 2) - src->width;
2548 const int dest_pad = (dest->bytes_per_line >> 2) - dest->width;
2549 const QRgb *src_data = (QRgb *) src->data;
2550 QRgb *dest_data = (QRgb *) dest->data;
2551
2552 for (int i = 0; i < src->height; ++i) {
2553 const QRgb *end = src_data + src->width;
2554 while (src_data < end) {
2555 *dest_data = 0xff000000 | INV_PREMUL(*src_data);
2556 ++src_data;
2557 ++dest_data;
2558 }
2559 src_data += src_pad;
2560 dest_data += dest_pad;
2561 }
2562}
2563
2564static void swap_bit_order(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
2565{
2566 Q_ASSERT(src->format == QImage::Format_Mono || src->format == QImage::Format_MonoLSB);
2567 Q_ASSERT(dest->format == QImage::Format_Mono || dest->format == QImage::Format_MonoLSB);
2568 Q_ASSERT(src->width == dest->width);
2569 Q_ASSERT(src->height == dest->height);
2570 Q_ASSERT(src->nbytes == dest->nbytes);
2571 Q_ASSERT(src->bytes_per_line == dest->bytes_per_line);
2572
2573 dest->colortable = src->colortable;
2574
2575 const uchar *src_data = src->data;
2576 const uchar *end = src->data + src->nbytes;
2577 uchar *dest_data = dest->data;
2578 while (src_data < end) {
2579 *dest_data = bitflip[*src_data];
2580 ++src_data;
2581 ++dest_data;
2582 }
2583}
2584
2585static void mask_alpha_converter(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
2586{
2587 Q_ASSERT(src->width == dest->width);
2588 Q_ASSERT(src->height == dest->height);
2589
2590 const int src_pad = (src->bytes_per_line >> 2) - src->width;
2591 const int dest_pad = (dest->bytes_per_line >> 2) - dest->width;
2592 const uint *src_data = (const uint *)src->data;
2593 uint *dest_data = (uint *)dest->data;
2594
2595 for (int i = 0; i < src->height; ++i) {
2596 const uint *end = src_data + src->width;
2597 while (src_data < end) {
2598 *dest_data = *src_data | 0xff000000;
2599 ++src_data;
2600 ++dest_data;
2601 }
2602 src_data += src_pad;
2603 dest_data += dest_pad;
2604 }
2605}
2606
2607static QVector<QRgb> fix_color_table(const QVector<QRgb> &ctbl, QImage::Format format)
2608{
2609 QVector<QRgb> colorTable = ctbl;
2610 if (format == QImage::Format_RGB32) {
2611 // check if the color table has alpha
2612 for (int i = 0; i < colorTable.size(); ++i)
2613 if (qAlpha(colorTable.at(i) != 0xff))
2614 colorTable[i] = colorTable.at(i) | 0xff000000;
2615 } else if (format == QImage::Format_ARGB32_Premultiplied) {
2616 // check if the color table has alpha
2617 for (int i = 0; i < colorTable.size(); ++i)
2618 colorTable[i] = PREMUL(colorTable.at(i));
2619 }
2620 return colorTable;
2621}
2622
2623//
2624// dither_to_1: Uses selected dithering algorithm.
2625//
2626
2627static void dither_to_Mono(QImageData *dst, const QImageData *src,
2628 Qt::ImageConversionFlags flags, bool fromalpha)
2629{
2630 Q_ASSERT(src->width == dst->width);
2631 Q_ASSERT(src->height == dst->height);
2632 Q_ASSERT(dst->format == QImage::Format_Mono || dst->format == QImage::Format_MonoLSB);
2633
2634 dst->colortable.clear();
2635 dst->colortable.append(0xffffffff);
2636 dst->colortable.append(0xff000000);
2637
2638 enum { Threshold, Ordered, Diffuse } dithermode;
2639
2640 if (fromalpha) {
2641 if ((flags & Qt::AlphaDither_Mask) == Qt::DiffuseAlphaDither)
2642 dithermode = Diffuse;
2643 else if ((flags & Qt::AlphaDither_Mask) == Qt::OrderedAlphaDither)
2644 dithermode = Ordered;
2645 else
2646 dithermode = Threshold;
2647 } else {
2648 if ((flags & Qt::Dither_Mask) == Qt::ThresholdDither)
2649 dithermode = Threshold;
2650 else if ((flags & Qt::Dither_Mask) == Qt::OrderedDither)
2651 dithermode = Ordered;
2652 else
2653 dithermode = Diffuse;
2654 }
2655
2656 int w = src->width;
2657 int h = src->height;
2658 int d = src->depth;
2659 uchar gray[256]; // gray map for 8 bit images
2660 bool use_gray = (d == 8);
2661 if (use_gray) { // make gray map
2662 if (fromalpha) {
2663 // Alpha 0x00 -> 0 pixels (white)
2664 // Alpha 0xFF -> 1 pixels (black)
2665 for (int i = 0; i < src->colortable.size(); i++)
2666 gray[i] = (255 - (src->colortable.at(i) >> 24));
2667 } else {
2668 // Pixel 0x00 -> 1 pixels (black)
2669 // Pixel 0xFF -> 0 pixels (white)
2670 for (int i = 0; i < src->colortable.size(); i++)
2671 gray[i] = qGray(src->colortable.at(i));
2672 }
2673 }
2674
2675 uchar *dst_data = dst->data;
2676 int dst_bpl = dst->bytes_per_line;
2677 const uchar *src_data = src->data;
2678 int src_bpl = src->bytes_per_line;
2679
2680 switch (dithermode) {
2681 case Diffuse: {
2682 QScopedArrayPointer<int> lineBuffer(new int[w * 2]);
2683 int *line1 = lineBuffer.data();
2684 int *line2 = lineBuffer.data() + w;
2685 int bmwidth = (w+7)/8;
2686
2687 int *b1, *b2;
2688 int wbytes = w * (d/8);
2689 register const uchar *p = src->data;
2690 const uchar *end = p + wbytes;
2691 b2 = line2;
2692 if (use_gray) { // 8 bit image
2693 while (p < end)
2694 *b2++ = gray[*p++];
2695 } else { // 32 bit image
2696 if (fromalpha) {
2697 while (p < end) {
2698 *b2++ = 255 - (*(uint*)p >> 24);
2699 p += 4;
2700 }
2701 } else {
2702 while (p < end) {
2703 *b2++ = qGray(*(uint*)p);
2704 p += 4;
2705 }
2706 }
2707 }
2708 for (int y=0; y<h; y++) { // for each scan line...
2709 int *tmp = line1; line1 = line2; line2 = tmp;
2710 bool not_last_line = y < h - 1;
2711 if (not_last_line) { // calc. grayvals for next line
2712 p = src->data + (y+1)*src->bytes_per_line;
2713 end = p + wbytes;
2714 b2 = line2;
2715 if (use_gray) { // 8 bit image
2716 while (p < end)
2717 *b2++ = gray[*p++];
2718 } else { // 24 bit image
2719 if (fromalpha) {
2720 while (p < end) {
2721 *b2++ = 255 - (*(uint*)p >> 24);
2722 p += 4;
2723 }
2724 } else {
2725 while (p < end) {
2726 *b2++ = qGray(*(uint*)p);
2727 p += 4;
2728 }
2729 }
2730 }
2731 }
2732
2733 int err;
2734 uchar *p = dst->data + y*dst->bytes_per_line;
2735 memset(p, 0, bmwidth);
2736 b1 = line1;
2737 b2 = line2;
2738 int bit = 7;
2739 for (int x=1; x<=w; x++) {
2740 if (*b1 < 128) { // black pixel
2741 err = *b1++;
2742 *p |= 1 << bit;
2743 } else { // white pixel
2744 err = *b1++ - 255;
2745 }
2746 if (bit == 0) {
2747 p++;
2748 bit = 7;
2749 } else {
2750 bit--;
2751 }
2752 if (x < w)
2753 *b1 += (err*7)>>4; // spread error to right pixel
2754 if (not_last_line) {
2755 b2[0] += (err*5)>>4; // pixel below
2756 if (x > 1)
2757 b2[-1] += (err*3)>>4; // pixel below left
2758 if (x < w)
2759 b2[1] += err>>4; // pixel below right
2760 }
2761 b2++;
2762 }
2763 }
2764 } break;
2765 case Ordered: {
2766
2767 memset(dst->data, 0, dst->nbytes);
2768 if (d == 32) {
2769 for (int i=0; i<h; i++) {
2770 const uint *p = (const uint *)src_data;
2771 const uint *end = p + w;
2772 uchar *m = dst_data;
2773 int bit = 7;
2774 int j = 0;
2775 if (fromalpha) {
2776 while (p < end) {
2777 if ((*p++ >> 24) >= qt_bayer_matrix[j++&15][i&15])
2778 *m |= 1 << bit;
2779 if (bit == 0) {
2780 m++;
2781 bit = 7;
2782 } else {
2783 bit--;
2784 }
2785 }
2786 } else {
2787 while (p < end) {
2788 if ((uint)qGray(*p++) < qt_bayer_matrix[j++&15][i&15])
2789 *m |= 1 << bit;
2790 if (bit == 0) {
2791 m++;
2792 bit = 7;
2793 } else {
2794 bit--;
2795 }
2796 }
2797 }
2798 dst_data += dst_bpl;
2799 src_data += src_bpl;
2800 }
2801 } else
2802 /* (d == 8) */ {
2803 for (int i=0; i<h; i++) {
2804 const uchar *p = src_data;
2805 const uchar *end = p + w;
2806 uchar *m = dst_data;
2807 int bit = 7;
2808 int j = 0;
2809 while (p < end) {
2810 if ((uint)gray[*p++] < qt_bayer_matrix[j++&15][i&15])
2811 *m |= 1 << bit;
2812 if (bit == 0) {
2813 m++;
2814 bit = 7;
2815 } else {
2816 bit--;
2817 }
2818 }
2819 dst_data += dst_bpl;
2820 src_data += src_bpl;
2821 }
2822 }
2823 } break;
2824 default: { // Threshold:
2825 memset(dst->data, 0, dst->nbytes);
2826 if (d == 32) {
2827 for (int i=0; i<h; i++) {
2828 const uint *p = (const uint *)src_data;
2829 const uint *end = p + w;
2830 uchar *m = dst_data;
2831 int bit = 7;
2832 if (fromalpha) {
2833 while (p < end) {
2834 if ((*p++ >> 24) >= 128)
2835 *m |= 1 << bit; // Set mask "on"
2836 if (bit == 0) {
2837 m++;
2838 bit = 7;
2839 } else {
2840 bit--;
2841 }
2842 }
2843 } else {
2844 while (p < end) {
2845 if (qGray(*p++) < 128)
2846 *m |= 1 << bit; // Set pixel "black"
2847 if (bit == 0) {
2848 m++;
2849 bit = 7;
2850 } else {
2851 bit--;
2852 }
2853 }
2854 }
2855 dst_data += dst_bpl;
2856 src_data += src_bpl;
2857 }
2858 } else
2859 if (d == 8) {
2860 for (int i=0; i<h; i++) {
2861 const uchar *p = src_data;
2862 const uchar *end = p + w;
2863 uchar *m = dst_data;
2864 int bit = 7;
2865 while (p < end) {
2866 if (gray[*p++] < 128)
2867 *m |= 1 << bit; // Set mask "on"/ pixel "black"
2868 if (bit == 0) {
2869 m++;
2870 bit = 7;
2871 } else {
2872 bit--;
2873 }
2874 }
2875 dst_data += dst_bpl;
2876 src_data += src_bpl;
2877 }
2878 }
2879 }
2880 }
2881
2882 if (dst->format == QImage::Format_MonoLSB) {
2883 // need to swap bit order
2884 uchar *sl = dst->data;
2885 int bpl = (dst->width + 7) * dst->depth / 8;
2886 int pad = dst->bytes_per_line - bpl;
2887 for (int y=0; y<dst->height; ++y) {
2888 for (int x=0; x<bpl; ++x) {
2889 *sl = bitflip[*sl];
2890 ++sl;
2891 }
2892 sl += pad;
2893 }
2894 }
2895}
2896
2897static void convert_X_to_Mono(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
2898{
2899 dither_to_Mono(dst, src, flags, false);
2900}
2901
2902static void convert_ARGB_PM_to_Mono(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
2903{
2904 QScopedPointer<QImageData> tmp(QImageData::create(QSize(src->width, src->height), QImage::Format_ARGB32));
2905 convert_ARGB_PM_to_ARGB(tmp.data(), src, flags);
2906 dither_to_Mono(dst, tmp.data(), flags, false);
2907}
2908
2909//
2910// convert_32_to_8: Converts a 32 bits depth (true color) to an 8 bit
2911// image with a colormap. If the 32 bit image has more than 256 colors,
2912// we convert the red,green and blue bytes into a single byte encoded
2913// as 6 shades of each of red, green and blue.
2914//
2915// if dithering is needed, only 1 color at most is available for alpha.
2916//
2917struct QRgbMap {
2918 inline QRgbMap() : used(0) { }
2919 uchar pix;
2920 uchar used;
2921 QRgb rgb;
2922};
2923
2924static void convert_RGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
2925{
2926 Q_ASSERT(src->format == QImage::Format_RGB32 || src->format == QImage::Format_ARGB32);
2927 Q_ASSERT(dst->format == QImage::Format_Indexed8);
2928 Q_ASSERT(src->width == dst->width);
2929 Q_ASSERT(src->height == dst->height);
2930
2931 bool do_quant = (flags & Qt::DitherMode_Mask) == Qt::PreferDither
2932 || src->format == QImage::Format_ARGB32;
2933 uint alpha_mask = src->format == QImage::Format_RGB32 ? 0xff000000 : 0;
2934
2935 const int tablesize = 997; // prime
2936 QRgbMap table[tablesize];
2937 int pix=0;
2938
2939 if (!dst->colortable.isEmpty()) {
2940 QVector<QRgb> ctbl = dst->colortable;
2941 dst->colortable.resize(256);
2942 // Preload palette into table.
2943 // Almost same code as pixel insertion below
2944 for (int i = 0; i < dst->colortable.size(); ++i) {
2945 // Find in table...
2946 QRgb p = ctbl.at(i) | alpha_mask;
2947 int hash = p % tablesize;
2948 for (;;) {
2949 if (table[hash].used) {
2950 if (table[hash].rgb == p) {
2951 // Found previous insertion - use it
2952 break;
2953 } else {
2954 // Keep searching...
2955 if (++hash == tablesize) hash = 0;
2956 }
2957 } else {
2958 // Cannot be in table
2959 Q_ASSERT (pix != 256); // too many colors
2960 // Insert into table at this unused position
2961 dst->colortable[pix] = p;
2962 table[hash].pix = pix++;
2963 table[hash].rgb = p;
2964 table[hash].used = 1;
2965 break;
2966 }
2967 }
2968 }
2969 }
2970
2971 if ((flags & Qt::DitherMode_Mask) != Qt::PreferDither) {
2972 dst->colortable.resize(256);
2973 const uchar *src_data = src->data;
2974 uchar *dest_data = dst->data;
2975 for (int y = 0; y < src->height; y++) { // check if <= 256 colors
2976 const QRgb *s = (const QRgb *)src_data;
2977 uchar *b = dest_data;
2978 for (int x = 0; x < src->width; ++x) {
2979 QRgb p = s[x] | alpha_mask;
2980 int hash = p % tablesize;
2981 for (;;) {
2982 if (table[hash].used) {
2983 if (table[hash].rgb == (p)) {
2984 // Found previous insertion - use it
2985 break;
2986 } else {
2987 // Keep searching...
2988 if (++hash == tablesize) hash = 0;
2989 }
2990 } else {
2991 // Cannot be in table
2992 if (pix == 256) { // too many colors
2993 do_quant = true;
2994 // Break right out
2995 x = src->width;
2996 y = src->height;
2997 } else {
2998 // Insert into table at this unused position
2999 dst->colortable[pix] = p;
3000 table[hash].pix = pix++;
3001 table[hash].rgb = p;
3002 table[hash].used = 1;
3003 }
3004 break;
3005 }
3006 }
3007 *b++ = table[hash].pix; // May occur once incorrectly
3008 }
3009 src_data += src->bytes_per_line;
3010 dest_data += dst->bytes_per_line;
3011 }
3012 }
3013 int numColors = do_quant ? 256 : pix;
3014
3015 dst->colortable.resize(numColors);
3016
3017 if (do_quant) { // quantization needed
3018
3019#define MAX_R 5
3020#define MAX_G 5
3021#define MAX_B 5
3022#define INDEXOF(r,g,b) (((r)*(MAX_G+1)+(g))*(MAX_B+1)+(b))
3023
3024 for (int rc=0; rc<=MAX_R; rc++) // build 6x6x6 color cube
3025 for (int gc=0; gc<=MAX_G; gc++)
3026 for (int bc=0; bc<=MAX_B; bc++)
3027 dst->colortable[INDEXOF(rc,gc,bc)] = 0xff000000 | qRgb(rc*255/MAX_R, gc*255/MAX_G, bc*255/MAX_B);
3028
3029 const uchar *src_data = src->data;
3030 uchar *dest_data = dst->data;
3031 if ((flags & Qt::Dither_Mask) == Qt::ThresholdDither) {
3032 for (int y = 0; y < src->height; y++) {
3033 const QRgb *p = (const QRgb *)src_data;
3034 const QRgb *end = p + src->width;
3035 uchar *b = dest_data;
3036
3037 while (p < end) {
3038#define DITHER(p,m) ((uchar) ((p * (m) + 127) / 255))
3039 *b++ =
3040 INDEXOF(
3041 DITHER(qRed(*p), MAX_R),
3042 DITHER(qGreen(*p), MAX_G),
3043 DITHER(qBlue(*p), MAX_B)
3044 );
3045#undef DITHER
3046 p++;
3047 }
3048 src_data += src->bytes_per_line;
3049 dest_data += dst->bytes_per_line;
3050 }
3051 } else if ((flags & Qt::Dither_Mask) == Qt::DiffuseDither) {
3052 int* line1[3];
3053 int* line2[3];
3054 int* pv[3];
3055 QScopedArrayPointer<int> lineBuffer(new int[src->width * 9]);
3056 line1[0] = lineBuffer.data();
3057 line2[0] = lineBuffer.data() + src->width;
3058 line1[1] = lineBuffer.data() + src->width * 2;
3059 line2[1] = lineBuffer.data() + src->width * 3;
3060 line1[2] = lineBuffer.data() + src->width * 4;
3061 line2[2] = lineBuffer.data() + src->width * 5;
3062 pv[0] = lineBuffer.data() + src->width * 6;
3063 pv[1] = lineBuffer.data() + src->width * 7;
3064 pv[2] = lineBuffer.data() + src->width * 8;
3065
3066 int endian = (QSysInfo::ByteOrder == QSysInfo::BigEndian);
3067 for (int y = 0; y < src->height; y++) {
3068 const uchar* q = src_data;
3069 const uchar* q2 = y < src->height - 1 ? q + src->bytes_per_line : src->data;
3070 uchar *b = dest_data;
3071 for (int chan = 0; chan < 3; chan++) {
3072 int *l1 = (y&1) ? line2[chan] : line1[chan];
3073 int *l2 = (y&1) ? line1[chan] : line2[chan];
3074 if (y == 0) {
3075 for (int i = 0; i < src->width; i++)
3076 l1[i] = q[i*4+chan+endian];
3077 }
3078 if (y+1 < src->height) {
3079 for (int i = 0; i < src->width; i++)
3080 l2[i] = q2[i*4+chan+endian];
3081 }
3082 // Bi-directional error diffusion
3083 if (y&1) {
3084 for (int x = 0; x < src->width; x++) {
3085 int pix = qMax(qMin(5, (l1[x] * 5 + 128)/ 255), 0);
3086 int err = l1[x] - pix * 255 / 5;
3087 pv[chan][x] = pix;
3088
3089 // Spread the error around...
3090 if (x + 1< src->width) {
3091 l1[x+1] += (err*7)>>4;
3092 l2[x+1] += err>>4;
3093 }
3094 l2[x]+=(err*5)>>4;
3095 if (x>1)
3096 l2[x-1]+=(err*3)>>4;
3097 }
3098 } else {
3099 for (int x = src->width; x-- > 0;) {
3100 int pix = qMax(qMin(5, (l1[x] * 5 + 128)/ 255), 0);
3101 int err = l1[x] - pix * 255 / 5;
3102 pv[chan][x] = pix;
3103
3104 // Spread the error around...
3105 if (x > 0) {
3106 l1[x-1] += (err*7)>>4;
3107 l2[x-1] += err>>4;
3108 }
3109 l2[x]+=(err*5)>>4;
3110 if (x + 1 < src->width)
3111 l2[x+1]+=(err*3)>>4;
3112 }
3113 }
3114 }
3115 if (endian) {
3116 for (int x = 0; x < src->width; x++) {
3117 *b++ = INDEXOF(pv[0][x],pv[1][x],pv[2][x]);
3118 }
3119 } else {
3120 for (int x = 0; x < src->width; x++) {
3121 *b++ = INDEXOF(pv[2][x],pv[1][x],pv[0][x]);
3122 }
3123 }
3124 src_data += src->bytes_per_line;
3125 dest_data += dst->bytes_per_line;
3126 }
3127 } else { // OrderedDither
3128 for (int y = 0; y < src->height; y++) {
3129 const QRgb *p = (const QRgb *)src_data;
3130 const QRgb *end = p + src->width;
3131 uchar *b = dest_data;
3132
3133 int x = 0;
3134 while (p < end) {
3135 uint d = qt_bayer_matrix[y & 15][x & 15] << 8;
3136
3137#define DITHER(p, d, m) ((uchar) ((((256 * (m) + (m) + 1)) * (p) + (d)) >> 16))
3138 *b++ =
3139 INDEXOF(
3140 DITHER(qRed(*p), d, MAX_R),
3141 DITHER(qGreen(*p), d, MAX_G),
3142 DITHER(qBlue(*p), d, MAX_B)
3143 );
3144#undef DITHER
3145
3146 p++;
3147 x++;
3148 }
3149 src_data += src->bytes_per_line;
3150 dest_data += dst->bytes_per_line;
3151 }
3152 }
3153
3154 if (src->format != QImage::Format_RGB32
3155 && src->format != QImage::Format_RGB16) {
3156 const int trans = 216;
3157 Q_ASSERT(dst->colortable.size() > trans);
3158 dst->colortable[trans] = 0;
3159 QScopedPointer<QImageData> mask(QImageData::create(QSize(src->width, src->height), QImage::Format_Mono));
3160 dither_to_Mono(mask.data(), src, flags, true);
3161 uchar *dst_data = dst->data;
3162 const uchar *mask_data = mask->data;
3163 for (int y = 0; y < src->height; y++) {
3164 for (int x = 0; x < src->width ; x++) {
3165 if (!(mask_data[x>>3] & (0x80 >> (x & 7))))
3166 dst_data[x] = trans;
3167 }
3168 mask_data += mask->bytes_per_line;
3169 dst_data += dst->bytes_per_line;
3170 }
3171 dst->has_alpha_clut = true;
3172 }
3173
3174#undef MAX_R
3175#undef MAX_G
3176#undef MAX_B
3177#undef INDEXOF
3178
3179 }
3180}
3181
3182static void convert_ARGB_PM_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
3183{
3184 QScopedPointer<QImageData> tmp(QImageData::create(QSize(src->width, src->height), QImage::Format_ARGB32));
3185 convert_ARGB_PM_to_ARGB(tmp.data(), src, flags);
3186 convert_RGB_to_Indexed8(dst, tmp.data(), flags);
3187}
3188
3189static void convert_ARGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
3190{
3191 convert_RGB_to_Indexed8(dst, src, flags);
3192}
3193
3194static void convert_Indexed8_to_X32(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
3195{
3196 Q_ASSERT(src->format == QImage::Format_Indexed8);
3197 Q_ASSERT(dest->format == QImage::Format_RGB32
3198 || dest->format == QImage::Format_ARGB32
3199 || dest->format == QImage::Format_ARGB32_Premultiplied);
3200 Q_ASSERT(src->width == dest->width);
3201 Q_ASSERT(src->height == dest->height);
3202
3203 QVector<QRgb> colorTable = fix_color_table(src->colortable, dest->format);
3204 if (colorTable.size() == 0) {
3205 colorTable.resize(256);
3206 for (int i=0; i<256; ++i)
3207 colorTable[i] = qRgb(i, i, i);
3208 }
3209
3210 int w = src->width;
3211 const uchar *src_data = src->data;
3212 uchar *dest_data = dest->data;
3213 int tableSize = colorTable.size() - 1;
3214 for (int y = 0; y < src->height; y++) {
3215 uint *p = (uint *)dest_data;
3216 const uchar *b = src_data;
3217 uint *end = p + w;
3218
3219 while (p < end)
3220 *p++ = colorTable.at(qMin<int>(tableSize, *b++));
3221
3222 src_data += src->bytes_per_line;
3223 dest_data += dest->bytes_per_line;
3224 }
3225}
3226
3227static void convert_Mono_to_X32(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
3228{
3229 Q_ASSERT(src->format == QImage::Format_Mono || src->format == QImage::Format_MonoLSB);
3230 Q_ASSERT(dest->format == QImage::Format_RGB32
3231 || dest->format == QImage::Format_ARGB32
3232 || dest->format == QImage::Format_ARGB32_Premultiplied);
3233 Q_ASSERT(src->width == dest->width);
3234 Q_ASSERT(src->height == dest->height);
3235
3236 QVector<QRgb> colorTable = fix_color_table(src->colortable, dest->format);
3237
3238 // Default to black / white colors
3239 if (colorTable.size() < 2) {
3240 if (colorTable.size() == 0)
3241 colorTable << 0xff000000;
3242 colorTable << 0xffffffff;
3243 }
3244
3245 const uchar *src_data = src->data;
3246 uchar *dest_data = dest->data;
3247 if (src->format == QImage::Format_Mono) {
3248 for (int y = 0; y < dest->height; y++) {
3249 register uint *p = (uint *)dest_data;
3250 for (int x = 0; x < dest->width; x++)
3251 *p++ = colorTable.at((src_data[x>>3] >> (7 - (x & 7))) & 1);
3252
3253 src_data += src->bytes_per_line;
3254 dest_data += dest->bytes_per_line;
3255 }
3256 } else {
3257 for (int y = 0; y < dest->height; y++) {
3258 register uint *p = (uint *)dest_data;
3259 for (int x = 0; x < dest->width; x++)
3260 *p++ = colorTable.at((src_data[x>>3] >> (x & 7)) & 1);
3261
3262 src_data += src->bytes_per_line;
3263 dest_data += dest->bytes_per_line;
3264 }
3265 }
3266}
3267
3268
3269static void convert_Mono_to_Indexed8(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
3270{
3271 Q_ASSERT(src->format == QImage::Format_Mono || src->format == QImage::Format_MonoLSB);
3272 Q_ASSERT(dest->format == QImage::Format_Indexed8);
3273 Q_ASSERT(src->width == dest->width);
3274 Q_ASSERT(src->height == dest->height);
3275
3276 QVector<QRgb> ctbl = src->colortable;
3277 if (ctbl.size() > 2) {
3278 ctbl.resize(2);
3279 } else if (ctbl.size() < 2) {
3280 if (ctbl.size() == 0)
3281 ctbl << 0xff000000;
3282 ctbl << 0xffffffff;
3283 }
3284 dest->colortable = ctbl;
3285 dest->has_alpha_clut = src->has_alpha_clut;
3286
3287
3288 const uchar *src_data = src->data;
3289 uchar *dest_data = dest->data;
3290 if (src->format == QImage::Format_Mono) {
3291 for (int y = 0; y < dest->height; y++) {
3292 register uchar *p = dest_data;
3293 for (int x = 0; x < dest->width; x++)
3294 *p++ = (src_data[x>>3] >> (7 - (x & 7))) & 1;
3295 src_data += src->bytes_per_line;
3296 dest_data += dest->bytes_per_line;
3297 }
3298 } else {
3299 for (int y = 0; y < dest->height; y++) {
3300 register uchar *p = dest_data;
3301 for (int x = 0; x < dest->width; x++)
3302 *p++ = (src_data[x>>3] >> (x & 7)) & 1;
3303 src_data += src->bytes_per_line;
3304 dest_data += dest->bytes_per_line;
3305 }
3306 }
3307}
3308
3309#define CONVERT_DECL(DST, SRC) \
3310 static void convert_##SRC##_to_##DST(QImageData *dest, \
3311 const QImageData *src, \
3312 Qt::ImageConversionFlags) \
3313 { \
3314 qt_rectconvert<DST, SRC>(reinterpret_cast<DST*>(dest->data), \
3315 reinterpret_cast<const SRC*>(src->data), \
3316 0, 0, src->width, src->height, \
3317 dest->bytes_per_line, src->bytes_per_line); \
3318 }
3319
3320CONVERT_DECL(quint32, quint16)
3321CONVERT_DECL(quint16, quint32)
3322CONVERT_DECL(quint32, qargb8565)
3323CONVERT_DECL(qargb8565, quint32)
3324CONVERT_DECL(quint32, qrgb555)
3325CONVERT_DECL(qrgb666, quint32)
3326CONVERT_DECL(quint32, qrgb666)
3327CONVERT_DECL(qargb6666, quint32)
3328CONVERT_DECL(quint32, qargb6666)
3329CONVERT_DECL(qrgb555, quint32)
3330#if !defined(Q_WS_QWS) || (defined(QT_QWS_DEPTH_15) && defined(QT_QWS_DEPTH_16))
3331CONVERT_DECL(quint16, qrgb555)
3332CONVERT_DECL(qrgb555, quint16)
3333#endif
3334CONVERT_DECL(quint32, qrgb888)
3335CONVERT_DECL(qrgb888, quint32)
3336CONVERT_DECL(quint32, qargb8555)
3337CONVERT_DECL(qargb8555, quint32)
3338CONVERT_DECL(quint32, qrgb444)
3339CONVERT_DECL(qrgb444, quint32)
3340CONVERT_DECL(quint32, qargb4444)
3341CONVERT_DECL(qargb4444, quint32)
3342#undef CONVERT_DECL
3343#define CONVERT_PTR(DST, SRC) convert_##SRC##_to_##DST
3344
3345/*
3346 Format_Invalid,
3347 Format_Mono,
3348 Format_MonoLSB,
3349 Format_Indexed8,
3350 Format_RGB32,
3351 Format_ARGB32,
3352 Format_ARGB32_Premultiplied,
3353 Format_RGB16,
3354 Format_ARGB8565_Premultiplied,
3355 Format_RGB666,
3356 Format_ARGB6666_Premultiplied,
3357 Format_RGB555,
3358 Format_ARGB8555_Premultiplied,
3359 Format_RGB888
3360 Format_RGB444
3361 Format_ARGB4444_Premultiplied
3362*/
3363
3364
3365// first index source, second dest
3366static Image_Converter converter_map[QImage::NImageFormats][QImage::NImageFormats] =
3367{
3368 {
3369 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3370 },
3371 {
3372 0,
3373 0,
3374 swap_bit_order,
3375 convert_Mono_to_Indexed8,
3376 convert_Mono_to_X32,
3377 convert_Mono_to_X32,
3378 convert_Mono_to_X32,
3379 0,
3380 0,
3381 0,
3382 0,
3383 0,
3384 0,
3385 0,
3386 0,
3387 0
3388 }, // Format_Mono
3389
3390 {
3391 0,
3392 swap_bit_order,
3393 0,
3394 convert_Mono_to_Indexed8,
3395 convert_Mono_to_X32,
3396 convert_Mono_to_X32,
3397 convert_Mono_to_X32,
3398 0,
3399 0,
3400 0,
3401 0,
3402 0,
3403 0,
3404 0,
3405 0,
3406 0
3407 }, // Format_MonoLSB
3408
3409 {
3410 0,
3411 convert_X_to_Mono,
3412 convert_X_to_Mono,
3413 0,
3414 convert_Indexed8_to_X32,
3415 convert_Indexed8_to_X32,
3416 convert_Indexed8_to_X32,
3417 0,
3418 0,
3419 0,
3420 0,
3421 0,
3422 0,
3423 0,
3424 0,
3425 0
3426 }, // Format_Indexed8
3427
3428 {
3429 0,
3430 convert_X_to_Mono,
3431 convert_X_to_Mono,
3432 convert_RGB_to_Indexed8,
3433 0,
3434 mask_alpha_converter,
3435 mask_alpha_converter,
3436 CONVERT_PTR(quint16, quint32),
3437 CONVERT_PTR(qargb8565, quint32),
3438 CONVERT_PTR(qrgb666, quint32),
3439 CONVERT_PTR(qargb6666, quint32),
3440 CONVERT_PTR(qrgb555, quint32),
3441 CONVERT_PTR(qargb8555, quint32),
3442 CONVERT_PTR(qrgb888, quint32),
3443 CONVERT_PTR(qrgb444, quint32),
3444 CONVERT_PTR(qargb4444, quint32)
3445 }, // Format_RGB32
3446
3447 {
3448 0,
3449 convert_X_to_Mono,
3450 convert_X_to_Mono,
3451 convert_ARGB_to_Indexed8,
3452 mask_alpha_converter,
3453 0,
3454 convert_ARGB_to_ARGB_PM,
3455 CONVERT_PTR(quint16, quint32),
3456 CONVERT_PTR(qargb8565, quint32),
3457 CONVERT_PTR(qrgb666, quint32),
3458 CONVERT_PTR(qargb6666, quint32),
3459 CONVERT_PTR(qrgb555, quint32),
3460 CONVERT_PTR(qargb8555, quint32),
3461 CONVERT_PTR(qrgb888, quint32),
3462 CONVERT_PTR(qrgb444, quint32),
3463 CONVERT_PTR(qargb4444, quint32)
3464 }, // Format_ARGB32
3465
3466 {
3467 0,
3468 convert_ARGB_PM_to_Mono,
3469 convert_ARGB_PM_to_Mono,
3470 convert_ARGB_PM_to_Indexed8,
3471 convert_ARGB_PM_to_RGB,
3472 convert_ARGB_PM_to_ARGB,
3473 0,
3474 0,
3475 0,
3476 0,
3477 0,
3478 0,
3479 0,
3480 0,
3481 0,
3482 0
3483 }, // Format_ARGB32_Premultiplied
3484
3485 {
3486 0,
3487 0,
3488 0,
3489 0,
3490 CONVERT_PTR(quint32, quint16),
3491 CONVERT_PTR(quint32, quint16),
3492 CONVERT_PTR(quint32, quint16),
3493 0,
3494 0,
3495 0,
3496 0,
3497#if !defined(Q_WS_QWS) || (defined(QT_QWS_DEPTH_15) && defined(QT_QWS_DEPTH_16))
3498 CONVERT_PTR(qrgb555, quint16),
3499#else
3500 0,
3501#endif
3502 0,
3503 0,
3504 0,
3505 0
3506 }, // Format_RGB16
3507
3508 {
3509 0,
3510 0,
3511 0,
3512 0,
3513 CONVERT_PTR(quint32, qargb8565),
3514 CONVERT_PTR(quint32, qargb8565),
3515 CONVERT_PTR(quint32, qargb8565),
3516 0,
3517 0,
3518 0,
3519 0,
3520 0,
3521 0,
3522 0,
3523 0,
3524 0
3525 }, // Format_ARGB8565_Premultiplied
3526
3527 {
3528 0,
3529 0,
3530 0,
3531 0,
3532 CONVERT_PTR(quint32, qrgb666),
3533 CONVERT_PTR(quint32, qrgb666),
3534 CONVERT_PTR(quint32, qrgb666),
3535 0,
3536 0,
3537 0,
3538 0,
3539 0,
3540 0,
3541 0,
3542 0,
3543 0
3544 }, // Format_RGB666
3545
3546 {
3547 0,
3548 0,
3549 0,
3550 0,
3551 CONVERT_PTR(quint32, qargb6666),
3552 CONVERT_PTR(quint32, qargb6666),
3553 CONVERT_PTR(quint32, qargb6666),
3554 0,
3555 0,
3556 0,
3557 0,
3558 0,
3559 0,
3560 0,
3561 0,
3562 0
3563 }, // Format_ARGB6666_Premultiplied
3564
3565 {
3566 0,
3567 0,
3568 0,
3569 0,
3570 CONVERT_PTR(quint32, qrgb555),
3571 CONVERT_PTR(quint32, qrgb555),
3572 CONVERT_PTR(quint32, qrgb555),
3573#if !defined(Q_WS_QWS) || (defined(QT_QWS_DEPTH_15) && defined(QT_QWS_DEPTH_16))
3574 CONVERT_PTR(quint16, qrgb555),
3575#else
3576 0,
3577#endif
3578 0,
3579 0,
3580 0,
3581 0,
3582 0,
3583 0,
3584 0,
3585 0
3586 }, // Format_RGB555
3587
3588 {
3589 0,
3590 0,
3591 0,
3592 0,
3593 CONVERT_PTR(quint32, qargb8555),
3594 CONVERT_PTR(quint32, qargb8555),
3595 CONVERT_PTR(quint32, qargb8555),
3596 0,
3597 0,
3598 0,
3599 0,
3600 0,
3601 0,
3602 0,
3603 0,
3604 0
3605 }, // Format_ARGB8555_Premultiplied
3606
3607 {
3608 0,
3609 0,
3610 0,
3611 0,
3612 CONVERT_PTR(quint32, qrgb888),
3613 CONVERT_PTR(quint32, qrgb888),
3614 CONVERT_PTR(quint32, qrgb888),
3615 0,
3616 0,
3617 0,
3618 0,
3619 0,
3620 0,
3621 0,
3622 0,
3623 0
3624 }, // Format_RGB888
3625
3626 {
3627 0,
3628 0,
3629 0,
3630 0,
3631 CONVERT_PTR(quint32, qrgb444),
3632 CONVERT_PTR(quint32, qrgb444),
3633 CONVERT_PTR(quint32, qrgb444),
3634 0,
3635 0,
3636 0,
3637 0,
3638 0,
3639 0,
3640 0,
3641 0,
3642 0
3643 }, // Format_RGB444
3644
3645 {
3646 0,
3647 0,
3648 0,
3649 0,
3650 CONVERT_PTR(quint32, qargb4444),
3651 CONVERT_PTR(quint32, qargb4444),
3652 CONVERT_PTR(quint32, qargb4444),
3653 0,
3654 0,
3655 0,
3656 0,
3657 0,
3658 0,
3659 0,
3660 0,
3661 0
3662 } // Format_ARGB4444_Premultiplied
3663};
3664
3665static InPlace_Image_Converter inplace_converter_map[QImage::NImageFormats][QImage::NImageFormats] =
3666{
3667 {
3668 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3669 },
3670 {
3671 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3672 }, // Format_Mono
3673 {
3674 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3675 }, // Format_MonoLSB
3676 {
3677 0,
3678 0,
3679 0,
3680 0,
3681 0,
3682 convert_indexed8_to_RGB_inplace,
3683 convert_indexed8_to_ARGB_PM_inplace,
3684 convert_indexed8_to_RGB16_inplace,
3685 0,
3686 0,
3687 0,
3688 0,
3689 0,
3690 0,
3691 0,
3692 0,
3693 }, // Format_Indexed8
3694 {
3695 0,
3696 0,
3697 0,
3698 0,
3699 0,
3700 0,
3701 0,
3702 convert_RGB_to_RGB16_inplace,
3703 0,
3704 0,
3705 0,
3706 0,
3707 0,
3708 0,
3709 0,
3710 0,
3711 }, // Format_ARGB32
3712 {
3713 0,
3714 0,
3715 0,
3716 0,
3717 0,
3718 0,
3719 convert_ARGB_to_ARGB_PM_inplace,
3720 0,
3721 0,
3722 0,
3723 0,
3724 0,
3725 0,
3726 0,
3727 0,
3728 0,
3729 }, // Format_ARGB32
3730 {
3731 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3732 }, // Format_ARGB32_Premultiplied
3733 {
3734 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3735 }, // Format_RGB16
3736 {
3737 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3738 }, // Format_ARGB8565_Premultiplied
3739 {
3740 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3741 }, // Format_RGB666
3742 {
3743 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3744 }, // Format_ARGB6666_Premultiplied
3745 {
3746 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3747 }, // Format_RGB555
3748 {
3749 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3750 }, // Format_ARGB8555_Premultiplied
3751 {
3752 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3753 }, // Format_RGB888
3754 {
3755 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3756 }, // Format_RGB444
3757 {
3758 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
3759 } // Format_ARGB4444_Premultiplied
3760};
3761
3762void qInitImageConversions()
3763{
3764 const uint features = qDetectCPUFeatures();
3765 Q_UNUSED(features);
3766
3767#ifdef QT_HAVE_SSE2
3768 if (features & SSE2) {
3769 extern bool convert_ARGB_to_ARGB_PM_inplace_sse2(QImageData *data, Qt::ImageConversionFlags);
3770 inplace_converter_map[QImage::Format_ARGB32][QImage::Format_ARGB32_Premultiplied] = convert_ARGB_to_ARGB_PM_inplace_sse2;
3771 }
3772#endif
3773#ifdef QT_HAVE_SSSE3
3774 if (features & SSSE3) {
3775 extern void convert_RGB888_to_RGB32_ssse3(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags);
3776 converter_map[QImage::Format_RGB888][QImage::Format_RGB32] = convert_RGB888_to_RGB32_ssse3;
3777 converter_map[QImage::Format_RGB888][QImage::Format_ARGB32] = convert_RGB888_to_RGB32_ssse3;
3778 converter_map[QImage::Format_RGB888][QImage::Format_ARGB32_Premultiplied] = convert_RGB888_to_RGB32_ssse3;
3779 }
3780#endif
3781#ifdef QT_HAVE_NEON
3782 if (features & NEON) {
3783 extern void convert_RGB888_to_RGB32_neon(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags);
3784 converter_map[QImage::Format_RGB888][QImage::Format_RGB32] = convert_RGB888_to_RGB32_neon;
3785 converter_map[QImage::Format_RGB888][QImage::Format_ARGB32] = convert_RGB888_to_RGB32_neon;
3786 converter_map[QImage::Format_RGB888][QImage::Format_ARGB32_Premultiplied] = convert_RGB888_to_RGB32_neon;
3787 }
3788#endif
3789}
3790
3791/*!
3792 Returns a copy of the image in the given \a format.
3793
3794 The specified image conversion \a flags control how the image data
3795 is handled during the conversion process.
3796
3797 \sa {QImage#Image Format}{Image Format}
3798*/
3799QImage QImage::convertToFormat(Format format, Qt::ImageConversionFlags flags) const
3800{
3801 if (!d || d->format == format)
3802 return *this;
3803
3804 if (format == Format_Invalid || d->format == Format_Invalid)
3805 return QImage();
3806
3807 const Image_Converter *converterPtr = &converter_map[d->format][format];
3808 Image_Converter converter = *converterPtr;
3809 if (converter) {
3810 QImage image(d->width, d->height, format);
3811
3812 QIMAGE_SANITYCHECK_MEMORY(image);
3813
3814 image.setDotsPerMeterY(dotsPerMeterY());
3815 image.setDotsPerMeterX(dotsPerMeterX());
3816
3817#if !defined(QT_NO_IMAGE_TEXT)
3818 image.d->text = d->text;
3819#endif // !QT_NO_IMAGE_TEXT
3820
3821 converter(image.d, d, flags);
3822 return image;
3823 }
3824
3825 Q_ASSERT(format != QImage::Format_ARGB32);
3826 Q_ASSERT(d->format != QImage::Format_ARGB32);
3827
3828 QImage image = convertToFormat(Format_ARGB32, flags);
3829 return image.convertToFormat(format, flags);
3830}
3831
3832
3833
3834static inline int pixel_distance(QRgb p1, QRgb p2) {
3835 int r1 = qRed(p1);
3836 int g1 = qGreen(p1);
3837 int b1 = qBlue(p1);
3838 int a1 = qAlpha(p1);
3839
3840 int r2 = qRed(p2);
3841 int g2 = qGreen(p2);
3842 int b2 = qBlue(p2);
3843 int a2 = qAlpha(p2);
3844
3845 return abs(r1 - r2) + abs(g1 - g2) + abs(b1 - b2) + abs(a1 - a2);
3846}
3847
3848static inline int closestMatch(QRgb pixel, const QVector<QRgb> &clut) {
3849 int idx = 0;
3850 int current_distance = INT_MAX;
3851 for (int i=0; i<clut.size(); ++i) {
3852 int dist = pixel_distance(pixel, clut.at(i));
3853 if (dist < current_distance) {
3854 current_distance = dist;
3855 idx = i;
3856 }
3857 }
3858 return idx;
3859}
3860
3861static QImage convertWithPalette(const QImage &src, QImage::Format format,
3862 const QVector<QRgb> &clut) {
3863 QImage dest(src.size(), format);
3864 dest.setColorTable(clut);
3865
3866#if !defined(QT_NO_IMAGE_TEXT)
3867 QString textsKeys = src.text();
3868 QStringList textKeyList = textsKeys.split(QLatin1Char('\n'), QString::SkipEmptyParts);
3869 foreach (const QString &textKey, textKeyList) {
3870 QStringList textKeySplitted = textKey.split(QLatin1String(": "));
3871 dest.setText(textKeySplitted[0], textKeySplitted[1]);
3872 }
3873#endif // !QT_NO_IMAGE_TEXT
3874
3875 int h = src.height();
3876 int w = src.width();
3877
3878 QHash<QRgb, int> cache;
3879
3880 if (format == QImage::Format_Indexed8) {
3881 for (int y=0; y<h; ++y) {
3882 QRgb *src_pixels = (QRgb *) src.scanLine(y);
3883 uchar *dest_pixels = (uchar *) dest.scanLine(y);
3884 for (int x=0; x<w; ++x) {
3885 int src_pixel = src_pixels[x];
3886 int value = cache.value(src_pixel, -1);
3887 if (value == -1) {
3888 value = closestMatch(src_pixel, clut);
3889 cache.insert(src_pixel, value);
3890 }
3891 dest_pixels[x] = (uchar) value;
3892 }
3893 }
3894 } else {
3895 QVector<QRgb> table = clut;
3896 table.resize(2);
3897 for (int y=0; y<h; ++y) {
3898 QRgb *src_pixels = (QRgb *) src.scanLine(y);
3899 for (int x=0; x<w; ++x) {
3900 int src_pixel = src_pixels[x];
3901 int value = cache.value(src_pixel, -1);
3902 if (value == -1) {
3903 value = closestMatch(src_pixel, table);
3904 cache.insert(src_pixel, value);
3905 }
3906 dest.setPixel(x, y, value);
3907 }
3908 }
3909 }
3910
3911 return dest;
3912}
3913
3914/*!
3915 \overload
3916
3917 Returns a copy of the image converted to the given \a format,
3918 using the specified \a colorTable.
3919
3920 Conversion from 32 bit to 8 bit indexed is a slow operation and
3921 will use a straightforward nearest color approach, with no
3922 dithering.
3923*/
3924QImage QImage::convertToFormat(Format format, const QVector<QRgb> &colorTable, Qt::ImageConversionFlags flags) const
3925{
3926 if (d->format == format)
3927 return *this;
3928
3929 if (format <= QImage::Format_Indexed8 && depth() == 32) {
3930 return convertWithPalette(*this, format, colorTable);
3931 }
3932
3933 const Image_Converter *converterPtr = &converter_map[d->format][format];
3934 Image_Converter converter = *converterPtr;
3935 if (!converter)
3936 return QImage();
3937
3938 QImage image(d->width, d->height, format);
3939 QIMAGE_SANITYCHECK_MEMORY(image);
3940
3941#if !defined(QT_NO_IMAGE_TEXT)
3942 image.d->text = d->text;
3943#endif // !QT_NO_IMAGE_TEXT
3944
3945 converter(image.d, d, flags);
3946 return image;
3947}
3948
3949#ifdef QT3_SUPPORT
3950/*!
3951 Converts the depth (bpp) of the image to the given \a depth and
3952 returns the converted image. The original image is not changed.
3953 Returns this image if \a depth is equal to the image depth, or a
3954 null image if this image cannot be converted. The \a depth
3955 argument must be 1, 8 or 32. If the image needs to be modified to
3956 fit in a lower-resolution result (e.g. converting from 32-bit to
3957 8-bit), use the \a flags to specify how you'd prefer this to
3958 happen.
3959
3960 Use the convertToFormat() function instead.
3961*/
3962
3963QImage QImage::convertDepth(int depth, Qt::ImageConversionFlags flags) const
3964{
3965 if (!d || d->depth == depth)
3966 return *this;
3967
3968 Format format = formatFor (depth, QImage::LittleEndian);
3969 return convertToFormat(format, flags);
3970}
3971#endif
3972
3973/*!
3974 \fn bool QImage::valid(const QPoint &pos) const
3975
3976 Returns true if \a pos is a valid coordinate pair within the
3977 image; otherwise returns false.
3978
3979 \sa rect(), QRect::contains()
3980*/
3981
3982/*!
3983 \overload
3984
3985 Returns true if QPoint(\a x, \a y) is a valid coordinate pair
3986 within the image; otherwise returns false.
3987*/
3988bool QImage::valid(int x, int y) const
3989{
3990 return d
3991 && x >= 0 && x < d->width
3992 && y >= 0 && y < d->height;
3993}
3994
3995/*!
3996 \fn int QImage::pixelIndex(const QPoint &position) const
3997
3998 Returns the pixel index at the given \a position.
3999
4000 If \a position is not valid, or if the image is not a paletted
4001 image (depth() > 8), the results are undefined.
4002
4003 \sa valid(), depth(), {QImage#Pixel Manipulation}{Pixel Manipulation}
4004*/
4005
4006/*!
4007 \overload
4008
4009 Returns the pixel index at (\a x, \a y).
4010*/
4011int QImage::pixelIndex(int x, int y) const
4012{
4013 if (!d || x < 0 || x >= d->width || y < 0 || y >= height()) {
4014 qWarning("QImage::pixelIndex: coordinate (%d,%d) out of range", x, y);
4015 return -12345;
4016 }
4017 const uchar * s = scanLine(y);
4018 switch(d->format) {
4019 case Format_Mono:
4020 return (*(s + (x >> 3)) >> (7- (x & 7))) & 1;
4021 case Format_MonoLSB:
4022 return (*(s + (x >> 3)) >> (x & 7)) & 1;
4023 case Format_Indexed8:
4024 return (int)s[x];
4025 default:
4026 qWarning("QImage::pixelIndex: Not applicable for %d-bpp images (no palette)", d->depth);
4027 }
4028 return 0;
4029}
4030
4031
4032/*!
4033 \fn QRgb QImage::pixel(const QPoint &position) const
4034
4035 Returns the color of the pixel at the given \a position.
4036
4037 If the \a position is not valid, the results are undefined.
4038
4039 \warning This function is expensive when used for massive pixel
4040 manipulations.
4041
4042 \sa setPixel(), valid(), {QImage#Pixel Manipulation}{Pixel
4043 Manipulation}
4044*/
4045
4046/*!
4047 \overload
4048
4049 Returns the color of the pixel at coordinates (\a x, \a y).
4050*/
4051QRgb QImage::pixel(int x, int y) const
4052{
4053 if (!d || x < 0 || x >= d->width || y < 0 || y >= height()) {
4054 qWarning("QImage::pixel: coordinate (%d,%d) out of range", x, y);
4055 return 12345;
4056 }
4057 const uchar * s = scanLine(y);
4058 switch(d->format) {
4059 case Format_Mono:
4060 return d->colortable.at((*(s + (x >> 3)) >> (7- (x & 7))) & 1);
4061 case Format_MonoLSB:
4062 return d->colortable.at((*(s + (x >> 3)) >> (x & 7)) & 1);
4063 case Format_Indexed8:
4064 return d->colortable.at((int)s[x]);
4065 case Format_ARGB8565_Premultiplied:
4066 return qt_colorConvert<quint32, qargb8565>(reinterpret_cast<const qargb8565*>(s)[x], 0);
4067 case Format_RGB666:
4068 return qt_colorConvert<quint32, qrgb666>(reinterpret_cast<const qrgb666*>(s)[x], 0);
4069 case Format_ARGB6666_Premultiplied:
4070 return qt_colorConvert<quint32, qargb6666>(reinterpret_cast<const qargb6666*>(s)[x], 0);
4071 case Format_RGB555:
4072 return qt_colorConvert<quint32, qrgb555>(reinterpret_cast<const qrgb555*>(s)[x], 0);
4073 case Format_ARGB8555_Premultiplied:
4074 return qt_colorConvert<quint32, qargb8555>(reinterpret_cast<const qargb8555*>(s)[x], 0);
4075 case Format_RGB888:
4076 return qt_colorConvert<quint32, qrgb888>(reinterpret_cast<const qrgb888*>(s)[x], 0);
4077 case Format_RGB444:
4078 return qt_colorConvert<quint32, qrgb444>(reinterpret_cast<const qrgb444*>(s)[x], 0);
4079 case Format_ARGB4444_Premultiplied:
4080 return qt_colorConvert<quint32, qargb4444>(reinterpret_cast<const qargb4444*>(s)[x], 0);
4081 case Format_RGB16:
4082 return qt_colorConvert<quint32, quint16>(reinterpret_cast<const quint16*>(s)[x], 0);
4083 default:
4084 return ((QRgb*)s)[x];
4085 }
4086}
4087
4088
4089/*!
4090 \fn void QImage::setPixel(const QPoint &position, uint index_or_rgb)
4091
4092 Sets the pixel index or color at the given \a position to \a
4093 index_or_rgb.
4094
4095 If the image's format is either monochrome or 8-bit, the given \a
4096 index_or_rgb value must be an index in the image's color table,
4097 otherwise the parameter must be a QRgb value.
4098
4099 If \a position is not a valid coordinate pair in the image, or if
4100 \a index_or_rgb >= colorCount() in the case of monochrome and
4101 8-bit images, the result is undefined.
4102
4103 \warning This function is expensive due to the call of the internal
4104 \c{detach()} function called within; if performance is a concern, we
4105 recommend the use of \l{QImage::}{scanLine()} to access pixel data
4106 directly.
4107
4108 \sa pixel(), {QImage#Pixel Manipulation}{Pixel Manipulation}
4109*/
4110
4111/*!
4112 \overload
4113
4114 Sets the pixel index or color at (\a x, \a y) to \a index_or_rgb.
4115*/
4116void QImage::setPixel(int x, int y, uint index_or_rgb)
4117{
4118 if (!d || x < 0 || x >= width() || y < 0 || y >= height()) {
4119 qWarning("QImage::setPixel: coordinate (%d,%d) out of range", x, y);
4120 return;
4121 }
4122 // detach is called from within scanLine
4123 uchar * s = scanLine(y);
4124 const quint32p p = quint32p::fromRawData(index_or_rgb);
4125 switch(d->format) {
4126 case Format_Mono:
4127 case Format_MonoLSB:
4128 if (index_or_rgb > 1) {
4129 qWarning("QImage::setPixel: Index %d out of range", index_or_rgb);
4130 } else if (format() == Format_MonoLSB) {
4131 if (index_or_rgb==0)
4132 *(s + (x >> 3)) &= ~(1 << (x & 7));
4133 else
4134 *(s + (x >> 3)) |= (1 << (x & 7));
4135 } else {
4136 if (index_or_rgb==0)
4137 *(s + (x >> 3)) &= ~(1 << (7-(x & 7)));
4138 else
4139 *(s + (x >> 3)) |= (1 << (7-(x & 7)));
4140 }
4141 break;
4142 case Format_Indexed8:
4143 if (index_or_rgb >= (uint)d->colortable.size()) {
4144 qWarning("QImage::setPixel: Index %d out of range", index_or_rgb);
4145 return;
4146 }
4147 s[x] = index_or_rgb;
4148 break;
4149 case Format_RGB32:
4150 //make sure alpha is 255, we depend on it in qdrawhelper for cases
4151 // when image is set as a texture pattern on a qbrush
4152 ((uint *)s)[x] = uint(255 << 24) | index_or_rgb;
4153 break;
4154 case Format_ARGB32:
4155 case Format_ARGB32_Premultiplied:
4156 ((uint *)s)[x] = index_or_rgb;
4157 break;
4158 case Format_RGB16:
4159 ((quint16 *)s)[x] = qt_colorConvert<quint16, quint32p>(p, 0);
4160 break;
4161 case Format_ARGB8565_Premultiplied:
4162 ((qargb8565*)s)[x] = qt_colorConvert<qargb8565, quint32p>(p, 0);
4163 break;
4164 case Format_RGB666:
4165 ((qrgb666*)s)[x] = qt_colorConvert<qrgb666, quint32p>(p, 0);
4166 break;
4167 case Format_ARGB6666_Premultiplied:
4168 ((qargb6666*)s)[x] = qt_colorConvert<qargb6666, quint32p>(p, 0);
4169 break;
4170 case Format_RGB555:
4171 ((qrgb555*)s)[x] = qt_colorConvert<qrgb555, quint32p>(p, 0);
4172 break;
4173 case Format_ARGB8555_Premultiplied:
4174 ((qargb8555*)s)[x] = qt_colorConvert<qargb8555, quint32p>(p, 0);
4175 break;
4176 case Format_RGB888:
4177 ((qrgb888*)s)[x] = qt_colorConvert<qrgb888, quint32p>(p, 0);
4178 break;
4179 case Format_RGB444:
4180 ((qrgb444*)s)[x] = qt_colorConvert<qrgb444, quint32p>(p, 0);
4181 break;
4182 case Format_ARGB4444_Premultiplied:
4183 ((qargb4444*)s)[x] = qt_colorConvert<qargb4444, quint32p>(p, 0);
4184 break;
4185 case Format_Invalid:
4186 case NImageFormats:
4187 Q_ASSERT(false);
4188 }
4189}
4190
4191#ifdef QT3_SUPPORT
4192/*!
4193 Converts the bit order of the image to the given \a bitOrder and
4194 returns the converted image. The original image is not changed.
4195 Returns this image if the given \a bitOrder is equal to the image
4196 current bit order, or a null image if this image cannot be
4197 converted.
4198
4199 Use convertToFormat() instead.
4200*/
4201
4202QImage QImage::convertBitOrder(Endian bitOrder) const
4203{
4204 if (!d || isNull() || d->depth != 1 || !(bitOrder == BigEndian || bitOrder == LittleEndian))
4205 return QImage();
4206
4207 if ((d->format == Format_Mono && bitOrder == BigEndian)
4208 || (d->format == Format_MonoLSB && bitOrder == LittleEndian))
4209 return *this;
4210
4211 QImage image(d->width, d->height, d->format == Format_Mono ? Format_MonoLSB : Format_Mono);
4212
4213 const uchar *data = d->data;
4214 const uchar *end = data + d->nbytes;
4215 uchar *ndata = image.d->data;
4216 while (data < end)
4217 *ndata++ = bitflip[*data++];
4218
4219 image.setDotsPerMeterX(dotsPerMeterX());
4220 image.setDotsPerMeterY(dotsPerMeterY());
4221
4222 image.d->colortable = d->colortable;
4223 return image;
4224}
4225#endif
4226/*!
4227 Returns true if all the colors in the image are shades of gray
4228 (i.e. their red, green and blue components are equal); otherwise
4229 false.
4230
4231 Note that this function is slow for images without color table.
4232
4233 \sa isGrayscale()
4234*/
4235bool QImage::allGray() const
4236{
4237 if (!d)
4238 return true;
4239
4240 if (d->depth == 32) {
4241 int p = width()*height();
4242 const QRgb* b = (const QRgb*)bits();
4243 while (p--)
4244 if (!qIsGray(*b++))
4245 return false;
4246 } else if (d->depth == 16) {
4247 int p = width()*height();
4248 const ushort* b = (const ushort *)bits();
4249 while (p--)
4250 if (!qIsGray(qt_colorConvert<quint32, quint16>(*b++, 0)))
4251 return false;
4252 } else if (d->format == QImage::Format_RGB888) {
4253 int p = width()*height();
4254 const qrgb888* b = (const qrgb888 *)bits();
4255 while (p--)
4256 if (!qIsGray(qt_colorConvert<quint32, qrgb888>(*b++, 0)))
4257 return false;
4258 } else {
4259 if (d->colortable.isEmpty())
4260 return true;
4261 for (int i = 0; i < colorCount(); i++)
4262 if (!qIsGray(d->colortable.at(i)))
4263 return false;
4264 }
4265 return true;
4266}
4267
4268/*!
4269 For 32-bit images, this function is equivalent to allGray().
4270
4271 For 8-bpp images, this function returns true if color(i) is
4272 QRgb(i, i, i) for all indexes of the color table; otherwise
4273 returns false.
4274
4275 \sa allGray(), {QImage#Image Formats}{Image Formats}
4276*/
4277bool QImage::isGrayscale() const
4278{
4279 if (!d)
4280 return false;
4281
4282 switch (depth()) {
4283 case 32:
4284 case 24:
4285 case 16:
4286 return allGray();
4287 case 8: {
4288 for (int i = 0; i < colorCount(); i++)
4289 if (d->colortable.at(i) != qRgb(i,i,i))
4290 return false;
4291 return true;
4292 }
4293 }
4294 return false;
4295}
4296
4297
4298/*!
4299 \fn QImage QImage::smoothScale(int width, int height, Qt::AspectRatioMode mode) const
4300
4301 Use scaled() instead.
4302
4303 \oldcode
4304 QImage image;
4305 image.smoothScale(width, height, mode);
4306 \newcode
4307 QImage image;
4308 image.scaled(width, height, mode, Qt::SmoothTransformation);
4309 \endcode
4310*/
4311
4312/*!
4313 \fn QImage QImage::smoothScale(const QSize &size, Qt::AspectRatioMode mode) const
4314 \overload
4315
4316 Use scaled() instead.
4317
4318 \oldcode
4319 QImage image;
4320 image.smoothScale(size, mode);
4321 \newcode
4322 QImage image;
4323 image.scaled(size, mode, Qt::SmoothTransformation);
4324 \endcode
4325*/
4326
4327/*!
4328 \fn QImage QImage::scaled(int width, int height, Qt::AspectRatioMode aspectRatioMode,
4329 Qt::TransformationMode transformMode) const
4330 \overload
4331
4332 Returns a copy of the image scaled to a rectangle with the given
4333 \a width and \a height according to the given \a aspectRatioMode
4334 and \a transformMode.
4335
4336 If either the \a width or the \a height is zero or negative, this
4337 function returns a null image.
4338*/
4339
4340/*!
4341 \fn QImage QImage::scaled(const QSize &size, Qt::AspectRatioMode aspectRatioMode,
4342 Qt::TransformationMode transformMode) const
4343
4344 Returns a copy of the image scaled to a rectangle defined by the
4345 given \a size according to the given \a aspectRatioMode and \a
4346 transformMode.
4347
4348 \image qimage-scaling.png
4349
4350 \list
4351 \i If \a aspectRatioMode is Qt::IgnoreAspectRatio, the image
4352 is scaled to \a size.
4353 \i If \a aspectRatioMode is Qt::KeepAspectRatio, the image is
4354 scaled to a rectangle as large as possible inside \a size, preserving the aspect ratio.
4355 \i If \a aspectRatioMode is Qt::KeepAspectRatioByExpanding,
4356 the image is scaled to a rectangle as small as possible
4357 outside \a size, preserving the aspect ratio.
4358 \endlist
4359
4360 If the given \a size is empty, this function returns a null image.
4361
4362 \sa isNull(), {QImage#Image Transformations}{Image
4363 Transformations}
4364*/
4365QImage QImage::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const
4366{
4367 if (!d) {
4368 qWarning("QImage::scaled: Image is a null image");
4369 return QImage();
4370 }
4371 if (s.isEmpty())
4372 return QImage();
4373
4374 QSize newSize = size();
4375 newSize.scale(s, aspectMode);
4376 if (newSize == size())
4377 return *this;
4378
4379 QTransform wm = QTransform::fromScale((qreal)newSize.width() / width(), (qreal)newSize.height() / height());
4380 QImage img = transformed(wm, mode);
4381 return img;
4382}
4383
4384/*!
4385 \fn QImage QImage::scaledToWidth(int width, Qt::TransformationMode mode) const
4386
4387 Returns a scaled copy of the image. The returned image is scaled
4388 to the given \a width using the specified transformation \a
4389 mode.
4390
4391 This function automatically calculates the height of the image so
4392 that its aspect ratio is preserved.
4393
4394 If the given \a width is 0 or negative, a null image is returned.
4395
4396 \sa {QImage#Image Transformations}{Image Transformations}
4397*/
4398QImage QImage::scaledToWidth(int w, Qt::TransformationMode mode) const
4399{
4400 if (!d) {
4401 qWarning("QImage::scaleWidth: Image is a null image");
4402 return QImage();
4403 }
4404 if (w <= 0)
4405 return QImage();
4406
4407 qreal factor = (qreal) w / width();
4408 QTransform wm = QTransform::fromScale(factor, factor);
4409 return transformed(wm, mode);
4410}
4411
4412/*!
4413 \fn QImage QImage::scaledToHeight(int height, Qt::TransformationMode mode) const
4414
4415 Returns a scaled copy of the image. The returned image is scaled
4416 to the given \a height using the specified transformation \a
4417 mode.
4418
4419 This function automatically calculates the width of the image so that
4420 the ratio of the image is preserved.
4421
4422 If the given \a height is 0 or negative, a null image is returned.
4423
4424 \sa {QImage#Image Transformations}{Image Transformations}
4425*/
4426QImage QImage::scaledToHeight(int h, Qt::TransformationMode mode) const
4427{
4428 if (!d) {
4429 qWarning("QImage::scaleHeight: Image is a null image");
4430 return QImage();
4431 }
4432 if (h <= 0)
4433 return QImage();
4434
4435 qreal factor = (qreal) h / height();
4436 QTransform wm = QTransform::fromScale(factor, factor);
4437 return transformed(wm, mode);
4438}
4439
4440
4441/*!
4442 \fn QMatrix QImage::trueMatrix(const QMatrix &matrix, int width, int height)
4443
4444 Returns the actual matrix used for transforming an image with the
4445 given \a width, \a height and \a matrix.
4446
4447 When transforming an image using the transformed() function, the
4448 transformation matrix is internally adjusted to compensate for
4449 unwanted translation, i.e. transformed() returns the smallest
4450 image containing all transformed points of the original image.
4451 This function returns the modified matrix, which maps points
4452 correctly from the original image into the new image.
4453
4454 \sa transformed(), {QImage#Image Transformations}{Image
4455 Transformations}
4456*/
4457QMatrix QImage::trueMatrix(const QMatrix &matrix, int w, int h)
4458{
4459 return trueMatrix(QTransform(matrix), w, h).toAffine();
4460}
4461
4462/*!
4463 Returns a copy of the image that is transformed using the given
4464 transformation \a matrix and transformation \a mode.
4465
4466 The transformation \a matrix is internally adjusted to compensate
4467 for unwanted translation; i.e. the image produced is the smallest
4468 image that contains all the transformed points of the original
4469 image. Use the trueMatrix() function to retrieve the actual matrix
4470 used for transforming an image.
4471
4472 \sa trueMatrix(), {QImage#Image Transformations}{Image
4473 Transformations}
4474*/
4475QImage QImage::transformed(const QMatrix &matrix, Qt::TransformationMode mode) const
4476{
4477 return transformed(QTransform(matrix), mode);
4478}
4479
4480/*!
4481 Builds and returns a 1-bpp mask from the alpha buffer in this
4482 image. Returns a null image if the image's format is
4483 QImage::Format_RGB32.
4484
4485 The \a flags argument is a bitwise-OR of the
4486 Qt::ImageConversionFlags, and controls the conversion
4487 process. Passing 0 for flags sets all the default options.
4488
4489 The returned image has little-endian bit order (i.e. the image's
4490 format is QImage::Format_MonoLSB), which you can convert to
4491 big-endian (QImage::Format_Mono) using the convertToFormat()
4492 function.
4493
4494 \sa createHeuristicMask(), {QImage#Image Transformations}{Image
4495 Transformations}
4496*/
4497QImage QImage::createAlphaMask(Qt::ImageConversionFlags flags) const
4498{
4499 if (!d || d->format == QImage::Format_RGB32)
4500 return QImage();
4501
4502 if (d->depth == 1) {
4503 // A monochrome pixmap, with alpha channels on those two colors.
4504 // Pretty unlikely, so use less efficient solution.
4505 return convertToFormat(Format_Indexed8, flags).createAlphaMask(flags);
4506 }
4507
4508 QImage mask(d->width, d->height, Format_MonoLSB);
4509 if (!mask.isNull())
4510 dither_to_Mono(mask.d, d, flags, true);
4511 return mask;
4512}
4513
4514#ifndef QT_NO_IMAGE_HEURISTIC_MASK
4515/*!
4516 Creates and returns a 1-bpp heuristic mask for this image.
4517
4518 The function works by selecting a color from one of the corners,
4519 then chipping away pixels of that color starting at all the edges.
4520 The four corners vote for which color is to be masked away. In
4521 case of a draw (this generally means that this function is not
4522 applicable to the image), the result is arbitrary.
4523
4524 The returned image has little-endian bit order (i.e. the image's
4525 format is QImage::Format_MonoLSB), which you can convert to
4526 big-endian (QImage::Format_Mono) using the convertToFormat()
4527 function.
4528
4529 If \a clipTight is true (the default) the mask is just large
4530 enough to cover the pixels; otherwise, the mask is larger than the
4531 data pixels.
4532
4533 Note that this function disregards the alpha buffer.
4534
4535 \sa createAlphaMask(), {QImage#Image Transformations}{Image
4536 Transformations}
4537*/
4538
4539QImage QImage::createHeuristicMask(bool clipTight) const
4540{
4541 if (!d)
4542 return QImage();
4543
4544 if (d->depth != 32) {
4545 QImage img32 = convertToFormat(Format_RGB32);
4546 return img32.createHeuristicMask(clipTight);
4547 }
4548
4549#define PIX(x,y) (*((QRgb*)scanLine(y)+x) & 0x00ffffff)
4550
4551 int w = width();
4552 int h = height();
4553 QImage m(w, h, Format_MonoLSB);
4554 QIMAGE_SANITYCHECK_MEMORY(m);
4555 m.setColorCount(2);
4556 m.setColor(0, QColor(Qt::color0).rgba());
4557 m.setColor(1, QColor(Qt::color1).rgba());
4558 m.fill(0xff);
4559
4560 QRgb background = PIX(0,0);
4561 if (background != PIX(w-1,0) &&
4562 background != PIX(0,h-1) &&
4563 background != PIX(w-1,h-1)) {
4564 background = PIX(w-1,0);
4565 if (background != PIX(w-1,h-1) &&
4566 background != PIX(0,h-1) &&
4567 PIX(0,h-1) == PIX(w-1,h-1)) {
4568 background = PIX(w-1,h-1);
4569 }
4570 }
4571
4572 int x,y;
4573 bool done = false;
4574 uchar *ypp, *ypc, *ypn;
4575 while(!done) {
4576 done = true;
4577 ypn = m.scanLine(0);
4578 ypc = 0;
4579 for (y = 0; y < h; y++) {
4580 ypp = ypc;
4581 ypc = ypn;
4582 ypn = (y == h-1) ? 0 : m.scanLine(y+1);
4583 QRgb *p = (QRgb *)scanLine(y);
4584 for (x = 0; x < w; x++) {
4585 // slowness here - it's possible to do six of these tests
4586 // together in one go. oh well.
4587 if ((x == 0 || y == 0 || x == w-1 || y == h-1 ||
4588 !(*(ypc + ((x-1) >> 3)) & (1 << ((x-1) & 7))) ||
4589 !(*(ypc + ((x+1) >> 3)) & (1 << ((x+1) & 7))) ||
4590 !(*(ypp + (x >> 3)) & (1 << (x & 7))) ||
4591 !(*(ypn + (x >> 3)) & (1 << (x & 7)))) &&
4592 ( (*(ypc + (x >> 3)) & (1 << (x & 7)))) &&
4593 ((*p & 0x00ffffff) == background)) {
4594 done = false;
4595 *(ypc + (x >> 3)) &= ~(1 << (x & 7));
4596 }
4597 p++;
4598 }
4599 }
4600 }
4601
4602 if (!clipTight) {
4603 ypn = m.scanLine(0);
4604 ypc = 0;
4605 for (y = 0; y < h; y++) {
4606 ypp = ypc;
4607 ypc = ypn;
4608 ypn = (y == h-1) ? 0 : m.scanLine(y+1);
4609 QRgb *p = (QRgb *)scanLine(y);
4610 for (x = 0; x < w; x++) {
4611 if ((*p & 0x00ffffff) != background) {
4612 if (x > 0)
4613 *(ypc + ((x-1) >> 3)) |= (1 << ((x-1) & 7));
4614 if (x < w-1)
4615 *(ypc + ((x+1) >> 3)) |= (1 << ((x+1) & 7));
4616 if (y > 0)
4617 *(ypp + (x >> 3)) |= (1 << (x & 7));
4618 if (y < h-1)
4619 *(ypn + (x >> 3)) |= (1 << (x & 7));
4620 }
4621 p++;
4622 }
4623 }
4624 }
4625
4626#undef PIX
4627
4628 return m;
4629}
4630#endif //QT_NO_IMAGE_HEURISTIC_MASK
4631
4632/*!
4633 Creates and returns a mask for this image based on the given \a
4634 color value. If the \a mode is MaskInColor (the default value),
4635 all pixels matching \a color will be opaque pixels in the mask. If
4636 \a mode is MaskOutColor, all pixels matching the given color will
4637 be transparent.
4638
4639 \sa createAlphaMask(), createHeuristicMask()
4640*/
4641
4642QImage QImage::createMaskFromColor(QRgb color, Qt::MaskMode mode) const
4643{
4644 if (!d)
4645 return QImage();
4646 QImage maskImage(size(), QImage::Format_MonoLSB);
4647 QIMAGE_SANITYCHECK_MEMORY(maskImage);
4648 maskImage.fill(0);
4649 uchar *s = maskImage.bits();
4650
4651 if (depth() == 32) {
4652 for (int h = 0; h < d->height; h++) {
4653 const uint *sl = (uint *) scanLine(h);
4654 for (int w = 0; w < d->width; w++) {
4655 if (sl[w] == color)
4656 *(s + (w >> 3)) |= (1 << (w & 7));
4657 }
4658 s += maskImage.bytesPerLine();
4659 }
4660 } else {
4661 for (int h = 0; h < d->height; h++) {
4662 for (int w = 0; w < d->width; w++) {
4663 if ((uint) pixel(w, h) == color)
4664 *(s + (w >> 3)) |= (1 << (w & 7));
4665 }
4666 s += maskImage.bytesPerLine();
4667 }
4668 }
4669 if (mode == Qt::MaskOutColor)
4670 maskImage.invertPixels();
4671 return maskImage;
4672}
4673
4674
4675/*
4676 This code is contributed by Philipp Lang,
4677 GeneriCom Software Germany (www.generi.com)
4678 under the terms of the QPL, Version 1.0
4679*/
4680
4681/*!
4682 \fn QImage QImage::mirror(bool horizontal, bool vertical) const
4683
4684 Use mirrored() instead.
4685*/
4686
4687/*!
4688 Returns a mirror of the image, mirrored in the horizontal and/or
4689 the vertical direction depending on whether \a horizontal and \a
4690 vertical are set to true or false.
4691
4692 Note that the original image is not changed.
4693
4694 \sa {QImage#Image Transformations}{Image Transformations}
4695*/
4696QImage QImage::mirrored(bool horizontal, bool vertical) const
4697{
4698 if (!d)
4699 return QImage();
4700
4701 if ((d->width <= 1 && d->height <= 1) || (!horizontal && !vertical))
4702 return *this;
4703
4704 int w = d->width;
4705 int h = d->height;
4706 // Create result image, copy colormap
4707 QImage result(d->width, d->height, d->format);
4708 QIMAGE_SANITYCHECK_MEMORY(result);
4709
4710 // check if we ran out of of memory..
4711 if (!result.d)
4712 return QImage();
4713
4714 result.d->colortable = d->colortable;
4715 result.d->has_alpha_clut = d->has_alpha_clut;
4716
4717 if (depth() == 1)
4718 w = (w+7)/8;
4719 int dxi = horizontal ? -1 : 1;
4720 int dxs = horizontal ? w-1 : 0;
4721 int dyi = vertical ? -1 : 1;
4722 int dy = vertical ? h-1: 0;
4723
4724 // 1 bit, 8 bit
4725 if (d->depth == 1 || d->depth == 8) {
4726 for (int sy = 0; sy < h; sy++, dy += dyi) {
4727 quint8* ssl = (quint8*)(d->data + sy*d->bytes_per_line);
4728 quint8* dsl = (quint8*)(result.d->data + dy*result.d->bytes_per_line);
4729 int dx = dxs;
4730 for (int sx = 0; sx < w; sx++, dx += dxi)
4731 dsl[dx] = ssl[sx];
4732 }
4733 }
4734 // 16 bit
4735 else if (d->depth == 16) {
4736 for (int sy = 0; sy < h; sy++, dy += dyi) {
4737 quint16* ssl = (quint16*)(d->data + sy*d->bytes_per_line);
4738 quint16* dsl = (quint16*)(result.d->data + dy*result.d->bytes_per_line);
4739 int dx = dxs;
4740 for (int sx = 0; sx < w; sx++, dx += dxi)
4741 dsl[dx] = ssl[sx];
4742 }
4743 }
4744 // 24 bit
4745 else if (d->depth == 24) {
4746 for (int sy = 0; sy < h; sy++, dy += dyi) {
4747 quint24* ssl = (quint24*)(d->data + sy*d->bytes_per_line);
4748 quint24* dsl = (quint24*)(result.d->data + dy*result.d->bytes_per_line);
4749 int dx = dxs;
4750 for (int sx = 0; sx < w; sx++, dx += dxi)
4751 dsl[dx] = ssl[sx];
4752 }
4753 }
4754 // 32 bit
4755 else if (d->depth == 32) {
4756 for (int sy = 0; sy < h; sy++, dy += dyi) {
4757 quint32* ssl = (quint32*)(d->data + sy*d->bytes_per_line);
4758 quint32* dsl = (quint32*)(result.d->data + dy*result.d->bytes_per_line);
4759 int dx = dxs;
4760 for (int sx = 0; sx < w; sx++, dx += dxi)
4761 dsl[dx] = ssl[sx];
4762 }
4763 }
4764
4765 // special handling of 1 bit images for horizontal mirroring
4766 if (horizontal && d->depth == 1) {
4767 int shift = width() % 8;
4768 for (int y = h-1; y >= 0; y--) {
4769 quint8* a0 = (quint8*)(result.d->data + y*d->bytes_per_line);
4770 // Swap bytes
4771 quint8* a = a0+dxs;
4772 while (a >= a0) {
4773 *a = bitflip[*a];
4774 a--;
4775 }
4776 // Shift bits if unaligned
4777 if (shift != 0) {
4778 a = a0+dxs;
4779 quint8 c = 0;
4780 if (format() == Format_MonoLSB) {
4781 while (a >= a0) {
4782 quint8 nc = *a << shift;
4783 *a = (*a >> (8-shift)) | c;
4784 --a;
4785 c = nc;
4786 }
4787 } else {
4788 while (a >= a0) {
4789 quint8 nc = *a >> shift;
4790 *a = (*a << (8-shift)) | c;
4791 --a;
4792 c = nc;
4793 }
4794 }
4795 }
4796 }
4797 }
4798
4799 return result;
4800}
4801
4802/*!
4803 \fn QImage QImage::swapRGB() const
4804
4805 Use rgbSwapped() instead.
4806
4807 \omit
4808 Returns a QImage in which the values of the red and blue
4809 components of all pixels have been swapped, effectively converting
4810 an RGB image to an BGR image. The original QImage is not changed.
4811 \endomit
4812*/
4813
4814/*!
4815 Returns a QImage in which the values of the red and blue
4816 components of all pixels have been swapped, effectively converting
4817 an RGB image to an BGR image.
4818
4819 The original QImage is not changed.
4820
4821 \sa {QImage#Image Transformations}{Image Transformations}
4822*/
4823QImage QImage::rgbSwapped() const
4824{
4825 if (isNull())
4826 return *this;
4827 QImage res;
4828 switch (d->format) {
4829 case Format_Invalid:
4830 case NImageFormats:
4831 Q_ASSERT(false);
4832 break;
4833 case Format_Mono:
4834 case Format_MonoLSB:
4835 case Format_Indexed8:
4836 res = copy();
4837 for (int i = 0; i < res.d->colortable.size(); i++) {
4838 QRgb c = res.d->colortable.at(i);
4839 res.d->colortable[i] = QRgb(((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00));
4840 }
4841 break;
4842 case Format_RGB32:
4843 case Format_ARGB32:
4844 case Format_ARGB32_Premultiplied:
4845 res = QImage(d->width, d->height, d->format);
4846 QIMAGE_SANITYCHECK_MEMORY(res);
4847 for (int i = 0; i < d->height; i++) {
4848 uint *q = (uint*)res.scanLine(i);
4849 uint *p = (uint*)constScanLine(i);
4850 uint *end = p + d->width;
4851 while (p < end) {
4852 *q = ((*p << 16) & 0xff0000) | ((*p >> 16) & 0xff) | (*p & 0xff00ff00);
4853 p++;
4854 q++;
4855 }
4856 }
4857 break;
4858 case Format_RGB16:
4859 res = QImage(d->width, d->height, d->format);
4860 QIMAGE_SANITYCHECK_MEMORY(res);
4861 for (int i = 0; i < d->height; i++) {
4862 ushort *q = (ushort*)res.scanLine(i);
4863 const ushort *p = (const ushort*)constScanLine(i);
4864 const ushort *end = p + d->width;
4865 while (p < end) {
4866 *q = ((*p << 11) & 0xf800) | ((*p >> 11) & 0x1f) | (*p & 0x07e0);
4867 p++;
4868 q++;
4869 }
4870 }
4871 break;
4872 case Format_ARGB8565_Premultiplied:
4873 res = QImage(d->width, d->height, d->format);
4874 QIMAGE_SANITYCHECK_MEMORY(res);
4875 for (int i = 0; i < d->height; i++) {
4876 const quint8 *p = constScanLine(i);
4877 quint8 *q = res.scanLine(i);
4878 const quint8 *end = p + d->width * sizeof(qargb8565);
4879 while (p < end) {
4880 q[0] = p[0];
4881 q[1] = (p[1] & 0xe0) | (p[2] >> 3);
4882 q[2] = (p[2] & 0x07) | (p[1] << 3);
4883 p += sizeof(qargb8565);
4884 q += sizeof(qargb8565);
4885 }
4886 }
4887 break;
4888 case Format_RGB666:
4889 res = QImage(d->width, d->height, d->format);
4890 QIMAGE_SANITYCHECK_MEMORY(res);
4891 for (int i = 0; i < d->height; i++) {
4892 qrgb666 *q = reinterpret_cast<qrgb666*>(res.scanLine(i));
4893 const qrgb666 *p = reinterpret_cast<const qrgb666*>(constScanLine(i));
4894 const qrgb666 *end = p + d->width;
4895 while (p < end) {
4896 const QRgb rgb = quint32(*p++);
4897 *q++ = qRgb(qBlue(rgb), qGreen(rgb), qRed(rgb));
4898 }
4899 }
4900 break;
4901 case Format_ARGB6666_Premultiplied:
4902 res = QImage(d->width, d->height, d->format);
4903 QIMAGE_SANITYCHECK_MEMORY(res);
4904 for (int i = 0; i < d->height; i++) {
4905 const quint8 *p = constScanLine(i);
4906 const quint8 *end = p + d->width * sizeof(qargb6666);
4907 quint8 *q = res.scanLine(i);
4908 while (p < end) {
4909 q[0] = (p[1] >> 4) | ((p[2] & 0x3) << 4) | (p[0] & 0xc0);
4910 q[1] = (p[1] & 0xf) | (p[0] << 4);
4911 q[2] = (p[2] & 0xfc) | ((p[0] >> 4) & 0x3);
4912 p += sizeof(qargb6666);
4913 q += sizeof(qargb6666);
4914 }
4915 }
4916 break;
4917 case Format_RGB555:
4918 res = QImage(d->width, d->height, d->format);
4919 QIMAGE_SANITYCHECK_MEMORY(res);
4920 for (int i = 0; i < d->height; i++) {
4921 quint16 *q = (quint16*)res.scanLine(i);
4922 const quint16 *p = (const quint16*)constScanLine(i);
4923 const quint16 *end = p + d->width;
4924 while (p < end) {
4925 *q = ((*p << 10) & 0x7c00) | ((*p >> 10) & 0x1f) | (*p & 0x3e0);
4926 p++;
4927 q++;
4928 }
4929 }
4930 break;
4931 case Format_ARGB8555_Premultiplied:
4932 res = QImage(d->width, d->height, d->format);
4933 QIMAGE_SANITYCHECK_MEMORY(res);
4934 for (int i = 0; i < d->height; i++) {
4935 const quint8 *p = constScanLine(i);
4936 quint8 *q = res.scanLine(i);
4937 const quint8 *end = p + d->width * sizeof(qargb8555);
4938 while (p < end) {
4939 q[0] = p[0];
4940 q[1] = (p[1] & 0xe0) | (p[2] >> 2);
4941 q[2] = (p[2] & 0x03) | ((p[1] << 2) & 0x7f);
4942 p += sizeof(qargb8555);
4943 q += sizeof(qargb8555);
4944 }
4945 }
4946 break;
4947 case Format_RGB888:
4948 res = QImage(d->width, d->height, d->format);
4949 QIMAGE_SANITYCHECK_MEMORY(res);
4950 for (int i = 0; i < d->height; i++) {
4951 quint8 *q = res.scanLine(i);
4952 const quint8 *p = constScanLine(i);
4953 const quint8 *end = p + d->width * sizeof(qrgb888);
4954 while (p < end) {
4955 q[0] = p[2];
4956 q[1] = p[1];
4957 q[2] = p[0];
4958 q += sizeof(qrgb888);
4959 p += sizeof(qrgb888);
4960 }
4961 }
4962 break;
4963 case Format_RGB444:
4964 case Format_ARGB4444_Premultiplied:
4965 res = QImage(d->width, d->height, d->format);
4966 QIMAGE_SANITYCHECK_MEMORY(res);
4967 for (int i = 0; i < d->height; i++) {
4968 quint16 *q = reinterpret_cast<quint16*>(res.scanLine(i));
4969 const quint16 *p = reinterpret_cast<const quint16*>(constScanLine(i));
4970 const quint16 *end = p + d->width;
4971 while (p < end) {
4972 *q = (*p & 0xf0f0) | ((*p & 0x0f) << 8) | ((*p & 0xf00) >> 8);
4973 p++;
4974 q++;
4975 }
4976 }
4977 break;
4978 }
4979 return res;
4980}
4981
4982/*!
4983 Loads an image from the file with the given \a fileName. Returns true if
4984 the image was successfully loaded; otherwise returns false.
4985
4986 The loader attempts to read the image using the specified \a format, e.g.,
4987 PNG or JPG. If \a format is not specified (which is the default), the
4988 loader probes the file for a header to guess the file format.
4989
4990 The file name can either refer to an actual file on disk or to one
4991 of the application's embedded resources. See the
4992 \l{resources.html}{Resource System} overview for details on how to
4993 embed images and other resource files in the application's
4994 executable.
4995
4996 \sa {QImage#Reading and Writing Image Files}{Reading and Writing Image Files}
4997*/
4998
4999bool QImage::load(const QString &fileName, const char* format)
5000{
5001 if (fileName.isEmpty())
5002 return false;
5003
5004 QImage image = QImageReader(fileName, format).read();
5005 if (!image.isNull()) {
5006 operator=(image);
5007 return true;
5008 }
5009 return false;
5010}
5011
5012/*!
5013 \overload
5014
5015 This function reads a QImage from the given \a device. This can,
5016 for example, be used to load an image directly into a QByteArray.
5017*/
5018
5019bool QImage::load(QIODevice* device, const char* format)
5020{
5021 QImage image = QImageReader(device, format).read();
5022 if(!image.isNull()) {
5023 operator=(image);
5024 return true;
5025 }
5026 return false;
5027}
5028
5029/*!
5030 \fn bool QImage::loadFromData(const uchar *data, int len, const char *format)
5031
5032 Loads an image from the first \a len bytes of the given binary \a
5033 data. Returns true if the image was successfully loaded; otherwise
5034 returns false.
5035
5036 The loader attempts to read the image using the specified \a format, e.g.,
5037 PNG or JPG. If \a format is not specified (which is the default), the
5038 loader probes the file for a header to guess the file format.
5039
5040 \sa {QImage#Reading and Writing Image Files}{Reading and Writing Image Files}
5041*/
5042
5043bool QImage::loadFromData(const uchar *data, int len, const char *format)
5044{
5045 QImage image = fromData(data, len, format);
5046 if (!image.isNull()) {
5047 operator=(image);
5048 return true;
5049 }
5050 return false;
5051}
5052
5053/*!
5054 \fn bool QImage::loadFromData(const QByteArray &data, const char *format)
5055
5056 \overload
5057
5058 Loads an image from the given QByteArray \a data.
5059*/
5060
5061/*!
5062 \fn QImage QImage::fromData(const uchar *data, int size, const char *format)
5063
5064 Constructs a QImage from the first \a size bytes of the given
5065 binary \a data. The loader attempts to read the image using the
5066 specified \a format. If \a format is not specified (which is the default),
5067 the loader probes the file for a header to guess the file format.
5068 binary \a data. The loader attempts to read the image, either using the
5069 optional image \a format specified or by determining the image format from
5070 the data.
5071
5072 If \a format is not specified (which is the default), the loader probes the
5073 file for a header to determine the file format. If \a format is specified,
5074 it must be one of the values returned by QImageReader::supportedImageFormats().
5075
5076 If the loading of the image fails, the image returned will be a null image.
5077
5078 \sa load(), save(), {QImage#Reading and Writing Image Files}{Reading and Writing Image Files}
5079 */
5080
5081QImage QImage::fromData(const uchar *data, int size, const char *format)
5082{
5083 QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(data), size);
5084 QBuffer b;
5085 b.setData(a);
5086 b.open(QIODevice::ReadOnly);
5087 return QImageReader(&b, format).read();
5088}
5089
5090/*!
5091 \fn QImage QImage::fromData(const QByteArray &data, const char *format)
5092
5093 \overload
5094
5095 Loads an image from the given QByteArray \a data.
5096*/
5097
5098/*!
5099 Saves the image to the file with the given \a fileName, using the
5100 given image file \a format and \a quality factor. If \a format is
5101 0, QImage will attempt to guess the format by looking at \a fileName's
5102 suffix.
5103
5104 The \a quality factor must be in the range 0 to 100 or -1. Specify
5105 0 to obtain small compressed files, 100 for large uncompressed
5106 files, and -1 (the default) to use the default settings.
5107
5108 Returns true if the image was successfully saved; otherwise
5109 returns false.
5110
5111 \sa {QImage#Reading and Writing Image Files}{Reading and Writing
5112 Image Files}
5113*/
5114bool QImage::save(const QString &fileName, const char *format, int quality) const
5115{
5116 if (isNull())
5117 return false;
5118 QImageWriter writer(fileName, format);
5119 return d->doImageIO(this, &writer, quality);
5120}
5121
5122/*!
5123 \overload
5124
5125 This function writes a QImage to the given \a device.
5126
5127 This can, for example, be used to save an image directly into a
5128 QByteArray:
5129
5130 \snippet doc/src/snippets/image/image.cpp 0
5131*/
5132
5133bool QImage::save(QIODevice* device, const char* format, int quality) const
5134{
5135 if (isNull())
5136 return false; // nothing to save
5137 QImageWriter writer(device, format);
5138 return d->doImageIO(this, &writer, quality);
5139}
5140
5141/* \internal
5142*/
5143
5144bool QImageData::doImageIO(const QImage *image, QImageWriter *writer, int quality) const
5145{
5146 if (quality > 100 || quality < -1)
5147 qWarning("QPixmap::save: Quality out of range [-1, 100]");
5148 if (quality >= 0)
5149 writer->setQuality(qMin(quality,100));
5150 return writer->write(*image);
5151}
5152
5153/*****************************************************************************
5154 QImage stream functions
5155 *****************************************************************************/
5156#if !defined(QT_NO_DATASTREAM)
5157/*!
5158 \fn QDataStream &operator<<(QDataStream &stream, const QImage &image)
5159 \relates QImage
5160
5161 Writes the given \a image to the given \a stream as a PNG image,
5162 or as a BMP image if the stream's version is 1. Note that writing
5163 the stream to a file will not produce a valid image file.
5164
5165 \sa QImage::save(), {Serializing Qt Data Types}
5166*/
5167
5168QDataStream &operator<<(QDataStream &s, const QImage &image)
5169{
5170 if (s.version() >= 5) {
5171 if (image.isNull()) {
5172 s << (qint32) 0; // null image marker
5173 return s;
5174 } else {
5175 s << (qint32) 1;
5176 // continue ...
5177 }
5178 }
5179 QImageWriter writer(s.device(), s.version() == 1 ? "bmp" : "png");
5180 writer.write(image);
5181 return s;
5182}
5183
5184/*!
5185 \fn QDataStream &operator>>(QDataStream &stream, QImage &image)
5186 \relates QImage
5187
5188 Reads an image from the given \a stream and stores it in the given
5189 \a image.
5190
5191 \sa QImage::load(), {Serializing Qt Data Types}
5192*/
5193
5194QDataStream &operator>>(QDataStream &s, QImage &image)
5195{
5196 if (s.version() >= 5) {
5197 qint32 nullMarker;
5198 s >> nullMarker;
5199 if (!nullMarker) {
5200 image = QImage(); // null image
5201 return s;
5202 }
5203 }
5204 image = QImageReader(s.device(), 0).read();
5205 return s;
5206}
5207#endif // QT_NO_DATASTREAM
5208
5209
5210#ifdef QT3_SUPPORT
5211/*!
5212 \fn QImage QImage::convertDepthWithPalette(int depth, QRgb* palette, int palette_count, Qt::ImageConversionFlags flags) const
5213
5214 Returns an image with the given \a depth, using the \a
5215 palette_count colors pointed to by \a palette. If \a depth is 1 or
5216 8, the returned image will have its color table ordered in the
5217 same way as \a palette.
5218
5219 If the image needs to be modified to fit in a lower-resolution
5220 result (e.g. converting from 32-bit to 8-bit), use the \a flags to
5221 specify how you'd prefer this to happen.
5222
5223 Note: currently no closest-color search is made. If colors are
5224 found that are not in the palette, the palette may not be used at
5225 all. This result should not be considered valid because it may
5226 change in future implementations.
5227
5228 Currently inefficient for non-32-bit images.
5229
5230 Use the convertToFormat() function in combination with the
5231 setColorTable() function instead.
5232*/
5233QImage QImage::convertDepthWithPalette(int d, QRgb* palette, int palette_count, Qt::ImageConversionFlags flags) const
5234{
5235 Format f = formatFor(d, QImage::LittleEndian);
5236 QVector<QRgb> colortable;
5237 for (int i = 0; i < palette_count; ++i)
5238 colortable.append(palette[i]);
5239 return convertToFormat(f, colortable, flags);
5240}
5241
5242/*!
5243 \relates QImage
5244
5245 Copies a block of pixels from \a src to \a dst. The pixels
5246 copied from source (src) are converted according to
5247 \a flags if it is incompatible with the destination
5248 (\a dst).
5249
5250 \a sx, \a sy is the top-left pixel in \a src, \a dx, \a dy is the
5251 top-left position in \a dst and \a sw, \a sh is the size of the
5252 copied block. The copying is clipped if areas outside \a src or \a
5253 dst are specified. If \a sw is -1, it is adjusted to
5254 src->width(). Similarly, if \a sh is -1, it is adjusted to
5255 src->height().
5256
5257 Currently inefficient for non 32-bit images.
5258
5259 Use copy() or QPainter::drawImage() instead.
5260*/
5261void bitBlt(QImage *dst, int dx, int dy, const QImage *src, int sx, int sy, int sw, int sh,
5262 Qt::ImageConversionFlags flags)
5263{
5264 if (dst->isNull() || src->isNull())
5265 return;
5266 QPainter p(dst);
5267 p.drawImage(QPoint(dx, dy), *src, QRect(sx, sy, sw, sh), flags);
5268}
5269#endif
5270
5271/*!
5272 \fn bool QImage::operator==(const QImage & image) const
5273
5274 Returns true if this image and the given \a image have the same
5275 contents; otherwise returns false.
5276
5277 The comparison can be slow, unless there is some obvious
5278 difference (e.g. different size or format), in which case the
5279 function will return quickly.
5280
5281 \sa operator=()
5282*/
5283
5284bool QImage::operator==(const QImage & i) const
5285{
5286 // same object, or shared?
5287 if (i.d == d)
5288 return true;
5289 if (!i.d || !d)
5290 return false;
5291
5292 // obviously different stuff?
5293 if (i.d->height != d->height || i.d->width != d->width || i.d->format != d->format)
5294 return false;
5295
5296 if (d->format != Format_RGB32) {
5297 if (d->format >= Format_ARGB32) { // all bits defined
5298 const int n = d->width * d->depth / 8;
5299 if (n == d->bytes_per_line && n == i.d->bytes_per_line) {
5300 if (memcmp(bits(), i.bits(), d->nbytes))
5301 return false;
5302 } else {
5303 for (int y = 0; y < d->height; ++y) {
5304 if (memcmp(scanLine(y), i.scanLine(y), n))
5305 return false;
5306 }
5307 }
5308 } else {
5309 const int w = width();
5310 const int h = height();
5311 const QVector<QRgb> &colortable = d->colortable;
5312 const QVector<QRgb> &icolortable = i.d->colortable;
5313 for (int y=0; y<h; ++y) {
5314 for (int x=0; x<w; ++x) {
5315 if (colortable[pixelIndex(x, y)] != icolortable[i.pixelIndex(x, y)])
5316 return false;
5317 }
5318 }
5319 }
5320 } else {
5321 //alpha channel undefined, so we must mask it out
5322 for(int l = 0; l < d->height; l++) {
5323 int w = d->width;
5324 const uint *p1 = reinterpret_cast<const uint*>(scanLine(l));
5325 const uint *p2 = reinterpret_cast<const uint*>(i.scanLine(l));
5326 while (w--) {
5327 if ((*p1++ & 0x00ffffff) != (*p2++ & 0x00ffffff))
5328 return false;
5329 }
5330 }
5331 }
5332 return true;
5333}
5334
5335
5336/*!
5337 \fn bool QImage::operator!=(const QImage & image) const
5338
5339 Returns true if this image and the given \a image have different
5340 contents; otherwise returns false.
5341
5342 The comparison can be slow, unless there is some obvious
5343 difference, such as different widths, in which case the function
5344 will return quickly.
5345
5346 \sa operator=()
5347*/
5348
5349bool QImage::operator!=(const QImage & i) const
5350{
5351 return !(*this == i);
5352}
5353
5354
5355
5356
5357/*!
5358 Returns the number of pixels that fit horizontally in a physical
5359 meter. Together with dotsPerMeterY(), this number defines the
5360 intended scale and aspect ratio of the image.
5361
5362 \sa setDotsPerMeterX(), {QImage#Image Information}{Image
5363 Information}
5364*/
5365int QImage::dotsPerMeterX() const
5366{
5367 return d ? qRound(d->dpmx) : 0;
5368}
5369
5370/*!
5371 Returns the number of pixels that fit vertically in a physical
5372 meter. Together with dotsPerMeterX(), this number defines the
5373 intended scale and aspect ratio of the image.
5374
5375 \sa setDotsPerMeterY(), {QImage#Image Information}{Image
5376 Information}
5377*/
5378int QImage::dotsPerMeterY() const
5379{
5380 return d ? qRound(d->dpmy) : 0;
5381}
5382
5383/*!
5384 Sets the number of pixels that fit horizontally in a physical
5385 meter, to \a x.
5386
5387 Together with dotsPerMeterY(), this number defines the intended
5388 scale and aspect ratio of the image, and determines the scale
5389 at which QPainter will draw graphics on the image. It does not
5390 change the scale or aspect ratio of the image when it is rendered
5391 on other paint devices.
5392
5393 \sa dotsPerMeterX(), {QImage#Image Information}{Image Information}
5394*/
5395void QImage::setDotsPerMeterX(int x)
5396{
5397 if (!d || !x)
5398 return;
5399 detach();
5400
5401 if (d)
5402 d->dpmx = x;
5403}
5404
5405/*!
5406 Sets the number of pixels that fit vertically in a physical meter,
5407 to \a y.
5408
5409 Together with dotsPerMeterX(), this number defines the intended
5410 scale and aspect ratio of the image, and determines the scale
5411 at which QPainter will draw graphics on the image. It does not
5412 change the scale or aspect ratio of the image when it is rendered
5413 on other paint devices.
5414
5415 \sa dotsPerMeterY(), {QImage#Image Information}{Image Information}
5416*/
5417void QImage::setDotsPerMeterY(int y)
5418{
5419 if (!d || !y)
5420 return;
5421 detach();
5422
5423 if (d)
5424 d->dpmy = y;
5425}
5426
5427/*!
5428 \fn QPoint QImage::offset() const
5429
5430 Returns the number of pixels by which the image is intended to be
5431 offset by when positioning relative to other images.
5432
5433 \sa setOffset(), {QImage#Image Information}{Image Information}
5434*/
5435QPoint QImage::offset() const
5436{
5437 return d ? d->offset : QPoint();
5438}
5439
5440
5441/*!
5442 \fn void QImage::setOffset(const QPoint& offset)
5443
5444 Sets the number of pixels by which the image is intended to be
5445 offset by when positioning relative to other images, to \a offset.
5446
5447 \sa offset(), {QImage#Image Information}{Image Information}
5448*/
5449void QImage::setOffset(const QPoint& p)
5450{
5451 if (!d)
5452 return;
5453 detach();
5454
5455 if (d)
5456 d->offset = p;
5457}
5458#ifndef QT_NO_IMAGE_TEXT
5459
5460/*!
5461 Returns the text keys for this image.
5462
5463 You can use these keys with text() to list the image text for a
5464 certain key.
5465
5466 \sa text()
5467*/
5468QStringList QImage::textKeys() const
5469{
5470 return d ? QStringList(d->text.keys()) : QStringList();
5471}
5472
5473/*!
5474 Returns the image text associated with the given \a key. If the
5475 specified \a key is an empty string, the whole image text is
5476 returned, with each key-text pair separated by a newline.
5477
5478 \sa setText(), textKeys()
5479*/
5480QString QImage::text(const QString &key) const
5481{
5482 if (!d)
5483 return QString();
5484
5485 if (!key.isEmpty())
5486 return d->text.value(key);
5487
5488 QString tmp;
5489 foreach (const QString &key, d->text.keys()) {
5490 if (!tmp.isEmpty())
5491 tmp += QLatin1String("\n\n");
5492 tmp += key + QLatin1String(": ") + d->text.value(key).simplified();
5493 }
5494 return tmp;
5495}
5496
5497/*!
5498 \fn void QImage::setText(const QString &key, const QString &text)
5499
5500 Sets the image text to the given \a text and associate it with the
5501 given \a key.
5502
5503 If you just want to store a single text block (i.e., a "comment"
5504 or just a description), you can either pass an empty key, or use a
5505 generic key like "Description".
5506
5507 The image text is embedded into the image data when you
5508 call save() or QImageWriter::write().
5509
5510 Not all image formats support embedded text. You can find out
5511 if a specific image or format supports embedding text
5512 by using QImageWriter::supportsOption(). We give an example:
5513
5514 \snippet doc/src/snippets/image/supportedformat.cpp 0
5515
5516 You can use QImageWriter::supportedImageFormats() to find out
5517 which image formats are available to you.
5518
5519 \sa text(), textKeys()
5520*/
5521void QImage::setText(const QString &key, const QString &value)
5522{
5523 if (!d)
5524 return;
5525 detach();
5526
5527 if (d)
5528 d->text.insert(key, value);
5529}
5530
5531/*!
5532 \fn QString QImage::text(const char* key, const char* language) const
5533 \obsolete
5534
5535 Returns the text recorded for the given \a key in the given \a
5536 language, or in a default language if \a language is 0.
5537
5538 Use text() instead.
5539
5540 The language the text is recorded in is no longer relevant since
5541 the text is always set using QString and UTF-8 representation.
5542*/
5543QString QImage::text(const char* key, const char* lang) const
5544{
5545 if (!d)
5546 return QString();
5547 QString k = QString::fromAscii(key);
5548 if (lang && *lang)
5549 k += QLatin1Char('/') + QString::fromAscii(lang);
5550 return d->text.value(k);
5551}
5552
5553/*!
5554 \fn QString QImage::text(const QImageTextKeyLang& keywordAndLanguage) const
5555 \overload
5556 \obsolete
5557
5558 Returns the text recorded for the given \a keywordAndLanguage.
5559
5560 Use text() instead.
5561
5562 The language the text is recorded in is no longer relevant since
5563 the text is always set using QString and UTF-8 representation.
5564*/
5565QString QImage::text(const QImageTextKeyLang& kl) const
5566{
5567 if (!d)
5568 return QString();
5569 QString k = QString::fromAscii(kl.key);
5570 if (!kl.lang.isEmpty())
5571 k += QLatin1Char('/') + QString::fromAscii(kl.lang);
5572 return d->text.value(k);
5573}
5574
5575/*!
5576 \obsolete
5577
5578 Returns the language identifiers for which some texts are
5579 recorded. Note that if you want to iterate over the list, you
5580 should iterate over a copy.
5581
5582 The language the text is recorded in is no longer relevant since
5583 the text is always set using QString and UTF-8 representation.
5584*/
5585QStringList QImage::textLanguages() const
5586{
5587 if (!d)
5588 return QStringList();
5589 QStringList keys = textKeys();
5590 QStringList languages;
5591 for (int i = 0; i < keys.size(); ++i) {
5592 int index = keys.at(i).indexOf(QLatin1Char('/'));
5593 if (index > 0)
5594 languages += keys.at(i).mid(index+1);
5595 }
5596
5597 return languages;
5598}
5599
5600/*!
5601 \obsolete
5602
5603 Returns a list of QImageTextKeyLang objects that enumerate all the
5604 texts key/language pairs set for this image.
5605
5606 Use textKeys() instead.
5607
5608 The language the text is recorded in is no longer relevant since
5609 the text is always set using QString and UTF-8 representation.
5610*/
5611QList<QImageTextKeyLang> QImage::textList() const
5612{
5613 QList<QImageTextKeyLang> imageTextKeys;
5614 if (!d)
5615 return imageTextKeys;
5616 QStringList keys = textKeys();
5617 for (int i = 0; i < keys.size(); ++i) {
5618 int index = keys.at(i).indexOf(QLatin1Char('/'));
5619 if (index > 0) {
5620 QImageTextKeyLang tkl;
5621 tkl.key = keys.at(i).left(index).toAscii();
5622 tkl.lang = keys.at(i).mid(index+1).toAscii();
5623 imageTextKeys += tkl;
5624 }
5625 }
5626
5627 return imageTextKeys;
5628}
5629
5630/*!
5631 \fn void QImage::setText(const char* key, const char* language, const QString& text)
5632 \obsolete
5633
5634 Sets the image text to the given \a text and associate it with the
5635 given \a key. The text is recorded in the specified \a language,
5636 or in a default language if \a language is 0.
5637
5638 Use setText() instead.
5639
5640 The language the text is recorded in is no longer relevant since
5641 the text is always set using QString and UTF-8 representation.
5642
5643 \omit
5644 Records string \a for the keyword \a key. The \a key should be
5645 a portable keyword recognizable by other software - some suggested
5646 values can be found in
5647 \l{http://www.libpng.org/pub/png/spec/1.2/png-1.2-pdg.html#C.Anc-text}
5648 {the PNG specification}. \a s can be any text. \a lang should
5649 specify the language code (see
5650 \l{http://www.rfc-editor.org/rfc/rfc1766.txt}{RFC 1766}) or 0.
5651 \endomit
5652*/
5653void QImage::setText(const char* key, const char* lang, const QString& s)
5654{
5655 if (!d)
5656 return;
5657 detach();
5658
5659 // In case detach() ran out of memory
5660 if (!d)
5661 return;
5662
5663 QString k = QString::fromAscii(key);
5664 if (lang && *lang)
5665 k += QLatin1Char('/') + QString::fromAscii(lang);
5666 d->text.insert(k, s);
5667}
5668
5669#endif // QT_NO_IMAGE_TEXT
5670
5671/*
5672 Sets the image bits to the \a pixmap contents and returns a
5673 reference to the image.
5674
5675 If the image shares data with other images, it will first
5676 dereference the shared data.
5677
5678 Makes a call to QPixmap::convertToImage().
5679*/
5680
5681/*! \fn QImage::Endian QImage::systemBitOrder()
5682
5683 Determines the bit order of the display hardware. Returns
5684 QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first).
5685
5686 This function is no longer relevant for QImage. Use QSysInfo
5687 instead.
5688*/
5689
5690
5691/*!
5692 \internal
5693
5694 Used by QPainter to retrieve a paint engine for the image.
5695*/
5696
5697QPaintEngine *QImage::paintEngine() const
5698{
5699 if (!d)
5700 return 0;
5701
5702 if (!d->paintEngine) {
5703 d->paintEngine = new QRasterPaintEngine(const_cast<QImage *>(this));
5704 }
5705
5706 return d->paintEngine;
5707}
5708
5709
5710/*!
5711 \internal
5712
5713 Returns the size for the specified \a metric on the device.
5714*/
5715int QImage::metric(PaintDeviceMetric metric) const
5716{
5717 if (!d)
5718 return 0;
5719
5720 switch (metric) {
5721 case PdmWidth:
5722 return d->width;
5723 break;
5724
5725 case PdmHeight:
5726 return d->height;
5727 break;
5728
5729 case PdmWidthMM:
5730 return qRound(d->width * 1000 / d->dpmx);
5731 break;
5732
5733 case PdmHeightMM:
5734 return qRound(d->height * 1000 / d->dpmy);
5735 break;
5736
5737 case PdmNumColors:
5738 return d->colortable.size();
5739 break;
5740
5741 case PdmDepth:
5742 return d->depth;
5743 break;
5744
5745 case PdmDpiX:
5746 return qRound(d->dpmx * 0.0254);
5747 break;
5748
5749 case PdmDpiY:
5750 return qRound(d->dpmy * 0.0254);
5751 break;
5752
5753 case PdmPhysicalDpiX:
5754 return qRound(d->dpmx * 0.0254);
5755 break;
5756
5757 case PdmPhysicalDpiY:
5758 return qRound(d->dpmy * 0.0254);
5759 break;
5760
5761 default:
5762 qWarning("QImage::metric(): Unhandled metric type %d", metric);
5763 break;
5764 }
5765 return 0;
5766}
5767
5768
5769
5770/*****************************************************************************
5771 QPixmap (and QImage) helper functions
5772 *****************************************************************************/
5773/*
5774 This internal function contains the common (i.e. platform independent) code
5775 to do a transformation of pixel data. It is used by QPixmap::transform() and by
5776 QImage::transform().
5777
5778 \a trueMat is the true transformation matrix (see QPixmap::trueMatrix()) and
5779 \a xoffset is an offset to the matrix.
5780
5781 \a msbfirst specifies for 1bpp images, if the MSB or LSB comes first and \a
5782 depth specifies the colordepth of the data.
5783
5784 \a dptr is a pointer to the destination data, \a dbpl specifies the bits per
5785 line for the destination data, \a p_inc is the offset that we advance for
5786 every scanline and \a dHeight is the height of the destination image.
5787
5788 \a sprt is the pointer to the source data, \a sbpl specifies the bits per
5789 line of the source data, \a sWidth and \a sHeight are the width and height of
5790 the source data.
5791*/
5792
5793#undef IWX_MSB
5794#define IWX_MSB(b) if (trigx < maxws && trigy < maxhs) { \
5795 if (*(sptr+sbpl*(trigy>>12)+(trigx>>15)) & \
5796 (1 << (7-((trigx>>12)&7)))) \
5797 *dptr |= b; \
5798 } \
5799 trigx += m11; \
5800 trigy += m12;
5801 // END OF MACRO
5802#undef IWX_LSB
5803#define IWX_LSB(b) if (trigx < maxws && trigy < maxhs) { \
5804 if (*(sptr+sbpl*(trigy>>12)+(trigx>>15)) & \
5805 (1 << ((trigx>>12)&7))) \
5806 *dptr |= b; \
5807 } \
5808 trigx += m11; \
5809 trigy += m12;
5810 // END OF MACRO
5811#undef IWX_PIX
5812#define IWX_PIX(b) if (trigx < maxws && trigy < maxhs) { \
5813 if ((*(sptr+sbpl*(trigy>>12)+(trigx>>15)) & \
5814 (1 << (7-((trigx>>12)&7)))) == 0) \
5815 *dptr &= ~b; \
5816 } \
5817 trigx += m11; \
5818 trigy += m12;
5819 // END OF MACRO
5820bool qt_xForm_helper(const QTransform &trueMat, int xoffset, int type, int depth,
5821 uchar *dptr, int dbpl, int p_inc, int dHeight,
5822 const uchar *sptr, int sbpl, int sWidth, int sHeight)
5823{
5824 int m11 = int(trueMat.m11()*4096.0);
5825 int m12 = int(trueMat.m12()*4096.0);
5826 int m21 = int(trueMat.m21()*4096.0);
5827 int m22 = int(trueMat.m22()*4096.0);
5828 int dx = qRound(trueMat.dx()*4096.0);
5829 int dy = qRound(trueMat.dy()*4096.0);
5830
5831 int m21ydx = dx + (xoffset<<16) + (m11 + m21) / 2;
5832 int m22ydy = dy + (m12 + m22) / 2;
5833 uint trigx;
5834 uint trigy;
5835 uint maxws = sWidth<<12;
5836 uint maxhs = sHeight<<12;
5837
5838 for (int y=0; y<dHeight; y++) { // for each target scanline
5839 trigx = m21ydx;
5840 trigy = m22ydy;
5841 uchar *maxp = dptr + dbpl;
5842 if (depth != 1) {
5843 switch (depth) {
5844 case 8: // 8 bpp transform
5845 while (dptr < maxp) {
5846 if (trigx < maxws && trigy < maxhs)
5847 *dptr = *(sptr+sbpl*(trigy>>12)+(trigx>>12));
5848 trigx += m11;
5849 trigy += m12;
5850 dptr++;
5851 }
5852 break;
5853
5854 case 16: // 16 bpp transform
5855 while (dptr < maxp) {
5856 if (trigx < maxws && trigy < maxhs)
5857 *((ushort*)dptr) = *((ushort *)(sptr+sbpl*(trigy>>12) +
5858 ((trigx>>12)<<1)));
5859 trigx += m11;
5860 trigy += m12;
5861 dptr++;
5862 dptr++;
5863 }
5864 break;
5865
5866 case 24: // 24 bpp transform
5867 while (dptr < maxp) {
5868 if (trigx < maxws && trigy < maxhs) {
5869 const uchar *p2 = sptr+sbpl*(trigy>>12) + ((trigx>>12)*3);
5870 dptr[0] = p2[0];
5871 dptr[1] = p2[1];
5872 dptr[2] = p2[2];
5873 }
5874 trigx += m11;
5875 trigy += m12;
5876 dptr += 3;
5877 }
5878 break;
5879
5880 case 32: // 32 bpp transform
5881 while (dptr < maxp) {
5882 if (trigx < maxws && trigy < maxhs)
5883 *((uint*)dptr) = *((uint *)(sptr+sbpl*(trigy>>12) +
5884 ((trigx>>12)<<2)));
5885 trigx += m11;
5886 trigy += m12;
5887 dptr += 4;
5888 }
5889 break;
5890
5891 default: {
5892 return false;
5893 }
5894 }
5895 } else {
5896 switch (type) {
5897 case QT_XFORM_TYPE_MSBFIRST:
5898 while (dptr < maxp) {
5899 IWX_MSB(128);
5900 IWX_MSB(64);
5901 IWX_MSB(32);
5902 IWX_MSB(16);
5903 IWX_MSB(8);
5904 IWX_MSB(4);
5905 IWX_MSB(2);
5906 IWX_MSB(1);
5907 dptr++;
5908 }
5909 break;
5910 case QT_XFORM_TYPE_LSBFIRST:
5911 while (dptr < maxp) {
5912 IWX_LSB(1);
5913 IWX_LSB(2);
5914 IWX_LSB(4);
5915 IWX_LSB(8);
5916 IWX_LSB(16);
5917 IWX_LSB(32);
5918 IWX_LSB(64);
5919 IWX_LSB(128);
5920 dptr++;
5921 }
5922 break;
5923# if defined(Q_WS_WIN)
5924 case QT_XFORM_TYPE_WINDOWSPIXMAP:
5925 while (dptr < maxp) {
5926 IWX_PIX(128);
5927 IWX_PIX(64);
5928 IWX_PIX(32);
5929 IWX_PIX(16);
5930 IWX_PIX(8);
5931 IWX_PIX(4);
5932 IWX_PIX(2);
5933 IWX_PIX(1);
5934 dptr++;
5935 }
5936 break;
5937# endif
5938 }
5939 }
5940 m21ydx += m21;
5941 m22ydy += m22;
5942 dptr += p_inc;
5943 }
5944 return true;
5945}
5946#undef IWX_MSB
5947#undef IWX_LSB
5948#undef IWX_PIX
5949
5950/*!
5951 \fn QImage QImage::xForm(const QMatrix &matrix) const
5952
5953 Use transformed() instead.
5954
5955 \oldcode
5956 QImage image;
5957 ...
5958 image.xForm(matrix);
5959 \newcode
5960 QImage image;
5961 ...
5962 image.transformed(matrix);
5963 \endcode
5964*/
5965
5966/*! \obsolete
5967 Returns a number that identifies the contents of this
5968 QImage object. Distinct QImage objects can only have the same
5969 serial number if they refer to the same contents (but they don't
5970 have to).
5971
5972 Use cacheKey() instead.
5973
5974 \warning The serial number doesn't necessarily change when the
5975 image is altered. This means that it may be dangerous to use
5976 it as a cache key.
5977
5978 \sa operator==()
5979*/
5980
5981int QImage::serialNumber() const
5982{
5983 if (!d)
5984 return 0;
5985 else
5986 return d->ser_no;
5987}
5988
5989/*!
5990 Returns a number that identifies the contents of this QImage
5991 object. Distinct QImage objects can only have the same key if they
5992 refer to the same contents.
5993
5994 The key will change when the image is altered.
5995*/
5996qint64 QImage::cacheKey() const
5997{
5998 if (!d)
5999 return 0;
6000 else
6001 return (((qint64) d->ser_no) << 32) | ((qint64) d->detach_no);
6002}
6003
6004/*!
6005 \internal
6006
6007 Returns true if the image is detached; otherwise returns false.
6008
6009 \sa detach(), {Implicit Data Sharing}
6010*/
6011
6012bool QImage::isDetached() const
6013{
6014 return d && d->ref == 1;
6015}
6016
6017
6018/*!
6019 \obsolete
6020 Sets the alpha channel of this image to the given \a alphaChannel.
6021
6022 If \a alphaChannel is an 8 bit grayscale image, the intensity values are
6023 written into this buffer directly. Otherwise, \a alphaChannel is converted
6024 to 32 bit and the intensity of the RGB pixel values is used.
6025
6026 Note that the image will be converted to the Format_ARGB32_Premultiplied
6027 format if the function succeeds.
6028
6029 Use one of the composition modes in QPainter::CompositionMode instead.
6030
6031 \warning This function is expensive.
6032
6033 \sa alphaChannel(), {QImage#Image Transformations}{Image
6034 Transformations}, {QImage#Image Formats}{Image Formats}
6035*/
6036
6037void QImage::setAlphaChannel(const QImage &alphaChannel)
6038{
6039 if (!d)
6040 return;
6041
6042 int w = d->width;
6043 int h = d->height;
6044
6045 if (w != alphaChannel.d->width || h != alphaChannel.d->height) {
6046 qWarning("QImage::setAlphaChannel: "
6047 "Alpha channel must have same dimensions as the target image");
6048 return;
6049 }
6050
6051 if (d->paintEngine && d->paintEngine->isActive()) {
6052 qWarning("QImage::setAlphaChannel: "
6053 "Unable to set alpha channel while image is being painted on");
6054 return;
6055 }
6056
6057 if (d->format == QImage::Format_ARGB32_Premultiplied)
6058 detach();
6059 else
6060 *this = convertToFormat(QImage::Format_ARGB32_Premultiplied);
6061
6062 if (isNull())
6063 return;
6064
6065 // Slight optimization since alphachannels are returned as 8-bit grays.
6066 if (alphaChannel.d->depth == 8 && alphaChannel.isGrayscale()) {
6067 const uchar *src_data = alphaChannel.d->data;
6068 const uchar *dest_data = d->data;
6069 for (int y=0; y<h; ++y) {
6070 const uchar *src = src_data;
6071 QRgb *dest = (QRgb *)dest_data;
6072 for (int x=0; x<w; ++x) {
6073 int alpha = *src;
6074 int destAlpha = qt_div_255(alpha * qAlpha(*dest));
6075 *dest = ((destAlpha << 24)
6076 | (qt_div_255(qRed(*dest) * alpha) << 16)
6077 | (qt_div_255(qGreen(*dest) * alpha) << 8)
6078 | (qt_div_255(qBlue(*dest) * alpha)));
6079 ++dest;
6080 ++src;
6081 }
6082 src_data += alphaChannel.d->bytes_per_line;
6083 dest_data += d->bytes_per_line;
6084 }
6085
6086 } else {
6087 const QImage sourceImage = alphaChannel.convertToFormat(QImage::Format_RGB32);
6088 const uchar *src_data = sourceImage.d->data;
6089 const uchar *dest_data = d->data;
6090 for (int y=0; y<h; ++y) {
6091 const QRgb *src = (const QRgb *) src_data;
6092 QRgb *dest = (QRgb *) dest_data;
6093 for (int x=0; x<w; ++x) {
6094 int alpha = qGray(*src);
6095 int destAlpha = qt_div_255(alpha * qAlpha(*dest));
6096 *dest = ((destAlpha << 24)
6097 | (qt_div_255(qRed(*dest) * alpha) << 16)
6098 | (qt_div_255(qGreen(*dest) * alpha) << 8)
6099 | (qt_div_255(qBlue(*dest) * alpha)));
6100 ++dest;
6101 ++src;
6102 }
6103 src_data += sourceImage.d->bytes_per_line;
6104 dest_data += d->bytes_per_line;
6105 }
6106 }
6107}
6108
6109
6110/*!
6111 \obsolete
6112
6113 Returns the alpha channel of the image as a new grayscale QImage in which
6114 each pixel's red, green, and blue values are given the alpha value of the
6115 original image. The color depth of the returned image is 8-bit.
6116
6117 You can see an example of use of this function in QPixmap's
6118 \l{QPixmap::}{alphaChannel()}, which works in the same way as
6119 this function on QPixmaps.
6120
6121 Most usecases for this function can be replaced with QPainter and
6122 using composition modes.
6123
6124 \warning This is an expensive function.
6125
6126 \sa setAlphaChannel(), hasAlphaChannel(),
6127 {QPixmap#Pixmap Information}{Pixmap},
6128 {QImage#Image Transformations}{Image Transformations}
6129*/
6130
6131QImage QImage::alphaChannel() const
6132{
6133 if (!d)
6134 return QImage();
6135
6136 int w = d->width;
6137 int h = d->height;
6138
6139 QImage image(w, h, Format_Indexed8);
6140 image.setColorCount(256);
6141
6142 // set up gray scale table.
6143 for (int i=0; i<256; ++i)
6144 image.setColor(i, qRgb(i, i, i));
6145
6146 if (!hasAlphaChannel()) {
6147 image.fill(255);
6148 return image;
6149 }
6150
6151 if (d->format == Format_Indexed8) {
6152 const uchar *src_data = d->data;
6153 uchar *dest_data = image.d->data;
6154 for (int y=0; y<h; ++y) {
6155 const uchar *src = src_data;
6156 uchar *dest = dest_data;
6157 for (int x=0; x<w; ++x) {
6158 *dest = qAlpha(d->colortable.at(*src));
6159 ++dest;
6160 ++src;
6161 }
6162 src_data += d->bytes_per_line;
6163 dest_data += image.d->bytes_per_line;
6164 }
6165 } else {
6166 QImage alpha32 = *this;
6167 if (d->format != Format_ARGB32 && d->format != Format_ARGB32_Premultiplied)
6168 alpha32 = convertToFormat(Format_ARGB32);
6169
6170 const uchar *src_data = alpha32.d->data;
6171 uchar *dest_data = image.d->data;
6172 for (int y=0; y<h; ++y) {
6173 const QRgb *src = (const QRgb *) src_data;
6174 uchar *dest = dest_data;
6175 for (int x=0; x<w; ++x) {
6176 *dest = qAlpha(*src);
6177 ++dest;
6178 ++src;
6179 }
6180 src_data += alpha32.d->bytes_per_line;
6181 dest_data += image.d->bytes_per_line;
6182 }
6183 }
6184
6185 return image;
6186}
6187
6188/*!
6189 Returns true if the image has a format that respects the alpha
6190 channel, otherwise returns false.
6191
6192 \sa {QImage#Image Information}{Image Information}
6193*/
6194bool QImage::hasAlphaChannel() const
6195{
6196 return d && (d->format == Format_ARGB32_Premultiplied
6197 || d->format == Format_ARGB32
6198 || d->format == Format_ARGB8565_Premultiplied
6199 || d->format == Format_ARGB8555_Premultiplied
6200 || d->format == Format_ARGB6666_Premultiplied
6201 || d->format == Format_ARGB4444_Premultiplied
6202 || (d->has_alpha_clut && (d->format == Format_Indexed8
6203 || d->format == Format_Mono
6204 || d->format == Format_MonoLSB)));
6205}
6206
6207
6208/*!
6209 \since 4.7
6210 Returns the number of bit planes in the image.
6211
6212 The number of bit planes is the number of bits of color and
6213 transparency information for each pixel. This is different from
6214 (i.e. smaller than) the depth when the image format contains
6215 unused bits.
6216
6217 \sa depth(), format(), {QImage#Image Formats}{Image Formats}
6218*/
6219int QImage::bitPlaneCount() const
6220{
6221 if (!d)
6222 return 0;
6223 int bpc = 0;
6224 switch (d->format) {
6225 case QImage::Format_Invalid:
6226 break;
6227 case QImage::Format_RGB32:
6228 bpc = 24;
6229 break;
6230 case QImage::Format_RGB666:
6231 bpc = 18;
6232 break;
6233 case QImage::Format_RGB555:
6234 bpc = 15;
6235 break;
6236 case QImage::Format_ARGB8555_Premultiplied:
6237 bpc = 23;
6238 break;
6239 case QImage::Format_RGB444:
6240 bpc = 12;
6241 break;
6242 default:
6243 bpc = depthForFormat(d->format);
6244 break;
6245 }
6246 return bpc;
6247}
6248
6249
6250#ifdef QT3_SUPPORT
6251#if defined(Q_WS_X11)
6252QT_BEGIN_INCLUDE_NAMESPACE
6253#include <private/qt_x11_p.h>
6254QT_END_INCLUDE_NAMESPACE
6255#endif
6256
6257QImage::Endian QImage::systemBitOrder()
6258{
6259#if defined(Q_WS_X11)
6260 return BitmapBitOrder(X11->display) == MSBFirst ? BigEndian : LittleEndian;
6261#else
6262 return BigEndian;
6263#endif
6264}
6265#endif
6266
6267/*!
6268 \fn QImage QImage::copy(const QRect &rect, Qt::ImageConversionFlags flags) const
6269 \compat
6270
6271 Use copy() instead.
6272*/
6273
6274/*!
6275 \fn QImage QImage::copy(int x, int y, int w, int h, Qt::ImageConversionFlags flags) const
6276 \compat
6277
6278 Use copy() instead.
6279*/
6280
6281/*!
6282 \fn QImage QImage::scaleWidth(int w) const
6283 \compat
6284
6285 Use scaledToWidth() instead.
6286*/
6287
6288/*!
6289 \fn QImage QImage::scaleHeight(int h) const
6290 \compat
6291
6292 Use scaledToHeight() instead.
6293*/
6294
6295static QImage smoothScaled(const QImage &source, int w, int h) {
6296 QImage src = source;
6297 if (src.format() == QImage::Format_ARGB32)
6298 src = src.convertToFormat(QImage::Format_ARGB32_Premultiplied);
6299 else if (src.depth() < 32) {
6300 if (src.hasAlphaChannel())
6301 src = src.convertToFormat(QImage::Format_ARGB32_Premultiplied);
6302 else
6303 src = src.convertToFormat(QImage::Format_RGB32);
6304 }
6305
6306 return qSmoothScaleImage(src, w, h);
6307}
6308
6309
6310static QImage rotated90(const QImage &image) {
6311 QImage out(image.height(), image.width(), image.format());
6312 if (image.colorCount() > 0)
6313 out.setColorTable(image.colorTable());
6314 int w = image.width();
6315 int h = image.height();
6316 switch (image.format()) {
6317 case QImage::Format_RGB32:
6318 case QImage::Format_ARGB32:
6319 case QImage::Format_ARGB32_Premultiplied:
6320 qt_memrotate270(reinterpret_cast<const quint32*>(image.bits()),
6321 w, h, image.bytesPerLine(),
6322 reinterpret_cast<quint32*>(out.bits()),
6323 out.bytesPerLine());
6324 break;
6325 case QImage::Format_RGB666:
6326 case QImage::Format_ARGB6666_Premultiplied:
6327 case QImage::Format_ARGB8565_Premultiplied:
6328 case QImage::Format_ARGB8555_Premultiplied:
6329 case QImage::Format_RGB888:
6330 qt_memrotate270(reinterpret_cast<const quint24*>(image.bits()),
6331 w, h, image.bytesPerLine(),
6332 reinterpret_cast<quint24*>(out.bits()),
6333 out.bytesPerLine());
6334 break;
6335 case QImage::Format_RGB555:
6336 case QImage::Format_RGB16:
6337 case QImage::Format_ARGB4444_Premultiplied:
6338 qt_memrotate270(reinterpret_cast<const quint16*>(image.bits()),
6339 w, h, image.bytesPerLine(),
6340 reinterpret_cast<quint16*>(out.bits()),
6341 out.bytesPerLine());
6342 break;
6343 case QImage::Format_Indexed8:
6344 qt_memrotate270(reinterpret_cast<const quint8*>(image.bits()),
6345 w, h, image.bytesPerLine(),
6346 reinterpret_cast<quint8*>(out.bits()),
6347 out.bytesPerLine());
6348 break;
6349 default:
6350 for (int y=0; y<h; ++y) {
6351 if (image.colorCount())
6352 for (int x=0; x<w; ++x)
6353 out.setPixel(h-y-1, x, image.pixelIndex(x, y));
6354 else
6355 for (int x=0; x<w; ++x)
6356 out.setPixel(h-y-1, x, image.pixel(x, y));
6357 }
6358 break;
6359 }
6360 return out;
6361}
6362
6363
6364static QImage rotated180(const QImage &image) {
6365 return image.mirrored(true, true);
6366}
6367
6368
6369static QImage rotated270(const QImage &image) {
6370 QImage out(image.height(), image.width(), image.format());
6371 if (image.colorCount() > 0)
6372 out.setColorTable(image.colorTable());
6373 int w = image.width();
6374 int h = image.height();
6375 switch (image.format()) {
6376 case QImage::Format_RGB32:
6377 case QImage::Format_ARGB32:
6378 case QImage::Format_ARGB32_Premultiplied:
6379 qt_memrotate90(reinterpret_cast<const quint32*>(image.bits()),
6380 w, h, image.bytesPerLine(),
6381 reinterpret_cast<quint32*>(out.bits()),
6382 out.bytesPerLine());
6383 break;
6384 case QImage::Format_RGB666:
6385 case QImage::Format_ARGB6666_Premultiplied:
6386 case QImage::Format_ARGB8565_Premultiplied:
6387 case QImage::Format_ARGB8555_Premultiplied:
6388 case QImage::Format_RGB888:
6389 qt_memrotate90(reinterpret_cast<const quint24*>(image.bits()),
6390 w, h, image.bytesPerLine(),
6391 reinterpret_cast<quint24*>(out.bits()),
6392 out.bytesPerLine());
6393 break;
6394 case QImage::Format_RGB555:
6395 case QImage::Format_RGB16:
6396 case QImage::Format_ARGB4444_Premultiplied:
6397 qt_memrotate90(reinterpret_cast<const quint16*>(image.bits()),
6398 w, h, image.bytesPerLine(),
6399 reinterpret_cast<quint16*>(out.bits()),
6400 out.bytesPerLine());
6401 break;
6402 case QImage::Format_Indexed8:
6403 qt_memrotate90(reinterpret_cast<const quint8*>(image.bits()),
6404 w, h, image.bytesPerLine(),
6405 reinterpret_cast<quint8*>(out.bits()),
6406 out.bytesPerLine());
6407 break;
6408 default:
6409 for (int y=0; y<h; ++y) {
6410 if (image.colorCount())
6411 for (int x=0; x<w; ++x)
6412 out.setPixel(y, w-x-1, image.pixelIndex(x, y));
6413 else
6414 for (int x=0; x<w; ++x)
6415 out.setPixel(y, w-x-1, image.pixel(x, y));
6416 }
6417 break;
6418 }
6419 return out;
6420}
6421
6422/*!
6423 Returns a copy of the image that is transformed using the given
6424 transformation \a matrix and transformation \a mode.
6425
6426 The transformation \a matrix is internally adjusted to compensate
6427 for unwanted translation; i.e. the image produced is the smallest
6428 image that contains all the transformed points of the original
6429 image. Use the trueMatrix() function to retrieve the actual matrix
6430 used for transforming an image.
6431
6432 Unlike the other overload, this function can be used to perform perspective
6433 transformations on images.
6434
6435 \sa trueMatrix(), {QImage#Image Transformations}{Image
6436 Transformations}
6437*/
6438
6439QImage QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode ) const
6440{
6441 if (!d)
6442 return QImage();
6443
6444 // source image data
6445 int ws = width();
6446 int hs = height();
6447
6448 // target image data
6449 int wd;
6450 int hd;
6451
6452 // compute size of target image
6453 QTransform mat = trueMatrix(matrix, ws, hs);
6454 bool complex_xform = false;
6455 bool scale_xform = false;
6456 if (mat.type() <= QTransform::TxScale) {
6457 if (mat.type() == QTransform::TxNone) // identity matrix
6458 return *this;
6459 else if (mat.m11() == -1. && mat.m22() == -1.)
6460 return rotated180(*this);
6461
6462 if (mode == Qt::FastTransformation) {
6463 hd = qRound(qAbs(mat.m22()) * hs);
6464 wd = qRound(qAbs(mat.m11()) * ws);
6465 } else {
6466 hd = int(qAbs(mat.m22()) * hs + 0.9999);
6467 wd = int(qAbs(mat.m11()) * ws + 0.9999);
6468 }
6469 scale_xform = true;
6470 } else {
6471 if (mat.type() <= QTransform::TxRotate && mat.m11() == 0 && mat.m22() == 0) {
6472 if (mat.m12() == 1. && mat.m21() == -1.)
6473 return rotated90(*this);
6474 else if (mat.m12() == -1. && mat.m21() == 1.)
6475 return rotated270(*this);
6476 }
6477
6478 QPolygonF a(QRectF(0, 0, ws, hs));
6479 a = mat.map(a);
6480 QRect r = a.boundingRect().toAlignedRect();
6481 wd = r.width();
6482 hd = r.height();
6483 complex_xform = true;
6484 }
6485
6486 if (wd == 0 || hd == 0)
6487 return QImage();
6488
6489 // Make use of the optimized algorithm when we're scaling
6490 if (scale_xform && mode == Qt::SmoothTransformation) {
6491 if (mat.m11() < 0.0F && mat.m22() < 0.0F) { // horizontal/vertical flip
6492 return smoothScaled(mirrored(true, true), wd, hd);
6493 } else if (mat.m11() < 0.0F) { // horizontal flip
6494 return smoothScaled(mirrored(true, false), wd, hd);
6495 } else if (mat.m22() < 0.0F) { // vertical flip
6496 return smoothScaled(mirrored(false, true), wd, hd);
6497 } else { // no flipping
6498 return smoothScaled(*this, wd, hd);
6499 }
6500 }
6501
6502 int bpp = depth();
6503
6504 int sbpl = bytesPerLine();
6505 const uchar *sptr = bits();
6506
6507 QImage::Format target_format = d->format;
6508
6509 if (complex_xform || mode == Qt::SmoothTransformation) {
6510 if (d->format < QImage::Format_RGB32 || !hasAlphaChannel()) {
6511 switch(d->format) {
6512 case QImage::Format_RGB16:
6513 target_format = Format_ARGB8565_Premultiplied;
6514 break;
6515 case QImage::Format_RGB555:
6516 target_format = Format_ARGB8555_Premultiplied;
6517 break;
6518 case QImage::Format_RGB666:
6519 target_format = Format_ARGB6666_Premultiplied;
6520 break;
6521 case QImage::Format_RGB444:
6522 target_format = Format_ARGB4444_Premultiplied;
6523 break;
6524 default:
6525 target_format = Format_ARGB32_Premultiplied;
6526 break;
6527 }
6528 }
6529 }
6530
6531 QImage dImage(wd, hd, target_format);
6532 QIMAGE_SANITYCHECK_MEMORY(dImage);
6533
6534 if (target_format == QImage::Format_MonoLSB
6535 || target_format == QImage::Format_Mono
6536 || target_format == QImage::Format_Indexed8) {
6537 dImage.d->colortable = d->colortable;
6538 dImage.d->has_alpha_clut = d->has_alpha_clut | complex_xform;
6539 }
6540
6541 dImage.d->dpmx = dotsPerMeterX();
6542 dImage.d->dpmy = dotsPerMeterY();
6543
6544 switch (bpp) {
6545 // initizialize the data
6546 case 8:
6547 if (dImage.d->colortable.size() < 256) {
6548 // colors are left in the color table, so pick that one as transparent
6549 dImage.d->colortable.append(0x0);
6550 memset(dImage.bits(), dImage.d->colortable.size() - 1, dImage.byteCount());
6551 } else {
6552 memset(dImage.bits(), 0, dImage.byteCount());
6553 }
6554 break;
6555 case 1:
6556 case 16:
6557 case 24:
6558 case 32:
6559 memset(dImage.bits(), 0x00, dImage.byteCount());
6560 break;
6561 }
6562
6563 if (target_format >= QImage::Format_RGB32) {
6564 QPainter p(&dImage);
6565 if (mode == Qt::SmoothTransformation) {
6566 p.setRenderHint(QPainter::Antialiasing);
6567 p.setRenderHint(QPainter::SmoothPixmapTransform);
6568 }
6569 p.setTransform(mat);
6570 p.drawImage(QPoint(0, 0), *this);
6571 } else {
6572 bool invertible;
6573 mat = mat.inverted(&invertible); // invert matrix
6574 if (!invertible) // error, return null image
6575 return QImage();
6576
6577 // create target image (some of the code is from QImage::copy())
6578 int type = format() == Format_Mono ? QT_XFORM_TYPE_MSBFIRST : QT_XFORM_TYPE_LSBFIRST;
6579 int dbpl = dImage.bytesPerLine();
6580 qt_xForm_helper(mat, 0, type, bpp, dImage.bits(), dbpl, 0, hd, sptr, sbpl, ws, hs);
6581 }
6582 return dImage;
6583}
6584
6585/*!
6586 \fn QTransform QImage::trueMatrix(const QTransform &matrix, int width, int height)
6587
6588 Returns the actual matrix used for transforming an image with the
6589 given \a width, \a height and \a matrix.
6590
6591 When transforming an image using the transformed() function, the
6592 transformation matrix is internally adjusted to compensate for
6593 unwanted translation, i.e. transformed() returns the smallest
6594 image containing all transformed points of the original image.
6595 This function returns the modified matrix, which maps points
6596 correctly from the original image into the new image.
6597
6598 Unlike the other overload, this function creates transformation
6599 matrices that can be used to perform perspective
6600 transformations on images.
6601
6602 \sa transformed(), {QImage#Image Transformations}{Image
6603 Transformations}
6604*/
6605
6606QTransform QImage::trueMatrix(const QTransform &matrix, int w, int h)
6607{
6608 const QRectF rect(0, 0, w, h);
6609 const QRect mapped = matrix.mapRect(rect).toAlignedRect();
6610 const QPoint delta = mapped.topLeft();
6611 return matrix * QTransform().translate(-delta.x(), -delta.y());
6612}
6613
6614bool QImageData::convertInPlace(QImage::Format newFormat, Qt::ImageConversionFlags flags)
6615{
6616 if (format == newFormat)
6617 return true;
6618
6619 // No in-place conversion if we have to detach
6620 if (ref > 1)
6621 return false;
6622
6623 const InPlace_Image_Converter *const converterPtr = &inplace_converter_map[format][newFormat];
6624 InPlace_Image_Converter converter = *converterPtr;
6625 if (converter)
6626 return converter(this, flags);
6627 else
6628 return false;
6629}
6630
6631/*!
6632 \typedef QImage::DataPtr
6633 \internal
6634*/
6635
6636/*!
6637 \fn DataPtr & QImage::data_ptr()
6638 \internal
6639*/
6640
6641QT_END_NAMESPACE
Note: See TracBrowser for help on using the repository browser.