~ubuntu-branches/ubuntu/saucy/darktable/saucy

« back to all changes in this revision

Viewing changes to src/rawspeed/RawSpeed/BitPumpMSB32.h

  • Committer: Bazaar Package Importer
  • Author(s): David Bremner
  • Date: 2011-04-14 23:42:12 UTC
  • Revision ID: james.westby@ubuntu.com-20110414234212-kuffcz5wiu18v6ra
Tags: upstream-0.8
ImportĀ upstreamĀ versionĀ 0.8

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
    RawSpeed - RAW file decoder.
 
3
 
 
4
    Copyright (C) 2009 Klaus Post
 
5
 
 
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.
 
10
 
 
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.
 
15
 
 
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
 
19
 
 
20
    http://www.klauspost.com
 
21
*/
 
22
#pragma once
 
23
#include "ByteStream.h"
 
24
 
 
25
#ifdef MIN_GET_BITS
 
26
#undef MIN_GET_BITS
 
27
#endif
 
28
 
 
29
#define BITS_PER_LONG_LONG (8*sizeof(uint64))
 
30
#define MIN_GET_BITS  (BITS_PER_LONG_LONG-33)    /* max value for long getBuffer */
 
31
 
 
32
namespace RawSpeed {
 
33
 
 
34
// Note: Allocated buffer MUST be at least size+sizeof(uint32) large.
 
35
 
 
36
class BitPumpMSB32
 
37
{
 
38
public:
 
39
  BitPumpMSB32(ByteStream *s);
 
40
  BitPumpMSB32(const uchar8* _buffer, uint32 _size );
 
41
        uint32 getBitsSafe(uint32 nbits);
 
42
        uint32 getBitSafe();
 
43
        uchar8 getByteSafe();
 
44
        void setAbsoluteOffset(uint32 offset);     // Set offset in bytes
 
45
  __inline uint32 getOffset() { return off-(mLeft>>3);}
 
46
  __inline void checkPos()  { if (off>size) throw IOException("Out of buffer read");};        // Check if we have a valid position
 
47
 
 
48
  // Fill the buffer with at least 24 bits
 
49
void fill();
 
50
 
 
51
  __inline uint32 getBit() {
 
52
    if (!mLeft) fill();
 
53
 
 
54
    return (uint32)((mCurr >> (--mLeft)) & 1);
 
55
  }
 
56
 
 
57
  __inline uint32 getBits(uint32 nbits) {
 
58
    if (mLeft < nbits) {
 
59
      fill();
 
60
    }
 
61
 
 
62
    return (uint32)((mCurr >> (mLeft -= (nbits))) & ((1 << nbits) - 1));
 
63
  }
 
64
 
 
65
  virtual ~BitPumpMSB32(void);
 
66
protected:
 
67
  void __inline init();
 
68
  const uchar8* buffer;
 
69
  const uint32 size;            // This if the end of buffer.
 
70
  uint32 mLeft;
 
71
  uint64 mCurr;
 
72
  uint32 off;                  // Offset in bytes
 
73
private:
 
74
};
 
75
 
 
76
} // namespace RawSpeed