bin/pdelta.pl
author Axel Jacobs <axel@jaloxa.eu>
Mon, 14 Apr 2014 22:20:45 +0100
changeset 72 9516e69e0be3
parent 8 9ed02f081b72
permissions -rwxr-xr-x
Update man page for ltview

#!/usr/bin/perl
#
# Compute 1976 CIE Lab deltaE* between two Radiance pictures
#
# This is a re-write of Greg's pdelta.csh script

use strict;
use warnings;
use File::Temp qw/ tempdir /;

if($#ARGV != 1) {
	print "Usage: $0 pic1.hdr pic2.hdr > output.hdr";
	exit(1);
}

my $cielab = 'sq(x):x*x;';
$cielab .= 'Ls(Yi):if(Yi/Yw-.01,116*(Yi/Yw)^(1/3)-16,903.3*Yi/Yw);';
$cielab .= 'as(Xi,Yi,Zi):500*((Xi/Xw)^(1/3)-(Yi/Yw)^(1/3));';
$cielab .= 'bs(Xi,Yi,Zi):200*((Yi/Yw)^(1/3)-(Zi/Zw)^(1/3));';
$cielab .= 'dE(X1,Y1,Z1,X2,Y2,Z2):sqrt(sq(Ls(Y1)-Ls(Y2))+sq(as(X1,Y1,Z1)';
$cielab .= '-as(X2,Y2,Z2))+sq(bs(X1,Y1,Z1)-bs(X2,Y2,Z2)))';

# The following is for Radiance RGB -> XYZ
my $rgb2xyz = 'X(R,G,B):92.03*R+57.98*G+28.99*B;';
$rgb2xyz .= 'Y(R,G,B):47.45*R+119.9*G+11.6*B;';
$rgb2xyz .= 'Z(R,G,B):4.31*R+21.99*G+152.7*B';

# The following is for sRGB -> XYZ
my $f1 = "$ARGV[0]";
my $inp1 = 'x1=ri(1);y1=gi(1);z1=bi(1)';
my $f2 = "$ARGV[1]";
my $inp2 = 'x2=ri(2);y2=gi(2);z2=bi(2)';

# Make sure both images are in XYZE format
`getinfo < $f1 | grep '^FORMAT=32-bit_rle_xyze'`;
# Exit status: 0 if no error (string is found -> image is in XYZE format)
if( $? != 0 ) {
	$inp1 = 'x1=X(ri(1),gi(1),bi(1));';
	$inp1 .= 'y1=Y(ri(1),gi(1),bi(1));';
	$inp1 .= 'z1=Z(ri(1),gi(1),bi(1))';
}

`getinfo < $f2 | grep '^FORMAT=32-bit_rle_xyze'`;
if( $? != 0 ) {
	$inp2 = 'x2=X(ri(2),gi(2),bi(2));';
	$inp2 .= 'y2=Y(ri(2),gi(2),bi(2));';
	$inp2 .= 'z2=Z(ri(2),gi(2),bi(2))';
}

my $td = tempdir( CLEANUP => 1 );
my $tempf = "$td/tf.hdr";

system "pfilt -1 -x 128 -y 128 -p 1 $f1 | pvalue -o -h -H -d > $tempf";
my @wht = split(/\s+/, `total -u $tempf`);
my $avg = `rcalc -e '\$1=\$2' $tempf | total -m`;
chomp $avg;

my $cmd = "pcomb -e '$cielab' -e '$rgb2xyz' ";
$cmd .= "-e 'Yw:179*3*$avg; Xw:$wht[0]*Yw/$wht[1]; Zw:$wht[2]*Yw/$wht[1]' ";
$cmd .= "-e '$inp1' -e '$inp2' -e 'lo=dE(x1,y1,z1,x2,y2,z2)' ";
$cmd .= "-o $f1 -o $f2";

system $cmd;

#EOF