Work on lossless->JPEG

This commit is contained in:
John Whitington
2023-12-17 12:58:32 +00:00
parent adeb7c6c33
commit 81c719aae3

View File

@ -457,7 +457,9 @@ let image_of_input fobj i =
let pdf, pageroot = Pdfpage.add_pagetree [page] pdf in let pdf, pageroot = Pdfpage.add_pagetree [page] pdf in
Pdfpage.add_root pageroot [] pdf Pdfpage.add_root pageroot [] pdf
(* For each image xobject, process it through convert to reduce JPEG quality if we can. *) (* FIXME Make sure this process is ok for masks too - do we get them, is it allowed etc. *)
(* FIXME Only do if quality < 100 *)
(* For each image xobject, process it through convert to reduce size. *)
let process pdf ~q ~qlossless ~path_to_convert = let process pdf ~q ~qlossless ~path_to_convert =
let process_obj _ s = let process_obj _ s =
match s with match s with
@ -490,8 +492,19 @@ let process pdf ~q ~qlossless ~path_to_convert =
end; end;
Sys.remove out; Sys.remove out;
Sys.remove out2 Sys.remove out2
| _ -> () | Some (Pdf.Name "/Image"), _ ->
(* 0. Test if this is one we can do - for now just Colourspace=RGB, BPC=8 *)
begin match Pdf.lookup_direct pdf "/ColorSpace" dict, Pdf.lookup_direct pdf "/BitsPerComponent" dict with
| Some (Pdf.Name "/DeviceRGB"), Some (Pdf.Integer 8) ->
Printf.printf "Found a lossless(rgb, 8) image to JPEGify\n"
(* 1. Decompress it - check we succeeded, bail if not *)
(* 1. Output to pnm *)
(* 2. Convert to JPEG with convert *)
(* 3. Check smaller, Read file, and build new dictionary - removing ColorSpace, BitsPerComponent replacing Filter *)
| _ -> Printf.printf "I"
end end
| _ -> () | _ -> () (* not an image *)
end
| _ -> () (* not a stream *)
in in
Pdf.objiter process_obj pdf Pdf.objiter process_obj pdf