blob: 85d8777712ef692ed3825e717b0de4bfc2b569a7 [file] [log] [blame]
bungeman@google.come3c8ddf2012-12-05 20:13:12 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "skdiff.h"
9#include "skdiff_html.h"
10#include "SkStream.h"
11#include "SkTime.h"
12
13/// Make layout more consistent by scaling image to 240 height, 360 width,
14/// or natural size, whichever is smallest.
15static int compute_image_height(int height, int width) {
16 int retval = 240;
17 if (height < retval) {
18 retval = height;
19 }
20 float scale = (float) retval / height;
21 if (width * scale > 360) {
22 scale = (float) 360 / width;
23 retval = static_cast<int>(height * scale);
24 }
25 return retval;
26}
27
28static void print_table_header(SkFILEWStream* stream,
29 const int matchCount,
30 const int colorThreshold,
31 const RecordArray& differences,
32 const SkString &baseDir,
33 const SkString &comparisonDir,
34 bool doOutputDate = false) {
35 stream->writeText("<table>\n");
36 stream->writeText("<tr><th>");
37 stream->writeText("select image</th>\n<th>");
38 if (doOutputDate) {
39 SkTime::DateTime dt;
40 SkTime::GetDateTime(&dt);
41 stream->writeText("SkDiff run at ");
42 stream->writeDecAsText(dt.fHour);
43 stream->writeText(":");
44 if (dt.fMinute < 10) {
45 stream->writeText("0");
46 }
47 stream->writeDecAsText(dt.fMinute);
48 stream->writeText(":");
49 if (dt.fSecond < 10) {
50 stream->writeText("0");
51 }
52 stream->writeDecAsText(dt.fSecond);
53 stream->writeText("<br>");
54 }
55 stream->writeDecAsText(matchCount);
56 stream->writeText(" of ");
57 stream->writeDecAsText(differences.count());
58 stream->writeText(" diffs matched ");
59 if (colorThreshold == 0) {
60 stream->writeText("exactly");
61 } else {
62 stream->writeText("within ");
63 stream->writeDecAsText(colorThreshold);
64 stream->writeText(" color units per component");
65 }
66 stream->writeText(".<br>");
67 stream->writeText("</th>\n<th>");
68 stream->writeText("every different pixel shown in white");
69 stream->writeText("</th>\n<th>");
70 stream->writeText("color difference at each pixel");
71 stream->writeText("</th>\n<th>baseDir: ");
72 stream->writeText(baseDir.c_str());
73 stream->writeText("</th>\n<th>comparisonDir: ");
74 stream->writeText(comparisonDir.c_str());
75 stream->writeText("</th>\n");
76 stream->writeText("</tr>\n");
77}
78
79static void print_pixel_count(SkFILEWStream* stream, const DiffRecord& diff) {
80 stream->writeText("<br>(");
81 stream->writeDecAsText(static_cast<int>(diff.fFractionDifference *
82 diff.fBase.fBitmap.width() *
83 diff.fBase.fBitmap.height()));
84 stream->writeText(" pixels)");
85/*
86 stream->writeDecAsText(diff.fWeightedFraction *
87 diff.fBaseWidth *
88 diff.fBaseHeight);
89 stream->writeText(" weighted pixels)");
90*/
91}
92
93static void print_checkbox_cell(SkFILEWStream* stream, const DiffRecord& diff) {
94 stream->writeText("<td><input type=\"checkbox\" name=\"");
95 stream->writeText(diff.fBase.fFilename.c_str());
96 stream->writeText("\" checked=\"yes\"></td>");
97}
98
99static void print_label_cell(SkFILEWStream* stream, const DiffRecord& diff) {
100 char metricBuf [20];
101
102 stream->writeText("<td><b>");
103 stream->writeText(diff.fBase.fFilename.c_str());
104 stream->writeText("</b><br>");
105 switch (diff.fResult) {
106 case DiffRecord::kEqualBits_Result:
107 SkDEBUGFAIL("should not encounter DiffRecord with kEqualBits here");
108 return;
109 case DiffRecord::kEqualPixels_Result:
110 SkDEBUGFAIL("should not encounter DiffRecord with kEqualPixels here");
111 return;
112 case DiffRecord::kDifferentSizes_Result:
113 stream->writeText("Image sizes differ</td>");
114 return;
115 case DiffRecord::kDifferentPixels_Result:
116 sprintf(metricBuf, "%.4f%%", 100 * diff.fFractionDifference);
117 stream->writeText(metricBuf);
118 stream->writeText(" of pixels differ");
119 stream->writeText("\n (");
120 sprintf(metricBuf, "%.4f%%", 100 * diff.fWeightedFraction);
121 stream->writeText(metricBuf);
122 stream->writeText(" weighted)");
123 // Write the actual number of pixels that differ if it's < 1%
124 if (diff.fFractionDifference < 0.01) {
125 print_pixel_count(stream, diff);
126 }
127 stream->writeText("<br>Average color mismatch ");
128 stream->writeDecAsText(static_cast<int>(MAX3(diff.fAverageMismatchR,
129 diff.fAverageMismatchG,
130 diff.fAverageMismatchB)));
131 stream->writeText("<br>Max color mismatch ");
132 stream->writeDecAsText(MAX3(diff.fMaxMismatchR,
133 diff.fMaxMismatchG,
134 diff.fMaxMismatchB));
135 stream->writeText("</td>");
136 break;
137 case DiffRecord::kCouldNotCompare_Result:
138 stream->writeText("Could not compare.<br>base: ");
139 stream->writeText(DiffResource::getStatusDescription(diff.fBase.fStatus));
140 stream->writeText("<br>comparison: ");
141 stream->writeText(DiffResource::getStatusDescription(diff.fComparison.fStatus));
142 stream->writeText("</td>");
143 return;
144 default:
145 SkDEBUGFAIL("encountered DiffRecord with unknown result type");
146 return;
147 }
148}
149
150static void print_image_cell(SkFILEWStream* stream, const SkString& path, int height) {
151 stream->writeText("<td><a href=\"");
152 stream->writeText(path.c_str());
153 stream->writeText("\"><img src=\"");
154 stream->writeText(path.c_str());
155 stream->writeText("\" height=\"");
156 stream->writeDecAsText(height);
157 stream->writeText("px\"></a></td>");
158}
159
160static void print_link_cell(SkFILEWStream* stream, const SkString& path, const char* text) {
161 stream->writeText("<td><a href=\"");
162 stream->writeText(path.c_str());
163 stream->writeText("\">");
164 stream->writeText(text);
165 stream->writeText("</a></td>");
166}
167
168static void print_diff_resource_cell(SkFILEWStream* stream, DiffResource& resource,
169 const SkString& relativePath, bool local) {
170 if (resource.fBitmap.empty()) {
171 if (DiffResource::kCouldNotDecode_Status == resource.fStatus) {
172 if (local && !resource.fFilename.isEmpty()) {
173 print_link_cell(stream, resource.fFilename, "N/A");
174 return;
175 }
176 if (!resource.fFullPath.isEmpty()) {
177 if (!resource.fFullPath.startsWith(PATH_DIV_STR)) {
178 resource.fFullPath.prepend(relativePath);
179 }
180 print_link_cell(stream, resource.fFullPath, "N/A");
181 return;
182 }
183 }
184 stream->writeText("<td>N/A</td>");
185 return;
186 }
187
188 int height = compute_image_height(resource.fBitmap.height(), resource.fBitmap.width());
189 if (local) {
190 print_image_cell(stream, resource.fFilename, height);
191 return;
192 }
193 if (!resource.fFullPath.startsWith(PATH_DIV_STR)) {
194 resource.fFullPath.prepend(relativePath);
195 }
196 print_image_cell(stream, resource.fFullPath, height);
197}
198
199static void print_diff_row(SkFILEWStream* stream, DiffRecord& diff, const SkString& relativePath) {
200 stream->writeText("<tr>\n");
201 print_checkbox_cell(stream, diff);
202 print_label_cell(stream, diff);
203 print_diff_resource_cell(stream, diff.fWhite, relativePath, true);
204 print_diff_resource_cell(stream, diff.fDifference, relativePath, true);
205 print_diff_resource_cell(stream, diff.fBase, relativePath, false);
206 print_diff_resource_cell(stream, diff.fComparison, relativePath, false);
207 stream->writeText("</tr>\n");
208 stream->flush();
209}
210
211void print_diff_page(const int matchCount,
212 const int colorThreshold,
213 const RecordArray& differences,
214 const SkString& baseDir,
215 const SkString& comparisonDir,
216 const SkString& outputDir) {
217
218 SkASSERT(!baseDir.isEmpty());
219 SkASSERT(!comparisonDir.isEmpty());
220 SkASSERT(!outputDir.isEmpty());
221
222 SkString outputPath(outputDir);
223 outputPath.append("index.html");
224 //SkFILEWStream outputStream ("index.html");
225 SkFILEWStream outputStream(outputPath.c_str());
226
227 // Need to convert paths from relative-to-cwd to relative-to-outputDir
228 // FIXME this doesn't work if there are '..' inside the outputDir
229
230 bool isPathAbsolute = false;
231 // On Windows or Linux, a path starting with PATH_DIV_CHAR is absolute.
232 if (outputDir.size() > 0 && PATH_DIV_CHAR == outputDir[0]) {
233 isPathAbsolute = true;
234 }
235#ifdef SK_BUILD_FOR_WIN32
236 // On Windows, absolute paths can also start with "x:", where x is any
237 // drive letter.
238 if (outputDir.size() > 1 && ':' == outputDir[1]) {
239 isPathAbsolute = true;
240 }
241#endif
242
243 SkString relativePath;
244 if (!isPathAbsolute) {
245 unsigned int ui;
246 for (ui = 0; ui < outputDir.size(); ui++) {
247 if (outputDir[ui] == PATH_DIV_CHAR) {
248 relativePath.append(".." PATH_DIV_STR);
249 }
250 }
251 }
252
253 outputStream.writeText(
254 "<html>\n<head>\n"
255 "<script src=\"https://ajax.googleapis.com/ajax/"
256 "libs/jquery/1.7.2/jquery.min.js\"></script>\n"
257 "<script type=\"text/javascript\">\n"
258 "function generateCheckedList() {\n"
259 "var boxes = $(\":checkbox:checked\");\n"
260 "var fileCmdLineString = '';\n"
261 "var fileMultiLineString = '';\n"
262 "for (var i = 0; i < boxes.length; i++) {\n"
263 "fileMultiLineString += boxes[i].name + '<br>';\n"
264 "fileCmdLineString += boxes[i].name + '&nbsp;';\n"
265 "}\n"
266 "$(\"#checkedList\").html(fileCmdLineString + "
267 "'<br><br>' + fileMultiLineString);\n"
268 "}\n"
269 "</script>\n</head>\n<body>\n");
270 print_table_header(&outputStream, matchCount, colorThreshold, differences,
271 baseDir, comparisonDir);
272 int i;
273 for (i = 0; i < differences.count(); i++) {
274 DiffRecord* diff = differences[i];
275
276 switch (diff->fResult) {
277 // Cases in which there is no diff to report.
278 case DiffRecord::kEqualBits_Result:
279 case DiffRecord::kEqualPixels_Result:
280 continue;
281 // Cases in which we want a detailed pixel diff.
282 case DiffRecord::kDifferentPixels_Result:
283 case DiffRecord::kDifferentSizes_Result:
284 case DiffRecord::kCouldNotCompare_Result:
285 print_diff_row(&outputStream, *diff, relativePath);
286 continue;
287 default:
288 SkDEBUGFAIL("encountered DiffRecord with unknown result type");
289 continue;
290 }
291 }
292 outputStream.writeText(
293 "</table>\n"
294 "<input type=\"button\" "
295 "onclick=\"generateCheckedList()\" "
296 "value=\"Create Rebaseline List\">\n"
297 "<div id=\"checkedList\"></div>\n"
298 "</body>\n</html>\n");
299 outputStream.flush();
300}