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 <algorithm>
24#include "bititerator.hpp"
25
26namespace OpenRaw {
27namespace Internals {
28
29BitIterator::BitIterator(const uint8_t * const p, size_t size)
30 : m_p(p)
31 , m_size(size)
32 , m_bitBuffer(0)
33 , m_bitsOnBuffer(0)
34
35{
36}
37
38void BitIterator::load(size_t numBits)
39{
40 size_t numBytes = (numBits + 7) / 8;
41
42 //align the bits on the right
43 m_bitBuffer >>= (32 - m_bitsOnBuffer);
44
45 m_bitsOnBuffer += 8 * numBytes;
46
47 //load the new bits from the right
48 for (size_t i = 0; i < numBytes && m_size > 0; ++i) {
49 m_bitBuffer = (m_bitBuffer << 8) | *m_p;
50 ++m_p;
51 m_size--;
52 }
53
54 //align the bits on the left
55 m_bitBuffer = m_bitBuffer << (32 - m_bitsOnBuffer);
56}
57
58uint32_t BitIterator::get(size_t n)
59{
60 uint32_t ret = peek(n);
61
62 skip(n);
63
64 return ret;
65}
66
67uint32_t BitIterator::peek(size_t n)
68{
69 assert(n <= 25);
70
71 if (n == 0)
72 return 0;
73
74 if (n > m_bitsOnBuffer)
75 load(n - m_bitsOnBuffer);
76
77 assert(n <= m_bitsOnBuffer);
78
79 return m_bitBuffer >> (32 - n);
80}
81
82void BitIterator::skip(size_t n)
83{
84 size_t num_bits = std::min(n, m_bitsOnBuffer);
85 m_bitsOnBuffer -= num_bits;
86 m_bitBuffer <<= num_bits;
87}
88
89
90}
91}
CIFF is the container for CRW files. It is an attempt from Canon to make this a standard....
Definition arwfile.cpp:30