mirror of
https://github.com/starovoid/rxing.git
synced 2026-07-26 04:12:34 +00:00
cargo fmt
This commit is contained in:
@@ -14,166 +14,190 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
use crate::{common::BitMatrix, RXingResultPoint, Exceptions, ResultPoint};
|
||||
use crate::{common::BitMatrix, Exceptions, RXingResultPoint, ResultPoint};
|
||||
|
||||
/**
|
||||
* @author Guenther Grau
|
||||
*/
|
||||
#[derive(Clone)]
|
||||
pub struct BoundingBox<'a> {
|
||||
|
||||
image:&'a BitMatrix,
|
||||
topLeft:RXingResultPoint,
|
||||
bottomLeft:RXingResultPoint,
|
||||
topRight:RXingResultPoint,
|
||||
bottomRight:RXingResultPoint,
|
||||
minX:u32,
|
||||
maxX:u32,
|
||||
minY:u32,
|
||||
maxY:u32,
|
||||
image: &'a BitMatrix,
|
||||
topLeft: RXingResultPoint,
|
||||
bottomLeft: RXingResultPoint,
|
||||
topRight: RXingResultPoint,
|
||||
bottomRight: RXingResultPoint,
|
||||
minX: u32,
|
||||
maxX: u32,
|
||||
minY: u32,
|
||||
maxY: u32,
|
||||
}
|
||||
impl<'a> BoundingBox<'_> {
|
||||
pub fn new(
|
||||
image: &'a BitMatrix,
|
||||
topLeft: Option<RXingResultPoint>,
|
||||
bottomLeft: Option<RXingResultPoint>,
|
||||
topRight: Option<RXingResultPoint>,
|
||||
bottomRight: Option<RXingResultPoint>,
|
||||
) -> Result<BoundingBox<'a>, Exceptions> {
|
||||
let leftUnspecified = topLeft.is_none() || bottomLeft.is_none();
|
||||
let rightUnspecified = topRight.is_none() || bottomRight.is_none();
|
||||
if leftUnspecified && rightUnspecified {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()));
|
||||
}
|
||||
|
||||
pub fn new( image:&'a BitMatrix,
|
||||
topLeft:Option<RXingResultPoint>,
|
||||
bottomLeft:Option<RXingResultPoint>,
|
||||
topRight:Option<RXingResultPoint>,
|
||||
bottomRight:Option<RXingResultPoint>) -> Result<BoundingBox<'a>,Exceptions> {
|
||||
let leftUnspecified = topLeft.is_none() || bottomLeft .is_none();
|
||||
let rightUnspecified = topRight .is_none() || bottomRight .is_none();
|
||||
if leftUnspecified && rightUnspecified {
|
||||
return Err(Exceptions::NotFoundException("".to_owned()))
|
||||
let newTopLeft;
|
||||
let newBottomLeft;
|
||||
let newTopRight;
|
||||
let newBottomRight;
|
||||
|
||||
if leftUnspecified {
|
||||
newTopRight = topRight.unwrap();
|
||||
newBottomRight = bottomRight.unwrap();
|
||||
newTopLeft = RXingResultPoint::new(0.0, newTopRight.getY());
|
||||
newBottomLeft = RXingResultPoint::new(0.0, newBottomRight.getY());
|
||||
} else if rightUnspecified {
|
||||
newTopLeft = topLeft.unwrap();
|
||||
newBottomLeft = bottomLeft.unwrap();
|
||||
newTopRight = RXingResultPoint::new(image.getWidth() as f32 - 1.0, newTopLeft.getY());
|
||||
newBottomRight =
|
||||
RXingResultPoint::new(image.getWidth() as f32 - 1.0, newBottomLeft.getY());
|
||||
} else {
|
||||
newTopLeft = topLeft.unwrap();
|
||||
newTopRight = topRight.unwrap();
|
||||
newBottomLeft = bottomLeft.unwrap();
|
||||
newBottomRight = bottomRight.unwrap();
|
||||
}
|
||||
|
||||
Ok(BoundingBox {
|
||||
image,
|
||||
minX: newTopLeft.getX().min(newBottomLeft.getX()) as u32,
|
||||
maxX: newTopRight.getX().max(newBottomRight.getX()) as u32,
|
||||
minY: newTopLeft.getY().min(newTopRight.getY()) as u32,
|
||||
maxY: newBottomLeft.getY().max(newBottomRight.getY()) as u32,
|
||||
topLeft: newTopLeft,
|
||||
bottomLeft: newBottomLeft,
|
||||
topRight: newTopRight,
|
||||
bottomRight: newBottomRight,
|
||||
})
|
||||
}
|
||||
|
||||
let newTopLeft;
|
||||
let newBottomLeft;
|
||||
let newTopRight;
|
||||
let newBottomRight;
|
||||
|
||||
if leftUnspecified {
|
||||
newTopRight = topRight.unwrap();
|
||||
newBottomRight = bottomRight.unwrap();
|
||||
newTopLeft = RXingResultPoint::new(0.0, newTopRight.getY());
|
||||
newBottomLeft = RXingResultPoint::new(0.0, newBottomRight.getY());
|
||||
} else if rightUnspecified {
|
||||
newTopLeft = topLeft.unwrap();
|
||||
newBottomLeft = bottomLeft.unwrap();
|
||||
newTopRight = RXingResultPoint::new(image.getWidth() as f32 - 1.0, newTopLeft.getY());
|
||||
newBottomRight = RXingResultPoint::new(image.getWidth() as f32 - 1.0, newBottomLeft.getY());
|
||||
}else {
|
||||
newTopLeft = topLeft.unwrap();
|
||||
newTopRight = topRight.unwrap();
|
||||
newBottomLeft = bottomLeft.unwrap();
|
||||
newBottomRight = bottomRight.unwrap();
|
||||
pub fn from_other(boundingBox: &'a BoundingBox) -> BoundingBox<'a> {
|
||||
BoundingBox {
|
||||
image: boundingBox.image,
|
||||
topLeft: boundingBox.topLeft,
|
||||
bottomLeft: boundingBox.bottomLeft,
|
||||
topRight: boundingBox.topRight,
|
||||
bottomRight: boundingBox.bottomRight,
|
||||
minX: boundingBox.minX,
|
||||
maxX: boundingBox.maxX,
|
||||
minY: boundingBox.minY,
|
||||
maxY: boundingBox.maxY,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(BoundingBox {
|
||||
image,
|
||||
minX: newTopLeft.getX().min(newBottomLeft.getX()) as u32,
|
||||
maxX: newTopRight.getX().max(newBottomRight.getX()) as u32,
|
||||
minY: newTopLeft.getY().min(newTopRight.getY()) as u32,
|
||||
maxY: newBottomLeft.getY().max(newBottomRight.getY()) as u32,
|
||||
topLeft: newTopLeft,
|
||||
bottomLeft: newBottomLeft,
|
||||
topRight: newTopRight,
|
||||
bottomRight: newBottomRight,
|
||||
})
|
||||
}
|
||||
pub fn merge(
|
||||
leftBox: Option<&'a BoundingBox>,
|
||||
rightBox: Option<&'a BoundingBox>,
|
||||
) -> Result<BoundingBox<'a>, Exceptions> {
|
||||
if leftBox.is_none() {
|
||||
return Ok(rightBox.unwrap().clone());
|
||||
}
|
||||
if rightBox.is_none() {
|
||||
return Ok(leftBox.unwrap().clone());
|
||||
}
|
||||
let leftBox = leftBox.unwrap();
|
||||
let rightBox = rightBox.unwrap();
|
||||
|
||||
pub fn from_other( boundingBox:&'a BoundingBox) -> BoundingBox<'a> {
|
||||
BoundingBox {
|
||||
image: boundingBox.image,
|
||||
topLeft: boundingBox.topLeft,
|
||||
bottomLeft: boundingBox.bottomLeft,
|
||||
topRight: boundingBox.topRight,
|
||||
bottomRight: boundingBox.bottomRight,
|
||||
minX: boundingBox.minX,
|
||||
maxX: boundingBox.maxX,
|
||||
minY: boundingBox.minY,
|
||||
maxY: boundingBox.maxY,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn merge( leftBox:Option<&'a BoundingBox>, rightBox:Option<&'a BoundingBox>) -> Result<BoundingBox<'a>,Exceptions> {
|
||||
if leftBox.is_none() {
|
||||
return Ok(rightBox.unwrap().clone())
|
||||
}
|
||||
if rightBox .is_none() {
|
||||
return Ok(leftBox.unwrap().clone())
|
||||
}
|
||||
let leftBox = leftBox.unwrap();
|
||||
let rightBox = rightBox.unwrap();
|
||||
|
||||
BoundingBox::new(leftBox.image, Some(leftBox.topLeft), Some(leftBox.bottomLeft), Some(rightBox.topRight), Some(rightBox.bottomRight))
|
||||
}
|
||||
|
||||
pub fn addMissingRows(&self, missingStartRows:u32, missingEndRows:u32, isLeft:bool) -> Result<BoundingBox,Exceptions> {
|
||||
let mut newTopLeft = self.topLeft;
|
||||
let mut newBottomLeft = self.bottomLeft;
|
||||
let mut newTopRight = self.topRight;
|
||||
let mut newBottomRight = self.bottomRight;
|
||||
|
||||
if missingStartRows > 0 {
|
||||
let top = if isLeft {self.topLeft} else {self.topRight};
|
||||
let mut newMinY = top.getY() - missingStartRows as f32;
|
||||
if newMinY < 0.0 {
|
||||
newMinY = 0.0;
|
||||
}
|
||||
let newTop = RXingResultPoint::new(top.getX(), newMinY);
|
||||
if isLeft {
|
||||
newTopLeft = newTop;
|
||||
} else {
|
||||
newTopRight = newTop;
|
||||
}
|
||||
BoundingBox::new(
|
||||
leftBox.image,
|
||||
Some(leftBox.topLeft),
|
||||
Some(leftBox.bottomLeft),
|
||||
Some(rightBox.topRight),
|
||||
Some(rightBox.bottomRight),
|
||||
)
|
||||
}
|
||||
|
||||
if missingEndRows > 0 {
|
||||
let bottom = if isLeft { self.bottomLeft} else {self.bottomRight};
|
||||
let mut newMaxY = bottom.getY() as u32 + missingEndRows;
|
||||
if newMaxY >= self.image.getHeight() {
|
||||
newMaxY = self.image.getHeight() - 1;
|
||||
}
|
||||
let newBottom = RXingResultPoint::new(bottom.getX(), newMaxY as f32);
|
||||
if isLeft {
|
||||
newBottomLeft = newBottom;
|
||||
} else {
|
||||
newBottomRight = newBottom;
|
||||
}
|
||||
pub fn addMissingRows(
|
||||
&self,
|
||||
missingStartRows: u32,
|
||||
missingEndRows: u32,
|
||||
isLeft: bool,
|
||||
) -> Result<BoundingBox, Exceptions> {
|
||||
let mut newTopLeft = self.topLeft;
|
||||
let mut newBottomLeft = self.bottomLeft;
|
||||
let mut newTopRight = self.topRight;
|
||||
let mut newBottomRight = self.bottomRight;
|
||||
|
||||
if missingStartRows > 0 {
|
||||
let top = if isLeft { self.topLeft } else { self.topRight };
|
||||
let mut newMinY = top.getY() - missingStartRows as f32;
|
||||
if newMinY < 0.0 {
|
||||
newMinY = 0.0;
|
||||
}
|
||||
let newTop = RXingResultPoint::new(top.getX(), newMinY);
|
||||
if isLeft {
|
||||
newTopLeft = newTop;
|
||||
} else {
|
||||
newTopRight = newTop;
|
||||
}
|
||||
}
|
||||
|
||||
if missingEndRows > 0 {
|
||||
let bottom = if isLeft {
|
||||
self.bottomLeft
|
||||
} else {
|
||||
self.bottomRight
|
||||
};
|
||||
let mut newMaxY = bottom.getY() as u32 + missingEndRows;
|
||||
if newMaxY >= self.image.getHeight() {
|
||||
newMaxY = self.image.getHeight() - 1;
|
||||
}
|
||||
let newBottom = RXingResultPoint::new(bottom.getX(), newMaxY as f32);
|
||||
if isLeft {
|
||||
newBottomLeft = newBottom;
|
||||
} else {
|
||||
newBottomRight = newBottom;
|
||||
}
|
||||
}
|
||||
|
||||
BoundingBox::new(
|
||||
self.image,
|
||||
Some(newTopLeft),
|
||||
Some(newBottomLeft),
|
||||
Some(newTopRight),
|
||||
Some(newBottomRight),
|
||||
)
|
||||
}
|
||||
|
||||
BoundingBox::new(self.image, Some(newTopLeft), Some(newBottomLeft), Some(newTopRight), Some(newBottomRight))
|
||||
}
|
||||
pub fn getMinX(&self) -> u32 {
|
||||
self.minX
|
||||
}
|
||||
|
||||
pub fn getMinX(&self) -> u32{
|
||||
self. minX
|
||||
}
|
||||
pub fn getMaxX(&self) -> u32 {
|
||||
self.maxX
|
||||
}
|
||||
|
||||
pub fn getMaxX(&self) -> u32{
|
||||
self. maxX
|
||||
}
|
||||
pub fn getMinY(&self) -> u32 {
|
||||
self.minY
|
||||
}
|
||||
|
||||
pub fn getMinY(&self) -> u32{
|
||||
self. minY
|
||||
}
|
||||
pub fn getMaxY(&self) -> u32 {
|
||||
self.maxY
|
||||
}
|
||||
|
||||
pub fn getMaxY(&self) -> u32{
|
||||
self. maxY
|
||||
}
|
||||
pub fn getTopLeft(&self) -> &RXingResultPoint {
|
||||
&self.topLeft
|
||||
}
|
||||
|
||||
pub fn getTopLeft(&self) -> &RXingResultPoint{
|
||||
&self. topLeft
|
||||
}
|
||||
pub fn getTopRight(&self) -> &RXingResultPoint {
|
||||
&self.topRight
|
||||
}
|
||||
|
||||
pub fn getTopRight(&self) -> &RXingResultPoint{
|
||||
&self. topRight
|
||||
}
|
||||
|
||||
pub fn getBottomLeft(&self) -> &RXingResultPoint{
|
||||
&self. bottomLeft
|
||||
}
|
||||
|
||||
pub fn getBottomRight(&self) -> &RXingResultPoint{
|
||||
&self. bottomRight
|
||||
}
|
||||
pub fn getBottomLeft(&self) -> &RXingResultPoint {
|
||||
&self.bottomLeft
|
||||
}
|
||||
|
||||
pub fn getBottomRight(&self) -> &RXingResultPoint {
|
||||
&self.bottomRight
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user