Lots of updates:
[zxing.git] / android / src / com / google / zxing / client / android / InterleavedYUV422LuminanceSource.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.android;
18
19 import com.google.zxing.LuminanceSource;
20
21 import android.graphics.Bitmap;
22
23 /**
24  * This object extends LuminanceSource around an array of YUV data returned from the camera driver,
25  * with the option to crop to a rectangle within the full data. This can be used to exclude
26  * superfluous pixels around the perimeter and speed up decoding.
27  *
28  * It handles YUV 422 interleaved data, where each pixel consists of first a Y value, then
29  * a color value, with U and V alternating at each pixel.
30  *
31  * @author dswitkin@google.com (Daniel Switkin)
32  */
33 public final class InterleavedYUV422LuminanceSource extends BaseLuminanceSource {
34   private final byte[] yuvData;
35   private final int dataWidth;
36   private final int dataHeight;
37   private final int left;
38   private final int top;
39
40   public InterleavedYUV422LuminanceSource(byte[] yuvData, int dataWidth, int dataHeight,
41       int left, int top, int width, int height) {
42     super(width, height);
43
44     if (left + width > dataWidth || top + height > dataHeight) {
45       throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
46     }
47
48     this.yuvData = yuvData;
49     this.dataWidth = dataWidth;
50     this.dataHeight = dataHeight;
51     this.left = left;
52     this.top = top;
53   }
54
55   @Override
56   public byte[] getRow(int y, byte[] row) {
57     if (y < 0 || y >= getHeight()) {
58       throw new IllegalArgumentException("Requested row is outside the image: " + y);
59     }
60     int width = getWidth();
61     if (row == null || row.length < width) {
62       row = new byte[width];
63     }
64     int offset = (y + top) * dataWidth * 2 + (left * 2);
65     byte[] yuv = yuvData;
66     for (int x = 0; x < width; x++) {
67       row[x] = yuv[offset + (x << 1)];
68     }
69     return row;
70   }
71
72   @Override
73   public byte[] getMatrix() {
74     int width = getWidth();
75     int height = getHeight();
76     int area = width * height;
77     byte[] matrix = new byte[area];
78     int inputOffset = top * dataWidth * 2 + (left * 2);
79     byte[] yuv = yuvData;
80
81     for (int y = 0; y < height; y++) {
82       int outputOffset = y * width;
83       for (int x = 0; x < width; x++) {
84         matrix[outputOffset + x] = yuv[inputOffset + (x << 1)];
85       }
86       inputOffset += (dataWidth * 2);
87     }
88     return matrix;
89   }
90
91   @Override
92   public boolean isCropSupported() {
93     return true;
94   }
95
96   @Override
97   public LuminanceSource crop(int left, int top, int width, int height) {
98     return new InterleavedYUV422LuminanceSource(yuvData, dataWidth, dataHeight, left, top,
99         width, height);
100   }
101
102   @Override
103   public int getDataWidth() {
104     return dataWidth;
105   }
106
107   @Override
108   public int getDataHeight() {
109     return dataHeight;
110   }
111
112   @Override
113   public Bitmap renderCroppedGreyscaleBitmap() {
114     int width = getWidth();
115     int height = getHeight();
116     int[] pixels = new int[width * height];
117     byte[] yuv = yuvData;
118     int inputOffset = top * dataWidth * 2 + (left * 2);
119
120     for (int y = 0; y < height; y++) {
121       int outputOffset = y * width;
122       for (int x = 0; x < width; x++) {
123         int grey = yuv[inputOffset + (x << 1)] & 0xff;
124         pixels[outputOffset + x] = (0xff000000) | (grey * 0x00010101);
125       }
126       inputOffset += (dataWidth * 2);
127     }
128
129     Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
130     bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
131     return bitmap;
132   }
133
134   @Override
135   public Bitmap renderFullColorBitmap(boolean halfSize) {
136     return null;
137   }
138 }