Major refactoring of 1D barcode code. Moved into com.google.zxing.oned package. Misc...
[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   private int lastArgument;
39   
40   private static final int LUMINANCE_BITS = 5;
41   private static final int LUMINANCE_SHIFT = 8 - LUMINANCE_BITS;
42   private static final int LUMINANCE_BUCKETS = 1 << LUMINANCE_BITS;
43
44   public BufferedImageMonochromeBitmapSource(BufferedImage image) {
45     this.image = image;
46     blackPoint = 0x7F;
47     lastMethod = null;
48     lastArgument = 0;
49   }
50
51   public boolean isBlack(int x, int y) {
52     return computeRGBLuminance(image.getRGB(x, y)) < blackPoint;
53   }
54
55   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
56     if (row == null) {
57       row = new BitArray(getWidth);
58     } else {
59       row.clear();
60     }
61     int[] pixelRow = image.getRGB(startX, y, getWidth, 1, null, 0, getWidth);
62     for (int i = 0; i < getWidth; i++) {
63       if (computeRGBLuminance(pixelRow[i]) < blackPoint) {
64         row.set(i);
65       }
66     }
67     return row;
68   }
69
70   public int getHeight() {
71     return image.getHeight();
72   }
73
74   public int getWidth() {
75     return image.getWidth();
76   }
77
78   public void estimateBlackPoint(BlackPointEstimationMethod method, int argument) {
79     if (!method.equals(lastMethod) || argument != lastArgument) {
80       int width = image.getWidth();
81       int height = image.getHeight();
82       int[] histogram = new int[LUMINANCE_BUCKETS];
83             float biasTowardsWhite = 1.0f;
84       if (method.equals(BlackPointEstimationMethod.TWO_D_SAMPLING)) {
85         int minDimension = width < height ? width : height;
86         int startI = height == minDimension ? 0 : (height - width) >> 1;
87         int startJ = width == minDimension ? 0 : (width - height) >> 1;
88         for (int n = 0; n < minDimension; n++) {
89           int pixel = image.getRGB(startJ + n, startI + n);
90           histogram[computeRGBLuminance(pixel) >> LUMINANCE_SHIFT]++;
91         }
92       } else if (method.equals(BlackPointEstimationMethod.ROW_SAMPLING)) {
93         if (argument < 0 || argument >= height) {
94           throw new IllegalArgumentException("Row is not within the image: " + argument);
95         }
96               biasTowardsWhite = 2.0f;
97         int[] rgbArray = new int[width];
98         image.getRGB(0, argument, width, 1, rgbArray, 0, width);
99         for (int x = 0; x < width; x++) {
100           histogram[computeRGBLuminance(rgbArray[x]) >> LUMINANCE_SHIFT]++;
101         }
102       } else {
103         throw new IllegalArgumentException("Unknown method: " + method);
104       }
105       blackPoint = BlackPointEstimator.estimate(histogram, biasTowardsWhite) << LUMINANCE_SHIFT;
106       lastMethod = method;
107       lastArgument = argument;
108     }
109   }
110
111   public BlackPointEstimationMethod getLastEstimationMethod() {
112     return lastMethod;
113   }
114
115   /**
116    * Extracts luminance from a pixel from this source. By default, the source is assumed to use RGB,
117    * so this implementation computes luminance is a function of a red, green and blue components as
118    * follows:
119    *
120    * <code>Y = 0.299R + 0.587G + 0.114B</code>
121    *
122    * where R, G, and B are values in [0,1].
123    */
124   private static int computeRGBLuminance(int pixel) {
125     // Coefficients add up to 1024 to make the divide into a fast shift
126     return (306 * ((pixel >> 16) & 0xFF) +
127         601 * ((pixel >> 8) & 0xFF) +
128         117 * (pixel & 0xFF)) >> 10;
129   }
130
131 }