- Main Page
- Data Structures
- Files

- File List

# E:/DevIL/src-IL/src/il_bits.c

    00001 //-----------------------------------------------------------------------------
    00002 //
    00003 // ImageLib Sources
    00004 // Copyright (C) 2000-2008 by Denton Woods
    00005 // Last modified: 08/14/2004
    00006 //
    00007 // Filename: src-IL/src/il_bits.c
    00008 //
    00009 // Description: Implements a file class that reads/writes bits directly.
    00010 //
    00011 //-----------------------------------------------------------------------------
    00012
    00013
    00014 #include "il_internal.h"
    00015 #include "il_bits.h"
    00016
    00017
    00018 // Opens a BITFILE just like fopen opens a FILE.
    00019 /*BITFILE *bopen(const char *FileName, const char *Mode)
    00020 {
    00021         BITFILE *ToReturn = NULL;
    00022
    00023         if (FileName != NULL) {
    00024                 ToReturn = (BITFILE*)ialloc(sizeof(BITFILE));
    00025                 if (ToReturn != NULL) {
    00026                         iopenr((char*)FileName);
    00027                         ToReturn->File = iGetFile();
    00028                         ToReturn->BitPos = 0;
    00029                         ToReturn->ByteBitOff = 8;
    00030                         ToReturn->Buff = 0;
    00031                 }
    00032         }
    00033
    00034         return ToReturn;
    00035 }*/
    00036
    00037
    00038 // Converts a FILE to a BITFILE.
    00039 BITFILE *bfile(ILHANDLE File)
    00040 {
    00041         BITFILE *ToReturn = NULL;
    00042
    00043         if (File != NULL) {
    00044                 ToReturn = (BITFILE*)ialloc(sizeof(BITFILE));
    00045                 if (ToReturn != NULL) {
    00046                         ToReturn->File = File;
    00047                         ToReturn->BitPos = itell() << 3;
    00048                         ToReturn->ByteBitOff = 8;
    00049                         ToReturn->Buff = 0;
    00050                 }
    00051         }
    00052
    00053         return ToReturn;
    00054 }
    00055
    00056
    00057 // Closes an open BITFILE and frees memory for it.
    00058 ILint bclose(BITFILE *BitFile)
    00059 {
    00060         if (BitFile == NULL || BitFile->File == NULL)
    00061                 return IL_EOF;
    00062
    00063         // Removed 01-26-2008.  The file will get closed later by
    00064         //  the calling function.
    00065         //icloser(BitFile->File);
    00066         ifree(BitFile);
    00067
    00068         return 0;
    00069 }
    00070
    00071
    00072 // Returns the current bit position of a BITFILE.
    00073 ILint btell(BITFILE *BitFile)
    00074 {
    00075         return BitFile->BitPos;
    00076 }
    00077
    00078
    00079 // Seeks in a BITFILE just like fseek for FILE.
    00080 ILint bseek(BITFILE *BitFile, ILuint Offset, ILuint Mode)
    00081 {
    00082         ILint KeepPos, Len;
    00083
    00084         if (BitFile == NULL || BitFile->File == NULL)
    00085                 return 1;
    00086
    00087         switch (Mode)
    00088         {
    00089                 case IL_SEEK_SET:
    00090                         if (!iseek(Offset >> 3, Mode)) {
    00091                                 BitFile->BitPos = Offset;
    00092                                 BitFile->ByteBitOff = BitFile->BitPos % 8;
    00093                         }
    00094                         break;
    00095                 case IL_SEEK_CUR:
    00096                         if (!iseek(Offset >> 3, Mode)) {
    00097                                 BitFile->BitPos += Offset;
    00098                                 BitFile->ByteBitOff = BitFile->BitPos % 8;
    00099                         }
    00100                         break;
    00101                 case IL_SEEK_END:
    00102                         KeepPos = itell();
    00103                         iseek(0, IL_SEEK_END);
    00104                         Len = itell();
    00105                         iseek(0, IL_SEEK_SET);
    00106
    00107                         if (!iseek(Offset >> 3, Mode)) {
    00108                                 BitFile->BitPos = (Len << 3) + Offset;
    00109                                 BitFile->ByteBitOff = BitFile->BitPos % 8;
    00110                         }
    00111
    00112                         break;
    00113
    00114                 default:
    00115                         return 1;
    00116         }
    00117
    00118         return 0;
    00119 }
    00120
    00121
    00122 // hehe, "bread".  It reads data into Buffer from the BITFILE, just like fread for FILE.
    00123 ILint bread(void *Buffer, ILuint Size, ILuint Number, BITFILE *BitFile)
    00124 {
    00125         // Note that this function is somewhat useless: In binary image
    00126         // formats, there are some pad bits after each scanline. This
    00127         // function does not take that into account, so you must use bseek to
    00128         // skip the calculated value of bits.
    00129
    00130         ILuint  BuffPos = 0, Count = Size * Number;
    00131
    00132         while (BuffPos < Count) {
    00133                 if (BitFile->ByteBitOff < 0 || BitFile->ByteBitOff > 7) {
    00134                         BitFile->ByteBitOff = 7;
    00135                         if (iread(&BitFile->Buff, 1, 1) != 1)  // Reached eof or error...
    00136                                 return BuffPos;
    00137                 }
    00138
    00139                 *((ILubyte*)(Buffer) + BuffPos) = (ILubyte)!!(BitFile->Buff & (1 << BitFile->ByteBitOff));
    00140
    00141                 BuffPos++;
    00142                 BitFile->ByteBitOff--;
    00143         }
    00144
    00145         return BuffPos;
    00146 }
    00147
    00148
    00149 // Reads bits and puts the first bit in the file as the highest in the return value.
    00150 ILuint breadVal(ILuint NumBits, BITFILE *BitFile)
    00151 {
    00152         ILuint  BuffPos = 0;
    00153         ILuint  Buffer = 0;
    00154
    00155         // Only returning up to 32 bits at one time
    00156         if (NumBits > 32) {
    00157                 ilSetError(IL_INTERNAL_ERROR);
    00158                 return 0;
    00159         }
    00160
    00161         while (BuffPos < NumBits) {
    00162                 Buffer <<= 1;
    00163                 if (BitFile->ByteBitOff < 0 || BitFile->ByteBitOff > 7) {
    00164                         BitFile->ByteBitOff = 7;
    00165                         if (iread(&BitFile->Buff, 1, 1) != 1)  // Reached eof or error...
    00166                                 return BuffPos;
    00167                 }
    00168
    00169                 Buffer = Buffer + (ILubyte)!!(BitFile->Buff & (1 << BitFile->ByteBitOff));
    00170
    00171                 BuffPos++;
    00172                 BitFile->ByteBitOff--;
    00173         }
    00174
    00175         return BuffPos;
    00176 }
    00177
    00178
    00179 // Not implemented yet.
    00180 /*ILint bwrite(void *Buffer, ILuint Size, ILuint Number, BITFILE *BitFile)
    00181 {
    00182
    00183
    00184         return 0;
    00185 }*/