Qt-验证加载的图像是否为灰度

Qt-验证加载的图像是否为灰度,qt,qt4,qt5,grayscale,qimage,Qt,Qt4,Qt5,Grayscale,Qimage,我已经用C++/Qt开发了一个小型GUI,我想知道一种快速测试加载的图像是否为灰度的方法。实际上,当我加载gif格式的灰度图像时,我希望它被识别为深度()为8的灰度图像,而当我加载彩色gif图像时,QImage的深度将为32 以下是我的开放式方法: void ImageViewer::open() { int status_off = 0; fileName = QFileDialog::getOpenFileName(this, tr("Open File"), QDir::curre

我已经用C++/Qt开发了一个小型GUI,我想知道一种快速测试加载的图像是否为灰度的方法。实际上,当我加载gif格式的灰度图像时,我希望它被识别为深度()为8的灰度图像,而当我加载彩色gif图像时,QImage的深度将为32

以下是我的开放式方法:

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(文件名)首先从图像内容之前的格式(此处为gif)继承。因此,QImage在两种情况下的深度()等于32


如何区分这两个gif图像(一个灰度图像和另一个彩色图像)

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