From c0c72b8ec4813833a476df69a3d7182224f6ab28 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Fri, 22 Dec 2023 19:33:10 +0000 Subject: [PATCH] Plumbing for path_to_jbig2enc --- cpdfcommand.ml | 18 +++++++++++++----- cpdfimage.ml | 12 +++++++++--- cpdfimage.mli | 2 +- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index cd5e0bd..dd47c07 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -469,6 +469,7 @@ type args = mutable path_to_im : string; mutable path_to_p2p : string; mutable path_to_convert : string; + mutable path_to_jbig2enc : string; mutable frombox : string option; mutable tobox : string option; mutable mediabox_if_missing : bool; @@ -589,10 +590,11 @@ let args = dashrange = "all"; outline = false; linewidth = 1.0; - path_to_ghostscript = ""; - path_to_im = ""; - path_to_p2p = ""; - path_to_convert = ""; + path_to_ghostscript = "gs"; + path_to_im = "magick"; + path_to_p2p = "pnm2png"; + path_to_convert = "convert"; + path_to_jbig2enc = "jbig2enc"; frombox = None; tobox = None; mediabox_if_missing = false; @@ -1528,6 +1530,9 @@ let setimpath p = let setconvertpath p = args.path_to_convert <- p +let setjbig2encpath p = + args.path_to_jbig2enc <- p + let setp2ppath p = args.path_to_p2p <- p @@ -2710,6 +2715,9 @@ and specs = ("-convert", Arg.String setconvertpath, " Path to convert executable"); + ("-jbig2enc", + Arg.String setjbig2encpath, + " Path to jbig2enc executable"); ("-jpeg-to-jpeg", Arg.Int setjpegquality, " Set JPEG quality for existing JPEGs"); @@ -4456,7 +4464,7 @@ let go () = write_pdf false (Cpdfchop.chop ~x ~y ~columns:args.impose_columns ~btt:args.impose_btt ~rtl:args.impose_rtl pdf range) | Some ProcessImages -> let pdf = get_single_pdf args.op false in - Cpdfimage.process pdf ~q:args.jpegquality ~qlossless:args.jpegqualitylossless ~onebppmethod:args.onebppmethod ~path_to_convert:args.path_to_convert; + Cpdfimage.process pdf ~q:args.jpegquality ~qlossless:args.jpegqualitylossless ~onebppmethod:args.onebppmethod ~path_to_jbig2enc:args.path_to_jbig2enc ~path_to_convert:args.path_to_convert; write_pdf false pdf (* Advise the user if a combination of command line flags makes little sense, diff --git a/cpdfimage.ml b/cpdfimage.ml index 653ee4d..45a6d94 100644 --- a/cpdfimage.ml +++ b/cpdfimage.ml @@ -492,6 +492,7 @@ let image_of_input fobj i = (* FIXME if lossy only 5% smaller, ignore? Set this parameter... *) (* FIXME error handling for Sys.remove, others *) (* FIXME Use raw format for all, and make it fast *) +(* FIXME combine path_to_im / path_to_convert *) let jpeg_to_jpeg pdf ~q ~path_to_convert s dict reference = Pdf.getstream s; let out = Filename.temp_file "cpdf" "convertin" ^ ".jpg" in @@ -590,13 +591,18 @@ let lossless_to_jpeg pdf ~qlossless ~path_to_convert s dict reference = print_string (Printf.sprintf "%s (%s) [%s]\n" colspace bpc filter); () (* an image we cannot or do not handle *) -let recompress_1bpp_jbig2_lossless pdf s dict reference = +let recompress_1bpp_jbig2_lossless ~path_to_jbig2enc pdf s dict reference = + (* 1. Decode it *) + (* 2. Check we are ok *) + (* 3. Call jbig2enc *) + (* 4. Read in result *) + (* 5. Set data and dictionary *) () (* JPEG to JPEG: RGB and CMYK JPEGS *) (* Lossless to JPEG: 8bpp Grey, 8bpp RGB, 8bpp CMYK including separation add ICCBased colourspaces *) (* 1 bit: anything to JBIG2 lossless (no globals) *) -let process ?q ?qlossless ?onebppmethod pdf ~path_to_convert = +let process ?q ?qlossless ?onebppmethod pdf ~path_to_jbig2enc ~path_to_convert = let process_obj _ s = match s with | Pdf.Stream ({contents = dict, _} as reference) -> @@ -614,7 +620,7 @@ let process ?q ?qlossless ?onebppmethod pdf ~path_to_convert = | Some (Pdf.Name "/Image"), _, Some (Pdf.Integer 1), _ | Some (Pdf.Name "/Image"), _, _, Some (Pdf.Boolean true) -> begin match onebppmethod with - | Some "JBIG2" -> recompress_1bpp_jbig2_lossless pdf s dict reference + | Some "JBIG2" -> recompress_1bpp_jbig2_lossless ~path_to_jbig2enc pdf s dict reference | _ -> () end | Some (Pdf.Name "/Image"), _, _, _ -> diff --git a/cpdfimage.mli b/cpdfimage.mli index 5542d32..7cc6c7a 100644 --- a/cpdfimage.mli +++ b/cpdfimage.mli @@ -11,7 +11,7 @@ val image_resolution : Pdf.t -> int list -> float -> (int * string * int * int * (** List images in JSON format *) val images : Pdf.t -> int list -> Cpdfyojson.Safe.t -val process : ?q:int -> ?qlossless:int -> ?onebppmethod:string -> Pdf.t -> path_to_convert:string -> unit +val process : ?q:int -> ?qlossless:int -> ?onebppmethod:string -> Pdf.t -> path_to_jbig2enc:string -> path_to_convert:string -> unit (**/**) val image_of_input : (unit -> Pdfio.bytes -> Pdf.pdfobject * (int * Pdf.pdfobject) list) -> Pdfio.input -> Pdf.t