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 class RGBMonochromeBitmapSource extends BaseMonochromeBitmapSource {
29 private byte[] mLuminances;
31 public RGBMonochromeBitmapSource(String path) throws FileNotFoundException {
32 Bitmap bitmap = BitmapFactory.decodeFile(path);
34 throw new FileNotFoundException("Couldn't open " + path);
37 int width = bitmap.getWidth();
38 int height = bitmap.getHeight();
39 int[] pixels = new int[width * height];
40 bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
42 // In order to measure pure decoding speed, we convert the entire image to a greyscale array up
43 // front, which is the same as the Y channel of the YUVMonochromeBitmapSource in the real app.
44 mLuminances = new byte[width * height];
47 for (int y = 0; y < height; y++) {
48 int offset = y * height;
49 for (int x = 0; x < width; x++) {
50 int pixel = pixels[offset + x];
51 int r = (pixel >> 16) & 0xff;
52 int g = (pixel >> 8) & 0xff;
54 if (r == g && g == b) {
55 // Image is already greyscale, so pick any channel
56 mLuminances[offset + x] = (byte) r;
58 // Calculate luminance cheaply, favoring green
59 mLuminances[offset + x] = (byte) ((r + g + g + b) >> 2);
65 public int getHeight() {
69 public int getWidth() {
73 protected int getLuminance(int x, int y) {
74 return mLuminances[y * mWidth + x] & 0xff;
77 protected int[] getLuminanceRow(int y, int[] row) {
79 if (row == null || row.length < width) {
82 int offset = y * width;
83 for (int x = 0; x < width; x++) {
84 row[x] = mLuminances[offset + x] & 0xff;
89 protected int[] getLuminanceColumn(int x, int[] column) {
92 if (column == null || column.length < height) {
93 column = new int[height];
96 for (int y = 0; y < height; y++) {
97 column[y] = mLuminances[offset] & 0xff;