diff --git a/cpdfimage.ml b/cpdfimage.ml index 1b4762d..99420ee 100644 --- a/cpdfimage.ml +++ b/cpdfimage.ml @@ -637,10 +637,10 @@ let test_bpc pdf dict = | _ -> 0 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); *) + (*Printf.printf "***lossless_resample IN dictionary: %S\n" (Pdfwrite.string_of_pdf dict); *) let in_components = test_components pdf dict in let in_bpc = test_bpc pdf dict in - 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 | None -> () | Some (_, _, _, 4, _, _) -> Printf.printf "lossless resampling for CMYK not supported yet\n%!" @@ -668,8 +668,17 @@ let lossless_resample pdf ~pixel_threshold ~length_threshold ~factor ~interpolat | Pdf.Stream {contents = Pdf.Dictionary d, data} as s -> let out_components = test_components pdf s in let out_bpc = test_bpc pdf s in - Printf.printf "***OUT components = %i, bpc = %i\n" out_components out_bpc; - if out_components <> in_components || in_bpc <> out_bpc then + (*Printf.printf "***OUT components = %i, bpc = %i\n" out_components out_bpc;*) + let rgb_to_grey_special = + let was_rgb = + match Pdf.lookup_direct pdf "/ColorSpace" dict with + | Some (Pdf.Name ("/DeviceRGB" | "/CalRGB")) -> true + | _ -> false + in + in_bpc = out_bpc && in_components = 3 && out_components = 1 && was_rgb + in + (*Printf.printf "***rgb_to_grey_special = %b\n" rgb_to_grey_special;*) + if (out_components <> in_components || in_bpc <> out_bpc) && not rgb_to_grey_special then begin if !debug_image_processing then Printf.printf "wrong bpc / components returned. Skipping.\n%!"; !reference @@ -677,9 +686,7 @@ let lossless_resample pdf ~pixel_threshold ~length_threshold ~factor ~interpolat else begin if !debug_image_processing then Printf.printf "lossless resample %i -> %i (%i%%)\n%!" size newsize (int_of_float (float newsize /. float size *. 100.)); - (* We will then test all the files we have available, and make sure all results correct *) - (* Then we will see about how to poke convert to do the correct thing - e.g not use PNG, force different output etc. *) - let d' = fold_right (fun (k, v) d -> if k <> "/ColorSpace" then add k v d else d) d (match dict with Pdf.Dictionary x -> x | _ -> []) in + let d' = fold_right (fun (k, v) d -> if k <> "/ColorSpace" || rgb_to_grey_special then add k v d else d) d (match dict with Pdf.Dictionary x -> x | _ -> []) in (*Printf.printf "***lossless_resample OUT dictionary: %S\n" (Pdfwrite.string_of_pdf (Pdf.Dictionary d')); *) (Pdf.Dictionary d', data) end diff --git a/cpdfmanual.pdf b/cpdfmanual.pdf index 557dcc0..39cd9f9 100644 Binary files a/cpdfmanual.pdf and b/cpdfmanual.pdf differ diff --git a/cpdfmanual.tex b/cpdfmanual.tex index 9a23aaf..f4b8011 100644 --- a/cpdfmanual.tex +++ b/cpdfmanual.tex @@ -3895,6 +3895,8 @@ Method & Effect\\\hline \noindent It is not currently possible to reprocess lossless JBIG2 into lossy JBIG2, nor is it possible to recompress into CCITT. +NB: CYMK images will be converted to RGB by some of these processes. A future version of cpdf will remove this limitation. + \begin{cpdflib} \clearpage \section*{C Interface}