Skip to content
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

Add workaround for QuantumRange not defined error when hdri is disabled #68

Merged
merged 1 commit into from
Jun 11, 2020
Merged
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: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,8 @@ libc = "0.2.70"
[build-dependencies]
bindgen = "0.53.2"
pkg-config = "0.3.17"

[features]

# Workaround for bindgen bug when ImageMagick is compiled with disable-hdri
disable-hdri = []
29 changes: 26 additions & 3 deletions src/wand/magick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,26 @@ impl MagickWand {
}
}

// Define two 'quantum_range' functions because the bindings::QuantumRange symbol
// is not available if hdri is disabled in the compiled ImageMagick libs
#[cfg(not(feature="disable-hdri"))]
fn quantum_range(&self) -> Result<f64, &'static str> {
return Ok(bindings::QuantumRange);
}

// with disable-hdri enabled we define our own quantum_range
// values lifted directly from magick-type.h
#[cfg(feature="disable-hdri")]
fn quantum_range(&self) -> Result<f64, &'static str> {
match bindings::MAGICKCORE_QUANTUM_DEPTH {
8 => Ok(255.0f64),
16 => Ok(65535.0f64),
32 => Ok(4294967295.0f64),
64 => Ok(18446744073709551615.0f64),
_ => Err("Quantum depth must be one of 8, 16, 32 or 64")
}
}

// Level an image. Black and white points are multiplied with QuantumRange to
// decrease dependencies on the end user.
pub fn level_image(
Expand All @@ -273,12 +293,14 @@ impl MagickWand {
gamma: f64,
white_point: f64,
) -> Result<(), &'static str> {
let quantum_range = self.quantum_range()?;

let result = unsafe {
bindings::MagickLevelImage(
self.wand,
black_point * bindings::QuantumRange,
black_point * quantum_range,
gamma,
white_point * bindings::QuantumRange,
white_point * quantum_range,
)
};
match result {
Expand All @@ -287,6 +309,7 @@ impl MagickWand {
}
}


/// Extend the image as defined by the geometry, gravity, and wand background color. Set the
/// (x,y) offset of the geometry to move the original wand relative to the extended wand.
pub fn extend_image(
Expand Down Expand Up @@ -586,7 +609,7 @@ impl MagickWand {
/// Returns the image resolution as a pair (horizontal resolution, vertical resolution)
pub fn sepia_tone_image(&self, threshold: f64) -> Result<(), &'static str> {
unsafe {
if bindings::MagickSepiaToneImage(self.wand, threshold * bindings::QuantumRange)
if bindings::MagickSepiaToneImage(self.wand, threshold * self.quantum_range()?)
== bindings::MagickBooleanType_MagickTrue
{
Ok(())
Expand Down