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
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ publish = false
include = ["src", "tests/reference.rs"]

[features]
default = ["pcx"]
default = ["pcx", "sgi"]
pcx = ["dep:pcx"]
sgi = []

[dependencies]
image = { version = "0.25.8", default-features = false }
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Decoding support for additional image formats beyond those provided by the [`ima
| Extension | File Format Description |
| --------- | -------------------- |
| PCX | [Wikipedia](https://en.wikipedia.org/wiki/PCX#PCX_file_format) |
| RGB | [Wikipedia](https://en.wikipedia.org/wiki/Silicon_Graphics_Image) |

## New Formats

Expand Down
28 changes: 28 additions & 0 deletions examples/convert.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//! An example of opening an image.
extern crate image;
extern crate image_extras;

use std::env;
use std::error::Error;
use std::path::Path;

fn main() -> Result<(), Box<dyn Error>> {
image_extras::register();

let (from, into) = if env::args_os().count() == 3 {
(
env::args_os().nth(1).unwrap(),
env::args_os().nth(2).unwrap(),
)
} else {
println!("Please enter a from and into path.");
std::process::exit(1);
};

// Use the open function to load an image from a Path.
// ```open``` returns a dynamic image.
let im = image::open(Path::new(&from)).unwrap();
// Write the contents of this image using extension guessing.
im.save(Path::new(&into)).unwrap();
Ok(())
}
4 changes: 4 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,7 @@ members = ["."]
[[bin]]
name = "fuzzer_script_pcx"
path = "fuzzers/fuzzer_script_pcx.rs"

[[bin]]
name = "fuzzer_script_sgi"
path = "fuzzers/fuzzer_script_sgi.rs"
22 changes: 22 additions & 0 deletions fuzz/fuzzers/fuzzer_script_sgi.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![no_main]
#[macro_use]
extern crate libfuzzer_sys;

use image::ImageDecoder;
use std::io::Cursor;

fuzz_target!(|data: &[u8]| {
let reader = Cursor::new(data);
let Ok(mut decoder) = image_extras::sgi::SgiDecoder::new(reader) else {
return;
};
let mut limits = image::Limits::default();
limits.max_alloc = Some(1024 * 1024); // 1 MiB
if limits.reserve(decoder.total_bytes()).is_err() {
return;
}
if decoder.set_limits(limits).is_err() {
return;
}
let _ = std::hint::black_box(image::DynamicImage::from_decoder(decoder));
});
32 changes: 30 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,41 @@
#[cfg(feature = "pcx")]
pub mod pcx;

#[cfg(feature = "sgi")]
pub mod sgi;

/// Register all enabled extra formats with the image crate.
pub fn register() {
let just_registered = image::hooks::register_decoding_hook(
let just_registered_pcx = image::hooks::register_decoding_hook(
"pcx".into(),
Box::new(|r| Ok(Box::new(pcx::PCXDecoder::new(r)?))),
);
if just_registered {
if just_registered_pcx {
image::hooks::register_format_detection_hook("pcx".into(), &[0x0a, 0x0], Some(b"\xFF\xF8"));
}

// SGI RGB images generally show up with a .rgb ending (whether or not they
// have 3 channels), and sometimes .bw (when grayscale) and .rgba. The
// extensions .sgi and .iris, while unambiguous, do not seem to have been
// used much. The extension .rgb is also used for a variety of other files,
// including bare image data, so to be sure it would be best to check both
// extension and leading bytes
let hook: for<'a> fn(
image::hooks::GenericReader<'a>,
) -> image::ImageResult<Box<dyn image::ImageDecoder + 'a>> =
|r| Ok(Box::new(sgi::SgiDecoder::new(r)?));
image::hooks::register_decoding_hook("bw".into(), Box::new(hook));
image::hooks::register_decoding_hook("rgb".into(), Box::new(hook));
image::hooks::register_decoding_hook("rgba".into(), Box::new(hook));
image::hooks::register_decoding_hook("iris".into(), Box::new(hook));
let just_registered_sgi = image::hooks::register_decoding_hook("sgi".into(), Box::new(hook));
if just_registered_sgi {
// The main signature bytes are technically just 01 da, but this is short
// and the following storage and bpc fields are constrained well enough to
// efficiently match them as well
image::hooks::register_format_detection_hook("sgi".into(), b"\x01\xda\x00\x01", None);
image::hooks::register_format_detection_hook("sgi".into(), b"\x01\xda\x01\x01", None);
image::hooks::register_format_detection_hook("sgi".into(), b"\x01\xda\x00\x02", None);
image::hooks::register_format_detection_hook("sgi".into(), b"\x01\xda\x01\x02", None);
}
}
Loading
Loading