Use chunks_exact
This commit is contained in:
parent
963fe7dd45
commit
76d24ff226
5 changed files with 10 additions and 10 deletions
|
|
@ -126,8 +126,8 @@ impl RowFilter {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut pixels: Vec<_> = data.chunks_mut(bpp).collect();
|
let mut pixels: Vec<_> = data.chunks_exact_mut(bpp).collect();
|
||||||
let prev_pixels: Vec<_> = prev_line.chunks(bpp).collect();
|
let prev_pixels: Vec<_> = prev_line.chunks_exact(bpp).collect();
|
||||||
for i in 0..pixels.len() {
|
for i in 0..pixels.len() {
|
||||||
if pixels[i].iter().skip(color_bytes).all(|b| *b == 0) {
|
if pixels[i].iter().skip(color_bytes).all(|b| *b == 0) {
|
||||||
// If the first pixel in the row is transparent, find the next non-transparent pixel and pretend
|
// If the first pixel in the row is transparent, find the next non-transparent pixel and pretend
|
||||||
|
|
|
||||||
|
|
@ -225,7 +225,7 @@ fn palette_to_rgba(
|
||||||
) -> Result<Vec<RGBA8>, PngError> {
|
) -> Result<Vec<RGBA8>, PngError> {
|
||||||
let palette_data = palette_data.ok_or_else(|| PngError::new("no palette in indexed image"))?;
|
let palette_data = palette_data.ok_or_else(|| PngError::new("no palette in indexed image"))?;
|
||||||
let mut palette: Vec<_> = palette_data
|
let mut palette: Vec<_> = palette_data
|
||||||
.chunks(3)
|
.chunks_exact(3)
|
||||||
.map(|color| RGBA8::new(color[0], color[1], color[2], 255))
|
.map(|color| RGBA8::new(color[0], color[1], color[2], 255))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ pub fn cleaned_alpha_channel(png: &PngImage) -> Option<PngImage> {
|
||||||
let colored_bytes = bpp - byte_depth;
|
let colored_bytes = bpp - byte_depth;
|
||||||
|
|
||||||
let mut reduced = Vec::with_capacity(png.data.len());
|
let mut reduced = Vec::with_capacity(png.data.len());
|
||||||
for pixel in png.data.chunks(bpp) {
|
for pixel in png.data.chunks_exact(bpp) {
|
||||||
if pixel.iter().skip(colored_bytes).all(|b| *b == 0) {
|
if pixel.iter().skip(colored_bytes).all(|b| *b == 0) {
|
||||||
reduced.resize(reduced.len() + bpp, 0);
|
reduced.resize(reduced.len() + bpp, 0);
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -46,7 +46,7 @@ pub fn reduced_alpha_channel(png: &PngImage, optimize_alpha: bool) -> Option<Png
|
||||||
let mut has_transparency = false;
|
let mut has_transparency = false;
|
||||||
let mut used_colors = vec![false; 256];
|
let mut used_colors = vec![false; 256];
|
||||||
|
|
||||||
for pixel in png.data.chunks(bpp) {
|
for pixel in png.data.chunks_exact(bpp) {
|
||||||
if optimize_alpha && pixel.iter().skip(colored_bytes).all(|b| *b == 0) {
|
if optimize_alpha && pixel.iter().skip(colored_bytes).all(|b| *b == 0) {
|
||||||
// Fully transparent, we may be able to reduce with tRNS
|
// Fully transparent, we may be able to reduce with tRNS
|
||||||
has_transparency = true;
|
has_transparency = true;
|
||||||
|
|
@ -75,7 +75,7 @@ pub fn reduced_alpha_channel(png: &PngImage, optimize_alpha: bool) -> Option<Png
|
||||||
};
|
};
|
||||||
|
|
||||||
let mut raw_data = Vec::with_capacity(png.data.len());
|
let mut raw_data = Vec::with_capacity(png.data.len());
|
||||||
for pixel in png.data.chunks(bpp) {
|
for pixel in png.data.chunks_exact(bpp) {
|
||||||
match transparency_pixel {
|
match transparency_pixel {
|
||||||
Some(trns) if pixel.iter().skip(colored_bytes).all(|b| *b == 0) => {
|
Some(trns) if pixel.iter().skip(colored_bytes).all(|b| *b == 0) => {
|
||||||
raw_data.resize(raw_data.len() + colored_bytes, trns);
|
raw_data.resize(raw_data.len() + colored_bytes, trns);
|
||||||
|
|
|
||||||
|
|
@ -16,13 +16,13 @@ pub fn reduced_bit_depth_16_to_8(png: &PngImage, force_scale: bool) -> Option<Pn
|
||||||
}
|
}
|
||||||
|
|
||||||
// Reduce from 16 to 8 bits per channel per pixel
|
// Reduce from 16 to 8 bits per channel per pixel
|
||||||
if png.data.chunks(2).any(|pair| pair[0] != pair[1]) {
|
if png.data.chunks_exact(2).any(|pair| pair[0] != pair[1]) {
|
||||||
// Can't reduce
|
// Can't reduce
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(PngImage {
|
Some(PngImage {
|
||||||
data: png.data.iter().step_by(2).copied().collect(),
|
data: png.data.chunks_exact(2).map(|pair| pair[0]).collect(),
|
||||||
ihdr: IhdrData {
|
ihdr: IhdrData {
|
||||||
color_type: png.ihdr.color_type.clone(),
|
color_type: png.ihdr.color_type.clone(),
|
||||||
bit_depth: BitDepth::Eight,
|
bit_depth: BitDepth::Eight,
|
||||||
|
|
@ -41,7 +41,7 @@ pub fn scaled_bit_depth_16_to_8(png: &PngImage) -> Option<PngImage> {
|
||||||
// Reduce from 16 to 8 bits per channel per pixel by scaling when necessary
|
// Reduce from 16 to 8 bits per channel per pixel by scaling when necessary
|
||||||
let data = png
|
let data = png
|
||||||
.data
|
.data
|
||||||
.chunks(2)
|
.chunks_exact(2)
|
||||||
.map(|pair| {
|
.map(|pair| {
|
||||||
if pair[0] == pair[1] {
|
if pair[0] == pair[1] {
|
||||||
return pair[0];
|
return pair[0];
|
||||||
|
|
|
||||||
|
|
@ -106,7 +106,7 @@ pub fn reduced_rgb_to_grayscale(png: &PngImage) -> Option<PngImage> {
|
||||||
let byte_depth = png.bytes_per_channel();
|
let byte_depth = png.bytes_per_channel();
|
||||||
let bpp = png.channels_per_pixel() * byte_depth;
|
let bpp = png.channels_per_pixel() * byte_depth;
|
||||||
let last_color = 2 * byte_depth;
|
let last_color = 2 * byte_depth;
|
||||||
for pixel in png.data.chunks(bpp) {
|
for pixel in png.data.chunks_exact(bpp) {
|
||||||
if byte_depth == 1 {
|
if byte_depth == 1 {
|
||||||
if pixel[0] != pixel[1] || pixel[1] != pixel[2] {
|
if pixel[0] != pixel[1] || pixel[1] != pixel[2] {
|
||||||
return None;
|
return None;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue