il_wal.c
Upload User: wmy0603
Upload Date: 2022-05-02
Package Size: 1808k
Code Size: 4k
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_wal.c
  8. //
  9. // Description: Loads a Quake .wal texture.
  10. //
  11. //-----------------------------------------------------------------------------
  12. #include "il_internal.h"
  13. #ifndef IL_NO_WAL
  14. #include "il_manip.h"
  15. #include "il_q2pal.h"
  16. typedef struct WALHEAD
  17. {
  18. ILbyte FileName[32]; // Image name
  19. ILuint Width; // Width of first image
  20. ILuint Height; // Height of first image
  21. ILuint Offsets[4]; // Offsets to image data
  22. ILbyte AnimName[32]; // Name of next frame
  23. ILuint Flags; // ??
  24. ILuint Contents; // ??
  25. ILuint Value; // ??
  26. } WALHEAD;
  27. ILboolean iLoadWalInternal(void);
  28. //! Reads a .wal file
  29. ILboolean ilLoad_WAL(ILconst_string FileName)
  30. {
  31. ILHANDLE WalFile;
  32. ILboolean bWal = IL_FALSE;
  33. WalFile = iopenr(FileName);
  34. if (WalFile == NULL) {
  35. ilSetError(IL_COULD_NOT_OPEN_FILE);
  36. return bWal;
  37. }
  38. bWal = ilLoadF_WAL(WalFile);
  39. icloser(WalFile);
  40. return bWal;
  41. }
  42. //! Reads an already-opened .wal file
  43. ILboolean ilLoadF_WAL(ILHANDLE File)
  44. {
  45. ILuint FirstPos;
  46. ILboolean bRet;
  47. iSetInputFile(File);
  48. FirstPos = itell();
  49. bRet = iLoadWalInternal();
  50. iseek(FirstPos, IL_SEEK_SET);
  51. return bRet;
  52. }
  53. //! Reads from a memory "lump" that contains a .wal file
  54. ILboolean ilLoadL_WAL(const void *Lump, ILuint Size)
  55. {
  56. iSetInputLump(Lump, Size);
  57. return iLoadWalInternal();
  58. }
  59. ILboolean iLoadWalInternal()
  60. {
  61. WALHEAD Header;
  62. ILimage *Mipmaps[3], *CurImage;
  63. ILuint i, NewW, NewH;
  64. if (iCurImage == NULL) {
  65. ilSetError(IL_ILLEGAL_OPERATION);
  66. return IL_FALSE;
  67. }
  68. CurImage = iCurImage;
  69. // Read header
  70. iread(&Header.FileName, 1, 32);
  71. Header.Width = GetLittleUInt();
  72. Header.Height = GetLittleUInt();
  73. for (i = 0; i < 4; i++)
  74. Header.Offsets[i] = GetLittleUInt();
  75. iread(Header.AnimName, 1, 32);
  76. Header.Flags = GetLittleUInt();
  77. Header.Contents = GetLittleUInt();
  78. Header.Value = GetLittleUInt();
  79. if (!ilTexImage(Header.Width, Header.Height, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL))
  80. return IL_FALSE;
  81. for (i = 0; i < 3; i++) {
  82. Mipmaps[i] = (ILimage*)icalloc(sizeof(ILimage), 1);
  83. if (Mipmaps[i] == NULL)
  84. goto cleanup_error;
  85. Mipmaps[i]->Pal.Palette = (ILubyte*)ialloc(768);
  86. if (Mipmaps[i]->Pal.Palette == NULL)
  87. goto cleanup_error;
  88. memcpy(Mipmaps[i]->Pal.Palette, ilDefaultQ2Pal, 768);
  89. Mipmaps[i]->Pal.PalType = IL_PAL_RGB24;
  90. }
  91. NewW = Header.Width;
  92. NewH = Header.Height;
  93. for (i = 0; i < 3; i++) {
  94. NewW /= 2;
  95. NewH /= 2;
  96. iCurImage = Mipmaps[i];
  97. if (!ilTexImage(NewW, NewH, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL))
  98. goto cleanup_error;
  99. // Don't set until now so ilTexImage won't get rid of the palette.
  100. Mipmaps[i]->Pal.PalSize = 768;
  101. Mipmaps[i]->Origin = IL_ORIGIN_UPPER_LEFT;
  102. }
  103. iCurImage = CurImage;
  104. ilCloseImage(iCurImage->Mipmaps);
  105. iCurImage->Mipmaps = Mipmaps[0];
  106. Mipmaps[0]->Mipmaps = Mipmaps[1];
  107. Mipmaps[1]->Mipmaps = Mipmaps[2];
  108. iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
  109. if (iCurImage->Pal.Palette && iCurImage->Pal.PalSize && iCurImage->Pal.PalType != IL_PAL_NONE)
  110. ifree(iCurImage->Pal.Palette);
  111. iCurImage->Pal.Palette = (ILubyte*)ialloc(768);
  112. if (iCurImage->Pal.Palette == NULL)
  113. goto cleanup_error;
  114. iCurImage->Pal.PalSize = 768;
  115. iCurImage->Pal.PalType = IL_PAL_RGB24;
  116. memcpy(iCurImage->Pal.Palette, ilDefaultQ2Pal, 768);
  117. iseek(Header.Offsets[0], IL_SEEK_SET);
  118. if (iread(iCurImage->Data, Header.Width * Header.Height, 1) != 1)
  119. goto cleanup_error;
  120. for (i = 0; i < 3; i++) {
  121. iseek(Header.Offsets[i+1], IL_SEEK_SET);
  122. if (iread(Mipmaps[i]->Data, Mipmaps[i]->Width * Mipmaps[i]->Height, 1) != 1)
  123. goto cleanup_error;
  124. }
  125. // Fixes all images, even mipmaps.
  126. return ilFixImage();
  127. cleanup_error:
  128. for (i = 0; i < 3; i++) {
  129. ilCloseImage(Mipmaps[i]);
  130. }
  131. return IL_FALSE;
  132. }
  133. #endif//IL_NO_WAL