2014-05-06 86 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)); 
      } 
     } 
    } 
}