1

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.

Community
  • 1
  • 1
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
  • Are you sure comparing the name() property is pertinent? – Martin Delille May 06 '14 at 15:01
  • @MartinDelille name() return the color hexadecimal code (ie : `#00ff00`) if the code differs, the pixel is not the same. – Thomas Ayoub May 06 '14 at 15:03
  • Try compare color componend instead: QColor::red(), QColor::blue() and QColor::green() – Martin Delille May 06 '14 at 15:04
  • Maybe the difference comes from another parameter? – Martin Delille May 06 '14 at 15:10
  • 3
    You don't mention what toolkit you use, but I figure the QImage is from here: http://qt-project.org/doc/qt-4.8/qimage.html#operator-eq-eq. It says that `operator==` would consider the images not equal if the format is different. I suspect that `grabFrameBuffer()` might give you the image in a different format than reading a BMP. – Reto Koradi May 06 '14 at 15:18
  • BTW, there's also what looks like a stray `+` sign in `QImage(+ PATH_TO_RESSOURCES, ...`. Not sure how this compiles. Couldn't you just compare with `wanted`, that you already read above? – Reto Koradi May 06 '14 at 15:20
  • @RetoKoradi I'm using Qt 5.2. You're right about the fact that I should compare with `wanted`. The + PATH... is a typo and the `operator==` works on some other bmp files compared with grabframebuffer() – Thomas Ayoub May 06 '14 at 16:41
  • @ꜱᴀᴍᴏᴛʜ Looking at the source code for `QImage::operator==` reveals that it will return false if the height, width or format of the two images are not identical. Even if `different pixel detected` is never printed out, any one of these things could be different. – RA. May 06 '14 at 18:05
  • @RA. I compare image that are identical. Furthermore, it works with other bmp files – Thomas Ayoub May 06 '14 at 18:18
  • @ꜱᴀᴍᴏᴛʜ They are obviously not identical if `QVERIFY` is failing. You need to double-check that the height, width and format are identical. Also, you are not checking all of the pixels in your `for` loop -- the indices should start at `0`, not `1`. – RA. May 06 '14 at 19:19
  • @RA. Damn. You're right. Well if you want some rep ;) – Thomas Ayoub May 07 '14 at 07:46

1 Answers1

1

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));
            }
        }
    }
}
RA.
  • 7,542
  • 1
  • 34
  • 35