Added some simple sharpening for 1D decoding which allowed a couple more blackbox...
[zxing.git] / android-m3 / src / com / google / zxing / client / android / RGBMonochromeBitmapSource.java
1 /*
2  * Copyright (C) 2008 Google Inc.
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.client.android;
18
19 import android.graphics.Bitmap;
20 import com.google.zxing.BlackPointEstimationMethod;
21 import com.google.zxing.MonochromeBitmapSource;
22 import com.google.zxing.ReaderException;
23 import com.google.zxing.common.BitArray;
24 import com.google.zxing.common.BlackPointEstimator;
25
26 /**
27  * This object implements MonochromeBitmapSource around an Android Bitmap.
28  *
29  * @author dswitkin@google.com (Daniel Switkin)
30  * @author srowen@google.com (Sean Owen)
31  */
32 final class RGBMonochromeBitmapSource implements MonochromeBitmapSource {
33
34   private final Bitmap image;
35   private int blackPoint;
36   private BlackPointEstimationMethod lastMethod;
37   private int lastArgument;
38
39   private static final int LUMINANCE_BITS = 5;
40   private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
41   private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
42
43   RGBMonochromeBitmapSource(Bitmap image) {
44     this.image = image;
45     blackPoint = 0x7F;
46     lastMethod = null;
47     lastArgument = 0;
48   }
49
50   public boolean isBlack(int x, int y) {
51     return computeRGBLuminance(image.getPixel(x, y)) < blackPoint;
52   }
53
54   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
55     if (row == null || row.getSize() < getWidth) {
56       row = new BitArray(getWidth);
57     } else {
58       row.clear();
59     }
60     int[] pixelRow = new int[getWidth];
61     image.getPixels(pixelRow, 0, getWidth, startX, y, getWidth, 1);
62
63     // If the current decoder calculated the blackPoint based on one row, assume we're trying to
64     // decode a 1D barcode, and apply some sharpening.
65     // TODO: We may want to add a fifth parameter to request the amount of shapening to be done.
66     if (lastMethod == BlackPointEstimationMethod.ROW_SAMPLING) {
67       int left = computeRGBLuminance(pixelRow[0]);
68       int center = computeRGBLuminance(pixelRow[1]);
69       for (int i = 1; i < getWidth - 1; i++) {
70         int right = computeRGBLuminance(pixelRow[i + 1]);
71         // Simple -1 4 -1 box filter with a weight of 2
72         int luminance = ((center << 2) - left - right) >> 1;
73         if (luminance < blackPoint) {
74           row.set(i);
75         }
76         left = center;
77         center = right;
78       }
79     } else {
80       for (int i = 0; i < getWidth; i++) {
81         if (computeRGBLuminance(pixelRow[i]) < blackPoint) {
82           row.set(i);
83         }
84       }
85     }
86     return row;
87   }
88
89   public int getHeight() {
90     return image.height();
91   }
92
93   public int getWidth() {
94     return image.width();
95   }
96
97   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) throws ReaderException {
98     if (!method.equals(lastMethod) || argument != lastArgument) {
99       int width = image.width();
100       int height = image.height();
101       int[] histogram = new int[LUMINANCE_BUCKETS];
102       if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
103         int minDimension = width < height ? width : height;
104         int startI = height == minDimension ? 0 : (height - width) >> 1;
105         int startJ = width == minDimension ? 0 : (width - height) >> 1;
106         for (int n = 0; n < minDimension; n++) {
107           int pixel = image.getPixel(startJ + n, startI + n);
108           histogram[computeRGBLuminance(pixel) >> LUMINANCE_SHIFT]++;
109         }
110       } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
111         if (argument < 0 || argument >= height) {
112           throw new IllegalArgumentException("Row is not within the image: " + argument);
113         }
114         int[] pixelRow = new int[width];
115         image.getPixels(pixelRow, 0, width, 0, argument, width, 1);
116         for (int x = 0; x < width; x++) {
117           histogram[computeRGBLuminance(pixelRow[x]) >> LUMINANCE_SHIFT]++;
118         }
119       } else {
120         throw new IllegalArgumentException("Unknown method: " + method);
121       }
122       blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
123       lastMethod = method;
124       lastArgument = argument;
125     }
126   }
127
128   public BlackPointEstimationMethod getLastEstimationMethod() {
129     return lastMethod;
130   }
131
132   public MonochromeBitmapSource rotateCounterClockwise() {
133     throw new IllegalStateException("Rotate not supported");
134   }
135
136   public boolean isRotateSupported() {
137     return false;
138   }
139
140   /**
141    * An optimized approximation of a more proper conversion from RGB to luminance which
142    * only uses shifts. See BufferedImageMonochromeBitmapSource for an original version.
143    *
144    * @param pixel An ARGB input pixel
145    * @return An eight bit luminance value
146    */
147   private static int computeRGBLuminance(int pixel) {
148     // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
149     // the multiplies can be implemented as shifts.
150     //
151     // Really, it's:
152     //
153     // return ((((pixel >> 16) & 0xFF) << 8) +
154     //         (((pixel >>  8) & 0xFF) << 9) +
155     //         (( pixel        & 0xFF) << 8)) >> 10;
156     //
157     // That is, we're replacing the coefficients in the original with powers of two,
158     // which can be implemented as shifts, even though changing the coefficients slightly
159     // corrupts the conversion. Not significant for our purposes.
160     //
161     // But we can get even cleverer and eliminate a few shifts:
162     return (((pixel & 0x00FF0000) >> 16)  +
163             ((pixel & 0x0000FF00) >>  7) +
164             ( pixel & 0x000000FF       )) >> 2;
165   }
166
167 }