Skip to content

Commit

Permalink
Merge branch 'fix/formatting'
Browse files Browse the repository at this point in the history
  • Loading branch information
Roughsketch committed Jun 17, 2024
2 parents ccd59d3 + eb46c00 commit 2280315
Show file tree
Hide file tree
Showing 27 changed files with 542 additions and 125 deletions.
12 changes: 7 additions & 5 deletions benches/parse_benchmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ fn size_benchmarks(c: &mut Criterion) {
group.bench_with_input(
BenchmarkId::from_parameter(paths.len()),
&paths,
|b, paths| b.iter(||
for path in paths {
let _ = imagesize::size(black_box(path));
}
),
|b, paths| {
b.iter(|| {
for path in paths {
let _ = imagesize::size(black_box(path));
}
})
},
);

group.finish();
Expand Down
80 changes: 41 additions & 39 deletions src/container/heif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,42 +96,41 @@ pub fn matches<R: BufRead + Seek>(header: &[u8], reader: &mut R) -> Option<Compr

let brand: [u8; 4] = header[8..12].try_into().unwrap();

// case 1: { heic, ... }
if let Some(v) = inner_matches(&brand) {
Some(v)

// case 2: { msf1, version, heic, msf1, ... }
// brand brand2 brand3
// case 3: { msf1, version, msf1, heic, ... }
// brand brand2 brand3
} else {
// REFS: https://github.com/nokiatech/heif/blob/be43efdf273ae9cf90e552b99f16ac43983f3d19/srcs/reader/heifreaderimpl.cpp#L738
let m_brands = [b"mif1", b"msf1", b"mif2", b"miaf"];

if m_brands.contains(&&brand) {
let mut buf = [0; 12];

if reader.read_exact(&mut buf).is_err() {
return Some(Compression::Unknown);
}
if let Some(compression) = inner_matches(&brand) {
// case 1: { heic, ... }
return Some(compression);
}

let brand2: [u8; 4] = buf[4..8].try_into().unwrap();
let brand3: [u8; 4] = buf[8..12].try_into().unwrap();
// REFS: https://github.com/nokiatech/heif/blob/be43efdf273ae9cf90e552b99f16ac43983f3d19/srcs/reader/heifreaderimpl.cpp#L738
let brands = [b"mif1", b"msf1", b"mif2", b"miaf"];

// case 2
if let Some(v) = inner_matches(&brand2) {
return Some(v);
if brands.contains(&&brand) {
let mut buf = [0; 12];

// case 3
} else if m_brands.contains(&&brand2) {
if let Some(v) = inner_matches(&brand3) {
return Some(v);
}
}
if reader.read_exact(&mut buf).is_err() {
return Some(Compression::Unknown);
}

let brand2: [u8; 4] = buf[4..8].try_into().unwrap();

if let Some(compression) = inner_matches(&brand2) {
// case 2: { msf1, version, heic, msf1, ... }
// brand brand2 brand3
return Some(compression);
}

Some(Compression::Unknown)
if brands.contains(&&brand2) {
// case 3: { msf1, version, msf1, heic, ... }
// brand brand2 brand3
let brand3: [u8; 4] = buf[8..12].try_into().unwrap();

if let Some(compression) = inner_matches(&brand3) {
return Some(compression);
}
}
}

Some(Compression::Unknown)
}

fn inner_matches(brand: &[u8; 4]) -> Option<Compression> {
Expand Down Expand Up @@ -159,16 +158,19 @@ fn inner_matches(brand: &[u8; 4]) -> Option<Compression> {
// REFS: https://github.com/nokiatech/heif/blob/be43efdf273ae9cf90e552b99f16ac43983f3d19/srcs/reader/heifreaderimpl.cpp#L1415
// REFS: https://github.com/nokiatech/heif/blob/be43efdf273ae9cf90e552b99f16ac43983f3d19/srcs/api-cpp/ImageItem.h#L37
// let feature_brands = [b"pred", b"auxl", b"thmb", b"base", b"dimg"];
if hevc_brands.contains(&brand) {
return Some(Compression::Hevc);
}

Some(if hevc_brands.contains(&brand) {
Compression::Hevc
} else if av1_brands.contains(&brand) {
Compression::Av1
} else if jpeg_brands.contains(&brand) {
Compression::Jpeg
} else {
return None;
})
if av1_brands.contains(&brand) {
return Some(Compression::Av1);
}

if jpeg_brands.contains(&brand) {
return Some(Compression::Jpeg);
}

None
}

fn skip_to_tag<R: BufRead + Seek>(reader: &mut R, tag: &[u8]) -> ImageResult<u32> {
Expand Down
5 changes: 4 additions & 1 deletion src/formats/gif.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::{util::{read_u16, Endian}, ImageResult, ImageSize};
use crate::{
util::{read_u16, Endian},
ImageResult, ImageSize,
};
use std::io::{BufRead, Seek, SeekFrom};

pub fn size<R: BufRead + Seek>(reader: &mut R) -> ImageResult<ImageSize> {
Expand Down
6 changes: 5 additions & 1 deletion src/formats/hdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ pub fn size<R: BufRead + Seek>(reader: &mut R) -> ImageResult<ImageSize> {
// means that no matter whether the line starts with X or Y, it will be read as height then width.

// Extract width and height information
if line.starts_with("-Y") || line.starts_with("+Y") || line.starts_with("-X") || line.starts_with("+X") {
if line.starts_with("-Y")
|| line.starts_with("+Y")
|| line.starts_with("-X")
|| line.starts_with("+X")
{
let dimensions: Vec<&str> = line.split_whitespace().collect();
if dimensions.len() != 4 {
return Err(io::Error::new(
Expand Down
22 changes: 13 additions & 9 deletions src/formats/tga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ pub fn matches<R: BufRead + Seek>(header: &[u8], reader: &mut R) -> bool {
is_tga(reader, image_type, colormap_type).unwrap_or(false)
}

fn is_tga<R: BufRead + Seek>(reader: &mut R, image_type: u8, colormap_type: u8) -> ImageResult<bool> {
fn is_tga<R: BufRead + Seek>(
reader: &mut R,
image_type: u8,
colormap_type: u8,
) -> ImageResult<bool> {
// Attempt to go to footer section. This also doubles as a size check since
// if there aren't 18 bytes available it will return an error.
reader.seek(SeekFrom::End(-18))?;
Expand All @@ -54,8 +58,8 @@ fn is_tga<R: BufRead + Seek>(reader: &mut R, image_type: u8, colormap_type: u8)
return Ok(true);
}

// Now we're into heuristic territory.
// With no footer I don't believe there is a 100% way to verify whether given bytes
// Now we're into heuristic territory.
// With no footer I don't believe there is a 100% way to verify whether given bytes
// are a TGA or not. To get around this we add a few corroborating byte checks and
// if they make up a valid TGA configuration we assume that it's a TGA.

Expand All @@ -82,12 +86,12 @@ fn is_tga<R: BufRead + Seek>(reader: &mut R, image_type: u8, colormap_type: u8)
}

// Assume color map sizes must be a multiple of 8
if colormap_type == 1 &&
(colormap_size != 0 &&
colormap_size != 8 &&
colormap_size != 16 &&
colormap_size != 24 &&
colormap_size != 32)
if colormap_type == 1
&& (colormap_size != 0
&& colormap_size != 8
&& colormap_size != 16
&& colormap_size != 24
&& colormap_size != 32)
{
return Ok(false);
}
Expand Down
23 changes: 18 additions & 5 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ pub fn read_tag<R: BufRead + Seek>(reader: &mut R) -> ImageResult<(String, usize
Ok((String::from_utf8_lossy(&tag_buf).into_owned(), size))
}

pub fn read_until_capped<R: BufRead>(reader: &mut R, delimiter: u8, max_size: usize) -> io::Result<Vec<u8>> {
pub fn read_until_capped<R: BufRead>(
reader: &mut R,
delimiter: u8,
max_size: usize,
) -> io::Result<Vec<u8>> {
let mut bytes = Vec::new();
let mut amount_read = 0;

Expand All @@ -92,7 +96,10 @@ pub fn read_until_capped<R: BufRead>(reader: &mut R, delimiter: u8, max_size: us
}

if amount_read >= max_size {
return Err(io::Error::new(io::ErrorKind::InvalidData, format!("Delimiter not found within {} bytes", max_size)));
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("Delimiter not found within {} bytes", max_size),
));
}

Ok(bytes)
Expand All @@ -108,7 +115,7 @@ pub fn read_until_whitespace<R: BufRead>(reader: &mut R, max_size: usize) -> io:

while amount_read < max_size {
amount_read += 1;

let mut byte = [0; 1];
reader.read_exact(&mut byte)?;

Expand All @@ -127,7 +134,10 @@ pub fn read_until_whitespace<R: BufRead>(reader: &mut R, max_size: usize) -> io:
}

if amount_read >= max_size {
return Err(io::Error::new(io::ErrorKind::InvalidData, format!("Delimiter not found within {} bytes", max_size)));
return Err(io::Error::new(
io::ErrorKind::InvalidData,
format!("Delimiter not found within {} bytes", max_size),
));
}

String::from_utf8(bytes).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
Expand All @@ -138,7 +148,10 @@ pub fn read_line_capped<R: BufRead>(reader: &mut R, max_size: usize) -> io::Resu
String::from_utf8(bytes).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
}

pub fn read_null_terminated_string<R: BufRead>(reader: &mut R, max_size: usize) -> io::Result<String> {
pub fn read_null_terminated_string<R: BufRead>(
reader: &mut R,
max_size: usize,
) -> io::Result<String> {
let bytes = read_until_capped(reader, 0, max_size)?;
String::from_utf8(bytes).map_err(|e| io::Error::new(io::ErrorKind::InvalidData, e))
}
16 changes: 14 additions & 2 deletions tests/aseprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,21 @@ use imagesize::{size, ImageSize};
fn aseprite_test() {
let dim = size("tests/images/aseprite/1.ase").unwrap();

assert_eq!(dim, ImageSize { width: 23, height: 1 });
assert_eq!(
dim,
ImageSize {
width: 23,
height: 1
}
);

let dim = size("tests/images/aseprite/2.ase").unwrap();

assert_eq!(dim, ImageSize { width: 10, height: 20 });
assert_eq!(
dim,
ImageSize {
width: 10,
height: 20
}
);
}
19 changes: 17 additions & 2 deletions tests/blob.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,20 @@ use imagesize::{blob_size, ImageSize};
#[test]
fn blob_test() {
// PNG Header with size 123x321
#[rustfmt::skip]
let data = vec![0x89, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x7B, 0x00, 0x00, 0x01, 0x41,
0x08, 0x06, 0x00, 0x00, 0x00, 0x9A, 0x38, 0xC4];

let dim = blob_size(&data).unwrap();
assert_eq!(dim, ImageSize { width: 123, height: 321 });
assert_eq!(
dim,
ImageSize {
width: 123,
height: 321
}
);
}

#[test]
Expand All @@ -22,6 +29,7 @@ fn blob_too_small_test() {
#[test]
fn blob_test_fail() {
// Invalid PNG header (0x51 instead of 0x50)
#[rustfmt::skip]
let data = vec![0x89, 0x51, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A,
0x00, 0x00, 0x00, 0x0D, 0x49, 0x48, 0x44, 0x52,
0x00, 0x00, 0x00, 0x7B, 0x00, 0x00, 0x01, 0x41,
Expand All @@ -38,6 +46,7 @@ fn gif_blob_too_small_test() {

#[test]
fn blob_test_partial_ico() {
#[rustfmt::skip]
let data = vec![
// Header (says 6 images are included)
0x00, 0x00, 0x01, 0x00, 0x06, 0x00,
Expand All @@ -51,5 +60,11 @@ fn blob_test_partial_ico() {
0xFF
];
let dim = blob_size(&data).unwrap();
assert_eq!(dim, ImageSize { width: 10, height: 100 });
assert_eq!(
dim,
ImageSize {
width: 10,
height: 100
}
);
}
8 changes: 7 additions & 1 deletion tests/bmp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ use imagesize::{size, ImageSize};
#[test]
fn bmp_test() {
let dim = size("tests/images/bmp/test.bmp").unwrap();
assert_eq!(dim, ImageSize { width: 512, height: 512 });
assert_eq!(
dim,
ImageSize {
width: 512,
height: 512
}
);
}
8 changes: 7 additions & 1 deletion tests/dds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ use imagesize::{size, ImageSize};
#[test]
fn dds_test() {
let dim = size("tests/images/dds/test.dds").unwrap();
assert_eq!(dim, ImageSize { width: 100, height: 67 });
assert_eq!(
dim,
ImageSize {
width: 100,
height: 67
}
);
}
8 changes: 7 additions & 1 deletion tests/exr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ use imagesize::{size, ImageSize};
#[test]
fn exr_test() {
let dim = size("tests/images/exr/test.exr").unwrap();
assert_eq!(dim, ImageSize { width: 100, height: 100 });
assert_eq!(
dim,
ImageSize {
width: 100,
height: 100
}
);
}
8 changes: 7 additions & 1 deletion tests/farbfeld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ use imagesize::{size, ImageSize};
#[test]
fn farbfeld_test() {
let dim = size("tests/images/farbfeld/test.ff").unwrap();
assert_eq!(dim, ImageSize { width: 32, height: 32 });
assert_eq!(
dim,
ImageSize {
width: 32,
height: 32
}
);
}
8 changes: 7 additions & 1 deletion tests/gif.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ use imagesize::{size, ImageSize};
#[test]
fn gif_test() {
let dim = size("tests/images/gif/test.gif").unwrap();
assert_eq!(dim, ImageSize { width: 100, height: 100 });
assert_eq!(
dim,
ImageSize {
width: 100,
height: 100
}
);
}
8 changes: 7 additions & 1 deletion tests/hdr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,11 @@ use imagesize::{size, ImageSize};
#[test]
fn hdr_test() {
let dim = size("tests/images/hdr/test.hdr").unwrap();
assert_eq!(dim, ImageSize { width: 100, height: 67 });
assert_eq!(
dim,
ImageSize {
width: 100,
height: 67
}
);
}
Loading

0 comments on commit 2280315

Please sign in to comment.