OLD | NEW |
| (Empty) |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // This file input format is based loosely on | |
6 // WebKitTools/DumpRenderTree/ImageDiff.m | |
7 | |
8 // The exact format of this tool's output to stdout is important, to match | |
9 // what the run-webkit-tests script expects. | |
10 | |
11 #include <algorithm> | |
12 #include <vector> | |
13 #include <string> | |
14 #include <iostream> | |
15 | |
16 #include "base/basictypes.h" | |
17 #include "base/command_line.h" | |
18 #include "base/file_util.h" | |
19 #include "base/gfx/png_decoder.h" | |
20 #include "base/gfx/png_encoder.h" | |
21 #include "base/logging.h" | |
22 #include "base/process_util.h" | |
23 #include "base/scoped_ptr.h" | |
24 #include "base/string_util.h" | |
25 | |
26 #if defined(OS_WIN) | |
27 #include "windows.h" | |
28 #endif | |
29 | |
30 // Causes the app to remain open, waiting for pairs of filenames on stdin. | |
31 // The caller is then responsible for terminating this app. | |
32 static const wchar_t kOptionPollStdin[] = L"use-stdin"; | |
33 static const wchar_t kOptionGenerateDiff[] = L"diff"; | |
34 | |
35 // Return codes used by this utility. | |
36 static const int kStatusSame = 0; | |
37 static const int kStatusDifferent = 1; | |
38 static const int kStatusError = 2; | |
39 | |
40 // Color codes. | |
41 static const uint32 RGBA_RED = 0x000000ff; | |
42 static const uint32 RGBA_ALPHA = 0xff000000; | |
43 | |
44 class Image { | |
45 public: | |
46 Image() : w_(0), h_(0) { | |
47 } | |
48 | |
49 Image(const Image& image) | |
50 : w_(image.w_), | |
51 h_(image.h_), | |
52 data_(image.data_) { | |
53 } | |
54 | |
55 bool has_image() const { | |
56 return w_ > 0 && h_ > 0; | |
57 } | |
58 | |
59 int w() const { | |
60 return w_; | |
61 } | |
62 | |
63 int h() const { | |
64 return h_; | |
65 } | |
66 | |
67 const unsigned char* data() const { | |
68 return &data_.front(); | |
69 } | |
70 | |
71 // Creates the image from stdin with the given data length. On success, it | |
72 // will return true. On failure, no other methods should be accessed. | |
73 bool CreateFromStdin(size_t byte_length) { | |
74 if (byte_length == 0) | |
75 return false; | |
76 | |
77 scoped_array<unsigned char> source(new unsigned char[byte_length]); | |
78 if (fread(source.get(), 1, byte_length, stdin) != byte_length) | |
79 return false; | |
80 | |
81 if (!PNGDecoder::Decode(source.get(), byte_length, PNGDecoder::FORMAT_RGBA, | |
82 &data_, &w_, &h_)) { | |
83 Clear(); | |
84 return false; | |
85 } | |
86 return true; | |
87 } | |
88 | |
89 // Creates the image from the given filename on disk, and returns true on | |
90 // success. | |
91 bool CreateFromFilename(const char* filename) { | |
92 FILE* f = file_util::OpenFile(std::string(filename), "rb"); | |
93 if (!f) | |
94 return false; | |
95 | |
96 std::vector<unsigned char> compressed; | |
97 const int buf_size = 1024; | |
98 unsigned char buf[buf_size]; | |
99 size_t num_read = 0; | |
100 while ((num_read = fread(buf, 1, buf_size, f)) > 0) { | |
101 std::copy(buf, &buf[num_read], std::back_inserter(compressed)); | |
102 } | |
103 | |
104 file_util::CloseFile(f); | |
105 | |
106 if (!PNGDecoder::Decode(&compressed[0], compressed.size(), | |
107 PNGDecoder::FORMAT_RGBA, &data_, &w_, &h_)) { | |
108 Clear(); | |
109 return false; | |
110 } | |
111 return true; | |
112 } | |
113 | |
114 void Clear() { | |
115 w_ = h_ = 0; | |
116 data_.clear(); | |
117 } | |
118 | |
119 // Returns the RGBA value of the pixel at the given location | |
120 const uint32 pixel_at(int x, int y) const { | |
121 DCHECK(x >= 0 && x < w_); | |
122 DCHECK(y >= 0 && y < h_); | |
123 return *reinterpret_cast<const uint32*>(&(data_[(y * w_ + x) * 4])); | |
124 } | |
125 | |
126 void set_pixel_at(int x, int y, uint32 color) const { | |
127 DCHECK(x >= 0 && x < w_); | |
128 DCHECK(y >= 0 && y < h_); | |
129 void* addr = &const_cast<unsigned char*>(&data_.front())[(y * w_ + x) * 4]; | |
130 *reinterpret_cast<uint32*>(addr) = color; | |
131 } | |
132 | |
133 private: | |
134 // pixel dimensions of the image | |
135 int w_, h_; | |
136 | |
137 std::vector<unsigned char> data_; | |
138 }; | |
139 | |
140 float PercentageDifferent(const Image& baseline, const Image& actual) { | |
141 int w = std::min(baseline.w(), actual.w()); | |
142 int h = std::min(baseline.h(), actual.h()); | |
143 | |
144 // compute pixels different in the overlap | |
145 int pixels_different = 0; | |
146 for (int y = 0; y < h; y++) { | |
147 for (int x = 0; x < w; x++) { | |
148 if (baseline.pixel_at(x, y) != actual.pixel_at(x, y)) | |
149 pixels_different++; | |
150 } | |
151 } | |
152 | |
153 // count pixels that are a difference in size as also being different | |
154 int max_w = std::max(baseline.w(), actual.w()); | |
155 int max_h = std::max(baseline.h(), actual.h()); | |
156 | |
157 // ...pixels off the right side, but not including the lower right corner | |
158 pixels_different += (max_w - w) * h; | |
159 | |
160 // ...pixels along the bottom, including the lower right corner | |
161 pixels_different += (max_h - h) * max_w; | |
162 | |
163 // Like the WebKit ImageDiff tool, we define percentage different in terms | |
164 // of the size of the 'actual' bitmap. | |
165 float total_pixels = static_cast<float>(actual.w()) * | |
166 static_cast<float>(actual.h()); | |
167 if (total_pixels == 0) | |
168 return 100.0f; // when the bitmap is empty, they are 100% different | |
169 return static_cast<float>(pixels_different) / total_pixels * 100; | |
170 } | |
171 | |
172 void PrintHelp() { | |
173 fprintf(stderr, | |
174 "Usage:\n" | |
175 " image_diff <compare file> <reference file>\n" | |
176 " Compares two files on disk, returning 0 when they are the same\n" | |
177 " image_diff --use-stdin\n" | |
178 " Stays open reading pairs of filenames from stdin, comparing them,\n" | |
179 " and sending 0 to stdout when they are the same\n" | |
180 " image_diff --diff <compare file> <reference file> <output file>\n" | |
181 " Compares two files on disk, outputs an image that visualizes the" | |
182 " difference to <output file>\n"); | |
183 /* For unfinished webkit-like-mode (see below) | |
184 "\n" | |
185 " image_diff -s\n" | |
186 " Reads stream input from stdin, should be EXACTLY of the format\n" | |
187 " \"Content-length: <byte length> <data>Content-length: ...\n" | |
188 " it will take as many file pairs as given, and will compare them as\n" | |
189 " (cmp_file, reference_file) pairs\n"); | |
190 */ | |
191 } | |
192 | |
193 int CompareImages(const char* file1, const char* file2) { | |
194 Image actual_image; | |
195 Image baseline_image; | |
196 | |
197 if (!actual_image.CreateFromFilename(file1)) { | |
198 fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1); | |
199 return kStatusError; | |
200 } | |
201 if (!baseline_image.CreateFromFilename(file2)) { | |
202 fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2); | |
203 return kStatusError; | |
204 } | |
205 | |
206 float percent = PercentageDifferent(actual_image, baseline_image); | |
207 if (percent > 0.0) { | |
208 // failure: The WebKit version also writes the difference image to | |
209 // stdout, which seems excessive for our needs. | |
210 printf("diff: %01.2f%% failed\n", percent); | |
211 return kStatusDifferent; | |
212 } | |
213 | |
214 // success | |
215 printf("diff: %01.2f%% passed\n", percent); | |
216 return kStatusSame; | |
217 | |
218 /* Untested mode that acts like WebKit's image comparator. I wrote this but | |
219 decided it's too complicated. We may use it in the future if it looks useful | |
220 | |
221 char buffer[2048]; | |
222 while (fgets(buffer, sizeof(buffer), stdin)) { | |
223 | |
224 if (strncmp("Content-length: ", buffer, 16) == 0) { | |
225 char* context; | |
226 strtok_s(buffer, " ", &context); | |
227 int image_size = strtol(strtok_s(NULL, " ", &context), NULL, 10); | |
228 | |
229 bool success = false; | |
230 if (image_size > 0 && actual_image.has_image() == 0) { | |
231 if (!actual_image.CreateFromStdin(image_size)) { | |
232 fputs("Error, input image can't be decoded.\n", stderr); | |
233 return 1; | |
234 } | |
235 } else if (image_size > 0 && baseline_image.has_image() == 0) { | |
236 if (!baseline_image.CreateFromStdin(image_size)) { | |
237 fputs("Error, baseline image can't be decoded.\n", stderr); | |
238 return 1; | |
239 } | |
240 } else { | |
241 fputs("Error, image size must be specified.\n", stderr); | |
242 return 1; | |
243 } | |
244 } | |
245 | |
246 if (actual_image.has_image() && baseline_image.has_image()) { | |
247 float percent = PercentageDifferent(actual_image, baseline_image); | |
248 if (percent > 0.0) { | |
249 // failure: The WebKit version also writes the difference image to | |
250 // stdout, which seems excessive for our needs. | |
251 printf("diff: %01.2f%% failed\n", percent); | |
252 } else { | |
253 // success | |
254 printf("diff: %01.2f%% passed\n", percent); | |
255 } | |
256 actual_image.Clear(); | |
257 baseline_image.Clear(); | |
258 } | |
259 | |
260 fflush(stdout); | |
261 } | |
262 */ | |
263 } | |
264 | |
265 bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) { | |
266 int w = std::min(image1.w(), image2.w()); | |
267 int h = std::min(image1.h(), image2.h()); | |
268 *out = Image(image1); | |
269 bool same = (image1.w() == image2.w()) && (image1.h() == image2.h()); | |
270 | |
271 // TODO(estade): do something with the extra pixels if the image sizes | |
272 // are different. | |
273 for (int y = 0; y < h; y++) { | |
274 for (int x = 0; x < w; x++) { | |
275 uint32 base_pixel = image1.pixel_at(x, y); | |
276 if (base_pixel != image2.pixel_at(x, y)) { | |
277 // Set differing pixels red. | |
278 out->set_pixel_at(x, y, RGBA_RED | RGBA_ALPHA); | |
279 same = false; | |
280 } else { | |
281 // Set same pixels as faded. | |
282 uint32 alpha = base_pixel & RGBA_ALPHA; | |
283 uint32 new_pixel = base_pixel - ((alpha / 2) & RGBA_ALPHA); | |
284 out->set_pixel_at(x, y, new_pixel); | |
285 } | |
286 } | |
287 } | |
288 | |
289 return same; | |
290 } | |
291 | |
292 int DiffImages(const char* file1, const char* file2, const char* out_file) { | |
293 Image actual_image; | |
294 Image baseline_image; | |
295 | |
296 if (!actual_image.CreateFromFilename(file1)) { | |
297 fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file1); | |
298 return kStatusError; | |
299 } | |
300 if (!baseline_image.CreateFromFilename(file2)) { | |
301 fprintf(stderr, "image_diff: Unable to open file \"%s\"\n", file2); | |
302 return kStatusError; | |
303 } | |
304 | |
305 Image diff_image; | |
306 bool same = CreateImageDiff(baseline_image, actual_image, &diff_image); | |
307 if (same) | |
308 return kStatusSame; | |
309 | |
310 std::vector<unsigned char> png_encoding; | |
311 PNGEncoder::Encode(diff_image.data(), PNGEncoder::FORMAT_RGBA, | |
312 diff_image.w(), diff_image.h(), diff_image.w() * 4, | |
313 false, &png_encoding); | |
314 if (file_util::WriteFile(UTF8ToWide(out_file), | |
315 reinterpret_cast<char*>(&png_encoding.front()), png_encoding.size()) < 0) | |
316 return kStatusError; | |
317 | |
318 return kStatusDifferent; | |
319 } | |
320 | |
321 int main(int argc, const char* argv[]) { | |
322 base::EnableTerminationOnHeapCorruption(); | |
323 CommandLine::Init(argc, argv); | |
324 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess(); | |
325 if (parsed_command_line.HasSwitch(kOptionPollStdin)) { | |
326 // Watch stdin for filenames. | |
327 std::string stdin_buffer; | |
328 std::string filename1_buffer; | |
329 bool have_filename1 = false; | |
330 while (std::getline(std::cin, stdin_buffer)) { | |
331 if (stdin_buffer.empty()) | |
332 continue; | |
333 | |
334 if (have_filename1) { | |
335 // CompareImages writes results to stdout unless an error occurred. | |
336 if (CompareImages(filename1_buffer.c_str(), stdin_buffer.c_str()) == | |
337 kStatusError) | |
338 printf("error\n"); | |
339 fflush(stdout); | |
340 have_filename1 = false; | |
341 } else { | |
342 // Save the first filename in another buffer and wait for the second | |
343 // filename to arrive via stdin. | |
344 filename1_buffer = stdin_buffer; | |
345 have_filename1 = true; | |
346 } | |
347 } | |
348 return 0; | |
349 } | |
350 | |
351 std::vector<std::wstring> values = parsed_command_line.GetLooseValues(); | |
352 if (parsed_command_line.HasSwitch(kOptionGenerateDiff)) { | |
353 if (values.size() == 3) { | |
354 return DiffImages(WideToUTF8(values[0]).c_str(), | |
355 WideToUTF8(values[1]).c_str(), | |
356 WideToUTF8(values[2]).c_str()); | |
357 } | |
358 } else if (values.size() == 2) { | |
359 return CompareImages(argv[1], argv[2]); | |
360 } | |
361 | |
362 PrintHelp(); | |
363 return kStatusError; | |
364 } | |
OLD | NEW |