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 convert_color to Colorspace #103

Merged
merged 1 commit into from
Feb 5, 2025
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
13 changes: 13 additions & 0 deletions mupdf-sys/wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,19 @@ fz_cookie *mupdf_new_cookie(fz_context *ctx, mupdf_error_t **errptr)
return cookie;
}

/* Colorspace */
void mupdf_convert_color(fz_context *ctx, fz_colorspace *ss, const float *sv, fz_colorspace *ds, float *dv, fz_colorspace *is, fz_color_params params, mupdf_error_t **errptr)
{
fz_try(ctx)
{
fz_convert_color(ctx, ss, sv, ds, dv, is, params);
}
fz_catch(ctx)
{
mupdf_save_error(ctx, errptr);
}
}

/* DisplayList */
fz_display_list *mupdf_new_display_list(fz_context *ctx, fz_rect mediabox, mupdf_error_t **errptr)
{
Expand Down
87 changes: 86 additions & 1 deletion src/colorspace.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
use std::cmp::PartialEq;
use std::ffi::CStr;
use std::fmt;
use std::ptr;

use mupdf_sys::*;

use crate::context;
use crate::{context, ColorParams, Error};

#[derive(Debug)]
pub struct Colorspace {
Expand Down Expand Up @@ -85,6 +86,62 @@ impl Colorspace {
let name_cstr = unsafe { CStr::from_ptr(ptr) };
name_cstr.to_str().unwrap()
}

pub fn convert_color(
&self,
color: &[f32],
to: &Colorspace,
via: Option<&Colorspace>,
params: ColorParams,
) -> Result<Vec<f32>, Error> {
let from_n = usize::try_from(self.n()).unwrap();
assert!(color.len() >= from_n);
let to_n = usize::try_from(to.n()).unwrap();

let via = via.map_or(ptr::null_mut(), |cs| cs.inner);
unsafe {
let mut out = Vec::with_capacity(to_n);
ffi_try!(mupdf_convert_color(
context(),
self.inner,
color.as_ptr(),
to.inner,
out.as_mut_ptr(),
via,
params.into()
));
out.set_len(to_n);
Ok(out)
}
}

pub fn convert_color_into(
&self,
color: &[f32],
to: &Colorspace,
out: &mut [f32],
via: Option<&Colorspace>,
params: ColorParams,
) -> Result<usize, Error> {
let from_n = usize::try_from(self.n()).unwrap();
assert!(color.len() >= from_n);
let n = usize::try_from(to.n()).unwrap();
assert!(out.len() >= n);

let via = via.map_or(ptr::null_mut(), |cs| cs.inner);
unsafe {
ffi_try!(mupdf_convert_color(
context(),
self.inner,
color.as_ptr(),
to.inner,
out.as_mut_ptr(),
via,
params.into()
));
Ok(n)
}
}
}

impl PartialEq for Colorspace {
Expand All @@ -106,6 +163,8 @@ impl fmt::Display for Colorspace {

#[cfg(test)]
mod test {
use crate::ColorParams;

use super::Colorspace;

#[test]
Expand All @@ -127,4 +186,30 @@ mod test {
assert!(cmyk.is_cmyk());
assert_eq!(cmyk.name(), "DeviceCMYK");
}

#[test]
fn test_color_conversion() {
let red = Colorspace::device_rgb()
.convert_color(
&[1.0, 0.0, 0.0],
&Colorspace::device_cmyk(),
None,
ColorParams::default(),
)
.unwrap();
assert_eq!(&*red, [0.0, 1.0, 1.0, 0.0]);

let mut gray = [0.0; 4];
let n = Colorspace::device_gray()
.convert_color_into(
&[0.6],
&Colorspace::device_rgb(),
&mut gray,
None,
ColorParams::default(),
)
.unwrap();
assert_eq!(n, 3);
assert_eq!(gray, [0.6, 0.6, 0.6, 0.0]);
}
}
Loading