- Main Page
- Data Structures
- Files

- File List

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

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Copyright (C) 2000-2008 by Denton Woods
    00005 // Last modified: 06/07/2007
    00006 //
    00007 // Filename: src-IL/src/il_doom.c
    00008 //
    00009 // Description: Reads Doom textures and flats
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 #include "il_internal.h"
    00015 #ifndef IL_NO_DOOM
    00016 #include "il_manip.h"
    00017 #include "il_pal.h"
    00018 #include "il_doompal.h"
    00019
    00020
    00021 ILboolean iLoadDoomInternal(void);
    00022 ILboolean iLoadDoomFlatInternal(void);
    00023
    00024
    00025 //
    00026 // READ A DOOM IMAGE
    00027 //
    00028
    00030 ILboolean ilLoadDoom(ILconst_string FileName)
    00031 {
    00032         ILHANDLE        DoomFile;
    00033         ILboolean       bDoom = IL_FALSE;
    00034
    00035         // Not sure of any kind of specified extension...maybe .lmp?
    00036         /*if (!iCheckExtension(FileName, "")) {
    00037                 ilSetError(IL_INVALID_EXTENSION);
    00038                 return NULL;
    00039         }*/
    00040
    00041         DoomFile = iopenr(FileName);
    00042         if (DoomFile == NULL) {
    00043                 ilSetError(IL_COULD_NOT_OPEN_FILE);
    00044                 return bDoom;
    00045         }
    00046
    00047         bDoom = ilLoadDoomF(DoomFile);
    00048         icloser(DoomFile);
    00049
    00050         return bDoom;
    00051 }
    00052
    00053
    00055 ILboolean ilLoadDoomF(ILHANDLE File)
    00056 {
    00057         ILuint          FirstPos;
    00058         ILboolean       bRet;
    00059
    00060         iSetInputFile(File);
    00061         FirstPos = itell();
    00062         bRet = iLoadDoomInternal();
    00063         iseek(FirstPos, IL_SEEK_SET);
    00064
    00065         return bRet;
    00066 }
    00067
    00068
    00070 ILboolean ilLoadDoomL(const void *Lump, ILuint Size)
    00071 {
    00072         iSetInputLump(Lump, Size);
    00073         return iLoadDoomInternal();
    00074 }
    00075
    00076
    00077 // From the DTE sources (mostly by Denton Woods with corrections by Randy Heit)
    00078 ILboolean iLoadDoomInternal()
    00079 {
    00080         ILshort width, height, graphic_header[2], column_loop, row_loop;
    00081         ILint   column_offset, pointer_position, first_pos;
    00082         ILubyte post, topdelta, length;
    00083         ILubyte *NewData;
    00084         ILuint  i;
    00085
    00086         if (iCurImage == NULL) {
    00087                 ilSetError(IL_ILLEGAL_OPERATION);
    00088                 return IL_FALSE;
    00089         }
    00090
    00091         first_pos = itell();  // Needed to go back to the offset table
    00092         width = GetLittleShort();
    00093         height = GetLittleShort();
    00094         graphic_header[0] = GetLittleShort();  // Not even used
    00095         graphic_header[1] = GetLittleShort();  // Not even used
    00096
    00097         if (!ilTexImage(width, height, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL)) {
    00098                 return IL_FALSE;
    00099         }
    00100         iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
    00101
    00102         iCurImage->Pal.Palette = (ILubyte*)ialloc(IL_DOOMPAL_SIZE);
    00103         if (iCurImage->Pal.Palette == NULL) {
    00104                 return IL_FALSE;
    00105         }
    00106         iCurImage->Pal.PalSize = IL_DOOMPAL_SIZE;
    00107         iCurImage->Pal.PalType = IL_PAL_RGB24;
    00108         memcpy(iCurImage->Pal.Palette, ilDefaultDoomPal, IL_DOOMPAL_SIZE);
    00109
    00110         // 247 is always the transparent colour (usually cyan)
    00111         memset(iCurImage->Data, 247, iCurImage->SizeOfData);
    00112
    00113         for (column_loop = 0; column_loop < width; column_loop++) {
    00114                 column_offset = GetLittleInt();
    00115                 pointer_position = itell();
    00116                 iseek(first_pos + column_offset, IL_SEEK_SET);
    00117
    00118                 while (1) {
    00119                         if (iread(&topdelta, 1, 1) != 1)
    00120                                 return IL_FALSE;
    00121                         if (topdelta == 255)
    00122                                 break;
    00123                         if (iread(&length, 1, 1) != 1)
    00124                                 return IL_FALSE;
    00125                         if (iread(&post, 1, 1) != 1)
    00126                                 return IL_FALSE; // Skip extra byte for scaling
    00127
    00128                         for (row_loop = 0; row_loop < length; row_loop++) {
    00129                                 if (iread(&post, 1, 1) != 1)
    00130                                         return IL_FALSE;
    00131                                 if (row_loop + topdelta < height)
    00132                                         iCurImage->Data[(row_loop+topdelta) * width + column_loop] = post;
    00133                         }
    00134                         iread(&post, 1, 1); // Skip extra scaling byte
    00135                 }
    00136
    00137                 iseek(pointer_position, IL_SEEK_SET);
    00138         }
    00139
    00140         // Converts palette entry 247 (cyan) to transparent.
    00141         if (ilGetBoolean(IL_CONV_PAL) == IL_TRUE) {
    00142                 NewData = (ILubyte*)ialloc(iCurImage->SizeOfData * 4);
    00143                 if (NewData == NULL) {
    00144                         return IL_FALSE;
    00145                 }
    00146
    00147                 for (i = 0; i < iCurImage->SizeOfData; i++) {
    00148                         NewData[i * 4] = iCurImage->Pal.Palette[iCurImage->Data[i]];
    00149                         NewData[i * 4] = iCurImage->Pal.Palette[iCurImage->Data[i]];
    00150                         NewData[i * 4] = iCurImage->Pal.Palette[iCurImage->Data[i]];
    00151                         NewData[i * 4 + 3] = iCurImage->Data[i] != 247 ? 255 : 0;
    00152                 }
    00153
    00154                 if (!ilTexImage(iCurImage->Width, iCurImage->Height, iCurImage->Depth,
    00155                         4, IL_RGBA, iCurImage->Type, NewData)) {
    00156                         ifree(NewData);
    00157                         return IL_FALSE;
    00158                 }
    00159                 iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
    00160                 ifree(NewData);
    00161         }
    00162
    00163         ilFixImage();
    00164
    00165         return IL_TRUE;
    00166 }
    00167
    00168
    00169 //
    00170 // READ A DOOM FLAT
    00171 //
    00172
    00174 ILboolean ilLoadDoomFlat(ILconst_string FileName)
    00175 {
    00176         ILHANDLE        FlatFile;
    00177         ILboolean       bFlat = IL_FALSE;
    00178
    00179         // Not sure of any kind of specified extension...maybe .lmp?
    00180         /*if (!iCheckExtension(FileName, "")) {
    00181                 ilSetError(IL_INVALID_EXTENSION);
    00182                 return NULL;
    00183         }*/
    00184
    00185         FlatFile = iopenr(FileName);
    00186         if (FlatFile == NULL) {
    00187                 ilSetError(IL_COULD_NOT_OPEN_FILE);
    00188                 return bFlat;
    00189         }
    00190
    00191         bFlat = ilLoadDoomF(FlatFile);
    00192         icloser(FlatFile);
    00193
    00194         return bFlat;
    00195 }
    00196
    00197
    00199 ILboolean ilLoadDoomFlatF(ILHANDLE File)
    00200 {
    00201         ILuint          FirstPos;
    00202         ILboolean       bRet;
    00203
    00204         iSetInputFile(File);
    00205         FirstPos = itell();
    00206         bRet = iLoadDoomFlatInternal();
    00207         iseek(FirstPos, IL_SEEK_SET);
    00208
    00209         return bRet;
    00210 }
    00211
    00212
    00214 ILboolean ilLoadDoomFlatL(const void *Lump, ILuint Size)
    00215 {
    00216         iSetInputLump(Lump, Size);
    00217         return iLoadDoomFlatInternal();
    00218 }
    00219
    00220
    00221 // Basically just ireads 4096 bytes and copies the palette
    00222 ILboolean iLoadDoomFlatInternal()
    00223 {
    00224         ILubyte *NewData;
    00225         ILuint  i;
    00226
    00227         if (iCurImage == NULL) {
    00228                 ilSetError(IL_ILLEGAL_OPERATION);
    00229                 return IL_FALSE;
    00230         }
    00231
    00232         if (!ilTexImage(64, 64, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL)) {
    00233                 return IL_FALSE;
    00234         }
    00235         iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
    00236
    00237         iCurImage->Pal.Palette = (ILubyte*)ialloc(IL_DOOMPAL_SIZE);
    00238         if (iCurImage->Pal.Palette == NULL) {
    00239                 return IL_FALSE;
    00240         }
    00241         iCurImage->Pal.PalSize = IL_DOOMPAL_SIZE;
    00242         iCurImage->Pal.PalType = IL_PAL_RGB24;
    00243         memcpy(iCurImage->Pal.Palette, ilDefaultDoomPal, IL_DOOMPAL_SIZE);
    00244
    00245         if (iread(iCurImage->Data, 1, 4096) != 4096)
    00246                 return IL_FALSE;
    00247
    00248         if (ilGetBoolean(IL_CONV_PAL) == IL_TRUE) {
    00249                 NewData = (ILubyte*)ialloc(iCurImage->SizeOfData * 4);
    00250                 if (NewData == NULL) {
    00251                         return IL_FALSE;
    00252                 }
    00253
    00254                 for (i = 0; i < iCurImage->SizeOfData; i++) {
    00255                         NewData[i * 4] = iCurImage->Pal.Palette[iCurImage->Data[i]];
    00256                         NewData[i * 4] = iCurImage->Pal.Palette[iCurImage->Data[i]];
    00257                         NewData[i * 4] = iCurImage->Pal.Palette[iCurImage->Data[i]];
    00258                         NewData[i * 4 + 3] = iCurImage->Data[i] != 247 ? 255 : 0;
    00259                 }
    00260
    00261                 if (!ilTexImage(iCurImage->Width, iCurImage->Height, iCurImage->Depth,
    00262                         4, IL_RGBA, iCurImage->Type, NewData)) {
    00263                         ifree(NewData);
    00264                         return IL_FALSE;
    00265                 }
    00266                 iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
    00267                 ifree(NewData);
    00268         }
    00269
    00270         ilFixImage();
    00271
    00272         return IL_TRUE;
    00273 }
    00274
    00275
    00276 #endif