From 8684ec30927a13fbbccec595f8bea4c41e60d6b6 Mon Sep 17 00:00:00 2001 From: John Whitington Date: Wed, 7 Feb 2024 14:42:38 +0000 Subject: [PATCH] Support for cpdflib getImageResolution --- cpdfcommand.ml | 13 +++++-------- cpdfimage.ml | 8 ++++++++ cpdfimage.mli | 2 ++ 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/cpdfcommand.ml b/cpdfcommand.ml index 08479ff..270c19e 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -4255,14 +4255,11 @@ let go () = Cpdfimage.extract_images ~raw:(args.encoding = Cpdfmetadata.Raw) ?path_to_p2p:(match args.path_to_p2p with "" -> None | x -> Some x) ?path_to_im:(match args.path_to_im with "" -> None | x -> Some x) args.encoding args.dedup args.dedup_per_page pdf range output_spec | Some (ImageResolution f) -> let pdf = get_single_pdf args.op true in - let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in - let images = Cpdfimage.image_resolution pdf range f in - if args.format_json then - flprint - (Cpdfyojson.Safe.pretty_to_string - (`List (map (fun (pagenum, xobject, w, h, wdpi, hdpi, objnum) -> - `Assoc [("Object", `Int objnum); ("Page", `Int pagenum); ("XObject", `String xobject); ("W", `Int w); ("H", `Int h); ("Xdpi", `Float wdpi); ("Ydpi", `Float hdpi)]) images))) - else + let range = parse_pagespec_allow_empty pdf (get_pagespec ()) in + if args.format_json then + flprint (Pdfio.string_of_bytes (Cpdfimage.image_resolution_json pdf range f)) + else + let images = Cpdfimage.image_resolution pdf range f in iter (function (pagenum, xobject, w, h, wdpi, hdpi, objnum) -> if wdpi < f || hdpi < f then diff --git a/cpdfimage.ml b/cpdfimage.ml index f16376e..75d43ff 100644 --- a/cpdfimage.ml +++ b/cpdfimage.ml @@ -324,6 +324,14 @@ let image_resolution pdf range dpi = image_resolution pdf range dpi; rev !image_results +let image_resolution_json pdf range dpi = + let images = image_resolution pdf range dpi in + Pdfio.bytes_of_string + (Cpdfyojson.Safe.pretty_to_string + (`List (map (fun (pagenum, xobject, w, h, wdpi, hdpi, objnum) -> + `Assoc [("Object", `Int objnum); ("Page", `Int pagenum); ("XObject", `String xobject); + ("W", `Int w); ("H", `Int h); ("Xdpi", `Float wdpi); ("Ydpi", `Float hdpi)]) images))) + (* All the images in file referenced at least once from the given range of pages. *) let images pdf range = let images = null_hash () in diff --git a/cpdfimage.mli b/cpdfimage.mli index 0e18936..85b32a6 100644 --- a/cpdfimage.mli +++ b/cpdfimage.mli @@ -11,6 +11,8 @@ val extract_images : (** Report image resolutions. *) val image_resolution : Pdf.t -> int list -> float -> (int * string * int * int * float * float * int) list +val image_resolution_json : Pdf.t -> int list -> float -> Pdfio.bytes + (** List images in JSON format *) val images : Pdf.t -> int list -> Cpdfyojson.Safe.t