kimageio: read and apply mask for BMP images that are not 32-bit

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2023-06-10 16:38:34 +03:00
parent 92857996e5
commit 1564dd849f

View file

@ -192,7 +192,7 @@ bool ICOHandler::read(QImage *image)
// fallbacks // fallbacks
const int imagewidth = (icowidth ? icowidth : bmpwidth); const int imagewidth = (icowidth ? icowidth : bmpwidth);
const int imageheight = (icoheight ? icoheight : bmpheight); const int imageheight = (icoheight ? icoheight : (bmpheight / 2));
int imageboundary = 0; int imageboundary = 0;
QImage::Format imageformat = QImage::Format_ARGB32; QImage::Format imageformat = QImage::Format_ARGB32;
@ -232,8 +232,7 @@ bool ICOHandler::read(QImage *image)
return false; return false;
} }
if (bmpimagesize != imageboundary) { if (Q_UNLIKELY(bmpimagesize != imageboundary)) {
// data may be padded
kDebug() << "BMP and QImage bytes count mismatch" << bmpimagesize << imageboundary; kDebug() << "BMP and QImage bytes count mismatch" << bmpimagesize << imageboundary;
} }
@ -264,8 +263,24 @@ bool ICOHandler::read(QImage *image)
break; break;
} }
} }
// image is flipped vertically
// NOTE: image and mask are flipped vertically
const bool bmphasmask = (bmpbpp != 32 && bmpheight && bmpheight == (icoheight * 2));
if (bmphasmask) {
QImage bmpmask(imagewidth, imageheight, QImage::Format_Mono);
bmpmask.setColor(0, qRgba(255, 255, 255, 255)); // white
bmpmask.setColor(1, qRgba(0, 0, 0, 255)); // black
char* maskbytes = reinterpret_cast<char*>(bmpmask.bits());
if (datastream.readRawData(maskbytes, bmpmask.byteCount()) != bmpmask.byteCount()) {
kWarning() << "Could not read image mask data";
} else {
kDebug() << "Masking image";
bmpimage.setAlphaChannel(bmpmask.mirrored(false, true));
}
}
*image = bmpimage.mirrored(false, true); *image = bmpimage.mirrored(false, true);
kDebug() << "Valid BMP image" << ii; kDebug() << "Valid BMP image" << ii;
return true; return true;
} }