Question

I want to run a few unit tests on my OpenGL application. Which cause me a few issue in the past (OpenGL draw difference between 2 computers) but now I know what I can and cannot do.

Here's a little test I wrote to check the rendering:

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);

The QVERIFY() fails but the message "different pixel detected" << i << j is never shown. If I compare the files with Photoshop (see photo.stackexchange), I can't find any different pixel. I'm kind of lost.

Edit : I'm using Qt 5.2 and if I change manually one pixel on file_010.bmp the error message "different pixel detected" << i << j is displayed.

Was it helpful?

Solution

The QImage equality operator will report that two QImage instances are different if the images have different formats, different sizes and/or different contents. For the benefit of others that might have trouble understanding why two QImage instances are different, the following function prints out what the differences are (though it may generate a lot of output if there are a lot of differing pixels):

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));
            }
        }
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top