Made the RGB to luminance approximation/optimization a little faster -- one less...
[zxing.git] / android / 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. Rather than capturing an
28  * RGB image and calculating the grey value at each pixel, we ask the camera driver for YUV data and
29  * strip out the luminance channel directly. This should be faster but provides fewer bits, i.e.
30  * fewer grey levels.
31  *
32  * @author dswitkin@google.com (Daniel Switkin)
33  * @author srowen@google.com (Sean Owen)
34  */
35 final class RGBMonochromeBitmapSource implements MonochromeBitmapSource {
36
37   private final Bitmap image;
38   private int blackPoint;
39   private BlackPointEstimationMethod lastMethod;
40   private int lastArgument;
41
42   private static final int LUMINANCE_BITS = 5;
43   private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
44   private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
45
46   RGBMonochromeBitmapSource(Bitmap image) {
47     this.image = image;
48     blackPoint = 0x7F;
49     lastMethod = null;
50     lastArgument = 0;
51   }
52
53   public boolean isBlack(int x, int y) {
54     return computeRGBLuminance(image.getPixel(x, y)) < blackPoint;
55   }
56
57   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
58     if (row == null) {
59       row = new BitArray(getWidth);
60     } else {
61       row.clear();
62     }
63     int[] pixelRow = new int[getWidth];
64      image.getPixels(pixelRow, 0, getWidth, startX, y, getWidth, 1);
65     for (int i = 0; i < getWidth; i++) {
66       if (computeRGBLuminance(pixelRow[i]) < blackPoint) {
67         row.set(i);
68       }
69     }
70     return row;
71   }
72
73   public int getHeight() {
74     return image.height();
75   }
76
77   public int getWidth() {
78     return image.width();
79   }
80
81   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) throws ReaderException {
82     if (!method.equals(lastMethod) || argument != lastArgument) {
83       int width = image.width();
84       int height = image.height();
85       int[] histogram = new int[LUMINANCE_BUCKETS];
86       if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
87         int minDimension = width < height ? width : height;
88         int startI = height == minDimension ? 0 : (height - width) >> 1;
89         int startJ = width == minDimension ? 0 : (width - height) >> 1;
90         for (int n = 0; n < minDimension; n++) {
91           int pixel = image.getPixel(startJ + n, startI + n);
92           histogram[computeRGBLuminance(pixel) >> LUMINANCE_SHIFT]++;
93         }
94       } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
95         if (argument < 0 || argument >= height) {
96           throw new IllegalArgumentException("Row is not within the image: " + argument);
97         }
98         int[] pixelRow = new int[width];
99         image.getPixels(pixelRow, 0, width, 0, argument, width, 1);
100         for (int x = 0; x < width; x++) {
101           histogram[computeRGBLuminance(pixelRow[x]) >> LUMINANCE_SHIFT]++;
102         }
103       } else {
104         throw new IllegalArgumentException("Unknown method: " + method);
105       }
106       blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
107       lastMethod = method;
108       lastArgument = argument;
109     }
110   }
111
112   public BlackPointEstimationMethod getLastEstimationMethod() {
113     return lastMethod;
114   }
115
116   public MonochromeBitmapSource rotateCounterClockwise() {
117     throw new IllegalStateException("Rotate not supported");
118   }
119
120   public boolean isRotateSupported() {
121     return false;
122   }
123
124   /**
125    * An optimized approximation of a more proper conversion from RGB to luminance which
126    * only uses shifts. See BufferedImageMonochromeBitmapSource for an original version.
127    */
128   private static int computeRGBLuminance(int pixel) {
129     // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
130     // the multiplies can be implemented as shifts.
131     //
132     // Really, it's:
133     //
134     // return ((((pixel >> 16) & 0xFF) << 8) +
135     //         (((pixel >>  8) & 0xFF) << 9) +
136     //         (( pixel        & 0xFF) << 8)) >> 10;
137     //
138     // That is, we're replacing the coefficients in the original with powers of two,
139     // which can be implemented as shifts, even though changing the coefficients slightly
140     // corrupts the conversion. Not significant for our purposes.
141     //
142     // But we can get even cleverer and eliminate a few shifts:
143     return (((pixel & 0x00FF0000) >> 16)  +
144             ((pixel & 0x0000FF00) >>  7) +
145             ( pixel & 0x000000FF       )) >> 2;
146   }
147
148 }