add util to compare images

Bug: skia:
Change-Id: Ia64e517c2fbb5560243c07ab17a887a104e07559
Reviewed-on: https://skia-review.googlesource.com/79721
Reviewed-by: Mike Reed <reed@google.com>
Commit-Queue: Mike Reed <reed@google.com>
This commit is contained in:
Mike Reed 2017-12-04 16:06:03 -05:00 committed by Skia Commit-Bot
parent d1ec4105f7
commit 2484660d1b
2 changed files with 28 additions and 6 deletions

View File

@ -13,6 +13,7 @@
#include "SkCommonFlags.h" #include "SkCommonFlags.h"
#include "SkFontMgr.h" #include "SkFontMgr.h"
#include "SkFontStyle.h" #include "SkFontStyle.h"
#include "SkImage.h"
#include "SkPixelRef.h" #include "SkPixelRef.h"
#include "SkPM4f.h" #include "SkPM4f.h"
#include "SkPoint3.h" #include "SkPoint3.h"
@ -513,11 +514,12 @@ void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
return SkMax32(dr, SkMax32(dg, SkMax32(db, da))); return SkMax32(dr, SkMax32(dg, SkMax32(db, da)));
} }
bool equal_pixels(const SkPixmap& a, const SkPixmap& b, unsigned maxDiff) { bool equal_pixels(const SkPixmap& a, const SkPixmap& b, unsigned maxDiff,
bool respectColorSpace) {
if (a.width() != b.width() || if (a.width() != b.width() ||
a.height() != b.height() || a.height() != b.height() ||
a.colorType() != b.colorType() || a.colorType() != b.colorType() ||
a.colorSpace() != b.colorSpace()) (respectColorSpace && (a.colorSpace() != b.colorSpace())))
{ {
return false; return false;
} }
@ -538,8 +540,23 @@ void copy_to_g8(SkBitmap* dst, const SkBitmap& src) {
return true; return true;
} }
bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1, unsigned maxDiff) { bool equal_pixels(const SkBitmap& bm0, const SkBitmap& bm1, unsigned maxDiff,
bool respectColorSpaces) {
SkPixmap pm0, pm1; SkPixmap pm0, pm1;
return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) && equal_pixels(pm0, pm1, maxDiff); return bm0.peekPixels(&pm0) && bm1.peekPixels(&pm1) &&
equal_pixels(pm0, pm1, maxDiff, respectColorSpaces);
}
bool equal_pixels(const SkImage* a, const SkImage* b, unsigned maxDiff,
bool respectColorSpaces) {
// ensure that peekPixels will succeed
auto imga = a->makeRasterImage();
auto imgb = b->makeRasterImage();
a = imga.get();
b = imgb.get();
SkPixmap pm0, pm1;
return a->peekPixels(&pm0) && b->peekPixels(&pm1) &&
equal_pixels(pm0, pm1, maxDiff, respectColorSpaces);
} }
} // namespace sk_tool_utils } // namespace sk_tool_utils

View File

@ -20,6 +20,7 @@
class SkBitmap; class SkBitmap;
class SkCanvas; class SkCanvas;
class SkColorFilter; class SkColorFilter;
class SkImage;
class SkPaint; class SkPaint;
class SkPath; class SkPath;
class SkRRect; class SkRRect;
@ -79,8 +80,12 @@ namespace sk_tool_utils {
* *
* If the colorType is half-float, then maxDiff is interpreted as 0..255 --> 0..1 * If the colorType is half-float, then maxDiff is interpreted as 0..255 --> 0..1
*/ */
bool equal_pixels(const SkPixmap&, const SkPixmap&, unsigned maxDiff = 0); bool equal_pixels(const SkPixmap&, const SkPixmap&, unsigned maxDiff = 0,
bool equal_pixels(const SkBitmap&, const SkBitmap&, unsigned maxDiff = 0); bool respectColorSpaces = false);
bool equal_pixels(const SkBitmap&, const SkBitmap&, unsigned maxDiff = 0,
bool respectColorSpaces = false);
bool equal_pixels(const SkImage* a, const SkImage* b, unsigned maxDiff,
bool respectColorSpaces = false);
// private to sk_tool_utils // private to sk_tool_utils
sk_sp<SkTypeface> create_font(const char* name, SkFontStyle); sk_sp<SkTypeface> create_font(const char* name, SkFontStyle);