#!/bin/bash
#
# Developed by Fred Weinhaus 1/2/2008 .......... revised 4/25/2015
#
# ------------------------------------------------------------------------------
# 
# Licensing:
# 
# Copyright © Fred Weinhaus
# 
# My scripts are available free of charge for non-commercial use, ONLY.
# 
# For use of my scripts in commercial (for-profit) environments or 
# non-free applications, please contact me (Fred Weinhaus) for 
# licensing arrangements. My email address is fmw at alink dot net.
# 
# If you: 1) redistribute, 2) incorporate any of these scripts into other 
# free applications or 3) reprogram them in another scripting language, 
# then you must contact me for permission, especially if the result might 
# be used in a commercial or for-profit environment.
# 
# My scripts are also subject, in a subordinate manner, to the ImageMagick 
# license, which can be found at: http://www.imagemagick.org/script/license.php
# 
# ------------------------------------------------------------------------------
# 
####
#
# USAGE: colortemp [-t temperatures] [-f fov] [infile] outfile
# USAGE: colortemp [-h or -help]
#
# OPTIONS:
#
# -t     temperture(s)      t1[,t2]; 1000 to 40000 deg K 
#                           t1=desired temperature; t2=current temperature;
#                           t2 default=pure white (near 6500)
# -f     fov				fov=field of view for observation;
#                           fov=2 or 10 deg; default=2
# infile outfile            if no infile and no temperature provided, then 
#                           outfile will be a color vs temperature chart;
#                           if no infile but t1 provided, then outfile 
#                           will be a color swatch for the provided temperture;
#                           if infile and t1[,t2] provided, then infile will 
#                           be color transformed to generate the outfile 
#                           according to the temperture(s) provided 
#
###
#
# NAME: COLORTEMP 
# 
# PURPOSE: To transform an image's overall color balance according to a desired 
# color temperature change.
# 
# DESCRIPTION: COLORTEMP transforms an image's overall color balance according 
# to a desired color temperature change. If no infile and no temperature is 
# provided, the script will generate a color vs temperature chart. If no infile 
# is provided, but a desired temperature is specified, then the script will 
# generate a color swatch for the specified temperature.
# 
# 
# OPTIONS: 
# 
# -t temperature(s) ... TEMPERATURE(S) specifies the desired (t1) temperature 
# and the current (t2) temperature. Values range from 1000 to 40,000 deg K. 
# The default for t2 is pure white which is near 6500 deg K.
# 
# -f fov ... FOV is the observer's field of view. Allowed values are fov=2 or 10 
# deg. The default is 2. Differences in color temperature are slight between 
# the two.
# 
# infile outfile ... If no infile and no temperature are provided, then 
# outfile will be a color vs temperature chart. If no infile but t1 is
# provided, then outfile will be a color swatch for the provided
# temperture. If infile and t1[,t2] provided, then infile will be color
# transformed to generate the outfile according to temperture(s) provided.
# 
# Requires NetPBM (http://netpbm.sourceforge.net/)
#
# CAVEAT: No guarantee that this script will work on all platforms, 
# nor that trapping of inconsistent parameters is complete and 
# foolproof. Use At Your Own Risk. 
# 
######
#

# set default values
temp=""
t1=""
t2=""
fov=2
infile=""
outfile=""

# set directory for temporary files
dir="."    # suggestions are dir="." or dir="/tmp"

# set up functions to report Usage and Usage with Description
PROGNAME=`type $0 | awk '{print $3}'`  # search for executable on path
PROGDIR=`dirname $PROGNAME`            # extract directory of program
PROGNAME=`basename $PROGNAME`          # base name of program
usage1() 
	{
	echo >&2 ""
	echo >&2 "$PROGNAME:" "$@"
	sed >&2 -e '1,/^####/d;  /^###/g;  /^#/!q;  s/^#//;  s/^ //;  4,$p' "$PROGDIR/$PROGNAME"
	}
