PDA

View Full Version : Pixmap transform: translate pixmap



DeepKling
22nd January 2015, 15:42
I want to shift a pixmap by say 0.5 px to the right and then use it for a label. Unfortunately, pixmap.transformed returns a "convenient" result, that is inconvenient to me:
"Internally, the transformation matrix is adjusted to compensate for unwanted translation, i.e. transformed() returns the smallest pixmap containing all transformed points of the original pixmap."

How can I shift an image by .5 px to the right? shifting a label position by .5 px doesn't work either, so I'm a bit lost here. Would be nice if there's an easy solution, I'm rather new to Qt, but I hope it's no newbie question :-)

anda_skoa
22nd January 2015, 16:22
A pixel is the smallest addressable unit of the image, shifting by half a pixel either shifts by one pixel or by zero.

Cheers,
_

ChrisW67
22nd January 2015, 19:54
I guess you could create a new, slightly larger QImage. With a QPainter draw the old image after turning on anti-aliasing and translating using the QPointF version of the function. You might then get a different image that appears to have been shifted half a pixel.

Why you would bother given the size of pixels these days...

DeepKling
23rd January 2015, 11:16
Ah, that lead to the solution, since draw doesn't accept subpixel either, I scaled up the png to 10*size, then I created a 10*larger image and pasted the huge image there, then I rescaled the image to 1/10. Thanks a lot!

Btw., I use the label as a marker, that's why I need subpixel precision.


QPixmap pixmap("CrossI.png");
QImage image(100,100,QImage::Format_ARGB32_Premultiplied) ;
QPainter p;
p.setRenderHint(QPainter::Antialiasing);
QPoint shift;

//for A
image.fill(0);

p.begin(&image);
shift.setX((Ax-floor(Ax))*10);
shift.setY((Ay-floor(Ay))*10);
p.drawPixmap(shift,pixmap);
p.end();
pixmap = QPixmap::fromImage(image.scaledToWidth(10,Qt::Smoo thTransformation),Qt::AutoColor);
ui->label_5->setPixmap(pixmap);

ChrisW67
23rd January 2015, 20:04
Ah, that lead to the solution, since draw doesn't accept subpixel either
No, but translate(QPointF point) does, and then you drawPixmap() at the (translated) origin. Not certain of the result but worth a try.

jefftee
23rd January 2015, 20:34
I would be interested in seeing the before/after images. While I'm sure you must have a valid reason for wanting to shift the image by 0.5 pixels, I'd love to see the visual difference of the result!