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 <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 | 30 |
31 bool ReadPNGFile(const base::FilePath& file_path, SkBitmap* bitmap) { | 31 bool ReadPNGFile(const base::FilePath& file_path, SkBitmap* bitmap) { |
32 DCHECK(bitmap); | 32 DCHECK(bitmap); |
33 std::string png_data; | 33 std::string png_data; |
34 return file_util::ReadFileToString(file_path, &png_data) && | 34 return file_util::ReadFileToString(file_path, &png_data) && |
35 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]), | 35 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]), |
36 png_data.length(), | 36 png_data.length(), |
37 bitmap); | 37 bitmap); |
38 } | 38 } |
39 | 39 |
40 bool IsSameAsPNGFile(const SkBitmap& gen_bmp, base::FilePath ref_img_path, | 40 bool MatchesPNGFile(const SkBitmap& gen_bmp, base::FilePath ref_img_path, |
41 bool discard_transparency) { | 41 const PixelComparator& comparator) { |
42 SkBitmap ref_bmp; | 42 SkBitmap ref_bmp; |
43 if (!ReadPNGFile(ref_img_path, &ref_bmp)) { | 43 if (!ReadPNGFile(ref_img_path, &ref_bmp)) { |
44 LOG(ERROR) << "Cannot read reference image: " << ref_img_path.value(); | 44 LOG(ERROR) << "Cannot read reference image: " << ref_img_path.value(); |
45 return false; | 45 return false; |
46 } | 46 } |
47 | 47 |
48 if (ref_bmp.width() != gen_bmp.width() || | 48 // Check if images size matches |
49 ref_bmp.height() != gen_bmp.height()) { | 49 if (gen_bmp.width() != ref_bmp.width() || |
| 50 gen_bmp.height() != ref_bmp.height()) { |
50 LOG(ERROR) | 51 LOG(ERROR) |
51 << "Dimensions do not match (Expected) vs (Actual):" | 52 << "Dimensions do not match! " |
52 << "(" << ref_bmp.width() << "x" << ref_bmp.height() | 53 << "Actual: " << gen_bmp.width() << "x" << gen_bmp.height() |
53 << ") vs. " | 54 << "; " |
54 << "(" << gen_bmp.width() << "x" << gen_bmp.height() << ")"; | 55 << "Expected: " << ref_bmp.width() << "x" << ref_bmp.height(); |
55 return false; | 56 return false; |
56 } | 57 } |
57 | 58 |
58 // Compare pixels and create a simple diff image. | 59 // Shortcut for empty images. They are always equal. |
59 int diff_pixels_count = 0; | 60 if (gen_bmp.width() == 0 || gen_bmp.height() == 0) |
60 SkAutoLockPixels lock_bmp(gen_bmp); | 61 return true; |
61 SkAutoLockPixels lock_ref_bmp(ref_bmp); | |
62 // The reference images were saved with no alpha channel. Use the mask to | |
63 // set alpha to 0. | |
64 uint32_t kAlphaMask; | |
65 if (discard_transparency) | |
66 kAlphaMask = 0x00FFFFFF; | |
67 else | |
68 kAlphaMask = 0xFFFFFFFF; | |
69 | 62 |
70 for (int x = 0; x < gen_bmp.width(); ++x) { | 63 return comparator.Compare(gen_bmp, ref_bmp); |
71 for (int y = 0; y < gen_bmp.height(); ++y) { | |
72 if ((*gen_bmp.getAddr32(x, y) & kAlphaMask) != | |
73 (*ref_bmp.getAddr32(x, y) & kAlphaMask)) { | |
74 ++diff_pixels_count; | |
75 } | |
76 } | |
77 } | |
78 | |
79 if (diff_pixels_count != 0) { | |
80 LOG(ERROR) << "Images differ by pixel count: " << diff_pixels_count; | |
81 return false; | |
82 } | |
83 | |
84 return true; | |
85 } | 64 } |
86 | 65 |
87 } // namespace cc | 66 } // namespace cc |
OLD | NEW |