4f929f22f6c526719ff793c05f48cc09639f843d
[zxing.git] / javame / src / com / google / zxing / client / j2me / LCDUIImageLuminanceSource.java
1 /*
2  * Copyright 2009 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.LuminanceSource;
20
21 import javax.microedition.lcdui.Image;
22
23 /**
24  * A LuminanceSource based on Java ME's Image class. It does not support cropping or rotation.
25  *
26  * @author dswitkin@google.com (Daniel Switkin)
27  * @author Sean Owen
28  */
29 public final class LCDUIImageLuminanceSource extends LuminanceSource {
30
31   private final Image image;
32   private int[] rgbData;
33
34   public LCDUIImageLuminanceSource(Image image) {
35     super(image.getWidth(), image.getHeight());
36     this.image = image;
37   }
38
39   // Instead of multiplying by 306, 601, 117, we multiply by 256, 512, 256, so that
40   // the multiplies can be implemented as shifts.
41   //
42   // Really, it's:
43   //
44   // return ((((pixel >> 16) & 0xFF) << 8) +
45   //         (((pixel >>  8) & 0xFF) << 9) +
46   //         (( pixel        & 0xFF) << 8)) >> 10;
47   //
48   // That is, we're replacing the coefficients in the original with powers of two,
49   // which can be implemented as shifts, even though changing the coefficients slightly
50   // alters the conversion. The difference is not significant for our purposes.
51   public byte[] getRow(int y, byte[] row) {
52     if (y < 0 || y >= getHeight()) {
53       throw new IllegalArgumentException("Requested row is outside the image: " + y);
54     }
55     int width = getWidth();
56     if (row == null || row.length < width) {
57       row = new byte[width];
58     }
59
60     if (rgbData == null || rgbData.length < width) {
61       rgbData = new int[width];
62     }
63     image.getRGB(rgbData, 0, width, 0, y, width, 1);
64     for (int x = 0; x < width; x++) {
65       int pixel = rgbData[x];
66       int luminance = (((pixel & 0x00FF0000) >> 16) +
67                        ((pixel & 0x0000FF00) >>  7) +
68                         (pixel & 0x000000FF       )) >> 2;
69       row[x] = (byte) luminance;
70     }
71     return row;
72   }
73
74   public byte[] getMatrix() {
75     int width = getWidth();
76     int height = getHeight();
77     int area = width * height;
78     byte[] matrix = new byte[area];
79
80     int[] rgb = new int[area];
81     image.getRGB(rgb, 0, width, 0, 0, width, height);
82     for (int y = 0; y < height; y++) {
83       int offset = y * width;
84       for (int x = 0; x < width; x++) {
85         int pixel = rgb[offset + x];
86         int luminance = (((pixel & 0x00FF0000) >> 16) +
87                          ((pixel & 0x0000FF00) >>  7) +
88                           (pixel & 0x000000FF       )) >> 2;
89         matrix[offset + x] = (byte) luminance;
90       }
91     }
92     return matrix;
93   }
94
95 }