• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2     Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies)
3 
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.
8 
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.
13 
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.
18 */
19 
20 #include <QApplication>
21 #include <QBuffer>
22 #include <QByteArray>
23 #include <QImage>
24 #include <QStringList>
25 
26 #include <math.h>
27 #include <stdio.h>
28 
main(int argc,char * argv[])29 int main(int argc, char* argv[])
30 {
31     QCoreApplication app(argc, argv);
32 
33     qreal tolerance = 0;
34 
35     QStringList args = app.arguments();
36     for (int i = 0; i < argc; ++i)
37         if (args[i] == "-t" || args[i] == "--tolerance")
38             tolerance = args[i + 1].toDouble();
39 
40     char buffer[2048];
41     QImage actualImage;
42     QImage baselineImage;
43 
44     while (fgets(buffer, sizeof(buffer), stdin)) {
45         // remove the CR
46         char* newLineCharacter = strchr(buffer, '\n');
47         if (newLineCharacter)
48             *newLineCharacter = '\0';
49 
50         if (!strncmp("Content-Length: ", buffer, 16)) {
51             strtok(buffer, " ");
52             int imageSize = strtol(strtok(0, " "), 0, 10);
53 
54             if (imageSize <= 0) {
55                 fputs("error, image size must be specified.\n", stdout);
56             } else {
57                 unsigned char buffer[2048];
58                 QBuffer data;
59 
60                 // Read all the incoming chunks
61                 data.open(QBuffer::WriteOnly);
62                 while (imageSize > 0) {
63                     size_t bytesToRead = qMin(imageSize, 2048);
64                     size_t bytesRead = fread(buffer, 1, bytesToRead, stdin);
65                     data.write(reinterpret_cast<const char*>(buffer), bytesRead);
66                     imageSize -= static_cast<int>(bytesRead);
67                 }
68 
69                 // Convert into QImage
70                 QImage decodedImage;
71                 decodedImage.loadFromData(data.data(), "PNG");
72                 decodedImage.convertToFormat(QImage::Format_ARGB32);
73 
74                 // Place it in the right place
75                 if (actualImage.isNull())
76                     actualImage = decodedImage;
77                 else
78                     baselineImage = decodedImage;
79             }
80         }
81 
82         if (!actualImage.isNull() && !baselineImage.isNull()) {
83 
84             if (actualImage.size() != baselineImage.size()) {
85                 fprintf(stderr, "error, test and reference image have different properties.\n");
86                 fflush(stderr);
87             } else {
88 
89                 int w = actualImage.width();
90                 int h = actualImage.height();
91                 QImage diffImage(w, h, QImage::Format_ARGB32);
92 
93                 int count = 0;
94                 qreal sum = 0;
95                 qreal maxDistance = 0;
96 
97                 for (int x = 0; x < w; ++x)
98                     for (int y = 0; y < h; ++y) {
99                         QRgb pixel = actualImage.pixel(x, y);
100                         QRgb basePixel = baselineImage.pixel(x, y);
101                         qreal red = (qRed(pixel) - qRed(basePixel)) / static_cast<float>(qMax(255 - qRed(basePixel), qRed(basePixel)));
102                         qreal green = (qGreen(pixel) - qGreen(basePixel)) / static_cast<float>(qMax(255 - qGreen(basePixel), qGreen(basePixel)));
103                         qreal blue = (qBlue(pixel) - qBlue(basePixel)) / static_cast<float>(qMax(255 - qBlue(basePixel), qBlue(basePixel)));
104                         qreal alpha = (qAlpha(pixel) - qAlpha(basePixel)) / static_cast<float>(qMax(255 - qAlpha(basePixel), qAlpha(basePixel)));
105                         qreal distance = sqrt(red * red + green * green + blue * blue + alpha * alpha) / 2.0f;
106                         int gray = distance * qreal(255);
107                         diffImage.setPixel(x, y, qRgb(gray, gray, gray));
108                         if (distance >= 1 / qreal(255)) {
109                             count++;
110                             sum += distance;
111                             maxDistance = qMax(maxDistance, distance);
112                         }
113                 }
114 
115                 qreal difference = 0;
116                 if (count)
117                     difference = 100 * sum / static_cast<qreal>(w * h);
118                 if (difference <= tolerance) {
119                     difference = 0;
120                 } else {
121                     difference = round(difference * 100) / 100;
122                     difference = qMax(difference, qreal(0.01));
123                 }
124 
125                 if (!count) {
126                     fprintf(stdout, "diff: %01.2f%% passed\n", difference);
127                 } else {
128                     QBuffer buffer;
129                     buffer.open(QBuffer::WriteOnly);
130                     diffImage.save(&buffer, "PNG");
131                     buffer.close();
132                     const QByteArray &data = buffer.data();
133                     printf("Content-Length: %lu\n", static_cast<unsigned long>(data.length()));
134                     fwrite(data.constData(), 1, data.length(), stdout);
135 
136                     fprintf(stdout, "diff: %01.2f%% failed\n", difference);
137                 }
138 
139                 fflush(stdout);
140             }
141         }
142     }
143 
144     return 0;
145 }
146