/*=========================================================================== * * File: Dfimgutl.CPP * Author: Dave Humphrey (uesp@m0use.net) * Created On: Tuesday, June 26, 2001 * * Implements Daggerfall IMG file related routines. * *=========================================================================*/ /* Include Files */ #include "uesp/dagger/image/dfimgutl.h" #include "uesp/dagger/image/dfpal.h" #include "file/pcx.h" #include "file/bmpfile.h" /*=========================================================================== * * Begin Local Variable Definitions * *=========================================================================*/ DEFINE_FILE(); /*=========================================================================== * End of Local Variable Definitions *=========================================================================*/ /*=========================================================================== * * Function - boolean ExportDFCIFtoPCX (pFilename, CIFFile); * * Exports the given CIF file to multiple PCX files. Returns FALSE on any * error. Exports files with a 2 digit number starting at 00 appended * before the PCX extension. For example: * WEAPON104##.PCX => WEAPON10400.PCX, WEPOAN10401.PCX, etc... * *=========================================================================*/ boolean ExportDFCIFtoPCX (const char* pFilename, const CDFCifFile& CIFFile) { DEFINE_FUNCTION("ExportDFCIFtoPCX()"); boolean Result; CDFImgFile* pImage; char* pEndFileBuffer; char FileBuffer[_MAX_PATH+10]; char NumBuffer[16]; int Index; /* Ensure valid input */ ASSERT(pFilename != NULL); /* Copy the file name */ strnncpy(FileBuffer, pFilename, _MAX_PATH); RemoveExtension(FileBuffer); pEndFileBuffer = &FileBuffer[0] + strlen(FileBuffer); /* Output each image in the CIF file */ for (Index = 0; Index < CIFFile.GetNumImages(); Index++) { /* Create the PCX filename string */ *pEndFileBuffer = NULL_CHAR; snprintf(NumBuffer, 10, "%02d.pcx", Index); strcat(FileBuffer, NumBuffer); pImage = (CDFImgFile *) (CIFFile.GetImage(Index)); /* Export the IMG file image */ Result = ExportDFIMGtoPCX(FileBuffer, *(CDFImgFile *)pImage); if (!Result) return (FALSE); } return (TRUE); } /*=========================================================================== * End of Function ExportDFCIFtoPCX() *=========================================================================*/ /*=========================================================================== * * Function - boolean ExportDFIMGtoPCX (pFilename, IMGFile); * * Save a DF IMG file to a simple PCX file. Uses the image's palette if * present or the default Daggerfall palette otherwise. Returns FALSE * on any error. * *=========================================================================*/ boolean ExportDFIMGtoPCX (const char* pFilename, const CDFImgFile& IMGFile) { DEFINE_FUNCTION("ExportDFIMGtoPCX()"); CPcxFile PCXFile; rgbpal_t Palette[256]; boolean Result; /* Choose the palette to use */ if (IMGFile.HasPalette()) CopyRGBPalette(&Palette[0], IMGFile.GetPalette(), 256, COPYRGBPAL_CONVERTFROMRAW); else CopyRGBPalette(&Palette[0], GetDefaultDFPal(), 256, COPYRGBPAL_CONVERTFROMRAW); /* Attempt to export the PCX file */ Result = PCXFile.ExportLBM(pFilename, IMGFile.GetWidth(), IMGFile.GetHeight(), IMGFile.GetImageData(), (byte*) &Palette[0]); return (Result); } /*=========================================================================== * End of Function ExportDFIMGtoPCX() *=========================================================================*/ /*=========================================================================== * * Function - boolean ExportDFTexturetoPCX (pFilename, TextureFile); * * Exports all images in the given texture file to PCX files. Returns * FALSE on any error. The extension in the given filename is removed * and the format: * iiiss.PCX * where iii = Texture image index (0 to 127) * ss = Texture image sub-image index (0 to 99) * * is appended to the string for each image/sub-image in the texture. * *=========================================================================*/ boolean ExportDFTexturetoPCX (const char* pFilename, const CDFTextureFile& TextureFile) { DEFINE_FUNCTION("ExportDFTexturetoPCX()"); CDFTextureImage* pImage; char* pEndFileBuffer; char FileBuffer[_MAX_PATH+10]; char NumBuffer[16]; boolean Result; int Index; /* Copy the file name */ strnncpy(FileBuffer, pFilename, _MAX_PATH); RemoveExtension(FileBuffer); pEndFileBuffer = &FileBuffer[0] + strlen(FileBuffer); /* Export each image in the texture file */ for (Index = 0; Index < TextureFile.GetNumImages(); Index++) { /* Create the PCX filename string */ *pEndFileBuffer = NULL_CHAR; snprintf(NumBuffer, 10, "%03d", Index); strcpy(pEndFileBuffer, NumBuffer); pImage = TextureFile.GetImage(Index); /* Attempt to export the texture image */ Result = ExportDFTxtImgtoPCX(FileBuffer, *pImage); } return (Result); } /*=========================================================================== * End of Function ExportDFTexturetoPCX() *=========================================================================*/ /*=========================================================================== * * Function - boolean ExportDFTxtImgtoPCX (pFilename, ImageFile); * * Exports the images in the given texture image data to the given filename. * Returns FALSE on any error. The extension from the given string is * removed and replaced with: * ss.PCX * where ss is the sub-image index that the texture image contains. If * there is only one image, only a .PCX is appended to the filename. * *=========================================================================*/ boolean ExportDFTxtImgtoPCX (const char* pFilename, const CDFTextureImage& ImageFile) { DEFINE_FUNCTION("ExportDFTxtImgtoPCX()"); CPcxFile PCXFile; rgbpal_t Palette[256]; char* pEndFileBuffer; char FileBuffer[_MAX_PATH+10]; char NumBuffer[16]; boolean Result; int Index; /* Create the palette to use */ CopyRGBPalette(&Palette[0], GetDefaultDFPal(), 256, COPYRGBPAL_CONVERTFROMRAW); /* Copy the file name */ strnncpy(FileBuffer, pFilename, _MAX_PATH); RemoveExtension(FileBuffer); pEndFileBuffer = &FileBuffer[0] + strlen(FileBuffer); /* Export each sub-image in the image file */ for (Index = 0; Index < ImageFile.GetNumSubImages(); Index++) { /* Create the PCX filename string */ *pEndFileBuffer = NULL_CHAR; if (ImageFile.GetNumSubImages() > 1) snprintf(NumBuffer, 10, "%02d.pcx", Index); else strcpy (NumBuffer, ".pcx"); strcpy(pEndFileBuffer, NumBuffer); /* Attempt to export the PCX file */ Result = PCXFile.ExportLBM(FileBuffer, ImageFile.GetWidth(Index), ImageFile.GetHeight(Index), ImageFile.GetImageData(Index), (byte*) &Palette[0]); if (!Result) return (FALSE); } return (TRUE); } /*=========================================================================== * End of Function ExportDFTxtImgtoPCX() *=========================================================================*/ /*=========================================================================== * * Function - boolean ExportDFTxtImgtoBMP (pFilename, ImageFile); * * Exports the images in the given texture image data to the given filename. * Returns FALSE on any error. The extension from the given string is * removed and replaced with: * ss.BMP * where ss is the sub-image index that the texture image contains. If * there is only one image, only a .BMP is appended to the filename. * *=========================================================================*/ boolean ExportDFTxtImgtoBMP (const char* pFilename, const CDFTextureImage& ImageFile) { DEFINE_FUNCTION("ExportDFTxtImgtoBMP()"); CBmpFile BMPFile; rgbpal_t Palette[256]; char* pEndFileBuffer; char FileBuffer[_MAX_PATH+10]; char NumBuffer[16]; boolean Result; int Index; /* Create the palette to use */ CopyRGBPalette(&Palette[0], GetDefaultDFPal(), 256, COPYRGBPAL_CONVERTFROMRAW); /* Copy the file name */ strnncpy(FileBuffer, pFilename, _MAX_PATH); RemoveExtension(FileBuffer); pEndFileBuffer = &FileBuffer[0] + strlen(FileBuffer); /* Export each sub-image in the image file */ for (Index = 0; Index < ImageFile.GetNumSubImages(); Index++) { /* Create the PCX filename string */ *pEndFileBuffer = NULL_CHAR; if (ImageFile.GetNumSubImages() > 1) snprintf(NumBuffer, 10, "%02d.bmp", Index); else strcpy (NumBuffer, ".bmp"); strcpy(pEndFileBuffer, NumBuffer); /* Attempt to export the PCX file */ Result = BMPFile.ExportLBM(FileBuffer, ImageFile.GetWidth(Index), ImageFile.GetHeight(Index), ImageFile.GetImageData(Index), (byte*) &Palette[0]); if (!Result) return (FALSE); } return (TRUE); return (TRUE); } /*=========================================================================== * End of Function ExportDFTxtImgtoBMP() *=========================================================================*/