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 FunctionalResize (VImage& Image, int Bands, int Rows, int Columns) { VImage zero; /* empty image */ /* resize image */ zero = VCreateImage (Bands, Rows, Columns, VShortRepn); VImageAttrList (zero) = VCopyAttrList (VImageAttrList (Image)); VFillImage (zero, VAllBands, 0); VDestroyImage (Image); Image = zero; } /* FunctionalResize */
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 */
VEdges VCopyEdges (VEdges src) { VEdges result; VEdge e; result = VCreateEdges (src->nrows, src->ncolumns, src->nedge_fields, src->npoint_fields); for (e = src->first; e != NULL; e = e->next) VAddEdge (result, e->edge_fields, e->npoints, e->point_index[0], e->closed, TRUE); if (VEdgesAttrList (result)) VDestroyAttrList (VEdgesAttrList (result)); if (VEdgesAttrList (src)) VEdgesAttrList (result) = VCopyAttrList (VEdgesAttrList (src)); return result; }
VImage VCopyImageAttrs (VImage src, VImage dest) { VAttrList list; if (src == dest) return dest; if (! dest) { dest = VCreateImage (VImageNBands (src), VImageNRows (src), VImageNColumns (src), VPixelRepn (src)); if (! dest) return NULL; } /* Clone the source image's attribute list if it isn't empty: */ if (! VAttrListEmpty (VImageAttrList (src))) { list = VImageAttrList (dest); VImageAttrList (dest) = VCopyAttrList (VImageAttrList (src)); } else if (! VAttrListEmpty (VImageAttrList (dest))) { list = VImageAttrList (dest); VImageAttrList (dest) = VCreateAttrList (); } else list = NULL; if (list) VDestroyAttrList (list); /* Preserve band interpretation attributes only if the source and destination images have the same number of bands: */ if (VImageNBands (src) > 1 && VImageNBands (dest) == VImageNBands (src)) { VImageNFrames (dest) = VImageNFrames (src); VImageNViewpoints (dest) = VImageNViewpoints (src); VImageNColors (dest) = VImageNColors (src); VImageNComponents (dest) = VImageNComponents (src); } else { VExtractAttr (VImageAttrList (dest), VFrameInterpAttr, NULL, VBitRepn, NULL, FALSE); VExtractAttr (VImageAttrList (dest), VViewpointInterpAttr, NULL, VBitRepn, NULL, FALSE); VExtractAttr (VImageAttrList (dest), VColorInterpAttr, NULL, VBitRepn, NULL, FALSE); VExtractAttr (VImageAttrList (dest), VComponentInterpAttr, NULL, VBitRepn, NULL, FALSE); VImageNComponents (dest) = VImageNColors (dest) = VImageNViewpoints (dest) = 1; VImageNFrames (dest) = VImageNBands (dest); } return dest; }
VGraph VCopyGraph (VGraph src) { VGraph dst; int i; dst = VCreateGraph (src->size, src->nfields, src->node_repn, src->useWeights); /* copy each used node in table */ for (i = 1; i <= src->size; i++) dst->table[i-1] = VCopyNodeDeep(src, VGraphGetNode(src, i)); dst->nnodes = src->nnodes; dst->lastUsed = src->lastUsed; if (VGraphAttrList (dst)) VDestroyAttrList (VGraphAttrList (dst)); if (VGraphAttrList (src)) VGraphAttrList (dst) = VCopyAttrList (VGraphAttrList (src)); return dst; }
VGraph VGraphExtractNodes (VGraph src) { VGraph dst; VAdjacency adj; VNode n; int i, j; /* create a destination graph much like src */ dst = VCreateGraph(src->size, src->nfields, src->node_repn, src->useWeights); /* copy selected nodes from src */ for (i = j = 1; i <= src->lastUsed; i++) { n = VGraphGetNode(src, i); if (n && n->base.hops) dst->table[j] = VCopyNodeShallow(src, n); }; /* set number of nodes used */ dst->nnodes = j-1; dst->lastUsed = j; /* now link nodes in new graph */ for (i = 1; i <= dst->lastUsed; i++) { n = VGraphGetNode(dst, i); if (n == 0) continue; j = VGraphLookupNode(src, n); if (j == 0) continue; n = VGraphGetNode(src, j); for (adj = n->base.head; adj; adj = adj->next) { n = VGraphGetNode(src, adj->id); j = VGraphLookupNode(dst, n); if (j) VGraphLinkNodes(dst, i, j); }; }; if (VGraphAttrList (dst)) VDestroyAttrList (VGraphAttrList (dst)); if (VGraphAttrList (src)) VGraphAttrList (dst) = VCopyAttrList (VGraphAttrList (src)); return dst; }
VAttrList VCopyAttrList (VAttrList list) { VAttrList new_list = VCreateAttrList (); size_t name_size, value_size; VAttrRec *old_a, *new_a; VBundle old_b, new_b; VTypeMethods *methods; /* For each node of the old list: */ for (old_a = list->next; old_a; old_a = old_a->next) { /* Compute the amount of storage needed for a copy of the node: */ name_size = strlen (old_a->name); value_size = (old_a->repn == VStringRepn) ? strlen ((VStringConst) old_a->value) + 1 : 0; /* Allocate that size and fill in the node's value: */ new_a = VMalloc (sizeof (VAttrRec) + name_size + value_size); strcpy (new_a->name, old_a->name); switch (new_a->repn = old_a->repn) { case VAttrListRepn: new_a->value = VCopyAttrList (old_a->value); break; case VBundleRepn: old_b = old_a->value; new_b = VCreateBundle (old_b->type_name, VCopyAttrList (old_b->list), old_b->length, NULL); if (old_b->length > 0) { new_b->data = VMalloc (old_b->length); memcpy (new_b->data, old_b->data, old_b->length); } new_a->value = new_b; break; case VPointerRepn: new_a->value = old_a->value; break; case VStringRepn: new_a->value = (VPointer) (new_a->name + name_size + 1); strcpy (new_a->value, old_a->value); break; default: if (methods = VRepnMethods (new_a->repn)) new_a->value = (methods->copy) (old_a->value); else VError ("VCopyAttrList: %s attribute has invalid repn %d", old_a->name, old_a->repn); } /* Append it to the new list: */ new_a->next = NULL; if (new_a->prev = new_list->prev) new_a->prev->next = new_a; if (! new_list->next) new_list->next = new_a; new_list->prev = new_a; } return new_list; }
VImage VCerebellum(VImage src) { VImage coronal, label_coronal; VImage sagital, label_sagital; VImage axial, label_axial; VImage bin0_image, bin1_image, label_image; VBit *bin0_pp, *src_pp; VUByte *ubyte_pp; int i, nbands, nrows, ncols, npixels, nl = 0; float x0, x1, x2; VString str; int b, r, c, cp, rp, bp, slice_extent, slice_origin, b0; nbands = VImageNBands(src); nrows = VImageNRows(src); ncols = VImageNColumns(src); npixels = nbands * nrows * ncols; str = VMalloc(80); if(VGetAttr(VImageAttrList(src), "cp", NULL, VStringRepn, (VPointer) &str) != VAttrFound) VError(" attribute 'cp' not found"); sscanf(str, "%f %f %f", &x0, &x1, &x2); bp = (int) VRint((double) x2); rp = (int) VRint((double) x1); cp = (int) VRint((double) x0); if(VGetAttr(VImageAttrList(src), "extent", NULL, VStringRepn, (VPointer) &str) != VAttrFound) VError(" attribute 'extent' not found"); sscanf(str, "%f %f %f", &x0, &x1, &x2); slice_extent = (int) VRint((double) x2); if(VGetAttr(VImageAttrList(src), "origin", NULL, VStringRepn, (VPointer) &str) != VAttrFound) VError(" attribute 'origin' not found"); sscanf(str, "%f %f %f", &x0, &x1, &x2); slice_origin = (int) VRint((double) x2); /* erode */ bin1_image = VDTErode(src, NULL, (VDouble) 2.0); /* readdress to coronal slices and remove small 2D components */ coronal = VCreateImage(1, nbands, ncols, VBitRepn); label_coronal = VCreateImage(1, nbands, ncols, VUByteRepn); ubyte_pp = (VUByte *) VImageData(label_coronal); for(i = 0; i < (nbands * ncols); i++) *ubyte_pp++ = 0; for(r = rp; r < nrows; r++) { bin0_pp = (VBit *) VImageData(coronal); for(i = 0; i < (nbands * ncols); i++) *bin0_pp++ = 0; for(b = 0; b < nbands; b++) { for(c = 0; c < ncols; c++) { VPixel(coronal, 0, b, c, VBit) = VPixel(bin1_image, b, r, c, VBit); } } label_coronal = VLabelImage2d(coronal, label_coronal, 8, VUByteRepn, &nl); VDeleteCereb(label_coronal, &coronal, 110, (int) 0); for(b = 0; b < nbands; b++) { for(c = 0; c < ncols; c++) { if(VPixel(coronal, 0, b, c, VBit) == 0) VPixel(bin1_image, b, r, c, VBit) = 0; } } } /* readdress to sagital slices and remove small 2D components */ sagital = VCreateImage(1, nbands, nrows, VBitRepn); label_sagital = VCreateImage(1, nbands, nrows, VUByteRepn); ubyte_pp = (VUByte *) VImageData(label_sagital); for(i = 0; i < (nbands * nrows); i++) *ubyte_pp++ = 0; for(c = 0; c < ncols; c++) { if(ABS(c - 80) < 10) continue; bin0_pp = (VBit *) VImageData(sagital); for(i = 0; i < (nbands * nrows); i++) *bin0_pp++ = 0; for(b = 0; b < nbands; b++) { for(r = 0; r < nrows; r++) { VPixel(sagital, 0, b, r, VBit) = VPixel(bin1_image, b, r, c, VBit); } } label_sagital = VLabelImage2d(sagital, label_sagital, (int) 8, VUByteRepn, &nl); VDeleteCereb(label_sagital, &sagital, 115, cp); for(b = 0; b < nbands; b++) { for(r = 0; r < nrows; r++) { if(VPixel(sagital, 0, b, r, VBit) == 0) VPixel(bin1_image, b, r, c, VBit) = 0; } } } /* readdress to axial slices and remove small 2D components */ axial = VCreateImage(1, nrows, ncols, VBitRepn); label_axial = VCreateImage(1, nrows, ncols, VUByteRepn); ubyte_pp = (VUByte *) VImageData(label_axial); for(i = 0; i < (nrows * ncols); i++) *ubyte_pp++ = 0; /* for (b=bp; b<nbands; b++) { */ for(b = 105; b < nbands; b++) { bin0_pp = (VBit *) VImageData(axial); for(i = 0; i < (nrows * ncols); i++) *bin0_pp++ = 0; for(r = 0; r < nrows; r++) { for(c = 0; c < ncols; c++) { VPixel(axial, 0, r, c, VBit) = VPixel(bin1_image, b, r, c, VBit); } } label_sagital = VLabelImage2d(axial, label_axial, (int) 8, VUByteRepn, &nl); VDeleteCereb(label_axial, &axial, 0, 0); for(r = 0; r < nrows; r++) { for(c = 0; c < ncols; c++) { if(VPixel(axial, 0, r, c, VBit) == 0) VPixel(bin1_image, b, r, c, VBit) = 0; } } } /* remove everything below slice extent */ b0 = slice_extent + slice_origin; npixels = nrows * ncols; for(b = b0; b < nbands; b++) { bin0_pp = (VBit *) VPixelPtr(bin1_image, b, 0, 0); for(i = 0; i < npixels; i++) *bin0_pp++ = 0; } /* dilate */ bin0_image = VDTDilate(bin1_image, NULL, (VDouble) 3.0); npixels = nrows * ncols; for(b = 0; b < bp; b++) { bin0_pp = (VBit *) VPixelPtr(bin0_image, b, 0, 0); src_pp = (VBit *) VPixelPtr(src, b, 0, 0); for(i = 0; i < npixels; i++) *bin0_pp++ = *src_pp++; } for(b = bp; b < nbands; b++) { bin0_pp = (VBit *) VPixelPtr(bin0_image, b, 0, 0); src_pp = (VBit *) VPixelPtr(src, b, 0, 0); for(i = 0; i < npixels; i++) { if(*src_pp == 0) *bin0_pp = 0; src_pp++; bin0_pp++; } } /* ** remove small remaining components */ label_image = VLabelImage3d(bin0_image, NULL, (int) 26, VShortRepn, &nl); bin0_image = VSelectBig(label_image, bin0_image); VImageAttrList(bin0_image) = VCopyAttrList(VImageAttrList(src)); return bin0_image; }