VBoolean VLookupAttr (VAttrList list, VStringConst name, VAttrListPosn *posn) { for (VFirstAttr (list, posn); VAttrExists (posn); VNextAttr (posn)) if (strcmp (VGetAttrName (posn), name) == 0) return TRUE; return FALSE; }
static VPointer VGraphEncodeDataMethod (VPointer value, VAttrList list, size_t length, VBoolean *free_itp) { VGraph graph = value; VAttrListPosn posn; VNode n; size_t len; VPointer p, ptr; VAdjacency adj; int i, nadj; #define pack(repn, cnt, dest) \ if (! VPackData (repn, cnt, dest, VMsbFirst, &len, &p, NULL)) return NULL; \ p = (char *) p + len; length -= len; len = length; /* Remove the attributes prepended by the VGraphEncodeAttrsMethod: */ for (VFirstAttr (list, & posn); strcmp (VGetAttrName (& posn), VRepnAttr) != 0; VDeleteAttr (& posn)); VDeleteAttr (& posn); /* Allocate a buffer for the encoded data: */ if (length == 0) { *free_itp = FALSE; return value; /* we may return anything != 0 here */ }; p = ptr = VMalloc (length); len = length; /* Pack each node: */ for (i = 1; i <= graph->size; i++) { n = VGraphGetNode(graph, i); if (n == 0) continue; /* Count the number of adjacencies : */ for (adj = n->base.head, nadj = 0; adj; adj = adj->next) nadj++; /* Pack the header */ pack(VLongRepn, 1, &i); pack(VLongRepn, 1, &nadj); /* Pack the adjacencies : */ for (adj = n->base.head; adj; adj = adj->next) { pack(VLongRepn, 1, &adj->id); if (graph->useWeights) { pack(VFloatRepn, 1, &adj->weight); }; }; /* Pack the node itself: */ if (graph->useWeights) { pack(VFloatRepn, 1, &(n->base.weight)); }; pack(graph->node_repn, graph->nfields, n->data); } *free_itp = TRUE; return ptr; }
VBoolean WriteImages (VString Name, VAttrList Images, VAttrList& history_list) { FILE* file; /* output file */ VAttrList list; /* attribute list */ VAttrListPosn pos; /* position in list */ VImage image; /* image in list */ VBoolean success; /* success flag */ char history[]="history"; /* open file */ file = fopen (Name, "w"); if (!file) { VError ("Failed to open output file '%s'", Name); return FALSE; } /* create list */ list = VCreateAttrList(); /* insert images */ for (VFirstAttr (Images, &pos); VAttrExists (&pos); VNextAttr (&pos)) { VGetAttrValue (&pos, NULL, VImageRepn, &image); VAppendAttr (list, VGetAttrName (&pos), NULL, VImageRepn, image); } /* Prepend history */ VPrependAttr(list, history ,NULL,VAttrListRepn,history_list); /* write file */ success = VWriteFile (file, list); if (!success) VError ("Failed to write output file '%s'", Name); /* remove images */ for (VFirstAttr (list, &pos); VAttrExists (&pos); VNextAttr (&pos)) if (VGetAttrRepn (&pos) == VImageRepn) VSetAttrValue (&pos, NULL, VImageRepn, NULL); /* clean-up*/ VDestroyAttrList (list); fclose (file); return success; } /* WriteImages */
static VPointer VImageEncodeDataMethod (VPointer value, VAttrList list, size_t length, VBoolean *free_itp) { VImage image = value; VAttrListPosn posn; size_t len; VPointer ptr; /* Remove the attributes prepended by the VImageEncodeAttrsMethod: */ for (VFirstAttr (list, & posn); strcmp (VGetAttrName (& posn), VRepnAttr) != 0; VDeleteAttr (& posn)) ; VDeleteAttr (& posn); /* Pack and return pixel data: */ if (! VPackData (VPixelRepn (image), VImageNPixels (image), VImageData (image), VMsbFirst, & len, & ptr, free_itp)) return NULL; if (len != length) VError ("VImageEncodeDataMethod: Encoded data has unexpected length"); return ptr; }
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; }
static VBoolean WriteAttr (FILE *f, VAttrListPosn *posn, int indent, VList *data_list, long *offset) { int i; char *str; VRepnKind repn; VAttrList sublist; VBundle b; DataBlock *db; VTypeMethods *methods; size_t length; VPointer value; VBoolean result; VAttrListPosn subposn; /* Indent by the specified amount: */ for (i = 0; i < indent; i++) FailTest (fputc ('\t', f)); indent++; /* Output the attribute's name: */ FailTest (fprintf (f, "%s: ", VGetAttrName (posn))); /* Ouput its value: */ switch (repn = VGetAttrRepn (posn)) { case VAttrListRepn: VGetAttrValue (posn, NULL, VAttrListRepn, (VPointer) & sublist); result = WriteAttrList (f, sublist, indent, data_list, offset); break; case VBundleRepn: VGetAttrValue (posn, NULL, VBundleRepn, (VBundle) & b); if (! WriteString (f, b->type_name)) return FALSE; FailTest (fputc (' ', f)); /* If it's a typed value with binary data... */ if (b->length > 0) { /* Include "data" and "length" attributes in its attribute list: */ VPrependAttr (b->list, VLengthAttr, NULL, VLongRepn, (VLong) b->length); VPrependAttr (b->list, VDataAttr, NULL, VLongRepn, (VLong) *offset); /* Add it to the queue of binary data blocks to be written: */ *offset += b->length; db = VNew (DataBlock); db->posn = *posn; db->list = b->list; db->length = b->length; VListAppend (*data_list, db); } /* Write the typed value's attribute list: */ result = WriteAttrList (f, b->list, indent, data_list, offset); /* Remove the "data" and "length" attributes added earlier: */ if (b->length > 0) { VFirstAttr (b->list, & subposn); VDeleteAttr (& subposn); VDeleteAttr (& subposn); } break; case VStringRepn: VGetAttrValue (posn, NULL, VStringRepn, (VPointer) & str); result = WriteString (f, str); break; default: if (! (methods = VRepnMethods (repn)) || ! methods->encode_attr || ! methods->encode_data) { VWarning ("VWriteFile: " "%s attribute has unwriteable representation: %s", VGetAttrName (posn), VRepnName (repn)); return FALSE; } /* Write the type name: */ if (! WriteString (f, VRepnName (repn))) return FALSE; FailTest (fputc (' ', f)); /* Invoke the object type's encode_attr method to obtain an attribute list: */ VGetAttrValue (posn, NULL, repn, & value); sublist = (methods->encode_attr) (value, & length); /* If binary data is indicated... */ if (length > 0) { /* Include "data" and "length" attributes in the attr list: */ VPrependAttr (sublist, VLengthAttr, NULL, VLongRepn, (VLong) length); VPrependAttr (sublist, VDataAttr, NULL, VLongRepn, (VLong) *offset); *offset += length; } /* Add the object to the queue of binary data blocks to be written: */ db = VNew (DataBlock); db->posn = *posn; db->list = sublist; db->length = length; VListAppend (*data_list, db); /* Write the typed value's attribute list: */ result = WriteAttrList (f, sublist, indent, data_list, offset); /* Remove the "data" and "length" attributes added earlier: */ if (length > 0) { VFirstAttr (sublist, & subposn); VDeleteAttr (& subposn); VDeleteAttr (& subposn); } } /* Output a trailing newline: */ if (result) FailTest (fputc ('\n', f)); return result; Fail: VWarning ("VWriteFile: Write to stream failed"); return FALSE; }
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; }
int main(int argc, char *argv[]) { VAttrList list, out_list; VImage src; VImage dest = NULL; VAttrListPosn posn; VString str; int nimages; char historystr[] = "history"; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vdelcereb 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); } /* Read source image(s): */ if(strcmp(in_filename, "-") == 0) in_file = stdin; else { in_file = fopen(in_filename, "r"); if(! in_file) VError("Failed to open input file %s", in_filename); } if(!(list = VReadFile(in_file, NULL))) exit(EXIT_FAILURE); fclose(in_file); /* Process image */ nimages = 0; for(VFirstAttr(list, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & src); dest = VCerebellum(src); VSetAttrValue(& posn, NULL, VImageRepn, dest); VDestroyImage(src); nimages++; break; } /* Create outlist */ out_list = VCreateAttrList(); /* Make History */ VHistory(VNumber(options), options, prg_name, &list, &out_list); /* Alle Attribute (ausser history:) in die neue Liste schreiben: */ for(VFirstAttr(list, &posn); VAttrExists(&posn); VNextAttr(&posn)) { if(strncmp(VGetAttrName(&posn), historystr, strlen(historystr)) == 0) continue; switch(VGetAttrRepn(&posn)) { case VImageRepn: VGetAttrValue(&posn, NULL, VImageRepn, &src); VAppendAttr(out_list, VGetAttrName(&posn), NULL, VImageRepn, src); break; case VStringRepn: VGetAttrValue(&posn, NULL, VStringRepn, &str); VAppendAttr(out_list, VGetAttrName(&posn), NULL, VImageRepn, str); break; default: break; } } /* Write the results to 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)) { if(verbose >= 1) fprintf(stderr, "%s: processed %d image%s.\n", argv[0], nimages, nimages == 1 ? "" : "s"); fprintf(stderr, "%s: done.\n", argv[0]); } return 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); }