| OLD | NEW |
| (Empty) |
| 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 <QtCore/qmath.h> | |
| 21 | |
| 22 #include <QApplication> | |
| 23 #include <QBuffer> | |
| 24 #include <QByteArray> | |
| 25 #include <QImage> | |
| 26 #include <QStringList> | |
| 27 | |
| 28 #include <stdio.h> | |
| 29 | |
| 30 int main(int argc, char* argv[]) | |
| 31 { | |
| 32 QCoreApplication app(argc, argv); | |
| 33 | |
| 34 qreal tolerance = 0; // Tolerated percentage of error pixels. | |
| 35 | |
| 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(); | |
| 40 | |
| 41 char buffer[2048]; | |
| 42 QImage actualImage; | |
| 43 QImage baselineImage; | |
| 44 | |
| 45 while (fgets(buffer, sizeof(buffer), stdin)) { | |
| 46 // remove the CR | |
| 47 char* newLineCharacter = strchr(buffer, '\n'); | |
| 48 if (newLineCharacter) | |
| 49 *newLineCharacter = '\0'; | |
| 50 | |
| 51 if (!strncmp("Content-Length: ", buffer, 16)) { | |
| 52 strtok(buffer, " "); | |
| 53 int imageSize = strtol(strtok(0, " "), 0, 10); | |
| 54 | |
| 55 if (imageSize <= 0) { | |
| 56 fputs("error, image size must be specified.\n", stdout); | |
| 57 } else { | |
| 58 unsigned char buffer[2048]; | |
| 59 QBuffer data; | |
| 60 | |
| 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); | |
| 68 } | |
| 69 | |
| 70 // Convert into QImage | |
| 71 QImage decodedImage; | |
| 72 decodedImage.loadFromData(data.data(), "PNG"); | |
| 73 decodedImage = decodedImage.convertToFormat(QImage::Format_ARGB3
2); | |
| 74 | |
| 75 // Place it in the right place | |
| 76 if (actualImage.isNull()) | |
| 77 actualImage = decodedImage; | |
| 78 else | |
| 79 baselineImage = decodedImage; | |
| 80 } | |
| 81 } | |
| 82 | |
| 83 if (!actualImage.isNull() && !baselineImage.isNull()) { | |
| 84 | |
| 85 if (actualImage.size() != baselineImage.size()) { | |
| 86 fprintf(stdout, "diff: 100%% failed\n"); | |
| 87 fprintf(stderr, "error, test and reference image have different
properties.\n"); | |
| 88 fflush(stderr); | |
| 89 fflush(stdout); | |
| 90 } else { | |
| 91 | |
| 92 int w = actualImage.width(); | |
| 93 int h = actualImage.height(); | |
| 94 QImage diffImage(w, h, QImage::Format_ARGB32); | |
| 95 | |
| 96 int errorCount = 0; | |
| 97 | |
| 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_cas
t<float>(qMax(255 - qRed(basePixel), qRed(basePixel))); | |
| 103 qreal green = (qGreen(pixel) - qGreen(basePixel)) / stat
ic_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)) / stat
ic_cast<float>(qMax(255 - qAlpha(basePixel), qAlpha(basePixel))); | |
| 106 qreal distance = qSqrt(red * red + green * green + blue
* blue + alpha * alpha) / 2.0f; | |
| 107 if (distance >= 1 / qreal(255)) { | |
| 108 errorCount++; | |
| 109 diffImage.setPixel(x, y, qRgb(255, 0, 0)); | |
| 110 } else | |
| 111 diffImage.setPixel(x, y, qRgba(qRed(basePixel), qGre
en(basePixel), qBlue(basePixel), qAlpha(basePixel) * 0.5)); | |
| 112 } | |
| 113 } | |
| 114 | |
| 115 qreal difference = 0; | |
| 116 if (errorCount) | |
| 117 difference = 100 * errorCount / static_cast<qreal>(w * h); | |
| 118 | |
| 119 if (difference <= tolerance) | |
| 120 fprintf(stdout, "diff: %01.2f%% passed\n", difference); | |
| 121 else { | |
| 122 QBuffer buffer; | |
| 123 buffer.open(QBuffer::WriteOnly); | |
| 124 diffImage.save(&buffer, "PNG"); | |
| 125 buffer.close(); | |
| 126 const QByteArray &data = buffer.data(); | |
| 127 printf("Content-Length: %lu\n", static_cast<unsigned long>(d
ata.length())); | |
| 128 | |
| 129 // We have to use the return value of fwrite to avoid "ignor
ing return value" gcc warning | |
| 130 // See https://bugs.webkit.org/show_bug.cgi?id=45384 for det
ails. | |
| 131 if (fwrite(data.constData(), 1, data.length(), stdout)) {} | |
| 132 | |
| 133 fprintf(stdout, "diff: %01.2f%% failed\n", difference); | |
| 134 } | |
| 135 | |
| 136 fflush(stdout); | |
| 137 } | |
| 138 actualImage = QImage(); | |
| 139 baselineImage = QImage(); | |
| 140 } | |
| 141 } | |
| 142 | |
| 143 return 0; | |
| 144 } | |
| OLD | NEW |