VAttrList VReadFile (FILE *f, VReadFileFilterProc *filter) { VAttrList list; long offset; int i; /* Ensure that the correct FIL_Vista data file header is there: */ if (! ReadHeader (f)) return NULL; /* Read all attributes in the file: */ if (! (list = ReadAttrList (f))) return NULL; /* Swallow the delimiter and read the binary data following it: */ offset = 0; if (! ReadDelimiter (f) || ! ReadData (f, list, filter, &offset)) { VDestroyAttrList (list); return NULL; } /* Now we should be at the end of the file: */ i = fgetc (f); if (i != EOF) { ungetc (i, f); VWarning ("VReadFile: File continues beyond expected EOF"); } 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; }
void VDestroyBundle (VBundle b) { VDestroyAttrList (b->list); if (b->length > 0) VFree (b->data); VFree (b); }
static void FreeAttrValue (VStringConst routine, VAttrRec *a) { VTypeMethods *methods; switch (a->repn) { case VAttrListRepn: VDestroyAttrList (a->value); break; case VBundleRepn: VDestroyBundle (a->value); break; case VPointerRepn: case VStringRepn: break; default: if (! (methods = VRepnMethods (a->repn))) VError ("%s: %s attribute has invalid repn %d", routine, a->name, a->repn); (methods->destroy) (a->value); } }
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 VDestroyGraph (VGraph graph) { int i; /* destroy each node */ for (i = 1; i <= graph->size; i++) VDestroyNodeSimple(graph, i); /* destroy the table */ VFree (graph->table); graph->table = NULL; graph->size = 0; /* again, to make sure */ VDestroyAttrList(graph->attributes); VFree (graph); }
void VDestroyEdges (VEdges edges) { VEdge edge, next_edge; for (edge = edges->first; edge; edge = next_edge) { next_edge = edge->next; if (edge->free) VFree (edge->free); } if (edges->free) VFree (edges->free); VDestroyAttrList (edges->attributes); VFree (edges); }
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; }
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 */
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; }
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; }
void VDestroyImage (VImage image) { if (image == NULL) return; if (! (image->flags & VImageSingleAlloc)) { if (image->data != NULL) { VFree (image->data); } if ((VPointer) image->row_index != NULL) { VFree ((VPointer) image->row_index); } if ((VPointer) image->band_index != NULL) { VFree ((VPointer) image->band_index); } } if (VImageAttrList(image) != NULL) { VDestroyAttrList (VImageAttrList (image)); } if ((VPointer) image != NULL) { VFree ((VPointer) image); } }
static VAttrList ReadAttrList (FILE *f) { VAttrList sublist, list = VCreateAttrList (); VAttrRec *a; int ch = 0; size_t name_size; VBundle b; char buf[2], *str, name_buf[VMaxAttrNameLength + 1]; /* Swallow a { marking the start of the attribute list: */ if (fscanf (f, " %1s", buf) != 1 || buf[0] != '{') { VWarning ("VReadFile: Missing {"); goto Fail; } /* For each attribute up to the next "}": */ while (fscanf (f, " %[^}: \t\n]", name_buf) == 1) { name_size = strlen (name_buf); /* Read a : and the first character of the attribute's value: */ if (fscanf (f, " %1s", buf) != 1 || buf[0] != ':' || fscanf (f, " %1s", buf) != 1) { VWarning ("VReadFile: Invalid %s attribute", name_buf); goto Fail; } /* The first character of the value tells us whether its an attribute list, quoted string, or unquoted string: */ if (buf[0] == '{') { /* The attribute value is another list of attributes: */ ungetc ('{', f); if (! (sublist = ReadAttrList (f))) goto Fail; a = VMalloc (sizeof (VAttrRec) + name_size); a->value = sublist; a->repn = VAttrListRepn; } else { /* The value doesn't start with '{' -- parse a word or string: */ if (! (str = ReadString (f, buf[0], name_buf))) goto Fail; while ((ch = fgetc (f)) && (ch == ' ' || ch == '\t')) ; ungetc (ch, f); /* If the word is followed by an '{'... */ if (ch == '{') { /* ...then it's a typed value -- the word is it's type name and the { is the start of it's attribute list value. */ b = VCreateBundle (str, NULL, 0, NULL); if (! (sublist = ReadAttrList (f))) { VFree (b); goto Fail; } b->list = sublist; a = VMalloc (sizeof (VAttrRec) + name_size); a->repn = VBundleRepn; a->value = b; } else { /* ...otherwise store it as a simple string value: */ a = VMalloc (sizeof (VAttrRec) + name_size + strlen (str) + 1); a->repn = VStringRepn; a->value = a->name + name_size + 1; strcpy (a->value, str); } VFree(str); } /* Copy the attribute's name into the newly allocated node: */ strcpy (a->name, name_buf); /* Place the new node on the end of the growing attribute list: */ a->next = NULL; a->prev = list->prev; if (a->prev) a->prev->next = a; else list->next = a; list->prev = a; } /* Swallow the terminating "}": */ if (fscanf (f, " %1s", buf) != 1 || buf[0] != '}') { VWarning ("VReadFile: Missing }"); Fail: VDestroyAttrList (list); return NULL; } return list; }