-jpeg-to-jpeg now working

This commit is contained in:
John Whitington 2023-12-07 15:08:34 +00:00
parent c7dc1cd424
commit 58122d8bae
1 changed files with 2 additions and 6 deletions

View File

@ -468,30 +468,26 @@ let process pdf ~q ~qlossless ~path_to_convert =
let out = Filename.temp_file "cpdf" "convertin" ^ ".jpg" in let out = Filename.temp_file "cpdf" "convertin" ^ ".jpg" in
let out2 = Filename.temp_file "cpdf" "convertout" ^ ".jpg" in let out2 = Filename.temp_file "cpdf" "convertout" ^ ".jpg" in
let fh = open_out_bin out in let fh = open_out_bin out in
(* 1. Write data to temp file *)
let size = let size =
begin match s with Pdf.Stream {contents = _, Pdf.Got d} -> Pdfio.bytes_to_output_channel fh d; bytes_size d | _ -> 0 end begin match s with Pdf.Stream {contents = _, Pdf.Got d} -> Pdfio.bytes_to_output_channel fh d; bytes_size d | _ -> 0 end
in in
close_out fh; close_out fh;
(* 2. Process with convert. *)
let retcode = let retcode =
let command = let command =
(Filename.quote_command path_to_convert (Filename.quote_command path_to_convert
[out; "-quality"; string_of_int q ^ "%"; out2]) [out; "-quality"; string_of_int q ^ "%"; out2])
in in
Printf.printf "%S\n" command; (*Printf.printf "%S\n" command;*)
Sys.command command Sys.command command
in in
if retcode = 0 then if retcode = 0 then
begin begin
(* 3. If return code 0, and output file produced, read it in *)
let result = open_in_bin out2 in let result = open_in_bin out2 in
let newsize = in_channel_length result in let newsize = in_channel_length result in
if newsize < size then if newsize < size then
(* 4. Replace the data, but only if it was smaller *) (*Printf.printf "%i -> %i\n" size newsize;*)
reference := Pdf.add_dict_entry dict "/Length" (Pdf.Integer newsize), Pdf.Got (Pdfio.bytes_of_input_channel result) reference := Pdf.add_dict_entry dict "/Length" (Pdf.Integer newsize), Pdf.Got (Pdfio.bytes_of_input_channel result)
end; end;
(* 5. Clean up *)
Sys.remove out; Sys.remove out;
Sys.remove out2 Sys.remove out2
| _ -> () | _ -> ()