home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 505a.lha / GrapicsGems / PixelInteger.c < prev    next >
C/C++ Source or Header  |  1991-05-01  |  2KB  |  54 lines

  1. /*
  2. Proper Treatment of Pixels as Integers
  3. by Alan Paeth
  4. from "Graphics Gems", Academic Press, 1990
  5. */
  6.  
  7. #define Min     code[2]
  8. #define Med     code[1]
  9. #define Max     code[0]
  10. #define NCODE   3
  11.  
  12. /*
  13.  * A call to getplanes of the form:
  14.  * getplanes(&red, &green, &blue, 256, "grb");
  15.  *
  16.  * fills the first three integer pointers with (near) identical
  17.  * values which maximize red*green*blue <= 256. The final parameter
  18.  * string defines tie-break order, here green>=red>=blue (the usual
  19.  * default). The present code procedure calls "err(string, arg)"
  20.  * given bad parameters; it is a simple task to rewrite the code as
  21.  * a function which returns a success/failure code(s), as needed.
  22.  *
  23.  * In the example given above the code fills in the values
  24.  * red = 6, green = 7, blue = 6.
  25.  */
  26.  
  27. getplanes(r, g, b, n, bias)
  28.     int *r, *g, *b;
  29.     char *bias;
  30.     {
  31.     int i, code[NCODE];
  32.     if(strlen(bias) != NCODE )
  33.         err("bias string \"%s\" wrong length",bias);
  34.     Min = Med = Max = 0;
  35.     *r = *g = *b = 0;
  36.     while(Min*Min*Min <= n) Min++;
  37.     Min--;
  38.     while(Med*Med*Min <= n) Med++;
  39.     Med--;
  40.     Max = n/(Min*Med);
  41.     for( i = 0; i < NCODE; i++ )
  42.        {
  43.         switch(bias[i])
  44.             {
  45.             case 'r': case 'R': *r = code[i]; break;
  46.             case 'g': case 'G': *g = code[i]; break;
  47.             case 'b': case 'B': *b = code[i]; break;
  48.             default: err("bad bias character: \'%c\'",bias[i]); break;
  49.             }
  50.         }
  51.     if (!(*r && *g && *b)) err("bias string \"%s\" deficient", bias);
  52.     }
  53.     
  54.