optimize masking of QImage from XImage

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2021-12-19 13:27:02 +02:00
parent 53e77f429e
commit 3de367fc21
4 changed files with 86 additions and 79 deletions

View file

@ -160,44 +160,7 @@ void QRasterPixmapData::fill(const QColor &color)
void QRasterPixmapData::setMask(const QBitmap &mask) void QRasterPixmapData::setMask(const QBitmap &mask)
{ {
if (mask.size().isEmpty()) { image = qt_mask_image(image, mask.toImage());
if (image.depth() != 1) { // hw: ????
image = image.convertToFormat(QImage::Format_RGB32);
}
} else {
const int w = image.width();
const int h = image.height();
switch (image.depth()) {
case 1: {
const QImage imageMask = mask.toImage().convertToFormat(image.format());
const int bpl = image.bytesPerLine();
uchar *dest = image.bits();
for (int y = 0; y < h; ++y) {
const uchar *mscan = imageMask.constScanLine(y);
uchar *tscan = QFAST_SCAN_LINE(dest, bpl, y);
for (int i = 0; i < bpl; ++i)
tscan[i] &= mscan[i];
}
break;
}
default: {
const QImage imageMask = mask.toImage().convertToFormat(QImage::Format_MonoLSB);
image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
const int bpl = image.bytesPerLine();
uchar *dest = image.bits();
for (int y = 0; y < h; ++y) {
const uchar *mscan = imageMask.constScanLine(y);
QRgb *tscan = reinterpret_cast<QRgb*>(QFAST_SCAN_LINE(dest, bpl, y));
for (int x = 0; x < w; ++x) {
if (!(mscan[x>>3] & qt_pixmap_bit_mask[x&7]))
tscan[x] = 0;
}
}
break;
}
}
}
} }
bool QRasterPixmapData::hasAlphaChannel() const bool QRasterPixmapData::hasAlphaChannel() const

View file

@ -240,9 +240,8 @@ void QX11Data::copyXImageToQImageWithMask(XImage *ximage, QImage &image, const Q
QX11Data::copyXImageToQImage(ximage, image); QX11Data::copyXImageToQImage(ximage, image);
const QImage::Format imageformat = image.format(); const QImage::Format imageformat = image.format();
QPixmap pixmap = QPixmap::fromImage(image); image = qt_mask_image(image, mask);
pixmap.setMask(QBitmap::fromImage(mask)); image = image.convertToFormat(imageformat);
image = pixmap.toImage().convertToFormat(imageformat);
} }
QT_END_NAMESPACE QT_END_NAMESPACE

View file

