Skip to content

Commit

Permalink
feat: Add XBM support
Browse files Browse the repository at this point in the history
  • Loading branch information
sorairolake committed Oct 29, 2024
1 parent 5d04fdc commit d53b099
Show file tree
Hide file tree
Showing 13 changed files with 679 additions and 11 deletions.
8 changes: 6 additions & 2 deletions BUILD.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ fd -t directory out ./target/*/build/qrtool-*

Enable decoding from the QOI image. This is enabled by default.

`decode-from-svg`::

Enable decoding from the SVG image. This is enabled by default.

`decode-from-tga`::

Enable decoding from the TGA image. This is enabled by default.
Expand All @@ -87,9 +91,9 @@ fd -t directory out ./target/*/build/qrtool-*

Enable decoding from the WebP image. This is enabled by default.

`decode-from-svg`::
`decode-from-xbm`::

Enable decoding from the SVG image. This is enabled by default.
Enable decoding from the XBM image. This is enabled by default.

`optimize-output-png`::

Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ project adheres to https://semver.org/[Semantic Versioning].

== {compare-url}/v0.11.5\...HEAD[Unreleased]

=== Added

* Supports decoding from XBM ({pull-request-url}/645[#645])

=== Fixed

* Fall-back to decoding an inverted image ({pull-request-url}/633[#633])
Expand Down
11 changes: 11 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 5 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ qrcode = "0.14.1"
resvg = { version = "0.44.0", default-features = false, optional = true }
rqrr = "0.8.0"
sysexits = "0.8.2"
xbm = { version = "0.1.2", optional = true }
yansi = { version = "1.0.1", default-features = false, optional = true }

[dev-dependencies]
Expand All @@ -53,10 +54,11 @@ default = [
"decode-from-exr",
"decode-from-pnm",
"decode-from-qoi",
"decode-from-svg",
"decode-from-tga",
"decode-from-tiff",
"decode-from-webp",
"decode-from-svg",
"decode-from-xbm",
"optimize-output-png",
"output-as-ansi",
]
Expand All @@ -70,10 +72,11 @@ decode-from-jpeg = ["image/jpeg"]
decode-from-exr = ["image/exr"]
decode-from-pnm = ["image/pnm"]
decode-from-qoi = ["image/qoi"]
decode-from-svg = ["dep:is-svg", "dep:resvg"]
decode-from-tga = ["image/tga"]
decode-from-tiff = ["image/tiff"]
decode-from-webp = ["image/webp"]
decode-from-svg = ["dep:is-svg", "dep:resvg"]
decode-from-xbm = ["dep:xbm"]
optimize-output-png = ["dep:oxipng"]
output-as-ansi = [
"dep:anstyle",
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ output format, etc.
- [TGA]
- [TIFF]
- [WebP]
- [XBM]

To support decoding from SVG image, the `decode-from-svg` feature must be
enabled at compile time. Note that the SVG image is rasterized before scanning.
Expand Down Expand Up @@ -355,6 +356,7 @@ licensing information.
[TGA]: https://en.wikipedia.org/wiki/Truevision_TGA
[TIFF]: https://en.wikipedia.org/wiki/TIFF
[WebP]: https://developers.google.com/speed/webp/
[XBM]: https://en.wikipedia.org/wiki/X_BitMap
[`oxipng`]: https://github.com/shssoichiro/oxipng
[`svgcleaner`]: https://github.com/RazrFalcon/svgcleaner
[ImageMagick]: https://imagemagick.org/
Expand Down
2 changes: 2 additions & 0 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ fn generate_man_page(out_dir: &str) -> io::Result<ExitStatus> {
command.args(["-a", "decode-from-tiff"]);
#[cfg(feature = "decode-from-webp")]
command.args(["-a", "decode-from-webp"]);
#[cfg(feature = "decode-from-xbm")]
command.args(["-a", "decode-from-xbm"]);
#[cfg(feature = "decode-from-svg")]
command.args(["-a", "decode-from-svg"]);
#[cfg(feature = "optimize-output-png")]
Expand Down
2 changes: 2 additions & 0 deletions docs/book/modules/ROOT/pages/usage.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
:tga-url: {enwp-article-url}/Truevision_TGA
:tiff-url: {enwp-article-url}/TIFF
:webp-url: https://developers.google.com/speed/webp/
:xbm-url: {enwp-article-url}/X_BitMap
:oxipng-repo-url: {github-url}/shssoichiro/oxipng
:svgcleaner-repo-url: {github-url}/RazrFalcon/svgcleaner
:imagemagick-url: https://imagemagick.org/
Expand Down Expand Up @@ -143,6 +144,7 @@ the output format, etc.
* {tga-url}[TGA]
* {tiff-url}[TIFF]
* {webp-url}[WebP]
* {xbm-url}[XBM]

To support decoding from SVG image, the `decode-from-svg` feature must be
enabled at compile time. Note that the SVG image is rasterized before scanning.
Expand Down
14 changes: 13 additions & 1 deletion docs/man/man1/qrtool-decode.1.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

= qrtool-decode(1)
// Specify in UTC.
:docdate: 2024-08-02
:docdate: 2024-10-29
:doctype: manpage
ifdef::revnumber[:mansource: qrtool {revnumber}]
ifndef::revnumber[:mansource: qrtool]
Expand Down Expand Up @@ -59,6 +59,9 @@ endif::[]
ifdef::decode-from-webp,env-github,site-gen-antora[]
:webp-url: https://developers.google.com/speed/webp/
endif::[]
ifdef::decode-from-xbm,env-github,site-gen-antora[]
:xbm-url: {enwp-article-url}/X_BitMap
endif::[]
:image-crates-url: https://crates.io/crates/image

== NAME
Expand Down Expand Up @@ -118,6 +121,9 @@ endif::[]
ifdef::decode-from-webp,env-github,site-gen-antora[]
* {webp-url}[WebP]
endif::[]
ifdef::decode-from-xbm,env-github,site-gen-antora[]
* {xbm-url}[XBM]
endif::[]

Use *-t* option to specify the image format. If this option is not specified,
the image format is determined based on the extension or the magic number.
Expand Down Expand Up @@ -233,6 +239,12 @@ ifdef::decode-from-webp,env-github,site-gen-antora[]
WebP.
endif::[]

ifdef::decode-from-xbm,env-github,site-gen-antora[]
*xbm*::::

X BitMap.
endif::[]

*--verbose*::

Also print the metadata. It is output to stderr. This option conflicts with
Expand Down
16 changes: 16 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,26 @@ pub fn run() -> anyhow::Result<()> {
#[cfg(feature = "decode-from-svg")]
let input_format = input_format
.or_else(|| is_svg::is_svg(&input).then_some(crate::cli::InputFormat::Svg));
#[cfg(feature = "decode-from-xbm")]
#[allow(clippy::option_if_let_else)]
let input_format = input_format.or_else(|| {
if let Some(ref path) = arg.input {
path.extension() == Some(std::ffi::OsStr::new("xbm"))
} else {
bool::default()
}
.then_some(crate::cli::InputFormat::Xbm)
});
#[allow(clippy::option_if_let_else)]
let image = match input_format {
#[cfg(feature = "decode-from-svg")]
Some(crate::cli::InputFormat::Svg) => decode::from_svg(&input),
#[cfg(feature = "decode-from-xbm")]
Some(crate::cli::InputFormat::Xbm) => {
let decoder = xbm::Decoder::new(Cursor::new(input))
.context("could not create new XBM decoder")?;
image::DynamicImage::from_decoder(decoder).map_err(anyhow::Error::from)
}
format => {
let format = if let Some(f) = format {
f.try_into()
Expand Down
18 changes: 12 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,10 @@ pub enum InputFormat {
/// WebP.
#[cfg(feature = "decode-from-webp")]
WebP,

/// X BitMap.
#[cfg(feature = "decode-from-xbm")]
Xbm,
}

impl TryFrom<InputFormat> for ImageFormat {
Expand Down Expand Up @@ -620,6 +624,10 @@ impl TryFrom<InputFormat> for ImageFormat {
InputFormat::Tiff => Ok(Self::Tiff),
#[cfg(feature = "decode-from-webp")]
InputFormat::WebP => Ok(Self::WebP),
#[cfg(feature = "decode-from-xbm")]
InputFormat::Xbm => Err(Self::Error::Unsupported(
image::error::ImageFormatHint::Unknown.into(),
)),
}
}
}
Expand Down Expand Up @@ -734,6 +742,8 @@ mod tests {
ImageFormat::try_from(InputFormat::Qoi).unwrap(),
ImageFormat::Qoi
);
#[cfg(feature = "decode-from-svg")]
assert!(ImageFormat::try_from(InputFormat::Svg).is_err());
#[cfg(feature = "decode-from-tga")]
assert_eq!(
ImageFormat::try_from(InputFormat::Tga).unwrap(),
Expand All @@ -749,11 +759,7 @@ mod tests {
ImageFormat::try_from(InputFormat::WebP).unwrap(),
ImageFormat::WebP
);
}

#[cfg(feature = "decode-from-svg")]
#[test]
fn try_from_input_format_to_image_format_when_svg() {
assert!(ImageFormat::try_from(InputFormat::Svg).is_err());
#[cfg(feature = "decode-from-xbm")]
assert!(ImageFormat::try_from(InputFormat::Xbm).is_err());
}
}
Loading

0 comments on commit d53b099

Please # to comment.