Fixed bug in rotation code for BufferedImageMonochromeBitmapSource; fixed "SKIP_N_BAR...
[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) {
61       row = new BitArray(getWidth);
62     } else {
63       row.clear();
64     }
65     for (int i = 0, offset = y * width + startX; i < getWidth; i++, offset++) {
66       if (computeRGBLuminance(rgbPixels[offset]) < blackPoint) {
67         row.set(i);
68       }
69     }
70     return row;
71   }
72
73   public int getHeight() {
74     return height;
75   }
76
77   public int getWidth() {
78     return width;
79   }
80
81   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) throws ReaderException {
82     if (!method.equals(lastMethod) || argument != lastArgument) {
83       int[] histogram = new int[LUMINANCE_BUCKETS];
84       float biasTowardsWhite = 1.0f;
85       if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
86         int minDimension = width < height ? width : height;
87         for (int n = 0, offset = 0; n < minDimension; n++, offset += width + 1) {
88           histogram[computeRGBLuminance(rgbPixels[offset]) >> LUMINANCE_SHIFT]++;
89         }
90       } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
91         if (argument < 0 || argument >= height) {
92           throw new IllegalArgumentException("Row is not within the image: " + argument);
93         }
94         biasTowardsWhite = 2.0f;
95         int offset = argument * width;
96         for (int x = 0; x < width; x++) {
97           histogram[computeRGBLuminance(rgbPixels[offset + x]) >> LUMINANCE_SHIFT]++;
98         }
99       } else {
100         throw new IllegalArgumentException("Unknown method: " + method);
101       }
102       blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
103       lastMethod = method;
104       lastArgument = argument;
105     }
106   }
107
108   public BlackPointEstimationMethod getLastEstimationMethod() {
109     return lastMethod;
110   }
111
112   public MonochromeBitmapSource rotateCounterClockwise() {
113     throw new IllegalStateException("Rotate not supported");
114   }
115
116   public boolean isRotateSupported() {
117     return false;
118   }
119
120   /**
121    * An optimized approximation of a more proper conversion from RGB to luminance which
122    * only uses shifts. See BufferedImageMonochromeBitmapSource for an original version.
123    */
124   private static int computeRGBLuminance(int pixel) {
125     // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
126     // the multiplies can be implemented as shifts.
127     //
128     // Really, it's:
129     //
130     // return ((((pixel >> 16) & 0xFF) << 8) +
131     //         (((pixel >>  8) & 0xFF) << 9) +
132     //         (( pixel        & 0xFF) << 8)) >> 10;
133     //
134     // That is, we're replacing the coefficients in the original with powers of two,
135     // which can be implemented as shifts, even though changing the coefficients slightly
136     // corrupts the conversion. Not significant for our purposes.
137     //
138     // But we can get even cleverer and eliminate a few shifts:
139     return (((pixel & 0x00FF0000) >> 8)  +
140             ((pixel & 0x0000FF00) << 1) +
141             ((pixel & 0x000000FF) << 8)) >> 10;
142   }
143
144 }