1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
// /*
// * Copyright 2013 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;
// /**
// * A wrapper implementation of {@link LuminanceSource} which inverts the luminances it returns -- black becomes
// * white and vice versa, and each value becomes (255-value).
// *
// * @author Sean Owen
// */
// pub struct InvertedLuminanceSource {
// width: usize,
// height: usize,
// delegate: Box<dyn LuminanceSource>,
// }
// impl InvertedLuminanceSource {
// pub fn new_with_delegate(delegate: Box<dyn LuminanceSource>) -> Self {
// Self {
// width: delegate.getWidth(),
// height: delegate.getHeight(),
// delegate,
// }
// }
// }
// impl LuminanceSource for InvertedLuminanceSource {
// fn getRow(&self, y: usize, row: &Vec<u8>) -> Vec<u8> {
// let mut new_row = self.delegate.getRow(y, row);
// let width = self.getWidth();
// for i in 0..width {
// //for (int i = 0; i < width; i++) {
// new_row[i] = 255 - (new_row[i] & 0xFF);
// }
// return new_row;
// }
// fn getMatrix(&self) -> Vec<u8> {
// let matrix = self.delegate.getMatrix();
// let length = self.getWidth() * self.getHeight();
// let mut invertedMatrix = Vec::with_capacity(length);
// for i in 0..length {
// //for (int i = 0; i < length; i++) {
// invertedMatrix[i] = 255 - (matrix[i] & 0xFF);
// }
// return invertedMatrix;
// }
// fn getWidth(&self) -> usize {
// self.width
// }
// fn getHeight(&self) -> usize {
// self.height
// }
// fn isCropSupported(&self) -> bool {
// return self.delegate.isCropSupported();
// }
// fn crop(
// &self,
// left: usize,
// top: usize,
// width: usize,
// height: usize,
// ) -> Result<Box<dyn LuminanceSource>, UnsupportedOperationException> {
// let crop = self.delegate.crop(left, top, width, height)?;
// return Ok(Box::new(InvertedLuminanceSource::new_with_delegate(crop)));
// }
// fn isRotateSupported(&self) -> bool {
// return self.delegate.isRotateSupported();
// }
// /**
// * @return original delegate {@link LuminanceSource} since invert undoes itself
// */
// fn invert(&self) -> Box<dyn LuminanceSource> {
// return self.delegate;
// }
// fn rotateCounterClockwise(
// &self,
// ) -> Result<Box<dyn LuminanceSource>, UnsupportedOperationException> {
// let rot = self.delegate.rotateCounterClockwise()?;
// return Ok(Box::new(InvertedLuminanceSource::new_with_delegate(rot)));
// }
// fn rotateCounterClockwise45(
// &self,
// ) -> Result<Box<dyn LuminanceSource>, UnsupportedOperationException> {
// let rot_45 = self.delegate.rotateCounterClockwise45()?;
// return Ok(Box::new(InvertedLuminanceSource::new_with_delegate(rot_45)));
// }
// }
/*
* 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;
use crate::common::Result;
use crate::{Exceptions, LuminanceSource};
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)
*/
#[derive(Debug, Clone)]
pub struct PlanarYUVLuminanceSource {
yuv_data: Vec<u8>,
data_width: usize,
data_height: usize,
left: usize,
top: usize,
width: usize,
height: usize,
invert: bool,
}
impl PlanarYUVLuminanceSource {
#[allow(clippy::too_many_arguments)]
pub fn new_with_all(
yuv_data: Vec<u8>,
data_width: usize,
data_height: usize,
left: usize,
top: usize,
width: usize,
height: usize,
reverse_horizontal: bool,
inverted: bool,
) -> Result<Self> {
if left + width > data_width || top + height > data_height {
return Err(Exceptions::illegalArgumentWith(
"Crop rectangle does not fit within image data.",
));
}
let mut new_s: Self = Self {
yuv_data,
data_width,
data_height,
left,
top,
width,
height,
invert: inverted,
};
if reverse_horizontal {
new_s.reverseHorizontal(width, height);
}
Ok(new_s)
}
pub fn renderThumbnail(&self) -> Vec<u8> {
let width = self.getWidth() / THUMBNAIL_SCALE_FACTOR;
let height = self.getHeight() / THUMBNAIL_SCALE_FACTOR;
let mut pixels = vec![0; width * height];
let yuv = &self.yuv_data;
let mut input_offset = self.top * self.data_width + self.left;
for y in 0..height {
let output_offset = y * width;
for x in 0..width {
let grey = yuv[input_offset + x * THUMBNAIL_SCALE_FACTOR];
pixels[output_offset + x] = (0xFF000000 | (grey as u32 * 0x00010101)) as u8;
}
input_offset += self.data_width * THUMBNAIL_SCALE_FACTOR;
}
pixels
}
/**
* @return width of image from {@link #renderThumbnail()}
*/
pub fn getThumbnailWidth(&self) -> usize {
self.getWidth() / THUMBNAIL_SCALE_FACTOR
}
/**
* @return height of image from {@link #renderThumbnail()}
*/
pub fn getThumbnailHeight(&self) -> usize {
self.getHeight() / THUMBNAIL_SCALE_FACTOR
}
fn reverseHorizontal(&mut self, width: usize, height: usize) {
let mut rowStart = self.top * self.data_width + self.left;
for _y in 0..height {
let middle = rowStart + width / 2;
let mut x2 = rowStart + width - 1;
for x1 in rowStart..middle {
self.yuv_data.swap(x1, x2);
x2 -= 1;
}
rowStart += self.data_width;
}
}
}
impl LuminanceSource for PlanarYUVLuminanceSource {
fn getRow(&self, y: usize) -> Vec<u8> {
if y >= self.getHeight() {
// //throw new IllegalArgumentException("Requested row is outside the image: " + y);
// panic!("Requested row is outside the image: {y}");
return Vec::new();
}
let width = self.getWidth();
let offset = (y + self.top) * self.data_width + self.left;
let mut row = vec![0; width];
row[..width].clone_from_slice(&self.yuv_data[offset..width + offset]);
if self.invert {
row = self.invert_block_of_bytes(row);
}
row
}
fn getMatrix(&self) -> Vec<u8> {
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.data_width && height == self.data_height {
let mut v = self.yuv_data.clone();
if self.invert {
v = self.invert_block_of_bytes(v);
}
return v;
}
let area = width * height;
let mut matrix = vec![0; area];
let mut inputOffset = self.top * self.data_width + self.left;
// If the width matches the full width of the underlying data, perform a single copy.
if width == self.data_width {
matrix[0..area].clone_from_slice(&self.yuv_data[inputOffset..area]);
if self.invert {
matrix = self.invert_block_of_bytes(matrix);
}
return matrix;
}
// Otherwise copy one cropped row at a time.
for y in 0..height {
let outputOffset = y * width;
matrix[outputOffset..outputOffset + width]
.clone_from_slice(&self.yuv_data[inputOffset..inputOffset + width]);
inputOffset += self.data_width;
}
if self.invert {
matrix = self.invert_block_of_bytes(matrix);
}
matrix
}
fn getWidth(&self) -> usize {
self.width
}
fn getHeight(&self) -> usize {
self.height
}
fn isCropSupported(&self) -> bool {
true
}
fn crop(
&self,
left: usize,
top: usize,
width: usize,
height: usize,
) -> Result<Box<dyn LuminanceSource>> {
match PlanarYUVLuminanceSource::new_with_all(
self.yuv_data.clone(),
self.data_width,
self.data_height,
self.left + left,
self.top + top,
width,
height,
false,
self.invert,
) {
Ok(new) => Ok(Box::new(new)),
Err(_err) => Err(Exceptions::unsupportedOperation),
}
}
fn isRotateSupported(&self) -> bool {
false
}
fn invert(&mut self) {
self.invert = !self.invert;
}
}