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