2
RawSpeed - RAW file decoder.
4
Copyright (C) 2009 Klaus Post
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Lesser General Public
8
License as published by the Free Software Foundation; either
9
version 2 of the License, or (at your option) any later version.
11
This library is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
Lesser General Public License for more details.
16
You should have received a copy of the GNU Lesser General Public
17
License along with this library; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
http://www.klauspost.com
22
#ifndef BIT_PUMP_MSB_H
23
#define BIT_PUMP_MSB_H
25
#include "ByteStream.h"
27
#define BITS_PER_LONG (8*sizeof(uint32))
28
#define MIN_GET_BITS (BITS_PER_LONG-7) /* max value for long getBuffer */
32
// Note: Allocated buffer MUST be at least size+sizeof(uint32) large.
37
BitPumpMSB(ByteStream *s);
38
BitPumpMSB(const uchar8* _buffer, uint32 _size );
39
uint32 getBitsSafe(uint32 nbits);
42
void setAbsoluteOffset(uint32 offset); // Set offset in bytes
43
__inline uint32 getOffset() { return off-(mLeft>>3);}
44
__inline void checkPos() { if (off>size+12) throw IOException("Out of buffer read");}; // Check if we have a valid position
46
// Fill the buffer with at least 24 bits
48
__inline uint32 peekBitsNoFill( uint32 nbits )
50
int shift = mLeft-nbits;
51
uint32 ret = *(uint32*)¤t_buffer[shift>>3];
53
return ret & ((1 << nbits) - 1);
57
__inline uint32 getBit() {
60
uint32 _byte = mLeft >> 3;
61
return (current_buffer[_byte] >> (mLeft & 0x7)) & 1;
64
__inline uint32 getBitsNoFill(uint32 nbits) {
65
uint32 ret = peekBitsNoFill(nbits);
69
__inline uint32 getBits(uint32 nbits) {
71
return getBitsNoFill(nbits);
74
__inline uint32 peekBit() {
76
return (current_buffer[(mLeft-1) >> 3] >> ((mLeft-1) & 0x7)) & 1;
78
__inline uint32 getBitNoFill() {
80
uint32 ret = (current_buffer[mLeft >> 3] >> (mLeft & 0x7)) & 1;
84
__inline uint32 peekByteNoFill() {
86
uint32 ret = *(uint32*)¤t_buffer[shift>>3];
91
__inline uint32 peekBits(uint32 nbits) {
93
return peekBitsNoFill(nbits);
96
__inline uint32 peekByte() {
100
throw IOException("Out of buffer read");
102
return peekByteNoFill();
105
__inline void skipBits(unsigned int nbits) {
109
int n = MIN(nbits, mLeft);
115
__inline void skipBitsNoFill(unsigned int nbits) {
119
__inline unsigned char getByte() {
123
uint32 ret = *(uint32*)¤t_buffer[shift>>3];
128
virtual ~BitPumpMSB(void);
130
void __inline init();
131
const uchar8* buffer;
132
uchar8* current_buffer;
133
const uint32 size; // This if the end of buffer.
135
uint32 off; // Offset in bytes
139
} // namespace RawSpeed
141
#endif//BIT_PUMP_MSB_H