Add sharpening filter to this implementation too
[zxing.git] / javame / src / com / google / zxing / client / j2me / LCDUIImageMonochromeBitmapSource.java
1 /*
2  * Copyright 2007 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.j2me;
18
19 import com.google.zxing.BlackPointEstimationMethod;
20 import com.google.zxing.MonochromeBitmapSource;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.common.BitArray;
23 import com.google.zxing.common.BlackPointEstimator;
24
25 import javax.microedition.lcdui.Image;
26
27 /**
28  * <p>An implementation based on Java ME's {@link Image} representation.</p>
29  *
30  * @author Sean Owen (srowen@google.com), Daniel Switkin (dswitkin@google.com)
31  */
32 public final class LCDUIImageMonochromeBitmapSource implements MonochromeBitmapSource {
33
34   private final int[] rgbPixels;
35   private final int width;
36   private final int height;
37   private int blackPoint;
38   private BlackPointEstimationMethod lastMethod;
39   private int lastArgument;
40
41   private static final int LUMINANCE_BITS = 5;
42   private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
43   private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
44
45   public LCDUIImageMonochromeBitmapSource(Image image) {
46     width = image.getWidth();
47     height = image.getHeight();
48     rgbPixels = new int[width * height];
49     image.getRGB(rgbPixels, 0, width, 0, 0, width, height);
50     blackPoint = 0x7F;
51     lastMethod = null;
52     lastArgument = 0;
53   }
54
55   public boolean isBlack(int x, int y) {
56     return computeRGBLuminance(rgbPixels[x + y * width]) < blackPoint;
57   }
58
59   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
60     if (row == null || row.getSize() < getWidth) {
61       row = new BitArray(getWidth);
62     } else {
63       row.clear();
64     }
65
66     // If the current decoder calculated the blackPoint based on one row, assume we're trying to
67     // decode a 1D barcode, and apply some sharpening.
68     // TODO: We may want to add a fifth parameter to request the amount of shapening to be done.
69     if (lastMethod.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
70       int offset = y * width + startX;
71       int left = computeRGBLuminance(rgbPixels[offset]);
72       offset++;
73       int center = computeRGBLuminance(rgbPixels[offset]);
74       for (int i = 1; i < getWidth - 1; i++, offset++) {
75         int right = computeRGBLuminance(rgbPixels[offset + 1]);
76         // Simple -1 4 -1 box filter with a weight of 2
77         int luminance = ((center << 2) - left - right) >> 1;
78         if (luminance < blackPoint) {
79           row.set(i);
80         }
81         left = center;
82         center = right;
83       }
84     } else {
85       for (int i = 0, offset = y * width + startX; i < getWidth; i++, offset++) {
86         if (computeRGBLuminance(rgbPixels[offset]) < blackPoint) {
87           row.set(i);
88         }
89       }
90     }
91     return row;
92   }
93
94   public int getHeight() {
95     return height;
96   }
97
98   public int getWidth() {
99     return width;
100   }
101
102   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) throws ReaderException {
103     if (!method.equals(lastMethod) || argument != lastArgument) {
104       int[] histogram = new int[LUMINANCE_BUCKETS];
105       if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
106         int minDimension = width < height ? width : height;
107         for (int n = 0, offset = 0; n < minDimension; n++, offset += width + 1) {
108           histogram[computeRGBLuminance(rgbPixels[offset]) >> LUMINANCE_SHIFT]++;
109         }
110       } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
111         if (argument < 0 || argument >= height) {
112           throw new IllegalArgumentException("Row is not within the image: " + argument);
113         }
114         int offset = argument * width;
115         for (int x = 0; x < width; x++) {
116           histogram[computeRGBLuminance(rgbPixels[offset + x]) >> LUMINANCE_SHIFT]++;
117         }
118       } else {
119         throw new IllegalArgumentException("Unknown method: " + method);
120       }
121       blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
122       lastMethod = method;
123       lastArgument = argument;
124     }
125   }
126
127   public BlackPointEstimationMethod getLastEstimationMethod() {
128     return lastMethod;
129   }
130
131   public MonochromeBitmapSource rotateCounterClockwise() {
132     throw new IllegalStateException("Rotate not supported");
133   }
134
135   public boolean isRotateSupported() {
136     return false;
137   }
138
139   /**
140    * An optimized approximation of a more proper conversion from RGB to luminance which
141    * only uses shifts. See BufferedImageMonochromeBitmapSource for an original version.
142    */
143   private static int computeRGBLuminance(int pixel) {
144     // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
145     // the multiplies can be implemented as shifts.
146     //
147     // Really, it's:
148     //
149     // return ((((pixel >> 16) & 0xFF) << 8) +
150     //         (((pixel >>  8) & 0xFF) << 9) +
151     //         (( pixel        & 0xFF) << 8)) >> 10;
152     //
153     // That is, we're replacing the coefficients in the original with powers of two,
154     // which can be implemented as shifts, even though changing the coefficients slightly
155     // corrupts the conversion. Not significant for our purposes.
156     //
157     // But we can get even cleverer and eliminate a few shifts:
158     return (((pixel & 0x00FF0000) >> 16)  +
159             ((pixel & 0x0000FF00) >>  7) +
160             ( pixel & 0x000000FF       )) >> 2;
161   }
162
163 }