Refactored the MonochromeBitmapSource class hierarchy into LuminanceSource, Binarizer...
[zxing.git] / javase / src / com / google / zxing / client / j2se / BufferedImageLuminanceSource.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.j2se;
18
19 import com.google.zxing.LuminanceSource;
20
21 import java.awt.image.BufferedImage;
22 import java.awt.image.BufferedImageOp;
23 import java.awt.image.AffineTransformOp;
24 import java.awt.geom.AffineTransform;
25
26 /**
27  * This LuminanceSource implementation is meant for J2SE clients and our blackbox unit tests.
28  *
29  * @author dswitkin@google.com (Daniel Switkin)
30  * @author Sean Owen
31  */
32 public final class BufferedImageLuminanceSource extends LuminanceSource {
33
34   private final BufferedImage image;
35   private final int left;
36   private final int top;
37   private int[] rgbData;
38
39   public BufferedImageLuminanceSource(BufferedImage image) {
40     this(image, 0, 0, image.getWidth(), image.getHeight());
41   }
42
43   public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width,
44       int height) {
45     super(width, height);
46
47     int sourceWidth = image.getWidth();
48     int sourceHeight = image.getHeight();
49     if (left + width > sourceWidth || top + height > sourceHeight) {
50       throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
51     }
52
53     this.image = image;
54     this.left = left;
55     this.top = top;
56   }
57
58   // These methods use an integer calculation for luminance derived from:
59   // <code>Y = 0.299R + 0.587G + 0.114B</code>
60   public byte[] getRow(int y, byte[] row) {
61     if (y < 0 || y >= getHeight()) {
62       throw new IllegalArgumentException("Requested row is outside the image: " + y);
63     }
64     int width = getWidth();
65     if (row == null || row.length < width) {
66       row = new byte[width];
67     }
68
69     if (rgbData == null || rgbData.length < width) {
70       rgbData = new int[width];
71     }
72     image.getRGB(left, top + y, width, 1, rgbData, 0, image.getWidth());
73     for (int x = 0; x < width; x++) {
74       int pixel = rgbData[x];
75       int luminance = (306 * ((pixel >> 16) & 0xFF) +
76           601 * ((pixel >> 8) & 0xFF) +
77           117 * (pixel & 0xFF)) >> 10;
78       row[x] = (byte) luminance;
79     }
80     return row;
81   }
82
83   public byte[] getMatrix() {
84     int width = getWidth();
85     int height = getHeight();
86     int area = width * height;
87     byte[] matrix = new byte[area];
88
89     int[] rgb = new int[area];
90     image.getRGB(left, top, width, height, rgb, 0, image.getWidth());
91     for (int y = 0; y < height; y++) {
92       int offset = y * width;
93       for (int x = 0; x < width; x++) {
94         int pixel = rgb[offset + x];
95         int luminance = (306 * ((pixel >> 16) & 0xFF) +
96             601 * ((pixel >> 8) & 0xFF) +
97             117 * (pixel & 0xFF)) >> 10;
98         matrix[offset + x] = (byte) luminance;
99       }
100     }
101     return matrix;
102   }
103
104   public boolean isCropSupported() {
105     return true;
106   }
107
108   public LuminanceSource crop(int left, int top, int width, int height) {
109     return new BufferedImageLuminanceSource(image, left, top, width, height);
110   }
111
112   // Can't run AffineTransforms on images of unknown format.
113   public boolean isRotateSupported() {
114     return image.getType() != BufferedImage.TYPE_CUSTOM;
115   }
116
117   public LuminanceSource rotateCounterClockwise() {
118     if (!isRotateSupported()) {
119       throw new IllegalStateException("Rotate not supported");
120     }
121     int sourceWidth = image.getWidth();
122     int sourceHeight = image.getHeight();
123
124     // Rotate 90 degrees counterclockwise.
125     AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
126     BufferedImageOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
127
128     // Note width/height are flipped since we are rotating 90 degrees.
129     BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, image.getType());
130     op.filter(image, rotatedImage);
131
132     // Maintain the cropped region, but rotate it too.
133     int width = getWidth();
134     return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width),
135         getHeight(), width);
136   }
137
138 }