OFFIS DCMTK  Version 3.6.0
encodstr.h
1 //
2 // (C) Jan de Vaan 2007-2010, all rights reserved. See the accompanying "License.txt" for licensed use.
3 //
4 
5 #ifndef CHARLS_ENCODERSTRATEGY
6 #define CHARLS_ENCODERSTRATEGY
7 
8 #include "dcmtk/ofstd/ofaptr.h"
9 #include "procline.h"
10 #include "decodstr.h"
11 
12 // Implements encoding to stream of bits. In encoding mode JpegLsCodec inherits from EncoderStrategy
13 
15 {
16 
17 public:
18  explicit EncoderStrategy(const JlsParameters& info) :
19  _qdecoder(0),
20  _info(info),
21  _processLine(0),
22  valcurrent(0),
23  bitpos(0),
24  _isFFWritten(false),
25  _bytesWritten(0)
26 
27  {
28  }
29 
30  virtual ~EncoderStrategy()
31  {
32  }
33 
34  LONG PeekByte();
35 
36  void OnLineBegin(LONG cpixel, void* ptypeBuffer, LONG pixelStride)
37  {
38  _processLine->NewLineRequested(ptypeBuffer, cpixel, pixelStride);
39  }
40 
41  void OnLineEnd(LONG /*cpixel*/, void* /*ptypeBuffer*/, LONG /*pixelStride*/) { }
42 
43  virtual void SetPresets(const JlsCustomParameters& presets) = 0;
44 
45  virtual size_t EncodeScan(const void* pvoid, void* pvoidOut, size_t byteCount, void* pvoidCompare) = 0;
46 
47 protected:
48 
49  void Init(BYTE* compressedBytes, size_t byteCount)
50  {
51  bitpos = 32;
52  valcurrent = 0;
53  _position = compressedBytes;
54  _compressedLength = byteCount;
55  }
56 
57 
58  void AppendToBitStream(LONG value, LONG length)
59  {
60  ASSERT(length < 32 && length >= 0);
61 
62  ASSERT((_qdecoder.get() == NULL) || (length == 0 && value == 0) ||( _qdecoder->ReadLongValue(length) == value));
63 
64 #ifndef NDEBUG
65  if (length < 32)
66  {
67  int mask = (1 << (length)) - 1;
68  ASSERT((value | mask) == mask);
69  }
70 #endif
71 
72  bitpos -= length;
73  if (bitpos >= 0)
74  {
75  valcurrent = valcurrent | (value << bitpos);
76  return;
77  }
78  valcurrent |= value >> -bitpos;
79 
80  Flush();
81 
82  ASSERT(bitpos >=0);
83  valcurrent |= value << bitpos;
84 
85  }
86 
87  void EndScan()
88  {
89  Flush();
90 
91  // if a 0xff was written, Flush() will force one unset bit anyway
92  if (_isFFWritten)
93  AppendToBitStream(0, (bitpos - 1) % 8);
94  else
95  AppendToBitStream(0, bitpos % 8);
96 
97  Flush();
98  ASSERT(bitpos == 0x20);
99  }
100 
101  void Flush()
102  {
103  for (LONG i = 0; i < 4; ++i)
104  {
105  if (bitpos >= 32)
106  break;
107 
108  if (_isFFWritten)
109  {
110  // insert highmost bit
111  *_position = BYTE(valcurrent >> 25);
112  valcurrent = valcurrent << 7;
113  bitpos += 7;
114  _isFFWritten = false;
115  }
116  else
117  {
118  *_position = BYTE(valcurrent >> 24);
119  valcurrent = valcurrent << 8;
120  bitpos += 8;
121  _isFFWritten = *_position == 0xFF;
122  }
123 
124  _position++;
125  _compressedLength--;
126  _bytesWritten++;
127 
128  }
129 
130  }
131 
132  size_t GetLength()
133  {
134  return _bytesWritten - (bitpos -32)/8;
135  }
136 
137 
138  inlinehint void AppendOnesToBitStream(LONG length)
139  {
140  AppendToBitStream((1 << length) - 1, length);
141  }
142 
143 
144  OFauto_ptr<DecoderStrategy> _qdecoder;
145 
146 protected:
147  JlsParameters _info;
148  OFauto_ptr<ProcessLine> _processLine;
149 private:
150 
151  unsigned int valcurrent;
152  LONG bitpos;
153  size_t _compressedLength;
154 
155  // encoding
156  BYTE* _position;
157  bool _isFFWritten;
158  size_t _bytesWritten;
159 
160 };
161 
162 #endif


Generated on Thu Dec 20 2012 for OFFIS DCMTK Version 3.6.0 by Doxygen 1.8.2