The QImage class provides a hardware-independent pixmap representation with direct access to the pixel data. More...
#include <qimage.h>
The direct pixel access functionality of QImage makes it very suitable for image processing and for pixmap archiving.
An image has the parameters width, height and depth (bits per pixel, bpp), a color table and the actual pixels. QImage supports 1-bpp, 8-bpp and 32-bpp image data. 1-bpp and 8-bpp images use a color lookup table; the pixel value is a color table index.
32-bpp images encode an RGB value in 24 bits and ignore the color table. The most significant byte is reserved for alpha channel support in a future version of Qt.
An entry in the color table is an RGB triplet encoded as uint.
Use
the qRed, qGreen and qBlue functions (qcolor.h) to access the
components, and qRgb to make an RGB triplet (see the QColor class
documentation).
1-bpp (monochrome) images have a color table with maximum 2 colors. There are two different formats; big endian (MSB first) or little endian (LSB first) bit order. To access a single bit, you will have to do some bitshifts:
QImage image; // sets bit at (x,y) to 1 if ( image.bitOrder() == QImage::LittleEndian ) *(image.scanLine(y) + (x >> 3)) |= 1 << (x & 7); else *(image.scanLine(y) + (x >> 3)) |= 1 << (7 -(x & 7));
If this looks complicated, it might be a good idea to convert the 1-bpp image to an 8-bpp image using convertDepth().
8-bpp images are much easier to work with than 1-bpp 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-bpp images ignore the color table, instead each pixel contains the RGB triplet. 24 bits contain the RGB value and the most significant byte is reserved for alpha channel.
QImage image; // sets 32 bit pixel at (x,y) to yellow. uint *p = (uint *)image.scanLine(y) + x; *p = qRgb(255,255,0);
The scanlines are 32-bit aligned for all depths.
The QImage class uses explicit sharing, similar to that of QArray and QString.
See also: QImageIO, QPixmap and Shared Classes
Examples: qmag/qmag.cpp
Constructs a shallow copy of image.
Constructs an image with w width, h height, depth bits per pixel, numColors colors and bit order bitOrder.
Using this constructor is the same as first constructing a null image and then calling the create() function.
See also: create().
Constructs a null image.
See also: isNull().
Destroys the image and cleans up.
Returns the bit order for the image.
If it is a 1-bit image, this function returns either QImage::BigEndian or QImage::LittleEndian.
If it is not a 1-bit image, this function returns QImage::IgnoreEndian.
See also: depth().
Returns a pointer to the first pixel data. Equivalent to scanLine(0).
See also: scanLine().
Returns the number of bytes per image scanline. This is equivalent to numBytes()/height().
Returns the color in the color table at index i.
A color value is an RGB triplet. Use the QRED, QGREEN and QBLUE functions (defined in qcolor.h) to get the color value components.
See also: setColor() and QColor.
Returns a pointer to the color table.
Converts the bit order of the image to bitOrder and returns the converted image.
Returns *this
if the bitOrder is equal to the image bit order, or a
null image if this image cannot be converted.
See also: bitOrder() and setBitOrder().
Converts the depth (bpp) of the image to depth and returns the converted image.
The depth argument must be 1, 8 or 32.
Returns *this
if depth is equal to the image depth, or a null
image if this image cannot be converted.
See also: depth() and isNull().
Returns a deep copy of the image.
Sets the image width, height, depth, number of colors and bit order. Returns TRUE if successful, or FALSE if the parameters are incorrect or if memory cannot be allocated.
The width and height is limited to 32767. depth must be 1, 8 or 32. If depth is 1, then bitOrder must be set to either QImage::LittleEndian or QImage::BigEndian. For other depths, bitOrder must be QImage::IgnoreEndian.
This function allocates a color table and a buffer for the image data. The image data is filled with the pixel value 0.
The image buffer is allocated as a single block that consists of a table of scanline pointers (jumpTable()) and the image data (bits()).
See also: width(), height(), depth(), numColors(), bitOrder(), jumpTable(), scanLine(), bits(), bytesPerLine() and numBytes().
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.
Detaches from shared image data and makes sure that this image is the only one referring 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.
Returns the height of the image.
See also: width(), size() and rect().
Returns TRUE if it is a null image.
A null image has all parameters set to zero and no allocated data.
Returns a pointer to the scanline pointer table.
This is the beginning of the data block for the image.
Returns the number of bytes occupied by the image data.
See also: bytesPerLine().
Returns the size of the color table for the image.
Notice that numColors() returns 0 for 32-bpp images, since these images do not use color tables, but instead encode pixel values as RGB triplets.
Sets the image bits to the pixmap contents and returns a reference to the image.
If the image shares data with other images, it will first dereference the shared data.
Makes a call to QPixmap::convertToImage().
Assigns a shallow copy of image to this image and returns a reference to this image.
See also: copy().
Returns the enclosing rectangle (0,0,width(),height()) of the image.
See also: width(), height() and size().
Resets all image parameters and deallocates the image data.
Returns a pointer to the pixel data at the i'th scanline.
The scanline data is aligned on a 32-bit boundary.
Warning: If you are accessing 32-bpp image data, cast the returned
pointer to uint*
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. Hint: use qRgb() and friends (qcolor.h) to access the pixels.
See also: bits().
Sets a color in the color table at index i to c.
A color value is an RGB triplet. Use the qRgb function (defined in qcolor.h) to make RGB triplets.
See also: color().
Resizes the color table to numColors colors.
If the color table is expanded, then all new colors will be set to black (RGB 0,0,0).
See also: color() and setColor().
Returns the size of the image.
See also: width(), height() and rect().
[static]
Determines the bit order of the display hardware. Returns QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first).
[static]
Determines the host computer byte order. Returns QImage::LittleEndian (LSB first) or QImage::BigEndian (MSB first).
Returns the width of the image.
See also: heigth(), size() and rect().
This file is part of the Qt toolkit, copyright © 1995-96 Troll Tech, all rights reserved.
It was generated from the following files: