PDA

View Full Version : Shifting a QImage up by 1 pixel row



MSUdom5
30th November 2009, 21:04
I was wondering if someone could suggest an efficient way to do this:

I have a QImage of an arbitrary size. I want to very quickly transform this to a QImage of the same size with the entire image shifted up by one row of pixels. The bottom row of pixels will be the same color. In other words, I want to shift the top row of pixels out and shift in a new row at the bottom of pixels with the same color.

I can think of ways to do this, but I need an extremely fast and efficient solution. Thoughts?

Thanks in advance,
MSUdom5

wysota
30th November 2009, 21:36
The fastest way would probably be this:


QImage shiftUp(const QImage &original) {
QImage shifted(original.size(), original.format());
if(!original.colorTable().isEmpty())
shifted.setColorTable(original.colorTable());
uchar *data = original.bits();
uchar *dest = shifted.bits();
memcpy(dest, data+original.bytesPerLine(), original.numBytes()-original.bytesPerLine());
return shifted;
}

Then just fill the last row with your colour (you can probably use memset() to do that quickly).

hickscorp
7th May 2010, 11:25
If you're trying to do it without duplicating your image (e.g. working directly in your original image), you can use a convolution matrix of this vector:
0 0 0
0 0 0
0 1 0
Or more simply:
0
0
1

Pierre.