orig/phisteq.csh
author Axel Jacobs <axel@jaloxa.eu>
Mon, 14 Apr 2014 22:21:17 +0100
changeset 73 87fa3132a136
parent 0 0aa115157c9c
permissions -rw-r--r--
New IES test file with spaces in file name
axel@0
     1
#!/bin/csh -f
axel@0
     2
# RCSid: $Id: phisteq.csh,v 3.4 2008/11/10 19:08:19 greg Exp $
axel@0
     3
set Ldmin=1		# minimum display luminance
axel@0
     4
set Ldmax=100		# maximum display luminance
axel@0
     5
set nsteps=100		# number of steps in perceptual histogram
axel@0
     6
set cvratio=0.08	# fraction of pixels to ignore in envelope clipping
axel@0
     7
set td=/tmp
axel@0
     8
set tf1=$td/hist$$
axel@0
     9
set tf1b=$td/hist$$.new
axel@0
    10
set tf2=$td/cumt$$
axel@0
    11
set tf3=$td/histeq$$.cal
axel@0
    12
set tf4=$td/cf$$.cal
axel@0
    13
set tf=($tf1 $tf1b $tf2 $tf3 $tf4)
axel@0
    14
if ( $#argv != 1 ) then
axel@0
    15
	echo "Usage: $0 input.hdr > output.hdr"
axel@0
    16
	exit 1
axel@0
    17
endif
axel@0
    18
set ifile=$1
axel@0
    19
set ibase=$ifile:t
axel@0
    20
if ( "$ibase" =~ *.pic ) set ibase=$ibase:r
axel@0
    21
set ibase=$ibase:t
axel@0
    22
onintr quit
axel@0
    23
cat > $tf3 << _EOF_
axel@0
    24
WE : 179;			{ Radiance white luminous efficacy }
axel@0
    25
Lmin : .0001;			{ minimum allowed luminance }
axel@0
    26
Ldmin : $Ldmin ;		{ minimum output luminance }
axel@0
    27
Ldmax : $Ldmax ;		{ maximum output luminance }
axel@0
    28
Stepsiz : 1/ $nsteps ;		{ brightness step size }
axel@0
    29
			{ Daly local amplitude nonlinearity formulae }
axel@0
    30
sq(x) : x*x;
axel@0
    31
c1 : 12.6;
axel@0
    32
b : .63;
axel@0
    33
Bl(L) : L / (L + (c1*L)^b);
axel@0
    34
Lb(B) : (c1^b*B/(1-B))^(1/(1-b));
axel@0
    35
BLw(Lw) : Bl(Ldmin) + (Bl(Ldmax)-Bl(Ldmin))*cf(Bl(Lw));
axel@0
    36
			{ first derivative functions }
axel@0
    37
Bl1(L) : (c1*L)^b*(1-b)/sq(L + (c1*L)^b);
axel@0
    38
Lb1(B) : c1^b/(1-b)/sq(1-B) * (c1^b*B/(1-B))^(b/(1-b));
axel@0
    39
			{ derivative clamping function }
axel@0
    40
clamp2(L, aLw) : Lb(aLw) / L / Lb1(aLw) / (Bl(Ldmax)-Bl(Ldmin)) / Bl1(L);
axel@0
    41
clamp(L) : clamp2(L, BLw(L));
axel@0
    42
			{ histogram equalization function }
axel@0
    43
lin = li(1);
axel@0
    44
Lw = WE/le(1) * lin;
axel@0
    45
Lout = Lb(BLw(Lw));
axel@0
    46
mult = if(Lw-Lmin, (Lout-Ldmin)/(Ldmax-Ldmin)/lin, 0) ;
axel@0
    47
ro = mult * ri(1);
axel@0
    48
go = mult * gi(1);
axel@0
    49
bo = mult * bi(1);
axel@0
    50
_EOF_
axel@0
    51
# Compute brightness histogram
axel@0
    52
pfilt -1 -p 1 -x 128 -y 128 $ifile | pvalue -o -b -d -h -H \
axel@0
    53
	| rcalc -f $tf3 -e 'Lw=WE*$1;$1=if(Lw-Lmin,Bl(Lw),-1)' \
axel@0
    54
	| histo 0 1 $nsteps | sed '/[ 	]0$/d' > $tf1
axel@0
    55
# Clamp frequency distribution
axel@0
    56
set totcount=`sed 's/^.*[ 	]//' $tf1 | total`
axel@0
    57
set tst=1
axel@0
    58
while ( $totcount > 0 )
axel@0
    59
	sed 's/^.*[ 	]//' $tf1 | total -1 -r \
axel@0
    60
		| rcalc -e '$1=$1/'$totcount | rlam $tf1 - \
axel@0
    61
		| tabfunc -i 0 cf > $tf4
axel@0
    62
	if ( $tst <= 0 ) break
axel@0
    63
	rcalc -f $tf4 -f $tf3 -e "T:$totcount*Stepsiz" \
axel@0
    64
			-e 'clfq=floor(T*clamp(Lb($1)))' \
axel@0
    65
			-e '$1=$1;$2=if($2-clfq,clfq,$2)' $tf1 > $tf1b
axel@0
    66
	set newtot=`sed 's/^.*[ 	]//' $tf1b | total`
axel@0
    67
	set tst=`ev "floor((1-$cvratio)*$totcount)-$newtot"`
axel@0
    68
	mv -f $tf1b $tf1
axel@0
    69
	set totcount=$newtot
axel@0
    70
end
axel@0
    71
if ( $totcount < 1 ) then
axel@0
    72
	# Fits in display range nicely already -- just normalize
axel@0
    73
	pfilt -1 -e `pextrem $ifile | rcalc -e 'cond=recno-1.5;$1=1/(.265*$3+.67*$4+.065*$5)'` $ifile
axel@0
    74
else
axel@0
    75
	# Plot the mapping function if we are in debug mode
axel@0
    76
	if ( $?DEBUG ) then
axel@0
    77
		cat > ${ibase}_histo.plt << _EOF_
axel@0
    78
include=curve.plt
axel@0
    79
title="Brightness Frequency Distribution"
axel@0
    80
subtitle= $ibase
axel@0
    81
ymin=0
axel@0
    82
xlabel="Perceptual Brightness B(Lw)"
axel@0
    83
ylabel="Frequency Count"
axel@0
    84
Alabel="Histogram"
axel@0
    85
Alintype=0
axel@0
    86
Blabel="Envelope"
axel@0
    87
Bsymsize=0
axel@0
    88
Adata=
axel@0
    89
_EOF_
axel@0
    90
		(cat $tf1; echo \;; echo Bdata=) >> ${ibase}_histo.plt
axel@0
    91
		rcalc -f $tf4 -f $tf3 -e "T:$totcount*Stepsiz" \
axel@0
    92
			-e '$1=$1;$2=T*clamp(Lb($1))' $tf1 \
axel@0
    93
			>> ${ibase}_histo.plt
axel@0
    94
		cat > ${ibase}_brmap.plt << _EOF_
axel@0
    95
include=line.plt
axel@0
    96
title="Brightness Mapping Function"
axel@0
    97
subtitle= $ibase
axel@0
    98
xlabel="World Luminance (log cd/m^2)"
axel@0
    99
ylabel="Display Luminance (cd/m^2)"
axel@0
   100
ymax= $Ldmax
axel@0
   101
Adata=
axel@0
   102
_EOF_
axel@0
   103
		cnt 100 | rcalc -f $tf4 -f $tf3 -e '$1=lx;$2=Lb(BLw(10^lx))' \
axel@0
   104
			-e Lmin:Lb\(`sed -n '1s/[ 	].*$//p' $tf1`\) \
axel@0
   105
			-e Lmax:Lb\(`sed -n '$s/[ 	].*$//p' $tf1`\) \
axel@0
   106
			-e 'lx=$1/99*(log10(Lmax)-log10(Lmin))+log10(Lmin)' \
axel@0
   107
			>> ${ibase}_brmap.plt
axel@0
   108
		if ( $?DISPLAY ) then
axel@0
   109
			bgraph ${ibase}_histo.plt ${ibase}_brmap.plt | x11meta &
axel@0
   110
		endif
axel@0
   111
	endif
axel@0
   112
	# Map our picture
axel@0
   113
	pcomb -f $tf4 -f $tf3 $ifile
axel@0
   114
endif
axel@0
   115
quit:
axel@0
   116
rm -f $tf