il_pix.c
Upload User: wmy0603
Upload Date: 2022-05-02
Package Size: 1808k
Code Size: 3k
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_pix.c
  8. //
  9. // Description: Reads from an Alias | Wavefront .pix file.
  10. //
  11. //-----------------------------------------------------------------------------
  12. #include "il_internal.h"
  13. #ifndef IL_NO_PIX
  14. #include "il_manip.h"
  15. #include "il_endian.h"
  16. #ifdef _MSC_VER
  17. #pragma pack(push, pix_struct, 1)
  18. #endif
  19. typedef struct PIXHEAD
  20. {
  21. ILushort Width;
  22. ILushort Height;
  23. ILushort OffX;
  24. ILushort OffY;
  25. ILushort Bpp;
  26. } IL_PACKSTRUCT PIXHEAD;
  27. #ifdef _MSC_VER
  28. #pragma pack(pop, pix_struct)
  29. #endif
  30. ILboolean iCheckPix(PIXHEAD *Header);
  31. ILboolean iLoadPixInternal(void);
  32. // Internal function used to get the Pix header from the current file.
  33. ILboolean iGetPixHead(PIXHEAD *Header)
  34. {
  35. Header->Width = GetBigUShort();
  36. Header->Height = GetBigUShort();
  37. Header->OffX = GetBigUShort();
  38. Header->OffY = GetBigUShort();
  39. Header->Bpp = GetBigUShort();
  40. return IL_TRUE;
  41. }
  42. // Internal function to get the header and check it.
  43. ILboolean iIsValidPix()
  44. {
  45. PIXHEAD Head;
  46. if (!iGetPixHead(&Head))
  47. return IL_FALSE;
  48. iseek(-(ILint)sizeof(PIXHEAD), IL_SEEK_CUR);
  49. return iCheckPix(&Head);
  50. }
  51. // Internal function used to check if the HEADER is a valid Pix header.
  52. ILboolean iCheckPix(PIXHEAD *Header)
  53. {
  54. if (Header->Width == 0 || Header->Height == 0)
  55. return IL_FALSE;
  56. if (Header->Bpp != 24)
  57. return IL_FALSE;
  58. //if (Header->OffY != Header->Height)
  59. // return IL_FALSE;
  60. return IL_TRUE;
  61. }
  62. //! Reads a Pix file
  63. ILboolean ilLoad_PIX(ILconst_string FileName)
  64. {
  65. ILHANDLE PixFile;
  66. ILboolean bPix = IL_FALSE;
  67. PixFile = iopenr(FileName);
  68. if (PixFile == NULL) {
  69. ilSetError(IL_COULD_NOT_OPEN_FILE);
  70. return bPix;
  71. }
  72. bPix = ilLoadF_PIX(PixFile);
  73. icloser(PixFile);
  74. return bPix;
  75. }
  76. //! Reads an already-opened Pix file
  77. ILboolean ilLoadF_PIX(ILHANDLE File)
  78. {
  79. ILuint FirstPos;
  80. ILboolean bRet;
  81. iSetInputFile(File);
  82. FirstPos = itell();
  83. bRet = iLoadPixInternal();
  84. iseek(FirstPos, IL_SEEK_SET);
  85. return bRet;
  86. }
  87. //! Reads from a memory "lump" that contains a Pix
  88. ILboolean ilLoadL_PIX(const void *Lump, ILuint Size)
  89. {
  90. iSetInputLump(Lump, Size);
  91. return iLoadPixInternal();
  92. }
  93. // Internal function used to load the Pix.
  94. ILboolean iLoadPixInternal()
  95. {
  96. PIXHEAD Header;
  97. ILuint i, j;
  98. ILubyte ByteHead, Colour[3];
  99. if (iCurImage == NULL) {
  100. ilSetError(IL_ILLEGAL_OPERATION);
  101. return IL_FALSE;
  102. }
  103. if (!iGetPixHead(&Header))
  104. return IL_FALSE;
  105. if (!iCheckPix(&Header)) {
  106. ilSetError(IL_INVALID_FILE_HEADER);
  107. return IL_FALSE;
  108. }
  109. if (!ilTexImage(Header.Width, Header.Height, 1, 3, IL_BGR, IL_UNSIGNED_BYTE, NULL))
  110. return IL_FALSE;
  111. for (i = 0; i < iCurImage->SizeOfData; ) {
  112. ByteHead = igetc();
  113. if (iread(Colour, 1, 3) != 3)
  114. return IL_FALSE;
  115. for (j = 0; j < ByteHead; j++) {
  116. iCurImage->Data[i++] = Colour[0];
  117. iCurImage->Data[i++] = Colour[1];
  118. iCurImage->Data[i++] = Colour[2];
  119. }
  120. }
  121. iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
  122. return ilFixImage();
  123. }
  124. #endif//IL_NO_PIX