orig/psquish.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: psquish.csh,v 3.5 2008/11/10 19:08:19 greg Exp $
axel@0
     3
set Lmin=.0001		# minimum visible world luminance
axel@0
     4
set Ldmin=1		# minimum display luminance
axel@0
     5
set Ldmax=100		# maximum display luminance
axel@0
     6
set nsteps=100		# number of steps in perceptual histogram
axel@0
     7
set cvratio=0.05	# fraction of pixels to ignore in envelope clipping
axel@0
     8
set td=/tmp
axel@0
     9
set tf0=$td/tf$$
axel@0
    10
set tf1=$td/hist$$
axel@0
    11
set tf1b=$td/hist$$.diff
axel@0
    12
set tf2=$td/cumt$$
axel@0
    13
set tf3=$td/histeq$$.cal
axel@0
    14
set tf4=$td/cf$$.cal
axel@0
    15
set tf=($tf0 $tf1 $tf1b $tf2 $tf3 $tf4)
axel@0
    16
if ( "$argv[1]" == "-a" ) then
axel@0
    17
	set adaptive
axel@0
    18
	shift argv
axel@0
    19
endif
axel@0
    20
if ( $#argv != 1 ) then
axel@0
    21
	echo "Usage: $0 [-a] input.hdr > output.hdr"
axel@0
    22
	exit 1
axel@0
    23
endif
axel@0
    24
set ifile=$1
axel@0
    25
set ibase=$ifile:t
axel@0
    26
if ( "$ibase" =~ *.hdr ) set ibase=$ibase:r
axel@0
    27
set ibase=$ibase:t
axel@0
    28
onintr quit
axel@0
    29
pextrem -o $ifile > $tf0
axel@0
    30
set Lmin=`rcalc -e 'L=179*(.265*$3+.67*$4+.065*$5)' -e 'cond=1.5-recno;$1=if('L-$Lmin,L,$Lmin')' $tf0`
axel@0
    31
set Lmax=`rcalc -e 'L=179*(.265*$3+.67*$4+.065*$5)' -e 'cond=recno-1.5;$1=if('$Ldmax-L,$Ldmax,L')' $tf0`
axel@0
    32
cat > $tf3 << _EOF_
axel@0
    33
min(a,b) : if(a-b, b, a);
axel@0
    34
WE : 179;			{ Radiance white luminous efficacy }
axel@0
    35
Lmin : $Lmin ;			{ minimum visible luminance }
axel@0
    36
Lmax : $Lmax ;			{ maximum picture luminance }
axel@0
    37
Ldmin : $Ldmin ;		{ minimum output luminance }
axel@0
    38
Ldmax : $Ldmax ;		{ maximum output luminance }
axel@0
    39
Stepsiz : (Bl(Lmax)-Bl(Lmin))/ $nsteps ;	{ brightness step size }
axel@0
    40
			{ Logarithmic brightness function }
axel@0
    41
Bl(L) : log(L);
axel@0
    42
Lb(B) : exp(B);
axel@0
    43
BLw(Lw) : Bl(Ldmin) + (Bl(Ldmax)-Bl(Ldmin))*cf(Bl(Lw));
axel@0
    44
			{ first derivative functions }
axel@0
    45
Bl1(L) : 1/L;
axel@0
    46
Lb1(B) : exp(B);
axel@0
    47
			{ histogram equalization function }
axel@0
    48
lin = li(1);
axel@0
    49
Lw = WE/le(1) * lin;
axel@0
    50
Lout = Lb(BLw(Lw));
axel@0
    51
mult = if(Lw-Lmin, (Lout-Ldmin)/(Ldmax-Ldmin)/lin, 0) ;
axel@0
    52
_EOF_
axel@0
    53
if ( $?adaptive ) then
axel@0
    54
	cat >> $tf3 << _EOF_
axel@0
    55
			{ Ferwerda contrast sensitivity function }
axel@0
    56
				{ log10 of cone threshold luminance }
axel@0
    57
ltp(lLa) : if(-2.6 - lLa, -.72, if(lLa - 1.9, lLa - 1.255,
axel@0
    58
		(.249*lLa + .65)^2.7 - .72));
axel@0
    59
				{ log10 of rod threshold luminance }
axel@0
    60
lts(lLa) : if(-3.94 - lLa, -2.86, if(lLa - -1.44, lLa - .395,
axel@0
    61
		(.405*lLa + 1.6)^2.18 - 2.86));
axel@0
    62
				{ threshold is minimum of rods and cones }
axel@0
    63
ldL2(lLa) : min(ltp(lLa),lts(lLa));
axel@0
    64
dL(La) : 10^ldL2(log10(La));
axel@0
    65
			{ derivative clamping function }
axel@0
    66
clamp2(L, bLw) : dL(Lb(bLw))/dL(L)/Lb1(bLw)/(Bl(Ldmax)-Bl(Ldmin))/Bl1(L);
axel@0
    67
clamp(L) : clamp2(L, BLw(L));
axel@0
    68
			{ shift direction for histogram }
axel@0
    69
shiftdir(B) : if(cf(B) - (B - Bl(Lmin))/(Bl(Ldmax) - Bl(Lmin)), 1, -1);
axel@0
    70
			{ Scotopic/Photopic color adjustment }
axel@0
    71
sL(r,g,b) : .062*r + .608*g + .330*b;	{ approx. scotopic brightness }
axel@0
    72
BotMesopic : 10^-2.25;		{ top of scotopic range }
axel@0
    73
TopMesopic : 10^0.75;		{ bottom of photopic range }
axel@0
    74
incolor = if(Lw-TopMesopic, 1, if(BotMesopic-Lw, 0,
axel@0
    75
		(Lw-BotMesopic)/(TopMesopic-BotMesopic)));
axel@0
    76
slf = (1 - incolor)*sL(ri(1),gi(1),bi(1));
axel@0
    77
ro = mult*(incolor*ri(1) + slf);
axel@0
    78
go = mult*(incolor*gi(1) + slf);
axel@0
    79
bo = mult*(incolor*bi(1) + slf);
axel@0
    80
_EOF_
axel@0
    81
else
axel@0
    82
	cat >> $tf3 << _EOF_
axel@0
    83
			{ derivative clamping function }
axel@0
    84
clamp2(L, bLw) : Lb(bLw)/L/Lb1(bLw)/(Bl(Ldmax)-Bl(Ldmin))/Bl1(L);
axel@0
    85
clamp(L) : clamp2(L, BLw(L));
axel@0
    86
			{ shift direction for histogram }
axel@0
    87
shiftdir(B) : -1;
axel@0
    88
ro = mult*ri(1);
axel@0
    89
go = mult*gi(1);
axel@0
    90
bo = mult*bi(1);
axel@0
    91
_EOF_
axel@0
    92
endif
axel@0
    93
# Compute brightness histogram
axel@0
    94
pfilt -1 -p 1 -x 128 -y 128 $ifile | pvalue -o -b -d -h -H \
axel@0
    95
	| rcalc -f $tf3 -e 'Lw=WE*$1;$1=if(Lw-Lmin,Bl(Lw),-1)' \
axel@0
    96
	| histo `ev "log($Lmin)" "log($Lmax)"` $nsteps > $tf1
axel@0
    97
# Clamp frequency distribution
axel@0
    98
set totcount=`sed 's/^.*[ 	]//' $tf1 | total`
axel@0
    99
set margin=`ev "floor($totcount*$cvratio+.5)"`
axel@0
   100
while ( 1 )
axel@0
   101
	# Compute mapping function
axel@0
   102
	sed 's/^.*[ 	]//' $tf1 | total -1 -r \
axel@0
   103
		| rcalc -e '$1=$1/'$totcount | rlam $tf1 - \
axel@0
   104
		| tabfunc -i 0 cf > $tf4
axel@0
   105
	# Compute difference with visible envelope
axel@0
   106
	rcalc -f $tf4 -f $tf3 -e "T:$totcount*Stepsiz" \
axel@0
   107
			-e 'clfq=floor(T*clamp(Lb($1))+.5)' \
axel@0
   108
			-e '$1=$2-clfq;$2=shiftdir($1)' $tf1 > $tf1b
axel@0
   109
	if (`sed 's/[	 ].*$//' $tf1b | total` >= 0) then
axel@0
   110
		# Nothing visible? -- just normalize
axel@0
   111
		pfilt -1 -e `pextrem $ifile | rcalc -e 'cond=recno-1.5;$1=1/(.265*$3+.67*$4+.065*$5)'` $ifile
axel@0
   112
		goto quit
axel@0
   113
	endif
axel@0
   114
	# Check to see if we're close enough
axel@0
   115
	if (`rcalc -e '$1=if($1,$1,0)' $tf1b | total` <= $margin) break
axel@0
   116
	# Squash frequency distribution
axel@0
   117
	set diffs=(`sed 's/[ 	].*$//' $tf1b`)
axel@0
   118
	set shftd=(`sed 's/^.*[ 	]//' $tf1b`)
axel@0
   119
	while ( 1 )
axel@0
   120
		set maxi=0
axel@0
   121
		set maxd=0
axel@0
   122
		set i=$nsteps
axel@0
   123
		while ( $i > 0 )
axel@0
   124
			if ( $diffs[$i] > $maxd ) then
axel@0
   125
				set maxd=$diffs[$i]
axel@0
   126
				set maxi=$i
axel@0
   127
			endif
axel@0
   128
			@ i--
axel@0
   129
		end
axel@0
   130
		if ( $maxd == 0 ) break
axel@0
   131
		set i=0
axel@0
   132
	tryagain:
axel@0
   133
		set r=$shftd[$maxi]
axel@0
   134
		while ( $i == 0 )
axel@0
   135
			@ t= $maxi + $r
axel@0
   136
			if ( $t < 1 || $t > $nsteps ) then
axel@0
   137
				@ shftd[$maxi]= -($shftd[$maxi])
axel@0
   138
				goto tryagain
axel@0
   139
			endif
axel@0
   140
			if ( $diffs[$t] < 0 ) set i=$t
axel@0
   141
			@ r+= $shftd[$maxi]
axel@0
   142
		end
axel@0
   143
		if ( $diffs[$i] <= -$diffs[$maxi] ) then
axel@0
   144
			@ diffs[$i]+= $diffs[$maxi]
axel@0
   145
			set diffs[$maxi]=0
axel@0
   146
		else
axel@0
   147
			@ diffs[$maxi]+= $diffs[$i]
axel@0
   148
			set diffs[$i]=0
axel@0
   149
		endif
axel@0
   150
	end
axel@0
   151
	# Mung histogram
axel@0
   152
	echo $diffs | tr ' ' '\012' | rlam $tf1 - \
axel@0
   153
		| rcalc -f $tf4 -f $tf3 -e "T:$totcount*Stepsiz" \
axel@0
   154
			-e 'clfq=floor(T*clamp(Lb($1))+.5)' \
axel@0
   155
			-e '$1=$1;$2=$3+clfq' > $tf1b
axel@0
   156
	mv -f $tf1b $tf1
axel@0
   157
end
axel@0
   158
# Plot the mapping function if we are in debug mode
axel@0
   159
if ( $?DEBUG ) then
axel@0
   160
	cat > ${ibase}_histo.plt << _EOF_
axel@0
   161
include=curve.plt
axel@0
   162
title="Brightness Frequency Distribution"
axel@0
   163
subtitle= $ibase
axel@0
   164
ymin=0
axel@0
   165
xlabel="Perceptual Brightness B(Lw)"
axel@0
   166
ylabel="Frequency Count"
axel@0
   167
Alabel="Histogram"
axel@0
   168
Alintype=0
axel@0
   169
Blabel="Envelope"
axel@0
   170
Bsymsize=0
axel@0
   171
Adata=
axel@0
   172
_EOF_
axel@0
   173
	(cat $tf1; echo \;; echo Bdata=) >> ${ibase}_histo.plt
axel@0
   174
	rcalc -f $tf4 -f $tf3 -e "T:$totcount*Stepsiz" \
axel@0
   175
		-e '$1=$1;$2=T*clamp(Lb($1))' $tf1 \
axel@0
   176
		>> ${ibase}_histo.plt
axel@0
   177
	cat > ${ibase}_brmap.plt << _EOF_
axel@0
   178
include=line.plt
axel@0
   179
title="Brightness Mapping Function"
axel@0
   180
subtitle= $ibase
axel@0
   181
xlabel="World Luminance (log cd/m^2)"
axel@0
   182
ylabel="Display Luminance (cd/m^2)"
axel@0
   183
ymax= $Ldmax
axel@0
   184
Adata=
axel@0
   185
_EOF_
axel@0
   186
	cnt 100 | rcalc -f $tf4 -f $tf3 -e '$1=lx;$2=Lb(BLw(10^lx))' \
axel@0
   187
		-e 'lx=$1/99*(log10(Lmax)-log10(Lmin))+log10(Lmin)' \
axel@0
   188
		>> ${ibase}_brmap.plt
axel@0
   189
	if ( $?DISPLAY ) then
axel@0
   190
		bgraph ${ibase}_histo.plt ${ibase}_brmap.plt | x11meta &
axel@0
   191
	endif
axel@0
   192
endif
axel@0
   193
# Map our picture
axel@0
   194
getinfo < $ifile | egrep '^((VIEW|PIXASPECT|PRIMARIES)=|[^ ]*(rpict|rview|pinterp) )'
axel@0
   195
pcomb -f $tf4 -f $tf3 $ifile
axel@0
   196
quit:
axel@0
   197
rm -f $tf