home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / i / iritsm3s.zip / poly3d-r / colortbl.c < prev    next >
C/C++ Source or Header  |  1991-09-08  |  8KB  |  216 lines

  1. /*****************************************************************************
  2. *   Routines to    prpepare the color table for the given scene. Each color     *
  3. * get equal share in the map table, so its modules does:             *
  4. * 1. Scans all objects and retrieve all possible colors.             *
  5. * 2. Create a color map table, of specified size (in GlblShadeInfo global    *
  6. *    structure) and interpolate intensity levels for each color into it.     *
  7. * 3. Update the objects for their index into the color map.             *
  8. *                                         *
  9. * Written by:  Gershon Elber                Ver 2.0, Mar. 1990   *
  10. *****************************************************************************/
  11.  
  12. #ifdef __MSDOS__
  13. #include <stdlib.h>
  14. #endif /* __MSDOS__ */
  15.  
  16. #include <math.h>
  17. #include <stdio.h>
  18. #include <time.h>
  19. #include "program.h"
  20. #include "iritprsr.h"
  21.  
  22. /* The colors can be specified as indices in which we must convert to RGB: */
  23. /* We uses the EGA convension (see also TC 2.0 graphics.h file).       */
  24. static int TransColorTable[][4] = {
  25.     { /* BLACK        */ 0,    0,   0,   0 },
  26.     { /* BLUE        */ 1,    0,   0, 255 },
  27.     { /* GREEN        */ 2,    0, 255,   0 },
  28.     { /* CYAN        */ 3,    0, 255, 255 },
  29.     { /* RED        */ 4,  255,   0,   0 },
  30.     { /* MAGENTA     */ 5,  255,   0, 255 },
  31.     { /* BROWN        */ 6,   50,   0,   0 },
  32.     { /* LIGHTGRAY    */ 7,  127, 127, 127 },
  33.     { /* DARKGRAY    */ 8,   63,  63,  63 },
  34.     { /* LIGHTBLUE    */ 9,    0,   0, 255 },
  35.     { /* LIGHTGREEN    */ 10,   0, 255,   0 },
  36.     { /* LIGHTCYAN    */ 11,   0, 255, 255 },
  37.     { /* LIGHTRED    */ 12, 255,   0,   0 },
  38.     { /* LIGHTMAGENTA    */ 13, 255,   0, 255 },
  39.     { /* YELLOW        */ 14, 255, 255,   0 },
  40.     { /* WHITE        */ 15, 255, 255, 255 },
  41.     { /* BROWN        */ 20,  50,   0,   0 },
  42.     { /* DARKGRAY    */ 56,  63,  63,  63 },
  43.     { /* LIGHTBLUE    */ 57,   0,   0, 255 },
  44.     { /* LIGHTGREEN    */ 58,   0, 255,   0 },
  45.     { /* LIGHTCYAN    */ 59,   0, 255, 255 },
  46.     { /* LIGHTRED    */ 60, 255,   0,   0 },
  47.     { /* LIGHTMAGENTA    */ 61, 255,   0, 255 },
  48.     { /* YELLOW        */ 62, 255, 255,   0 },
  49.     { /* WHITE        */ 63, 255, 255, 255 },
  50.     {               -1,   0,   0,   0 }
  51. };
  52.  
  53.  
  54. static void PrepareAllObjects1(IPObjectStruct *PObjects,
  55.                    GifColorType *Colors, int *NumColors);
  56. static void InsertColor(IPObjectStruct *PObject, GifColorType *Colors,
  57.                             int *NumColors);
  58. static void PrepareAllObjects2(IPObjectStruct *PObjects, GifColorType *Colors,
  59.                             int NumColors);
  60. static void UpdateColorIndex(IPObjectStruct *PObject, GifColorType *Colors,
  61.                             int NumColors);
  62.  
  63. /*****************************************************************************
  64. * Routine to prepare color map for the given scene.                 *
  65. *****************************************************************************/
  66. void PrepareColorTable(IPObjectStruct *PObjects)
  67. {
  68.     int    i, j, NumColors = 0, NumOfObjs, ColorMapSize, Levels,
  69.     *MinIntensityIndex;
  70.     RealType DIntensity, CrntIntensity;
  71.     GifColorType *Colors, *ColorMap;
  72.     IPObjectStruct *PObject;
  73.  
  74.     for (NumOfObjs = 0, PObject = PObjects;  /* How many objects do we have? */
  75.      PObject != NULL;
  76.      NumOfObjs++, PObject = PObject -> Pnext);
  77.  
  78.     Colors = (GifColorType *) MyMalloc(sizeof(GifColorType) * NumOfObjs);
  79.  
  80.     PrepareAllObjects1(PObjects, Colors, &NumColors);
  81.  
  82.     /* Allocate color table and fill it in: */
  83.     ColorMapSize = (1 << GlblShadeInfo.BitsPerPixel);
  84.     GlblShadeInfo.PColorMap = ColorMap =
  85.     (GifColorType *) MyMalloc(sizeof(GifColorType) * ColorMapSize);
  86.     GlblShadeInfo.MinIntensityIndex = MinIntensityIndex =
  87.     (int *) MyMalloc(sizeof(int) * ColorMapSize);
  88.  
  89.     /* Save one place to hold the back ground color: */
  90.     if (NumColors == 0) {
  91.     fprintf(stderr, "No color allocated - that is really wierd!\n");
  92.     MyExit(1);
  93.     }
  94.     GlblShadeInfo.LevelsPerColor = Levels = (ColorMapSize - 1) / NumColors;
  95.     if (Levels == 0) {
  96.     fprintf(stderr, "Too many colors (%d) for color map size (%d), exit.\n",
  97.                         NumColors, ColorMapSize);
  98.     MyExit(1);
  99.     }
  100.  
  101.     /* Insert the back ground color in: */
  102.     for (i = 0; TransColorTable[i][0] >= 0; i++)
  103.     if (TransColorTable[i][0] == GlblShadeInfo.BackGroundColor) break;
  104.     if (TransColorTable[i][0] < 0) {
  105.     fprintf(stderr,
  106.         "Undefined color required (%d) for background, default selected instead.\n",
  107.         PObjects -> Color);
  108.     PObjects -> Color = GlblShadeInfo.DefaultColor;
  109.     }
  110.     ColorMap[0].Red   = TransColorTable[i][1];
  111.     ColorMap[0].Green = TransColorTable[i][2];
  112.     ColorMap[0].Blue  = TransColorTable[i][3];
  113.  
  114.     /* O.k. time to interpolate the colors. Each given color intensities are */
  115.     /* interplated between the ambient light and full intensity (1.0):         */
  116.     DIntensity = (1.0 - GlblShadeInfo.Ambient) / Levels;
  117.     for (i = 0; i < NumColors; i++) {
  118.     CrntIntensity = 1.0;
  119.     for (j = 0; j < Levels; j++) {
  120.         ColorMap[i * Levels + j + 1].Red =
  121.                     (int) (Colors[i].Red * CrntIntensity);
  122.         ColorMap[i * Levels + j + 1].Green =
  123.                     (int) (Colors[i].Green * CrntIntensity);
  124.         ColorMap[i * Levels + j + 1].Blue =
  125.                     (int) (Colors[i].Blue * CrntIntensity);
  126.         MinIntensityIndex[i * Levels + j + 1] = (i + 1) * Levels;
  127.         CrntIntensity -= DIntensity;
  128.     }
  129.     }
  130.  
  131.     /* Now the final step - let the objects know where their color is mapped */
  132.     /* to in the color table.                             */
  133.     PrepareAllObjects2(PObjects, Colors, NumColors);
  134.  
  135.     free((char *) Colors);
  136. }
  137.  
  138. /*****************************************************************************
  139. * Scan all objects (first pass).                         *
  140. *****************************************************************************/
  141. static void PrepareAllObjects1(IPObjectStruct *PObjects, GifColorType *Colors,
  142.                             int *NumColors)
  143. {
  144.     while (PObjects) {
  145.     InsertColor(PObjects, Colors, NumColors);
  146.     PObjects = PObjects -> Pnext;
  147.     }
  148. }
  149.  
  150. /*****************************************************************************
  151. * Scanning all the object in tree PBinTree and prepare them.             *
  152. *****************************************************************************/
  153. static void InsertColor(IPObjectStruct *PObject, GifColorType *Colors,
  154.                             int *NumColors)
  155. {
  156.     int i;
  157.  
  158.     if (PObject -> Color != RGB_COLOR_GIVEN) {
  159.     /* Translate the color into RGB form: */
  160.     for (i = 0; TransColorTable[i][0] >= 0; i++)
  161.         if (TransColorTable[i][0] == PObject -> Color) break;
  162.     if (TransColorTable[i][0] < 0) {
  163.         fprintf(stderr,
  164.         "Undefined color required (%d), default selected instead.\n",
  165.         PObject -> Color);
  166.         PObject -> Color = GlblShadeInfo.DefaultColor;
  167.     }
  168.     PObject -> RGB[0] = TransColorTable[i][1];
  169.     PObject -> RGB[1] = TransColorTable[i][2];
  170.     PObject -> RGB[2] = TransColorTable[i][3];
  171.     }
  172.  
  173.     /* Lets see if this color exists in table already, and if so do nothing: */
  174.     for (i = 0; i < *NumColors; i++)
  175.     if (PObject -> RGB[0] == Colors[i].Red &&
  176.         PObject -> RGB[1] == Colors[i].Green &&
  177.         PObject -> RGB[2] == Colors[i].Blue) return;
  178.  
  179.     /* Its a new color - add it to out colors table to allocate: */
  180.     Colors[*NumColors].Red      = PObject -> RGB[0];
  181.     Colors[*NumColors].Green    = PObject -> RGB[1];
  182.     Colors[(*NumColors)++].Blue = PObject -> RGB[2];
  183. }
  184.  
  185. /*****************************************************************************
  186. * Scan all objects (second pass).                         *
  187. *****************************************************************************/
  188. static void PrepareAllObjects2(IPObjectStruct *PObjects,
  189.                    GifColorType *Colors, int NumColors)
  190. {
  191.     while (PObjects) {
  192.     UpdateColorIndex(PObjects, Colors, NumColors);
  193.     PObjects = PObjects -> Pnext;
  194.     }
  195. }
  196.  
  197. /*****************************************************************************
  198. * Update object with the index to its color in the color map.             *
  199. *****************************************************************************/
  200. static void UpdateColorIndex(IPObjectStruct *PObject, GifColorType *Colors,
  201.                             int NumColors)
  202. {
  203.     int i;
  204.  
  205.     /* Find this color in table (must be in it...): */
  206.     for (i = 0; i < NumColors; i++)
  207.     if (PObject -> RGB[0] == Colors[i].Red &&
  208.         PObject -> RGB[1] == Colors[i].Green &&
  209.         PObject -> RGB[2] == Colors[i].Blue) break;
  210.  
  211.     /* Color table hold this object color, with decreasing intensity, from   */
  212.     /* this index up to (i + 1) * GlblShadeInfo.LevelsPerColor.             */
  213.     PObject -> Color = i * GlblShadeInfo.LevelsPerColor + 1;
  214. }
  215.  
  216.