|
Hỗ trợ nén ZLib nâng cao
Tổng quan
HotPDF hiện bao gồm hỗ trợ nén tương thích ZLib toàn diện, được hậu thuẫn bởi zlib-ng đi kèm cho các luồng PDF và nội dung
API tương thích zlib cung cấp nén deflate/inflate hiệu quả, được dùng rộng rãi trong tài liệu PDF cho
các thao tác bộ lọc FlateDecode và nén dữ liệu nói chung
Tính năng chính
- API tương thích ZLib được hậu thuẫn bởi zlib-ng đi kèm
- Hỗ trợ tham số FlateDecode của PDF
- Nén và giải nén theo luồng
- Xử lý lỗi và khả năng gỡ lỗi được cải thiện
- Xử lý tiết kiệm bộ nhớ cho các luồng dữ liệu lớn
- Cầu nối nén ảnh một lần Win64 cho an toàn ABI về độ dài của zlib-ng
- Phân phối SIMD lúc chạy của zlib-ng cho Win64x và Win64 MSVC chẩn đoán với các phương án dự phòng tổng quát
- Cầu nối cấp phát C căn chỉnh 32 byte cho các bộ đệm gốc thân thiện với SIMD
- Tích hợp thư viện C đa nền tảng
- Nhiều mức nén khác nhau (none, fastest, default, maximum)
Triển khai kỹ thuật
Phần hỗ trợ ZLib nâng cao được triển khai qua đơn vị HPDFZLib.pas, đơn vị này cung cấp:
- API ZLib đầy đủ: Cài đặt đầy đủ các hàm ZLib
- Các lớp luồng: TCustomZStream và các lớp dẫn xuất cho xử lý luồng
- Tích hợp PDF: TPDFFlateParms cho các tham số riêng của PDF
- Các hàm nâng cao: InflateStr, InflateStrParms cho cách dùng đơn giản hơn
Các bộ đối tượng zlib-ng 64-bit gồm các đường SSE2, SSSE3, SSE4.1, SSE4.2, PCLMULQDQ và AVX2 được chọn lúc chạy Win32 dùng bộ đối tượng zlib-ng tổng quát để giữ tương thích với chuỗi công cụ OMF bcc32c, còn các đối tượng JPEG/TIFF của Win32 vẫn ở bcc32 cổ điển
Phạm vi hồi quy
Phần phụ trợ Flate của zlib-ng được bao phủ bởi các bộ kiểm thử hồi quy Delphi và C++Builder tự động Bộ xác thực hiện tại chạy qua 20 test DUnitX Delphi trên Win32 và 20 test trên Win64, cộng thêm 17 test GoogleTest C++Builder trên Win32 và 17 test trên Win64x Việc xây dựng lại đối tượng gốc cũng đi qua từ đầu ra sạch: 206 đối tượng Win32, 205 đối tượng Win64x và 206 đối tượng MSVC Win64 Các luồng được bao phủ gồm nén luồng trang, nén phông nhúng và ToUnicode CMap, luồng ảnh Flate, đặt ảnh JPEG chuẩn, nhập TIFF, đầu ra nhiều barcode, các tình huống sao chép/hợp nhất/chỉnh sửa và các PDF do demo tạo ra
Các mức nén
- Z_NO_COMPRESSION (0): Không nén
- Z_BEST_SPEED (1): Nén nhanh nhất
- Z_BEST_COMPRESSION (9): Nén tối đa
- Z_DEFAULT_COMPRESSION (-1): Cân bằng mặc định giữa tốc độ và mức nén
Ví dụ sử dụng
Nén và giải nén chuỗi cơ bản
// 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;
Nén dựa trên luồng
// 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;
FlateDecode của PDF với tham số
// 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;
Thao tác ZLib mức thấp
// 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;
Tham số FlateDecode của PDF
Cấu trúc TPDFFlateParms
- Predictor: Hàm predictor (1=không dự đoán, 2=TIFF, 10-15=PNG)
- Colors: Số thành phần màu
- BitsPerComponent: Số bit trên mỗi thành phần màu
- Columns: Số mẫu trên mỗi hàng
- 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
|