il_lif.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_lif.c
  8. //
  9. // Description: Reads a Homeworld image.
  10. //
  11. //-----------------------------------------------------------------------------
  12. #include "il_internal.h"
  13. #ifndef IL_NO_LIF
  14. #include "il_lif.h"
  15. //! Checks if the file specified in FileName is a valid Lif file.
  16. ILboolean ilIsValid_LIF(ILconst_string FileName)
  17. {
  18. ILHANDLE LifFile;
  19. ILboolean bLif = IL_FALSE;
  20. if (!iCheckExtension(FileName, IL_TEXT("lif"))) {
  21. ilSetError(IL_INVALID_EXTENSION);
  22. return bLif;
  23. }
  24. LifFile = iopenr(FileName);
  25. if (LifFile == NULL) {
  26. ilSetError(IL_COULD_NOT_OPEN_FILE);
  27. return bLif;
  28. }
  29. bLif = ilIsValidF_LIF(LifFile);
  30. icloser(LifFile);
  31. return bLif;
  32. }
  33. //! Checks if the ILHANDLE contains a valid Lif file at the current position.
  34. ILboolean ilIsValidF_LIF(ILHANDLE File)
  35. {
  36. ILuint FirstPos;
  37. ILboolean bRet;
  38. iSetInputFile(File);
  39. FirstPos = itell();
  40. bRet = iIsValidLif();
  41. iseek(FirstPos, IL_SEEK_SET);
  42. return bRet;
  43. }
  44. //! Checks if Lump is a valid Lif lump.
  45. ILboolean ilIsValidL_LIF(const void *Lump, ILuint Size)
  46. {
  47. iSetInputLump(Lump, Size);
  48. return iIsValidLif();
  49. }
  50. // Internal function used to get the Lif header from the current file.
  51. ILboolean iGetLifHead(LIF_HEAD *Header)
  52. {
  53. iread(Header->Id, 1, 8);
  54. Header->Version = GetLittleUInt();
  55. Header->Flags = GetLittleUInt();
  56. Header->Width = GetLittleUInt();
  57. Header->Height = GetLittleUInt();
  58. Header->PaletteCRC = GetLittleUInt();
  59. Header->ImageCRC = GetLittleUInt();
  60. Header->PalOffset = GetLittleUInt();
  61. Header->TeamEffect0 = GetLittleUInt();
  62. Header->TeamEffect1 = GetLittleUInt();
  63. return IL_TRUE;
  64. }
  65. // Internal function to get the header and check it.
  66. ILboolean iIsValidLif()
  67. {
  68. LIF_HEAD Head;
  69. if (!iGetLifHead(&Head))
  70. return IL_FALSE;
  71. iseek(-(ILint)sizeof(LIF_HEAD), IL_SEEK_CUR);
  72. return iCheckLif(&Head);
  73. }
  74. // Internal function used to check if the HEADER is a valid Lif header.
  75. ILboolean iCheckLif(LIF_HEAD *Header)
  76. {
  77. if (Header->Version != 260 || Header->Flags != 50)
  78. return IL_FALSE;
  79. if (stricmp(Header->Id, "Willy 7"))
  80. return IL_FALSE;
  81. return IL_TRUE;
  82. }
  83. //! Reads a .Lif file
  84. ILboolean ilLoad_LIF(ILconst_string FileName)
  85. {
  86. ILHANDLE LifFile;
  87. ILboolean bLif = IL_FALSE;
  88. LifFile = iopenr(FileName);
  89. if (LifFile == NULL) {
  90. ilSetError(IL_COULD_NOT_OPEN_FILE);
  91. return bLif;
  92. }
  93. bLif = ilLoadF_LIF(LifFile);
  94. icloser(LifFile);
  95. return bLif;
  96. }
  97. //! Reads an already-opened .Lif file
  98. ILboolean ilLoadF_LIF(ILHANDLE File)
  99. {
  100. ILuint FirstPos;
  101. ILboolean bRet;
  102. iSetInputFile(File);
  103. FirstPos = itell();
  104. bRet = iLoadLifInternal();
  105. iseek(FirstPos, IL_SEEK_SET);
  106. return bRet;
  107. }
  108. //! Reads from a memory "lump" that contains a .Lif
  109. ILboolean ilLoadL_LIF(const void *Lump, ILuint Size)
  110. {
  111. iSetInputLump(Lump, Size);
  112. return iLoadLifInternal();
  113. }
  114. ILboolean iLoadLifInternal()
  115. {
  116. LIF_HEAD LifHead;
  117. ILuint i;
  118. if (iCurImage == NULL) {
  119. ilSetError(IL_ILLEGAL_OPERATION);
  120. return IL_FALSE;
  121. }
  122. if (!iGetLifHead(&LifHead))
  123. return IL_FALSE;
  124. if (!ilTexImage(LifHead.Width, LifHead.Height, 1, 1, IL_COLOUR_INDEX, IL_UNSIGNED_BYTE, NULL)) {
  125. return IL_FALSE;
  126. }
  127. iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
  128. iCurImage->Pal.Palette = (ILubyte*)ialloc(1024);
  129. if (iCurImage->Pal.Palette == NULL)
  130. return IL_FALSE;
  131. iCurImage->Pal.PalSize = 1024;
  132. iCurImage->Pal.PalType = IL_PAL_RGBA32;
  133. if (iread(iCurImage->Data, LifHead.Width * LifHead.Height, 1) != 1)
  134. return IL_FALSE;
  135. if (iread(iCurImage->Pal.Palette, 1, 1024) != 1024)
  136. return IL_FALSE;
  137. // Each data offset is offset by -1, so we add one.
  138. for (i = 0; i < iCurImage->SizeOfData; i++) {
  139. iCurImage->Data[i]++;
  140. }
  141. return ilFixImage();
  142. }
  143. #endif//IL_NO_LIF