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 IMPASZLIB}
49 { $DEFINE ZLIBPAS}
50 { $DEFINE FPCPASZLIB}
51 { $DEFINE ZLIBEX}
52 { $DEFINE DELPHIZLIB}
54 { Automatically use FPC's PasZLib when compiling with FPC.}
56 {$IFDEF FPC}
57 {$UNDEF IMPASZLIB}
58 {$DEFINE FPCPASZLIB}
59 {$ENDIF}
61 uses
62 {$IF Defined(IMPASZLIB)}
63 { Use paszlib modified by me for Delphi and FPC }
65 {$ELSEIF Defined(FPCPASZLIB)}
66 { Use FPC's paszlib }
68 {$ELSEIF Defined(ZLIBPAS)}
69 { Pascal interface to ZLib shipped with ZLib C source }
70 zlibpas,
71 {$ELSEIF Defined(ZLIBEX)}
72 { Use ZlibEx unit }
73 ZLibEx,
74 {$ELSEIF Defined(DELPHIZLIB)}
75 { Use ZLib unit shipped with Delphi }
76 ZLib,
77 {$IFEND}
80 {$IF Defined(IMPASZLIB) or Defined(FPCPASZLIB) or Defined(ZLIBPAS)}
81 type
83 {$IFEND}
85 const
118 type
119 { Abstract ancestor class }
121 private
127 protected
133 { TCompressionStream compresses data on the fly as data is written to it, and
134 stores the compressed data to another stream.
136 TCompressionStream is write-only and strictly sequential. Reading from the
137 stream will raise an exception. Using Seek to move the stream pointer
138 will raise an exception.
140 Output data is cached internally, written to the output stream only when
141 the internal output buffer is full. All pending output data is flushed
142 when the stream is destroyed.
144 The Position property returns the number of uncompressed bytes of
145 data that have been written to the stream so far.
147 CompressionRate returns the on-the-fly percentage by which the original
148 data has been compressed: (1 - (CompressedBytes / UncompressedBytes)) * 100
149 If raw data size = 100 and compressed data size = 25, the CompressionRate
150 is 75%
152 The OnProgress event is called each time the output buffer is filled and
153 written to the output stream. This is useful for updating a progress
154 indicator when you are writing a large chunk of data to the compression
155 stream in a single call.}
161 private
163 public
173 { TDecompressionStream decompresses data on the fly as data is read from it.
175 Compressed data comes from a separate source stream. TDecompressionStream
176 is read-only and unidirectional; you can seek forward in the stream, but not
177 backwards. The special case of setting the stream position to zero is
178 allowed. Seeking forward decompresses data until the requested position in
179 the uncompressed data has been reached. Seeking backwards, seeking relative
180 to the end of the stream, requesting the size of the stream, and writing to
181 the stream will raise an exception.
183 The Position property returns the number of bytes of uncompressed data that
184 have been read from the stream so far.
186 The OnProgress event is called each time the internal input buffer of
187 compressed data is exhausted and the next block is read from the input stream.
188 This is useful for updating a progress indicator when you are reading a
189 large chunk of data from the decompression stream in a single call.}
192 public
203 { CompressBuf compresses data, buffer to buffer, in one call.
204 In: InBuf = ptr to compressed data
205 InBytes = number of bytes in InBuf
206 Out: OutBuf = ptr to newly allocated buffer containing decompressed data
207 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
290 try
292 begin
299 finally
304 except
306 raise
312 var
316 begin
318 {$IFNDEF FPCPASZLIB}
321 {$ENDIF}
324 OutBytes := BufInc
325 else
328 try
334 try
336 begin
343 finally
348 except
350 raise
355 { TCustomZlibStream }
358 begin
362 {$IFNDEF FPCPASZLIB}
365 {$ENDIF}
369 begin
373 { TCompressionStream }
377 const
380 begin
388 begin
391 try
395 begin
402 finally
409 begin
414 begin
419 begin
422 begin
434 begin
437 else
442 begin
445 else
449 { TDecompressionStream }
452 begin
460 begin
466 begin
471 begin
473 begin
476 begin
478 Exit;
490 begin
495 var
498 begin
500 begin
506 end
509 begin
512 begin
517 end
518 else