1/*
2 * Copyright (C) 2017 Igalia S.L.
3 * Copyright (C) 2005, 2007, 2015 Apple Inc. All rights reserved.
4 * Copyright (C) 2005 Ben La Monica <ben.lamonica@gmail.com>. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY
16 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
18 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR
19 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
20 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
22 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
23 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28// FIXME: We need to be able to include these defines from a config.h somewhere.
29#define JS_EXPORT_PRIVATE
30
31#include "PlatformImage.h"
32#include <algorithm>
33#include <cstdlib>
34#include <stdio.h>
35#include <string.h>
36
37#ifdef _WIN32
38#include <fcntl.h>
39#include <io.h>
40#include <windows.h>
41#endif
42
43using namespace ImageDiff;
44
45#ifdef _WIN32
46#define FORMAT_SIZE_T "Iu"
47#else
48#define FORMAT_SIZE_T "zu"
49#endif
50
51int main(int argc, const char* argv[])
52{
53#ifdef _WIN32
54 _setmode(0, _O_BINARY);
55 _setmode(1, _O_BINARY);
56#endif
57
58 float tolerance = 0.0f;
59
60 for (int i = 1; i < argc; ++i) {
61 if (!strcmp(argv[i], "-t") || !strcmp(argv[i], "--tolerance")) {
62 if (i >= argc - 1)
63 exit(1);
64 tolerance = strtof(argv[i + 1], 0);
65 ++i;
66 continue;
67 }
68 }
69
70 char buffer[2048];
71 std::unique_ptr<PlatformImage> actualImage;
72 std::unique_ptr<PlatformImage> baselineImage;
73
74 while (fgets(buffer, sizeof(buffer), stdin)) {
75 // Convert the first newline into a NUL character so that strtok doesn't produce it.
76 char* newLineCharacter = strchr(buffer, '\n');
77 if (newLineCharacter)
78 *newLineCharacter = '\0';
79
80 if (!strncmp("Content-Length: ", buffer, 16)) {
81 strtok(buffer, " ");
82
83 int imageSize = strtol(strtok(0, " "), 0, 10);
84 if (imageSize <= 0) {
85 fprintf(stderr, "Error: image size must be specified.\n");
86 return EXIT_FAILURE;
87 }
88
89 if (!actualImage) {
90 if (!(actualImage = PlatformImage::createFromStdin(imageSize))) {
91 fprintf(stderr, "Error: could not read actual image.\n");
92 return EXIT_FAILURE;
93 }
94 } else if (!baselineImage) {
95 if (!(baselineImage = PlatformImage::createFromStdin(imageSize))) {
96 fprintf(stderr, "Error: could not read baseline image.\n");
97 return EXIT_FAILURE;
98 }
99 }
100 }
101
102 if (actualImage && baselineImage) {
103 if (!actualImage->isCompatible(*baselineImage)) {
104 if (actualImage->width() != baselineImage->width() || actualImage->height() != baselineImage->height()) {
105 fprintf(stderr, "Error: test and reference images have different sizes. Test image is %" FORMAT_SIZE_T "x%" FORMAT_SIZE_T ", reference image is %" FORMAT_SIZE_T "x%" FORMAT_SIZE_T "\n",
106 actualImage->width(), actualImage->height(), baselineImage->width(), baselineImage->height());
107 } else if (actualImage->hasAlpha() != baselineImage->hasAlpha()) {
108 fprintf(stderr, "Error: test and reference images differ in alpha. Test image %s alpha, reference image %s alpha.\n",
109 actualImage->hasAlpha() ? "has" : "does not have", baselineImage->hasAlpha() ? "has" : "does not have");
110 }
111
112 return EXIT_FAILURE;
113 }
114
115 float difference = 100.0f;
116 std::unique_ptr<PlatformImage> diffImage = actualImage->difference(*baselineImage, difference);
117 if (difference <= tolerance)
118 difference = 0.0f;
119 else {
120 difference = roundf(difference * 100.0f) / 100.0f;
121 difference = std::max<float>(difference, 0.01f); // round to 2 decimal places
122 }
123
124 if (difference > 0.0f) {
125 if (diffImage)
126 diffImage->writeAsPNGToStdout();
127 fprintf(stdout, "diff: %01.2f%% failed\n", difference);
128 } else
129 fprintf(stdout, "diff: %01.2f%% passed\n", difference);
130
131 actualImage = nullptr;
132 baselineImage = nullptr;
133 }
134
135 fflush(stdout);
136 }
137
138 return EXIT_SUCCESS;
139}
140
141#ifdef _WIN32
142extern "C" __declspec(dllexport) int WINAPI dllLauncherEntryPoint(int argc, const char* argv[])
143{
144 return main(argc, argv);
145}
146#endif
147