Avoid cloning jpeg_tables and instead pass Arc<Vec<u8>>
This commit is contained in:
1 parent
9e6f9771d3
commit
623a3d957d
3 files changed
+75
-88
No files matched your search
+11
-3
@@ -1,4 +1,5 @@
|
||||
use super::ifd::{Directory, Value};
|
||||
use super::stream::{add_app14segment, JpegTagApp14Transform};
|
||||
use super::tag_reader::TagReader;
|
||||
use super::Limits;
|
||||
use super::{stream::SmartReader, ChunkType};
|
||||
@@ -6,6 +7,7 @@ use crate::tags::{CompressionMethod, PhotometricInterpretation, Predictor, Sampl
|
||||
use crate::{TiffError, TiffFormatError, TiffResult, TiffUnsupportedError};
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
use std::io::{Read, Seek};
|
||||
use std::sync::Arc;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub(crate) struct StripDecodeState {
|
||||
@@ -87,7 +89,7 @@ pub(crate) struct Image {
|
||||
pub photometric_interpretation: PhotometricInterpretation,
|
||||
pub compression_method: CompressionMethod,
|
||||
pub predictor: Predictor,
|
||||
pub jpeg_tables: Option<Vec<u8>>,
|
||||
pub jpeg_tables: Option<Arc<Vec<u8>>>,
|
||||
pub chunk_type: ChunkType,
|
||||
pub strip_decoder: Option<StripDecodeState>,
|
||||
pub tile_attributes: Option<TileAttributes>,
|
||||
@@ -130,7 +132,7 @@ impl Image {
|
||||
let jpeg_tables = if compression_method == CompressionMethod::ModernJPEG
|
||||
&& ifd.contains_key(&Tag::JPEGTables)
|
||||
{
|
||||
let vec = tag_reader
|
||||
let mut vec = tag_reader
|
||||
.find_tag(Tag::JPEGTables)?
|
||||
.unwrap()
|
||||
.into_u8_vec()?;
|
||||
@@ -139,7 +141,13 @@ impl Image {
|
||||
TiffFormatError::InvalidTagValueType(Tag::JPEGTables),
|
||||
));
|
||||
}
|
||||
Some(vec)
|
||||
|
||||
// TODO: Can we avoid this somehow?
|
||||
if photometric_interpretation == PhotometricInterpretation::RGB {
|
||||
add_app14segment(&mut vec, JpegTagApp14Transform::App14TransformUnknown)
|
||||
}
|
||||
|
||||
Some(Arc::new(vec))
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
+8
-70
@@ -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(),
|
||||
}
|
||||
}
|
||||
}
|
||||
+56
-15
@@ -1,7 +1,8 @@
|
||||
//! All IO functionality needed for TIFF decoding
|
||||
|
||||
use std::convert::TryFrom;
|
||||
use std::io::{self, BufRead, BufReader, Read, Seek, Take};
|
||||
use std::io::{self, BufRead, BufReader, Read, Seek, SeekFrom, Take};
|
||||
use std::sync::Arc;
|
||||
|
||||
use crate::tags::PhotometricInterpretation;
|
||||
|
||||
@@ -189,7 +190,13 @@ impl<R: Read> Read for LZWReader<R> {
|
||||
///
|
||||
|
||||
pub(crate) struct JpegReader {
|
||||
jpeg_tables: Option<Arc<Vec<u8>>>,
|
||||
|
||||
photometric_interpretation: Option<PhotometricInterpretation>,
|
||||
|
||||
buffer: io::Cursor<Vec<u8>>,
|
||||
|
||||
offset: usize,
|
||||
}
|
||||
|
||||
impl JpegReader {
|
||||
@@ -208,7 +215,7 @@ impl JpegReader {
|
||||
pub fn new<R>(
|
||||
reader: &mut SmartReader<R>,
|
||||
length: u64,
|
||||
jpeg_tables: Option<Vec<u8>>,
|
||||
jpeg_tables: Option<Arc<Vec<u8>>>,
|
||||
photometric_interpretation: &PhotometricInterpretation,
|
||||
) -> io::Result<JpegReader>
|
||||
where
|
||||
@@ -220,11 +227,11 @@ impl JpegReader {
|
||||
reader.read_exact(&mut segment[..])?;
|
||||
|
||||
match jpeg_tables {
|
||||
Some(mut jpeg_data) => {
|
||||
Some(jpeg_tables) => {
|
||||
assert!(
|
||||
jpeg_data.len() >= 2,
|
||||
jpeg_tables.len() >= 2,
|
||||
"jpeg_tables, if given, must be at least 2 bytes long. Got {:?}",
|
||||
jpeg_data
|
||||
jpeg_tables
|
||||
);
|
||||
|
||||
assert!(
|
||||
@@ -233,33 +240,43 @@ impl JpegReader {
|
||||
length
|
||||
);
|
||||
|
||||
if photometric_interpretation == &PhotometricInterpretation::RGB {
|
||||
add_app14segment(&mut jpeg_data, JpegTagApp14Transform::App14TransformUnknown)
|
||||
}
|
||||
// if photometric_interpretation == &PhotometricInterpretation::RGB {
|
||||
// add_app14segment(&mut jpeg_data, JpegTagApp14Transform::App14TransformUnknown)
|
||||
// }
|
||||
|
||||
let truncated_length = jpeg_data.len() - 2;
|
||||
jpeg_data.truncate(truncated_length);
|
||||
jpeg_data.extend_from_slice(&segment[2..]);
|
||||
// let truncated_length = jpeg_data.len() - 2;
|
||||
// jpeg_data.truncate(truncated_length);
|
||||
// jpeg_data.extend_from_slice(&segment[2..]);
|
||||
|
||||
let mut buffer = io::Cursor::new(segment);
|
||||
// Skip the first two bytes
|
||||
buffer.seek(SeekFrom::Start(2))?;
|
||||
|
||||
Ok(JpegReader {
|
||||
buffer: io::Cursor::new(jpeg_data),
|
||||
buffer,
|
||||
jpeg_tables: Some(jpeg_tables),
|
||||
photometric_interpretation: Some(*photometric_interpretation),
|
||||
offset: 0,
|
||||
})
|
||||
}
|
||||
None => Ok(JpegReader {
|
||||
buffer: io::Cursor::new(segment),
|
||||
jpeg_tables: None,
|
||||
photometric_interpretation: None,
|
||||
offset: 0,
|
||||
}),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[repr(u8)]
|
||||
enum JpegTagApp14Transform {
|
||||
pub(crate) enum JpegTagApp14Transform {
|
||||
// App14TransformYCCK = 2,
|
||||
// App14TransformYCbCr = 1,
|
||||
App14TransformUnknown = 0,
|
||||
}
|
||||
|
||||
fn add_app14segment(jpeg_tables: &mut Vec<u8>, transform: JpegTagApp14Transform) {
|
||||
pub(crate) fn add_app14segment(jpeg_tables: &mut Vec<u8>, transform: JpegTagApp14Transform) {
|
||||
// Add JPEG Tag APP14 Adobe segment to jpeg_tables.
|
||||
// This segment stores image encoding information for DCT filters.
|
||||
// When `transform` value is 0 which is defined as Unknown, jpeg-decoder interpret the
|
||||
@@ -307,7 +324,31 @@ fn add_app14segment(jpeg_tables: &mut Vec<u8>, transform: JpegTagApp14Transform)
|
||||
impl Read for JpegReader {
|
||||
// #[inline]
|
||||
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
|
||||
self.buffer.read(buf)
|
||||
let mut start = 0;
|
||||
|
||||
if let Some(jpeg_tables) = &self.jpeg_tables {
|
||||
if jpeg_tables.len() - 2 > self.offset {
|
||||
// Read (rest of) jpeg_tables to buf (without the last two bytes)
|
||||
let size_remaining = jpeg_tables.len() - self.offset - 2;
|
||||
let to_copy = size_remaining.min(buf.len());
|
||||
|
||||
buf[start..start + to_copy]
|
||||
.copy_from_slice(&jpeg_tables[self.offset..self.offset + to_copy]);
|
||||
|
||||
self.offset += to_copy;
|
||||
|
||||
if to_copy == buf.len() {
|
||||
return Ok(to_copy);
|
||||
}
|
||||
|
||||
start += to_copy;
|
||||
}
|
||||
}
|
||||
|
||||
let read = self.buffer.read(&mut buf[start..])?;
|
||||
self.offset += read;
|
||||
|
||||
Ok(read + start)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user