/*===========================================================================
 *
 * File:	Dfpal.CPP
 * Author:	Dave Humphrey (uesp@m0use.net)
 * Created On:	Tuesday, June 26, 2001
 *
 * Implements the CDFPalette class and other Daggerfall palette related
 * routines.
 *
 *=========================================================================*/

	/* Include Files */
#include "dfpal.h"
#include "common/file/genfile.h"


/*===========================================================================
 *
 * Begin Local Variable Definitions
 *
 *=========================================================================*/
  DEFINE_FILE();

	/* Include the default Daggerfall palette data */
  #include "dfdefpal.h"

/*===========================================================================
 *		End of Local Variable Definitions
 *=========================================================================*/


/*===========================================================================
 *
 * Class CDFPalette Constructor
 *
 *=========================================================================*/
CDFPalette::CDFPalette() {
  m_FileType    = DFPALETTE_PAL;
  m_PaletteSize = 256;

	/* Copy the default palette data to initialize palette data array */
  memcpy (m_Palette, GetDefaultDFPal(), 256*sizeof(rgbpalraw_t));
 }
/*===========================================================================
 *		End of Class CDFPalette Constructor
 *=========================================================================*/


/*===========================================================================
 *
 * Class CDFPalette Method - boolean Load (pFilename);
 *
 * Attempt to load the specified PAL or COL file.  Returns FALSE on any
 * error.  Attempts to determine the filetype by first the file extension
 * and then by the filesize. Use LoadPAL() or LoadCOL() to bypass this.
 *
 *=========================================================================*/
boolean CDFPalette::Load (const char* pFilename) {
  DEFINE_FUNCTION("CDFPalette::Load()");
  long FileSize;

	/* Ensure valid input */
  ASSERT(pFilename != NULL);
  
	/* Check for extension matches */
  if (CompareExtension(pFilename, "COL"))
    return LoadCOL(pFilename);
  else if (CompareExtension(pFilename, "PAL"))
    return LoadPAL(pFilename);

	/* Check for filesize matches */
  FileSize = GetFileSize(pFilename);

  if (FileSize == DF_PAL_FILESIZE)
    return LoadPAL(pFilename);
  else if (FileSize == DF_COL_FILESIZE)
    return LoadCOL(pFilename);
  
	/* Oh-oh, no idea what the filetype is, try COL by default */
  SystemLog.Printf ("No idea what file type is for DF palette %s!", pFilename);
  return LoadCOL(pFilename);
 }
/*===========================================================================
 *		End of Class Method CDFPalette::Load()
 *=========================================================================*/


/*===========================================================================
 *
 * Class CDFPalette Method - boolean LoadCOL (pFilename);
 *
 * Attempt to load the specified COL file.  Returns FALSE on any
 * error.
 *
 *=========================================================================*/
boolean CDFPalette::LoadCOL (const char* pFilename) {
  DEFINE_FUNCTION("CDFPalette::LoadCOL()");
  CGenFile COLFile;
  boolean  Result;

	/* Attempt to open the file for input */
  Result = COLFile.Open(pFilename, "rb");
  if (!Result) return (FALSE);

	/* Read the COL header all at once */
  Result = COLFile.Read((char *) &m_COLHeader, DF_COL_HEADERSIZE);
  if (!Result) return (FALSE);
  
	/* Compute the number of palette entries */
  m_FileType = DFPALETTE_PAL;
  m_PaletteSize = (m_COLHeader.FileSize - 8)/3;

	/* Ensure a valid number of colors */
  if (m_PaletteSize < 0 || m_PaletteSize >= DF_MAX_PALENTRIES) {
    ErrorHandler.AddError(DFERR_COL_NUMENTRIES);
    m_PaletteSize = 0;
    return (FALSE);
   }

	/* Read the palette data all at once */
  Result = COLFile.Read((char *) m_Palette, m_PaletteSize * sizeof(rgbpalraw_t));

  if (!Result) {
    m_PaletteSize = 0;
    return (FALSE);
   }

  return (TRUE);
 }
/*===========================================================================
 *		End of Class Method CDFPalette::LoadCOL()
 *=========================================================================*/


/*===========================================================================
 *
 * Class CDFPalette Method - boolean LoadPAL (pFilename);
 *
 * Attempt to load the specified PAL file.  Returns FALSE on any
 * error.
 *
 *=========================================================================*/
boolean CDFPalette::LoadPAL (const char* pFilename) {
  DEFINE_FUNCTION("CDFPalette::LoadPAL()");
  CGenFile PALFile;
  boolean  Result;

	/* Attempt to open the file for input */
  Result = PALFile.Open (pFilename, "rb");
  if (!Result) return (FALSE);

	/* Read the palette data all at once */
  Result = PALFile.Read((char *) m_Palette, sizeof(rgbpalraw_t) * 256);
  if (!Result) return (FALSE);

  m_FileType = DFPALETTE_PAL;
  m_PaletteSize = 256;
  return (TRUE);
 }
/*===========================================================================
 *		End of Class Method CDFPalette::LoadPAL()
 *=========================================================================*/


/*===========================================================================
 *
 * Function - rgbpalraw_t* GetDefaultDFPal (void);
 *
 * Returns a pointer to the default Daggerfall raw palette structure.
 * The raw palette has color values ranging from 0 to 63.
 *
 *=========================================================================*/
rgbpalraw_t* GetDefaultDFPal (void) {
  return (&l_DFDefaultPal[0]);
 }
/*===========================================================================
 *		End of Function GetDefaultDFPal()
 *=========================================================================*/