2 * Copyright 2007 Google Inc.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.j2me;
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;
25 import javax.microedition.lcdui.Image;
28 * <p>An implementation based on Java ME's {@link Image} representation.</p>
30 * @author Sean Owen (srowen@google.com), Daniel Switkin (dswitkin@google.com)
32 public final class LCDUIImageMonochromeBitmapSource implements MonochromeBitmapSource {
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;
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;
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);
55 public boolean isBlack(int x, int y) {
56 return computeRGBLuminance(rgbPixels[x + y * width]) < blackPoint;
59 public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
61 row = new BitArray(getWidth);
65 for (int i = 0, offset = y * width + startX; i < getWidth; i++, offset++) {
66 if (computeRGBLuminance(rgbPixels[offset]) < blackPoint) {
73 public int getHeight() {
77 public int getWidth() {
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]++;
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);
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]++;
100 throw new IllegalArgumentException("Unknown method: " + method);
102 blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
104 lastArgument = argument;
108 public BlackPointEstimationMethod getLastEstimationMethod() {
112 public MonochromeBitmapSource rotateCounterClockwise() {
113 throw new IllegalStateException("Rotate not supported");
116 public boolean isRotateSupported() {
121 * An optimized approximation of a more proper conversion from RGB to luminance which
122 * only uses shifts. See BufferedImageMonochromeBitmapSource for an original version.
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.
130 // return ((((pixel >> 16) & 0xFF) << 8) +
131 // (((pixel >> 8) & 0xFF) << 9) +
132 // (( pixel & 0xFF) << 8)) >> 10;
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.
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;