2
#include "BitPumpJPEG.h"
4
RawSpeed - RAW file decoder.
6
Copyright (C) 2009 Klaus Post
8
This library is free software; you can redistribute it and/or
9
modify it under the terms of the GNU Lesser General Public
10
License as published by the Free Software Foundation; either
11
version 2 of the License, or (at your option) any later version.
13
This library is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16
Lesser General Public License for more details.
18
You should have received a copy of the GNU Lesser General Public
19
License along with this library; if not, write to the Free Software
20
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22
http://www.klauspost.com
24
This is based on code by Hubert Figuiere.
25
Copyright (C) 2007 Hubert Figuiere, released under LGPL
30
/*** Used for entropy encoded sections ***/
32
#define BITS_PER_LONG (8*sizeof(uint32))
33
#define MIN_GET_BITS (BITS_PER_LONG-7) /* max value for long getBuffer */
36
BitPumpJPEG::BitPumpJPEG(ByteStream *s):
37
buffer(s->getData()), size(s->getRemainSize() + sizeof(uint32)), mLeft(0), mCurr(0), off(0) {
42
BitPumpJPEG::BitPumpJPEG(const uchar8* _buffer, uint32 _size) :
43
buffer(_buffer), size(_size + sizeof(uint32)), mLeft(0), mCurr(0), off(0) {
48
void __inline BitPumpJPEG::init() {
53
uint32 BitPumpJPEG::getBit() {
56
return (mCurr >> (--mLeft)) & 1;
60
uint32 BitPumpJPEG::getBits(uint32 nbits) {
66
return ((mCurr >> (mLeft -= (nbits)))) & ((1 << nbits) - 1);
70
uint32 BitPumpJPEG::peekBit() {
72
return (mCurr >> (mLeft - 1)) & 1;
76
uint32 BitPumpJPEG::peekBits(uint32 nbits) {
80
return ((mCurr >> (mLeft - nbits))) & ((1 << nbits) - 1);
84
uint32 BitPumpJPEG::peekByte() {
89
throw IOException("Out of buffer read");
91
return ((mCurr >> (mLeft - 8))) & 0xff;
95
uint32 BitPumpJPEG::getBitSafe() {
99
throw IOException("Out of buffer read");
102
return (mCurr >> (--mLeft)) & 1;
106
uint32 BitPumpJPEG::getBitsSafe(unsigned int nbits) {
107
if (nbits > MIN_GET_BITS)
108
throw IOException("Too many bits requested");
114
return ((mCurr >> (mLeft -= (nbits)))) & ((1 << nbits) - 1);
118
void BitPumpJPEG::skipBits(unsigned int nbits) {
119
_ASSERTE(nbits < 24);
130
uchar8 BitPumpJPEG::getByte() {
135
return ((mCurr >> (mLeft -= 8))) & 0xff;
139
uchar8 BitPumpJPEG::getByteSafe() {
145
return ((mCurr >> (mLeft -= 8))) & 0xff;
149
void BitPumpJPEG::setAbsoluteOffset(unsigned int offset) {
151
throw IOException("Offset set out of buffer");
159
BitPumpJPEG::~BitPumpJPEG(void) {
162
} // namespace RawSpeed