2 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
20 #include <QtCore/qmath.h>
22 #include <QApplication>
26 #include <QStringList>
30 int main(int argc, char* argv[])
32 QCoreApplication app(argc, argv);
36 QStringList args = app.arguments();
37 for (int i = 0; i < argc; ++i)
38 if (args[i] == "-t" || args[i] == "--tolerance")
39 tolerance = args[i + 1].toDouble();
45 while (fgets(buffer, sizeof(buffer), stdin)) {
47 char* newLineCharacter = strchr(buffer, '\n');
49 *newLineCharacter = '\0';
51 if (!strncmp("Content-Length: ", buffer, 16)) {
53 int imageSize = strtol(strtok(0, " "), 0, 10);
56 fputs("error, image size must be specified.\n", stdout);
58 unsigned char buffer[2048];
61 // Read all the incoming chunks
62 data.open(QBuffer::WriteOnly);
63 while (imageSize > 0) {
64 size_t bytesToRead = qMin(imageSize, 2048);
65 size_t bytesRead = fread(buffer, 1, bytesToRead, stdin);
66 data.write(reinterpret_cast<const char*>(buffer), bytesRead);
67 imageSize -= static_cast<int>(bytesRead);
70 // Convert into QImage
72 decodedImage.loadFromData(data.data(), "PNG");
73 decodedImage.convertToFormat(QImage::Format_ARGB32);
75 // Place it in the right place
76 if (actualImage.isNull())
77 actualImage = decodedImage;
79 baselineImage = decodedImage;
83 if (!actualImage.isNull() && !baselineImage.isNull()) {
85 if (actualImage.size() != baselineImage.size()) {
86 fprintf(stderr, "error, test and reference image have different properties.\n");
90 int w = actualImage.width();
91 int h = actualImage.height();
92 QImage diffImage(w, h, QImage::Format_ARGB32);
96 qreal maxDistance = 0;
98 for (int x = 0; x < w; ++x)
99 for (int y = 0; y < h; ++y) {
100 QRgb pixel = actualImage.pixel(x, y);
101 QRgb basePixel = baselineImage.pixel(x, y);
102 qreal red = (qRed(pixel) - qRed(basePixel)) / static_cast<float>(qMax(255 - qRed(basePixel), qRed(basePixel)));
103 qreal green = (qGreen(pixel) - qGreen(basePixel)) / static_cast<float>(qMax(255 - qGreen(basePixel), qGreen(basePixel)));
104 qreal blue = (qBlue(pixel) - qBlue(basePixel)) / static_cast<float>(qMax(255 - qBlue(basePixel), qBlue(basePixel)));
105 qreal alpha = (qAlpha(pixel) - qAlpha(basePixel)) / static_cast<float>(qMax(255 - qAlpha(basePixel), qAlpha(basePixel)));
106 qreal distance = qSqrt(red * red + green * green + blue * blue + alpha * alpha) / 2.0f;
107 int gray = distance * qreal(255);
108 diffImage.setPixel(x, y, qRgb(gray, gray, gray));
109 if (distance >= 1 / qreal(255)) {
112 maxDistance = qMax(maxDistance, distance);
116 qreal difference = 0;
118 difference = 100 * sum / static_cast<qreal>(w * h);
119 if (difference <= tolerance) {
122 difference = qRound(difference * 100) / 100;
123 difference = qMax(difference, qreal(0.01));
127 fprintf(stdout, "diff: %01.2f%% passed\n", difference);
130 buffer.open(QBuffer::WriteOnly);
131 diffImage.save(&buffer, "PNG");
133 const QByteArray &data = buffer.data();
134 printf("Content-Length: %lu\n", static_cast<unsigned long>(data.length()));
136 // We have to use the return value of fwrite to avoid "ignoring return value" gcc warning
137 // See https://bugs.webkit.org/show_bug.cgi?id=45384 for details.
138 if (fwrite(data.constData(), 1, data.length(), stdout)) {}
140 fprintf(stdout, "diff: %01.2f%% failed\n", difference);
145 actualImage = QImage();
146 baselineImage = QImage();