QImage Class Reference


The QImage class provides a hardware-independent pixmap representation with direct access to the pixel data. (details) (complete member list)

#include <qimage.h>

Public Members

Static Public Members


Detailed Description

The QImage class provides a hardware-independent pixmap representation with direct access to the pixel data.

The direct pixel access functionality of QImage makes it very suitable for image processing and for pixmap archiving.

An image contains the parameters width, height and depth (bits per pixel, bpp), a color table and the actual pixels. QImage support 1 bit, 8 bits and 24 bits depths. 1 bit and 8 bit images use a color lookup table, where the pixel value is the index of a color.

An entry in the color table is an RGB triplet encoded as ulong. Use the QRED, QGREEN and QBLUE functions (qcolor.h) to separate the components, and QRGB to set an RGB value (see the QColor class documentation).

1-bpp (monochrome) images have a color table with maximum 2 colors. There are two ways of encoding such images; big endian (MSB first) or little endian bit order (LSB first). 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 >> 8) |= 1 << (x & 7);
  else
      *(image.scanline(y) + x >> 8) |= 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 use 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;

24-bpp images do not have a color table, instead each pixel is encoded as red + green + blue.

  QImage image;
    // sets 24 bit pixel at (x,y) to yellow.
  *(image.scanline(y) + 3*x) = QRGB(255, 255, 0);

The QImage class uses explicit data sharing, similar to that of QArray and QString. This makes it easy to use images in your program, because you never need to worry about who should be responsible for deleting images. An image is automatically destroyed when the last reference to the data is lost.

The disadvantage of explicit data sharing is that changing one image might affect others (when you do not want it). Call the detach() function to make sure that you get your own private copy of an image.

The QPixmap class, on the other hand, uses implicit data sharing, which means that the object automatically detaches when it is about to change. Implicit data sharing is easy to implement for classes that can detect change through member functions, but impossible to implement for classes that export pointers to internal data.


Member Function Documentation

QImage::QImage ()

Constructs a null image.

See also: isNull().

QImage::QImage (int w, int h, int depth, int numColors=0, int bitOrder=IgnoreEndian)

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().

QImage::QImage (const QImage &image)

Constructs a shallow copy of image.

QImage::~QImage ()

Destroys the image and cleans up.

int QImage::bitOrder () const

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.

uchar * QImage::bits () const

Returns a pointer to the first pixel data. Similar to scanline(0).

If the image data is segmented (not contiguous), use the scanline() function to get a pointer to each scanline of image data.

See also: contiguousBits(), scanline().

int QImage::bytesPerLine () const

Returns the number of bytes per image scanline.

ulong QImage::color (int i) const

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().

ulong * QImage::colorTable () const

Returns a pointer to the color table.

bool QImage::contiguousBits () const

Returns TRUE if the image data bits are encoded as a contiguous array of bytes, or FALSE if the data is segmented into separate buffers for each scanline.

Segmented data can only occur on 16-bits systems, like Windows 3.x, when the total image data takes more than 64 kbytes of memory. All 32-bit operating systems (UNIX/X, Win32, OS/2 etc.) use contiguous image data.

QImage QImage::convertBitOrder (int bitOrder) const

Converts the bit order of the image to bitOrder and returns the converted image.

See also: bitOrder(), setBitOrder().

QImage QImage::convertDepth (int depth) const

Converts the depth (bpp) of the image to depth and returns the converted image.

The depths parameter can be 1, 8 or 24.

See also: depth().

QImage QImage::copy () const

Returns a deep copy of the image.

bool QImage::create (int width, int height, int depth, int numColors=0, int bitOrder=IgnoreEndian)

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.

Allocates a color table and a buffer for the image data. The image data is filled with the pixel value 0.

If depth is 1, then bitOrder must be set to QImage::LittleEndian or QImage::BigEndian, otherwise bitOrder must be QImage::IgnoreEndian.

On 32-bit systems, the image data is always allocated as one block (contiguous data). On Windows 3.x (16 bit) the image data is allocated in smaller chunks, (one block per scanline) when the image data occupies more than 64k. The image data structure ('bits' member of QImage) consists of a table of pointers to each scanline.

See also: contiguousBits().

int QImage::depth () const

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 24 bit.

void QImage::detach ()

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 dereferences the data and gets a copy of the data. Nothing will be done if there is just a single reference.

void QImage::freeBits ()

Internal function that deallocates the image data and sets the bits pointer to 0.

int QImage::height () const

Returns the height of the image.

bool QImage::isNull () const

Returns TRUE if it is a null image.

A null image has all parameters set to zero and no allocated data.

uchar ** QImage::jumpTable () const

Returns a pointer to the scanline pointer table.

This is the beginning of the data block for contiguous images.

long QImage::numBytes () const

Returned the number of bytes occupied by the image data.

int QImage::numColors () const

Returns the size of the color table for the image.

Notice that numColors() returns 0 for 24-bit images, since these images do not use color tables, but instead encode pixel values as RGB triplets.

QImage & QImage::operator= (const QImage &image)

Assigns a shallow copy of image to this image and returns a reference to this image.

See also: copy().

QImage & QImage::operator= (const QPixmap &pixmap)

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().

QRect QImage::rect () const

Returns the enclosing rectangle of the image.

uchar * QImage::scanline (int i) const

Returns a pointer to the pixel data at the i'th scanline.

See also: bits().

void QImage::setColor (int i, ulong c)

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 color values.

See also: color().

void QImage::setNumColors (int numColors)

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(), setColor().

QSize QImage::size () const

Returns the size of the image.

int QImage::width () const

Returns the width of the image


This file is part of the Qt toolkit, copyright 1995 Troll Tech, all rights reserved.

It was generated from the following files:


Generated at 04:27, 1995/05/20 by the webmaster at Troll Tech