Small tweaks on top of Daniel's excellent refactoring
[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.common.BaseMonochromeBitmapSource;
20
21 import javax.microedition.lcdui.Image;
22
23 /**
24  * <p>An implementation based on Java ME's {@link Image} representation.</p>
25  *
26  * @author Sean Owen (srowen@google.com), Daniel Switkin (dswitkin@google.com)
27  */
28 public final class LCDUIImageMonochromeBitmapSource extends BaseMonochromeBitmapSource {
29
30   private final int[] rgbPixels;
31   private final int width;
32   private final int height;
33
34   public LCDUIImageMonochromeBitmapSource(Image image) {
35     width = image.getWidth();
36     height = image.getHeight();
37     rgbPixels = new int[width * height];
38     image.getRGB(rgbPixels, 0, width, 0, 0, width, height);
39   }
40
41   public int getHeight() {
42     return height;
43   }
44
45   public int getWidth() {
46     return width;
47   }
48
49   public int getLuminance(int x, int y) {
50     int pixel = rgbPixels[y * width + x];
51
52     // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
53     // the multiplies can be implemented as shifts.
54     //
55     // Really, it's:
56     //
57     // return ((((pixel >> 16) & 0xFF) << 8) +
58     //         (((pixel >>  8) & 0xFF) << 9) +
59     //         (( pixel        & 0xFF) << 8)) >> 10;
60     //
61     // That is, we're replacing the coefficients in the original with powers of two,
62     // which can be implemented as shifts, even though changing the coefficients slightly
63     // corrupts the conversion. Not significant for our purposes.
64     return (((pixel & 0x00FF0000) >> 16) +
65             ((pixel & 0x0000FF00) >>  7) +
66              (pixel & 0x000000FF       )) >> 2;
67   }
68
69   // Nothing to do, since we have direct access to the image data.
70   public void cacheRowForLuminance(int y) {
71
72   }
73
74 }