Ready for 2.7beta

This commit is contained in:
John Whitington 2024-02-20 19:41:49 +00:00
parent 3d0a4676a5
commit 9f4de0e069
2 changed files with 59 additions and 42 deletions

View File

@ -3,7 +3,7 @@ let demo = false
let noncomp = false let noncomp = false
let major_version = 2 let major_version = 2
let minor_version = 7 let minor_version = 7
let version_date = "(13th February 2024, RC1)" let version_date = "(20th February 2024, Beta 1)"
open Pdfutil open Pdfutil
open Pdfio open Pdfio

View File

@ -4,8 +4,14 @@ open Cpdferror
let debug_image_processing = ref false let debug_image_processing = ref false
let complain_jbig2enc path =
if path = "" then error "Specify jbig2enc location with -jbig2enc"
let complain_convert path =
if path = "" then error "Specify convert location with -convert"
let remove x = let remove x =
try Printf.printf "%s\n" x; Sys.remove x with _ -> () try (*Printf.printf "%s\n" x;*) Sys.remove x with _ -> ()
let pnm_white ch = output_char ch ' ' let pnm_white ch = output_char ch ' '
let pnm_newline ch = output_char ch '\n' let pnm_newline ch = output_char ch '\n'
@ -500,6 +506,8 @@ let image_of_input fobj i =
Pdfpage.add_root pageroot [] pdf Pdfpage.add_root pageroot [] pdf
let jpeg_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~q ~path_to_convert s dict reference = let jpeg_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~q ~path_to_convert s dict reference =
if q < 0. || q > 100. then error "Out of range quality";
complain_convert path_to_convert;
let w = match Pdf.lookup_direct pdf "/Width" dict with Some (Pdf.Integer i) -> i | _ -> error "bad width" in let w = match Pdf.lookup_direct pdf "/Width" dict with Some (Pdf.Integer i) -> i | _ -> error "bad width" in
let h = match Pdf.lookup_direct pdf "/Height" dict with Some (Pdf.Integer i) -> i | _ -> error "bad height" in let h = match Pdf.lookup_direct pdf "/Height" dict with Some (Pdf.Integer i) -> i | _ -> error "bad height" in
if w * h < pixel_threshold then (if !debug_image_processing then Printf.printf "pixel threshold not met\n%!") else if w * h < pixel_threshold then (if !debug_image_processing then Printf.printf "pixel threshold not met\n%!") else
@ -513,12 +521,13 @@ let jpeg_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~q
close_out fh; close_out fh;
let retcode = let retcode =
let command = let command =
(Filename.quote_command path_to_convert [out; "-quality"; string_of_float q ^ "%"; out2]) (*FIXME check percentage as float here *) (Filename.quote_command path_to_convert [out; "-quality"; string_of_float q ^ "%"; out2])
in in
(*Printf.printf "%S\n" command;*) Sys.command command (*Printf.printf "%S\n" command;*) Sys.command command
in in
if retcode = 0 then if retcode = 0 then
begin begin
try
let result = open_in_bin out2 in let result = open_in_bin out2 in
let newsize = in_channel_length result in let newsize = in_channel_length result in
let perc_ok = float newsize /. float size < percentage_threshold /. 100. in let perc_ok = float newsize /. float size < percentage_threshold /. 100. in
@ -532,6 +541,9 @@ let jpeg_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~q
if !debug_image_processing then Printf.printf "no size reduction\n%!" if !debug_image_processing then Printf.printf "no size reduction\n%!"
end; end;
close_in result close_in result
with _ ->
remove out;
remove out2
end end
else else
begin Printf.printf "external process failed\n%!" end; begin Printf.printf "external process failed\n%!" end;
@ -580,19 +592,20 @@ let lossless_out pdf ~pixel_threshold ~length_threshold extension s dict referen
Some (out, out2, size, components, w, h) Some (out, out2, size, components, w, h)
end end
| colspace, bpc -> | colspace, bpc ->
let colspace = Pdf.lookup_direct pdf "/ColorSpace" dict in (*let colspace = Pdf.lookup_direct pdf "/ColorSpace" dict in
let colspace, bpc, filter = let colspace, bpc, filter =
(match colspace with None -> "none" | Some x -> Pdfwrite.string_of_pdf x), (match colspace with None -> "none" | Some x -> Pdfwrite.string_of_pdf x),
(match bpc with None -> "none" | Some x -> Pdfwrite.string_of_pdf x), (match bpc with None -> "none" | Some x -> Pdfwrite.string_of_pdf x),
(match Pdf.lookup_direct pdf "/Filter" dict with None -> "none" | Some x -> Pdfwrite.string_of_pdf x) (match Pdf.lookup_direct pdf "/Filter" dict with None -> "none" | Some x -> Pdfwrite.string_of_pdf x)
in in
print_string (Pdfwrite.string_of_pdf dict); print_string (Pdfwrite.string_of_pdf dict);
print_string (Printf.sprintf "%s (%s) [%s]\n" colspace bpc filter); print_string (Printf.sprintf "%s (%s) [%s]\n" colspace bpc filter);*)
if !debug_image_processing then Printf.printf "colourspace not suitable\n%!"; if !debug_image_processing then Printf.printf "colourspace not suitable\n%!";
restore (); restore ();
None (* an image we cannot or do not handle *) None (* an image we cannot or do not handle *)
let lossless_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~qlossless ~path_to_convert s dict reference = let lossless_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~qlossless ~path_to_convert s dict reference =
complain_convert path_to_convert;
match lossless_out pdf ~pixel_threshold ~length_threshold ".jpg" s dict reference with None -> () | Some (out, out2, size, components, w, h) -> match lossless_out pdf ~pixel_threshold ~length_threshold ".jpg" s dict reference with None -> () | Some (out, out2, size, components, w, h) ->
let retcode = let retcode =
let command = let command =
@ -606,6 +619,7 @@ let lossless_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshol
in in
if retcode = 0 then if retcode = 0 then
begin begin
try
let result = open_in_bin out2 in let result = open_in_bin out2 in
let newsize = in_channel_length result in let newsize = in_channel_length result in
let perc_ok = float newsize /. float size < percentage_threshold /. 100. in let perc_ok = float newsize /. float size < percentage_threshold /. 100. in
@ -624,6 +638,10 @@ let lossless_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshol
if !debug_image_processing then Printf.printf "no size reduction\n%!" if !debug_image_processing then Printf.printf "no size reduction\n%!"
end; end;
close_in result close_in result
with
_ ->
remove out;
remove out2
end; end;
remove out; remove out;
remove out2 remove out2
@ -637,9 +655,10 @@ let test_bpc pdf dict =
| _ -> 0 | _ -> 0
let lossless_resample pdf ~pixel_threshold ~length_threshold ~factor ~interpolate ~path_to_convert s dict reference = let lossless_resample pdf ~pixel_threshold ~length_threshold ~factor ~interpolate ~path_to_convert s dict reference =
(*Printf.printf "***lossless_resample IN dictionary: %S\n" (Pdfwrite.string_of_pdf dict); *) complain_convert path_to_convert;
let in_components = test_components pdf dict in let in_components = test_components pdf dict in
let in_bpc = test_bpc pdf dict in let in_bpc = test_bpc pdf dict in
(*Printf.printf "***lossless_resample IN dictionary: %S\n" (Pdfwrite.string_of_pdf dict); *)
(*Printf.printf "\n***IN components = %i, bpc = %i\n" in_components in_bpc;*) (*Printf.printf "\n***IN components = %i, bpc = %i\n" in_components in_bpc;*)
match lossless_out pdf ~pixel_threshold ~length_threshold ".png" s dict reference with match lossless_out pdf ~pixel_threshold ~length_threshold ".png" s dict reference with
| None -> () | None -> ()
@ -662,7 +681,6 @@ let lossless_resample pdf ~pixel_threshold ~length_threshold ~factor ~interpolat
let newsize = in_channel_length result in let newsize = in_channel_length result in
if newsize < size then if newsize < size then
begin begin
reference := reference :=
(match fst (obj_of_png_data (Pdfio.bytes_of_input_channel result)) with (match fst (obj_of_png_data (Pdfio.bytes_of_input_channel result)) with
| Pdf.Stream {contents = Pdf.Dictionary d, data} as s -> | Pdf.Stream {contents = Pdf.Dictionary d, data} as s ->
@ -700,7 +718,9 @@ let lossless_resample pdf ~pixel_threshold ~length_threshold ~factor ~interpolat
end; end;
remove out; remove out;
remove out2 remove out2
with _ -> () (* FIXME Remove *) with _ ->
remove out;
remove out2
let lossless_resample_target_dpi objnum pdf ~pixel_threshold ~length_threshold ~factor ~target_dpi_info ~interpolate ~path_to_convert s dict reference = let lossless_resample_target_dpi objnum pdf ~pixel_threshold ~length_threshold ~factor ~target_dpi_info ~interpolate ~path_to_convert s dict reference =
(*Printf.printf "lossless_resample_target_dpi\n";*) (*Printf.printf "lossless_resample_target_dpi\n";*)
@ -709,9 +729,6 @@ let lossless_resample_target_dpi objnum pdf ~pixel_threshold ~length_threshold ~
if real_factor < 100. then if real_factor < 100. then
lossless_resample pdf ~pixel_threshold ~length_threshold ~factor:real_factor ~interpolate ~path_to_convert s dict reference lossless_resample pdf ~pixel_threshold ~length_threshold ~factor:real_factor ~interpolate ~path_to_convert s dict reference
let complain_jbig2enc path =
if path = "" then error "Specify jbig2enc location with -jbig2enc"
let recompress_1bpp_jbig2_lossless ~pixel_threshold ~length_threshold ~path_to_jbig2enc pdf s dict reference = let recompress_1bpp_jbig2_lossless ~pixel_threshold ~length_threshold ~path_to_jbig2enc pdf s dict reference =
complain_jbig2enc path_to_jbig2enc; complain_jbig2enc path_to_jbig2enc;
let old = !reference in let old = !reference in