@@ -13,6 +13,7 @@ let diff
1313 ~output
1414 ?(ignoreRegions = [] )
1515 ?(threshold = 0 )
16+ ~generateDiffImage
1617 ~diffPixel
1718 ~original_image_data
1819 ~new_image_data
@@ -26,99 +27,101 @@ let diff
2627 try Io. loadImage new_image_data |> Result. ok with
2728 | _ -> Result. error Io
2829 in
29- let free_images () =
30- Io. freeImage original_image;
31- Io. freeImage new_image
30+ let diff_result =
31+ Diff. diff
32+ original_image
33+ new_image
34+ ~output DiffMask:true
35+ ~threshold: 0.1
36+ ~fail OnLayoutChange:false
37+ ~antialiasing: true
38+ ~ignore Regions
39+ ~diff Pixel
40+ ()
3241 in
33- Diff. diff
34- original_image
35- new_image
36- ~output DiffMask:true
37- ~threshold: 0.1
38- ~fail OnLayoutChange:false
39- ~antialiasing: true
40- ~ignore Regions
41- ~diff Pixel
42- ()
43- |> function
44- | Pixel (_ , diffCount , _ , _ ) when diffCount < = threshold ->
45- free_images () ;
46- Result. ok ()
47- | Layout ->
48- free_images () ;
49- Result. error Layout
42+ match diff_result with
43+ | Pixel (_ , diffCount , _ , _ ) when diffCount < = threshold -> Result. ok ()
44+ | Layout -> Result. error Layout
5045 | Pixel (diff_mask , diffCount , diffPercentage , _ ) ->
51- let border_width = 5 in
52- let complete_width =
53- original_image.width
54- + border_width
55- + diff_mask.width
56- + border_width
57- + new_image.width
58- in
59- let complete_height =
60- original_image.height |> max diff_mask.height |> max new_image.height
61- in
62- let complete_image =
63- Array1. create int32 c_layout (complete_width * complete_height * 4 )
64- in
65- let original_image_start = 0 in
66- let original_image_end = original_image.width in
67- let diff_mask_start = original_image_end + border_width in
68- let diff_mask_end = diff_mask_start + diff_mask.width in
69- let new_image_start = diff_mask_end + border_width in
70- let new_image_end = new_image_start + new_image.width in
71- for offset = 0 to Array1. dim complete_image / 4 do
72- let row = offset / complete_width in
73- let col = offset mod complete_width in
74- let fill_with =
75- if col > = original_image_start
76- && col < original_image_end
77- && row < original_image.height
78- then Array1. get original_image.image ((row * original_image.width) + col)
79- else if col > diff_mask_start && col < diff_mask_end
80- then (
81- let diff_pixel =
82- if row < diff_mask.height
46+ let () =
47+ if generateDiffImage
48+ then (
49+ let border_width = 5 in
50+ let complete_width =
51+ original_image.width
52+ + border_width
53+ + diff_mask.width
54+ + border_width
55+ + new_image.width
56+ in
57+ let complete_height =
58+ original_image.height |> max diff_mask.height |> max new_image.height
59+ in
60+ let complete_image =
61+ Array1. create int32 c_layout (complete_width * complete_height * 4 )
62+ in
63+ let original_image_start = 0 in
64+ let original_image_end = original_image.width in
65+ let diff_mask_start = original_image_end + border_width in
66+ let diff_mask_end = diff_mask_start + diff_mask.width in
67+ let new_image_start = diff_mask_end + border_width in
68+ let new_image_end = new_image_start + new_image.width in
69+ for offset = 0 to Array1. dim complete_image / 4 do
70+ let row = offset / complete_width in
71+ let col = offset mod complete_width in
72+ let fill_with =
73+ if col > = original_image_start
74+ && col < original_image_end
75+ && row < original_image.height
76+ then Array1. get original_image.image ((row * original_image.width) + col)
77+ else if col > diff_mask_start && col < diff_mask_end
78+ then (
79+ let diff_pixel =
80+ if row < diff_mask.height
81+ then
82+ Array1. get
83+ diff_mask.image
84+ ((row * diff_mask.width) + (col - diff_mask_start))
85+ else 0x00000000l
86+ in
87+ if not (Int32. equal diff_pixel 0x00000000l )
88+ then diff_pixel
89+ else if row < original_image.height
90+ then (
91+ let pixel =
92+ Array1. get
93+ original_image.image
94+ ((row * original_image.width) + (col - diff_mask_start))
95+ |> Int32. to_int
96+ in
97+ let a = (pixel lsr 24 ) land 0xFF in
98+ let b = (pixel lsr 16 ) land 0xFF in
99+ let g = (pixel lsr 8 ) land 0xFF in
100+ let r = (pixel lsr 0 ) land 0xFF in
101+ let brightness = ((r * 54 ) + (g * 182 ) + (b * 19 )) / 255 in
102+ let mono = min 255 (brightness + 80 ) in
103+ let a = (a land 0xFF ) lsl 24 in
104+ let b = (mono land 0xFF ) lsl 16 in
105+ let g = (mono land 0xFF ) lsl 8 in
106+ let r = (mono land 0xFF ) lsl 0 in
107+ Int32. of_int (a lor b lor g lor r))
108+ else 0x00000000l )
109+ else if col > new_image_start
110+ && col < = new_image_end
111+ && row < new_image.height
83112 then
84113 Array1. get
85- diff_mask .image
86- ((row * diff_mask .width) + (col - diff_mask_start ))
114+ new_image .image
115+ ((row * new_image .width) + (col - new_image_start ))
87116 else 0x00000000l
88117 in
89- if not (Int32. equal diff_pixel 0x00000000l )
90- then diff_pixel
91- else if row < original_image.height
92- then (
93- let pixel =
94- Array1. get
95- original_image.image
96- ((row * original_image.width) + (col - diff_mask_start))
97- |> Int32. to_int
98- in
99- let a = (pixel lsr 24 ) land 0xFF in
100- let b = (pixel lsr 16 ) land 0xFF in
101- let g = (pixel lsr 8 ) land 0xFF in
102- let r = (pixel lsr 0 ) land 0xFF in
103- let brightness = ((r * 54 ) + (g * 182 ) + (b * 19 )) / 255 in
104- let mono = min 255 (brightness + 80 ) in
105- let a = (a land 0xFF ) lsl 24 in
106- let b = (mono land 0xFF ) lsl 16 in
107- let g = (mono land 0xFF ) lsl 8 in
108- let r = (mono land 0xFF ) lsl 0 in
109- Int32. of_int (a lor b lor g lor r))
110- else 0x00000000l )
111- else if col > new_image_start && col < = new_image_end && row < new_image.height
112- then Array1. get new_image.image ((row * new_image.width) + (col - new_image_start))
113- else 0x00000000l
114- in
115- Array1. set complete_image offset fill_with
116- done ;
117- free_images () ;
118- Osnap_Diff_Png.PngIo. write_png_bigarray
119- output
120- complete_image
121- complete_width
122- complete_height;
118+ Array1. set complete_image offset fill_with
119+ done ;
120+ Osnap_Diff_Png.PngIo. write_png_bigarray
121+ output
122+ complete_image
123+ complete_width
124+ complete_height)
125+ in
123126 Result. error (Pixel (diffCount, diffPercentage))
124127;;
0 commit comments