2015-05-02 01:11:49 +02:00
|
|
|
// Copyright 2015 Google Inc. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the COPYING file in the root of the source
|
|
|
|
// tree. An additional intellectual property rights grant can be found
|
|
|
|
// in the file PATENTS. All contributing project authors may
|
|
|
|
// be found in the AUTHORS file in the root of the source tree.
|
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Checks if given pair of animated GIF/WebP images are identical:
|
|
|
|
// That is: their reconstructed canvases match pixel-by-pixel and their other
|
|
|
|
// animation properties (loop count etc) also match.
|
|
|
|
//
|
|
|
|
// example: anim_diff foo.gif bar.webp
|
|
|
|
|
2016-04-04 20:43:58 +02:00
|
|
|
#include <assert.h>
|
2015-10-01 20:14:49 +02:00
|
|
|
#include <limits.h>
|
2015-05-02 01:11:49 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h> // for 'strtod'.
|
|
|
|
#include <string.h> // for 'strcmp'.
|
|
|
|
|
|
|
|
#include "./anim_util.h"
|
|
|
|
|
2015-11-06 07:47:03 +01:00
|
|
|
#if defined(_MSC_VER) && _MSC_VER < 1900
|
|
|
|
#define snprintf _snprintf
|
|
|
|
#endif
|
2015-05-02 01:11:49 +02:00
|
|
|
|
2015-11-06 07:47:03 +01:00
|
|
|
// Returns true if 'a + b' will overflow.
|
|
|
|
static int AdditionWillOverflow(int a, int b) {
|
2015-10-01 20:14:49 +02:00
|
|
|
return (b > 0) && (a > INT_MAX - b);
|
|
|
|
}
|
|
|
|
|
2016-04-04 20:43:58 +02:00
|
|
|
static int FramesAreEqual(const uint8_t* const rgba1,
|
|
|
|
const uint8_t* const rgba2, int width, int height) {
|
|
|
|
const int stride = width * 4; // Always true for 'DecodedFrame.rgba'.
|
|
|
|
return !memcmp(rgba1, rgba2, stride * height);
|
|
|
|
}
|
|
|
|
|
|
|
|
static WEBP_INLINE int PixelsAreSimilar(uint32_t src, uint32_t dst,
|
|
|
|
int max_allowed_diff) {
|
|
|
|
const int src_a = (src >> 24) & 0xff;
|
|
|
|
const int src_r = (src >> 16) & 0xff;
|
|
|
|
const int src_g = (src >> 8) & 0xff;
|
|
|
|
const int src_b = (src >> 0) & 0xff;
|
|
|
|
const int dst_a = (dst >> 24) & 0xff;
|
|
|
|
const int dst_r = (dst >> 16) & 0xff;
|
|
|
|
const int dst_g = (dst >> 8) & 0xff;
|
|
|
|
const int dst_b = (dst >> 0) & 0xff;
|
|
|
|
|
|
|
|
return (abs(src_r * src_a - dst_r * dst_a) <= (max_allowed_diff * 255)) &&
|
|
|
|
(abs(src_g * src_a - dst_g * dst_a) <= (max_allowed_diff * 255)) &&
|
|
|
|
(abs(src_b * src_a - dst_b * dst_a) <= (max_allowed_diff * 255)) &&
|
|
|
|
(abs(src_a - dst_a) <= max_allowed_diff);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int FramesAreSimilar(const uint8_t* const rgba1,
|
|
|
|
const uint8_t* const rgba2,
|
|
|
|
int width, int height, int max_allowed_diff) {
|
|
|
|
int i, j;
|
|
|
|
assert(max_allowed_diff > 0);
|
|
|
|
for (j = 0; j < height; ++j) {
|
|
|
|
for (i = 0; i < width; ++i) {
|
|
|
|
const int stride = width * 4;
|
|
|
|
const size_t offset = j * stride + i;
|
|
|
|
if (!PixelsAreSimilar(rgba1[offset], rgba2[offset], max_allowed_diff)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Minimize number of frames by combining successive frames that have at max
|
|
|
|
// 'max_diff' difference per channel between corresponding pixels.
|
|
|
|
static void MinimizeAnimationFrames(AnimatedImage* const img, int max_diff) {
|
2015-11-06 07:47:03 +01:00
|
|
|
uint32_t i;
|
|
|
|
for (i = 1; i < img->num_frames; ++i) {
|
2015-10-01 20:14:49 +02:00
|
|
|
DecodedFrame* const frame1 = &img->frames[i - 1];
|
|
|
|
DecodedFrame* const frame2 = &img->frames[i];
|
2015-11-06 07:47:03 +01:00
|
|
|
const uint8_t* const rgba1 = frame1->rgba;
|
|
|
|
const uint8_t* const rgba2 = frame2->rgba;
|
2016-04-04 20:43:58 +02:00
|
|
|
int should_merge_frames = 0;
|
2015-10-01 20:14:49 +02:00
|
|
|
// If merging frames will result in integer overflow for 'duration',
|
|
|
|
// skip merging.
|
|
|
|
if (AdditionWillOverflow(frame1->duration, frame2->duration)) continue;
|
2016-04-04 20:43:58 +02:00
|
|
|
if (max_diff > 0) {
|
|
|
|
should_merge_frames = FramesAreSimilar(rgba1, rgba2, img->canvas_width,
|
|
|
|
img->canvas_height, max_diff);
|
|
|
|
} else {
|
|
|
|
should_merge_frames =
|
|
|
|
FramesAreEqual(rgba1, rgba2, img->canvas_width, img->canvas_height);
|
|
|
|
}
|
|
|
|
if (should_merge_frames) { // Merge 'i+1'th frame into 'i'th frame.
|
2015-10-01 20:14:49 +02:00
|
|
|
frame1->duration += frame2->duration;
|
2015-11-06 07:47:03 +01:00
|
|
|
if (i + 1 < img->num_frames) {
|
|
|
|
memmove(&img->frames[i], &img->frames[i + 1],
|
|
|
|
(img->num_frames - i - 1) * sizeof(*img->frames));
|
|
|
|
}
|
|
|
|
--img->num_frames;
|
2015-10-01 20:14:49 +02:00
|
|
|
--i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 07:47:03 +01:00
|
|
|
static int CompareValues(uint32_t a, uint32_t b, const char* output_str) {
|
2015-05-02 01:11:49 +02:00
|
|
|
if (a != b) {
|
2015-11-06 07:47:03 +01:00
|
|
|
fprintf(stderr, "%s: %d vs %d\n", output_str, a, b);
|
|
|
|
return 0;
|
2015-05-02 01:11:49 +02:00
|
|
|
}
|
2015-11-06 07:47:03 +01:00
|
|
|
return 1;
|
2015-05-02 01:11:49 +02:00
|
|
|
}
|
|
|
|
|
2016-09-02 00:11:44 +02:00
|
|
|
static int CompareBackgroundColor(uint32_t bg1, uint32_t bg2, int premultiply) {
|
|
|
|
if (premultiply) {
|
|
|
|
const int alpha1 = (bg1 >> 24) & 0xff;
|
|
|
|
const int alpha2 = (bg2 >> 24) & 0xff;
|
|
|
|
if (alpha1 == 0 && alpha2 == 0) return 1;
|
|
|
|
}
|
|
|
|
if (bg1 != bg2) {
|
|
|
|
fprintf(stderr, "Background color mismatch: 0x%08x vs 0x%08x\n",
|
|
|
|
bg1, bg2);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-02 01:11:49 +02:00
|
|
|
// Note: As long as frame durations and reconstructed frames are identical, it
|
|
|
|
// is OK for other aspects like offsets, dispose/blend method to vary.
|
2015-11-06 07:47:03 +01:00
|
|
|
static int CompareAnimatedImagePair(const AnimatedImage* const img1,
|
|
|
|
const AnimatedImage* const img2,
|
|
|
|
int premultiply,
|
|
|
|
double min_psnr) {
|
|
|
|
int ok = 1;
|
|
|
|
const int is_multi_frame_image = (img1->num_frames > 1);
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
ok = CompareValues(img1->canvas_width, img2->canvas_width,
|
2015-05-02 01:11:49 +02:00
|
|
|
"Canvas width mismatch") && ok;
|
2015-11-06 07:47:03 +01:00
|
|
|
ok = CompareValues(img1->canvas_height, img2->canvas_height,
|
2015-05-02 01:11:49 +02:00
|
|
|
"Canvas height mismatch") && ok;
|
2015-11-06 07:47:03 +01:00
|
|
|
ok = CompareValues(img1->num_frames, img2->num_frames,
|
2015-05-02 01:11:49 +02:00
|
|
|
"Frame count mismatch") && ok;
|
2015-11-06 07:47:03 +01:00
|
|
|
if (!ok) return 0; // These are fatal failures, can't proceed.
|
2015-05-02 01:11:49 +02:00
|
|
|
|
|
|
|
if (is_multi_frame_image) { // Checks relevant for multi-frame images only.
|
2015-11-06 07:47:03 +01:00
|
|
|
ok = CompareValues(img1->loop_count, img2->loop_count,
|
2015-05-02 01:11:49 +02:00
|
|
|
"Loop count mismatch") && ok;
|
2016-09-02 00:11:44 +02:00
|
|
|
ok = CompareBackgroundColor(img1->bgcolor, img2->bgcolor,
|
|
|
|
premultiply) && ok;
|
2015-05-02 01:11:49 +02:00
|
|
|
}
|
|
|
|
|
2015-11-06 07:47:03 +01:00
|
|
|
for (i = 0; i < img1->num_frames; ++i) {
|
2015-05-02 01:11:49 +02:00
|
|
|
// Pixel-by-pixel comparison.
|
2015-11-06 07:47:03 +01:00
|
|
|
const uint8_t* const rgba1 = img1->frames[i].rgba;
|
|
|
|
const uint8_t* const rgba2 = img2->frames[i].rgba;
|
2015-05-02 01:11:49 +02:00
|
|
|
int max_diff;
|
|
|
|
double psnr;
|
2015-11-06 07:47:03 +01:00
|
|
|
if (is_multi_frame_image) { // Check relevant for multi-frame images only.
|
|
|
|
const char format[] = "Frame #%d, duration mismatch";
|
|
|
|
char tmp[sizeof(format) + 8];
|
|
|
|
ok = ok && (snprintf(tmp, sizeof(tmp), format, i) >= 0);
|
|
|
|
ok = ok && CompareValues(img1->frames[i].duration,
|
|
|
|
img2->frames[i].duration, tmp);
|
|
|
|
}
|
|
|
|
GetDiffAndPSNR(rgba1, rgba2, img1->canvas_width, img1->canvas_height,
|
2015-07-08 00:45:49 +02:00
|
|
|
premultiply, &max_diff, &psnr);
|
2015-05-02 01:11:49 +02:00
|
|
|
if (min_psnr > 0.) {
|
|
|
|
if (psnr < min_psnr) {
|
2015-11-06 07:47:03 +01:00
|
|
|
fprintf(stderr, "Frame #%d, psnr = %.2lf (min_psnr = %f)\n", i,
|
2015-05-02 01:11:49 +02:00
|
|
|
psnr, min_psnr);
|
2015-11-06 07:47:03 +01:00
|
|
|
ok = 0;
|
2015-05-02 01:11:49 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (max_diff != 0) {
|
2015-11-06 07:47:03 +01:00
|
|
|
fprintf(stderr, "Frame #%d, max pixel diff: %d\n", i, max_diff);
|
|
|
|
ok = 0;
|
2015-05-02 01:11:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ok;
|
|
|
|
}
|
|
|
|
|
2015-11-06 07:47:03 +01:00
|
|
|
static void Help(void) {
|
2015-12-17 03:08:01 +01:00
|
|
|
printf("Usage: anim_diff <image1> <image2> [options]\n");
|
|
|
|
printf("\nOptions:\n");
|
|
|
|
printf(" -dump_frames <folder> dump decoded frames in PAM format\n");
|
|
|
|
printf(" -min_psnr <float> ... minimum per-frame PSNR\n");
|
|
|
|
printf(" -raw_comparison ..... if this flag is not used, RGB is\n");
|
|
|
|
printf(" premultiplied before comparison\n");
|
2018-03-27 06:37:29 +02:00
|
|
|
printf(" -max_diff <int> ..... maximum allowed difference per channel\n"
|
|
|
|
" between corresponding pixels in subsequent\n"
|
2016-04-04 20:43:58 +02:00
|
|
|
" frames\n");
|
2015-05-02 01:11:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, const char* argv[]) {
|
2015-11-06 07:47:03 +01:00
|
|
|
int return_code = -1;
|
|
|
|
int dump_frames = 0;
|
2015-05-02 01:11:49 +02:00
|
|
|
const char* dump_folder = NULL;
|
|
|
|
double min_psnr = 0.;
|
2015-11-06 07:47:03 +01:00
|
|
|
int got_input1 = 0;
|
|
|
|
int got_input2 = 0;
|
|
|
|
int premultiply = 1;
|
2016-04-04 20:43:58 +02:00
|
|
|
int max_diff = 0;
|
2015-11-06 07:47:03 +01:00
|
|
|
int i, c;
|
|
|
|
const char* files[2] = { NULL, NULL };
|
|
|
|
AnimatedImage images[2];
|
2015-05-02 01:11:49 +02:00
|
|
|
|
|
|
|
if (argc < 3) {
|
|
|
|
Help();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2015-11-06 07:47:03 +01:00
|
|
|
for (c = 1; c < argc; ++c) {
|
|
|
|
int parse_error = 0;
|
2015-05-02 01:11:49 +02:00
|
|
|
if (!strcmp(argv[c], "-dump_frames")) {
|
|
|
|
if (c < argc - 1) {
|
2015-11-06 07:47:03 +01:00
|
|
|
dump_frames = 1;
|
2015-05-02 01:11:49 +02:00
|
|
|
dump_folder = argv[++c];
|
|
|
|
} else {
|
2015-11-06 07:47:03 +01:00
|
|
|
parse_error = 1;
|
2015-05-02 01:11:49 +02:00
|
|
|
}
|
|
|
|
} else if (!strcmp(argv[c], "-min_psnr")) {
|
|
|
|
if (c < argc - 1) {
|
|
|
|
const char* const v = argv[++c];
|
|
|
|
char* end = NULL;
|
|
|
|
const double d = strtod(v, &end);
|
|
|
|
if (end == v) {
|
2015-11-06 07:47:03 +01:00
|
|
|
parse_error = 1;
|
2015-05-02 01:11:49 +02:00
|
|
|
fprintf(stderr, "Error! '%s' is not a floating point number.\n", v);
|
|
|
|
}
|
|
|
|
min_psnr = d;
|
|
|
|
} else {
|
2015-11-06 07:47:03 +01:00
|
|
|
parse_error = 1;
|
2015-05-02 01:11:49 +02:00
|
|
|
}
|
2015-07-08 00:45:49 +02:00
|
|
|
} else if (!strcmp(argv[c], "-raw_comparison")) {
|
2015-11-06 07:47:03 +01:00
|
|
|
premultiply = 0;
|
2016-04-04 20:43:58 +02:00
|
|
|
} else if (!strcmp(argv[c], "-max_diff")) {
|
|
|
|
if (c < argc - 1) {
|
|
|
|
const char* const v = argv[++c];
|
|
|
|
char* end = NULL;
|
|
|
|
const int n = (int)strtol(v, &end, 10);
|
|
|
|
if (end == v) {
|
|
|
|
parse_error = 1;
|
|
|
|
fprintf(stderr, "Error! '%s' is not an integer.\n", v);
|
|
|
|
}
|
|
|
|
max_diff = n;
|
|
|
|
} else {
|
|
|
|
parse_error = 1;
|
|
|
|
}
|
2015-05-02 01:11:49 +02:00
|
|
|
} else {
|
|
|
|
if (!got_input1) {
|
|
|
|
files[0] = argv[c];
|
2015-11-06 07:47:03 +01:00
|
|
|
got_input1 = 1;
|
2015-05-02 01:11:49 +02:00
|
|
|
} else if (!got_input2) {
|
|
|
|
files[1] = argv[c];
|
2015-11-06 07:47:03 +01:00
|
|
|
got_input2 = 1;
|
2015-05-02 01:11:49 +02:00
|
|
|
} else {
|
2015-11-06 07:47:03 +01:00
|
|
|
parse_error = 1;
|
2015-05-02 01:11:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (parse_error) {
|
|
|
|
Help();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!got_input2) {
|
|
|
|
Help();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dump_frames) {
|
|
|
|
printf("Dumping decoded frames in: %s\n", dump_folder);
|
|
|
|
}
|
|
|
|
|
2015-11-06 07:47:03 +01:00
|
|
|
memset(images, 0, sizeof(images));
|
|
|
|
for (i = 0; i < 2; ++i) {
|
2015-05-02 01:11:49 +02:00
|
|
|
printf("Decoding file: %s\n", files[i]);
|
|
|
|
if (!ReadAnimatedImage(files[i], &images[i], dump_frames, dump_folder)) {
|
|
|
|
fprintf(stderr, "Error decoding file: %s\n Aborting.\n", files[i]);
|
2015-11-06 07:47:03 +01:00
|
|
|
return_code = -2;
|
|
|
|
goto End;
|
|
|
|
} else {
|
2016-04-04 20:43:58 +02:00
|
|
|
MinimizeAnimationFrames(&images[i], max_diff);
|
2015-05-02 01:11:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-06 07:47:03 +01:00
|
|
|
if (!CompareAnimatedImagePair(&images[0], &images[1],
|
2015-07-08 00:45:49 +02:00
|
|
|
premultiply, min_psnr)) {
|
2015-05-02 01:11:49 +02:00
|
|
|
fprintf(stderr, "\nFiles %s and %s differ.\n", files[0], files[1]);
|
2015-11-06 07:47:03 +01:00
|
|
|
return_code = -3;
|
|
|
|
} else {
|
|
|
|
printf("\nFiles %s and %s are identical.\n", files[0], files[1]);
|
|
|
|
return_code = 0;
|
2015-05-02 01:11:49 +02:00
|
|
|
}
|
2015-11-06 07:47:03 +01:00
|
|
|
End:
|
|
|
|
ClearAnimatedImage(&images[0]);
|
|
|
|
ClearAnimatedImage(&images[1]);
|
|
|
|
return return_code;
|
2015-05-02 01:11:49 +02:00
|
|
|
}
|