diff options
Diffstat (limited to 'src/plugins/imageformats/webp/qwebphandler.cpp')
-rw-r--r-- | src/plugins/imageformats/webp/qwebphandler.cpp | 38 |
1 files changed, 22 insertions, 16 deletions
diff --git a/src/plugins/imageformats/webp/qwebphandler.cpp b/src/plugins/imageformats/webp/qwebphandler.cpp index 3a7bf43..578a701 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,19 +236,27 @@ 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; } - config.quality = m_quality < 0 ? 75 : qMin(m_quality, 100); - config.lossless = (config.quality >= 100); + int reqQuality = m_quality < 0 ? 75 : qMin(m_quality, 100); + if (reqQuality < 100) { + config.lossless = 0; + config.quality = reqQuality; + } else { + config.lossless = 1; + config.quality = 70; // For lossless, specifies compression effort; 70 is libwebp default + } + config.alpha_quality = config.quality; picture.writer = pictureWriter; picture.custom_ptr = device(); |