biasTowardsWhite was, embarassingly, not accomplishing anything mathematically. It...
[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.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  * @deprecated
35  */
36 @Deprecated
37 final class YUVMonochromeBitmapSource implements MonochromeBitmapSource {
38
39   private final Bitmap image;
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     this.image = image;
50     blackPoint = 0x7F;
51     lastMethod = null;
52     lastArgument = 0;
53   }
54
55   public boolean isBlack(int x, int y) {
56     return ((image.getPixel(x, y) >> 16) & 0xFF) < blackPoint;
57   }
58
59   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
60     if (row == null) {
61       row = new BitArray(getWidth);
62     } else {
63       row.clear();
64     }
65     int[] pixelRow = new int[getWidth];
66      image.getPixels(pixelRow, 0, getWidth, startX, y, getWidth, 1);
67     for (int i = 0; i < getWidth; i++) {
68       if (((pixelRow[i] >> 16) & 0xFF) < blackPoint) {
69         row.set(i);
70       }
71     }
72     return row;
73   }
74
75   public int getHeight() {
76     return image.height();
77   }
78
79   public int getWidth() {
80     return image.width();
81   }
82
83   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) throws ReaderException {
84     if (!method.equals(lastMethod) || argument != lastArgument) {
85       int width = image.width();
86       int height = image.height();
87       int[] histogram = new int[LUMINANCE_BUCKETS];
88       if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
89         int minDimension = width < height ? width : height;
90         int startI = height == minDimension ? 0 : (height - width) >> 1;
91         int startJ = width == minDimension ? 0 : (width - height) >> 1;
92         for (int n = 0; n < minDimension; n++) {
93           int pixel = image.getPixel(startJ + n, startI + n);
94           histogram[((pixel >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
95         }
96       } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
97         if (argument < 0 || argument >= height) {
98           throw new IllegalArgumentException("Row is not within the image: " + argument);
99         }
100         int[] pixelRow = new int[width];
101         image.getPixels(pixelRow, 0, width, 0, argument, width, 1);
102         for (int x = 0; x < width; x++) {
103           histogram[((pixelRow[x] >> 16) & 0xFF) >> LUMINANCE_SHIFT]++;
104         }
105       } else {
106         throw new IllegalArgumentException("Unknown method: " + method);
107       }
108       blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
109       lastMethod = method;
110       lastArgument = argument;
111     }
112   }
113
114   public BlackPointEstimationMethod getLastEstimationMethod() {
115     return lastMethod;
116   }
117
118   public MonochromeBitmapSource rotateCounterClockwise() {
119     throw new IllegalStateException("Rotate not supported");
120   }
121
122   public boolean isRotateSupported() {
123     return false;
124   }
125
126 }