Add more unit tests for client.result, and more small code tweaks.
[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 int mWidth;
28   private final int mHeight;
29   private final byte[] mLuminances;
30
31   public RGBMonochromeBitmapSource(String path) throws FileNotFoundException {
32     Bitmap bitmap = BitmapFactory.decodeFile(path);
33     if (bitmap == null) {
34       throw new FileNotFoundException("Couldn't open " + path);
35     }
36
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);
41
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];
45     mWidth = width;
46     mHeight = 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;
53         int b = pixel & 0xff;
54         if (r == g && g == b) {
55           // Image is already greyscale, so pick any channel
56           mLuminances[offset + x] = (byte) r;
57         } else {
58           // Calculate luminance cheaply, favoring green
59           mLuminances[offset + x] = (byte) ((r + g + g + b) >> 2);
60         }
61       }
62     }
63   }
64
65   @Override
66   public int getHeight() {
67     return mHeight;
68   }
69
70   @Override
71   public int getWidth() {
72     return mWidth;
73   }
74
75   @Override
76   protected int getLuminance(int x, int y) {
77     return mLuminances[y * mWidth + x] & 0xff;
78   }
79
80   @Override
81   protected int[] getLuminanceRow(int y, int[] row) {
82     int width = mWidth;
83     if (row == null || row.length < width) {
84       row = new int[width];
85     }
86     int offset = y * width;
87     for (int x = 0; x < width; x++) {
88       row[x] = mLuminances[offset + x] & 0xff;
89     }
90     return row;
91   }
92
93   @Override
94   protected int[] getLuminanceColumn(int x, int[] column) {
95     int width = mWidth;
96     int height = mHeight;
97     if (column == null || column.length < height) {
98       column = new int[height];
99     }
100     int offset = x;
101     for (int y = 0; y < height; y++) {
102       column[y] = mLuminances[offset] & 0xff;
103       offset += width;
104     }
105     return column;
106   }
107
108 }