Qt 4.8
Public Types | Public Functions | Protected Functions | Protected Variables | List of all members
QImageIOHandler Class Referenceabstract

The QImageIOHandler class defines the common image I/O interface for all image formats in Qt. More...

#include <qimageiohandler.h>

Inheritance diagram for QImageIOHandler:
QBmpHandler QGifHandler QJpegHandler QMngHandler QPngHandler QPpmHandler QSvgIOHandler QTgaHandler QtIcoHandler QTiffHandler QXbmHandler QXpmHandler

Public Types

enum  ImageOption {
  Size, ClipRect, Description, ScaledClipRect,
  ScaledSize, CompressionRatio, Gamma, Quality,
  Name, SubType, IncrementalReading, Endianness,
  Animation, BackgroundColor, ImageFormat
}
 This enum describes the different options supported by QImageIOHandler. More...
 

Public Functions

virtual bool canRead () const =0
 Returns true if an image can be read from the device (i. More...
 
virtual int currentImageNumber () const
 For image formats that support animation, this function returns the sequence number of the current image in the animation. More...
 
virtual QRect currentImageRect () const
 Returns the rect of the current image. More...
 
QIODevicedevice () const
 Returns the device currently assigned to the QImageIOHandler. More...
 
QByteArray format () const
 Returns the format that is currently assigned to QImageIOHandler. More...
 
virtual int imageCount () const
 For image formats that support animation, this function returns the number of images in the animation. More...
 
virtual bool jumpToImage (int imageNumber)
 For image formats that support animation, this function jumps to the image whose sequence number is imageNumber. More...
 
virtual bool jumpToNextImage ()
 For image formats that support animation, this function jumps to the next image. More...
 
virtual int loopCount () const
 For image formats that support animation, this function returns the number of times the animation should loop. More...
 
virtual QByteArray name () const
 Use format() instead. More...
 
virtual int nextImageDelay () const
 For image formats that support animation, this function returns the number of milliseconds to wait until reading the next image. More...
 
virtual QVariant option (ImageOption option) const
 Returns the value assigned to option as a QVariant. More...
 
 QImageIOHandler ()
 Constructs a QImageIOHandler object. More...
 
virtual bool read (QImage *image)=0
 Read an image from the device, and stores it in image. More...
 
void setDevice (QIODevice *device)
 Sets the device of the QImageIOHandler to device. More...
 
void setFormat (const QByteArray &format)
 Sets the format of the QImageIOHandler to format. More...
 
void setFormat (const QByteArray &format) const
 Sets the format of the QImageIOHandler to format. More...
 
virtual void setOption (ImageOption option, const QVariant &value)
 Sets the option option with the value value. More...
 
virtual bool supportsOption (ImageOption option) const
 Returns true if the QImageIOHandler supports the option option; otherwise returns false. More...
 
virtual bool write (const QImage &image)
 Writes the image image to the assigned device. More...
 
virtual ~QImageIOHandler ()
 Destructs the QImageIOHandler object. More...
 

Protected Functions

 QImageIOHandler (QImageIOHandlerPrivate &dd)
 Constructs a QImageIOHandler object, using the private member dd. More...
 

Protected Variables

QScopedPointer< QImageIOHandlerPrivated_ptr
 

Detailed Description

The QImageIOHandler class defines the common image I/O interface for all image formats in Qt.

Note
This class or function is reentrant.

Qt uses QImageIOHandler for reading and writing images through QImageReader and QImageWriter. You can also derive from this class to write your own image format handler using Qt's plugin mechanism.

Call setDevice() to assign a device to the handler, and setFormat() to assign a format to it. One QImageIOHandler may support more than one image format. canRead() returns true if an image can be read from the device, and read() and write() return true if reading or writing an image was completed successfully.

QImageIOHandler also has support for animations formats, through the functions loopCount(), imageCount(), nextImageDelay() and currentImageNumber().

In order to determine what options an image handler supports, Qt will call supportsOption() and setOption(). Make sure to reimplement these functions if you can provide support for any of the options in the ImageOption enum.

To write your own image handler, you must at least reimplement canRead() and read(). Then create a QImageIOPlugin that can create the handler. Finally, install your plugin, and QImageReader and QImageWriter will then automatically load the plugin, and start using it.

See also
QImageIOPlugin, QImageReader, QImageWriter

Definition at line 61 of file qimageiohandler.h.

Enumerations

◆ ImageOption

This enum describes the different options supported by QImageIOHandler.

Some options are used to query an image for properties, and others are used to toggle the way in which an image should be written.

  • Size The original size of an image. A handler that supports this option is expected to read the size of the image from the image metadata, and return this size from option() as a QSize.
  • ClipRect The clip rect, or ROI (Region Of Interest). A handler that supports this option is expected to only read the provided QRect area from the original image in read(), before any other transformation is applied.
  • ScaledSize The scaled size of the image. A handler that supports this option is expected to scale the image to the provided size (a QSize), after applying any clip rect transformation (ClipRect). If the handler does not support this option, QImageReader will perform the scaling after the image has been read.
  • ScaledClipRect The scaled clip rect (or ROI, Region Of Interest) of the image. A handler that supports this option is expected to apply the provided clip rect (a QRect), after applying any scaling (ScaleSize) or regular clipping (ClipRect). If the handler does not support this option, QImageReader will apply the scaled clip rect after the image has been read.
  • Description The image description. Some image formats, such as GIF and PNG, allow embedding of text or comments into the image data (e.g., for storing copyright information). It's common that the text is stored in key-value pairs, but some formats store all text in one continuous block. QImageIOHandler returns the text as one QString, where keys and values are separated by a ':', and keys-value pairs are separated by two newlines (\n\n). For example, "Title: Sunset\\n\\nAuthor: Jim Smith\\nSarah Jones\\n\\n". Formats that store text in a single block can use "Description" as the key.
  • CompressionRatio The compression ratio of the image data. A handler that supports this option is expected to set its compression rate depending on the value of this option (an int) when writing.
  • Gamma The gamma level of the image. A handler that supports this option is expected to set the image gamma level depending on the value of this option (a float) when writing.
  • Quality The quality level of the image. A handler that supports this option is expected to set the image quality level depending on the value of this option (an int) when writing.
  • Name The name of the image. A handler that supports this option is expected to read the name from the image metadata and return this as a QString, or when writing an image it is expected to store the name in the image metadata.
  • SubType The subtype of the image. A handler that supports this option can use the subtype value to help when reading and writing images. For example, a PPM handler may have a subtype value of "ppm" or "ppmraw".
  • IncrementalReading A handler that supports this option is expected to read the image in several passes, as if it was an animation. QImageReader will treat the image as an animation.
  • Endianness The endianness of the image. Certain image formats can be stored as BigEndian or LittleEndian. A handler that supports Endianness uses the value of this option to determine how the image should be stored.
  • BackgroundColor Certain image formats allow the background color to be specified. A handler that supports BackgroundColor initializes the background color to this option (a QColor) when reading an image.
  • ImageFormat The image's data format returned by the handler. This can be any of the formats listed in QImage::Format.
Enumerator
Size 
ClipRect 
Description 
ScaledClipRect 
ScaledSize 
CompressionRatio 
Gamma 
Quality 
Name 
SubType 
IncrementalReading 
Endianness 
Animation 
BackgroundColor 
ImageFormat 

Definition at line 81 of file qimageiohandler.h.

Constructors and Destructors

◆ QImageIOHandler() [1/2]

QImageIOHandler::QImageIOHandler ( )

Constructs a QImageIOHandler object.

Definition at line 268 of file qimageiohandler.cpp.

269  : d_ptr(new QImageIOHandlerPrivate(this))
270 {
271 }
QScopedPointer< QImageIOHandlerPrivate > d_ptr

◆ ~QImageIOHandler()

QImageIOHandler::~QImageIOHandler ( )
virtual

Destructs the QImageIOHandler object.

Definition at line 289 of file qimageiohandler.cpp.

290 {
291 }

◆ QImageIOHandler() [2/2]

QImageIOHandler::QImageIOHandler ( QImageIOHandlerPrivate dd)
protected

Constructs a QImageIOHandler object, using the private member dd.

Warning
This function is not part of the public interface.

Definition at line 281 of file qimageiohandler.cpp.

282  : d_ptr(&dd)
283 {
284 }
QScopedPointer< QImageIOHandlerPrivate > d_ptr

Functions

◆ canRead()

bool QImageIOHandler::canRead ( ) const
pure virtual

Returns true if an image can be read from the device (i.

e., the image format is supported, the device can be read from and the initial header information suggests that the image can be read); otherwise returns false.

When reimplementing canRead(), make sure that the I/O device (device()) is left in its original state (e.g., by using peek() rather than read()).

See also
read(), QIODevice::peek()

Implemented in QBmpHandler, QPngHandler, QPpmHandler, QXbmHandler, QXpmHandler, QGifHandler, QSvgIOHandler, QMngHandler, QJpegHandler, QTgaHandler, QtIcoHandler, and QTiffHandler.

Referenced by QImageReader::canRead(), QImageReader::format(), imageCount(), and QImageReader::imageFormat().

◆ currentImageNumber()

int QImageIOHandler::currentImageNumber ( ) const
virtual

For image formats that support animation, this function returns the sequence number of the current image in the animation.

If this function is called before any image is read(), -1 is returned. The number of the first image in the sequence is 0.

If the image format does not support animation, 0 is returned.

See also
read()

Reimplemented in QGifHandler, and QMngHandler.

Definition at line 465 of file qimageiohandler.cpp.

Referenced by QImageReader::currentImageNumber().

466 {
467  return 0;
468 }

◆ currentImageRect()

QRect QImageIOHandler::currentImageRect ( ) const
virtual

Returns the rect of the current image.

If no rect is defined for the image, and empty QRect() is returned.

This function is useful for animations, where only parts of the frame may be updated at a time.

Definition at line 477 of file qimageiohandler.cpp.

Referenced by QImageReader::currentImageRect().

478 {
479  return QRect();
480 }
The QRect class defines a rectangle in the plane using integer precision.
Definition: qrect.h:58

◆ device()

QIODevice * QImageIOHandler::device ( ) const

◆ format()

QByteArray QImageIOHandler::format ( ) const

Returns the format that is currently assigned to QImageIOHandler.

If no format has been assigned, an empty string is returned.

See also
setFormat()

Definition at line 353 of file qimageiohandler.cpp.

Referenced by QImageReader::format(), QImageReader::imageFormat(), QSvgIOHandlerPrivate::load(), name(), QPpmHandler::option(), QBmpHandler::option(), and setFormat().

354 {
355  Q_D(const QImageIOHandler);
356  return d->format;
357 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QImageIOHandler class defines the common image I/O interface for all image formats in Qt...

◆ imageCount()

int QImageIOHandler::imageCount ( ) const
virtual

For image formats that support animation, this function returns the number of images in the animation.

If the image format does not support animation, or if it is unable to determine the number of images, 0 is returned.

The default implementation returns 1 if canRead() returns true; otherwise 0 is returned.

Reimplemented in QGifHandler, QMngHandler, and QtIcoHandler.

Definition at line 491 of file qimageiohandler.cpp.

Referenced by QImageReader::imageCount().

492 {
493  return canRead() ? 1 : 0;
494 }
virtual bool canRead() const =0
Returns true if an image can be read from the device (i.

◆ jumpToImage()

bool QImageIOHandler::jumpToImage ( int  imageNumber)
virtual

For image formats that support animation, this function jumps to the image whose sequence number is imageNumber.

The next call to read() will attempt to read this image.

The default implementation does nothing, and returns false.

Reimplemented in QMngHandler, and QtIcoHandler.

Definition at line 514 of file qimageiohandler.cpp.

Referenced by QImageReader::jumpToImage().

515 {
516  Q_UNUSED(imageNumber);
517  return false;
518 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ jumpToNextImage()

bool QImageIOHandler::jumpToNextImage ( )
virtual

For image formats that support animation, this function jumps to the next image.

The default implementation does nothing, and returns false.

Reimplemented in QMngHandler, and QtIcoHandler.

Definition at line 502 of file qimageiohandler.cpp.

Referenced by QImageReader::jumpToNextImage().

503 {
504  return false;
505 }

◆ loopCount()

int QImageIOHandler::loopCount ( ) const
virtual

For image formats that support animation, this function returns the number of times the animation should loop.

If the image format does not support animation, 0 is returned.

Reimplemented in QGifHandler, and QMngHandler.

Definition at line 525 of file qimageiohandler.cpp.

Referenced by QImageReader::loopCount().

526 {
527  return 0;
528 }

◆ name()

QByteArray QImageIOHandler::name ( ) const
virtual

Use format() instead.

Reimplemented in QBmpHandler, QPngHandler, QXpmHandler, QPpmHandler, QXbmHandler, QGifHandler, QSvgIOHandler, QJpegHandler, QMngHandler, QTgaHandler, QtIcoHandler, and QTiffHandler.

Definition at line 400 of file qimageiohandler.cpp.

401 {
402  return format();
403 }
QByteArray format() const
Returns the format that is currently assigned to QImageIOHandler.

◆ nextImageDelay()

int QImageIOHandler::nextImageDelay ( ) const
virtual

For image formats that support animation, this function returns the number of milliseconds to wait until reading the next image.

If the image format does not support animation, 0 is returned.

Reimplemented in QGifHandler, and QMngHandler.

Definition at line 536 of file qimageiohandler.cpp.

Referenced by QImageReader::nextImageDelay().

537 {
538  return 0;
539 }

◆ option()

QVariant QImageIOHandler::option ( ImageOption  option) const
virtual

Returns the value assigned to option as a QVariant.

The type of the value depends on the option. For example, option(Size) returns a QSize variant.

See also
setOption(), supportsOption()

Reimplemented in QBmpHandler, QPngHandler, QPpmHandler, QXbmHandler, QXpmHandler, QMngHandler, QGifHandler, QtIcoHandler, QJpegHandler, QSvgIOHandler, QTgaHandler, and QTiffHandler.

Definition at line 435 of file qimageiohandler.cpp.

Referenced by QImageReader::backgroundColor(), QImageReaderPrivate::getText(), QImageReader::imageFormat(), QImageReader::size(), and QImageReader::supportsAnimation().

436 {
437  Q_UNUSED(option);
438  return QVariant();
439 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
virtual QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ read()

bool QImageIOHandler::read ( QImage image)
pure virtual

Read an image from the device, and stores it in image.

Returns true if the image is successfully read; otherwise returns false.

For image formats that support incremental loading, and for animation formats, the image handler can assume that image points to the previous frame.

See also
canRead()

Implemented in QBmpHandler, QPngHandler, QPpmHandler, QXbmHandler, QXpmHandler, QGifHandler, QSvgIOHandler, QMngHandler, QJpegHandler, QTgaHandler, QtIcoHandler, and QTiffHandler.

Referenced by QImageReader::read().

◆ setDevice()

void QImageIOHandler::setDevice ( QIODevice device)

Sets the device of the QImageIOHandler to device.

The image handler will use this device when reading and writing images.

The device can only be set once and must be set before calling canRead(), read(), write(), etc. If you need to read multiple files, construct multiple instances of the appropriate QImageIOHandler subclass.

See also
device()

Definition at line 304 of file qimageiohandler.cpp.

Referenced by QTiffPlugin::create(), QJpegPlugin::create(), QTgaPlugin::create(), QSvgPlugin::create(), QMngPlugin::create(), QGifPlugin::create(), createReadHandlerHelper(), and if().

305 {
307  d->device = device;
308 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
The QImageIOHandler class defines the common image I/O interface for all image formats in Qt...
QIODevice * device() const
Returns the device currently assigned to the QImageIOHandler.

◆ setFormat() [1/2]

void QImageIOHandler::setFormat ( const QByteArray format)

Sets the format of the QImageIOHandler to format.

The format is most useful for handlers that support multiple image formats.

See also
format()

Definition at line 326 of file qimageiohandler.cpp.

Referenced by QTiffHandler::canRead(), QTgaHandler::canRead(), QJpegHandler::canRead(), QMngHandler::canRead(), QSvgIOHandler::canRead(), QXbmHandler::canRead(), QXpmHandler::canRead(), QPpmHandler::canRead(), QPngHandler::canRead(), QBmpHandler::canRead(), QTiffPlugin::create(), QTgaPlugin::create(), QICOPlugin::create(), QJpegPlugin::create(), QSvgPlugin::create(), QGifPlugin::create(), QMngPlugin::create(), createReadHandlerHelper(), and if().

327 {
329  d->format = format;
330 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
QByteArray format() const
Returns the format that is currently assigned to QImageIOHandler.
The QImageIOHandler class defines the common image I/O interface for all image formats in Qt...

◆ setFormat() [2/2]

void QImageIOHandler::setFormat ( const QByteArray format) const

Sets the format of the QImageIOHandler to format.

The format is most useful for handlers that support multiple image formats.

This function is declared const so that it can be called from canRead().

See also
format()

Definition at line 340 of file qimageiohandler.cpp.

341 {
342  Q_D(const QImageIOHandler);
343  d->format = format;
344 }
double d
Definition: qnumeric_p.h:62
#define Q_D(Class)
Definition: qglobal.h:2482
QByteArray format() const
Returns the format that is currently assigned to QImageIOHandler.
The QImageIOHandler class defines the common image I/O interface for all image formats in Qt...

◆ setOption()

void QImageIOHandler::setOption ( ImageOption  option,
const QVariant value 
)
virtual

Sets the option option with the value value.

See also
option(), ImageOption

Reimplemented in QBmpHandler, QPngHandler, QPpmHandler, QXbmHandler, QXpmHandler, QMngHandler, QGifHandler, QJpegHandler, QSvgIOHandler, QTgaHandler, and QTiffHandler.

Definition at line 422 of file qimageiohandler.cpp.

Referenced by createReadHandlerHelper(), if(), QImageReader::read(), QImageReader::setBackgroundColor(), and QImageWriter::write().

423 {
424  Q_UNUSED(option);
425  Q_UNUSED(value);
426 }
virtual QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ supportsOption()

bool QImageIOHandler::supportsOption ( ImageOption  option) const
virtual

Returns true if the QImageIOHandler supports the option option; otherwise returns false.

For example, if the QImageIOHandler supports the Size option, supportsOption(Size) must return true.

See also
setOption(), option()

Reimplemented in QBmpHandler, QPngHandler, QPpmHandler, QXbmHandler, QXpmHandler, QMngHandler, QGifHandler, QJpegHandler, QSvgIOHandler, QtIcoHandler, QTgaHandler, and QTiffHandler.

Definition at line 449 of file qimageiohandler.cpp.

Referenced by QImageReader::backgroundColor(), QImageReaderPrivate::getText(), QImageReader::imageFormat(), QImageReader::read(), QImageReader::setBackgroundColor(), QImageReader::size(), QImageReader::supportsAnimation(), QImageWriter::supportsOption(), QImageReader::supportsOption(), and QImageWriter::write().

450 {
451  Q_UNUSED(option);
452  return false;
453 }
virtual QVariant option(ImageOption option) const
Returns the value assigned to option as a QVariant.
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

◆ write()

bool QImageIOHandler::write ( const QImage image)
virtual

Writes the image image to the assigned device.

Returns true on success; otherwise returns false.

The default implementation does nothing, and simply returns false.

Reimplemented in QBmpHandler, QPngHandler, QPpmHandler, QXbmHandler, QXpmHandler, QGifHandler, QMngHandler, QJpegHandler, QtIcoHandler, and QTiffHandler.

Definition at line 411 of file qimageiohandler.cpp.

Referenced by QImageWriter::write().

412 {
413  Q_UNUSED(image);
414  return false;
415 }
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729

Properties

◆ d_ptr

QScopedPointer<QImageIOHandlerPrivate> QImageIOHandler::d_ptr
protected

Definition at line 113 of file qimageiohandler.h.


The documentation for this class was generated from the following files: