kimgio: simplify WebP image writing

the last piece of code not written by me in the file

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-10-20 00:17:31 +03:00
parent 1b0dcc4f10
commit fe342fa103
2 changed files with 38 additions and 65 deletions

View file

@ -1,23 +1,19 @@
/* /* This file is part of the KDE libraries
QImageIO Routines to read/write WebP images. Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
Copyright (c) 2012,2013 Martin Koller <kollix@aon.at> This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2, as published by the Free Software Foundation.
This library is free software; you can redistribute it and/or This library is distributed in the hope that it will be useful,
modify it under the terms of the GNU Lesser General Public but WITHOUT ANY WARRANTY; without even the implied warranty of
License as published by the Free Software Foundation; either MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
version 2.1 of the License, or (at your option) version 3, or any Library General Public License for more details.
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful, You should have received a copy of the GNU Library General Public License
but WITHOUT ANY WARRANTY; without even the implied warranty of along with this library; see the file COPYING.LIB. If not, write to
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Lesser General Public License for more details. Boston, MA 02110-1301, USA.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "webp.h" #include "webp.h"
@ -107,17 +103,13 @@ bool WebPHandler::read(QImage *image)
#if Q_BYTE_ORDER == Q_BIG_ENDIAN #if Q_BYTE_ORDER == Q_BIG_ENDIAN
const uint8_t* webpoutput = WebPDecodeARGBInto( const uint8_t* webpoutput = WebPDecodeARGBInto(
webpiter.fragment.bytes, webpiter.fragment.size,
reinterpret_cast<uint8_t*>(image->bits()), image->byteCount(),
image->bytesPerLine()
);
#else #else
const uint8_t* webpoutput = WebPDecodeBGRAInto( const uint8_t* webpoutput = WebPDecodeBGRAInto(
#endif
webpiter.fragment.bytes, webpiter.fragment.size, webpiter.fragment.bytes, webpiter.fragment.size,
reinterpret_cast<uint8_t*>(image->bits()), image->byteCount(), reinterpret_cast<uint8_t*>(image->bits()), image->byteCount(),
image->bytesPerLine() image->bytesPerLine()
); );
#endif
if (Q_UNLIKELY(!webpoutput)) { if (Q_UNLIKELY(!webpoutput)) {
kWarning() << "Could not decode image"; kWarning() << "Could not decode image";
WebPDemuxReleaseIterator(&webpiter); WebPDemuxReleaseIterator(&webpiter);
@ -145,34 +137,19 @@ bool WebPHandler::write(const QImage &image)
return false; return false;
} }
QImage image32 = image; QImage image32 = image.convertToFormat(QImage::Format_ARGB32);
if (image32.depth() != 32) {
image32 = image32.convertToFormat(QImage::Format_RGB32);
}
size_t idx = 0;
uint8_t *webpimagedata = new uint8_t[image32.width() * image32.height() * (3 + image32.hasAlphaChannel())];
for (int y = 0; y < image32.height(); y++) {
const QRgb *scanline = reinterpret_cast<const QRgb*>(image32.constScanLine(y));
for (int x = 0; x < image32.width(); x++) {
webpimagedata[idx++] = qRed(scanline[x]);
webpimagedata[idx++] = qGreen(scanline[x]);
webpimagedata[idx++] = qBlue(scanline[x]);
if (image32.hasAlphaChannel()) {
webpimagedata[idx++] = qAlpha(scanline[x]);
}
}
}
size_t webpsize = 0;
uint8_t *webpoutput = nullptr; uint8_t *webpoutput = nullptr;
if (image32.hasAlphaChannel()) { #if Q_BYTE_ORDER == Q_BIG_ENDIAN
webpsize = WebPEncodeRGBA(webpimagedata, image32.width(), image32.height(), image32.width() * 4, m_quality, &webpoutput); const size_t webpsize = WebPEncodeRGBA(
} else { #else
webpsize = WebPEncodeRGB(webpimagedata, image32.width(), image32.height(), image32.width() * 3, m_quality, &webpoutput); const size_t webpsize = WebPEncodeBGRA(
} #endif
delete []webpimagedata; image32.constBits(),
image32.width(), image32.height(), image32.width() * 4,
m_quality,
&webpoutput
);
if (Q_UNLIKELY(webpsize == 0)) { if (Q_UNLIKELY(webpsize == 0)) {
kWarning() << "Could not encode image"; kWarning() << "Could not encode image";

View file

@ -1,23 +1,19 @@
/* /* This file is part of the KDE libraries
QImageIO Routines to read/write WebP images. Copyright (C) 2022 Ivailo Monev <xakepa10@gmail.com>
Copyright (c) 2012,2013 Martin Koller <kollix@aon.at> This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2, as published by the Free Software Foundation.
This library is free software; you can redistribute it and/or This library is distributed in the hope that it will be useful,
modify it under the terms of the GNU Lesser General Public but WITHOUT ANY WARRANTY; without even the implied warranty of
License as published by the Free Software Foundation; either MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
version 2.1 of the License, or (at your option) version 3, or any Library General Public License for more details.
later version accepted by the membership of KDE e.V. (or its
successor approved by the membership of KDE e.V.), which shall
act as a proxy defined in Section 6 of version 3 of the license.
This library is distributed in the hope that it will be useful, You should have received a copy of the GNU Library General Public License
but WITHOUT ANY WARRANTY; without even the implied warranty of along with this library; see the file COPYING.LIB. If not, write to
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Lesser General Public License for more details. Boston, MA 02110-1301, USA.
You should have received a copy of the GNU Lesser General Public
License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef WEBP_H #ifndef WEBP_H