Skip to content

Commit

Permalink
bit of refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Your Name committed Jul 13, 2020
1 parent fef052b commit 727aaef
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 11 deletions.
8 changes: 8 additions & 0 deletions src/imageLib.mli
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ val writefile : extension:string ->

module PPM :
sig
include WriteImage
include ReadImage

module ReadPPM : ReadImage

type ppm_mode = Binary | ASCII
Expand All @@ -72,10 +75,15 @@ module PPM :

module PNG :
sig
include WriteImage
include ReadImage

module ReadPNG : ReadImage

[@@ocaml.deprecated]
val write_png : chunk_writer -> image -> unit

[@@ocaml.deprecated]
val bytes_of_png : image -> Bytes.t
end

Expand Down
6 changes: 3 additions & 3 deletions src/imagePNG.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1206,9 +1206,7 @@ let output_png img (och:chunk_writer) =
raise (Corrupted_image "PNG: img.pixels * bd (bitdepth) mismatch")
);
let data = Buffer.contents buf in
Printf.printf "about to compress\n%!";
let data = compress_string data in
Printf.printf "did do the compress\n%!";

let datalen = String.length data in
let max_idat_len = 1048576 in (* 2^20 should be enough *)
Expand Down Expand Up @@ -1242,10 +1240,12 @@ let bytes_of_png img =
let approx_size = img.width * img.height in
let buf = Buffer.create approx_size in
let och = chunk_writer_of_buffer buf in
Printf.printf "made chunk writer\n%!";
PngWriter.output_png img och;
close_chunk_writer och;
(* Do this instead of Buffer.to_bytes to avoid copying
since the underlying string backing the Buffer.to_bytes
does not escape this scope: *)
Bytes.unsafe_of_string (Buffer.contents buf)

include ReadPNG
let write = write_png
3 changes: 3 additions & 0 deletions src/imagePPM.ml
Original file line number Diff line number Diff line change
Expand Up @@ -304,3 +304,6 @@ let write_ppm (och:ImageUtil.chunk_writer) img mode =
);

close_chunk_writer och

include ReadPPM
let write cw img = write_ppm cw img Binary
12 changes: 6 additions & 6 deletions tests/tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,14 @@ module ImageLib_tests = struct
[crowbar_gen_cr_gif]
(crowbar_skip_known_errors(fun cr -> (ignore @@ ImageLib.GIF.parsefile cr; true)))
;
Crowbar.add_test ~name:"ImageLib.PNG.ReadPNG.size"
Crowbar.add_test ~name:"ImageLib.PNG.size"
[crowbar_gen_cr_png]
(crowbar_skip_known_errors(fun cr -> (ImageLib.PNG.ReadPNG.size cr <> (0, 0))))
(crowbar_skip_known_errors(fun cr -> (ImageLib.PNG.size cr <> (0, 0))))

let crowbar_png_parsefile () =
Crowbar.add_test ~name:"ImageLib.PNG.ReadPNG.openfile"
Crowbar.add_test ~name:"ImageLib.PNG.openfile"
[crowbar_gen_cr_png]
(crowbar_skip_known_errors(fun cr -> (ignore @@ ImageLib.PNG.ReadPNG.parsefile cr; true)))
(crowbar_skip_known_errors(fun cr -> (ignore @@ ImageLib.PNG.parsefile cr; true)))

let fuzzing : unit Alcotest.test_case list =
[ "PNG fuzz size", `Slow, crowbar_png_size
Expand All @@ -60,12 +60,12 @@ module ImageLib_PNG_tests = struct
let chunk_reader_of_string_raises _ =
Alcotest.(check_raises) "when reading outside bounds, End_of_file is raised"
End_of_file
(fun () -> ignore @@ ImageLib.PNG.ReadPNG.size(cr_as "\149\218\249"))
(fun () -> ignore @@ ImageLib.PNG.size(cr_as "\149\218\249"))

let self_test_1 () =
let img = Image.create_rgb 3 3 in
let enc = ImageLib.PNG.bytes_of_png img in
let dec = ImageLib.PNG.ReadPNG.parsefile
let dec = ImageLib.PNG.parsefile
(ImageUtil.chunk_reader_of_string (Bytes.to_string enc)) in
Alcotest.check Alcotest.int "equality" 0 (Image.compare_image img dec) ;
Image.write_rgb img 0 0 1 0 0 ;
Expand Down
7 changes: 5 additions & 2 deletions unix/imageLib_unix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ let warning fn msg =

let convert fn fn' =
let ret =
(* don't accidentally put command-line options here *)
assert (String.get fn 0 <> '-');
assert (String.get fn' 0 <> '-');
Unix.create_process "convert" [| "convert"; fn ; fn' |]
(* "--" ; see:
https://github.com/rlepigre/ocaml-imagelib/pull/15#discussion_r198867027
Expand All @@ -29,7 +32,7 @@ let size fn =
let fn' = Filename.temp_file "image" ".png" in
convert fn fn';
let ich' = ImageUtil_unix.chunk_reader_of_path fn' in
let sz = ImagePNG.ReadPNG.size ich' in
let sz = ImagePNG.size ich' in
rm fn'; sz
end

Expand All @@ -44,7 +47,7 @@ let openfile fn : image =
let fn' = Filename.temp_file "image" ".png" in
convert fn fn';
let ich' = ImageUtil_unix.chunk_reader_of_path fn' in
let img = ImagePNG.ReadPNG.parsefile ich' in
let img = ImagePNG.parsefile ich' in
rm fn'; img
in
if extension = "gif" then
Expand Down

0 comments on commit 727aaef

Please sign in to comment.