Add backup JPEG dimensions code to -process-images

This commit is contained in:
John Whitington 2025-01-14 13:19:34 +08:00
parent 34bf294fac
commit 4a9fa711b7
2 changed files with 25 additions and 5 deletions

View File

@ -7,10 +7,12 @@ o -obj/-obj-json can follow a chain from an object number
o -obj/-obj-json can explore through arrays and name/number trees
o -print-dict-entry, -remove-dict-entry and -replace-dict-entry
can follow a chain from each found dictionary entry
o More of the PDF/UA Matterhorn verification suite implemented
Fixes:
o Harden auto-compression against malformed streams
o Add backup JPEG dimensions method in Cpdfimage
2.8 (December 2024)

View File

@ -563,6 +563,19 @@ let image_of_input ?subformat ?title ~process_struct_tree fobj i =
let pdf, pageroot = Pdfpage.add_pagetree [page] pdf in
Pdfpage.add_root pageroot [] pdf
let backup_jpeg_dimensions ~path_to_convert filename =
let tmp = Filename.temp_file "cpdf" "info" in
let command = Filename.quote_command path_to_convert ["-format"; "%[width] %[height]"; filename; "info:"] ^ " >" ^ tmp in
let out = Sys.command command in
if out > 0 then (Pdfe.log "unable to find JPEG dimensions"; (0, 0)) else
let w, h =
let w, rest = cleavewhile (neq ' ') (explode (contents_of_file tmp)) in
let h = tl rest in
int_of_string (implode w), int_of_string (implode h)
in
remove tmp;
(w, h)
let jpeg_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~jpeg_to_jpeg_scale ~interpolate ~q ~path_to_convert s dict reference =
if q < 0. || q > 100. then error "Out of range quality";
complain_convert path_to_convert;
@ -598,11 +611,16 @@ let jpeg_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~j
if newsize < size && perc_ok then
begin
let data = Pdfio.bytes_of_input_channel result in
let w, h = try Cpdfjpeg.jpeg_dimensions data with _ -> (w, h) in (* TODO. https://github.com/johnwhitington/cpdf-source/issues/349 *)
if !debug_image_processing then Printf.printf "JPEG to JPEG %i -> %i (%i%%)\n%!" size newsize (int_of_float (float newsize /. float size *. 100.));
reference :=
Pdf.add_dict_entry (Pdf.add_dict_entry (Pdf.add_dict_entry dict "/Length" (Pdf.Integer newsize)) "/Width" (Pdf.Integer w)) "/Height" (Pdf.Integer h),
Pdf.Got data
let w, h = try Cpdfjpeg.jpeg_dimensions data with e -> try close_in result; backup_jpeg_dimensions ~path_to_convert out2 with e -> (-1, -1) in
if (w, h) = (-1, -1) then
Printf.printf "Could not determine JPEG dimensions. Skipping.\n%!"
else
begin
if !debug_image_processing then Printf.printf "JPEG to JPEG %i -> %i (%i%%)\n%!" size newsize (int_of_float (float newsize /. float size *. 100.));
reference :=
Pdf.add_dict_entry (Pdf.add_dict_entry (Pdf.add_dict_entry dict "/Length" (Pdf.Integer newsize)) "/Width" (Pdf.Integer w)) "/Height" (Pdf.Integer h),
Pdf.Got data
end
end
else
begin