home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_09_08 / 9n08047a < prev    next >
Text File  |  1991-06-18  |  3KB  |  125 lines

  1. #include <stdlib.h>
  2. #include "pof.h"
  3.  
  4. ColorNode *Build_Tree(rgb *pal)
  5. {
  6.     /* this function builds a color tree based on the palette passed */
  7.     int i;
  8.     int watch;
  9.     ColorNode *Root;
  10.     rgb temp;
  11.     unsigned r_ave=0, g_ave=0, b_ave=0;
  12.  
  13.     for (i=1; i<256; i++)
  14.     {
  15.         r_ave += pal->Red;
  16.         g_ave += pal->Grn;
  17.         b_ave += pal->Blu;
  18.     }
  19.     r_ave /= 255;
  20.     g_ave /= 255;
  21.     b_ave /= 255;
  22.  
  23.     temp.Red = r_ave;
  24.     temp.Grn = g_ave;
  25.     temp.Blu = b_ave;
  26.  
  27.     Root = init_node(temp, 0);     /* Initialize the root */
  28.  
  29.  
  30.     for (i=1; i<256; i++)          /* For each color */
  31.     {
  32.         newnode(pal[i], i, Root, Root);
  33.     }
  34.  
  35.     return(Root);
  36. }
  37.  
  38.  
  39. ColorNode *init_node(rgb color, char pal)
  40. {
  41.     int i;
  42.     ColorNode *newnode;
  43.  
  44.     /* Allocate a newnode from memory */
  45.     newnode = (ColorNode *)malloc(sizeof(ColorNode));
  46.     if (newnode == NULL)
  47.     {
  48.         printf("\n A Critical Error has occurred during node allocation");
  49.         printf("\n for Palette #%3i.",pal);
  50.         exit(-1);
  51.     }
  52.  
  53.  
  54.     /* Initialize the color variables */
  55.     newnode->key.Red = color.Red;
  56.     newnode->key.Grn = color.Grn;
  57.     newnode->key.Blu = color.Blu;
  58.     newnode->pal_num = pal;
  59.  
  60.     /* Initialize the links */
  61.     for (i=0; i<8; i++) newnode->Link[i] = NULL;
  62.  
  63.     return(newnode);
  64. }
  65.  
  66. ColorNode *newnode(rgb color, char pal_num, ColorNode *parent, ColorNode *child)
  67. {
  68.     char watch;
  69.  
  70.     if (child != NULL)   /* The node exists */
  71.     {
  72.         /* Calculate case value  */
  73.         watch = (((color.Red < child->key.Red)<<2) +
  74.                 ((color.Grn < child->key.Grn)<<1) +
  75.                 (color.Blu < child->key.Blu));
  76.  
  77.         if ((watch == 0) && (color.Red == child->key.Red) &&
  78.                     (color.Grn == child->key.Grn) &&
  79.                                                   (color.Blu == child->key.Blu))
  80.                                 /* We have a degenerate case where two colors match! */
  81.                                 /* So we really just want to skip over this color    */
  82.                     return(child);
  83.         child = newnode(color, pal_num, child, child->Link[watch]);
  84.     } else {
  85.                 /*  A NULL node was sent with a parent node     */
  86.                 /*  therefore a new node must be initialized    */
  87.  
  88.         child = init_node(color, pal_num);
  89.  
  90.         /* Now we need to link up to the parent node */
  91.         /* Calculate case value  */
  92.  
  93.         watch = (((color.Red < parent->key.Red)<<2) +
  94.                  ((color.Grn < parent->key.Grn)<<1) +
  95.                   (color.Blu < parent->key.Blu));
  96.  
  97.         if ((watch == 0) && (color.Red == parent->key.Red) &&
  98.                     (color.Grn == parent->key.Grn) &&
  99.                                                   (color.Blu == parent->key.Blu))
  100.                   {
  101.                                 free(child);        /* Deallocate the newly created node */
  102.                                 return(parent);     /* Need to return a ColorNode ptr    */
  103.                   }
  104.                   parent->Link[watch] = child;
  105.          }
  106.  
  107.     return(child);
  108. }
  109.  
  110.  
  111. void Kill_Tree(ColorNode *node)
  112. {
  113.     int i;
  114.  
  115.     for (i=0; i<8; i++)
  116.     {
  117.         /* If the link is other than null, launch another kill */
  118.         if (node->Link[i]) Kill_Tree(node->Link[i]);
  119.     }
  120.         /* after all the links are NULL, deallocate the memory */
  121.     free(node);
  122.     return;
  123. }
  124.  
  125.