| 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; |
| 18 | |
| 19 | /** |
| 20 | * This class is used to help decode images from files which arrive as RGB data from |
| 21 | * an ARGB pixel array. It does not support rotation. |
| 22 | * |
| 23 | * @author dswitkin@google.com (Daniel Switkin) |
| 24 | * @author Betaminos |
| 25 | */ |
| 26 | public final class RGBLuminanceSource extends LuminanceSource { |
| 27 | |
| 28 | private final byte[] luminances; |
| 29 | private final int dataWidth; |
| 30 | private final int dataHeight; |
| 31 | private final int left; |
| 32 | private final int top; |
| 33 | |
| 34 | public RGBLuminanceSource(int width, int height, int[] pixels) { |
| 35 | super(width, height); |
| 36 | |
| 37 | dataWidth = width; |
| 38 | dataHeight = height; |
| 39 | left = 0; |
| 40 | top = 0; |
| 41 | |
| 42 | // In order to measure pure decoding speed, we convert the entire image to a greyscale array |
| 43 | // up front, which is the same as the Y channel of the YUVLuminanceSource in the real app. |
| 44 | // |
| 45 | // Total number of pixels suffices, can ignore shape |
| 46 | int size = width * height; |
| 47 | luminances = new byte[size]; |
| 48 | for (int offset = 0; offset < size; offset++) { |
| 49 | int pixel = pixels[offset]; |
| 50 | int r = (pixel >> 16) & 0xff; // red |
| 51 | int g2 = (pixel >> 7) & 0x1fe; // 2 * green |
| 52 | int b = pixel & 0xff; // blue |
| 53 | // Calculate green-favouring average cheaply |
| 54 | luminances[offset] = (byte) ((r + g2 + b) / 4); |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | private RGBLuminanceSource(byte[] pixels, |
| 59 | int dataWidth, |
| 60 | int dataHeight, |
| 61 | int left, |
| 62 | int top, |
| 63 | int width, |
| 64 | int height) { |
| 65 | super(width, height); |
| 66 | if (left + width > dataWidth || top + height > dataHeight) { |
| 67 | throw new IllegalArgumentException("Crop rectangle does not fit within image data."); |
| 68 | } |
| 69 | this.luminances = pixels; |
| 70 | this.dataWidth = dataWidth; |
| 71 | this.dataHeight = dataHeight; |
| 72 | this.left = left; |
| 73 | this.top = top; |
| 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public byte[] getRow(int y, byte[] row) { |
| 78 | if (y < 0 || y >= getHeight()) { |
| 79 | throw new IllegalArgumentException("Requested row is outside the image: " + y); |
| 80 | } |
| 81 | int width = getWidth(); |
| 82 | if (row == null || row.length < width) { |
| 83 | row = new byte[width]; |
| 84 | } |
| 85 | int offset = (y + top) * dataWidth + left; |
| 86 | System.arraycopy(luminances, offset, row, 0, width); |
| 87 | return row; |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public byte[] getMatrix() { |
| 92 | int width = getWidth(); |
| 93 | int height = getHeight(); |
| 94 | |
| 95 | // If the caller asks for the entire underlying image, save the copy and give them the |
| 96 | // original data. The docs specifically warn that result.length must be ignored. |
| 97 | if (width == dataWidth && height == dataHeight) { |
| 98 | return luminances; |
| 99 | } |
| 100 | |
| 101 | int area = width * height; |
| 102 | byte[] matrix = new byte[area]; |
| 103 | int inputOffset = top * dataWidth + left; |
| 104 | |
| 105 | // If the width matches the full width of the underlying data, perform a single copy. |
| 106 | if (width == dataWidth) { |
| 107 | System.arraycopy(luminances, inputOffset, matrix, 0, area); |
| 108 | return matrix; |
| 109 | } |
| 110 | |
| 111 | // Otherwise copy one cropped row at a time. |
| 112 | for (int y = 0; y < height; y++) { |
| 113 | int outputOffset = y * width; |
| 114 | System.arraycopy(luminances, inputOffset, matrix, outputOffset, width); |
| 115 | inputOffset += dataWidth; |
| 116 | } |
| 117 | return matrix; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public boolean isCropSupported() { |
| 122 | return true; |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public LuminanceSource crop(int left, int top, int width, int height) { |
| 127 | return new RGBLuminanceSource(luminances, |
| 128 | dataWidth, |
| 129 | dataHeight, |
| 130 | this.left + left, |
| 131 | this.top + top, |
| 132 | width, |
| 133 | height); |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | |