Loaded Image Extraction
Loaded image extraction lets applications enumerate image XObjects after LoadFromFile or LoadFromStream opens an existing PDF, inspect their metadata, and decode supported images into TBitmap instances
Main APIs
GetLoadedImageCountreturns the number of enumerable image objects in the loaded documentGetLoadedImageInfofillsTHPDFLoadedImageInfowith object number, page index, size, bit depth, colour space, filter list, and decodability metadata without decoding pixelsExtractLoadedImagedecodes the selected image to a newTBitmap; the caller owns the returned bitmapGetLoadedImageCountPerPagescopes enumeration to one loaded page
Supported sources
- Data filters include
FlateDecode,LZWDecode,ASCIIHexDecode,ASCII85Decode,RunLengthDecode, and multi-filter chains that end in an image filter - Image filters include
DCTDecode,JPXDecode,JBIG2Decode, andCCITTFaxDecode - Raster paths include DeviceRGB, DeviceGray, Indexed, DeviceCMYK, and low-bit-depth greyscale or indexed images
Decode parameters and safety
DecodeParmsand its abbreviatedDPkey are resolved whether the value is a dictionary or the final dictionary in a filter-parameter arrayPredictor,Colors,BitsPerComponent, andColumnsare applied after Flate or LZW decompression- LZW
EarlyChange=0andEarlyChange=1are both honoured - DCT images follow ISO 32000-1 7.4.8: an Adobe APP14 transform inside the JPEG data takes precedence, otherwise
ColorTransform0 keeps RGB or CMYK samples and 1 selects YCbCr or YCCK, with defaults of 1 for three components and 0 for four, and anullvalue counts as absent; marker-free data without a JFIF header whose components are labelled R, G and B decodes untransformed unlessColorTransformsays otherwise; this applies to full-image and reduced-region decoding and to inline images in VCL page rendering, in process and in the codec worker, and an illegalColorTransformvalue makes the image undecodable; the shared resolution lives inHPDFNormalizeDCTColorTransform, which the VCL decode path consumes from the headless image-codec unit - TIFF Predictor 2 supports packed 1, 2, and 4-bit samples as well as 8 and 16-bit components
- PNG predictors require a valid filter tag for every complete row
- Raster dimensions and expected byte counts are checked for overflow before allocation or decoding
- Malformed compressed data is reported as a decode failure and is never treated as uncompressed pixels
Example
var
Doc: THotPDF;
Info: THPDFLoadedImageInfo;
Bmp: TBitmap;
I: Integer;
begin
Doc := THotPDF.Create(nil);
try
Doc.LoadFromFile('input.pdf');
for I := 0 to Doc.GetLoadedImageCount - 1 do
if Doc.GetLoadedImageInfo(I, Info) and Info.Decodable then
begin
Bmp := Doc.ExtractLoadedImage(I);
try
if Assigned(Bmp) then
Bmp.SaveToFile(Format('image-%d.bmp', [I]));
finally
Bmp.Free;
end;
end;
finally
Doc.Free;
end;
end;