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
Wrap
Text File
|
1995-11-14
|
5KB
|
176 lines
/**********************************************************************
*
* File : pal.c
*
* Abstract : Implementation of a simple function which reads
* PaintShopPro palette files.
*
* This application had been written to be compatible with
* both the fixed and floating-point versions of the
* RenderWare library, i.e., it uses the macros CREAL,
* INT2REAL, RAdd, RDiv, RSub etc. If your application is
* intended for the floating-point version of the library
* only these macros are not necessary.
*
* Please note that this application is intended for
* demonstration purposes only. No support will be
* provided for this code and it comes with no warranty.
*
**********************************************************************
*
* This file is a product of Criterion Software Ltd.
*
* This file is provided as is with no warranties of any kind and is
* provided without any obligation on Criterion Software Ltd. or
* Canon Inc. to assist in its use or modification.
*
* Criterion Software Ltd. will not, under any
* circumstances, be liable for any lost revenue or other damages arising
* from the use of this file.
*
* Copyright (c) 1994, 1995 Criterion Software Ltd.
* All Rights Reserved.
*
* RenderWare is a trademark of Canon Inc.
*
**********************************************************************/
/**********************************************************************
*
* Header files.
*
**********************************************************************/
#include <windows.h>
#include <string.h>
#include <rwlib.h>
#include <rwwin.h>
#include "pal.h"
/**********************************************************************
*
* Constant definitions.
*
**********************************************************************/
/*
* Palette file strings.
*/
#define PALETTEIDENTSTRING "JASC-PAL"
#define PALETTEVERSIONSTRING "0100"
/**********************************************************************
*
* Functions.
*
**********************************************************************/
/**********************************************************************/
/*
* Read a Paint Shop Pro format palette file into an array of
* RwPaletteEntrys and return the number of entrys read.
*/
RwInt32
ReadPalette(char *pathName, RwPaletteEntry *palEntry)
{
FILE *file;
char buffer[128];
int numPalEntries;
int i;
int r;
int g;
int b;
/*
* Open the input file.
*/
file = fopen(pathName, "r");
if (file == NULL)
return 0;
/*
* Read the file ident string and ensure it matches the PaintShopPro
* palette ident string.
*/
if (fgets(buffer, sizeof(buffer), file) == NULL)
{
fclose(file);
return 0;
}
if (strncmp(buffer, PALETTEIDENTSTRING, sizeof(PALETTEIDENTSTRING) - 1) != 0)
{
fclose(file);
return 0;
}
/*
* Read the version string and ensure it matches the palette
* file version string.
*/
if (fgets(buffer, sizeof(buffer), file) == NULL)
{
fclose(file);
return 0;
}
if (strncmp(buffer, PALETTEVERSIONSTRING, sizeof(PALETTEVERSIONSTRING) - 1) != 0)
{
fclose(file);
return 0;
}
/*
* Read and extract the number of colors.
*/
if (fgets(buffer, sizeof(buffer), file) == NULL)
{
fclose(file);
return 0;
}
if (sscanf(buffer, "%d", &numPalEntries) != 1)
{
fclose(file);
return 0;
}
/*
* Ensure there are a sensible number of colors in the palette.
*/
if ((numPalEntries < 0) || (numPalEntries > 256))
{
fclose(file);
return 0;
}
/*
* Read the palette entries themselves.
*/
for (i = 0; i < numPalEntries; i++)
{
/*
* Get the red, green and blue color components and
* store them in the palette.
*/
if (fscanf(file, "%u %u %u", &r, &g, &b) != 3)
{
fclose(file);
return 0;
}
palEntry[i].r = (unsigned char)r;
palEntry[i].g = (unsigned char)g;
palEntry[i].b = (unsigned char)b;
}
/*
* The palette was read successfully - close the file and return
* the new palette.
*/
fclose(file);
return (RwInt32)numPalEntries;
}
/**********************************************************************/