From e928818ccb15d3bdbbd394478482246bc44e2451 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Mon, 4 Dec 2023 16:32:12 +0000 Subject: [PATCH] Scaffolding for JBIG2Globals --- cpdfcommand.ml | 16 +++++++++++++++- cpdfdrawcontrol.ml | 4 ++-- cpdfimage.ml | 8 ++++---- cpdfimage.mli | 8 ++++---- 4 files changed, 25 insertions(+), 11 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 5e23542..0ef5c9c 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -1787,9 +1787,11 @@ let set_input_image f s = with Sys_error _ -> error "Image file not found" +let jbig2_global = ref None + let set_input_png = set_input_image Cpdfimage.obj_of_png_data let set_input_jpeg = set_input_image Cpdfimage.obj_of_jpeg_data -let set_input_jbig2 = set_input_image (Cpdfimage.obj_of_jbig2_data ~global:1) +let set_input_jbig2 = set_input_image (Cpdfimage.obj_of_jbig2_data ?global:!jbig2_global) let embed_font_inner font = match font with @@ -1896,6 +1898,12 @@ let setlistimagesjson () = setop ListImages (); args.format_json <- true +let set_jbig2_global f = + jbig2_global := Some (Pdfio.bytes_of_string (contents_of_file f)) + +let clear_jbig2_global () = + jbig2_global := None + (* Parse a control file, make an argv, and then make Arg parse it. *) let rec make_control_argv_and_parse filename = control_args := !control_args @ parse_control_file filename @@ -1919,6 +1927,12 @@ and specs = ("-jbig2", Arg.String set_input_jbig2, " Load from a JBIG2 fragment, converting to PDF"); + ("-jbig2-global", + Arg.String set_jbig2_global, + " Load a JBIG2 global stream"); + ("-jbig2-global-clear", + Arg.Unit clear_jbig2_global, + " Forget any JBIG2 global stream"); ("-idir", Arg.String set_input_dir, " Add a directory of files"); diff --git a/cpdfdrawcontrol.ml b/cpdfdrawcontrol.ml index 71012df..e13c965 100644 --- a/cpdfdrawcontrol.ml +++ b/cpdfdrawcontrol.ml @@ -245,7 +245,7 @@ let addjpeg n = in try let data = Pdfio.bytes_of_string (contents_of_file filename) in - addop (Cpdfdraw.ImageXObject (name, Cpdfimage.obj_of_jpeg_data data)) + addop (Cpdfdraw.ImageXObject (name, fst (Cpdfimage.obj_of_jpeg_data data))) with _ -> error "addjpeg: could not load JPEG" @@ -256,7 +256,7 @@ let addpng n = | _ -> error "addpng: bad file specification" in let data = Pdfio.bytes_of_string (contents_of_file filename) in - addop (Cpdfdraw.ImageXObject (name, Cpdfimage.obj_of_png_data data)) + addop (Cpdfdraw.ImageXObject (name, fst (Cpdfimage.obj_of_png_data data))) let addimage s = addop (Cpdfdraw.Image s) diff --git a/cpdfimage.ml b/cpdfimage.ml index 0e05505..251516c 100644 --- a/cpdfimage.ml +++ b/cpdfimage.ml @@ -388,7 +388,7 @@ let obj_of_jpeg_data data = "/Width", Pdf.Integer w; "/Height", Pdf.Integer h] in - Pdf.Stream {contents = (Pdf.Dictionary d, Pdf.Got data)} + Pdf.Stream {contents = (Pdf.Dictionary d, Pdf.Got data)}, [] let obj_of_png_data data = let png = Cpdfpng.read_png (Pdfio.input_of_bytes data) in @@ -406,7 +406,7 @@ let obj_of_png_data data = "/Width", Pdf.Integer png.width; "/Height", Pdf.Integer png.height] in - Pdf.Stream {contents = (Pdf.Dictionary d, Pdf.Got png.idat)} + Pdf.Stream {contents = (Pdf.Dictionary d, Pdf.Got png.idat)}, [] let jbig2_dimensions data = (bget data 11 * 256 * 256 * 256 + bget data 12 * 256 * 256 + bget data 13 * 256 + bget data 14, @@ -424,12 +424,12 @@ let obj_of_jbig2_data ?global:int data = ("/Width", Pdf.Integer w); ("/Height", Pdf.Integer h)] in - Pdf.Stream {contents = (Pdf.Dictionary d, Pdf.Got data)} + Pdf.Stream {contents = (Pdf.Dictionary d, Pdf.Got data)}, [] let image_of_input fobj i = let pdf = Pdf.empty () in let data = Pdfio.bytes_of_input i 0 i.Pdfio.in_channel_length in - let obj = fobj data in + let obj, extras = fobj data in let w = match Pdf.lookup_direct pdf "/Width" obj with Some x -> Pdf.getnum pdf x | _ -> assert false in let h = match Pdf.lookup_direct pdf "/Height" obj with Some x -> Pdf.getnum pdf x | _ -> assert false in let page = diff --git a/cpdfimage.mli b/cpdfimage.mli index ef2c838..4c42d42 100644 --- a/cpdfimage.mli +++ b/cpdfimage.mli @@ -12,7 +12,7 @@ val image_resolution : Pdf.t -> int list -> float -> (int * string * int * int * val images : Pdf.t -> int list -> Cpdfyojson.Safe.t (**/**) -val image_of_input : (Pdfio.bytes -> Pdf.pdfobject) -> Pdfio.input -> Pdf.t -val obj_of_jpeg_data : Pdfio.bytes -> Pdf.pdfobject -val obj_of_png_data : Pdfio.bytes -> Pdf.pdfobject -val obj_of_jbig2_data : ?global:int -> Pdfio.bytes -> Pdf.pdfobject +val image_of_input : (Pdfio.bytes -> Pdf.pdfobject * (int * Pdf.pdfobject) list) -> Pdfio.input -> Pdf.t +val obj_of_jpeg_data : Pdfio.bytes -> Pdf.pdfobject * (int * Pdf.pdfobject) list +val obj_of_png_data : Pdfio.bytes -> Pdf.pdfobject * (int * Pdf.pdfobject) list +val obj_of_jbig2_data : ?global:Pdfio.bytes -> Pdfio.bytes -> Pdf.pdfobject * (int * Pdf.pdfobject) list