THotPDF.OptimizeLoadedImagesByVisualCost Method
Runs a bounded quality search for eligible loaded JPEG and JPEG 2000 images and adopts the lowest-cost candidate that satisfies both quality and savings limits
Declaration
function OptimizeLoadedImagesByVisualCost(
const Options: THPDFImageRecompressionCostOptions;
out AInfo: THPDFImageRecompressionCostInfo): Integer;
Cost model
Each encoded candidate is decoded and compared with the decoded source through sampled luma and chroma residuals on a normalised zero-to-100 perceptual-error scale
Candidates above MaximumPerceptualError or below MinimumSavingsBytes are rejected before scoring
score = VisualErrorWeight * sqr(error / MaximumPerceptualError)
+ ByteCostWeight * (candidateBytes / sourceBytes)
The lowest score wins, followed by smaller encoded size and then higher quality when scores tie, so repeated runs use the same output
THPDFImageRecompressionCostOptions
MinimumQuality, MaximumQuality and QualityStep define the bounded quality trials, and the maximum quality is always included even when the step does not divide the range
SampleStride controls deterministic pixel subsampling from 1 to 64, whilst the two non-negative weights let callers favour visual fidelity or compact output
THPDFImageRecompressionCostOptions.Default searches quality 30 to 90 in steps of 10, limits perceptual error to 3, requires one saved byte, samples every fourth pixel in each dimension and weights visual error and byte cost equally
THPDFImageRecompressionCostInfo
The result reports scanned and eligible image counts, JPEG and JPEG 2000 rewrites, encoding trials, visual and byte-cost rejections, selected quality totals, maximum accepted perceptual error, byte totals and DeterministicSelection status
Safety
Image masks, colour-key masks, spot colours, Lab images and non-RGB ICCBased sources remain unchanged, whilst convertible calibrated RGB sources retain an explicit calibrated output colour space
Cancellation is checked before each image and encoding trial, and no source stream is replaced unless a complete accepted candidate has been selected