File: LSBFEncoder.cc

package info (click to toggle)
apngasm 2.91-6
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,760 kB
  • sloc: ansic: 34,259; cpp: 7,258; makefile: 40
file content (42 lines) | stat: -rw-r--r-- 804 bytes parent folder | download | duplicates (20)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "Portable.h"
#include "LSBFEncoder.h"

namespace NStream {
namespace NLSBF {

void CEncoder::WriteBits(UINT32 aValue, UINT32 aNumBits)
{
  while(aNumBits > 0)
  {
    UINT32 aNumNewBits = MyMin(aNumBits, m_BitPos);
    aNumBits -= aNumNewBits;

    UINT32 aMask = (1 << aNumNewBits) - 1;
    m_CurByte |= (aValue & aMask) << (8 - m_BitPos);
    aValue >>= aNumNewBits;

    m_BitPos -= aNumNewBits;

    if (m_BitPos == 0)
    {
      m_Stream.WriteByte(m_CurByte);
      m_BitPos = 8;
      m_CurByte = 0;
    }
  }
}


void CReverseEncoder::WriteBits(UINT32 aValue, UINT32 aNumBits)
{
  UINT32 aReverseValue = 0;
  for(UINT32 i = 0; i < aNumBits; i++) 
  {
    aReverseValue <<= 1;
    aReverseValue |= aValue & 1;
    aValue >>= 1;
  } 
  m_Encoder->WriteBits(aReverseValue, aNumBits);
}

}}