This is the first version of the ZXing Android client.
[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 com.google.zxing.MonochromeBitmapSource;
20 import com.google.zxing.BlackPointEstimationMethod;
21 import com.google.zxing.common.BitArray;
22 import com.google.zxing.common.BlackPointEstimator;
23
24 import android.graphics.Bitmap;
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 YUVMonochromeBitmapSource implements MonochromeBitmapSource {
36
37   private final Bitmap image;
38   private final BitArray[] blackWhitePixels;
39   private final int width;
40   private final int height;
41   private int blackPoint;
42   private BlackPointEstimationMethod lastMethod;
43   private int lastArgument;
44   
45   private static final int LUMINANCE_BITS = 5;
46   private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
47   private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
48
49   YUVMonochromeBitmapSource(Bitmap image) {
50     width = image.width();
51     height = image.height();
52     this.image = image;
53     blackWhitePixels = new BitArray[height];
54     blackPoint = 0x7F;
55     lastMethod = null;
56     lastArgument = 0;
57   }
58
59   public boolean isBlack(int x, int y) {
60     BitArray blackWhite = blackWhitePixels[y];
61     if (blackWhite == null) {
62       blackWhite = parseBlackWhite(y);
63     }
64     return blackWhite.get(x);
65   }
66
67   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
68     BitArray blackWhite = blackWhitePixels[y];
69     if (blackWhite == null) {
70       blackWhite = parseBlackWhite(y);
71     }
72     if (row == null) {
73       if (startX == 0 && getWidth == width) {
74         return blackWhite;
75       }
76       row = new BitArray(getWidth);
77     } else {
78       row.clear();
79     }
80     for (int i = 0; i < getWidth; i++) {
81       if (blackWhite.get(startX + i)) {
82         row.set(i);
83       }
84     }
85     return row;
86   }
87
88   private BitArray parseBlackWhite(int y) {
89     int width = this.width;
90     int[] pixelRow = new int[width];
91     image.getPixels(pixelRow, 0, width, 0, y, width, 1);
92     BitArray luminanceRow = new BitArray(width);
93     int blackPoint = this.blackPoint;
94     // Calculate 32 bits at a time to more efficiently set the bit array
95     int bits = 0;
96     int bitCount = 0;
97     for (int j = 0; j < width; j++) {
98       bits >>>= 1;
99       // Computation of luminance is inlined here for speed:      
100       if (((pixelRow[j] >> 16) & 0xFF) <= blackPoint) {
101         bits |= 0x80000000;
102       }
103       if (++bitCount == 32) {
104         luminanceRow.setBulk(j, bits);
105         bits = 0;
106         bitCount = 0;
107       }
108     }
109     if (bitCount > 0) {
110       luminanceRow.setBulk(width, bits >>> (32 - bitCount));
111     }
112     blackWhitePixels[y] = luminanceRow;
113     return luminanceRow;
114   }
115
116   public int getHeight() {
117     return height;
118   }
119
120   public int getWidth() {
121     return width;
122   }
123
124   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
125     if (!method.equals(lastMethod) || argument != lastArgument) {
126       for (int i = 0; i < blackWhitePixels.length; i++) {
127         blackWhitePixels[i] = null;
128       }
129       int[] histogram = new int[LUMINANCE_BUCKETS];
130       if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
131         int minDimension = width < height ? width : height;
132         int startI = height == minDimension ? 0 : (height - width) >> 1;
133         int startJ = width == minDimension ? 0 : (width - height) >> 1;
134         for (int n = 0; n < minDimension; n++) {
135           int pixel = image.getPixel(startJ + n, startI + n);
136           // Computation of luminance is inlined here for speed:
137           histogram[((pixel >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
138         }
139       } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
140         if (argument < 0 || argument >= height) {
141           throw new IllegalArgumentException("Row is not within the image: " + argument);
142         }
143         int[] yuvArray = new int[width];
144         image.getPixels(yuvArray, 0, width, 0, argument, width, 1);
145         for (int x = 0; x < width; x++) {
146           histogram[((yuvArray[x] >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
147         }
148       } else {
149         throw new IllegalArgumentException("Unknown method: " + method);
150       }
151       blackPoint = BlackPointEstimator.estimate(histogram, 1.0f) << LUMINANCE_SHIFT;
152       lastMethod = method;
153       lastArgument = argument;
154     }
155   }
156
157   public BlackPointEstimationMethod getLastEstimationMethod() {
158     return lastMethod;
159   }
160
161 }