Skip to content

Commit 39bfb1f

Browse files
committed
fix: do not generate diff image on retries
1 parent ab4c942 commit 39bfb1f

File tree

3 files changed

+106
-102
lines changed

3 files changed

+106
-102
lines changed

lib/OSnap_Diff/OSnap_Diff.ml

Lines changed: 91 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
~outputDiffMask:true
35+
~threshold:0.1
36+
~failOnLayoutChange:false
37+
~antialiasing:true
38+
~ignoreRegions
39+
~diffPixel
40+
()
3241
in
33-
Diff.diff
34-
original_image
35-
new_image
36-
~outputDiffMask:true
37-
~threshold:0.1
38-
~failOnLayoutChange:false
39-
~antialiasing:true
40-
~ignoreRegions
41-
~diffPixel
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
;;

lib/OSnap_Diff/OSnap_Diff.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ val diff
77
: output:string
88
-> ?ignoreRegions:((int * int) * (int * int)) list
99
-> ?threshold:int
10+
-> generateDiffImage:bool
1011
-> diffPixel:int32
1112
-> original_image_data:string
1213
-> new_image_data:string

lib/OSnap_Test/OSnap_Test.ml

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,13 @@ let run ~env (global_config : Config.Types.global) target test =
183183
then (
184184
Printer.success_message ~name:test.name ~width:test.width ~height:test.height;
185185
Result.ok `Passed)
186-
else
186+
else (
187+
let should_retry =
188+
match test.result with
189+
| None -> Some 1
190+
| Some (`Retry i) when i < test.retry -> Some (succ i)
191+
| _ -> None
192+
in
187193
let*? ignoreRegions =
188194
test.ignore_regions |> get_ignore_regions ~document target test.size_name
189195
in
@@ -195,6 +201,7 @@ let run ~env (global_config : Config.Types.global) target test =
195201
~output:(Eio.Path.native_exn diff_image)
196202
~original_image_data
197203
~new_image_data:screenshot
204+
~generateDiffImage:(Option.is_none should_retry)
198205
in
199206
match diff () with
200207
| Ok () ->
@@ -216,22 +223,15 @@ let run ~env (global_config : Config.Types.global) target test =
216223
let*? () = save_screenshot screenshot ~path:updated_snapshot in
217224
Result.ok (`Failed `Layout)
218225
| Error (Pixel (diffCount, diffPercentage)) ->
219-
(match test.result with
220-
| None ->
226+
(match should_retry with
227+
| Some i ->
221228
Printer.retry_message
222-
~count:1
229+
~count:i
223230
~name:test.name
224231
~width:test.width
225232
~height:test.height;
226-
Result.ok (`Retry 1)
227-
| Some (`Retry i) when i < test.retry ->
228-
Printer.retry_message
229-
~count:(succ i)
230-
~name:test.name
231-
~width:test.width
232-
~height:test.height;
233-
Result.ok (`Retry (succ i))
234-
| _ ->
233+
Result.ok (`Retry i)
234+
| None ->
235235
Printer.diff_message
236236
~print_head:true
237237
~name:test.name
@@ -240,7 +240,7 @@ let run ~env (global_config : Config.Types.global) target test =
240240
~diffCount
241241
~diffPercentage;
242242
let*? () = save_screenshot screenshot ~path:updated_snapshot in
243-
Result.ok (`Failed (`Pixel (diffCount, diffPercentage))))
243+
Result.ok (`Failed (`Pixel (diffCount, diffPercentage)))))
244244
in
245245
{ test with result = Some result } |> Result.ok)
246246
;;

0 commit comments

Comments
 (0)