Avoid cloning jpeg_tables and instead pass Arc<Vec<u8>>

This commit is contained in:
Alan Race committed 2022-04-30 14:33:12 +02:00
1 parent 9e6f9771d3
commit 623a3d957d
3 files changed
+75 -88

No files matched your search

+8 -70
View File
@@ -1,6 +1,7 @@
use std::collections::{HashMap, HashSet};
use std::convert::TryFrom;
use std::io::{self, Cursor, Read, Seek};
use std::sync::Arc;
use std::{cmp, ops::Range};
use crate::{
@@ -1151,7 +1152,7 @@ impl<R: Read + Seek> Decoder<R> {
photometric_interpretation: PhotometricInterpretation,
compression_method: CompressionMethod,
compressed_length: u64,
jpeg_tables: Option<Vec<u8>>,
jpeg_tables: Option<Arc<Vec<u8>>>,
) -> TiffResult<Box<dyn Read + 'r>> {
Ok(match compression_method {
CompressionMethod::None => Box::new(reader),
@@ -1228,70 +1229,9 @@ impl<R: Read + Seek> Decoder<R> {
Ok(u32::try_from(self.image().chunk_offsets.len())?)
}
#[deprecated = "Use read_image instead"]
pub fn read_jpeg(&mut self) -> TiffResult<DecodingResult> {
self.check_chunk_type(ChunkType::Strip)?;
if self.image().chunk_offsets.len() == 0 {
return Err(TiffError::FormatError(TiffFormatError::RequiredTagEmpty(
Tag::StripOffsets,
)));
}
if self.image().chunk_offsets[0] as usize > self.limits.intermediate_buffer_size
|| self
.image()
.chunk_bytes
.iter()
.any(|&x| x as usize > self.limits.intermediate_buffer_size)
{
return Err(TiffError::LimitsExceeded);
}
let mut res_img = Vec::with_capacity(self.image().chunk_offsets[0] as usize);
for idx in 0..self.image.chunk_offsets.len() {
let offset = self.image.chunk_offsets[idx];
let length = self.image.chunk_bytes[idx];
self.goto_offset_u64(offset)?;
if self.image().jpeg_tables.is_some() && length < 2 {
return Err(TiffError::FormatError(
TiffFormatError::InvalidTagValueType(Tag::JPEGTables),
));
}
let jpeg_tables = self.image().jpeg_tables.clone();
let photometric_interpretation = self.image().photometric_interpretation;
let jpeg_reader = JpegReader::new(
&mut self.reader,
length,
jpeg_tables,
&photometric_interpretation,
)?;
let mut decoder = jpeg::Decoder::new(jpeg_reader);
match decoder.decode() {
Ok(mut val) => res_img.append(&mut val),
Err(e) => {
return match e {
jpeg::Error::Io(io_err) => Err(TiffError::IoError(io_err)),
jpeg::Error::Format(fmt_err) => {
Err(TiffError::FormatError(TiffFormatError::Format(fmt_err)))
}
jpeg::Error::Unsupported(_) => Err(TiffError::UnsupportedError(
TiffUnsupportedError::UnknownInterpretation,
)),
jpeg::Error::Internal(_) => Err(TiffError::UnsupportedError(
TiffUnsupportedError::UnknownInterpretation,
)),
}
}
}
}
Ok(DecodingResult::U8(res_img))
self.read_image()
}
pub fn read_strip_to_buffer(&mut self, mut buffer: DecodingBuffer) -> TiffResult<()> {
@@ -1546,11 +1486,9 @@ impl<R: Read + Seek> Decoder<R> {
/// Decodes the entire image and return it as a Vector
pub fn read_image(&mut self) -> TiffResult<DecodingResult> {
let result = match self.image().chunk_type {
ChunkType::Strip => self.read_stripped_image()?,
ChunkType::Tile => self.read_tiled_image()?,
};
Ok(result)
match self.image().chunk_type {
ChunkType::Strip => self.read_stripped_image(),
ChunkType::Tile => self.read_tiled_image(),
}
}
}