Exemple #1
0
void VDestroyBundle (VBundle b)
{
    VDestroyAttrList (b->list);
    if (b->length > 0)
	VFree (b->data);
    VFree (b);
}
Exemple #2
0
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);
}
Exemple #3
0
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);
}
Exemple #4
0
void VListDestroy (VList vlist, void (*item_free) ())
{
   VPointer free_me;

   vlist->current = vlist->head->next;
   while ( vlist->current != vlist->tail )
   {
       free_me = VListRemove (vlist);
       (*item_free)(free_me);
   }

   VFree (vlist->head);
   VFree (vlist->tail);
   VFree (vlist);
}
Exemple #5
0
VGraph VCreateGraph (int size, int nfields, VRepnKind repn, int useW)
{
  VGraph graph;

  /* Check parameters: */
  if (size < 1  || nfields < 1)
    VWarning ("VCreateGraph: Invalid number of nodes or fields.");

  /* Allocate memory for the VGraph, and the node table: */
  graph = VMalloc (sizeof (VGraphRec));
  if (graph == NULL) return NULL;

  graph->table = VCalloc(size, sizeof(VNode));
  if (graph->table == NULL) {
    VFree(graph);
    return NULL;
  };

  /* Initialize the VGraph: */
  graph->nnodes = 0;
  graph->nfields = nfields;
  graph->node_repn = repn;
  graph->attributes = VCreateAttrList ();
  graph->lastUsed = 0;
  graph->size = size;
  graph->useWeights = useW;
  graph->iter = 0;

  return graph;
}
Exemple #6
0
void VDestroyNode(VGraph graph, int i)
{
  VAdjacency p, q;
  VNode n;
    
  n = VGraphGetNode(graph, i); if (n == 0) return;
    
  /* destroy adjacency list */
  for (p = n->base.head; p; p = q)  {
    /* remove connection from other node to this node */
    VGraphUnlinkNodes(graph, p->id, i);
    q = p->next; VFree(p);
  };
  VFree(n);
  VGraphGetNode(graph, i) = 0;
  graph->nnodes--;
  assert(graph->nnodes >= 0);
}
Exemple #7
0
static void VDestroyNodeSimple(VGraph graph, int i)
     /* simple deletion: just look at structures of this node */
{
  VAdjacency p, q;
  VNode n;
    
  n = VGraphGetNode(graph, i);
  if (n == NULL) return;
    
  /* destroy adjacency list */
  for (p = n->base.head; p; p = q)  {
    q = p->next; VFree(p);
  };
  VFree(n);

  VGraphGetNode(graph, i) = 0;
  graph->nnodes--;
  assert(graph->nnodes >= 0);
}
Exemple #8
0
static int growGraph (VGraph graph)
     /* note that we grow just a pointer table */
{
  int newsize = (graph->size * 3) / 2;
  VNode *t = VCalloc(newsize, sizeof(VNode));
  if (t == 0) return 0;
  memcpy(t, graph->table, graph->size * sizeof(VNode));
  VFree(graph->table); graph->table = t;
  graph->size = newsize; graph->nnodes = newsize; return newsize;
}
Exemple #9
0
void VNodeRemoveLinks(VGraph graph, int pos)
{
	VNode n;
	VAdjacency adj, nxt;

	if (VGraphNodeIsFree(graph, pos)) return;
	n = VGraphGetNode(graph, pos);
	for (adj = n->base.head; adj; adj = nxt)  {
		nxt = adj->next; VFree(adj);
	};
	n->base.head = 0;
}
Exemple #10
0
VGraph
VGraphHemi(VGraph src,VLong hemi)
{
  VString str;
  VGraph dest=NULL;
  int i,j,n,ncols,half=0;
  float x,y,z,val=0;
  VNode node,node1;
  int *table;

  half = 80;   /* standard coordinate for inter-hemisperic cleft */

  if (VGetAttr (VGraphAttrList (src), "ca", NULL,VStringRepn, (VPointer) & str) == VAttrFound) {
    sscanf(str,"%f %f %f",&x,&y,&z);
    ncols = (int) x;
    half  = ncols/2;
  }

  table = (int *) VMalloc(sizeof(int) * (src)->lastUsed + 1);
  for (i=0; i<= src->lastUsed; i++) table[i] = 0;

  n = (src)->lastUsed/2;
  dest = VCreateGraph(n,VGraphNFields(src),VNodeRepn(src),src->useWeights);
 
  for (i=1; i<=(src)->lastUsed; i++) {
    node = (VNode) VGraphGetNode (src,i);
    if (node == 0) continue;
    VReadNode((src),node,&x,&y,&z,&val);

    if ((hemi == 0 && x <= half) || (hemi == 1 && x > half)) {
      n = VGraphAddNode(dest,(VNode) node);
      table[i] = n;
    }
  }


  for (i=1; i<=(src)->lastUsed; i++) {
    node = (VNode) VGraphGetNode (src,i);
    if (node == 0) continue;

    for (j=1; j<=(src)->lastUsed; j++) {
      node1 = (VNode) VGraphGetNode (src,j);
      if (node1 == 0) continue;
      if (table[i] == 0 || table[j] == 0) continue;

      if (VGraphIsAdjacent(src,i,j) == TRUE) {
	VGraphLinkNodes(dest,table[i],table[j]);
      }
    }
  }
  VFree(table);
  return dest;
}
Exemple #11
0
void VDestroyAttrList (VAttrList list)
{
    VAttrRec *a, *a_next;

    if (! list) {
	VWarning ("VDestroyAttrList: Called with NULL list");
	return;
    }

    /* For each attribute in the list: */
    for (a = list->next; a; a = a_next) {
		a_next = a->next;

		/* Free any storage used for the attribute's value: */
		FreeAttrValue ("VDestroyAttrList", a);

		/* Free the attribute record itself: */
		VFree (a);
    }
    VFree (list);
}
Exemple #12
0
VPointer VRealloc (VPointer p, size_t size)
{
    if (size == 0) {
        VFree (p);
        return NULL;
    }
    if (! p)
        return VMalloc (size);
    if (! (p = (VPointer) realloc (p, size)))
        VSystemError ("VRealloc: Memory allocation failure");
    return p;
}
Exemple #13
0
static int growGraphPos(VGraph graph, int pos)
/* note that we grow just a pointer table */
{
  VNode *t;

  int newsize = (graph->size * 3) / 2;
  if (pos > newsize) newsize = pos+1;
  t = (VNode *)VCalloc(newsize, sizeof(VNode));
  if (t == 0) return 0;
  memcpy(t, graph->table, graph->size * sizeof(VNode));
  VFree(graph->table); graph->table = t;
  graph->size = newsize; graph->nnodes = newsize; return newsize;
}
Exemple #14
0
void VListConcat (VList vlist1, VList vlist2)
{
    VNodePtrType free_me, free_me_too;

    free_me = vlist1->tail;
    free_me_too = vlist2->head;

    vlist1->tail->prev->next = vlist2->head->next;
    vlist2->head->next->prev = vlist1->tail->prev;

    if ( vlist1->current == vlist1->tail )
	/* current pointer of vlist1 points beyond end,
	   set it to first node of vlist2 */
	vlist1->current = vlist2->head->next;

    vlist1->tail = vlist2->tail;
    vlist1->count += vlist2->count;

    VFree (free_me);
    VFree (free_me_too);
    VFree (vlist2);
}
Exemple #15
0
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);
  }
}
Exemple #16
0
int VGraphResizeFields (VGraph graph, int newfields)
{
  VNode o, n;
  int i;
  int nsize = sizeof(VNodeBaseRec) + (newfields * VRepnPrecision(graph->node_repn)) / 8;
  int osize = VNodeSize(graph);
  if (newfields <= graph->nfields) return TRUE;
  for (i = 1; i <= graph->lastUsed; i++)  {
    if (VGraphNodeIsFree(graph, i)) continue;
    o = VGraphGetNode(graph, i);
    n = VCalloc(1, nsize);
    memcpy(n, o, osize);
    VGraphGetNode(graph, i) = n; VFree(o);
  };
  graph->nfields = newfields;
  return TRUE;
}    
Exemple #17
0
void VDeleteAttr (VAttrListPosn *posn)
{
    VAttrRec *a = posn->ptr;

    /* Remove it from the list: */
    if (a->next)
	a->next->prev = a->prev;
    if (a->prev)
	a->prev->next = a->next;
    if (posn->list->next == a)
	posn->list->next = a->next;
    if (posn->list->prev == a)
	posn->list->prev = a->prev;

    /* Make posn point to the next attribute, or nothing: */
    posn->ptr = a->next;

    VFree (a);
}
Exemple #18
0
int VGraphUnlinkNodes (VGraph graph, int a, int b)
{
  VNode n;
  VAdjacency adj, prev;
    
  if (VGraphNodeIsFree(graph, a) || VGraphNodeIsFree(graph, b)) return FALSE;
  n = VGraphGetNode(graph, a);
  prev = 0;
  for (adj = n->base.head; adj; adj = adj->next)  {
    if (adj->id == (unsigned int)b)  {
      if (prev)
	prev->next = adj->next;
      else
	n->base.head = adj->next;
      VFree(adj);
      return TRUE;
    };
    prev = adj;
  };
  return FALSE;
}    
Exemple #19
0
VPointer VListRemove (VList vlist)
{
    VPointer return_me;
    VNodePtrType free_me;

    return_me = vlist->current->item;

    if ((vlist->current == vlist->tail)
	|| (vlist->current == vlist->head) )
	/* current pointer before beginning or beyond end, no action */
	;
    else {  /* free current node */

	vlist->current->prev->next = vlist->current->next;
	vlist->current->next->prev = vlist->current->prev;
	free_me = vlist->current;
	vlist->current = vlist->current->next;

	VFree (free_me);
	vlist->count--;
    }

    return return_me;
}
Exemple #20
0
/* Parse command without license information */
VBoolean VParseCommand_nl (int noptions, VOptionDescRec options[],
			int *argc, char **argv)
{
  int arg, nvalues, i, j;
  char *cp;
  VBoolean *opts_seen, result = TRUE;
  VOptionDescRec *opt, *opt_t;

  /* Note the program's name: */
  VSetProgramName (argv[0]);

  /* Allocate storage for a set of flags indicating which
     arguments have been seen: */
  opts_seen = VCalloc (noptions, sizeof (VBoolean));

  /* Initialize any "found" flags to false, and the number field of any
     VArgVector values to zero: */
  for (opt = options + noptions - 1; opt >= options; opt--) {
    if (opt->found)
      *opt->found = FALSE;
    if (opt->number == 0 && opt->value)
      ((VArgVector *) opt->value)->number = 0;
  }

  /* For each argument supplied with the command: */
  for (arg = 1; arg < *argc; ) {
    cp = argv[arg++];

    /* If it doesn't start with - it can't be an option: */
    if (cp[0] != '-' || cp[1] == 0)
      continue;

    /* Check for -help: */
    if (strcmp (cp + 1, "help") == 0) {

      /* If found, return FALSE to force printing of usage info: */
      *argc = 1;
      return FALSE;
    }

    /* Look up the argument in the list of options: */
    i = strlen (cp + 1);
    opt = NULL;
    for (opt_t = options + noptions - 1; opt_t >= options; opt_t--) {
      if (strncmp (cp + 1, opt_t->keyword, i) != 0)
	continue;	/* not this one */
      if (i == strlen (opt_t->keyword)) {
	opt = opt_t;
	break;		/* an exact match */
      }
      if (opt)
	goto NextArg;	/* already matched another prefix */
      opt = opt_t;	/* note a prefix match */
    }

    /* If the argument isn't recognized, skip it: */
    if (! opt)
      goto NextArg;	/* not recognized */

    /* Remove it from the list of command arguments: */
    argv[arg - 1] = 0;

    /* Ensure that the option has not already been seen: */
    if (opts_seen[opt - options]) {
      fprintf (stderr,
	       "%s: Duplicate -%s option; ignoring all but last.\n",
	       argv[0], opt->keyword);

      /* If it has been seen, delete its previous value: */
      if (opt->number == 0) {
	VFree (((VArgVector *) opt->value)->vector);
	((VArgVector *) opt->value)->number = 0;
      }
    } else opts_seen[opt - options] = TRUE;

    /* Swallow any value(s) that follow: */
    switch (opt->repn) {

    case VBitRepn:
    case VUByteRepn:
    case VSByteRepn:
    case VShortRepn:
    case VLongRepn:
    case VFloatRepn:
    case VDoubleRepn:
    case VBooleanRepn:
    case VStringRepn:
      nvalues = ParseArgValues (& arg, *argc, argv, opt);
      break;

    default:
      VError ("Parsing of command options with %s values "
	      "is not implemented", VRepnName (opt->repn));
      nvalues = 0;	/* to quiet lint */
    }

    /* Ensure that the expected number of arguments was found: */
    if (opt->number && nvalues != opt->number) {

      /* Either we encountered an argument we couldn't parse, or
	 we used up all arguments before finding the expected number
	 of them: */
      fprintf (stderr, "%s: Option -%s ", argv[0], opt->keyword);
      if (arg < *argc)
	fprintf (stderr, "has incorrect value %s.\n", argv[arg]);
      else if (opt->number > 1)
	fprintf (stderr, "requires %d values; found only %d.\n",
		 opt->number, nvalues);
      else fprintf (stderr, "requires a value.\n");
      result = FALSE;
      break;
    }
    if (opt->number == 0)
      ((VArgVector *) opt->value)->number = nvalues;

    /* Note that a value was successfully obtained for this option: */
    if (opt->found)
      *(opt->found) = TRUE;

  NextArg: ;
  }

  /* Ensure that each mandatory option was seen: */
  for (i = 0; i < noptions; i++)
    if (options[i].found == VRequiredOpt && ! opts_seen[i]) {
      fprintf (stderr, "%s: Option -%s must be specified.\n",
	       argv[0], options[i].keyword);
      result = FALSE;
    }
  VFree ((VPointer) opts_seen);

  /* Squeeze together the remaining arguments in argv: */
  for (i = j = 1; i < *argc; i++)
    if (argv[i])
      argv[j++] = argv[i];
  *argc = j;

  return result;
}
Exemple #21
0
VBoolean VParseCommand (int noptions, VOptionDescRec options[],
			int *argc, char **argv)
{
  int arg, nvalues, i, j;
  char *cp;
  VBoolean *opts_seen, result = TRUE;
  VOptionDescRec *opt, *opt_t;

  /* Note the program's name: */
  VSetProgramName (argv[0]);

  /* Allocate storage for a set of flags indicating which
     arguments have been seen: */
  opts_seen = VCalloc (noptions, sizeof (VBoolean));

  /* Initialize any "found" flags to false, and the number field of any
     VArgVector values to zero: */
  for (opt = options + noptions - 1; opt >= options; opt--) {
    if (opt->found)
      *opt->found = FALSE;
    if (opt->number == 0 && opt->value)
      ((VArgVector *) opt->value)->number = 0;
  }

 /* For each argument supplied with the command: */
  for (arg = 1; arg < *argc; ) {
    cp = argv[arg++];

    /* If it doesn't start with - it can't be an option: */
    if (cp[0] != '-' || cp[1] == 0)
      continue;

   /* Check for -help: */
    if (strcmp (cp + 1, "license") == 0) {

    char* license="This program is free software; you can redistribute it and/or\n modify it under the terms of the GNU General Public License\n as published by the Free Software Foundation; either version 2\n of the License, or (at your option) any later version.\n This program is distributed in the hope that it will be useful,\n but WITHOUT ANY WARRANTY; without even the implied warranty of\n MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n GNU General Public License for more details.\n\n You should have received a copy of the GNU General Public License\n along with this program; if not, write to the Free Software\n Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.\n";


    fprintf(stderr,"%s\n",license);
    exit(0);
    }
  }


  /* For each argument supplied with the command: */
  for (arg = 1; arg < *argc; ) {
    cp = argv[arg++];

    /* If it doesn't start with - it can't be an option: */
    if (cp[0] != '-' || cp[1] == 0)
      continue;

    /* Check for -help: */
    if (strcmp (cp + 1, "help") == 0) {

      /* If found, return FALSE to force printing of usage info: */
      *argc = 1;
      return FALSE;
    }

    /* Look up the argument in the list of options: */
    i = strlen (cp + 1);
    opt = NULL;
    for (opt_t = options + noptions - 1; opt_t >= options; opt_t--) {
      if (strncmp (cp + 1, opt_t->keyword, i) != 0)
	continue;	/* not this one */
      if (i == strlen (opt_t->keyword)) {
	opt = opt_t;
	break;		/* an exact match */
      }
      if (opt)
	goto NextArg;	/* already matched another prefix */
      opt = opt_t;	/* note a prefix match */
    }

    /* If the argument isn't recognized, skip it: */
    if (! opt)
      goto NextArg;	/* not recognized */

    /* Remove it from the list of command arguments: */
    argv[arg - 1] = 0;

    /* Ensure that the option has not already been seen: */
    if (opts_seen[opt - options]) {
      fprintf (stderr,
	       "%s: Duplicate -%s option; ignoring all but last.\n",
	       argv[0], opt->keyword);

      /* If it has been seen, delete its previous value: */
      if (opt->number == 0) {
	VFree (((VArgVector *) opt->value)->vector);
	((VArgVector *) opt->value)->number = 0;
      }
    } else opts_seen[opt - options] = TRUE;

    /* Swallow any value(s) that follow: */
    switch (opt->repn) {

    case VBitRepn:
    case VUByteRepn:
    case VSByteRepn:
    case VShortRepn:
    case VLongRepn:
    case VFloatRepn:
    case VDoubleRepn:
    case VBooleanRepn:
    case VStringRepn:
      nvalues = ParseArgValues (& arg, *argc, argv, opt);
      break;

    default:
      VError ("Parsing of command options with %s values "
	      "is not implemented", VRepnName (opt->repn));
      nvalues = 0;	/* to quiet lint */
    }

    /* Ensure that the expected number of arguments was found: */
    if (opt->number && nvalues != opt->number) {

      /* Either we encountered an argument we couldn't parse, or
	 we used up all arguments before finding the expected number
	 of them: */
      fprintf (stderr, "%s: Option -%s ", argv[0], opt->keyword);
      if (arg < *argc)
	fprintf (stderr, "has incorrect value %s.\n", argv[arg]);
      else if (opt->number > 1)
	fprintf (stderr, "requires %d values; found only %d.\n",
		 opt->number, nvalues);
      else fprintf (stderr, "requires a value.\n");
      result = FALSE;
      break;
    }
    if (opt->number == 0)
      ((VArgVector *) opt->value)->number = nvalues;

    /* Note that a value was successfully obtained for this option: */
    if (opt->found)
      *(opt->found) = TRUE;

  NextArg: ;
  }

  /* Ensure that each mandatory option was seen: */
  for (i = 0; i < noptions; i++)
    if (options[i].found == VRequiredOpt && ! opts_seen[i]) {
      fprintf (stderr, "%s: Option -%s must be specified.\n",
	       argv[0], options[i].keyword);
      result = FALSE;
    }
  VFree ((VPointer) opts_seen);

  /* Squeeze together the remaining arguments in argv: */
  for (i = j = 1; i < *argc; i++)
    if (argv[i])
      argv[j++] = argv[i];
  *argc = j;

  return result;
}
Exemple #22
0
VBoolean VWriteFile (FILE *f, VAttrList list)
{
    DataBlock *db;
    VBundle b;
    VTypeMethods *methods;
    VRepnKind repn;
    VPointer value, ptr;
    VBoolean result, free_it;
    VList data_list;

    /* Write the FIL_Vista data file header, attribute list, and delimeter
       while queuing on data_list any binary data blocks to be written: */
    long offset = 0;
    data_list = VListCreate ();
    FailTest (fprintf (f, "%s %d ", VFileHeader, VFileVersion));
    if (! WriteAttrList (f, list, 1, &data_list, &offset)) {
	VListDestroy (data_list, VFree);
	return FALSE;
    }
    FailTest (fputs ("\n" VFileDelimiter, f));
    fflush (f);

    /* Traverse data_list to write the binary data blocks: */
    for (db = VListFirst (data_list); db; db = VListNext (data_list)) {
	repn = VGetAttrRepn (& db->posn);
	if (repn == VBundleRepn) {

	    /* A typed value includes its binary data block explicitly: */
	    VGetAttrValue (& db->posn, NULL, VBundleRepn, & b);
	    ptr = b->data;
	    free_it = FALSE;

	} else {

	    /* For any other representation, obtain the binary data block
	       from its encode_data method: */
	    VGetAttrValue (& db->posn, NULL, repn, & value);
	    methods = VRepnMethods (repn);
	    ptr = (methods->encode_data)
		(value, db->list, db->length, & free_it);
	    if (! ptr)
		goto Fail;
	}

	/* Write the binary data and free the buffer containing it if it was
	   allocated temporarily by an encode_data method: */
	if (db->length > 0) {
	    result = fwrite (ptr, 1, db->length, f) == db->length;
	    if (free_it)
		VFree (ptr);
	    if (! result)
		goto Fail;
	}
    }
    VListDestroy (data_list, VFree);
    return TRUE;

Fail:
    VWarning ("VWriteFile: Write to stream failed");
    VListDestroy (data_list, VFree);
    return FALSE;
}
Exemple #23
0
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;
}
Exemple #24
0
/*!
\fn void VDeriche2d(VImage src,VFloat alpha,VImage *gradr,VImage *gradc);
\param src     input image (ubyte repn)
\param alpha   parameter controlling edge strength
\param *gradr  output gradient in row direction (float repn)
\param *gradc  output gradient in column direction (float repn)
*/
void
VDeriche2d (VImage src,VFloat alpha,VImage *gradr,VImage *gradc)
{
  int b,r,c;
  int nbands,nrows,ncols,npixels,len;
  double s,a,a0,a1,a2,a3,b1,b2,exp_alpha;
  float *left,*right;
  
  nbands  = VImageNBands (src);
  nrows   = VImageNRows (src);
  ncols   = VImageNColumns (src);
  npixels = nbands * nrows * ncols;

  len = (ncols > nrows) ? ncols : nrows;

  left  = (float *) VMalloc(sizeof(float) * len);
  right = (float *) VMalloc(sizeof(float) * len);

 
  exp_alpha = exp((double) ( - alpha));
  a  = 1.0 * exp_alpha;
  b1 = -2.0 * exp_alpha;
  b2 = exp((double) (-2.0 * alpha));

  s = ((1.0 - exp_alpha) * (1.0 - exp_alpha)) / (1.0 + 2.0 * alpha * exp_alpha - b2);

  a0 = s;
  a1 = s * (alpha - 1.0) * exp_alpha;
  a2 = a1 - s * b1;
  a3 = - s * b2;


  /* col-grad */

  *gradc = VCreateImage(nbands,nrows,ncols,VFloatRepn);

  for (b=0; b<nbands; b++) {
    for (r=0; r<nrows; r++) {

      /* left-to-right */
      left[0] = 0;
      left[1] = 0;
      for (c=2; c<ncols; c++) {
	left[c] = (VFloat) VGetPixel(src,b,r,c-1) 
	  - b1 * left[c-1]  - b2 * left[c-2];
      }

      /* right-to-left */
      right[ncols-1] = 0;
      right[ncols-2] = 0;
      for (c=ncols-3; c>=0; c--) {
	right[c] = (VFloat) VGetPixel(src,b,r,c+1) 
	  - b1 * right[c+1]  - b2 * right[c+2];
      }

      /* combine */

      for (c=0; c<ncols; c++) {
	VPixel(*gradc,b,r,c,VFloat) =  a * (left[c] - right[c]);
      }
    }
  }

  /* row-smooth */

  for (b=0; b<nbands; b++) {
    for (c=0; c<ncols; c++) {

      /* left-to-right */
      left[0] = 0;
      left[1] = 0;
      for (r=2; r<nrows; r++) {
	left[r] = 
	    a0 * (VFloat) VPixel(*gradc,b,r,c,VFloat)
	  + a1 * (VFloat) VPixel(*gradc,b,r-1,c,VFloat)
	  - b1 * left[r-1] - b2 * left[r-2];
      }

      /* right-to-left */
      right[nrows-1] = 0;
      right[nrows-2] = 0;

      for (r=nrows-3; r>=0; r--) {
	right[r] = 
	    a2 * (VFloat) VPixel(*gradc,b,r+1,c,VFloat)
	  + a3 * (VFloat) VPixel(*gradc,b,r+2,c,VFloat)
	  - b1 * right[r+1] - b2 * right[r+2];
      }
      /* combine */

      for (r=0; r<nrows; r++) {
	VPixel(*gradc,b,r,c,VFloat) += left[r] + right[r];
      }
    }
  }



  /* 
  ** row-grad 
  */
  *gradr = VCreateImage(nbands,nrows,ncols,VFloatRepn);

  /* row-deriv */
  for (b=0; b<nbands; b++) {
    for (c=0; c<ncols; c++) {

      /* left-to-right */
      left[0] = 0;
      left[1] = 0;
      for (r=2; r<nrows; r++) {
	left[r] = (VFloat) VGetPixel(src,b,r-1,c) 
	  - b1 * left[r-1]  - b2 * left[r-2];
      }

      /* right-to-left */
      right[ncols-1] = 0;
      right[ncols-2] = 0;
      for (r=nrows-3; r>=0; r--) {
	right[r] = (VFloat) VGetPixel(src,b,r+1,c) 
	  - b1 * right[r+1]  - b2 * right[r+2];
      }

      /* combine */

      for (r=0; r<nrows; r++) {
	VPixel(*gradr,b,r,c,VFloat) =  a * (left[r] - right[r]);
      }
    }
  }

  /* col-smooth */

  for (b=0; b<nbands; b++) {
    for (r=0; r<nrows; r++) {

      /* left-to-right */
      left[0] = 0;
      left[1] = 0;
      for (c=2; c<ncols; c++) {
	left[c] = 
	    a0 * (VFloat) VPixel(*gradr,b,r,c,VFloat)
	  + a1 * (VFloat) VPixel(*gradr,b,r,c-1,VFloat)
	  - b1 * left[c-1] - b2 * left[c-2];
      }

      /* right-to-left */
      right[ncols-1] = 0;
      right[ncols-2] = 0;

      for (c=ncols-3; c>=0; c--) {
	right[r] = 
	    a2 * (VFloat) VPixel(*gradr,b,r,c+1,VFloat)
	  + a3 * (VFloat) VPixel(*gradr,b,r,c+2,VFloat)
	  - b1 * right[c+1] - b2 * right[c+2];
      }
      /* combine */

      for (c=0; c<ncols; c++) {
	VPixel(*gradr,b,r,c,VFloat) += left[c] + right[c];
      }
    }
  }


  VFree(left);
  VFree(right);
}
Exemple #25
0
static void SetAttr (VAttrListPosn *posn, VDictEntry *dict,
		     VRepnKind repn, va_list *args)
{
    size_t old_value_size, new_value_size, name_size;
    VPointer value;
    VAttrRec *a = posn->ptr;

    /* Determine the amount of storage needed to record the new value. In some
       cases, this requires first encoding the new value as a string. */
    name_size = strlen (a->name);
    switch (repn) {

    case VBitRepn:
    case VUByteRepn:
    case VSByteRepn:
    case VShortRepn:
    case VLongRepn:
    case VFloatRepn:
    case VDoubleRepn:
    case VBooleanRepn:
    case VStringRepn:
	if (repn == VStringRepn && ! dict)
	    value = (VPointer) va_arg (*args, VStringConst);
	else value = (VPointer) Encode (dict, repn, args);
	new_value_size = strlen (value) + 1;
	break;

    default:
	value = va_arg (*args, VPointer);
	new_value_size = 0;
    }

    /* Is enough storage allocated for it in the existing attribute node? */
    switch (a->repn) {

    case VStringRepn:
	old_value_size = strlen (a->value) + 1;
	break;

    default:
	old_value_size = 0;
    }
    if (old_value_size < new_value_size) {

	/* It exists, but it's too small: */
	a = VMalloc (sizeof (VAttrRec) + name_size + new_value_size);
	a->next = posn->ptr->next;
	a->prev = posn->ptr->prev;
	if (a->next)
	    a->next->prev = a;
	else posn->list->prev = a;
	if (a->prev)
	    a->prev->next = a;
	else posn->list->next = a;
	strcpy (a->name, posn->ptr->name);
	VFree (posn->ptr);
	posn->ptr = a;
    }

    /* Copy in the attribute's new value: */
    switch (repn) {

    case VBitRepn:
    case VUByteRepn:
    case VSByteRepn:
    case VShortRepn:
    case VLongRepn:
    case VFloatRepn:
    case VDoubleRepn:
    case VBooleanRepn:
    case VStringRepn:
	a->repn = VStringRepn;
	a->value = a->name + name_size + 1;
	strcpy (a->value, value);
	break;

    default:
	a->repn = repn;
	a->value = value;
    }
}
Exemple #26
0
VImage VScaleIntensity(VImage src, double white, double black)
/* scale any input image to VUByte,
   mapping white (black) percent of the voxel to white (black) */
{
	int x, y, z, nx, ny, nz, i, range, maxshort;
	unsigned int lb, ub, limit, sum, *histo;
	double m, b, max, min, mean, var, v;
	VImage dst;
	
	maxshort = (int)(VRepnMaxValue(VShortRepn));
	histo = (unsigned int *)VCalloc(maxshort, sizeof(unsigned int));
	nx = VImageNColumns(src);
	ny = VImageNRows(src);	
	nz = VImageNBands(src);

	if (white < 0 || white > 100 || black < 0 || black > 100 || white+black >= 100)  {
		fprintf(stderr, "VScaleIntensity: illegal percentage given.\n");
		return 0;
	};
	
	/* first pass: find maximum and minimum values */
	VImageStats(src, VAllBands, &min, &max, &mean, &var);
	if (max == min) {
		fprintf(stderr, "VScaleIntensity: illegal data in image.\n");
		return 0;
	};
	b = min;
	m = (max-min) / (double)maxshort;

	/* second pass: build a histogram*/
	for (z = 0; z < nz; z++)  {
		for (y = 0; y < ny; y++)  {
			for (x = 0; x < nx; x++)  {
				v = VGetPixel(src, z, y, x);
				i = (int)((v-b)/m+0.5);
				histo[i]++;
			};
		};
	};

	/* throw away pc percent of the voxel below lb and pc percent above ub */
	limit = (black * nx * ny * nz) / 100;
        lb = 0; sum = 0;
        for (i = 0; i < maxshort; i++)  {
        	sum += histo[i];
        	if (sum >= limit) { lb = i; break; };
        };
	limit = (white * nx * ny * nz) / 100;
        ub = maxshort-1; sum = 0;
        for (i = maxshort-1; i >= 0; i--)  {
        	sum += histo[i];
        	if (sum >= limit) { ub = i; break; };
        };
	min = lb*m+b;
	max = ub*m+b;

	/* third pass: create and convert image */
	dst = VCreateImage(nz, ny, nx, VUByteRepn);
	if (dst == 0) return 0;
	
	range = 256;
        m = range / (max - min);
        b = range - (m * max);
	for (z = 0; z < nz; z++)  {
		for (y = 0; y < ny; y++)  {
			for (x = 0; x < nx; x++)  {
				v = VGetPixel(src, z, y, x);
				i = (int)(v * m + b + 0.5);
                        	if (i < 0) i = 0;
                        	else if (i >= range) i = range-1;
                        	VPixel(dst, z, y, x, VUByte) = i;
                	};
                };
        };
        VFree(histo);
	VCopyImageAttrs(src, dst);
        return dst;
}
Exemple #27
0
VImage
VHistoEqualize(VImage src,VImage dest,VFloat exponent)
{
  int nbands,nrows,ncols,npixels;
  float u,v,sum;
  float *histo,*p;
  int i,j,dim;
  VShort *src_pp;
  VUByte *dest_pp;
  double smin,smax,x,y;


  if (VPixelRepn(src) != VShortRepn) VError(" input pixel repn must be short");
  if (exponent < 0.5) VError("parameter '-exponent' should be >= 0.5"); 
  if (exponent > 10) VWarning("parameter '-exponent' should be < 10"); 

  nbands = VImageNBands(src);
  nrows  = VImageNRows(src);
  ncols  = VImageNColumns(src);
  npixels = nbands * nrows * ncols;

  dest = VSelectDestImage("VContrastShort",dest,nbands,nrows,ncols,VUByteRepn);
  if (! dest) VError(" err creating dest image");
  VFillImage(dest,VAllBands,0);


  y = (double) exponent;
  smin = VRepnMinValue(VShortRepn);
  smax = VRepnMaxValue(VShortRepn);

  dim = 2.0 * smax + 1.0;
  histo = (float *) VCalloc(dim,sizeof(float));
  for (j=0; j<dim; j++) histo[j] = 0;

  p = (float *) VCalloc(dim,sizeof(float));


  src_pp = (VShort *) VImageData(src);
  for (i=0; i<npixels; i++) {
    j = *src_pp++;
    j -= smin;
    if (j == 0) continue;
    histo[j]++;
  }
 
  sum = 0;
  for (j=0; j<dim; j++) sum += histo[j];
  for (j=0; j<dim; j++) histo[j] /= sum;


  /* cumulative hist */
  for (i=0; i<dim; i++) {
    sum = 0;
    for (j=0; j<=i; j++) sum += histo[j];
    p[i] = sum;
  }


  /* make lut */
  for (i=0; i<dim; i++) {
    x = (double)p[i];
    if (x > 0)
      p[i] = (float)(pow(x,y) * 255.0);
  }

  /* apply lut */
  src_pp  = (VShort *) VImageData(src);
  dest_pp = (VUByte *) VImageData(dest);
  for (i=0; i<npixels; i++) {
    u = *src_pp++;
    j = (int) (u-smin);
    v = (double)p[j];
    if (v < 0) v = 0;
    if (v > 255) v = 255;
    *dest_pp++ = (VUByte) v;
  }

  VFree(p);
  VFree(histo);
  VCopyImageAttrs (src, dest);
  return dest;
}
Exemple #28
0
/*!
\fn VImage VContrastUByte(VImage src,VImage dest,VFloat percent,VFloat background)
\param src   input image  (ubyte repn)
\param dest  output image  (ubyte repn)
\param percent percentage of pixels to ignore at either end of the histogram.
\param background input grey values with absolute values less than <background> are
assumed to be image background.
*/
VImage
VContrastUByte(VImage src,VImage dest,VFloat low,VFloat high)
{
  int nbands,nrows,ncols,npixels;
  float u,v,xmin,xmax,slope,sum,background;
  float histo[256];
  int i,j;
  VUByte *src_pp,*dest_pp;

  if (VPixelRepn(src) != VUByteRepn) VError(" input pixel repn must be ubyte");

  nbands = VImageNBands(src);
  nrows  = VImageNRows(src);
  ncols  = VImageNColumns(src);
  npixels = nbands * nrows * ncols;

  dest = VSelectDestImage("VContrastUByte",dest,nbands,nrows,ncols,VUByteRepn);
  if (! dest) VError(" err creating dest image");
  VFillImage(dest,VAllBands,0);

  for (j=0; j<256; j++) histo[j] = 0;

  xmin = VPixelMaxValue(src);
  xmax = VPixelMinValue(src);
  background = xmin;
  
  src_pp = (VUByte *) VImageData(src);
  for (i=0; i<npixels; i++) {
    j = *src_pp++;
    if (j <= background) continue;
    histo[j]++;
  }
 
  sum = 0;
  for (j=0; j<256; j++) sum += histo[j];
  for (j=0; j<256; j++) histo[j] /= sum;

  xmin = 0;
  sum  = 0;
  for (j=0; j<256; j++) {
    if (j > background) sum += histo[j];
    if (sum > low) break;
  }
  xmin = j;

  xmax = 255.0;
  sum = 0;
  for (j=255; j>0; j--) {
    if (j > background) sum += histo[j];
    if (sum > high) break;
  }
  xmax = j;


  slope = (float) (255.0) / ((float) (xmax - xmin));
  
  src_pp  = (VUByte *) VImageData(src);
  dest_pp = (VUByte *) VImageData(dest);

  for (i=0; i<npixels; i++) {
    u = *src_pp;
 
    if (u <= background) {
      v = 0;
    }
    else {
      v = (int) (slope * (u - xmin) + 0.5);
      if (v <   0) v = 0;
      if (v > 255) v = 255;
    }

    *dest_pp = (VUByte) v;
    src_pp++;
    dest_pp++;
  }

  VFree(histo);
  VCopyImageAttrs (src, dest);
  return dest;
}
Exemple #29
0
/*!
\fn VImage VContrastShort(VImage src,VImage dest,VFloat percent,VFloat background)
\param src   input image  (short repn)
\param dest  output image  (ubyte repn)
*/
VImage
VContrastShort(VImage src,VImage dest,VFloat low,VFloat high)
{
  int nbands,nrows,ncols,npixels;
  float u,v,xmin,xmax,slope,sum;
  float *histo;
  int i,j,dim;
  VShort *src_pp;
  VUByte *dest_pp;
  double smin,smax;
  double percent1,percent2;

  if (VPixelRepn(src) != VShortRepn) VError(" input pixel repn must be short");

  nbands = VImageNBands(src);
  nrows  = VImageNRows(src);
  ncols  = VImageNColumns(src);
  npixels = nbands * nrows * ncols;

  dest = VSelectDestImage("VContrastShort",dest,nbands,nrows,ncols,VUByteRepn);
  if (! dest) VError(" err creating dest image");
  VFillImage(dest,VAllBands,0);


  smin = VRepnMinValue(VShortRepn);
  smax = VRepnMaxValue(VShortRepn);

  percent1 = low;    /* unten  */
  percent2 = high;   /* oben   */


  dim = 2.0 * smax + 1.0;
  histo = (float *) VCalloc(dim,sizeof(float));
  for (j=0; j<dim; j++) histo[j] = 0;


  src_pp = (VShort *) VImageData(src);
  for (i=0; i<npixels; i++) {
    j = *src_pp++;
    j -= smin;
    histo[j]++;
  }
 
  sum = 0;
  for (j=0; j<dim; j++) sum += histo[j];
  for (j=0; j<dim; j++) histo[j] /= sum;

  xmin = 0;
  sum  = 0;
  for (j=0; j<dim; j++) {
    sum += histo[j];
    if (sum > percent1) break;
  }
  xmin = j+smin;

  xmax = dim;
  sum = 0;
  for (j=dim; j>0; j--) {
    sum += histo[j];
    if (sum > percent2) break;
  }
  xmax = j+smin;


  slope = 255.0f / (xmax - xmin);
  
  src_pp  = (VShort *) VImageData(src);
  dest_pp = (VUByte *) VImageData(dest);
  for (i=0; i<npixels; i++) {
    u = *src_pp++;
    v = (int) (slope * (u - xmin) + 0.5);
    if (v < 0) v = 0;
    if (v > 255) v = 255;
    *dest_pp++ = (VUByte) v;
  }

  VFree(histo);
  VCopyImageAttrs (src, dest);
  return dest;
}