usage2() 
	{
	echo >&2 ""
	echo >&2 "$PROGNAME:" "$@"
	sed >&2 -e '1,/^####/d;  /^######/g;  /^#/!q;  s/^#*//;  s/^ //;  4,$p' "$PROGDIR/$PROGNAME"
	}


# function to report error messages
errMsg()
	{
	echo ""
	echo $1
	echo ""
	usage1
	exit 1
	}


# function to test for minus at start of value of second part of option 1 or 2
checkMinus()
	{
	test=`echo "$1" | grep -c '^-.*$'`   # returns 1 if match; 0 otherwise
    [ $test -eq 1 ] && errMsg "$errorMsg"
	}

# test for correct number of arguments and get values
if [ $# -eq 0 ]
	then
	# help information
   echo ""
   usage2
   exit 0
elif [ $# -gt 6 ]
	then
	errMsg "--- TOO MANY ARGUMENTS WERE PROVIDED ---"
else
	while [ $# -gt 0 ]
		do
			# get parameter values
			case "$1" in
		  -h|-help)    # help information
					   echo ""
					   usage2
					   exit 0
					   ;;
				-t)    # get temperatures
					   shift  # to get the next parameter - temperatures
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID TEMPERATURE SPECIFICATION ---"
					   checkMinus "$1"
					   temp="$1,"
					   ;;
				-f)    # get fov
					   shift  # to get the next parameter - fov
					   # test if parameter starts with minus sign 
					   errorMsg="--- INVALID FOV SPECIFICATION ---"
					   checkMinus "$1"
					   fov="$1"
					   [ "$fov" != "2" -a "$fov" != "10" ] && errMsg "--- FOV MUST BE EITHER 2 OR 10 ---"
					   ;;
				 -)    # STDIN and end of arguments
					   break
					   ;;
				-*)    # any other - argument
					   errMsg "--- UNKNOWN OPTION ---"
					   ;;
				*)     # end of arguments
					   break
					   ;;
			esac
			shift   # next option
	done
	#
	# get infile and outfile
	file1="$1"
	file2="$2"
fi

# setup temporary images and auto delete upon exit
# use mpc/cache to hold input image temporarily in memory
tmpA="$dir/colortemp_$$.mpc"
tmpB="$dir/colortemp_$$.cache"
tmp0="$dir/colortemp_0_$$.png"
tmp1="$dir/colortemp_1_$$.png"
tmp2="$dir/colortemp_2_$$.png"
tmp3="$dir/colortemp_3_$$.png"
trap "rm -f $tmpA $tmpB $tmp0 $tmp1 $tmp2 $tmp3;" 0
trap "rm -f $tmpA $tmpB $tmp0 $tmp1 $tmp2 $tmp3; exit 1" 1 2 3 15
trap "rm -f $tmpA $tmpB $tmp0 $tmp1 $tmp2 $tmp3; exit 1" ERR

if [ "$file1" != "" -a "$file2" = "" ]
	then
	outfile="$file1"
elif [ "$file1" != "" -a "$file2" != "" ]
	then
	infile="$file1"
	outfile="$file2"
	if convert -quiet "$infile" +repage "$tmpA"
		then
		: 'do nothing - continue processing below'
		else
			errMsg "--- FILE $infile DOES NOT EXIST OR IS NOT AN ORDINARY FILE, NOT READABLE OR HAS ZERO SIZE ---"
	fi
else
	errMsg "--- NO OUTFILE WAS PROVIDED ---"
fi

if [ "$temp" != "" ]
	then
	t1=`echo "$temp" | cut -d, -f1`
	t2=`echo "$temp" | cut -d, -f2`
	
	[ $t1 -lt 1000 -o $t1 -gt 40000 ] && errMsg "--- TEMPERATURE T1 IS NOT VALID ---"
	if [ "$t2" != "" ]
		then
		[ $t2 -lt 1000 -o $t2 -gt 40000 ] && errMsg "--- TEMPERATURE T2 IS NOT VALID ---"
	fi
fi


