home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / gems / graphics / rgbto4bi.c < prev    next >
Text File  |  1992-04-09  |  1KB  |  52 lines

  1. /*
  2. Mapping RGB Triples onto Four Bits
  3. by Alan Paeth
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. remap8(R, G, B, R2, G2, B2)
  8.     float R, G, B, *R2, *G2, *B2;
  9.     {
  10. /*
  11.  * remap8 maps floating (R,G,B) triples onto quantized
  12.  * (R2,B2,B2) triples and returns the code (vertex)
  13.  * value/color table entry for the quantization. The
  14.  * points (eight) are the vertices of the cube.
  15.  */
  16.     int code;
  17.     *R2 = *G2 = *B2 = 0.0;
  18.     code = 0;
  19.     if (R >= 0.5) { *R2 = 1.0; code |= 1; }
  20.     if (G >= 0.5) { *G2 = 1.0; code |= 2; }
  21.     if (B >= 0.5) { *B2 = 1.0; code |= 4; }
  22.     return(code);
  23.     }
  24.  
  25. /*
  26.  * remap14 maps floating (R,G,B) triples onto quantized
  27.  * (R2,B2,B2) triples and returns the code (vertex)
  28.  * value/color table entry for the quantization. The
  29.  * points (fourteen) are the vertices of the cuboctahedron.
  30.  */
  31.  
  32. float rval[] = { 0.,.5 ,.5 , 1.,.0 , 0., 0.,.5,
  33.                 .5 , 1., 1., 1., 0.,.5 ,.5 , 1.};
  34. float gval[] = { 0.,.5 , 0., 0.,.5 , 1., 0.,.5,
  35.                 .5 , 1., 0.,.5 , 1., 1.,.5 , 1.};
  36. float bval[] = { 0., 0.,.5 , 0.,.5 , 0., 1.,.5,
  37.                 .5 , 0., 1.,.5 , 1.,.5 , 1., 1.};
  38.  
  39. int remap14(R, G, B,  R2, G2, B2)
  40.     float R, G, B, *R2, *G2, *B2;
  41.     {
  42.     int code = 0;
  43.     if ( R + G + B > 1.5) code |= 8;
  44.     if (-R + G + B > 0.5) code |= 4;
  45.     if ( R - G + B > 0.5) code |= 2;
  46.     if ( R + G - B > 0.5) code |= 1;
  47.     *R2 = rval[code];
  48.     *G2 = gval[code];
  49.     *B2 = bval[code];
  50.     return(code);
  51.     }
  52.