cpdf-source/cpdfpng.ml

71 lines
2.0 KiB
OCaml
Raw Normal View History

2022-12-23 15:29:47 +01:00
(* Read a non-interlaced, non-transparent 24 bit PNG for inclusion in a PDF file *)
2022-12-23 16:24:59 +01:00
open Pdfutil
open Pdfio
2022-12-23 15:29:47 +01:00
type t =
{width : int;
height : int;
2022-12-23 16:24:59 +01:00
idat : bytes}
2022-12-23 15:29:47 +01:00
exception BadPNG of string
2022-12-23 16:24:59 +01:00
let string_of_tag t =
Printf.sprintf "%c%c%c%c"
(char_of_int (i32toi (Int32.shift_right t 24)))
(char_of_int (i32toi (Int32.logand 0x000000FFl (Int32.shift_right t 16))))
(char_of_int (i32toi (Int32.logand 0x000000FFl (Int32.shift_right t 8))))
(char_of_int (i32toi (Int32.logand 0x000000FFl t)))
2022-12-23 17:19:40 +01:00
let read_unsigned_4byte i =
let a = i32ofi (i.input_byte ()) in
let b = i32ofi (i.input_byte ()) in
let c = i32ofi (i.input_byte ()) in
let d = i32ofi (i.input_byte ()) in
lor32 (lor32 (lsl32 a 24) (lsl32 b 16)) (lor32 (lsl32 c 8) d)
2022-12-23 16:24:59 +01:00
2022-12-23 17:19:40 +01:00
let read_data l i =
let l = i32toi l in
let b = mkbytes l in
setinit i b 0 l;
b
2022-12-23 16:24:59 +01:00
let read_chunk i =
let chunklen = read_unsigned_4byte i in
2022-12-23 17:19:40 +01:00
Printf.printf "chunklen = %li\n" chunklen;
2022-12-23 16:24:59 +01:00
let chunktype = read_unsigned_4byte i in
2022-12-23 17:19:40 +01:00
Printf.printf "chunktype = %s\n" (string_of_tag chunktype);
2022-12-23 16:24:59 +01:00
let chunkdata = read_data chunklen i in
let _ (* crc *) = read_unsigned_4byte i in
(string_of_tag chunktype, chunkdata)
2022-12-23 15:29:47 +01:00
let read_png i =
2022-12-23 16:24:59 +01:00
try
i.seek_in 8;
let ihdr, ihdrdata = read_chunk i in
if ihdr <> "IHDR" then raise (Pdf.PDFError "read_png: first table not IHDR") else
let width = 0 in
let height = 0 in
let bitdepth = 0 in
let colortype = 0 in
let compressionmethod = 0 in
let filtermethod = 0 in
let interlacemethod = 0 in
let idat = ref None in
begin try
while true do
let chunkname, chunkdata = read_chunk i in
if chunkname = "IDAT" then
idat := Some chunkdata
done
with
_ -> ()
end;
{width; height; idat = unopt !idat}
with
e -> raise (Pdf.PDFError (Printf.sprintf "read_png: failed on %s" (Printexc.to_string e)))
2022-12-23 15:29:47 +01:00
let _ =
read_png
2022-12-23 16:24:59 +01:00
(input_of_string
2022-12-23 15:29:47 +01:00
(Pdfutil.contents_of_file "/Users/john/Desktop/cpdfdraw/Untitled.png"))