2014-05-06 87 views
1

我想在我的OpenGL应用程序上运行几个单元测试。这导致我在过去几个问题(OpenGL draw difference between 2 computers),但现在我知道我能做什么,不能做什么。奇怪的QImage比较结果

这里有一个小测试,我写了检查渲染:

QImage display(grabFrameBuffer()); 
QImage wanted(PATH_TO_RESSOURCES + "/file_010.bmp"); 

int Qimage_width = display.width(); 
int Qimage_height = display.height(); 
for(int i = 1; i < Qimage_width; i++) { 
    for(int j = 1; j < Qimage_height; j++) { 
     if(QColor(display.pixel(i, j)).name() != QColor(wanted.pixel(i, j)).name()) { 
      qDebug() << "different pixel detected" << i << j; 
     } 
    } 
} 
QVERIFY(wanted == display); 

的QVERIFY()失败,但消息"different pixel detected" << i << j从未显示。 如果我用Photoshop比较文件(请参阅photo.stackexchange),我找不到任何不同的像素。我有点迷路。

编辑:我正在使用Qt 5.2,如果手动更改file_010.bmp上的一个像素,将显示错误消息"different pixel detected" << i << j

+0

你确定比较name()属性是否恰当? –

+0

@MartinDelille name()返回颜色十六进制代码(即:##00ff00)如果代码不同,像素不一样。 –

+0

尝试比较颜色组分而不是:QColor :: red(),QColor :: blue()和QColor :: green() –

回答

1

QImage相等运算符会报告如果图像具有不同的格式,不同的大小和/或不同的内容,则两个QImage实例会有所不同。对于其他人可能很难理解的好处为什么是两个QImage情况是不同的,下面的函数打印出的差异是什么(虽然它可能会产生大量的输出,如果有很多不同的像素):

void displayDifferencesInImages(const QImage& image1, const QImage& image2) 
{ 
    if (image1 == image2) 
    { 
     qDebug("Images are identical"); 
     return; 
    } 

    qDebug("Found the following differences:"); 
    if (image1.size() != image2.size()) 
    { 
     qDebug(" - Image sizes are different (%dx%d vs. %dx%d)", 
       image1.width(), image1.height(), 
       image2.width(), image2.height()); 
    } 
    if (image1.format() != image2.format()) 
    { 
     qDebug(" - Image formats are different (%d vs. %d)", 
       static_cast<int>(image1.format()), 
       static_cast<int>(image2.format())); 
    } 

    int smallestWidth = qMin(image1.width(), image2.width()); 
    int smallestHeight = qMin(image1.height(), image2.height()); 

    for (int i=0; i<smallestWidth; ++i) 
    { 
     for (int j=0; j<smallestHeight; ++j) 
     { 
      if (image1.pixel(i, j) != image2.pixel(i, j)) 
      { 
       qDebug(" - Image pixel (%d, %d) is different (%x vs. %x)", 
         i, j, image1.pixel(i, j), image2.pixel(i, j)); 
      } 
     } 
    } 
}