#!/bin/csh
# j
# jiffy display of images (in .fld and related formats) using xv
# requires Aspire "op" program.
# for more options, use "op show" instead.

alias rm /bin/rm	# play unsafe since only temporary files removed

set op = "op -chat 0"
set geom = "-geometry +0+0"	# fix: ir7 annoyance
#set geom = ""

# make colors work properly when displaying from pnyd to ir7
set color = "-stdcmap -24"

# all xv options
set opts = "$geom $color"

if ($#argv < 1) then
	echo "Use: j [--red -b val] file"
	echo "Use: j -s files [-mN]	(stack of files, magnified by N)"
	echo "Use: j -a [0] [-nrow n] files (layout array)"
	echo "Use: j file 0	(for zero negatives)"
	echo "Use: j file plane0 plane1 [0]"
	echo "Use: j file -f|p frame|plane [0]"
	echo "Use --red to show negatives in red"
	echo "Use '-b val' to add a border"
	exit -1
endif

set tmpdir = .
if !(-w .) then
	echo 'warn: using /tmp since . not writable'
	set tmpdir = /tmp
endif
set root = $tmpdir


set nrow = 0	# default to autosquare for montage

set pgm = pgm
if ("$1" == "--red") then
	set pgm = ppm
	set argv = ($argv[2-])
endif

set border = 0
if ("$1" == "-b") then
	set border_value = $2
	set border = 1
	set argv = ($argv[3-])
endif

if ($#argv > 1 && "$1" == "-a") then
	set tfile = $tmpdir/.tmp,$$.fld
	set zerneg = ""
	if ("$2" == "0") then
		set zerneg = 0
		set argv = ($argv[2-])
	endif
	if ("$2" == "-nrow") then
		set nrow = $3
		set argv = ($argv[3-])
	endif
	$op stack $tfile float $argv[2-]
	if ($pgm == pgm) then
		(j -nrow $nrow $tfile $zerneg; if (-e $tfile) rm $tfile) &
	else
		(j --red -nrow $nrow $tfile; if (-e $tfile) rm $tfile) &
	endif
	exit
endif

if ($#argv > 1 && "$1" == "-s") then
	set ii = 0
	set root = $tmpdir/.tmp,$$
	set mag = 1
	set zero = 0
	foreach file ($argv[2-])
		@ ii++
		if ("$file" == "0") then
			set zero = 1
			@ ii--
			continue
		endif
		if ("$file" == "-m2") then
			set mag = 2
			@ ii--
			continue
		endif
		if ("$file" == "-m3") then
			set mag = 3
			@ ii--
			continue
		endif
		if ("$file" == "-m4") then
			set mag = 4
			@ ii--
			continue
		endif
		if ("$file" == "-m8") then
			set mag = 8
			@ ii--
			continue
		endif
		echo $ii $mag $zero $file
		if ($mag > 1) then
			$op rep $root,$ii.fld $file 1 $mag $mag
			set file = $root,$ii.fld
		endif
		set tpgm = $root,$ii.$pgm
		if ($zero) then
			set tfile = $tmpdir/.t.$$.fld
			$op vol2mat $tfile $file $nrow
			$op conv $tpgm $tfile byte - 1 1 float
			rm -f $tfile
		else
			$op vol2mat $tpgm $file $nrow
		endif
		if ($mag > 1)	rm $file
	end
	(xv $opts $root,*.$pgm; if (-e $root,1.$pgm) rm $root,*.???) &
	exit
endif

if ("$1" == "-nrow") then
	set nrow = $2
	set argv = ($argv[3-])
endif

set file = $1

if !(-e $file) then
	echo "Error: $file not found"
	exit -1
endif

set tpgm = $root/tmp-$$.$pgm
set tfld = $root/tmp-$$.fld
set t2	= $root/tmp-$$-2.fld

if ($#argv == 1) then
	if $border then
		$op border $tfld $file 0 1 0 1 $border_value
		$op vol2mat $tpgm $tfld $nrow
		rm $tfld
	else
		$op vol2mat $tpgm $file $nrow
	endif
else if ($#argv == 2) then
	$op vol2mat $tfld $file $nrow
	$op conv $tpgm $tfld byte - 1
	rm $tfld
else if ($#argv == 3) then
	if ("$2" == "-f") then
		$op slice $tfld $file float 0 -1 $3 $3
	else if ("$2" == "-p") then
		$op slice $tfld $file float $3 $3 0 -1
	else
		$op slice $tfld $file float $2 $3
	endif
	if $border then
		echo y | $op border $tfld $tfld 0 1 0 1 $border_value
	endif
	$op vol2mat $tpgm $tfld $nrow
	rm $tfld
else if ($#argv == 4) then
	if ("$2" == "-f") then
		$op slice $tfld $file float 0 -1 $3 $3
	else if ("$2" == "-p") then
		$op slice $tfld $file float $3 $3 0 -1
	else
		$op slice $tfld $file float $2 $3
	endif
	$op vol2mat $t2 $tfld $nrow
	$op conv $tpgm $t2 byte - 1
	rm $tfld $t2
endif

if !(-e $tpgm) then
	echo "Error: conversion failed, no file $tpgm"
	exit -1
endif

(xv $opts $tpgm; if ((-e $tpgm) && ($status == 0)) rm $tpgm) &

exit

# OLD WAY
if "$2" == "-p" then
	set plane = $3
	$op slice $tfld $file float $plane $plane 0 -1
	(xtrim disp $tfld | xwud; rm $tfld) &
	exit 0
endif
