qimagereader.cpp

来自「奇趣公司比较新的qt/emd版本」· C++ 代码 · 共 1,290 行 · 第 1/3 页

CPP
1,290
字号
/*!    \since 4.1    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.    \sa setBackgroundColor(), read()*/QColor QImageReader::backgroundColor() const{    if (!d->initHandler())        return QColor();    if (d->handler->supportsOption(QImageIOHandler::BackgroundColor))        return qVariantValue<QColor>(d->handler->option(QImageIOHandler::BackgroundColor));    return QColor();}/*!    \since 4.1    Returns true if the image format supports animation;    otherwise, false is returned.    \sa QMovie::supportedFormats()*/bool QImageReader::supportsAnimation() const{    if (!d->initHandler())        return false;    if (d->handler->supportsOption(QImageIOHandler::Animation))        return d->handler->option(QImageIOHandler::Animation).toBool();    return false;}/*!    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.    \sa read(), supportedImageFormats()*/bool QImageReader::canRead() const{    if (!d->initHandler())        return false;    return d->handler->canRead();}/*!    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.    \sa canRead(), supportedImageFormats(), supportsAnimation(), QMovie*/QImage QImageReader::read(){    // Because failed image reading might have side effects, we explicitly    // return a null image instead of the image we've just created.    QImage image;    return read(&image) ? image : QImage();}/*!    \overload    Reads an image from the device into \a image, which must point to a    QImage. Returns true on success; otherwise, returns false.    If \a 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.    \code        QImage icon(64, 64, QImage::Format_RGB32);        QImageReader reader("icon_64x64.bmp");        if (reader.read(&icon)) {            // Display icon        }    \endcode    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.    \sa canRead(), supportedImageFormats(), supportsAnimation(), QMovie*/bool QImageReader::read(QImage *image){    if (!image) {        qWarning("QImageReader::read: cannot read into null pointer");        return false;    }    if (!d->handler && !d->initHandler())        return false;    // set the handler specific options.    if (d->handler->supportsOption(QImageIOHandler::ScaledSize) && d->scaledSize.isValid()) {        if ((d->handler->supportsOption(QImageIOHandler::ClipRect) && !d->clipRect.isNull())            || d->clipRect.isNull()) {            // Only enable the ScaledSize option if there is no clip rect, or            // if the handler also supports ClipRect.            d->handler->setOption(QImageIOHandler::ScaledSize, d->scaledSize);        }    }    if (d->handler->supportsOption(QImageIOHandler::ClipRect) && !d->clipRect.isNull())        d->handler->setOption(QImageIOHandler::ClipRect, d->clipRect);    if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull())        d->handler->setOption(QImageIOHandler::ScaledClipRect, d->scaledClipRect);    if (d->handler->supportsOption(QImageIOHandler::Quality))        d->handler->setOption(QImageIOHandler::Quality, d->quality);    // read the image    if (!d->handler->read(image)) {        d->imageReaderError = InvalidDataError;        d->errorString = QLatin1String(QT_TRANSLATE_NOOP(QImageReader, "Unable to read image data"));        return false;    }    // provide default implementations for any unsupported image    // options    if (d->handler->supportsOption(QImageIOHandler::ClipRect) && !d->clipRect.isNull()) {        if (d->handler->supportsOption(QImageIOHandler::ScaledSize) && d->scaledSize.isValid()) {            if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) {                // all features are supported by the handler; nothing to do.            } else {                // the image is already scaled, so apply scaled clipping.                if (!d->scaledClipRect.isNull())                    *image = image->copy(d->scaledClipRect);            }        } else {            if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) {                // supports scaled clipping but not scaling, most                // likely a broken handler.            } else {                if (d->scaledSize.isValid()) {                    *image = image->scaled(d->scaledSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);                }                if (d->scaledClipRect.isValid()) {                    *image = image->copy(d->scaledClipRect);                }            }        }    } else {        if (d->handler->supportsOption(QImageIOHandler::ScaledSize) && d->scaledSize.isValid()) {            // in this case, there's nothing we can do. if the            // plugin supports scaled size but not ClipRect, then            // we have to ignore ClipRect."            if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) {                // nothing to do (ClipRect is ignored!)            } else {                // provide all workarounds.                if (d->scaledClipRect.isValid()) {                    *image = image->copy(d->scaledClipRect);                }            }        } else {            if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) {                // this makes no sense; a handler that supports                // ScaledClipRect but not ScaledSize is broken, and we                // can't work around it.            } else {                // provide all workarounds.                if (d->clipRect.isValid())                    *image = image->copy(d->clipRect);                if (d->scaledSize.isValid())                    *image = image->scaled(d->scaledSize, Qt::IgnoreAspectRatio, Qt::SmoothTransformation);                if (d->scaledClipRect.isValid())                    *image = image->copy(d->scaledClipRect);            }        }    }    return true;}/*!   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.   \sa jumpToImage(), QImageIOHandler::jumpToNextImage()*/bool QImageReader::jumpToNextImage(){    if (!d->initHandler())        return false;    return d->handler->jumpToNextImage();}/*!   For image formats that support animation, this function skips to the image   whose sequence number is \a 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.   \sa jumpToNextImage(), QImageIOHandler::jumpToImage()*/bool QImageReader::jumpToImage(int imageNumber){    if (!d->initHandler())        return false;    return d->handler->jumpToImage(imageNumber);}/*!    For image formats that support animation, this function returns    the number of times the animation should loop. Otherwise, it    returns -1.    \sa supportsAnimation(), QImageIOHandler::loopCount()*/int QImageReader::loopCount() const{    if (!d->initHandler())        return -1;    return d->handler->loopCount();}/*!    For image formats that support animation, this function returns    the total number of images in the animation.    Certain animation formats do not support this feature, in which    case 0 is returned.    \sa supportsAnimation(), QImageIOHandler::imageCount()*/int QImageReader::imageCount() const{    if (!d->initHandler())        return -1;    return d->handler->imageCount();}/*!    For image formats that support animation, this function returns    the number of milliseconds to wait until displaying the next frame    in the animation. Otherwise, 0 is returned.    \sa supportsAnimation(), QImageIOHandler::nextImageDelay()*/int QImageReader::nextImageDelay() const{    if (!d->initHandler())        return -1;    return d->handler->nextImageDelay();}/*!    For image formats that support animation, this function returns    the sequence number of the current frame. Otherwise, -1 is    returned.    \sa supportsAnimation(), QImageIOHandler::currentImageNumber()*/int QImageReader::currentImageNumber() const{    if (!d->initHandler())        return -1;    return d->handler->currentImageNumber();}/*!    For image formats that support animation, this function returns    the rect for the current frame. Otherwise, a null rect is returned.    \sa supportsAnimation(), QImageIOHandler::currentImageRect()*/QRect QImageReader::currentImageRect() const{    if (!d->initHandler())        return QRect();    return d->handler->currentImageRect();}/*!    Returns the type of error that occurred last.    \sa ImageReaderError, errorString()*/QImageReader::ImageReaderError QImageReader::error() const{    return d->imageReaderError;}/*!    Returns a human readable description of the last error that    occurred.    \sa error()*/QString QImageReader::errorString() const{    return d->errorString;}/*!    \since 4.2    Returns true if the reader supports \a 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()).    \code        QImageReader reader(":/image.png");        if (reader.supportsOption(QImageIOHandler::Size))            qDebug() << "Size:" << reader.size();    \endcode    \sa QImageWriter::supportsOption()*/bool QImageReader::supportsOption(QImageIOHandler::ImageOption option) const{    if (!d->initHandler())        return false;    return d->handler->supportsOption(option);}/*!    If supported, this function returns the image format of the file    \a fileName. Otherwise, an empty string is returned.*/QByteArray QImageReader::imageFormat(const QString &fileName){    QFile file(fileName);    if (!file.open(QFile::ReadOnly))        return QByteArray();    return imageFormat(&file);}/*!    If supported, this function returns the image format of the device    \a device. Otherwise, an empty string is returned.*/QByteArray QImageReader::imageFormat(QIODevice *device){    QByteArray format;    QImageIOHandler *handler = ::createReadHandler(device, format);    if (handler) {        if (handler->canRead())            format = handler->format();        delete handler;    }    return format;}/*!    Returns the list of image formats supported by QImageReader.    By default, Qt can read the following formats:    \table    \header \o Format \o Description    \row    \o BMP    \o Windows Bitmap    \row    \o GIF    \o Graphic Interchange Format (optional)    \row    \o JPG    \o Joint Photographic Experts Group    \row    \o JPEG   \o Joint Photographic Experts Group    \row    \o MNG    \o Multiple-image Network Graphics    \row    \o PNG    \o Portable Network Graphics    \row    \o PBM    \o Portable Bitmap    \row    \o PGM    \o Portable Graymap    \row    \o PPM    \o Portable Pixmap    \row    \o TIFF   \o Tagged Image File Format    \row    \o XBM    \o X11 Bitmap    \row    \o XPM    \o X11 Pixmap    \endtable    To configure Qt with GIF support, pass \c -qt-gif to the \c    configure script or check the appropriate option in the graphical    installer.    \sa setFormat(), QImageWriter::supportedImageFormats(), QImageIOPlugin*/QList<QByteArray> QImageReader::supportedImageFormats(){    QSet<QByteArray> formats;    for (int i = 0; i < _qt_NumFormats; ++i)        formats << _qt_BuiltInFormats[i].extension;#ifndef QT_NO_LIBRARY    QFactoryLoader *l = loader();    QStringList keys = l->keys();    for (int i = 0; i < keys.count(); ++i) {        QImageIOPlugin *plugin = qobject_cast<QImageIOPlugin *>(l->instance(keys.at(i)));        if (plugin && plugin->capabilities(0, keys.at(i).toLatin1()) & QImageIOPlugin::CanRead)            formats << keys.at(i).toLatin1();    }#endif // QT_NO_LIBRARY    QList<QByteArray> sortedFormats;    for (QSet<QByteArray>::ConstIterator it = formats.constBegin(); it != formats.constEnd(); ++it)        sortedFormats << *it;    qSort(sortedFormats);    return sortedFormats;}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?