il_bits.c
上传用户:wmy0603
上传日期:2022-05-02
资源大小:1808k
文件大小:4k
源码类别:

压缩解压

开发平台:

Visual C++

  1. //-----------------------------------------------------------------------------
  2. //
  3. // ImageLib Sources
  4. // Copyright (C) 2000-2008 by Denton Woods
  5. // Last modified: 08/14/2004
  6. //
  7. // Filename: src-IL/src/il_bits.c
  8. //
  9. // Description: Implements a file class that reads/writes bits directly.
  10. //
  11. //-----------------------------------------------------------------------------
  12. #include "il_internal.h"
  13. #include "il_bits.h"
  14. // Opens a BITFILE just like fopen opens a FILE.
  15. /*BITFILE *bopen(const char *FileName, const char *Mode)
  16. {
  17. BITFILE *ToReturn = NULL;
  18. if (FileName != NULL) {
  19. ToReturn = (BITFILE*)ialloc(sizeof(BITFILE));
  20. if (ToReturn != NULL) {
  21. iopenr((char*)FileName);
  22. ToReturn->File = iGetFile();
  23. ToReturn->BitPos = 0;
  24. ToReturn->ByteBitOff = 8;
  25. ToReturn->Buff = 0;
  26. }
  27. }
  28. return ToReturn;
  29. }*/
  30. // Converts a FILE to a BITFILE.
  31. BITFILE *bfile(ILHANDLE File)
  32. {
  33. BITFILE *ToReturn = NULL;
  34. if (File != NULL) {
  35. ToReturn = (BITFILE*)ialloc(sizeof(BITFILE));
  36. if (ToReturn != NULL) {
  37. ToReturn->File = File;
  38. ToReturn->BitPos = itell() << 3;
  39. ToReturn->ByteBitOff = 8;
  40. ToReturn->Buff = 0;
  41. }
  42. }
  43. return ToReturn;
  44. }
  45. // Closes an open BITFILE and frees memory for it.
  46. ILint bclose(BITFILE *BitFile)
  47. {
  48. if (BitFile == NULL || BitFile->File == NULL)
  49. return IL_EOF;
  50. // Removed 01-26-2008.  The file will get closed later by
  51. //  the calling function.
  52. //icloser(BitFile->File);
  53. ifree(BitFile);
  54. return 0;
  55. }
  56. // Returns the current bit position of a BITFILE.
  57. ILint btell(BITFILE *BitFile)
  58. {
  59. return BitFile->BitPos;
  60. }
  61. // Seeks in a BITFILE just like fseek for FILE.
  62. ILint bseek(BITFILE *BitFile, ILuint Offset, ILuint Mode)
  63. {
  64. ILint KeepPos, Len;
  65. if (BitFile == NULL || BitFile->File == NULL)
  66. return 1;
  67. switch (Mode)
  68. {
  69. case IL_SEEK_SET:
  70. if (!iseek(Offset >> 3, Mode)) {
  71. BitFile->BitPos = Offset;
  72. BitFile->ByteBitOff = BitFile->BitPos % 8;
  73. }
  74. break;
  75. case IL_SEEK_CUR:
  76. if (!iseek(Offset >> 3, Mode)) {
  77. BitFile->BitPos += Offset;
  78. BitFile->ByteBitOff = BitFile->BitPos % 8;
  79. }
  80. break;
  81. case IL_SEEK_END:
  82. KeepPos = itell();
  83. iseek(0, IL_SEEK_END);
  84. Len = itell();
  85. iseek(0, IL_SEEK_SET);
  86. if (!iseek(Offset >> 3, Mode)) {
  87. BitFile->BitPos = (Len << 3) + Offset;
  88. BitFile->ByteBitOff = BitFile->BitPos % 8;
  89. }
  90. break;
  91. default:
  92. return 1;
  93. }
  94. return 0;
  95. }
  96. // hehe, "bread".  It reads data into Buffer from the BITFILE, just like fread for FILE.
  97. ILint bread(void *Buffer, ILuint Size, ILuint Number, BITFILE *BitFile)
  98. {
  99. // Note that this function is somewhat useless: In binary image
  100. // formats, there are some pad bits after each scanline. This
  101. // function does not take that into account, so you must use bseek to
  102. // skip the calculated value of bits.
  103. ILuint BuffPos = 0, Count = Size * Number;
  104. while (BuffPos < Count) {
  105. if (BitFile->ByteBitOff < 0 || BitFile->ByteBitOff > 7) {
  106. BitFile->ByteBitOff = 7;
  107. if (iread(&BitFile->Buff, 1, 1) != 1)  // Reached eof or error...
  108. return BuffPos;
  109. }
  110. *((ILubyte*)(Buffer) + BuffPos) = (ILubyte)!!(BitFile->Buff & (1 << BitFile->ByteBitOff));
  111. BuffPos++;
  112. BitFile->ByteBitOff--;
  113. }
  114. return BuffPos;
  115. }
  116. // Reads bits and puts the first bit in the file as the highest in the return value.
  117. ILuint breadVal(ILuint NumBits, BITFILE *BitFile)
  118. {
  119. ILuint BuffPos = 0;
  120. ILuint Buffer = 0;
  121. // Only returning up to 32 bits at one time
  122. if (NumBits > 32) {
  123. ilSetError(IL_INTERNAL_ERROR);
  124. return 0;
  125. }
  126. while (BuffPos < NumBits) {
  127. Buffer <<= 1;
  128. if (BitFile->ByteBitOff < 0 || BitFile->ByteBitOff > 7) {
  129. BitFile->ByteBitOff = 7;
  130. if (iread(&BitFile->Buff, 1, 1) != 1)  // Reached eof or error...
  131. return BuffPos;
  132. }
  133. Buffer = Buffer + (ILubyte)!!(BitFile->Buff & (1 << BitFile->ByteBitOff));
  134. BuffPos++;
  135. BitFile->ByteBitOff--;
  136. }
  137. return BuffPos;
  138. }
  139. // Not implemented yet.
  140. /*ILint bwrite(void *Buffer, ILuint Size, ILuint Number, BITFILE *BitFile)
  141. {
  142. return 0;
  143. }*/