2013-02-27 51 views
-1

我已经开发了一个小的GUI在C + +/Qt,我想知道一个快速的方式来测试如果加载图像是灰度。实际上,当我加载gif格式的灰度图像时,我希望它被识别为depth()= 8的灰度图像,并且当我加载彩色gif图像时,QImage的深度将为32.Qt - 验证如果图像加载灰度

这里是我开的方法:

void ImageViewer::open() 
{ 
    int status_off = 0; 
    fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::currentPath()); 

    if (!fileName.isEmpty()) { 
    current_qimage = QImage(fileName); 
    if (current_qimage.isNull()) { 
     QMessageBox::information(this, tr("Image Viewer"), 
      tr("Cannot load %1.").arg(fileName)); 
     return; 
    } 
    updateStatus(status_off); 
    // Image is colored with depth=8 
    if (current_qimage.depth()/8 != 4) 
     {rgblum_status = 0;} 
     else // Image is grayscale with depth=32 
     {rgblum_status = 1;} 

    loadImage(); 
    } 
} 

从我第一次测试,似乎current_qimage,在current_qimage = QImage(fileName);首先从格式(GIF这里)图像的内容之前继承。因此,QImage在两种情况下的深度()等于32.

如何区分这两个gif图像(一个灰度和其他颜色)?

回答

2

QImage类有一个函数,您可以调用以测试图像是否灰度:QImage::isGrayscale()。它适用于8位色表索引图像和32位图像。