# 2 deg color matching function temperature vs color table
# see http://www.vendian.org/mncharity/dir3/blackbody/UnstableURLs/bbr_color.html
# see http://www.vendian.org/mncharity/dir3/blackbody/
# temperature,red,green,blue

table2="1000,255,51,0
1100,255,69,0
1200,255,82,0
1300,255,93,0
1400,255,102,0
1500,255,111,0
1600,255,118,0
1700,255,124,0
1800,255,130,0
1900,255,135,0
2000,255,141,11
2100,255,146,29
2200,255,152,41
2300,255,157,51
2400,255,162,60
2500,255,166,69
2600,255,170,77
2700,255,174,84
2800,255,178,91
2900,255,182,98
3000,255,185,105
3100,255,189,111
3200,255,192,118
3300,255,195,124
3400,255,198,130
3500,255,201,135
3600,255,203,141
3700,255,206,146
3800,255,208,151
3900,255,211,156
4000,255,213,161
4100,255,215,166
4200,255,217,171
4300,255,219,175
4400,255,221,180
4500,255,223,184
4600,255,225,188
4700,255,226,192
4800,255,228,196
4900,255,229,200
5000,255,231,204
5100,255,232,208
5200,255,234,211
5300,255,235,215
5400,255,237,218
5500,255,238,222
5600,255,239,225
5700,255,240,228
5800,255,241,231
5900,255,243,234
6000,255,244,237
6100,255,245,240
6200,255,246,243
6300,255,247,245
6400,255,248,248
6500,255,249,251
6600,255,249,253
6700,254,250,255
6800,252,248,255
6900,250,247,255
7000,247,245,255
7100,245,244,255
7200,243,243,255
7300,241,241,255
7400,239,240,255
7500,238,239,255
7600,236,238,255
7700,234,237,255
7800,233,236,255
7900,231,234,255
8000,229,233,255
8100,228,233,255
8200,227,232,255
8300,225,231,255
8400,224,230,255
8500,223,229,255
8600,221,228,255
8700,220,227,255
8800,219,226,255
8900,218,226,255
9000,217,225,255
9100,216,224,255
9200,215,223,255
9300,214,223,255
9400,213,222,255
9500,212,221,255
9600,211,221,255
9700,210,220,255
9800,209,220,255
9900,208,219,255
10000,207,218,255
10100,207,218,255
10200,206,217,255
10300,205,217,255
10400,204,216,255
10500,204,216,255
10600,203,215,255
10700,202,215,255
10800,202,214,255
10900,201,214,255
11000,200,213,255
11100,200,213,255
11200,199,212,255
11300,198,212,255
11400,198,212,255
11500,197,211,255
11600,197,211,255
11700,196,210,255
11800,196,210,255
11900,195,210,255
12000,195,209,255
12100,194,209,255
12200,194,208,255
12300,193,208,255
12400,193,208,255
12500,192,207,255
12600,192,207,255
12700,191,207,255
12800,191,206,255
12900,190,206,255
13000,190,206,255
13100,190,206,255
13200,189,205,255
13300,189,205,255
13400,188,205,255
13500,188,204,255
13600,188,204,255
13700,187,204,255
13800,187,204,255
13900,187,203,255
14000,186,203,255
14100,186,203,255
14200,186,203,255
14300,185,202,255
14400,185,202,255
14500,185,202,255
14600,184,202,255
14700,184,201,255
14800,184,201,255
14900,184,201,255
15000,183,201,255
15100,183,201,255
15200,183,200,255
15300,182,200,255
15400,182,200,255
15500,182,200,255
15600,182,200,255
15700,181,199,255
15800,181,199,255
15900,181,199,255
16000,181,199,255
16100,180,199,255
16200,180,198,255
16300,180,198,255
16400,180,198,255
16500,179,198,255
16600,179,198,255
16700,179,198,255
16800,179,197,255
16900,179,197,255
17000,178,197,255
17100,178,197,255
17200,178,197,255
17300,178,197,255
17400,178,196,255
17500,177,196,255
17600,177,196,255
17700,177,196,255
17800,177,196,255
17900,177,196,255
18000,176,196,255
18100,176,195,255
18200,176,195,255
18300,176,195,255
18400,176,195,255
18500,176,195,255
18600,175,195,255
18700,175,195,255
18800,175,194,255
18900,175,194,255
19000,175,194,255
19100,175,194,255
19200,174,194,255
19300,174,194,255
19400,174,194,255
19500,174,194,255
19600,174,194,255
19700,174,193,255
19800,174,193,255
19900,173,193,255
20000,173,193,255
20100,173,193,255
20200,173,193,255
20300,173,193,255
20400,173,193,255
20500,173,193,255
20600,173,192,255
20700,172,192,255
20800,172,192,255
20900,172,192,255
21000,172,192,255
21100,172,192,255
21200,172,192,255
21300,172,192,255
21400,172,192,255
21500,171,192,255
21600,171,192,255
21700,171,191,255
21800,171,191,255
21900,171,191,255
22000,171,191,255
22100,171,191,255
22200,171,191,255
22300,171,191,255
22400,170,191,255
22500,170,191,255
22600,170,191,255
22700,170,191,255
22800,170,190,255
22900,170,190,255
23000,170,190,255
23100,170,190,255
23200,170,190,255
23300,170,190,255
23400,169,190,255
23500,169,190,255
23600,169,190,255
23700,169,190,255
23800,169,190,255
23900,169,190,255
24000,169,190,255
24100,169,190,255
24200,169,189,255
24300,169,189,255
24400,169,189,255
24500,168,189,255
24600,168,189,255
24700,168,189,255
24800,168,189,255
24900,168,189,255
25000,168,189,255
25100,168,189,255
25200,168,189,255
25300,168,189,255
25400,168,189,255
25500,168,189,255
25600,168,189,255
25700,167,188,255
25800,167,188,255
25900,167,188,255
26000,167,188,255
26100,167,188,255
26200,167,188,255
26300,167,188,255
26400,167,188,255
26500,167,188,255
26600,167,188,255
26700,167,188,255
26800,167,188,255
26900,167,188,255
27000,167,188,255
27100,166,188,255
27200,166,188,255
27300,166,188,255
27400,166,187,255
27500,166,187,255
27600,166,187,255
27700,166,187,255
27800,166,187,255
27900,166,187,255
28000,166,187,255
28100,166,187,255
28200,166,187,255
28300,166,187,255
28400,166,187,255
28500,166,187,255
28600,166,187,255
28700,165,187,255
28800,165,187,255
28900,165,187,255
29000,165,187,255
29100,165,187,255
29200,165,187,255
29300,165,187,255
29400,165,187,255
29500,165,186,255
29600,165,186,255
29700,165,186,255
29800,165,186,255
29900,165,186,255
30000,165,186,255
30100,165,186,255
30200,165,186,255
30300,165,186,255
30400,165,186,255
30500,165,186,255
30600,164,186,255
30700,164,186,255
30800,164,186,255
30900,164,186,255
31000,164,186,255
31100,164,186,255
31200,164,186,255
31300,164,186,255
31400,164,186,255
31500,164,186,255
31600,164,186,255
31700,164,186,255
31800,164,186,255
31900,164,186,255
32000,164,185,255
32100,164,185,255
32200,164,185,255
32300,164,185,255
32400,164,185,255
32500,164,185,255
32600,164,185,255
32700,163,185,255
32800,163,185,255
32900,163,185,255
33000,163,185,255
33100,163,185,255
33200,163,185,255
33300,163,185,255
33400,163,185,255
33500,163,185,255
33600,163,185,255
33700,163,185,255
33800,163,185,255
33900,163,185,255
34000,163,185,255
34100,163,185,255
34200,163,185,255
34300,163,185,255
34400,163,185,255
34500,163,185,255
34600,163,185,255
34700,163,185,255
34800,163,185,255
34900,163,185,255
35000,163,184,255
35100,163,184,255
35200,162,184,255
35300,162,184,255
35400,162,184,255
35500,162,184,255
35600,162,184,255
35700,162,184,255
35800,162,184,255
35900,162,184,255
36000,162,184,255
36100,162,184,255
36200,162,184,255
36300,162,184,255
36400,162,184,255
36500,162,184,255
36600,162,184,255
36700,162,184,255
36800,162,184,255
36900,162,184,255
37000,162,184,255
37100,162,184,255
37200,162,184,255
37300,162,184,255
37400,162,184,255
37500,162,184,255
37600,162,184,255
37700,162,184,255
37800,162,184,255
37900,162,184,255
38000,162,184,255
38100,162,184,255
38200,162,184,255
38300,161,184,255
38400,161,184,255
38500,161,184,255
38600,161,183,255
38700,161,183,255
38800,161,183,255
38900,161,183,255
39000,161,183,255
39100,161,183,255
39200,161,183,255
39300,161,183,255
39400,161,183,255
39500,161,183,255
39600,161,183,255
39700,161,183,255
39800,161,183,255
39900,161,183,255
40000,161,183,255"

