aboutsummaryrefslogtreecommitdiff
path: root/image_diff.pl
blob: 9199f618cdead192544ce6c2b072e23db6f15aa4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/perl -w

# given 2 images of the same size, create a 3rd image, with the pixel
# at each (x,y) transparent if it's the same RGB color in both images,
# or set to the color in the 2nd image if they're different.

# the 2nd image should be an edited version of the 1st image, and the
# output image can be displayed overlaid on the 1st image to show what
# the 2nd image looked like.

use Image::Magick;

sub die_usage {
	die "usage: $0 <input-image1> <input-image2> <output-image>]\n";
}

sub readimage {
	my $file = shift;
	my $i = new Image::Magick;
	my $r = $i->Read($file);
	die "$r\n" if $r;
	warn "read $file, OK\n";
	return $i;
}

die_usage() if @ARGV != 3;
$a = readimage(shift);
$b = readimage(shift);

$w = $a->Get('width');
$h = $a->Get('height');

if($w != $b->Get('width') || $h != $b->Get('height')) {
	die "input images are not the same pixel size\n";
}

$out = Image::Magick->new(size => $w . 'x' . $h);
$r = $out->ReadImage('xc:transparent');
die "$r\n" if $r;

# this is rather slow...
for($y = 0; $y < $h; $y++) {
	for($x = 0; $x < $w; $x++) {
		my @ap = $a->GetPixel(map => "RGB", x => $x, y => $y);
		my @bp = $b->GetPixel(map => "RGB", x => $x, y => $y);
		if($ap[0] != $bp[0] || $ap[1] != $bp[1] || $ap[2] != $bp[2]) {
			push @bp, 65535; # fully opaque
		} else {
			@bp = (0, 0, 0, 0); # transparent (and black)
		}
		$r = $out->SetPixel(map => "RGBA", x => $x, y => $y, color => \@bp);
		die "$r\n" if $r;
	}
}

$r = $out->Write($ARGV[0]);
die "$r\n" if $r;
warn "wrote to $ARGV[0]\n";