Back to index

plt-scheme  4.2.1
Functions | Variables
wx_24to8.cc File Reference
#include <stdlib.h>
#include "wx_image.h"

Go to the source code of this file.

Functions

static void assign_color (CBOX *ptr, byte *rp, byte *gp, byte *bp)
static CCELLcreate_colorcell (int r1, int g1, int b1, byte *r, byte *g, byte *b)
static void map_colortable (byte *r, byte *g, byte *b)
void InitFSDTables ()

Variables

static int num_colors
static int WIDE
static int HIGH
static int histogram [B_LEN][B_LEN][B_LEN]
CBOXfreeboxes
CBOXusedboxes
CCELL ** ColorCells
static byte tbl1 [256]
static byte tbl3 [256]
static byte tbl5 [256]
static byte tbl7 [256]

Function Documentation

static void assign_color ( CBOX ptr,
byte rp,
byte gp,
byte bp 
) [static]

Definition at line 496 of file wx_24to8.cc.

{
  *rp = ((ptr->rmin + ptr->rmax) << (COLOR_DEPTH - B_DEPTH)) / 2;
  *gp = ((ptr->gmin + ptr->gmax) << (COLOR_DEPTH - B_DEPTH)) / 2;
  *bp = ((ptr->bmin + ptr->bmax) << (COLOR_DEPTH - B_DEPTH)) / 2;
}
CCELL * create_colorcell ( int  r1,
int  g1,
int  b1,
byte r,
byte g,
byte b 
) [static]

Definition at line 507 of file wx_24to8.cc.

