2 * Copyright (C) 2008 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.androidtest;
19 import android.graphics.Bitmap;
20 import android.graphics.BitmapFactory;
21 import com.google.zxing.common.BaseMonochromeBitmapSource;
23 import java.io.FileNotFoundException;
25 public final class RGBMonochromeBitmapSource extends BaseMonochromeBitmapSource {
27 private final byte[] mLuminances;
29 public RGBMonochromeBitmapSource(String path) throws FileNotFoundException {
30 this(loadBitmap(path));
33 private static Bitmap loadBitmap(String path) throws FileNotFoundException {
34 Bitmap bitmap = BitmapFactory.decodeFile(path);
36 throw new FileNotFoundException("Couldn't open " + path);
41 public RGBMonochromeBitmapSource(Bitmap bitmap) {
42 super(bitmap.getWidth(), bitmap.getHeight());
43 int width = bitmap.getWidth();
44 int height = bitmap.getHeight();
45 int[] pixels = new int[width * height];
46 bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
48 // In order to measure pure decoding speed, we convert the entire image to a greyscale array up
49 // front, which is the same as the Y channel of the YUVMonochromeBitmapSource in the real app.
50 mLuminances = new byte[width * height];
51 for (int y = 0; y < height; y++) {
52 int offset = y * height;
53 for (int x = 0; x < width; x++) {
54 int pixel = pixels[offset + x];
55 int r = (pixel >> 16) & 0xff;
56 int g = (pixel >> 8) & 0xff;
58 if (r == g && g == b) {
59 // Image is already greyscale, so pick any channel
60 mLuminances[offset + x] = (byte) r;
62 // Calculate luminance cheaply, favoring green
63 mLuminances[offset + x] = (byte) ((r + g + g + b) >> 2);
70 protected int getLuminance(int x, int y) {
71 return mLuminances[y * getWidth() + x] & 0xff;
75 protected int[] getLuminanceRow(int y, int[] row) {
76 int width = getWidth();
77 if (row == null || row.length < width) {
80 int offset = y * width;
81 for (int x = 0; x < width; x++) {
82 row[x] = mLuminances[offset + x] & 0xff;
88 protected int[] getLuminanceColumn(int x, int[] column) {
89 int width = getWidth();
90 int height = getHeight();
91 if (column == null || column.length < height) {
92 column = new int[height];
95 for (int y = 0; y < height; y++) {
96 column[y] = mLuminances[offset] & 0xff;