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 // Check if images size matches |
45 ref_bmp.height() != gen_bmp.height()) { | 46 if (gen_bmp.width() != ref_bmp.width() || |
47 gen_bmp.height() != ref_bmp.height()) { | |
46 LOG(ERROR) | 48 LOG(ERROR) |
47 << "Dimensions do not match (Expected) vs (Actual):" | 49 << "Dimensions do not match! " |
48 << "(" << ref_bmp.width() << "x" << ref_bmp.height() | 50 << "Actual: " << gen_bmp.width() << "x" << gen_bmp.height() |
49 << ") vs. " | 51 << "; " |
50 << "(" << gen_bmp.width() << "x" << gen_bmp.height() << ")"; | 52 << "Expected: " << ref_bmp.width() << "x" << ref_bmp.height(); |
reveman
2013/03/08 04:41:57
nit: I'd avoid changing this just to make the patc
| |
51 return false; | 53 return false; |
52 } | 54 } |
53 | 55 |
54 // Compare pixels and create a simple diff image. | 56 // Shortcut for empty images. They are always equal. |
55 int diff_pixels_count = 0; | 57 if (gen_bmp.width() == 0 || gen_bmp.height() == 0) |
56 SkAutoLockPixels lock_bmp(gen_bmp); | 58 return true; |
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; | |
reveman
2013/03/08 04:41:57
oh, sorry I didn't realize the old code was doing
| |
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 | 59 |
70 if (diff_pixels_count != 0) { | 60 return comparator.Compare(gen_bmp, ref_bmp); |
71 LOG(ERROR) << "Images differ by pixel count: " << diff_pixels_count; | |
72 return false; | |
73 } | |
74 | |
75 return true; | |
76 } | 61 } |
77 | 62 |
78 } // namespace cc | 63 } // namespace cc |
OLD | NEW |