void ReadField (VString Name, VImage& X, VImage& Y, VImage& Z, VAttrList& history_list) { FILE* file; /* input file */ VAttrList list; /* attribute list */ VAttrListPosn pos; /* position in list */ /* initialize results */ X = Y = Z = NULL; /* open file */ file = fopen (Name, "r"); if (!file) { VError ("Failed to open input file '%s'", Name); return; } /* read file */ list = VReadFile (file, NULL); if (!list) { VError ("Failed to read input file '%s'", Name); fclose (file); return; } /* Read History */ history_list = VCopyAttrList(VReadHistory(&list)); /* extract field images */ if (VLookupAttr (list, "X", &pos)) { VGetAttrValue (&pos, NULL, VImageRepn, &X); VSetAttrValue (&pos, NULL, VImageRepn, NULL); } else VError ("Input file '%s' does not contain an image 'X'", Name); if (VLookupAttr (list, "Y", &pos)) { VGetAttrValue (&pos, NULL, VImageRepn, &Y); VSetAttrValue (&pos, NULL, VImageRepn, NULL); } else VError ("Input file '%s' does not contain an image 'Y'", Name); if (VLookupAttr (list, "Z", &pos)) { VGetAttrValue (&pos, NULL, VImageRepn, &Z); VSetAttrValue (&pos, NULL, VImageRepn, NULL); } else VError ("Input file '%s' does not contain an image 'Z'", Name); /* clean-up*/ VDestroyAttrList (list); fclose (file); } /* ReadField */
VBoolean VExtractAttr (VAttrList list, VStringConst name, VDictEntry *dict, VRepnKind repn, VPointer value, VBooleanPromoted required) { VAttrListPosn posn; /* If the attribute is in the list... */ if (VLookupAttr (list, name, & posn)) { if (value) { /* Get its value: */ if (! VGetAttrValue (& posn, dict, repn, value)) { VWarning ("VExtractAttr: %s attribute has bad value", name); return FALSE; } /* Clone or hide the value if we're about to delete it: */ if (repn == VStringRepn) * (VString *) value = VNewString (* (VString *) value); } /* Remove it from the list: */ VDeleteAttr (& posn); return TRUE; } /* Otherwise complain if the attribute was a required one: */ if (required) VWarning ("VExtractAttr: %s attribute missing", name); return ! required; }
VGetAttrResult VGetAttr (VAttrList list, VStringConst name, VDictEntry *dict, VRepnKind repn, VPointer value) { VAttrListPosn posn; /* Look up the attribute name in the list: */ if (! VLookupAttr (list, name, & posn)) return VAttrMissing; /* Get its value in the specified representation: */ return VGetAttrValue (& posn, dict, repn, value) ? VAttrFound : VAttrBadValue; }
void VSetAttr (VAttrList list, VStringConst name, VDictEntry *dict, VRepnKind repn, ...) { va_list args; VAttrListPosn posn; VAttrRec *a; /* Locate any existing attribute of the specified name: */ va_start (args, repn); if (VLookupAttr (list, name, & posn)) SetAttr (& posn, dict, repn, & args); else { /* None exists -- append a new attribute of that name: */ a = NewAttr (name, dict, repn, & args); a->next = NULL; if (a->prev = list->prev) a->prev->next = a; else list->next = a; list->prev = a; } va_end (args); }
static VBoolean ReadData (FILE *f, VAttrList list, VReadFileFilterProc *filter, long *offset) { VAttrListPosn posn, subposn; VAttrList sublist; VBundle b; VRepnKind repn; VBoolean read_data, data_found, length_found; VLong data, length; VTypeMethods *methods; VPointer value; for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) { switch (VGetAttrRepn (& posn)) { case VAttrListRepn: /* Recurse on nested attribute list: */ VGetAttrValue (& posn, NULL, VAttrListRepn, & sublist); if (! ReadData (f, sublist, filter, offset)) return FALSE; break; case VBundleRepn: VGetAttrValue (& posn, NULL, VBundleRepn, & b); repn = VLookupType (b->type_name); /* If a filter routine was supplied, ask it whether to bother with the binary data: */ read_data = ! filter || (*filter) (b, repn); /* Extract any data and length attributes in the object's value: */ data_found = VLookupAttr (b->list, VDataAttr, & subposn); if (data_found) { if (! VGetAttrValue (& subposn, NULL, VLongRepn, & data)) { VWarning ("VReadFile: " "%s attribute's data attribute incorrect", VGetAttrName (& posn)); return FALSE; } VDeleteAttr (& subposn); } length_found = VLookupAttr (b->list, VLengthAttr, & subposn); if (length_found) { if (! VGetAttrValue (& subposn, NULL, VLongRepn, & length)) { VWarning ("VReadFile: " "%s attribute's length attribute incorrect", VGetAttrName (& posn)); return FALSE; } 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; } /* Read the binary data associated with the object: */ if (data_found) { if (data < *offset) { VWarning ("VReadFile: " "%s attribute's data attribute incorrect", VGetAttrName (& posn)); return FALSE; } if (! read_data) data += length; /* To seek forward to the start of the data block we first try fseek. That will fail on a pipe, in which case we seek by reading. */ if (data != *offset && fseek (f, (long) data - *offset, SEEK_CUR) == -1 && errno == ESPIPE && ! MySeek (f, data - *offset)) { VSystemWarning ("VReadFile: Seek within file failed"); return FALSE; } if (read_data) { b->data = VMalloc (b->length = length); if (fread (b->data, 1, length, f) != length) { VWarning ("VReadFile: Read from stream failed"); return FALSE; } *offset = data + length; } else /* bug: read error occured when bundle was not read by a filter function. FK 24/03/98 */ *offset = data; } /* Recurse to read binary data for sublist attributes: */ if (! ReadData (f, b->list, filter, offset)) return FALSE; /* If the object's type is registered and has a decode method, invoke it to decode the binary data: */ if (read_data && repn != VUnknownRepn && (methods = VRepnMethods (repn)) && methods->decode) { if (! (value = (methods->decode) (VGetAttrName (& posn), b))) return FALSE; /* Replace the old typed value with the newly decoded one: */ VSetAttrValue (& posn, NULL, repn, value); VDestroyBundle (b); } break; default: break; } } return TRUE; }
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; }