- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-IL/src/il_wal.c

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Copyright (C) 2000-2009 by Denton Woods
    00005 // Last modified: 01/30/2009
    00006 //
    00007 // Filename: src-IL/src/il_wal.c
    00008 //
    00009 // Description: Loads a Quake .wal texture.
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 #include "il_internal.h"
    00015 #ifndef IL_NO_WAL
    00016 #include "il_manip.h"
    00017 #include "il_q2pal.h"
    00018
    00019
    00020 typedef struct WALHEAD
    00021 {
    00022         ILbyte  FileName[32];   // Image name
    00023         ILuint  Width;                  // Width of first image
    00024         ILuint  Height;                 // Height of first image
    00025         ILuint  Offsets[4];             // Offsets to image data
    00026         ILbyte  AnimName[32];   // Name of next frame
    00027         ILuint  Flags;                  // ??
    00028         ILuint  Contents;               // ??
    00029         ILuint  Value;                  // ??
    00030 } WALHEAD;
    00031
    00032 ILboolean iLoadWalInternal(void);
    00033
    00034
    00036 ILboolean ilLoadWal(ILconst_string FileName)
    00037 {
    00038         ILHANDLE        WalFile;
    00039         ILboolean       bWal = IL_FALSE;
    00040
    00041         WalFile = iopenr(FileName);
    00042         if (WalFile == NULL) {
    00043                 ilSetError(IL_COULD_NOT_OPEN_FILE);
    00044                 return bWal;
    00045         }
    00046
    00047         bWal = ilLoadWalF(WalFile);
    00048         icloser(WalFile);
    00049
    00050         return bWal;
    00051 }
    00052
    00053
    00055 ILboolean ilLoadWalF(ILHANDLE File)
    00056 {
    00057         ILuint          FirstPos;
    00058         ILboolean       bRet;
    00059
    00060         iSetInputFile(File);
    00061         FirstPos = itell();
    00062         bRet = iLoadWalInternal();
    00063         iseek(FirstPos, IL_SEEK_SET);
    00064
    00065         return bRet;
    00066 }
    00067
    00068
    00070 ILboolean ilLoadWalL(const void *Lump, ILuint Size)
    00071 {
    00072         iSetInputLump(Lump, Size);
    00073         return iLoadWalInternal();
    00074 }
    00075
    00076
    00077 ILboolean iLoadWalInternal()
    00078 {
    00079         WALHEAD Header;
    00080         ILimage *Mipmaps[3], *CurImage;
    00081         ILuint  i, NewW, NewH;
    00082
    00083         if (iCurImage == NULL) {
    00084                 ilSetError(IL_ILLEGAL_OPERATION);
    00085                 return IL_FALSE;
    00086         }
    00087         CurImage = iCurImage;
    00088
    00089
    00090         // Read header
    00091         iread(&Header.FileName, 1, 32);
    00092         Header.Width = GetLittleUInt();
    00093         Header.Height = GetLittleUInt();
    00094
    00095         for (i = 0; i < 4; i++)
    00096                 Header.Offsets[i] = GetLittleUInt();
    00097
    00098         iread(Header.AnimName, 1, 32);
    00099         Header.Flags = GetLittleUInt();
    00100         Header.Contents = GetLittleUInt();
    00101         Header.Value = GetLittleUInt();
    00102
    00103         if (!ilTexImage(Header.Width, Header.Height, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL))
    00104                 return IL_FALSE;
    00105
    00106         for (i = 0; i < 3; i++) {
    00107                 Mipmaps[i] = (ILimage*)icalloc(sizeof(ILimage), 1);
    00108                 if (Mipmaps[i] == NULL)
    00109                         goto cleanup_error;
    00110                 Mipmaps[i]->Pal.Palette = (ILubyte*)ialloc(768);
    00111                 if (Mipmaps[i]->Pal.Palette == NULL)
    00112                         goto cleanup_error;
    00113                 memcpy(Mipmaps[i]->Pal.Palette, ilDefaultQ2Pal, 768);
    00114                 Mipmaps[i]->Pal.PalType = IL_PAL_RGB24;
    00115         }
    00116
    00117         NewW = Header.Width;
    00118         NewH = Header.Height;
    00119         for (i = 0; i < 3; i++) {
    00120                 NewW /= 2;
    00121                 NewH /= 2;
    00122                 iCurImage = Mipmaps[i];
    00123                 if (!ilTexImage(NewW, NewH, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL))
    00124                         goto cleanup_error;
    00125                 // Don't set until now so ilTexImage won't get rid of the palette.
    00126                 Mipmaps[i]->Pal.PalSize = 768;
    00127                 Mipmaps[i]->Origin = IL_ORIGIN_UPPER_LEFT;
    00128         }
    00129
    00130         iCurImage = CurImage;
    00131         ilCloseImage(iCurImage->Mipmaps);
    00132         iCurImage->Mipmaps = Mipmaps[0];
    00133         Mipmaps[0]->Mipmaps = Mipmaps[1];
    00134         Mipmaps[1]->Mipmaps = Mipmaps[2];
    00135
    00136         iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
    00137
    00138         if (iCurImage->Pal.Palette && iCurImage->Pal.PalSize && iCurImage->Pal.PalType != IL_PAL_NONE)
    00139                 ifree(iCurImage->Pal.Palette);
    00140         iCurImage->Pal.Palette = (ILubyte*)ialloc(768);
    00141         if (iCurImage->Pal.Palette == NULL)
    00142                 goto cleanup_error;
    00143
    00144         iCurImage->Pal.PalSize = 768;
    00145         iCurImage->Pal.PalType = IL_PAL_RGB24;
    00146         memcpy(iCurImage->Pal.Palette, ilDefaultQ2Pal, 768);
    00147
    00148         iseek(Header.Offsets[0], IL_SEEK_SET);
    00149         if (iread(iCurImage->Data, Header.Width * Header.Height, 1) != 1)
    00150                 goto cleanup_error;
    00151
    00152         for (i = 0; i < 3; i++) {
    00153                 iseek(Header.Offsets[i+1], IL_SEEK_SET);
    00154                 if (iread(Mipmaps[i]->Data, Mipmaps[i]->Width * Mipmaps[i]->Height, 1) != 1)
    00155                         goto cleanup_error;
    00156         }
    00157
    00158         // Fixes all images, even mipmaps.
    00159         ilFixImage();
    00160
    00161         return IL_TRUE;
    00162
    00163 cleanup_error:
    00164         for (i = 0; i < 3; i++) {
    00165                 ilCloseImage(Mipmaps[i]);
    00166         }
    00167         return IL_FALSE;
    00168 }
    00169
    00170
    00171 #endif//IL_NO_WAL