Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(429)

Side by Side Diff: tools/imagediff/image_diff.cc

Issue 1549203002: Switch to standard integer types in tools/. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « tools/gn/value_unittest.cc ('k') | tools/imagediff/image_diff_png.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // This file input format is based loosely on 5 // This file input format is based loosely on
6 // Tools/DumpRenderTree/ImageDiff.m 6 // Tools/DumpRenderTree/ImageDiff.m
7 7
8 // The exact format of this tool's output to stdout is important, to match 8 // The exact format of this tool's output to stdout is important, to match
9 // what the run-webkit-tests script expects. 9 // what the run-webkit-tests script expects.
10 10
11 #include <stddef.h>
12 #include <stdint.h>
13
11 #include <algorithm> 14 #include <algorithm>
12 #include <iostream> 15 #include <iostream>
13 #include <string> 16 #include <string>
14 #include <vector> 17 #include <vector>
15 18
16 #include "base/basictypes.h"
17 #include "base/command_line.h" 19 #include "base/command_line.h"
18 #include "base/containers/hash_tables.h" 20 #include "base/containers/hash_tables.h"
19 #include "base/files/file_path.h" 21 #include "base/files/file_path.h"
20 #include "base/files/file_util.h" 22 #include "base/files/file_util.h"
21 #include "base/logging.h" 23 #include "base/logging.h"
22 #include "base/memory/scoped_ptr.h" 24 #include "base/memory/scoped_ptr.h"
23 #include "base/numerics/safe_conversions.h" 25 #include "base/numerics/safe_conversions.h"
24 #include "base/process/memory.h" 26 #include "base/process/memory.h"
25 #include "base/strings/string_util.h" 27 #include "base/strings/string_util.h"
26 #include "base/strings/utf_string_conversions.h" 28 #include "base/strings/utf_string_conversions.h"
29 #include "build/build_config.h"
27 #include "tools/imagediff/image_diff_png.h" 30 #include "tools/imagediff/image_diff_png.h"
28 31
29 #if defined(OS_WIN) 32 #if defined(OS_WIN)
30 #include "windows.h" 33 #include "windows.h"
31 #endif 34 #endif
32 35
33 // Causes the app to remain open, waiting for pairs of filenames on stdin. 36 // Causes the app to remain open, waiting for pairs of filenames on stdin.
34 // The caller is then responsible for terminating this app. 37 // The caller is then responsible for terminating this app.
35 static const char kOptionPollStdin[] = "use-stdin"; 38 static const char kOptionPollStdin[] = "use-stdin";
36 // Causes the app to additionally calculate a diff of the color histograms 39 // Causes the app to additionally calculate a diff of the color histograms
37 // (which is resistant to shifts in layout). 40 // (which is resistant to shifts in layout).
38 static const char kOptionCompareHistograms[] = "histogram"; 41 static const char kOptionCompareHistograms[] = "histogram";
39 // Causes the app to output an image that visualizes the difference. 42 // Causes the app to output an image that visualizes the difference.
40 static const char kOptionGenerateDiff[] = "diff"; 43 static const char kOptionGenerateDiff[] = "diff";
41 44
42 // Return codes used by this utility. 45 // Return codes used by this utility.
43 static const int kStatusSame = 0; 46 static const int kStatusSame = 0;
44 static const int kStatusDifferent = 1; 47 static const int kStatusDifferent = 1;
45 static const int kStatusError = 2; 48 static const int kStatusError = 2;
46 49
47 // Color codes. 50 // Color codes.
48 static const uint32 RGBA_RED = 0x000000ff; 51 static const uint32_t RGBA_RED = 0x000000ff;
49 static const uint32 RGBA_ALPHA = 0xff000000; 52 static const uint32_t RGBA_ALPHA = 0xff000000;
50 53
51 class Image { 54 class Image {
52 public: 55 public:
53 Image() : w_(0), h_(0) { 56 Image() : w_(0), h_(0) {
54 } 57 }
55 58
56 Image(const Image& image) 59 Image(const Image& image)
57 : w_(image.w_), 60 : w_(image.w_),
58 h_(image.h_), 61 h_(image.h_),
59 data_(image.data_) { 62 data_(image.data_) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
117 } 120 }
118 return true; 121 return true;
119 } 122 }
120 123
121 void Clear() { 124 void Clear() {
122 w_ = h_ = 0; 125 w_ = h_ = 0;
123 data_.clear(); 126 data_.clear();
124 } 127 }
125 128
126 // Returns the RGBA value of the pixel at the given location 129 // Returns the RGBA value of the pixel at the given location
127 uint32 pixel_at(int x, int y) const { 130 uint32_t pixel_at(int x, int y) const {
128 DCHECK(x >= 0 && x < w_); 131 DCHECK(x >= 0 && x < w_);
129 DCHECK(y >= 0 && y < h_); 132 DCHECK(y >= 0 && y < h_);
130 return *reinterpret_cast<const uint32*>(&(data_[(y * w_ + x) * 4])); 133 return *reinterpret_cast<const uint32_t*>(&(data_[(y * w_ + x) * 4]));
131 } 134 }
132 135
133 void set_pixel_at(int x, int y, uint32 color) const { 136 void set_pixel_at(int x, int y, uint32_t color) const {
134 DCHECK(x >= 0 && x < w_); 137 DCHECK(x >= 0 && x < w_);
135 DCHECK(y >= 0 && y < h_); 138 DCHECK(y >= 0 && y < h_);
136 void* addr = &const_cast<unsigned char*>(&data_.front())[(y * w_ + x) * 4]; 139 void* addr = &const_cast<unsigned char*>(&data_.front())[(y * w_ + x) * 4];
137 *reinterpret_cast<uint32*>(addr) = color; 140 *reinterpret_cast<uint32_t*>(addr) = color;
138 } 141 }
139 142
140 private: 143 private:
141 // pixel dimensions of the image 144 // pixel dimensions of the image
142 int w_, h_; 145 int w_, h_;
143 146
144 std::vector<unsigned char> data_; 147 std::vector<unsigned char> data_;
145 }; 148 };
146 149
147 float PercentageDifferent(const Image& baseline, const Image& actual) { 150 float PercentageDifferent(const Image& baseline, const Image& actual) {
(...skipping 21 matching lines...) Expand all
169 // of the size of the 'actual' bitmap. 172 // of the size of the 'actual' bitmap.
170 float total_pixels = static_cast<float>(actual.w()) * 173 float total_pixels = static_cast<float>(actual.w()) *
171 static_cast<float>(actual.h()); 174 static_cast<float>(actual.h());
172 if (total_pixels == 0) { 175 if (total_pixels == 0) {
173 // When the bitmap is empty, they are 100% different. 176 // When the bitmap is empty, they are 100% different.
174 return 100.0f; 177 return 100.0f;
175 } 178 }
176 return 100.0f * pixels_different / total_pixels; 179 return 100.0f * pixels_different / total_pixels;
177 } 180 }
178 181
179 typedef base::hash_map<uint32, int32> RgbaToCountMap; 182 typedef base::hash_map<uint32_t, int32_t> RgbaToCountMap;
180 183
181 float HistogramPercentageDifferent(const Image& baseline, const Image& actual) { 184 float HistogramPercentageDifferent(const Image& baseline, const Image& actual) {
182 // TODO(johnme): Consider using a joint histogram instead, as described in 185 // TODO(johnme): Consider using a joint histogram instead, as described in
183 // "Comparing Images Using Joint Histograms" by Pass & Zabih 186 // "Comparing Images Using Joint Histograms" by Pass & Zabih
184 // http://www.cs.cornell.edu/~rdz/papers/pz-jms99.pdf 187 // http://www.cs.cornell.edu/~rdz/papers/pz-jms99.pdf
185 188
186 int w = std::min(baseline.w(), actual.w()); 189 int w = std::min(baseline.w(), actual.w());
187 int h = std::min(baseline.h(), actual.h()); 190 int h = std::min(baseline.h(), actual.h());
188 191
189 // Count occurences of each RGBA pixel value of baseline in the overlap. 192 // Count occurences of each RGBA pixel value of baseline in the overlap.
190 RgbaToCountMap baseline_histogram; 193 RgbaToCountMap baseline_histogram;
191 for (int y = 0; y < h; y++) { 194 for (int y = 0; y < h; y++) {
192 for (int x = 0; x < w; x++) { 195 for (int x = 0; x < w; x++) {
193 // hash_map operator[] inserts a 0 (default constructor) if key not found. 196 // hash_map operator[] inserts a 0 (default constructor) if key not found.
194 baseline_histogram[baseline.pixel_at(x, y)]++; 197 baseline_histogram[baseline.pixel_at(x, y)]++;
195 } 198 }
196 } 199 }
197 200
198 // Compute pixels different in the histogram of the overlap. 201 // Compute pixels different in the histogram of the overlap.
199 int pixels_different = 0; 202 int pixels_different = 0;
200 for (int y = 0; y < h; y++) { 203 for (int y = 0; y < h; y++) {
201 for (int x = 0; x < w; x++) { 204 for (int x = 0; x < w; x++) {
202 uint32 actual_rgba = actual.pixel_at(x, y); 205 uint32_t actual_rgba = actual.pixel_at(x, y);
203 RgbaToCountMap::iterator it = baseline_histogram.find(actual_rgba); 206 RgbaToCountMap::iterator it = baseline_histogram.find(actual_rgba);
204 if (it != baseline_histogram.end() && it->second > 0) 207 if (it != baseline_histogram.end() && it->second > 0)
205 it->second--; 208 it->second--;
206 else 209 else
207 pixels_different++; 210 pixels_different++;
208 } 211 }
209 } 212 }
210 213
211 // Count pixels that are a difference in size as also being different. 214 // Count pixels that are a difference in size as also being different.
212 int max_w = std::max(baseline.w(), actual.w()); 215 int max_w = std::max(baseline.w(), actual.w());
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after
335 bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) { 338 bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) {
336 int w = std::min(image1.w(), image2.w()); 339 int w = std::min(image1.w(), image2.w());
337 int h = std::min(image1.h(), image2.h()); 340 int h = std::min(image1.h(), image2.h());
338 *out = Image(image1); 341 *out = Image(image1);
339 bool same = (image1.w() == image2.w()) && (image1.h() == image2.h()); 342 bool same = (image1.w() == image2.w()) && (image1.h() == image2.h());
340 343
341 // TODO(estade): do something with the extra pixels if the image sizes 344 // TODO(estade): do something with the extra pixels if the image sizes
342 // are different. 345 // are different.
343 for (int y = 0; y < h; y++) { 346 for (int y = 0; y < h; y++) {
344 for (int x = 0; x < w; x++) { 347 for (int x = 0; x < w; x++) {
345 uint32 base_pixel = image1.pixel_at(x, y); 348 uint32_t base_pixel = image1.pixel_at(x, y);
346 if (base_pixel != image2.pixel_at(x, y)) { 349 if (base_pixel != image2.pixel_at(x, y)) {
347 // Set differing pixels red. 350 // Set differing pixels red.
348 out->set_pixel_at(x, y, RGBA_RED | RGBA_ALPHA); 351 out->set_pixel_at(x, y, RGBA_RED | RGBA_ALPHA);
349 same = false; 352 same = false;
350 } else { 353 } else {
351 // Set same pixels as faded. 354 // Set same pixels as faded.
352 uint32 alpha = base_pixel & RGBA_ALPHA; 355 uint32_t alpha = base_pixel & RGBA_ALPHA;
353 uint32 new_pixel = base_pixel - ((alpha / 2) & RGBA_ALPHA); 356 uint32_t new_pixel = base_pixel - ((alpha / 2) & RGBA_ALPHA);
354 out->set_pixel_at(x, y, new_pixel); 357 out->set_pixel_at(x, y, new_pixel);
355 } 358 }
356 } 359 }
357 } 360 }
358 361
359 return same; 362 return same;
360 } 363 }
361 364
362 int DiffImages(const base::FilePath& file1, const base::FilePath& file2, 365 int DiffImages(const base::FilePath& file1, const base::FilePath& file2,
363 const base::FilePath& out_file) { 366 const base::FilePath& out_file) {
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after
441 base::FilePath(args[2])); 444 base::FilePath(args[2]));
442 } 445 }
443 } else if (args.size() == 2) { 446 } else if (args.size() == 2) {
444 return CompareImages( 447 return CompareImages(
445 base::FilePath(args[0]), base::FilePath(args[1]), histograms); 448 base::FilePath(args[0]), base::FilePath(args[1]), histograms);
446 } 449 }
447 450
448 PrintHelp(); 451 PrintHelp();
449 return kStatusError; 452 return kStatusError;
450 } 453 }
OLDNEW
« no previous file with comments | « tools/gn/value_unittest.cc ('k') | tools/imagediff/image_diff_png.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698