#! /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 "Spot Review" >! ${htmlfile} set frame = 1 set frames = `nawk '$1 !~ /[^0-9]/' $keyfile | wc -l` set noglob while ($frame <= $frames ) # extract image filename 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}'` set graphicsfile = "${movieframe}.pgm" # allow a different box center for different files set center = `echo "$keyline" | nawk 'BEGIN{RS=" "} p>0{print;--p} $0=="-center"{p=2}'` if("$center" != "") then if("$box" == "") then # create a "box" from this center 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 "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 # figure out which converter to use set converter = "$convert" set ext = `echo "$xrayimage" | nawk 'BEGIN{FS="."} {print $NF}'` if("$ext" == "img") set converter = "$adsc2pgm" if("$ext" == "osc") set converter = "$osc2pgm" if("$ext" =~ mar*) set converter = "$mar2pgm" if("$ext" == "gif") set converter = "$convert" # only use zoom for image reduction here (PGMs are big) set zoom_card = `echo "$zoom" | nawk '$1+0<1{print "-zoom", $1}'` if($?NEED_ZOOM) set zoom_card = "" # convert the specified x-ray image region to a pgm echo -n "converting $xrayimage to ${movieframe}.$fmt" $converter $xrayimage $zoom_card -scale $scale $box $neg $graphicsfile >! ${tempfile}.log if($status) then # woops echo "" echo "ERROR: bad converter program: $converter" echo "$converter $xrayimage $zoom_card -scale $scale $box $neg $graphicsfile" $converter $xrayimage $zoom_card -scale $scale $box $neg $graphicsfile goto cleanup endif echo "" # read the scale of this image from the output set imagescale = `nawk '/intensity scale set to/{printf "%.3f", $NF}' ${tempfile}.log` rm -f ${tempfile}.log >& /dev/null # print out the intensity scale used if("$printscale" == "yes") echo -n " scaled by $imagescale" ################################## # apply any needed image transformations set xform = "" # built-in converter does not do flipping if($?SLOW_convert) then # flip/flop to mosflm-window convention if("$ext" == "img") set xform = "$xform -rotate 90 -flip" if("$ext" == "osc") set xform = "$xform -rotate -90" endif ################################## # make sure all images are the same size if("$imagesize" == "") then # make ImageMagick-style zoom spec set temp = `echo "$zoom" | nawk '{printf "-sample %.0f%%", $1*100}'` # only zoom image if $converter did not already do it if("$zoom_card" == "") set xform = "$xform $temp" else set xform = "$xform -sample $imagesize" endif # manual zoom if mogrify is not available if(("$zoom_card" == "")&&("$mogrify" == "")) then # use this (slow) PGM zoomer cat $graphicsfile |\ nawk -v zoom=$zoom 'BEGIN{x=y=0} \ NR==2{max_x=$1;max_y=$2; \ max_x_out=int(zoom*max_y);max_y_out=int(zoom*max_y);\ print max_x_out, max_y_out; next}\ NR<4 || /^#/{print; next}\ {for(i=1;i<=NF;++i){ \ # initialize an internal array with everything \ image[x "," y]=$i; \ ++x; if(x == max_x){x=0;++y}}}\ END{i=0;\ # start printing zoomed pixels \ for(x_out=0;x_out! ${tempfile}zoomed.pgm mv ${tempfile}zoomed.pgm $graphicsfile >& /dev/null endif if(("$xform" != "")&&("$mogrify" != "")) then # use "mogrify" to flip/zoom the image $mogrify $xform $graphicsfile endif if("$imagesize" == "") then # set the global image size from this result set temp = `od -c $graphicsfile |& nawk '$2=="P" && $3=="2" && $4=="\\n"{print $2, $3} {exit}'` if("$temp" != "") then set temp = `nawk '! /^#/{++n} n==2 && NF==2{print $1 "x" $2;exit} n>4{exit}' $graphicsfile` endif if("$temp" != "") then # this will be used to _define_ the image size from now on set imagesize = "$temp" endif endif ################################## # add a graphics label (if desired) if(("$mogrify" != "")&&($?LABEL)) then set pen = white if("$neg" != "") set pen = black # individual keyfile label? nawk '$1 !~ /[^0-9]/' $keyfile |\ nawk -v frame=$frame '{++n; nf=split($2,w,"/"); label=w[nf]}\ / -label /{label=substr($0, index($0, " -label ")+8)}\ n==frame{print label; exit}' |\ cat >! ${tempfile}label set temp = `cat ${tempfile}label` if("$temp" == "") then # default: label by filename basename $xrayimage >! ${tempfile}label endif cat ${tempfile}label | nawk '{printf " label: %s", $0}' # only ImageMagick can do this $mogrify -pen $pen -draw 'text 10,10 "@'${tempfile}label'"' $graphicsfile if($status) exit rm -f ${tempfile}label endif ################################## # finally, convert to gif, if possible if("$togif" != "") then # this is either "mogrify -fomrat gif" or "convert" $togif $graphicsfile ${movieframe}.gif87 mv ${movieframe}.gif87 ${movieframe}.gif # save space rm -f $graphicsfile >& /dev/null set graphicsfile = ${movieframe}.gif endif # finish off the line echo "" # user option to get intensity scale from first image if(("$scale" == "same")&&("$imagescale" != "")) then set scale = "$imagescale" echo "Image intensity scale set to: $scale" endif if("$frame" == 1) then # make a special 0th frame? #cp $graphicsfile 0000.$fmt endif echo "$graphicsfile" |\ nawk '{printf "\n", $1+0, $1+1;\ printf "

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;x0' >! \${tempfile} mv \${tempfile} \${tempfile}.pixeldump >& /dev/null endif # calculate pixel statistics (and autoscale) set sigma = \`nawk '\$1+0>0{++n;sum+=\$1*\$1} END{if(n) print sqrt(sum/n)}' \${tempfile}.pixeldump\` if("\$sigma" == "") set sigma = 1 if(\$scale == 0) set scale = \`echo \$sigma | nawk '{print 256/(5*\$1)}'\` echo "intensity scale set to: \$scale" ################################### # print out the PGM header echo "\$box " |\\ nawk '{print int(\$4-\$2), int(\$3-\$1)}' |\\ nawk '{printf "P2\\n%d %d\\n255\\n",\$1,\$2;}' |\\ cat >! \$outfile # add comments echo "\$box \$image \$scale" |\\ nawk '{printf "# (%d,%d)-(%d,%d) on %s\\n",\$1,\$2,\$3,\$4,\$5;\\ print "# values shown are " \$6 "*pixel"}' |\\ cat >> \$outfile # now print these pixels in Portable Greymap Format (PGM) cat \${tempfile}.pixeldump |\\ nawk -v scale=\$scale -v invert=\$?invert '/n/{print}\\ ! /n/{pix=scale*\$1; if(pix>255)pix=255; if(invert) pix= 255-pix;\\ print pix}' |\\ nawk '\$1=="n" || i==20 {print "";i=0} ! /n/{printf "%3d ", \$1;++i}' |\\ cat >> \$outfile if(! \$?silent) echo " output image: \$outfile" # clean up if(! \$?debug) rm -f \${tempfile}.pixeldump ################################################################################ exit Setup: ################################################################################ ################################################################################ ################################################################################ # determine byte order on this hardware set test = \`echo "az" | od -d | awk 'NR==1{print \$2}'\` if("\$test" == "24954") set BYTE_ORDER = "big_endian" if("\$test" == "31329") set BYTE_ORDER = "little_endian" # test od functionality alias ood od set test = \`echo "test" | od -c -j 2 |& awk '{print \$2}'\` if("\$test" != "s") then # no -j option supported, compensate: # od -x -j offset file --> od -x file offset alias od 'od \\!:1 \\!:\$ \\!:3.' set WEAK_od endif set mult = 8 ######################################## # scan command line set i = 0 while ( \$i < \$#argv ) @ i = ( \$i + 1 ) set arg = "\$argv[\$i]" # recognize image files if("\$arg" =~ *.pgm) then # must just be a preferred output filename set outfile = "\$arg" continue endif if(-e "\$arg") then set image = "\$arg" set ext = \`echo \$image | nawk 'BEGIN{FS="."} {print \$NF}'\` # figure out what kind of file this is set magic = \`ood -b \$image |& nawk '{print \$2 \$3 \$4 \$5 \$6 \$7 \$8 \$9 \$10;exit}'\` if("\$magic" =~ 122055101130111123*) set type = RAXIS if("\$magic" =~ 173012110105101104105122*) set type = ADSC continue endif # recognize image inversion flag if("\$arg" == "-invert") then set invert continue endif # recognize user scale flag if(("\$arg" == "-scale")&&(\$i < \$#argv)) then @ j = ( \$i + 1 ) set scale = "\$argv[\$j]" # take this as an "invert" signal if("\$scale" =~ "-*") set invert continue endif # read user-specified box if("\$arg" == "-box") then # check the box parameters @ j = ( \$i + 1 ) set box_temp = "" 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: \$arg ... 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 end # see if we can go on if(! -e "\$image") goto Help # make sure these are numbers (0 -> autoscale) set scale = \`echo "\$scale" | nawk '{print \$1+0}'\` ######################################## # deduce header and record sizes if("\$type" == "RAXIS") then set record = \`od -dv -j 784 \$image | nawk '{print \$2+\$3; exit}'\` set xsize = \`od -dv -j 788 \$image | nawk '{print \$2+\$3; exit}'\` set ysize = \`od -dv -j 768 \$image | nawk '{print \$2+\$3; exit}'\` set word = 2 # check for a reasonable number of IPs (relative byte-swap) set test = \`od -dv -j 796 \$image | nawk '\$3<100{print \$3+0} {exit}'\` if("\$test" == "") set SWAP # swap header bytes (if necessary) if(\$?SWAP) then set record = \`echo \$record | nawk '{printf "%d", (\$1%256)*256 + \$1/256}'\` set xsize = \`echo \$xsize | nawk '{printf "%d", (\$1%256)*256 + \$1/256}'\` set ysize = \`echo \$ysize | nawk '{printf "%d", (\$1%256)*256 + \$1/256}'\` endif # retrieve hi-range multiplier from header set temp = \`od -f -j 800 \$image | nawk '\$2+0>1 && \$2<200{print \$2+0} {exit}'\` if("\$temp" != "") set mult = "\$temp" set header = "\$record" endif # ADSC images if("\$type" == "ADSC") then # find a "safe" head parameter set head = \`od -cv \$image | nawk '{for(i=2;i<=NF;++i) print \$i}' | nawk '/\\\\n/{++n;c=0} {++c} c>1000{exit} END{print n}'\` set header = \`head -\$head \$image | nawk 'BEGIN{FS="="} /^HEADER_BYTES/{print \$2+0; exit}'\` set xsize = \`head -\$head \$image | nawk 'BEGIN{FS="="} /^SIZE1/{print \$2+0}' | tail -1\` set ysize = \`head -\$head \$image | nawk 'BEGIN{FS="="} /^SIZE2/{print \$2+0}' | tail -1\` set word = 2 set record = \`echo "\$word \$ysize" | nawk '{print \$1*\$2}'\` head -5 \$image | grep -q "\$BYTE_ORDER" >& /dev/null if(\$status) set SWAP endif if(! \$?header) then echo "ERROR: don't know how to read \$ext files." goto Help endif # sanity check? set filesize = \`ls -lL \$image | nawk '{print \$5}'\` set temp = \`echo "\$word \$xsize \$ysize \$filesize" | nawk '\$NF<\$1*(\$2+\$3){print "wrong"}'\` if("\$temp" != "") then # this doesn't make sense echo "WARNING: header does not make sense! " echo "x size = \$xsize" echo "y size = \$ysize" echo "record = \$record" echo "header = \$header" set xsize = \`echo "\$filesize" | nawk '{printf "%d", sqrt(\$1/2)}'\` set ysize = \$xsize set record = \`echo \$word \$xsize | nawk '{print \$1*\$2}'\` if("\$type" == "ADSC") set header = 512 if("\$type" == "RAXIS") then set record = 4096 set xsize = 1900 set ysize = 1900 if("\$filesize" > 10000000) set mult = 32 if("\$filesize" > 10000000) set record = 6000 if("\$filesize" > 30000000) set record = 4000 set header = \$record endif endif if("\$header" == "") then echo "ERROR: unrecognized image type: \$ext" goto Help endif ######################################## # figure out what to do with the box if("\$box" == "") then echo "WARNING: converting whole image (this will take a while...)" set box = ( 0 0 \$xsize \$ysize ) endif set X1 = \$box[1] set Y1 = \$box[2] set X2 = \$box[3] set Y2 = \$box[4] # rectify backwords box if("\$X1" > "\$X2") then set temp = \$X2 set X1 = \$X2 set X2 = \$temp endif if("\$Y1" > "\$Y2") then set temp = \$Y2 set Y1 = \$Y2 set Y2 = \$temp endif # clip edges for reading if(\$X1 < 0) set X1 = 0 if(\$Y1 < 0) set Y1 = 0 if(\$X2 > \$xsize) set X2 = "\$xsize" if(\$Y2 > \$ysize) set Y2 = "\$ysize" goto Return_Setup # future developments? put zoom feature back in? EOF-GetBox ################################################################################### chmod a+x GetBox.com goto Return_from_Setup exit # avoid onintr bugs end end endif endif # the future: support mar, else? support random graphics file formats on-board converter binaries? on-board c code for converters? color?