OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 #include "cc/test/pixel_test_utils.h" | 5 #include "cc/test/pixel_test_utils.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/logging.h" | 8 #include "base/logging.h" |
9 #include "third_party/skia/include/core/SkBitmap.h" | 9 #include "third_party/skia/include/core/SkBitmap.h" |
10 #include "ui/gfx/codec/png_codec.h" | 10 #include "ui/gfx/codec/png_codec.h" |
(...skipping 16 matching lines...) Expand all Loading... |
27 | 27 |
28 bool ReadPNGFile(const base::FilePath& file_path, SkBitmap* bitmap) { | 28 bool ReadPNGFile(const base::FilePath& file_path, SkBitmap* bitmap) { |
29 DCHECK(bitmap); | 29 DCHECK(bitmap); |
30 std::string png_data; | 30 std::string png_data; |
31 return file_util::ReadFileToString(file_path, &png_data) && | 31 return file_util::ReadFileToString(file_path, &png_data) && |
32 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]), | 32 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]), |
33 png_data.length(), | 33 png_data.length(), |
34 bitmap); | 34 bitmap); |
35 } | 35 } |
36 | 36 |
37 bool IsSameAsPNGFile(const SkBitmap& gen_bmp, base::FilePath ref_img_path) { | 37 bool MatchesPNGFile(const SkBitmap& gen_bmp, base::FilePath ref_img_path, |
| 38 const PixelComparator& comparator) { |
38 SkBitmap ref_bmp; | 39 SkBitmap ref_bmp; |
39 if (!ReadPNGFile(ref_img_path, &ref_bmp)) { | 40 if (!ReadPNGFile(ref_img_path, &ref_bmp)) { |
40 LOG(ERROR) << "Cannot read reference image: " << ref_img_path.value(); | 41 LOG(ERROR) << "Cannot read reference image: " << ref_img_path.value(); |
41 return false; | 42 return false; |
42 } | 43 } |
43 | 44 |
44 if (ref_bmp.width() != gen_bmp.width() || | 45 return comparator.Compare(gen_bmp, ref_bmp); |
45 ref_bmp.height() != gen_bmp.height()) { | |
46 LOG(ERROR) | |
47 << "Dimensions do not match (Expected) vs (Actual):" | |
48 << "(" << ref_bmp.width() << "x" << ref_bmp.height() | |
49 << ") vs. " | |
50 << "(" << gen_bmp.width() << "x" << gen_bmp.height() << ")"; | |
51 return false; | |
52 } | |
53 | |
54 // Compare pixels and create a simple diff image. | |
55 int diff_pixels_count = 0; | |
56 SkAutoLockPixels lock_bmp(gen_bmp); | |
57 SkAutoLockPixels lock_ref_bmp(ref_bmp); | |
58 // The reference images were saved with no alpha channel. Use the mask to | |
59 // set alpha to 0. | |
60 uint32_t kAlphaMask = 0x00FFFFFF; | |
61 for (int x = 0; x < gen_bmp.width(); ++x) { | |
62 for (int y = 0; y < gen_bmp.height(); ++y) { | |
63 if ((*gen_bmp.getAddr32(x, y) & kAlphaMask) != | |
64 (*ref_bmp.getAddr32(x, y) & kAlphaMask)) { | |
65 ++diff_pixels_count; | |
66 } | |
67 } | |
68 } | |
69 | |
70 if (diff_pixels_count != 0) { | |
71 LOG(ERROR) << "Images differ by pixel count: " << diff_pixels_count; | |
72 return false; | |
73 } | |
74 | |
75 return true; | |
76 } | 46 } |
77 | 47 |
78 } // namespace cc | 48 } // namespace cc |
OLD | NEW |