This commit is contained in:
John Whitington 2023-01-11 13:51:50 +08:00
parent 68a57fa99b
commit 18d3763b05
1 changed files with 25 additions and 23 deletions

View File

@ -1950,10 +1950,10 @@ let addjpeg n =
with with
_ -> error "addjpeg: could not load JPEG" _ -> error "addjpeg: could not load JPEG"
let jpeg_of_input i = let image_of_input fobj i =
let pdf = Pdf.empty () in let pdf = Pdf.empty () in
let data = Pdfio.bytes_of_input i 0 i.Pdfio.in_channel_length in let data = Pdfio.bytes_of_input i 0 i.Pdfio.in_channel_length in
let obj = obj_of_jpeg_data data in let obj = fobj data in
let w = match Pdf.lookup_direct pdf "/Width" obj with Some x -> Pdf.getnum pdf x | _ -> assert false in let w = match Pdf.lookup_direct pdf "/Width" obj with Some x -> Pdf.getnum pdf x | _ -> assert false in
let h = match Pdf.lookup_direct pdf "/Height" obj with Some x -> Pdf.getnum pdf x | _ -> assert false in let h = match Pdf.lookup_direct pdf "/Height" obj with Some x -> Pdf.getnum pdf x | _ -> assert false in
let page = let page =
@ -1976,18 +1976,12 @@ let set_input_jpeg s =
args.original_filename <- s; args.original_filename <- s;
args.create_objstm <- true; args.create_objstm <- true;
let fh = open_in_bin s in let fh = open_in_bin s in
let pdf = jpeg_of_input (Pdfio.input_of_channel fh) in let pdf = image_of_input obj_of_jpeg_data (Pdfio.input_of_channel fh) in
close_in fh; close_in fh;
args.inputs <- (AlreadyInMemory pdf, "all", "", "", ref false, None)::args.inputs args.inputs <- (AlreadyInMemory pdf, "all", "", "", ref false, None)::args.inputs
let addpng n = let obj_of_png_data data =
let name, filename = let png = Cpdfpng.read_png (Pdfio.input_of_bytes data) in
match String.split_on_char '=' n with
| [name; filename] -> name, filename
| _ -> error "addjpeg: bad file specification"
in
let data = Pdfio.input_of_string (contents_of_file filename) in
let png = Cpdfpng.read_png data in
let d = let d =
["/Length", Pdf.Integer (Pdfio.bytes_size png.idat); ["/Length", Pdf.Integer (Pdfio.bytes_size png.idat);
"/Filter", Pdf.Name "/FlateDecode"; "/Filter", Pdf.Name "/FlateDecode";
@ -2002,8 +1996,16 @@ let addpng n =
"/Width", Pdf.Integer png.width; "/Width", Pdf.Integer png.width;
"/Height", Pdf.Integer png.height] "/Height", Pdf.Integer png.height]
in in
let obj = Pdf.Stream {contents = (Pdf.Dictionary d , Pdf.Got png.idat)} in Pdf.Stream {contents = (Pdf.Dictionary d , Pdf.Got png.idat)}
addop (Cpdfdraw.ImageXObject (name, obj))
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 = bytes_of_string (contents_of_file filename) in
addop (Cpdfdraw.ImageXObject (name, obj_of_png_data data))
let png_of_input i = Pdf.empty () let png_of_input i = Pdf.empty ()
@ -2011,7 +2013,7 @@ let set_input_png s =
args.original_filename <- s; args.original_filename <- s;
args.create_objstm <- true; args.create_objstm <- true;
let fh = open_in_bin s in let fh = open_in_bin s in
let pdf = jpeg_of_input (Pdfio.input_of_channel fh) in let pdf = image_of_input obj_of_png_data (Pdfio.input_of_channel fh) in
close_in fh; close_in fh;
args.inputs <- (AlreadyInMemory pdf, "all", "", "", ref false, None)::args.inputs args.inputs <- (AlreadyInMemory pdf, "all", "", "", ref false, None)::args.inputs