bin/ran2tiff.pl
author Axel Jacobs <axel@jaloxa.eu>
Mon, 27 Jun 2011 20:37:51 +0100
changeset 19 fd9e518890bc
parent 8 9ed02f081b72
permissions -rwxr-xr-x
"Added test for objpict."
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
     1
#!/usr/bin/perl
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
     2
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
     3
# Convert Radiance animation frames to TIFF output
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
     4
#
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
     5
# This is a re-write of Greg's ran2tiff.csh
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
     6
#
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
     7
# Axel, Oct 2010
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
     8
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
     9
use strict;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    10
use warnings;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    11
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    12
use File::Basename;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    13
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    14
my $histosiz = 200;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    15
my $histof = '';      # Histogram file (-H)
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    16
my $fwt = 0.9;        # Frame weight (-W)
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    17
my $outdir = '';      # Output directory for TIFFs (-D)
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    18
my @pcopts = ();      # Options passed through to pcond
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    19
my @tfopts = ();      # Options passed through to ra_tiff
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    20
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    21
if ($#ARGV < 0) {
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    22
	@ARGV = ('DUMMY');
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    23
}
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    24
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    25
# Process options for pcond and ra_tiff
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    26
while (@ARGV) {
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    27
	$_ = $ARGV[0];
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    28
	if (m/-W/) {   # Weight of each frame
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    29
		$pfwt = $ARGV[1];
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    30
		shift @ARGV;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    31
	} elsif (m/-H/) {   # Histogram file
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    32
		$histof = "$ARGV[1]";
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    33
		shift @ARGV;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    34
	} elsif (m/-D/) {   # Output directory for TIFFs
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    35
		die("$0: Directory $ARGV[1] does not exist.\n")
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    36
				unless (-d "$ARGV[1]");
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    37
		$outdir = "$argv[1]";
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    38
		shift @ARGV;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    39
	} elsif ((m/-h\w+/) or (m/-a\w+/) or   # human, acuity
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    40
			(m/-v\w+/) or (m/-s\w+/) or    # veiling glare, contrast sensitiviy
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    41
			(m/-c\w+/) or (m/-l\w+/)) {    # mesopic, linear response
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    42
		push(@pcopts, $_);
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    43
	} elsif ((m/-u/) or (m/-d/) or (m/-f/)) {
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    44
		# Lmax of device, dynamic range of device, macbeth.cal file
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    45
		push(@pcopts, $_, $ARGV[1]);
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    46
		shift @ARGV;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    47
	} elsif (m/-p/) {   # pcond -p (primaries)
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    48
		push(@pcopts, '-p');
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    49
		for (my $i=0; $i<6, $i++) {
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    50
			push(@pcopts, $ARGV[1]);
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    51
			shift @ARGV;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    52
		}
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    53
	} elsif ((m/-z/) or (m/-b/) or (m/-w/)) {
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    54
		# LZW-compression, 8-bit greyscale, 16-bit
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    55
		push(@tfopts, $_);
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    56
	} elsif (m/-g/) {
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    57
		push(@tfopts, '-g', $_);   # Gamma value for ra_tiff
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    58
		shift @ARGV;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    59
	} elsif (m/^-/) {
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    60
		die("$0: Bad option: $_\n");
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    61
	} else {
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    62
		last;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    63
	}
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    64
	shift @ARGV;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    65
}
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    66
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    67
die("$0: Need at least two frames.\n") unless ($#ARGV >= 1);
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    68
#echo Usage: "$0 [-W prev_frame_wt][-H histo][-D dir][pcond opts][ra_tiff opts] frame1 frame2 .."
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    69
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    70
my $td = tempdir( CLEANPU => 1 );
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    71
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    72
# Get shrunken image luminances
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    73
foreach (@ARGV) {
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    74
	my ($name, undef, undef) = fileparse($_);
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    75
	my $datf = "$td/$name.dat";
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    76
	my $cmd = "pfilt -1 -x 128 -y 128 -p 1 \"$_\"";
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    77
	$cmd .= " |pvalue -o -h -H -b -df";
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    78
	$cmd .= " |rcalc -if1 -e 'L=\$1*179;cond=L-1e-7;\$1=log10(L)' > $datf";
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    79
	system("$cmd");
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    80
}
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    81
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    82
# Get Min. and Max. log values
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    83
my $Lmin = `cat $td/*.dat | total -l | rcalc -e '\$1=\$1-.01'`;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    84
chomp($Lmin);
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    85
my $Lmax = `cat $td/*.dat | total -u | rcalc -e '\$1=\$1+.01'`;
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    86
chomp($Lmax);
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    87
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    88
if ($histof) {
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    89
	if (-r $histof) then
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    90
		# Fix min/max and translate histogram
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    91
		set Lmin=`sed -n '1p' $histof | rcalc -e 'min(a,b):if(a-b,b,a);$1=min($1,'"$Lmin)"`
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    92
		set Lmax=`sed -n '$p' $histof | rcalc -e 'max(a,b):if(a-b,a,b);$1=max($1,'"$Lmax)"`
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    93
		tabfunc -i hfunc < $histof > $td/oldhist.cal
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    94
		cnt $histosiz \
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    95
			| rcalc -e "L10=$Lmin+($Lmax-$Lmin)/$histosiz"'*($1+.5)' \
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    96
				-f $td/oldhist.cal -e '$1=L10;$2=hfunc(L10)' \
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    97
			> $td/oldhisto.dat
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    98
	endif
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
    99
}
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   100
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   101
foreach ($ARGV) {
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   102
	#inp = $_
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   103
	set datf="$inp:t"
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   104
	set datf="$td/$datf:r.dat"
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   105
	set outp="$inp:t"
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   106
	set outp="$outdir$outp:r.tif"
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   107
	endif
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   108
	histo $Lmin $Lmax $histosiz < $datf > $td/newhisto.dat
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   109
	if (-f $td/oldhisto.dat) then
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   110
		rlam $td/newhisto.dat $td/oldhisto.dat \
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   111
			| rcalc -e '$1=$1;$2=$2+$4*'$pfwt \
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   112
			> $td/histo.dat
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   113
	else
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   114
		mv $td/{new,}histo.dat
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   115
	endif
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   116
	pcond $pcopts -I $inp:q < $td/histo.dat \
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   117
		| ra_tiff $tfopts - $outp:q
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   118
	mv $td/{,old}histo.dat
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   119
}
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   120
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   121
if ($?histof) then
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   122
	cp -f $td/oldhisto.dat $histof
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   123
endif
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   124
9ed02f081b72 Added the rest of the files
Axel Jacobs <axel@jaloxa.eu>
parents:
diff changeset
   125
#EOF