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.
5 #include "cc/test/pixel_test_utils.h"
10 #include "base/file_util.h"
11 #include "base/logging.h"
12 #include "third_party/skia/include/core/SkBitmap.h"
13 #include "ui/gfx/codec/png_codec.h"
17 bool WritePNGFile(const SkBitmap
& bitmap
, const base::FilePath
& file_path
,
18 bool discard_transparency
) {
19 std::vector
<unsigned char> png_data
;
20 if (gfx::PNGCodec::EncodeBGRASkBitmap(bitmap
,
23 base::CreateDirectory(file_path
.DirName())) {
24 char* data
= reinterpret_cast<char*>(&png_data
[0]);
25 int size
= static_cast<int>(png_data
.size());
26 return base::WriteFile(file_path
, data
, size
) == size
;
31 bool ReadPNGFile(const base::FilePath
& file_path
, SkBitmap
* bitmap
) {
34 return base::ReadFileToString(file_path
, &png_data
) &&
35 gfx::PNGCodec::Decode(reinterpret_cast<unsigned char*>(&png_data
[0]),
40 bool MatchesPNGFile(const SkBitmap
& gen_bmp
, base::FilePath ref_img_path
,
41 const PixelComparator
& comparator
) {
43 if (!ReadPNGFile(ref_img_path
, &ref_bmp
)) {
44 LOG(ERROR
) << "Cannot read reference image: " << ref_img_path
.value();
48 // Check if images size matches
49 if (gen_bmp
.width() != ref_bmp
.width() ||
50 gen_bmp
.height() != ref_bmp
.height()) {
52 << "Dimensions do not match! "
53 << "Actual: " << gen_bmp
.width() << "x" << gen_bmp
.height()
55 << "Expected: " << ref_bmp
.width() << "x" << ref_bmp
.height();
59 // Shortcut for empty images. They are always equal.
60 if (gen_bmp
.width() == 0 || gen_bmp
.height() == 0)
63 return comparator
.Compare(gen_bmp
, ref_bmp
);