table10="1000,255,56,0
1100,255,71,0
1200,255,83,0
1300,255,93,0
1400,255,101,0
1500,255,109,0
1600,255,115,0
1700,255,121,0
1800,255,126,0
1900,255,131,0
2000,255,137,18
2100,255,142,33
2200,255,147,44
2300,255,152,54
2400,255,157,63
2500,255,161,72
2600,255,165,79
2700,255,169,87
2800,255,173,94
2900,255,177,101
3000,255,180,107
3100,255,184,114
3200,255,187,120
3300,255,190,126
3400,255,193,132
3500,255,196,137
3600,255,199,143
3700,255,201,148
3800,255,204,153
3900,255,206,159
4000,255,209,163
4100,255,211,168
4200,255,213,173
4300,255,215,177
4400,255,217,182
4500,255,219,186
4600,255,221,190
4700,255,223,194
4800,255,225,198
4900,255,227,202
5000,255,228,206
5100,255,230,210
5200,255,232,213
5300,255,233,217
5400,255,235,220
5500,255,236,224
5600,255,238,227
5700,255,239,230
5800,255,240,233
5900,255,242,236
6000,255,243,239
6100,255,244,242
6200,255,245,245
6300,255,246,248
6400,255,248,251
6500,255,249,253
6600,254,249,255
6700,252,247,255
6800,249,246,255
6900,247,245,255
7000,245,243,255
7100,243,242,255
7200,240,241,255
7300,239,240,255
7400,237,239,255
7500,235,238,255
7600,233,237,255
7700,231,236,255
7800,230,235,255
7900,228,234,255
8000,227,233,255
8100,225,232,255
8200,224,231,255
8300,222,230,255
8400,221,230,255
8500,220,229,255
8600,218,228,255
8700,217,227,255
8800,216,227,255
8900,215,226,255
9000,214,225,255
9100,212,225,255
9200,211,224,255
9300,210,223,255
9400,209,223,255
9500,208,222,255
9600,207,221,255
9700,207,221,255
9800,206,220,255
9900,205,220,255
10000,204,219,255
10100,203,219,255
10200,202,218,255
10300,201,218,255
10400,201,217,255
10500,200,217,255
10600,199,216,255
10700,199,216,255
10800,198,216,255
10900,197,215,255
11000,196,215,255
11100,196,214,255
11200,195,214,255
11300,195,214,255
11400,194,213,255
11500,193,213,255
11600,193,212,255
11700,192,212,255
11800,192,212,255
11900,191,211,255
12000,191,211,255
12100,190,211,255
12200,190,210,255
12300,189,210,255
12400,189,210,255
12500,188,210,255
12600,188,209,255
12700,187,209,255
12800,187,209,255
12900,186,208,255
13000,186,208,255
13100,185,208,255
13200,185,208,255
13300,185,207,255
13400,184,207,255
13500,184,207,255
13600,183,207,255
13700,183,206,255
13800,183,206,255
13900,182,206,255
14000,182,206,255
14100,182,205,255
14200,181,205,255
14300,181,205,255
14400,181,205,255
14500,180,205,255
14600,180,204,255
14700,180,204,255
14800,179,204,255
14900,179,204,255
15000,179,204,255
15100,178,203,255
15200,178,203,255
15300,178,203,255
15400,178,203,255
15500,177,203,255
15600,177,202,255
15700,177,202,255
15800,177,202,255
15900,176,202,255
16000,176,202,255
16100,176,202,255
16200,175,201,255
16300,175,201,255
16400,175,201,255
16500,175,201,255
16600,175,201,255
16700,174,201,255
16800,174,201,255
16900,174,200,255
17000,174,200,255
17100,173,200,255
17200,173,200,255
17300,173,200,255
17400,173,200,255
17500,173,200,255
17600,172,199,255
17700,172,199,255
17800,172,199,255
17900,172,199,255
18000,172,199,255
18100,171,199,255
18200,171,199,255
18300,171,199,255
18400,171,198,255
18500,171,198,255
18600,170,198,255
18700,170,198,255
18800,170,198,255
18900,170,198,255
19000,170,198,255
19100,170,198,255
19200,169,198,255
19300,169,197,255
19400,169,197,255
19500,169,197,255
19600,169,197,255
19700,169,197,255
19800,169,197,255
19900,168,197,255
20000,168,197,255
20100,168,197,255
20200,168,197,255
20300,168,196,255
20400,168,196,255
20500,168,196,255
20600,167,196,255
20700,167,196,255
20800,167,196,255
20900,167,196,255
21000,167,196,255
21100,167,196,255
21200,167,196,255
21300,166,196,255
21400,166,195,255
21500,166,195,255
21600,166,195,255
21700,166,195,255
21800,166,195,255
21900,166,195,255
22000,166,195,255
22100,165,195,255
22200,165,195,255
22300,165,195,255
22400,165,195,255
22500,165,195,255
22600,165,195,255
22700,165,194,255
22800,165,194,255
22900,165,194,255
23000,164,194,255
23100,164,194,255
23200,164,194,255
23300,164,194,255
23400,164,194,255
23500,164,194,255
23600,164,194,255
23700,164,194,255
23800,164,194,255
23900,164,194,255
24000,163,194,255
24100,163,194,255
24200,163,193,255
24300,163,193,255
24400,163,193,255
24500,163,193,255
24600,163,193,255
24700,163,193,255
24800,163,193,255
24900,163,193,255
25000,163,193,255
25100,162,193,255
25200,162,193,255
25300,162,193,255
25400,162,193,255
25500,162,193,255
25600,162,193,255
25700,162,193,255
25800,162,193,255
25900,162,192,255
26000,162,192,255
26100,162,192,255
26200,162,192,255
26300,162,192,255
26400,161,192,255
26500,161,192,255
26600,161,192,255
26700,161,192,255
26800,161,192,255
26900,161,192,255
27000,161,192,255
27100,161,192,255
27200,161,192,255
27300,161,192,255
27400,161,192,255
27500,161,192,255
27600,161,192,255
27700,161,192,255
27800,160,192,255
27900,160,192,255
28000,160,191,255
28100,160,191,255
28200,160,191,255
28300,160,191,255
28400,160,191,255
28500,160,191,255
28600,160,191,255
28700,160,191,255
28800,160,191,255
28900,160,191,255
29000,160,191,255
29100,160,191,255
29200,160,191,255
29300,159,191,255
29400,159,191,255
29500,159,191,255
29600,159,191,255
29700,159,191,255
29800,159,191,255
29900,159,191,255
30000,159,191,255
30100,159,191,255
30200,159,191,255
30300,159,191,255
30400,159,190,255
30500,159,190,255
30600,159,190,255
30700,159,190,255
30800,159,190,255
30900,159,190,255
31000,159,190,255
31100,158,190,255
31200,158,190,255
31300,158,190,255
31400,158,190,255
31500,158,190,255
31600,158,190,255
31700,158,190,255
31800,158,190,255
31900,158,190,255
32000,158,190,255
32100,158,190,255
32200,158,190,255
32300,158,190,255
32400,158,190,255
32500,158,190,255
32600,158,190,255
32700,158,190,255
32800,158,190,255
32900,158,190,255
33000,158,190,255
33100,158,190,255
33200,157,190,255
33300,157,190,255
33400,157,189,255
33500,157,189,255
33600,157,189,255
33700,157,189,255
33800,157,189,255
33900,157,189,255
34000,157,189,255
34100,157,189,255
34200,157,189,255
34300,157,189,255
34400,157,189,255
34500,157,189,255
34600,157,189,255
34700,157,189,255
34800,157,189,255
34900,157,189,255
35000,157,189,255
35100,157,189,255
35200,157,189,255
35300,157,189,255
35400,157,189,255
35500,157,189,255
35600,156,189,255
35700,156,189,255
35800,156,189,255
35900,156,189,255
36000,156,189,255
36100,156,189,255
36200,156,189,255
36300,156,189,255
36400,156,189,255
36500,156,189,255
36600,156,189,255
36700,156,189,255
36800,156,189,255
36900,156,189,255
37000,156,189,255
37100,156,189,255
37200,156,188,255
37300,156,188,255
37400,156,188,255
37500,156,188,255
37600,156,188,255
37700,156,188,255
37800,156,188,255
37900,156,188,255
38000,156,188,255
38100,156,188,255
38200,156,188,255
38300,156,188,255
38400,155,188,255
38500,155,188,255
38600,155,188,255
38700,155,188,255
38800,155,188,255
38900,155,188,255
39000,155,188,255
39100,155,188,255
39200,155,188,255
39300,155,188,255
39400,155,188,255
39500,155,188,255
39600,155,188,255
39700,155,188,255
39800,155,188,255
39900,155,188,255
40000,155,188,255"

