From 963c9da285d654779068f3da0c9a17b261c69046 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 16 Mar 2023 13:32:20 -0500 Subject: [PATCH 1/5] correct Luma8LuminanceSource rotation This addresses #27, it adds a test case to validate square, wide, and tall variants of the rotation matrix. The current implementation is slower and less memor efficient for rectangular matrixes. --- src/luma_luma_source.rs | 97 +++++++++++++++++++++++++++++++++++++---- 1 file changed, 88 insertions(+), 9 deletions(-) diff --git a/src/luma_luma_source.rs b/src/luma_luma_source.rs index 0526a92..bc98443 100644 --- a/src/luma_luma_source.rs +++ b/src/luma_luma_source.rs @@ -98,20 +98,22 @@ impl LuminanceSource for Luma8LuminanceSource { impl Luma8LuminanceSource { fn reverseColumns(&mut self) { - for i in 0..self.get_width() { - let mut j = 0; - let mut k = self.get_width() - 1; - while j < k { - let offset_a = (self.get_width() * i) + j; - let offset_b = (self.get_width() * i) + k; + for col in 0..(self.get_width()) { + let mut a = 0; + let mut b = self.get_height() - 1; + while a < b { + let offset_a = a * self.get_width() + col; + let offset_b = b * self.get_width() + col; self.data.swap(offset_a, offset_b); - j += 1; - k -= 1; + + a += 1; + b -= 1; } } + // print_matrix(&self.data, self.get_width(), self.get_height()); } - fn transpose(&mut self) { + fn transpose_square(&mut self) { for i in 0..self.get_height() { for j in i..self.get_width() { let offset_a = (self.get_width() * i) + j; @@ -120,6 +122,29 @@ impl Luma8LuminanceSource { } } } + + fn transpose_rect(&mut self) { + let mut new_data = vec![0; self.data.len()]; + let new_dim = (self.dimensions.1, self.dimensions.0); + for i in 0..self.get_height() { + for j in 0..self.get_width() { + let offset_a = (self.get_width() * i) + j; + let offset_b = (self.get_height() * j) + i; + new_data[offset_b] = self.data[offset_a]; + } + } + self.data = new_data; + self.dimensions = new_dim + } + + fn transpose(&mut self) { + if self.get_width() == self.get_height() { + self.transpose_square() + } else { + self.transpose_rect() + } + // print_matrix(&self.data, self.get_width(), self.get_height()); + } } impl Luma8LuminanceSource { @@ -142,3 +167,57 @@ impl Luma8LuminanceSource { } } } + +#[cfg(test)] +mod tests { + use crate::{Luma8LuminanceSource, LuminanceSource}; + + #[test] + fn test_rotate() { + let src_square = vec![1, 2, 3, 4, 5, 6, 7, 8, 9]; + + let src_rect = vec![0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0]; + + let square = Luma8LuminanceSource::new(src_square, 3, 3); + let rect_tall = Luma8LuminanceSource::new(src_rect.clone(), 3, 4); + let rect_wide = Luma8LuminanceSource::new(src_rect.clone(), 4, 3); + + let rotated_square = square.rotate_counter_clockwise().expect("rotate"); + // print_matrix(&src_rect, 4, 3); + let rotated_wide_rect = rect_wide.rotate_counter_clockwise().expect("rotate"); + // print_matrix(&src_rect, 3, 4); + let rotated_tall_rect = rect_tall.rotate_counter_clockwise().expect("rotate"); + + assert_eq!(rotated_square.dimensions, square.dimensions); + assert_eq!( + rotated_tall_rect.dimensions, + (rect_tall.dimensions.1, rect_tall.dimensions.0) + ); + assert_eq!( + rotated_wide_rect.dimensions, + (rect_wide.dimensions.1, rect_wide.dimensions.0) + ); + + assert_eq!(rotated_square.data, vec![3, 6, 9, 2, 5, 8, 1, 4, 7]); + + assert_eq!( + rotated_wide_rect.data, + vec![1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1] + ); + + assert_eq!( + rotated_tall_rect.data, + vec![0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0] + ); + } +} + +// fn print_matrix(matrix: &[u8], width: usize, height: usize) { +// for y in 0..height { +// for x in 0..width { +// print!("{}, ",matrix[y*width + x ]); +// } +// println!() +// } +// println!() +// } From d379dc9ded6ba73856ee79abfebd0e79f886dc6b Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 16 Mar 2023 13:37:13 -0500 Subject: [PATCH 2/5] v0.4.0: This is a breaking change --- Cargo.toml | 2 +- README.md | 7 +++++++ crates/cli/Cargo.toml | 2 +- crates/one-d-proc-derive/Cargo.toml | 2 +- 4 files changed, 10 insertions(+), 3 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f01a483..172fc2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rxing" -version = "0.3.2" +version = "0.4.0" description="A rust port of the zxing barcode library." license="Apache-2.0" repository="https://github.com/rxing-core/rxing" diff --git a/README.md b/README.md index b6b02e6..1b993f1 100644 --- a/README.md +++ b/README.md @@ -76,6 +76,13 @@ fn main() { ``` ## Latest Release Notes +* *v0.4.0* -> Rewrite of the API to implement generics. This largely eliminates dynamic dispatch from the library. + + This release has many under-the-hood changes: better Point class, better Error handling, improved API + ergonomics with dynamics. For an understanding of how the new API works check out the `helper` functions. + This release was made possible with PRs from Asha20 and SteveCookTU. A big thanks to them. This release does + not have the improved QRCode support from the ZXing-CPP library, as that port is still in progress. + * *v0.3.1* -> Support for closures in NEEDS_RESULT_CALLBACK. Numerous code cleanups were performed between *v0.3.0* and *v.0.3.1* rxing has moved to https://github.com/rxing-core/rxing. * *v0.2.21* -> Adds partial support for detecting and decoding rotated MaxiCode symbols. Adds support for basic serialization of many public facing datatypes using serde (gated behind `serde` feature). diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index c3a098f..b8bb2a7 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -11,4 +11,4 @@ keywords = ["barcode", "2d_barcode", "1d_barcode", "barcode_reader", "barcode_wr [dependencies] clap = { version = "4.1.1", features = ["derive"] } -rxing = {path = "../../", version = "~0.3.2", features = ["image", "svg_read", "svg_write"] } +rxing = {path = "../../", version = "~0.4.0", features = ["image", "svg_read", "svg_write"] } diff --git a/crates/one-d-proc-derive/Cargo.toml b/crates/one-d-proc-derive/Cargo.toml index ae28a96..8f39b57 100644 --- a/crates/one-d-proc-derive/Cargo.toml +++ b/crates/one-d-proc-derive/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rxing-one-d-proc-derive" repository = "https://github.com/rxing-core/rxing/tree/main/crates/one-d-proc-derive" -version = "0.3.1" +version = "0.3.2" edition = "2021" description = "proc macros to simplify the development of one-d barcode symbologies in rxing (https://github.com/rxing-core/rxing)" license = "Apache-2.0" From 9527bb28f96c84aa8f130da04acb8e8de7214fa4 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 16 Mar 2023 13:39:17 -0500 Subject: [PATCH 3/5] fix versions --- Cargo.toml | 2 +- crates/one-d-proc-derive/Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 172fc2d..0e4cfb2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,7 +25,7 @@ image = {version = "0.24", optional = true} imageproc = {version = "0.23", optional = true} unicode-segmentation = "1.10" codepage-437 = "0.1.0" -rxing-one-d-proc-derive = {version = "0.3", path ="./crates/one-d-proc-derive"} +rxing-one-d-proc-derive = {version = "0.4", path ="./crates/one-d-proc-derive"} num = "0.4.0" svg = {version = "0.13", optional = true} resvg = {version = "0.28.0", optional = true, default-features=false} diff --git a/crates/one-d-proc-derive/Cargo.toml b/crates/one-d-proc-derive/Cargo.toml index 8f39b57..f55fa5a 100644 --- a/crates/one-d-proc-derive/Cargo.toml +++ b/crates/one-d-proc-derive/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "rxing-one-d-proc-derive" repository = "https://github.com/rxing-core/rxing/tree/main/crates/one-d-proc-derive" -version = "0.3.2" +version = "0.4.0" edition = "2021" description = "proc macros to simplify the development of one-d barcode symbologies in rxing (https://github.com/rxing-core/rxing)" license = "Apache-2.0" From 112e20c9c365d4b415715d4ff8bb68bf72d758bb Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 16 Mar 2023 13:41:04 -0500 Subject: [PATCH 4/5] fix version --- crates/cli/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index b8bb2a7..45e3d1f 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rxing-cli" -version = "0.1.12" +version = "0.1.13" edition = "2021" description = "A command line interface for rxing supporting encoding and decoding of multiple barcode formats" license="Apache-2.0" From 4bb228073ae237768eab34d645f87a111a3f4846 Mon Sep 17 00:00:00 2001 From: Henry Schimke Date: Thu, 16 Mar 2023 14:01:31 -0500 Subject: [PATCH 5/5] fix rotation regression for Luma8 part of #27. This is an issue with get_matrix after a rotation. --- Cargo.toml | 2 +- src/luma_luma_source.rs | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 0e4cfb2..072219f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "rxing" -version = "0.4.0" +version = "0.4.1" description="A rust port of the zxing barcode library." license="Apache-2.0" repository="https://github.com/rxing-core/rxing" diff --git a/src/luma_luma_source.rs b/src/luma_luma_source.rs index bc98443..12e4ec4 100644 --- a/src/luma_luma_source.rs +++ b/src/luma_luma_source.rs @@ -134,7 +134,8 @@ impl Luma8LuminanceSource { } } self.data = new_data; - self.dimensions = new_dim + self.dimensions = new_dim; + self.original_dimension = (self.original_dimension.1, self.original_dimension.0); } fn transpose(&mut self) {