#! /bin/csh -f # echo "Moviefy 1.1 Because movies DO make a difference. James Holton 12-20-01" # # Converts x-ray images RAXIS II, RAXIS IV or ADSC Q4 # into a series of Potrable Graymap (pgm) files # or an SGI movie (if on an SGI) # or an animated GIF (if ImageMagik is available) # # machine-specific stuff alias nawk /usr/bin/nawk nawk 'BEGIN{exit}' >& /dev/null if($status) alias nawk awk set uname = `uname` # # Defaults # set outmovie = "moviefied.movie" # SGI output movie set outgif = "moviefied.gif" # GIF animation output file (if ImageMagick's convert is available) set keyfile = moviekey.txt # text file for indexing movie set htmlfile = moviekey.html # html file for indexing movie set tempfile = ./moviefy_temp # prefix for temporary files made by this script set zoom = 0.33 # default zoom factor (smaller value = smaller images) set imagesize = "" # output image size (eg: 100x100) set scale = auto # intensity scale for reducing x-ray data to 8-bit greyscale (usually ~ 0.1 - 1.0) default: auto set center = "" # define area of image to be converted (in pixels) set edge = "" # define square area of image to be converted (in pixels) #set center = "1124 1124" # example middle quarter of Q4 detector #set edge = "1124" # example middle quarter of Q4 detector set box = "" # define arbitrary area of image to be converted (in pixels) set neg = "" # make images white-on-black #set neg = "-invert" # make images black-on-white set LABEL # flag to lable output files (if ImageMagick's mogrify is available) set ANIMATE # flag to animate output files (if ImageMagick's animate is available) set EXT = "" # image file extension (default: auto-determine from first image) set converter = "" # program to convert x-ray images to pgm files with the following interface: # -box x1 y1 x2 y2 -scale n -zoom n (as above) # -invert (to negate) # accesory programs, (if not in path) set adsc2pgm = "adsc2pgm" set osc2pgm = "osc2pgm" set mar2pgm = "mar2pgm" # anyone feel like writing this? set convert = "convert" set mogrify = "mogrify" set animate = "animate" set togif = "mogrify -format gif87" # ################################################################################# goto Setup # scan command line for: # x-ray images # options Help: cat << EOF usage: $0 image???.ext [...] [outfile.movie] [-zoom #] [-scale #| -scale same] or $0 moviekey.txt [outfile.movie] [-zoom #] [-scale #| -scale same] files can be: image???.ext - X-ray images you want to put in a movie (R-axis or Q4) outfile.movie - name of the new SGI movie file moviekey.txt - a text file, listing filenames, and individualized options: i.e.: frame_001.img -center 232 435 -label 0 0 2 on frame_001.img options are: -help - display this message -center x y - center (pixel units) of region of image to convert (default: center of image) -edge - edge size (pixels) of region of image to convert (default: whole image) -zoom # - zoom factor (default: $zoom) -scale # - image intensity scale factor (default: autoscale each image) -scale same - autoscale first image, rest of images on same scale -save - save intermediate pgm/gif files EOF exit 9 Return_from_Setup: echo "" ################################################################################# # predict what format intermediate files should be in set fmt = "pgm" if("$togif" != "") set fmt = "gif" if(($uname !~ IRIX*)&&("$outmovie" != "")) then echo "cannot make an SGI movie on $uname ..." set outmovie = "" endif if(("$convert" == "")&&("$outgif" != "")) then echo "cannot make an animated GIF without ImageMagick convert program" echo "you can get ImageMagick for free from:" echo "netscape http://www.wizards.dupont.com/cristy/ImageMagick.html" echo "" set outgif = "" endif if(("$mogrify" == "")&&("$convert" == "")&&($?LABEL)) then echo "cannot label files without ImageMagick programs" echo "you can get ImageMagick for free from:" echo "netscape http://www.wizards.dupont.com/cristy/ImageMagick.html" echo "" unset LABEL endif if(("$mogrify" != "")&&($?LABEL)) then # check and see if a label is going to go off the edge of the image nawk '$1 !~ /[^0-9]/' $keyfile |\ nawk '{nf=split($2,w,"/"); label=w[nf]}\ / -label /{label=substr($0, index($0, " -label ")+8)}\ {print label}' |\ nawk 'length($0)>maxlen{print; maxlen=length($0)}' |\ tail -1 >! ${tempfile}label # check the size of this label in pixels nawk 'BEGIN{print "P2\n10000 1\n255"; for(i=0;i<10000;++i) print 0}' |\ cat >! ${tempfile}test.pgm $mogrify -box white -draw 'text 10,0 "@'${tempfile}label'"' -crop 0x0 ${tempfile}test.pgm set label_length = `nawk '! /^#/{++n} n==2 && NF==2{print $1;exit} n>4{exit}' ${tempfile}test.pgm` rm -f ${tempfile}test.pgm ${tempfile}label >& /dev/null # auto-zoom for this font? if((! $?USER_zoom)&&("$label_length" != "")&&("$edge" != "")) then set temp = `echo "$label_length $edge $zoom" | nawk '(20+$1)/$2>$3{print (20+$1)/$2}'` if("$temp" != "") then set zoom = "$temp" echo "" echo "auto-zooming" endif endif endif if($?debug) then cat << EOF convert = $convert mogrify = $mogrify animate = $animate adsc2pgm = $adsc2pgm osc2pgm = $osc2pgm mar2pgm = $mar2pgm EOF endif ################################################################################# # introductory message echo "" nawk '$1 !~ /[^0-9]/' $keyfile | wc -l | nawk '{printf "converting %d images.\n", $1}' cat $keyfile |\ nawk '$1 !~ /[^0-9]/{++n;frame=$2}\ n==1{printf "%s ", frame} END{printf "to %s ", frame}' if("$outmovie" != "") echo " -> $outmovie" if(("$outmovie" != "")&&("$outgif" != "")) echo -n " and" if("$outgif" != "") echo " -> $outgif" if(("$outmovie" == "")&&("$outgif" == "")) echo "????.$fmt" echo "" echo "Zoom factor set to: $zoom" if("$box" != "") then set temp = `echo "$box" | nawk '{printf "%.0fx%.0f\n", sqrt(($4-$2)^2), sqrt(($5-$3)^2)}'` echo "getting a $temp pixel box" set imagesize = `echo "$box $zoom" | nawk '{printf "%.0fx%.0f\n", $NF*sqrt(($4-$2)^2), $NF*sqrt(($5-$3)^2)}'` endif # skip ahead if images have already been converted if($?SKIP) goto makemovie if(("$scale" == 0)||("$scale" == "auto")) then echo "Autoscaling intensity of each image." set printscale = "yes" else echo "Image intensity scale set to: $scale" set printscale = "no" endif echo "" makeimages: ################################################################################# echo "
next
", $1;\ printf "prev\n", $1-1}' |\
cat >> ${htmlfile}
@ frame = ( $frame + 1 )
end
unset noglob
echo "" >> ${htmlfile}
makemovie:
#################################################################################
makeBIGgif:
if("$outgif" == "") then
goto makeSGImovie
endif
echo "making animated gif: $outgif"
$convert -delay 50 -loop 0 [0-9][0-9][0-9][0-9].gif $outgif
if((! $status)&&(-e "$outgif")) then
echo "you can view the movie now using:"
echo "netscape $outgif"
else
echo ""
echo "FAILED! "
echo "please make sure $convert is really the ImageMagick convert program."
echo "you should be able to make the animated gif yourself using:"
echo "$convert -delay 20 -loop *.gif $outgif"
echo "Good Luck! "
endif
makeSGImovie:
if("$outmovie" == "") then
# no SGI movie to be made
goto animatefiles
endif
echo "converting *.$fmt -> $outmovie"
echo ""
if("$fmt" == "gif") then
# dmconvert only works with gif87
endif
set start = `nawk '$1 !~ /[^0-9]/{print $1;exit}' $keyfile`
#set start = 0
set end = `tail -1 $keyfile | nawk '{print $1}'`
rm -f $outmovie >& /dev/null
dmconvert -f sgimv -n \#\#\#\#.$fmt,start=$start,end=$end -p video,comp=mvc2 \#\#\#\#.$fmt $outmovie
if((! $status)&&(-e $outmovie)) then
echo "To view the movie, type: movieplayer $outmovie"
echo "To view the image key, type: more $keyfile"
else
# don't erase the image files
set SAVE
cat << EOF
Something went wrong. ... Nuts.
Please make sure that:
1) this SGI has graphics (not an Origin)
2) all the *.$fmt files have the same x,y size
you can still make an SGI movie using:
dmconvert -v -f sgimv -n ####.$fmt,start=$start,end=$end -p video,comp=mvc2 ####.$fmt $outmovie
or
movieconvert
EOF
endif
animatefiles:
if(! $?ANIMATE) goto cleanup
if("$animate" == "") then
echo ""
echo "unable to animate files without ImageMagick"
echo "you can get ImageMagick for free from:"
echo "netscape http://www.wizards.dupont.com/cristy/ImageMagick.html"
echo ""
set SAVE
goto cleanup
endif
echo ""
echo "animating files with ImageMagick:"
echo "animate ????.$fmt"
echo '">" slow down'
echo '"<" speed up'
echo '"a" auto-reverse'
echo '"s" frame-by-frame'
animate [0-9][0-9][0-9][0-9].$fmt
goto cleanup
cleanup:
#################################################################################
echo ""
rm -f ${tempfile}* >& /dev/null
if($?SAVE) then
echo "you can now review *.$fmt"
echo "these files' origins are referenced in $keyfile"
echo "you can (probably) view these files using:"
if(-e "$outgif") echo "netscape $outgif"
echo "netscape ????.$fmt"
echo "netscape ${htmlfile}"
if("$uname" =~ IRIX*) echo "imgview ????.$fmt"
if("$uname" =~ Lin*) echo "using gimp"
echo "or xv ????.$fmt (if you have it)"
exit
endif
echo "Cleaning up ..."
set frame = 1
set frames = `nawk '$1 !~ /[^0-9]/' $keyfile | wc -l`
set noglob
while ($frame <= $frames )
# extract image from the keyfile
set keyline = `nawk -v frame=$frame '$1 !~ /[^0-9]/{++n} n==frame{print}' $keyfile`
set xrayimage = `echo "$keyline" | nawk '{print $2}'`
set movieframe = `echo "$keyline" | nawk '{printf "%04d", $1}'`
rm -f ${movieframe}.pgm >& /dev/null
rm -f ${movieframe}.gif >& /dev/null
@ frame = ($frame + 1)
end
unset noglob
# useless now,
rm -f ${htmlfile}
exit
#################################################################################
Setup:
#################################################################################
#### ###### ##### # # #####
# # # # # # #
#### ##### # # # # #
# # # # # #####
# # # # # # #
#### ###### # #### #
#################################################################################
#################################################################################
set temp
set frames = ""
set frame = 0
echo -n "" >! ${tempfile}key
set i = 0
while ( $i < $#argv )
@ i = ($i + 1)
set arg = $argv[$i]
# identify request for help
if("$arg" =~ -h*) then
goto Help
endif
# user-specified output file name
if("$arg" =~ *.movie) then
set outmovie = "$arg"
continue
endif
if("$arg" =~ *.gif) then
set outgif = "$arg"
continue
endif
# check on existing files
if(-e "$arg") then
# user-preferred executables
test -x "$arg"
if(! $status ) then
set temp = `basename $arg`
if("$temp" == "adsc2pgm") set adsc2pgm = "$arg"
if("$temp" == "osc2pgm") set osc2pgm = "$arg"
if("$temp" == "mar2pgm") set mar2pgm = "$arg"
if("$temp" == "mogrify") set mogrify = "$arg"
if("$temp" == "convert") set convert = "$arg"
if("$temp" == "animate") set animate = "$arg"
continue
endif
# recognize directories
test -d "$arg"
if(! $status ) then
if("$arg" !~ */) set arg = "${arg}/"
# this is a directory (with frames in it? )
ls -1 "$arg" |\
nawk '/.img$/ || /.osc$/' |\
nawk -v dir=$arg '{print 1, dir $1}' |\
cat >> ${tempfile}key
continue
endif
# identify ordinary, arbitrary images
if(("$arg" =~ *.osc)||("$arg" =~ *.img)) then
# add it to the pile
echo "1 $arg" >> ${tempfile}key
continue
endif
# identify pre-fomratted keyfile
set size = `ls -lLd $arg |& nawk '$5+0>10{print $5+0}'`
if($size < 1000000) then
set temp = `nawk '$2 ~ /.img$/ || $2 ~ /.osc$/' $arg | wc -l`
if($temp > 0) then
# okay, must actually be a formatted $keyfile list
cat $arg |\
nawk '$2 ~ /.img$/ || $2 ~ /.osc$/' |\
cat >> ${tempfile}key
continue
endif
set temp = `nawk '$1 ~ /.img$/ || $1 ~ /.osc$/' $arg | wc -l`
if($temp > 0) then
# must just be a list of images
cat $arg |\
nawk '$1 ~ /.img$/ || $1 ~ /.osc$/ {++n; print n, $0}' |\
cat >> ${tempfile}key
continue
endif
endif
else
# $arg is not a file
# see if its a filename pattern
set dirname = `dirname $arg`
if((-e "$dirname")&&("$dirname" != ".")) then
set prefix = `basename $arg`
ls -1 $dirname |\
egrep "^${prefix}" |\
nawk -v dirname=$dirname '\
/.img$/ || /.osc/{++n; print n, dirname "/" $1}' |\
cat >! ${tempfile}frames
set temp = `cat ${tempfile}frames | wc -l`
if($temp != 0) then
cat ${tempfile}frames >> ${tempfile}key
else
echo "WARNING: no files matching ${arg}"'*'" exist! "
endif
rm -f ${tempfile}frames >& /dev/null
continue
endif
# see if it looked like a file
if(("$arg" =~ *.osc)||("$arg" =~ *.img)) then
echo "WARNING: $arg does not exist! "
continue
endif
# simplefy interpretation of settings
set arg = `echo "$arg" | nawk '{print tolower($0)}'`
if(("$arg" == "no")||("$arg" == "not")) then
# this will remain set until we know what it means
set NO
continue
endif
endif
if($?NO) then
if("$arg" == "convert") then
# must not want to use "convert"
set convert = ""
unset NO
continue
endif
if("$arg" == "mogrify") then
# must not want to use "mogrify"
set mogrify = ""
unset NO
continue
endif
if("$arg" == "animate") then
# must not want to use "animate"
set animate = ""
unset NO
continue
endif
if("$arg" == "adsc2pgm") then
# must not have "adsc2pgm"
set adsc2pgm = ""
unset NO
continue
endif
if("$arg" == "osc2pgm") then
# must not have "osc2pgm"
set osc2pgm = ""
unset NO
continue
endif
if("$arg" == "-zoom") set zoom = 1
if("$arg" == "zoom") set zoom = 1
if("$arg" == "-scale") set scale = 1
if("$arg" == "scale") set scale = 1
if("$arg" == "-negate") set neg = ""
if("$arg" == "-negative") set neg = ""
if("$arg" == "-invert") set neg = ""
if("$arg" == "-white") set neg = ""
if("$arg" == "-black") set neg = "-invert"
if("$arg" == "-label") unset LABEL
if("$arg" == "-save") unset SAVE
if("$arg" == "-skip") unset SKIP
if("$arg" == "negate") set neg = ""
if("$arg" == "negative") set neg = ""
if("$arg" == "invert") set neg = ""
if("$arg" == "white") set neg = ""
if("$arg" == "black") set neg = "-invert"
if("$arg" == "label") unset LABEL
if("$arg" == "save") unset SAVE
if("$arg" == "skip") unset SKIP
unset NO
continue
endif
# read user-specified zoom factor
if("$arg" == "-zoom") then
@ i = ($i + 1)
set arg = $argv[$i]
if("$arg" =~ [0-9]*) then
set zoom = "$arg"
set USER_zoom
else
echo "ERROR: bad zoom factor: $arg ... ignoring it."
endif
endif
# read user-specified scale
if("$arg" == "-scale") then
@ i = ($i + 1)
set scale = "$argv[$i]"
continue
endif
# read user-specified box edge
if("$arg" == "-edge") then
@ i = ($i + 1)
set num = `echo $argv[$i] | nawk '/^[0-9]/{print $1+0}'`
if("$num" == "") then
echo "bad edge definition: $arg $argv[$i] ... ignoring it"
else
set edge = "$num"
endif
continue
endif
# read user-specified image center
if("$arg" == "-center") then
# check for valid parameters
set temp = ""
foreach j ( 1 2 )
@ j = ( $i + $j )
set num = `echo $argv[$j] | nawk '/^[0-9]/{print $1+0}'`
if("$num" != "") then
set temp = ( $temp $num )
else
echo "ERROR: bad center definition: -center $temp $argv[$j] ... ignoring it."
break;
endif
end
if($#temp == 2) then
# a valid center was read
set center = ( $temp )
# back up one
@ i = ( $i + 1 )
endif
continue
endif
# read user-specified box
if("$arg" == "-box") then
# check the box parameters
@ j = ( $i + 1 )
set box_temp = "-box"
while($j <= ( $i + 4))
set temp = `echo "$argv[$j]" | nawk '$1 !~ /[^0-9\-]/{print $1+0}'`
if("$temp" != "") then
set box_temp = "$box_temp $temp"
else
echo "ERROR: bad box definition: $argv[$j] ... ignoring it."
break;
endif
@ j = ($j + 1)
end
if($j == ($i + 5)) then
# made it all the way through
set box = "$box_temp"
@ i = ( $j - 1 )
endif
endif
# read other user-specified options
if("$arg" == "-negate") set neg = "-invert"
if("$arg" == "-negative") set neg = "-invert"
if("$arg" == "-invert") set neg = "-invert"
if("$arg" == "-white") set neg = "-invert"
if("$arg" == "-label") set LABEL
if("$arg" == "-nolabel") unset LABEL
if("$arg" == "-noanimate") set animate = ""
if("$arg" == "-save") set SAVE
if("$arg" == "-skip") set SKIP
end
if("$animate" == "") unset ANIMATE
if("$convert" == "") set outgif = ""
# clean up the keyfile
echo "checking image files..."
cat ${tempfile}key |\
nawk 'NF>1{if(! system("ls " $2 " > /dev/null")) print}' |\
cat >! $keyfile
rm -f ${tempfile}key >& /dev/null
set frames = `cat $keyfile | wc -l`
if($frames == 0) then
# no recognizable frames, user should see help
rm -f $keyfile >& /dev/null
goto Help
endif
# make printing look nice
set width = `nawk '{print length($2)}' $keyfile | sort -n | tail -1`
# add a descriptive header
echo "frame# image_file options..." |\
nawk -v width=$width '{printf "%6s %"width"s %s\n", $1, $2, $3}' >! ${tempfile}key
cat $keyfile |\
nawk -v width=$width '{++n; printf "%6d %"width"s %s\n", n+0, $2, substr($0, index($0,$2)+length($2)+1)}' |\
cat >> ${tempfile}key
mv ${tempfile}key $keyfile >& /dev/null
# make sure we know if centering is being used
if("$center" == "") then
# check in keyfile for centers...
set center = `nawk 'BEGIN{RS=" "} p>0 && NF{if($1 !~ /[^0-9.-]/) print;--p; if(p==0) exit} $0=="-center"{p=2}' $keyfile`
if($#center != 2) then
# centering is not being used
set center = ""
if("$box" != "") then
# create a "center" from this box
set center = `echo "$box" | nawk '{print ($4+$2)/2, ($5+$3)/2}'`
else
# no box, no center: converters will default to whole image
endif
endif
endif
# if $center is not set, then no center is specified anywhere
# organize "box" parameters
if("$center" != "") then
# centering is being used, reconcile it with the box
if("$box" == "") then
# create a "box" at this center
# better default?
if("$edge" == "") set edge = 100
set box = `echo "$center $edge" | nawk '{printf "-box %.0f %.0f %.0f %.0f", $1-$3/2, $2-$3/2, $1+$3/2, $2+$3/2}'`
else
# move the defined box to this center
set temp = `echo "$box" | nawk '{print sqrt(($2-$4)^2), sqrt(($3-$5)^2)}'`
set box = `echo "$center $temp" | nawk '{printf "-box %.0f %.0f %.0f %.0f", $1-$3/2, $2-$4/2, $1+$3/2, $2+$4/2}'`
endif
endif
if(("$box" != "")&&("$edge" == "")) then
# make "edge" the box width
set edge = `echo "$box" | nawk '{print $4-$2}'`
endif
FindPrograms:
#################################################################################
# find converters for x-ray and ordinary images
echo "checking programs..."
# get list of extensions we will need to support
set EXTs = `nawk '$1 !~ /[^0-9]/{nf=split($2,w,"."); ++count[w[nf]]} END{for(ext in count) print ext}' $keyfile`
echo -n "" >! ${tempfile}programs
foreach ext ( $EXTs convert mogrify animate )
# connect "extention" with converter program name
set name = ""
if("$ext" == "img") set name = "$adsc2pgm"
if("$ext" == "osc") set name = "$osc2pgm"
if("$ext" =~ mar*) set name = "$mar2pgm"
if("$ext" == "gif") set name = "$convert"
if("$ext" == "mogrify") set name = "$mogrify"
if("$ext" == "convert") set name = "$convert"
if("$ext" == "animate") set name = "$animate"
# distinct signatures for each program
set signature = ""
if("$ext" == "img") set signature = '/Holton/'
if("$ext" == "osc") set signature = '/Holton/'
if("$ext" =~ mar*) set signature = '/anyone?/'
if("$ext" == "gif") set signature = '/ImageMagick/'
# also look for ImageMagick programs
if("$ext" == "mogrify") set signature = '/ImageMagick/'
if("$ext" == "convert") set signature = '/ImageMagick/'
if("$ext" == "animate") set signature = '/ImageMagick/'
if("$name" == "missing") continue
if("$name" == "") then
if("$ext" == "animate") continue
if("$ext" == "mogrify") continue
if("$ext" == "convert") continue
echo "WARNING: unsupported image type: $ext"
continue
endif
set program = "$name"
# perhaps file locations was specified above
test -x "$program"
if(! $status) then
# test it
set temp = `echo "" | $program -help |& nawk "$signature"'{print; exit}' | wc -l`
if($temp) then
# that's it
echo "$ext $program "`dirname $program` >> ${tempfile}programs
continue
endif
endif
# maybe we should see if it's in the path already?
set program = `basename "$program"`
test -x "$program"
if(! $status) then
# test it
set temp = `echo "" | $program -help |& nawk "$signature"'{print; exit}' | wc -l`
if($temp) then
# that's it
echo "$ext $program "`dirname $program` >> ${tempfile}programs
continue
endif
endif
# check $PATH with "which" (redundant? )
set program = `basename "$name"`
set program = `which $program |& nawk 'NR==1 && NF==1'`
test -x "$program"
if(! $status) then
# test it
set temp = `echo "" | $program -help |& nawk "$signature"'{print; exit}' | wc -l`
if($temp) then
# that's it
echo "$ext $program "`dirname $program` >> ${tempfile}programs
continue
endif
endif
# check same place as this script
set program = `dirname $0`/`basename $name`
test -x "$program"
if(! $status) then
# test it
set temp = `echo "" | $program -help |& nawk "$signature"'{print; exit}' | wc -l`
if($temp) then
# that's it
echo "$ext $program "`dirname $program` >> ${tempfile}programs
continue
endif
endif
# check current directory
set program = ./`basename $name`
test -x "$program"
if(! $status) then
# test it
set temp = `echo "" | $program -help |& nawk "$signature"'{print; exit}' | wc -l`
if($temp) then
# that's it
echo "$ext $program "`dirname $program` >> ${tempfile}programs
continue
endif
endif
# unable to find "$name"...
set name = `basename $name`
echo "$ext $name not found" >> ${tempfile}programs
end
# see if we got them all
set EXTs = `nawk '/ not found$/{print $1}' ${tempfile}programs`
if("$EXTs" == "") goto FoundPrograms
# more desperate search
onintr Skip_find_program
foreach pass ( 1 2 )
foreach ext ( $EXTs )
if("$ext" == "img") set signature = '/Holton/'
if("$ext" == "osc") set signature = '/Holton/'
if("$ext" =~ mar*) set signature = '/anyone?/'
if("$ext" == "gif") set signature = '/ImageMagick/'
if("$ext" == "mogrify") set signature = '/ImageMagick/'
if("$ext" == "convert") set signature = '/ImageMagick/'
if("$ext" == "animate") set signature = '/ImageMagick/'
set name = `nawk -v ext=$ext '$1==ext{print $2; exit}' ${tempfile}programs`
if("$name" == "mogrify") then
# see if we already have "convert"
set temp = `nawk '$1 == "convert" && NF==3' ${tempfile}programs | tail -1`
if("$temp" != "") then
# only look for mogrify if we havn't found convert
continue
endif
endif
echo -n "looking for $name ."
# read in directories where programs have been found
set goodplaces = `nawk 'NF==3{print $3}' ${tempfile}programs | sort -u`
foreach place ( $goodplaces /programs /usr/local /usr /home )
echo -n "."
if(! -e "$place") continue
set program = `find $place -name $name -perm -a+x -follow -print |& nawk 'NF==1{print; exit}'`
# test it
set temp = `echo "" | $program -help |& nawk "$signature"'{print; exit}' | wc -l`
if($temp) then
# that's it
echo "found $program"
echo "$ext $program "`dirname $program` >> ${tempfile}programs
break
endif
end
end
# clean up "found" list
cat ${tempfile}programs |\
nawk 'NF==3{found[$1]=$0} NF>3 && ! found[$1]{found[$1]=$0}\
END{for(ext in found) print found[ext]}' |\
cat >! ${tempfile}
mv ${tempfile} ${tempfile}programs >& /dev/null
# re-define "missing" list
set EXTs = `nawk '/ not found$/{print $1}' ${tempfile}programs`
echo ""
end
endif
Skip_find_program:
onintr
# clean up "found" list
cat ${tempfile}programs |\
nawk 'NF==3{found[$1]=$0} NF>3 && ! found[$1]{found[$1]=$0}\
END{for(ext in found) print found[ext]}' |\
cat >! ${tempfile}
mv ${tempfile} ${tempfile}programs >& /dev/null
# see if we can make do
nawk '/ not found$/{print $1}' ${tempfile}programs | grep "mogrify" >& /dev/null
if("$status") then
# mogrify is missing, but we can substitute with convert
nawk '/ not found$/{print $1}' ${tempfile}programs | grep "convert" >& /dev/null
if("$status") then
# no mogrify, but convert is available
set mogrify = "mogrify"
# alias "mogrify" syntax to "convert"
alias mogrify $convert' \!* '"${tempfile} ; mv ${tempfile}"' \!:$'
# we have "found" mogrify
echo "mogrify mogrify ." >> ${tempfile}programs
endif
endif
# clean up "found" list
cat ${tempfile}programs |\
nawk 'NF==3{found[$1]=$0} NF>3 && ! found[$1]{found[$1]=$0}\
END{for(ext in found) print found[ext]}' |\
cat >! ${tempfile}
mv ${tempfile} ${tempfile}programs >& /dev/null
# re-define "missing" list
set EXTs = `nawk '/ not found$/{print $1}' ${tempfile}programs`
# complain about ones that are still missing
foreach ext ( $EXTs )
set program = `nawk -v ext=$ext '$1==ext{print $2; exit}' ${tempfile}programs`
echo "unable to find $program"
if("$ext" == "img") set adsc2pgm = ""
if("$ext" == "osc") set osc2pgm = ""
if("$ext" =~ mar*) set mar2pgm = ""
if("$ext" == "gif") set convert = ""
if("$ext" == "mogrify") set mogrify = ""
if("$ext" == "convert") set convert = ""
if("$ext" == "animate") set animate = ""
end
FoundPrograms:
# now initialize program names
foreach ext ( `nawk 'NF==3{print $1}' ${tempfile}programs` )
set program = `nawk -v ext=$ext '$1==ext{print $2; exit}' ${tempfile}programs`
if("$ext" == "img") set adsc2pgm = "$program"
if("$ext" == "osc") set osc2pgm = "$program"
if("$ext" =~ mar*) set mar2pgm = "$program"
if("$ext" == "gif") set convert = "$program"
if("$ext" == "mogrify") set mogrify = "$program"
if("$ext" == "convert") set convert = "$program"
if("$ext" == "animate") set animate = "$program"
end
rm -f ${tempfile}programs >& /dev/null
# decide on a GIF converter
set togif = ""
if("$mogrify" != "") then
set togif = "$mogrify -format gif87"
endif
if("$convert" != "") then
# prefer to use "convert"
set togif = "$convert"
endif
# export any needed auxillary scripts
if(("$adsc2pgm" == "")||("$osc2pgm" == "")) then
# we can't do anything without these!
goto Compile
endif
# reaching here means we should be able to get something done
goto Return_from_Setup
Compile:
################################################################################
################################################################################
# try to compile adsc2pgm and osc2pgm from c source
################################################################################
echo "compiler unimplemented! "
goto Deploy_GetBox
# reaching here means we should be able to get something done
goto Return_from_Setup
Deploy_GetBox:
################################################################################
################################################################################
# extract a universal image2pgm converter, implemented in csh/od/awk
# it's slow, but usually faster than finding a compiler..
################################################################################
# indicate that -zoom flag is not supported
set NEED_ZOOM
echo "using portable (slow) converter: ./GetBox.com"
set SLOW_convert
# this script will work for either
if("$osc2pgm" == "") set osc2pgm = "./GetBox.com"
if("$adsc2pgm" == "") set adsc2pgm = "./GetBox.com"
if(-e "./GetBox.com") goto Return_from_Setup
cat << EOF-GetBox >! ./GetBox.com
#! /bin/csh -f
#
# GetBox.com - -James Holton 2-1-01
#
# Retrieve a pixel box from an R-axis II/IV/V or ADSC Q4 x-ray image.
# then print out the pixels in portable graymap
# (default: normalized to: out = in / 5*sigma(in))
#
# NOTE: X,Y convention here is (slow,fast) in image
# however, the output images may be flipped or flopped relative
# to the way they are displayed in mosflm
#
################################################################################
# make sure nawk works right
alias nawk nawk
echo "" | nawk '{print \$1}' >& /dev/null
if(\$status) alias nawk awk
# Defaults:
set image = ""
set mogrify = ""
set ext = ""
set box = ""
set scale = "auto"
set outfile = "out.pgm"
#set silent
set tempfile = getbox_temp
if("\$1" == "") goto Help
###############################################################################
goto Setup
# read options off of command-line
Help:
cat << EOF
usage: \$0 xrayimage.ext -box X1 Y1 X1 Y2 -scale factor -invert
where:
xrayimage.ext - is the x-ray image you want some pixels from (ADSC or Raxis)
X1 Y1 - pixel coordinates of the top, left-hand corner of the box
X2 Y2 - pixel coordinates of the lower, right-hand corner of the box
factor - is the intensity scale you want to put the 16-bit image into 8 bits
-invert - mean make the output image black on white
the X,Y convention here is the same as the mosflm display window,
but the output image may be flipped or flopped relative to mosflm
example:
\$0 se1_1_001.img -box 1100 1100 1200 1200 -scale auto
EOF
exit 9
Return_Setup:
###############################################################################
if(! \$?silent) echo "retreiveing box: (\${X1},\${Y1})-(\${X2},\${Y2}) from \$image"
# calculate starting point (in the frame file) of interesting pixels
@ offset = ( \$header + (\$record * \$X1) + (\$Y1 * \$word))
###################################
# dump file, retrieving only word values we are interested in
# for mosflm convention, y is the fast-varying axis here
od -dv -j \${offset} \$image |\\
nawk "BEGIN{record=\$record/\$word; xpixels=\$X2-\$X1; ypixels=\$Y2-\$Y1;}"' \\
{# scan each "od" printout line \\
for(i=2;i<=NF;++i){++pixels; ++y; \\
# only print pixels within "y" limits \\
if(y<=ypixels) print \$i;\\
# "record" boundary is now "x" boundary \\
if(pixels%record==0){y=0; ++x; print "n"};\\
# detect end of desired image \\
if(x>=xpixels) exit}}' |\\
cat >! \${tempfile}.pixeldump
# now we have a file that contains the word values from the frame
if(\$?SWAP) then
if(! \$?silent) echo "swaping bytes"
# swap bytes in each word
cat \${tempfile}.pixeldump |\\
nawk '! /n/ {printf "%d\\n", (\$1%256)*256 + \$1/256} /n/' |\\
cat >! \${tempfile}
mv \${tempfile} \${tempfile}.pixeldump >& /dev/null
endif
# now convert the word values to x-ray counts
if("\$type" == "RAXIS") then
cat \${tempfile}.pixeldump |\\
nawk -v mult=\$mult '\\
! /n/{if(\$1+0 <= 32768) printf "%d\\n", \$1+0;\\
if(\$1+0 > 32768) printf "%d\\n", (\$1%32768)*mult;\\
} /n/{print}' |\\
cat >! \${tempfile}
mv \${tempfile} \${tempfile}.pixeldump >& /dev/null
endif
# fill unread parts of the image with black
set PADs = \`echo "\$box \$X1 \$Y1 \$X2 \$Y2" | nawk '{print \$5-\$1, \$6-\$2, \$3-\$7, \$4-\$8}'\`
if("\$PADs" != "0 0 0 0") then
# pad edges with black
echo "\$box \$X1 \$Y1 \$X2 \$Y2" |\\
nawk '{print "PAD", \$5-\$1, \$6-\$2, \$3-\$7, \$4-\$8, \$3-\$1, \$4-\$2 }' |\\
cat - \${tempfile}.pixeldump |\\
nawk '/PAD/{xpre=\$2;ypre=\$3;xpost=\$4;ypost=\$5; xpixels=\$6; ypixels=\$7; \\
# pad the top will full-width black lines \\
for(x=0;x