Support memory JPEGs and PNGs for cpdflib

This commit is contained in:
John Whitington 2024-03-26 20:02:41 +00:00
parent aacc8cf633
commit 718d93713a
2 changed files with 33 additions and 21 deletions

View File

@ -237,26 +237,38 @@ let setmsheary s =
let usexobj s =
addop (Cpdfdraw.Use s)
let addjpeg n =
let name, filename =
match String.split_on_char '=' n with
| [name; filename] -> name, filename
| _ -> error "addjpeg: bad file specification"
in
try
let data = Pdfio.bytes_of_string (contents_of_file filename) in
addop (Cpdfdraw.ImageXObject (name, fst (Cpdfimage.obj_of_jpeg_data data)))
with
_ -> error "addjpeg: could not load JPEG"
let addjpeg ?data n =
match data with
| Some d ->
addop
(Cpdfdraw.ImageXObject
(n, fst (Cpdfimage.obj_of_jpeg_data (Pdfio.bytes_of_raw d))))
| None ->
let name, filename =
match String.split_on_char '=' n with
| [name; filename] -> name, filename
| _ -> error "addjpeg: bad file specification"
in
try
let data = Pdfio.bytes_of_string (contents_of_file filename) in
addop (Cpdfdraw.ImageXObject (name, fst (Cpdfimage.obj_of_jpeg_data data)))
with
_ -> error "addjpeg: could not load JPEG"
let addpng n =
let name, filename =
match String.split_on_char '=' n with
| [name; filename] -> name, filename
| _ -> error "addpng: bad file specification"
in
let data = Pdfio.bytes_of_string (contents_of_file filename) in
addop (Cpdfdraw.ImageXObject (name, fst (Cpdfimage.obj_of_png_data data)))
let addpng ?data n =
match data with
| Some d ->
addop
(Cpdfdraw.ImageXObject
(n, fst (Cpdfimage.obj_of_png_data (Pdfio.bytes_of_raw d))))
| None ->
let name, filename =
match String.split_on_char '=' n with
| [name; filename] -> name, filename
| _ -> error "addpng: bad file specification"
in
let data = Pdfio.bytes_of_string (contents_of_file filename) in
addop (Cpdfdraw.ImageXObject (name, fst (Cpdfimage.obj_of_png_data data)))
let addimage s =
addop (Cpdfdraw.Image s)

View File

@ -48,8 +48,8 @@ val xobjbbox : string -> unit
val startxobj : string -> unit
val endxobj : unit -> unit
val usexobj : string -> unit
val addjpeg : string -> unit
val addpng : string -> unit
val addjpeg : ?data:Pdfio.rawbytes -> string -> unit
val addpng : ?data:Pdfio.rawbytes -> string -> unit
val addimage : string -> unit
val addopacity : float -> unit
val addsopacity : float -> unit