Skip to content

Commit f99d747

Browse files
committed
Rewrite in ocaml
1 parent f578935 commit f99d747

File tree

110 files changed

+2113
-2328
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

110 files changed

+2113
-2328
lines changed

esy.lock/overrides/opam__s__ocamlbuild_opam__c__0.14.2+win_opam_override/files/ocamlbuild-0.14.2.patch renamed to .ocamlformat

File renamed without changes.

bin/Color.ml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
let ofHexString s =
2+
if String.length s = 4 || String.length s = 7 then
3+
let short = String.length s = 4 in
4+
let r' =
5+
match short with true -> String.sub s 1 1 | false -> String.sub s 1 2
6+
in
7+
let g' =
8+
match short with true -> String.sub s 2 1 | false -> String.sub s 3 2
9+
in
10+
let b' =
11+
match short with true -> String.sub s 3 1 | false -> String.sub s 5 2
12+
in
13+
let r = int_of_string_opt ("0x" ^ r') in
14+
let g = int_of_string_opt ("0x" ^ g') in
15+
let b = int_of_string_opt ("0x" ^ b') in
16+
match (r, g, b) with
17+
| Some r, Some g, Some b when short ->
18+
Some ((16 * r) + r, (16 * g) + g, (16 * b) + b)
19+
| Some r, Some g, Some b -> Some (r, g, b)
20+
| _ -> None
21+
else None

bin/Color.re

Lines changed: 0 additions & 19 deletions
This file was deleted.

bin/Main.ml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
open Odiff.ImageIO
2+
open Odiff.Diff
3+
4+
let getIOModule filename =
5+
Filename.extension filename |> function
6+
| ".png" -> (module ODiffIO.Png.IO : ImageIO)
7+
| ".jpg" | ".jpeg" -> (module ODiffIO.Jpg.IO : ImageIO)
8+
| ".bmp" -> (module ODiffIO.Bmp.IO : ImageIO)
9+
| ".tiff" -> (module ODiffIO.Tiff.IO : ImageIO)
10+
| f -> failwith ("This format is not supported: " ^ f)
11+
12+
type 'output diffResult = { exitCode : int; diff : 'output option }
13+
14+
let main img1Path img2Path diffPath threshold outputDiffMask failOnLayoutChange
15+
diffColorHex stdoutParsableString antialiasing ignoreRegions diffLines =
16+
let module IO1 = (val getIOModule img1Path) in
17+
let module IO2 = (val getIOModule img2Path) in
18+
let module Diff = MakeDiff (IO1) (IO2) in
19+
let img1 = IO1.loadImage img1Path in
20+
let img2 = IO2.loadImage img2Path in
21+
let { diff; exitCode } =
22+
Diff.diff img1 img2 ~outputDiffMask ~threshold ~failOnLayoutChange
23+
~antialiasing ~ignoreRegions ~diffLines
24+
~diffPixel:
25+
(Color.ofHexString diffColorHex |> function
26+
| Some col -> col
27+
| None -> (255, 0, 0))
28+
()
29+
|> Print.printDiffResult stdoutParsableString
30+
|> function
31+
| Layout -> { diff = None; exitCode = 21 }
32+
| Pixel (diffOutput, diffCount, stdoutParsableString, _) when diffCount = 0
33+
->
34+
{ exitCode = 0; diff = Some diffOutput }
35+
| Pixel (diffOutput, diffCount, diffPercentage, _) ->
36+
IO1.saveImage diffOutput diffPath;
37+
{ exitCode = 22; diff = Some diffOutput }
38+
in
39+
IO1.freeImage img1;
40+
IO2.freeImage img2;
41+
(match diff with
42+
| ((Some output) [@explicit_arity]) when outputDiffMask ->
43+
IO1.freeImage output
44+
| _ -> ());
45+
exit exitCode

bin/Main.re

Lines changed: 0 additions & 86 deletions
This file was deleted.

bin/ODiffBin.ml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
open Cmdliner
2+
3+
let diffPath =
4+
let open Arg in
5+
value & pos 2 string ""
6+
& info [] ~docv:"DIFF" ~doc:"Diff output path (.png only)"
7+
8+
let base =
9+
let open Arg in
10+
value & pos 0 file "" & info [] ~docv:"BASE" ~doc:"Path to base image"
11+
12+
let comp =
13+
let open Arg in
14+
value & pos 1 file ""
15+
& info [] ~docv:"COMPARING" ~doc:"Path to comparing image"
16+
17+
let threshold =
18+
let open Arg in
19+
value & opt float 0.1
20+
& info [ "t"; "threshold" ] ~docv:"THRESHOLD"
21+
~doc:"Color difference threshold (from 0 to 1). Less more precise."
22+
23+
let diffMask =
24+
let open Arg in
25+
value & flag
26+
& info [ "dm"; "diff-mask" ] ~docv:"DIFF_IMAGE"
27+
~doc:"Output only changed pixel over transparent background."
28+
29+
let failOnLayout =
30+
let open Arg in
31+
value & flag
32+
& info [ "fail-on-layout" ] ~docv:"FAIL_ON_LAYOUT"
33+
~doc:
34+
"Do not compare images and produce output if images layout is \
35+
different."
36+
37+
let parsableOutput =
38+
let open Arg in
39+
value & flag
40+
& info [ "parsable-stdout" ] ~docv:"PARSABLE_OUTPUT"
41+
~doc:"Stdout parsable output"
42+
43+
let diffColor =
44+
let open Arg in
45+
value & opt string ""
46+
& info [ "diff-color" ]
47+
~doc:
48+
"Color used to highlight different pixels in the output (in hex format \
49+
e.g. #cd2cc9)."
50+
51+
let antialiasing =
52+
let open Arg in
53+
value & flag
54+
& info [ "aa"; "antialiasing" ]
55+
~doc:
56+
"With this flag enabled, antialiased pixels are not counted to the \
57+
diff of an image"
58+
59+
let diffLines =
60+
let open Arg in
61+
value & flag
62+
& info [ "output-diff-lines" ]
63+
~doc:
64+
"With this flag enabled, output result in case of different images \
65+
will output lines for all the different pixels"
66+
67+
let ignoreRegions =
68+
let open Arg in
69+
value
70+
& opt
71+
(list ~sep:',' (t2 ~sep:'-' (t2 ~sep:':' int int) (t2 ~sep:':' int int)))
72+
[]
73+
& info [ "i"; "ignore" ]
74+
~doc:
75+
"An array of regions to ignore in the diff. One region looks like \
76+
\"x1:y1-x2:y2\". Multiple regions are separated with a ','."
77+
78+
let cmd =
79+
let man =
80+
[
81+
`S Manpage.s_description;
82+
`P "$(tname) is the fastest pixel-by-pixel image comparison tool.";
83+
`P "Supported image types: .png, .jpg, .jpeg, .bitmap";
84+
]
85+
in
86+
( (let open Term in
87+
const Main.main $ base $ comp $ diffPath $ threshold $ diffMask
88+
$ failOnLayout $ diffColor $ parsableOutput $ antialiasing $ ignoreRegions
89+
$ diffLines),
90+
Term.info "odiff" ~version:"2.6.1" ~doc:"Find difference between 2 images."
91+
~exits:
92+
(Term.exit_info 0 ~doc:"on image match"
93+
:: Term.exit_info 21 ~doc:"on layout diff when --fail-on-layout"
94+
:: Term.exit_info 22 ~doc:"on image pixel difference"
95+
:: Term.default_error_exits)
96+
~man )
97+
98+
let () = Term.eval cmd |> Term.exit

0 commit comments

Comments
 (0)