Did a big refactoring on the MonochromeBitmapSource. I removed all the caching lumina...
[zxing.git] / core / src / com / google / zxing / common / BaseMonochromeBitmapSource.java
1 /*
2  * Copyright 2008 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 package com.google.zxing.common;
17
18 import com.google.zxing.MonochromeBitmapSource;
19 import com.google.zxing.BlackPointEstimationMethod;
20 import com.google.zxing.ReaderException;
21
22 /**
23  * @author dswitkin@google.com (Daniel Switkin)
24  */
25 public abstract class BaseMonochromeBitmapSource implements MonochromeBitmapSource {
26
27   private static final int LUMINANCE_BITS = 5;
28   private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
29   private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
30
31   private int blackPoint;
32   private BlackPointEstimationMethod lastMethod;
33   private int lastArgument;
34   private int[] luminances;
35
36   protected BaseMonochromeBitmapSource() {
37     blackPoint = 0x7F;
38     lastMethod = null;
39     lastArgument = 0;
40   }
41
42   private void initLuminances() {
43     if (luminances == null) {
44       int width = getWidth();
45       int height = getHeight();
46       int max = width > height ? width : height;
47       luminances = new int[max];
48     }
49   }
50
51   public boolean isBlack(int x, int y) {
52     return getLuminance(x, y) < blackPoint;
53   }
54
55   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
56     if (row == null || row.getSize() < getWidth) {
57       row = new BitArray(getWidth);
58     } else {
59       row.clear();
60     }
61
62     // Reuse the same int array each time
63     initLuminances();
64     luminances = getLuminanceRow(y, luminances);
65
66     // If the current decoder calculated the blackPoint based on one row, assume we're trying to
67     // decode a 1D barcode, and apply some sharpening.
68     if (lastMethod.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
69       int left = luminances[startX];
70       int center = luminances[startX + 1];
71       for (int x = 1; x < getWidth - 1; x++) {
72         int right = luminances[startX + x + 1];
73         // Simple -1 4 -1 box filter with a weight of 2
74         int luminance = ((center << 2) - left - right) >> 1;
75         if (luminance < blackPoint) {
76           row.set(x);
77         }
78         left = center;
79         center = right;
80       }
81     } else {
82       for (int x = 0; x < getWidth; x++) {
83         if (luminances[startX + x] < blackPoint) {
84           row.set(x);
85         }
86       }
87     }
88     return row;
89   }
90
91   public BitArray getBlackColumn(int x, BitArray column, int startY, int getHeight) {
92     if (column == null || column.getSize() < getHeight) {
93       column = new BitArray(getHeight);
94     } else {
95       column.clear();
96     }
97
98     // Reuse the same int array each time
99     initLuminances();
100     luminances = getLuminanceColumn(x, luminances);
101
102     // We don't handle "row sampling" specially here
103     for (int y = 0; y < getHeight; y++) {
104       if (luminances[startY + y] < blackPoint) {
105         column.set(y);
106       }
107     }
108     return column;
109   }
110
111   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) throws ReaderException {
112     if (!method.equals(lastMethod) || argument != lastArgument) {
113       int width = getWidth();
114       int height = getHeight();
115       int[] histogram = new int[LUMINANCE_BUCKETS];
116       if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
117         int minDimension = width < height ? width : height;
118         int startX = (width - minDimension) >> 1;
119         int startY = (height - minDimension) >> 1;
120         for (int n = 0; n < minDimension; n++) {
121           int luminance = getLuminance(startX + n, startY + n);
122           histogram[luminance >> LUMINANCE_SHIFT]++;
123         }
124       } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
125         if (argument < 0 || argument >= height) {
126           throw new IllegalArgumentException("Row is not within the image: " + argument);
127         }
128         initLuminances();
129         luminances = getLuminanceRow(argument, luminances);
130         for (int x = 0; x < width; x++) {
131           histogram[luminances[x] >> LUMINANCE_SHIFT]++;
132         }
133       } else {
134         throw new IllegalArgumentException("Unknown method: " + method);
135       }
136       blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
137       lastMethod = method;
138       lastArgument = argument;
139     }
140   }
141
142   public BlackPointEstimationMethod getLastEstimationMethod() {
143     return lastMethod;
144   }
145
146   public MonochromeBitmapSource rotateCounterClockwise() {
147     throw new IllegalArgumentException("Rotate not supported");
148   }
149
150   public boolean isRotateSupported() {
151     return false;
152   }
153
154   // These two methods should not need to exist because they are defined in the interface that
155   // this abstract class implements. However this seems to cause problems on some Nokias. 
156   // So we write these redundant declarations.
157
158   public abstract int getHeight();
159
160   public abstract int getWidth();
161
162   /**
163    * Retrieves the luminance at the pixel x,y in the bitmap. This method is only used for estimating
164    * the black point and implementing getBlackRow() - it is not meant for decoding, hence it is not
165    * part of MonochromeBitmapSource itself, and is protected.
166    *
167    * @param x The x coordinate in the image.
168    * @param y The y coordinate in the image.
169    * @return The luminance value between 0 and 255.
170    */
171   protected abstract int getLuminance(int x, int y);
172
173   /**
174    * This is the main mechanism for retrieving luminance data. It is dramatically more efficient
175    * than repeatedly calling getLuminance(). As above, this is not meant for decoders.
176    *
177    * @param y The row to fetch
178    * @param row The array to write luminance values into. It is <b>strongly</b> suggested that you
179    *            allocate this yourself, making sure row.length >= getWidth(), and reuse the same
180    *            array on subsequent calls for performance. If you pass null, you will be flogged,
181    *            but then I will take pity on you and allocate a sufficient array internally.
182    * @return The array containing the luminance data. This is the same as row if it was usable.
183    */
184   protected abstract int[] getLuminanceRow(int y, int[] row);
185
186   /**
187    * The same as getLuminanceRow(), but for columns.
188    *
189    * @param x The column to fetch
190    * @param column The array to write luminance values into. See above.
191    * @return The array containing the luminance data.
192    */
193   protected abstract int[] getLuminanceColumn(int x, int[] column);
194
195 }