Fixed bug in rotation code for BufferedImageMonochromeBitmapSource; fixed "SKIP_N_BAR...
[zxing.git] / javase / src / com / google / zxing / client / j2se / BufferedImageMonochromeBitmapSource.java
1 /*
2  * Copyright 2007 Google Inc.
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.BlackPointEstimationMethod;
20 import com.google.zxing.MonochromeBitmapSource;
21 import com.google.zxing.ReaderException;
22 import com.google.zxing.common.BitArray;
23 import com.google.zxing.common.BlackPointEstimator;
24
25 import java.awt.geom.AffineTransform;
26 import java.awt.image.AffineTransformOp;
27 import java.awt.image.BufferedImage;
28 import java.awt.image.BufferedImageOp;
29
30 /**
31  * <p>An implementation based upon {@link BufferedImage}. This provides access to the
32  * underlying image as if it were a monochrome image. Behind the scenes, it is evaluating
33  * the luminance of the underlying image by retrieving its pixels' RGB values.</p>
34  *
35  * @author srowen@google.com (Sean Owen), Daniel Switkin (dswitkin@google.com)
36  */
37 public final class BufferedImageMonochromeBitmapSource implements MonochromeBitmapSource {
38
39   private final BufferedImage image;
40   private int blackPoint;
41   private BlackPointEstimationMethod lastMethod;
42   private int lastArgument;
43
44   private static final int LUMINANCE_BITS = 5;
45   private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
46   private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
47
48   public BufferedImageMonochromeBitmapSource(BufferedImage image) {
49     this.image = image;
50     blackPoint = 0x7F;
51     lastMethod = null;
52     lastArgument = 0;
53   }
54
55   public BufferedImage getImage() {
56     return image;
57   }
58
59   public boolean isBlack(int x, int y) {
60     return computeRGBLuminance(image.getRGB(x, y)) < blackPoint;
61   }
62
63   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
64     if (row == null) {
65       row = new BitArray(getWidth);
66     } else {
67       row.clear();
68     }
69     int[] pixelRow = image.getRGB(startX, y, getWidth, 1, null, 0, getWidth);
70     for (int i = 0; i < getWidth; i++) {
71       if (computeRGBLuminance(pixelRow[i]) < blackPoint) {
72         row.set(i);
73       }
74     }
75     return row;
76   }
77
78   public int getHeight() {
79     return image.getHeight();
80   }
81
82   public int getWidth() {
83     return image.getWidth();
84   }
85
86   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) throws ReaderException {
87     if (!method.equals(lastMethod) || argument != lastArgument) {
88       int width = image.getWidth();
89       int height = image.getHeight();
90       int[] histogram = new int[LUMINANCE_BUCKETS];
91       float biasTowardsWhite = 1.0f;
92       if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
93         int minDimension = width < height ? width : height;
94         int startI = height == minDimension ? 0 : (height - width) >> 1;
95         int startJ = width == minDimension ? 0 : (width - height) >> 1;
96         for (int n = 0; n < minDimension; n++) {
97           int pixel = image.getRGB(startJ + n, startI + n);
98           histogram[computeRGBLuminance(pixel) >> LUMINANCE_SHIFT]++;
99         }
100       } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
101         if (argument < 0 || argument >= height) {
102           throw new IllegalArgumentException("Row is not within the image: " + argument);
103         }
104         biasTowardsWhite = 2.0f;
105         int[] rgbArray = new int[width];
106         image.getRGB(0, argument, width, 1, rgbArray, 0, width);
107         for (int x = 0; x < width; x++) {
108           histogram[computeRGBLuminance(rgbArray[x]) >> LUMINANCE_SHIFT]++;
109         }
110       } else {
111         throw new IllegalArgumentException("Unknown method: " + method);
112       }
113       blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
114       lastMethod = method;
115       lastArgument = argument;
116     }
117   }
118
119   public BlackPointEstimationMethod getLastEstimationMethod() {
120     return lastMethod;
121   }
122
123   public MonochromeBitmapSource rotateCounterClockwise() {
124     if (!isRotateSupported()) {
125       throw new IllegalStateException("Rotate not supported");
126     }
127     // 90 degrees counterclockwise:
128     AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, image.getWidth());
129     BufferedImageOp op = new AffineTransformOp(transform, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
130     // Note width/height are flipped since we are rotating 90 degrees:
131     BufferedImage rotatedImage = new BufferedImage(image.getHeight(), image.getWidth(), image.getType());
132     op.filter(image, rotatedImage);
133     return new BufferedImageMonochromeBitmapSource(rotatedImage);
134   }
135
136   public boolean isRotateSupported() {
137     // Can't run AffineTransforms on images of unknown format
138     return image.getType() != BufferedImage.TYPE_CUSTOM;
139   }
140
141   /**
142    * Extracts luminance from a pixel from this source. By default, the source is assumed to use RGB,
143    * so this implementation computes luminance is a function of a red, green and blue components as
144    * follows:
145    *
146    * <code>Y = 0.299R + 0.587G + 0.114B</code>
147    *
148    * where R, G, and B are values in [0,1].
149    */
150   private static int computeRGBLuminance(int pixel) {
151     // Coefficients add up to 1024 to make the divide into a fast shift
152     return (306 * ((pixel >> 16) & 0xFF) +
153         601 * ((pixel >> 8) & 0xFF) +
154         117 * (pixel & 0xFF)) >> 10;
155   }
156
157 }