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

« back to all changes in this revision

Viewing changes to src/rawspeed/RawSpeed/BitPumpJPEG.cpp

  • 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
#include "StdAfx.h"
 
2
#include "BitPumpJPEG.h"
 
3
/*
 
4
    RawSpeed - RAW file decoder.
 
5
 
 
6
    Copyright (C) 2009 Klaus Post
 
7
 
 
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.
 
12
 
 
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.
 
17
 
 
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
 
21
 
 
22
    http://www.klauspost.com
 
23
 
 
24
    This is based on code by Hubert Figuiere.
 
25
    Copyright (C) 2007 Hubert Figuiere, released under LGPL
 
26
*/
 
27
 
 
28
namespace RawSpeed {
 
29
 
 
30
/*** Used for entropy encoded sections ***/
 
31
 
 
32
#define BITS_PER_LONG (8*sizeof(uint32))
 
33
#define MIN_GET_BITS  (BITS_PER_LONG-7)    /* max value for long getBuffer */
 
34
 
 
35
 
 
36
BitPumpJPEG::BitPumpJPEG(ByteStream *s):
 
37
    buffer(s->getData()), size(s->getRemainSize() + sizeof(uint32)), mLeft(0), mCurr(0), off(0) {
 
38
  init();
 
39
}
 
40
 
 
41
 
 
42
BitPumpJPEG::BitPumpJPEG(const uchar8* _buffer, uint32 _size) :
 
43
    buffer(_buffer), size(_size + sizeof(uint32)), mLeft(0), mCurr(0), off(0) {
 
44
  init();
 
45
}
 
46
 
 
47
 
 
48
void __inline BitPumpJPEG::init() {
 
49
  stuffed = 0;
 
50
  fill();
 
51
}
 
52
 
 
53
uint32 BitPumpJPEG::getBit() {
 
54
  if (!mLeft) fill();
 
55
 
 
56
  return (mCurr >> (--mLeft)) & 1;
 
57
}
 
58
 
 
59
 
 
60
uint32 BitPumpJPEG::getBits(uint32 nbits) {
 
61
  _ASSERTE(nbits < 24);
 
62
 
 
63
  if (mLeft < nbits) {
 
64
    fill();
 
65
  }
 
66
  return ((mCurr >> (mLeft -= (nbits)))) & ((1 << nbits) - 1);
 
67
}
 
68
 
 
69
 
 
70
uint32 BitPumpJPEG::peekBit() {
 
71
  if (!mLeft) fill();
 
72
  return (mCurr >> (mLeft - 1)) & 1;
 
73
}
 
74
 
 
75
 
 
76
uint32 BitPumpJPEG::peekBits(uint32 nbits) {
 
77
  if (mLeft < nbits) {
 
78
    fill();
 
79
  }
 
80
  return ((mCurr >> (mLeft - nbits))) & ((1 << nbits) - 1);
 
81
}
 
82
 
 
83
 
 
84
uint32 BitPumpJPEG::peekByte() {
 
85
  if (mLeft < 8) {
 
86
    fill();
 
87
  }
 
88
  if (off > size)
 
89
    throw IOException("Out of buffer read");
 
90
 
 
91
  return ((mCurr >> (mLeft - 8))) & 0xff;
 
92
}
 
93
 
 
94
 
 
95
uint32 BitPumpJPEG::getBitSafe() {
 
96
  if (!mLeft) {
 
97
    fill();
 
98
    if (off > size)
 
99
      throw IOException("Out of buffer read");
 
100
  }
 
101
 
 
102
  return (mCurr >> (--mLeft)) & 1;
 
103
}
 
104
 
 
105
 
 
106
uint32 BitPumpJPEG::getBitsSafe(unsigned int nbits) {
 
107
  if (nbits > MIN_GET_BITS)
 
108
    throw IOException("Too many bits requested");
 
109
 
 
110
  if (mLeft < nbits) {
 
111
    fill();
 
112
    checkPos();
 
113
  }
 
114
  return ((mCurr >> (mLeft -= (nbits)))) & ((1 << nbits) - 1);
 
115
}
 
116
 
 
117
 
 
118
void BitPumpJPEG::skipBits(unsigned int nbits) {
 
119
  _ASSERTE(nbits < 24);
 
120
 
 
121
  if (mLeft < nbits) {
 
122
    fill();
 
123
    checkPos();
 
124
  }
 
125
 
 
126
  mLeft -= nbits;
 
127
}
 
128
 
 
129
 
 
130
uchar8 BitPumpJPEG::getByte() {
 
131
  if (mLeft < 8) {
 
132
    fill();
 
133
  }
 
134
 
 
135
  return ((mCurr >> (mLeft -= 8))) & 0xff;
 
136
}
 
137
 
 
138
 
 
139
uchar8 BitPumpJPEG::getByteSafe() {
 
140
  if (mLeft < 8) {
 
141
    fill();
 
142
    checkPos();
 
143
  }
 
144
 
 
145
  return ((mCurr >> (mLeft -= 8))) & 0xff;
 
146
}
 
147
 
 
148
 
 
149
void BitPumpJPEG::setAbsoluteOffset(unsigned int offset) {
 
150
  if (offset >= size)
 
151
    throw IOException("Offset set out of buffer");
 
152
 
 
153
  mLeft = 0;
 
154
  off = offset;
 
155
  fill();
 
156
}
 
157
 
 
158
 
 
159
BitPumpJPEG::~BitPumpJPEG(void) {
 
160
}
 
161
 
 
162
} // namespace RawSpeed