Skip to content

Commit

Permalink
fix bit expansion.
Browse files Browse the repository at this point in the history
  • Loading branch information
Lokathor committed Apr 12, 2023
1 parent 23aed00 commit ce50f00
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/png/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
//! [png-spec]: https://www.w3.org/TR/2003/REC-PNG-20031110/

use crate::{sRGBIntent, ImagineError};

Check warning on line 7 in src/png/mod.rs

View workflow job for this annotation

GitHub Actions / Test Rust stable on windows-latest

unused import: `ImagineError`

Check warning on line 7 in src/png/mod.rs

View workflow job for this annotation

GitHub Actions / Test Rust stable on windows-latest

unused import: `ImagineError`
use bitfrob::u8_replicate_bits;
use core::fmt::{Debug, Write};
use pixel_formats::{r32g32b32a32_Sfloat, r8g8b8_Unorm, r8g8b8a8_Unorm};

Check warning on line 9 in src/png/mod.rs

View workflow job for this annotation

GitHub Actions / Test Rust stable on windows-latest

unused imports: `r32g32b32a32_Sfloat`, `r8g8b8_Unorm`, `r8g8b8a8_Unorm`

Check warning on line 9 in src/png/mod.rs

View workflow job for this annotation

GitHub Actions / Test Rust stable on windows-latest

unused imports: `r32g32b32a32_Sfloat`, `r8g8b8_Unorm`, `r8g8b8a8_Unorm`

Expand Down Expand Up @@ -199,6 +198,7 @@ where
{
#[allow(unused)]
use alloc::vec::Vec;
use bitfrob::{U8_SCALE_1_TO_8, U8_SCALE_2_TO_8, U8_SCALE_4_TO_8};
use bytemuck::cast_slice;
use pixel_formats::{r8g8b8_Srgb, r8g8b8a8_Srgb};

Expand Down Expand Up @@ -328,25 +328,37 @@ where
ihdr.unfilter_decompressed_data(&mut zlib_buffer, unfilter_op).ok();
}
PngColorType::Y if is_srgb => {
let mult = match ihdr.bit_depth {
1 => U8_SCALE_1_TO_8,
2 => U8_SCALE_2_TO_8,
4 => U8_SCALE_4_TO_8,
_ => 1,
};
let unfilter_op = |x: u32, y: u32, data: &[u8]| {
if let Some(p) = bitmap.get_mut(x, y) {
*p = if Some(u16::from(data[0])) == trns_y {
transparent_black
} else {
let y = u8_replicate_bits(u32::from(ihdr.bit_depth), data[0]);
let y = data[0] * mult;
P::from(r32g32b32a32_Sfloat::from(r8g8b8a8_Srgb { r: y, g: y, b: y, a: u8::MAX }))
};
}
};
ihdr.unfilter_decompressed_data(&mut zlib_buffer, unfilter_op).ok();
}
PngColorType::Y => {
let mult = match ihdr.bit_depth {
1 => U8_SCALE_1_TO_8,
2 => U8_SCALE_2_TO_8,
4 => U8_SCALE_4_TO_8,
_ => 1,
};
let unfilter_op = |x: u32, y: u32, data: &[u8]| {
if let Some(p) = bitmap.get_mut(x, y) {
*p = if Some(u16::from(data[0])) == trns_y {
transparent_black
} else {
let y = u8_replicate_bits(u32::from(ihdr.bit_depth), data[0]);
let y = data[0] * mult;
let y = (y as f32) / (u8::MAX as f32);
let sfloat = r32g32b32a32_Sfloat { r: y, g: y, b: y, a: 1.0 };
let gamma_corrected = r32g32b32a32_Sfloat {
Expand Down

0 comments on commit ce50f00

Please sign in to comment.