Made sure the BitmapSource subclasses do not reuse a BitArray which is too small.
[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     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       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         int offset = argument * width;
94         for (int x = 0; x < width; x++) {
95           histogram[computeRGBLuminance(rgbPixels[offset + x]) >> LUMINANCE_SHIFT]++;
96         }
97       } else {
98         throw new IllegalArgumentException("Unknown method: " + method);
99       }
100       blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
101       lastMethod = method;
102       lastArgument = argument;
103     }
104   }
105
106   public BlackPointEstimationMethod getLastEstimationMethod() {
107     return lastMethod;
108   }
109
110   public MonochromeBitmapSource rotateCounterClockwise() {
111     throw new IllegalStateException("Rotate not supported");
112   }
113
114   public boolean isRotateSupported() {
115     return false;
116   }
117
118   /**
119    * An optimized approximation of a more proper conversion from RGB to luminance which
120    * only uses shifts. See BufferedImageMonochromeBitmapSource for an original version.
121    */
122   private static int computeRGBLuminance(int pixel) {
123     // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
124     // the multiplies can be implemented as shifts.
125     //
126     // Really, it's:
127     //
128     // return ((((pixel >> 16) & 0xFF) << 8) +
129     //         (((pixel >>  8) & 0xFF) << 9) +
130     //         (( pixel        & 0xFF) << 8)) >> 10;
131     //
132     // That is, we're replacing the coefficients in the original with powers of two,
133     // which can be implemented as shifts, even though changing the coefficients slightly
134     // corrupts the conversion. Not significant for our purposes.
135     //
136     // But we can get even cleverer and eliminate a few shifts:
137     return (((pixel & 0x00FF0000) >> 16)  +
138             ((pixel & 0x0000FF00) >>  7) +
139             ( pixel & 0x000000FF       )) >> 2;
140   }
141
142 }