Workaround (I think) for bizarre array corruption problem on Sun WTK and some SE...
[zxing.git] / javame / src / com / google / zxing / client / j2me / LCDUIImageMonochromeBitmapSource.java
1 /*
2  * Copyright 2007 ZXing authors
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 Image image;
31   private final int height;
32   private final int width;
33   // For why this isn't final, see below
34   private int[] rgbRow;
35   private final int[] pixelHolder;
36   private int cachedRow;
37
38   public LCDUIImageMonochromeBitmapSource(Image image) {
39     this.image = image;
40     height = image.getHeight();
41     width = image.getWidth();
42     rgbRow = new int[width];
43     pixelHolder = new int[1];
44     cachedRow = -1;
45   }
46
47   public int getHeight() {
48     return height;
49   }
50
51   public int getWidth() {
52     return width;
53   }
54
55   public int getLuminance(int x, int y) {
56
57     // Below, why the check for rgbRow being the right size? it should never change size
58     // or need to be reallocated. But bizarrely we have seen a but on Sun's WTK, and on
59     // some phones, where the array becomes zero-sized somehow. So we keep making sure the
60     // array is OK.
61     int pixel;
62     if (cachedRow == y && rgbRow.length == width) {
63       pixel = rgbRow[x];
64     } else {
65       image.getRGB(pixelHolder, 0, width, x, y, 1, 1);
66       pixel = pixelHolder[0];
67     }
68
69     // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
70     // the multiplies can be implemented as shifts.
71     //
72     // Really, it's:
73     //
74     // return ((((pixel >> 16) & 0xFF) << 8) +
75     //         (((pixel >>  8) & 0xFF) << 9) +
76     //         (( pixel        & 0xFF) << 8)) >> 10;
77     //
78     // That is, we're replacing the coefficients in the original with powers of two,
79     // which can be implemented as shifts, even though changing the coefficients slightly
80     // corrupts the conversion. Not significant for our purposes.
81     return (((pixel & 0x00FF0000) >> 16) +
82             ((pixel & 0x0000FF00) >>  7) +
83              (pixel & 0x000000FF       )) >> 2;
84   }
85
86   public void cacheRowForLuminance(int y) {
87     if (y != cachedRow) {
88       // See explanation above
89       if (rgbRow.length != width) {
90         rgbRow = new int[width];
91       }
92       image.getRGB(rgbRow, 0, width, 0, y, width, 1);
93       cachedRow = y;
94     }
95   }
96
97 }