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

Side by Side Diff: cc/test/pixel_comparator.cc

Issue 12558003: cc: Made image comparison for pixel tests error tolerant. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Code cleanups Created 7 years, 9 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
OLDNEW
(Empty)
1 // Copyright (c) 2013 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 #include "cc/test/pixel_comparator.h"
6
7 #include <algorithm>
8
9 #include "base/logging.h"
10
11 namespace cc {
12
13 bool ExactPixelComparator::Compare(const SkBitmap& actual_bmp,
14 const SkBitmap& expected_bmp) const {
15 // Number of pixels with an error
16 int error_pixels_count = 0;
17
18 // Check that bitmaps have identical dimensions.
19 DCHECK(actual_bmp.width() == expected_bmp.width() &&
20 actual_bmp.height() == expected_bmp.height());
21
22 SkAutoLockPixels lock_actual_bmp(actual_bmp);
23 SkAutoLockPixels lock_expected_bmp(expected_bmp);
24
25 for (int x = 0; x < actual_bmp.width(); ++x) {
26 for (int y = 0; y < actual_bmp.height(); ++y) {
27 // Fetch color values. Set alpha to 0, because rhe reference images were
28 // saved with no alpha channel.
29 SkColor actual_color = actual_bmp.getColor(x, y);
30 SkColorSetA(actual_color, 0);
31 SkColor expected_color = expected_bmp.getColor(x, y);
32 SkColorSetA(expected_color, 0);
33
34 if (actual_color != expected_color) {
35 ++error_pixels_count;
36 }
37 }
38 }
39
40 if (error_pixels_count != 0) {
41 LOG(ERROR) << "Number of pixel with an error: " << error_pixels_count;
42 return false;
43 }
44
45 return true;
46 }
47
48 FuzzyPixelComparator::FuzzyPixelComparator(
49 const float error_pixels_percentage_limit,
50 const float small_error_pixels_percentage_limit,
51 const float avg_abs_error_limit,
52 const int max_abs_error_limit,
53 const int small_error_threshold)
54 : error_pixels_percentage_limit_(error_pixels_percentage_limit),
55 small_error_pixels_percentage_limit_(small_error_pixels_percentage_limit),
56 avg_abs_error_limit_(avg_abs_error_limit),
57 max_abs_error_limit_(max_abs_error_limit),
58 small_error_threshold_(small_error_threshold) {
59 }
60
61 bool FuzzyPixelComparator::Compare(const SkBitmap& actual_bmp,
62 const SkBitmap& expected_bmp) const {
63 // Number of pixels with an error
64 int error_pixels_count = 0;
65 // Number of pixels with a small error
66 int small_error_pixels_count = 0;
67 // The per channel sums of absolute errors over all pixels.
68 int64 sum_abs_error_r = 0;
69 int64 sum_abs_error_g = 0;
70 int64 sum_abs_error_b = 0;
71 // The per channel maximum absolute errors over all pixels.
72 int max_abs_error_r = 0;
73 int max_abs_error_g = 0;
74 int max_abs_error_b = 0;
75
76 // Check that bitmaps have identical dimensions.
77 DCHECK(actual_bmp.width() == expected_bmp.width() &&
78 actual_bmp.height() == expected_bmp.height());
79
80 // Check that bitmaps are not empty.
81 DCHECK(actual_bmp.width() > 0 && actual_bmp.height() > 0);
82
83 SkAutoLockPixels lock_actual_bmp(actual_bmp);
84 SkAutoLockPixels lock_expected_bmp(expected_bmp);
85
86 for (int x = 0; x < actual_bmp.width(); ++x) {
87 for (int y = 0; y < actual_bmp.height(); ++y) {
88 // Fetch color values. Set alpha to 0, because rhe reference images were
89 // saved with no alpha channel.
90 SkColor actual_color = actual_bmp.getColor(x, y);
91 SkColorSetA(actual_color, 0);
92 SkColor expected_color = expected_bmp.getColor(x, y);
93 SkColorSetA(expected_color, 0);
94
95 if (actual_color != expected_color) {
96 ++error_pixels_count;
97
98 // Compute per channel absolute errors
99 int abs_error_r =
100 std::max(SkColorGetR(actual_color), SkColorGetR(expected_color)) -
slavi 2013/03/08 19:49:04 Why not std::abs(SkColorGetR(actual_color) - SkCo
ernstm 2013/03/18 20:19:39 Done.
101 std::min(SkColorGetR(actual_color), SkColorGetR(expected_color));
102 int abs_error_g =
103 std::max(SkColorGetG(actual_color), SkColorGetG(expected_color)) -
104 std::min(SkColorGetG(actual_color), SkColorGetG(expected_color));
105 int abs_error_b =
106 std::max(SkColorGetB(actual_color), SkColorGetB(expected_color)) -
107 std::min(SkColorGetB(actual_color), SkColorGetB(expected_color));
108
109 // Increment small error counter if error is below threshold
110 if (abs_error_r <= small_error_threshold_ &&
111 abs_error_g <= small_error_threshold_ &&
112 abs_error_b <= small_error_threshold_)
113 ++small_error_pixels_count;
114
115 // Update per channel maximum absolute errors
116 if (abs_error_r > max_abs_error_r) max_abs_error_r = abs_error_r;
slavi 2013/03/08 19:49:04 max_abs_error_r = std::max(max_abs_error_r, abs_er
ernstm 2013/03/18 20:19:39 Done.
117 if (abs_error_g > max_abs_error_g) max_abs_error_g = abs_error_g;
118 if (abs_error_b > max_abs_error_b) max_abs_error_b = abs_error_b;
reveman 2013/03/08 04:41:57 nit: put max_abs_error_? = abs_error_?; statements
ernstm 2013/03/18 20:19:39 Done.
119
120 // Update per channel absolute error sums
121 sum_abs_error_r += abs_error_r;
122 sum_abs_error_g += abs_error_g;
123 sum_abs_error_b += abs_error_b;
124 }
125 }
126 }
127
128 // Compute error metrics from collected data
129 int pixels_count = actual_bmp.width() * actual_bmp.height();
130 float error_pixels_percentage = 0.0f;
131 float small_error_pixels_percentage = 0.0f;
132 if (pixels_count > 0) {
133 error_pixels_percentage = static_cast<float>(error_pixels_count) /
134 pixels_count * 100.0f;
135 small_error_pixels_percentage =
136 static_cast<float>(small_error_pixels_count) / pixels_count * 100.0f;
137 }
138 float avg_abs_error_r = 0.0f;
139 float avg_abs_error_g = 0.0f;
140 float avg_abs_error_b = 0.0f;
141 if (error_pixels_count > 0) {
142 avg_abs_error_r = static_cast<float>(sum_abs_error_r) / error_pixels_count;
143 avg_abs_error_g = static_cast<float>(sum_abs_error_g) / error_pixels_count;
144 avg_abs_error_b = static_cast<float>(sum_abs_error_b) / error_pixels_count;
145 }
146
147 if (error_pixels_percentage > error_pixels_percentage_limit_ ||
148 small_error_pixels_percentage > small_error_pixels_percentage_limit_ ||
149 avg_abs_error_r > avg_abs_error_limit_ ||
150 avg_abs_error_g > avg_abs_error_limit_ ||
151 avg_abs_error_b > avg_abs_error_limit_ ||
152 max_abs_error_r > max_abs_error_limit_ ||
153 max_abs_error_g > max_abs_error_limit_ ||
154 max_abs_error_b > max_abs_error_limit_) {
155 LOG(ERROR) << "Percentage of pixels with an error: "
156 << error_pixels_percentage << "; "
157 << "Percentage of pixels with errors not greater than "
158 << small_error_threshold_ << ": "
159 << small_error_pixels_percentage << "; "
160 << "Average absolute error (excluding identical pixels): "
161 << "R=" << avg_abs_error_r << " "
162 << "G=" << avg_abs_error_g << " "
163 << "B=" << avg_abs_error_b << "; "
164 << "Largest absolute error: "
165 << "R=" << max_abs_error_r << " "
166 << "G=" << max_abs_error_g << " "
167 << "B=" << max_abs_error_b;
168 return false;
169 } else {
170 return true;
171 }
172 }
173
174 } // namespace cc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698