rxing/binarizer.rs
1/*
2 * Copyright 2009 ZXing authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//package com.google.zxing;
18
19use std::borrow::Cow;
20
21use crate::{
22 common::{BitArray, BitMatrix, LineOrientation, Result},
23 LuminanceSource,
24};
25
26/**
27 * This class hierarchy provides a set of methods to convert luminance data to 1 bit data.
28 * It allows the algorithm to vary polymorphically, for example allowing a very expensive
29 * thresholding technique for servers and a fast one for mobile. It also permits the implementation
30 * to vary, e.g. a JNI version for Android and a Java fallback version for other platforms.
31 *
32 * @author dswitkin@google.com (Daniel Switkin)
33 */
34pub trait Binarizer {
35 //private final LuminanceSource source;
36 //fn new(source:dyn LuminanceSource) -> Self;
37
38 type Source: LuminanceSource;
39
40 fn get_luminance_source(&self) -> &Self::Source;
41
42 /**
43 * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
44 * cached data. Callers should assume this method is expensive and call it as seldom as possible.
45 * This method is intended for decoding 1D barcodes and may choose to apply sharpening.
46 * For callers which only examine one row of pixels at a time, the same BitArray should be reused
47 * and passed in with each call for performance. However it is legal to keep more than one row
48 * at a time if needed.
49 *
50 * @param y The row to fetch, which must be in [0, bitmap height)
51 * @param row An optional preallocated array. If null or too small, it will be ignored.
52 * If used, the Binarizer will call BitArray.clear(). Always use the returned object.
53 * @return The array of bits for this row (true means black).
54 * @throws NotFoundException if row can't be binarized
55 */
56 fn get_black_row(&self, y: usize) -> Result<Cow<'_, BitArray>>;
57
58 // An alternate version of get_black_row that fetches the line from the matrix if
59 // it has already been generated, falling back to get_black_row if it hasn't.
60 fn get_black_row_from_matrix(&self, y: usize) -> Result<Cow<'_, BitArray>>;
61
62 /**
63 * Converts a 2D array of luminance data to 1 bit data. As above, assume this method is expensive
64 * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
65 * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
66 * fetched using getBlackRow(), so don't mix and match between them.
67 *
68 * @return The 2D array of bits for the image (true means black).
69 * @throws NotFoundException if image can't be binarized to make a matrix
70 */
71 fn get_black_matrix(&self) -> Result<&BitMatrix>;
72
73 /// Get a row or column of the image
74 fn get_black_line(&self, l: usize, lt: LineOrientation) -> Result<Cow<'_, BitArray>>;
75
76 /**
77 * Creates a new object with the same type as this Binarizer implementation, but with pristine
78 * state. This is needed because Binarizer implementations may be stateful, e.g. keeping a cache
79 * of 1 bit data. See Effective Java for why we can't use Java's clone() method.
80 *
81 * @param source The LuminanceSource this Binarizer will operate on.
82 * @return A new concrete Binarizer implementation object.
83 */
84 fn create_binarizer(&self, source: Self::Source) -> Self
85 where
86 Self: Sized;
87
88 fn get_width(&self) -> usize;
89
90 fn get_height(&self) -> usize;
91}