From 0234445fc614eedcc31b9945d75d5432ff306ce9 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Fri, 12 Jan 2024 15:00:28 +0000 Subject: [PATCH] Lossy JBIG2 reprocesing finished --- cpdfcommand.ml | 15 ++++++++++++--- cpdfimage.ml | 10 +++++----- cpdfimage.mli | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 807b4ef..b857737 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -532,7 +532,8 @@ type args = mutable dpi_threshold : int; mutable dpi_target : int; mutable resample_factor : int; - mutable resample_interpolate : bool} + mutable resample_interpolate : bool; + mutable jbig2_lossy_threshold : float} let args = {op = None; @@ -665,7 +666,8 @@ let args = dpi_threshold = 0; dpi_target = 0; resample_factor = 101; - resample_interpolate = false} + resample_interpolate = false; + jbig2_lossy_threshold = 0.85} (* Do not reset original_filename or cpdflin or was_encrypted or was_decrypted_with_owner or recrypt or producer or creator or path_to_* or @@ -785,6 +787,7 @@ let reset_arguments () = args.dpi_target <- 0; args.resample_factor <- 101; args.resample_interpolate <- false; + args.jbig2_lossy_threshold <- 0.85; clear Cpdfdrawcontrol.fontpack_initialised (* Prefer a) the one given with -cpdflin b) a local cpdflin, c) otherwise assume @@ -1980,6 +1983,9 @@ let setlosslessresample i = let setresampleinterpolate () = args.resample_interpolate <- true +let setjbig2_lossy_threshold f = + args.jbig2_lossy_threshold <- f + let setprocessimagesinfo () = set Cpdfimage.debug_image_processing @@ -2775,6 +2781,9 @@ and specs = ("-1bpp-method", Arg.String set1bppmethod, " Set 1bpp compression method for existing images"); + ("-jbig2-lossy-threshold", + Arg.Float setjbig2_lossy_threshold, + " Set jbig2enc lossy threshold"); ("-pixel-threshold", Arg.Int setpixelthreshold, " Only process images with more pixels than this"); @@ -4533,7 +4542,7 @@ let go () = let pdf = get_single_pdf args.op false in let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in Cpdfimage.process - ~q:args.jpegquality ~qlossless:args.jpegqualitylossless ~onebppmethod:args.onebppmethod + ~q:args.jpegquality ~qlossless:args.jpegqualitylossless ~onebppmethod:args.onebppmethod ~jbig2_lossy_threshold:args.jbig2_lossy_threshold ~length_threshold:args.length_threshold ~percentage_threshold:args.percentage_threshold ~pixel_threshold:args.pixel_threshold ~dpi_threshold:args.dpi_threshold ~dpi_target:args.dpi_target ~factor:args.resample_factor ~interpolate:args.resample_interpolate ~path_to_jbig2enc:args.path_to_jbig2enc ~path_to_convert:args.path_to_convert range pdf; diff --git a/cpdfimage.ml b/cpdfimage.ml index 5a56986..cba0d7d 100644 --- a/cpdfimage.ml +++ b/cpdfimage.ml @@ -682,7 +682,7 @@ let recompress_1bpp_jbig2_lossless ~pixel_threshold ~length_threshold ~path_to_j pnm_to_channel_1_inverted fh w h data; close_out fh; let retcode = - let command = Filename.quote_command ~stdout:out2 path_to_jbig2enc ["-p"; out] in + let command = Filename.quote_command ~stdout:out2 path_to_jbig2enc ["-d"; "-p"; out] in (*Printf.printf "%S\n" command;*) Sys.command command in if retcode = 0 then @@ -711,7 +711,7 @@ let recompress_1bpp_jbig2_lossless ~pixel_threshold ~length_threshold ~path_to_j end (* Recompress 1bpp images (except existing JBIG2 compressed ones) to lossy jbig2 *) -let preprocess_jbig2_lossy ~path_to_jbig2enc ~length_threshold ~pixel_threshold ~dpi_threshold inrange highdpi pdf = +let preprocess_jbig2_lossy ~path_to_jbig2enc ~jbig2_lossy_threshold ~length_threshold ~pixel_threshold ~dpi_threshold inrange highdpi pdf = let objnum_name_pairs = ref [] in let process_obj objnum s = match s with @@ -758,7 +758,7 @@ let preprocess_jbig2_lossy ~path_to_jbig2enc ~length_threshold ~pixel_threshold Filename.quote_command path_to_jbig2enc ?stderr:(if !debug_image_processing then None else Some Filename.null) - (["-p"; "-s"; "-b"; jbig2out] @ map snd !objnum_name_pairs) + (["-p"; "-s"; "-d"; "-t"; string_of_float jbig2_lossy_threshold; "-b"; jbig2out] @ map snd !objnum_name_pairs) in (*Printf.printf "%S\n" command;*) Sys.command command in @@ -799,7 +799,7 @@ let preprocess_jbig2_lossy ~path_to_jbig2enc ~length_threshold ~pixel_threshold remove (jbig2out ^ ".sym") let process - ?q ?qlossless ?onebppmethod ~length_threshold ~percentage_threshold ~pixel_threshold ~dpi_threshold + ?q ?qlossless ?onebppmethod ~jbig2_lossy_threshold ~length_threshold ~percentage_threshold ~pixel_threshold ~dpi_threshold ~dpi_target ~factor ~interpolate ~path_to_jbig2enc ~path_to_convert range pdf = let inrange = @@ -822,7 +822,7 @@ let process in hashset_of_list objnums in - begin match onebppmethod with Some "JBIG2Lossy" -> preprocess_jbig2_lossy ~path_to_jbig2enc ~dpi_threshold ~length_threshold ~pixel_threshold inrange highdpi pdf | _ -> () end; + begin match onebppmethod with Some "JBIG2Lossy" -> preprocess_jbig2_lossy ~path_to_jbig2enc ~jbig2_lossy_threshold ~dpi_threshold ~length_threshold ~pixel_threshold inrange highdpi pdf | _ -> () end; let nobjects = Pdf.objcard pdf in let ndone = ref 0 in let process_obj objnum s = diff --git a/cpdfimage.mli b/cpdfimage.mli index e0316bc..07afccd 100644 --- a/cpdfimage.mli +++ b/cpdfimage.mli @@ -16,7 +16,7 @@ val images : Pdf.t -> int list -> Cpdfyojson.Safe.t (** Reprocess images *) val process : - ?q:int -> ?qlossless:int -> ?onebppmethod:string -> + ?q:int -> ?qlossless:int -> ?onebppmethod:string -> jbig2_lossy_threshold:float -> length_threshold:int -> percentage_threshold:int -> pixel_threshold:int -> dpi_threshold:int -> dpi_target:int -> factor:int -> interpolate:bool -> path_to_jbig2enc:string -> path_to_convert:string -> int list -> Pdf.t -> unit