1
/****************************************************************************
3
** Copyright (C) 1992-2005 Trolltech AS. All rights reserved.
5
** This file is part of the painting module of the Qt Toolkit.
7
** This file may be distributed under the terms of the Q Public License
8
** as defined by Trolltech AS of Norway and appearing in the file
9
** LICENSE.QPL included in the packaging of this file.
11
** This file may be distributed and/or modified under the terms of the
12
** GNU General Public License version 2 as published by the Free Software
13
** Foundation and appearing in the file LICENSE.GPL included in the
14
** packaging of this file.
16
** See http://www.trolltech.com/pricing.html or email sales@trolltech.com for
17
** information about Qt Commercial License Agreements.
18
** See http://www.trolltech.com/qpl/ for QPL licensing information.
19
** See http://www.trolltech.com/gpl/ for GPL licensing information.
21
** Contact info@trolltech.com if any conditions of this licensing are
24
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
25
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27
****************************************************************************/
30
#include "qdatastream.h"
34
#include "qimagereader.h"
35
#include "qimagewriter.h"
36
#include "qstringlist.h"
42
#include <private/qdrawhelper_p.h>
43
#include <private/qpixmap_p.h>
45
#ifdef QT_RASTER_IMAGEENGINE
46
#include <private/qpaintengine_raster_p.h>
48
#include <qpaintengine.h>
51
#if defined(Q_CC_DEC) && defined(__alpha) && (__DECCXX_VER-0 >= 50190001)
52
#pragma message disable narrowptr
55
struct QImageData { // internal image data
58
static QImageData *create(const QSize &size, QImage::Format format, int numColors = 0);
65
int nbytes; // number of bytes data
66
QVector<QRgb> colortable;
71
QImage::Format format;
73
int ser_no; // serial number
75
qreal dpmx; // dots per meter X (or 0)
76
qreal dpmy; // dots per meter Y (or 0)
77
QPoint offset; // offset in pixels
79
uint has_alpha_clut : 1;
81
#ifndef QT_NO_IMAGE_TEXT
82
QMap<QImageTextKeyLang, QString> text_lang;
84
QStringList languages() const
87
QMap<QImageTextKeyLang,QString>::const_iterator it = text_lang.begin();
88
for (; it != text_lang.end(); ++it) {
89
r.removeAll(it.key().lang);
90
r.append(it.key().lang);
94
QStringList keys() const
97
QMap<QImageTextKeyLang,QString>::const_iterator it = text_lang.begin();
98
for (; it != text_lang.end(); ++it) {
99
r.removeAll(it.key().key);
100
r.append(it.key().key);
105
#ifndef QT_NO_IMAGEIO
106
bool doImageIO(const QImage *image, QImageWriter* io, int quality) const;
109
QPaintEngine *paintEngine;
112
extern int qt_defaultDpi();
114
QImageData::QImageData()
116
static int serial = 0;
121
width = height = depth = 0;
129
format = QImage::Format_ARGB32;
131
dpmx = qt_defaultDpi()*100./2.54;
132
dpmy = qt_defaultDpi()*100./2.54;
133
offset = QPoint(0,0);
138
static int depthForFormat(QImage::Format format)
142
case QImage::Format_Invalid:
144
case QImage::Format_Mono:
145
case QImage::Format_MonoLSB:
148
case QImage::Format_Indexed8:
151
case QImage::Format_RGB32:
152
case QImage::Format_ARGB32:
153
case QImage::Format_ARGB32_Premultiplied:
160
QImageData * QImageData::create(const QSize &size, QImage::Format format, int numColors)
162
int width = size.width();
163
int height = size.height();
164
if (width <= 0 || height <= 0 || numColors < 0 || format == QImage::Format_Invalid)
165
return 0; // invalid parameter(s)
169
case QImage::Format_Invalid:
171
case QImage::Format_Mono:
172
case QImage::Format_MonoLSB:
176
case QImage::Format_Indexed8:
178
numColors = qMin(numColors, 256);
179
numColors = qMax(0, numColors);
181
case QImage::Format_RGB32:
182
case QImage::Format_ARGB32:
183
case QImage::Format_ARGB32_Premultiplied:
189
QImageData *d = new QImageData;
190
d->colortable.resize(numColors);
192
d->colortable[0] = QColor(Qt::black).rgba();
193
d->colortable[1] = QColor(Qt::white).rgba();
195
for (int i = 0; i < numColors; ++i)
196
d->colortable[i] = 0;
203
d->has_alpha_clut = false;
205
d->bytes_per_line = ((width * d->depth + 31) >> 5) << 2; // bytes per scanline (must be multiple of 8)
207
d->nbytes = d->bytes_per_line*height;
208
d->data = (uchar *)malloc(d->nbytes);
220
QImageData::~QImageData()
223
if (data && own_data)
235
\brief The QImage class provides a hardware-independent pixmap
236
that allows direct access to the pixel data, and can be used as a
243
Qt provides two classes for handling image data: QImage and QPixmap.
244
QImage is designed and optimized for I/O, and for direct pixel
245
access/manipulation. QPixmap is designed and optimized for drawing.
246
There are functions to convert between QImage and QPixmap:
247
QPixmap::convertToImage() and QPixmap::convertFromImage().
249
QImage supports a number of \link QImage::Format formats\endlink. These
250
include monochrome images, 8-bit images, and 32-bit images with an optional
251
alpha channel. Monochrome and 8-bit images are indexed based and use a
252
color lookup table, while 32-bit images use RGB or ARGB values.
254
An entry in the color table is an RGB triplet encoded as an \c qRgb
255
value. Use the color() function to obtain an entry from the table,
256
and the qRed(), qGreen(), and qBlue() functions (\c qcolor.h) to access
257
the components. The qRgb() function is used to make an RGB triplet
258
suitable for use with the setColor() function.
260
Monochrome images have a color table with at most two
261
colors. There are two different types of monochrome images: big endian (MSB first) or
262
little endian (LSB first) bit order. To access a single bit you
263
must do some bit shifts:
265
\quotefromfile snippets/image/image.cpp
270
If this looks complicated, you can convert the monochrome image to an 8-bit indexed
271
image using convertToFormat(). 8-bit images are much easier to work with than
272
1-bit images because they have a single byte per pixel:
274
\quotefromfile snippets/image/image.cpp
279
32-bit images have no color table; instead, each pixel contains
280
an ARGB value. 24 bits contain the RGB value; the most
281
significant byte is reserved for the alpha buffer.
287
On Qt/Embedded, scanlines are aligned to the pixel depth and may
288
be padded to any degree, while on all other platforms, the
289
scanlines are 32-bit aligned for all depths. The constructor
290
taking a \c{uchar*} argument always expects 32-bit aligned data.
291
On Qt/Embedded, an additional constructor allows the number of
292
bytes-per-line to be specified.
294
Pixel colors are retrieved with pixel() and set with setPixel().
296
QImage supports a variety of functions that can be used to obtain
297
information about the image. width(), height(), dotsPerMeterX(), and
298
dotsPerMeterY() provide information about the image size and resolution.
299
The depth(), numColors(), isGrayscale(), and colorTable() functions
300
provide information about the color depth and available color components
301
used to store the image data.
303
It is possible to determine whether a color image can be safely
304
converted to a grayscale image by using the allGray() function. The
305
format(), bytesPerLine(), and numBytes() functions provide low-level
306
information about the data stored in the image.
308
QImage also supports a number of functions for creating a new
309
image that is a transformed version of the original. For example,
310
convertToFormat(), createAlphaMask(), createHeuristicMask(), mirrored(), scaled(), rgbSwapped()
311
and transformed(). There are also functions for changing attributes of
312
an image in-place, for example, setAlphaChannel(), setColor(),
313
setDotsPerMeterX() and setDotsPerMeterY() and setNumColors().
315
Images can be loaded and saved in the supported formats. Images
316
are saved to a file with save(). Images are loaded from a file
317
with load() (or in the constructor) or from an array of data with
318
loadFromData(). The lists of supported formats are available from
319
QImageReader::supportedImageFormats() and
320
QImageWriter::supportedImageFormats().
322
When loading an image, the file name can be either refer to an
323
actual file on disk or to one of the application's embedded
324
resources. See the \l{resources.html}{Resource System} overview
325
for details on how to embed images and other resource files in
326
the application's executable.
328
Strings of text may be added to images using setText().
330
The QImage class uses \l{shclass.html}{implicit sharing}, so you
331
can pass QImage objects around by value.
333
New image formats can be added as \link plugins-howto.html
336
\sa QImageReader, QImageWriter, QPixmap QColor {shclass.html}{Shared Classes}
342
This enum type is used to describe the endianness of the CPU and
343
graphics hardware. It is provided here for compatibility with earlier versions of Qt.
345
\value IgnoreEndian Endianness does not matter. Useful for some
346
operations that are independent of endianness.
347
\value BigEndian Most significant bit first or network byte order, as on SPARC, PowerPC, and Motorola CPUs.
348
\value LittleEndian Least significant bit first or little endian byte order, as on Intel x86.
352
\enum QImage::InvertMode
354
This enum type is used to describe how pixel values should be
355
inverted in the invertPixels() function.
357
\value InvertRgb Invert only the RGB values and leave the alpha
360
\value InvertRgba Invert all channels, including the alpha channel.
366
\value Format_Invalid The image is invalid.
367
\value Format_Mono The image is stored using 1-bit per pixel. Bytes are
368
packed with the most significant bit (MSB) first.
369
\value Format_MonoLSB The image is stored using 1-bit per pixel. Bytes are
370
packed with the less significant bit (LSB) first.
371
\value Format_Indexed8 The image is stored using 8-bit indexes into a colormap.
372
\value Format_RGB32 The image is stored using a 32-bit RGB format (0xffRRGGBB).
373
\value Format_ARGB32 The image is stored using a 32-bit ARGB format (0xAARRGGBB).
374
\value Format_ARGB32_Premultiplied The image is stored using a premultiplied 32-bit
375
ARGB format (0xAARRGGBB), i.e. the red,
376
green, and blue channels are multiplied
377
by the alpha component divided by 255. (If RR, GG, or BB
378
has a higher value than the alpha channel, the results are undefined.)
380
\sa format(), convertToFormat()
383
/*****************************************************************************
384
QImage member functions
385
*****************************************************************************/
387
// table to flip bits
388
static const uchar bitflip[256] = {
392
print OUT (($i >> 7) & 0x01) | (($i >> 5) & 0x02) |
393
(($i >> 3) & 0x04) | (($i >> 1) & 0x08) |
394
(($i << 7) & 0x80) | (($i << 5) & 0x40) |
395
(($i << 3) & 0x20) | (($i << 1) & 0x10), ", ";
399
0, 128, 64, 192, 32, 160, 96, 224, 16, 144, 80, 208, 48, 176, 112, 240,
400
8, 136, 72, 200, 40, 168, 104, 232, 24, 152, 88, 216, 56, 184, 120, 248,
401
4, 132, 68, 196, 36, 164, 100, 228, 20, 148, 84, 212, 52, 180, 116, 244,
402
12, 140, 76, 204, 44, 172, 108, 236, 28, 156, 92, 220, 60, 188, 124, 252,
403
2, 130, 66, 194, 34, 162, 98, 226, 18, 146, 82, 210, 50, 178, 114, 242,
404
10, 138, 74, 202, 42, 170, 106, 234, 26, 154, 90, 218, 58, 186, 122, 250,
405
6, 134, 70, 198, 38, 166, 102, 230, 22, 150, 86, 214, 54, 182, 118, 246,
406
14, 142, 78, 206, 46, 174, 110, 238, 30, 158, 94, 222, 62, 190, 126, 254,
407
1, 129, 65, 193, 33, 161, 97, 225, 17, 145, 81, 209, 49, 177, 113, 241,
408
9, 137, 73, 201, 41, 169, 105, 233, 25, 153, 89, 217, 57, 185, 121, 249,
409
5, 133, 69, 197, 37, 165, 101, 229, 21, 149, 85, 213, 53, 181, 117, 245,
410
13, 141, 77, 205, 45, 173, 109, 237, 29, 157, 93, 221, 61, 189, 125, 253,
411
3, 131, 67, 195, 35, 163, 99, 227, 19, 147, 83, 211, 51, 179, 115, 243,
412
11, 139, 75, 203, 43, 171, 107, 235, 27, 155, 91, 219, 59, 187, 123, 251,
413
7, 135, 71, 199, 39, 167, 103, 231, 23, 151, 87, 215, 55, 183, 119, 247,
414
15, 143, 79, 207, 47, 175, 111, 239, 31, 159, 95, 223, 63, 191, 127, 255
417
const uchar *qt_get_bitflip_array() // called from QPixmap code
423
static QImage::Format formatFor(int depth, QImage::Endian bitOrder)
425
QImage::Format format;
427
format = bitOrder == QImage::BigEndian ? QImage::Format_Mono : QImage::Format_MonoLSB;
428
} else if (depth == 8) {
429
format = QImage::Format_Indexed8;
430
} else if (depth == 32) {
431
format = QImage::Format_RGB32;
433
qWarning("QImage: depth %d not supported", depth);
434
format = QImage::Format_Invalid;
441
Constructs a null image.
453
Constructs an image with \a width, \a height in format \a format.
455
QImage::QImage(int width, int height, Format format)
458
d = QImageData::create(QSize(width, height), format, 0);
462
Constructs an image with \a size in format \a format.
464
QImage::QImage(const QSize &size, Format format)
467
d = QImageData::create(size, format, 0);
471
Constructs an image \a width pixels wide, \a height pixels high with a
472
format of \a format, that uses an existing memory buffer, \a
473
data. The buffer must remain valid throughout the life of the
474
QImage. The image does not delete the buffer at destruction.
476
Note that \a data must be 32-bit aligned.
478
If the image is in an indexed color format, set the color table
479
for the image using setColorTable().
481
QImage::QImage(uchar* data, int width, int height, Format format)
485
if (format == Format_Invalid || width <= 0 || height <= 0 || !data)
486
return; // invalid parameter(s)
494
d->depth = depthForFormat(format);
497
d->bytes_per_line = ((width * d->depth + 31)/32) * 4;
498
d->nbytes = d->bytes_per_line * height;
501
#ifndef QT_NO_IMAGEIO
503
Constructs an image and tries to load the image from the file \a
506
If \a format is specified, the loader attempts to read the image
507
using the specified format. If \a format is not specified (which
508
is the default), the loader probes the file for a header to
509
guess the file format.
511
If the loading of the image failed, this object is a \link
512
isNull() null\endlink image.
514
The QImageReader documentation lists the supported image formats and
515
explains how to add extra formats.
517
The file name can be either refer to an actual file on disk or to
518
one of the application's embedded resources. See the
519
\l{resources.html}{Resource System} overview for details on how
520
to embed images and other resource files in the application's
523
\sa load(), isNull(), QImageReader
526
QImage::QImage(const QString &fileName, const char *format)
530
load(fileName, format);
535
Constructs an image and tries to load the image from the file \a
538
If \a format is specified, the loader attempts to read the image
539
using the specified format. If \a format is not specified (which
540
is the default), the loader probes the files for a header to
541
guess the file format.
543
If the loading of the image failed, this object is a \link
544
isNull() null\endlink image.
546
The QImageReader documentation lists the supported image formats and
547
explains how to add extra formats.
549
The file name can be either refer to an actual file on disk or to
550
one of the application's embedded resources. See the
551
\l{resources.html}{Resource System} overview for details on how
552
to embed images and other resource files in the application's
555
You can disable this constructor by defining \c
556
QT_NO_CAST_FROM_ASCII when you compile your applications. This
557
can be useful if you want to ensure that all user-visible strings
558
go through QObject::tr(), for example.
560
\sa QString::fromAscii(), load(), isNull(), QImageReader
562
QImage::QImage(const char *fileName, const char *format)
565
// ### Qt 5: if you remove the QImage(const QByteArray &) QT3_SUPPORT
566
// constructor, remove this constructor as well. The constructor here
567
// exists so that QImage("foo.png") compiles without ambiguity.
569
load(QString::fromAscii(fileName), format);
572
extern bool qt_read_xpm_image_or_array(QIODevice *device, const char * const *source, QImage &image);
575
Constructs an image from \a xpm, which must be a valid XPM image.
577
Errors are silently ignored.
579
Note that it's possible to squeeze the XPM variable a little bit
580
by using an unusual declaration:
583
static const char * const start_xpm[] = {
589
The extra \c const makes the entire definition read-only, which is
590
slightly more efficient (e.g. when the code is in a shared
591
library) and ROMable when the application is to be stored in ROM.
594
QImage::QImage(const char * const xpm[])
598
if (!qt_read_xpm_image_or_array(0, xpm, *this)) {
599
// We use a qFatal rather than disabling the whole function,
600
// as this constructor may be ambiguous.
601
qFatal("QImage::QImage(), XPM is not supported");
606
\fn QImage::QImage(const QByteArray &data)
608
Use QImage::fromData(\a data) instead.
617
QImage image = QImage::fromData(data);
620
#endif //QT_NO_IMAGEIO
624
Constructs a \link shclass.html shallow copy\endlink of \a image.
627
QImage::QImage(const QImage &image)
637
Constructs an image with \a w width, \a h height, \a depth bits
638
per pixel, \a numColors colors and bit order \a bitOrder.
640
Using this constructor is the same as first constructing a null
641
image and then calling the create() function.
646
QImage::QImage(int w, int h, int depth, int numColors, Endian bitOrder)
649
d = QImageData::create(QSize(w, h), formatFor(depth, bitOrder), numColors);
653
Constructs an image with size \a size pixels, depth \a depth bits,
654
\a numColors and \a bitOrder endianness.
656
Using this constructor is the same as first constructing a null
657
image and then calling the create() function.
661
QImage::QImage(const QSize& size, int depth, int numColors, Endian bitOrder)
664
d = QImageData::create(size, formatFor(depth, bitOrder), numColors);
668
Constructs an image \a w pixels wide, \a h pixels high with a
669
color depth of \a depth, that uses an existing memory buffer, \a
670
data. The buffer must remain valid throughout the life of the
671
QImage. The image does not delete the buffer at destruction.
673
If \a colortable is 0, a color table sufficient for \a numColors
674
will be allocated (and destructed later).
676
Note that \a data must be 32-bit aligned.
678
The endianness is given in \a bitOrder.
680
QImage::QImage(uchar* data, int w, int h, int depth, const QRgb* colortable, int numColors, Endian bitOrder)
684
Format f = formatFor(depth, bitOrder);
685
if (f == Format_Invalid)
687
if (w <= 0 || h <= 0 || numColors < 0 || !data)
688
return; // invalid parameter(s)
701
d->bytes_per_line = ((w*depth+31)/32)*4; // bytes per scanline
702
d->nbytes = d->bytes_per_line * h;
704
d->colortable.resize(numColors);
705
for (int i = 0; i < numColors; ++i)
706
d->colortable[i] = colortable[i];
707
} else if (numColors) {
708
setNumColors(numColors);
715
Constructs an image that uses an existing memory buffer. The
716
buffer must remain valid for the life of the QImage. The image
717
does not delete the buffer at destruction. The buffer is passed as
718
\a data. The image's width is \a w and its height is \a h. The
719
color depth is \a depth. \a bpl specifies the number of bytes per
722
If \a colortable is 0, a color table sufficient for \a numColors
723
will be allocated (and destructed later).
725
The endianness is specified by \a bitOrder.
727
\warning This constructor is only available on Qt/Embedded.
729
QImage::QImage(uchar* data, int w, int h, int depth, int bpl, const QRgb* colortable, int numColors, Endian bitOrder)
733
Format f = formatFor(depth, bitOrder);
734
if (f == Format_Invalid)
736
if (!data || w <= 0 || h <= 0 || depth <= 0 || numColors < 0)
737
return; // invalid parameter(s)
749
d->bytes_per_line = bpl;
750
d->nbytes = d->bytes_per_line * h;
752
d->colortable.reserve(numColors);
753
for (int i = 0; i < numColors; ++i)
754
d->colortable[i] = colortable[i];
755
} else if (numColors) {
756
setNumColors(numColors);
763
Destroys the image and cleans up.
768
if (d && !d->ref.deref())
773
Assigns a \link shclass.html shallow copy\endlink of \a image to
774
this image and returns a reference to this image.
779
QImage &QImage::operator=(const QImage &image)
781
QImageData *x = image.d;
784
x = qAtomicSetPtr(&d, x);
785
if (x && !x->ref.deref())
793
int QImage::devType() const
795
return QInternal::Image;
799
Returns the image as a QVariant
801
QImage::operator QVariant() const
803
return QVariant(QVariant::Image, this);
807
Detaches from shared image d and makes sure that this image is
808
the only one referring to the data.
810
If multiple images share common data, this image makes a copy of
811
the data and detaches itself from the sharing mechanism.
812
Nothing is done if there is just a single reference.
817
void QImage::detach()
819
if (d && d->ref != 1)
825
\fn inline QImage QImage::copy(int x, int y, int w, int h) const
828
Returns a sub-area of the image.
830
The returned image is always \a w by \a h pixels in size, and is
831
copied from position \a x, \a y in this image. In areas beyond
832
this image pixels are filled with pixel 0.
838
Returns a sub-area of the image.
840
The returned image always has the size of the rectangle \a r. In
841
areas beyond this image pixels are filled with pixel 0.
843
If \a r is a ull rectangle the entire image is copied.
845
QImage QImage::copy(const QRect& r) const
851
QImage image(d->width, d->height, d->format);
854
// Qt/Embedded can create images with non-default bpl
855
// make sure we don't crash.
856
if (image.d->nbytes != d->nbytes) {
857
int bpl = image.bytesPerLine();
858
for (int i = 0; i < height(); i++)
859
memcpy(image.scanLine(i), scanLine(i), bpl);
862
memcpy(image.bits(), bits(), d->nbytes);
863
image.d->colortable = d->colortable;
864
image.d->dpmx = d->dpmx;
865
image.d->dpmy = d->dpmy;
866
image.d->offset = d->offset;
867
image.d->has_alpha_clut = d->has_alpha_clut;
868
#ifndef QT_NO_IMAGE_TEXT
869
image.d->text_lang = d->text_lang;
881
if (w <= 0 || h <= 0)
884
QImage image(w, h, d->format);
886
if (x < 0 || y < 0 || x + w > d->width || y + h > d->height) {
887
// bitBlt will not cover entire image - clear it.
899
image.d->colortable = d->colortable;
901
int pixels_to_copy = w - dx;
902
if (pixels_to_copy > x + d->width)
903
pixels_to_copy = d->width - x;
904
int lines_to_copy = h - dy;
905
if (lines_to_copy > y + d->height)
906
lines_to_copy = d->height - y;
908
bool byteAligned = true;
909
if (d->format == Format_Mono || d->format == Format_MonoLSB)
910
byteAligned = !(dx & 7) && !(x & 7) && !(pixels_to_copy & 7);
913
const uchar *src = d->data + ((x * d->depth) >> 3) + y * d->bytes_per_line;
914
uchar *dest = image.d->data + ((dx * d->depth) >> 3) + dy * image.d->bytes_per_line;
915
const int bytes_to_copy = (pixels_to_copy * d->depth) >> 3;
916
for (int i = 0; i < lines_to_copy; ++i) {
917
memcpy(dest, src, bytes_to_copy);
918
src += d->bytes_per_line;
919
dest += image.d->bytes_per_line;
921
} else if (d->format == Format_Mono) {
922
const uchar *src = d->data + y * d->bytes_per_line;
923
uchar *dest = image.d->data + dy * image.d->bytes_per_line;
924
for (int i = 0; i < lines_to_copy; ++i) {
925
for (int j = 0; j < pixels_to_copy; ++j) {
926
if (src[(x + j) >> 3] & (0x80 >> ((x + j) & 7)))
927
dest[(dx + j) >> 3] |= (0x80 >> ((dx + j) & 7));
929
dest[(dx + j) >> 3] &= ~(0x80 >> ((dx + j) & 7));
931
src += d->bytes_per_line;
932
dest += image.d->bytes_per_line;
934
} else { // Format_MonoLSB
935
Q_ASSERT(d->format == Format_MonoLSB);
936
const uchar *src = d->data + y * d->bytes_per_line;
937
uchar *dest = image.d->data + dy * image.d->bytes_per_line;
938
for (int i = 0; i < lines_to_copy; ++i) {
939
for (int j = 0; j < pixels_to_copy; ++j) {
940
if (src[(x + j) >> 3] & (0x1 << ((x + j) & 7)))
941
dest[(dx + j) >> 3] |= (0x1 << ((dx + j) & 7));
943
dest[(dx + j) >> 3] &= ~(0x1 << ((dx + j) & 7));
945
src += d->bytes_per_line;
946
dest += image.d->bytes_per_line;
950
image.d->dpmx = dotsPerMeterX();
951
image.d->dpmy = dotsPerMeterY();
952
image.d->offset = offset();
953
image.d->has_alpha_clut = d->has_alpha_clut;
954
#ifndef QT_NO_IMAGE_TEXT
955
image.d->text_lang = d->text_lang;
962
\fn bool QImage::isNull() const
964
Returns true if it is a null image; otherwise returns false.
966
A null image has all parameters set to zero and no allocated data.
968
bool QImage::isNull() const
974
\fn int QImage::width() const
976
Returns the width of the image.
978
\sa height() size() rect()
980
int QImage::width() const
982
return d ? d->width : 0;
986
\fn int QImage::height() const
988
Returns the height of the image.
990
\sa width() size() rect()
992
int QImage::height() const
994
return d ? d->height : 0;
998
\fn QSize QImage::size() const
1000
Returns the size of the image, i.e. its width and height.
1002
\sa width() height() rect()
1004
QSize QImage::size() const
1006
return d ? QSize(d->width, d->height) : QSize();
1010
\fn QRect QImage::rect() const
1012
Returns the enclosing rectangle (0, 0, width(), height()) of the
1015
\sa width() height() size()
1017
QRect QImage::rect() const
1019
return d ? QRect(0, 0, d->width, d->height) : QRect();
1023
\fn int QImage::depth() const
1025
Returns the depth of the image.
1027
The image depth is the number of bits used to encode a single
1028
pixel, also called bits per pixel (bpp) or bit planes of an image.
1030
The supported depths are 1, 8 and 32.
1034
int QImage::depth() const
1036
return d ? d->depth : 0;
1040
\fn int QImage::numColors() const
1042
Returns the size of the color table for the image.
1044
Notice that numColors() returns 0 for 32-bpp images because these
1045
images do not use color tables, but instead encode pixel values as
1048
\sa setNumColors() colorTable()
1050
int QImage::numColors() const
1052
return d ? d->colortable.size() : 0;
1058
\fn QImage::Endian QImage::bitOrder() const
1060
Returns the bit order for the image.
1062
If it is a 1-bpp image, this function returns either
1063
QImage::BigEndian or QImage::LittleEndian.
1065
If it is not a 1-bpp image, this function returns
1066
QImage::IgnoreEndian.
1072
Returns a pointer to the scanline pointer table.
1074
This is the beginning of the data block for the image.
1076
\sa bits() scanLine()
1078
uchar **QImage::jumpTable()
1084
if (!d->jumptable) {
1085
d->jumptable = (uchar **)malloc(d->height*sizeof(uchar *));
1086
uchar *data = d->data;
1087
int height = d->height;
1088
uchar **p = d->jumptable;
1091
data += d->bytes_per_line;
1094
return d->jumptable;
1100
const uchar * const *QImage::jumpTable() const
1104
if (!d->jumptable) {
1105
d->jumptable = (uchar **)malloc(d->height*sizeof(uchar *));
1106
uchar *data = d->data;
1107
int height = d->height;
1108
uchar **p = d->jumptable;
1111
data += d->bytes_per_line;
1114
return d->jumptable;
1119
Sets the color table used to translate color indexes to RGB values
1120
to the specified \a colors.
1122
void QImage::setColorTable(const QVector<QRgb> colors)
1127
d->colortable = colors;
1128
for (int i = 0; i < d->colortable.size(); ++i)
1129
d->has_alpha_clut |= (qAlpha(d->colortable.at(i)) != 255);
1133
Returns the color table of the image, or an empty list of
1134
the image does not have a color table
1138
QVector<QRgb> QImage::colorTable() const
1140
return d ? d->colortable : QVector<QRgb>();
1145
Returns the number of bytes occupied by the image data.
1147
\sa bytesPerLine() bits()
1149
int QImage::numBytes() const
1151
return d ? d->nbytes : 0;
1158
const uchar * QImage::qwsScanLine(int i) const
1166
int QImage::qwsBytesPerLine() const
1168
return bytesPerLine();
1173
Returns the number of bytes per image scanline. This is equivalent
1174
to numBytes()/height().
1176
\sa numBytes() scanLine()
1178
int QImage::bytesPerLine() const
1180
return (d && d->height) ? d->nbytes / d->height : 0;
1185
Returns the color in the color table at index \a i. The first
1186
color is at index 0.
1188
A color value is an RGB triplet. Use the qRed(), qGreen(), and qBlue()
1189
functions to get the color value components.
1191
\sa setColor() numColors() QColor
1193
QRgb QImage::color(int i) const
1195
Q_ASSERT(i < numColors());
1196
return d ? d->colortable.at(i) : QRgb(uint(-1));
1200
Sets a color in the color table at index \a i to \a c.
1202
A color value is an RGB triplet. Use the qRed(), qGreen(), and qBlue()
1203
functions to get the color value components.
1205
\sa color() setNumColors() numColors()
1207
void QImage::setColor(int i, QRgb c)
1210
Q_ASSERT(i < numColors());
1211
d->colortable[i] = c;
1212
d->has_alpha_clut |= (qAlpha(c) != 255);
1216
Returns a pointer to the pixel data at the scanline with index \a
1217
i. The first scanline is at index 0.
1219
The scanline data is aligned on a 32-bit boundary.
1221
\warning If you are accessing 32-bpp image data, cast the returned
1222
pointer to \c{QRgb*} (QRgb has a 32-bit size) and use it to
1223
read/write the pixel value. You cannot use the \c{uchar*} pointer
1224
directly, because the pixel format depends on the byte order on
1225
the underlying platform. Use qRed(), qGreen(), qBlue(), and
1226
qAlpha() to access the pixels.
1228
\sa bytesPerLine() bits() jumpTable()
1230
uchar *QImage::scanLine(int i)
1233
Q_ASSERT(i >= 0 && i < height());
1234
return d->data + i * d->bytes_per_line;
1240
const uchar *QImage::scanLine(int i) const
1242
Q_ASSERT(i >= 0 && i < height());
1243
return d->data + i * d->bytes_per_line;
1248
Returns a pointer to the first pixel data. This is equivalent to
1251
\sa numBytes() scanLine() jumpTable()
1253
uchar *QImage::bits()
1264
const uchar *QImage::bits() const
1266
return d ? d->data : 0;
1272
\fn void QImage::reset()
1274
Resets all image parameters and deallocates the image data.
1278
Fills the entire image with the pixel value \a pixel.
1280
If the \link depth() depth\endlink of this image is 1, only the
1281
lowest bit is used. If you say fill(0), fill(2), etc., the image
1282
is filled with 0s. If you say fill(1), fill(3), etc., the image is
1283
filled with 1s. If the depth is 8, the lowest 8 bits are used.
1285
Note: QImage::pixel() returns the color of the pixel at the given
1286
coordinates; QColor::pixel() returns the pixel value of the
1287
underlying window system (essentially an index value), so normally
1288
you will want to use QImage::pixel() to use a color from an
1289
existing image or QColor::rgb() to use a specific color.
1291
\sa invertPixels() depth() hasAlphaBuffer() create()
1294
void QImage::fill(uint pixel)
1300
if (d->depth == 1 || d->depth == 8) {
1301
if (d->depth == 1) {
1309
memset(d->data, pixel, d->nbytes);
1310
} else if (d->depth == 32) {
1311
if (d->format == Format_RGB32)
1312
pixel |= 0xff000000;
1313
if (pixel == 0 || pixel == 0xffffffff) {
1314
memset(d->data, (pixel & 0xff), d->nbytes);
1316
uint *data = (uint *)d->data;
1317
uint *end = (uint *)(d->data + d->nbytes);
1325
Inverts all pixel values in the image.
1327
If the depth is 32: if \a mode is InvertRgba (the default), the alpha bits are
1328
also inverted, otherwise they are left unchanged.
1330
If the depth is not 32, the argument \a mode has no meaning. The
1331
default mode is InvertRgb, which leaves the alpha channel
1334
Note that inverting an 8-bit image means to replace all pixels
1335
using color index \e i with a pixel using color index 255 minus \e
1336
i. Similarly for a 1-bit image. The color table is not changed.
1338
\sa fill() depth() hasAlphaBuffer()
1341
void QImage::invertPixels(InvertMode mode)
1347
if (depth() != 32) {
1348
// number of used bytes pr line
1349
int bpl = (d->width + 7) * d->depth / 8;
1350
int pad = d->bytes_per_line - bpl;
1351
uchar *sl = d->data;
1352
for (int y=0; y<d->height; ++y) {
1353
for (int x=0; x<bpl; ++x)
1358
quint32 *p = (quint32*)d->data;
1359
quint32 *end = (quint32*)(d->data + d->nbytes);
1360
uint xorbits = (mode == InvertRgba) ? 0xffffffff : 0x00ffffff;
1367
\fn void QImage::invertPixels(bool invertAlpha)
1369
Use the invertPixels() function that takes a QImage::InvertMode
1373
/*! \fn QImage::Endian QImage::systemByteOrder()
1375
Determines the host computer byte order. Returns
1376
QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first).
1378
\sa systemBitOrder()
1381
// Windows defines these
1393
Resizes the color table to \a numColors colors.
1395
If the color table is expanded all the extra colors will be set to
1398
\sa numColors() color() setColor() colorTable()
1401
void QImage::setNumColors(int numColors)
1407
if (numColors == d->colortable.size())
1409
if (numColors <= 0) { // use no color table
1410
d->colortable = QVector<QRgb>();
1413
int nc = d->colortable.size();
1414
d->colortable.resize(numColors);
1415
for (int i = nc; i < numColors; ++i)
1416
d->colortable[i] = 0;
1420
returns the format of the image.
1422
QImage::Format QImage::format() const
1424
return d ? d->format : Format_Invalid;
1430
Returns true if alpha buffer mode is enabled; otherwise returns
1433
\sa setAlphaBuffer()
1435
bool QImage::hasAlphaBuffer() const
1437
return (d && (d->format != Format_RGB32));
1441
Enables alpha buffer mode if \a enable is true, otherwise disables
1442
it. The default setting is disabled.
1444
An 8-bpp image has 8-bit pixels. A pixel is an index into the
1445
\link color() color table\endlink, which contains 32-bit color
1446
values. In a 32-bpp image, the 32-bit pixels are the color values.
1448
This 32-bit value is encoded as follows: The lower 24 bits are
1449
used for the red, green, and blue components. The upper 8 bits
1450
contain the alpha component.
1452
The alpha component specifies the transparency of a pixel. 0 means
1453
completely transparent and 255 means opaque. The alpha component
1454
is ignored if you do not enable alpha buffer mode.
1456
The alpha buffer is used to set a mask when a QImage is translated
1459
\sa hasAlphaBuffer() createAlphaMask()
1462
void QImage::setAlphaBuffer(bool enable)
1465
|| d->format == Format_Mono
1466
|| d->format == Format_MonoLSB
1467
|| d->format == Format_Indexed8)
1469
if (enable && (d->format == Format_ARGB32 || d->format == Format_ARGB32_Premultiplied))
1471
if (!enable && d->format == Format_RGB32)
1474
d->format = (enable ? Format_ARGB32 : Format_RGB32);
1479
\fn bool QImage::create(int width, int height, int depth, int numColors, Endian bitOrder)
1481
Sets the image \a width, \a height, \a depth, its number of colors
1482
(in \a numColors), and bit order. Returns true if successful, or
1483
false if the parameters are incorrect or if memory cannot be
1486
The \a width and \a height is limited to 32767. \a depth must be
1487
1, 8, or 32. If \a depth is 1, \a bitOrder must be set to
1488
either QImage::LittleEndian or QImage::BigEndian. For other depths
1489
\a bitOrder must be QImage::IgnoreEndian.
1491
This function allocates a color table and a buffer for the image
1492
data. The image data is not initialized.
1494
The image buffer is allocated as a single block that consists of a
1495
table of \link scanLine() scanline\endlink pointers (jumpTable())
1496
and the image data (bits()).
1498
\sa fill() width() height() depth() numColors() bitOrder()
1499
jumpTable() scanLine() bits() bytesPerLine() numBytes()
1501
bool QImage::create(int width, int height, int depth, int numColors, Endian bitOrder)
1503
if (d && !d->ref.deref())
1505
d = QImageData::create(QSize(width, height), formatFor(depth, bitOrder), numColors);
1510
\fn bool QImage::create(const QSize& size, int depth, int numColors, Endian bitOrder)
1513
The width and height are specified in the \a size argument.
1515
bool QImage::create(const QSize& size, int depth, int numColors, QImage::Endian bitOrder)
1517
if (d && !d->ref.deref())
1519
d = QImageData::create(size, formatFor(depth, bitOrder), numColors);
1522
#endif // QT3_SUPPORT
1524
/*****************************************************************************
1525
Internal routines for converting image depth.
1526
*****************************************************************************/
1528
typedef void (*Image_Converter)(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags);
1530
static void convert_ARGB_to_ARGB_PM(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
1532
Q_ASSERT(src->format == QImage::Format_ARGB32);
1533
Q_ASSERT(dest->format == QImage::Format_ARGB32_Premultiplied);
1534
Q_ASSERT(src->width == dest->width);
1535
Q_ASSERT(src->height == dest->height);
1536
Q_ASSERT(src->nbytes == dest->nbytes);
1537
Q_ASSERT(src->bytes_per_line == dest->bytes_per_line);
1539
const QRgb *src_data = (QRgb *) src->data;
1540
const QRgb *end = src_data + (src->nbytes>>2);
1541
QRgb *dest_data = (QRgb *) dest->data;
1542
while (src_data < end) {
1543
*dest_data = PREMUL(*src_data);
1549
static void convert_ARGB_PM_to_ARGB(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
1551
Q_ASSERT(src->format == QImage::Format_ARGB32_Premultiplied);
1552
Q_ASSERT(dest->format == QImage::Format_ARGB32);
1553
Q_ASSERT(src->width == dest->width);
1554
Q_ASSERT(src->height == dest->height);
1555
Q_ASSERT(src->nbytes == dest->nbytes);
1556
Q_ASSERT(src->bytes_per_line == dest->bytes_per_line);
1558
const QRgb *src_data = (QRgb *) src->data;
1559
const QRgb *end = src_data + (src->nbytes>>2);
1560
QRgb *dest_data = (QRgb *) dest->data;
1561
while (src_data < end) {
1562
*dest_data = INV_PREMUL(*src_data);
1568
static void convert_ARGB_PM_to_RGB(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
1570
Q_ASSERT(src->format == QImage::Format_ARGB32_Premultiplied);
1571
Q_ASSERT(dest->format == QImage::Format_RGB32);
1572
Q_ASSERT(src->width == dest->width);
1573
Q_ASSERT(src->height == dest->height);
1574
Q_ASSERT(src->nbytes == dest->nbytes);
1575
Q_ASSERT(src->bytes_per_line == dest->bytes_per_line);
1577
const QRgb *src_data = (QRgb *) src->data;
1578
const QRgb *end = src_data + (src->nbytes>>2);
1579
QRgb *dest_data = (QRgb *) dest->data;
1580
while (src_data < end) {
1581
*dest_data = 0xff000000 | INV_PREMUL(*src_data);
1587
static void swap_bit_order(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
1589
Q_ASSERT(src->format == QImage::Format_Mono || src->format == QImage::Format_MonoLSB);
1590
Q_ASSERT(dest->format == QImage::Format_Mono || dest->format == QImage::Format_MonoLSB);
1591
Q_ASSERT(src->width == dest->width);
1592
Q_ASSERT(src->height == dest->height);
1593
Q_ASSERT(src->nbytes == dest->nbytes);
1594
Q_ASSERT(src->bytes_per_line == dest->bytes_per_line);
1596
dest->colortable = src->colortable;
1598
const uchar *src_data = src->data;
1599
const uchar *end = src->data + src->nbytes;
1600
uchar *dest_data = dest->data;
1601
while (src_data < end) {
1602
*dest_data = bitflip[*src_data];
1608
static void mask_alpha_converter(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
1610
Q_ASSERT(src->width == dest->width);
1611
Q_ASSERT(src->height == dest->height);
1612
Q_ASSERT(src->nbytes == dest->nbytes);
1614
const uint *src_data = (const uint *)src->data;
1615
const uint *end = (const uint *)(src->data + src->nbytes);
1616
uint *dest_data = (uint *)dest->data;
1617
while (src_data < end) {
1618
*dest_data = *src_data | 0xff000000;
1624
static QVector<QRgb> fix_color_table(const QVector<QRgb> &ctbl, QImage::Format format)
1626
QVector<QRgb> colorTable = ctbl;
1627
if (format == QImage::Format_RGB32) {
1628
// check if the color table has alpha
1629
for (int i = 0; i < colorTable.size(); ++i)
1630
if (qAlpha(colorTable.at(i) != 0xff))
1631
colorTable[i] = colorTable.at(i) | 0xff000000;
1632
} else if (format == QImage::Format_ARGB32_Premultiplied) {
1633
// check if the color table has alpha
1634
for (int i = 0; i < colorTable.size(); ++i)
1635
colorTable[i] = PREMUL(colorTable.at(i));
1640
#ifndef QT_NO_IMAGE_DITHER_TO_1
1642
// dither_to_1: Uses selected dithering algorithm.
1645
static void dither_to_Mono(QImageData *dst, const QImageData *src,
1646
Qt::ImageConversionFlags flags, bool fromalpha)
1648
Q_ASSERT(src->width == dst->width);
1649
Q_ASSERT(src->height == dst->height);
1650
Q_ASSERT(dst->format == QImage::Format_Mono || dst->format == QImage::Format_MonoLSB);
1652
dst->colortable.clear();
1653
dst->colortable.append(0xffffffff);
1654
dst->colortable.append(0xff000000);
1656
enum { Threshold, Ordered, Diffuse } dithermode;
1659
if ((flags & Qt::AlphaDither_Mask) == Qt::DiffuseAlphaDither)
1660
dithermode = Diffuse;
1661
else if ((flags & Qt::AlphaDither_Mask) == Qt::OrderedAlphaDither)
1662
dithermode = Ordered;
1664
dithermode = Threshold;
1666
if ((flags & Qt::Dither_Mask) == Qt::ThresholdDither)
1667
dithermode = Threshold;
1668
else if ((flags & Qt::Dither_Mask) == Qt::OrderedDither)
1669
dithermode = Ordered;
1671
dithermode = Diffuse;
1675
int h = src->height;
1677
uchar gray[256]; // gray map for 8 bit images
1678
bool use_gray = (d == 8);
1679
if (use_gray) { // make gray map
1681
// Alpha 0x00 -> 0 pixels (white)
1682
// Alpha 0xFF -> 1 pixels (black)
1683
for (int i = 0; i < src->colortable.size(); i++)
1684
gray[i] = (255 - (src->colortable.at(i) >> 24));
1686
// Pixel 0x00 -> 1 pixels (black)
1687
// Pixel 0xFF -> 0 pixels (white)
1688
for (int i = 0; i < src->colortable.size(); i++)
1689
gray[i] = qGray(src->colortable.at(i));
1693
uchar *dst_data = dst->data;
1694
int dst_bpl = dst->bytes_per_line;
1695
const uchar *src_data = src->data;
1696
int src_bpl = src->bytes_per_line;
1698
switch (dithermode) {
1700
int *line1 = new int[w];
1701
int *line2 = new int[w];
1702
int bmwidth = (w+7)/8;
1705
int wbytes = w * (d/8);
1706
register const uchar *p = src->data;
1707
const uchar *end = p + wbytes;
1709
if (use_gray) { // 8 bit image
1712
} else { // 32 bit image
1715
*b2++ = 255 - (*(uint*)p >> 24);
1720
*b2++ = qGray(*(uint*)p);
1725
for (int y=0; y<h; y++) { // for each scan line...
1726
int *tmp = line1; line1 = line2; line2 = tmp;
1727
bool not_last_line = y < h - 1;
1728
if (not_last_line) { // calc. grayvals for next line
1729
p = src->data + (y+1)*src->bytes_per_line;
1732
if (use_gray) { // 8 bit image
1735
} else { // 24 bit image
1738
*b2++ = 255 - (*(uint*)p >> 24);
1743
*b2++ = qGray(*(uint*)p);
1751
uchar *p = dst->data + y*dst->bytes_per_line;
1752
memset(p, 0, bmwidth);
1756
for (int x=1; x<=w; x++) {
1757
if (*b1 < 128) { // black pixel
1760
} else { // white pixel
1770
*b1 += (err*7)>>4; // spread error to right pixel
1771
if (not_last_line) {
1772
b2[0] += (err*5)>>4; // pixel below
1774
b2[-1] += (err*3)>>4; // pixel below left
1776
b2[1] += err>>4; // pixel below right
1786
memset(dst->data, 0, dst->nbytes);
1788
for (int i=0; i<h; i++) {
1789
const uint *p = (const uint *)src_data;
1790
const uint *end = p + w;
1791
uchar *m = dst_data;
1796
if ((*p++ >> 24) >= qt_bayer_matrix[j++&15][i&15])
1807
if ((uint)qGray(*p++) < qt_bayer_matrix[j++&15][i&15])
1817
dst_data += dst_bpl;
1818
src_data += src_bpl;
1822
for (int i=0; i<h; i++) {
1823
const uchar *p = src_data;
1824
const uchar *end = p + w;
1825
uchar *m = dst_data;
1829
if ((uint)gray[*p++] < qt_bayer_matrix[j++&15][i&15])
1838
dst_data += dst_bpl;
1839
src_data += src_bpl;
1843
default: { // Threshold:
1844
memset(dst->data, 0, dst->nbytes);
1846
for (int i=0; i<h; i++) {
1847
const uint *p = (const uint *)src_data;
1848
const uint *end = p + w;
1849
uchar *m = dst_data;
1853
if ((*p++ >> 24) >= 128)
1854
*m |= 1 << bit; // Set mask "on"
1864
if (qGray(*p++) < 128)
1865
*m |= 1 << bit; // Set pixel "black"
1874
dst_data += dst_bpl;
1875
src_data += src_bpl;
1879
for (int i=0; i<h; i++) {
1880
const uchar *p = src_data;
1881
const uchar *end = p + w;
1882
uchar *m = dst_data;
1885
if (gray[*p++] < 128)
1886
*m |= 1 << bit; // Set mask "on"/ pixel "black"
1894
dst_data += dst_bpl;
1895
src_data += src_bpl;
1901
if (dst->format == QImage::Format_MonoLSB) {
1902
// need to swap bit order
1903
uchar *sl = dst->data;
1904
int bpl = (dst->width + 7) * dst->depth / 8;
1905
int pad = dst->bytes_per_line - bpl;
1906
for (int y=0; y<dst->height; ++y) {
1907
for (int x=0; x<bpl; ++x) {
1917
static void convert_X_to_Mono(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
1919
dither_to_Mono(dst, src, flags, false);
1922
static void convert_ARGB_PM_to_Mono(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
1924
QImageData *tmp = QImageData::create(QSize(src->width, src->height), QImage::Format_ARGB32);
1925
convert_ARGB_PM_to_ARGB(tmp, src, flags);
1926
dither_to_Mono(dst, tmp, flags, false);
1931
// convert_32_to_8: Converts a 32 bits depth (true color) to an 8 bit
1932
// image with a colormap. If the 32 bit image has more than 256 colors,
1933
// we convert the red,green and blue bytes into a single byte encoded
1934
// as 6 shades of each of red, green and blue.
1936
// if dithering is needed, only 1 color at most is available for alpha.
1939
inline QRgbMap() : rgb(0xffffffff) { }
1940
inline bool used() const { return rgb!=0xffffffff; }
1945
static void convert_RGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
1947
Q_ASSERT(src->format == QImage::Format_RGB32 || src->format == QImage::Format_ARGB32);
1948
Q_ASSERT(dst->format == QImage::Format_Indexed8);
1949
Q_ASSERT(src->width == dst->width);
1950
Q_ASSERT(src->height == dst->height);
1952
bool do_quant = (flags & Qt::DitherMode_Mask) == Qt::PreferDither
1953
|| src->format == QImage::Format_ARGB32;
1954
uint alpha_mask = src->format == QImage::Format_RGB32 ? 0xff000000 : 0;
1956
const int tablesize = 997; // prime
1957
QRgbMap table[tablesize];
1960
if (!dst->colortable.isEmpty()) {
1961
QVector<QRgb> ctbl = dst->colortable;
1962
dst->colortable.resize(256);
1963
// Preload palette into table.
1964
// Almost same code as pixel insertion below
1965
for (int i = 0; i < dst->colortable.size(); ++i) {
1967
QRgb p = ctbl.at(i) | alpha_mask;
1968
int hash = p % tablesize;
1970
if (table[hash].used()) {
1971
if (table[hash].rgb == p) {
1972
// Found previous insertion - use it
1975
// Keep searching...
1976
if (++hash == tablesize) hash = 0;
1979
// Cannot be in table
1980
Q_ASSERT (pix != 256); // too many colors
1981
// Insert into table at this unused position
1982
dst->colortable[pix] = p;
1983
table[hash].pix = pix++;
1984
table[hash].rgb = p;
1991
if ((flags & Qt::DitherMode_Mask) != Qt::PreferDither) {
1992
dst->colortable.resize(256);
1993
const uchar *src_data = src->data;
1994
uchar *dest_data = dst->data;
1995
for (int y = 0; y < src->height; y++) { // check if <= 256 colors
1996
const QRgb *s = (const QRgb *)src_data;
1997
uchar *b = dest_data;
1998
for (int x = 0; x < src->width; ++x) {
1999
QRgb p = s[x] | alpha_mask;
2000
int hash = p % tablesize;
2002
if (table[hash].used()) {
2003
if (table[hash].rgb == (p)) {
2004
// Found previous insertion - use it
2007
// Keep searching...
2008
if (++hash == tablesize) hash = 0;
2011
// Cannot be in table
2012
if (pix == 256) { // too many colors
2018
// Insert into table at this unused position
2019
dst->colortable[pix] = p;
2020
table[hash].pix = pix++;
2021
table[hash].rgb = p;
2026
*b++ = table[hash].pix; // May occur once incorrectly
2028
src_data += src->bytes_per_line;
2029
dest_data += dst->bytes_per_line;
2032
int numColors = do_quant ? 256 : pix;
2034
dst->colortable.resize(numColors);
2036
if (do_quant) { // quantization needed
2041
#define INDEXOF(r,g,b) (((r)*(MAX_G+1)+(g))*(MAX_B+1)+(b))
2043
for (int rc=0; rc<=MAX_R; rc++) // build 6x6x6 color cube
2044
for (int gc=0; gc<=MAX_G; gc++)
2045
for (int bc=0; bc<=MAX_B; bc++)
2046
dst->colortable[INDEXOF(rc,gc,bc)] = 0xff000000 | qRgb(rc*255/MAX_R, gc*255/MAX_G, bc*255/MAX_B);
2048
const uchar *src_data = src->data;
2049
uchar *dest_data = dst->data;
2050
if ((flags & Qt::Dither_Mask) == Qt::ThresholdDither) {
2051
for (int y = 0; y < src->height; y++) {
2052
const QRgb *p = (const QRgb *)src_data;
2053
const QRgb *end = p + src->width;
2054
uchar *b = dest_data;
2057
#define DITHER(p,m) ((uchar) ((p * (m) + 127) / 255))
2060
DITHER(qRed(*p), MAX_R),
2061
DITHER(qGreen(*p), MAX_G),
2062
DITHER(qBlue(*p), MAX_B)
2067
src_data += src->bytes_per_line;
2068
dest_data += dst->bytes_per_line;
2070
} else if ((flags & Qt::Dither_Mask) == Qt::DiffuseDither) {
2074
line1[0] = new int[src->width];
2075
line2[0] = new int[src->width];
2076
line1[1] = new int[src->width];
2077
line2[1] = new int[src->width];
2078
line1[2] = new int[src->width];
2079
line2[2] = new int[src->width];
2080
pv[0] = new int[src->width];
2081
pv[1] = new int[src->width];
2082
pv[2] = new int[src->width];
2084
int endian = (QSysInfo::ByteOrder == QSysInfo::BigEndian);
2085
for (int y = 0; y < src->height; y++) {
2086
const uchar* q = src_data;
2087
const uchar* q2 = y < src->height - 1 ? q + src->bytes_per_line : src->data;
2088
uchar *b = dest_data;
2089
for (int chan = 0; chan < 3; chan++) {
2090
int *l1 = (y&1) ? line2[chan] : line1[chan];
2091
int *l2 = (y&1) ? line1[chan] : line2[chan];
2093
for (int i = 0; i < src->width; i++)
2094
l1[i] = q[i*4+chan+endian];
2096
if (y+1 < src->height) {
2097
for (int i = 0; i < src->width; i++)
2098
l2[i] = q2[i*4+chan+endian];
2100
// Bi-directional error diffusion
2102
for (int x = 0; x < src->width; x++) {
2103
int pix = qMax(qMin(5, (l1[x] * 5 + 128)/ 255), 0);
2104
int err = l1[x] - pix * 255 / 5;
2107
// Spread the error around...
2108
if (x + 1< src->width) {
2109
l1[x+1] += (err*7)>>4;
2114
l2[x-1]+=(err*3)>>4;
2117
for (int x = src->width; x-- > 0;) {
2118
int pix = qMax(qMin(5, (l1[x] * 5 + 128)/ 255), 0);
2119
int err = l1[x] - pix * 255 / 5;
2122
// Spread the error around...
2124
l1[x-1] += (err*7)>>4;
2128
if (x + 1 < src->width)
2129
l2[x+1]+=(err*3)>>4;
2134
for (int x = 0; x < src->width; x++) {
2135
*b++ = INDEXOF(pv[0][x],pv[1][x],pv[2][x]);
2138
for (int x = 0; x < src->width; x++) {
2139
*b++ = INDEXOF(pv[2][x],pv[1][x],pv[0][x]);
2142
src_data += src->bytes_per_line;
2143
dest_data += dst->bytes_per_line;
2154
} else { // OrderedDither
2155
for (int y = 0; y < src->height; y++) {
2156
const QRgb *p = (const QRgb *)src_data;
2157
const QRgb *end = p + src->width;
2158
uchar *b = dest_data;
2162
uint d = qt_bayer_matrix[y & 15][x & 15] << 8;
2164
#define DITHER(p, d, m) ((uchar) ((((256 * (m) + (m) + 1)) * (p) + (d)) >> 16))
2167
DITHER(qRed(*p), d, MAX_R),
2168
DITHER(qGreen(*p), d, MAX_G),
2169
DITHER(qBlue(*p), d, MAX_B)
2176
src_data += src->bytes_per_line;
2177
dest_data += dst->bytes_per_line;
2181
#ifndef QT_NO_IMAGE_DITHER_TO_1
2182
if (src->format != QImage::Format_RGB32) {
2183
const int trans = 216;
2184
Q_ASSERT(dst->colortable.size() > trans);
2185
dst->colortable[trans] = 0;
2186
QImageData *mask = QImageData::create(QSize(src->width, src->height), QImage::Format_Mono);
2187
dither_to_Mono(mask, src, flags, true);
2188
uchar *dst_data = dst->data;
2189
const uchar *mask_data = mask->data;
2190
for (int y = 0; y < src->height; y++) {
2191
for (int x = 0; x < src->width ; x++) {
2192
if (!(mask_data[x>>3] & (0x80 >> (x & 7))))
2193
dst_data[x] = trans;
2195
mask_data += mask->bytes_per_line;
2196
dst_data += dst->bytes_per_line;
2198
dst->has_alpha_clut = true;
2210
static void convert_ARGB_PM_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
2212
QImageData *tmp = QImageData::create(QSize(src->width, src->height), QImage::Format_ARGB32);
2213
convert_ARGB_PM_to_ARGB(tmp, src, flags);
2214
convert_RGB_to_Indexed8(dst, tmp, flags);
2218
static void convert_ARGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags)
2220
convert_RGB_to_Indexed8(dst, src, flags);
2223
static void convert_Indexed8_to_X32(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
2225
Q_ASSERT(src->format == QImage::Format_Indexed8);
2226
Q_ASSERT(dest->format == QImage::Format_RGB32
2227
|| dest->format == QImage::Format_ARGB32
2228
|| dest->format == QImage::Format_ARGB32_Premultiplied);
2229
Q_ASSERT(src->width == dest->width);
2230
Q_ASSERT(src->height == dest->height);
2232
QVector<QRgb> colorTable = fix_color_table(src->colortable, dest->format);
2235
const uchar *src_data = src->data;
2236
uchar *dest_data = dest->data;
2237
for (int y = 0; y < src->height; y++) {
2238
uint *p = (uint *)dest_data;
2239
const uchar *b = src_data;
2243
*p++ = colorTable.at(*b++);
2245
src_data += src->bytes_per_line;
2246
dest_data += dest->bytes_per_line;
2250
static void convert_Mono_to_X32(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
2252
Q_ASSERT(src->format == QImage::Format_Mono || src->format == QImage::Format_MonoLSB);
2253
Q_ASSERT(dest->format == QImage::Format_RGB32
2254
|| dest->format == QImage::Format_ARGB32
2255
|| dest->format == QImage::Format_ARGB32_Premultiplied);
2256
Q_ASSERT(src->width == dest->width);
2257
Q_ASSERT(src->height == dest->height);
2259
QVector<QRgb> colorTable = fix_color_table(src->colortable, dest->format);
2261
// Default to black / white colors
2262
if (colorTable.size() < 2) {
2263
if (colorTable.size() == 0)
2264
colorTable << 0xff000000;
2265
colorTable << 0xffffffff;
2268
const uchar *src_data = src->data;
2269
uchar *dest_data = dest->data;
2270
if (src->format == QImage::Format_Mono) {
2271
for (int y = 0; y < dest->height; y++) {
2272
register uint *p = (uint *)dest_data;
2273
for (int x = 0; x < dest->width; x++)
2274
*p++ = colorTable.at((src_data[x>>3] >> (7 - (x & 7))) & 1);
2276
src_data += src->bytes_per_line;
2277
dest_data += dest->bytes_per_line;
2280
for (int y = 0; y < dest->height; y++) {
2281
register uint *p = (uint *)dest_data;
2282
for (int x = 0; x < dest->width; x++)
2283
*p++ = colorTable.at((src_data[x>>3] >> (x & 7)) & 1);
2285
src_data += src->bytes_per_line;
2286
dest_data += dest->bytes_per_line;
2292
static void convert_Mono_to_Indexed8(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
2294
Q_ASSERT(src->format == QImage::Format_Mono || src->format == QImage::Format_MonoLSB);
2295
Q_ASSERT(dest->format == QImage::Format_Indexed8);
2296
Q_ASSERT(src->width == dest->width);
2297
Q_ASSERT(src->height == dest->height);
2299
QVector<QRgb> ctbl = src->colortable;
2300
if (ctbl.size() > 2) {
2302
} else if (ctbl.size() < 2) {
2303
if (ctbl.size() == 0)
2307
dest->colortable = ctbl;
2310
const uchar *src_data = src->data;
2311
uchar *dest_data = dest->data;
2312
if (src->format == QImage::Format_Mono) {
2313
for (int y = 0; y < dest->height; y++) {
2314
register uchar *p = dest_data;
2315
for (int x = 0; x < dest->width; x++)
2316
*p++ = (src_data[x>>3] >> (7 - (x & 7))) & 1;
2317
src_data += src->bytes_per_line;
2318
dest_data += dest->bytes_per_line;
2321
for (int y = 0; y < dest->height; y++) {
2322
register uchar *p = dest_data;
2323
for (int x = 0; x < dest->width; x++)
2324
*p++ = (src_data[x>>3] >> (x & 7)) & 1;
2325
src_data += src->bytes_per_line;
2326
dest_data += dest->bytes_per_line;
2331
// first index source, second dest
2332
static const Image_Converter converter_map[QImage::Format_ARGB32_Premultiplied][QImage::Format_ARGB32_Premultiplied] =
2336
convert_Mono_to_Indexed8,
2337
convert_Mono_to_X32,
2338
convert_Mono_to_X32,
2344
convert_Mono_to_Indexed8,
2345
convert_Mono_to_X32,
2346
convert_Mono_to_X32,
2348
}, // Format_MonoLSB
2354
convert_Indexed8_to_X32,
2355
convert_Indexed8_to_X32,
2356
convert_Indexed8_to_X32
2357
}, // Format_Indexed8
2362
convert_RGB_to_Indexed8,
2364
mask_alpha_converter,
2365
mask_alpha_converter
2371
convert_ARGB_to_Indexed8,
2372
mask_alpha_converter,
2374
convert_ARGB_to_ARGB_PM
2378
convert_ARGB_PM_to_Mono,
2379
convert_ARGB_PM_to_Mono,
2380
convert_ARGB_PM_to_Indexed8,
2381
convert_ARGB_PM_to_RGB,
2382
convert_ARGB_PM_to_ARGB,
2384
} // Format_ARGB32_Premultiplied
2388
Returns a copy of the image in the given \a format.
2390
The image conversion flags specified by \a flags control how the image
2391
data is handled during the conversion process.
2393
QImage QImage::convertToFormat(Format format, Qt::ImageConversionFlags flags) const
2395
if (!d || d->format == format)
2398
#if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
2399
const Image_Converter *converterPtr = &converter_map[d->format - 1][format - 1];
2400
Image_Converter converter = *converterPtr;
2402
Image_Converter converter = converter_map[d->format - 1][format - 1];
2407
QImage image(d->width, d->height, format);
2408
converter(image.d, d, flags);
2413
Returns a copy of the image converted to the given \a format, using
2414
a color table specified by \a colorTable.
2416
The image conversion flags specified by \a flags control how the image
2417
data is handled during the conversion process.
2419
QImage QImage::convertToFormat(Format format, const QVector<QRgb> &colorTable, Qt::ImageConversionFlags flags) const
2421
if (d->format == format)
2424
#if defined(Q_OS_IRIX) && defined(Q_CC_GNU)
2425
const Image_Converter *converterPtr = &converter_map[d->format - 1][format - 1];
2426
Image_Converter converter = *converterPtr;
2428
Image_Converter converter = converter_map[d->format - 1][format - 1];
2434
QImage image(d->width, d->height, format);
2435
if (image.d->depth <= 8)
2436
image.d->colortable = colorTable;
2437
converter(image.d, d, flags);
2443
Converts the depth (bpp) of the image to \a depth and returns the
2444
converted image. The original image is not changed.
2446
The \a depth argument must be 1, 8 or 32.
2448
Returns \c *this if \a depth is equal to the image depth, or a
2449
\link isNull() null\endlink image if this image cannot be
2452
If the image needs to be modified to fit in a lower-resolution
2453
result (e.g. converting from 32-bit to 8-bit), use the \a
2454
flags to specify how you'd prefer this to happen.
2456
\sa Qt::ImageConversionFlags depth() isNull()
2459
QImage QImage::convertDepth(int depth, Qt::ImageConversionFlags flags) const
2461
if (!d || d->depth == depth)
2464
Format format = formatFor (depth, QImage::LittleEndian);
2465
return convertToFormat(format, flags);
2470
Returns true if (\a x, \a y) is a valid coordinate in the image;
2471
otherwise returns false.
2473
\sa width() height() pixelIndex()
2476
bool QImage::valid(int x, int y) const
2479
&& x >= 0 && x < d->width
2480
&& y >= 0 && y < d->height;
2484
Returns the pixel index at the given coordinates.
2486
If (\a x, \a y) is not \link valid() valid\endlink, or if the
2487
image is not a paletted image (depth() \> 8), the results are
2493
int QImage::pixelIndex(int x, int y) const
2495
if (!d || x < 0 || x >= d->width) {
2496
qWarning("QImage::pixel: x=%d out of range", x);
2499
const uchar * s = scanLine(y);
2502
return (*(s + (x >> 3)) >> (7- (x & 7))) & 1;
2503
case Format_MonoLSB:
2504
return (*(s + (x >> 3)) >> (x & 7)) & 1;
2505
case Format_Indexed8:
2508
qWarning("QImage::pixelIndex: Not applicable for %d-bpp images (no palette)", d->depth);
2515
Returns the color of the pixel at the coordinates (\a x, \a y).
2517
If (\a x, \a y) is not \link valid() on the image\endlink, the
2518
results are undefined.
2520
\sa setPixel() qRed() qGreen() qBlue() valid()
2523
QRgb QImage::pixel(int x, int y) const
2525
if (!d || x < 0 || x >= d->width) {
2526
qWarning("QImage::pixel: x=%d out of range", x);
2529
const uchar * s = scanLine(y);
2532
return d->colortable.at((*(s + (x >> 3)) >> (7- (x & 7))) & 1);
2533
case Format_MonoLSB:
2534
return d->colortable.at((*(s + (x >> 3)) >> (x & 7)) & 1);
2535
case Format_Indexed8:
2536
return d->colortable.at((int)s[x]);
2538
return ((QRgb*)s)[x];
2544
Sets the pixel index or color at the coordinates (\a x, \a y) to
2547
If (\a x, \a y) is not \link valid() valid\endlink, the result is
2550
If the image is a paletted image (depth() \<= 8) and \a
2551
index_or_rgb \>= numColors(), the result is undefined.
2553
\sa pixelIndex() pixel() qRgb() qRgba() valid()
2556
void QImage::setPixel(int x, int y, uint index_or_rgb)
2558
if (!d || x < 0 || x >= width()) {
2559
qWarning("QImage::setPixel: x=%d out of range", x);
2563
if (d->depth == 1) {
2564
uchar * s = scanLine(y);
2565
if (index_or_rgb > 1) {
2566
qWarning("QImage::setPixel: index=%d out of range", index_or_rgb);
2567
} else if (format() == Format_MonoLSB) {
2568
if (index_or_rgb==0)
2569
*(s + (x >> 3)) &= ~(1 << (x & 7));
2571
*(s + (x >> 3)) |= (1 << (x & 7));
2573
if (index_or_rgb==0)
2574
*(s + (x >> 3)) &= ~(1 << (7-(x & 7)));
2576
*(s + (x >> 3)) |= (1 << (7-(x & 7)));
2578
} else if (depth() == 8) {
2579
if (index_or_rgb > (uint)d->colortable.size()) {
2580
qWarning("QImage::setPixel: index=%d out of range", index_or_rgb);
2583
uchar * s = scanLine(y);
2584
s[x] = index_or_rgb;
2585
} else if (depth() == 32) {
2586
QRgb * s = (QRgb*)scanLine(y);
2587
s[x] = index_or_rgb;
2593
Converts the bit order of the image to \a bitOrder and returns the
2594
converted image. The original image is not changed.
2596
Returns \c *this if the \a bitOrder is equal to the image bit
2597
order, or a \link isNull() null\endlink image if this image cannot
2600
\sa bitOrder() systemBitOrder() isNull()
2603
QImage QImage::convertBitOrder(Endian bitOrder) const
2605
if (!d || isNull() || d->depth != 1 || !(bitOrder == BigEndian || bitOrder == LittleEndian))
2608
if ((d->format == Format_Mono && bitOrder == BigEndian)
2609
|| (d->format == Format_MonoLSB && bitOrder == LittleEndian))
2612
QImage image(d->width, d->height, d->format == Format_Mono ? Format_MonoLSB : Format_Mono);
2614
const uchar *data = d->data;
2615
const uchar *end = data + d->nbytes;
2616
uchar *ndata = image.d->data;
2618
*ndata++ = bitflip[*data++];
2620
image.setDotsPerMeterX(dotsPerMeterX());
2621
image.setDotsPerMeterY(dotsPerMeterY());
2623
image.d->colortable = d->colortable;
2628
Returns true if all the colors in the image are shades of gray
2629
(i.e. their red, green and blue components are equal); otherwise
2632
This function is slow for large 32-bit images.
2636
bool QImage::allGray() const
2642
int p = width()*height();
2643
QRgb* b = (QRgb*)bits();
2648
if (d->colortable.isEmpty())
2650
for (int i = 0; i < numColors(); i++)
2651
if (!qIsGray(d->colortable.at(i)))
2658
For 32-bit images, this function is equivalent to allGray().
2660
For 8-bpp images, this function returns true if color(i) is
2661
QRgb(i,i,i) for all indexes of the color table; otherwise returns
2664
\sa allGray() depth()
2666
bool QImage::isGrayscale() const
2675
for (int i = 0; i < numColors(); i++)
2676
if (d->colortable.at(i) != qRgb(i,i,i))
2686
\fn QImage QImage::smoothScale(int width, int height, Qt::AspectRatioMode mode) const
2688
Use scale(\a width, \a height, \a mode, Qt::SmoothTransformation) instead.
2692
\fn QImage QImage::smoothScale(const QSize &size, Qt::AspectRatioMode mode) const
2695
Use scale(\a size, \a mode, Qt::SmoothTransformation) instead.
2699
\fn QImage QImage::scaled(int w, int h, Qt::AspectRatioMode aspectRatioMode,
2700
Qt::TransformationMode transformMode) const
2702
Returns a copy of the image scaled to a rectangle of width \a w
2703
and height \a h according to \a aspectRatioMode and \a transformMode.
2706
\i If \a aspectRatioMode is \c Qt::IgnoreAspectRatio, the image
2707
is scaled to (\a w, \a h).
2708
\i If \a aspectRatioMode is \c Qt::KeepAspectRatio, the image is
2709
scaled to a rectangle as large as possible inside (\a w, \a
2710
h), preserving the aspect ratio.
2711
\i If \a aspectRatioMode is \c Qt::KeepAspectRatioByExpanding,
2712
the image is scaled to a rectangle as small as possible
2713
outside (\a w, \a h), preserving the aspect ratio.
2716
If either the width \a w or the height \a h is zero or negative,
2717
this function returns a \l{isNull()}{null} image.
2719
\sa scaledToWidth(), scaledToHeight(), transformed()
2723
\fn QImage QImage::scaled(const QSize &size, Qt::AspectRatioMode aspectMode, Qt::TransformationMode transformMode) const
2727
Scales the image to the given \a size, using the aspect ratio and
2728
transformation modes specified by \a aspectMode and \a transformMode.
2730
#ifndef QT_NO_IMAGE_TRANSFORMATION
2731
QImage QImage::scaled(const QSize& s, Qt::AspectRatioMode aspectMode, Qt::TransformationMode mode) const
2734
qWarning("QImage::scaled: Image is a null image");
2740
QSize newSize = size();
2741
newSize.scale(s, aspectMode);
2742
if (newSize == size())
2747
wm.scale((double)newSize.width() / width(), (double)newSize.height() / height());
2748
img = transformed(wm, mode);
2754
Returns a scaled copy of the image with a width of \a w pixels using
2755
the specified transformation \a mode. This function automatically
2756
calculates the height of the image so that its aspect ratio is preserved.
2758
If \a w is 0 or negative a \link isNull() null\endlink image is
2761
\sa scaled(), scaledToHeight(), transformed()
2763
#ifndef QT_NO_IMAGE_TRANSFORMATION
2764
QImage QImage::scaledToWidth(int w, Qt::TransformationMode mode) const
2767
qWarning("QImage::scaleWidth: Image is a null image");
2774
double factor = (double) w / width();
2775
wm.scale(factor, factor);
2776
return transformed(wm, mode);
2781
Returns a scaled copy of the image with a height of \a h pixels
2782
using a transformation specified by \a mode.
2783
This function automatically calculates the width of the image so that
2784
the ratio of the image is preserved.
2786
If \a h is 0 or negative a \link isNull() null\endlink image is
2789
\sa scaled(), scaledToWidth(), transformed()
2791
#ifndef QT_NO_IMAGE_TRANSFORMATION
2792
QImage QImage::scaledToHeight(int h, Qt::TransformationMode mode) const
2795
qWarning("QImage::scaleHeight: Image is a null image");
2802
double factor = (double) h / height();
2803
wm.scale(factor, factor);
2804
return transformed(wm, mode);
2810
Returns the actual matrix used for transforming a image with \a w
2811
width and \a h height and matrix \a matrix.
2813
When transforming a image with transformed(), the transformation matrix
2814
is internally adjusted to compensate for unwanted translation,
2815
i.e. transformed() returns the smallest image containing all
2816
transformed points of the original image.
2818
This function returns the modified matrix, which maps points
2819
correctly from the original image into the new image.
2821
\sa transformed(), QMatrix
2823
#ifndef QT_NO_PIXMAP_TRANSFORMATION
2824
QMatrix QImage::trueMatrix(const QMatrix &matrix, int w, int h)
2826
const qreal dt = qreal(0.);
2827
qreal x1,y1, x2,y2, x3,y3, x4,y4; // get corners
2828
qreal xx = qreal(w);
2829
qreal yy = qreal(h);
2831
QMatrix mat(matrix.m11(), matrix.m12(), matrix.m21(), matrix.m22(), 0., 0.);
2833
mat.map(dt, dt, &x1, &y1);
2834
mat.map(xx, dt, &x2, &y2);
2835
mat.map(xx, yy, &x3, &y3);
2836
mat.map(dt, yy, &x4, &y4);
2838
qreal ymin = y1; // lowest y value
2839
if (y2 < ymin) ymin = y2;
2840
if (y3 < ymin) ymin = y3;
2841
if (y4 < ymin) ymin = y4;
2842
qreal xmin = x1; // lowest x value
2843
if (x2 < xmin) xmin = x2;
2844
if (x3 < xmin) xmin = x3;
2845
if (x4 < xmin) xmin = x4;
2847
qreal ymax = y1; // lowest y value
2848
if (y2 > ymax) ymax = y2;
2849
if (y3 > ymax) ymax = y3;
2850
if (y4 > ymax) ymax = y4;
2851
qreal xmax = x1; // lowest x value
2852
if (x2 > xmax) xmax = x2;
2853
if (x3 > xmax) xmax = x3;
2854
if (x4 > xmax) xmax = x4;
2856
if (xmax-xmin > 1.0)
2857
xmin -= xmin/(xmax-xmin);
2858
if (ymax-ymin > 1.0)
2859
ymin -= ymin/(ymax-ymin);
2861
mat.setMatrix(matrix.m11(), matrix.m12(), matrix.m21(), matrix.m22(), -xmin, -ymin);
2864
#endif // QT_NO_WMATRIX
2867
Returns a copy of the image that is transformed with the transformation
2868
matrix specified by \a matrix and using the transformation mode specified
2871
The transformation \a matrix is internally adjusted to compensate
2872
for unwanted translation; i.e. the image produced is the smallest image
2873
that contains all the transformed points of the original image.
2875
\sa scaled(), QPixmap::transformed(), QPixmap::trueMatrix(), QMatrix
2877
#ifndef QT_NO_IMAGE_TRANSFORMATION
2878
QImage QImage::transformed(const QMatrix &matrix, Qt::TransformationMode mode) const
2883
// source image data
2887
// target image data
2891
// compute size of target image
2892
QMatrix mat = trueMatrix(matrix, ws, hs);
2893
bool complex_xform = false;
2894
if (mat.m12() == 0.0F && mat.m21() == 0.0F) {
2895
if (mat.m11() == 1.0F && mat.m22() == 1.0F) // identity matrix
2897
hd = int(qAbs(mat.m22()) * hs + 0.9999);
2898
wd = int(qAbs(mat.m11()) * ws + 0.9999);
2901
} else { // rotation or shearing
2902
QPolygonF a(QRectF(0, 0, ws, hs));
2904
QRectF r = a.boundingRect().normalized();
2905
wd = int(r.width() + 0.9999);
2906
hd = int(r.height() + 0.9999);
2907
complex_xform = true;
2910
if (wd == 0 || hd == 0)
2915
int sbpl = bytesPerLine();
2916
const uchar *sptr = bits();
2918
QImage dImage(wd, hd, d->format);
2919
dImage.d->colortable = d->colortable;
2920
dImage.d->has_alpha_clut = d->has_alpha_clut;
2921
if (dImage.d->format == Format_RGB32 && complex_xform)
2922
dImage.d->format = Format_ARGB32_Premultiplied;
2923
dImage.d->dpmx = dotsPerMeterX();
2924
dImage.d->dpmy = dotsPerMeterY();
2927
// initizialize the data
2929
memset(dImage.bits(), 0, dImage.numBytes());
2932
if (dImage.d->colortable.size() < 256) {
2933
// colors are left in the color table, so pick that one as transparent
2934
dImage.d->colortable.append(0x0);
2935
memset(dImage.bits(), dImage.d->colortable.size() - 1, dImage.numBytes());
2937
memset(dImage.bits(), 0, dImage.numBytes());
2941
memset(dImage.bits(), 0x00, dImage.numBytes());
2945
if (d->format == QImage::Format_RGB32 || d->format == QImage::Format_ARGB32_Premultiplied) {
2946
QPainter p(&dImage);
2947
if (mode == Qt::SmoothTransformation) {
2948
p.setRenderHint(QPainter::Antialiasing);
2949
p.setRenderHint(QPainter::SmoothPixmapTransform);
2952
p.drawImage(QPoint(0, 0), *this);
2955
mat = mat.inverted(&invertible); // invert matrix
2956
if (!invertible) // error, return null image
2959
// create target image (some of the code is from QImage::copy())
2960
int type = format() == Format_Mono ? QT_XFORM_TYPE_MSBFIRST : QT_XFORM_TYPE_LSBFIRST;
2961
int dbpl = dImage.bytesPerLine();
2962
qt_xForm_helper(mat, 0, type, bpp, dImage.bits(), dbpl, 0, hd, sptr, sbpl, ws, hs);
2969
Builds and returns a 1-bpp mask from the alpha buffer in this
2970
image. Returns a \link isNull() null\endlink image if the image is
2973
See QPixmap::convertFromImage() for a description of the \a
2976
The returned image has little-endian bit order, which you can
2977
convert to big-endian using convertToFormat().
2979
\sa createHeuristicMask()
2981
#ifndef QT_NO_IMAGE_DITHER_TO_1
2982
QImage QImage::createAlphaMask(Qt::ImageConversionFlags flags) const
2984
if (!d || d->format == QImage::Format_RGB32)
2987
if (d->depth == 1) {
2988
// A monochrome pixmap, with alpha channels on those two colors.
2989
// Pretty unlikely, so use less efficient solution.
2990
return convertToFormat(Format_Indexed8, flags).createAlphaMask(flags);
2993
QImage mask(d->width, d->height, Format_MonoLSB);
2994
dither_to_Mono(mask.d, d, flags, true);
2999
#ifndef QT_NO_IMAGE_HEURISTIC_MASK
3001
Creates and returns a 1-bpp heuristic mask for this image. It
3002
works by selecting a color from one of the corners, then chipping
3003
away pixels of that color starting at all the edges.
3005
The four corners vote for which color is to be masked away. In
3006
case of a draw (this generally means that this function is not
3007
applicable to the image), the result is arbitrary.
3009
The returned image has little-endian bit order, which you can
3010
convert to big-endian using convertToFormat().
3012
If \a clipTight is true (the default) the mask is just large enough to cover the
3013
pixels; otherwise, the mask is larger than the data pixels.
3015
This function disregards the alpha buffer.
3017
\sa createAlphaMask()
3020
QImage QImage::createHeuristicMask(bool clipTight) const
3025
if (d->depth != 32) {
3026
QImage img32 = convertToFormat(Format_RGB32);
3027
return img32.createHeuristicMask(clipTight);
3030
#define PIX(x,y) (*((QRgb*)scanLine(y)+x) & 0x00ffffff)
3034
QImage m(w, h, Format_MonoLSB);
3036
m.setColor(0, 0xffffff);
3040
QRgb background = PIX(0,0);
3041
if (background != PIX(w-1,0) &&
3042
background != PIX(0,h-1) &&
3043
background != PIX(w-1,h-1)) {
3044
background = PIX(w-1,0);
3045
if (background != PIX(w-1,h-1) &&
3046
background != PIX(0,h-1) &&
3047
PIX(0,h-1) == PIX(w-1,h-1)) {
3048
background = PIX(w-1,h-1);
3054
uchar *ypp, *ypc, *ypn;
3057
ypn = m.scanLine(0);
3059
for (y = 0; y < h; y++) {
3062
ypn = (y == h-1) ? 0 : m.scanLine(y+1);
3063
QRgb *p = (QRgb *)scanLine(y);
3064
for (x = 0; x < w; x++) {
3065
// slowness here - it's possible to do six of these tests
3066
// together in one go. oh well.
3067
if ((x == 0 || y == 0 || x == w-1 || y == h-1 ||
3068
!(*(ypc + ((x-1) >> 3)) & (1 << ((x-1) & 7))) ||
3069
!(*(ypc + ((x+1) >> 3)) & (1 << ((x+1) & 7))) ||
3070
!(*(ypp + (x >> 3)) & (1 << (x & 7))) ||
3071
!(*(ypn + (x >> 3)) & (1 << (x & 7)))) &&
3072
( (*(ypc + (x >> 3)) & (1 << (x & 7)))) &&
3073
((*p & 0x00ffffff) == background)) {
3075
*(ypc + (x >> 3)) &= ~(1 << (x & 7));
3083
ypn = m.scanLine(0);
3085
for (y = 0; y < h; y++) {
3088
ypn = (y == h-1) ? 0 : m.scanLine(y+1);
3089
QRgb *p = (QRgb *)scanLine(y);
3090
for (x = 0; x < w; x++) {
3091
if ((*p & 0x00ffffff) != background) {
3093
*(ypc + ((x-1) >> 3)) |= (1 << ((x-1) & 7));
3095
*(ypc + ((x+1) >> 3)) |= (1 << ((x+1) & 7));
3097
*(ypp + (x >> 3)) |= (1 << (x & 7));
3099
*(ypn + (x >> 3)) |= (1 << (x & 7));
3110
#endif //QT_NO_IMAGE_HEURISTIC_MASK
3112
#ifndef QT_NO_IMAGE_MIRROR
3114
This code is contributed by Philipp Lang,
3115
GeneriCom Software Germany (www.generi.com)
3116
under the terms of the QPL, Version 1.0
3120
\fn QImage QImage::mirror(bool horizontal, bool vertical) const
3124
Returns a mirror of the image, mirrored in the horizontal and/or
3125
the vertical direction depending on whether \a horizontal and \a
3126
vertical are set to true or false. The original image is not
3135
Returns a mirror of the image, mirrored in the horizontal and/or
3136
the vertical direction depending on whether \a horizontal and \a
3137
vertical are set to true or false. The original image is not
3142
QImage QImage::mirrored(bool horizontal, bool vertical) const
3147
if ((d->width <= 1 && d->height <= 1) || (!horizontal && !vertical))
3152
// Create result image, copy colormap
3153
QImage result(d->width, d->height, d->format);
3154
result.d->colortable = d->colortable;
3158
int dxi = horizontal ? -1 : 1;
3159
int dxs = horizontal ? w-1 : 0;
3160
int dyi = vertical ? -1 : 1;
3161
int dy = vertical ? h-1: 0;
3164
if (d->depth == 1 || d->depth == 8) {
3165
for (int sy = 0; sy < h; sy++, dy += dyi) {
3166
quint8* ssl = (quint8*)(d->data + sy*d->bytes_per_line);
3167
quint8* dsl = (quint8*)(result.d->data + dy*result.d->bytes_per_line);
3169
for (int sx = 0; sx < w; sx++, dx += dxi)
3174
else if (depth() == 32) {
3175
for (int sy = 0; sy < h; sy++, dy += dyi) {
3176
quint32* ssl = (quint32*)(d->data + sy*d->bytes_per_line);
3177
quint32* dsl = (quint32*)(result.d->data + dy*result.d->bytes_per_line);
3179
for (int sx = 0; sx < w; sx++, dx += dxi)
3184
// special handling of 1 bit images for horizontal mirroring
3185
if (horizontal && depth() == 1) {
3186
int shift = width() % 8;
3187
for (int y = h-1; y >= 0; y--) {
3188
quint8* a0 = (quint8*)(result.d->data + y*d->bytes_per_line);
3195
// Shift bits if unaligned
3199
if (format() == Format_MonoLSB) {
3201
quint8 nc = *a << shift;
3202
*a = (*a >> (8-shift)) | c;
3208
quint8 nc = *a >> shift;
3209
*a = (*a << (8-shift)) | c;
3220
#endif //QT_NO_IMAGE_MIRROR
3223
\fn QImage QImage::swapRGB() const
3225
Returns a QImage in which the values of the red and blue
3226
components of all pixels have been swapped, effectively converting
3227
an RGB image to a BGR image. The original QImage is not changed.
3231
Returns a QImage in which the values of the red and blue
3232
components of all pixels have been swapped, effectively converting
3233
an RGB image to a BGR image. The original QImage is not changed.
3235
QImage QImage::rgbSwapped() const
3237
QImage res = copy();
3239
if (depth() == 32) {
3240
for (int i=0; i < height(); i++) {
3241
uint *p = (uint*)scanLine(i);
3242
uint *q = (uint*)res.scanLine(i);
3243
uint *end = p + width();
3245
*q = ((*p << 16) & 0xff0000) | ((*p >> 16) & 0xff) |
3253
QVector<QRgb> newcolors;
3254
for (int i = 0; i < res.d->colortable.size(); i++) {
3255
QRgb c = res.d->colortable.at(i);
3256
newcolors.append(((c << 16) & 0xff0000) | ((c >> 16) & 0xff) | (c & 0xff00ff00));
3263
#ifndef QT_NO_IMAGEIO
3265
Loads an image from the file \a fileName. Returns true if the
3266
image was successfully loaded; otherwise returns false.
3268
If \a format is specified, the loader attempts to read the image
3269
using the specified format. If \a format is not specified (which
3270
is the default), the loader probes the file for a header to
3271
guess the file format.
3273
The QImageReader documentation lists the supported image formats and
3274
explains how to add extra formats.
3276
The file name can either refer to an actual file on disk or to
3277
one of the application's embedded resources. See the
3278
\l{resources.html}{Resource System} overview for details on how
3279
to embed images and other resource files in the application's
3282
\sa loadFromData(), save(), QImageReader::imageFormat(), QPixmap::load(),
3286
bool QImage::load(const QString &fileName, const char* format)
3288
QImage image = QImageReader(fileName, format).read();
3289
if (!image.isNull()) {
3297
Loads an image from the first \a len bytes of binary data in \a
3298
data. Returns true if the image was successfully loaded; otherwise
3301
If \a format is specified, the loader attempts to read the image
3302
using the specified format. If \a format is not specified (which
3303
is the default), the loader probes the file for a header to
3304
guess the file format.
3306
The QImageReader documentation lists the supported image formats and
3307
explains how to add extra formats.
3309
\sa load(), save(), QImageReader::imageFormat(), QPixmap::loadFromData(),
3313
bool QImage::loadFromData(const uchar *data, int len, const char *format)
3315
QImage image = fromData(data, len, format);
3316
if (!image.isNull()) {
3324
\fn bool QImage::loadFromData(const QByteArray &data, const char *format)
3328
Loads an image from the QByteArray \a data.
3332
\fn QImage QImage::fromData(const uchar *data, int size, const char *format)
3334
Constructs a QImage from the first \a size bytes of binary data
3335
in \a data. If the loading of the image failed, this object is a \link
3336
isNull() null\endlink image.
3338
If \a format is specified, the loader attempts to read the image
3339
using the specified format. If \a format is not specified (which
3340
is the default), the loader probes the file for a header to
3341
guess the file format.
3343
The QImageReader documentation lists the supported image formats and
3344
explains how to add extra formats.
3346
\sa load(), save(), QImageReader::imageFormat(), QPixmap::loadFromData(),
3349
QImage QImage::fromData(const uchar *data, int size, const char *format)
3351
QByteArray a = QByteArray::fromRawData(reinterpret_cast<const char *>(data), size);
3354
b.open(QIODevice::ReadOnly);
3355
return QImageReader(&b, format).read();
3359
\fn QImage QImage::fromData(const QByteArray &data, const char *format)
3363
Loads an image from the QByteArray \a data.
3367
Saves the image to the file \a fileName, using the image file
3368
format \a format and a quality factor of \a quality. \a quality
3369
must be in the range 0 to 100 or -1. Specify 0 to obtain small
3370
compressed files, 100 for large uncompressed files, and -1 (the
3371
default) to use the default settings.
3373
Returns true if the image was successfully saved; otherwise
3376
\sa load(), loadFromData(), QImageReader::imageFormat(), QPixmap::save(),
3379
bool QImage::save(const QString &fileName, const char *format, int quality) const
3383
QImageWriter writer(fileName, format);
3384
return d->doImageIO(this, &writer, quality);
3390
This function writes a QImage to the QIODevice, \a device. This
3391
can be used, for example, to save an image directly into a
3394
\quotefromfile snippets/image/image.cpp
3400
bool QImage::save(QIODevice* device, const char* format, int quality) const
3403
return false; // nothing to save
3404
QImageWriter writer(device, format);
3405
return d->doImageIO(this, &writer, quality);
3411
bool QImageData::doImageIO(const QImage *image, QImageWriter *writer, int quality) const
3413
if (quality > 100 || quality < -1)
3414
qWarning("QPixmap::save: quality out of range [-1,100]");
3416
writer->setQuality(qMin(quality,100));
3417
return writer->write(*image);
3419
#endif //QT_NO_IMAGEIO
3421
/*****************************************************************************
3422
QImage stream functions
3423
*****************************************************************************/
3424
#if !defined(QT_NO_DATASTREAM) && !defined(QT_NO_IMAGEIO)
3428
Writes the image \a image to the stream \a s as a PNG image, or as a
3429
BMP image if the stream's version is 1.
3431
Note that writing the stream to a file will not produce a valid image file.
3434
\link datastreamformat.html Format of the QDataStream operators \endlink
3437
QDataStream &operator<<(QDataStream &s, const QImage &image)
3439
if (s.version() >= 5) {
3440
if (image.isNull()) {
3441
s << (qint32) 0; // null image marker
3448
QImageWriter writer(s.device(), s.version() == 1 ? "bmp" : "png");
3449
writer.write(image);
3456
Reads an image from the stream \a s and stores it in \a image.
3459
\link datastreamformat.html Format of the QDataStream operators \endlink
3462
QDataStream &operator>>(QDataStream &s, QImage &image)
3464
if (s.version() >= 5) {
3468
image = QImage(); // null image
3472
image = QImageReader(s.device(), 0).read();
3480
Returns an image with depth \a d, using the \a palette_count
3481
colors pointed to by \a palette. If \a d is 1 or 8, the returned
3482
image will have its color table ordered the same as \a palette.
3484
If the image needs to be modified to fit in a lower-resolution
3485
result (e.g. converting from 32-bit to 8-bit), use the \a
3486
flags to specify how you'd prefer this to happen.
3488
Note: currently no closest-color search is made. If colors are
3489
found that are not in the palette, the palette may not be used at
3490
all. This result should not be considered valid because it may
3491
change in future implementations.
3493
Currently inefficient for non-32-bit images.
3495
\sa Qt::ImageConversionFlags
3497
QImage QImage::convertDepthWithPalette(int d, QRgb* palette, int palette_count, Qt::ImageConversionFlags flags) const
3499
Format f = formatFor(d, QImage::LittleEndian);
3500
QVector<QRgb> colortable;
3501
for (int i = 0; i < palette_count; ++i)
3502
colortable.append(palette[i]);
3503
return convertToFormat(f, colortable, flags);
3509
Copies a block of pixels from \a src to \a dst. The pixels
3510
copied from source (src) are converted according to
3511
\a flags if it is incompatible with the destination
3514
\a sx, \a sy is the top-left pixel in \a src, \a dx, \a dy
3515
is the top-left position in \a dst and \a sw, \a sh is the
3516
size of the copied block.
3518
The copying is clipped if areas outside \a src or \a dst are
3521
If \a sw is -1, it is adjusted to src->width(). Similarly, if \a
3522
sh is -1, it is adjusted to src->height().
3524
Currently inefficient for non 32-bit images.
3526
void bitBlt(QImage *dst, int dx, int dy, const QImage *src, int sx, int sy, int sw, int sh,
3527
Qt::ImageConversionFlags flags)
3529
if (dst->isNull() || src->isNull())
3532
p.drawImage(QPoint(dx, dy), *src, QRect(sx, sy, sw, sh), flags);
3537
Returns true if this image and image \a i have the same contents;
3538
otherwise returns false. The comparison can be slow, unless there
3539
is some obvious difference, such as different widths, in which
3540
case the function will return quickly.
3545
bool QImage::operator==(const QImage & i) const
3547
// same object, or shared?
3553
// obviously different stuff?
3554
if (i.d->height != d->height || i.d->width != d->width || i.d->format != d->format)
3556
if (d->format != Format_RGB32) {
3557
if (d->colortable != i.d->colortable)
3559
if (d->format == Format_ARGB32 || d->format == Format_ARGB32_Premultiplied) {
3560
if (memcmp(bits(), i.bits(), d->nbytes))
3565
for (int y=0; y<h; ++y) {
3566
for (int x=0; x<w; ++x) {
3567
if (pixelIndex(x, y) != i.pixelIndex(x, y))
3573
//alpha channel undefined, so we must mask it out
3574
for(int l = 0; l < d->height; l++) {
3576
const uint *p1 = reinterpret_cast<const uint*>(scanLine(l));
3577
const uint *p2 = reinterpret_cast<const uint*>(i.scanLine(l));
3579
if ((*p1++ & 0x00ffffff) != (*p2++ & 0x00ffffff))
3589
Returns true if this image and image \a i have different contents;
3590
otherwise returns false. The comparison can be slow, unless there
3591
is some obvious difference, such as different widths, in which
3592
case the function will return quickly.
3597
bool QImage::operator!=(const QImage & i) const
3599
return !(*this == i);
3606
Returns the number of pixels that fit horizontally in a physical
3607
meter. This and dotsPerMeterY() define the intended scale and
3608
aspect ratio of the image.
3610
\sa setDotsPerMeterX()
3612
int QImage::dotsPerMeterX() const
3614
return d ? qRound(d->dpmx) : 0;
3618
Returns the number of pixels that fit vertically in a physical
3619
meter. This and dotsPerMeterX() define the intended scale and
3620
aspect ratio of the image.
3622
\sa setDotsPerMeterY()
3624
int QImage::dotsPerMeterY() const
3626
return d ? qRound(d->dpmy) : 0;
3630
Sets the value returned by dotsPerMeterX() to \a x.
3632
void QImage::setDotsPerMeterX(int x)
3641
Sets the value returned by dotsPerMeterY() to \a y.
3643
void QImage::setDotsPerMeterY(int y)
3652
\fn QPoint QImage::offset() const
3654
Returns the number of pixels by which the image is intended to be
3655
offset by when positioning relative to other images.
3657
QPoint QImage::offset() const
3659
return d ? d->offset : QPoint();
3664
Sets the value returned by offset() to \a p.
3666
void QImage::setOffset(const QPoint& p)
3673
#ifndef QT_NO_IMAGE_TEXT
3676
Returns the string recorded for the keyword \a key in language \a
3677
lang, or in a default language if \a lang is 0.
3679
QString QImage::text(const char* key, const char* lang) const
3681
return d ? d->text_lang.value(QImageTextKeyLang(key,lang)) : QString();
3687
Returns the string recorded for the keyword and language \a kl.
3689
QString QImage::text(const QImageTextKeyLang& kl) const
3691
return d ? d->text_lang.value(kl) : QString();
3695
Returns the language identifiers for which some texts are
3698
Note that if you want to iterate over the list, you should iterate
3702
QStringList list = myImage.textLanguages();
3703
QStringList::Iterator it = list.begin();
3704
while(it != list.end()) {
3710
\sa textList() text() setText() textKeys()
3712
QStringList QImage::textLanguages() const
3714
return d ? d->languages() : QStringList();
3718
Returns the keywords for which some texts are recorded.
3720
Note that if you want to iterate over the list, you should iterate
3724
QStringList list = myImage.textKeys();
3725
QStringList::Iterator it = list.begin();
3726
while(it != list.end()) {
3732
\sa textList() text() setText() textLanguages()
3734
QStringList QImage::textKeys() const
3736
return d ? d->keys() : QStringList();
3740
Returns a list of QImageTextKeyLang objects that enumerate all the
3741
texts key/language pairs set by setText() for this image.
3743
QList<QImageTextKeyLang> QImage::textList() const
3745
return d ? d->text_lang.keys() : QList<QImageTextKeyLang>();
3749
Records string \a s for the keyword \a key. The \a key should be
3750
a portable keyword recognizable by other software - some suggested
3751
values can be found in
3752
\link http://www.libpng.org/pub/png/spec/PNG-Chunks.html#C.Anc-text
3753
the PNG specification\endlink. \a s can be any text. \a lang should
3754
specify the language code (see
3755
\link ftp://ftp.isi.edu/in-notes/1766 RFC 1766\endlink) or 0.
3757
void QImage::setText(const char* key, const char* lang, const QString& s)
3762
QImageTextKeyLang x(key, lang);
3763
d->text_lang.insert(x, s);
3766
#endif // QT_NO_IMAGE_TEXT
3769
Sets the image bits to the \a pixmap contents and returns a
3770
reference to the image.
3772
If the image shares data with other images, it will first
3773
dereference the shared data.
3775
Makes a call to QPixmap::convertToImage().
3778
/*! \fn QImage::Endian QImage::systemBitOrder()
3780
Determines the bit order of the display hardware. Returns
3781
QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first).
3783
\sa systemByteOrder()
3790
Used by QPainter to retreive a paint engine for the image.
3793
QPaintEngine *QImage::paintEngine() const
3798
#ifdef QT_RASTER_IMAGEENGINE
3799
if (!d->paintEngine) {
3800
d->paintEngine = new QRasterPaintEngine();
3803
return d->paintEngine;
3808
Returns the size for the specified \a metric on the device.
3810
int QImage::metric(PaintDeviceMetric metric) const
3825
return qRound(d->width * 1000 / d->dpmx);
3829
return qRound(d->height * 1000 / d->dpmy);
3833
return d->colortable.size();
3841
return (int)(d->dpmx * 0.0254);
3845
return (int)(d->dpmy * 0.0254);
3848
case PdmPhysicalDpiX:
3849
return (int)(d->dpmx * 0.0254);
3852
case PdmPhysicalDpiY:
3853
return (int)(d->dpmy * 0.0254);
3857
qWarning("QImage::metric(), Unhandled metric type: %d\n", metric);
3865
/*****************************************************************************
3866
QPixmap (and QImage) helper functions
3867
*****************************************************************************/
3869
This internal function contains the common (i.e. platform independent) code
3870
to do a transformation of pixel data. It is used by QPixmap::transform() and by
3871
QImage::transform().
3873
\a trueMat is the true transformation matrix (see QPixmap::trueMatrix()) and
3874
\a xoffset is an offset to the matrix.
3876
\a msbfirst specifies for 1bpp images, if the MSB or LSB comes first and \a
3877
depth specifies the colordepth of the data.
3879
\a dptr is a pointer to the destination data, \a dbpl specifies the bits per
3880
line for the destination data, \a p_inc is the offset that we advance for
3881
every scanline and \a dHeight is the height of the destination image.
3883
\a sprt is the pointer to the source data, \a sbpl specifies the bits per
3884
line of the source data, \a sWidth and \a sHeight are the width and height of
3888
#ifndef QT_NO_PIXMAP_TRANSFORMATION
3890
#define IWX_MSB(b) if (trigx < maxws && trigy < maxhs) { \
3891
if (*(sptr+sbpl*(trigy>>12)+(trigx>>15)) & \
3892
(1 << (7-((trigx>>12)&7)))) \
3899
#define IWX_LSB(b) if (trigx < maxws && trigy < maxhs) { \
3900
if (*(sptr+sbpl*(trigy>>12)+(trigx>>15)) & \
3901
(1 << ((trigx>>12)&7))) \
3908
#define IWX_PIX(b) if (trigx < maxws && trigy < maxhs) { \
3909
if ((*(sptr+sbpl*(trigy>>12)+(trigx>>15)) & \
3910
(1 << (7-((trigx>>12)&7)))) == 0) \
3916
bool qt_xForm_helper(const QMatrix &trueMat, int xoffset, int type, int depth,
3917
uchar *dptr, int dbpl, int p_inc, int dHeight,
3918
const uchar *sptr, int sbpl, int sWidth, int sHeight)
3920
int m11 = qRound(trueMat.m11()*4096.0);
3921
int m12 = qRound(trueMat.m12()*4096.0);
3922
int m21 = qRound(trueMat.m21()*4096.0);
3923
int m22 = qRound(trueMat.m22()*4096.0);
3924
int dx = qRound(trueMat.dx()*4096.0);
3925
int dy = qRound(trueMat.dy()*4096.0);
3927
int m21ydx = dx + (xoffset<<16);
3931
uint maxws = sWidth<<12;
3932
uint maxhs = sHeight<<12;
3934
for (int y=0; y<dHeight; y++) { // for each target scanline
3937
uchar *maxp = dptr + dbpl;
3940
case 8: // 8 bpp transform
3941
while (dptr < maxp) {
3942
if (trigx < maxws && trigy < maxhs)
3943
*dptr = *(sptr+sbpl*(trigy>>12)+(trigx>>12));
3950
case 16: // 16 bpp transform
3951
while (dptr < maxp) {
3952
if (trigx < maxws && trigy < maxhs)
3953
*((ushort*)dptr) = *((ushort *)(sptr+sbpl*(trigy>>12) +
3962
case 24: // 24 bpp transform
3963
while (dptr < maxp) {
3964
if (trigx < maxws && trigy < maxhs) {
3965
const uchar *p2 = sptr+sbpl*(trigy>>12) + ((trigx>>12)*3);
3976
case 32: // 32 bpp transform
3977
while (dptr < maxp) {
3978
if (trigx < maxws && trigy < maxhs)
3979
*((uint*)dptr) = *((uint *)(sptr+sbpl*(trigy>>12) +
3993
case QT_XFORM_TYPE_MSBFIRST:
3994
while (dptr < maxp) {
4006
case QT_XFORM_TYPE_LSBFIRST:
4007
while (dptr < maxp) {
4019
# if defined(Q_WS_WIN)
4020
case QT_XFORM_TYPE_WINDOWSPIXMAP:
4021
while (dptr < maxp) {
4045
#endif // QT_NO_PIXMAP_TRANSFORMATION
4048
\fn QImage QImage::xForm(const QMatrix &matrix) const
4050
Use transform(\a matrix) instead.
4054
Returns a number that uniquely identifies the contents of this
4055
QImage object. This means that multiple QImage objects can have
4056
the same serial number as long as they refer to the same contents.
4058
A null image always have a serial number of 0.
4060
An example of where this is useful is for caching QImages.
4063
int QImage::serialNumber() const
4072
Returns true if the image is detached; otherwise returns false.
4077
bool QImage::isDetached() const
4079
return d && d->ref == 1;
4084
Sets the alpha channel of this image to \a alphaChannel.
4086
If \a alphaChannel is an 8 bit grayscale image, the intensity
4087
values are written into this buffer directly. Otherwise, \a
4088
alphaChannel is converted to 32 bit and the intensity of the RGB
4089
pixel values is used.
4091
The image will be converted to the format
4092
Format_ARGB32_Premultiplied if the function succeeds.
4095
void QImage::setAlphaChannel(const QImage &alphaChannel)
4103
if (w != alphaChannel.d->width || h != alphaChannel.d->height) {
4104
qWarning("QImage::setAlphaChannel(), "
4105
"alpha channel must have same dimensions as the target image.");
4111
*this = convertToFormat(QImage::Format_ARGB32_Premultiplied);
4113
// Slight optimization since alphachannels are returned as 8-bit grays.
4114
if (alphaChannel.d->depth == 8 && alphaChannel.isGrayscale()) {
4115
const uchar *src_data = alphaChannel.d->data;
4116
const uchar *dest_data = d->data;
4117
for (int y=0; y<h; ++y) {
4118
const uchar *src = src_data;
4119
QRgb *dest = (QRgb *)dest_data;
4120
for (int x=0; x<w; ++x) {
4122
int destAlpha = qt_div_255(alpha * qAlpha(*dest));
4123
*dest = ((destAlpha << 24)
4124
| (qt_div_255(qRed(*dest) * alpha) << 16)
4125
| (qt_div_255(qGreen(*dest) * alpha) << 8)
4126
| (qt_div_255(qBlue(*dest) * alpha)));
4130
src_data += alphaChannel.d->bytes_per_line;
4131
dest_data += d->bytes_per_line;
4135
const QImage sourceImage = alphaChannel.convertToFormat(QImage::Format_RGB32);
4136
const uchar *src_data = sourceImage.d->data;
4137
const uchar *dest_data = d->data;
4138
for (int y=0; y<h; ++y) {
4139
const QRgb *src = (const QRgb *) src_data;
4140
QRgb *dest = (QRgb *) dest_data;
4141
for (int x=0; x<w; ++x) {
4142
int alpha = qGray(*src);
4143
int destAlpha = qt_div_255(alpha * qAlpha(*dest));
4144
*dest = ((destAlpha << 24)
4145
| (qt_div_255(qRed(*dest) * alpha) << 16)
4146
| (qt_div_255(qGreen(*dest) * alpha) << 8)
4147
| (qt_div_255(qBlue(*dest) * alpha)));
4151
src_data += sourceImage.d->bytes_per_line;
4152
dest_data += d->bytes_per_line;
4159
Extracts the alpha channel from this image as an 8 bit gray scale
4160
image and returns it.
4163
QImage QImage::alphaChannel() const
4171
QImage image(w, h, Format_Indexed8);
4172
image.setNumColors(256);
4174
// set up gray scale table.
4175
for (int i=0; i<256; ++i)
4176
image.setColor(i, qRgb(i, i, i));
4178
if (d->format == Format_Indexed8 && hasAlphaChannel()) {
4179
const uchar *src_data = d->data;
4180
uchar *dest_data = image.d->data;
4181
for (int y=0; y<h; ++y) {
4182
const QRgb *src = (const QRgb *) src_data;
4183
uchar *dest = dest_data;
4184
for (int x=0; x<w; ++x) {
4185
*dest = qAlpha(d->colortable.at(*src));
4189
src_data += d->bytes_per_line;
4190
dest_data += image.d->bytes_per_line;
4192
} else if (d->format == Format_ARGB32 || d->format == Format_ARGB32_Premultiplied) {
4193
const uchar *src_data = d->data;
4194
uchar *dest_data = image.d->data;
4195
for (int y=0; y<h; ++y) {
4196
const QRgb *src = (const QRgb *) src_data;
4197
uchar *dest = dest_data;
4198
for (int x=0; x<w; ++x) {
4199
*dest = qAlpha(*src);
4203
src_data += d->bytes_per_line;
4204
dest_data += image.d->bytes_per_line;
4214
Returns true if the image has an alpha channel.
4216
bool QImage::hasAlphaChannel() const
4218
return d && (d->format == Format_ARGB32_Premultiplied
4219
|| d->format == Format_ARGB32
4220
|| (d->format == Format_Indexed8 && d->has_alpha_clut));
4225
#if defined(Q_WS_X11)
4226
#include <private/qt_x11_p.h>
4229
inline QImage::Endian QImage::systemBitOrder()
4231
#if defined(Q_WS_X11)
4232
return BitmapBitOrder(X11->display) == MSBFirst ? BigEndian : LittleEndian;
4240
\fn QImage QImage::copy(const QRect &rect, Qt::ImageConversionFlags flags) const
4243
Use copy() and convertToFormat() instead.
4247
\fn QImage QImage::copy(int x, int y, int w, int h, Qt::ImageConversionFlags flags) const
4250
Use copy() and convertToFormat() instead.
4254
\fn QImage QImage::scaleWidth(int w) const
4257
Use scaledToWidth() instead.
4261
\fn QImage QImage::scaleHeight(int h) const
4264
Use scaledToHeight() instead.