From f729f5ea7819ffeee65f90da348f1f7423f85631 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Mon, 14 Aug 2023 16:15:11 +0100 Subject: [PATCH] Move functions into cpdfimage.ml for libcpdf --- cpdfcommand.ml | 65 +++++--------------------------------------------- cpdfimage.ml | 52 ++++++++++++++++++++++++++++++++++++++++ cpdfimage.mli | 5 ++++ 3 files changed, 63 insertions(+), 59 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 1bd13b2..e37af8c 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -1946,19 +1946,6 @@ let setmsheary s = let usexobj s = addop (Cpdfdraw.Use s) -let obj_of_jpeg_data data = - let w, h = Cpdfjpeg.jpeg_dimensions data in - let d = - ["/Length", Pdf.Integer (Pdfio.bytes_size data); - "/Filter", Pdf.Name "/DCTDecode"; - "/BitsPerComponent", Pdf.Integer 8; - "/ColorSpace", Pdf.Name "/DeviceRGB"; - "/Subtype", Pdf.Name "/Image"; - "/Width", Pdf.Integer w; - "/Height", Pdf.Integer h] - in - Pdf.Stream {contents = (Pdf.Dictionary d, Pdf.Got data)} - let addjpeg n = let name, filename = match String.split_on_char '=' n with @@ -1967,50 +1954,10 @@ let addjpeg n = in try let data = Pdfio.bytes_of_string (contents_of_file filename) in - addop (Cpdfdraw.ImageXObject (name, obj_of_jpeg_data data)) + addop (Cpdfdraw.ImageXObject (name, Cpdfimage.obj_of_jpeg_data data)) with _ -> error "addjpeg: could not load JPEG" -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 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 = - {Pdfpage.content = - [Pdfops.stream_of_ops - [Pdfops.Op_cm (Pdftransform.matrix_of_transform [Pdftransform.Translate (0., 0.); - Pdftransform.Scale ((0., 0.), w, h)]); - Pdfops.Op_Do "/I0"]]; - Pdfpage.mediabox = Pdf.Array [Pdf.Real 0.; Pdf.Real 0.; Pdf.Real w; Pdf.Real h]; - Pdfpage.resources = - Pdf.Dictionary - ["/XObject", Pdf.Dictionary ["/I0", Pdf.Indirect (Pdf.addobj pdf obj)]]; - Pdfpage.rotate = Pdfpage.Rotate0; - Pdfpage.rest = Pdf.Dictionary []} - in - let pdf, pageroot = Pdfpage.add_pagetree [page] pdf in - Pdfpage.add_root pageroot [] pdf - -let obj_of_png_data data = - let png = Cpdfpng.read_png (Pdfio.input_of_bytes data) in - let d = - ["/Length", Pdf.Integer (Pdfio.bytes_size png.idat); - "/Filter", Pdf.Name "/FlateDecode"; - "/Subtype", Pdf.Name "/Image"; - "/BitsPerComponent", Pdf.Integer 8; - "/ColorSpace", Pdf.Name "/DeviceRGB"; - "/DecodeParms", Pdf.Dictionary - ["/BitsPerComponent", Pdf.Integer 8; - "/Colors", Pdf.Integer 3; - "/Columns", Pdf.Integer png.width; - "/Predictor", Pdf.Integer 15]; - "/Width", Pdf.Integer png.width; - "/Height", Pdf.Integer png.height] - in - Pdf.Stream {contents = (Pdf.Dictionary d , Pdf.Got png.idat)} - let addpng n = let name, filename = match String.split_on_char '=' n with @@ -2018,12 +1965,12 @@ let addpng n = | _ -> error "addpng: bad file specification" in let data = bytes_of_string (contents_of_file filename) in - addop (Cpdfdraw.ImageXObject (name, obj_of_png_data data)) + addop (Cpdfdraw.ImageXObject (name, Cpdfimage.obj_of_png_data data)) let set_input_image f s = try let fh = open_in_bin s in - let pdf = image_of_input f (Pdfio.input_of_channel fh) in + let pdf = Cpdfimage.image_of_input f (Pdfio.input_of_channel fh) in begin try close_in fh with _ -> () end; args.original_filename <- s; args.create_objstm <- true; @@ -2031,8 +1978,8 @@ let set_input_image f s = with Sys_error _ -> error "Image file not found" -let set_input_png = set_input_image obj_of_png_data -let set_input_jpeg = set_input_image obj_of_jpeg_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 addimage s = addop (Cpdfdraw.Image s) @@ -2121,7 +2068,7 @@ let loadttf n = begin match args.op with Some Draw -> addop (Cpdfdraw.FontPack (fontname, embed_font_inner (EmbeddedFont name), null_hash ())) | _ -> () end with - _ -> error "addjpeg: could not load JPEG" + _ -> error "addtff: could not load TTF" let add_default_fontpack () = if not !fontpack_initialised then diff --git a/cpdfimage.ml b/cpdfimage.ml index 82ef279..2b329d8 100644 --- a/cpdfimage.ml +++ b/cpdfimage.ml @@ -271,3 +271,55 @@ let image_resolution pdf range dpi = image_resolution pdf range dpi; rev !image_results +let obj_of_jpeg_data data = + let w, h = Cpdfjpeg.jpeg_dimensions data in + let d = + ["/Length", Pdf.Integer (Pdfio.bytes_size data); + "/Filter", Pdf.Name "/DCTDecode"; + "/BitsPerComponent", Pdf.Integer 8; + "/ColorSpace", Pdf.Name "/DeviceRGB"; + "/Subtype", Pdf.Name "/Image"; + "/Width", Pdf.Integer w; + "/Height", Pdf.Integer h] + in + 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 + let d = + ["/Length", Pdf.Integer (Pdfio.bytes_size png.idat); + "/Filter", Pdf.Name "/FlateDecode"; + "/Subtype", Pdf.Name "/Image"; + "/BitsPerComponent", Pdf.Integer 8; + "/ColorSpace", Pdf.Name "/DeviceRGB"; + "/DecodeParms", Pdf.Dictionary + ["/BitsPerComponent", Pdf.Integer 8; + "/Colors", Pdf.Integer 3; + "/Columns", Pdf.Integer png.width; + "/Predictor", Pdf.Integer 15]; + "/Width", Pdf.Integer png.width; + "/Height", Pdf.Integer png.height] + in + Pdf.Stream {contents = (Pdf.Dictionary d , Pdf.Got png.idat)} + +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 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 = + {Pdfpage.content = + [Pdfops.stream_of_ops + [Pdfops.Op_cm (Pdftransform.matrix_of_transform [Pdftransform.Translate (0., 0.); + Pdftransform.Scale ((0., 0.), w, h)]); + Pdfops.Op_Do "/I0"]]; + Pdfpage.mediabox = Pdf.Array [Pdf.Real 0.; Pdf.Real 0.; Pdf.Real w; Pdf.Real h]; + Pdfpage.resources = + Pdf.Dictionary + ["/XObject", Pdf.Dictionary ["/I0", Pdf.Indirect (Pdf.addobj pdf obj)]]; + Pdfpage.rotate = Pdfpage.Rotate0; + Pdfpage.rest = Pdf.Dictionary []} + in + let pdf, pageroot = Pdfpage.add_pagetree [page] pdf in + Pdfpage.add_root pageroot [] pdf diff --git a/cpdfimage.mli b/cpdfimage.mli index e0cf226..3a70306 100644 --- a/cpdfimage.mli +++ b/cpdfimage.mli @@ -7,3 +7,8 @@ val extract_images : string -> (** Report image resolutions. *) val image_resolution : Pdf.t -> int list -> float -> (int * string * int * int * float * float) list + +(**/**) +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