LZW 壓縮支援

總覽

HotPDF 現在包含完整的 LZW(Lempel-Ziv-Welch)壓縮支援,可用於 PDF 串流。LZW 是無損資料壓縮演算法,對文字與簡單圖形特別有效,常見於 PDF 文件的內容串流壓縮LZWDecode filter,並可反轉常與該 filter 搭配的 TIFF 或 PNG predictor

主要特色

  • 完整支援 PDF 串流的 LZW 解壓縮EarlyChange=1 and EarlyChange=0 code-width transitions
  • 可設定的 predictor,支援增強壓縮
  • Clear-code reset, end-of-information, and KwKwK expansion handling
  • Direct output to a TStream without constructing a second result string
  • 多種填充順序(由上而下、由下而上)
  • 支援 early code change 以相容各種來源
  • 省記憶體的串流處理
  • 整合 PDF 參數(Predictor、Colors、BitsPerComponent、Columns)

技術實作

  • TPDFLZWDecompressor: 主要解壓縮類別TPDFLZWDecompressor 是可重用的解碼類別
  • TPDFLZWCompressor 是對應的編碼類別
  • TPDFLZWParms: PDF 專屬設定的參數結構TPDFLZWParms 攜帶 PDF predictor 幾何
  • TPDFLZWFillOrder: 位元順序處理的列舉TPDFLZWFillOrder selects foTop or foBottom bit order
  • EarlyChange: 控制 early code 的碼長切換時機EHPDFLZWError 回報格式錯誤的輸入、不支援的幾何與超出設定上限的失敗

方法:

  • Decompress(Input: AnsiString): AnsiString - 基本解壓縮FillOrder 選擇壓縮位元組序
  • Decompress(Input: AnsiString; Parms: TPDFLZWParms): AnsiString - 帶 PDF 參數的解壓縮EarlyChange selects the PDF dictionary-width transition rule and defaults to True
  • InitialCodeSize accepts values from 2 through 9 and defaults to 9 for PDF
  • 縮減字母集時,每個未壓縮位元組都必須小於 1 shl (InitialCodeSize - 1);若有位元組超出該範圍,壓縮會在寫出輸出前丟出 EHPDFLZWError
  • MaxOutputBytes stops excessive expansion before the configured limit is crossed, with zero meaning unlimited
  • RequireInitialClear requires the first code to reset the dictionary
  • RequireEndOfInformation rejects a stream that ends without its terminator

使用範例

function Decompress(const Input: AnsiString): AnsiString; overload;
function Decompress(const Input: AnsiString;
  Parms: TPDFLZWParms): AnsiString; overload;
function TryDecompress(const Input: AnsiString;
  out Output: AnsiString): Boolean; overload;
function TryDecompress(const Input: AnsiString; Parms: TPDFLZWParms;
  out Output: AnsiString): Boolean; overload;
function DecompressToStream(const Input: AnsiString;
  OutputStream: TStream): Int64;
function DecompressStream(InputStream, OutputStream: TStream;
  RestoreInputPosition: Boolean = True): Int64;
  • Decompress raises EHPDFLZWError on invalid input
  • TryDecompress returns False and exposes the reason through LastError
  • DecompressToStream and DecompressStream return the number of decoded bytes written
  • DecompressStream reads from the current input position and restores that position by default when the stream is seekable

基本 LZW 解壓縮

TPDFLZWCompressor.CompressToStream 與 CompressStream 回傳寫出的編碼位元組數,空輸入也包含 clear code 與 end-of-information code;預設 PDF 組態對空輸入寫出 3 位元組


// Delphi example - Basic LZW decompression
procedure DecompressLZWData;
var
  Decompressor: TPDFLZWDecompressor;
  CompressedData: AnsiString;
  DecompressedData: AnsiString;
begin
  Decompressor := TPDFLZWDecompressor.Create;
  try
    // Configure decompressor
    Decompressor.FillOrder := foTop;
    Decompressor.EarlyChange := True;
    Decompressor.InitialCodeSize := 9;

    // Decompress data
    DecompressedData := Decompressor.Decompress(CompressedData);

    // Use decompressed data
    ProcessDecompressedData(DecompressedData);
  finally
    Decompressor.Free;
  end;
end;
        

帶 PDF 參數的進階 LZW 解壓縮


// Delphi example - Advanced LZW decompression with PDF parameters
procedure DecompressLZWWithParms;
var
  Decompressor: TPDFLZWDecompressor;
  CompressedData: AnsiString;
  DecompressedData: AnsiString;
  Parms: TPDFLZWParms;
begin
  Decompressor := TPDFLZWDecompressor.Create;
  try
    // Configure PDF parameters
    Parms.Predictor := 2;           // Horizontal differencing predictor
    Parms.Colors := 3;              // RGB color space
    Parms.BitsPerComponent := 8;    // 8 bits per component
    Parms.Columns := 100;           // Image width in pixels
    Parms.ExpandedTo8Bit := True;   // Expand to 8-bit components
    Parms.ColorSpace := 'DeviceRGB';

    // Decompress with parameters
    DecompressedData := Decompressor.Decompress(CompressedData, Parms);

    // Process the decompressed image data
    ProcessImageData(DecompressedData, Parms);
  finally
    Decompressor.Free;
  end;
end;
        

PDF 參數支援

  • Predictor: 預測器函式(0=無、2=水平差分)Predictor=1 原樣回傳解壓縮後的位元組
  • Colors: 顏色 component 數(1=灰階、3=RGB、4=CMYK)Predictor=2 反轉 TIFF 水平差分
  • BitsPerComponent: 每個顏色 component 的位元數(1、2、4、8、16)Predictor=10..15 反轉儲存在每列開頭的 PNG filter tag
  • Columns: 每列樣本數Colors, BitsPerComponent, and Columns define the exact row geometry
  • ExpandedTo8Bit: 是否擴展為 8 位元 componentExpandedTo8Bit and ColorSpace remain available for source compatibility

演算法細節

  • 字典式壓縮: 建立並維護動態字典CodesRead, ClearCodesRead, and DictionaryAdds
  • 可變碼長: 支援 9 到 12 位元的碼長KwKwKExpansions, PeakCodeSize, and PeakDictionarySize
  • 清除碼處理: 正確處理 clear 與 end-of-information 碼OutputBytes and EndedWithEndOfInformation

效能特性

  • 記憶體效率: 針對大型資料串流最佳化
  • 快速解壓縮: 高度最佳化的解壓縮演算法
  • Truncated input is rejected when RequireEndOfInformation is enabled
  • 效能可預期: 不同資料類型下表現一致
  • 記憶體佔用低: 處理過程的記憶體開銷極小

常見使用情境

  • PDF 內容串流解壓縮
  • PDF 檔案中的影像資料解壓縮
  • 文字串流解壓縮
  • 表單資料解壓縮
  • PostScript 串流解壓縮