Ejemplo n.º 1
0
void remap(Texture *texture)
{
  int i,j,r,g,b,maxval=0;
  FILE *fp;
  double dist,d,angle,a;
  
  int cmap[256];
  Color color_map[256];
  
  printf("Width %d Height %d\n",Width,Height);
  
  fp = fopen(DEFAULT_PALETTE_FILE, "rb");
  if (fp == NULL)
    fatal_error("unable to open palette file");
  
  for (i = 0; i < PALETTE_ENTRIES; i++) {
    r = getc(fp);
    g = getc(fp);
    b = getc(fp);
    if (b == EOF)
      fatal_error("error reading palette file");
    
    palette[i].red = (double)r;
    palette[i].green = (double)g;
    palette[i].blue = (double)b;
    if (maxval<r) maxval=r;
    if (maxval<g) maxval=g;
    if (maxval<b) maxval=b;
  }
  fclose(fp);
  
  /*
   * scale down to max rgb values
   */
  for (i=0;i<ColorMapSize;i++)
    {
      color_map[i].red=(((double)Red[i])/(255.0/(double)maxval));
      color_map[i].green=(((double)Green[i])/(255.0/(double)maxval));
      color_map[i].blue=(((double)Blue[i])/(255.0/(double)maxval));
    }

  
  for (i=0;i<ColorMapSize;i++)
    {
      dist=10000.0;
      angle=M_PI_2;
      for (j=0;j<PALETTE_ENTRIES;j++)
	{
	  Color col;
	  vminus(color_map[i],palette[j],col);
	  if (  (fabs(col.red)<epsilon)
              &&(fabs(col.green)<epsilon)
              &&(fabs(col.blue)<epsilon))
            {
              cmap[i]=j;
            }
          else
	  if ((d=Norm(col)) < dist)
	    {
	      dist=d;
	      cmap[i]=j;
	    }
	  else
	    if ((fabs(dist-d)<epsilon)
		&& (Norm(palette[j])>1.0)
		&& (Norm(color_map[i])>1.0)
		&& ((a=Winkel(color_map[i],palette[j]))<angle))
	      {
		angle=a;
		cmap[i]=j;
	      }
	}
/*
      printf("%d (%g %g %g) -> %d (%g %g %g)\n",
	     i,
	     color_map[i].red,color_map[i].green,color_map[i].blue,
	     cmap[i],
	     palette[cmap[i]].red,palette[cmap[i]].green,palette[cmap[i]].blue);
*/

    }
  
  for (i=0;i<Width*Height;i++)
    {
      byte pix=texture->texels[i];
      byte newpix=cmap[pix];
      texture->texels[i]=newpix;
    }	     
}
Ejemplo n.º 2
0
double distance (Vector *a, Vector *b) {

	Vector d = vminus (a, b);
	return norm (&d);
}