summaryrefslogtreecommitdiffstats
path: root/src/plugins/imageformats/webp/qwebphandler.cpp
diff options
context:
space:
mode:
authorEirik Aavitsland <eirik.aavitsland@qt.io>2018-05-25 16:15:14 +0200
committerEirik Aavitsland <eirik.aavitsland@qt.io>2018-05-28 11:15:48 +0000
commitb9ba2217566531b51f3c5c5158bfcc386539d810 (patch)
tree9922d820dcebdc3eec29e2a8f4416ce0de1896bd /src/plugins/imageformats/webp/qwebphandler.cpp
parentaa332026488e8e441a4187898534c59cd657e70e (diff)
webp handler: support alpha-less reading and writing
Webp files can be with or without alpha channel. The handler would ignore this and read all as Format_ARGB32 images, and write all as having alpha, in both cases losing that important bit of information. As a driveby, simplify the endianness handling in write(). By always converting the source image to an endianness-independent QImage format, no special handling is required. Task-number: QTBUG-48628 Change-Id: I624ed72b18a8b59a542979efcb4e8ff81214e0d7 Reviewed-by: Liang Qi <liang.qi@qt.io>
Diffstat (limited to 'src/plugins/imageformats/webp/qwebphandler.cpp')
-rw-r--r--src/plugins/imageformats/webp/qwebphandler.cpp27
1 files changed, 13 insertions, 14 deletions
diff --git a/src/plugins/imageformats/webp/qwebphandler.cpp b/src/plugins/imageformats/webp/qwebphandler.cpp
index 3a7bf43..12c4001 100644
--- a/src/plugins/imageformats/webp/qwebphandler.cpp
+++ b/src/plugins/imageformats/webp/qwebphandler.cpp
@@ -174,7 +174,8 @@ bool QWebpHandler::read(QImage *image)
if (status != VP8_STATUS_OK)
return false;
- QImage frame(m_iter.width, m_iter.height, QImage::Format_ARGB32);
+ QImage::Format format = m_features.has_alpha ? QImage::Format_ARGB32 : QImage::Format_RGB32;
+ QImage frame(m_iter.width, m_iter.height, format);
uint8_t *output = frame.bits();
size_t output_size = frame.sizeInBytes();
#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
@@ -219,13 +220,10 @@ bool QWebpHandler::write(const QImage &image)
}
QImage srcImage = image;
-#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
- if (srcImage.format() != QImage::Format_ARGB32)
- srcImage = srcImage.convertToFormat(QImage::Format_ARGB32);
-#else /* Q_BIG_ENDIAN */
- if (srcImage.format() != QImage::Format_RGBA8888)
- srcImage = srcImage.convertToFormat(QImage::Format_RGBA8888);
-#endif
+ bool alpha = srcImage.hasAlphaChannel();
+ QImage::Format newFormat = alpha ? QImage::Format_RGBA8888 : QImage::Format_RGB888;
+ if (srcImage.format() != newFormat)
+ srcImage = srcImage.convertToFormat(newFormat);
WebPPicture picture;
WebPConfig config;
@@ -238,13 +236,14 @@ bool QWebpHandler::write(const QImage &image)
picture.width = srcImage.width();
picture.height = srcImage.height();
picture.use_argb = 1;
-#if Q_BYTE_ORDER == Q_LITTLE_ENDIAN
- if (!WebPPictureImportBGRA(&picture, srcImage.bits(), srcImage.bytesPerLine())) {
-#else /* Q_BIG_ENDIAN */
- if (!WebPPictureImportRGBA(&picture, srcImage.bits(), srcImage.bytesPerLine())) {
-#endif
- qWarning() << "failed to import image data to webp picture.";
+ bool failed = false;
+ if (alpha)
+ failed = !WebPPictureImportRGBA(&picture, srcImage.bits(), srcImage.bytesPerLine());
+ else
+ failed = !WebPPictureImportRGB(&picture, srcImage.bits(), srcImage.bytesPerLine());
+ if (failed) {
+ qWarning() << "failed to import image data to webp picture.";
WebPPictureFree(&picture);
return false;
}