Missed one place that needs to cache the luminance data.
[zxing.git] / core / src / com / google / zxing / common / BaseMonochromeBitmapSource.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 package com.google.zxing.common;
17
18 import com.google.zxing.MonochromeBitmapSource;
19 import com.google.zxing.BlackPointEstimationMethod;
20 import com.google.zxing.ReaderException;
21
22 /**
23  * @author dswitkin@google.com (Daniel Switkin)
24  */
25 public abstract class BaseMonochromeBitmapSource implements MonochromeBitmapSource {
26
27   private int blackPoint;
28   private BlackPointEstimationMethod lastMethod;
29   private int lastArgument;
30
31   private static final int LUMINANCE_BITS = 5;
32   private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
33   private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
34
35   public BaseMonochromeBitmapSource() {
36     blackPoint = 0x7F;
37     lastMethod = null;
38     lastArgument = 0;
39   }
40
41   public boolean isBlack(int x, int y) {
42     return getLuminance(x, y) < blackPoint;
43   }
44
45   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
46     if (row == null || row.getSize() < getWidth) {
47       row = new BitArray(getWidth);
48     } else {
49       row.clear();
50     }
51
52     // If the current decoder calculated the blackPoint based on one row, assume we're trying to
53     // decode a 1D barcode, and apply some sharpening.
54     // TODO: We may want to add a fifth parameter to request the amount of shapening to be done.
55     cacheRowForLuminance(y);
56     if (lastMethod == BlackPointEstimationMethod.ROW_SAMPLING) {
57       int left = getLuminance(startX, y);
58       int center = getLuminance(startX + 1, y);
59       for (int x = 1; x < getWidth - 1; x++) {
60         int right = getLuminance(startX + x + 1, y);
61         // Simple -1 4 -1 box filter with a weight of 2
62         int luminance = ((center << 2) - left - right) >> 1;
63         if (luminance < blackPoint) {
64           row.set(x);
65         }
66         left = center;
67         center = right;
68       }
69     } else {
70       for (int x = 0; x < getWidth; x++) {
71         if (getLuminance(startX + x, y) < blackPoint) {
72           row.set(x);
73         }
74       }
75     }
76     return row;
77   }
78
79   public abstract int getHeight();
80   public abstract int getWidth();
81   public abstract int getLuminance(int x, int y);
82   public abstract void cacheRowForLuminance(int y);
83
84   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) throws ReaderException {
85     if (!method.equals(lastMethod) || argument != lastArgument) {
86       int width = getWidth();
87       int height = getHeight();
88       int[] histogram = new int[LUMINANCE_BUCKETS];
89       if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
90         int minDimension = width < height ? width : height;
91         int startX = (width - minDimension) >> 1;
92         int startY = (height - minDimension) >> 1;
93         for (int n = 0; n < minDimension; n++) {
94           int luminance = getLuminance(startX + n, startY + n);
95           histogram[luminance >> LUMINANCE_SHIFT]++;
96         }
97       } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
98         if (argument < 0 || argument >= height) {
99           throw new IllegalArgumentException("Row is not within the image: " + argument);
100         }
101         cacheRowForLuminance(argument);
102         for (int x = 0; x < width; x++) {
103           int luminance = getLuminance(x, argument);
104           histogram[luminance >> LUMINANCE_SHIFT]++;
105         }
106       } else {
107         throw new IllegalArgumentException("Unknown method: " + method);
108       }
109       blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
110       lastMethod = method;
111       lastArgument = argument;
112     }
113   }
114
115   public BlackPointEstimationMethod getLastEstimationMethod() {
116     return lastMethod;
117   }
118
119   public MonochromeBitmapSource rotateCounterClockwise() {
120     throw new IllegalStateException("Rotate not supported");
121   }
122
123   public boolean isRotateSupported() {
124     return false;
125   }
126
127
128 }