2013-07-17 19:29:19 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2013-11-12 18:29:17 +00:00
|
|
|
#include "SkDifferentPixelsMetric.h"
|
2013-07-17 19:29:19 +00:00
|
|
|
|
|
|
|
#include "SkBitmap.h"
|
|
|
|
#include "skpdiff_util.h"
|
|
|
|
|
2013-11-12 18:29:17 +00:00
|
|
|
const char* SkDifferentPixelsMetric::getName() const {
|
2013-07-17 19:29:19 +00:00
|
|
|
return "different_pixels";
|
|
|
|
}
|
|
|
|
|
2014-07-02 14:43:04 +00:00
|
|
|
bool SkDifferentPixelsMetric::diff(SkBitmap* baseline, SkBitmap* test,
|
|
|
|
const BitmapsToCreate& bitmapsToCreate,
|
2013-11-12 18:29:17 +00:00
|
|
|
Result* result) const {
|
2013-07-17 19:29:19 +00:00
|
|
|
double startTime = get_seconds();
|
|
|
|
|
|
|
|
// Ensure the images are comparable
|
|
|
|
if (baseline->width() != test->width() || baseline->height() != test->height() ||
|
|
|
|
baseline->width() <= 0 || baseline->height() <= 0 ||
|
2014-06-17 15:38:31 +00:00
|
|
|
baseline->colorType() != test->colorType()) {
|
2014-07-02 14:43:04 +00:00
|
|
|
SkASSERT(baseline->width() == test->width());
|
|
|
|
SkASSERT(baseline->height() == test->height());
|
|
|
|
SkASSERT(baseline->width() > 0);
|
|
|
|
SkASSERT(baseline->height() > 0);
|
|
|
|
SkASSERT(baseline->colorType() == test->colorType());
|
2013-11-12 18:29:17 +00:00
|
|
|
return false;
|
2013-07-17 19:29:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int width = baseline->width();
|
|
|
|
int height = baseline->height();
|
2014-07-02 14:43:04 +00:00
|
|
|
int maxRedDiff = 0;
|
|
|
|
int maxGreenDiff = 0;
|
|
|
|
int maxBlueDiff = 0;
|
2013-07-17 19:29:19 +00:00
|
|
|
|
2014-07-02 14:43:04 +00:00
|
|
|
// Prepare any bitmaps we will be filling in
|
|
|
|
if (bitmapsToCreate.alphaMask) {
|
2014-06-10 02:52:07 +00:00
|
|
|
result->poiAlphaMask.allocPixels(SkImageInfo::MakeA8(width, height));
|
2013-11-12 18:29:17 +00:00
|
|
|
result->poiAlphaMask.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0);
|
2013-11-07 19:24:06 +00:00
|
|
|
}
|
2014-07-02 14:43:04 +00:00
|
|
|
if (bitmapsToCreate.rgbDiff) {
|
|
|
|
result->rgbDiffBitmap.allocPixels(SkImageInfo::Make(width, height, baseline->colorType(),
|
|
|
|
kPremul_SkAlphaType));
|
|
|
|
result->rgbDiffBitmap.eraseARGB(SK_AlphaTRANSPARENT, 0, 0, 0);
|
|
|
|
}
|
|
|
|
if (bitmapsToCreate.whiteDiff) {
|
|
|
|
result->whiteDiffBitmap.allocPixels(SkImageInfo::MakeN32Premul(width, height));
|
|
|
|
result->whiteDiffBitmap.eraseARGB(SK_AlphaOPAQUE, 0, 0, 0);
|
|
|
|
}
|
2013-11-07 19:24:06 +00:00
|
|
|
|
2013-07-17 19:29:19 +00:00
|
|
|
// Prepare the pixels for comparison
|
2013-11-12 18:29:17 +00:00
|
|
|
result->poiCount = 0;
|
2013-07-17 19:29:19 +00:00
|
|
|
baseline->lockPixels();
|
|
|
|
test->lockPixels();
|
|
|
|
for (int y = 0; y < height; y++) {
|
|
|
|
// Grab a row from each image for easy comparison
|
2014-07-02 14:43:04 +00:00
|
|
|
// TODO(epoger): The code below already assumes 4 bytes per pixel, so I think
|
|
|
|
// we could just call getAddr32() to save a little time.
|
|
|
|
// OR, if we want to play it safe, call ComputeBytesPerPixel instead
|
|
|
|
// of assuming 4 bytes per pixel.
|
|
|
|
uint32_t* baselineRow = static_cast<uint32_t *>(baseline->getAddr(0, y));
|
|
|
|
uint32_t* testRow = static_cast<uint32_t *>(test->getAddr(0, y));
|
2013-07-17 19:29:19 +00:00
|
|
|
for (int x = 0; x < width; x++) {
|
|
|
|
// Compare one pixel at a time so each differing pixel can be noted
|
2014-07-02 14:43:04 +00:00
|
|
|
// TODO(epoger): This loop looks like a good place to work on performance,
|
|
|
|
// but we should run the code through a profiler to be sure.
|
|
|
|
uint32_t baselinePixel = baselineRow[x];
|
|
|
|
uint32_t testPixel = testRow[x];
|
|
|
|
if (baselinePixel != testPixel) {
|
2013-11-12 18:29:17 +00:00
|
|
|
result->poiCount++;
|
2014-07-02 14:43:04 +00:00
|
|
|
|
2015-08-26 12:15:46 +00:00
|
|
|
int redDiff = SkTAbs(static_cast<int>(SkColorGetR(baselinePixel) -
|
|
|
|
SkColorGetR(testPixel)));
|
2014-07-02 14:43:04 +00:00
|
|
|
if (redDiff > maxRedDiff) {maxRedDiff = redDiff;}
|
2015-08-26 12:15:46 +00:00
|
|
|
int greenDiff = SkTAbs(static_cast<int>(SkColorGetG(baselinePixel) -
|
|
|
|
SkColorGetG(testPixel)));
|
2014-07-02 14:43:04 +00:00
|
|
|
if (greenDiff > maxGreenDiff) {maxGreenDiff = greenDiff;}
|
2015-08-26 12:15:46 +00:00
|
|
|
int blueDiff = SkTAbs(static_cast<int>(SkColorGetB(baselinePixel) -
|
|
|
|
SkColorGetB(testPixel)));
|
2014-07-02 14:43:04 +00:00
|
|
|
if (blueDiff > maxBlueDiff) {maxBlueDiff = blueDiff;}
|
|
|
|
|
|
|
|
if (bitmapsToCreate.alphaMask) {
|
2013-11-12 18:29:17 +00:00
|
|
|
*result->poiAlphaMask.getAddr8(x,y) = SK_AlphaTRANSPARENT;
|
2013-11-07 19:24:06 +00:00
|
|
|
}
|
2014-07-02 14:43:04 +00:00
|
|
|
if (bitmapsToCreate.rgbDiff) {
|
|
|
|
*result->rgbDiffBitmap.getAddr32(x,y) =
|
|
|
|
SkColorSetRGB(redDiff, greenDiff, blueDiff);
|
|
|
|
}
|
|
|
|
if (bitmapsToCreate.whiteDiff) {
|
|
|
|
*result->whiteDiffBitmap.getAddr32(x,y) = SK_ColorWHITE;
|
|
|
|
}
|
2013-07-17 19:29:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
test->unlockPixels();
|
|
|
|
baseline->unlockPixels();
|
|
|
|
|
2014-07-02 14:43:04 +00:00
|
|
|
result->maxRedDiff = maxRedDiff;
|
|
|
|
result->maxGreenDiff = maxGreenDiff;
|
|
|
|
result->maxBlueDiff = maxBlueDiff;
|
|
|
|
|
|
|
|
if (bitmapsToCreate.alphaMask) {
|
2013-11-12 18:29:17 +00:00
|
|
|
result->poiAlphaMask.unlockPixels();
|
2013-07-17 19:29:19 +00:00
|
|
|
}
|
2014-07-02 14:43:04 +00:00
|
|
|
if (bitmapsToCreate.rgbDiff) {
|
|
|
|
result->rgbDiffBitmap.unlockPixels();
|
|
|
|
}
|
|
|
|
if (bitmapsToCreate.whiteDiff) {
|
|
|
|
result->whiteDiffBitmap.unlockPixels();
|
|
|
|
}
|
2013-07-17 19:29:19 +00:00
|
|
|
|
2013-11-12 18:29:17 +00:00
|
|
|
// Calculates the percentage of identical pixels
|
|
|
|
result->result = 1.0 - ((double)result->poiCount / (width * height));
|
|
|
|
result->timeElapsed = get_seconds() - startTime;
|
2013-11-07 19:24:06 +00:00
|
|
|
|
2013-11-12 18:29:17 +00:00
|
|
|
return true;
|
2013-11-07 19:24:06 +00:00
|
|
|
}
|