Skip to content

Commit a1399b1

Browse files
authored
Merge pull request #85 from JasperDeSutter/optimize
Some memory optimizations
2 parents 413a105 + 586ef4d commit a1399b1

12 files changed

Lines changed: 33 additions & 33 deletions

src/buffered_image_luminance_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ impl LuminanceSource for BufferedImageLuminanceSource {
9191
pixels
9292
}
9393

94-
fn get_matrix(&self) -> Vec<u8> {
94+
fn get_matrix(&self) -> Cow<'_, [u8]> {
9595
// if self.height == self.image.height() as usize && self.width == self.image.width() as usize
9696
// {
97-
self.image.as_bytes().to_vec()
97+
Cow::Borrowed(self.image.as_bytes())
9898
// }
9999
// let skip = self.image.width();
100100
// let row_skip = 0;

src/common/adaptive_threshold_binarizer.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl<LS: LuminanceSource> AdaptiveThresholdBinarizer<LS> {
3232
let Some(buff): Option<ImageBuffer<Luma<u8>, Vec<u8>>> = ImageBuffer::from_vec(
3333
self.source.get_width() as u32,
3434
self.source.get_height() as u32,
35-
self.source.get_matrix(),
35+
self.source.get_matrix().into_owned(),
3636
) else {
3737
return Err(Exceptions::ILLEGAL_ARGUMENT);
3838
};

src/common/bit_matrix.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -792,9 +792,9 @@ impl From<&BitMatrix> for image::DynamicImage {
792792

793793
for (x, y, pixel) in pixels.enumerate_pixels_mut() {
794794
let new_pixel = if value.get(x, y) {
795-
image::Rgb([0, 0, 0])
795+
image::Luma([0])
796796
} else {
797-
image::Rgb([u8::MAX, u8::MAX, u8::MAX])
797+
image::Luma([u8::MAX])
798798
};
799799
*pixel = new_pixel
800800
}

src/common/hybrid_binarizer.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl<LS: LuminanceSource> HybridBinarizer<LS> {
174174
sub_height: u32,
175175
width: u32,
176176
height: u32,
177-
black_points: &[Vec<u32>],
177+
black_points: &[u32],
178178
matrix: &mut BitMatrix,
179179
) {
180180
let maxYOffset = height - BLOCK_SIZE as u32;
@@ -192,7 +192,7 @@ impl<LS: LuminanceSource> HybridBinarizer<LS> {
192192
let mut sum = 0;
193193
for z in -2..=2 {
194194
// for (int z = -2; z <= 2; z++) {
195-
let blackRow = &black_points[(top as i32 + z) as usize];
195+
let blackRow = &black_points[((top as i32 + z) as u32 * sub_width) as usize..];
196196
sum += blackRow[(left - 2) as usize]
197197
+ blackRow[(left - 1) as usize]
198198
+ blackRow[left as usize]
@@ -241,10 +241,10 @@ impl<LS: LuminanceSource> HybridBinarizer<LS> {
241241
subHeight: u32,
242242
width: u32,
243243
height: u32,
244-
) -> Vec<Vec<u32>> {
244+
) -> Vec<u32> {
245245
let maxYOffset = height as usize - BLOCK_SIZE;
246246
let maxXOffset = width as usize - BLOCK_SIZE;
247-
let mut blackPoints = vec![vec![0; subWidth as usize]; subHeight as usize];
247+
let mut blackPoints = vec![0; (subHeight * subWidth) as usize];
248248
for y in 0..subHeight {
249249
// for (int y = 0; y < subHeight; y++) {
250250
let yoffset = u32::min(y << BLOCK_SIZE_POWER, maxYOffset as u32);
@@ -304,17 +304,17 @@ impl<LS: LuminanceSource> HybridBinarizer<LS> {
304304
// the boundaries is used for the interior.
305305

306306
// The (min < bp) is arbitrary but works better than other heuristics that were tried.
307-
let average_neighbor_black_point: u32 = (blackPoints[y as usize - 1]
308-
[x as usize]
309-
+ (2 * blackPoints[y as usize][x as usize - 1])
310-
+ blackPoints[y as usize - 1][x as usize - 1])
307+
let average_neighbor_black_point: u32 = (blackPoints
308+
[(y as usize - 1) * subWidth as usize + x as usize]
309+
+ (2 * blackPoints[y as usize * subWidth as usize + x as usize - 1])
310+
+ blackPoints[(y as usize - 1) * subWidth as usize + x as usize - 1])
311311
/ 4;
312312
if (min as u32) < average_neighbor_black_point {
313313
average = average_neighbor_black_point;
314314
}
315315
}
316316
}
317-
blackPoints[y as usize][x as usize] = average;
317+
blackPoints[(y * subWidth + x) as usize] = average;
318318
}
319319
}
320320
blackPoints

src/common/test_utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ impl LuminanceSource for MockLuminanceSource {
5454
column
5555
}
5656

57-
fn get_matrix(&self) -> Vec<u8> {
58-
self.luminances.clone()
57+
fn get_matrix(&self) -> Cow<'_, [u8]> {
58+
Cow::Borrowed(&self.luminances)
5959
}
6060

6161
fn get_width(&self) -> usize {

src/filtered_image_reader.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ impl<R: Reader> Reader for FilteredImageReader<R> {
3030
) -> crate::common::Result<crate::RXingResult> {
3131
let pyramids = LumImagePyramid::new(
3232
Luma8LuminanceSource::new(
33-
image.get_source().get_matrix(),
33+
image.get_source().get_matrix().into_owned(),
3434
image.get_source().get_width() as u32,
3535
image.get_source().get_height() as u32,
3636
),

src/luma_luma_source.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ impl LuminanceSource for Luma8LuminanceSource {
4646
})
4747
}
4848

49-
fn get_matrix(&self) -> Vec<u8> {
50-
self.data.clone().into()
49+
fn get_matrix(&self) -> Cow<'_, [u8]> {
50+
Cow::Borrowed(&self.data)
5151
}
5252

5353
fn get_width(&self) -> usize {

src/luminance_source.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub trait LuminanceSource {
6161
* larger than width * height bytes on some platforms. Do not modify the contents
6262
* of the result.
6363
*/
64-
fn get_matrix(&self) -> Vec<u8>;
64+
fn get_matrix(&self) -> Cow<'_, [u8]>;
6565

6666
/**
6767
* @return The width of the bitmap.

src/planar_yuv_luminance_source.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl LuminanceSource for PlanarYUVLuminanceSource {
265265
unimplemented!()
266266
}
267267

268-
fn get_matrix(&self) -> Vec<u8> {
268+
fn get_matrix(&self) -> Cow<'_, [u8]> {
269269
let width = self.get_width();
270270
let height = self.get_height();
271271

@@ -276,7 +276,7 @@ impl LuminanceSource for PlanarYUVLuminanceSource {
276276
if self.invert {
277277
v = self.invert_block_of_bytes(v);
278278
}
279-
return v;
279+
return Cow::Owned(v);
280280
}
281281

282282
let area = width * height;
@@ -289,7 +289,7 @@ impl LuminanceSource for PlanarYUVLuminanceSource {
289289
if self.invert {
290290
matrix = self.invert_block_of_bytes(matrix);
291291
}
292-
return matrix;
292+
return Cow::Owned(matrix);
293293
}
294294

295295
// Otherwise copy one cropped row at a time.
@@ -304,7 +304,7 @@ impl LuminanceSource for PlanarYUVLuminanceSource {
304304
matrix = self.invert_block_of_bytes(matrix);
305305
}
306306

307-
matrix
307+
Cow::Owned(matrix)
308308
}
309309

310310
fn get_width(&self) -> usize {

src/rgb_luminance_source.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,11 @@ impl LuminanceSource for RGBLuminanceSource {
6363
unimplemented!()
6464
}
6565

66-
fn get_matrix(&self) -> Vec<u8> {
66+
fn get_matrix(&self) -> Cow<'_, [u8]> {
6767
if self.invert {
68-
self.invert_block_of_bytes(self.luminances.to_vec())
68+
Cow::Owned(self.invert_block_of_bytes(self.luminances.to_vec()))
6969
} else {
70-
self.luminances.to_vec()
70+
Cow::Borrowed(&self.luminances)
7171
}
7272
}
7373

0 commit comments

Comments
 (0)