libopenraw
bititerator.cpp
1 /* -*- tab-width:4; c-basic-offset:4 -*- */
2 /*
3  * libopenraw - bititerator.cpp
4  *
5  * Copyright (C) 2008 Rafael Avila de Espindola.
6  *
7  * This library is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation, either version 3 of
10  * the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library. If not, see
19  * <http://www.gnu.org/licenses/>.
20  */
21 
22 #include <assert.h>
23 #include "bititerator.h"
24 
25 namespace OpenRaw {
26 namespace Internals {
27 
28 BitIterator::BitIterator(const void * const p) :
29  m_p(static_cast<const uint8_t * const>(p)),
30  m_bitBuffer(0), m_bitsOnBuffer(0)
31 
32 {
33 }
34 
35 void BitIterator::load(size_t numBits)
36 {
37  size_t numBytes = (numBits + 7) / 8;
38 
39  //align the bits on the right
40  m_bitBuffer >>= (32 - m_bitsOnBuffer);
41 
42  m_bitsOnBuffer += 8 * numBytes;
43 
44  //load the new bits from the right
45  for (size_t i = 0; i < numBytes; ++i) {
46  m_bitBuffer = (m_bitBuffer << 8) | *m_p;
47  ++m_p;
48  }
49 
50  //align the bits on the left
51  m_bitBuffer = m_bitBuffer << (32 - m_bitsOnBuffer);
52 }
53 
54 uint32_t BitIterator::get(size_t n)
55 {
56  assert(n <= 25);
57 
58  if (n == 0)
59  return 0;
60 
61  if (n > m_bitsOnBuffer)
62  load(n - m_bitsOnBuffer);
63 
64  assert(n <= m_bitsOnBuffer);
65 
66  uint32_t ret = m_bitBuffer >> (32 - n);
67  m_bitsOnBuffer -= n;
68  m_bitBuffer <<= n;
69 
70  return ret;
71 }
72 
73 }
74 }
CIFF is the container for CRW files. It is an attempt from Canon to make this a standard. I guess it failed.
Definition: arwfile.cpp:33