tempinc=100

if [ $fov -eq 10 ]
	then
	table="$table10"
else
	table="$table2"
fi

# convert table to arrays
tempArr=(`echo "$table" | cut -d, -f1`)
redArr=(`echo "$table" | cut -d, -f2`)
greenArr=(`echo "$table" | cut -d, -f3`)
blueArr=(`echo "$table" | cut -d, -f4`)


# test array sizes
tempnum=${#tempArr[*]}
rednum=${#redArr[*]}
greennum=${#redArr[*]}
bluenum=${#redArr[*]}
if [ $tempnum -eq $rednum -a $tempnum -eq $greennum -a $tempnum -eq $bluenum ]
	then
	num=$tempnum
	numm1=`expr $num - 1`
	firsttemp=${tempArr[0]}
	lasttemp=${tempArr[$numm1]}
else
	errMsg "--- RED, GREEN AND BLUE COUNTS ARE UNEQUAL ---"
fi


# generate color vs temperature chart
if [ "$infile" = "" -a "$t1" = "" ]
	then
	# now convert arrays into images
	# Use NetPBM (PGM format implied intermediate image)
#	echo "P2 391 1 65535 ${tempArr[*]}" | convert - -scale 391x50! $tmp0
	echo "P2 391 1 255 ${redArr[*]}" | convert - -scale 391x50! $tmp1
	echo "P2 391 1 255 ${greenArr[*]}" | convert - -scale 391x50! $tmp2
	echo "P2 391 1 255 ${blueArr[*]}" | convert - -scale 391x50! $tmp3

	convert $tmp1 $tmp2 $tmp3 -combine $tmp0
	
	convert \( $tmp0[120x50+0+0] -resize 480x50! \) $tmp0[1x50+120+0] +append $tmp1
	convert \( -size 481x10 xc:gray \) -stroke white \
		-draw "line 0,0 0,7 line 40,0 40,7 line 80,0 80,7 line 120,0 120,7 line 160,0 160,7 line 200,0 200,7 line 240,0 240,7 line 280,0 280,7 line 320,0 320,7 line 360,0 360,7 line 400,0 400,7 line 440,0 440,7 line 480,0 480,7 " $tmp2
	convert $tmp1 $tmp2 -append -bordercolor gray -border 30 -bordercolor white -border 2 $tmp3
	convert $tmp3 -stroke white -font Arial -pointsize 10 \
		-draw "text 20,100 '1000' text 60,100 '2000' text 100,100 '3000' text 140,100 '4000' text 180,100 '5000' text 220,100 '6000' text 260,100 '7000' text 300,100 '8000' text 340,100 '9000' text 380,100 '10000' text 420,100 '11000' text 460,100 '12000' text 500,100 '13000'" \
		-stroke white -font Arial -pointsize 14 \
		-draw "text 180,20 'Color vs Temperture (deg K)'" "$outfile"
fi


# function to interpolate color from table
interpolateColor()
	{
	temp=$1
	# get integer and fractional temperature position
	int=`echo "scale=0; $num * ($temp - $firsttemp) / ($lasttemp - $firsttemp)" | bc`
	
	# interpolate colors
	if [ $temp -eq $lasttemp ]
		then
		red=${redArr[$numm1]}
		green=${greenArr[$numm1]}
		blue=${blueArr[$numm1]}
	else
		frac=`echo "scale=2; ($temp - ($tempinc * $int + $firsttemp)) / $tempinc" | bc`
		int1=`expr $int + 1`
		red=`echo "scale=0; ($frac * (${redArr[$int1]} - ${redArr[$int]}) + ${redArr[$int]}) / 1" | bc`
		green=`echo "scale=0; ($frac * (${greenArr[$int1]} - ${greenArr[$int]}) + ${greenArr[$int]}) / 1" | bc`
		blue=`echo "scale=0; ($frac * (${blueArr[$int1]} - ${blueArr[$int]}) + ${blueArr[$int]}) / 1" | bc`
	fi
	}


# generate color swatch for given temperature
if [ "$infile" = "" -a "$t1" != "" ]
	then
	interpolateColor $t1
	convert -size 100x100 xc:"rgb($red,$green,$blue)" \
		-bordercolor gray -border 30 -bordercolor white -border 2 \
		-stroke white -font Arial -pointsize 12 \
		-draw "text 50,150 '$temp deg K'" "$outfile"
fi

# get im version
im_version=`convert -list configure | \
	sed '/^LIB_VERSION_NUMBER */!d;  s//,/;  s/,/,0/g;  s/,0*\([0-9][0-9]\)/\1/g' | head -n 1`

# set up -recolor or -color-matrix
if [ "$im_version" -lt "06060100" ]; then
	process="-recolor"
else
	process="-color-matrix"
fi

# color transform input image
if [ "$infile" != "" -a "$outfile" != "" -a "$t1" != "" ]
	then
	interpolateColor $t1
	dred=$red
	dgreen=$green
	dblue=$blue

	if [ "$t2" = "" ]
		then
		cred=255
		cgreen=255
		cblue=255
	else
		interpolateColor $t2
		cred=$red
		cgreen=$green
		cblue=$blue
	fi
	
	# compute color ratio and trap for divide by zero
	if [ $cred -eq 0 ]
		then 
		redfrac=$dred
	else 
		redfrac=`echo "scale=3; $dred / $cred" | bc`
	fi
	if [ $cgreen -eq 0 ]
		then 
		greenfrac=$dgreen
	else 
		greenfrac=`echo "scale=3; $dgreen / $cgreen" | bc`
	fi
	if [ $cblue -eq 0 ]
		then 
		bluefrac=$dblue
	else 
		bluefrac=`echo "scale=3; $dblue / $cblue" | bc`
	fi

	convert $tmpA $process "$redfrac 0 0 0 $greenfrac 0 0 0 $bluefrac" "$outfile"
fi
exit 0


