Ejemplo n.º 1
0
/*
** read several rows of data from a file
*/
void
VReadObjectData (FILE *fp,int object_id,VImage *image)
{
  static VImageInfo imageInfo;
  static VAttrList list=NULL;

  fseek(fp,0L,SEEK_SET);
  if (! ReadHeader (fp)) VError("error reading header"); 


  if (! (list = ReadAttrList (fp)))  
    VError("error reading attr list"); 
  
  if (! VGetImageInfo(fp,list,object_id,&imageInfo))
    VError(" error reading image info");

  if (imageInfo.nbands != VImageNBands((*image)))
    VError("incorrect number of bands");
  if (imageInfo.nrows != VImageNRows((*image))) 
    VError("incorrect number of rows");
  if (imageInfo.ncolumns != VImageNColumns((*image)))
    VError("incorrect number of columns");
  if (imageInfo.repn != VPixelRepn((*image))) 
    VError("incorrect pixel representation");
  
  if (! VReadBlockData (fp,&imageInfo,0,imageInfo.nrows,image))
    VError(" error reading data");
}
Ejemplo n.º 2
0
VAttrList VReadFile (FILE *f, VReadFileFilterProc *filter)
{
    VAttrList list;
    long offset;
    int i;

    /* Ensure that the correct FIL_Vista data file header is there: */
    if (! ReadHeader (f))
	return NULL;

    /* Read all attributes in the file: */
    if (! (list = ReadAttrList (f)))
	return NULL;

    /* Swallow the delimiter and read the binary data following it: */
    offset = 0;
    if (! ReadDelimiter (f) || ! ReadData (f, list, filter, &offset)) {
	VDestroyAttrList (list);
	return NULL;
    }

    /* Now we should be at the end of the file: */
    i = fgetc (f);
    if (i != EOF) {
	ungetc (i, f);
	VWarning ("VReadFile: File continues beyond expected EOF");
    }
    return list;
}
Ejemplo n.º 3
0
EXPORT_VISTA VistaIOAttrList VistaIOReadFile (FILE * f, VistaIOReadFileFilterProc * filter)
{
	VistaIOAttrList list;
	int i;
 	ReadStringBuf sbuf = {0,0}; 
	
#ifdef SupportUbcIff

	/* If the first byte of the file is "I", it looks like a UBC IFF file: */
	{
		i = fgetc (f);
		ungetc (i, f);
		if (i == 'I') {
			VistaIOImage image = VistaIOReadUbcIff (f);

			if (!image)
				return FALSE;
			list = VistaIOCreateAttrList ();
			VistaIOSetAttr (list, "image", NULL, VistaIOImageRepn, image);
			return list;
		}
	}

#endif

	/* Ensure that the correct Vista data file header is there: */
	if (!ReadHeader (f))
		return NULL;

	list = ReadAttrList (f, &sbuf); 
	if (sbuf.max_len)
		VistaIOFree(sbuf.buf); 
	/* Read all attributes in the file: */
	if (!list)
		return NULL;
	
	/* Swallow the delimiter and read the binary data following it: */
	offset = 0;
	if (!ReadDelimiter (f) || !ReadData (f, list, filter)) {
		VistaIODestroyAttrList (list);
		return NULL;
	}

	/* Now we should be at the end of the file: */
	i = fgetc (f);
	if (i != EOF) {
		ungetc (i, f);
		VistaIOWarning ("VistaIOReadFile: File continues beyond expected EOF");
	}
	return list;
}
Ejemplo n.º 4
0
VAttrList
GetListInfo(VString in_filename, ListInfo *linfo) {
    VAttrList list = NULL;
    VAttrListPosn posn;
    FILE *in_file = NULL;
    VString str, voxel = NULL;
    VRepnKind repn = VShortRepn;
    int ntimesteps, nrows, ncols;
    int id, j, itr, found, nobject, nbands;
    VImageInfo *imageInfo = NULL;
    in_file = VOpenInputFile(in_filename, TRUE);
    if(!in_file)
        VError("error opening file %s", in_filename);
    if(! ReadHeader(in_file))
        VError("error reading header");
    if(!(list = ReadAttrList(in_file)))
        VError("error reading attr list");
    j = 0;
    for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) {
        j++;
    }
    imageInfo = (VImageInfo *) VMalloc(sizeof(VImageInfo) * (j + 1));
    itr = ntimesteps = nrows = ncols = 0;
    nobject = nbands = found = id = 0;
    for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) {
        str = VGetAttrName(&posn);
        if(strncmp(str, "history", 7) == 0) {
            nobject++;
            continue;
        }
        VImageInfoIni(&imageInfo[nbands]);
        if(! VGetImageInfo(in_file, list, nobject, &imageInfo[nbands]))
            VError(" error reading image info");
        linfo->ntimesteps = linfo->nrows = linfo->ncols = 0;
        if(imageInfo[nbands].repn == VShortRepn) {
            found = 1;
            repn = imageInfo[nbands].repn;
            if(imageInfo[nbands].nbands > ntimesteps)
                ntimesteps = imageInfo[nbands].nbands;
            if(imageInfo[nbands].nrows > nrows)
                nrows = imageInfo[nbands].nrows;
            if(imageInfo[nbands].ncolumns > ncols)
                ncols = imageInfo[nbands].ncolumns;
            if(voxel == NULL)
                voxel = imageInfo[nbands].voxel;
            /* check if slice contains non-zero data */
            linfo->zero[nbands] = 1;
            if(imageInfo[nbands].nrows < 2)
                linfo->zero[nbands] = 0;
            linfo->info[id] = imageInfo[nbands];
            itr = imageInfo[nbands].repetition_time;
            id++;
            nbands++;
        }
        nobject++;
    }
    fclose(in_file);
    if(!found)
        VError(" couldn't find functional data");
    linfo->ntimesteps = ntimesteps;
    linfo->nrows    = nrows;
    linfo->ncols    = ncols;
    linfo->nslices  = id;
    linfo->itr      = itr;
    linfo->repn     = repn;
    linfo->voxel    = voxel;
    linfo->filename = VNewString(in_filename);
    return list;
}
Ejemplo n.º 5
0
static VAttrList ReadAttrList (FILE *f)
{
    VAttrList sublist, list = VCreateAttrList ();
    VAttrRec *a;
    int ch = 0;
    size_t name_size;
    VBundle b;
    char buf[2], *str, name_buf[VMaxAttrNameLength + 1];

    /* Swallow a { marking the start of the attribute list: */
    if (fscanf (f, " %1s", buf) != 1 || buf[0] != '{') {
	VWarning ("VReadFile: Missing {");
	goto Fail;
    }

    /* For each attribute up to the next "}": */
    while (fscanf (f, " %[^}: \t\n]", name_buf) == 1) {
	name_size = strlen (name_buf);

	/* Read a : and the first character of the attribute's value: */
	if (fscanf (f, " %1s", buf) != 1 || buf[0] != ':' ||
	    fscanf (f, " %1s", buf) != 1) {
	    VWarning ("VReadFile: Invalid %s attribute", name_buf);
	    goto Fail;
	}

	/* The first character of the value tells us whether its an attribute
	   list, quoted string, or unquoted string: */
	if (buf[0] == '{') {

	    /* The attribute value is another list of attributes: */
	    ungetc ('{', f);
	    if (! (sublist = ReadAttrList (f)))
		goto Fail;
	    a = VMalloc (sizeof (VAttrRec) + name_size);
	    a->value = sublist;
	    a->repn = VAttrListRepn;

	} else {

	    /* The value doesn't start with '{' -- parse a word or string: */
	    if (! (str = ReadString (f, buf[0], name_buf)))
		goto Fail;
	    while ((ch = fgetc (f)) && (ch == ' ' || ch == '\t')) ;
	    ungetc (ch, f);

	    /* If the word is followed by an '{'... */
	    if (ch == '{') {

		/* ...then it's a typed value -- the word is it's type name
		   and the { is the start of it's attribute list value. */
		b = VCreateBundle (str, NULL, 0, NULL);
		if (! (sublist = ReadAttrList (f))) {
		    VFree (b);
		    goto Fail;
		}
		b->list = sublist;
		a = VMalloc (sizeof (VAttrRec) + name_size);
		a->repn = VBundleRepn;
		a->value = b;

	    } else {

		/* ...otherwise store it as a simple string value: */
		a = VMalloc (sizeof (VAttrRec) + name_size + strlen (str) + 1);
		a->repn = VStringRepn;
		a->value = a->name + name_size + 1;
		strcpy (a->value, str);
	    }
	    VFree(str);
	}

	/* Copy the attribute's name into the newly allocated node: */
	strcpy (a->name, name_buf);

	/* Place the new node on the end of the growing attribute list: */
	a->next = NULL;
	a->prev = list->prev;
	if (a->prev) a->prev->next = a;
	else list->next = a;
	list->prev = a;
    }

    /* Swallow the terminating "}": */
    if (fscanf (f, " %1s", buf) != 1 || buf[0] != '}') {
	VWarning ("VReadFile: Missing }");
Fail:	VDestroyAttrList (list);
	return NULL;
    }
    return list;
}
Ejemplo n.º 6
0
VBoolean
VGetImageInfo(FILE *fp,VAttrList list,int object_id,VImageInfo *imageInfo)
{
  VBoolean  data_found,length_found,found;
  VAttrListPosn posn, subposn;
  VBundle b;
  int nobj=0;
  VLong xdata=0;
  VLong x=0;
  VLong lx=0;
  VDouble lf=0;
  VString str;

  /* if attr list not there, read it from disk */
  if (list == NULL) { 
    fseek(fp,0L,SEEK_SET);
    if (! ReadHeader (fp)) return FALSE;
    if (! (list = ReadAttrList (fp))) return FALSE;
  }

  nobj=0;
  for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) {
    nobj++;


    if (nobj-1 != object_id) continue;
    if (!VGetAttrValue (& posn, NULL, VBundleRepn, & b))
      VWarning("could not read bundle");

    /* get image dimensions */
    if (VLookupAttr (b->list, "nbands", & subposn)) {
      if (! VGetAttrValue (& subposn, NULL, VLongRepn, &x)) {
	VWarning ("VReadFile: "
		  "%s attribute's nbands attribute incorrect",
		  VGetAttrName (& posn));
	return FALSE;
      }
      imageInfo->nbands = x;
      VDeleteAttr (& subposn);
    }
    else {
      imageInfo->nbands = 1;
/*       VWarning(" attribute's nbands attribute not found"); */
    }

    if (VLookupAttr (b->list, VNRowsAttr, & subposn)) {
      if (! VGetAttrValue (& subposn, NULL, VLongRepn, &x)) {
	VWarning ("VReadFile: "
		  "%s attribute's nrows attribute incorrect",
		  VGetAttrName (& posn));
	return FALSE;
      }
      imageInfo->nrows = x;
      VDeleteAttr (& subposn);
    }
    else
      VWarning(" attribute's nrows attribute not found");



    if (VLookupAttr (b->list, VNColumnsAttr, & subposn)) {
      if (! VGetAttrValue (& subposn, NULL, VLongRepn, &x)) {
	VWarning ("VReadFile: "
		  "%s attribute's ncolumns attribute incorrect",
		  VGetAttrName (& posn));
	return FALSE;
      }
      imageInfo->ncolumns = x;
      VDeleteAttr (& subposn);
    }
    else
      VWarning(" attribute's ncolumns attribute not found");



    /* get compression info */
    imageInfo->ori_nbands = 0;
    if (VLookupAttr (b->list, "ori_nbands", & subposn)) {
      if (! VGetAttrValue (& subposn, NULL, VLongRepn, &x)) {
	VWarning ("VReadFile: "
		  "%s attribute's ori_nbands attribute incorrect",
		  VGetAttrName (& posn));
	return FALSE;
      }
      imageInfo->ori_nbands = x;
      VDeleteAttr (& subposn);
    }

    imageInfo->ori_nrows = 0;
    if (VLookupAttr (b->list, "ori_nrows", & subposn)) {
      if (! VGetAttrValue (& subposn, NULL, VLongRepn, &x)) {
	VWarning ("VReadFile: "
		  "%s attribute's ori_nrows attribute incorrect",
		  VGetAttrName (& posn));
	return FALSE;
      }
      imageInfo->ori_nrows = x;
      VDeleteAttr (& subposn);
    }

    imageInfo->ori_ncolumns = 0;
    if (VLookupAttr (b->list, "ori_ncolumns", & subposn)) {
      if (! VGetAttrValue (& subposn, NULL, VLongRepn, &x)) {
	VWarning ("VReadFile: "
		  "%s attribute's ori_ncolumns attribute incorrect",
		  VGetAttrName (& posn));
	return FALSE;
      }
      imageInfo->ori_ncolumns = x;
      VDeleteAttr (& subposn);
    }

    imageInfo->left_margin = 0;
    if (VLookupAttr (b->list, "left_margin", & subposn)) {
      if (! VGetAttrValue (& subposn, NULL, VLongRepn, &x)) {
	VWarning ("VReadFile: "
		  "%s attribute's left_margin attribute incorrect",
		  VGetAttrName (& posn));
	return FALSE;
      }
      imageInfo->left_margin = x;
      VDeleteAttr (& subposn);
    }

    imageInfo->top_margin = 0;
    if (VLookupAttr (b->list, "top_margin", & subposn)) {
      if (! VGetAttrValue (& subposn, NULL, VLongRepn, &x)) {
	VWarning ("VReadFile: "
		  "%s attribute's top_margin attribute incorrect",
		  VGetAttrName (& posn));
	return FALSE;
      }
      imageInfo->top_margin = x;
      VDeleteAttr (& subposn);
    }

    /* Extract any data and length attributes in the object's value: */
    if (data_found = VLookupAttr (b->list, VDataAttr, & subposn)) {
      if (! VGetAttrValue (& subposn, NULL, VLongRepn, &xdata)) {
	VWarning ("VReadFile: "
		  "%s attribute's data attribute incorrect",
		  VGetAttrName (& posn));
	return FALSE;
      }
      imageInfo->data = (size_t)xdata;
      VDeleteAttr (& subposn);
    }
    else
      VWarning(" attribute's data attribute not found");
    

    if (length_found = VLookupAttr (b->list, VLengthAttr, & subposn)) {
      if (! VGetAttrValue (& subposn, NULL, VLongRepn, &x)) {
	VWarning ("VReadFile: "
		  "%s attribute's length attribute incorrect",
		  VGetAttrName (& posn));
	return FALSE;
      }
      imageInfo->length = x;
      VDeleteAttr (& subposn);
    }

    /* None or both must be present: */
    if (data_found ^ length_found) {
      VWarning ("VReadFile: %s attribute has %s but not %s",
		VGetAttrName (& posn),
		data_found ? "data" : "length",
		data_found ? "length" : "data");
      return FALSE;
    }


    /* get pixel repn */
    if (found = VLookupAttr (b->list,VRepnAttr, & subposn)) {
      if (! VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	VWarning ("VReadFile: "
		  "%s attribute's repn attribute incorrect",
		  VGetAttrName (& posn));
	return FALSE;
      }
      if (strncmp(str,"bit",3) == 0) {
	imageInfo->repn = VBitRepn;
	imageInfo->pixelSize = sizeof(VBit);
      }
      else if  (strncmp(str,"ubyte",5) == 0) {
	imageInfo->repn = VUByteRepn;
	imageInfo->pixelSize = sizeof(VUByte);
      }
      else if  (strncmp(str,"sbyte",5) == 0) {
	imageInfo->repn = VSByteRepn;
	imageInfo->pixelSize = sizeof(VSByte);
      }
      else if  (strncmp(str,"short",5) == 0) {
	imageInfo->repn = VShortRepn;
	imageInfo->pixelSize = sizeof(VShort);
      }
      else if  (strncmp(str,"long",4) == 0) {
	imageInfo->repn = VLongRepn;
	imageInfo->pixelSize = sizeof(VLong);
      }
      else if  (strncmp(str,"float",5) == 0) {
	imageInfo->repn = VFloatRepn;
	imageInfo->pixelSize = sizeof(VFloat);
      }
      else if  (strncmp(str,"double",6) == 0) {
	imageInfo->repn = VDoubleRepn;
	imageInfo->pixelSize = sizeof(VDouble);
      }
      else
	return FALSE;



      /* get fmri specifics */
      if (found = VLookupAttr (b->list,"patient", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->patient,0,STRLEN);
	  strncpy(imageInfo->patient,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"modality", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->modality,0,STRLEN);
	  strncpy(imageInfo->modality,str,strlen(str));
	}
      }
      if (found = VLookupAttr (b->list,"angle", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->angle,0,STRLEN);
	  strncpy(imageInfo->angle,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"voxel", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->voxel,0,STRLEN);
	  strncpy(imageInfo->voxel,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"name", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->name,0,STRLEN);
	  strncpy(imageInfo->name,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"fixpoint", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->fixpoint,0,STRLEN);
	  strncpy(imageInfo->fixpoint,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"ca", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->ca,0,STRLEN);
	  strncpy(imageInfo->ca,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"cp", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->cp,0,STRLEN);
	  strncpy(imageInfo->cp,str,strlen(str));
	}
      }
      if (found = VLookupAttr (b->list,"location", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->location,0,STRLEN);
	  strncpy(imageInfo->location,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"orientation", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->orientation,0,STRLEN);
	  strncpy(imageInfo->orientation,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"talairach", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->talairach,0,STRLEN);
	  strncpy(imageInfo->talairach,str,strlen(str));
	}
      }
      if (found = VLookupAttr (b->list,"MPIL_vista_0", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->MPIL_vista_0,0,STRLEN);
	  strncpy(imageInfo->MPIL_vista_0,str,strlen(str));
	  sscanf(str," repetition_time=%ld ",&lx);
	  imageInfo->repetition_time = lx;
	}
      }
      if (found = VLookupAttr (b->list,"extent", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->extent,0,STRLEN);
	  strncpy(imageInfo->extent,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"spPH", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VLongRepn, &lx)) {
	  imageInfo->spPH = lx;
	}
      }
      if (found = VLookupAttr (b->list,"spPG", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VLongRepn, &lx)) {
	  imageInfo->spPG = lx;
	}
      }
      if (found = VLookupAttr (b->list,"subjects", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VLongRepn, &lx)) {
	  imageInfo->subjects = lx;
	}
      }
      if (found = VLookupAttr (b->list,"ntimesteps", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VLongRepn, &lx)) {
	  imageInfo->ntimesteps = lx;
	}
      }
      if (found = VLookupAttr (b->list,"df", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VDoubleRepn, &lf)) {
	  imageInfo->df = lf;
	}
      }
      if (found = VLookupAttr (b->list,"norm_mean", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VDoubleRepn, &lf)) {
	  imageInfo->norm_mean = lf;
	}
      }
      if (found = VLookupAttr (b->list,"norm_sig", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VDoubleRepn, &lf)) {
	  imageInfo->norm_sig = lf;
	}
      }            
      if (found = VLookupAttr (b->list,"repetition_time", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VLongRepn, &lx)) {
	  imageInfo->repetition_time = lx;
	}
      }

      /* new, 28.3.2013 */
      if (found = VLookupAttr (b->list,"indexOrigin", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->indexOrigin,0,STRLEN);
	  strncpy(imageInfo->indexOrigin,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"columnVec", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->columnVec,0,STRLEN);
	  strncpy(imageInfo->columnVec,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"rowVec", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->rowVec,0,STRLEN);
	  strncpy(imageInfo->rowVec,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"sliceVec", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->sliceVec,0,STRLEN);
	  strncpy(imageInfo->sliceVec,str,strlen(str));
	}
      }

      if (found = VLookupAttr (b->list,"FOV", & subposn)) {
	if (VGetAttrValue (& subposn, NULL, VStringRepn, &str)) {
	  memset(imageInfo->FOV,0,STRLEN);
	  strncpy(imageInfo->FOV,str,strlen(str));
	}
      }

    }
  }
  x = VGetHeaderLength(fp);
  imageInfo->offsetHdr = x;

  /* fprintf(stderr," info: %d %d %d\n",imageInfo->nbands,imageInfo->nrows,imageInfo->ncolumns); */

  return TRUE;
}
Ejemplo n.º 7
0
int main(int argc, char *argv[]) {
    /* command line arguments */
    static VString out_filename;
    static VArgVector in_files;
    static VString trans_filename = "";
    static VBoolean in_found, out_found;
    static VShort minval = 0;
    static VBoolean compress = TRUE;
    static VFloat resolution = 3;
    static VOptionDescRec options[] = {
        { "in", VStringRepn, 0, & in_files, & in_found, NULL, "Input file" },
        { "out", VStringRepn, 1, & out_filename, & out_found, NULL, "Output file" },
        {
            "trans", VStringRepn, 1, &trans_filename, VRequiredOpt, NULL,
            "File containing transformation matrix"
        },
        {
            "resolution", VFloatRepn, 1, &resolution, VOptionalOpt, NULL,
            "Output voxel resolution in mm"
        },
        {
            "minval", VShortRepn, 1, &minval, VOptionalOpt, NULL,
            "Signal threshold"
        },
        {
            "compress", VBooleanRepn, 1, &compress, VOptionalOpt, NULL,
            "Whether to compress empty slices"
        }
    };
    VStringConst in_filename;
    FILE *in_file, *out_file, *fp;
    VAttrList list, list1, out_list;
    VAttrListPosn posn;
    VImage trans = NULL;
    VImage *dst_image;
    VImageInfo *imageInfo;
    int nobject = 0, ntimesteps = 0, nbands = 0, nrows = 0, ncols = 0;
    VString ca, cp, extent, str;
    int found = 0;
    int j, dest_nbands;
    char prg_name[100];
	char ver[100];
	getLipsiaVersion(ver, sizeof(ver));
	sprintf(prg_name, "vfunctrans V%s", ver);
    fprintf(stderr, "%s\n", prg_name);
    /* Parse command line arguments: */
    if(! VParseCommand(VNumber(options), options, & argc, argv) ||
            ! VIdentifyFiles(VNumber(options), options, "in", & argc, argv, 0) ||
            ! VIdentifyFiles(VNumber(options), options, "out", & argc, argv, -1))
        goto Usage;
    if(argc > 1) {
        VReportBadArgs(argc, argv);
Usage:
        VReportUsage(argv[0], VNumber(options), options, NULL);
        exit(EXIT_FAILURE);
    }
    if(resolution <= 0)
        VError(" 'resolution' must be an integer > 0");
    /*
    ** Read the transformation matrix:
    */
    fp = VOpenInputFile(trans_filename, TRUE);
    list1 = VReadFile(fp, NULL);
    if(! list1)
        VError("Error reading image");
    fclose(fp);
    for(VFirstAttr(list1, & posn); VAttrExists(& posn); VNextAttr(& posn)) {
        if(VGetAttrRepn(& posn) != VImageRepn)
            continue;
        if(strncmp(VGetAttrName(&posn), "transform", 9) != 0)
            continue;
        VGetAttrValue(& posn, NULL, VImageRepn, & trans);
        break;
    }
    if(trans == NULL)
        VError("transformation matrix not found");
    /*
    ** check attributes
    */
    if(VGetAttr(VImageAttrList(trans), "ca", NULL,
                VStringRepn, (VPointer) & ca) != VAttrFound)
        VError(" attribute 'ca' missing in transformation matrix ");
    if(VGetAttr(VImageAttrList(trans), "cp", NULL,
                VStringRepn, (VPointer) & cp) != VAttrFound)
        VError(" attribute 'cp' missing in transformation matrix ");
    if(VGetAttr(VImageAttrList(trans), "extent", NULL,
                VStringRepn, (VPointer) & extent) != VAttrFound)
        VError(" attribute 'extent' missing in transformation matrix ");
    /*
    ** open in-file
    */
    if(in_files.number < 1 || in_files.number > 1)
        VError(" incorrect number of input files: %d", in_files.number);
    in_filename = ((VStringConst *) in_files.vector)[0];
    if(strcmp(in_filename, "-") == 0)
        in_file = stdin;
    else {
        in_file = fopen((char *)in_filename, "r");
        if(! in_file)
            VError("Failed to open input file %s", in_filename);
    }
    /*
    ** read file info
    */
    if(! ReadHeader(in_file))
        VError("error reading header");
    if(!(list = ReadAttrList(in_file)))
        VError("error reading attr list");
    j = 0;
    for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) {
        j++;
    }
    imageInfo = (VImageInfo *) VMalloc(sizeof(VImageInfo) * (j + 1));
    nobject = nbands = found = 0;
    for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) {
        str = VGetAttrName(&posn);
        if(strncmp(str, "history", 7) == 0) {
            nobject++;
            continue;
        }
        VImageInfoIni(&imageInfo[nbands]);
        if(! VGetImageInfo(in_file, list, nobject, &imageInfo[nbands]))
            VError(" error reading image info");
        if(imageInfo[nbands].repn == VShortRepn) {
            found = 1;
            nrows = imageInfo[nbands].nrows;
            ncols = imageInfo[nbands].ncolumns;
            ntimesteps = imageInfo[nbands].nbands;
            nbands++;
        }
        nobject++;
    }
    fclose(in_file);
    if(!found)
        VError(" couldn't find functional data");
    /*
    ** process each time step
    */
    dst_image = VFunctrans(in_filename, imageInfo, nbands, trans, resolution,
                           ntimesteps, minval, compress, &dest_nbands);
    /*
    ** output
    */
    out_list = VCreateAttrList();
    VHistory(VNumber(options), options, prg_name, &list, &out_list);
    for(j = 0; j < dest_nbands; j++) {
        VAppendAttr(out_list, "image", NULL, VImageRepn, dst_image[j]);
    }
    /* Open and write the output file: */
    if(strcmp(out_filename, "-") == 0)
        out_file = stdout;
    else {
        out_file = fopen(out_filename, "w");
        if(! out_file)
            VError("Failed to open output file %s", out_filename);
    }
    if(!VWriteFile(out_file, out_list) || fclose(out_file))
        VSystemError("error writing output file");
    fprintf(stderr, "\n%s: done.\n", argv[0]);
    return (EXIT_SUCCESS);
}
Ejemplo n.º 8
0
static VistaIOAttrList ReadAttrList (FILE * f, ReadStringBuf *sbuf)
{
	VistaIOAttrList sublist, list = VistaIOCreateAttrList ();
	VistaIOAttrRec *a;
	int ch = 0;
	size_t name_size;
	VistaIOBundle b;
	char buf[2], *str, name_buf[VistaIOMaxAttrNameLength + 1];

	/* Swallow a { marking the start of the attribute list: */
	if (fscanf (f, " %1s", buf) != 1 || buf[0] != '{') {
		VistaIOWarning ("VistaIOReadFile: Missing {");
		goto Fail;
	}

	/* For each attribute up to the next "}": */
	while (fscanf (f, " %255[^}: \t\n]", name_buf) == 1) {
		name_size = strlen (name_buf);
		/* Covertiy does not detect that strlen(name_buf) <= 255 */
		   
		
		/* Read a : */
		if (fscanf (f, " %1s", buf) != 1 || buf[0] != ':') {
			VistaIOWarning ("VistaIOReadFile: Invalid %s attribute", name_buf);
			goto Fail;
		}
		
		do {
			ch = fgetc(f);
		} while (ch == ' ');

		/* File terminated early? */ 
		if (ch == EOF)
			goto Fail;
		
		buf[0] = (char)ch;
		
		/* The first character of the value tells us whether its an attribute
		   list, quoted string, or unquoted string: */
		if (buf[0] == '{') {

			/* The attribute value is another list of attributes: */
			ungetc ('{', f);
			if (!(sublist = ReadAttrList (f, sbuf)))
				goto Fail;
			a = VistaIOMalloc (sizeof (VistaIOAttrRec) + name_size);
			a->value = sublist;
			a->repn = VistaIOAttrListRepn;

		} else if (buf[0] != '}' && buf[0] != '\n') {/* non-empty attr */

			/* The value doesn't start with '{' -- parse a word or string: */
			if (!(str = ReadString (f, buf[0], name_buf, sbuf))) 
				goto Fail;
			
			while ((ch = fgetc (f)) && (ch == ' ' || ch == '\t'));
			ungetc (ch, f);

			/* If the word is followed by an '{'... */
			if (ch == '{') {

				/* ...then it's a typed value -- the word is it's type name
				   and the { is the start of it's attribute list value. */
				b = VistaIOCreateBundle (str, NULL, 0, NULL);
				if (!(sublist = ReadAttrList (f, sbuf))) {
					VistaIOFree (b);
					goto Fail;
				}
				b->list = sublist;
				a = VistaIOMalloc (sizeof (VistaIOAttrRec) + name_size);
				a->repn = VistaIOBundleRepn;
				a->value = b;
			} else {

				/* ...otherwise store it as a simple string value: */
				/*coverity[INTEGER_OVERFLOW]   name_size is at most 255 (line 309)*/
				a = VistaIOMalloc (sizeof (VistaIOAttrRec) + name_size +
					     strlen (str) + 1);
				a->repn = VistaIOStringRepn;
				a->value = a->name + name_size + 1;
				strcpy (a->value, str);
			}
		}else{
			ungetc (buf[0], f);
			a = VistaIOMalloc (sizeof (VistaIOAttrRec) + name_size +
				     strlen ("") + 1);
			a->repn = VistaIOStringRepn;
			a->value = a->name + name_size + 1;
			strcpy (a->value, "");
		}

		/* Copy the attribute's name into the newly allocated node: */
		strcpy (a->name, name_buf);

		/* Place the new node on the end of the growing attribute list: */
		a->next = NULL;
		if ((a->prev = list->prev))
			a->prev->next = a;
		else
			list->next = a;
		list->prev = a;
	}

	/* Swallow the terminating "}": */
	if (fscanf (f, " %1s", buf) != 1 || buf[0] != '}') {
		VistaIOWarning ("VistaIOReadFile: Missing }");
	Fail:
		VistaIODestroyAttrList (list);
		return NULL;
	}
	return list;
}