72031aa9d1a87141b3f94086549d096e7afaae1e
1 {*******************************************************}
2 { }
3 { Delphi Supplemental Components }
4 { ZLIB Data Compression Interface Unit }
5 { }
6 { Copyright (c) 1997 Borland International }
7 { Copyright (c) 1998 Jacques Nomssi Nzali }
8 { }
9 {*******************************************************}
11 {
12 Modified for
13 Vampyre Imaging Library
14 by Marek Mauder
15 http://imaginglib.sourceforge.net
17 You can choose which pascal zlib implementation will be
18 used. IMPASZLIB and FPCPASZLIB are translations of zlib
19 to pascal so they don't need any *.obj files.
20 The others are interfaces to *.obj files (Windows) or
21 *.so libraries (Linux).
22 Default implementation is IMPASZLIB because it can be compiled
23 by all supported compilers and works on all supported platforms.
24 I usually use implementation with the fastest decompression
25 when building release Win32 binaries.
26 FPCPASZLIB is useful for Lazarus applications. FPC's zlib is linked
27 to exe by default so there is no need to link additional (and almost identical)
28 IMPASZLIB.
30 There is a small speed comparison table of some of the
31 supported implementations (TGA image 28 311 570 bytes, compression level = 6,
32 Delphi 9, Win32, Athlon XP 1900).
34 ZLib version Decompression Compression Comp. Size
35 IMPASZLIB | 1.1.2 | 824 ms | 4 280 ms | 18 760 133 B
36 ZLIBEX | 1.2.2 | 710 ms | 1 590 ms* | 19 056 621 B
37 DELPHIZLIB | 1.0.4 | 976 ms | 9 190 ms | 18 365 562 B
38 ZLIBPAS | 1.2.3 | 680 ms | 3 790 ms | 18 365 387 B
39 * obj files are compiled with compression level hardcoded to 1 (fastest)
40 }
44 {$I ImagingOptions.inc}
46 interface
48 { $DEFINE ZLIBEX}
49 { $DEFINE DELPHIZLIB}
50 { $DEFINE ZLIBPAS}
51 {$DEFINE IMPASZLIB}
52 { $DEFINE FPCPASZLIB}
54 { Automatically use FPC's PasZLib when compiling with Lazarus.}
56 {$IFDEF LCL}
57 {$UNDEF IMPASZLIB}
58 {$DEFINE FPCPASZLIB}
59 {$ENDIF}
61 uses
62 {$IF Defined(ZLIBEX)}
63 { Use ZlibEx unit.}
64 ZLibEx,
65 {$ELSEIF Defined(DELPHIZLIB)}
66 { Use ZLib unit shipped with Delphi.}
67 ZLib,
68 {$ELSEIF Defined(ZLIBPAS)}
69 { Pascal interface to ZLib shipped with ZLib C source.}
70 zlibpas,
71 {$ELSEIF Defined(IMPASZLIB)}
72 { Use paszlib modified by me for Delphi and FPC.}
74 {$ELSEIF Defined(FPCPASZLIB)}
75 { Use FPC's paszlib.}
77 {$IFEND}
80 {$IF Defined(IMPASZLIB) or Defined(FPCPASZLIB) or Defined(ZLIBPAS)}
81 type
83 {$IFEND}
84 {$IFDEF ZLIBEX}
85 const
117 {$ENDIF}
119 type
120 { Abstract ancestor class }
122 private
128 protected
134 { TCompressionStream compresses data on the fly as data is written to it, and
135 stores the compressed data to another stream.
137 TCompressionStream is write-only and strictly sequential. Reading from the
138 stream will raise an exception. Using Seek to move the stream pointer
139 will raise an exception.
141 Output data is cached internally, written to the output stream only when
142 the internal output buffer is full. All pending output data is flushed
143 when the stream is destroyed.
145 The Position property returns the number of uncompressed bytes of
146 data that have been written to the stream so far.
148 CompressionRate returns the on-the-fly percentage by which the original
149 data has been compressed: (1 - (CompressedBytes / UncompressedBytes)) * 100
150 If raw data size = 100 and compressed data size = 25, the CompressionRate
151 is 75%
153 The OnProgress event is called each time the output buffer is filled and
154 written to the output stream. This is useful for updating a progress
155 indicator when you are writing a large chunk of data to the compression
156 stream in a single call.}
162 private
164 public
174 { TDecompressionStream decompresses data on the fly as data is read from it.
176 Compressed data comes from a separate source stream. TDecompressionStream
177 is read-only and unidirectional; you can seek forward in the stream, but not
178 backwards. The special case of setting the stream position to zero is
179 allowed. Seeking forward decompresses data until the requested position in
180 the uncompressed data has been reached. Seeking backwards, seeking relative
181 to the end of the stream, requesting the size of the stream, and writing to
182 the stream will raise an exception.
184 The Position property returns the number of bytes of uncompressed data that
185 have been read from the stream so far.
187 The OnProgress event is called each time the internal input buffer of
188 compressed data is exhausted and the next block is read from the input stream.
189 This is useful for updating a progress indicator when you are reading a
190 large chunk of data from the decompression stream in a single call.}
193 public
204 { CompressBuf compresses data, buffer to buffer, in one call.
205 In: InBuf = ptr to compressed data
206 InBytes = number of bytes in InBuf
207 Out: OutBuf = ptr to newly allocated buffer containing decompressed data
208 OutBytes = number of bytes in OutBuf }
213 { DecompressBuf decompresses data, buffer to buffer, in one call.
214 In: InBuf = ptr to compressed data
215 InBytes = number of bytes in InBuf
216 OutEstimate = zero, or est. size of the decompressed data
217 Out: OutBuf = ptr to newly allocated buffer containing decompressed data
218 OutBytes = number of bytes in OutBuf }
223 type
228 implementation
230 const
244 begin
249 begin
254 begin
261 begin
270 var
273 begin
275 {$IFNDEF FPCPASZLIB}
278 {$ENDIF}
281 try
287 try
289 begin
296 finally
301 except
303 raise
309 var
313 begin
315 {$IFNDEF FPCPASZLIB}
318 {$ENDIF}
321 OutBytes := BufInc
322 else
325 try
331 try
333 begin
340 finally
345 except
347 raise
352 { TCustomZlibStream }
355 begin
359 {$IFNDEF FPCPASZLIB}
362 {$ENDIF}
366 begin
370 { TCompressionStream }
374 const
377 begin
385 begin
388 try
392 begin
399 finally
406 begin
411 begin
416 begin
419 begin
431 begin
434 else
439 begin
442 else
446 { TDecompressionStream }
449 begin
457 begin
463 begin
468 begin
470 begin
473 begin
475 Exit;
487 begin
492 var
495 begin
497 begin
503 end
506 begin
509 begin
514 end
515 else