Skip to content

Draft: Add support for XBM and XPM #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
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
5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ publish = false
include = ["src", "tests/reference.rs"]

[features]
default = ["pcx"]
default = ["pcx", "xbm", "xpm"]
pcx = ["dep:pcx"]
xbm = []
xpm = ["dep:image-x11r6colors"]

[dependencies]
image = { version = "0.25.5", default-features = false }
pcx = { version = "0.2.4", optional = true }
image-x11r6colors = { path = 'image-x11r6colors', version = "1.0.0", optional = true }

[dev-dependencies]
image = { version = "0.25.5", default-features = false, features = ["png"] }
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ 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) |
| XBM | [Wikipedia](https://en.wikipedia.org/wiki/X_BitMap) |
| XPM | [Wikipedia](https://en.wikipedia.org/wiki/X_PixMap) |

## New Formats

Expand Down
1 change: 1 addition & 0 deletions deny.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ allow = [
"BSD-2-Clause",
"BSD-3-Clause",
"MIT",
"X11",
"MIT-0",
"MPL-2.0",
"Unicode-DFS-2016",
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(())
}
35 changes: 35 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@

[package]
name = "image-fuzz"
version = "0.0.1"
authors = ["Automatically generated"]
edition = "2021"
publish = false

[package.metadata]
cargo-fuzz = true

[dependencies]
image = { version = "0.25.5", default-features = false }

[dependencies.image-extras]
path = ".."
features = ["xbm", "xpm"]
[dependencies.libfuzzer-sys]
version = "0.4"

# Temporarily needed for image-extras to build, see ../Cargo.toml
[patch.crates-io]
image = { git = "https://github.com/fintelia/image", branch = "decoding-hooks" }

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

[[bin]]
name = "fuzzer_script_xbm"
path = "fuzzers/fuzzer_script_xbm.rs"

[[bin]]
name = "fuzzer_script_xpm"
path = "fuzzers/fuzzer_script_xpm.rs"
16 changes: 16 additions & 0 deletions fuzz/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Fuzzing with libfuzzer
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth having this also reference the note in the crate README.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done (linked to the README and repeated part of the note).


For the possibly more up-to-date guide see <https://fuzz.rs/book/cargo-fuzz/setup.html>.

> $ cargo install cargo-fuzz
> $ cargo +nightly fuzz run fuzzer_script_<format>

Fuzzing may progress faster for certain formats if seeded with a dictionary:

> $ cargo +nightly fuzz run fuzzer_script_xbm -- -dict=fuzz/dictionaries/xbm.dict

# Bug reports

As explained in the project [README](../README.md), fuzzing is not a priority for
this crate and decoders may panic or worse on malformed input. Please do not
open issues for crashes found by fuzzing, though PRs fixing them are welcome.
16 changes: 16 additions & 0 deletions fuzz/dictionaries/xbm.dict
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"#define"
"_width"
"_height"
"_x_hot"
"_y_hot"
"static"
"unsigned"
"char"
"_bits[]"
"="
"{"
"0x"
"0X"
","
"}"
";"
22 changes: 22 additions & 0 deletions fuzz/fuzzers/fuzzer_script_xbm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![no_main]
#[macro_use]
extern crate libfuzzer_sys;

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

fuzz_target!(|data: &[u8]| {
let reader = Cursor::new(data);
let Ok(mut decoder) = image_extras::xbm::XbmDecoder::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));
});
21 changes: 21 additions & 0 deletions fuzz/fuzzers/fuzzer_script_xpm.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#![no_main]
#[macro_use] extern crate libfuzzer_sys;

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

fuzz_target!(|data: &[u8]| {
let reader = Cursor::new(data);
let Ok(mut decoder) = image_extras::xpm::XpmDecoder::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));
});
8 changes: 8 additions & 0 deletions image-x11r6colors/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "image-x11r6colors"
version = "1.0.0"
license = "(MIT OR Apache-2.0) AND X11"
rust-version = "1.17.0"
description = "Color database matching X11R6"
readme = "README.md"
publish = false
23 changes: 23 additions & 0 deletions image-x11r6colors/LICENSE-X11
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Copyright (C) 1994 X Consortium

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Except as contained in this notice, the name of the X Consortium shall not
be used in advertising or otherwise to promote the sale, use or other deal-
ings in this Software without prior written authorization from the X Consor-
tium.
12 changes: 12 additions & 0 deletions image-x11r6colors/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
## X11R6 Color names

This mini-library contains the color names database (often referred to as
`rgb.txt`) from X11R6, as a sorted array with names case folded. It has been
separated out into a distinct crate because it is possible that databases of
colors with names are copyrightable and therefore subject to the X11 license.
Splitting out the list makes it easier for users of the `image-extras`, which
uses this library if its `xpm` feature is enabled, to keep track of licensing
information.

Only the color database itself (from 1994) is X11 licensed; everything since
then is offered under `MIT OR Apache-2.0`.
Loading
Loading