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