QImage垂直翻转

QImage flipped vertically

本文关键字:垂直翻转 QImage      更新时间:2023-10-16

大家好,我有一个map_server,读取PGM文件,但在QImage上打印出它的翻转图像。下面是我的代码

  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" );

高度的"for循环"从"0"到极限(高度),我可以假设它在极限中读取的图片,直到"0"。

由于声誉的关系,我不能提供图片。但我仍然希望我能在这方面得到一些帮助。

谢谢!

JeremyEthanKoh .

在JPG和BMP之间转换时,扫描线以y反转,这是特定于BMP格式的。看来你的QImage是一个RGB的24像素位图,你直接写在它的像素图逐行。只需将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 );
  }
}