Did a big refactoring on the MonochromeBitmapSource. I removed all the caching lumina...
[zxing.git] / javase / src / com / google / zxing / client / j2se / BufferedImageMonochromeBitmapSource.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.j2se;
18
19 import com.google.zxing.MonochromeBitmapSource;
20 import com.google.zxing.common.BaseMonochromeBitmapSource;
21
22 import java.awt.geom.AffineTransform;
23 import java.awt.image.AffineTransformOp;
24 import java.awt.image.BufferedImage;
25 import java.awt.image.BufferedImageOp;
26
27 /**
28  * <p>An implementation based upon {@link BufferedImage}. This provides access to the
29  * underlying image as if it were a monochrome image. Behind the scenes, it is evaluating
30  * the luminance of the underlying image by retrieving its pixels' RGB values.</p>
31  *
32  * <p>This may also be used to construct a {@link MonochromeBitmapSource}
33  * based on a region of a {@link BufferedImage}; see
34  * {@link #BufferedImageMonochromeBitmapSource(BufferedImage, int, int, int, int)}.</p>
35  *
36  * @author srowen@google.com (Sean Owen), Daniel Switkin (dswitkin@google.com)
37  */
38 public final class BufferedImageMonochromeBitmapSource extends BaseMonochromeBitmapSource {
39
40   private final BufferedImage image;
41   private final int left;
42   private final int top;
43   private final int width;
44   private final int height;
45
46   /**
47    * Creates an instance that uses the entire given image as a source of pixels to decode.
48    *
49    * @param image image to decode
50    */
51   public BufferedImageMonochromeBitmapSource(BufferedImage image) {
52     this(image, 0, 0, image.getWidth(), image.getHeight());
53   }
54
55   /**
56    * Creates an instance that uses only a region of the given image as a source of pixels to decode.
57    *
58    * @param image image to decode a region of
59    * @param left x coordinate of leftmost pixels to decode
60    * @param top y coordinate of topmost pixels to decode
61    * @param right one more than the x coordinate of rightmost pixels to decode. That is, we will decode
62    *  pixels whose x coordinate is in [left,right)
63    * @param bottom likewise, one more than the y coordinate of the bottommost pixels to decode
64    */
65   public BufferedImageMonochromeBitmapSource(BufferedImage image, int left, int top, int right, int bottom) {
66     this.image = image;
67     int sourceHeight = image.getHeight();
68     int sourceWidth = image.getWidth();
69     if (left < 0 || top < 0 || right > sourceWidth || bottom > sourceHeight || right <= left || bottom <= top) {
70       throw new IllegalArgumentException("Invalid bounds: (" + top + ',' + left + ") (" + right + ',' + bottom + ')');
71     }
72     this.left = left;
73     this.top = top;
74     this.width = right - left;
75     this.height = bottom - top;
76   }
77
78   /**
79    * @return underlying {@link BufferedImage} behind this instance. Note that even if this instance
80    *  only uses a subset of the full image, the returned value here represents the entire backing image.
81    */
82   public BufferedImage getImage() {
83     return image;
84   }
85
86   public int getHeight() {
87     return height;
88   }
89
90   public int getWidth() {
91     return width;
92   }
93
94   @Override
95   public MonochromeBitmapSource rotateCounterClockwise() {
96     if (!isRotateSupported()) {
97       throw new IllegalStateException("Rotate not supported");
98     }
99     int sourceWidth = image.getWidth();
100     int sourceHeight = image.getHeight();
101     // 90 degrees counterclockwise:
102     AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
103     BufferedImageOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
104     // Note width/height are flipped since we are rotating 90 degrees:
105     BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, image.getType());
106     op.filter(image, rotatedImage);
107     return new BufferedImageMonochromeBitmapSource(rotatedImage,
108                                                    top,
109                                                    sourceWidth - (left + width),
110                                                    top + height,
111                                                    sourceWidth - left);
112   }
113
114   @Override
115   public boolean isRotateSupported() {
116     // Can't run AffineTransforms on images of unknown format
117     return image.getType() != BufferedImage.TYPE_CUSTOM;
118   }
119
120   /**
121    * Extracts luminance from a pixel from this source. By default, the source is assumed to use RGB,
122    * so this implementation computes luminance is a function of a red, green and blue components as
123    * follows:
124    *
125    * <code>Y = 0.299R + 0.587G + 0.114B</code>
126    *
127    * where R, G, and B are values in [0,1].
128    */
129   protected int getLuminance(int x, int y) {
130     int pixel = image.getRGB(left + x, top + y);
131     // Coefficients add up to 1024 to make the divide into a fast shift
132     return (306 * ((pixel >> 16) & 0xFF) +
133         601 * ((pixel >> 8) & 0xFF) +
134         117 * (pixel & 0xFF)) >> 10;
135   }
136
137   protected int[] getLuminanceRow(int y, int[] row) {
138     if (row == null || row.length < width) {
139       row = new int[width];
140     }
141     image.getRGB(left, top + y, width, 1, row, 0, width);
142     for (int x = 0; x < width; x++) {
143       int pixel = row[x];
144       row[x] = (306 * ((pixel >> 16) & 0xFF) +
145           601 * ((pixel >> 8) & 0xFF) +
146           117 * (pixel & 0xFF)) >> 10;
147     }
148     return row;
149   }
150
151   protected int[] getLuminanceColumn(int x, int[] column) {
152     if (column == null || column.length < height) {
153       column = new int[height];
154     }
155     image.getRGB(left + x, top, 1, height, column, 0, 1);
156     for (int y = 0; y < height; y++) {
157       int pixel = column[y];
158       column[y] = (306 * ((pixel >> 16) & 0xFF) +
159           601 * ((pixel >> 8) & 0xFF) +
160           117 * (pixel & 0xFF)) >> 10;
161     }
162     return column;
163   }
164
165 }