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