1cb93a386Sopenharmony_ci// Copyright 2019 Google LLC. 2cb93a386Sopenharmony_ci// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file. 3cb93a386Sopenharmony_ci 4cb93a386Sopenharmony_ci// Image Diff: Provides a metric for measuring the difference between two encoded images. Prints 5cb93a386Sopenharmony_ci// out a single floating-point number between 0.0 and 1.0; 0 means that the images are identical; 1 6cb93a386Sopenharmony_ci// means that each pixel is maximally different in each channel. A non-zero return value indicates 7cb93a386Sopenharmony_ci// that something went wrong. 8cb93a386Sopenharmony_ci 9cb93a386Sopenharmony_ci#include "include/codec/SkCodec.h" 10cb93a386Sopenharmony_ci#include "include/core/SkBitmap.h" 11cb93a386Sopenharmony_ci#include "include/core/SkData.h" 12cb93a386Sopenharmony_ci#include "include/core/SkPixmap.h" 13cb93a386Sopenharmony_ci#include "include/core/SkSize.h" 14cb93a386Sopenharmony_ci 15cb93a386Sopenharmony_ci#include <cmath> 16cb93a386Sopenharmony_ci#include <cstdio> 17cb93a386Sopenharmony_ci 18cb93a386Sopenharmony_ciint main(int argc, char** argv) { 19cb93a386Sopenharmony_ci if (argc != 3) { 20cb93a386Sopenharmony_ci const char usage[] = "\nUsage:\n %s {FILE1}.png {FILE2}.png\n\n"; 21cb93a386Sopenharmony_ci fprintf(stderr, usage, argv[0]); 22cb93a386Sopenharmony_ci return 1; 23cb93a386Sopenharmony_ci } 24cb93a386Sopenharmony_ci SkBitmap bm[2]; 25cb93a386Sopenharmony_ci for (int i = 0; i < 2; ++i) { 26cb93a386Sopenharmony_ci const char* path = argv[i + 1]; 27cb93a386Sopenharmony_ci if (std::unique_ptr<SkCodec> codec = 28cb93a386Sopenharmony_ci SkCodec::MakeFromData(SkData::MakeFromFileName(path))) { 29cb93a386Sopenharmony_ci bm[i].allocN32Pixels(codec->dimensions().fWidth, codec->dimensions().fHeight); 30cb93a386Sopenharmony_ci if (SkCodec::kSuccess == codec->getPixels(bm[i].pixmap())) { 31cb93a386Sopenharmony_ci continue; 32cb93a386Sopenharmony_ci } 33cb93a386Sopenharmony_ci } 34cb93a386Sopenharmony_ci fprintf(stderr, "\nBad file: '%s'.\n\n", path); 35cb93a386Sopenharmony_ci return 2; 36cb93a386Sopenharmony_ci } 37cb93a386Sopenharmony_ci SkISize dim = bm[0].dimensions(); 38cb93a386Sopenharmony_ci if (dim != bm[1].dimensions()) { 39cb93a386Sopenharmony_ci fprintf(stderr, "\nImages must be same size: (%d,%d) != (%d,%d)\n\n", 40cb93a386Sopenharmony_ci dim.fWidth, dim.fHeight, bm[1].dimensions().fWidth, bm[1].dimensions().fHeight); 41cb93a386Sopenharmony_ci return 3; 42cb93a386Sopenharmony_ci } 43cb93a386Sopenharmony_ci int64_t totalDiffs = 0; // Manhattan distance in ARGB color-space. 44cb93a386Sopenharmony_ci for (int y = 0; y < dim.fHeight; ++y) { 45cb93a386Sopenharmony_ci const uint8_t* row1 = reinterpret_cast<const uint8_t*>(bm[0].pixmap().addr32(0, y)); 46cb93a386Sopenharmony_ci const uint8_t* row2 = reinterpret_cast<const uint8_t*>(bm[1].pixmap().addr32(0, y)); 47cb93a386Sopenharmony_ci for (size_t i = 0; i < (size_t)dim.fWidth * (size_t)4; ++i) { 48cb93a386Sopenharmony_ci totalDiffs += std::abs((int)row1[i] - (int)row2[i]); 49cb93a386Sopenharmony_ci } 50cb93a386Sopenharmony_ci } 51cb93a386Sopenharmony_ci printf("%g\n", (double)totalDiffs / 52cb93a386Sopenharmony_ci ((uint64_t)255 * 4 * (uint64_t)dim.fWidth * (uint64_t)dim.fHeight)); 53cb93a386Sopenharmony_ci return 0; 54cb93a386Sopenharmony_ci} 55