From 48b08e0a9f90ab5ee1b7e3484d19d39ae92d0ca9 Mon Sep 17 00:00:00 2001 From: Henry Date: Sun, 21 Aug 2022 18:56:38 -0500 Subject: [PATCH] PlanarYUVLuminanceSource --- src/PlanarYUVLuminanceSource.java | 168 ----------------------- src/lib.rs | 220 ++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+), 168 deletions(-) delete mode 100644 src/PlanarYUVLuminanceSource.java diff --git a/src/PlanarYUVLuminanceSource.java b/src/PlanarYUVLuminanceSource.java deleted file mode 100644 index cecff3e..0000000 --- a/src/PlanarYUVLuminanceSource.java +++ /dev/null @@ -1,168 +0,0 @@ -/* - * Copyright 2009 ZXing authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.google.zxing; - -/** - * This object extends LuminanceSource around an array of YUV data returned from the camera driver, - * with the option to crop to a rectangle within the full data. This can be used to exclude - * superfluous pixels around the perimeter and speed up decoding. - * - * It works for any pixel format where the Y channel is planar and appears first, including - * YCbCr_420_SP and YCbCr_422_SP. - * - * @author dswitkin@google.com (Daniel Switkin) - */ -public final class PlanarYUVLuminanceSource extends LuminanceSource { - - private static final int THUMBNAIL_SCALE_FACTOR = 2; - - private final byte[] yuvData; - private final int dataWidth; - private final int dataHeight; - private final int left; - private final int top; - - public PlanarYUVLuminanceSource(byte[] yuvData, - int dataWidth, - int dataHeight, - int left, - int top, - int width, - int height, - boolean reverseHorizontal) { - super(width, height); - - if (left + width > dataWidth || top + height > dataHeight) { - throw new IllegalArgumentException("Crop rectangle does not fit within image data."); - } - - this.yuvData = yuvData; - this.dataWidth = dataWidth; - this.dataHeight = dataHeight; - this.left = left; - this.top = top; - if (reverseHorizontal) { - reverseHorizontal(width, height); - } - } - - @Override - public byte[] getRow(int y, byte[] row) { - if (y < 0 || y >= getHeight()) { - throw new IllegalArgumentException("Requested row is outside the image: " + y); - } - int width = getWidth(); - if (row == null || row.length < width) { - row = new byte[width]; - } - int offset = (y + top) * dataWidth + left; - System.arraycopy(yuvData, offset, row, 0, width); - return row; - } - - @Override - public byte[] getMatrix() { - int width = getWidth(); - int height = getHeight(); - - // If the caller asks for the entire underlying image, save the copy and give them the - // original data. The docs specifically warn that result.length must be ignored. - if (width == dataWidth && height == dataHeight) { - return yuvData; - } - - int area = width * height; - byte[] matrix = new byte[area]; - int inputOffset = top * dataWidth + left; - - // If the width matches the full width of the underlying data, perform a single copy. - if (width == dataWidth) { - System.arraycopy(yuvData, inputOffset, matrix, 0, area); - return matrix; - } - - // Otherwise copy one cropped row at a time. - for (int y = 0; y < height; y++) { - int outputOffset = y * width; - System.arraycopy(yuvData, inputOffset, matrix, outputOffset, width); - inputOffset += dataWidth; - } - return matrix; - } - - @Override - public boolean isCropSupported() { - return true; - } - - @Override - public LuminanceSource crop(int left, int top, int width, int height) { - return new PlanarYUVLuminanceSource(yuvData, - dataWidth, - dataHeight, - this.left + left, - this.top + top, - width, - height, - false); - } - - public int[] renderThumbnail() { - int width = getWidth() / THUMBNAIL_SCALE_FACTOR; - int height = getHeight() / THUMBNAIL_SCALE_FACTOR; - int[] pixels = new int[width * height]; - byte[] yuv = yuvData; - int inputOffset = top * dataWidth + left; - - for (int y = 0; y < height; y++) { - int outputOffset = y * width; - for (int x = 0; x < width; x++) { - int grey = yuv[inputOffset + x * THUMBNAIL_SCALE_FACTOR] & 0xff; - pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101); - } - inputOffset += dataWidth * THUMBNAIL_SCALE_FACTOR; - } - return pixels; - } - - /** - * @return width of image from {@link #renderThumbnail()} - */ - public int getThumbnailWidth() { - return getWidth() / THUMBNAIL_SCALE_FACTOR; - } - - /** - * @return height of image from {@link #renderThumbnail()} - */ - public int getThumbnailHeight() { - return getHeight() / THUMBNAIL_SCALE_FACTOR; - } - - private void reverseHorizontal(int width, int height) { - byte[] yuvData = this.yuvData; - for (int y = 0, rowStart = top * dataWidth + left; y < height; y++, rowStart += dataWidth) { - int middle = rowStart + width / 2; - for (int x1 = rowStart, x2 = rowStart + width - 1; x1 < middle; x1++, x2--) { - byte temp = yuvData[x1]; - yuvData[x1] = yuvData[x2]; - yuvData[x2] = temp; - } - } - } - -} diff --git a/src/lib.rs b/src/lib.rs index 3101207..fff1041 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1622,3 +1622,223 @@ for i in 0..length { } } + +/* + * Copyright 2009 ZXing authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//package com.google.zxing; + +const THUMBNAIL_SCALE_FACTOR: usize = 2; + +/** + * This object extends LuminanceSource around an array of YUV data returned from the camera driver, + * with the option to crop to a rectangle within the full data. This can be used to exclude + * superfluous pixels around the perimeter and speed up decoding. + * + * It works for any pixel format where the Y channel is planar and appears first, including + * YCbCr_420_SP and YCbCr_422_SP. + * + * @author dswitkin@google.com (Daniel Switkin) + */ +pub struct PlanarYUVLuminanceSource { + + yuvData:Vec, + dataWidth:usize, + dataHeight:usize, + left:usize, + top:usize, + width:usize, + height:usize, +} + +impl PlanarYUVLuminanceSource { + pub fn new_with_all( + yuvData:Vec, + dataWidth:usize, + dataHeight:usize, + left:usize, + top:usize, + width:usize, + height:usize, + reverseHorizontal:bool) -> Result { + + + if (left + width > dataWidth || top + height > dataHeight) { + return Err( IllegalArgumentException::new("Crop rectangle does not fit within image data.")); + } + + let new_s : Self = Self { + yuvData, + dataWidth, + dataHeight, + left, + top, + width, + height, + }; + + if reverseHorizontal { + new_s.reverseHorizontal(width, height); + } + + Ok(new_s) + } + + pub fn renderThumbnail(&self) -> Vec { + let width = self.getWidth() / THUMBNAIL_SCALE_FACTOR; + let height = self.getHeight() / THUMBNAIL_SCALE_FACTOR; + let pixels = Vec::with_capacity(width*height); + let yuv:Vec = Vec::new(); + let inputOffset = self.top *self. dataWidth + self.left; + + for y in 0..height{ + //for (int y = 0; y < height; y++) { + let outputOffset = y * width; + for x in 0..width { + //for (int x = 0; x < width; x++) { + let grey = yuv[inputOffset + x * THUMBNAIL_SCALE_FACTOR] & 0xff; + pixels[outputOffset + x] = 0xFF000000 | (grey * 0x00010101); + } + inputOffset += self.dataWidth * THUMBNAIL_SCALE_FACTOR; + } + return pixels; + } + + /** + * @return width of image from {@link #renderThumbnail()} + */ + pub fn getThumbnailWidth(&self) -> usize{ + return self.getWidth() / THUMBNAIL_SCALE_FACTOR; + } + + /** + * @return height of image from {@link #renderThumbnail()} + */ + pub fn getThumbnailHeight(&self) -> usize{ + return self.getHeight() / THUMBNAIL_SCALE_FACTOR; + } + + fn reverseHorizontal(&self, width:usize, height:usize) { + let yuvData = self.yuvData; + let mut rowStart = self.top * self.dataWidth + self.left; + for y in 0..height { + let middle = rowStart + width / 2; + let mut x2 = rowStart + width -1 ; + for x1 in rowStart..middle { + //for (int x1 = rowStart, x2 = rowStart + width - 1; x1 < middle; x1++, x2--) { + let temp = yuvData[x1]; + yuvData[x1] = yuvData[x2]; + yuvData[x2] = temp; + x2 -= 1; + } + rowStart += self.dataWidth; + } + self.yuvData = yuvData; + /*for (int y = 0, rowStart = top * dataWidth + left; y < height; y++, rowStart += dataWidth) { + let middle = rowStart + width / 2; + for (int x1 = rowStart, x2 = rowStart + width - 1; x1 < middle; x1++, x2--) { + let temp = yuvData[x1]; + yuvData[x1] = yuvData[x2]; + yuvData[x2] = temp; + } + }*/ + } + +} + +impl LuminanceSource for PlanarYUVLuminanceSource{ + fn new( width:usize, height:usize) -> Self { + let new_ils : Self; + new_ils.width = width; + new_ils.height = height; + + new_ils + } + + fn getRow(&self, y:usize, row:&Vec) -> Vec { + if (y < 0 || y >= self.getHeight()) { + //throw new IllegalArgumentException("Requested row is outside the image: " + y); + panic!("Requested row is outside the image: {}",y); + } + let width = self.getWidth(); + + let offset = (y + self.top) * self.dataWidth + self.left; + + row.clone_from_slice(&self.yuvData[offset..width]); + //System.arraycopy(yuvData, offset, row, 0, width); + return row; + } + + fn getMatrix(&self) -> Vec { + let width = self.getWidth(); + let height = self.getHeight(); + + // If the caller asks for the entire underlying image, save the copy and give them the + // original data. The docs specifically warn that result.length must be ignored. + if (width == self.dataWidth && height == self.dataHeight) { + return self.yuvData; + } + + let area = width * height; + let matrix = Vec::with_capacity(area); + let inputOffset = self.top * self.dataWidth + self.left; + + // If the width matches the full width of the underlying data, perform a single copy. + if (width == self.dataWidth) { + matrix[0..area].clone_from_slice(&self.yuvData[inputOffset..area]); + //System.arraycopy(yuvData, inputOffset, matrix, 0, area); + return matrix; + } + + // Otherwise copy one cropped row at a time. + for y in 0..height{ + //for (int y = 0; y < height; y++) { + let outputOffset = y * width; + matrix[outputOffset..width].clone_from_slice(&self.yuvData[inputOffset..width]); + //System.arraycopy(yuvData, inputOffset, matrix, outputOffset, width); + inputOffset += self.dataWidth; + } + return matrix; + } + + fn getWidth(&self) -> usize { + self.width + } + + fn getHeight(&self) -> usize { + self.height + } + + fn isCropSupported(&self) -> bool { + return true; + } + + fn crop(&self, left:usize, top:usize, width:usize, height:usize) -> Result { + return PlanarYUVLuminanceSource::new_with_all(self.yuvData, + self.dataWidth, + self.dataHeight, + self.left + left, + self.top + top, + width, + height, + false); + } + + fn isRotateSupported(&self) -> bool { + return false; + } + +}