Skip to content

Instantly share code, notes, and snippets.

@HalCanary
Created March 13, 2017 18:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save HalCanary/f484227fedb5821bf4140f06fe43ecce to your computer and use it in GitHub Desktop.
Save HalCanary/f484227fedb5821bf4140f06fe43ecce to your computer and use it in GitHub Desktop.
/*
* Copyright 2017 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
/*
## build.ninja ##
PNGD = /home/halcanary/src/skia/third_party/libpng
cflags = -I$PNGD
ldflags = -lz -lm
rule cc
command = cc -c $cflags $in -o $out
rule ar
command = ar rs $out $in
rule ln
command = cc $in $ldflags -o $out
build png.o: cc $PNGD/png.c
build pngerror.o: cc $PNGD/pngerror.c
build pngget.o: cc $PNGD/pngget.c
build pngmem.o: cc $PNGD/pngmem.c
build pngpread.o: cc $PNGD/pngpread.c
build pngread.o: cc $PNGD/pngread.c
build pngrio.o: cc $PNGD/pngrio.c
build pngrtran.o: cc $PNGD/pngrtran.c
build pngrutil.o: cc $PNGD/pngrutil.c
build pngset.o: cc $PNGD/pngset.c
build pngtrans.o: cc $PNGD/pngtrans.c
build pngwio.o: cc $PNGD/pngwio.c
build pngwrite.o: cc $PNGD/pngwrite.c
build pngwtran.o: cc $PNGD/pngwtran.c
build pngwutil.o: cc $PNGD/pngwutil.c
build imgdiff.o: cc imgdiff.c
build png.a: ar png.o pngerror.o pngget.o pngmem.o pngpread.o $
pngread.o pngrio.o pngrtran.o pngrutil.o pngset.o pngtrans.o $
pngwio.o pngwrite.o pngwtran.o pngwutil.o
build imgdiff: ln imgdiff.o png.a
default imgdiff
*/
#include <stdlib.h>
#include <string.h>
#include <png.h>
int main(int argc, char** argv) {
size_t i, N;
size_t s[2];
unsigned char* img[2];
int64_t totalDiffs;
if (argc != 3) {
return 1;
}
for (i = 0; i < 2; ++i) {
png_image image;
memset(&image, 0, (sizeof image));
image.version = PNG_IMAGE_VERSION;
if (png_image_begin_read_from_file(&image, argv[1 + i]) == 0) {
abort();
}
image.format = PNG_FORMAT_RGBA;
image.flags |= PNG_TRANSFORM_STRIP_16;
s[i] = PNG_IMAGE_SIZE(image);
if (s[i] != image.width * image.height * 4) { abort(); }
img[i] = (unsigned char*)malloc(PNG_IMAGE_SIZE(image));
if (png_image_finish_read(&image, NULL, img[i], 0, NULL) == 0) {
abort();
}
}
N = s[0] < s[1] ? s[0] : s[1];
totalDiffs = 0;
for (i = 0; i < N; ++i) {
totalDiffs += abs((int)img[0][i] - (int)img[0][i]);
}
printf("%g\n", (double)totalDiffs / (255 * N));
free(img[0]);
free(img[1]);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment