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