warzone2100/lib/ivis_common/bitimage.c

141 lines
3.6 KiB
C
Raw Normal View History

/*
This file is part of Warzone 2100.
Copyright (C) 1999-2004 Eidos Interactive
Copyright (C) 2005-2007 Warzone Resurrection Project
Warzone 2100 is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Warzone 2100 is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Warzone 2100; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "piepalette.h"
#include "tex.h"
#include "ivispatch.h"
#include "bitimage.h"
#include "lib/framework/frameresource.h"
static BOOL LoadTextureFile(const char *FileName, iTexture *pSprite, int *texPageID)
{
unsigned int i = 0;
debug(LOG_TEXTURE, "LoadTextureFile: %s", FileName);
if (!resPresent("IMGPAGE", FileName)) {
debug(LOG_ERROR, "Texture file \"%s\" not preloaded.", FileName);
assert(FALSE);
return FALSE;
} else {
*pSprite = *(iTexture*)resGetData("IMGPAGE", FileName);
debug(LOG_TEXTURE, "Load texture from resource cache: %s (%d, %d)",
FileName, pSprite->width, pSprite->height);
}
/* We have already loaded this one? */
while (i < _TEX_INDEX) {
if (strcasecmp(FileName, _TEX_PAGE[i].name) == 0) {
*texPageID = _TEX_PAGE[i].id;
debug(LOG_TEXTURE, "LoadTextureFile: already loaded");
return TRUE;
}
i++;
}
*texPageID = pie_AddTexPage(pSprite, FileName, 1, TRUE);
return TRUE;
}
IMAGEFILE *iV_LoadImageFile(const char *FileData, WZ_DECL_UNUSED const UDWORD FileSize)
{
const char *Ptr;
IMAGEHEADER *Header;
IMAGEFILE *ImageFile;
IMAGEDEF *ImageDef;
unsigned int i = 0;
Ptr = FileData;
Header = (IMAGEHEADER*)Ptr;
Ptr += sizeof(IMAGEHEADER);
endian_uword(&Header->Version);
endian_uword(&Header->NumImages);
endian_uword(&Header->BitDepth);
endian_uword(&Header->NumTPages);
ImageFile = (IMAGEFILE*)malloc(sizeof(IMAGEFILE));
if(ImageFile == NULL) {
debug( LOG_ERROR, "Out of memory" );
return NULL;
}
ImageFile->TexturePages = (iTexture*)malloc(sizeof(iTexture)*Header->NumTPages);
if(ImageFile->TexturePages == NULL) {
debug( LOG_ERROR, "Out of memory" );
return NULL;
}
ImageFile->ImageDefs = (IMAGEDEF*)malloc(sizeof(IMAGEDEF)*Header->NumImages);
if(ImageFile->ImageDefs == NULL) {
debug( LOG_ERROR, "Out of memory" );
return NULL;
}
ImageFile->Header = *Header;
// Load the texture pages.
for (i = 0; i < Header->NumTPages; i++) {
int tmp=0; /* Workaround for MacOS gcc 4.0.0 bug. */
LoadTextureFile((char*)Header->TPageFiles[i],
&ImageFile->TexturePages[i],
&tmp);
ImageFile->TPageIDs[i] = tmp;
}
ImageDef = (IMAGEDEF*)Ptr;
for(i=0; i<Header->NumImages; i++) {
endian_uword(&ImageDef->TPageID);
endian_uword(&ImageDef->PalID);
endian_uword(&ImageDef->Tu);
endian_uword(&ImageDef->Tv);
endian_uword(&ImageDef->Width);
endian_uword(&ImageDef->Height);
endian_sword(&ImageDef->XOffset);
endian_sword(&ImageDef->YOffset);
ImageFile->ImageDefs[i] = *ImageDef;
if( (ImageDef->Width <= 0) || (ImageDef->Height <= 0) ) {
debug( LOG_ERROR, "Illegal image size" );
return NULL;
}
ImageDef++;
}
return ImageFile;
}
void iV_FreeImageFile(IMAGEFILE *ImageFile)
{
free(ImageFile->TexturePages);
free(ImageFile->ImageDefs);
free(ImageFile);
}