Qt 4.8
|
The QImageReader class provides a format independent interface for reading images from files or other devices. More...
#include <qimagereader.h>
Public Types | |
enum | ImageReaderError { UnknownError, FileNotFoundError, DeviceError, UnsupportedFormatError, InvalidDataError } |
This enum describes the different types of errors that can occur when reading images with QImageReader. More... | |
Public Functions | |
bool | autoDetectImageFormat () const |
Returns true if image format autodetection is enabled on this image reader; otherwise returns false. More... | |
QColor | backgroundColor () const |
Returns the background color that's used when reading an image. More... | |
bool | canRead () const |
Returns true if an image can be read for the device (i.e., the image format is supported, and the device seems to contain valid data); otherwise returns false. More... | |
QRect | clipRect () const |
Returns the clip rect (also known as the ROI, or Region Of Interest) of the image. More... | |
int | currentImageNumber () const |
For image formats that support animation, this function returns the sequence number of the current frame. More... | |
QRect | currentImageRect () const |
For image formats that support animation, this function returns the rect for the current frame. More... | |
bool | decideFormatFromContent () const |
Returns whether the image reader should decide which plugin to use only based on the contents of the datastream rather than on the file extension. More... | |
QIODevice * | device () const |
Returns the device currently assigned to QImageReader, or 0 if no device has been assigned. More... | |
ImageReaderError | error () const |
Returns the type of error that occurred last. More... | |
QString | errorString () const |
Returns a human readable description of the last error that occurred. More... | |
QString | fileName () const |
If the currently assigned device is a QFile, or if setFileName() has been called, this function returns the name of the file QImageReader reads from. More... | |
QByteArray | format () const |
Returns the format QImageReader uses for reading images. More... | |
int | imageCount () const |
For image formats that support animation, this function returns the total number of images in the animation. More... | |
QImage::Format | imageFormat () const |
Returns the format of the image, without actually reading the image contents. More... | |
bool | jumpToImage (int imageNumber) |
For image formats that support animation, this function skips to the image whose sequence number is imageNumber, returning true if successful or false if the corresponding image cannot be found. More... | |
bool | jumpToNextImage () |
For image formats that support animation, this function steps over the current image, returning true if successful or false if there is no following image in the animation. More... | |
int | loopCount () const |
For image formats that support animation, this function returns the number of times the animation should loop. More... | |
int | nextImageDelay () const |
For image formats that support animation, this function returns the number of milliseconds to wait until displaying the next frame in the animation. More... | |
QImageReader () | |
Constructs an empty QImageReader object. More... | |
QImageReader (QIODevice *device, const QByteArray &format=QByteArray()) | |
Constructs a QImageReader object with the device device and the image format format. More... | |
QImageReader (const QString &fileName, const QByteArray &format=QByteArray()) | |
Constructs a QImageReader object with the file name fileName and the image format format. More... | |
int | quality () const |
Returns the quality level of the image. More... | |
QImage | read () |
Reads an image from the device. More... | |
bool | read (QImage *image) |
Reads an image from the device into image, which must point to a QImage. More... | |
QRect | scaledClipRect () const |
Returns the scaled clip rect of the image. More... | |
QSize | scaledSize () const |
Returns the scaled size of the image. More... | |
void | setAutoDetectImageFormat (bool enabled) |
If enabled is true, image format autodetection is enabled; otherwise, it is disabled. More... | |
void | setBackgroundColor (const QColor &color) |
Sets the background color to color. More... | |
void | setClipRect (const QRect &rect) |
Sets the image clip rect (also known as the ROI, or Region Of Interest) to rect. More... | |
void | setDecideFormatFromContent (bool ignored) |
If ignored is set to true, then the image reader will ignore specified formats or file extensions and decide which plugin to use only based on the contents in the datastream. More... | |
void | setDevice (QIODevice *device) |
Sets QImageReader's device to device. More... | |
void | setFileName (const QString &fileName) |
Sets the file name of QImageReader to fileName. More... | |
void | setFormat (const QByteArray &format) |
Sets the format QImageReader will use when reading images, to format. More... | |
void | setQuality (int quality) |
This is an image format specific function that sets the quality level of the image to quality. More... | |
void | setScaledClipRect (const QRect &rect) |
Sets the scaled clip rect to rect. More... | |
void | setScaledSize (const QSize &size) |
Sets the scaled size of the image to size. More... | |
QSize | size () const |
Returns the size of the image, without actually reading the image contents. More... | |
bool | supportsAnimation () const |
Returns true if the image format supports animation; otherwise, false is returned. More... | |
bool | supportsOption (QImageIOHandler::ImageOption option) const |
Returns true if the reader supports option; otherwise returns false. More... | |
QString | text (const QString &key) const |
Returns the image text associated with key. More... | |
QStringList | textKeys () const |
Returns the text keys for this image. More... | |
~QImageReader () | |
Destructs the QImageReader object. More... | |
Static Public Functions | |
static QByteArray | imageFormat (const QString &fileName) |
If supported, this function returns the image format of the file fileName. More... | |
static QByteArray | imageFormat (QIODevice *device) |
If supported, this function returns the image format of the device device. More... | |
static QList< QByteArray > | supportedImageFormats () |
Returns the list of image formats supported by QImageReader. More... | |
Properties | |
QImageReaderPrivate * | d |
The QImageReader class provides a format independent interface for reading images from files or other devices.
The most common way to read images is through QImage and QPixmap's constructors, or by calling QImage::load() and QPixmap::load(). QImageReader is a specialized class which gives you more control when reading images. For example, you can read an image into a specific size by calling setScaledSize(), and you can select a clip rect, effectively loading only parts of an image, by calling setClipRect(). Depending on the underlying support in the image format, this can save memory and speed up loading of images.
To read an image, you start by constructing a QImageReader object. Pass either a file name or a device pointer, and the image format to QImageReader's constructor. You can then set several options, such as the clip rect (by calling setClipRect()) and scaled size (by calling setScaledSize()). canRead() returns the image if the QImageReader can read the image (i.e., the image format is supported and the device is open for reading). Call read() to read the image.
If any error occurs when reading the image, read() will return a null QImage. You can then call error() to find the type of error that occurred, or errorString() to get a human readable description of what went wrong.
Call supportedImageFormats() for a list of formats that QImageReader can read. QImageReader supports all built-in image formats, in addition to any image format plugins that support reading.
QImageReader autodetects the image format by default, by looking at the provided (optional) format string, the file name suffix, and the data stream contents. You can enable or disable this feature, by calling setAutoDetectImageFormat().
Definition at line 62 of file qimagereader.h.
This enum describes the different types of errors that can occur when reading images with QImageReader.
Enumerator | |
---|---|
UnknownError | |
FileNotFoundError | |
DeviceError | |
UnsupportedFormatError | |
InvalidDataError |
Definition at line 65 of file qimagereader.h.
QImageReader::QImageReader | ( | ) |
Constructs an empty QImageReader object.
Before reading an image, call setDevice() or setFileName().
Definition at line 655 of file qimagereader.cpp.
|
explicit |
Constructs a QImageReader object with the device device and the image format format.
Definition at line 664 of file qimagereader.cpp.
|
explicit |
Constructs a QImageReader object with the file name fileName and the image format format.
Definition at line 677 of file qimagereader.cpp.
QImageReader::~QImageReader | ( | ) |
Destructs the QImageReader object.
Definition at line 689 of file qimagereader.cpp.
bool QImageReader::autoDetectImageFormat | ( | ) | const |
Returns true if image format autodetection is enabled on this image reader; otherwise returns false.
By default, autodetection is enabled.
Definition at line 785 of file qimagereader.cpp.
QColor QImageReader::backgroundColor | ( | ) | const |
Returns the background color that's used when reading an image.
If the image format does not support setting the background color an invalid color is returned.
Definition at line 1116 of file qimagereader.cpp.
Referenced by QMoviePrivate::infoForFrame().
bool QImageReader::canRead | ( | ) | const |
Returns true if an image can be read for the device (i.e., the image format is supported, and the device seems to contain valid data); otherwise returns false.
canRead() is a lightweight function that only does a quick test to see if the image data is valid. read() may still return false after canRead() returns true, if the image data is corrupt.
For images that support animation, canRead() returns false when all frames have been read.
Definition at line 1159 of file qimagereader.cpp.
Referenced by QMoviePrivate::isValid().
QRect QImageReader::clipRect | ( | ) | const |
Returns the clip rect (also known as the ROI, or Region Of Interest) of the image.
If no clip rect has been set, an invalid QRect is returned.
Definition at line 1032 of file qimagereader.cpp.
int QImageReader::currentImageNumber | ( | ) | const |
For image formats that support animation, this function returns the sequence number of the current frame.
If the image format doesn't support animation, 0 is returned.
This function returns -1 if an error occurred.
Definition at line 1391 of file qimagereader.cpp.
QRect QImageReader::currentImageRect | ( | ) | const |
For image formats that support animation, this function returns the rect for the current frame.
Otherwise, a null rect is returned.
Definition at line 1404 of file qimagereader.cpp.
bool QImageReader::decideFormatFromContent | ( | ) | const |
Returns whether the image reader should decide which plugin to use only based on the contents of the datastream rather than on the file extension.
Definition at line 819 of file qimagereader.cpp.
QIODevice * QImageReader::device | ( | ) | const |
Returns the device currently assigned to QImageReader, or 0 if no device has been assigned.
Definition at line 853 of file qimagereader.cpp.
Referenced by QMoviePrivate::infoForFrame(), QImageReader(), QMoviePrivate::reset(), and setDevice().
QImageReader::ImageReaderError QImageReader::error | ( | ) | const |
Returns the type of error that occurred last.
Definition at line 1416 of file qimagereader.cpp.
Referenced by QMoviePrivate::_q_loadNextFrame().
QString QImageReader::errorString | ( | ) | const |
Returns a human readable description of the last error that occurred.
Definition at line 1427 of file qimagereader.cpp.
Referenced by readImage().
QString QImageReader::fileName | ( | ) | const |
If the currently assigned device is a QFile, or if setFileName() has been called, this function returns the name of the file QImageReader reads from.
Otherwise (i.e., if no device has been assigned or the device is not a QFile), an empty QString is returned.
Definition at line 883 of file qimagereader.cpp.
Referenced by QMoviePrivate::infoForFrame().
QByteArray QImageReader::format | ( | ) | const |
Returns the format QImageReader uses for reading images.
You can call this function after assigning a device to the reader to determine the format of the device. For example:
If the reader cannot read any image from the device (e.g., there is no image there, or the image has already been read), or if the format is unsupported, this function returns an empty QByteArray().
Definition at line 724 of file qimagereader.cpp.
Referenced by imageFormat(), QMoviePrivate::infoForFrame(), QImageReader(), and setFormat().
int QImageReader::imageCount | ( | ) | const |
For image formats that support animation, this function returns the total number of images in the animation.
If the format does not support animation, 0 is returned.
This function returns -1 if an error occurred.
Definition at line 1359 of file qimagereader.cpp.
Referenced by QMoviePrivate::frameCount().
QImage::Format QImageReader::imageFormat | ( | ) | const |
Returns the format of the image, without actually reading the image contents.
The format describes the image format QImageReader::read() returns, not the format of the actual image.
If the image format does not support this feature, this function returns an invalid format.
Definition at line 962 of file qimagereader.cpp.
Referenced by imageFormat().
|
static |
If supported, this function returns the image format of the file fileName.
Otherwise, an empty string is returned.
Definition at line 1464 of file qimagereader.cpp.
|
static |
If supported, this function returns the image format of the device device.
Otherwise, an empty string is returned.
Definition at line 1479 of file qimagereader.cpp.
bool QImageReader::jumpToImage | ( | int | imageNumber | ) |
For image formats that support animation, this function skips to the image whose sequence number is imageNumber, returning true if successful or false if the corresponding image cannot be found.
The next call to read() will attempt to read this image.
Definition at line 1328 of file qimagereader.cpp.
Referenced by QMoviePrivate::infoForFrame().
bool QImageReader::jumpToNextImage | ( | ) |
For image formats that support animation, this function steps over the current image, returning true if successful or false if there is no following image in the animation.
The default implementation calls read(), then discards the resulting image, but the image handler may have a more efficient way of implementing this operation.
Definition at line 1312 of file qimagereader.cpp.
int QImageReader::loopCount | ( | ) | const |
For image formats that support animation, this function returns the number of times the animation should loop.
If this function returns -1, it can either mean the animation should loop forever, or that an error occurred. If an error occurred, canRead() will return false.
Definition at line 1343 of file qimagereader.cpp.
Referenced by QMoviePrivate::next().
int QImageReader::nextImageDelay | ( | ) | const |
For image formats that support animation, this function returns the number of milliseconds to wait until displaying the next frame in the animation.
If the image format doesn't support animation, 0 is returned.
This function returns -1 if an error occurred.
Definition at line 1375 of file qimagereader.cpp.
int QImageReader::quality | ( | ) | const |
Returns the quality level of the image.
Definition at line 920 of file qimagereader.cpp.
Referenced by setQuality().
QImage QImageReader::read | ( | ) |
Reads an image from the device.
On success, the image that was read is returned; otherwise, a null QImage is returned. You can then call error() to find the type of error that occurred, or errorString() to get a human readable description of the error.
For image formats that support animation, calling read() repeatedly will return the next frame. When all frames have been read, a null image will be returned.
Definition at line 1179 of file qimagereader.cpp.
Referenced by QRasterPixmapData::fromData(), QVGPixmapData::fromData(), QPixmapData::fromData(), QGLPixmapData::fromData(), QImage::fromData(), QVGPixmapData::fromFile(), QPixmapData::fromFile(), QGLPixmapData::fromFile(), QRasterPixmapData::fromImageReader(), QVGPixmapData::fromImageReader(), QPixmapData::fromImageReader(), QGLPixmapData::fromImageReader(), QImage::load(), and readImage().
bool QImageReader::read | ( | QImage * | image | ) |
Reads an image from the device into image, which must point to a QImage.
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.
Returns true on success; otherwise, returns false.
If image has same format and size as the image data that is about to be read, this function may not need to allocate a new image before reading. Because of this, it can be faster than the other read() overload, which always constructs a new image; especially when reading several images with the same format and size.
For image formats that support animation, calling read() repeatedly will return the next frame. When all frames have been read, a null image will be returned.
Definition at line 1210 of file qimagereader.cpp.
QRect QImageReader::scaledClipRect | ( | ) | const |
Returns the scaled clip rect of the image.
Definition at line 1079 of file qimagereader.cpp.
QSize QImageReader::scaledSize | ( | ) | const |
Returns the scaled size of the image.
Definition at line 1057 of file qimagereader.cpp.
Referenced by QMoviePrivate::infoForFrame(), and QMoviePrivate::next().
void QImageReader::setAutoDetectImageFormat | ( | bool | enabled | ) |
If enabled is true, image format autodetection is enabled; otherwise, it is disabled.
By default, autodetection is enabled.
QImageReader uses an extensive approach to detecting the image format; firstly, if you pass a file name to QImageReader, it will attempt to detect the file extension if the given file name does not point to an existing file, by appending supported default extensions to the given file name, one at a time. It then uses the following approach to detect the image format:
Image plugins are queried first, based on either the optional format string, or the file name suffix (if the source device is a file). No content detection is done at this stage. QImageReader will choose the first plugin that supports reading for this format.
If no plugin supports the image format, Qt's built-in handlers are checked based on either the optional format string, or the file name suffix.
If no capable plugins or built-in handlers are found, each plugin is tested by inspecting the content of the data stream.
If no plugins could detect the image format based on data contents, each built-in image handler is tested by inspecting the contents.
Finally, if all above approaches fail, QImageReader will report failure when trying to read the image.
By disabling image format autodetection, QImageReader will only query the plugins and built-in handlers based on the format string (i.e., no file name extensions are tested).
Definition at line 774 of file qimagereader.cpp.
void QImageReader::setBackgroundColor | ( | const QColor & | color | ) |
Sets the background color to color.
Image formats that support this operation are expected to initialize the background to color before reading an image.
Definition at line 1096 of file qimagereader.cpp.
void QImageReader::setClipRect | ( | const QRect & | rect | ) |
Sets the image clip rect (also known as the ROI, or Region Of Interest) to rect.
The coordinates of rect are relative to the untransformed image size, as returned by size().
Definition at line 1020 of file qimagereader.cpp.
void QImageReader::setDecideFormatFromContent | ( | bool | ignored | ) |
If ignored is set to true, then the image reader will ignore specified formats or file extensions and decide which plugin to use only based on the contents in the datastream.
Setting this flag means that all image plugins gets loaded. Each plugin will read the first bytes in the image data and decide if the plugin is compatible or not.
This also disables auto detecting the image format.
Definition at line 805 of file qimagereader.cpp.
void QImageReader::setDevice | ( | QIODevice * | device | ) |
Sets QImageReader's device to device.
If a device has already been set, the old device is removed from QImageReader and is otherwise left unchanged.
If the device is not already open, QImageReader will attempt to open the device in QIODevice::ReadOnly mode by calling open(). Note that this does not work for certain devices, such as QProcess, QTcpSocket and QUdpSocket, where more logic is required to open the device.
Definition at line 838 of file qimagereader.cpp.
Referenced by setFileName().
void QImageReader::setFileName | ( | const QString & | fileName | ) |
Sets the file name of QImageReader to fileName.
Internally, QImageReader will create a QFile object and open it in QIODevice::ReadOnly mode, and use this when reading images.
If fileName does not include a file extension (e.g., .png or .bmp), QImageReader will cycle through all supported extensions until it finds a matching file.
Definition at line 868 of file qimagereader.cpp.
void QImageReader::setFormat | ( | const QByteArray & | format | ) |
Sets the format QImageReader will use when reading images, to format.
format is a case insensitive text string. Example:
You can call supportedImageFormats() for the full list of formats QImageReader supports.
Definition at line 705 of file qimagereader.cpp.
Referenced by readImage().
void QImageReader::setQuality | ( | int | quality | ) |
This is an image format specific function that sets the quality level of the image to quality.
For image formats that do not support setting the quality, this value is ignored.
The value range of quality depends on the image format. For example, the "jpeg" format supports a quality range from 0 (low quality, high compression) to 100 (high quality, low compression).
Definition at line 905 of file qimagereader.cpp.
void QImageReader::setScaledClipRect | ( | const QRect & | rect | ) |
Sets the scaled clip rect to rect.
The scaled clip rect is the clip rect (also known as ROI, or Region Of Interest) that is applied after the image has been scaled.
Definition at line 1069 of file qimagereader.cpp.
void QImageReader::setScaledSize | ( | const QSize & | size | ) |
Sets the scaled size of the image to size.
The scaling is performed after the initial clip rect, but before the scaled clip rect is applied. The algorithm used for scaling depends on the image format. By default (i.e., if the image format does not support scaling), QImageReader will use QImage::scale() with Qt::SmoothScaling.
Definition at line 1047 of file qimagereader.cpp.
Referenced by readImage().
QSize QImageReader::size | ( | ) | const |
Returns the size of the image, without actually reading the image contents.
If the image format does not support this feature, this function returns an invalid size. Qt's built-in image handlers all support this feature, but custom image format plugins are not required to do so.
Definition at line 936 of file qimagereader.cpp.
Referenced by readImage(), and setScaledSize().
|
static |
Returns the list of image formats supported by QImageReader.
By default, Qt can read the following formats:
Format | Description |
BMP | Windows Bitmap |
GIF | Graphic Interchange Format (optional) |
JPG | Joint Photographic Experts Group |
JPEG | Joint Photographic Experts Group |
MNG | Multiple-image Network Graphics |
PNG | Portable Network Graphics |
PBM | Portable Bitmap |
PGM | Portable Graymap |
PPM | Portable Pixmap |
TIFF | Tagged Image File Format |
XBM | X11 Bitmap |
XPM | X11 Pixmap |
SVG | Scalable Vector Graphics |
TGA | Targa Image Format |
Reading and writing SVG files is supported through Qt's QtSvg Module{SVG Module}.
TGA support only extends to reading non-RLE compressed files. In particular calls to capabilities for the tga plugin returns only QImageIOPlugin::CanRead, not QImageIOPlugin::CanWrite.
To configure Qt with GIF support, pass -qt-gif
to the configure
script or check the appropriate option in the graphical installer.
Note that the QApplication instance must be created before this function is called.
Definition at line 1530 of file qimagereader.cpp.
Referenced by imageReadMimeFormats(), QImageReaderPrivate::initHandler(), and QMovie::supportedFormats().
bool QImageReader::supportsAnimation | ( | ) | const |
Returns true if the image format supports animation; otherwise, false is returned.
Definition at line 1136 of file qimagereader.cpp.
bool QImageReader::supportsOption | ( | QImageIOHandler::ImageOption | option | ) | const |
Returns true if the reader supports option; otherwise returns false.
Different image formats support different options. Call this function to determine whether a certain option is supported by the current format. For example, the PNG format allows you to embed text into the image's metadata (see text()), and the BMP format allows you to determine the image's size without loading the whole image into memory (see size()).
Definition at line 1453 of file qimagereader.cpp.
Returns the image text associated with key.
Support for this option is implemented through QImageIOHandler::Description.
Definition at line 1007 of file qimagereader.cpp.
QStringList QImageReader::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.
Support for this option is implemented through QImageIOHandler::Description.
Definition at line 988 of file qimagereader.cpp.
|
private |
Definition at line 140 of file qimagereader.h.
Referenced by autoDetectImageFormat(), backgroundColor(), canRead(), clipRect(), currentImageNumber(), currentImageRect(), decideFormatFromContent(), device(), error(), errorString(), fileName(), format(), imageCount(), imageFormat(), jumpToImage(), jumpToNextImage(), loopCount(), nextImageDelay(), QImageReader(), quality(), read(), scaledClipRect(), scaledSize(), setAutoDetectImageFormat(), setBackgroundColor(), setClipRect(), setDecideFormatFromContent(), setDevice(), setFileName(), setFormat(), setQuality(), setScaledClipRect(), setScaledSize(), size(), supportsAnimation(), supportsOption(), text(), textKeys(), and ~QImageReader().