-------------------------------------------
entry variable: $inoutim -- editable image set
-------------------------------------------
keyboard equiv: [none]

Main/Editable Image Set

The "im:" entry (not visible on fn-F2 data
overlay interface), shows the name of the
editable 3D data set.

On startup, this entry will contain the data set
initially read when tkmedit was started, which
will typically be read from:

  $SUBJECTS_DIR/<subject>/mri

N.B.: this entry is not visible on the fn-F2
(data overlay) interface.

The initial part of the path will be abbreviated
using a tidle (~) to represent the current
$SUBJECTS_DIR.

To re-read that data set (e.g., to revert to
original), click "R" to the right of the entry.

To read in a different editable data set, enter
either:

(1) a COR dir (containing 256 256x256x1byte
    or 512 512x512x1byte COR-??? images in
    coronal orientation, and COR-.info file)

       *or*

(2) an .mgh/.mgh format file (with similar
    orientation, datasize, and imagesize)

and click the "R" to the right of the entry.

The file name can be entered as a relative,
abbreviated, or absolute path.  Assuming
SUBJECTS_DIR equals /fmri/subjects, the following
3 names refer to the same COR dir data set:

  orig
  ~/mri/orig
  /fmri/subjects/martys/mri/orig

and the following 3 names refer to the same .mgz
data set:

  orig.mgz
  ~/mri/orig.mgz
  /fmri/subjects/martys/mri/orig.mgz

A <Return> is same as "R".  Edits always affect
this image set, even when it is not visible.

To write out the initial dataset, use SAVEIMG or
"W".  Change the name in the entry before
clicking "W" to write out edited data set with a
different name.

================================================
COR dir format (coronal slices)
 --256 256x256x1byte imgs, COR-001 to COR-256
       *or*
 --512 512x512x1byte imgs, COR-001 to COR-512
================================================
### COR-.info (1x1x1mm or 0.5x0.5x0.5mm)
imnr0 1
imnr1 256 *or* 512
ptype 2
x 256 *or* 512
y 256 *or* 512
fov 0.256000
thick 0.001000
psiz 0.001000
locatn 0.000000
strtx -0.128000
endx 0.128000
strty -0.128000
endy 0.128000
strtz -0.128000
endz 0.128000
tr 0.000000
te 0.000000
ti 0.000000
xform talairach.xfm

================================================
mgh/mgz format -- single-file 1byte brik
================================================
/*** tkmedit.c: read mgh/mgz file to im[ ][ ][ ] ***/

#define FIRST_IMG_BYTE 284 /*0-based (hdr=byte0-283)*/
#define MATCH_SUFFIX(S,A) \
  ((strlen(A)>=strlen(S) && \
  !strncmp(A+strlen(A)-strlen(S),S,strlen(S))))

unsigned char  **im[512];  /* 3D img */
size_t bufsize;
float ps;        /* pix size */
float st;        /* slice thick */
float xx0, xx1;  /* img xmin,xmax */
float yy0, yy1;
float zz0, zz1;

