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 VWriteObjects (FILE *file, VRepnKind repn, VAttrList attributes, int nobjects, VPointer objects[]) { VAttrList list; VAttrListPosn posn; int i; VBoolean result; /* Create an attribute list if none was supplied: */ list = attributes ? attributes : VCreateAttrList (); /* Prepend to the attribute list an attribute for each object: */ for (i = nobjects - 1; i >= 0; i--) VPrependAttr (list, VRepnName (repn), NULL, repn, objects[i]); /* Write the attribute list: */ result = VWriteFile (file, list); /* Remove the attributes just prepended: */ VFirstAttr (list, & posn); for (i = 0; i < nobjects; i++) VDeleteAttr (& posn); if (list != attributes) VDestroyAttrList (list); return result; }
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; }
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; }
int VReadObjects (FILE *file, VRepnKind repn, VAttrList *attributes, VPointer **objects) { VAttrList list; VAttrListPosn posn; int i, nobjects = 0; VPointer *vector; /* Read the file's contents: */ list = VReadFile (file, NULL); if (! list) return FALSE; /* Count the objects found: */ for (VFirstAttr (list, & posn); VAttrExists (& posn); VNextAttr (& posn)) nobjects += (VGetAttrRepn (& posn) == repn); if (nobjects == 0) { VWarning ("VReadObjects: No %s objects present in stream", VRepnName (repn)); VDestroyAttrList (list); return FALSE; } /* Allocate a vector of that many object pointers: */ vector = VMalloc (nobjects * sizeof (VPointer)); /* Extract the objects from the attribute list and place them in the vector: */ for (VFirstAttr (list, & posn), i = 0; VAttrExists (& posn); ) if (VGetAttrRepn (& posn) == repn) { VGetAttrValue (& posn, NULL, repn, vector + i); VDeleteAttr (& posn); i++; } else VNextAttr (& posn); /* Return the objects and the remaining attributes: */ *attributes = list; *objects = vector; return nobjects; }
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; }