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