home *** CD-ROM | disk | FTP | other *** search
/ Learn 3D Graphics Programming on the PC / Learn_3D_Graphics_Programming_on_the_PC_Ferraro.iso / rwwin / xal.c_ / xal.bin
Text File  |  1995-11-14  |  5KB  |  176 lines

  1. /**********************************************************************
  2.  *
  3.  * File :     pal.c
  4.  *
  5.  * Abstract : Implementation of a simple function which reads
  6.  *            PaintShopPro palette files.
  7.  *
  8.  *            This application had been written to be compatible with
  9.  *            both the fixed and floating-point versions of the
  10.  *            RenderWare library, i.e., it uses the macros CREAL,
  11.  *            INT2REAL, RAdd, RDiv, RSub etc. If your application is
  12.  *            intended for the floating-point version of the library
  13.  *            only these macros are not necessary.
  14.  *
  15.  *            Please note that this application is intended for
  16.  *            demonstration purposes only. No support will be
  17.  *            provided for this code and it comes with no warranty.
  18.  *
  19.  **********************************************************************
  20.  *
  21.  * This file is a product of Criterion Software Ltd.
  22.  *
  23.  * This file is provided as is with no warranties of any kind and is
  24.  * provided without any obligation on Criterion Software Ltd. or
  25.  * Canon Inc. to assist in its use or modification.
  26.  *
  27.  * Criterion Software Ltd. will not, under any
  28.  * circumstances, be liable for any lost revenue or other damages arising
  29.  * from the use of this file.
  30.  *
  31.  * Copyright (c) 1994, 1995 Criterion Software Ltd.
  32.  * All Rights Reserved.
  33.  *
  34.  * RenderWare is a trademark of Canon Inc.
  35.  *
  36.  **********************************************************************/
  37.  
  38. /**********************************************************************
  39.  *
  40.  * Header files.
  41.  *
  42.  **********************************************************************/
  43.  
  44. #include <windows.h>
  45. #include <string.h>
  46.  
  47. #include <rwlib.h>
  48. #include <rwwin.h>
  49.  
  50. #include "pal.h"
  51.  
  52. /**********************************************************************
  53.  *
  54.  * Constant definitions.
  55.  *
  56.  **********************************************************************/
  57.  
  58. /*
  59.  * Palette file strings.
  60.  */
  61. #define PALETTEIDENTSTRING        "JASC-PAL"
  62. #define PALETTEVERSIONSTRING      "0100"
  63.  
  64. /**********************************************************************
  65.  *
  66.  * Functions.
  67.  *
  68.  **********************************************************************/
  69.  
  70. /**********************************************************************/
  71.  
  72. /*
  73.  * Read a Paint Shop Pro format palette file into an array of
  74.  * RwPaletteEntrys and return the number of entrys read.
  75.  */
  76. RwInt32
  77. ReadPalette(char *pathName, RwPaletteEntry *palEntry)
  78. {
  79.     FILE *file;
  80.     char  buffer[128];
  81.     int   numPalEntries;
  82.     int   i;
  83.     int   r;
  84.     int   g;
  85.     int   b;
  86.     
  87.     /*
  88.      * Open the input file.
  89.      */
  90.     file = fopen(pathName, "r");    
  91.     if (file == NULL)
  92.         return 0;
  93.  
  94.     /*
  95.      * Read the file ident string and ensure it matches the PaintShopPro
  96.      * palette ident string.
  97.      */
  98.     if (fgets(buffer, sizeof(buffer), file) == NULL)
  99.     {
  100.         fclose(file);
  101.         return 0;
  102.     }
  103.     if (strncmp(buffer, PALETTEIDENTSTRING, sizeof(PALETTEIDENTSTRING) - 1) != 0)
  104.     {
  105.         fclose(file);
  106.         return 0;
  107.     }
  108.  
  109.     /*
  110.      * Read the version string and ensure it matches the palette
  111.      * file version string.
  112.      */    
  113.     if (fgets(buffer, sizeof(buffer), file) == NULL)
  114.     {
  115.         fclose(file);
  116.         return 0;
  117.     }
  118.     if (strncmp(buffer, PALETTEVERSIONSTRING, sizeof(PALETTEVERSIONSTRING) - 1) != 0)
  119.     {
  120.         fclose(file);
  121.         return 0;
  122.     }
  123.  
  124.     /*
  125.      * Read and extract the number of colors.
  126.      */
  127.     if (fgets(buffer, sizeof(buffer), file) == NULL)
  128.     {
  129.         fclose(file);
  130.         return 0;
  131.     }
  132.     if (sscanf(buffer, "%d", &numPalEntries) != 1)
  133.     {
  134.         fclose(file);
  135.         return 0;
  136.     }
  137.  
  138.     /*
  139.      * Ensure there are a sensible number of colors in the palette.
  140.      */
  141.     if ((numPalEntries < 0) || (numPalEntries > 256))
  142.     {
  143.         fclose(file);
  144.         return 0;
  145.     }
  146.  
  147.     /*
  148.      * Read the palette entries themselves.
  149.      */
  150.     for (i = 0; i < numPalEntries; i++)
  151.     {
  152.         /*
  153.          * Get the red, green and blue color components and
  154.          * store them in the palette.
  155.          */
  156.         if (fscanf(file, "%u %u %u", &r, &g, &b) != 3)
  157.         {
  158.             fclose(file);
  159.             return 0;
  160.         }
  161.         palEntry[i].r = (unsigned char)r;
  162.         palEntry[i].g = (unsigned char)g;
  163.         palEntry[i].b = (unsigned char)b;
  164.     }
  165.  
  166.     /*
  167.      * The palette was read successfully - close the file and return
  168.      * the new palette.
  169.      */
  170.     fclose(file);
  171.     
  172.     return (RwInt32)numPalEntries;
  173. }
  174.  
  175. /**********************************************************************/
  176.