il_doom.c
Upload User: wmy0603
Upload Date: 2022-05-02
Package Size: 1808k
Code Size: 6k
Development Platform:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. //
  3. // ImageLib Sources
  4. // Copyright (C) 2000-2009 by Denton Woods
  5. // Last modified: 03/07/2009
  6. //
  7. // Filename: src-IL/src/il_doom.c
  8. //
  9. // Description: Reads Doom textures and flats
  10. //
  11. //-----------------------------------------------------------------------------
  12. #include "il_internal.h"
  13. #ifndef IL_NO_DOOM
  14. #include "il_manip.h"
  15. #include "il_pal.h"
  16. #include "il_doompal.h"
  17. ILboolean iLoadDoomInternal(void);
  18. ILboolean iLoadDoomFlatInternal(void);
  19. //
  20. // READ A DOOM IMAGE
  21. //
  22. //! Reads a Doom file
  23. ILboolean ilLoad_DOOM(ILconst_string FileName)
  24. {
  25. ILHANDLE DoomFile;
  26. ILboolean bDoom = IL_FALSE;
  27. // Not sure of any kind of specified extension...maybe .lmp?
  28. /*if (!iCheckExtension(FileName, "")) {
  29. ilSetError(IL_INVALID_EXTENSION);
  30. return NULL;
  31. }*/
  32. DoomFile = iopenr(FileName);
  33. if (DoomFile == NULL) {
  34. ilSetError(IL_COULD_NOT_OPEN_FILE);
  35. return bDoom;
  36. }
  37. bDoom = ilLoadF_DOOM(DoomFile);
  38. icloser(DoomFile);
  39. return bDoom;
  40. }
  41. //! Reads an already-opened Doom file
  42. ILboolean ilLoadF_DOOM(ILHANDLE File)
  43. {
  44. ILuint FirstPos;
  45. ILboolean bRet;
  46. iSetInputFile(File);
  47. FirstPos = itell();
  48. bRet = iLoadDoomInternal();
  49. iseek(FirstPos, IL_SEEK_SET);
  50. return bRet;
  51. }
  52. //! Reads from a memory "lump" that contains a Doom texture
  53. ILboolean ilLoadL_DOOM(const void *Lump, ILuint Size)
  54. {
  55. iSetInputLump(Lump, Size);
  56. return iLoadDoomInternal();
  57. }
  58. // From the DTE sources (mostly by Denton Woods with corrections by Randy Heit)
  59. ILboolean iLoadDoomInternal()
  60. {
  61. ILshort width, height, graphic_header[2], column_loop, row_loop;
  62. ILint column_offset, pointer_position, first_pos;
  63. ILubyte post, topdelta, length;
  64. ILubyte *NewData;
  65. ILuint i;
  66. if (iCurImage == NULL) {
  67. ilSetError(IL_ILLEGAL_OPERATION);
  68. return IL_FALSE;
  69. }
  70. first_pos = itell();  // Needed to go back to the offset table
  71. width = GetLittleShort();
  72. height = GetLittleShort();
  73. graphic_header[0] = GetLittleShort();  // Not even used
  74. graphic_header[1] = GetLittleShort();  // Not even used
  75. if (!ilTexImage(width, height, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL)) {
  76. return IL_FALSE;
  77. }
  78. iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
  79. iCurImage->Pal.Palette = (ILubyte*)ialloc(IL_DOOMPAL_SIZE);
  80. if (iCurImage->Pal.Palette == NULL) {
  81. return IL_FALSE;
  82. }
  83. iCurImage->Pal.PalSize = IL_DOOMPAL_SIZE;
  84. iCurImage->Pal.PalType = IL_PAL_RGB24;
  85. memcpy(iCurImage->Pal.Palette, ilDefaultDoomPal, IL_DOOMPAL_SIZE);
  86. // 247 is always the transparent colour (usually cyan)
  87. memset(iCurImage->Data, 247, iCurImage->SizeOfData);
  88. for (column_loop = 0; column_loop < width; column_loop++) {
  89. column_offset = GetLittleInt();
  90. pointer_position = itell();
  91. iseek(first_pos + column_offset, IL_SEEK_SET);
  92. while (1) {
  93. if (iread(&topdelta, 1, 1) != 1)
  94. return IL_FALSE;
  95. if (topdelta == 255)
  96. break;
  97. if (iread(&length, 1, 1) != 1)
  98. return IL_FALSE;
  99. if (iread(&post, 1, 1) != 1)
  100. return IL_FALSE; // Skip extra byte for scaling
  101. for (row_loop = 0; row_loop < length; row_loop++) {
  102. if (iread(&post, 1, 1) != 1)
  103. return IL_FALSE;
  104. if (row_loop + topdelta < height)
  105. iCurImage->Data[(row_loop+topdelta) * width + column_loop] = post;
  106. }
  107. iread(&post, 1, 1); // Skip extra scaling byte
  108. }
  109. iseek(pointer_position, IL_SEEK_SET);
  110. }
  111. // Converts palette entry 247 (cyan) to transparent.
  112. if (ilGetBoolean(IL_CONV_PAL) == IL_TRUE) {
  113. NewData = (ILubyte*)ialloc(iCurImage->SizeOfData * 4);
  114. if (NewData == NULL) {
  115. return IL_FALSE;
  116. }
  117. for (i = 0; i < iCurImage->SizeOfData; i++) {
  118. NewData[i * 4] = iCurImage->Pal.Palette[iCurImage->Data[i]];
  119. NewData[i * 4] = iCurImage->Pal.Palette[iCurImage->Data[i]];
  120. NewData[i * 4] = iCurImage->Pal.Palette[iCurImage->Data[i]];
  121. NewData[i * 4 + 3] = iCurImage->Data[i] != 247 ? 255 : 0;
  122. }
  123. if (!ilTexImage(iCurImage->Width, iCurImage->Height, iCurImage->Depth,
  124. 4, IL_RGBA, iCurImage->Type, NewData)) {
  125. ifree(NewData);
  126. return IL_FALSE;
  127. }
  128. iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
  129. ifree(NewData);
  130. }
  131. return ilFixImage();
  132. }
  133. //
  134. // READ A DOOM FLAT
  135. //
  136. //! Reads a Doom flat file
  137. ILboolean ilLoad_DOOM_FLAT(ILconst_string FileName)
  138. {
  139. ILHANDLE FlatFile;
  140. ILboolean bFlat = IL_FALSE;
  141. // Not sure of any kind of specified extension...maybe .lmp?
  142. /*if (!iCheckExtension(FileName, "")) {
  143. ilSetError(IL_INVALID_EXTENSION);
  144. return NULL;
  145. }*/
  146. FlatFile = iopenr(FileName);
  147. if (FlatFile == NULL) {
  148. ilSetError(IL_COULD_NOT_OPEN_FILE);
  149. return bFlat;
  150. }
  151. bFlat = ilLoadF_DOOM(FlatFile);
  152. icloser(FlatFile);
  153. return bFlat;
  154. }
  155. //! Reads an already-opened Doom flat file
  156. ILboolean ilLoadF_DOOM_FLAT(ILHANDLE File)
  157. {
  158. ILuint FirstPos;
  159. ILboolean bRet;
  160. iSetInputFile(File);
  161. FirstPos = itell();
  162. bRet = iLoadDoomFlatInternal();
  163. iseek(FirstPos, IL_SEEK_SET);
  164. return bRet;
  165. }
  166. //! Reads from a memory "lump" that contains a Doom flat
  167. ILboolean ilLoadL_DOOM_FLAT(const void *Lump, ILuint Size)
  168. {
  169. iSetInputLump(Lump, Size);
  170. return iLoadDoomFlatInternal();
  171. }
  172. // Basically just ireads 4096 bytes and copies the palette
  173. ILboolean iLoadDoomFlatInternal()
  174. {
  175. ILubyte *NewData;
  176. ILuint i;
  177. if (iCurImage == NULL) {
  178. ilSetError(IL_ILLEGAL_OPERATION);
  179. return IL_FALSE;
  180. }
  181. if (!ilTexImage(64, 64, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL)) {
  182. return IL_FALSE;
  183. }
  184. iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
  185. iCurImage->Pal.Palette = (ILubyte*)ialloc(IL_DOOMPAL_SIZE);
  186. if (iCurImage->Pal.Palette == NULL) {
  187. return IL_FALSE;
  188. }
  189. iCurImage->Pal.PalSize = IL_DOOMPAL_SIZE;
  190. iCurImage->Pal.PalType = IL_PAL_RGB24;
  191. memcpy(iCurImage->Pal.Palette, ilDefaultDoomPal, IL_DOOMPAL_SIZE);
  192. if (iread(iCurImage->Data, 1, 4096) != 4096)
  193. return IL_FALSE;
  194. if (ilGetBoolean(IL_CONV_PAL) == IL_TRUE) {
  195. NewData = (ILubyte*)ialloc(iCurImage->SizeOfData * 4);
  196. if (NewData == NULL) {
  197. return IL_FALSE;
  198. }
  199. for (i = 0; i < iCurImage->SizeOfData; i++) {
  200. NewData[i * 4] = iCurImage->Pal.Palette[iCurImage->Data[i]];
  201. NewData[i * 4] = iCurImage->Pal.Palette[iCurImage->Data[i]];
  202. NewData[i * 4] = iCurImage->Pal.Palette[iCurImage->Data[i]];
  203. NewData[i * 4 + 3] = iCurImage->Data[i] != 247 ? 255 : 0;
  204. }
  205. if (!ilTexImage(iCurImage->Width, iCurImage->Height, iCurImage->Depth,
  206. 4, IL_RGBA, iCurImage->Type, NewData)) {
  207. ifree(NewData);
  208. return IL_FALSE;
  209. }
  210. iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
  211. ifree(NewData);
  212. }
  213. return ilFixImage();
  214. }
  215. #endif