2 * Copyright 2008 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.google.zxing.common;
18 import com.google.zxing.MonochromeBitmapSource;
19 import com.google.zxing.BlackPointEstimationMethod;
20 import com.google.zxing.ReaderException;
23 * @author dswitkin@google.com (Daniel Switkin)
25 public abstract class BaseMonochromeBitmapSource implements MonochromeBitmapSource {
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;
31 private int blackPoint;
32 private BlackPointEstimationMethod lastMethod;
33 private int lastArgument;
34 private int[] luminances;
36 protected BaseMonochromeBitmapSource() {
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];
51 public boolean isBlack(int x, int y) {
52 return getLuminance(x, y) < blackPoint;
55 public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
56 if (row == null || row.getSize() < getWidth) {
57 row = new BitArray(getWidth);
62 // Reuse the same int array each time
64 luminances = getLuminanceRow(y, luminances);
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) {
82 for (int x = 0; x < getWidth; x++) {
83 if (luminances[startX + x] < blackPoint) {
91 public BitArray getBlackColumn(int x, BitArray column, int startY, int getHeight) {
92 if (column == null || column.getSize() < getHeight) {
93 column = new BitArray(getHeight);
98 // Reuse the same int array each time
100 luminances = getLuminanceColumn(x, luminances);
102 // We don't handle "row sampling" specially here
103 for (int y = 0; y < getHeight; y++) {
104 if (luminances[startY + y] < blackPoint) {
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]++;
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);
129 luminances = getLuminanceRow(argument, luminances);
130 for (int x = 0; x < width; x++) {
131 histogram[luminances[x] >> LUMINANCE_SHIFT]++;
134 throw new IllegalArgumentException("Unknown method: " + method);
136 blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
138 lastArgument = argument;
142 public BlackPointEstimationMethod getLastEstimationMethod() {
146 public MonochromeBitmapSource rotateCounterClockwise() {
147 throw new IllegalArgumentException("Rotate not supported");
150 public boolean isRotateSupported() {
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.
158 public abstract int getHeight();
160 public abstract int getWidth();
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.
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.
171 protected abstract int getLuminance(int x, int y);
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.
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.
184 protected abstract int[] getLuminanceRow(int y, int[] row);
187 * The same as getLuminanceRow(), but for columns.
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.
193 protected abstract int[] getLuminanceColumn(int x, int[] column);