Skip to content

Commit

Permalink
Merge pull request #996 from HeroicKatora/style-fix
Browse files Browse the repository at this point in the history
Style fixes
  • Loading branch information
HeroicKatora authored Jul 24, 2019
2 parents 9fc1f01 + 1b8e277 commit a4cf9b5
Show file tree
Hide file tree
Showing 20 changed files with 271 additions and 272 deletions.
4 changes: 2 additions & 2 deletions src/animation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ use image::ImageResult;

/// An implementation dependent iterator, reading the frames as requested
pub struct Frames<'a> {
iterator: Box<Iterator<Item = ImageResult<Frame>> + 'a>
iterator: Box<dyn Iterator<Item = ImageResult<Frame>> + 'a>
}

impl<'a> Frames<'a> {
/// Creates a new `Frames` from an implementation specific iterator.
pub fn new(iterator: Box<Iterator<Item = ImageResult<Frame>> + 'a>) -> Self {
pub fn new(iterator: Box<dyn Iterator<Item = ImageResult<Frame>> + 'a>) -> Self {
Frames { iterator }
}

Expand Down
100 changes: 50 additions & 50 deletions src/bmp/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,19 +217,19 @@ where

if !top_down {
for row in buffer.chunks_mut(row_width).rev() {
try!(func(row));
func(row)?;
}

// If we need more space, extend the buffer.
if buffer.len() < full_image_size {
let new_space = extend_buffer(buffer, full_image_size, false);
for row in new_space.chunks_mut(row_width).rev() {
try!(func(row));
func(row)?;
}
}
} else {
for row in buffer.chunks_mut(row_width) {
try!(func(row));
func(row)?;
}
if buffer.len() < full_image_size {
// If the image is stored in top-down order, we can simply use the extend function
Expand All @@ -238,7 +238,7 @@ where
buffer.extend(repeat(0xFF).take(extend));
let len = buffer.len();
for row in buffer[len - row_width..].chunks_mut(row_width) {
try!(func(row));
func(row)?;
}
};
}
Expand Down Expand Up @@ -410,10 +410,10 @@ impl Bitfields {
max_len: u32,
) -> ImageResult<Bitfields> {
let bitfields = Bitfields {
r: try!(Bitfield::from_mask(r_mask, max_len)),
g: try!(Bitfield::from_mask(g_mask, max_len)),
b: try!(Bitfield::from_mask(b_mask, max_len)),
a: try!(Bitfield::from_mask(a_mask, max_len)),
r: Bitfield::from_mask(r_mask, max_len)?,
g: Bitfield::from_mask(g_mask, max_len)?,
b: Bitfield::from_mask(b_mask, max_len)?,
a: Bitfield::from_mask(a_mask, max_len)?,
};
if bitfields.r.len == 0 || bitfields.g.len == 0 || bitfields.b.len == 0 {
return Err(ImageError::FormatError("Missing bitfield mask".to_string()));
Expand Down Expand Up @@ -571,7 +571,7 @@ impl<R: Read + Seek> BMPDecoder<R> {
return Ok(());
}
let mut signature = [0; 2];
try!(self.r.read_exact(&mut signature));
self.r.read_exact(&mut signature)?;

if signature != b"BM"[..] {
return Err(ImageError::FormatError(
Expand All @@ -581,8 +581,8 @@ impl<R: Read + Seek> BMPDecoder<R> {

// The next 8 bytes represent file size, followed the 4 reserved bytes
// We're not interesting these values
try!(self.r.read_u32::<LittleEndian>());
try!(self.r.read_u32::<LittleEndian>());
self.r.read_u32::<LittleEndian>()?;
self.r.read_u32::<LittleEndian>()?;

self.data_offset = u64::from(self.r.read_u32::<LittleEndian>()?);

Expand Down Expand Up @@ -611,7 +611,7 @@ impl<R: Read + Seek> BMPDecoder<R> {
));
}

self.bit_count = try!(self.r.read_u16::<LittleEndian>());
self.bit_count = self.r.read_u16::<LittleEndian>()?;
self.image_type = match self.bit_count {
1 | 4 | 8 => ImageType::Palette,
24 => ImageType::RGB24,
Expand All @@ -626,8 +626,8 @@ impl<R: Read + Seek> BMPDecoder<R> {
///
/// returns Err if any of the values are invalid.
fn read_bitmap_info_header(&mut self) -> ImageResult<()> {
self.width = try!(self.r.read_i32::<LittleEndian>());
self.height = try!(self.r.read_i32::<LittleEndian>());
self.width = self.r.read_i32::<LittleEndian>()?;
self.height = self.r.read_i32::<LittleEndian>()?;

// Width can not be negative
if self.width < 0 {
Expand Down Expand Up @@ -661,8 +661,8 @@ impl<R: Read + Seek> BMPDecoder<R> {
));
}

self.bit_count = try!(self.r.read_u16::<LittleEndian>());
let image_type_u32 = try!(self.r.read_u32::<LittleEndian>());
self.bit_count = self.r.read_u16::<LittleEndian>()?;
let image_type_u32 = self.r.read_u32::<LittleEndian>()?;

// Top-down dibs can not be compressed.
if self.top_down && image_type_u32 != 0 && image_type_u32 != 3 {
Expand Down Expand Up @@ -719,23 +719,23 @@ impl<R: Read + Seek> BMPDecoder<R> {
// followed the horizontal and vertical printing resolutions
// We will calculate the pixel array size using width & height of image
// We're not interesting the horz or vert printing resolutions
try!(self.r.read_u32::<LittleEndian>());
try!(self.r.read_u32::<LittleEndian>());
try!(self.r.read_u32::<LittleEndian>());
self.r.read_u32::<LittleEndian>()?;
self.r.read_u32::<LittleEndian>()?;
self.r.read_u32::<LittleEndian>()?;

self.colors_used = try!(self.r.read_u32::<LittleEndian>());
self.colors_used = self.r.read_u32::<LittleEndian>()?;

// The next 4 bytes represent number of "important" colors
// We're not interested in this value, so we'll skip it
try!(self.r.read_u32::<LittleEndian>());
self.r.read_u32::<LittleEndian>()?;

Ok(())
}

fn read_bitmasks(&mut self) -> ImageResult<()> {
let r_mask = try!(self.r.read_u32::<LittleEndian>());
let g_mask = try!(self.r.read_u32::<LittleEndian>());
let b_mask = try!(self.r.read_u32::<LittleEndian>());
let r_mask = self.r.read_u32::<LittleEndian>()?;
let g_mask = self.r.read_u32::<LittleEndian>()?;
let b_mask = self.r.read_u32::<LittleEndian>()?;

let a_mask = match self.bmp_header_type {
BMPHeaderType::V3 | BMPHeaderType::V4 | BMPHeaderType::V5 => {
Expand Down Expand Up @@ -763,9 +763,9 @@ impl<R: Read + Seek> BMPDecoder<R> {

fn read_metadata(&mut self) -> ImageResult<()> {
if !self.has_loaded_metadata {
try!(self.read_file_header());
let bmp_header_offset = try!(self.r.seek(SeekFrom::Current(0)));
let bmp_header_size = try!(self.r.read_u32::<LittleEndian>());
self.read_file_header()?;
let bmp_header_offset = self.r.seek(SeekFrom::Current(0))?;
let bmp_header_size = self.r.read_u32::<LittleEndian>()?;
let bmp_header_end = bmp_header_offset + u64::from(bmp_header_size);

self.bmp_header_type = match bmp_header_size {
Expand All @@ -784,32 +784,32 @@ impl<R: Read + Seek> BMPDecoder<R> {

match self.bmp_header_type {
BMPHeaderType::Core => {
try!(self.read_bitmap_core_header());
self.read_bitmap_core_header()?;
}
BMPHeaderType::Info
| BMPHeaderType::V2
| BMPHeaderType::V3
| BMPHeaderType::V4
| BMPHeaderType::V5 => {
try!(self.read_bitmap_info_header());
self.read_bitmap_info_header()?;
}
};

match self.image_type {
ImageType::Bitfields16 | ImageType::Bitfields32 => try!(self.read_bitmasks()),
ImageType::Bitfields16 | ImageType::Bitfields32 => self.read_bitmasks()?,
_ => {}
};

try!(self.r.seek(SeekFrom::Start(bmp_header_end)));
self.r.seek(SeekFrom::Start(bmp_header_end))?;

match self.image_type {
ImageType::Palette | ImageType::RLE4 | ImageType::RLE8 => try!(self.read_palette()),
ImageType::Palette | ImageType::RLE4 | ImageType::RLE8 => self.read_palette()?,
_ => {}
};

if self.no_file_header {
// Use the offset of the end of metadata instead of reading a BMP file header.
self.data_offset = try!(self.r.seek(SeekFrom::Current(0)));
self.data_offset = self.r.seek(SeekFrom::Current(0))?;
}

self.has_loaded_metadata = true;
Expand All @@ -822,7 +822,7 @@ impl<R: Read + Seek> BMPDecoder<R> {
pub fn read_metadata_in_ico_format(&mut self) -> ImageResult<()> {
self.no_file_header = true;
self.add_alpha_channel = true;
try!(self.read_metadata());
self.read_metadata()?;

// The height field in an ICO file is doubled to account for the AND mask
// (whether or not an AND mask is actually present).
Expand Down Expand Up @@ -856,7 +856,7 @@ impl<R: Read + Seek> BMPDecoder<R> {
const MAX_PALETTE_SIZE: usize = 256; // Palette indices are u8.

let bytes_per_color = self.bytes_per_color();
let palette_size = try!(self.get_palette_size());
let palette_size = self.get_palette_size()?;
let max_length = MAX_PALETTE_SIZE * bytes_per_color;

let length = palette_size * bytes_per_color;
Expand All @@ -866,15 +866,15 @@ impl<R: Read + Seek> BMPDecoder<R> {
// We limit the buffer to at most 256 colours to avoid any oom issues as
// 8-bit images can't reference more than 256 indexes anyhow.
buf.resize(cmp::min(length, max_length), 0);
try!(self.r.by_ref().read_exact(&mut buf));
self.r.by_ref().read_exact(&mut buf)?;

// Allocate 256 entries even if palette_size is smaller, to prevent corrupt files from
// causing an out-of-bounds array access.
if length < max_length {
buf.resize(max_length, 0);
} else if length > max_length {
// Ignore any excess palette colors.
try!(self.r.seek(SeekFrom::Current((length - max_length) as i64)));
self.r.seek(SeekFrom::Current((length - max_length) as i64))?;
};

let p: Vec<(u8, u8, u8)> = (0..MAX_PALETTE_SIZE)
Expand Down Expand Up @@ -936,7 +936,7 @@ impl<R: Read + Seek> BMPDecoder<R> {
let reader = &mut self.r;
let width = self.width as usize;

try!(reader.seek(SeekFrom::Start(self.data_offset)));
reader.seek(SeekFrom::Start(self.data_offset))?;

try!(with_rows(
&mut pixel_data,
Expand All @@ -945,7 +945,7 @@ impl<R: Read + Seek> BMPDecoder<R> {
num_channels,
self.top_down,
|row| {
try!(reader.read_exact(&mut indices));
reader.read_exact(&mut indices)?;
let mut pixel_iter = row.chunks_mut(num_channels);
match bit_count {
1 => {
Expand Down Expand Up @@ -980,7 +980,7 @@ impl<R: Read + Seek> BMPDecoder<R> {
};
let reader = &mut self.r;

try!(reader.seek(SeekFrom::Start(self.data_offset)));
reader.seek(SeekFrom::Start(self.data_offset))?;

try!(with_rows(
&mut pixel_data,
Expand Down Expand Up @@ -1014,7 +1014,7 @@ impl<R: Read + Seek> BMPDecoder<R> {
let bitfields = self.bitfields.as_ref().unwrap();

let reader = &mut self.r;
try!(reader.seek(SeekFrom::Start(self.data_offset)));
reader.seek(SeekFrom::Start(self.data_offset))?;

try!(with_rows(
&mut pixel_data,
Expand All @@ -1024,7 +1024,7 @@ impl<R: Read + Seek> BMPDecoder<R> {
self.top_down,
|row| {
for pixel in row.chunks_mut(num_channels) {
let data = try!(reader.read_u32::<LittleEndian>());
let data = reader.read_u32::<LittleEndian>()?;

pixel[0] = bitfields.r.read(data);
pixel[1] = bitfields.g.read(data);
Expand All @@ -1050,7 +1050,7 @@ impl<R: Read + Seek> BMPDecoder<R> {
};
let row_padding = &mut [0; 4][..row_padding_len];

try!(self.r.seek(SeekFrom::Start(self.data_offset)));
self.r.seek(SeekFrom::Start(self.data_offset))?;

let reader = &mut self.r;

Expand All @@ -1063,22 +1063,22 @@ impl<R: Read + Seek> BMPDecoder<R> {
|row| {
for pixel in row.chunks_mut(num_channels) {
if *format == FormatFullBytes::Format888 {
try!(reader.read_u8());
reader.read_u8()?;
}

// Read the colour values (b, g, r).
// Reading 3 bytes and reversing them is significantly faster than reading one
// at a time.
try!(reader.read_exact(&mut pixel[0..3]));
reader.read_exact(&mut pixel[0..3])?;
pixel[0..3].reverse();

if *format == FormatFullBytes::RGB32 {
try!(reader.read_u8());
reader.read_u8()?;
}

// Read the alpha channel if present
if *format == FormatFullBytes::RGBA32 {
try!(reader.read_exact(&mut pixel[3..4]));
reader.read_exact(&mut pixel[3..4])?;
}
}
reader.read_exact(row_padding)
Expand All @@ -1090,7 +1090,7 @@ impl<R: Read + Seek> BMPDecoder<R> {

fn read_rle_data(&mut self, image_type: ImageType) -> ImageResult<Vec<u8>> {
// Seek to the start of the actual image data.
try!(self.r.seek(SeekFrom::Start(self.data_offset)));
self.r.seek(SeekFrom::Start(self.data_offset))?;

let full_image_size = try!(
num_bytes(self.width, self.height, self.num_channels()).ok_or_else(|| {
Expand All @@ -1099,15 +1099,15 @@ impl<R: Read + Seek> BMPDecoder<R> {
);
let mut pixel_data = self.create_pixel_data();
let (skip_pixels, skip_rows, eof_hit) =
try!(self.read_rle_data_step(&mut pixel_data, image_type, 0, 0));
self.read_rle_data_step(&mut pixel_data, image_type, 0, 0)?;
// Extend the buffer if there is still data left.
// If eof_hit is true, it means that we hit an end-of-file marker in the last step and
// we won't extend the buffer further to avoid small files with a large specified size causing memory issues.
// This is only a rudimentary check, a file could still create a large buffer, but the
// file would now have to at least have some data in it.
if pixel_data.len() < full_image_size && !eof_hit {
let new = extend_buffer(&mut pixel_data, full_image_size, true);
try!(self.read_rle_data_step(new, image_type, skip_pixels, skip_rows));
self.read_rle_data_step(new, image_type, skip_pixels, skip_rows)?;
}
Ok(pixel_data)
}
Expand Down
Loading

0 comments on commit a4cf9b5

Please sign in to comment.