OSDN Git Service

reduce the size of private QImageReader and QImageWriter data pointers by not storing...
authorIvailo Monev <xakepa10@gmail.com>
Thu, 3 Mar 2022 14:24:46 +0000 (16:24 +0200)
committerIvailo Monev <xakepa10@gmail.com>
Thu, 3 Mar 2022 14:24:46 +0000 (16:24 +0200)
Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
src/gui/image/qimagereader.cpp
src/gui/image/qimagewriter.cpp

index 0b67624..fe186df 100644 (file)
@@ -93,9 +93,6 @@
 #include "qimagereader.h"
 
 #include "qbytearray.h"
-#ifdef QIMAGEREADER_DEBUG
-#include "qdebug.h"
-#endif
 #include "qfile.h"
 #include "qimage.h"
 #include "qimageiohandler.h"
 #include "qsize.h"
 #include "qcolor.h"
 #include "qvariant.h"
+#include "qapplication.h"
 
 // factory loader
 #include "qcoreapplication.h"
 #include "qkathandler_p.h"
 #include "qpnghandler_p.h"
 
+#ifdef QIMAGEREADER_DEBUG
+#  include "qdebug.h"
+#endif
+
 QT_BEGIN_NAMESPACE
 
 static QImageIOHandler *createReadHandlerHelper(QIODevice *device,
@@ -266,7 +268,6 @@ public:
 
     // error
     QImageReader::ImageReaderError imageReaderError;
-    QString errorString;
 };
 
 /*!
@@ -299,20 +300,17 @@ bool QImageReaderPrivate::initHandler()
     // check some preconditions
     if (!device) {
         imageReaderError = QImageReader::DeviceError;
-        errorString = QCoreApplication::translate("QImageReader", "Invalid device");
         return false;
     }
 
     if (!device->isOpen() && !device->open(QIODevice::ReadOnly)) {
         imageReaderError = QImageReader::FileNotFoundError;
-        errorString = QCoreApplication::translate("QImageReader", "File not found");
         return false;
     }
 
     // assign a handler
     if (!handler && (handler = createReadHandlerHelper(device, format, autoDetectImageFormat)) == 0) {
         imageReaderError = QImageReader::UnsupportedFormatError;
-        errorString = QCoreApplication::translate("QImageReader", "Unsupported image format");
         return false;
     }
     return true;
@@ -688,8 +686,7 @@ bool QImageReader::read(QImage *image)
 
     // read the image
     if (!d->handler->read(image)) {
-        d->imageReaderError = InvalidDataError;
-        d->errorString = QCoreApplication::translate("QImageReader", "Unable to read image data");
+        d->imageReaderError = QImageReader::InvalidDataError;
         return false;
     }
 
@@ -816,9 +813,25 @@ QImageReader::ImageReaderError QImageReader::error() const
 */
 QString QImageReader::errorString() const
 {
-    if (d->errorString.isEmpty())
-        return QCoreApplication::translate("QImageReader", "Unknown error");
-    return d->errorString;
+    switch (d->imageReaderError) {
+        case QImageReader::UnknownError: {
+            return QApplication::translate("QImageReader", "Unknown error");
+        }
+        case QImageReader::FileNotFoundError: {
+            return QApplication::translate("QImageReader", "File not found");
+        }
+        case QImageReader::DeviceError: {
+            return QApplication::translate("QImageReader", "Device not readable");
+        }
+        case QImageReader::UnsupportedFormatError: {
+            return QApplication::translate("QImageReader", "Unsupported image format");
+        }
+        case QImageReader::InvalidDataError: {
+            return QApplication::translate("QImageReader", "Unable to read image data");
+        }
+    }
+    Q_UNREACHABLE();
+    return QString();
 }
 
 /*!
index 337e838..5215101 100644 (file)
@@ -81,6 +81,7 @@
 #include "qset.h"
 #include "qvariant.h"
 #include "qfileinfo.h"
+#include "qapplication.h"
 
 // factory loader
 #include "qcoreapplication.h"
@@ -156,7 +157,6 @@ public:
 
     // error
     QImageWriter::ImageWriterError imageWriterError;
-    QString errorString;
 };
 
 /*!
@@ -169,8 +169,7 @@ QImageWriterPrivate::QImageWriterPrivate()
     quality(100),
     compression(1),
     gamma(0.0),
-    imageWriterError(QImageWriter::UnknownError),
-    errorString(QT_TRANSLATE_NOOP(QImageWriter, QLatin1String("Unknown error")))
+    imageWriterError(QImageWriter::UnknownError)
 {
 }
 
@@ -405,14 +404,12 @@ bool QImageWriter::canWrite() const
 {
     if (d->device && !d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {
         d->imageWriterError = QImageWriter::UnsupportedFormatError;
-        d->errorString = QT_TRANSLATE_NOOP(QImageWriter, QLatin1String("Unsupported image format"));
         return false;
     }
     if (d->device && !d->device->isOpen())
         d->device->open(QIODevice::WriteOnly);
     if (!d->device || !d->device->isWritable()) {
         d->imageWriterError = QImageWriter::DeviceError;
-        d->errorString = QT_TRANSLATE_NOOP(QImageWriter, QLatin1String("Device not writable"));
         return false;
     }
     return true;
@@ -463,7 +460,19 @@ QImageWriter::ImageWriterError QImageWriter::error() const
 */
 QString QImageWriter::errorString() const
 {
-    return d->errorString;
+    switch (d->imageWriterError) {
+        case QImageWriter::UnknownError: {
+            return QApplication::translate("QImageWriter", "Unknown error");
+        }
+        case QImageWriter::DeviceError: {
+            return QApplication::translate("QImageWriter", "Device not writable");
+        }
+        case QImageWriter::UnsupportedFormatError: {
+            return QApplication::translate("QImageWriter", "Unsupported image format");
+        }
+    }
+    Q_UNREACHABLE();
+    return QString();
 }
 
 /*!
@@ -487,7 +496,6 @@ bool QImageWriter::supportsOption(QImageIOHandler::ImageOption option) const
 {
     if (!d->handler && (d->handler = createWriteHandlerHelper(d->device, d->format)) == 0) {
         d->imageWriterError = QImageWriter::UnsupportedFormatError;
-        d->errorString = QT_TRANSLATE_NOOP(QImageWriter, QLatin1String("Unsupported image format"));
         return false;
     }