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 */
void ReadImages (VString Name, VAttrList& Images, VAttrList& history_list) { FILE* file; /* input file */ VAttrList list, list1; /* attribute list */ VAttrListPosn pos; /* position in list */ VImage image; /* image in list */ /* initialize results */ Images = 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 */ list1 = VReadHistory(&list); if (list1==NULL) list1=VCreateAttrList(); history_list = VCopyAttrList(list1); /* extract images */ for (VFirstAttr (list, &pos); VAttrExists (&pos); VNextAttr (&pos)) { if (VGetAttrRepn (&pos) != VImageRepn) continue; if (!Images) Images = VCreateAttrList (); VGetAttrValue (&pos, NULL, VImageRepn, &image); VSetAttrValue (&pos, NULL, VImageRepn, NULL); VAppendAttr (Images, "image", NULL, VImageRepn, image); } if (!Images) VError ("Input file '%s' does not contain an image", Name); /* clean-up*/ VDestroyAttrList (list); fclose (file); } /* ReadImages */
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; }