Small style stuff
[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       row[x] = toLuminance(rgbData[x]);
66     }
67     return row;
68   }
69
70   public byte[] getMatrix() {
71     int width = getWidth();
72     int height = getHeight();
73     int area = width * height;
74     byte[] matrix = new byte[area];
75
76     int[] rgb = new int[area];
77     image.getRGB(rgb, 0, width, 0, 0, width, height);
78     for (int y = 0; y < height; y++) {
79       int offset = y * width;
80       for (int x = 0; x < width; x++) {
81         matrix[offset + x] = toLuminance(rgb[offset + x]);
82       }
83     }
84     return matrix;
85   }
86
87   public boolean isRotateSupported() {
88     return true;
89   }
90
91   public LuminanceSource rotateCounterClockwise() {
92     return new CCRotatedLCDUIImageLuminanceSource(image);
93   }
94
95   static byte toLuminance(int pixel) {
96     return (byte) ((((pixel & 0x00FF0000) >> 16) +
97                     ((pixel & 0x0000FF00) >>  7) +
98                      (pixel & 0x000000FF       )) >> 2);
99   }
100
101   /**
102    * A variant on {@link LCDUIImageLuminanceSource} that acts as if the input is rotated 90 degrees
103    * counter-clockwise.
104    */
105   private static final class CCRotatedLCDUIImageLuminanceSource extends LuminanceSource {
106
107     private final Image image;
108     private int[] rgbData;
109
110     private CCRotatedLCDUIImageLuminanceSource(Image image) {
111       super(image.getHeight(), image.getWidth());
112       this.image = image;
113     }
114
115     public byte[] getRow(int y, byte[] row) {
116       int height = getHeight();
117       if (y < 0 || y >= height) {
118         throw new IllegalArgumentException("Requested row is outside the image: " + y);
119       }
120       int width = getWidth();
121       if (row == null || row.length < width) {
122         row = new byte[width];
123       }
124
125       if (rgbData == null || rgbData.length < width) {
126         rgbData = new int[width];
127       }
128       image.getRGB(rgbData, 0, height, height - 1 - y, 0, 1, width);
129       for (int x = 0; x < width; x++) {
130         row[x] = toLuminance(rgbData[x]);
131       }
132       return row;
133     }
134
135     public byte[] getMatrix() {
136       int width = getWidth();
137       int height = getHeight();
138       int area = width * height;
139       byte[] matrix = new byte[area];
140
141       int[] rgb = new int[area];
142       image.getRGB(rgb, 0, width, 0, 0, width, height);
143       // This flips x/y in the target to result in a rotated image
144       int offset = height * (width - 1);
145       for (int y = 0; y < height; y++) {
146         for (int x = 0; x < width; x++) {
147           matrix[offset - height * x + y] = toLuminance(rgb[y * width + x]);
148         }
149       }
150       return matrix;
151     }
152   }
153 }