From 67d1cb604ed14abdfcdf8ee5fb22731f3a097e7b Mon Sep 17 00:00:00 2001 From: John Whitington Date: Sun, 31 Dec 2023 11:59:48 +0000 Subject: [PATCH] Scaffolding for -lossless-resample --- cpdfcommand.ml | 35 +++++++++++++++++++++++++++-------- cpdfimage.ml | 12 ++++++++++-- cpdfimage.mli | 1 + 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index ba00437..1b3e8f9 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -528,7 +528,9 @@ type args = mutable onebppmethod : string; mutable pixel_threshold : int; mutable length_threshold : int; - mutable percentage_threshold : int} + mutable percentage_threshold : int; + mutable resample_factor : int; + mutable resample_interpolate : bool} let args = {op = None; @@ -657,8 +659,15 @@ let args = onebppmethod = ""; pixel_threshold = 25; length_threshold = 100; - percentage_threshold = 99} + percentage_threshold = 99; + resample_factor = 100; + resample_interpolate = false} +(* 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 +gs_malformed or gs_quiet or no-warn-rotate, since we want these to work +across ANDs. Or squeeze options: a little odd, but we want it to happen on +eventual output. Or -debug-force (from v2.6). *) let reset_arguments () = args.op <- None; args.preserve_objstm <- true; @@ -768,11 +777,8 @@ let reset_arguments () = args.pixel_threshold <- 25; args.length_threshold <- 100; args.percentage_threshold <- 99; - (* 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 - gs_malformed or gs_quiet or no-warn-rotate, since we want these to work - across ANDs. Or squeeze options: a little odd, but we want it to happen on - eventual output. Or -debug-force (from v2.6). *) + args.resample_factor <- 100; + args.resample_interpolate <- false; clear Cpdfdrawcontrol.fontpack_initialised (* Prefer a) the one given with -cpdflin b) a local cpdflin, c) otherwise assume @@ -1956,6 +1962,12 @@ let setlengththreshold i = let setpercentagethreshold i = args.percentage_threshold <- i +let setlosslessresample i = + args.resample_factor <- i + +let setresampleinterpolate b = + args.resample_interpolate <- b + let setprocessimagesinfo () = set Cpdfimage.debug_image_processing @@ -2760,6 +2772,12 @@ and specs = ("-percentage-threshold", Arg.Int setpercentagethreshold, " Only substitute lossy image when smaller than this"); + ("-lossless-resample", + Arg.Int setlosslessresample, + " Resample lossless images to given part of original"); + ("-resample-interpolate", + Arg.Bool setresampleinterpolate, + " Interpolate when resampling"); ("-squeeze", Arg.Unit setsqueeze, " Squeeze"); @@ -4499,7 +4517,8 @@ let go () = let pdf = get_single_pdf args.op false in Cpdfimage.process ~q:args.jpegquality ~qlossless:args.jpegqualitylossless ~onebppmethod:args.onebppmethod - ~length_threshold:args.length_threshold ~pixel_threshold:args.pixel_threshold ~percentage_threshold:args.percentage_threshold + ~length_threshold:args.length_threshold ~percentage_threshold:args.percentage_threshold ~pixel_threshold:args.pixel_threshold + ~factor:args.resample_factor ~interpolate:args.resample_interpolate ~path_to_jbig2enc:args.path_to_jbig2enc ~path_to_convert:args.path_to_convert pdf; write_pdf false pdf diff --git a/cpdfimage.ml b/cpdfimage.ml index 956a241..9e7b8d3 100644 --- a/cpdfimage.ml +++ b/cpdfimage.ml @@ -617,7 +617,7 @@ let lossless_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshol remove out; remove out2 -let lossless_resample pdf ~pixel_threshold ~length_threshold ~percentage_threshold~interpolate ~path_to_convert s dict reference = +let lossless_resample pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~factor ~interpolate ~path_to_convert s dict reference = match lossless_out pdf ~pixel_threshold ~length_threshold ".png" s dict reference with None -> () | Some (out, out2, size, components, w, h) -> let retcode = let command = @@ -702,7 +702,7 @@ let recompress_1bpp_jbig2_lossless ~pixel_threshold ~length_threshold ~path_to_j (* Lossless to JPEG: 8bpp Grey, 8bpp RGB, 8bpp CMYK including separation and ICCBased colourspaces *) (* 1 bit: anything to JBIG2 lossless (no globals) *) let process - ?q ?qlossless ?onebppmethod ~length_threshold ~percentage_threshold ~pixel_threshold + ?q ?qlossless ?onebppmethod ~length_threshold ~percentage_threshold ~pixel_threshold ~factor ~interpolate ~path_to_jbig2enc ~path_to_convert pdf = let nobjects = Pdf.objcard pdf in @@ -745,6 +745,14 @@ let process if !debug_image_processing then Printf.printf "(%i/%i) object %i (lossless)... %!" !ndone nobjects objnum; lossless_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~qlossless ~path_to_convert s dict reference end + else + begin + if factor < 100 then + begin + if !debug_image_processing then Printf.printf "(%i/%i) object %i (lossless)... %!" !ndone nobjects objnum; + lossless_resample pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~factor ~interpolate ~path_to_convert s dict reference + end + end | None -> () end | _ -> () (* not an image *) diff --git a/cpdfimage.mli b/cpdfimage.mli index 9b05ef8..913b990 100644 --- a/cpdfimage.mli +++ b/cpdfimage.mli @@ -17,6 +17,7 @@ val images : Pdf.t -> int list -> Cpdfyojson.Safe.t val process : ?q:int -> ?qlossless:int -> ?onebppmethod:string -> length_threshold:int -> percentage_threshold:int -> pixel_threshold:int -> + factor:int -> interpolate:bool -> path_to_jbig2enc:string -> path_to_convert:string -> Pdf.t -> unit (**/**)