Advertisement
Guest User

Untitled

a guest
Apr 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. #include "OF12BitStream.h"
  2. using namespace std;
  3. OF12BitStream::OF12BitStream()
  4. {
  5. init();
  6. }
  7. OF12BitStream::OF12BitStream(const char * aFileName)
  8. {
  9. open(aFileName);
  10. init();
  11. }
  12.  
  13. OF12BitStream::~OF12BitStream()
  14. {
  15. close();
  16. }
  17.  
  18. void OF12BitStream::init()
  19. {
  20. for (int i = 0; i < 32; i++) {
  21. fBuffer[i] = 0;
  22. }
  23. fByteIndex = 0;
  24. fBitIndex = 8;
  25. }
  26.  
  27. void OF12BitStream::writeBit0()
  28. {
  29. fBitIndex--;
  30. finishWriteBit();
  31. }
  32.  
  33. void OF12BitStream::writeBit1()
  34. {
  35. fBuffer[fByteIndex] += 1 << (fBitIndex - 1);
  36. fBitIndex--;
  37. finishWriteBit();
  38. }
  39.  
  40. void OF12BitStream::finishWriteBit()
  41. {
  42. if (fBitIndex == 0)
  43. {
  44. if (fByteIndex == 31)
  45. {
  46. fByteIndex++;
  47. flush();
  48. }
  49. else
  50. {
  51. fByteIndex++;
  52. fBitIndex = 8;
  53. }
  54. }
  55. }
  56.  
  57.  
  58. void OF12BitStream::open(const char * aFileName)
  59. {
  60. ofstream::open(aFileName, ofstream::binary);
  61. }
  62.  
  63. void OF12BitStream::close()
  64. {
  65. flush();
  66. ofstream::close();
  67. }
  68.  
  69. OF12BitStream & OF12BitStream::put(int aValue)
  70. {
  71. fBuffer[fByteIndex] = aValue;
  72. fByteIndex++;
  73. return *this;
  74. }
  75.  
  76. OF12BitStream & OF12BitStream::write(int aValues[], unsigned int aNElements)
  77. {
  78. for (int i = 0; i < aNElements; i++) {
  79. aValues[i] = aValues[i] & 0x0fff; // mask 12 lower bits
  80.  
  81. for (int i = 0; i < 12; i++) // write 12 bits
  82. {
  83. if (aValues[i] & 0x01) // the current lowest bit is set.
  84. writeBit1();
  85. else
  86. writeBit0();
  87. aValues[i] >>= 1; // code := code / 2
  88. }
  89. put(aValues[i]);
  90. }
  91. return *this;
  92. }
  93.  
  94. OF12BitStream & OF12BitStream::operator<<(int aValue)
  95. {
  96. put(aValue);
  97. return *this;
  98. }
  99.  
  100. OF12BitStream & OF12BitStream::flush()
  101. {
  102. write((int*)fBuffer, fByteIndex + (fBitIndex % 8 ? 1 : 0));
  103. return *this;
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement