mirror of
https://github.com/denoland/deno.git
synced 2025-03-07 03:42:40 -05:00
handle 32-bit depth
This commit is contained in:
parent
5e52b862fb
commit
1b545f980a
2 changed files with 35 additions and 36 deletions
ext/canvas
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
use bytemuck::cast_slice;
|
use bytemuck::cast_slice;
|
||||||
use bytemuck::cast_slice_mut;
|
use bytemuck::cast_slice_mut;
|
||||||
|
use deno_core::error::type_error;
|
||||||
use deno_core::error::AnyError;
|
use deno_core::error::AnyError;
|
||||||
use image::ColorType;
|
use image::ColorType;
|
||||||
use image::DynamicImage;
|
use image::DynamicImage;
|
||||||
|
@ -20,6 +21,15 @@ use lcms2::Transform;
|
||||||
use num_traits::NumCast;
|
use num_traits::NumCast;
|
||||||
use num_traits::SaturatingMul;
|
use num_traits::SaturatingMul;
|
||||||
|
|
||||||
|
use crate::error::image_error_message;
|
||||||
|
|
||||||
|
/// Image formats that is 32-bit depth are not supported currently due to the following reasons:
|
||||||
|
/// - e.g. OpenEXR, it's not covered by the spec.
|
||||||
|
/// - JPEG XL supported by WebKit, but it cannot be called a standard today.
|
||||||
|
/// https://github.com/whatwg/mimesniff/issues/143
|
||||||
|
const NOT_SUPPORTED_BIT_DEPTH: &str =
|
||||||
|
"The 32-bit depth image format is not supported.";
|
||||||
|
|
||||||
pub(crate) trait PremultiplyAlpha {
|
pub(crate) trait PremultiplyAlpha {
|
||||||
fn premultiply_alpha(&self) -> Self;
|
fn premultiply_alpha(&self) -> Self;
|
||||||
}
|
}
|
||||||
|
@ -103,6 +113,12 @@ pub(crate) fn premultiply_alpha(
|
||||||
ColorType::Rgba16 => Ok(DynamicImage::ImageRgba16(
|
ColorType::Rgba16 => Ok(DynamicImage::ImageRgba16(
|
||||||
process_premultiply_alpha(image.as_rgba16().unwrap()),
|
process_premultiply_alpha(image.as_rgba16().unwrap()),
|
||||||
)),
|
)),
|
||||||
|
ColorType::Rgb32F | ColorType::Rgba32F => {
|
||||||
|
Err(type_error(image_error_message(
|
||||||
|
"processing un-premultiply alpha",
|
||||||
|
NOT_SUPPORTED_BIT_DEPTH,
|
||||||
|
)))
|
||||||
|
}
|
||||||
// If the image does not have an alpha channel, return the image as is.
|
// If the image does not have an alpha channel, return the image as is.
|
||||||
_ => Ok(image),
|
_ => Ok(image),
|
||||||
}
|
}
|
||||||
|
@ -244,6 +260,12 @@ pub(crate) fn unpremultiply_alpha(
|
||||||
image.into_rgba16()
|
image.into_rgba16()
|
||||||
},
|
},
|
||||||
)),
|
)),
|
||||||
|
ColorType::Rgb32F | ColorType::Rgba32F => {
|
||||||
|
Err(type_error(image_error_message(
|
||||||
|
"processing un-premultiply alpha",
|
||||||
|
NOT_SUPPORTED_BIT_DEPTH,
|
||||||
|
)))
|
||||||
|
}
|
||||||
// If the image does not have an alpha channel, return the image as is.
|
// If the image does not have an alpha channel, return the image as is.
|
||||||
_ => Ok(image),
|
_ => Ok(image),
|
||||||
}
|
}
|
||||||
|
@ -350,13 +372,7 @@ where
|
||||||
ColorType::Rgb16 => PixelFormat::RGB_16,
|
ColorType::Rgb16 => PixelFormat::RGB_16,
|
||||||
ColorType::Rgba8 => PixelFormat::RGBA_8,
|
ColorType::Rgba8 => PixelFormat::RGBA_8,
|
||||||
ColorType::Rgba16 => PixelFormat::RGBA_16,
|
ColorType::Rgba16 => PixelFormat::RGBA_16,
|
||||||
// This arm usually doesn't reach, but it should be handled with returning the original image.
|
_ => unreachable!("{}", NOT_SUPPORTED_BIT_DEPTH),
|
||||||
_ => {
|
|
||||||
for (x, y, pixel) in image.pixels() {
|
|
||||||
out.put_pixel(x, y, pixel);
|
|
||||||
}
|
|
||||||
return out;
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
let transformer = Transform::new(
|
let transformer = Transform::new(
|
||||||
&input_icc_profile,
|
&input_icc_profile,
|
||||||
|
@ -383,10 +399,6 @@ where
|
||||||
pub(crate) fn to_srgb_from_icc_profile(
|
pub(crate) fn to_srgb_from_icc_profile(
|
||||||
image: DynamicImage,
|
image: DynamicImage,
|
||||||
icc_profile: Option<Vec<u8>>,
|
icc_profile: Option<Vec<u8>>,
|
||||||
unmatch_color_handler: fn(
|
|
||||||
ColorType,
|
|
||||||
DynamicImage,
|
|
||||||
) -> Result<DynamicImage, AnyError>,
|
|
||||||
) -> Result<DynamicImage, AnyError> {
|
) -> Result<DynamicImage, AnyError> {
|
||||||
match icc_profile {
|
match icc_profile {
|
||||||
// If there is no color profile information, return the image as is.
|
// If there is no color profile information, return the image as is.
|
||||||
|
@ -462,7 +474,10 @@ pub(crate) fn to_srgb_from_icc_profile(
|
||||||
srgb_icc_profile,
|
srgb_icc_profile,
|
||||||
)))
|
)))
|
||||||
}
|
}
|
||||||
x => unmatch_color_handler(x, image),
|
_ => Err(type_error(image_error_message(
|
||||||
|
"processing un-premultiply alpha",
|
||||||
|
NOT_SUPPORTED_BIT_DEPTH,
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
@ -618,20 +633,11 @@ pub(crate) fn to_srgb_from_icc_profile(
|
||||||
// }
|
// }
|
||||||
|
|
||||||
// /// Convert the color space of the image from sRGB to Display-P3.
|
// /// Convert the color space of the image from sRGB to Display-P3.
|
||||||
// fn srgb_to_display_p3(
|
// fn srgb_to_display_p3(image: DynamicImage) -> Result<DynamicImage, AnyError> {
|
||||||
// image: DynamicImage,
|
|
||||||
// unmatch_color_handler: fn(
|
|
||||||
// ColorType,
|
|
||||||
// DynamicImage,
|
|
||||||
// ) -> Result<DynamicImage, AnyError>,
|
|
||||||
// ) -> Result<DynamicImage, AnyError> {
|
|
||||||
// match image.color() {
|
// match image.color() {
|
||||||
// // The conversion of the lumincance color types to the display-p3 color space is meaningless.
|
// // The conversion of the lumincance color types to the display-p3 color space is meaningless.
|
||||||
// ColorType::L8 => Ok(DynamicImage::ImageLuma8(image.into_luma8())),
|
// ColorType::L8 | ColorType::L16 | ColorType::La8 | ColorType::La16 => {
|
||||||
// ColorType::L16 => Ok(DynamicImage::ImageLuma16(image.into_luma16())),
|
// Ok(image)
|
||||||
// ColorType::La8 => Ok(DynamicImage::ImageLumaA8(image.into_luma_alpha8())),
|
|
||||||
// ColorType::La16 => {
|
|
||||||
// Ok(DynamicImage::ImageLumaA16(image.into_luma_alpha16()))
|
|
||||||
// }
|
// }
|
||||||
// ColorType::Rgb8 => Ok(DynamicImage::ImageRgb8(process_srgb_to_display_p3(
|
// ColorType::Rgb8 => Ok(DynamicImage::ImageRgb8(process_srgb_to_display_p3(
|
||||||
// image.as_rgb8().unwrap(),
|
// image.as_rgb8().unwrap(),
|
||||||
|
@ -645,7 +651,10 @@ pub(crate) fn to_srgb_from_icc_profile(
|
||||||
// ColorType::Rgba16 => Ok(DynamicImage::ImageRgba16(
|
// ColorType::Rgba16 => Ok(DynamicImage::ImageRgba16(
|
||||||
// process_srgb_to_display_p3(image.as_rgba16().unwrap()),
|
// process_srgb_to_display_p3(image.as_rgba16().unwrap()),
|
||||||
// )),
|
// )),
|
||||||
// x => unmatch_color_handler(x, image),
|
// _ => Err(type_error(image_error_message(
|
||||||
|
// "processing ICC color profile conversion to sRGB",
|
||||||
|
// NOT_SUPPORTED_BIT_DEPTH,
|
||||||
|
// ))),
|
||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,6 @@ use image::codecs::png::PngDecoder;
|
||||||
use image::codecs::webp::WebPDecoder;
|
use image::codecs::webp::WebPDecoder;
|
||||||
use image::imageops::overlay;
|
use image::imageops::overlay;
|
||||||
use image::imageops::FilterType;
|
use image::imageops::FilterType;
|
||||||
use image::ColorType;
|
|
||||||
use image::DynamicImage;
|
use image::DynamicImage;
|
||||||
use image::ImageError;
|
use image::ImageError;
|
||||||
use image::RgbaImage;
|
use image::RgbaImage;
|
||||||
|
@ -218,16 +217,7 @@ fn apply_color_space_conversion(
|
||||||
// return the decoded image as is.
|
// return the decoded image as is.
|
||||||
ColorSpaceConversion::None => Ok(image),
|
ColorSpaceConversion::None => Ok(image),
|
||||||
ColorSpaceConversion::Default => {
|
ColorSpaceConversion::Default => {
|
||||||
fn unmatch_color_handler(
|
to_srgb_from_icc_profile(image, icc_profile)
|
||||||
x: ColorType,
|
|
||||||
_: DynamicImage,
|
|
||||||
) -> Result<DynamicImage, AnyError> {
|
|
||||||
Err(type_error(image_error_message(
|
|
||||||
"apply colorspaceConversion: default",
|
|
||||||
&format!("The color type {:?} is not supported.", x),
|
|
||||||
)))
|
|
||||||
}
|
|
||||||
to_srgb_from_icc_profile(image, icc_profile, unmatch_color_handler)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue