OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "cc/test/pixel_test_utils.h" | |
6 | |
7 #include "base/file_util.h" | |
8 #include "third_party/skia/include/core/SkBitmap.h" | |
9 #include "ui/gfx/codec/png_codec.h" | |
10 | |
11 namespace cc { | |
12 namespace test { | |
13 | |
14 bool WritePNGFile(const SkBitmap& bitmap, const FilePath& file_path) { | |
15 std::vector<unsigned char> png_data; | |
16 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, true, &png_data) && | |
danakj
2012/12/03 19:14:10
Use a temp var here for discard_transparency pleas
| |
17 file_util::CreateDirectory(file_path.DirName())) { | |
18 char* data = reinterpret_cast<char*>(&png_data[0]); | |
19 int size = static_cast<int>(png_data.size()); | |
danakj
2012/12/03 19:14:10
doh /o\
| |
20 return file_util::WriteFile(file_path, data, size) == size; | |
21 } | |
22 return false; | |
23 } | |
24 | |
25 bool ReadPNGFile(const FilePath& file_path, SkBitmap* bitmap) { | |
26 DCHECK(bitmap); | |
27 std::string png_data; | |
28 return file_util::ReadFileToString(file_path, &png_data) && | |
29 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data[0]), | |
30 png_data.length(), | |
31 bitmap); | |
32 } | |
33 | |
34 bool IsSameAsPNGFile(const SkBitmap& gen_bmp, FilePath ref_img_path) { | |
35 SkBitmap ref_bmp; | |
36 if (!ReadPNGFile(ref_img_path, &ref_bmp)) { | |
37 LOG(ERROR) << "Cannot read reference image: " << ref_img_path.value(); | |
38 return false; | |
39 } | |
40 | |
41 if (ref_bmp.width() != gen_bmp.width() || | |
42 ref_bmp.height() != gen_bmp.height()) { | |
43 LOG(ERROR) | |
44 << "Dimensions do not match (Expected) vs (Actual):" | |
45 << "(" << ref_bmp.width() << "x" << ref_bmp.height() | |
46 << ") vs. " | |
47 << "(" << gen_bmp.width() << "x" << gen_bmp.height() << ")"; | |
48 return false; | |
49 } | |
50 | |
51 // Compare pixels and create a simple diff image. | |
52 int diff_pixels_count = 0; | |
53 SkAutoLockPixels lock_bmp(gen_bmp); | |
54 SkAutoLockPixels lock_ref_bmp(ref_bmp); | |
55 // The reference images were saved with no alpha channel. Use the mask to | |
56 // set alpha to 0. | |
57 uint32_t kAlphaMask = 0x00FFFFFF; | |
58 for (int x = 0; x < gen_bmp.width(); ++x) { | |
59 for (int y = 0; y < gen_bmp.height(); ++y) { | |
60 if ((*gen_bmp.getAddr32(x, y) & kAlphaMask) != | |
61 (*ref_bmp.getAddr32(x, y) & kAlphaMask)) { | |
62 ++diff_pixels_count; | |
63 } | |
64 } | |
65 } | |
66 | |
67 if (diff_pixels_count != 0) { | |
68 LOG(ERROR) << "Images differ by pixel count: " << diff_pixels_count; | |
69 return false; | |
70 } | |
71 | |
72 return true; | |
73 } | |
74 | |
75 } // namespace test | |
76 } // namespace cc | |
77 | |
OLD | NEW |