home *** CD-ROM | disk | FTP | other *** search
/ Learn 3D Graphics Programming on the PC / Learn_3D_Graphics_Programming_on_the_PC_Ferraro.iso / rwdos / palette.c < prev    next >
C/C++ Source or Header  |  1995-02-15  |  11KB  |  373 lines

  1. /**********************************************************************
  2.  *
  3.  * File :     palette.c
  4.  *
  5.  * Abstract : Some RenderWare palette handling functions
  6.  *
  7.  **********************************************************************
  8.  *
  9.  * This file is a product of Criterion Software Ltd.
  10.  *
  11.  * This file is provided as is with no warranties of any kind and is
  12.  * provided without any obligation on Criterion Software Ltd. or
  13.  * Canon Inc. to assist in its use or modification.
  14.  *
  15.  * Criterion Software Ltd. will not, under any
  16.  * circumstances, be liable for any lost revenue or other damages arising
  17.  * from the use of this file.
  18.  *
  19.  * Copyright (c) 1995 Criterion Software Ltd.
  20.  * All Rights Reserved.
  21.  *
  22.  * RenderWare is a trademark of Canon Inc.
  23.  *
  24.  ************************************************************************/
  25.  
  26. /**********************************************************************
  27.  *
  28.  * Header files.
  29.  *
  30.  **********************************************************************/
  31.  
  32. #include <stdlib.h>
  33. #include <stdio.h>
  34. #include <string.h>
  35.  
  36. #include "rwlib.h"
  37. #include "palette.h"
  38.  
  39. /**********************************************************************
  40.  *
  41.  * Constant definitions.
  42.  *
  43.  **********************************************************************/
  44.  
  45. /* Maximum line length when parsing a file. */
  46.  
  47. #define MAXLINELEN           80
  48.  
  49. /* Palette file strings. */
  50.  
  51. #define PALETTEIDENTSTRING   "JASC-PAL"
  52. #define PALETTEVERSIONSTRING "0100"
  53.  
  54. /**********************************************************************
  55.  *
  56.  * Functions.
  57.  *
  58.  **********************************************************************/
  59.  
  60. /************************************************************************
  61.  *
  62.  *      Function:       ReadPalette()
  63.  *                      
  64.  *      Description:    Read a palette file into an array
  65.  *
  66.  *      Parameters:     filename - name of file to read.
  67.  *                      palette - array of RwPalleteEntry to store the
  68.  *                                read palette.
  69.  *                      palette_size - number of elements in 'palette' array
  70.  *
  71.  *      Return Value:   Returns number of entries read or 0 on error
  72.  *                      palette contains palette.
  73.  *
  74.  ************************************************************************/
  75.  
  76. int ReadPalette(char *fileName, RwPaletteEntry *palette, int palette_size)
  77. {
  78.     FILE          *file;
  79.     char          buffer[MAXLINELEN];
  80.     int           i;
  81.     unsigned int  red;
  82.     unsigned int  green;
  83.     unsigned int  blue;
  84.     int           numEntries;
  85.  
  86.     /* Ensure the given palette is valid. */
  87.  
  88.     if (!palette)
  89.         return(0);
  90.  
  91.     /* Open the input file. */
  92.     if (!(file = fopen(fileName, "r")))
  93.         return(0);
  94.  
  95.     /* Read the file ident string. */
  96.  
  97.     if (fgets(buffer, sizeof(buffer), file) == NULL)
  98.     {
  99.         fclose(file);
  100.         return(0);
  101.     }
  102.  
  103.     /* Ensure it matches the palette file ident string. */
  104.  
  105.     if (strncmp(buffer, PALETTEIDENTSTRING, sizeof(PALETTEIDENTSTRING) - 1) != 0)
  106.     {
  107.         fclose(file);
  108.         return(0);
  109.     }
  110.  
  111.     /* Read the version string. */    
  112.  
  113.     if (fgets(buffer, sizeof(buffer), file) == NULL)
  114.     {
  115.         fclose(file);
  116.         return(0);
  117.     }
  118.  
  119.     /* Ensure it matches the palette file version string. */
  120.  
  121.     if (strncmp(buffer, PALETTEVERSIONSTRING, sizeof(PALETTEVERSIONSTRING) - 1) != 0)
  122.     {
  123.         fclose(file);
  124.         return(0);
  125.     }
  126.  
  127.     /* Read the number of colors. */
  128.     if (fgets(buffer, sizeof(buffer), file) == NULL)
  129.     {
  130.         fclose(file);
  131.         return(0);
  132.     }
  133.  
  134.     /* Extract the number of colors. */
  135.  
  136.     if (sscanf(buffer, "%d", &numEntries) != 1)
  137.     {
  138.         fclose(file);
  139.         return (0);
  140.     }
  141.  
  142.     /* Ensure there are a sensible number of colors in the palette. */
  143.     if ((numEntries < 0) || (numEntries > 256) || (numEntries > palette_size))
  144.     {
  145.         fclose(file);
  146.         return 0;
  147.     }
  148.  
  149.     /* Read the palette entries. */
  150.  
  151.     for (i = 0; i < numEntries; i++)
  152.     {
  153.         /* Extract the red, green and blue color components. */
  154.  
  155.         if (fscanf(file, "%u %u %u", &red, &green, &blue) != 3)
  156.         {
  157.             fclose(file);
  158.             return(0);
  159.         }
  160.  
  161.         /* Store this palette entry. */
  162.  
  163.         palette[i].r = (unsigned char)red;
  164.         palette[i].g = (unsigned char)green;
  165.         palette[i].b = (unsigned char)blue;
  166.     }
  167.  
  168.     /*
  169.      * The palette was read successfully - close the file and return
  170.      * the new palette.
  171.      */
  172.  
  173.     fclose(file);
  174.  
  175.     return numEntries;
  176. }
  177.  
  178. /************************************************************************
  179.  *
  180.  *      Function:       WritePalette()
  181.  *                      
  182.  *      Description:    Write a palette into a file
  183.  *
  184.  *      Parameters:     filename - name of file to write.
  185.  *                      palette - array of RwPalleteEntry to write
  186.  *                      palette_size - number of elements in 'palette' array
  187.  *
  188.  *      Return Value:   TRUE on success. FALSE on failure
  189.  *
  190.  ************************************************************************/
  191. int WritePalette(char *fileName, RwPaletteEntry *palette, int palette_size )
  192. {
  193.     FILE *file;
  194.     int   i;
  195.  
  196.     /* Ensure a valid palette has been given. */
  197.     if (palette == NULL)
  198.         return FALSE;
  199.  
  200.     /* Open the output file. */
  201.     file = fopen(fileName, "w");
  202.     if (file == NULL)
  203.         return FALSE;
  204.  
  205.     /* Output the file ident string. */
  206.  
  207.     if (fprintf(file, "%s\n", PALETTEIDENTSTRING) < 0)
  208.     {
  209.         fclose(file);
  210.             return FALSE;
  211.     }
  212.  
  213.     /* Output the file version string. */
  214.  
  215.     if (fprintf(file, "%s\n", PALETTEVERSIONSTRING) < 0)
  216.     {
  217.         fclose(file);
  218.             return FALSE;
  219.     }
  220.  
  221.     /* Output the number of colors. */
  222.  
  223.     if (fprintf(file, "%d\n", palette_size) < 0)
  224.     {
  225.         fclose(file);
  226.             return FALSE;
  227.     }
  228.  
  229.     /* Output each palette entry in turn */
  230.     for (i = 0; i < palette_size; i++)
  231.     {
  232.         /* Output a single palette entry. */
  233.  
  234.         if (fprintf(file, "%u %u %u\n",
  235.                           (unsigned int)palette[i].r,
  236.                           (unsigned int)palette[i].g,
  237.                           (unsigned int)palette[i].b) < 0)
  238.         {
  239.             fclose(file);
  240.                 return FALSE;
  241.         }
  242.     }
  243.  
  244.     /* All went well, so close the file and return the palette. */
  245.  
  246.     fclose(file);
  247.  
  248.     return TRUE;
  249. }
  250.  
  251. /************************************************************************
  252.  *
  253.  *      Function:       BuildColorCube()
  254.  *                      
  255.  *      Description:    Construct a color cube (with the given number of 
  256.  *                      bits for red, green and blue) in the given palette 
  257.  *                      starting at the given offset.
  258.  *
  259.  *      Parameters:     palette - color cube returned here.
  260.  *                      palette_size - number of elements in 'palette' array
  261.  *                      redBits, greenBits, blueBits - number of bits
  262.  *                      of Red, Green and Blue respectively to use when
  263.  *                      constructing the color cube
  264.  *
  265.  *      Return Value:   TRUE on success. FALSE on failure
  266.  *
  267.  ************************************************************************/
  268. int 
  269. BuildColorCube(RwPaletteEntry *palette, int palette_size,
  270.                  int redBits, int greenBits, int blueBits)
  271. {
  272.     int             numReds;
  273.     int             numGreens;
  274.     int             numBlues;
  275.     int             red;
  276.     int             green;
  277.     int             blue;
  278.  
  279.     /* Ensure the palette is valid. */        
  280.  
  281.     if (palette == NULL)
  282.         return FALSE;
  283.  
  284.     /*
  285.      * Compute the number of reds, greens and blues given the number
  286.      * of bits specified.
  287.      */
  288.  
  289.     numReds   = 1 << redBits;
  290.     numGreens = 1 << greenBits;
  291.     numBlues  = 1 << blueBits;
  292.  
  293.     /*
  294.      * Ensure that the number of colors in the color cube will fit in
  295.      * the number of entries available in the palette.
  296.      */
  297.  
  298.     if (numReds * numGreens * numBlues > palette_size)
  299.         return FALSE;
  300.  
  301.     /*
  302.      * Generate the color cube.
  303.      */    
  304.  
  305.     for (red = 0; red < numReds; red++)
  306.     {
  307.         for (green = 0; green < numGreens; green++)
  308.         {
  309.             for (blue = 0; blue < numBlues; blue++)
  310.             {
  311.                 palette->r = (unsigned char)(((double)red   / (double)numReds) * 255.0);
  312.                 palette->g = (unsigned char)(((double)green / (double)numGreens) * 255.0);
  313.                 palette->b = (unsigned char)(((double)blue  / (double)numBlues)  * 255.0);
  314.                 palette++;
  315.             }
  316.         }
  317.     }
  318.  
  319.     /*
  320.      * All is well so return success.
  321.      */
  322.     return TRUE;
  323. }
  324.  
  325. /************************************************************************
  326.  *
  327.  *      Function:       CheckAndReadPalette()
  328.  *                      
  329.  *      Description:    If we are running on a palette based device then
  330.  *                      load the specified palette. The first 'n' palette
  331.  *                      entries will be loaded if the total number is
  332.  *                      greater that the number available.
  333.  *
  334.  ************************************************************************/
  335. void CheckAndReadPalette(char *filename)
  336. {
  337.     RwInt32 PaletteBased;
  338.  
  339.     /* Are we running on a palette based device */
  340.  
  341.     RwGetDeviceInfo(rwPALETTEBASED, &PaletteBased, sizeof(RwInt32));
  342.  
  343.     if (PaletteBased)
  344.     {
  345.         /* Yes */
  346.         RwInt32 firstEntry;
  347.         RwInt32 lastEntry;
  348.     RwPaletteEntry palette[256];
  349.     RwInt32 numEntries;
  350.  
  351.         /* find the range of available entries */
  352.       RwGetDeviceInfo(rwFIRSTPALETTEENTRY, &firstEntry, sizeof(firstEntry));
  353.       RwGetDeviceInfo(rwLASTPALETTEENTRY, &lastEntry, sizeof(lastEntry));
  354.  
  355.         /* Read the palette */
  356.     numEntries = (RwInt32)ReadPalette(filename, palette, sizeof(palette));
  357.  
  358.         /* clamp the number of entries that we can set */
  359.  
  360.         if (numEntries > lastEntry + 1 - firstEntry)
  361.             numEntries = lastEntry + 1 - firstEntry;
  362.  
  363.         /* Set the RenderWare palette */
  364.         RwSetPaletteEntries(firstEntry, numEntries, palette, (RwInt32)0);
  365.     }
  366. }
  367.  
  368. /**********************************************************************
  369.  *
  370.  * End of file.
  371.  *
  372.  **********************************************************************/
  373.