Enhanced ZLib Compression Support

Prezentare generală

HotPDF include acum suport complet pentru comprimare compatibilă ZLib, susținut de zlib-ng livrat împreună pentru fluxuri și conținut PDF. API-ul compatibil zlib oferă o comprimare deflate/inflate eficientă, folosită pe scară largă în documentele PDF pentru operațiile de filtru FlateDecode și comprimarea generală a datelor

Funcționalități cheie

  • API compatibil ZLib susținut de zlib-ng livrat împreună
  • Suport pentru parametrii PDF FlateDecode
  • Comprimare și decompresie bazate pe fluxuri
  • Gestionare îmbunătățită a erorilor și capabilități de depanare
  • Procesare eficientă din punct de vedere al memoriei pentru fluxuri mari de date
  • Punte de comprimare imagini one-shot pe Win64 pentru siguranța ABI a lungimilor zlib-ng
  • Dispatch SIMD pe runtime pentru zlib-ng Win64x și MSVC Win64 de diagnostic, cu fallback-uri generice
  • Punte de alocare C aliniată la 32 de octeți pentru buffer-e native prietenoase cu SIMD
  • Integrare cross-platform a bibliotecii C
  • Mai multe niveluri de compresie (niciuna, cea mai rapidă, implicită, maximă)

Implementare tehnică

Suportul îmbunătățit ZLib este implementat prin unitatea HPDFZLib.pas, care oferă:

  • API ZLib complet: Implementare completă a funcțiilor ZLib
  • Clase de flux: TCustomZStream și clase derivate pentru procesarea fluxurilor
  • Integrare PDF: TPDFFlateParms pentru parametri specifici PDF
  • Funcții extinse: InflateStr, InflateStrParms pentru utilizare simplificată

Seturile de obiecte zlib-ng pe 64 de biți includ căi SSE2, SSSE3, SSE4.1, SSE4.2, PCLMULQDQ și AVX2 selectate pe runtime. Win32 folosește setul generic de obiecte zlib-ng pentru a rămâne compatibil cu lanțul de unelte OMF bcc32c, iar obiectele JPEG/TIFF pe Win32 rămân pe bcc32 clasic

Acoperire pentru regresii

Backend-ul Flate zlib-ng este acoperit de suitele automate de regresii Delphi și C++Builder. Setul curent de validare trece 20 de teste Delphi DUnitX pe Win32 și 20 pe Win64, plus 17 teste C++Builder GoogleTest pe Win32 și 17 pe Win64x. Reconstrucțiile de obiecte native trec de asemenea din ieșiri curate: 206 obiecte Win32, 205 obiecte Win64x și 206 obiecte MSVC Win64. Fluxurile acoperite includ comprimarea fluxurilor de pagină, comprimarea fonturilor încorporate și a CMap-ului ToUnicode, fluxurile de imagine Flate, plasarea standard a imaginilor JPEG, importul TIFF, ieșirea cu multe coduri de bare, scenariile copy / merge / edit și PDF-urile generate de demo

Niveluri de compresie

  • Z_NO_COMPRESSION (0): Fără compresie
  • Z_BEST_SPEED (1): Cea mai rapidă compresie
  • Z_BEST_COMPRESSION (9): Compresie maximă
  • Z_DEFAULT_COMPRESSION (-1): Echilibru implicit între viteză și compresie

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

See Also