479865c911de36b0576de481e74d81974a500ca1
[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.MonochromeBitmapSource;
20 import com.google.zxing.BlackPointEstimationMethod;
21 import com.google.zxing.common.BitArray;
22 import com.google.zxing.common.BlackPointEstimator;
23
24 import java.awt.image.BufferedImage;
25
26 /**
27  * <p>An implementation based upon {@link BufferedImage}. This provides access to the
28  * underlying image as if it were a monochrome image. Behind the scenes, it is evaluating
29  * the luminance of the underlying image by retrieving its pixels' RGB values.</p>
30  * 
31  * @author srowen@google.com (Sean Owen), Daniel Switkin (dswitkin@google.com)
32  */
33 public final class BufferedImageMonochromeBitmapSource implements MonochromeBitmapSource {
34
35   private final BufferedImage image;
36   private int blackPoint;
37   private BlackPointEstimationMethod lastMethod;
38
39   public BufferedImageMonochromeBitmapSource(BufferedImage image) {
40     this.image = image;
41     blackPoint = 0x7F;
42   }
43
44   public boolean isBlack(int x, int y) {
45     return computeRGBLuminance(image.getRGB(x, y)) < blackPoint;
46   }
47
48   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
49     if (row == null) {
50       row = new BitArray(getWidth);
51     } else {
52       row.clear();
53     }
54     int[] pixelRow = image.getRGB(startX, y, getWidth, 1, null, 0, getWidth);
55     for (int i = 0; i < getWidth; i++) {
56       if (computeRGBLuminance(pixelRow[i]) < blackPoint) {
57         row.set(i);
58       }
59     }
60     return row;
61   }
62
63   public int getHeight() {
64     return image.getHeight();
65   }
66
67   public int getWidth() {
68     return image.getWidth();
69   }
70
71   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
72     if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
73       if (!BlackPointEstimationMethod.TWO_D_SAMPLING.equals(lastMethod)) {
74         int width = image.getWidth();
75         int height = image.getHeight();
76         int[] luminanceBuckets = new int[32];
77         int minDimension = width < height ? width : height;
78         int startI = height == minDimension ? 0 : (height - width) >> 1;
79         int startJ = width == minDimension ? 0 : (width - height) >> 1;
80         for (int n = 0; n < minDimension; n++) {
81           int pixel = image.getRGB(startJ + n, startI + n);
82           luminanceBuckets[computeRGBLuminance(pixel) >> 3]++;
83         }
84         blackPoint = BlackPointEstimator.estimate(luminanceBuckets) << 3;
85       }
86     } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
87       // TODO
88     } else {
89       throw new IllegalArgumentException("Unknown method: " + method);
90     }
91     lastMethod = method;
92   }
93
94   public BlackPointEstimationMethod getLastEstimationMethod() {
95     return lastMethod;
96   }
97
98   /**
99    * Extracts luminance from a pixel from this source. By default, the source is assumed to use RGB,
100    * so this implementation computes luminance is a function of a red, green and blue components as
101    * follows:
102    *
103    * <code>Y = 0.299R + 0.587G + 0.114B</code>
104    *
105    * where R, G, and B are values in [0,1].
106    */
107   private static int computeRGBLuminance(int pixel) {
108     // Coefficients add up to 1024 to make the divide into a fast shift
109     return (306 * ((pixel >> 16) & 0xFF) +
110         601 * ((pixel >> 8) & 0xFF) +
111         117 * (pixel & 0xFF)) >> 10;
112   }
113
114 }