QImageReader: Reduce code redundancy

Refactor methods to simplify the code and improve readability.
Replace calls to initHandler() and checks for handler support
with calls to the supportsOption() method directly.
Reduces code redundancy and improves the overall structure of the class.

Additionally, use supportScaledSize, supportClipRect, and supportScaledClipRect
to streamline the conditional logic and make it more understandable.

Change-Id: I77d5dab8bcdd0b0eb6d7e9edcf87751aa03b80c7
Reviewed-by: Eirik Aavitsland <eirik.aavitsland@qt.io>
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
This commit is contained in:
Mike Chen 2023-11-02 13:19:49 +08:00
parent 6ea2a12e27
commit 6a873a6edf

View File

@ -558,7 +558,7 @@ bool QImageReaderPrivate::initHandler()
*/ */
void QImageReaderPrivate::getText() void QImageReaderPrivate::getText()
{ {
if (text.isEmpty() && initHandler() && handler->supportsOption(QImageIOHandler::Description)) if (text.isEmpty() && q->supportsOption(QImageIOHandler::Description))
text = qt_getImageTextFromDescription(handler->option(QImageIOHandler::Description).toString()); text = qt_getImageTextFromDescription(handler->option(QImageIOHandler::Description).toString());
} }
@ -848,10 +848,7 @@ int QImageReader::quality() const
*/ */
QSize QImageReader::size() const QSize QImageReader::size() const
{ {
if (!d->initHandler()) if (supportsOption(QImageIOHandler::Size))
return QSize();
if (d->handler->supportsOption(QImageIOHandler::Size))
return d->handler->option(QImageIOHandler::Size).toSize(); return d->handler->option(QImageIOHandler::Size).toSize();
return QSize(); return QSize();
@ -871,10 +868,7 @@ QSize QImageReader::size() const
*/ */
QImage::Format QImageReader::imageFormat() const QImage::Format QImageReader::imageFormat() const
{ {
if (!d->initHandler()) if (supportsOption(QImageIOHandler::ImageFormat))
return QImage::Format_Invalid;
if (d->handler->supportsOption(QImageIOHandler::ImageFormat))
return (QImage::Format)d->handler->option(QImageIOHandler::ImageFormat).toInt(); return (QImage::Format)d->handler->option(QImageIOHandler::ImageFormat).toInt();
return QImage::Format_Invalid; return QImage::Format_Invalid;
@ -996,9 +990,7 @@ QRect QImageReader::scaledClipRect() const
*/ */
void QImageReader::setBackgroundColor(const QColor &color) void QImageReader::setBackgroundColor(const QColor &color)
{ {
if (!d->initHandler()) if (supportsOption(QImageIOHandler::BackgroundColor))
return;
if (d->handler->supportsOption(QImageIOHandler::BackgroundColor))
d->handler->setOption(QImageIOHandler::BackgroundColor, color); d->handler->setOption(QImageIOHandler::BackgroundColor, color);
} }
@ -1013,9 +1005,7 @@ void QImageReader::setBackgroundColor(const QColor &color)
*/ */
QColor QImageReader::backgroundColor() const QColor QImageReader::backgroundColor() const
{ {
if (!d->initHandler()) if (supportsOption(QImageIOHandler::BackgroundColor))
return QColor();
if (d->handler->supportsOption(QImageIOHandler::BackgroundColor))
return qvariant_cast<QColor>(d->handler->option(QImageIOHandler::BackgroundColor)); return qvariant_cast<QColor>(d->handler->option(QImageIOHandler::BackgroundColor));
return QColor(); return QColor();
} }
@ -1030,9 +1020,7 @@ QColor QImageReader::backgroundColor() const
*/ */
bool QImageReader::supportsAnimation() const bool QImageReader::supportsAnimation() const
{ {
if (!d->initHandler()) if (supportsOption(QImageIOHandler::Animation))
return false;
if (d->handler->supportsOption(QImageIOHandler::Animation))
return d->handler->option(QImageIOHandler::Animation).toBool(); return d->handler->option(QImageIOHandler::Animation).toBool();
return false; return false;
} }
@ -1044,10 +1032,7 @@ bool QImageReader::supportsAnimation() const
*/ */
QByteArray QImageReader::subType() const QByteArray QImageReader::subType() const
{ {
if (!d->initHandler()) if (supportsOption(QImageIOHandler::SubType))
return QByteArray();
if (d->handler->supportsOption(QImageIOHandler::SubType))
return d->handler->option(QImageIOHandler::SubType).toByteArray(); return d->handler->option(QImageIOHandler::SubType).toByteArray();
return QByteArray(); return QByteArray();
} }
@ -1059,10 +1044,7 @@ QByteArray QImageReader::subType() const
*/ */
QList<QByteArray> QImageReader::supportedSubTypes() const QList<QByteArray> QImageReader::supportedSubTypes() const
{ {
if (!d->initHandler()) if (supportsOption(QImageIOHandler::SupportedSubTypes))
return QList<QByteArray>();
if (d->handler->supportsOption(QImageIOHandler::SupportedSubTypes))
return qvariant_cast<QList<QByteArray> >(d->handler->option(QImageIOHandler::SupportedSubTypes)); return qvariant_cast<QList<QByteArray> >(d->handler->option(QImageIOHandler::SupportedSubTypes));
return QList<QByteArray>(); return QList<QByteArray>();
} }
@ -1078,7 +1060,7 @@ QList<QByteArray> QImageReader::supportedSubTypes() const
QImageIOHandler::Transformations QImageReader::transformation() const QImageIOHandler::Transformations QImageReader::transformation() const
{ {
int option = QImageIOHandler::TransformationNone; int option = QImageIOHandler::TransformationNone;
if (d->initHandler() && d->handler->supportsOption(QImageIOHandler::ImageTransformation)) if (supportsOption(QImageIOHandler::ImageTransformation))
option = d->handler->option(QImageIOHandler::ImageTransformation).toInt(); option = d->handler->option(QImageIOHandler::ImageTransformation).toInt();
return QImageIOHandler::Transformations(option); return QImageIOHandler::Transformations(option);
} }
@ -1196,20 +1178,23 @@ bool QImageReader::read(QImage *image)
if (!d->initHandler()) if (!d->initHandler())
return false; return false;
const bool supportScaledSize = supportsOption(QImageIOHandler::ScaledSize) && d->scaledSize.isValid();
const bool supportClipRect = supportsOption(QImageIOHandler::ClipRect) && !d->clipRect.isNull();
const bool supportScaledClipRect = supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull();
// set the handler specific options. // set the handler specific options.
if (d->handler->supportsOption(QImageIOHandler::ScaledSize) && d->scaledSize.isValid()) { if (supportScaledSize) {
if ((d->handler->supportsOption(QImageIOHandler::ClipRect) && !d->clipRect.isNull()) if (supportClipRect || d->clipRect.isNull()) {
|| d->clipRect.isNull()) {
// Only enable the ScaledSize option if there is no clip rect, or // Only enable the ScaledSize option if there is no clip rect, or
// if the handler also supports ClipRect. // if the handler also supports ClipRect.
d->handler->setOption(QImageIOHandler::ScaledSize, d->scaledSize); d->handler->setOption(QImageIOHandler::ScaledSize, d->scaledSize);
} }
} }
if (d->handler->supportsOption(QImageIOHandler::ClipRect) && !d->clipRect.isNull()) if (supportClipRect)
d->handler->setOption(QImageIOHandler::ClipRect, d->clipRect); d->handler->setOption(QImageIOHandler::ClipRect, d->clipRect);
if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) if (supportScaledClipRect)
d->handler->setOption(QImageIOHandler::ScaledClipRect, d->scaledClipRect); d->handler->setOption(QImageIOHandler::ScaledClipRect, d->scaledClipRect);
if (d->handler->supportsOption(QImageIOHandler::Quality)) if (supportsOption(QImageIOHandler::Quality))
d->handler->setOption(QImageIOHandler::Quality, d->quality); d->handler->setOption(QImageIOHandler::Quality, d->quality);
// read the image // read the image
@ -1230,9 +1215,9 @@ bool QImageReader::read(QImage *image)
// provide default implementations for any unsupported image // provide default implementations for any unsupported image
// options // options
if (d->handler->supportsOption(QImageIOHandler::ClipRect) && !d->clipRect.isNull()) { if (supportClipRect) {
if (d->handler->supportsOption(QImageIOHandler::ScaledSize) && d->scaledSize.isValid()) { if (supportScaledSize) {
if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) { if (supportScaledClipRect) {
// all features are supported by the handler; nothing to do. // all features are supported by the handler; nothing to do.
} else { } else {
// the image is already scaled, so apply scaled clipping. // the image is already scaled, so apply scaled clipping.
@ -1240,7 +1225,7 @@ bool QImageReader::read(QImage *image)
*image = image->copy(d->scaledClipRect); *image = image->copy(d->scaledClipRect);
} }
} else { } else {
if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) { if (supportScaledClipRect) {
// supports scaled clipping but not scaling, most // supports scaled clipping but not scaling, most
// likely a broken handler. // likely a broken handler.
} else { } else {
@ -1253,8 +1238,8 @@ bool QImageReader::read(QImage *image)
} }
} }
} else { } else {
if (d->handler->supportsOption(QImageIOHandler::ScaledSize) && d->scaledSize.isValid() && d->clipRect.isNull()) { if (supportScaledSize && d->clipRect.isNull()) {
if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) { if (supportScaledClipRect) {
// nothing to do (ClipRect is ignored!) // nothing to do (ClipRect is ignored!)
} else { } else {
// provide all workarounds. // provide all workarounds.
@ -1263,7 +1248,7 @@ bool QImageReader::read(QImage *image)
} }
} }
} else { } else {
if (d->handler->supportsOption(QImageIOHandler::ScaledClipRect) && !d->scaledClipRect.isNull()) { if (supportScaledClipRect) {
// this makes no sense; a handler that supports // this makes no sense; a handler that supports
// ScaledClipRect but not ScaledSize is broken, and we // ScaledClipRect but not ScaledSize is broken, and we
// can't work around it. // can't work around it.