changeset 591:f133ad1932bd

Initial revision
author neelin <neelin>
date Mon, 12 Sep 1994 13:56:34 +0000
parents 97293bd0f924
children a579eb71e100
files progs/mincview/Makefile progs/mincview/invert_raw_image.c progs/mincview/mincview
diffstat 3 files changed, 291 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
new file mode 100644
--- /dev/null
+++ b/progs/mincview/Makefile
@@ -0,0 +1,78 @@
+# --------------------------------------------------------------------
+#
+# MINC Makefile
+#
+
+ROOT = ../..
+include $(ROOT)/Makefile_machine_specific
+include $(ROOT)/Makefile_configuration
+
+# Executable names
+SCRIPTS  = mincview
+PROGS    = invert_raw_image
+EXTRA_OBJS = 
+HEADERS  = 
+MANSECT  = 1
+CDEFINES = -DDEBUG#                        cpp defines
+LDOPT    = $(PROG_LDOPT)
+
+# --------------------------------------------------------------------
+
+CFLAGS    = $(CDEFINES) $(INCLUDES) $(OPT)# CFLAGS and LINTFLAGS should
+LINTFLAGS = $(CDEFINES) $(INCLUDES)#        be same, except for -g/-O
+
+PROG_OBJ  = $(PROGS:=.o)#                 list of objects
+LINT_LIST = $(PROG_OBJ:.o=.ln)
+LINT_EXTRA= $(EXTRA_OBJS:.o=.ln)
+LINT_LIST_EXE = $(LINT_LIST:.ln=.)#       list of executable names to lint
+MANPAGES = #$(PROGS).$(MANSECT)
+
+# --------------------------------------------------------------------
+
+#Suffixes for man pages
+.SUFFIXES: .1 .man1 .3 .man3
+
+default: build
+
+all: build lint
+
+build: $(PROGS) man
+
+man: $(MANPAGES)
+
+#Dependency on Makefile
+$(PROG_OBJ) $(LINT_LIST) $(EXTRA_OBJS) $(LINT_EXTRA) : Makefile
+
+.c.ln:#                                   defines the rule for creating .ln
+	lint $(LINTFLAGS) -c $< -o $@
+
+.c.o:#                                    defines the rule for creating .o
+	$(CC) $(CFLAGS) -c $< -o $@
+
+#Dependency of .o and .ln on .h
+$(PROG_OBJ) $(EXTRA_OBJS) : $(HEADERS)
+
+$(LINT_LIST) $(LINT_EXTRA) : $(HEADERS)
+
+# How to make executables
+$(PROGS) : $$@.o $(EXTRA_OBJS) $(CC_PROG_LIB) $(CC_MINC_LIB)
+	$(CC) -o $@ $@.o $(EXTRA_OBJS) $(LDOPT)
+
+#  how to lint the executable source
+lint: $(LINT_LIST_EXE)
+
+$(LINT_LIST_EXE) : $$@ln $(LINT_EXTRA) $(LINT_PROG_LIB) $(LINT_MINC_LIB)
+	lint -u $(LINTFLAGS) $@ln $(LINT_EXTRA) $(LINT_PROG_LIB) $(LINT_MINC_LIB)
+
+# how to make man pages
+.man1.1:
+	$(NROFF) $(NROFF_FLAGS) $< > $@
+
+# Remove all derived files in this directory
+clean:
+	$(RM) $(RM_FLAGS) $(LINT_LIST) $(PROGS) $(PROG_OBJ) \
+	            $(EXTRA_OBJS) $(LINT_EXTRA) $(MANPAGES)
+
+install:
+	$(CP) $(CP_FLAGS) $(SCRIPTS) $(PROGS) $(INSTALL_BINDIR)
+
new file mode 100644
--- /dev/null
+++ b/progs/mincview/invert_raw_image.c
@@ -0,0 +1,100 @@
+/* ----------------------------- MNI Header -----------------------------------
+@NAME       : invert_raw_image.c
+@INPUT      : argc - number of arguments
+              argv - arguments
+                 1 - image size in x (-ve means invert)
+                 2 - image size in y (-ve means invert)
+                 3 - number of bytes per pixel (optional - default = 1)
+@OUTPUT     : (none)
+@DESCRIPTION: Reads an image from standard input and copies it to standard
+              output, inverting along either or both dimensions according 
+              to the arguments
+@METHOD     : 
+@GLOBALS    : 
+@CALLS      : 
+@CREATED    : December 3,1991 (Peter Neelin)
+@MODIFIED   : 
+---------------------------------------------------------------------------- */
+#include <stdlib.h>
+#include <stdio.h>
+
+#define ABS( x )  ( ((x) > (0)) ? (x) : (-(x)) )
+#define MAX( x, y )  ( ((x) >= (y)) ? (x) : (y) )
+#define SIGN( x )  ( ((x) > (0)) ? (1) : (-1) )
+
+#define ERROR_STATUS -1
+#define NORMAL_STATUS 0
+
+main(int argc, char *argv[])
+{
+   int i,j,k,oi,image_size,offset,bytes_per_pixel,row_size,nread;
+   int xsize,ysize,xstart,ystart,xstop,ystop,xstep,ystep;
+   char *pname;
+   char *buffer,*outbuf;
+
+   /* Check arguments */
+   pname=argv[0];
+   if ((argc != 3)&&(argc != 4)) {
+      (void) fprintf(stderr,"Usage : %s xsize ysize <bytesperpixel>\n",pname);
+      (void) exit(ERROR_STATUS);
+   }
+   xsize = atol(argv[1]);
+   ysize = atol(argv[2]);
+   if (argc == 4) {
+      bytes_per_pixel = atol(argv[3]);
+      if (bytes_per_pixel <=0) {
+         (void) fprintf(stderr,"%s : Negative bytes per pixel\n",pname);
+      }
+   }
+   else {
+      bytes_per_pixel = 1;
+   }
+   if ((xsize == 0) || (ysize == 0)) {
+      (void) fprintf(stderr,"%s : Illegal image size\n",pname);
+      (void) exit(ERROR_STATUS);
+   }
+   image_size = ABS(xsize*ysize);
+   row_size = ABS(xsize);
+   if (((buffer=malloc(image_size*bytes_per_pixel)) == NULL) ||
+       ((outbuf=malloc(row_size*bytes_per_pixel)) == NULL)){
+      (void) fprintf(stderr,"%s : Image too large\n",pname);
+      (void) exit(ERROR_STATUS);
+   }
+
+   /* Get range of loop */
+   xstart = MAX(0,-xsize-1);
+   xstop  = MAX(0,xsize-1);
+   xstep  = SIGN(xsize);
+   ystart = MAX(0,-ysize-1);
+   ystop  = MAX(0,ysize-1);
+   ystep  = SIGN(ysize);
+
+   /* Loop through images */
+   while ((nread=fread(buffer, bytes_per_pixel, image_size, stdin))
+                     == image_size) {
+
+      /* Write out inverted image */
+      for (j=ystart; ystep*j <= ystop; j += ystep) {
+         offset=j*ABS(xsize);
+         for (i=xstart, oi=0; xstep*i <= xstop; i += xstep, oi++) {
+            for (k=0; k<bytes_per_pixel; k++) {
+               outbuf[oi*bytes_per_pixel+k]=
+                  buffer[(offset+i)*bytes_per_pixel+k];
+            }
+         }
+         (void) fwrite(outbuf, bytes_per_pixel, row_size, stdout);
+      }
+
+   }
+
+   /* Check that input corresponds to complete images */
+   if (nread>0) {
+      (void) fprintf(stderr,"%s : Insufficient data\n",pname);
+      (void) exit(ERROR_STATUS);
+   }
+
+   return NORMAL_STATUS;
+
+}
+
+
new file mode 100755
--- /dev/null
+++ b/progs/mincview/mincview
@@ -0,0 +1,113 @@
+#! /bin/csh -f
+# Script for viewing a minc file.
+# Requires a pnm file viewer such as xv or display from ImageMagick,
+# as wells as the filter invert_raw_image.
+
+# Constants
+set xv_visual = `xdpyinfo | awk '/class.*TrueColor/{found=1};END{if (found) print "-visual TrueColor"}'`
+set VIEWER = "xv"       # Any pnm display program that handles a list of files
+set VIEWER_OPTIONS = "-geometry 512x512 -fixed -cmap -raw $xv_visual"
+set PGM_CODE = "P5"
+set PPM_CODE = "P6"
+
+# Check arguments
+if (($#argv < 1) || ($#argv > 2)) then
+   echo "Usage: $0 <filename.mnc> [<slice number>]"
+   exit
+endif
+set filename = "$1"
+set slice = `awk "BEGIN{print $2+0}" < /dev/null`
+@ slice_specified = ( $#argv >= 2 )
+set workingdir = /usr/tmp
+if ($?TMPDIR) then
+   set workingdir = $TMPDIR
+else
+   set workingdir = /usr/tmp
+endif
+set workingdir = $workingdir/mincview-$$
+
+set dims = `mincinfo $filename -vardims image`
+
+# Check for vector dimension
+set pnm_code = $PGM_CODE
+set bytes_per_pixel = 1
+if ("$dims[$#dims]" == "vector_dimension") then
+   @ ndims = $#dims - 1
+   set nvec = `mincinfo $filename -dimlength $dims[$#dims]`
+   set start_suffix = ",0"
+   if ($nvec != 3) then
+      set count_suffix = ",1"
+   else
+      set count_suffix = ",3"
+      set pnm_code = $PPM_CODE
+      set bytes_per_pixel = 3
+   endif
+else
+   set ndims = $#dims
+   set start_suffix = ""
+   set count_suffix = ""
+endif
+
+if ($ndims > 3) then
+   @ nprefix = $ndims - 3
+   set start_prefix = \
+      "`awk 'BEGIN{for (i=0;i<$nprefix;i++) print "'"'"0,"'"'"}' < /dev/null`"
+   set count_prefix = \
+      "`awk 'BEGIN{for (i=0;i<$nprefix;i++) print "'"'"1,"'"'"}' < /dev/null`"
+else if ($ndims < 2) then
+   echo "No image found in file $filename"
+   exit -1
+else
+   set start_prefix = ""
+   set count_prefix = ""
+endif
+
+# Get number of slices and image dimensions
+@ ind1 = $ndims - 2
+@ ind2 = $ndims - 1
+@ ind3 = $ndims
+if ($ind1 > 0) then
+   set nslices = `mincinfo $filename -dimlength $dims[$ind1]`
+else
+   set nslices = 1
+endif
+set imgsize = `mincinfo $filename -dimlength $dims[$ind2] -dimlength $dims[$ind3]`
+if ($slice_specified) then
+   if (($slice >= $nslices) || ($slice < 0)) then
+      echo "Slice number out of range"
+      exit -1
+   endif
+   @ nslices = $slice + 1
+endif
+
+# Check for inverting images to get standard orientation
+set imgstep = `mincinfo $filename -attvalue ${dims[$ind2]}:step -attvalue ${dims[$ind3]}:step -error 1`
+set rows = `echo $imgstep[1] $imgsize[1] | nawk '{if ($1<0) print $2; else print -$2;}'`
+set cols = `echo $imgstep[2] $imgsize[2] | nawk '{if ($1<0) print -$2; else print $2;}'`
+
+# Loop through slices, if needed
+onintr cleanup
+mkdir $workingdir
+echo -n Loading slices
+while ($slice < $nslices)
+   echo -n .
+   if ($ndims > 2) then
+      set start = "$start_prefix $slice,0,0 $start_suffix"
+      set count = "$count_prefix 1,$imgsize[1],$imgsize[2] $count_suffix"
+   else
+      set start = "0,0 $start_suffix"
+      set count = "$imgsize[1],$imgsize[2] $count_suffix"
+   endif
+   mincextract $filename -byte -start "$start" -count "$count" |\
+      invert_raw_image $cols $rows $bytes_per_pixel | \
+      (echo "$pnm_code\n$imgsize[1] $imgsize[2]\n255";cat) \
+         > $workingdir/$slice
+   @ slice++
+end
+echo Done
+
+$VIEWER $VIEWER_OPTIONS $workingdir/{?,??,???,????} 
+
+cleanup:
+   rm -f $workingdir/*
+   rmdir $workingdir