HDK
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
UT_BitStream.h
Go to the documentation of this file.
1 /*
2  * PROPRIETARY INFORMATION. This software is proprietary to
3  * Side Effects Software Inc., and is not to be reproduced,
4  * transmitted, or disclosed in any way without written permission.
5  *
6  * NAME: UT_BitStream.h ( UT Library, C++)
7  *
8  * COMMENTS: This allows you to read and write from binary
9  * buffers on a per bit basis. It can handle up to 32bit
10  * reads or writes.
11  */
12 
13 #ifndef __UT_BitStream__
14 #define __UT_BitStream__
15 
16 #include "UT_API.h"
17 
18 #include "UT_IntArray.h"
19 
20 class UT_String;
21 class UT_WorkBuffer;
22 
24 {
25 public:
26  UT_BitStream();
27  UT_BitStream(int nbits);
28  virtual ~UT_BitStream();
29 
30  UT_BitStream(const UT_BitStream &) = delete;
31  UT_BitStream &operator=(const UT_BitStream &) = delete;
32 
33  // Delete & reset the buffer.
34  void clear();
35 
36  int getLen() const;
37  unsigned int *getBuf() const { return myBuf; }
38  void setBuf(unsigned int *buf, int len);
39 
40  int getBitLength() const { return myBitLength; }
41 
42  // Note that the write/read head is shared, so it is not proper
43  // to try and write and read to the same buffer.
44  // You can write to the buffer, do a reset, and then read, however.
45  // Just be careful you don't try to read past the end or you'll assert.
46  void write(int val, int bits);
47  int read(int bits); // Read, sign extended
48  unsigned uread(int bits); // Read, zero extended
49 
50  // Seek nbits into the stream. When:
51  // whence[0] -- Seek from beginning of stream
52  // whence[1] -- Seek from current location
53  // whence[2] -- Seek from end of stream
54  int seek(int nbits, int whence = 0);
55  int tell() const;
56  int rewind() { return seek(0, 0); }
57 
58  //
59  // All the save methods take a number of "words" to process. The number of
60  // bits per word is defined by the method. For example, saveToHex() has a
61  // word-size of 4 bits. This may be counter intuitive, but that's how it
62  // works.
63  // The offset parameter determines how many BITS into the stream to start
64  // reading from when generating the string.
65  //
66 
67  // Load from a hexidecimal string or generate a hex string
68  // (word size = 4 bits)
69  void loadFromHex(const char *hex);
70  void saveToHex(UT_String &result, int nwords=-1,
71  int offset=0);
72  void saveToHex(UT_WorkBuffer &result, int nwords=-1,
73  int offset=0);
74 
75  // Load or save a 6-bit encoded string
76  // (word size = 6 bits)
77  void loadFrom6Bit(const char *data);
78  void saveTo6Bit(UT_String &result, int nwords=-1,
79  int offset=0);
80  void saveTo6Bit(UT_WorkBuffer &result, int nwords=-1,
81  int offset=0);
82 
83  // Load or save a 8-bit encoded string. Note that the binary data
84  // may not be handled correctly since nulls can be inserted into the
85  // UT_String (and are considered terminators).
86  // (word size = 8 bits)
87  void loadFromByte(const char *data);
88  void saveToByte(UT_String &result, int nwords=-1,
89  int offset=0);
90  // Load or save a 8-bit encoded buffer. These are used for the binary
91  // data so that it handles the nulls within the data.
92  void loadFromByte(const char *data, int nwords);
93  int saveToByte(unsigned char *data, int nwords,
94  int offset=0);
95 
96  // Initialize from a penta string. If failed, returns non-zero
97  // Also initializes the errors array to the start and end character
98  // of each penta which failed.
99  // A penta is 5 character sequense delimitted by non-alnum chars.
100  // Each one stores 20 bits of data and has a 5 bits of checksum data.
101  int loadFromPenta(const char *penta, UT_IntArray &errors);
102  // Write out the penta, only using the preferred characters.
103  void saveToPenta(UT_String &result, int nwords=-1,
104  int offset=0);
105 
106 protected:
107  // Load a single penta word.
108  int loadPenta(const char *penta);
109  int padTo(int nbits, int max, int off, int &savelen);
110  void popPad(int savelen)
111  {
112  myBitLength = savelen;
113  seek(0, 2); // Seek to end of file again
114  }
115 
116  // Local data.
117  unsigned int *myBuf; // The buffer to read bits from
118  int myHard; // Whether we own the buffer.
119  int myBitOff, myWordOff; // The seek position
120  int myBitLength; // Total written length
121  int mySize; // Allocated size
122 };
123 
124 #endif
125 
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glcorearb.h:2540
int getBitLength() const
Definition: UT_BitStream.h:40
#define UT_API
Definition: UT_API.h:14
**But if you need a result
Definition: thread.h:613
unsigned int * getBuf() const
Definition: UT_BitStream.h:37
GLintptr offset
Definition: glcorearb.h:665
unsigned int * myBuf
Definition: UT_BitStream.h:117
LeafData & operator=(const LeafData &)=delete
ImageBuf OIIO_API max(Image_or_Const A, Image_or_Const B, ROI roi={}, int nthreads=0)
GLuint GLfloat * val
Definition: glcorearb.h:1608
void popPad(int savelen)
Definition: UT_BitStream.h:110
Definition: format.h:895