-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(operations): added "apply icc profile" operation
- Loading branch information
Showing
5 changed files
with
203 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
use lcms2::*; | ||
use zune_core::{bit_depth::BitType, colorspace::ColorSpace}; | ||
use zune_image::{ | ||
errors::{ImageErrors, ImageOperationsErrors}, | ||
frame::Frame, | ||
image::Image, | ||
traits::OperationsTrait, | ||
}; | ||
|
||
/// Apply icc profile | ||
pub struct ApplyICC { | ||
profile: Profile, | ||
} | ||
|
||
impl ApplyICC { | ||
/// Create a new icc apply operation | ||
/// | ||
/// # Arguments | ||
/// - chunk: ICC profile chunk | ||
#[must_use] | ||
pub fn new(profile: Profile) -> Self { | ||
Self { profile } | ||
} | ||
} | ||
|
||
impl OperationsTrait for ApplyICC { | ||
fn name(&self) -> &'static str { | ||
"apply icc profile" | ||
} | ||
|
||
fn execute_impl(&self, image: &mut Image) -> Result<(), ImageErrors> { | ||
let icc_chunk = match image.metadata().icc_chunk() { | ||
Some(icc) => icc, | ||
None => { | ||
log::warn!("No icc profile in the image, skipping"); | ||
return Ok(()); | ||
} | ||
}; | ||
|
||
let src_profile = Profile::new_icc(icc_chunk) | ||
.map_err(|e| ImageOperationsErrors::GenericString(e.to_string()))?; | ||
|
||
let colorspace = image.colorspace(); | ||
|
||
let format = match (colorspace, image.depth().bit_size()) { | ||
(ColorSpace::RGB, 8) => PixelFormat::RGB_8, | ||
(ColorSpace::RGB, 16) => PixelFormat::RGB_16, | ||
(ColorSpace::RGBA, 8) => PixelFormat::RGBA_8, | ||
(ColorSpace::RGBA, 16) => PixelFormat::RGBA_16, | ||
(ColorSpace::YCbCr, 8) => PixelFormat::YCbCr_8, | ||
(ColorSpace::YCbCr, 16) => PixelFormat::YCbCr_16, | ||
(ColorSpace::Luma, 8) => PixelFormat::GRAY_8, | ||
(ColorSpace::Luma, 16) => PixelFormat::GRAY_16, | ||
(ColorSpace::LumaA, 8) => PixelFormat::GRAYA_8, | ||
(ColorSpace::LumaA, 16) => PixelFormat::GRAYA_16, | ||
(ColorSpace::CMYK, 8) => PixelFormat::CMYK_8, | ||
(ColorSpace::CMYK, 16) => PixelFormat::CMYK_16, | ||
(ColorSpace::BGR, 8) => PixelFormat::BGR_8, | ||
(ColorSpace::BGR, 16) => PixelFormat::BGR_16, | ||
(ColorSpace::BGRA, 8) => PixelFormat::BGRA_8, | ||
(ColorSpace::BGRA, 16) => PixelFormat::BGRA_16, | ||
(ColorSpace::ARGB, 8) => PixelFormat::ARGB_8, | ||
(ColorSpace::ARGB, 16) => PixelFormat::ARGB_16, | ||
(ColorSpace::HSV, 8) => PixelFormat::HSV_8, | ||
(ColorSpace::HSV, 16) => PixelFormat::HSV_16, | ||
_ => unreachable!("This should be handled in supported_colorspaces"), | ||
}; | ||
|
||
let t = Transform::new( | ||
&src_profile, | ||
format, | ||
&self.profile, | ||
format, | ||
Intent::Perceptual, | ||
) | ||
.map_err(|e| ImageOperationsErrors::GenericString(e.to_string()))?; | ||
|
||
for frame in image.frames_mut() { | ||
let mut buffer = frame.flatten::<u8>(colorspace); | ||
t.transform_in_place(&mut buffer); | ||
let _ = std::mem::replace(frame, Frame::from_u8(&buffer, colorspace, 0, 0)); | ||
} | ||
|
||
image.metadata_mut().set_icc_chunk( | ||
self.profile | ||
.icc() | ||
.map_err(|e| ImageOperationsErrors::GenericString(e.to_string()))?, | ||
); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn supported_types(&self) -> &'static [BitType] { | ||
&[BitType::U8, BitType::U16] | ||
} | ||
|
||
fn supported_colorspaces(&self) -> &'static [ColorSpace] { | ||
&[ | ||
ColorSpace::RGB, | ||
ColorSpace::RGBA, | ||
ColorSpace::YCbCr, | ||
ColorSpace::Luma, | ||
ColorSpace::LumaA, | ||
ColorSpace::CMYK, | ||
ColorSpace::BGR, | ||
ColorSpace::BGRA, | ||
ColorSpace::ARGB, | ||
ColorSpace::HSV, | ||
] | ||
} | ||
} | ||
|
||
/// Apply srgb icc profile | ||
pub struct ApplySRGB; | ||
|
||
impl OperationsTrait for ApplySRGB { | ||
fn name(&self) -> &'static str { | ||
"apply srgb profile" | ||
} | ||
|
||
fn execute_impl(&self, image: &mut Image) -> Result<(), ImageErrors> { | ||
ApplyICC::new(Profile::new_srgb()).execute_impl(image) | ||
} | ||
|
||
fn supported_types(&self) -> &'static [BitType] { | ||
&[BitType::U8, BitType::U16] | ||
} | ||
|
||
fn supported_colorspaces(&self) -> &'static [ColorSpace] { | ||
&[ | ||
ColorSpace::RGB, | ||
ColorSpace::RGBA, | ||
ColorSpace::YCbCr, | ||
ColorSpace::Luma, | ||
ColorSpace::LumaA, | ||
ColorSpace::CMYK, | ||
ColorSpace::BGR, | ||
ColorSpace::BGRA, | ||
ColorSpace::ARGB, | ||
ColorSpace::HSV, | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters