Changed the order of the BaseMonochromeBitmapSource constructor arguments to be width...
[zxing.git] / androidtest / src / com / google / zxing / client / androidtest / RGBMonochromeBitmapSource.java
1 /*
2  * Copyright (C) 2008 Google Inc.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package com.google.zxing.client.androidtest;
18
19 import android.graphics.Bitmap;
20 import android.graphics.BitmapFactory;
21 import com.google.zxing.common.BaseMonochromeBitmapSource;
22
23 import java.io.FileNotFoundException;
24
25 public final class RGBMonochromeBitmapSource extends BaseMonochromeBitmapSource {
26
27   private final byte[] mLuminances;
28
29   public RGBMonochromeBitmapSource(String path) throws FileNotFoundException {
30     this(loadBitmap(path));
31   }
32
33   private static Bitmap loadBitmap(String path) throws FileNotFoundException {
34     Bitmap bitmap = BitmapFactory.decodeFile(path);
35     if (bitmap == null) {
36       throw new FileNotFoundException("Couldn't open " + path);
37     }
38     return bitmap;
39   }
40
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);
47
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;
57         int b = pixel & 0xff;
58         if (r == g && g == b) {
59           // Image is already greyscale, so pick any channel
60           mLuminances[offset + x] = (byte) r;
61         } else {
62           // Calculate luminance cheaply, favoring green
63           mLuminances[offset + x] = (byte) ((r + g + g + b) >> 2);
64         }
65       }
66     }
67   }
68
69   @Override
70   protected int getLuminance(int x, int y) {
71     return mLuminances[y * getWidth() + x] & 0xff;
72   }
73
74   @Override
75   protected int[] getLuminanceRow(int y, int[] row) {
76     int width = getWidth();
77     if (row == null || row.length < width) {
78       row = new int[width];
79     }
80     int offset = y * width;
81     for (int x = 0; x < width; x++) {
82       row[x] = mLuminances[offset + x] & 0xff;
83     }
84     return row;
85   }
86
87   @Override
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];
93     }
94     int offset = x;
95     for (int y = 0; y < height; y++) {
96       column[y] = mLuminances[offset] & 0xff;
97       offset += width;
98     }
99     return column;
100   }
101
102 }