QtBase
v6.3.1
|
The QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device. More...
#include <qimage.h>
Static Public Member Functions | |
static QTransform | trueMatrix (const QTransform &, int w, int h) |
static QImage | fromData (QByteArrayView data, const char *format=nullptr) |
static QImage | fromData (const uchar *data, int size, const char *format=nullptr) |
static QImage | fromData (const QByteArray &data, const char *format=nullptr) |
static QPixelFormat | toPixelFormat (QImage::Format format) noexcept |
static QImage::Format | toImageFormat (QPixelFormat format) noexcept |
Static Public Member Functions inherited from QPaintDevice | |
static qreal | devicePixelRatioFScale () |
Protected Member Functions | |
virtual int | metric (PaintDeviceMetric metric) const override |
QImage | mirrored_helper (bool horizontal, bool vertical) const |
QImage | rgbSwapped_helper () const |
void | mirrored_inplace (bool horizontal, bool vertical) |
void | rgbSwapped_inplace () |
QImage | convertToFormat_helper (Format format, Qt::ImageConversionFlags flags) const |
bool | convertToFormat_inplace (Format format, Qt::ImageConversionFlags flags) |
QImage | smoothScaled (int w, int h) const |
Protected Member Functions inherited from QPaintDevice | |
QPaintDevice () noexcept | |
virtual void | initPainter (QPainter *painter) const |
virtual QPaintDevice * | redirected (QPoint *offset) const |
virtual QPainter * | sharedPainter () const |
Friends | |
class | QRasterPlatformPixmap |
class | QBlittablePlatformPixmap |
class | QPixmapCacheEntry |
struct | QImageData |
Related Functions | |
(Note that these are not member functions.) | |
QImageCleanupFunction | |
QDataStream & | operator<< (QDataStream &stream, const QImage &image) |
QDataStream & | operator>> (QDataStream &stream, QImage &image) |
Additional Inherited Members | |
Protected Attributes inherited from QPaintDevice | |
ushort | painters |
The QImage class provides a hardware-independent image representation that allows direct access to the pixel data, and can be used as a paint device.
\inmodule QtGui
\reentrant
Qt provides four classes for handling image data: QImage, QPixmap, QBitmap and QPicture. QImage is designed and optimized for I/O, and for direct pixel access and manipulation, while QPixmap is designed and optimized for showing images on screen. QBitmap is only a convenience class that inherits QPixmap, ensuring a depth of 1. Finally, the QPicture class is a paint device that records and replays QPainter commands.
Because QImage is a QPaintDevice subclass, QPainter can be used to draw directly onto images. When using QPainter on a QImage, the painting can be performed in another thread than the current GUI thread.
The QImage class supports several image formats described by the \l Format enum. These include monochrome, 8-bit, 32-bit and alpha-blended images which are available in all versions of Qt 4.x.
QImage provides a collection of functions that can be used to obtain a variety of information about the image. There are also several functions that enables transformation of the image.
QImage objects can be passed around by value since the QImage class uses \l{Implicit Data Sharing}{implicit data sharing}. QImage objects can also be streamed and compared.
enum QImage::Format |
The following image formats are available in Qt. See the notes after the table.
\value Format_Invalid The image is invalid. \value Format_Mono The image is stored using 1-bit per pixel. Bytes are packed with the most significant bit (MSB) first. \value Format_MonoLSB The image is stored using 1-bit per pixel. Bytes are packed with the less significant bit (LSB) first.
\value Format_Indexed8 The image is stored using 8-bit indexes into a colormap.
\value Format_RGB32 The image is stored using a 32-bit RGB format (0xffRRGGBB).
\value Format_ARGB32 The image is stored using a 32-bit ARGB format (0xAARRGGBB).
\value Format_ARGB32_Premultiplied The image is stored using a premultiplied 32-bit ARGB format (0xAARRGGBB), i.e. the red, green, and blue channels are multiplied by the alpha component divided by 255. (If RR, GG, or BB has a higher value than the alpha channel, the results are undefined.) Certain operations (such as image composition using alpha blending) are faster using premultiplied ARGB32 than with plain ARGB32.
\value Format_RGB16 The image is stored using a 16-bit RGB format (5-6-5).
\value Format_ARGB8565_Premultiplied The image is stored using a premultiplied 24-bit ARGB format (8-5-6-5). \value Format_RGB666 The image is stored using a 24-bit RGB format (6-6-6). The unused most significant bits is always zero. \value Format_ARGB6666_Premultiplied The image is stored using a premultiplied 24-bit ARGB format (6-6-6-6). \value Format_RGB555 The image is stored using a 16-bit RGB format (5-5-5). The unused most significant bit is always zero. \value Format_ARGB8555_Premultiplied The image is stored using a premultiplied 24-bit ARGB format (8-5-5-5). \value Format_RGB888 The image is stored using a 24-bit RGB format (8-8-8). \value Format_RGB444 The image is stored using a 16-bit RGB format (4-4-4). The unused bits are always zero. \value Format_ARGB4444_Premultiplied The image is stored using a premultiplied 16-bit ARGB format (4-4-4-4). \value Format_RGBX8888 The image is stored using a 32-bit byte-ordered RGB(x) format (8-8-8-8). This is the same as the Format_RGBA8888 except alpha must always be 255. (added in Qt 5.2) \value Format_RGBA8888 The image is stored using a 32-bit byte-ordered RGBA format (8-8-8-8). Unlike ARGB32 this is a byte-ordered format, which means the 32bit encoding differs between big endian and little endian architectures, being respectively (0xRRGGBBAA) and (0xAABBGGRR). The order of the colors is the same on any architecture if read as bytes 0xRR,0xGG,0xBB,0xAA. (added in Qt 5.2) \value Format_RGBA8888_Premultiplied The image is stored using a premultiplied 32-bit byte-ordered RGBA format (8-8-8-8). (added in Qt 5.2) \value Format_BGR30 The image is stored using a 32-bit BGR format (x-10-10-10). (added in Qt 5.4) \value Format_A2BGR30_Premultiplied The image is stored using a 32-bit premultiplied ABGR format (2-10-10-10). (added in Qt 5.4) \value Format_RGB30 The image is stored using a 32-bit RGB format (x-10-10-10). (added in Qt 5.4) \value Format_A2RGB30_Premultiplied The image is stored using a 32-bit premultiplied ARGB format (2-10-10-10). (added in Qt 5.4) \value Format_Alpha8 The image is stored using an 8-bit alpha only format. (added in Qt 5.5) \value Format_Grayscale8 The image is stored using an 8-bit grayscale format. (added in Qt 5.5) \value Format_Grayscale16 The image is stored using an 16-bit grayscale format. (added in Qt 5.13) \value Format_RGBX64 The image is stored using a 64-bit halfword-ordered RGB(x) format (16-16-16-16). This is the same as the Format_RGBA64 except alpha must always be 65535. (added in Qt 5.12) \value Format_RGBA64 The image is stored using a 64-bit halfword-ordered RGBA format (16-16-16-16). (added in Qt 5.12) \value Format_RGBA64_Premultiplied The image is stored using a premultiplied 64-bit halfword-ordered RGBA format (16-16-16-16). (added in Qt 5.12) \value Format_BGR888 The image is stored using a 24-bit BGR format. (added in Qt 5.14) \value Format_RGBX16FPx4 The image is stored using a 4 16-bit halfword floating point RGBx format (16FP-16FP-16FP-16FP). This is the same as the Format_RGBA16FPx4 except alpha must always be 1.0. (added in Qt 6.2) \value Format_RGBA16FPx4 The image is stored using a 4 16-bit halfword floating point RGBA format (16FP-16FP-16FP-16FP). (added in Qt 6.2) \value Format_RGBA16FPx4_Premultiplied The image is stored using a premultiplied 4 16-bit halfword floating point RGBA format (16FP-16FP-16FP-16FP). (added in Qt 6.2) \value Format_RGBX32FPx4 The image is stored using a 4 32-bit floating point RGBx format (32FP-32FP-32FP-32FP). This is the same as the Format_RGBA32FPx4 except alpha must always be 1.0. (added in Qt 6.2) \value Format_RGBA32FPx4 The image is stored using a 4 32-bit floating point RGBA format (32FP-32FP-32FP-32FP). (added in Qt 6.2) \value Format_RGBA32FPx4_Premultiplied The image is stored using a premultiplied 4 32-bit floating point RGBA format (32FP-32FP-32FP-32FP). (added in Qt 6.2)
Format_RGB32
and Format_ARGB32_Premultiplied
formats, and secondarily for rendering to the Format_RGB16
, Format_RGBX8888
, Format_RGBA8888_Premultiplied
, Format_RGBX64
and Format_RGBA64_Premultiplied
formatsenum QImage::InvertMode |
This enum type is used to describe how pixel values should be inverted in the invertPixels() function.
\value InvertRgb Invert only the RGB values and leave the alpha channel unchanged.
\value InvertRgba Invert all channels, including the alpha channel.
Enumerator | |
---|---|
InvertRgb | |
InvertRgba |
|
noexcept |
Constructs a null image.
Definition at line 788 of file qimage.cpp.
Constructs an image with the given size and format.
A \l{isNull()}{null} image is returned if memory cannot be allocated.
Definition at line 818 of file qimage.cpp.
QImage::QImage | ( | int | width, |
int | height, | ||
Format | format | ||
) |
Constructs an image with the given width, height and format.
A \l{isNull()}{null} image will be returned if memory cannot be allocated.
Definition at line 804 of file qimage.cpp.
QImage::QImage | ( | uchar * | data, |
int | width, | ||
int | height, | ||
Format | format, | ||
QImageCleanupFunction | cleanupFunction = nullptr , |
||
void * | cleanupInfo = nullptr |
||
) |
Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.
The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.
If format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount() or setColorTable() before the image is used.
Definition at line 885 of file qimage.cpp.
QImage::QImage | ( | const uchar * | data, |
int | width, | ||
int | height, | ||
Format | format, | ||
QImageCleanupFunction | cleanupFunction = nullptr , |
||
void * | cleanupInfo = nullptr |
||
) |
Constructs an image with the given width, height and format, that uses an existing read-only memory buffer, data. The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.
The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.
If format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount() or setColorTable() before the image is used.
Unlike the similar QImage constructor that takes a non-const data buffer, this version will never alter the contents of the buffer. For example, calling QImage::bits() will return a deep copy of the image, rather than the buffer passed to the constructor. This allows for the efficiency of constructing a QImage from raw data, without the possibility of the raw data being changed.
Definition at line 916 of file qimage.cpp.
QImage::QImage | ( | uchar * | data, |
int | width, | ||
int | height, | ||
qsizetype | bytesPerLine, | ||
Format | format, | ||
QImageCleanupFunction | cleanupFunction = nullptr , |
||
void * | cleanupInfo = nullptr |
||
) |
Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels. bytesPerLine specifies the number of bytes per line (stride).
The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.
If format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount() or setColorTable() before the image is used.
Definition at line 940 of file qimage.cpp.
QImage::QImage | ( | const uchar * | data, |
int | width, | ||
int | height, | ||
qsizetype | bytesPerLine, | ||
Format | format, | ||
QImageCleanupFunction | cleanupFunction = nullptr , |
||
void * | cleanupInfo = nullptr |
||
) |
Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels. bytesPerLine specifies the number of bytes per line (stride).
The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.
If format is an indexed color format, the image color table is initially empty and must be sufficiently expanded with setColorCount() or setColorTable() before the image is used.
Unlike the similar QImage constructor that takes a non-const data buffer, this version will never alter the contents of the buffer. For example, calling QImage::bits() will return a deep copy of the image, rather than the buffer passed to the constructor. This allows for the efficiency of constructing a QImage from raw data, without the possibility of the raw data being changed.
Definition at line 971 of file qimage.cpp.
Constructs an image from the given xpm image.
Make sure that the image is a valid XPM image. Errors are silently ignored.
Note that it's possible to squeeze the XPM variable a little bit by using an unusual declaration:
The extra const
makes the entire definition read-only, which is slightly more efficient (e.g., when the code is in a shared library) and able to be stored in ROM with the application.
Definition at line 1023 of file qimage.cpp.
Constructs an image and tries to load the image from the file with the given fileName.
The loader attempts to read the image using the specified format. If the format is not specified (which is the default), it is auto-detected based on the file's suffix and header. For details, see {QImageReader::setAutoDetectImageFormat()}{QImageReader}.
If the loading of the image failed, this object is a null image.
The file name can either refer to an actual file on disk or to one of the application's embedded resources. See the \l{resources.html}{Resource System} overview for details on how to embed images and other resource files in the application's executable.
Definition at line 997 of file qimage.cpp.
Constructs a shallow copy of the given image.
For more information about shallow copies, see the \l {Implicit Data Sharing} documentation.
Definition at line 1044 of file qimage.cpp.
|
inlinenoexcept |
QImage::~QImage | ( | ) |
Destroys the image and cleans up.
Definition at line 1061 of file qimage.cpp.
bool QImage::allGray | ( | ) | const |
Returns true
if all the colors in the image are shades of gray (i.e. their red, green and blue components are equal); otherwise false.
Note that this function is slow for images without color table.
Definition at line 2827 of file qimage.cpp.
void QImage::applyColorTransform | ( | const QColorTransform & | transform | ) |
Applies the color transformation transform to all pixels in the image.
Definition at line 5013 of file qimage.cpp.
int QImage::bitPlaneCount | ( | ) | const |
The number of bit planes is the number of bits of color and transparency information for each pixel. This is different from (i.e. smaller than) the depth when the image format contains unused bits.
Definition at line 4562 of file qimage.cpp.
uchar * QImage::bits | ( | ) |
Returns a pointer to the first pixel data. This is equivalent to scanLine(0).
Note that QImage uses \l{Implicit Data Sharing} {implicit data sharing}. This function performs a deep copy of the shared pixel data, thus ensuring that this QImage is the only one using the current return value.
Definition at line 1675 of file qimage.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Note that QImage uses \l{Implicit Data Sharing} {implicit data sharing}, but this function does not perform a deep copy of the shared pixel data, because the returned data is const.
Definition at line 1695 of file qimage.cpp.
qsizetype QImage::bytesPerLine | ( | ) | const |
Returns the number of bytes per image scanline.
This is equivalent to sizeInBytes() / height() if height() is non-zero.
Definition at line 1536 of file qimage.cpp.
qint64 QImage::cacheKey | ( | ) | const |
Returns a number that identifies the contents of this QImage object. Distinct QImage objects can only have the same key if they refer to the same contents.
The key will change when the image is altered.
Definition at line 4457 of file qimage.cpp.
QRgb QImage::color | ( | int | i | ) | const |
Returns the color in the color table at index i. The first color is at index 0.
The colors in an image's color table are specified as ARGB quadruplets (QRgb). Use the qAlpha(), qRed(), qGreen(), and qBlue() functions to get the color value components.
Definition at line 1553 of file qimage.cpp.
int QImage::colorCount | ( | ) | const |
Returns the size of the color table for the image.
Notice that colorCount() returns 0 for 32-bpp images because these images do not use color tables, but instead encode pixel values as ARGB quadruplets.
Definition at line 1402 of file qimage.cpp.
QColorSpace QImage::colorSpace | ( | ) | const |
Returns the color space of the image if a color space is defined.
Definition at line 5001 of file qimage.cpp.
Returns a list of the colors contained in the image's color table, or an empty list if the image does not have a color table
Definition at line 1444 of file qimage.cpp.
Returns a pointer to the first pixel data.
Note that QImage uses \l{Implicit Data Sharing} {implicit data sharing}, but this function does not perform a deep copy of the shared pixel data, because the returned data is const.
Definition at line 1711 of file qimage.cpp.
Returns a pointer to the pixel data at the scanline with index i. The first scanline is at index 0.
The scanline data is as minimum 32-bit aligned. For 64-bit formats it follows the native alignment of 64-bit integers (64-bit for most platforms, but notably 32-bit on i386).
Note that QImage uses \l{Implicit Data Sharing} {implicit data sharing}, but this function does not perform a deep copy of the shared pixel data, because the returned data is const.
Definition at line 1655 of file qimage.cpp.
|
inline |
|
inline |
QImage QImage::convertedToColorSpace | ( | const QColorSpace & | colorSpace | ) | const |
Returns the image converted to colorSpace.
If the image has no valid color space, a null QImage is returned.
Definition at line 4987 of file qimage.cpp.
void QImage::convertTo | ( | Format | format, |
Qt::ImageConversionFlags | flags = Qt::AutoColor |
||
) |
Converts the image to the given format in place, detaching if necessary.
The specified image conversion flags control how the image data is handled during the conversion process.
Definition at line 2357 of file qimage.cpp.
void QImage::convertToColorSpace | ( | const QColorSpace & | colorSpace | ) |
Converts the image to colorSpace.
If the image has no valid color space, the method does nothing.
Definition at line 4962 of file qimage.cpp.
QImage QImage::convertToFormat | ( | Format | format, |
const QList< QRgb > & | colorTable, | ||
Qt::ImageConversionFlags | flags = Qt::AutoColor |
||
) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns a copy of the image converted to the given format, using the specified colorTable.
Conversion from RGB formats to indexed formats is a slow operation and will use a straightforward nearest color approach, with no dithering.
Definition at line 2287 of file qimage.cpp.
|
inline |
|
inline |
|
protected |
Definition at line 2152 of file qimage.cpp.
|
protected |
Definition at line 2199 of file qimage.cpp.
Returns a sub-area of the image as a new image.
The returned image is copied from the position ({rectangle}.x(), {rectangle}.y()) in this image, and will always have the size of the given rectangle.
In areas beyond this image, pixels are set to 0. For 32-bit RGB images, this means black; for 32-bit ARGB images, this means transparent black; for 8-bit images, this means the color with index 0 in the color table which can be anything; for 1-bit images, this means Qt::color0.
If the given rectangle is a null rectangle the entire image is copied.
Definition at line 1197 of file qimage.cpp.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
The returned image is copied from the position (x, y) in this image, and will always have the given width and height. In areas beyond this image, pixels are set to 0.
Definition at line 155 of file qimage.h.
QImage QImage::createAlphaMask | ( | Qt::ImageConversionFlags | flags = Qt::AutoColor | ) | const |
Builds and returns a 1-bpp mask from the alpha buffer in this image. Returns a null image if the image's format is QImage::Format_RGB32.
The flags argument is a bitwise-OR of the Qt::ImageConversionFlags, and controls the conversion process. Passing 0 for flags sets all the default options.
The returned image has little-endian bit order (i.e. the image's format is QImage::Format_MonoLSB), which you can convert to big-endian (QImage::Format_Mono) using the convertToFormat() function.
Definition at line 3068 of file qimage.cpp.
Creates and returns a 1-bpp heuristic mask for this image.
The function works by selecting a color from one of the corners, then chipping away pixels of that color starting at all the edges. The four corners vote for which color is to be masked away. In case of a draw (this generally means that this function is not applicable to the image), the result is arbitrary.
The returned image has little-endian bit order (i.e. the image's format is QImage::Format_MonoLSB), which you can convert to big-endian (QImage::Format_Mono) using the convertToFormat() function.
If clipTight is true (the default) the mask is just large enough to cover the pixels; otherwise, the mask is larger than the data pixels.
Note that this function disregards the alpha buffer.
Definition at line 3112 of file qimage.cpp.
QImage QImage::createMaskFromColor | ( | QRgb | color, |
Qt::MaskMode | mode = Qt::MaskInColor |
||
) | const |
Creates and returns a mask for this image based on the given color value. If the mode is MaskInColor (the default value), all pixels matching color will be opaque pixels in the mask. If mode is MaskOutColor, all pixels matching the given color will be transparent.
Definition at line 3216 of file qimage.cpp.
|
inline |
int QImage::depth | ( | ) | const |
Returns the depth of the image.
The image depth is the number of bits used to store a single pixel, also called bits per pixel (bpp).
The supported depths are 1, 8, 16, 24, 32 and 64.
Definition at line 1385 of file qimage.cpp.
void QImage::detach | ( | ) |
Definition at line 1126 of file qimage.cpp.
QSizeF QImage::deviceIndependentSize | ( | ) | const |
Returns the size of the pixmap in device independent pixels.
This value should be used when using the pixmap size in user interface size calculations.
The return value is equivalent to pixmap.size() / pixmap.devicePixelRatio(),
Definition at line 1509 of file qimage.cpp.
qreal QImage::devicePixelRatio | ( | ) | const |
Returns the device pixel ratio for the image. This is the ratio between {device pixels} and {device independent pixels}.
Use this function when calculating layout geometry based on the image size: QSize layoutSize = image.size() / image.devicePixelRatio()
The default value is 1.0.
Definition at line 1460 of file qimage.cpp.
|
overridevirtual |
Reimplemented from QPaintDevice.
Definition at line 1102 of file qimage.cpp.
int QImage::dotsPerMeterX | ( | ) | const |
Returns the number of pixels that fit horizontally in a physical meter. Together with dotsPerMeterY(), this number defines the intended scale and aspect ratio of the image.
Definition at line 4036 of file qimage.cpp.
int QImage::dotsPerMeterY | ( | ) | const |
Returns the number of pixels that fit vertically in a physical meter. Together with dotsPerMeterX(), this number defines the intended scale and aspect ratio of the image.
Definition at line 4049 of file qimage.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Fills the entire image with the given color.
If the depth of the image is 1, the image will be filled with 1 if color equals Qt::color1; it will otherwise be filled with 0.
If the depth of the image is 8, the image will be filled with the index corresponding the color in the color table if present; it will otherwise be filled with 0.
Definition at line 1846 of file qimage.cpp.
void QImage::fill | ( | Qt::GlobalColor | color | ) |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Fills the image with the given color, described as a standard global color.
Definition at line 1822 of file qimage.cpp.
Fills the entire image with the given pixelValue.
If the depth of this image is 1, only the lowest bit is used. If you say fill(0), fill(2), etc., the image is filled with 0s. If you say fill(1), fill(3), etc., the image is filled with 1s. If the depth is 8, the lowest 8 bits are used and if the depth is 16 the lowest 16 bits are used.
If the image depth is higher than 32bit the result is undefined.
Definition at line 1736 of file qimage.cpp.
QImage::Format QImage::format | ( | ) | const |
Returns the format of the image.
Definition at line 2119 of file qimage.cpp.
|
inlinestatic |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Constructs a QImage from the given QByteArray data.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Constructs a QImage from the first size bytes of the given binary data.
Definition at line 3813 of file qimage.cpp.
|
static |
Constructs an image from the given QByteArrayView data. The loader attempts to read the image using the specified format. If format is not specified (which is the default), the loader probes the data for a header to guess the file format.
If format is specified, it must be one of the values returned by QImageReader::supportedImageFormats().
If the loading of the image fails, the image returned will be a null image.
Definition at line 3796 of file qimage.cpp.
bool QImage::hasAlphaChannel | ( | ) | const |
Returns true
if the image has a format that respects the alpha channel, otherwise returns false
.
Definition at line 4539 of file qimage.cpp.
int QImage::height | ( | ) | const |
Returns the height of the image.
Definition at line 1343 of file qimage.cpp.
void QImage::invertPixels | ( | InvertMode | mode = InvertRgb | ) |
Inverts all pixel values in the image.
The given invert mode only have a meaning when the image's depth is 32. The default mode is InvertRgb, which leaves the alpha channel unchanged. If the mode is InvertRgba, the alpha bits are also inverted.
Inverting an 8-bit image means to replace all pixels using color index i with a pixel using color index 255 minus i. The same is the case for a 1-bit image. Note that the color table is not changed.
If the image has a premultiplied alpha channel, the image is first converted to an unpremultiplied image format to be inverted and then converted back.
Definition at line 1943 of file qimage.cpp.
bool QImage::isDetached | ( | ) | const |
Definition at line 4473 of file qimage.cpp.
bool QImage::isGrayscale | ( | ) | const |
For 32-bit images, this function is equivalent to allGray().
For color indexed images, this function returns true
if color(i) is QRgb(i, i, i) for all indexes of the color table; otherwise returns false
.
Definition at line 2903 of file qimage.cpp.
bool QImage::isNull | ( | ) | const |
Returns true
if it is a null image, otherwise returns false
.
A null image has all parameters set to zero and no allocated data.
Definition at line 1319 of file qimage.cpp.
Loads an image from the file with the given fileName. Returns true
if the image was successfully loaded; otherwise invalidates the image and returns false
.
The loader attempts to read the image using the specified format, e.g., PNG or JPG. If format is not specified (which is the default), it is auto-detected based on the file's suffix and header. For details, see QImageReader::setAutoDetectImageFormat().
The file name can either refer to an actual file on disk or to one of the application's embedded resources. See the \l{resources.html}{Resource System} overview for details on how to embed images and other resource files in the application's executable.
Definition at line 3722 of file qimage.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This function reads a QImage from the given device. This can, for example, be used to load an image directly into a QByteArray.
Definition at line 3735 of file qimage.cpp.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Loads an image from the given QByteArray data.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Loads an image from the first len bytes of the given binary data.
Definition at line 3768 of file qimage.cpp.
bool QImage::loadFromData | ( | QByteArrayView | data, |
const char * | format = nullptr |
||
) |
Loads an image from the given QByteArrayView data. Returns true
if the image was successfully loaded; otherwise invalidates the image and returns false
.
The loader attempts to read the image using the specified format, e.g., PNG or JPG. If format is not specified (which is the default), the loader probes the file for a header to guess the file format.
Definition at line 3754 of file qimage.cpp.
|
overrideprotectedvirtual |
Reimplemented from QPaintDevice.
Definition at line 4228 of file qimage.cpp.
Mirrors of the image in the horizontal and/or the vertical direction depending on whether horizontal and vertical are set to true or false.
|
protected |
|
protected |
QPoint QImage::offset | ( | ) | const |
Returns the number of pixels by which the image is intended to be offset by when positioning relative to other images.
Definition at line 4106 of file qimage.cpp.
QImage::operator QVariant | ( | ) | const |
Returns the image as a QVariant.
Definition at line 1110 of file qimage.cpp.
bool QImage::operator!= | ( | const QImage & | image | ) | const |
Returns true
if this image and the given image have different contents; otherwise returns false
.
The comparison can be slow, unless there is some obvious difference, such as different widths, in which case the function will return quickly.
Definition at line 4020 of file qimage.cpp.
Move-assigns other to this QImage instance.
Assigns a shallow copy of the given image to this image and returns a reference to this image.
For more information about shallow copies, see the \l {Implicit Data Sharing} documentation.
Definition at line 1077 of file qimage.cpp.
Returns true
if this image and the given image have the same contents; otherwise returns false
.
The comparison can be slow, unless there is some obvious difference (e.g. different size or format), in which case the function will return quickly.
Definition at line 3955 of file qimage.cpp.
|
overridevirtual |
Implements QPaintDevice.
Definition at line 4205 of file qimage.cpp.
Returns the color of the pixel at the given position.
If the position is not valid, the results are undefined.
Definition at line 350 of file qimage.h.
QRgb QImage::pixel | ( | int | x, |
int | y | ||
) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns the color of the pixel at coordinates (x, y).
Definition at line 2451 of file qimage.cpp.
Returns the color of the pixel at the given position as a QColor.
If the position is not valid, an invalid QColor is returned.
Definition at line 352 of file qimage.h.
QColor QImage::pixelColor | ( | int | x, |
int | y | ||
) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns the color of the pixel at coordinates (x, y) as a QColor.
Definition at line 2664 of file qimage.cpp.
|
noexcept |
Returns the QImage::Format as a QPixelFormat
Definition at line 5626 of file qimage.cpp.
int QImage::pixelIndex | ( | int | x, |
int | y | ||
) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns the pixel index at (x, y).
Definition at line 2410 of file qimage.cpp.
QRect QImage::rect | ( | ) | const |
Returns the enclosing rectangle (0, 0, width(), height()) of the image.
Definition at line 1368 of file qimage.cpp.
bool QImage::reinterpretAsFormat | ( | Format | format | ) |
Changes the format of the image to format without changing the data. Only works between formats of the same depth.
Returns true
if successful.
This function can be used to change images with alpha-channels to their corresponding opaque formats if the data is known to be opaque-only, or to change the format of a given image buffer before overwriting it with new data.
true
if the depths are compatible. Operations on an image with invalid data are undefined.Definition at line 2323 of file qimage.cpp.
|
inline |
Swaps the values of the red and blue components of all pixels, effectively converting an RGB image to an BGR image.
|
inline |
|
protected |
|
protected |
bool QImage::save | ( | const QString & | fileName, |
const char * | format = nullptr , |
||
int | quality = -1 |
||
) | const |
Saves the image to the file with the given fileName, using the given image file format and quality factor. If format is \nullptr, QImage will attempt to guess the format by looking at fileName's suffix.
The quality factor must be in the range 0 to 100 or -1. Specify 0 to obtain small compressed files, 100 for large uncompressed files, and -1 (the default) to use the default settings.
Returns true
if the image was successfully saved; otherwise returns false
.
Definition at line 3843 of file qimage.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
This function writes a QImage to the given device.
This can, for example, be used to save an image directly into a QByteArray:
Definition at line 3862 of file qimage.cpp.
QImage QImage::scaled | ( | const QSize & | size, |
Qt::AspectRatioMode | aspectRatioMode = Qt::IgnoreAspectRatio , |
||
Qt::TransformationMode | transformMode = Qt::FastTransformation |
||
) | const |
Returns a copy of the image scaled to a rectangle defined by the given size according to the given aspectRatioMode and transformMode.
\list
If the given size is empty, this function returns a null image.
Definition at line 2968 of file qimage.cpp.
|
inline |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns a copy of the image scaled to a rectangle with the given width and height according to the given aspectRatioMode and transformMode.
If either the width or the height is zero or negative, this function returns a null image.
Definition at line 244 of file qimage.h.
QImage QImage::scaledToHeight | ( | int | height, |
Qt::TransformationMode | mode = Qt::FastTransformation |
||
) | const |
Returns a scaled copy of the image. The returned image is scaled to the given height using the specified transformation mode.
This function automatically calculates the width of the image so that the ratio of the image is preserved.
If the given height is 0 or negative, a null image is returned.
Definition at line 3035 of file qimage.cpp.
QImage QImage::scaledToWidth | ( | int | width, |
Qt::TransformationMode | mode = Qt::FastTransformation |
||
) | const |
[9]
[10]
Returns a scaled copy of the image. The returned image is scaled to the given width using the specified transformation mode.
This function automatically calculates the height of the image so that its aspect ratio is preserved.
If the given width is 0 or negative, a null image is returned.
Definition at line 3005 of file qimage.cpp.
uchar * QImage::scanLine | ( | int | i | ) |
Returns a pointer to the pixel data at the scanline with index i. The first scanline is at index 0.
The scanline data is as minimum 32-bit aligned. For 64-bit formats it follows the native alignment of 64-bit integers (64-bit for most platforms, but notably 32-bit on i386).
For example, to remove the green component of each pixel in an image:
{QRgb*} (QRgb has a 32-bit size) and use it to read/write the pixel value. You cannot use the
{uchar*} pointer directly, because the pixel format depends on the byte order on the underlying platform. Use qRed(), qGreen(), qBlue(), and qAlpha() to access the pixels.Definition at line 1613 of file qimage.cpp.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Definition at line 1630 of file qimage.cpp.
Sets the alpha channel of this image to the given alphaChannel.
If alphaChannel is an 8 bit alpha image, the alpha values are used directly. Otherwise, alphaChannel is converted to 8 bit grayscale and the intensity of the pixel values is used.
If the image already has an alpha channel, the existing alpha channel is multiplied with the new one. If the image doesn't have an alpha channel it will be converted to a format that does.
The operation is similar to painting alphaChannel as an alpha image over this image using QPainter::CompositionMode_DestinationIn
.
Definition at line 4498 of file qimage.cpp.
Sets the color at the given index in the color table, to the given to colorValue. The color value is an ARGB quadruplet.
If index is outside the current size of the color table, it is expanded with setColorCount().
Definition at line 1571 of file qimage.cpp.
void QImage::setColorCount | ( | int | colorCount | ) |
If the color table is expanded, all the extra colors will be set to transparent (i.e qRgba(0, 0, 0, 0)).
When the image is used, the color table must be large enough to have entries for all the pixel/index values present in the image, otherwise the results are undefined.
Definition at line 2089 of file qimage.cpp.
void QImage::setColorSpace | ( | const QColorSpace & | colorSpace | ) |
Sets the image color space to colorSpace without performing any conversions on image data.
Definition at line 4942 of file qimage.cpp.
Sets the color table used to translate color indexes to QRgb values, to the specified colors.
When the image is used, the color table must be large enough to have entries for all the pixel/index values present in the image, otherwise the results are undefined.
Definition at line 1418 of file qimage.cpp.
Sets the device pixel ratio for the image. This is the ratio between image pixels and device-independent pixels.
The default scaleFactor is 1.0. Setting it to something else has two effects:
QPainters that are opened on the image will be scaled. For example, painting on a 200x200 image if with a ratio of 2.0 will result in effective (device-independent) painting bounds of 100x100.
Code paths in Qt that calculate layout geometry based on the image size will take the ratio into account: QSize layoutSize = image.size() / image.devicePixelRatio() The net effect of this is that the image is displayed as high-DPI image rather than a large image (see \l{Drawing High Resolution Versions of Pixmaps and Images}).
Definition at line 1488 of file qimage.cpp.
void QImage::setDotsPerMeterX | ( | int | x | ) |
Sets the number of pixels that fit horizontally in a physical meter, to x.
Together with dotsPerMeterY(), this number defines the intended scale and aspect ratio of the image, and determines the scale at which QPainter will draw graphics on the image. It does not change the scale or aspect ratio of the image when it is rendered on other paint devices.
Definition at line 4066 of file qimage.cpp.
void QImage::setDotsPerMeterY | ( | int | y | ) |
Sets the number of pixels that fit vertically in a physical meter, to y.
Together with dotsPerMeterX(), this number defines the intended scale and aspect ratio of the image, and determines the scale at which QPainter will draw graphics on the image. It does not change the scale or aspect ratio of the image when it is rendered on other paint devices.
Definition at line 4088 of file qimage.cpp.
Sets the number of pixels by which the image is intended to be offset by when positioning relative to other images, to offset.
Definition at line 4120 of file qimage.cpp.
Sets the pixel index or color at the given position to index_or_rgb.
If the image's format is either monochrome or paletted, the given index_or_rgb value must be an index in the image's color table, otherwise the parameter must be a QRgb value.
If position is not a valid coordinate pair in the image, or if index_or_rgb >= colorCount() in the case of monochrome and paletted images, the result is undefined.
{detach()} function called within; if performance is a concern, we recommend the use of scanLine() or bits() to access pixel data directly.Definition at line 351 of file qimage.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Sets the pixel index or color at (x, y) to index_or_rgb.
Definition at line 2546 of file qimage.cpp.
Sets the color at the given position to color.
If position is not a valid coordinate pair in the image, or the image's format is either monochrome or paletted, the result is undefined.
{detach()} function called within; if performance is a concern, we recommend the use of scanLine() or bits() to access pixel data directly.Definition at line 353 of file qimage.h.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Sets the pixel color at (x, y) to color.
Definition at line 2743 of file qimage.cpp.
Sets the image text to the given text and associate it with the given key.
If you just want to store a single text block (i.e., a "comment" or just a description), you can either pass an empty key, or use a generic key like "Description".
The image text is embedded into the image data when you call save() or QImageWriter::write().
Not all image formats support embedded text. You can find out if a specific image or format supports embedding text by using QImageWriter::supportsOption(). We give an example:
You can use QImageWriter::supportedImageFormats() to find out which image formats are available to you.
Definition at line 4190 of file qimage.cpp.
QSize QImage::size | ( | ) | const |
Returns the size of the image, i.e. its width() and height().
Definition at line 1355 of file qimage.cpp.
qsizetype QImage::sizeInBytes | ( | ) | const |
Definition at line 1524 of file qimage.cpp.
|
protected |
Returns the image text associated with the given key. If the specified key is an empty string, the whole image text is returned, with each key-text pair separated by a newline.
Definition at line 4150 of file qimage.cpp.
QStringList QImage::textKeys | ( | ) | const |
Returns the text keys for this image.
You can use these keys with text() to list the image text for a certain key.
Definition at line 4138 of file qimage.cpp.
|
staticnoexcept |
Converts format into a QImage::Format
Definition at line 5643 of file qimage.cpp.
|
staticnoexcept |
Converts format into a QPixelFormat
Definition at line 5634 of file qimage.cpp.
QImage QImage::transformed | ( | const QTransform & | matrix, |
Qt::TransformationMode | mode = Qt::FastTransformation |
||
) | const |
Returns a copy of the image that is transformed using the given transformation matrix and transformation mode.
The returned image will normally have the same {Image Formats}{format} as the original image. However, a complex transformation may result in an image where not all pixels are covered by the transformed pixels of the original image. In such cases, those background pixels will be assigned a transparent color value, and the transformed image will be given a format with an alpha channel, even if the original image did not have that.
The transformation matrix is internally adjusted to compensate for unwanted translation; i.e. the image produced is the smallest image that contains all the transformed points of the original image. Use the trueMatrix() function to retrieve the actual matrix used for transforming an image.
Unlike the other overload, this function can be used to perform perspective transformations on images.
Definition at line 4747 of file qimage.cpp.
|
static |
Returns the actual matrix used for transforming an image with the given width, height and matrix.
When transforming an image using the transformed() function, the transformation matrix is internally adjusted to compensate for unwanted translation, i.e. transformed() returns the smallest image containing all transformed points of the original image. This function returns the modified matrix, which maps points correctly from the original image into the new image.
Unlike the other overload, this function creates transformation matrices that can be used to perform perspective transformations on images.
Definition at line 4927 of file qimage.cpp.
Returns true
if pos is a valid coordinate pair within the image; otherwise returns false
.
bool QImage::valid | ( | int | x, |
int | y | ||
) | const |
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns true
if QPoint(x, y) is a valid coordinate pair within the image; otherwise returns false
.
Definition at line 2387 of file qimage.cpp.
int QImage::width | ( | ) | const |
Returns the width of the image.
Definition at line 1331 of file qimage.cpp.
|
related |
Writes the given image to the given stream as a PNG image, or as a BMP image if the stream's version is 1. Note that writing the stream to a file will not produce a valid image file.
|
related |
Reads an image from the given stream and stores it in the given image.
|
friend |
|
related |
A function with the following signature that can be used to implement basic image memory management:
|
friend |
|
friend |
|
friend |