b65b715939974329f069627251ad0a76f92b5dbc
[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
27  * @author Daniel Switkin (dswitkin@google.com)
28  */
29 public final class LCDUIImageMonochromeBitmapSource extends BaseMonochromeBitmapSource {
30
31   private final Image image;
32   private final int[] pixelHolder;
33
34   public LCDUIImageMonochromeBitmapSource(Image image) {
35     super(image.getWidth(), image.getHeight());
36     this.image = image;
37     pixelHolder = new int[1];
38   }
39
40   public final int getHeight() {
41     return image.getHeight();
42   }
43
44   public final int getWidth() {
45     return image.getWidth();
46   }
47
48   // This is expensive and should be used very sparingly.
49   protected int getLuminance(int x, int y) {
50     image.getRGB(pixelHolder, 0, getWidth(), x, y, 1, 1);
51     int pixel = pixelHolder[0];
52
53     // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
54     // the multiplies can be implemented as shifts.
55     //
56     // Really, it's:
57     //
58     // return ((((pixel >> 16) & 0xFF) << 8) +
59     //         (((pixel >>  8) & 0xFF) << 9) +
60     //         (( pixel        & 0xFF) << 8)) >> 10;
61     //
62     // That is, we're replacing the coefficients in the original with powers of two,
63     // which can be implemented as shifts, even though changing the coefficients slightly
64     // corrupts the conversion. Not significant for our purposes.
65     return (((pixel & 0x00FF0000) >> 16) +
66             ((pixel & 0x0000FF00) >>  7) +
67              (pixel & 0x000000FF       )) >> 2;
68   }
69
70   // For efficiency, the RGB data and the luminance data share the same array.
71   protected int[] getLuminanceRow(int y, int[] row) {
72     int width = getWidth();
73     if (row == null || row.length < width) {
74       row = new int[width];
75     }
76     image.getRGB(row, 0, width, 0, y, width, 1);
77     for (int x = 0; x < width; x++) {
78       int pixel = row[x];
79       row[x] = (((pixel & 0x00FF0000) >> 16) +
80                 ((pixel & 0x0000FF00) >>  7) +
81                  (pixel & 0x000000FF       )) >> 2;
82     }
83     return row;
84   }
85
86   protected int[] getLuminanceColumn(int x, int[] column) {
87     int height = getHeight();
88     if (column == null || column.length < height) {
89       column = new int[height];
90     }
91     image.getRGB(column, 0, 1, x, 0, 1, height);
92     for (int y = 0; y < height; y++) {
93       int pixel = column[y];
94       column[y] = (((pixel & 0x00FF0000) >> 16) +
95                    ((pixel & 0x0000FF00) >>  7) +
96                     (pixel & 0x000000FF       )) >> 2;
97     }
98     return column;
99   }
100
101 }