Ready for 2.7beta
This commit is contained in:
parent
3d0a4676a5
commit
9f4de0e069
|
@ -3,7 +3,7 @@ let demo = false
|
||||||
let noncomp = false
|
let noncomp = false
|
||||||
let major_version = 2
|
let major_version = 2
|
||||||
let minor_version = 7
|
let minor_version = 7
|
||||||
let version_date = "(13th February 2024, RC1)"
|
let version_date = "(20th February 2024, Beta 1)"
|
||||||
|
|
||||||
open Pdfutil
|
open Pdfutil
|
||||||
open Pdfio
|
open Pdfio
|
||||||
|
|
99
cpdfimage.ml
99
cpdfimage.ml
|
@ -4,8 +4,14 @@ open Cpdferror
|
||||||
|
|
||||||
let debug_image_processing = ref false
|
let debug_image_processing = ref false
|
||||||
|
|
||||||
|
let complain_jbig2enc path =
|
||||||
|
if path = "" then error "Specify jbig2enc location with -jbig2enc"
|
||||||
|
|
||||||
|
let complain_convert path =
|
||||||
|
if path = "" then error "Specify convert location with -convert"
|
||||||
|
|
||||||
let remove x =
|
let remove x =
|
||||||
try Printf.printf "%s\n" x; Sys.remove x with _ -> ()
|
try (*Printf.printf "%s\n" x;*) Sys.remove x with _ -> ()
|
||||||
|
|
||||||
let pnm_white ch = output_char ch ' '
|
let pnm_white ch = output_char ch ' '
|
||||||
let pnm_newline ch = output_char ch '\n'
|
let pnm_newline ch = output_char ch '\n'
|
||||||
|
@ -500,6 +506,8 @@ let image_of_input fobj i =
|
||||||
Pdfpage.add_root pageroot [] pdf
|
Pdfpage.add_root pageroot [] pdf
|
||||||
|
|
||||||
let jpeg_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~q ~path_to_convert s dict reference =
|
let jpeg_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~q ~path_to_convert s dict reference =
|
||||||
|
if q < 0. || q > 100. then error "Out of range quality";
|
||||||
|
complain_convert path_to_convert;
|
||||||
let w = match Pdf.lookup_direct pdf "/Width" dict with Some (Pdf.Integer i) -> i | _ -> error "bad width" 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
|
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
|
if w * h < pixel_threshold then (if !debug_image_processing then Printf.printf "pixel threshold not met\n%!") else
|
||||||
|
@ -513,25 +521,29 @@ let jpeg_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~q
|
||||||
close_out fh;
|
close_out fh;
|
||||||
let retcode =
|
let retcode =
|
||||||
let command =
|
let command =
|
||||||
(Filename.quote_command path_to_convert [out; "-quality"; string_of_float q ^ "%"; out2]) (*FIXME check percentage as float here *)
|
(Filename.quote_command path_to_convert [out; "-quality"; string_of_float q ^ "%"; out2])
|
||||||
in
|
in
|
||||||
(*Printf.printf "%S\n" command;*) Sys.command command
|
(*Printf.printf "%S\n" command;*) Sys.command command
|
||||||
in
|
in
|
||||||
if retcode = 0 then
|
if retcode = 0 then
|
||||||
begin
|
begin
|
||||||
let result = open_in_bin out2 in
|
try
|
||||||
let newsize = in_channel_length result in
|
let result = open_in_bin out2 in
|
||||||
let perc_ok = float newsize /. float size < percentage_threshold /. 100. in
|
let newsize = in_channel_length result in
|
||||||
if newsize < size && perc_ok then
|
let perc_ok = float newsize /. float size < percentage_threshold /. 100. in
|
||||||
begin
|
if newsize < size && perc_ok then
|
||||||
if !debug_image_processing then Printf.printf "JPEG to JPEG %i -> %i (%i%%)\n%!" size newsize (int_of_float (float newsize /. float size *. 100.));
|
begin
|
||||||
reference := Pdf.add_dict_entry dict "/Length" (Pdf.Integer newsize), Pdf.Got (Pdfio.bytes_of_input_channel result)
|
if !debug_image_processing then Printf.printf "JPEG to JPEG %i -> %i (%i%%)\n%!" size newsize (int_of_float (float newsize /. float size *. 100.));
|
||||||
end
|
reference := Pdf.add_dict_entry dict "/Length" (Pdf.Integer newsize), Pdf.Got (Pdfio.bytes_of_input_channel result)
|
||||||
else
|
end
|
||||||
begin
|
else
|
||||||
if !debug_image_processing then Printf.printf "no size reduction\n%!"
|
begin
|
||||||
end;
|
if !debug_image_processing then Printf.printf "no size reduction\n%!"
|
||||||
close_in result
|
end;
|
||||||
|
close_in result
|
||||||
|
with _ ->
|
||||||
|
remove out;
|
||||||
|
remove out2
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
begin Printf.printf "external process failed\n%!" end;
|
begin Printf.printf "external process failed\n%!" end;
|
||||||
|
@ -580,19 +592,20 @@ let lossless_out pdf ~pixel_threshold ~length_threshold extension s dict referen
|
||||||
Some (out, out2, size, components, w, h)
|
Some (out, out2, size, components, w, h)
|
||||||
end
|
end
|
||||||
| colspace, bpc ->
|
| colspace, bpc ->
|
||||||
let colspace = Pdf.lookup_direct pdf "/ColorSpace" dict in
|
(*let colspace = Pdf.lookup_direct pdf "/ColorSpace" dict in
|
||||||
let colspace, bpc, filter =
|
let colspace, bpc, filter =
|
||||||
(match colspace with None -> "none" | Some x -> Pdfwrite.string_of_pdf x),
|
(match colspace with None -> "none" | Some x -> Pdfwrite.string_of_pdf x),
|
||||||
(match bpc with None -> "none" | Some x -> Pdfwrite.string_of_pdf x),
|
(match bpc with None -> "none" | Some x -> Pdfwrite.string_of_pdf x),
|
||||||
(match Pdf.lookup_direct pdf "/Filter" dict with None -> "none" | Some x -> Pdfwrite.string_of_pdf x)
|
(match Pdf.lookup_direct pdf "/Filter" dict with None -> "none" | Some x -> Pdfwrite.string_of_pdf x)
|
||||||
in
|
in
|
||||||
print_string (Pdfwrite.string_of_pdf dict);
|
print_string (Pdfwrite.string_of_pdf dict);
|
||||||
print_string (Printf.sprintf "%s (%s) [%s]\n" colspace bpc filter);
|
print_string (Printf.sprintf "%s (%s) [%s]\n" colspace bpc filter);*)
|
||||||
if !debug_image_processing then Printf.printf "colourspace not suitable\n%!";
|
if !debug_image_processing then Printf.printf "colourspace not suitable\n%!";
|
||||||
restore ();
|
restore ();
|
||||||
None (* an image we cannot or do not handle *)
|
None (* an image we cannot or do not handle *)
|
||||||
|
|
||||||
let lossless_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~qlossless ~path_to_convert s dict reference =
|
let lossless_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshold ~qlossless ~path_to_convert s dict reference =
|
||||||
|
complain_convert path_to_convert;
|
||||||
match lossless_out pdf ~pixel_threshold ~length_threshold ".jpg" s dict reference with None -> () | Some (out, out2, size, components, w, h) ->
|
match lossless_out pdf ~pixel_threshold ~length_threshold ".jpg" s dict reference with None -> () | Some (out, out2, size, components, w, h) ->
|
||||||
let retcode =
|
let retcode =
|
||||||
let command =
|
let command =
|
||||||
|
@ -606,24 +619,29 @@ let lossless_to_jpeg pdf ~pixel_threshold ~length_threshold ~percentage_threshol
|
||||||
in
|
in
|
||||||
if retcode = 0 then
|
if retcode = 0 then
|
||||||
begin
|
begin
|
||||||
let result = open_in_bin out2 in
|
try
|
||||||
let newsize = in_channel_length result in
|
let result = open_in_bin out2 in
|
||||||
let perc_ok = float newsize /. float size < percentage_threshold /. 100. in
|
let newsize = in_channel_length result in
|
||||||
if newsize < size && perc_ok then
|
let perc_ok = float newsize /. float size < percentage_threshold /. 100. in
|
||||||
begin
|
if newsize < size && perc_ok then
|
||||||
if !debug_image_processing then Printf.printf "lossless to JPEG %i -> %i (%i%%)\n%!" size newsize (int_of_float (float newsize /. float size *. 100.));
|
begin
|
||||||
reference :=
|
if !debug_image_processing then Printf.printf "lossless to JPEG %i -> %i (%i%%)\n%!" size newsize (int_of_float (float newsize /. float size *. 100.));
|
||||||
(Pdf.add_dict_entry
|
reference :=
|
||||||
(Pdf.add_dict_entry dict "/Length" (Pdf.Integer newsize))
|
(Pdf.add_dict_entry
|
||||||
"/Filter"
|
(Pdf.add_dict_entry dict "/Length" (Pdf.Integer newsize))
|
||||||
(Pdf.Name "/DCTDecode")),
|
"/Filter"
|
||||||
Pdf.Got (Pdfio.bytes_of_input_channel result)
|
(Pdf.Name "/DCTDecode")),
|
||||||
end
|
Pdf.Got (Pdfio.bytes_of_input_channel result)
|
||||||
else
|
end
|
||||||
begin
|
else
|
||||||
if !debug_image_processing then Printf.printf "no size reduction\n%!"
|
begin
|
||||||
end;
|
if !debug_image_processing then Printf.printf "no size reduction\n%!"
|
||||||
close_in result
|
end;
|
||||||
|
close_in result
|
||||||
|
with
|
||||||
|
_ ->
|
||||||
|
remove out;
|
||||||
|
remove out2
|
||||||
end;
|
end;
|
||||||
remove out;
|
remove out;
|
||||||
remove out2
|
remove out2
|
||||||
|
@ -637,9 +655,10 @@ let test_bpc pdf dict =
|
||||||
| _ -> 0
|
| _ -> 0
|
||||||
|
|
||||||
let lossless_resample pdf ~pixel_threshold ~length_threshold ~factor ~interpolate ~path_to_convert s dict reference =
|
let lossless_resample pdf ~pixel_threshold ~length_threshold ~factor ~interpolate ~path_to_convert s dict reference =
|
||||||
(*Printf.printf "***lossless_resample IN dictionary: %S\n" (Pdfwrite.string_of_pdf dict); *)
|
complain_convert path_to_convert;
|
||||||
let in_components = test_components pdf dict in
|
let in_components = test_components pdf dict in
|
||||||
let in_bpc = test_bpc pdf dict in
|
let in_bpc = test_bpc pdf dict in
|
||||||
|
(*Printf.printf "***lossless_resample IN dictionary: %S\n" (Pdfwrite.string_of_pdf dict); *)
|
||||||
(*Printf.printf "\n***IN components = %i, bpc = %i\n" in_components in_bpc;*)
|
(*Printf.printf "\n***IN components = %i, bpc = %i\n" in_components in_bpc;*)
|
||||||
match lossless_out pdf ~pixel_threshold ~length_threshold ".png" s dict reference with
|
match lossless_out pdf ~pixel_threshold ~length_threshold ".png" s dict reference with
|
||||||
| None -> ()
|
| None -> ()
|
||||||
|
@ -662,7 +681,6 @@ let lossless_resample pdf ~pixel_threshold ~length_threshold ~factor ~interpolat
|
||||||
let newsize = in_channel_length result in
|
let newsize = in_channel_length result in
|
||||||
if newsize < size then
|
if newsize < size then
|
||||||
begin
|
begin
|
||||||
|
|
||||||
reference :=
|
reference :=
|
||||||
(match fst (obj_of_png_data (Pdfio.bytes_of_input_channel result)) with
|
(match fst (obj_of_png_data (Pdfio.bytes_of_input_channel result)) with
|
||||||
| Pdf.Stream {contents = Pdf.Dictionary d, data} as s ->
|
| Pdf.Stream {contents = Pdf.Dictionary d, data} as s ->
|
||||||
|
@ -700,7 +718,9 @@ let lossless_resample pdf ~pixel_threshold ~length_threshold ~factor ~interpolat
|
||||||
end;
|
end;
|
||||||
remove out;
|
remove out;
|
||||||
remove out2
|
remove out2
|
||||||
with _ -> () (* FIXME Remove *)
|
with _ ->
|
||||||
|
remove out;
|
||||||
|
remove out2
|
||||||
|
|
||||||
let lossless_resample_target_dpi objnum pdf ~pixel_threshold ~length_threshold ~factor ~target_dpi_info ~interpolate ~path_to_convert s dict reference =
|
let lossless_resample_target_dpi objnum pdf ~pixel_threshold ~length_threshold ~factor ~target_dpi_info ~interpolate ~path_to_convert s dict reference =
|
||||||
(*Printf.printf "lossless_resample_target_dpi\n";*)
|
(*Printf.printf "lossless_resample_target_dpi\n";*)
|
||||||
|
@ -709,9 +729,6 @@ let lossless_resample_target_dpi objnum pdf ~pixel_threshold ~length_threshold ~
|
||||||
if real_factor < 100. then
|
if real_factor < 100. then
|
||||||
lossless_resample pdf ~pixel_threshold ~length_threshold ~factor:real_factor ~interpolate ~path_to_convert s dict reference
|
lossless_resample pdf ~pixel_threshold ~length_threshold ~factor:real_factor ~interpolate ~path_to_convert s dict reference
|
||||||
|
|
||||||
let complain_jbig2enc path =
|
|
||||||
if path = "" then error "Specify jbig2enc location with -jbig2enc"
|
|
||||||
|
|
||||||
let recompress_1bpp_jbig2_lossless ~pixel_threshold ~length_threshold ~path_to_jbig2enc pdf s dict reference =
|
let recompress_1bpp_jbig2_lossless ~pixel_threshold ~length_threshold ~path_to_jbig2enc pdf s dict reference =
|
||||||
complain_jbig2enc path_to_jbig2enc;
|
complain_jbig2enc path_to_jbig2enc;
|
||||||
let old = !reference in
|
let old = !reference in
|
||||||
|
|
Loading…
Reference in New Issue