Home · All Classes · Main Classes · Grouped Classes · Modules · Functions |
The QImage class provides a hardware-independent pixmap that allows direct access to the pixel data, and can be used as a paint device. More...
#include <QImage>
Inherits QPaintDevice.
The QImage class provides a hardware-independent pixmap that allows direct access to the pixel data, and can be used as a paint device.
Qt provides two classes for handling image data: QImage and QPixmap. QImage is designed and optimized for I/O, and for direct pixel access/manipulation. QPixmap is designed and optimized for drawing. There are functions to convert between QImage and QPixmap: QPixmap::convertToImage() and QPixmap::convertFromImage().
QImage supports a number of formats. These include monochrome images, 8-bit images, and 32-bit images with an optional alpha channel. Monochrome and 8-bit images are index-based and use a color lookup table, while 32-bit images use RGB or ARGB values.
An entry in the color table is an RGB triplet encoded as an qRgb value. Use the color() function to obtain an entry from the table, and the qRed(), qGreen(), and qBlue() functions (qcolor.h) to access the components. The qRgb() function is used to make an RGB triplet suitable for use with the setColor() function.
Monochrome images have a color table with at most two colors. There are two different types of monochrome images: big endian (MSB first) or little endian (LSB first) bit order. To access a single bit you must do some bit shifts:
QImage image; // sets bit at (x, y) to 1 if (image.format() == QImage::Format_MonoLSB) image.scanLine(y)[x >> 3] |= 1 << (x & 7); else image.scanLine(y)[x >> 3] |= 1 << (7 - (x & 7));
If this looks complicated, you can convert the monochrome image to an 8-bit indexed image using convertToFormat(). 8-bit images are much easier to work with than 1-bit images because they have a single byte per pixel:
QImage image; // set entry 19 in the color table to yellow image.setColor(19, qRgb(255, 255, 0)); // set 8 bit pixel at (x,y) to value yellow (in color table) image.scanLine(y)[x] = 19;
32-bit images have no color table; instead, each pixel contains an ARGB value. 24 bits contain the RGB value; the most significant byte is reserved for the alpha buffer.
QImage image; // sets 32 bit pixel at (x,y) to yellow. uint *ptr = reinterpret_cast<uint *>(image.scanLine(y)) + x; *ptr = qRgb(255, 255, 0);
In Qtopia Core, scanlines are aligned to the pixel depth and may be padded to any degree, while on all other platforms, the scanlines are 32-bit aligned for all depths. The constructor taking a uchar* argument always expects 32-bit aligned data. In Qtopia Core, an additional constructor allows the number of bytes-per-line to be specified.
Pixel colors are retrieved with pixel() and set with setPixel().
QImage supports a variety of functions that can be used to obtain information about the image. width(), height(), dotsPerMeterX(), and dotsPerMeterY() provide information about the image size and resolution. The depth(), numColors(), isGrayscale(), and colorTable() functions provide information about the color depth and available color components used to store the image data.
It is possible to determine whether a color image can be safely converted to a grayscale image by using the allGray() function. The format(), bytesPerLine(), and numBytes() functions provide low-level information about the data stored in the image.
QImage also supports a number of functions for creating a new image that is a transformed version of the original. For example, convertToFormat(), createAlphaMask(), createHeuristicMask(), mirrored(), scaled(), rgbSwapped() and transformed(). There are also functions for changing attributes of an image in-place, for example, setAlphaChannel(), setColor(), setDotsPerMeterX() and setDotsPerMeterY() and setNumColors().
Images can be loaded and saved in the supported file formats. Images are saved to a file with save(). Images are loaded from a file with load() (or in the constructor) or from an array of data with loadFromData(). The lists of supported formats are available from QImageReader::supportedImageFormats() and QImageWriter::supportedImageFormats(). By default, Qt supports the following formats:
Format | Description | Qt's support |
---|---|---|
BMP | Windows Bitmap | Read/write |
GIF | Graphic Interchange Format (optional) | Read |
JPG | Joint Photographic Experts Group | Read/write |
JPEG | Joint Photographic Experts Group | Read/write |
PNG | Portable Network Graphics | Read/write |
PBM | Portable Bitmap | Read |
PGM | Portable Graymap | Read |
PPM | Portable Pixmap | Read/write |
XBM | X11 Bitmap | Read/write |
XPM | X11 Pixmap | Read/write |
(To configure Qt with GIF support, pass -qt-gif to the configure script or check the appropriate option in the graphical installer.)
When loading an image, the file name can be either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.
Strings of text may be added to images using setText().
The QImage class uses implicit sharing, so you can pass QImage objects around by value.
New image formats can be added as plugins.
See also QImageReader, QImageWriter, QPixmap, QColor, and Shared Classes.
This enum type is used to describe the endianness of the CPU and graphics hardware. It is provided here for compatibility with earlier versions of Qt.
Constant | Value | Description |
---|---|---|
QImage::IgnoreEndian | 2 | Endianness does not matter. Useful for some operations that are independent of endianness. |
QImage::BigEndian | 0 | Most significant bit first or network byte order, as on SPARC, PowerPC, and Motorola CPUs. |
QImage::LittleEndian | 1 | Least significant bit first or little endian byte order, as on Intel x86. |
The following image formats are available in all versions of Qt:
Constant | Value | Description |
---|---|---|
QImage::Format_Invalid | 0 | The image is invalid. |
QImage::Format_Mono | 1 | The image is stored using 1-bit per pixel. Bytes are packed with the most significant bit (MSB) first. |
QImage::Format_MonoLSB | 2 | The image is stored using 1-bit per pixel. Bytes are packed with the less significant bit (LSB) first. |
QImage::Format_Indexed8 | 3 | The image is stored using 8-bit indexes into a colormap. Note that you will also need to call setNumColors() with a value of 256 before setting any of the colors in the index. |
QImage::Format_RGB32 | 4 | The image is stored using a 32-bit RGB format (0xffRRGGBB). |
QImage::Format_ARGB32 | 5 | The image is stored using a 32-bit ARGB format (0xAARRGGBB). |
QImage::Format_ARGB32_Premultiplied | 6 | 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.) |
The following image formats are specific to Qtopia Core:
Constant | Value | Description |
---|---|---|
QImage::Format_RGB16 | 7 | The image is stored using a 16-bit RGB format. |
QImage::Format_RGB15 | 8 | The image is stored using a 15-bit RGB format. |
QImage::Format_Grayscale16 | 9 | The image is stored with 65536 levels of gray (16 bits per pixel). |
QImage::Format_Grayscale8 | 10 | The image is stored with 256 levels of gray (8 bits per pixel). |
QImage::Format_Grayscale4 | 11 | The image is stored with 16 levels of gray (4 bits per pixel). |
QImage::Format_Grayscale4LSB | 12 | The image is stored with 16 levels of gray, stored in a form where the lowest 4 bits in a byte contain the first pixel in each pair. |
QImage::Format_Grayscale2 | 13 | The image is stored with 4 levels of gray (2 bits per pixel). |
QImage::Format_Grayscale2LSB | 14 | The image is stored with 4 levels of gray, stored in a form where the least significant bits contain the first pixel value and the most significant bits contain the last pixel value in each byte containing a group of 4 pixels. |
See also format() and convertToFormat().
This enum type is used to describe how pixel values should be inverted in the invertPixels() function.
Constant | Value | Description |
---|---|---|
QImage::InvertRgb | 0 | Invert only the RGB values and leave the alpha channel unchanged. |
QImage::InvertRgba | 1 | Invert all channels, including the alpha channel. |
Constructs a null image.
See also isNull().
Constructs an image with size in format format.
Constructs an image with width, height in format format.
Constructs an image width pixels wide, height pixels high with a format of format, that uses an existing memory buffer, data. The buffer must remain valid throughout the life of the QImage. The image does not delete the buffer at destruction.
Note that data must be 32-bit aligned.
If the image is in an indexed color format, set the color table for the image using setColorTable().
Constructs an image from xpm, which must be 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:
static const char * const start_xpm[] = { "16 15 8 1", "a c #cec6bd", ....
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 ROMable when the application is to be stored in ROM.
Constructs an image and tries to load the image from the file fileName.
If format is specified, the loader attempts to read the image using the specified format. If format is not specified (which is the default), the loader probes the file for a header to guess the file format.
If the loading of the image failed, this object is a null image.
The QImageReader documentation lists the supported image formats and explains how to add extra formats.
The file name can be either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.
See also load(), isNull(), and QImageReader.
Constructs a shallow copy of image.
Constructs an image and tries to load the image from the file fileName.
If format is specified, the loader attempts to read the image using the specified format. If format is not specified (which is the default), the loader probes the files for a header to guess the file format.
If the loading of the image failed, this object is a null image.
The QImageReader documentation lists the supported image formats and explains how to add extra formats.
The file name can be either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.
You can disable this constructor by defining QT_NO_CAST_FROM_ASCII when you compile your applications. This can be useful if you want to ensure that all user-visible strings go through QObject::tr(), for example.
See also QString::fromAscii(), load(), isNull(), and QImageReader.
Destroys the image and cleans up.
Returns true if all the colors in the image are shades of gray (i.e. their red, green and blue components are equal); otherwise returns false.
This function is slow for large 32-bit images.
See also isGrayscale().
Extracts the alpha channel from this image as an 8 bit gray scale image and returns it.
See also setAlphaChannel().
Returns a pointer to the first pixel data. This is equivalent to scanLine(0).
See also numBytes(), scanLine(), and jumpTable().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns the number of bytes per image scanline. This is equivalent to numBytes()/height().
See also numBytes() and scanLine().
Returns the color in the color table at index i. The first color is at index 0.
A color value is an RGB triplet. Use the qRed(), qGreen(), and qBlue() functions to get the color value components.
See also setColor(), numColors(), and QColor.
Returns the color table of the image, or an empty list of the image does not have a color table
See also setColorTable() and numColors().
Returns a copy of the image in the given format.
The image conversion flags specified by flags control how the image data is handled during the conversion process.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns a copy of the image converted to the given format, using a color table specified by colorTable.
The image conversion flags specified by flags control how the image data is handled during the conversion process.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns a sub-area of the image.
The returned image is always w by h pixels in size, and is copied from position x, y in this image. In areas beyond this image pixels are filled with pixel 0.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns a sub-area of the image.
The returned image always has the size of the rectangle r. In areas beyond this image pixels are filled with pixel 0.
If r is a null rectangle the entire image is copied.
Builds and returns a 1-bpp mask from the alpha buffer in this image. Returns a null image if the image is of format RGB32.
See QPixmap::convertFromImage() for a description of the flags argument.
The returned image has little-endian bit order, which you can convert to big-endian using convertToFormat().
See also createHeuristicMask().
Creates and returns a 1-bpp heuristic mask for this image. It 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, which you can convert to big-endian using convertToFormat().
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.
This function disregards the alpha buffer.
See also createAlphaMask().
Returns the depth of the image.
The image depth is the number of bits used to encode a single pixel, also called bits per pixel (bpp) or bit planes of an image.
The supported depths are 1, 8 and 32.
See also convertToFormat().
Detaches from shared image d and makes sure that this image is the only one referring to the data.
If multiple images share common data, this image makes a copy of the data and detaches itself from the sharing mechanism. Nothing is done if there is just a single reference.
See also copy().
Returns the number of pixels that fit horizontally in a physical meter. This and dotsPerMeterY() define the intended scale and aspect ratio of the image.
See also setDotsPerMeterX().
Returns the number of pixels that fit vertically in a physical meter. This and dotsPerMeterX() define the intended scale and aspect ratio of the image.
See also setDotsPerMeterY().
Fills the entire image with the pixel value pixel.
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.
Note: QImage::pixel() returns the color of the pixel at the given coordinates; QColor::pixel() returns the pixel value of the underlying window system (essentially an index value), so normally you will want to use QImage::pixel() to use a color from an existing image or QColor::rgb() to use a specific color.
See also invertPixels(), depth(), hasAlphaBuffer(), and create().
returns the format of the image.
Constructs a QImage from the first size bytes of binary data in data. If the loading of the image failed, this object is a null image.
If format is specified, the loader attempts to read the image using the specified format. If format is not specified (which is the default), the loader probes the file for a header to guess the file format.
The QImageReader documentation lists the supported image formats and explains how to add extra formats.
See also load(), save(), QImageReader::imageFormat(), QPixmap::loadFromData(), and QImageReader.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Loads an image from the QByteArray data.
Returns true if the image has an alpha channel.
Returns the height of the image.
See also width(), size(), and rect().
Inverts all pixel values in the image.
The default mode is InvertRgb, which leaves the alpha channel unchanged.
If the depth is 32 and the mode is InvertRgba, the alpha bits are also inverted, otherwise they are left unchanged. If the depth is not 32, the argument mode has no meaning.
Note that inverting an 8-bit image means to replace all pixels using color index i with a pixel using color index 255 minus i. Similarly for a 1-bit image. The color table is not changed.
See also fill(), depth(), and hasAlphaBuffer().
Returns true if the image is detached; otherwise returns false.
See also detach().
For 32-bit images, this function is equivalent to allGray().
For 8-bpp images, this function returns true if color(i) is QRgb(i,i,i) for all indexes of the color table; otherwise returns false.
See also allGray() and depth().
Returns true if it is a null image; otherwise returns false.
A null image has all parameters set to zero and no allocated data.
Loads an image from the file fileName. Returns true if the image was successfully loaded; otherwise returns false.
If format is specified, the loader attempts to read the image using the specified format. If format is not specified (which is the default), the loader probes the file for a header to guess the file format.
The QImageReader documentation lists the supported image formats and explains how to add extra formats.
The file name can either refer to an actual file on disk or to one of the application's embedded resources. See the Resource System overview for details on how to embed images and other resource files in the application's executable.
See also loadFromData(), save(), QImageReader::imageFormat(), QPixmap::load(), and QImageReader.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
This function reads a QImage from the QIODevice, device. This can be used, for example, to load an image directly into a QByteArray.
Loads an image from the first len bytes of binary data in data. Returns true if the image was successfully loaded; otherwise returns false.
If format is specified, the loader attempts to read the image using the specified format. If format is not specified (which is the default), the loader probes the file for a header to guess the file format.
The QImageReader documentation lists the supported image formats and explains how to add extra formats.
See also load(), save(), QImageReader::imageFormat(), QPixmap::loadFromData(), and QImageReader.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Loads an image from the QByteArray data.
Returns the size for the specified metric on the device.
Returns a mirror of the image, mirrored in the horizontal and/or the vertical direction depending on whether horizontal and vertical are set to true or false. The original image is not changed.
See also scaled().
Returns the number of bytes occupied by the image data.
See also bytesPerLine() and bits().
Returns the size of the color table for the image.
Notice that numColors() returns 0 for 32-bpp images because these images do not use color tables, but instead encode pixel values as RGB triplets.
See also setNumColors() and colorTable().
Returns the number of pixels by which the image is intended to be offset by when positioning relative to other images.
See also setOffset().
Returns the color of the pixel at the coordinates (x, y).
If (x, y) is not on the image, the results are undefined.
See also setPixel(), qRed(), qGreen(), qBlue(), and valid().
Returns the pixel index at the given coordinates.
If (x, y) is not valid, or if the image is not a paletted image (depth() > 8), the results are undefined.
Returns the enclosing rectangle (0, 0, width(), height()) of the image.
See also width(), height(), and size().
Returns a QImage in which the values of the red and blue components of all pixels have been swapped, effectively converting an RGB image to a BGR image. The original QImage is not changed.
Saves the image to the file fileName, using the image file format format and a quality factor of quality. quality 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.
See also load(), loadFromData(), QImageReader::imageFormat(), QPixmap::save(), and QImageReader.
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
This function writes a QImage to the QIODevice, device. This can be used, for example, to save an image directly into a QByteArray:
QImage image; QByteArray ba; QBuffer buffer(&ba); buffer.open(QIODevice::WriteOnly); image.save(&buffer, "PNG"); // writes image into ba in PNG format
Returns a copy of the image scaled to a rectangle of width w and height h according to aspectRatioMode and transformMode.
If either the width w or the height h is zero or negative, this function returns a null image.
See also scaledToWidth(), scaledToHeight(), and transformed().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Scales the image to the given size, using the aspect ratio and transformation modes specified by aspectMode and transformMode.
Returns a scaled copy of the image with a height of h pixels using a transformation specified by mode. This function automatically calculates the width of the image so that the ratio of the image is preserved.
If h is 0 or negative a null image is returned.
See also scaled(), scaledToWidth(), and transformed().
Returns a scaled copy of the image with a width of w pixels using the specified transformation mode. This function automatically calculates the height of the image so that its aspect ratio is preserved.
If w is 0 or negative a null image is returned.
See also scaled(), scaledToHeight(), and transformed().
Returns a pointer to the pixel data at the scanline with index i. The first scanline is at index 0.
The scanline data is aligned on a 32-bit boundary.
Warning: If you are accessing 32-bpp image data, cast the returned pointer to 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.
See also bytesPerLine(), bits(), and jumpTable().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Returns a number that uniquely identifies the contents of this QImage object. This means that multiple QImage objects can have the same serial number as long as they refer to the same contents.
A null image always have a serial number of 0.
An example of where this is useful is for caching QImages.
Sets the alpha channel of this image to alphaChannel.
If alphaChannel is an 8 bit grayscale image, the intensity values are written into this buffer directly. Otherwise, alphaChannel is converted to 32 bit and the intensity of the RGB pixel values is used.
The image will be converted to the format Format_ARGB32_Premultiplied if the function succeeds.
See also alphaChannel().
Sets a color in the color table at index i to c.
A color value is an RGB triplet. Use the qRed(), qGreen(), and qBlue() functions to get the color value components.
Note that, for images in Format_Indexed8 format, you will first need to call setNumColors() with a value of 256 before using this function.
See also color(), setNumColors(), and numColors().
Sets the color table used to translate color indexes to RGB values to the specified colors.
See also colorTable().
Sets the value returned by dotsPerMeterX() to x.
See also dotsPerMeterX().
Sets the value returned by dotsPerMeterY() to y.
See also dotsPerMeterY().
Resizes the color table to numColors colors.
If the color table is expanded all the extra colors will be set to black (RGB 0,0,0).
See also numColors(), color(), setColor(), and colorTable().
Sets the value returned by offset() to p.
See also offset().
Sets the pixel index or color at the coordinates (x, y) to index_or_rgb.
If (x, y) is not valid, the result is undefined.
If the image is a paletted image (depth() <= 8) and index_or_rgb >= numColors(), the result is undefined.
See also pixelIndex(), pixel(), qRgb(), qRgba(), and valid().
Sets the image text value with the key 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().
See also text() and QImageWriter::setText().
Returns the size of the image, i.e. its width and height.
See also width(), height(), and rect().
Returns the image text associated with key. If key is an empty string, the whole image text is returned, with each key-text pair separated by a newline.
You can also set the image text by calling setText().
See also textKeys() and setText().
Returns the text keys for this image. You can use these keys with text() to list the image text for a certain key.
See also text(), setText(), and QImageReader::textKeys().
Returns the language identifiers for which some texts are recorded.
Note that if you want to iterate over the list, you should iterate over a copy, e.g.
QStringList list = myImage.textLanguages(); QStringList::Iterator it = list.begin(); while(it != list.end()) { myProcessing(*it); ++it; }
See also textList(), text(), setText(), and textKeys().
Returns a copy of the image that is transformed with the transformation matrix specified by matrix and using the transformation mode specified by mode.
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.
See also scaled(), QPixmap::transformed(), QPixmap::trueMatrix(), and QMatrix.
Returns the actual matrix used for transforming a image with w width and h height and matrix matrix.
When transforming a image with transformed(), 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.
See also transformed() and QMatrix.
Returns true if (x, y) is a valid coordinate in the image; otherwise returns false.
See also width(), height(), and pixelIndex().
Returns the width of the image.
See also height(), size(), and rect().
Returns the image as a QVariant
Returns true if this image and image i 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.
See also operator=().
Assigns a shallow copy of image to this image and returns a reference to this image.
See also copy().
Returns true if this image and image i 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.
See also operator=().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Writes the image image to the stream s 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.
Format of the QDataStream operators
See also QImage::save().
This is an overloaded member function, provided for convenience. It behaves essentially like the above function.
Reads an image from the stream s and stores it in image.
Format of the QDataStream operators
See also QImage::load().
Copyright © 2005 Trolltech | Trademarks | Qt 4.1.0 |