0

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".

I can't provide Images due to the reputation. But I still hope I could garner some help in this...

Thanks!

JeremyEthanKoh.

hotandkoh
  • 3
  • 3
  • 1
    I admit guessing here, but if screen coordinate system would be used in one image only it could cause it. – user2672165 Aug 07 '14 at 06:04
  • @user2672165 What do you mean? But anyway it is a separate thing isn't it..? It reads pixels by pixels then, printing it out pixels by pixels and the only problem is that the starting first pixel is different. Ah man, I don't even know myself. – hotandkoh Aug 07 '14 at 08:37
  • Have a look at this http://stackoverflow.com/questions/8346115/why-are-bmps-stored-upside-down – user2672165 Aug 07 '14 at 10:57

1 Answers1

1

When converting between JPG and BMP scan lines are inverted in Y. This is specific to the BMP format. It seems that your QImage is a RGB 24-bil bitmap and you directly write in its pixel map line by line. Just reverse the scanlines in Y:

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( height-i-1 ), lineImage, width * 3 );
  }
}
Baj Mile
  • 750
  • 1
  • 8
  • 17
  • omg WOW. Thanks man! You saved my life. But could you do me a favour by explaining why is it ( height-i-1 )? :) @Baj Mile – hotandkoh Aug 08 '14 at 02:45