{
  register int    i,tmp, dist;
  register CCELL *ptr;
  register byte  *rp,*gp,*bp;
  int             ir,ig,ib, mindist;

  ir = r1 >> (COLOR_DEPTH-C_DEPTH);
  ig = g1 >> (COLOR_DEPTH-C_DEPTH);
  ib = b1 >> (COLOR_DEPTH-C_DEPTH);

  r1 &= ~1 << (COLOR_DEPTH-C_DEPTH);
  g1 &= ~1 << (COLOR_DEPTH-C_DEPTH);
  b1 &= ~1 << (COLOR_DEPTH-C_DEPTH);

  ptr = (CCELL *) malloc(sizeof(CCELL));
  *(ColorCells + ir*C_LEN*C_LEN + ig*C_LEN + ib) = ptr;
  ptr->num_ents = 0;

  /* step 1: find all colors inside this cell, while we're at
     it, find distance of centermost point to furthest
     corner */

  mindist = 99999999;

  rp=r;  gp=g;  bp=b;
  for (i=0; i<num_colors; i++,rp++,gp++,bp++) {
    if( *rp>>(COLOR_DEPTH-C_DEPTH) == ir  &&
        *gp>>(COLOR_DEPTH-C_DEPTH) == ig  &&
        *bp>>(COLOR_DEPTH-C_DEPTH) == ib) {

      ptr->entries[ptr->num_ents][0] = i;
      ptr->entries[ptr->num_ents][1] = 0;
      ++ptr->num_ents;

      tmp = *rp - r1;
      if (tmp < (MAX_COLOR/C_LEN/2)) tmp = MAX_COLOR/C_LEN-1 - tmp;
      dist = tmp*tmp;

      tmp = *gp - g1;
      if (tmp < (MAX_COLOR/C_LEN/2)) tmp = MAX_COLOR/C_LEN-1 - tmp;
      dist += tmp*tmp;

      tmp = *bp - b1;
      if (tmp < (MAX_COLOR/C_LEN/2)) tmp = MAX_COLOR/C_LEN-1 - tmp;
      dist += tmp*tmp;

      if (dist < mindist) mindist = dist;
    }
  }

  /* step 3: find all points within that distance to box */

  rp=r;  gp=g;  bp=b;
  for (i=0; i<num_colors; i++,rp++,gp++,bp++) {
    if (*rp >> (COLOR_DEPTH-C_DEPTH) != ir  ||
       *gp >> (COLOR_DEPTH-C_DEPTH) != ig  ||
       *bp >> (COLOR_DEPTH-C_DEPTH) != ib) {

      dist = 0;

      if ((tmp = r1 - *rp)>0 || (tmp = *rp - (r1 + MAX_COLOR/C_LEN-1)) > 0 )
       dist += tmp*tmp;

      if( (tmp = g1 - *gp)>0 || (tmp = *gp - (g1 + MAX_COLOR/C_LEN-1)) > 0 )
       dist += tmp*tmp;

      if( (tmp = b1 - *bp)>0 || (tmp = *bp - (b1 + MAX_COLOR/C_LEN-1)) > 0 )
       dist += tmp*tmp;

      if( dist < mindist ) {
       ptr->entries[ptr->num_ents][0] = i;
       ptr->entries[ptr->num_ents][1] = dist;
       ++ptr->num_ents;
      }
    }
  }

  /* sort color cells by distance, use cheap exchange sort */
  {
    int n, next_n;

    n = ptr->num_ents - 1;
    while (n>0) {
      next_n = 0;
      for (i=0; i<n; ++i) {
       if (ptr->entries[i][1] > ptr->entries[i+1][1]) {
         tmp = ptr->entries[i][0];
         ptr->entries[i][0] = ptr->entries[i+1][0];
         ptr->entries[i+1][0] = tmp;
         tmp = ptr->entries[i][1];
         ptr->entries[i][1] = ptr->entries[i+1][1];
         ptr->entries[i+1][1] = tmp;
         next_n = i;
       }
      }
      n = next_n;
    }
  }
  return (ptr);
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 870 of file wx_24to8.cc.

{
  int i;
  for (i=0; i<256; i++) {     /* initialize Floyd-Steinberg division tables */
    tbl1[i] = i/16;
    tbl3[i] = (3*i)/16;
    tbl5[i] = (5*i)/16;
    tbl7[i] = (7*i)/16;
  }
}
static void map_colortable ( byte r,
byte g,
byte b 
) [static]

Definition at line 615 of file wx_24to8.cc.

{
  int    ir,ig,ib, *histp;
  CCELL *cell;

  histp  = &histogram[0][0][0];
  for (ir=0; ir<B_LEN; ir++) {
    for (ig=0; ig<B_LEN; ig++) {
      for (ib=0; ib<B_LEN; ib++) {
       if (*histp==0) *histp = -1;
       else {
         int  i, j, tmp, d2, dist;
         
         cell = *(ColorCells +
                 ( ((ir>>(B_DEPTH-C_DEPTH)) << C_DEPTH*2)
                 + ((ig>>(B_DEPTH-C_DEPTH)) << C_DEPTH)
                 +  (ib>>(B_DEPTH-C_DEPTH)) ) );
              
         if (cell==NULL)
           cell = create_colorcell(ir<<(COLOR_DEPTH-B_DEPTH),
                                ig<<(COLOR_DEPTH-B_DEPTH),
                                ib<<(COLOR_DEPTH-B_DEPTH),
                                r, g, b);

         dist = 9999999;
         for (i=0; i<cell->num_ents && dist>cell->entries[i][1]; i++) {
           j = cell->entries[i][0];
           d2 = r[j] - (ir << (COLOR_DEPTH-B_DEPTH));
           d2 *= d2;
           tmp = g[j] - (ig << (COLOR_DEPTH-B_DEPTH));
           d2 += tmp*tmp;
           tmp = b[j] - (ib << (COLOR_DEPTH-B_DEPTH));
           d2 += tmp*tmp;
           if( d2 < dist ) { dist = d2;  *histp = j; }
         }
       }
       histp++;
      }
    }
  }
}

Here is the call graph for this function:


Variable Documentation

Definition at line 48 of file wx_24to8.cc.

Definition at line 47 of file wx_24to8.cc.

int HIGH [static]

Definition at line 44 of file wx_24to8.cc.

int histogram[B_LEN][B_LEN][B_LEN] [static]

Definition at line 45 of file wx_24to8.cc.

int num_colors [static]

Definition at line 44 of file wx_24to8.cc.

byte tbl1[256] [static]

Definition at line 55 of file wx_24to8.cc.

byte tbl3[256] [static]

Definition at line 55 of file wx_24to8.cc.

byte tbl5[256] [static]

Definition at line 55 of file wx_24to8.cc.

byte tbl7[256] [static]

Definition at line 55 of file wx_24to8.cc.

Definition at line 47 of file wx_24to8.cc.

int WIDE [static]

Definition at line 44 of file wx_24to8.cc.