diff --git a/cpdfcommand.ml b/cpdfcommand.ml index a9696dd..8580204 100644 --- a/cpdfcommand.ml +++ b/cpdfcommand.ml @@ -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 diff --git a/cpdfimage.ml b/cpdfimage.ml index e486e88..8a3ff9f 100644 --- a/cpdfimage.ml +++ b/cpdfimage.ml @@ -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;