623e28b4f70e32147f951cca1a95b31a5153ecf5
[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 class RGBMonochromeBitmapSource extends BaseMonochromeBitmapSource {
26
27   private int mWidth;
28   private int mHeight;
29   private 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   public int getHeight() {
66     return mHeight;
67   }
68
69   public int getWidth() {
70     return mWidth;
71   }
72
73   public int getLuminance(int x, int y) {
74     return mLuminances[y * mWidth + x] & 0xff;
75   }
76
77   public void cacheRowForLuminance(int y) {
78
79   }
80
81   public void cacheColumnForLuminance(int x) {
82
83   }
84
85 }