2011-01-19 64 views
1

这个24比特图像我有一个像这样限定的无符号字符数组:如何输出Qt中

unsigned char **chars = new unsigned char *[H]; 
for(int i = 0; i < H; i++) 
{ 

    chars[i] = new unsigned char[W*3]; 
} 

其中H是图像的高度,W是宽度,字符是填充在该函数的其余部分循环遍历输入图像的行x列。字符填充顺序红绿蓝

我试图像这样的东西来阅读:

QImage *qi = new QImage(imwidth, imheight, QImage::Format_RGB888); 
    for (int i = 0 ; i < imheight ; i++) 
     for (int j = 0 ; j < imwidth ; j++) 
     {  //not sure why, but have to flip j and i in the index for setPixel 
       qi->setPixel(j,i,qRgb(imageData[i][j],imageData[i][j+1],imageData[i][j+2])); 
       j+=3; 

      } 

    QPixmap p(QPixmap::fromImage(*qi,Qt::AutoColor)); 
    QPixmap p1(p.scaled(ui->menuBar->width(),ui->menuBar->width(), Qt::KeepAspectRatio, Qt::SmoothTransformation)); 
    ui->viewLabel->setPixmap(p1); 
    ui->viewLabel->setFixedHeight(p1.height()); 
    ui->viewLabel->setFixedWidth(p1.width()); 

其中字符被送回的imageData数组中此调用函数。

我在做什么错?我应该使用不同的格式,即使我明确地为每个像素分配3个无符号字符(这就是我选择RGB888格式的原因)。张贴此代码返回的图像,但它显示错误 - 局部扰乱,冲了出去,等

感谢

+0

我没有看到任何提及什么是错的?崩溃?空白图像? – 2011-01-19 01:40:31

+0

我将添加信息 - 在问题中的代码给了我错误的图像。它看起来乱七八糟并且被冲走,但我可以看到我原始图像的痕迹。 – Derek 2011-01-19 02:45:02

回答

4

你可以不复制的每一个像素创建QImage directly from a block of data

由于您的图像存储与单独的行,你需要像

QImage image = new QImage(width,height,QImage::RGB888) 

for (int h=0;h<height;h++) { 
    // scanLine returns a ptr to the start of the data for that row 
    memcpy(image.scanLine(h),chars[h],width*3); 
} 

如果你使用RGB32,那么你需要手动对每个像素设置为0xFF alpha通道 - )只是memset的(整个数据到0xff第一