void read_images(char *fpref)
{
  char   cmd[MAXNAMELEN];
  int    version, width, height, depth, nframes;
  int    type, dof, used, bpv= -1;
  short  goodRASFlag;
  float  spacingX, spacingY, spacingZ;
  float  x_r, x_a, x_s, y_r, y_a, y_s;
  float  z_r, z_a, z_s, c_r, c_a, c_s;
  FILE   *fp, *pfp;

  if (MATCH_SUFFIX(".mgz",fpref) ||
      MATCH_SUFFIX(".mgh",fpref)) {
    if (MATCH_SUFFIX(".mgh",fpref))
      sprintf(cmd,"cat %s",fpref);
    if (MATCH_SUFFIX(".mgz",fpref))
      sprintf(cmd,"gunzip -c %s",fpref);
    pfp = popen(cmd,"r");
    version = freadInt(pfp);
    printf("medit: version = %d\n",version);
    width = freadInt(pfp);
    printf("medit: width = %d\n",width);
    height = freadInt(pfp);
    printf("medit: height = %d\n",height);
    depth =  freadInt(pfp);
    printf("medit: depth = %d\n",depth);
    nframes = freadInt(pfp);
    printf("medit: nframes = %d\n",nframes);/*briks*/
    type = freadInt(pfp);
    printf("medit: type = %d\n",type);
    dof = freadInt(pfp);
    printf("medit: dof = %d\n",dof);
    spacingX = freadFloat(pfp);
    printf("medit: spacingX = %f\n",spacingX);/*1*/
    spacingY = freadFloat(pfp);
    printf("medit: spacingY = %f\n",spacingY);
    spacingZ = freadFloat(pfp);
    printf("medit: spacingZ = %f\n",spacingZ);
    goodRASFlag=freadShort(pfp);
    printf("medit: goodRASFlag = %d\n",goodRASFlag);
    x_r = freadFloat(pfp);
    x_a = freadFloat(pfp);
    x_s = freadFloat(pfp);
    y_r = freadFloat(pfp);
    y_a = freadFloat(pfp);
    y_s = freadFloat(pfp);
    z_r = freadFloat(pfp);
    z_a = freadFloat(pfp);
    z_s = freadFloat(pfp);
    c_r = freadFloat(pfp);
    c_a = freadFloat(pfp);
    c_s = freadFloat(pfp);
    printf("medit: x_r=%f x_a=%f x_s=%f\n",x_r,x_a,x_s);
    printf("medit: y_r=%f y_a=%f y_s=%f\n",x_r,x_a,x_s);
    printf("medit: z_r=%f z_a=%f z_s=%f\n",x_r,x_a,x_s);
    printf("medit: c_r=%f c_a=%f c_s=%f\n",x_r,x_a,x_s);
    if (type==0) bpv = sizeof(char);  /* MRI_UCHAR */
    if (type==1) bpv = sizeof(int);   /* MRI_INT   */
    if (type==3) bpv = sizeof(float); /* MRI_FLOAT */
    if (type==4) bpv = sizeof(short); /* MRI_SHORT */
    used = 7*sizeof(int) +
           3*sizeof(float) +
           1*sizeof(short) +
           12*sizeof(float);
    for (i=0; i<FIRST_IMG_BYTE-used; i++)
      fgetc(pfp);  /* can't fseek a pipe */
    if (((width==256 && height==256 && depth==256) ||
         (width==512 && height==512 && depth==512)) &&
         nframes==1 && bpv==1) {
      imnr0 = 1; imnr1 = width;
      ptype = 2;  /* N.B.: assumes COR orientation! */
      xnum = ynum = width;  /* force cube */
      ps = st = 1.0;
      xx0 = yy0 = zz0 = -128.0;
      xx1 = yy1 = zz1 =  128.0;
    }
    else {
      printf(
      "medit: ### %s not 256^3 or 512^3 1frm byteimg\n",fpref);
      exit(1);
    }
    numimg = imnr1-imnr0+1;
    bufsize = ((unsigned long)xnum)*ynum;
    buf = (unsigned char *)lcalloc(bufsize,sizeof(char));
    for (k=0; k<numimg; k++) {
      im[k] = (unsigned char **)lcalloc(ynum,sizeof(char *));
      for (i=0;i<ynum;i++)
       im[k][i] = (unsigned char *)lcalloc(xnum,sizeof(char));
    }
    for (k=0; k<numimg; k++) {
      changed[k] = 0;
      fread(buf,sizeof(char),bufsize,pfp);
      buffer_to_image(buf,im[k],xnum,ynum);
    }
    pclose(pfp);
  }
}

void buffer_to_image(
  unsigned char *buf, unsigned char **im,
  int ysize, int xsize)
{
  int            i,j;
  unsigned long  k=0;
  float          sum=0;

  for (i=0;i<ysize;i++)
  for (j=0;j<xsize;j++) {
    im[i][j] = buf[k++];
    sum += im[i][j];
  }
}
