Scaffolding for JBIG2Globals

This commit is contained in:
John Whitington 2023-12-04 16:32:12 +00:00
parent cd9512a4de
commit e928818ccb
4 changed files with 25 additions and 11 deletions

View File

@ -1787,9 +1787,11 @@ let set_input_image f s =
with with
Sys_error _ -> error "Image file not found" 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_png = set_input_image Cpdfimage.obj_of_png_data
let set_input_jpeg = set_input_image Cpdfimage.obj_of_jpeg_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 = let embed_font_inner font =
match font with match font with
@ -1896,6 +1898,12 @@ let setlistimagesjson () =
setop ListImages (); setop ListImages ();
args.format_json <- true 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. *) (* Parse a control file, make an argv, and then make Arg parse it. *)
let rec make_control_argv_and_parse filename = let rec make_control_argv_and_parse filename =
control_args := !control_args @ parse_control_file filename control_args := !control_args @ parse_control_file filename
@ -1919,6 +1927,12 @@ and specs =
("-jbig2", ("-jbig2",
Arg.String set_input_jbig2, Arg.String set_input_jbig2,
" Load from a JBIG2 fragment, converting to PDF"); " 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", ("-idir",
Arg.String set_input_dir, Arg.String set_input_dir,
" Add a directory of files"); " Add a directory of files");

View File

@ -245,7 +245,7 @@ let addjpeg n =
in in
try try
let data = Pdfio.bytes_of_string (contents_of_file filename) in 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 with
_ -> error "addjpeg: could not load JPEG" _ -> error "addjpeg: could not load JPEG"
@ -256,7 +256,7 @@ let addpng n =
| _ -> error "addpng: bad file specification" | _ -> error "addpng: bad file specification"
in in
let data = Pdfio.bytes_of_string (contents_of_file filename) 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 = let addimage s =
addop (Cpdfdraw.Image s) addop (Cpdfdraw.Image s)

View File

@ -388,7 +388,7 @@ let obj_of_jpeg_data data =
"/Width", Pdf.Integer w; "/Width", Pdf.Integer w;
"/Height", Pdf.Integer h] "/Height", Pdf.Integer h]
in 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 obj_of_png_data data =
let png = Cpdfpng.read_png (Pdfio.input_of_bytes data) in 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; "/Width", Pdf.Integer png.width;
"/Height", Pdf.Integer png.height] "/Height", Pdf.Integer png.height]
in 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 = let jbig2_dimensions data =
(bget data 11 * 256 * 256 * 256 + bget data 12 * 256 * 256 + bget data 13 * 256 + bget data 14, (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); ("/Width", Pdf.Integer w);
("/Height", Pdf.Integer h)] ("/Height", Pdf.Integer h)]
in 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 image_of_input fobj i =
let pdf = Pdf.empty () in let pdf = Pdf.empty () in
let data = Pdfio.bytes_of_input i 0 i.Pdfio.in_channel_length 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 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 h = match Pdf.lookup_direct pdf "/Height" obj with Some x -> Pdf.getnum pdf x | _ -> assert false in
let page = let page =

View File

@ -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 images : Pdf.t -> int list -> Cpdfyojson.Safe.t
(**/**) (**/**)
val image_of_input : (Pdfio.bytes -> Pdf.pdfobject) -> Pdfio.input -> Pdf.t 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 val obj_of_jpeg_data : Pdfio.bytes -> Pdf.pdfobject * (int * Pdf.pdfobject) list
val obj_of_png_data : Pdfio.bytes -> Pdf.pdfobject val obj_of_png_data : Pdfio.bytes -> Pdf.pdfobject * (int * Pdf.pdfobject) list
val obj_of_jbig2_data : ?global:int -> Pdfio.bytes -> Pdf.pdfobject val obj_of_jbig2_data : ?global:Pdfio.bytes -> Pdfio.bytes -> Pdf.pdfobject * (int * Pdf.pdfobject) list