This source file includes following definitions.
- h_
- data_
- has_image
- w
- h
- data
- CreateFromStdin
- CreateFromFilename
- Clear
- pixel_at
- set_pixel_at
- PercentageDifferent
- HistogramPercentageDifferent
- PrintHelp
- CompareImages
- CreateImageDiff
- DiffImages
- FilePathFromASCII
- main
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#include "base/basictypes.h"
#include "base/command_line.h"
#include "base/containers/hash_tables.h"
#include "base/file_util.h"
#include "base/files/file_path.h"
#include "base/logging.h"
#include "base/memory/scoped_ptr.h"
#include "base/numerics/safe_conversions.h"
#include "base/process/memory.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "tools/imagediff/image_diff_png.h"
#if defined(OS_WIN)
#include "windows.h"
#endif
static const char kOptionPollStdin[] = "use-stdin";
static const char kOptionCompareHistograms[] = "histogram";
static const char kOptionGenerateDiff[] = "diff";
static const int kStatusSame = 0;
static const int kStatusDifferent = 1;
static const int kStatusError = 2;
static const uint32 RGBA_RED = 0x000000ff;
static const uint32 RGBA_ALPHA = 0xff000000;
class Image {
public:
Image() : w_(0), h_(0) {
}
Image(const Image& image)
: w_(image.w_),
h_(image.h_),
data_(image.data_) {
}
bool has_image() const {
return w_ > 0 && h_ > 0;
}
int w() const {
return w_;
}
int h() const {
return h_;
}
const unsigned char* data() const {
return &data_.front();
}
bool CreateFromStdin(size_t byte_length) {
if (byte_length == 0)
return false;
scoped_ptr<unsigned char[]> source(new unsigned char[byte_length]);
if (fread(source.get(), 1, byte_length, stdin) != byte_length)
return false;
if (!image_diff_png::DecodePNG(source.get(), byte_length,
&data_, &w_, &h_)) {
Clear();
return false;
}
return true;
}
bool CreateFromFilename(const base::FilePath& path) {
FILE* f = base::OpenFile(path, "rb");
if (!f)
return false;
std::vector<unsigned char> compressed;
const int buf_size = 1024;
unsigned char buf[buf_size];
size_t num_read = 0;
while ((num_read = fread(buf, 1, buf_size, f)) > 0) {
compressed.insert(compressed.end(), buf, buf + num_read);
}
base::CloseFile(f);
if (!image_diff_png::DecodePNG(&compressed[0], compressed.size(),
&data_, &w_, &h_)) {
Clear();
return false;
}
return true;
}
void Clear() {
w_ = h_ = 0;
data_.clear();
}
uint32 pixel_at(int x, int y) const {
DCHECK(x >= 0 && x < w_);
DCHECK(y >= 0 && y < h_);
return *reinterpret_cast<const uint32*>(&(data_[(y * w_ + x) * 4]));
}
void set_pixel_at(int x, int y, uint32 color) const {
DCHECK(x >= 0 && x < w_);
DCHECK(y >= 0 && y < h_);
void* addr = &const_cast<unsigned char*>(&data_.front())[(y * w_ + x) * 4];
*reinterpret_cast<uint32*>(addr) = color;
}
private:
int w_, h_;
std::vector<unsigned char> data_;
};
float PercentageDifferent(const Image& baseline, const Image& actual) {
int w = std::min(baseline.w(), actual.w());
int h = std::min(baseline.h(), actual.h());
int pixels_different = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
if (baseline.pixel_at(x, y) != actual.pixel_at(x, y))
pixels_different++;
}
}
int max_w = std::max(baseline.w(), actual.w());
int max_h = std::max(baseline.h(), actual.h());
pixels_different += (max_w - w) * h;
pixels_different += (max_h - h) * max_w;
float total_pixels = static_cast<float>(actual.w()) *
static_cast<float>(actual.h());
if (total_pixels == 0) {
return 100.0f;
}
return 100.0f * pixels_different / total_pixels;
}
typedef base::hash_map<uint32, int32> RgbaToCountMap;
float HistogramPercentageDifferent(const Image& baseline, const Image& actual) {
int w = std::min(baseline.w(), actual.w());
int h = std::min(baseline.h(), actual.h());
RgbaToCountMap baseline_histogram;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
baseline_histogram[baseline.pixel_at(x, y)]++;
}
}
int pixels_different = 0;
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
uint32 actual_rgba = actual.pixel_at(x, y);
RgbaToCountMap::iterator it = baseline_histogram.find(actual_rgba);
if (it != baseline_histogram.end() && it->second > 0)
it->second--;
else
pixels_different++;
}
}
int max_w = std::max(baseline.w(), actual.w());
int max_h = std::max(baseline.h(), actual.h());
pixels_different += (max_w - w) * h;
pixels_different += (max_h - h) * max_w;
float total_pixels = static_cast<float>(actual.w()) *
static_cast<float>(actual.h());
if (total_pixels == 0) {
return 100.0f;
}
return 100.0f * pixels_different / total_pixels;
}
void PrintHelp() {
fprintf(stderr,
"Usage:\n"
" image_diff [--histogram] <compare file> <reference file>\n"
" Compares two files on disk, returning 0 when they are the same;\n"
" passing \"--histogram\" additionally calculates a diff of the\n"
" RGBA value histograms (which is resistant to shifts in layout)\n"
" image_diff --use-stdin\n"
" Stays open reading pairs of filenames from stdin, comparing them,\n"
" and sending 0 to stdout when they are the same\n"
" image_diff --diff <compare file> <reference file> <output file>\n"
" Compares two files on disk, outputs an image that visualizes the\n"
" difference to <output file>\n");
}
int CompareImages(const base::FilePath& file1,
const base::FilePath& file2,
bool compare_histograms) {
Image actual_image;
Image baseline_image;
if (!actual_image.CreateFromFilename(file1)) {
fprintf(stderr, "image_diff: Unable to open file \"%" PRFilePath "\"\n",
file1.value().c_str());
return kStatusError;
}
if (!baseline_image.CreateFromFilename(file2)) {
fprintf(stderr, "image_diff: Unable to open file \"%" PRFilePath "\"\n",
file2.value().c_str());
return kStatusError;
}
if (compare_histograms) {
float percent = HistogramPercentageDifferent(actual_image, baseline_image);
const char* passed = percent > 0.0 ? "failed" : "passed";
printf("histogram diff: %01.2f%% %s\n", percent, passed);
}
const char* diff_name = compare_histograms ? "exact diff" : "diff";
float percent = PercentageDifferent(actual_image, baseline_image);
const char* passed = percent > 0.0 ? "failed" : "passed";
printf("%s: %01.2f%% %s\n", diff_name, percent, passed);
if (percent > 0.0) {
return kStatusDifferent;
}
return kStatusSame;
}
bool CreateImageDiff(const Image& image1, const Image& image2, Image* out) {
int w = std::min(image1.w(), image2.w());
int h = std::min(image1.h(), image2.h());
*out = Image(image1);
bool same = (image1.w() == image2.w()) && (image1.h() == image2.h());
for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) {
uint32 base_pixel = image1.pixel_at(x, y);
if (base_pixel != image2.pixel_at(x, y)) {
out->set_pixel_at(x, y, RGBA_RED | RGBA_ALPHA);
same = false;
} else {
uint32 alpha = base_pixel & RGBA_ALPHA;
uint32 new_pixel = base_pixel - ((alpha / 2) & RGBA_ALPHA);
out->set_pixel_at(x, y, new_pixel);
}
}
}
return same;
}
int DiffImages(const base::FilePath& file1, const base::FilePath& file2,
const base::FilePath& out_file) {
Image actual_image;
Image baseline_image;
if (!actual_image.CreateFromFilename(file1)) {
fprintf(stderr, "image_diff: Unable to open file \"%" PRFilePath "\"\n",
file1.value().c_str());
return kStatusError;
}
if (!baseline_image.CreateFromFilename(file2)) {
fprintf(stderr, "image_diff: Unable to open file \"%" PRFilePath "\"\n",
file2.value().c_str());
return kStatusError;
}
Image diff_image;
bool same = CreateImageDiff(baseline_image, actual_image, &diff_image);
if (same)
return kStatusSame;
std::vector<unsigned char> png_encoding;
image_diff_png::EncodeRGBAPNG(
diff_image.data(), diff_image.w(), diff_image.h(),
diff_image.w() * 4, &png_encoding);
if (base::WriteFile(out_file,
reinterpret_cast<char*>(&png_encoding.front()),
base::checked_cast<int>(png_encoding.size())) < 0)
return kStatusError;
return kStatusDifferent;
}
base::FilePath FilePathFromASCII(const std::string& str) {
#if defined(OS_WIN)
return base::FilePath(base::ASCIIToWide(str));
#else
return base::FilePath(str);
#endif
}
int main(int argc, const char* argv[]) {
base::EnableTerminationOnHeapCorruption();
CommandLine::Init(argc, argv);
const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
bool histograms = parsed_command_line.HasSwitch(kOptionCompareHistograms);
if (parsed_command_line.HasSwitch(kOptionPollStdin)) {
std::string stdin_buffer;
base::FilePath filename1;
while (std::getline(std::cin, stdin_buffer)) {
if (stdin_buffer.empty())
continue;
if (!filename1.empty()) {
base::FilePath filename2 = FilePathFromASCII(stdin_buffer);
if (CompareImages(filename1, filename2, histograms) == kStatusError)
printf("error\n");
fflush(stdout);
filename1 = base::FilePath();
} else {
filename1 = FilePathFromASCII(stdin_buffer);
}
}
return 0;
}
const CommandLine::StringVector& args = parsed_command_line.GetArgs();
if (parsed_command_line.HasSwitch(kOptionGenerateDiff)) {
if (args.size() == 3) {
return DiffImages(base::FilePath(args[0]),
base::FilePath(args[1]),
base::FilePath(args[2]));
}
} else if (args.size() == 2) {
return CompareImages(
base::FilePath(args[0]), base::FilePath(args[1]), histograms);
}
PrintHelp();
return kStatusError;
}