ScummVM API documentation
decompressor.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #ifndef SCI_RESOURCE_DECOMPRESSOR_H
23 #define SCI_RESOURCE_DECOMPRESSOR_H
24 
25 #include "common/scummsys.h"
26 
27 namespace Common {
28 class ReadStream;
29 }
30 
31 namespace Sci {
32 
33 enum ResourceCompression {
34  kCompUnknown = -1,
35  kCompNone = 0,
36  kCompLZW,
37  kCompHuffman,
38  kCompLZW1, // LZW-like compression used in SCI01 and SCI1
39  kCompLZW1View, // Comp3 + view Post-processing
40  kCompLZW1Pic, // Comp3 + pic Post-processing
41 #ifdef ENABLE_SCI32
42  kCompSTACpack, // ? Used in SCI32
43 #endif
44  kCompDCL
45 };
46 
51 class Decompressor {
52 public:
53  Decompressor() :
54  _dwBits(0),
55  _nBits(0),
56  _szPacked(0),
57  _szUnpacked(0),
58  _dwRead(0),
59  _dwWrote(0),
60  _src(nullptr),
61  _dest(nullptr)
62  {}
63 
64  virtual ~Decompressor() {}
65 
66 
67  virtual int unpack(Common::ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked);
68 
69 protected:
78  virtual void init(Common::ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked);
79 
86  uint32 getBitsMSB(int n);
87 
94  uint32 getBitsLSB(int n);
95 
100  byte getByteMSB();
101  byte getByteLSB();
102 
103  void fetchBitsMSB();
104  void fetchBitsLSB();
105 
111  virtual void putByte(byte b);
112 
117  bool isFinished() {
118  return (_dwWrote == _szUnpacked) && (_dwRead >= _szPacked);
119  }
120 
121  uint32 _dwBits;
122  byte _nBits;
123  uint32 _szPacked;
124  uint32 _szUnpacked;
125  uint32 _dwRead;
126  uint32 _dwWrote;
127  Common::ReadStream *_src;
128  byte *_dest;
129 };
130 
135 public:
136  int unpack(Common::ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked) override;
137 
138 protected:
139  int16 getc2();
140 
141  byte *_nodes;
142 };
143 
149 public:
150  DecompressorLZW(int nCompression) : _numbits(0), _curtoken(0), _endtoken(0) {
151  _compression = nCompression;
152  }
153  void init(Common::ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked) override;
154  int unpack(Common::ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked) override;
155 
156 protected:
157  enum {
158  PIC_OPX_EMBEDDED_VIEW = 1,
159  PIC_OPX_SET_PALETTE = 2,
160  PIC_OP_OPX = 0xfe
161  };
162  // unpacking procedures
163  // TODO: unpackLZW and unpackLZW1 are similar and should be merged
164  int unpackLZW1(Common::ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked);
165  int unpackLZW(Common::ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked);
166 
167  // functions to post-process view and pic resources
168  void reorderPic(byte *src, byte *dest, int dsize);
169  void reorderView(byte *src, byte *dest);
170  void decodeRLE(byte **rledata, byte **pixeldata, byte *outbuffer, int size);
171  int getRLEsize(byte *rledata, int dsize);
172  void buildCelHeaders(byte **seeker, byte **writer, int celindex, int *cc_lengths, int max);
173 
174  // decompressor data
175  struct Tokenlist {
176  byte data;
177  uint16 next;
178  };
179  uint16 _numbits;
180  uint16 _curtoken, _endtoken;
181  int _compression;
182 };
183 
188 public:
189  int unpack(Common::ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked) override;
190 };
191 
192 #ifdef ENABLE_SCI32
193 
196 class DecompressorLZS : public Decompressor {
197 public:
198  int unpack(Common::ReadStream *src, byte *dest, uint32 nPacked, uint32 nUnpacked) override;
199 protected:
200  int unpackLZS();
201  uint32 getCompLen();
202  void copyComp(int offs, uint32 clen);
203 };
204 #endif
205 
206 } // End of namespace Sci
207 
208 #endif // SCI_RESOURCE_DECOMPRESSOR_H
uint32 _dwRead
number of bytes read from _src
Definition: decompressor.h:125
byte _nBits
number of unread bits in _dwBits
Definition: decompressor.h:122
uint32 _dwBits
bits buffer
Definition: decompressor.h:121
Definition: decompressor.h:175
uint32 _dwWrote
number of bytes written to _dest
Definition: decompressor.h:126
Definition: decompressor.h:148
Definition: algorithm.h:29
Definition: console.h:28
bool isFinished()
Definition: decompressor.h:117
Definition: decompressor.h:51
uint32 _szPacked
size of the compressed data
Definition: decompressor.h:123
uint32 _szUnpacked
size of the decompressed data
Definition: decompressor.h:124
Definition: stream.h:385
Definition: decompressor.h:187
Definition: decompressor.h:134