1a17dbe29dc1c86ca0b18f9361dd61e71f947a7a
[zxing.git] / core / src / com / google / zxing / BinaryBitmap.java
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
19 import com.google.zxing.common.BitArray;
20 import com.google.zxing.common.BitMatrix;
21
22 /**
23  * This class is the core bitmap class used by ZXing to represent 1 bit data. Reader objects
24  * accept a BinaryBitmap and attempt to decode it.
25  *
26  * @author dswitkin@google.com (Daniel Switkin)
27  */
28 public final class BinaryBitmap {
29
30   private final Binarizer binarizer;
31   private BitMatrix matrix;
32
33   public BinaryBitmap(Binarizer binarizer) {
34     if (binarizer == null) {
35       throw new IllegalArgumentException("Binarizer must be non-null.");
36     }
37     this.binarizer = binarizer;
38     matrix = null;
39   }
40
41   /**
42    * @return The width of the bitmap.
43    */
44   public int getWidth() {
45     return binarizer.getLuminanceSource().getWidth();
46   }
47
48   /**
49    * @return The height of the bitmap.
50    */
51   public int getHeight() {
52     return binarizer.getLuminanceSource().getHeight();
53   }
54
55   /**
56    * Converts one row of luminance data to 1 bit data. May actually do the conversion, or return
57    * cached data. Callers should assume this method is expensive and call it as seldom as possible.
58    * This method is intended for decoding 1D barcodes and may choose to apply sharpening.
59    *
60    * @param y The row to fetch, 0 <= y < bitmap height.
61    * @param row An optional preallocated array. If null or too small, it will be ignored.
62    *            If used, the Binarizer will call BitArray.clear(). Always use the returned object.
63    * @return The array of bits for this row (true means black).
64    */
65   public BitArray getBlackRow(int y, BitArray row) throws ReaderException {
66     return binarizer.getBlackRow(y, row);
67   }
68
69   /**
70    * Converts a 2D array of luminance data to 1 bit. As above, assume this method is expensive
71    * and do not call it repeatedly. This method is intended for decoding 2D barcodes and may or
72    * may not apply sharpening. Therefore, a row from this matrix may not be identical to one
73    * fetched using getBlackRow(), so don't mix and match between them.
74    *
75    * @return The 2D array of bits for the image (true means black).
76    */
77   public BitMatrix getBlackMatrix() throws ReaderException {
78     // The matrix is created on demand the first time it is requested, then cached. There are two
79     // reasons for this:
80     // 1. This work will never be done if the caller only installs 1D Reader objects.
81     // 2. This work will only be done once even if the caller installs multiple 2D Readers.
82     if (matrix == null) {
83       matrix = binarizer.getBlackMatrix();
84     }
85     return matrix;
86   }
87
88   /**
89    * @return Whether this bitmap can be cropped.
90    */
91   public boolean isCropSupported() {
92     return binarizer.getLuminanceSource().isCropSupported();
93   }
94
95   /**
96    * Returns a new object with cropped image data. Implementations may keep a reference to the
97    * original data rather than a copy. Only callable if isCropSupported() is true.
98    *
99    * @param left The left coordinate, 0 <= left < getWidth().
100    * @param top The top coordinate, 0 <= top <= getHeight().
101    * @param width The width of the rectangle to crop.
102    * @param height The height of the rectangle to crop.
103    * @return A cropped version of this object.
104    */
105   public BinaryBitmap crop(int left, int top, int width, int height) {
106     LuminanceSource newSource = binarizer.getLuminanceSource().crop(left, top, width, height);
107     return new BinaryBitmap(binarizer.createBinarizer(newSource));
108   }
109
110   /**
111    * @return Whether this bitmap supports counter-clockwise rotation.
112    */
113   public boolean isRotateSupported() {
114     return binarizer.getLuminanceSource().isRotateSupported();
115   }
116
117   /**
118    * Returns a new object with rotated image data. Only callable if isRotateSupported() is true.
119    *
120    * @return A rotated version of this object.
121    */
122   public BinaryBitmap rotateCounterClockwise() {
123     LuminanceSource newSource = binarizer.getLuminanceSource().rotateCounterClockwise();
124     return new BinaryBitmap(binarizer.createBinarizer(newSource));
125   }
126
127   /**
128    * @deprecated
129    *
130    * FIXME: REMOVE!
131    * These three methods are TEMPORARY and should be removed by the end of July 2009.
132    * They are only here so the transition from MonochromeBitmapSource to BinaryBitmap
133    * can be done in stages. We need to go through all the Reader objects and convert
134    * these calls to getBlackRow() and getBlackMatrix() at the top of this file.
135    *
136    * TIP: Replace calls to isBlack() with a single call to getBlackMatrix(), then call
137    * BitMatrix.get(x, y) per pixel.
138    */
139   public boolean isBlack(int x, int y) throws ReaderException {
140     if (matrix == null) {
141       matrix = binarizer.getBlackMatrix();
142     }
143     return matrix.get(x, y);
144   }
145
146   /**
147    * @deprecated
148    *
149    * FIXME: REMOVE!
150    *
151    * TIP: 2D Readers should replace uses of this method with a single call to getBlackMatrix(),
152    * then perform random access on that BitMatrix as needed. The version of getBlackRow() with
153    * two arguments is only meant for 1D Readers, which I've already converted.
154    */
155   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth)
156       throws ReaderException {
157     if (row == null || row.getSize() < getWidth) {
158       row = new BitArray(getWidth);
159     } else {
160       row.clear();
161     }
162
163     if (matrix == null) {
164       matrix = binarizer.getBlackMatrix();
165     }
166     for (int x = 0; x < getWidth; x++) {
167       if (matrix.get(startX + x, y)) {
168         row.set(x);
169       }
170     }
171     return row;
172   }
173
174   /**
175    * @deprecated
176    *
177    * FIXME: REMOVE!
178    *
179    * TIP: Replace calls to getBlackColumn() with a single call to getBlackMatrix(), then
180    * perform random access on that BitMatrix as needed.
181    */
182   public BitArray getBlackColumn(int x, BitArray column, int startY, int getHeight)
183       throws ReaderException {
184     if (column == null || column.getSize() < getHeight) {
185       column = new BitArray(getHeight);
186     } else {
187       column.clear();
188     }
189
190     if (matrix == null) {
191       matrix = binarizer.getBlackMatrix();
192     }
193     for (int y = 0; y < getHeight; y++) {
194       if (matrix.get(x, startY + y)) {
195         column.set(y);
196       }
197     }
198     return column;
199   }
200
201 }