summaryrefslogtreecommitdiff
path: root/docview/src/lzwdecompress.pas
blob: 6645890c058f9d15080deacaec081c524d906345 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
{
  fpGUI  -  Free Pascal GUI Toolkit

  Copyright (C) 2001 - Aaron Lawrence (aaronl@consultant.com)
  Copyright (C) 2006 - 2015 See the file AUTHORS.txt, included in this
  distribution, for details of the copyright.

  See the file COPYING.modifiedLGPL, included in this distribution,
  for details about redistributing fpGUI.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  Description:
    LZW decompression code for uncompressing IPF bitmaps.
}

unit LZWDecompress;

{$mode objfpc}{$H+}

interface

uses
  SysUtils, types;

procedure LZWDecompressBlock( pbInput: PByte;
                              number_bytes: LongWord;
                              pbOutput: PByte;
                              Var bytesOut: LongWord;
                              Var FinalCode: byte );

Implementation

(*
/********************************************************************
 *                                                                  *
 *  LZW decompression                                               *
 *                                                                  *
 *******************************************************************/

/*
 *  This is based on code (W) by Peter Fitzsimmons, pfitz@ican.net.
 *  His liner notes in the original:
 *      has its roots in a June 1990
 *      DDJ article "LZW REVISITED", by Shawn M. Regan
 *      --=>revision history<=--
 *      1 lzw.c 21-Aug-96,2:24:36,`PLF' ;
 *      2 lzw.c 24-Aug-96,2:27:24,`PLF' wip
 *
 *  The code has been modified to take the input not from an
 *  open file, but from any memory region. For this, a double
 *  pointer is used, which must be passed to LZWDecompressBlock.
 *  I've also added a few comments for clarity.
 *
 * Ported to Sibyl Pascal by Aaron Lawrence
 * Variables renamed etc to make things clearer.
 */
*)
// -- Stuff for LZW decompression --
const INIT_BITS = 9;
const MAX_BITS = 12;
const HASHING_SHIFT = MAX_BITS - 8;

{if MAX_BITS == 15
const TABLE_SIZE 36768
#elif MAX_BITS == 14
const TABLE_SIZE 18041
#elif MAX_BITS == 13
const TABLE_SIZE 9029
#else}
// For max_bits = 12:
const TABLE_SIZE = 5021;

const CLEAR_TABLE = 256;
const TERMINATOR = 257;
const FIRST_CODE = 258;

function MaxValNBits( N: word ): word;
begin
  Result:= ( 1 shl n ) - 1;
end;

var
  prefix_code: array[ 0..TABLE_SIZE ] of longword;
  append_character: array[ 0..TABLE_SIZE ] of Byte;
  decode_stack: array[ 0..10000 ] of byte;
  bitsPerCode: longint;
  maxDictionaryCode: longint;

(*
 * decode_string:
 *
 *)
function decode_string( buffer: PByte; code: longword ): PByte;
var
  i: longint;
begin
  i:= 0;

  while Code > 255 do
  begin
    buffer^:= append_character[ Code ];
    inc( Buffer );
    code:= prefix_code[ code ];

    inc( i );
    if i > High( decode_stack ) then
      assert( false, 'Out of space decompressing bitmap!' );
  end;

  buffer^ := code;
  Result:= buffer;
end;

(*
 * input_code:
 *      this function reads in bytes from the input
 *      stream.
 *)

var
  bytes_out: longword = 0;
  input_bit_count: longword = 0;
  input_bit_buffer: longword = 0;

// I think this simply reads the next bitsPerCode bits of the input data
// returning the resulting code.
function input_code( var pbInput: PBYTE; bytes_to_read: longword ): longword;
var
  return_value: longword;
begin
  while input_bit_count <= 24 do
  begin
    if bytes_out <= bytes_to_read then
    begin
      input_bit_buffer:= input_bit_buffer
                         or
                         ( ( longword( pbInput^ ) shl (24 - input_bit_count) ) );
      inc( pbInput );
    end
    else
      input_bit_buffer := input_bit_buffer or longword($00);
{ The C version of LZWDecompress uses only "or $00", so I'm assuming the code
  below is not needed. Tested and I see no difference. }
//                         or
//                         ( longword( 0 ) shl ( 24 - input_bit_count ) );
    inc( bytes_out );
    inc( input_bit_count, 8 );
  end;

  return_value:= input_bit_buffer shr (32 - bitsPerCode);
  input_bit_buffer:= input_bit_buffer shl bitsPerCode;
  dec( input_bit_count, bitsPerCode );

  if bytes_out > bytes_to_read then
  begin
    // flush static vars and quit
    bytes_out:= 0;
    input_bit_count:= 0;
    input_bit_buffer:= 0;
    Result:= TERMINATOR;
  end
  else
    Result:= return_value;
end;

// LZWDecompressBlock:
//      this takes one of the INF bitmap blocks
//      and decompresses it using LZW algorithms.

procedure LZWDecompressBlock(pbInput: PByte; number_bytes: LongWord; pbOutput: PByte; var bytesOut: LongWord; var FinalCode: byte);
var
  nextAvailableCode: LongWord;
  currentCode: LongWord;
  lastCode: LongWord;
  character: longword;
  clear_flag: boolean;
  theString: pByte;
begin
  clear_flag:= true;

  nextAvailableCode:= FIRST_CODE;
  bitsPerCode:= INIT_BITS;
  maxDictionaryCode:= MaxValNBits( bitsPerCode );

  bytesOut:= 0;
  input_bit_count:= 0;
  input_bit_buffer:= 0;

  // read the first code from input
  currentCode:= input_code( pbInput, number_bytes );
  while currentCode <> TERMINATOR do
  begin
    if clear_flag then
    begin
      clear_flag := false;
      lastCode := currentCode;
      character := currentCode;

      pbOutput^ := currentCode;
      inc( pbOutput );
      FinalCode := currentCode;
      inc( BytesOut );
    end
    else if currentCode = CLEAR_TABLE then
    begin
      clear_flag := true;
      nextAvailableCode := FIRST_CODE;
      bitsPerCode := INIT_BITS;
      maxDictionaryCode := MaxValNBits( bitsPerCode );
    end
    else
    begin
      if currentCode >= nextAvailableCode then
      begin
         decode_stack[ 0 ]:= character;
         theString := decode_string( Addr( decode_stack[ 1 ] ),
                                    lastCode );
      end
      else
        theString := decode_string( Addr( decode_stack[ 0 ] ),
                                   currentCode );

      character := longword( theString^ );
      while theString >= Addr( decode_stack[ 0 ] ) do
      begin
        FinalCode := theString^;

        pbOutput^ := theString^;
        inc( pbOutput );
        dec( TheString );

        inc( BytesOut );
      end;

      if nextAvailableCode <= maxDictionaryCode then
      begin
        prefix_code[ nextAvailableCode ]:= lastCode;
        append_character[ nextAvailableCode ]:= character;

        inc( nextAvailableCode );

        if ( nextAvailableCode = maxDictionaryCode ) and ( bitsPerCode < MAX_BITS ) then
        begin
          // expand dictionary
          inc( bitsPerCode );
          maxDictionaryCode:= MaxValNBits( bitsPerCode );
        end;
      end;

      lastCode:= currentCode;
    end;

    // Read next code from input
    currentCode:= input_code( pbInput, number_bytes );
  end;
end;


End.