@ -41,6 +41,46 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
// avoid going through QImage::scanLine() which calls detach
#define QFAST_SCAN_LINE(data, bpl, y) (data + y * bpl)
static const uchar qt_pixmap_bit_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
static const uint qt_bayer_matrix[16][16] = {
{ 0x1, 0xc0, 0x30, 0xf0, 0xc, 0xcc, 0x3c, 0xfc,
0x3, 0xc3, 0x33, 0xf3, 0xf, 0xcf, 0x3f, 0xff},
{ 0x80, 0x40, 0xb0, 0x70, 0x8c, 0x4c, 0xbc, 0x7c,
0x83, 0x43, 0xb3, 0x73, 0x8f, 0x4f, 0xbf, 0x7f},
{ 0x20, 0xe0, 0x10, 0xd0, 0x2c, 0xec, 0x1c, 0xdc,
0x23, 0xe3, 0x13, 0xd3, 0x2f, 0xef, 0x1f, 0xdf},
{ 0xa0, 0x60, 0x90, 0x50, 0xac, 0x6c, 0x9c, 0x5c,
0xa3, 0x63, 0x93, 0x53, 0xaf, 0x6f, 0x9f, 0x5f},
{ 0x8, 0xc8, 0x38, 0xf8, 0x4, 0xc4, 0x34, 0xf4,
0xb, 0xcb, 0x3b, 0xfb, 0x7, 0xc7, 0x37, 0xf7},
{ 0x88, 0x48, 0xb8, 0x78, 0x84, 0x44, 0xb4, 0x74,
0x8b, 0x4b, 0xbb, 0x7b, 0x87, 0x47, 0xb7, 0x77},
{ 0x28, 0xe8, 0x18, 0xd8, 0x24, 0xe4, 0x14, 0xd4,
0x2b, 0xeb, 0x1b, 0xdb, 0x27, 0xe7, 0x17, 0xd7},
{ 0xa8, 0x68, 0x98, 0x58, 0xa4, 0x64, 0x94, 0x54,
0xab, 0x6b, 0x9b, 0x5b, 0xa7, 0x67, 0x97, 0x57},
{ 0x2, 0xc2, 0x32, 0xf2, 0xe, 0xce, 0x3e, 0xfe,
0x1, 0xc1, 0x31, 0xf1, 0xd, 0xcd, 0x3d, 0xfd},
{ 0x82, 0x42, 0xb2, 0x72, 0x8e, 0x4e, 0xbe, 0x7e,
0x81, 0x41, 0xb1, 0x71, 0x8d, 0x4d, 0xbd, 0x7d},
{ 0x22, 0xe2, 0x12, 0xd2, 0x2e, 0xee, 0x1e, 0xde,
0x21, 0xe1, 0x11, 0xd1, 0x2d, 0xed, 0x1d, 0xdd},
{ 0xa2, 0x62, 0x92, 0x52, 0xae, 0x6e, 0x9e, 0x5e,
0xa1, 0x61, 0x91, 0x51, 0xad, 0x6d, 0x9d, 0x5d},
{ 0xa, 0xca, 0x3a, 0xfa, 0x6, 0xc6, 0x36, 0xf6,
0x9, 0xc9, 0x39, 0xf9, 0x5, 0xc5, 0x35, 0xf5},
{ 0x8a, 0x4a, 0xba, 0x7a, 0x86, 0x46, 0xb6, 0x76,
0x89, 0x49, 0xb9, 0x79, 0x85, 0x45, 0xb5, 0x75},
{ 0x2a, 0xea, 0x1a, 0xda, 0x26, 0xe6, 0x16, 0xd6,
0x29, 0xe9, 0x19, 0xd9, 0x25, 0xe5, 0x15, 0xd5},
{ 0xaa, 0x6a, 0x9a, 0x5a, 0xa6, 0x66, 0x96, 0x56,
0xa9, 0x69, 0x99, 0x59, 0xa5, 0x65, 0x95, 0x55}
};
/******************************************************************************* /*******************************************************************************
* QSpan * QSpan
* *
@ -398,43 +438,50 @@ inline void qt_memconvert(DST *dest, const SRC *src, int count)
static inline int qt_div_255(int x) { return (x + (x>>8) + 0x80) >> 8; } static inline int qt_div_255(int x) { return (x + (x>>8) + 0x80) >> 8; }
static const uint qt_bayer_matrix[16][16] = { static inline QImage qt_mask_image(const QImage &image, const QImage &mask)
{ 0x1, 0xc0, 0x30, 0xf0, 0xc, 0xcc, 0x3c, 0xfc, {
0x3, 0xc3, 0x33, 0xf3, 0xf, 0xcf, 0x3f, 0xff}, if (mask.size().isEmpty()) {
{ 0x80, 0x40, 0xb0, 0x70, 0x8c, 0x4c, 0xbc, 0x7c, if (image.depth() != 1) { // hw: ????
0x83, 0x43, 0xb3, 0x73, 0x8f, 0x4f, 0xbf, 0x7f}, return image.convertToFormat(QImage::Format_RGB32);
{ 0x20, 0xe0, 0x10, 0xd0, 0x2c, 0xec, 0x1c, 0xdc, }
0x23, 0xe3, 0x13, 0xd3, 0x2f, 0xef, 0x1f, 0xdf}, return image;
{ 0xa0, 0x60, 0x90, 0x50, 0xac, 0x6c, 0x9c, 0x5c, }
0xa3, 0x63, 0x93, 0x53, 0xaf, 0x6f, 0x9f, 0x5f},
{ 0x8, 0xc8, 0x38, 0xf8, 0x4, 0xc4, 0x34, 0xf4,
0xb, 0xcb, 0x3b, 0xfb, 0x7, 0xc7, 0x37, 0xf7},
{ 0x88, 0x48, 0xb8, 0x78, 0x84, 0x44, 0xb4, 0x74,
0x8b, 0x4b, 0xbb, 0x7b, 0x87, 0x47, 0xb7, 0x77},
{ 0x28, 0xe8, 0x18, 0xd8, 0x24, 0xe4, 0x14, 0xd4,
0x2b, 0xeb, 0x1b, 0xdb, 0x27, 0xe7, 0x17, 0xd7},
{ 0xa8, 0x68, 0x98, 0x58, 0xa4, 0x64, 0x94, 0x54,
0xab, 0x6b, 0x9b, 0x5b, 0xa7, 0x67, 0x97, 0x57},
{ 0x2, 0xc2, 0x32, 0xf2, 0xe, 0xce, 0x3e, 0xfe,
0x1, 0xc1, 0x31, 0xf1, 0xd, 0xcd, 0x3d, 0xfd},
{ 0x82, 0x42, 0xb2, 0x72, 0x8e, 0x4e, 0xbe, 0x7e,
0x81, 0x41, 0xb1, 0x71, 0x8d, 0x4d, 0xbd, 0x7d},
{ 0x22, 0xe2, 0x12, 0xd2, 0x2e, 0xee, 0x1e, 0xde,
0x21, 0xe1, 0x11, 0xd1, 0x2d, 0xed, 0x1d, 0xdd},
{ 0xa2, 0x62, 0x92, 0x52, 0xae, 0x6e, 0x9e, 0x5e,
0xa1, 0x61, 0x91, 0x51, 0xad, 0x6d, 0x9d, 0x5d},
{ 0xa, 0xca, 0x3a, 0xfa, 0x6, 0xc6, 0x36, 0xf6,
0x9, 0xc9, 0x39, 0xf9, 0x5, 0xc5, 0x35, 0xf5},
{ 0x8a, 0x4a, 0xba, 0x7a, 0x86, 0x46, 0xb6, 0x76,
0x89, 0x49, 0xb9, 0x79, 0x85, 0x45, 0xb5, 0x75},
{ 0x2a, 0xea, 0x1a, 0xda, 0x26, 0xe6, 0x16, 0xd6,
0x29, 0xe9, 0x19, 0xd9, 0x25, 0xe5, 0x15, 0xd5},
{ 0xaa, 0x6a, 0x9a, 0x5a, 0xa6, 0x66, 0x96, 0x56,
0xa9, 0x69, 0x99, 0x59, 0xa5, 0x65, 0x95, 0x55}
};
// avoid going through QImage::scanLine() which calls detach const int w = image.width();
#define QFAST_SCAN_LINE(data, bpl, y) (data + y * bpl) const int h = image.height();
QImage result(image);
switch (result.depth()) {
case 1: {
const QImage imageMask = mask.convertToFormat(result.format());
const int bpl = result.bytesPerLine();
uchar *dest = result.bits();
for (int y = 0; y < h; ++y) {
const uchar *mscan = imageMask.constScanLine(y);
uchar *tscan = QFAST_SCAN_LINE(dest, bpl, y);
for (int i = 0; i < bpl; ++i)
tscan[i] &= mscan[i];
}
break;
}
default: {
const QImage imageMask = mask.convertToFormat(QImage::Format_MonoLSB);
result = result.convertToFormat(QImage::Format_ARGB32_Premultiplied);
const int bpl = result.bytesPerLine();
uchar *dest = result.bits();
for (int y = 0; y < h; ++y) {
const uchar *mscan = imageMask.constScanLine(y);
QRgb *tscan = reinterpret_cast<QRgb*>(QFAST_SCAN_LINE(dest, bpl, y));
for (int x = 0; x < w; ++x) {
if (!(mscan[x>>3] & qt_pixmap_bit_mask[x&7]))
tscan[x] = 0;
}
}
break;
}
}
return result;
}
QT_END_NAMESPACE QT_END_NAMESPACE

View file

@ -604,8 +604,6 @@ Q_GLOBAL_STATIC_WITH_ARGS(QFactoryLoader, imageloader,
(QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats"))) (QImageIOHandlerFactoryInterface_iid, QLatin1String("/imageformats")))
#endif #endif
static const uchar qt_pixmap_bit_mask[] = { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80 };
#ifndef QT_NO_TABBAR #ifndef QT_NO_TABBAR
inline static bool verticalTabs(QTabBar::Shape shape) inline static bool verticalTabs(QTabBar::Shape shape)
{ {