Skip to content

Commit

Permalink
port fix for luma rotation in #27 into v3 branch
Browse files Browse the repository at this point in the history
  • Loading branch information
hschimke committed Mar 16, 2023
1 parent 24e35f4 commit 50c314c
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 10 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rxing"
version = "0.3.2"
version = "0.3.3"
description="A rust port of the zxing barcode library."
license="Apache-2.0"
repository="https://github.com/rxing-core/rxing"
Expand Down
88 changes: 79 additions & 9 deletions src/luma_luma_source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,20 +104,22 @@ impl LuminanceSource for Luma8LuminanceSource {

impl Luma8LuminanceSource {
fn reverseColumns(&mut self) {
for i in 0..self.getWidth() {
let mut j = 0;
let mut k = self.getWidth() - 1;
while j < k {
let offset_a = (self.getWidth() * i) + j;
let offset_b = (self.getWidth() * i) + k;
for col in 0..(self.getWidth()) {
let mut a = 0;
let mut b = self.getHeight() - 1;
while a < b {
let offset_a = a * self.getWidth() + col;
let offset_b = b * self.getWidth() + 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.getHeight() {
for j in i..self.getWidth() {
let offset_a = (self.getWidth() * i) + j;
Expand All @@ -126,6 +128,30 @@ 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.getHeight() {
for j in 0..self.getWidth() {
let offset_a = (self.getWidth() * i) + j;
let offset_b = (self.getHeight() * j) + i;
new_data[offset_b] = self.data[offset_a];
}
}
self.data = new_data;
self.dimensions = new_dim;
self.original_dimension = (self.original_dimension.1, self.original_dimension.0);
}

fn transpose(&mut self) {
if self.getWidth() == self.getHeight() {
self.transpose_square()
} else {
self.transpose_rect()
}
// print_matrix(&self.data, self.get_width(), self.get_height());
}
}

impl Luma8LuminanceSource {
Expand All @@ -148,3 +174,47 @@ 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.rotateCounterClockwise().expect("rotate");
// print_matrix(&src_rect, 4, 3);
let rotated_wide_rect = rect_wide.rotateCounterClockwise().expect("rotate");
// print_matrix(&src_rect, 3, 4);
let rotated_tall_rect = rect_tall.rotateCounterClockwise().expect("rotate");

assert_eq!((rotated_square.getWidth(),rotated_square.getHeight()), (3,3));
assert_eq!(
(rotated_tall_rect.getWidth(),rotated_tall_rect.getHeight()),
(rect_tall.dimensions.1 as usize, rect_tall.dimensions.0 as usize)
);
assert_eq!(
(rotated_wide_rect.getWidth(),rotated_wide_rect.getHeight()),
(rect_wide.dimensions.1 as usize, rect_wide.dimensions.0 as usize)
);

assert_eq!(rotated_square.getMatrix(), vec![3, 6, 9, 2, 5, 8, 1, 4, 7]);

assert_eq!(
rotated_wide_rect.getMatrix(),
vec![1, 1, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1]
);

assert_eq!(
rotated_tall_rect.getMatrix(),
vec![0, 1, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0]
);
}
}

0 comments on commit 50c314c

Please sign in to comment.