2012-12-05 20:13:12 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2012 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
#include "skdiff.h"
|
|
|
|
#include "skdiff_utils.h"
|
2016-11-23 15:55:18 +00:00
|
|
|
#include "sk_tool_utils.h"
|
2012-12-05 20:13:12 +00:00
|
|
|
#include "SkBitmap.h"
|
2016-03-17 14:17:54 +00:00
|
|
|
#include "SkCodec.h"
|
2012-12-05 20:13:12 +00:00
|
|
|
#include "SkData.h"
|
|
|
|
#include "SkImageEncoder.h"
|
|
|
|
#include "SkStream.h"
|
|
|
|
#include "SkTypes.h"
|
|
|
|
|
2015-08-19 18:56:48 +00:00
|
|
|
#include <memory>
|
|
|
|
|
2012-12-05 20:13:12 +00:00
|
|
|
bool are_buffers_equal(SkData* skdata1, SkData* skdata2) {
|
2015-08-27 14:41:13 +00:00
|
|
|
if ((nullptr == skdata1) || (nullptr == skdata2)) {
|
2012-12-05 20:13:12 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (skdata1->size() != skdata2->size()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return (0 == memcmp(skdata1->data(), skdata2->data(), skdata1->size()));
|
|
|
|
}
|
|
|
|
|
2016-08-02 21:40:46 +00:00
|
|
|
sk_sp<SkData> read_file(const char* file_path) {
|
|
|
|
sk_sp<SkData> data(SkData::MakeFromFileName(file_path));
|
2014-09-12 19:12:27 +00:00
|
|
|
if (!data) {
|
2012-12-05 20:13:12 +00:00
|
|
|
SkDebugf("WARNING: could not open file <%s> for reading\n", file_path);
|
|
|
|
}
|
2014-09-12 19:12:27 +00:00
|
|
|
return data;
|
2012-12-05 20:13:12 +00:00
|
|
|
}
|
|
|
|
|
2016-09-12 19:01:44 +00:00
|
|
|
bool get_bitmap(sk_sp<SkData> fileBits, DiffResource& resource, bool sizeOnly) {
|
2016-11-03 18:40:50 +00:00
|
|
|
std::unique_ptr<SkCodec> codec(SkCodec::NewFromData(fileBits));
|
2016-03-17 14:17:54 +00:00
|
|
|
if (!codec) {
|
|
|
|
SkDebugf("ERROR: could not create codec for <%s>\n", resource.fFullPath.c_str());
|
|
|
|
resource.fStatus = DiffResource::kCouldNotDecode_Status;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!resource.fBitmap.setInfo(codec->getInfo().makeColorType(kN32_SkColorType))) {
|
|
|
|
SkDebugf("ERROR: could not set bitmap info for <%s>\n", resource.fFullPath.c_str());
|
|
|
|
resource.fStatus = DiffResource::kCouldNotDecode_Status;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sizeOnly) {
|
|
|
|
return true;
|
|
|
|
}
|
2012-12-05 20:13:12 +00:00
|
|
|
|
2016-03-17 14:17:54 +00:00
|
|
|
if (!resource.fBitmap.tryAllocPixels()) {
|
|
|
|
SkDebugf("ERROR: could not allocate pixels for <%s>\n", resource.fFullPath.c_str());
|
2012-12-05 20:13:12 +00:00
|
|
|
resource.fStatus = DiffResource::kCouldNotDecode_Status;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2016-03-17 14:17:54 +00:00
|
|
|
if (SkCodec::kSuccess != codec->getPixels(resource.fBitmap.info(),
|
|
|
|
resource.fBitmap.getPixels(), resource.fBitmap.rowBytes())) {
|
2012-12-05 20:13:12 +00:00
|
|
|
SkDebugf("ERROR: codec failed for basePath <%s>\n", resource.fFullPath.c_str());
|
|
|
|
resource.fStatus = DiffResource::kCouldNotDecode_Status;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
resource.fStatus = DiffResource::kDecoded_Status;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Thanks to PNG, we need to force all pixels 100% opaque. */
|
|
|
|
static void force_all_opaque(const SkBitmap& bitmap) {
|
|
|
|
for (int y = 0; y < bitmap.height(); y++) {
|
|
|
|
for (int x = 0; x < bitmap.width(); x++) {
|
|
|
|
*bitmap.getAddr32(x, y) |= (SK_A32_MASK << SK_A32_SHIFT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool write_bitmap(const SkString& path, const SkBitmap& bitmap) {
|
|
|
|
SkBitmap copy;
|
2014-04-11 17:15:40 +00:00
|
|
|
bitmap.copyTo(©, kN32_SkColorType);
|
2012-12-05 20:13:12 +00:00
|
|
|
force_all_opaque(copy);
|
2016-11-23 15:55:18 +00:00
|
|
|
return sk_tool_utils::EncodeImageToFile(path.c_str(), copy,
|
|
|
|
SkEncodedImageFormat::kPNG, 100);
|
2012-12-05 20:13:12 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Return a copy of the "input" string, within which we have replaced all instances
|
|
|
|
/// of oldSubstring with newSubstring.
|
|
|
|
///
|
|
|
|
/// TODO: If we like this, we should move it into the core SkString implementation,
|
|
|
|
/// adding more checks and ample test cases, and paying more attention to efficiency.
|
|
|
|
static SkString replace_all(const SkString &input,
|
|
|
|
const char oldSubstring[], const char newSubstring[]) {
|
|
|
|
SkString output;
|
|
|
|
const char *input_cstr = input.c_str();
|
|
|
|
const char *first_char = input_cstr;
|
|
|
|
const char *match_char;
|
2013-10-14 21:53:24 +00:00
|
|
|
size_t oldSubstringLen = strlen(oldSubstring);
|
2014-09-05 20:34:00 +00:00
|
|
|
while ((match_char = strstr(first_char, oldSubstring))) {
|
2012-12-05 20:13:12 +00:00
|
|
|
output.append(first_char, (match_char - first_char));
|
|
|
|
output.append(newSubstring);
|
|
|
|
first_char = match_char + oldSubstringLen;
|
|
|
|
}
|
|
|
|
output.append(first_char);
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
|
|
|
static SkString filename_to_derived_filename(const SkString& filename, const char *suffix) {
|
|
|
|
SkString diffName (filename);
|
|
|
|
const char* cstring = diffName.c_str();
|
2013-10-15 17:47:36 +00:00
|
|
|
size_t dotOffset = strrchr(cstring, '.') - cstring;
|
2012-12-05 20:13:12 +00:00
|
|
|
diffName.remove(dotOffset, diffName.size() - dotOffset);
|
|
|
|
diffName.append(suffix);
|
|
|
|
|
|
|
|
// In case we recursed into subdirectories, replace slashes with something else
|
|
|
|
// so the diffs will all be written into a single flat directory.
|
|
|
|
diffName = replace_all(diffName, PATH_DIV_STR, "_");
|
|
|
|
return diffName;
|
|
|
|
}
|
|
|
|
|
|
|
|
SkString filename_to_diff_filename(const SkString& filename) {
|
|
|
|
return filename_to_derived_filename(filename, "-diff.png");
|
|
|
|
}
|
|
|
|
|
|
|
|
SkString filename_to_white_filename(const SkString& filename) {
|
|
|
|
return filename_to_derived_filename(filename, "-white.png");
|
|
|
|
}
|
|
|
|
|
|
|
|
void create_and_write_diff_image(DiffRecord* drp,
|
|
|
|
DiffMetricProc dmp,
|
|
|
|
const int colorThreshold,
|
|
|
|
const SkString& outputDir,
|
|
|
|
const SkString& filename) {
|
|
|
|
const int w = drp->fBase.fBitmap.width();
|
|
|
|
const int h = drp->fBase.fBitmap.height();
|
|
|
|
|
|
|
|
if (w != drp->fComparison.fBitmap.width() || h != drp->fComparison.fBitmap.height()) {
|
|
|
|
drp->fResult = DiffRecord::kDifferentSizes_Result;
|
|
|
|
} else {
|
2014-06-10 02:52:07 +00:00
|
|
|
drp->fDifference.fBitmap.allocN32Pixels(w, h);
|
2012-12-05 20:13:12 +00:00
|
|
|
|
2014-06-10 02:52:07 +00:00
|
|
|
drp->fWhite.fBitmap.allocN32Pixels(w, h);
|
2012-12-05 20:13:12 +00:00
|
|
|
|
|
|
|
SkASSERT(DiffRecord::kUnknown_Result == drp->fResult);
|
|
|
|
compute_diff(drp, dmp, colorThreshold);
|
|
|
|
SkASSERT(DiffRecord::kUnknown_Result != drp->fResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (outputDir.isEmpty()) {
|
|
|
|
drp->fDifference.fStatus = DiffResource::kUnspecified_Status;
|
|
|
|
drp->fWhite.fStatus = DiffResource::kUnspecified_Status;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
drp->fDifference.fFilename = filename_to_diff_filename(filename);
|
|
|
|
drp->fDifference.fFullPath = outputDir;
|
|
|
|
drp->fDifference.fFullPath.append(drp->fDifference.fFilename);
|
|
|
|
drp->fDifference.fStatus = DiffResource::kSpecified_Status;
|
|
|
|
|
|
|
|
drp->fWhite.fFilename = filename_to_white_filename(filename);
|
|
|
|
drp->fWhite.fFullPath = outputDir;
|
|
|
|
drp->fWhite.fFullPath.append(drp->fWhite.fFilename);
|
|
|
|
drp->fWhite.fStatus = DiffResource::kSpecified_Status;
|
|
|
|
|
|
|
|
if (DiffRecord::kDifferentPixels_Result == drp->fResult) {
|
|
|
|
if (write_bitmap(drp->fDifference.fFullPath, drp->fDifference.fBitmap)) {
|
|
|
|
drp->fDifference.fStatus = DiffResource::kExists_Status;
|
|
|
|
} else {
|
|
|
|
drp->fDifference.fStatus = DiffResource::kDoesNotExist_Status;
|
|
|
|
}
|
|
|
|
if (write_bitmap(drp->fWhite.fFullPath, drp->fWhite.fBitmap)) {
|
|
|
|
drp->fWhite.fStatus = DiffResource::kExists_Status;
|
|
|
|
} else {
|
|
|
|
drp->fWhite.fStatus = DiffResource::kDoesNotExist_Status;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|