Convert asserts to exceptions where the conditions could be false in a correct, bug...
[zxing.git] / android / src / com / google / zxing / client / android / YUVMonochromeBitmapSource.java
1 /*
2  * Copyright (C) 2008 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 android.graphics.Bitmap;
20 import android.graphics.Rect;
21 import com.google.zxing.common.BaseMonochromeBitmapSource;
22
23 /**
24  * This object implements MonochromeBitmapSource around an array of YUV data, giving you the option
25  * to crop to a rectangle within the full data. This can be used to exclude superfluous pixels
26  * around the perimeter and speed up decoding.
27  */
28 final class YUVMonochromeBitmapSource extends BaseMonochromeBitmapSource {
29
30   private final byte[] mYUVData;
31   private final int mDataWidth;
32   private final Rect mCrop;
33
34   /**
35    * Builds an object around a YUV buffer from the camera.
36    *
37    * @param yuvData    A byte array of planar Y data, followed by interleaved U and V
38    * @param dataWidth  The width of the Y data
39    * @param dataHeight The height of the Y data
40    * @param crop       The rectangle within the yuvData to expose to MonochromeBitmapSource users
41    */
42   YUVMonochromeBitmapSource(byte[] yuvData, int dataWidth, int dataHeight, Rect crop) {
43     if (crop.width() > dataWidth || crop.height() > dataHeight) {
44       throw new IllegalArgumentException();
45     }
46     mYUVData = yuvData;
47     mDataWidth = dataWidth;
48     mCrop = crop;
49   }
50
51   @Override
52   public int getHeight() {
53     return mCrop.height();
54   }
55
56   @Override
57   public int getWidth() {
58     return mCrop.width();
59   }
60
61   /**
62    * The Y channel is stored as planar data at the head of the array, so we just ignore the
63    * interleavd U and V which follow it.
64    *
65    * @param x The x coordinate to fetch within crop
66    * @param y The y coordinate to fetch within crop
67    * @return The luminance as an int, from 0-255
68    */
69   @Override
70   protected int getLuminance(int x, int y) {
71     return mYUVData[(y + mCrop.top) * mDataWidth + x + mCrop.left] & 0xff;
72   }
73
74   @Override
75   protected int[] getLuminanceRow(int y, int[] row) {
76     int width = getWidth();
77     if (row == null || row.length < width) {
78       row = new int[width];
79     }
80     int offset = (y + mCrop.top) * mDataWidth + mCrop.left;
81     for (int x = 0; x < width; x++) {
82       row[x] = mYUVData[offset + x] & 0xff;
83     }
84     return row;
85   }
86
87   @Override
88   protected int[] getLuminanceColumn(int x, int[] column) {
89     int height = getHeight();
90     if (column == null || column.length < height) {
91       column = new int[height];
92     }
93     int offset = mCrop.top * mDataWidth + mCrop.left + x;
94     for (int y = 0; y < height; y++) {
95       column[y] = mYUVData[offset] & 0xff;
96       offset += mDataWidth;
97     }
98     return column;
99   }
100
101   /**
102    * Create a greyscale Android Bitmap from the YUV data based on the crop rectangle.
103    *
104    * @return An 8888 bitmap.
105    */
106   public Bitmap renderToBitmap() {
107     int width = mCrop.width();
108     int height = mCrop.height();
109     int[] pixels = new int[width * height];
110     for (int y = 0; y < height; y++) {
111       int base = (y + mCrop.top) * mDataWidth + mCrop.left;
112       for (int x = 0; x < width; x++) {
113         int grey = mYUVData[base + x] & 0xff;
114         pixels[y * width + x] = (0xff << 24) | (grey << 16) | (grey << 8) | grey;
115       }
116     }
117
118     Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
119     bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
120     return bitmap;
121   }
122
123 }