Skeleton for G4

This commit is contained in:
John Whitington 2025-01-11 07:22:59 +08:00
parent 89c5efc8cf
commit 3bad9aac44
2 changed files with 33 additions and 2 deletions

View File

@ -1,6 +1,6 @@
(* cpdf command line tools *)
let demo = false
let agpl = true
let agpl = false
let major_version = 2
let minor_version = 8
let minor_minor_version = 1

View File

@ -856,7 +856,38 @@ let recompress_1bpp_ccitt_lossless ~pixel_threshold ~length_threshold pdf s dict
if !debug_image_processing then Printf.printf "no size reduction\n%!"
end
let recompress_1bpp_ccittg4_lossless ~pixel_threshold ~length_threshold pdf s dict reference = ()
let recompress_1bpp_ccittg4_lossless ~pixel_threshold ~length_threshold pdf s dict reference =
let old = !reference in
let restore () = reference := old in
let w = match Pdf.lookup_direct pdf "/Width" dict with Some (Pdf.Integer i) -> i | _ -> error "bad width" in
let h = match Pdf.lookup_direct pdf "/Height" dict with Some (Pdf.Integer i) -> i | _ -> error "bad height" in
if w * h < pixel_threshold then (if !debug_image_processing then Printf.printf "pixel threshold not met\n%!") else (* (but also, jbig2enc fails on tiny images) *)
let size = match Pdf.lookup_direct pdf "/Length" dict with Some (Pdf.Integer i) -> i | _ -> 0 in
if size < length_threshold then (if !debug_image_processing then Printf.printf "length threshold not met\n%!") else
begin
Pdfcodec.decode_pdfstream_until_unknown pdf s;
match Pdf.lookup_direct pdf "/Filter" (fst !reference) with
| Some x ->
if !debug_image_processing then Printf.printf "could not decode - skipping %s length %i\n%!" (Pdfwrite.string_of_pdf x) size;
restore ()
| None ->
let data = match s with Pdf.Stream {contents = _, Pdf.Got d} -> d | _ -> assert false in
let compressed = Pdfcodec.encode_ccitt w data in
let newsize = bytes_size compressed in
if newsize < size then
begin
if !debug_image_processing then Printf.printf "1bpp to CCITT G4 %i -> %i (%i%%)\n%!" size newsize (int_of_float (float newsize /. float size *. 100.));
reference :=
(Pdf.add_dict_entry
(Pdf.add_dict_entry
(Pdf.add_dict_entry dict "/Length" (Pdf.Integer newsize))
"/Filter"
(Pdf.Name "/CCITTFaxDecode")) "/DecodeParms" (Pdf.Array [Pdf.Dictionary [("/K", Pdf.Integer ~-1); ("/Columns", Pdf.Integer w)]])),
Pdf.Got (compressed)
end
else
if !debug_image_processing then Printf.printf "no size reduction\n%!"
end
let recompress_1bpp_jbig2_lossless ~pixel_threshold ~length_threshold ~path_to_jbig2enc pdf s dict reference =
complain_jbig2enc path_to_jbig2enc;