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