Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion container/src/container_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ pub trait ProcessBuffer {
buffer = &[];
} else {
buffer = input.fill_buf().context()?;
if buffer.len() == 0 {
if buffer.is_empty() {
input_complete = true
}
};
Expand Down
52 changes: 23 additions & 29 deletions container/src/container_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ impl ProcessBuffer for RecreateContainerProcessor {
input_complete: bool,
writer: &mut impl Write,
) -> Result<()> {
if self.input_complete && (input.len() > 0 || !input_complete) {
if self.input_complete && (!input.is_empty() || !input_complete) {
return Err(PreflateError::new(
ExitCode::InvalidParameter,
"more data provided after input_complete signaled",
Expand Down Expand Up @@ -176,7 +176,7 @@ impl RecreateContainerProcessor {
loop {
match &mut self.state {
DecompressionState::Start => {
if !self.input_complete && self.input.len() == 0 {
if !self.input_complete && self.input.is_empty() {
break;
}

Expand All @@ -196,7 +196,7 @@ impl RecreateContainerProcessor {
}
DecompressionState::StartSegment => {
// here's a good place to stop if we run out of input
if self.input.len() == 0 {
if self.input.is_empty() {
break;
}

Expand Down Expand Up @@ -292,19 +292,16 @@ impl RecreateContainerProcessor {
}

let lepton_bytes: Vec<u8> = self.input.drain(0..*lepton_length).collect();
match lepton_jpeg::decode_lepton(
if let Err(e) = lepton_jpeg::decode_lepton(
&mut Cursor::new(&lepton_bytes),
writer,
&EnabledFeatures::compat_lepton_vector_read(),
&DEFAULT_THREAD_POOL,
) {
Err(e) => {
return Err(PreflateError::new(
ExitCode::InvalidCompressedWrapper,
format!("JPEG Lepton decode failed: {}", e),
));
}
Ok(_) => {}
return Err(PreflateError::new(
ExitCode::InvalidCompressedWrapper,
format!("JPEG Lepton decode failed: {}", e),
));
}
self.state = DecompressionState::StartSegment;
}
Expand Down Expand Up @@ -513,23 +510,20 @@ fn webp_decompress(
header: &crate::idat_parse::PngHeader,
) -> Result<Vec<u8>> {
#[cfg(feature = "webp")]
match webp::Decoder::new(webp.as_slice()).decode() {
Some(result) => {
use crate::idat_parse::apply_png_filters_with_types;
use std::ops::Deref;

let m = result.deref();

return Ok(apply_png_filters_with_types(
m,
header.width as usize,
header.height as usize,
if result.is_alpha() { 4 } else { 3 },
header.color_type.bytes_per_pixel(),
&filters,
));
}
_ => {}
if let Some(result) = webp::Decoder::new(webp.as_slice()).decode() {
use crate::idat_parse::apply_png_filters_with_types;
use std::ops::Deref;

let m = result.deref();

return Ok(apply_png_filters_with_types(
m,
header.width as usize,
header.height as usize,
if result.is_alpha() { 4 } else { 3 },
header.color_type.bytes_per_pixel(),
filters,
));
}
return err_exit_code(ExitCode::InvalidCompressedWrapper, "Webp decode failed");
err_exit_code(ExitCode::InvalidCompressedWrapper, "Webp decode failed")
}
29 changes: 15 additions & 14 deletions container/src/container_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub(crate) enum ChunkParseState {
/// so we need to keep track of the IHDR chunk so we can use it later to properly
/// compress the PNG data.
Searching(Option<PngHeader>),
DeflateContinue(PreflateStreamProcessor),
DeflateContinue(Box<PreflateStreamProcessor>),
}

/// V2 variant of write_chunk_block: block content goes through the persistent Zstd encoder.
Expand All @@ -55,7 +55,7 @@ pub(crate) fn write_chunk_block_v2(
write_varint(encoder, chunk.corrections.len() as u32)?;
write_varint(encoder, state.plain_text().text().len() as u32)?;
encoder.write_all(&chunk.corrections)?;
encoder.write_all(&state.plain_text().text())?;
encoder.write_all(state.plain_text().text())?;

let compressed_size = emit_compressed_block(
BLOCK_COMPRESSION_ZSTD | BLOCK_TYPE_DEFLATE,
Expand All @@ -68,7 +68,7 @@ pub(crate) fn write_chunk_block_v2(
stats.hash_algorithm = parameters.hash_algorithm;

if !state.is_done() {
return Ok((compressed_size, Some(state)));
return Ok((compressed_size, Some(*state)));
}
Ok((compressed_size, None))
}
Expand Down Expand Up @@ -198,14 +198,14 @@ impl ProcessBuffer for PreflateContainerProcessor {
) -> Result<()> {
use crate::container_common::COMPRESSED_WRAPPER_VERSION_2;

if self.input_complete && (input.len() > 0 || !input_complete) {
if self.input_complete && (!input.is_empty() || !input_complete) {
return Err(PreflateError::new(
ExitCode::InvalidParameter,
"more data provided after input_complete signaled",
));
}

if input.len() > 0 {
if !input.is_empty() {
self.compression_stats.deflate_compressed_size += input.len() as u64;
self.input_crc.update(input);
self.content.extend_from_slice(input);
Expand Down Expand Up @@ -258,7 +258,8 @@ impl ProcessBuffer for PreflateContainerProcessor {
input_complete,
&self.config,
) {
FindStreamResult::Found(next, chunk) => {
FindStreamResult::Found(next, chunk_box) => {
let chunk = *chunk_box;
// the gap between the start and the beginning of the deflate stream
// is written out as a literal block
if next.start != 0 {
Expand All @@ -285,7 +286,7 @@ impl ProcessBuffer for PreflateContainerProcessor {
if let Some(mut state) = next_state {
self.total_plain_text_seen += state.plain_text().len() as u64;
state.shrink_to_dictionary();
self.state = ChunkParseState::DeflateContinue(state);
self.state = ChunkParseState::DeflateContinue(Box::new(state));
}

self.content.drain(0..next.end);
Expand Down Expand Up @@ -359,7 +360,7 @@ impl ProcessBuffer for PreflateContainerProcessor {
write_varint(encoder, res.corrections.len() as u32)?;
write_varint(encoder, state.plain_text().len() as u32)?;
encoder.write_all(&res.corrections)?;
encoder.write_all(&state.plain_text().text())?;
encoder.write_all(state.plain_text().text())?;
let sz = emit_compressed_block(
BLOCK_COMPRESSION_ZSTD | BLOCK_TYPE_DEFLATE_CONTINUE,
encoder,
Expand Down Expand Up @@ -389,7 +390,7 @@ impl ProcessBuffer for PreflateContainerProcessor {
if input_complete && !self.input_complete {
self.input_complete = true;

if self.content.len() > 0 {
if !self.content.is_empty() {
let encoder = self.encoder.as_mut().unwrap();
write_varint(encoder, self.content.len() as u32)?;
encoder.write_all(&self.content)?;
Expand Down Expand Up @@ -478,8 +479,8 @@ fn webp_compress(
let enc = webp::Encoder::new(
&bitmap,
match png_header.color_type {
PngColorType::RGB => webp::PixelLayout::Rgb,
PngColorType::RGBA => webp::PixelLayout::Rgba,
PngColorType::Rgb => webp::PixelLayout::Rgb,
PngColorType::Rgba => webp::PixelLayout::Rgba,
},
png_header.width,
png_header.height,
Expand Down Expand Up @@ -520,15 +521,15 @@ fn webp_compress(
idat.write_to_bytestream(result)?;
result.write_all(&filters)?;

result.write_all(&corrections)?;
result.write_all(corrections)?;
result.write_all(comp.deref())?;

return Ok(());
}
}

return preflate_rs::err_exit_code(
preflate_rs::err_exit_code(
ExitCode::InvalidCompressedWrapper,
"Webp compression not supported",
);
)
}
19 changes: 9 additions & 10 deletions container/src/idat_parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ fn test_idat_header_roundtrip() {
png_header: Some(PngHeader {
width: 5,
height: 5,
color_type: PngColorType::RGB,
color_type: PngColorType::Rgb,
}),
};

Expand All @@ -127,19 +127,19 @@ pub struct PngHeader {
#[derive(Debug, PartialEq, Copy, Clone)]
pub enum PngColorType {
// Grayscale = 0,
RGB = 2,
Rgb = 2,
// 3 = Pallete not supported
//GrayscaleAlpha = 4,
RGBA = 6,
Rgba = 6,
}

impl PngColorType {
fn parse(byte: u8) -> Option<Self> {
match byte {
//0 => Some(PngColorType::Grayscale),
2 => Some(PngColorType::RGB),
2 => Some(PngColorType::Rgb),
//4 => Some(PngColorType::GrayscaleAlpha),
6 => Some(PngColorType::RGBA),
6 => Some(PngColorType::Rgba),
_ => None,
}
}
Expand All @@ -148,9 +148,9 @@ impl PngColorType {
pub fn bytes_per_pixel(&self) -> usize {
match self {
//PngColorType::Grayscale => 1,
PngColorType::RGB => 3,
PngColorType::Rgb => 3,
//PngColorType::GrayscaleAlpha => 2,
PngColorType::RGBA => 4,
PngColorType::Rgba => 4,
}
}
}
Expand Down Expand Up @@ -184,7 +184,7 @@ pub fn parse_ihdr(ihdr_chunk: &[u8]) -> Result<PngHeader> {
})
} else {
log::debug!("IHDR unsupported color type {}", ihdr_data[9]);
return err_exit_code(ExitCode::InvalidIDat, "IHDR unsupported color type");
err_exit_code(ExitCode::InvalidIDat, "IHDR unsupported color type")
}
}

Expand Down Expand Up @@ -412,8 +412,7 @@ pub fn apply_png_filters_with_types(
let mut rgba = vec![0u8; target_stride];
let mut encoded = vec![0u8; target_stride];

for row in 0..height {
let filter_type = filter_types[row];
for (row, &filter_type) in filter_types.iter().enumerate().take(height) {
let offset = row * source_stride;

let scanline = if source_bbp == 3 && target_bpp == 4 {
Expand Down
Loading
Loading