diff --git a/cpdfdrawcontrol.ml b/cpdfdrawcontrol.ml index 9eba079..de7da28 100644 --- a/cpdfdrawcontrol.ml +++ b/cpdfdrawcontrol.ml @@ -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) diff --git a/cpdfdrawcontrol.mli b/cpdfdrawcontrol.mli index f918733..654cb37 100644 --- a/cpdfdrawcontrol.mli +++ b/cpdfdrawcontrol.mli @@ -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