Fix incorrect QImage transformation when its devicePixelRatio != 1

QImage::transformed compensates for unwanted translation. This compensation
is performed in "pixel space". However, a possible code path to perform the
transformation uses QPainter which is devicePixelRatio-aware and expects the
transformation matrix to be in logical coordinates.

For example, image.transformed(QTransform().rotate(45)) will result in
cropped out image if devicePixelRatio == 2.

Change-Id: I830ff3ffa25531d842dc9c77f1d0e8d4bd502c9d
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@theqtcompany.com>
bb10
MihailNaydenov 2014-09-26 12:23:55 +03:00 committed by Tor Arne Vestbø
parent d47b9ace50
commit dc583a0576
1 changed files with 9 additions and 2 deletions

View File

@ -4480,7 +4480,6 @@ QImage QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode
dImage.d->dpmx = dotsPerMeterX();
dImage.d->dpmy = dotsPerMeterY();
dImage.d->devicePixelRatio = devicePixelRatio();
switch (bpp) {
// initizialize the data
@ -4502,13 +4501,19 @@ QImage QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode
}
if (target_format >= QImage::Format_RGB32) {
// Prevent QPainter from applying devicePixelRatio corrections
const QImage sImage = (devicePixelRatio() != 1) ? QImage(constBits(), width(), height(), format()) : *this;
Q_ASSERT(sImage.devicePixelRatio() == 1);
Q_ASSERT(sImage.devicePixelRatio() == dImage.devicePixelRatio());
QPainter p(&dImage);
if (mode == Qt::SmoothTransformation) {
p.setRenderHint(QPainter::Antialiasing);
p.setRenderHint(QPainter::SmoothPixmapTransform);
}
p.setTransform(mat);
p.drawImage(QPoint(0, 0), *this);
p.drawImage(QPoint(0, 0), sImage);
} else {
bool invertible;
mat = mat.inverted(&invertible); // invert matrix
@ -4520,6 +4525,8 @@ QImage QImage::transformed(const QTransform &matrix, Qt::TransformationMode mode
int dbpl = dImage.bytesPerLine();
qt_xForm_helper(mat, 0, type, bpp, dImage.bits(), dbpl, 0, hd, sptr, sbpl, ws, hs);
}
dImage.d->devicePixelRatio = devicePixelRatio();
return dImage;
}