3b7626609dd8edaa840e38f7c30fd096be91e657
[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.common.BitArray;
21 import com.google.zxing.common.BlackPointEstimator;
22
23 import javax.microedition.lcdui.Image;
24
25 /**
26  * <p>An implementation based on Java ME's {@link Image} representation.</p>
27  * 
28  * @author Sean Owen (srowen@google.com)
29  */
30 final class LCDUIImageMonochromeBitmapSource implements MonochromeBitmapSource {
31
32   private final int[] rgbPixels;
33   private final int blackPoint;
34   private final int width;
35   private final int height;
36
37   LCDUIImageMonochromeBitmapSource(final Image image) {
38     int width = image.getWidth();
39     int height = image.getHeight();
40     this.width = width;
41     this.height = height;
42     int[] rgbPixels = new int[width * height];
43     this.rgbPixels = rgbPixels;
44     image.getRGB(rgbPixels, 0, width, 0, 0, width, height);
45     int[] luminanceBuckets = new int[32];
46     int minDimension = width < height ? width : height;
47     for (int n = 0, offset = 0; n < minDimension; n++, offset += width + 1) {
48       luminanceBuckets[computeRGBLuminance(rgbPixels[offset]) >> 3]++;
49     }
50     blackPoint = BlackPointEstimator.estimate(luminanceBuckets) << 3;
51   }
52
53   public boolean isBlack(int x, int y) {
54     return computeRGBLuminance(rgbPixels[x + y * width]) < blackPoint;
55   }
56
57   public BitArray getBlackRow(int y, BitArray row, int startX, int getWidth) {
58     if (row == null) {
59       row = new BitArray(getWidth);
60     } else {
61       row.clear();
62     }
63     for (int i = 0, offset = y * width + startX; i < getWidth; i++, offset++) {
64       if (computeRGBLuminance(rgbPixels[offset]) < blackPoint) {
65         row.set(i);
66       }
67     }
68     return row;
69   }
70
71   public int getHeight() {
72     return height;
73   }
74
75   public int getWidth() {
76     return width;
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 }