Home | History | Annotate | Download | only in test
      1 // Copyright (c) 2013 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 #ifndef CC_TEST_PIXEL_COMPARATOR_H_
      6 #define CC_TEST_PIXEL_COMPARATOR_H_
      7 
      8 #include "base/compiler_specific.h"
      9 #include "third_party/skia/include/core/SkBitmap.h"
     10 
     11 namespace cc {
     12 
     13 // Interface for pixel comparators.
     14 class PixelComparator {
     15  public:
     16   virtual ~PixelComparator() {}
     17 
     18   virtual bool Compare(const SkBitmap& actual_bmp,
     19                        const SkBitmap& expected_bmp) const = 0;
     20 };
     21 
     22 // Exact pixel comparator. Counts the number of pixel with an error.
     23 class ExactPixelComparator : public PixelComparator {
     24  public:
     25   explicit ExactPixelComparator(const bool discard_alpha);
     26   virtual ~ExactPixelComparator() {}
     27 
     28   // Returns true if the two bitmaps are identical. Otherwise, returns false
     29   // and report the number of pixels with an error on LOG(ERROR). Differences
     30   // in the alpha channel are ignored.
     31   virtual bool Compare(const SkBitmap& actual_bmp,
     32                        const SkBitmap& expected_bmp) const OVERRIDE;
     33 
     34  private:
     35   // Exclude alpha channel from comparison?
     36   bool discard_alpha_;
     37 };
     38 
     39 // Fuzzy pixel comparator. Counts small and arbitrary errors separately and
     40 // computes average and maximum absolute errors per color channel.
     41 class FuzzyPixelComparator : public PixelComparator {
     42  public:
     43   FuzzyPixelComparator(const bool discard_alpha,
     44                        const float error_pixels_percentage_limit,
     45                        const float small_error_pixels_percentage_limit,
     46                        const float avg_abs_error_limit,
     47                        const int max_abs_error_limit,
     48                        const int small_error_threshold);
     49   virtual ~FuzzyPixelComparator() {}
     50 
     51   // Computes error metrics and returns true if the errors don't exceed the
     52   // specified limits. Otherwise, returns false and reports the error metrics on
     53   // LOG(ERROR). Differences in the alpha channel are ignored.
     54   virtual bool Compare(const SkBitmap& actual_bmp,
     55                        const SkBitmap& expected_bmp) const OVERRIDE;
     56 
     57  private:
     58   // Exclude alpha channel from comparison?
     59   bool discard_alpha_;
     60   // Limit for percentage of pixels with an error.
     61   float error_pixels_percentage_limit_;
     62   // Limit for percentage of pixels with a small error.
     63   float small_error_pixels_percentage_limit_;
     64   // Limit for average absolute error (excluding identical pixels).
     65   float avg_abs_error_limit_;
     66   // Limit for largest absolute error.
     67   int max_abs_error_limit_;
     68   // Threshold for small errors.
     69   int small_error_threshold_;
     70 };
     71 
     72 }  // namespace cc
     73 
     74 #endif  // CC_TEST_PIXEL_COMPARATOR_H_
     75