il_raw.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_raw.c
  8. //
  9. // Description: "Raw" file functions
  10. //
  11. //-----------------------------------------------------------------------------
  12. #include "il_internal.h"
  13. #ifndef IL_NO_RAW
  14. ILboolean iLoadRawInternal(void);
  15. ILboolean iSaveRawInternal(void);
  16. //! Reads a raw file
  17. ILboolean ilLoad_RAW(ILconst_string FileName)
  18. {
  19. ILHANDLE RawFile;
  20. ILboolean bRaw = IL_FALSE;
  21. // No need to check for raw
  22. /*if (!iCheckExtension(FileName, "raw")) {
  23. ilSetError(IL_INVALID_EXTENSION);
  24. return bRaw;
  25. }*/
  26. RawFile = iopenr(FileName);
  27. if (RawFile == NULL) {
  28. ilSetError(IL_COULD_NOT_OPEN_FILE);
  29. return bRaw;
  30. }
  31. bRaw = ilLoadF_RAW(RawFile);
  32. icloser(RawFile);
  33. return bRaw;
  34. }
  35. //! Reads an already-opened raw file
  36. ILboolean ilLoadF_RAW(ILHANDLE File)
  37. {
  38. ILuint FirstPos;
  39. ILboolean bRet;
  40. iSetInputFile(File);
  41. FirstPos = itell();
  42. bRet = iLoadRawInternal();
  43. iseek(FirstPos, IL_SEEK_SET);
  44. return bRet;
  45. }
  46. //! Reads from a raw memory "lump"
  47. ILboolean ilLoadL_RAW(const void *Lump, ILuint Size)
  48. {
  49. iSetInputLump(Lump, Size);
  50. return iLoadRawInternal();
  51. }
  52. // Internal function to load a raw image
  53. ILboolean iLoadRawInternal()
  54. {
  55. if (iCurImage == NULL) {
  56. ilSetError(IL_ILLEGAL_OPERATION);
  57. return IL_FALSE;
  58. }
  59. iCurImage->Width = GetLittleUInt();
  60. iCurImage->Height = GetLittleUInt();
  61. iCurImage->Depth = GetLittleUInt();
  62. iCurImage->Bpp = (ILubyte)igetc();
  63. if (iread(&iCurImage->Bpc, 1, 1) != 1)
  64. return IL_FALSE;
  65. if (!ilTexImage(iCurImage->Width, iCurImage->Height, iCurImage->Depth, iCurImage->Bpp, 0, ilGetTypeBpc(iCurImage->Bpc), NULL)) {
  66. return IL_FALSE;
  67. }
  68. iCurImage->Origin = IL_ORIGIN_LOWER_LEFT;
  69. // Tries to read the correct amount of data
  70. if (iread(iCurImage->Data, 1, iCurImage->SizeOfData) < iCurImage->SizeOfData)
  71. return IL_FALSE;
  72. if (ilIsEnabled(IL_ORIGIN_SET)) {
  73. iCurImage->Origin = ilGetInteger(IL_ORIGIN_MODE);
  74. }
  75. else {
  76. iCurImage->Origin = IL_ORIGIN_UPPER_LEFT;
  77. }
  78. if (iCurImage->Bpp == 1)
  79. iCurImage->Format = IL_LUMINANCE;
  80. else if (iCurImage->Bpp == 3)
  81. iCurImage->Format = IL_RGB;
  82. else  // 4
  83. iCurImage->Format = IL_RGBA;
  84. return ilFixImage();
  85. }
  86. //! Writes a Raw file
  87. ILboolean ilSave_RAW(const ILstring FileName)
  88. {
  89. ILHANDLE RawFile;
  90. ILuint RawSize;
  91. if (ilGetBoolean(IL_FILE_MODE) == IL_FALSE) {
  92. if (iFileExists(FileName)) {
  93. ilSetError(IL_FILE_ALREADY_EXISTS);
  94. return IL_FALSE;
  95. }
  96. }
  97. RawFile = iopenw(FileName);
  98. if (RawFile == NULL) {
  99. ilSetError(IL_COULD_NOT_OPEN_FILE);
  100. return IL_FALSE;
  101. }
  102. RawSize = ilSaveF_RAW(RawFile);
  103. iclosew(RawFile);
  104. if (RawSize == 0)
  105. return IL_FALSE;
  106. return IL_TRUE;
  107. }
  108. //! Writes Raw to an already-opened file
  109. ILuint ilSaveF_RAW(ILHANDLE File)
  110. {
  111. ILuint Pos;
  112. iSetOutputFile(File);
  113. Pos = itellw();
  114. if (iSaveRawInternal() == IL_FALSE)
  115. return 0;  // Error occurred
  116. return itellw() - Pos;  // Return the number of bytes written.
  117. }
  118. //! Writes Raw to a memory "lump"
  119. ILuint ilSaveL_RAW(void *Lump, ILuint Size)
  120. {
  121. ILuint Pos;
  122. iSetOutputLump(Lump, Size);
  123. Pos = itellw();
  124. if (iSaveRawInternal() == IL_FALSE)
  125. return 0;  // Error occurred
  126. return itellw() - Pos;  // Return the number of bytes written.
  127. }
  128. // Internal function used to load the raw data.
  129. ILboolean iSaveRawInternal()
  130. {
  131. if (iCurImage == NULL) {
  132. ilSetError(IL_ILLEGAL_OPERATION);
  133. return IL_FALSE;
  134. }
  135. SaveLittleUInt(iCurImage->Width);
  136. SaveLittleUInt(iCurImage->Height);
  137. SaveLittleUInt(iCurImage->Depth);
  138. iputc(iCurImage->Bpp);
  139. iputc(iCurImage->Bpc);
  140. iwrite(iCurImage->Data, 1, iCurImage->SizeOfData);
  141. return IL_TRUE;
  142. }
  143. #endif//IL_NO_RAW