mirror of
https://github.com/johnwhitington/cpdf-source.git
synced 2025-02-16 11:50:54 +01:00
Direct PNG output was misinformed
This commit is contained in:
parent
5f688481ea
commit
261f121040
39
cpdfimage.ml
39
cpdfimage.ml
@ -22,42 +22,13 @@ let pnm_to_channel_24 channel w h s =
|
|||||||
done
|
done
|
||||||
done
|
done
|
||||||
|
|
||||||
(* FIXME do this all at once *)
|
|
||||||
let write_stream name stream =
|
let write_stream name stream =
|
||||||
let fh = open_out_bin name in
|
let fh = open_out_bin name in
|
||||||
for x = 0 to bytes_size stream - 1 do
|
Pdfio.bytes_to_output_channel fh stream;
|
||||||
output_byte fh (bget stream x)
|
|
||||||
done;
|
|
||||||
close_out fh
|
close_out fh
|
||||||
|
|
||||||
(* Detect images we can write directly as PNGs, to avoid going through pnm2png or imagemagick.
|
|
||||||
This is when BPC = 8, colourspace = DeviceRGB or CalRGB, compression is /FlateDecode. *)
|
|
||||||
let write_image_png pdf resources name dict =
|
|
||||||
(*Printf.printf "%s\n" (Pdfwrite.string_of_pdf (Pdf.direct pdf dict));*)
|
|
||||||
match
|
|
||||||
Pdfimage.colspace pdf dict resources,
|
|
||||||
Pdfimage.bpc pdf dict,
|
|
||||||
Pdf.lookup_direct pdf "/Filter" dict
|
|
||||||
with
|
|
||||||
| (Pdfspace.DeviceRGB | Pdfspace.CalRGB _),
|
|
||||||
Some (Pdf.Integer 8),
|
|
||||||
Some (Pdf.Name "/FlateDecode" | Pdf.Array [Pdf.Name "/FlateDecode"]) ->
|
|
||||||
(*Printf.printf "Direct to png...\n";*)
|
|
||||||
Pdf.getstream (Pdf.direct pdf dict);
|
|
||||||
let ch = open_out_bin (name ^ ".png") in
|
|
||||||
let o = Pdfio.output_of_channel ch in
|
|
||||||
let width = match Pdf.lookup_direct pdf "/Width" dict with Some (Pdf.Integer x) -> x | _ -> raise Exit in
|
|
||||||
let height = match Pdf.lookup_direct pdf "/Height" dict with Some (Pdf.Integer x) -> x | _ -> raise Exit in
|
|
||||||
let idat = match Pdf.direct pdf dict with Pdf.Stream {contents = (_, Got bytes)} -> bytes | _ -> raise Exit in
|
|
||||||
(*Printf.printf "all ok...\n";*)
|
|
||||||
Cpdfpng.write_png {width; height; idat} o;
|
|
||||||
close_out ch;
|
|
||||||
true
|
|
||||||
| _ -> false
|
|
||||||
|
|
||||||
let write_image path_to_p2p path_to_im pdf resources name image =
|
let write_image path_to_p2p path_to_im pdf resources name image =
|
||||||
let main () =
|
match Pdfimage.get_image_24bpp pdf resources image with
|
||||||
begin match Pdfimage.get_image_24bpp pdf resources image with
|
|
||||||
| Pdfimage.JPEG (stream, _) -> write_stream (name ^ ".jpg") stream
|
| Pdfimage.JPEG (stream, _) -> write_stream (name ^ ".jpg") stream
|
||||||
| Pdfimage.JPEG2000 (stream, _) -> write_stream (name ^ ".jpx") stream
|
| Pdfimage.JPEG2000 (stream, _) -> write_stream (name ^ ".jpx") stream
|
||||||
| Pdfimage.JBIG2 (stream, _) -> write_stream (name ^ ".jbig2") stream
|
| Pdfimage.JBIG2 (stream, _) -> write_stream (name ^ ".jbig2") stream
|
||||||
@ -93,12 +64,6 @@ let write_image path_to_p2p path_to_im pdf resources name image =
|
|||||||
end
|
end
|
||||||
| _ ->
|
| _ ->
|
||||||
Pdfe.log (Printf.sprintf "Unsupported image type when extracting image %s " name)
|
Pdfe.log (Printf.sprintf "Unsupported image type when extracting image %s " name)
|
||||||
end
|
|
||||||
in
|
|
||||||
match write_image_png pdf resources name image with
|
|
||||||
| true -> ()
|
|
||||||
| exception x -> Pdfe.log (Printf.sprintf "Failed to write PNG directly (%s)\n" (Printexc.to_string x)); main ()
|
|
||||||
| _ -> main ()
|
|
||||||
|
|
||||||
let written = ref []
|
let written = ref []
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user