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

Implement wrapper for sws_getCoefficients() #174

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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 src/software/scaling/color_space.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ pub enum ColorSpace {
ITU624,
SMPTE170M,
SMPTE240M,
BT2020,
}

impl From<c_int> for ColorSpace {
fn from(value: c_int) -> ColorSpace {
match value {
SWS_CS_ITU709 => ColorSpace::ITU709,
SWS_CS_FCC => ColorSpace::FCC,
// Default is same as ITU601
SWS_CS_DEFAULT => ColorSpace::Default,
SWS_CS_SMPTE240M => ColorSpace::SMPTE240M,

SWS_CS_BT2020 => ColorSpace::BT2020,
_ => ColorSpace::Default,
}
}
Expand All @@ -36,6 +38,7 @@ impl From<ColorSpace> for c_int {
ColorSpace::ITU624 => SWS_CS_ITU624,
ColorSpace::SMPTE170M => SWS_CS_SMPTE170M,
ColorSpace::SMPTE240M => SWS_CS_SMPTE240M,
ColorSpace::BT2020 => SWS_CS_BT2020,
}
}
}
52 changes: 51 additions & 1 deletion src/software/scaling/context.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::ptr;

use super::Flags;
use crate::color;

use super::{ColorSpace, Flags};
use ffi::*;
use libc::c_int;
use util::format;
Expand Down Expand Up @@ -78,6 +80,54 @@ impl Context {
}
}

pub fn set_colorspace_details(
&mut self,
input_space: color::Space,
src_range: color::Range,
dst_range: color::Range,
brightness: i32,
contrast: i32,
saturation: i32,
) -> Result<(), Error> {
unsafe {
let input_color_space_int = match input_space {
color::Space::BT709 => ColorSpace::ITU709,
color::Space::BT2020CL => ColorSpace::BT2020,
color::Space::BT2020NCL => ColorSpace::BT2020,
_ => ColorSpace::ITU601,
};
let coefficients: *const i32 = sws_getCoefficients(input_color_space_int.into());

// 0 means limited range (16-235), 1 means full range (0-255)
let src_range_value = match src_range {
color::Range::MPEG => 0,
color::Range::JPEG => 1,
color::Range::Unspecified => 1,
};
// 0 means limited range, 1 means full range
// For an RGB image, we want full range, for YUV, most of the time we want limited range
let dst_range_value = match dst_range {
color::Range::MPEG => 0,
color::Range::JPEG => 1,
color::Range::Unspecified => 1,
};

match sws_setColorspaceDetails(
self.as_mut_ptr(),
coefficients,
src_range_value,
coefficients,
dst_range_value,
brightness,
contrast,
saturation,
) {
e if e < 0 => Err(Error::from(e)),
_ => Ok(()),
}
}
}

pub fn cached(
&mut self,
src_format: format::Pixel,
Expand Down