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.MonochromeBitmapSource;
20 import com.google.zxing.BlackPointEstimationMethod;
21 import com.google.zxing.common.BitArray;
22 import com.google.zxing.common.BlackPointEstimator;
24 import javax.microedition.lcdui.Image;
27 * <p>An implementation based on Java ME's {@link Image} representation.</p>
29 * @author Sean Owen (srowen@google.com), Daniel Switkin (dswitkin@google.com)
31 final class LCDUIImageMonochromeBitmapSource implements MonochromeBitmapSource {
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;
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;
44 LCDUIImageMonochromeBitmapSource(final 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);
54 public boolean isBlack(int x, int y) {
55 return computeRGBLuminance(rgbPixels[x + y * width]) < blackPoint;
58 public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
60 row = new BitArray(getWidth);
64 for (int i = 0, offset = y * width + startX; i < getWidth; i++, offset++) {
65 if (computeRGBLuminance(rgbPixels[offset]) < blackPoint) {
72 public int getHeight() {
76 public int getWidth() {
80 public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
81 if (!method.equals(lastMethod) || argument != lastArgument) {
82 int[] histogram = new int[LUMINANCE_BUCKETS];
83 if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
84 int minDimension = width < height ? width : height;
85 for (int n = 0, offset = 0; n < minDimension; n++, offset += width + 1) {
86 histogram[computeRGBLuminance(rgbPixels[offset]) >> LUMINANCE_SHIFT]++;
88 } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
89 if (argument < 0 || argument >= height) {
90 throw new IllegalArgumentException("Row is not within the image: " + argument);
92 int offset = argument * width;
93 for (int x = 0; x < width; x++) {
94 histogram[computeRGBLuminance(rgbPixels[offset + x]) >> LUMINANCE_SHIFT]++;
97 throw new IllegalArgumentException("Unknown method: " + method);
99 blackPoint = BlackPointEstimator.estimate(histogram) << LUMINANCE_SHIFT;
101 lastArgument = argument;
105 public BlackPointEstimationMethod getLastEstimationMethod() {
110 * Extracts luminance from a pixel from this source. By default, the source is assumed to use RGB,
111 * so this implementation computes luminance is a function of a red, green and blue components as
114 * <code>Y = 0.299R + 0.587G + 0.114B</code>
116 * where R, G, and B are values in [0,1].
118 private static int computeRGBLuminance(int pixel) {
119 // Coefficients add up to 1024 to make the divide into a fast shift
120 return (306 * ((pixel >> 16) & 0xFF) +
121 601 * ((pixel >> 8) & 0xFF) +
122 117 * (pixel & 0xFF)) >> 10;