2 * Copyright 2007 ZXing authors
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.common.BaseMonochromeBitmapSource;
21 import javax.microedition.lcdui.Image;
24 * <p>An implementation based on Java ME's {@link Image} representation.</p>
27 * @author Daniel Switkin (dswitkin@google.com)
29 public final class LCDUIImageMonochromeBitmapSource extends BaseMonochromeBitmapSource {
31 private final Image image;
32 private final int[] pixelHolder;
34 public LCDUIImageMonochromeBitmapSource(Image image) {
35 super(image.getWidth(), image.getHeight());
37 pixelHolder = new int[1];
40 // This is expensive and should be used very sparingly.
41 protected int getLuminance(int x, int y) {
42 image.getRGB(pixelHolder, 0, getWidth(), x, y, 1, 1);
43 int pixel = pixelHolder[0];
45 // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
46 // the multiplies can be implemented as shifts.
50 // return ((((pixel >> 16) & 0xFF) << 8) +
51 // (((pixel >> 8) & 0xFF) << 9) +
52 // (( pixel & 0xFF) << 8)) >> 10;
54 // That is, we're replacing the coefficients in the original with powers of two,
55 // which can be implemented as shifts, even though changing the coefficients slightly
56 // corrupts the conversion. Not significant for our purposes.
57 return (((pixel & 0x00FF0000) >> 16) +
58 ((pixel & 0x0000FF00) >> 7) +
59 (pixel & 0x000000FF )) >> 2;
62 // For efficiency, the RGB data and the luminance data share the same array.
63 protected int[] getLuminanceRow(int y, int[] row) {
64 int width = getWidth();
65 if (row == null || row.length < width) {
68 image.getRGB(row, 0, width, 0, y, width, 1);
69 for (int x = 0; x < width; x++) {
71 row[x] = (((pixel & 0x00FF0000) >> 16) +
72 ((pixel & 0x0000FF00) >> 7) +
73 (pixel & 0x000000FF )) >> 2;
78 protected int[] getLuminanceColumn(int x, int[] column) {
79 int height = getHeight();
80 if (column == null || column.length < height) {
81 column = new int[height];
83 image.getRGB(column, 0, 1, x, 0, 1, height);
84 for (int y = 0; y < height; y++) {
85 int pixel = column[y];
86 column[y] = (((pixel & 0x00FF0000) >> 16) +
87 ((pixel & 0x0000FF00) >> 7) +
88 (pixel & 0x000000FF )) >> 2;