Verbeterde ZLib-compressieondersteuning

Overzicht

HotPDF bevat nu uitgebreide ZLib-compatibele compressieondersteuning, ondersteund door gebundelde zlib-ng voor PDF-stromen en inhoud De zlib-compatibele API biedt efficiënte deflate/inflate-compressie die veel wordt gebruikt in PDF-documenten voor FlateDecode-filteroperaties en algemene datacompressie

Belangrijkste kenmerken

  • ZLib-compatibele API ondersteund door gebundelde zlib-ng
  • Ondersteuning voor PDF FlateDecode-parameters
  • Op streams gebaseerde compressie en decompressie
  • Verbeterde foutafhandeling en debugmogelijkheden
  • Geheugenefficiënte verwerking van grote datastromen
  • Win64-brug voor eenmalige beeldcompressie met zlib-ng lengte-ABI-veiligheid
  • Win64x- en diagnostische MSVC Win64 zlib-ng runtime SIMD-dispatch met generieke fallback
  • 32-byte uitgelijnde C-toewijzingsbrug voor SIMD-vriendelijke native buffers
  • Cross-platform C-libraryintegratie
  • Meerdere compressieniveaus (geen, snelst, standaard, maximaal)

Technische implementatie

De verbeterde ZLib-ondersteuning is geïmplementeerd via de HPDFZLib.pas-unit, die het volgende biedt:

  • Volledige ZLib-API: Volledige implementatie van ZLib-functies
  • Streamklassen: TCustomZStream en afgeleide klassen voor streamverwerking
  • PDF-integratie: TPDFFlateParms voor PDF-specifieke parameters
  • Uitgebreide functies: InflateStr, InflateStrParms voor eenvoudiger gebruik

De 64-bits zlib-ng-objectsets bevatten op runtime gekozen SSE2-, SSSE3-, SSE4.1-, SSE4.2-, PCLMULQDQ- en AVX2-paden. Win32 gebruikt de generieke zlib-ng-objectset om compatibel te blijven met de bcc32c OMF-toolchain, terwijl de Win32 JPEG/TIFF-objecten op klassieke bcc32 blijven

Regressiedekking

De zlib-ng Flate-backend wordt gedekt door de geautomatiseerde Delphi- en C++Builder-regressiesuites. De huidige validatieset haalt 20 Delphi DUnitX-tests op Win32 en 20 op Win64, plus 17 C++Builder GoogleTest-tests op Win32 en 17 op Win64x. De native object-herbuilds slagen ook vanaf schone outputs: 206 Win32-objecten, 205 Win64x-objecten en 206 MSVC Win64-objecten. De gedekte workflows omvatten paginastroomcompressie, compressie van ingebedde lettertypen en ToUnicode CMap, Flate-afbeeldingsstromen, standaard JPEG-afbeeldingsplaatsing, TIFF-import, uitvoer met veel barcodes, copy/merge/edit-scenario's en door demo's gegenereerde PDF's

Compressieniveaus

  • Z_NO_COMPRESSION (0): Geen compressie
  • Z_BEST_SPEED (1): Snelste compressie
  • Z_BEST_COMPRESSION (9): Maximale compressie
  • Z_DEFAULT_COMPRESSION (-1): Standaardbalans tussen snelheid en compressie

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

Zie ook