static VAttrList VGraphEncodeAttrMethod (VPointer value, size_t *lengthp) { VGraph graph = value; VAttrList list; size_t len, nadj; int i, slong, sfloat, spriv, nnodes; VNode n; VAdjacency adj; /* Compute the file space needed for the Graph's binary data: */ len = 0; slong = VRepnPrecision (VLongRepn) / 8; sfloat = VRepnPrecision (VFloatRepn) / 8; spriv = graph->nfields * VRepnPrecision (graph->node_repn) / 8; nnodes = 0; for (i = 1; i <= graph->size; i++) { n = VGraphGetNode(graph, i); if (n == 0) continue; ++nnodes; /* Count the number of adjacencies : */ for (adj = n->base.head, nadj = 0; adj; adj = adj->next) nadj++; /* each node contains: * an index and the number of adjacencies * the private data area * the list of adjacencies * optionally reserve space for weights */ len += 2 * slong + nadj * slong + spriv; if (graph->useWeights) len += (nadj+1) * sfloat; }; *lengthp = len; assert(nnodes == graph->nnodes); /* for debugging */ graph->nnodes = nnodes; /* just to be safe for now... */ /* Temporarily prepend several attributes to the graph's list: */ if ((list = VGraphAttrList (graph)) == NULL) list = VGraphAttrList (graph) = VCreateAttrList (); VPrependAttr (list, VRepnAttr, VNumericRepnDict, VLongRepn, (VLong) graph->node_repn); VPrependAttr (list, VNNodeFieldsAttr, NULL, VLongRepn, (VLong) graph->nfields); VPrependAttr (list, VNGraphSizeAttr, NULL, VLongRepn, (VLong) graph->size); VPrependAttr (list, VNNodeWeightsAttr, NULL, VLongRepn, (VLong) graph->useWeights); return list; }
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; }
static VAttrList VImageEncodeAttrMethod (VPointer value, size_t *lengthp) { VImage image = value; VAttrList list; size_t length; #define OptionallyPrepend(value, name) \ if (value != 1) \ VPrependAttr (list, name, NULL, VLongRepn, (VLong) value) /* Temporarily prepend several attributes to the image's attribute list: */ if ((list = VImageAttrList (image)) == NULL) list = VImageAttrList (image) = VCreateAttrList (); VPrependAttr (list, VRepnAttr, VNumericRepnDict, VLongRepn, (VLong) VPixelRepn (image)); VPrependAttr (list, VNColumnsAttr, NULL, VLongRepn, (VLong) VImageNColumns (image)); VPrependAttr (list, VNRowsAttr, NULL, VLongRepn, (VLong) VImageNRows (image)); OptionallyPrepend (VImageNComponents (image), VNComponentsAttr); OptionallyPrepend (VImageNColors (image), VNColorsAttr); OptionallyPrepend (VImageNViewpoints (image), VNViewpointsAttr); OptionallyPrepend (VImageNFrames (image), VNFramesAttr); OptionallyPrepend (VImageNBands (image), VNBandsAttr); /* Compute the file space needed for the image's binary data: */ length = VImageNPixels (image); if (VPixelRepn (image) == VBitRepn) length = (length + 7) / 8; else length *= VPixelPrecision (image) / 8; *lengthp = length; return list; #undef OptionallyPrepend }
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 */
int main(int argc, char *argv[]) { static VArgVector in_files; static VString out_filename; static VString filename; static VShort minval = 0; static VFloat fwhm = 4.0; static VOptionDescRec options[] = { {"in", VStringRepn, 0, & in_files, VRequiredOpt, NULL, "Input files" }, {"out", VStringRepn, 1, & out_filename, VRequiredOpt, NULL, "Output file" }, {"design", VStringRepn, 1, (VPointer) &filename, VRequiredOpt, NULL, "Design file"}, { "fwhm", VFloatRepn, 1, (VPointer) &fwhm, VOptionalOpt, NULL, "FWHM of temporal Gaussian filter in seconds" }, {"minval", VShortRepn, 1, (VPointer) &minval, VOptionalOpt, NULL, "Signal threshold"} }; FILE *fp = NULL, *f = NULL; VStringConst in_filename; VString ifilename; VAttrList list = NULL, list1 = NULL; VAttrList out_list = NULL, history_list = NULL; VAttrListPosn posn; VImage design = NULL; ListInfo *linfo; VLong itr = 0; VFloat sigma = 0, tr = 0; int i, n, nimages; char prg_name[100]; char ver[100]; getLipsiaVersion(ver, sizeof(ver)); sprintf(prg_name, "vcolorglm V%s", ver); fprintf(stderr, "%s\n", prg_name); /* ** parse command line */ if(! VParseCommand(VNumber(options), options, & argc, argv)) { VReportUsage(argv[0], VNumber(options), options, NULL); exit(EXIT_FAILURE); } if(argc > 1) { VReportBadArgs(argc, argv); exit(EXIT_FAILURE); } /* ** read design matrix */ fp = VOpenInputFile(filename, TRUE); list1 = VReadFile(fp, NULL); if(! list1) VError("Error reading design file"); fclose(fp); n = 0; for(VFirstAttr(list1, & posn); VAttrExists(& posn); VNextAttr(& posn)) { if(VGetAttrRepn(& posn) != VImageRepn) continue; VGetAttrValue(& posn, NULL, VImageRepn, & design); if(VPixelRepn(design) != VFloatRepn /* && VPixelRepn(design) != VDoubleRepn */) continue; n++; break; } if(n == 0) VError(" design matrix not found "); /* ** get pre-coloring info */ if(VGetAttr(VImageAttrList(design), "repetition_time", NULL, VLongRepn, &itr) != VAttrFound) VError(" TR info missing in header"); tr = (float) itr / 1000.0; sigma = 0; if(tr > 0.001 && fwhm > 0.001) { fprintf(stderr, " TR: %.3f seconds\n", tr); sigma = fwhm / 2.35482; sigma /= tr; if(sigma < 0.1) { VWarning(" 'fwhm/sigma' too small (%.3f / %.3f), will be set to zero", fwhm, sigma); sigma = 0; } } /* ** Read each input file */ nimages = in_files.number; linfo = (ListInfo *) VMalloc(sizeof(ListInfo) * nimages); for(i = 0; i < nimages; i++) { in_filename = ((VStringConst *) in_files.vector)[i]; ifilename = VNewString(in_filename); fprintf(stderr, " file: %s\n", ifilename); list = GetListInfo(ifilename, &linfo[i]); /* Create history */ if(i == 0) { history_list = VReadHistory(&list); if(history_list == NULL) history_list = VCreateAttrList(); VPrependHistory(VNumber(options), options, prg_name, &history_list); } } /* ** GLM */ out_list = VRegression(linfo, nimages, minval, design, sigma, itr); /* ** Output: */ VPrependAttr(out_list, "history", NULL, VAttrListRepn, history_list); f = VOpenOutputFile(out_filename, TRUE); if(!f) VError(" error opening outout file %s", out_filename); if(! VWriteFile(f, out_list)) exit(1); fprintf(stderr, "%s: done.\n", argv[0]); return 0; }
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; }