1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
* Copyright 2026 RXing authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
use super::HybridBinarizer;
use super::test_utils::MockLuminanceSource;
use crate::Binarizer;
#[test]
fn test_hybrid_binarizer_small_image() {
// Small image should fall back to GlobalHistogramBinarizer
let width = 10;
let height = 10;
let mut luminances = vec![0; width * height];
// Create a simple black and white pattern
for y in 0..height {
for x in 0..width {
if x < 5 {
luminances[y * width + x] = 0; // Black
} else {
luminances[y * width + x] = 255; // White
}
}
}
let source = MockLuminanceSource::new(width, height, luminances);
let binarizer = HybridBinarizer::new(source);
let matrix = binarizer.get_black_matrix().unwrap();
assert_eq!(width as u32, matrix.getWidth());
assert_eq!(height as u32, matrix.getHeight());
for y in 0..height {
for x in 0..width {
if x < 5 {
assert!(matrix.get(x as u32, y as u32));
} else {
assert!(!matrix.get(x as u32, y as u32));
}
}
}
}
#[test]
fn test_hybrid_binarizer_large_image() {
// Large image uses local thresholding
let width = 40; // HybridBinarizer::MINIMUM_DIMENSION is 40
let height = 40;
let mut luminances = vec![0; width * height];
// Create a pattern with a gradient to test local thresholding
for y in 0..height {
for x in 0..width {
if x < 20 {
luminances[y * width + x] = 50; // Dark grey
} else {
luminances[y * width + x] = 200; // Light grey
}
}
}
let source = MockLuminanceSource::new(width, height, luminances);
let binarizer = HybridBinarizer::new(source);
let matrix = binarizer.get_black_matrix().unwrap();
assert_eq!(width as u32, matrix.getWidth());
assert_eq!(height as u32, matrix.getHeight());
for y in 0..height {
for x in 0..width {
if x < 20 {
assert!(
matrix.get(x as u32, y as u32),
"Bit at ({}, {}) should be set",
x,
y
);
} else {
assert!(
!matrix.get(x as u32, y as u32),
"Bit at ({}, {}) should NOT be set",
x,
y
);
}
}
}
}