Refactored the MonochromeBitmapSource class hierarchy into LuminanceSource, Binarizer...
[zxing.git] / bug / src / com / google / zxing / client / bug / AWTImageLuminanceSource.java
1 /*
2  * Copyright 2009 ZXing authors
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.bug;
18
19 import com.google.zxing.LuminanceSource;
20 import com.google.zxing.ReaderException;
21
22 import java.awt.Image;
23 import java.awt.image.PixelGrabber;
24
25 /**
26  * An implementation based on AWT's Image representation. This can be used on CDC devices
27  * or other devices that do not have access to the Mobile Information Device Profile
28  * and thus do not have access to javax.microedition.lcdui.Image.
29  *
30  * @author dswitkin@google.com (Daniel Switkin)
31  * @author David Albert
32  * @author Sean Owen
33  */
34 public final class AWTImageLuminanceSource extends LuminanceSource {
35
36   private final int[] pixels;
37
38   public AWTImageLuminanceSource(Image image) throws ReaderException {
39     super(image.getWidth(null), image.getHeight(null));
40
41     int width = getWidth();
42     int height = getHeight();
43     pixels = new int[width * height];
44
45     // Seems best in this situation to grab all pixels upfront. Grabbing any individual pixel
46     // entails creating a relatively expensive object and calling through several methods.
47     PixelGrabber grabber = new PixelGrabber(image, 0, 0, width, height, pixels, 0, width);
48     try {
49       grabber.grabPixels();
50     } catch (InterruptedException ie) {
51       throw ReaderException.getInstance();
52     }
53   }
54
55   public byte[] getRow(int y, byte[] row) {
56     if (y < 0 || y >= getHeight()) {
57       throw new IllegalArgumentException("Requested row is outside the image: " + y);
58     }
59     int width = getWidth();
60     if (row == null || row.length < width) {
61       row = new byte[width];
62     }
63
64     int offset = y * width;
65     for (int x = 0; x < width; x++) {
66       int pixel = pixels[offset + x];
67       int luminance = (((pixel & 0x00FF0000) >> 16) +
68                        ((pixel & 0x0000FF00) >>  7) +
69                         (pixel & 0x000000FF       )) >> 2;
70       row[x] = (byte) luminance;
71     }
72     return row;
73   }
74
75   public byte[] getMatrix() {
76     int width = getWidth();
77     int height = getHeight();
78     int area = width * height;
79     byte[] matrix = new byte[area];
80
81     for (int y = 0; y < height; y++) {
82       int offset = y * width;
83       for (int x = 0; x < width; x++) {
84         int pixel = pixels[offset + x];
85         int luminance = (((pixel & 0x00FF0000) >> 16) +
86                          ((pixel & 0x0000FF00) >>  7) +
87                           (pixel & 0x000000FF       )) >> 2;
88         matrix[x] = (byte) luminance;
89       }
90     }
91     return matrix;
92   }
93
94 }