PDA

View Full Version : Vertically flipped QImage.



xtrememoo
7th August 2014, 08:28
Hello guys, I've a map_server that reads in a PGM file yet prints out a flipped image of it on a QImage. Here is my code.



int width = msg->info.width;
int height = msg->info.height;
const std::vector<int8_t>& data (msg->data);
unsigned char *p, *q;

if( mapImage == NULL ) {
lineImage = new unsigned char[ width * 3 ];
mapImage = new QImage( width, height, QImage::Format_RGB888 );
}

if( mapImage != NULL ) {
for( int i = 0; i < height; i++ ) {
q = lineImage;
p = ( unsigned char * )&data[ i * width ];
for( int j = 0; j < width; j++ ) {
*q++ = *p;
*q++ = *p;
*q++ = *p;
p++;
}
memcpy( mapImage->scanLine( i ), lineImage, width * 3 );
}
}

printf( "Map received!\n" );


The "for loop" for height takes in from "0" till the limit(height) and I can assume that the picture it reads in the limit, till "0".
But everything seems to be in placed and correct. I'm lost to what I can do to correct this "wrong".

Would greatly appreciate your help! Thanks.

JeremyEthanKoh.

Ginsengelf
8th August 2014, 07:59
Hi, it's not directly related to your code, but I think you should be able to flip the image using a QMatrix and QImage::transformed().

Ginsengelf

stampede
8th August 2014, 09:08
Maybe your input data is flipped.

d_stranz
8th August 2014, 17:22
The "for loop" for height takes in from "0" till the limit(height) and I can assume that the picture it reads in the limit, till "0".
But everything seems to be in placed and correct. I'm lost to what I can do to correct this "wrong".

QImage (and all other Qt objects with pixel-based coordinates) assign pixel (0, 0) to the upper left corner. Your map server apparently assumes that the (0, 0) map coordinate is in the lower left corner. This would make sense for a geographical map.

You can fix this by assigning your scan lines from the top down:


memcpy( mapImage->scanLine( height - i - 1 ), lineImage, width * 3 );


------------------
If the original designers of the oscilloscope (and its descendent, the television) had designed it to draw the scan lines from the bottom up, instead of from the top down, we wouldn't have this (0,0) at the top left corner problem. (0,0) would be at the bottom right where it makes sense, and we wouldn't be faced with having to flip y coordinates for everything we draw. It wouldn't have been hard - all they would have had to do is change the polarity on the CRT deflection plates. See how decisions made nearly a century ago still haunt us?

xtrememoo
11th August 2014, 03:36
Yep that's what I needed! Thanks so much. :)