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