Biiig standardization of whitespace. 2 space indents now, no tabs.
[zxing.git] / android / src / com / google / zxing / client / android / YUVMonochromeBitmapSource.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.common.BitArray;
23 import com.google.zxing.common.BlackPointEstimator;
24
25 /**
26  * This object implements MonochromeBitmapSource around an Android Bitmap. Rather than capturing an
27  * RGB image and calculating the grey value at each pixel, we ask the camera driver for YUV data and
28  * strip out the luminance channel directly. This should be faster but provides fewer bits, i.e.
29  * fewer grey levels.
30  *
31  * @author dswitkin@google.com (Daniel Switkin)
32  * @author srowen@google.com (Sean Owen)
33  */
34 final class YUVMonochromeBitmapSource implements MonochromeBitmapSource {
35
36   private final Bitmap image;
37   private final BitArray[] blackWhitePixels;
38   private final int width;
39   private final int height;
40   private int blackPoint;
41   private BlackPointEstimationMethod lastMethod;
42   private int lastArgument;
43
44   private static final int LUMINANCE_BITS = 5;
45   private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
46   private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
47
48   YUVMonochromeBitmapSource(Bitmap image) {
49     width = image.width();
50     height = image.height();
51     this.image = image;
52     blackWhitePixels = new BitArray[height];
53     blackPoint = 0x7F;
54     lastMethod = null;
55     lastArgument = 0;
56   }
57
58   public boolean isBlack(int x, int y) {
59     BitArray blackWhite = blackWhitePixels[y];
60     if (blackWhite == null) {
61       blackWhite = parseBlackWhite(y);
62     }
63     return blackWhite.get(x);
64   }
65
66   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
67     BitArray blackWhite = blackWhitePixels[y];
68     if (blackWhite == null) {
69       blackWhite = parseBlackWhite(y);
70     }
71     if (row == null) {
72       if (startX == 0 && getWidth == width) {
73         return blackWhite;
74       }
75       row = new BitArray(getWidth);
76     } else {
77       row.clear();
78     }
79     for (int i = 0; i < getWidth; i++) {
80       if (blackWhite.get(startX + i)) {
81         row.set(i);
82       }
83     }
84     return row;
85   }
86
87   private BitArray parseBlackWhite(int y) {
88     int width = this.width;
89     int[] pixelRow = new int[width];
90     image.getPixels(pixelRow, 0, width, 0, y, width, 1);
91     BitArray luminanceRow = new BitArray(width);
92     int blackPoint = this.blackPoint;
93     // Calculate 32 bits at a time to more efficiently set the bit array
94     int bits = 0;
95     int bitCount = 0;
96     for (int j = 0; j < width; j++) {
97       bits >>>= 1;
98       // Computation of luminance is inlined here for speed:      
99       if (((pixelRow[j] >> 16) & 0xFF) <= blackPoint) {
100         bits |= 0x80000000;
101       }
102       if (++bitCount == 32) {
103         luminanceRow.setBulk(j, bits);
104         bits = 0;
105         bitCount = 0;
106       }
107     }
108     if (bitCount > 0) {
109       luminanceRow.setBulk(width, bits >>> (32 - bitCount));
110     }
111     blackWhitePixels[y] = luminanceRow;
112     return luminanceRow;
113   }
114
115   public int getHeight() {
116     return height;
117   }
118
119   public int getWidth() {
120     return width;
121   }
122
123   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
124     if (!method.equals(lastMethod) || argument != lastArgument) {
125       for (int i = 0; i < blackWhitePixels.length; i++) {
126         blackWhitePixels[i] = null;
127       }
128       int[] histogram = new int[LUMINANCE_BUCKETS];
129       if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
130         int minDimension = width < height ? width : height;
131         int startI = height == minDimension ? 0 : (height - width) >> 1;
132         int startJ = width == minDimension ? 0 : (width - height) >> 1;
133         for (int n = 0; n < minDimension; n++) {
134           int pixel = image.getPixel(startJ + n, startI + n);
135           // Computation of luminance is inlined here for speed:
136           histogram[((pixel >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
137         }
138       } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
139         if (argument < 0 || argument >= height) {
140           throw new IllegalArgumentException("Row is not within the image: " + argument);
141         }
142         int[] yuvArray = new int[width];
143         image.getPixels(yuvArray, 0, width, 0, argument, width, 1);
144         for (int x = 0; x < width; x++) {
145           histogram[((yuvArray[x] >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
146         }
147       } else {
148         throw new IllegalArgumentException("Unknown method: " + method);
149       }
150       blackPoint = BlackPointEstimator.estimate(histogram, 1.0f) << LUMINANCE_SHIFT;
151       lastMethod = method;
152       lastArgument = argument;
153     }
154   }
155
156   public BlackPointEstimationMethod getLastEstimationMethod() {
157     return lastMethod;
158   }
159
160 }