[Qt-interest] QPainter::drawImage behaviour, a bug?
Mihail Naydenov
mlists at ymail.com
Fri Jun 11 14:08:37 CEST 2010
Yes, Qt will pre-multiply your images, try using QImage::Format_ARGB32_Premultiplied instead.
----- Original Message ----
> From: nsf <no.smile.face at gmail.com>
> To: qt-interest at trolltech.com
> Sent: Fri, June 11, 2010 2:01:26 PM
> Subject: [Qt-interest] QPainter::drawImage behaviour, a bug?
>
> Hello.
I have the following case:
One image is being composed from
> multiple smaller images and I use
QPainter::drawImage for that. The problem
> is, even if QPainter has
CompositionMode == Source and both source/dest
> rectangles have the same
width/height and images have the same format
> (ARGB32), the resulting
image slightly differs from the original
> image.
Small example source
> code:
------------------------------------------------------------------------
#include
> <QtCore>
#include <QtGui>
static const unsigned char
> test_png[] = {
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00,
> 0x00, 0x0d,
0x49, 0x48, 0x44, 0x52, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
> 0x10,
0x08, 0x06, 0x00, 0x00, 0x00, 0x1f, 0xf3, 0xff, 0x61, 0x00, 0x00,
> 0x00,
0x19, 0x74, 0x45, 0x58, 0x74, 0x53, 0x6f, 0x66, 0x74, 0x77, 0x61,
> 0x72,
0x65, 0x00, 0x41, 0x64, 0x6f, 0x62, 0x65, 0x20, 0x49, 0x6d, 0x61,
> 0x67,
0x65, 0x52, 0x65, 0x61, 0x64, 0x79, 0x71, 0xc9, 0x65, 0x3c, 0x00,
> 0x00,
0x00, 0xeb, 0x49, 0x44, 0x41, 0x54, 0x78, 0xda, 0x62, 0xfc, 0xff,
> 0xff,
0x3f, 0x03, 0x25, 0x80, 0xa5, 0x65, 0x75, 0xd4, 0x34, 0x20, 0x9d,
> 0x49,
0xa6, 0xfe, 0xe9, 0x2c, 0x7f, 0xff, 0xfe, 0xcd, 0x34, 0x56, 0x76,
> 0x21,
0x4b, 0xf7, 0xd9, 0xbb, 0x7b, 0x32, 0x59, 0xfe, 0xfc, 0xfe, 0x03,
> 0x62,
0x90, 0x65, 0x00, 0x48, 0x2f, 0xd0, 0x80, 0xdf, 0x0c, 0x8f, 0xef,
> 0x3f,
0x24, 0xcb, 0x00, 0x49, 0x19, 0x29, 0x06, 0x96, 0xdf, 0xbf, 0x7e,
> 0x31,
0x14, 0x45, 0xf7, 0x90, 0x65, 0xc0, 0xa2, 0xdd, 0x1d, 0x0c, 0x2c,
> 0xbf,
0x80, 0x06, 0x7c, 0xfa, 0xf6, 0x8a, 0xe1, 0xfc, 0xbd, 0x6d, 0x60,
> 0x41,
0x26, 0x46, 0x66, 0x06, 0x26, 0x26, 0x08, 0x66, 0x66, 0x62, 0x01,
> 0x62,
0x56, 0x30, 0x66, 0x61, 0x46, 0xa5, 0x45, 0x78, 0xe5, 0x19, 0x40,
> 0x7a,
0x99, 0x7e, 0xfd, 0xfc, 0x05, 0x37, 0x91, 0x58, 0xcd, 0xac, 0xcc,
> 0xec,
0x60, 0xf5, 0x20, 0xbd, 0x40, 0x03, 0x7e, 0x92, 0xac, 0x19, 0xc4,
> 0x86,
0x18, 0xf0, 0x13, 0x68, 0xc0, 0x0f, 0x88, 0x0b, 0x48, 0xd1, 0xcc,
> 0xc2,
0xcc, 0x06, 0x31, 0xe0, 0x07, 0xb2, 0x0b, 0x48, 0xd0, 0xcc, 0x02,
> 0xf7,
0xc2, 0x4f, 0x44, 0x18, 0x90, 0xa2, 0x99, 0x85, 0x85, 0x0d, 0x29,
> 0x0c,
0x7e, 0xc1, 0x0c, 0x20, 0x5e, 0x33, 0x2b, 0xcc, 0x0b, 0xa0, 0x58,
> 0xf8,
0x8d, 0x64, 0x00, 0xb1, 0x9a, 0x59, 0x98, 0x20, 0x06, 0x80, 0xf4,
> 0x82,
0x53, 0x22, 0x08, 0x28, 0x89, 0x1b, 0x13, 0x4c, 0x38, 0xff, 0xff,
> 0x02,
0x6d, 0xfd, 0xfb, 0x8b, 0x01, 0x04, 0x21, 0x49, 0xf9, 0x37, 0x03,
> 0xa3,
0x4d, 0x38, 0x0f, 0x45, 0xb9, 0x11, 0x20, 0xc0, 0x00, 0x88, 0xd4,
> 0x69,
0x15, 0xeb, 0x38, 0xa6, 0x79, 0x00, 0x00, 0x00, 0x00, 0x49, 0x45,
> 0x4e,
0x44, 0xae, 0x42, 0x60, 0x82,
};
static bool theSame(const
> QImage &img1, const QImage &img2)
{
if
> (img1.width() != img2.width() || img1.height() !=
> img2.height())
return
> false;
for (int i = 0; i < img1.height(); ++i)
> {
QRgb *pixels1 =
> (QRgb*)img1.scanLine(i);
QRgb *pixels2
> = (QRgb*)img2.scanLine(i);
for (int j =
> 0; j < img1.width(); ++j) {
> bool sameColor =
> qRed(*pixels1) ==
> qRed(*pixels2) &&
> qGreen(*pixels1) == qGreen(*pixels2)
> &&
> qBlue(*pixels1) == qBlue(*pixels2)
> &&
> qAlpha(*pixels1) == qAlpha(*pixels2);
> bool meaningfulAlpha = qAlpha(*pixels1)
> > 0 || qAlpha(*pixels2) > 0;
> // we don't care about the color if alpha is
> zero
if
> (meaningfulAlpha && !sameColor) {
> qDebug() <<
> "pixel mismatch:"
> << "first:"
>
> << qRed(*pixels1) << qGreen(*pixels1) << qBlue(*pixels1)
> << qAlpha(*pixels1)
> <<
> "second:"
> << qRed(*pixels2) <<
> qGreen(*pixels2) << qBlue(*pixels2) <<
> qAlpha(*pixels2);
> return false;
> }
> pixels1++;
> pixels2++;
> }
}
return true;
}
int
> main(int argc, char **argv)
{
QImage test =
> QImage::fromData(test_png, sizeof(test_png), "PNG");
> qDebug("image loaded: %dx%d", test.width(),
> test.height());
// explicitly convert to a required
> format
test =
> test.convertToFormat(QImage::Format_ARGB32);
qDebug()
> << "should be true:" << theSame(test, test); // just to make
> sure
// same format as image
> above
QImage canvas(test.width(), test.height(),
> QImage::Format_ARGB32);
// create painter with
> CompositionMode == Source (e.g. exact copy)
QPainter
> painter(&canvas);
> painter.setCompositionMode(QPainter::CompositionMode_Source);
> painter.drawImage(QPoint(0,0), test);
qDebug()
> << theSame(test, canvas);
return
> 0;
}
------------------------------------------------------------------------
Output:
>
image loaded: 16x16
should be true: true
pixel mismatch: first: 131
> 170 89 150 second: 130 169 88 150
>
false
------------------------------------------------------------------------
As
> you can see there is a slight mismatch between pixels. My guess is
that it's
> happening because raster paint engine uses premultiplied
alpha format when
> doing this operation. Because such mismatch happens
only when alpha !=
> 255.
So.. the question is: should this be considered as bug or should
> I
write my own copy function using memcpy in that case? Or maybe there
> is
another way to do what I want using painting API?
Note: in the
> example above images are equal in width/height and I can
use QImage::copy
> obviously, but in my app - destination image is bigger
than source
> one.
_______________________________________________
Qt-interest
> mailing list
> href="mailto:Qt-interest at trolltech.com">Qt-interest at trolltech.com
http://lists.trolltech.com/mailman/listinfo/qt-interest
More information about the Qt-interest-old
mailing list