Enhanced ZLib Compression Support

Omówienie

HotPDF zawiera teraz kompleksową obsługę kompresji zgodnej z ZLib, opartą na dołączonym zlib-ng dla strumieni i treści PDF. API zgodne z zlib zapewnia wydajną kompresję deflate/inflate, szeroko używaną w dokumentach PDF dla operacji filtra FlateDecode i ogólnej kompresji danych.

Key Features

  • ZLib-compatible API backed by bundled zlib-ng
  • PDF FlateDecode parameters support
  • Stream-based compression and decompression
  • Enhanced error handling and debugging capabilities
  • Memory-efficient processing for large data streams
  • Win64 one-shot image compression bridge for zlib-ng length ABI safety
  • Win64x and diagnostic MSVC Win64 zlib-ng runtime SIMD dispatch with generic fallbacks
  • 32-byte aligned C allocation bridge for SIMD-friendly native buffers
  • Cross-platform C library integration
  • Multiple compression levels (none, fastest, default, maximum)

Implementacja techniczna

Rozszerzona obsługa ZLib jest implementowana przez unit HPDFZLib.pas, który zapewnia:

  • Complete ZLib API: Full implementation of ZLib functions
  • Stream Classes: TCustomZStream and derived classes for stream processing
  • PDF Integration: TPDFFlateParms for PDF-specific parameters
  • Enhanced Functions: InflateStr, InflateStrParms for simplified usage

64-bitowe zestawy obiektów zlib-ng obejmują ścieżki SSE2, SSSE3, SSE4.1, SSE4.2, PCLMULQDQ i AVX2 wybierane w runtime. Win32 używa ogólnego zestawu obiektów zlib-ng, aby zachować zgodność z toolchainem OMF bcc32c, podczas gdy obiekty JPEG/TIFF Win32 pozostają przy klasycznym bcc32.

Pokrycie regresji

Backend Flate zlib-ng jest objęty zautomatyzowanymi zestawami regresji Delphi i C++Builder. Bieżący zestaw walidacji przechodzi 20 testów Delphi DUnitX na Win32 i 20 na Win64 oraz 17 testów C++Builder GoogleTest na Win32 i 17 na Win64x. Przebudowy obiektów natywnych także przechodzą z czystych wyjść: 206 obiektów Win32, 205 obiektów Win64x i 206 obiektów MSVC Win64. Pokryte przepływy obejmują kompresję strumieni stron, kompresję osadzonych fontów i ToUnicode CMap, strumienie obrazów Flate, standardowe umieszczanie obrazów JPEG, import TIFF, wyniki z wieloma kodami kreskowymi, scenariusze kopiowania/łączenia/edycji oraz PDF generowane przez demo.

Compression Levels

  • Z_NO_COMPRESSION (0): No compression
  • Z_BEST_SPEED (1): Fastest compression
  • Z_BEST_COMPRESSION (9): Maximum compression
  • Z_DEFAULT_COMPRESSION (-1): Default balance of speed and compression

Usage Examples

Basic String Compression/Decompression


// Delphi example - Basic string inflation
function DecompressString(const CompressedData: AnsiString): AnsiString;
begin
  // Simple decompression
  Result := InflateStr(CompressedData);

  // With debug output enabled
  Result := InflateStr(CompressedData, True);
end;
        

Stream-based Compression


// Delphi example - Stream-based decompression
procedure DecompressStream(InStream, OutStream: TStream);
begin
  // Decompress from input stream to output stream
  PLZDecompressStream(InStream, OutStream);

  // For raw deflate data (without ZLib headers)
  PLZDecompressStreamRaw(InStream, OutStream);
end;
        

PDF FlateDecode with Parameters


// Delphi example - PDF FlateDecode with parameters
function DecompressPDFStream(const CompressedData: AnsiString;
  Predictor, Colors, BitsPerComponent, Columns: Integer): AnsiString;
var
  Parms: TPDFFlateParms;
begin
  // Configure PDF parameters
  Parms.Predictor := Predictor;
  Parms.Colors := Colors;
  Parms.BitsPerComponent := BitsPerComponent;
  Parms.Columns := Columns;
  Parms.ExpandedTo8Bit := True;
  Parms.ColorSpace := 'DeviceRGB';

  // Decompress with PDF parameters
  Result := InflateStrParms(CompressedData, Parms);
end;
        

Low-level ZLib Operations


// Delphi example - Low-level ZLib operations
procedure CompressData;
var
  strm: z_stream;
  ret: Integer;
  input: AnsiString;
  output: AnsiString;
begin
  input := 'Data to compress';
  SetLength(output, Length(input) * 2);

  // Initialize stream
  FillChar(strm, SizeOf(strm), 0);
  ret := deflateInit(strm, Z_DEFAULT_COMPRESSION);

  if ret = Z_OK then
  try
    // Set input
    strm.next_in := PByte(PAnsiChar(input));
    strm.avail_in := Length(input);

    // Set output
    strm.next_out := PByte(PAnsiChar(output));
    strm.avail_out := Length(output);

    // Compress
    ret := deflate(strm, Z_FINISH);

    if ret = Z_STREAM_END then
    begin
      // Compression successful
      SetLength(output, Length(output) - strm.avail_out);
    end;
  finally
    deflateEnd(strm);
  end;
end;
        

PDF FlateDecode Parameters

TPDFFlateParms Structure

  • Predictor: Predictor function (1=no prediction, 2=TIFF, 10-15=PNG)
  • Colors: Number of color components
  • BitsPerComponent: Bits per color component
  • Columns: Number of samples per row
  • ExpandedTo8Bit: Whether to expand to 8-bit components
  • ColorSpace: Color space identifier

Enhanced Features

  • Memory Management: Optimized memory allocation and deallocation
  • Error Handling: Comprehensive error checking and reporting
  • Debug Support: Optional debug output for troubleshooting
  • Performance Optimization: Highly optimized compression algorithms
  • Stream Integration: Seamless integration with Delphi stream classes

Performance Benefits

  • Fast compression and decompression speeds
  • Excellent compression ratios for text and simple graphics
  • Low memory footprint during processing
  • Optimized for PDF content streams
  • Minimal CPU overhead

Standards Compliance

  • RFC 1950 (ZLib format specification)
  • RFC 1951 (Deflate compression algorithm)
  • PDF FlateDecode filter specification
  • PNG predictor algorithm support
  • TIFF predictor algorithm support

Error Codes and Handling

  • Z_OK (0): Success
  • Z_STREAM_END (1): End of stream reached
  • Z_NEED_DICT (2): Dictionary required
  • Z_ERRNO (-1): File error
  • Z_STREAM_ERROR (-2): Stream state error
  • Z_DATA_ERROR (-3): Data integrity error
  • Z_MEM_ERROR (-4): Memory allocation error
  • Z_BUF_ERROR (-5): Buffer size error

Zobacz też