home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 8 / CDASC08.ISO / NEWS / RADIANCE / SRC / COMMON / SPEC_RGB.C < prev    next >
C/C++ Source or Header  |  1993-10-07  |  3KB  |  116 lines

  1. /* Copyright (c) 1990 Regents of the University of California */
  2.  
  3. #ifndef lint
  4. static char SCCSid[] = "@(#)spec_rgb.c 2.1 11/12/91 LBL";
  5. #endif
  6.  
  7. /*
  8.  * Convert colors and spectral ranges.
  9.  */
  10.  
  11. #include "color.h"
  12.  
  13. /*
  14.  *    The following table contains the CIE tristimulus integrals
  15.  *  for X, Y, and Z.  The table is cumulative, so that
  16.  *  each color coordinate integrates to 1.
  17.  */
  18.  
  19. #define  STARTWL    380        /* starting wavelength (nanometers) */
  20. #define  INCWL        10        /* wavelength increment */
  21. #define  NINC        40        /* # of values */
  22.  
  23. static BYTE  chroma[3][NINC] = {
  24.     {                            /* X */
  25.         0,   0,   0,   2,   6,   13,  22,  30,  36,  41,
  26.         42,  43,  43,  44,  46,  52,  60,  71,  87,  106,
  27.         128, 153, 178, 200, 219, 233, 243, 249, 252, 254,
  28.         255, 255, 255, 255, 255, 255, 255, 255, 255, 255
  29.     }, {                            /* Y */
  30.         0,   0,   0,   0,   0,   1,   2,   4,   7,   11,
  31.         17,  24,  34,  48,  64,  84,  105, 127, 148, 169,
  32.         188, 205, 220, 232, 240, 246, 250, 253, 254, 255,
  33.         255, 255, 255, 255, 255, 255, 255, 255, 255, 255
  34.     }, {                            /* Z */
  35.         0,   0,   2,   10,  32,  66,  118, 153, 191, 220,
  36.         237, 246, 251, 253, 254, 255, 255, 255, 255, 255,
  37.         255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
  38.         255, 255, 255, 255, 255, 255, 255, 255, 255, 255
  39.     }
  40. };
  41.  
  42. #ifdef  NTSC
  43. static float  xyz2rgbmat[3][3] = {    /* XYZ to RGB (NTSC) */
  44.     1.73, -.48, -.26,
  45.     -.81, 1.65, -.02,
  46.      .08, -.17, 1.28,
  47. };
  48. #else
  49. static float xyz2rgbmat[3][3] = {    /* XYZ to RGB (color monitor) */
  50.      2.739, -1.145, -.424,
  51.     -1.119,  2.029,  .033,
  52.       .138,  -.333, 1.105,
  53. };
  54. #endif
  55.  
  56.  
  57.  
  58. spec_rgb(col, s, e)        /* compute RGB color from spectral range */
  59. COLOR  col;
  60. int  s, e;
  61. {
  62.     COLOR  ciecolor;
  63.  
  64.     spec_cie(ciecolor, s, e);
  65.     cie_rgb(col, ciecolor);
  66. }
  67.  
  68.  
  69. spec_cie(col, s, e)        /* compute a color from a spectral range */
  70. COLOR  col;        /* returned color */
  71. int  s, e;        /* starting and ending wavelengths */
  72. {
  73.     register int  i, d, r;
  74.     
  75.     s -= STARTWL;
  76.     if (s < 0)
  77.         s = 0;
  78.  
  79.     e -= STARTWL;
  80.     if (e <= s) {
  81.         col[RED] = col[GRN] = col[BLU] = 0.0;
  82.         return;
  83.     }
  84.     if (e >= INCWL*(NINC - 1))
  85.         e = INCWL*(NINC - 1) - 1;
  86.  
  87.     d = e / INCWL;            /* interpolate values */
  88.     r = e % INCWL;
  89.     for (i = 0; i < 3; i++)
  90.         col[i] = chroma[i][d]*(INCWL - r) + chroma[i][d + 1]*r;
  91.  
  92.     d = s / INCWL;
  93.     r = s % INCWL;
  94.     for (i = 0; i < 3; i++)
  95.         col[i] -= chroma[i][d]*(INCWL - r) - chroma[i][d + 1]*r;
  96.  
  97.     col[RED] = (col[RED] + 0.5) / (256*INCWL);
  98.     col[GRN] = (col[GRN] + 0.5) / (256*INCWL);
  99.     col[BLU] = (col[BLU] + 0.5) / (256*INCWL);
  100. }
  101.  
  102.  
  103. cie_rgb(rgbcolor, ciecolor)        /* convert CIE to RGB */
  104. register COLOR  rgbcolor, ciecolor;
  105. {
  106.     register int  i;
  107.  
  108.     for (i = 0; i < 3; i++) {
  109.         rgbcolor[i] =    xyz2rgbmat[i][0]*ciecolor[0] +
  110.                 xyz2rgbmat[i][1]*ciecolor[1] +
  111.                 xyz2rgbmat[i][2]*ciecolor[2] ;
  112.         if (rgbcolor[i] < 0.0)
  113.             rgbcolor[i] = 0.0;
  114.     }
  115. }
  116.