LZW 압축 지원

Overview

HotPDF는 LZWDecode 필터를 사용하는 레거시 PDF 스트림을 디코딩하며, 이 필터와 흔히 함께 쓰이는 TIFF 또는 PNG predictor를 복원할 수 있습니다

Key Features

  • PDF EarlyChange=1 and EarlyChange=0 code-width transitions
  • Most-significant-bit and least-significant-bit fill order
  • Clear-code reset, end-of-information, and KwKwK expansion handling
  • Direct output to a TStream without constructing a second result string
  • 구성 가능한 출력 제한, 그리고 선택적인 엄격한 initial-clear 및 end-of-information 검사
  • TIFF Predictor 2 at 1, 2, 4, 8, or 16 bits per component
  • PNG None, Sub, Up, Average, and Paeth row filters
  • 코드 폭, 사전 증가, 리셋, 확장, 출력 바이트에 대한 디코딩별 통계

Types

  • TPDFLZWDecompressor는 재사용 가능한 디코더 클래스입니다
  • TPDFLZWCompressor는 짝이 되는 인코더 클래스입니다
  • TPDFLZWParms carries PDF predictor geometry
  • TPDFLZWFillOrder selects foTop or foBottom bit order
  • EHPDFLZWError reports malformed input, unsupported geometry, and configured-limit failures

Configuration Properties

  • FillOrder selects the compressed bit order
  • EarlyChange는 PDF 사전 폭 전환 규칙을 선택하며 기본값은 True입니다
  • InitialCodeSize는 2~9 값을 받으며 PDF에서는 기본값이 9입니다
  • 축소된 알파벳에서는 압축하지 않은 모든 바이트가 1 shl (InitialCodeSize - 1)보다 작아야 하고, 그 범위를 벗어나는 바이트가 있으면 compression은 출력을 쓰기 전에 EHPDFLZWError를 발생시킵니다
  • MaxOutputBytes는 설정된 제한을 넘기 전에 과도한 확장을 중단시키며, 0은 무제한을 의미합니다
  • RequireInitialClear는 첫 번째 코드가 사전을 리셋하도록 요구합니다
  • RequireEndOfInformation은 종결 코드 없이 끝나는 스트림을 거부합니다

Methods

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는 False를 반환하고 그 이유를 LastError로 노출합니다
  • DecompressToStream과 DecompressStream은 기록한 디코딩 바이트 수를 반환합니다
  • DecompressStream은 현재 입력 위치에서 읽으며, 스트림이 seek 가능하면 기본적으로 그 위치를 복원합니다

Basic LZW Decompression

TPDFLZWCompressor.CompressToStream과 CompressStream은 기록한 인코딩 바이트 수를 반환하며, 빈 입력의 clear 코드와 end-of-information 코드도 포함됩니다. 기본 PDF 구성은 빈 입력에 3바이트를 기록합니다

uses
  HPDFLZW;

procedure DecompressLZWData;
var
  Decompressor: TPDFLZWDecompressor;
  CompressedData: AnsiString;
  DecompressedData: AnsiString;
begin
  Decompressor := TPDFLZWDecompressor.Create;
  try
    Decompressor.FillOrder := foTop;
    Decompressor.EarlyChange := True;
    Decompressor.MaxOutputBytes := 64 * 1024 * 1024;
    DecompressedData := Decompressor.Decompress(CompressedData);
    ProcessDecompressedData(DecompressedData);
  finally
    Decompressor.Free;
  end;
end;

Stream Output and Diagnostics

procedure DecodeLZWToStream(const CompressedData: AnsiString;
  Destination: TStream);
var
  Decompressor: TPDFLZWDecompressor;
begin
  Decompressor := TPDFLZWDecompressor.Create;
  try
    Decompressor.RequireInitialClear := True;
    Decompressor.RequireEndOfInformation := True;
    Decompressor.DecompressToStream(CompressedData, Destination);
    LogDecodeStatistics(
      Decompressor.CodesRead,
      Decompressor.PeakCodeSize,
      Decompressor.PeakDictionarySize,
      Decompressor.OutputBytes);
  finally
    Decompressor.Free;
  end;
end;

Predictor Parameters

  • Predictor=1 returns the decompressed bytes unchanged
  • Predictor=2 reverses TIFF horizontal differencing
  • Predictor=10..15는 각 행의 시작에 저장된 PNG 필터 태그를 복원합니다
  • Colors, BitsPerComponent, and Columns define the exact row geometry
  • ExpandedTo8Bit and ColorSpace remain available for source compatibility

Decode Statistics

  • CodesRead, ClearCodesRead, and DictionaryAdds
  • KwKwKExpansions, PeakCodeSize, and PeakDictionarySize
  • OutputBytes and EndedWithEndOfInformation

Error Handling

  • 다음 사전 항목을 넘어서는 코드는 거부됩니다
  • 끊어진 prefix 체인과 유효하지 않은 첫 번째 코드는 거부됩니다
  • RequireEndOfInformation이 활성화되어 있으면 잘린 입력은 거부됩니다
  • 출력 확장은 각 phrase를 출력하기 전에 검사합니다
  • Predictor 행은 완전해야 하며 유효한 지오메트리와 PNG 행 태그를 사용해야 합니다

See Also