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