mirror of
https://github.com/lexmount/moli.git
synced 2026-11-21 00:03:15 +00:00
feat(image): preserve native raster color profiles and precision
This commit is contained in:
Generated
+2
@@ -3071,9 +3071,11 @@ dependencies = [
|
||||
name = "moli-image"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"color",
|
||||
"image",
|
||||
"jpeg-decoder",
|
||||
"jpeg-encoder",
|
||||
"moxcms",
|
||||
"png",
|
||||
"thiserror",
|
||||
"usvg",
|
||||
|
||||
@@ -5,9 +5,11 @@ edition = "2024"
|
||||
license = "MIT OR Apache-2.0"
|
||||
|
||||
[dependencies]
|
||||
image = { version = "0.25.10", default-features = false, features = ["gif", "webp", "bmp"] }
|
||||
color = "0.3.3"
|
||||
image = { version = "0.25.10", default-features = false, features = ["bmp", "gif", "webp"] }
|
||||
jpeg-decoder = "0.3"
|
||||
jpeg-encoder = "0.6"
|
||||
moxcms = { version = "0.8.1", features = ["extended_range"] }
|
||||
png = "0.18"
|
||||
thiserror = "2.0.17"
|
||||
usvg = { version = "=0.48.1", default-features = true }
|
||||
|
||||
@@ -6,3 +6,7 @@ pub const MAX_ENCODED_IMAGE_BYTES: usize = 128 * 1024 * 1024;
|
||||
/// Paint independently bounds transient raster allocations. This codec limit
|
||||
/// also protects network image decode.
|
||||
pub const MAX_DECODED_RGBA_BYTES: usize = 128 * 1024 * 1024;
|
||||
|
||||
/// Maximum retained layout surface, full precision colors and unconverted source
|
||||
/// samples for one image. Renderer owners also impose an aggregate image budget.
|
||||
pub(crate) const MAX_DECODED_COLOR_BYTES: usize = 256 * 1024 * 1024;
|
||||
|
||||
@@ -0,0 +1,323 @@
|
||||
//! Image colors retain extended sRGB until a consumer chooses its output gamut.
|
||||
|
||||
use ::color::ColorSpace;
|
||||
use moxcms::{ColorProfile, DataColorSpace, Layout, RenderingIntent, TransformOptions};
|
||||
|
||||
use crate::{RgbaImage, RgbaImageError, budget::MAX_DECODED_COLOR_BYTES};
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
|
||||
pub enum RasterColorPrecision {
|
||||
#[default]
|
||||
Unorm8,
|
||||
Float32,
|
||||
}
|
||||
|
||||
impl RasterColorPrecision {
|
||||
pub(crate) fn retained_bytes_per_pixel(self) -> usize {
|
||||
match self {
|
||||
Self::Unorm8 => 4,
|
||||
// Layout, full precision colors and up to 16-bit unconverted samples.
|
||||
Self::Float32 => 4 + 4 * size_of::<f32>() + 4 * size_of::<u16>(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Straight-alpha extended sRGB, without clipping to an RGBA8 layout surface.
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct ColorManagedRasterImage {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub rgba: Vec<f32>,
|
||||
pub unconverted: Option<UnconvertedRasterPixels>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub enum UnconvertedRasterPixels {
|
||||
Unorm8(Vec<u8>),
|
||||
Unorm16(Vec<u16>),
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum ColorDecodeError {
|
||||
#[error(transparent)]
|
||||
InvalidOutput(#[from] RgbaImageError),
|
||||
#[error("unable to allocate decoded image colors")]
|
||||
Allocation,
|
||||
#[error("image color transformation failed: {0}")]
|
||||
Transformation(#[from] moxcms::CmsError),
|
||||
}
|
||||
|
||||
pub(crate) enum ColorSamples {
|
||||
U8(Vec<u8>),
|
||||
U16(Vec<u16>),
|
||||
}
|
||||
|
||||
impl ColorSamples {
|
||||
fn len(&self) -> usize {
|
||||
match self {
|
||||
Self::U8(values) => values.len(),
|
||||
Self::U16(values) => values.len(),
|
||||
}
|
||||
}
|
||||
|
||||
fn sample(&self, index: usize) -> f32 {
|
||||
match self {
|
||||
Self::U8(values) => f32::from(values[index]) / 255.0,
|
||||
Self::U16(values) => f32::from(values[index]) / 65535.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(crate) struct RawColorImage {
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub samples: ColorSamples,
|
||||
pub layout: Layout,
|
||||
pub profile: Option<ColorProfile>,
|
||||
pub cmyk: bool,
|
||||
}
|
||||
|
||||
pub(crate) type DecodedColorPixels = (RgbaImage, Option<ColorManagedRasterImage>);
|
||||
|
||||
impl RawColorImage {
|
||||
pub(crate) fn finish(
|
||||
self,
|
||||
convert_profile: bool,
|
||||
) -> Result<DecodedColorPixels, ColorDecodeError> {
|
||||
let length = crate::rgba::checked_rgba_len(self.width, self.height)?;
|
||||
let count = length / 4;
|
||||
let channels = if self.cmyk {
|
||||
4
|
||||
} else {
|
||||
match self.layout {
|
||||
Layout::Rgb => 3,
|
||||
Layout::Rgba => 4,
|
||||
Layout::Gray => 1,
|
||||
Layout::GrayAlpha => 2,
|
||||
_ => unreachable!("only decoded RGB, gray and CMYK images are admitted"),
|
||||
}
|
||||
};
|
||||
if self.samples.len() != count * channels {
|
||||
return Err(RgbaImageError::InvalidBufferLength {
|
||||
expected: count * channels,
|
||||
actual: self.samples.len(),
|
||||
}
|
||||
.into());
|
||||
}
|
||||
let transform = self
|
||||
.profile
|
||||
.as_ref()
|
||||
.filter(|profile| {
|
||||
convert_profile
|
||||
&& match profile.color_space {
|
||||
DataColorSpace::Cmyk => self.cmyk,
|
||||
DataColorSpace::Rgb => !self.cmyk && channels >= 3,
|
||||
DataColorSpace::Gray => channels <= 2,
|
||||
_ => false,
|
||||
}
|
||||
})
|
||||
.and_then(|profile| {
|
||||
// Unsupported or malformed profiles do not make valid pixels fail
|
||||
// to load. The untagged fallback still handles CMYK and gray.
|
||||
// ICC parametric TRCs extend their linear toe to negative
|
||||
// inputs. Canvas uses the sign-preserving CSS transfer curve.
|
||||
// Keep CMS output linear, then use the native CSS color space
|
||||
// conversion without clipping wide-gamut negative components.
|
||||
let mut destination = ColorProfile::new_srgb();
|
||||
let linear = moxcms::ToneReprCurve::Parametric(vec![1.0, 1.0, 0.0, 1.0, 0.0]);
|
||||
destination.red_trc = Some(linear.clone());
|
||||
destination.green_trc = Some(linear.clone());
|
||||
destination.blue_trc = Some(linear);
|
||||
destination.cicp = None;
|
||||
profile
|
||||
.create_transform_f32(
|
||||
self.layout,
|
||||
&destination,
|
||||
Layout::Rgba,
|
||||
TransformOptions {
|
||||
rendering_intent: RenderingIntent::RelativeColorimetric,
|
||||
prefer_fixed_point: false,
|
||||
allow_extended_range_rgb_xyz: true,
|
||||
..Default::default()
|
||||
},
|
||||
)
|
||||
.ok()
|
||||
});
|
||||
if transform.is_none()
|
||||
&& let ColorSamples::U8(samples) = self.samples
|
||||
{
|
||||
// Ordinary RGBA keeps its existing zero-copy path. Untagged RGB
|
||||
// and gray also need no floating-point CMS scratch buffers.
|
||||
let rgba = if self.cmyk {
|
||||
samples
|
||||
.as_chunks::<4>()
|
||||
.0
|
||||
.iter()
|
||||
.flat_map(|ink| {
|
||||
let black = 255 - u16::from(ink[3]);
|
||||
let color =
|
||||
|sample: u8| (((255 - u16::from(sample)) * black + 127) / 255) as u8;
|
||||
[color(ink[0]), color(ink[1]), color(ink[2]), 255]
|
||||
})
|
||||
.collect()
|
||||
} else {
|
||||
match self.layout {
|
||||
Layout::Rgba => samples,
|
||||
Layout::Rgb => samples
|
||||
.as_chunks::<3>()
|
||||
.0
|
||||
.iter()
|
||||
.flat_map(|p| [p[0], p[1], p[2], 255])
|
||||
.collect(),
|
||||
Layout::Gray => samples.into_iter().flat_map(|v| [v, v, v, 255]).collect(),
|
||||
Layout::GrayAlpha => samples
|
||||
.as_chunks::<2>()
|
||||
.0
|
||||
.iter()
|
||||
.flat_map(|p| [p[0], p[0], p[0], p[1]])
|
||||
.collect(),
|
||||
_ => unreachable!("decoded image layout"),
|
||||
}
|
||||
};
|
||||
return Ok((RgbaImage::try_new(self.width, self.height, rgba)?, None));
|
||||
}
|
||||
let retain_precision = transform.is_some() || matches!(self.samples, ColorSamples::U16(_));
|
||||
let precision = if retain_precision {
|
||||
RasterColorPrecision::Float32
|
||||
} else {
|
||||
RasterColorPrecision::Unorm8
|
||||
};
|
||||
let retained = count * precision.retained_bytes_per_pixel();
|
||||
if retained > MAX_DECODED_COLOR_BYTES {
|
||||
return Err(RgbaImageError::BufferBudgetExceeded {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
required: retained,
|
||||
max: MAX_DECODED_COLOR_BYTES,
|
||||
}
|
||||
.into());
|
||||
}
|
||||
let mut rgba = Vec::new();
|
||||
rgba.try_reserve_exact(length)
|
||||
.map_err(|_| ColorDecodeError::Allocation)?;
|
||||
let mut managed = retain_precision.then(Vec::new);
|
||||
if let Some(values) = &mut managed {
|
||||
values
|
||||
.try_reserve_exact(length)
|
||||
.map_err(|_| ColorDecodeError::Allocation)?;
|
||||
}
|
||||
let mut unconverted = if transform.is_some() {
|
||||
if matches!(self.samples, ColorSamples::U16(_)) {
|
||||
Some(UnconvertedRasterPixels::Unorm16(Vec::new()))
|
||||
} else {
|
||||
Some(UnconvertedRasterPixels::Unorm8(Vec::new()))
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
match &mut unconverted {
|
||||
Some(UnconvertedRasterPixels::Unorm8(values)) => values
|
||||
.try_reserve_exact(length)
|
||||
.map_err(|_| ColorDecodeError::Allocation)?,
|
||||
Some(UnconvertedRasterPixels::Unorm16(values)) => values
|
||||
.try_reserve_exact(length)
|
||||
.map_err(|_| ColorDecodeError::Allocation)?,
|
||||
None => {}
|
||||
}
|
||||
// CMS transforms have no inter-pixel state. Fixed scratch buffers keep
|
||||
// a very wide image from allocating another full floating-point row.
|
||||
let mut input = [0.0_f32; 4096];
|
||||
let mut output = [0.0_f32; 4096];
|
||||
for first in (0..count).step_by(1024) {
|
||||
let pixels = (count - first).min(1024);
|
||||
for (index, sample) in input[..pixels * channels].iter_mut().enumerate() {
|
||||
*sample = self.samples.sample(first * channels + index);
|
||||
}
|
||||
if let Some(unconverted) = &mut unconverted {
|
||||
for source in input[..pixels * channels].chunks_exact(channels) {
|
||||
let alpha = if !self.cmyk && self.layout.has_alpha() {
|
||||
source[channels - 1]
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
let raw = if self.cmyk {
|
||||
let black = 1.0 - source[3];
|
||||
[
|
||||
(1.0 - source[0]) * black,
|
||||
(1.0 - source[1]) * black,
|
||||
(1.0 - source[2]) * black,
|
||||
alpha,
|
||||
]
|
||||
} else if channels <= 2 {
|
||||
[source[0], source[0], source[0], alpha]
|
||||
} else {
|
||||
[source[0], source[1], source[2], alpha]
|
||||
};
|
||||
match unconverted {
|
||||
UnconvertedRasterPixels::Unorm8(values) => {
|
||||
values.extend(raw.map(|v| (v * 255.0).round() as u8))
|
||||
}
|
||||
UnconvertedRasterPixels::Unorm16(values) => {
|
||||
values.extend(raw.map(|v| (v * 65535.0).round() as u16))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(transform) = &transform {
|
||||
transform.transform(&input[..pixels * channels], &mut output[..pixels * 4])?;
|
||||
for pixel in output[..pixels * 4].as_chunks_mut::<4>().0 {
|
||||
let encoded = ::color::Srgb::from_linear_srgb([pixel[0], pixel[1], pixel[2]]);
|
||||
pixel[..3].copy_from_slice(&encoded);
|
||||
}
|
||||
} else {
|
||||
for (source, target) in input[..pixels * channels]
|
||||
.chunks_exact(channels)
|
||||
.zip(output[..pixels * 4].as_chunks_mut::<4>().0)
|
||||
{
|
||||
if self.cmyk {
|
||||
let black = 1.0 - source[3];
|
||||
target.copy_from_slice(&[
|
||||
(1.0 - source[0]) * black,
|
||||
(1.0 - source[1]) * black,
|
||||
(1.0 - source[2]) * black,
|
||||
1.0,
|
||||
]);
|
||||
} else if channels <= 2 {
|
||||
target[..3].fill(source[0]);
|
||||
} else {
|
||||
target[..3].copy_from_slice(&source[..3]);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (source, target) in input[..pixels * channels]
|
||||
.chunks_exact(channels)
|
||||
.zip(output[..pixels * 4].as_chunks_mut::<4>().0)
|
||||
{
|
||||
// A CMYK K sample is ink, never an alpha channel.
|
||||
target[3] = if !self.cmyk && self.layout.has_alpha() {
|
||||
source[channels - 1]
|
||||
} else {
|
||||
1.0
|
||||
};
|
||||
rgba.extend(
|
||||
target
|
||||
.iter()
|
||||
.map(|value| (value.clamp(0.0, 1.0) * 255.0).round() as u8),
|
||||
);
|
||||
}
|
||||
if let Some(values) = &mut managed {
|
||||
values.extend_from_slice(&output[..pixels * 4]);
|
||||
}
|
||||
}
|
||||
let image = RgbaImage::try_new(self.width, self.height, rgba)?;
|
||||
Ok((
|
||||
image,
|
||||
managed.map(|rgba| ColorManagedRasterImage {
|
||||
width: self.width,
|
||||
height: self.height,
|
||||
rgba,
|
||||
unconverted,
|
||||
}),
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
use std::borrow::Cow;
|
||||
|
||||
use crate::*;
|
||||
|
||||
const P3: &[u8] = include_bytes!("../tests/data/color_profiles/Display-P3-FF0000FF.png");
|
||||
const P3_ALPHA: &[u8] = include_bytes!("../tests/data/color_profiles/Display-P3-BB0000CC.png");
|
||||
const P3_16: &[u8] = include_bytes!("../tests/data/color_profiles/Display-P3-BBBC00000000CCCC.png");
|
||||
|
||||
fn png_with_info(info: ::png::Info<'_>, pixels: &[u8]) -> Vec<u8> {
|
||||
let mut bytes = Vec::new();
|
||||
let cicp = info.coding_independent_code_points;
|
||||
let mut writer = ::png::Encoder::with_info(&mut bytes, info)
|
||||
.unwrap()
|
||||
.write_header()
|
||||
.unwrap();
|
||||
// png 0.18 reads cICP but its header encoder does not emit this field.
|
||||
if let Some(cicp) = cicp {
|
||||
writer
|
||||
.write_chunk(
|
||||
::png::chunk::cICP,
|
||||
&[
|
||||
cicp.color_primaries,
|
||||
cicp.transfer_function,
|
||||
cicp.matrix_coefficients,
|
||||
u8::from(cicp.is_video_full_range_image),
|
||||
],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
writer.write_image_data(pixels).unwrap();
|
||||
writer.finish().unwrap();
|
||||
bytes
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_profiles_preserve_extended_gamut_alpha_and_unconverted_samples() {
|
||||
let srgb = decode_raster_image(include_bytes!(
|
||||
"../tests/data/color_profiles/sRGB-BB0000FF.png"
|
||||
))
|
||||
.unwrap();
|
||||
assert_eq!(&srgb.image.rgba[..4], &[187, 0, 0, 255]);
|
||||
let adobe = decode_raster_image(include_bytes!(
|
||||
"../tests/data/color_profiles/Adobe-RGB-FF0000FF.png"
|
||||
))
|
||||
.unwrap();
|
||||
assert!(adobe.color_managed.unwrap().rgba[0] > 1.0);
|
||||
let red = decode_raster_image(P3).unwrap();
|
||||
let native = red.color_managed.unwrap();
|
||||
assert!(native.rgba[0] > 1.0);
|
||||
assert!(native.rgba[1] < 0.0 && native.rgba[2] < 0.0);
|
||||
for (actual, expected) in native.rgba[..3].iter().zip([1.0931, -0.2267, -0.1501]) {
|
||||
assert!(
|
||||
(actual - expected).abs() < 0.002,
|
||||
"extended sRGB {:?}",
|
||||
&native.rgba[..3]
|
||||
);
|
||||
}
|
||||
assert_eq!(native.rgba[3], 1.0);
|
||||
assert_eq!(&red.image.rgba[..4], &[255, 0, 0, 255]);
|
||||
for bytes in [P3_ALPHA, P3_16] {
|
||||
let decoded = decode_raster_image(bytes).unwrap();
|
||||
assert_eq!(&decoded.image.rgba[..4], &[205, 0, 0, 204]);
|
||||
let native = decoded.color_managed.unwrap();
|
||||
assert!((native.rgba[3] - 0.8).abs() < 0.0001);
|
||||
assert!(native.unconverted.is_some());
|
||||
let raw = decode_raster_image_with_color_conversion(bytes, ColorProfileConversion::Ignore)
|
||||
.unwrap();
|
||||
assert_eq!(&raw.image.rgba[..4], &[187, 0, 0, 204]);
|
||||
assert!(
|
||||
probe_raster_image(bytes).unwrap().decoded_byte_len() >= 4 * (4 + 4 * size_of::<f32>())
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_profile_lut_converts_cmyk_inks_without_treating_black_as_alpha() {
|
||||
let bytes = include_bytes!("../tests/data/color_profiles/Generic-CMYK-FF000000.jpg");
|
||||
let decoded = decode_raster_image(bytes).unwrap();
|
||||
for (actual, expected) in decoded.image.rgba[..4].iter().zip([0_u8, 163, 218, 255]) {
|
||||
assert!(
|
||||
actual.abs_diff(expected) <= 2,
|
||||
"actual {:?}",
|
||||
&decoded.image.rgba[..4]
|
||||
);
|
||||
}
|
||||
assert_eq!(decoded.color_managed.unwrap().rgba[3], 1.0);
|
||||
let raw =
|
||||
decode_raster_image_with_color_conversion(bytes, ColorProfileConversion::Ignore).unwrap();
|
||||
// JPEG quantization leaves 254 in the cyan sample of this fixture.
|
||||
let inks = jpeg_decoder::Decoder::new(std::io::Cursor::new(bytes))
|
||||
.decode()
|
||||
.unwrap();
|
||||
assert_eq!(&inks[..4], &[254, 0, 0, 0]);
|
||||
assert_eq!(&raw.image.rgba[..4], &[1, 255, 255, 255]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_png_sixteen_bit_samples_keep_low_bits_and_straight_alpha() {
|
||||
let mut info = ::png::Info::with_size(1, 1);
|
||||
info.color_type = ::png::ColorType::Rgba;
|
||||
info.bit_depth = ::png::BitDepth::Sixteen;
|
||||
let values = [0x1234_u16, 0x5678, 0x9abc, 0xdef0];
|
||||
let bytes = png_with_info(
|
||||
info,
|
||||
&values
|
||||
.into_iter()
|
||||
.flat_map(u16::to_be_bytes)
|
||||
.collect::<Vec<_>>(),
|
||||
);
|
||||
let decoded = decode_raster_image(&bytes).unwrap();
|
||||
let native = decoded.color_managed.unwrap();
|
||||
for (actual, value) in native.rgba.into_iter().zip(values) {
|
||||
assert!((actual - f32::from(value) / 65535.0).abs() < 0.000001);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_png_gamma_applies_to_gray_colors_without_changing_alpha() {
|
||||
let mut info = ::png::Info::with_size(1, 1);
|
||||
info.color_type = ::png::ColorType::GrayscaleAlpha;
|
||||
info.bit_depth = ::png::BitDepth::Eight;
|
||||
info.source_gamma = Some(::png::ScaledFloat::new(1.0));
|
||||
let bytes = png_with_info(info, &[128, 64]);
|
||||
let reader = ::png::Decoder::new(std::io::Cursor::new(&bytes))
|
||||
.read_info()
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
reader.info().gama_chunk.map(::png::ScaledFloat::into_value),
|
||||
Some(1.0)
|
||||
);
|
||||
let decoded = decode_raster_image(&bytes).unwrap();
|
||||
assert_eq!(&decoded.image.rgba[..4], &[188, 188, 188, 64]);
|
||||
|
||||
let raw =
|
||||
decode_raster_image_with_color_conversion(&bytes, ColorProfileConversion::Ignore).unwrap();
|
||||
assert_eq!(&raw.image.rgba[..4], &[128, 128, 128, 64]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_invalid_icc_profiles_leave_valid_png_pixels_readable() {
|
||||
let mut info = ::png::Info::with_size(1, 1);
|
||||
info.color_type = ::png::ColorType::Rgba;
|
||||
info.bit_depth = ::png::BitDepth::Eight;
|
||||
info.icc_profile = Some(Cow::Borrowed(b"invalid ICC profile"));
|
||||
let bytes = png_with_info(info, &[23, 74, 145, 211]);
|
||||
let decoded = decode_raster_image(&bytes).unwrap();
|
||||
assert_eq!(&decoded.image.rgba[..4], &[23, 74, 145, 211]);
|
||||
assert!(decoded.color_managed.is_none());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_png_cicp_takes_precedence_over_icc_and_converts_neutral_gray() {
|
||||
let mut info = ::png::Info::with_size(1, 1);
|
||||
info.color_type = ::png::ColorType::Rgba;
|
||||
info.bit_depth = ::png::BitDepth::Eight;
|
||||
info.icc_profile = Some(Cow::Owned(
|
||||
moxcms::ColorProfile::new_srgb().encode().unwrap(),
|
||||
));
|
||||
info.coding_independent_code_points = Some(::png::CodingIndependentCodePoints {
|
||||
color_primaries: 12,
|
||||
transfer_function: 13,
|
||||
matrix_coefficients: 0,
|
||||
is_video_full_range_image: true,
|
||||
});
|
||||
let red = decode_raster_image(&png_with_info(info, &[255, 0, 0, 255]))
|
||||
.unwrap()
|
||||
.color_managed
|
||||
.unwrap();
|
||||
assert!(red.rgba[0] > 1.0 && red.rgba[1] < 0.0);
|
||||
let mut info = ::png::Info::with_size(1, 1);
|
||||
info.color_type = ::png::ColorType::GrayscaleAlpha;
|
||||
info.bit_depth = ::png::BitDepth::Eight;
|
||||
info.coding_independent_code_points = Some(::png::CodingIndependentCodePoints {
|
||||
color_primaries: 1,
|
||||
transfer_function: 8,
|
||||
matrix_coefficients: 0,
|
||||
is_video_full_range_image: true,
|
||||
});
|
||||
let gray = decode_raster_image(&png_with_info(info, &[128, 64])).unwrap();
|
||||
assert_eq!(&gray.image.rgba[..4], &[188, 188, 188, 64]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn image_jpeg_late_icc_cannot_bypass_color_plane_admission() {
|
||||
let image = RgbaImage::try_new(1, 1, vec![155, 27, 26, 255]).unwrap();
|
||||
let bytes = encode_jpeg(&image, 100).unwrap().bytes;
|
||||
let profile = moxcms::ColorProfile::new_display_p3().encode().unwrap();
|
||||
let mut payload = b"ICC_PROFILE\0\x01\x01".to_vec();
|
||||
payload.extend(profile);
|
||||
let mut marker = vec![0xff, 0xe2];
|
||||
marker.extend(u16::try_from(payload.len() + 2).unwrap().to_be_bytes());
|
||||
marker.extend(payload);
|
||||
assert_eq!(&bytes[bytes.len() - 2..], &[0xff, 0xd9]);
|
||||
let mut late = bytes.clone();
|
||||
late.splice(late.len() - 2..late.len() - 2, marker.iter().copied());
|
||||
let mut decoder = jpeg_decoder::Decoder::new(std::io::Cursor::new(&late));
|
||||
decoder.decode().unwrap();
|
||||
assert!(decoder.icc_profile().is_some());
|
||||
let decoded = decode_raster_image(&late).unwrap();
|
||||
assert_eq!(
|
||||
decoded.metadata.color_precision,
|
||||
RasterColorPrecision::Unorm8
|
||||
);
|
||||
assert!(decoded.color_managed.is_none());
|
||||
assert_eq!(decoded.image, decode_raster_image(&bytes).unwrap().image);
|
||||
|
||||
let mut header = bytes;
|
||||
header.splice(2..2, marker);
|
||||
let decoded = decode_raster_image(&header).unwrap();
|
||||
assert_eq!(
|
||||
decoded.metadata.color_precision,
|
||||
RasterColorPrecision::Float32
|
||||
);
|
||||
assert!(decoded.color_managed.is_some());
|
||||
}
|
||||
+89
-15
@@ -38,6 +38,8 @@ pub enum JpegDecodeError {
|
||||
UnsupportedPixelFormat { format: &'static str },
|
||||
#[error(transparent)]
|
||||
InvalidOutput(#[from] RgbaImageError),
|
||||
#[error(transparent)]
|
||||
Color(#[from] crate::ColorDecodeError),
|
||||
}
|
||||
|
||||
pub fn encode_jpeg(image: &RgbaImage, quality: u8) -> Result<EncodedJpeg, JpegEncodeError> {
|
||||
@@ -75,39 +77,111 @@ pub fn encode_jpeg(image: &RgbaImage, quality: u8) -> Result<EncodedJpeg, JpegEn
|
||||
}
|
||||
|
||||
pub fn decode_jpeg(bytes: &[u8]) -> Result<RgbaImage, JpegDecodeError> {
|
||||
decode_jpeg_colors(bytes, true).map(|(image, _)| image)
|
||||
}
|
||||
|
||||
pub(crate) fn decode_jpeg_colors(
|
||||
bytes: &[u8],
|
||||
convert_profile: bool,
|
||||
) -> Result<crate::color::DecodedColorPixels, JpegDecodeError> {
|
||||
use crate::color::{ColorSamples, RawColorImage};
|
||||
let mut decoder = jpeg_decoder::Decoder::new(Cursor::new(bytes));
|
||||
decoder.set_max_decoding_buffer_size(MAX_DECODED_RGBA_BYTES);
|
||||
let decoded = decoder.decode()?;
|
||||
let info = decoder.info().ok_or(JpegDecodeError::MissingImageInfo)?;
|
||||
let rgba = match info.pixel_format {
|
||||
jpeg_decoder::PixelFormat::L8 => decoded
|
||||
.into_iter()
|
||||
.flat_map(|value| [value, value, value, 255])
|
||||
.collect(),
|
||||
jpeg_decoder::PixelFormat::RGB24 => decoded
|
||||
.as_chunks::<3>()
|
||||
.0
|
||||
.iter()
|
||||
.flat_map(|pixel| [pixel[0], pixel[1], pixel[2], 255])
|
||||
.collect(),
|
||||
// Use the same admission condition as the metadata probe. The decoder
|
||||
// collects APP2 markers even after scan data; a late-only profile must not
|
||||
// materialize color planes that the resource owner could not reserve.
|
||||
let profile = if has_icc_header(bytes) {
|
||||
decoder
|
||||
.icc_profile()
|
||||
.and_then(|bytes| moxcms::ColorProfile::new_from_slice(&bytes).ok())
|
||||
} else {
|
||||
None
|
||||
};
|
||||
let (layout, samples, cmyk) = match info.pixel_format {
|
||||
jpeg_decoder::PixelFormat::L8 => (moxcms::Layout::Gray, ColorSamples::U8(decoded), false),
|
||||
jpeg_decoder::PixelFormat::RGB24 => (moxcms::Layout::Rgb, ColorSamples::U8(decoded), false),
|
||||
jpeg_decoder::PixelFormat::L16 => {
|
||||
return Err(JpegDecodeError::UnsupportedPixelFormat { format: "L16" });
|
||||
}
|
||||
jpeg_decoder::PixelFormat::CMYK32 => {
|
||||
return Err(JpegDecodeError::UnsupportedPixelFormat { format: "CMYK32" });
|
||||
(moxcms::Layout::Rgba, ColorSamples::U8(decoded), true)
|
||||
}
|
||||
};
|
||||
RgbaImage::try_new(u32::from(info.width), u32::from(info.height), rgba).map_err(Into::into)
|
||||
Ok(RawColorImage {
|
||||
width: u32::from(info.width),
|
||||
height: u32::from(info.height),
|
||||
samples,
|
||||
layout,
|
||||
profile,
|
||||
cmyk,
|
||||
}
|
||||
.finish(convert_profile)?)
|
||||
}
|
||||
|
||||
pub(crate) fn jpeg_dimensions(bytes: &[u8]) -> Result<(u32, u32), JpegDecodeError> {
|
||||
pub(crate) fn jpeg_metadata(
|
||||
bytes: &[u8],
|
||||
) -> Result<(u32, u32, crate::RasterColorPrecision), JpegDecodeError> {
|
||||
let mut decoder = jpeg_decoder::Decoder::new(Cursor::new(bytes));
|
||||
decoder.set_max_decoding_buffer_size(MAX_DECODED_RGBA_BYTES);
|
||||
decoder.read_info()?;
|
||||
let info = decoder.info().ok_or(JpegDecodeError::MissingImageInfo)?;
|
||||
let dimensions = (u32::from(info.width), u32::from(info.height));
|
||||
crate::rgba::checked_rgba_len(dimensions.0, dimensions.1)?;
|
||||
Ok(dimensions)
|
||||
let high_precision = matches!(
|
||||
info.pixel_format,
|
||||
jpeg_decoder::PixelFormat::L16 | jpeg_decoder::PixelFormat::CMYK32
|
||||
) || has_icc_header(bytes);
|
||||
Ok((
|
||||
dimensions.0,
|
||||
dimensions.1,
|
||||
if high_precision {
|
||||
crate::RasterColorPrecision::Float32
|
||||
} else {
|
||||
crate::RasterColorPrecision::Unorm8
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
fn has_icc_header(bytes: &[u8]) -> bool {
|
||||
let mut remaining = bytes.get(2..).unwrap_or_default();
|
||||
while let Some(marker) = remaining.strip_prefix(&[0xff]) {
|
||||
let Some((&kind, tail)) = marker.split_first() else {
|
||||
return false;
|
||||
};
|
||||
if kind == 0xff {
|
||||
remaining = marker;
|
||||
continue;
|
||||
}
|
||||
if kind == 0xda || kind == 0xd9 {
|
||||
break;
|
||||
}
|
||||
if kind == 0x01 || (0xd0..=0xd8).contains(&kind) {
|
||||
remaining = tail;
|
||||
continue;
|
||||
}
|
||||
let Some(length) = tail
|
||||
.get(..2)
|
||||
.map(|value| usize::from(u16::from_be_bytes([value[0], value[1]])))
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
if length < 2 {
|
||||
return false;
|
||||
}
|
||||
let Some(segment) = tail.get(2..length) else {
|
||||
return false;
|
||||
};
|
||||
if kind == 0xe2 && segment.starts_with(b"ICC_PROFILE\0") {
|
||||
return true;
|
||||
}
|
||||
let Some(next) = tail.get(length..) else {
|
||||
return false;
|
||||
};
|
||||
remaining = next;
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
+10
-3
@@ -6,6 +6,9 @@
|
||||
|
||||
mod bounded;
|
||||
mod budget;
|
||||
mod color;
|
||||
#[cfg(test)]
|
||||
mod color_tests;
|
||||
mod jpeg;
|
||||
mod png;
|
||||
mod raster;
|
||||
@@ -13,15 +16,19 @@ mod rgba;
|
||||
mod svg;
|
||||
|
||||
pub use budget::{MAX_DECODED_RGBA_BYTES, MAX_ENCODED_IMAGE_BYTES};
|
||||
pub use color::{
|
||||
ColorDecodeError, ColorManagedRasterImage, RasterColorPrecision, UnconvertedRasterPixels,
|
||||
};
|
||||
pub use jpeg::{EncodedJpeg, JpegDecodeError, JpegEncodeError, decode_jpeg, encode_jpeg};
|
||||
pub use png::{
|
||||
EncodedPng, PngDecodeError, PngEncodeError, PngEncodeOptions, decode_png, encode_png,
|
||||
encode_png_rgba8, encode_png_rgba8_with_options, encode_png_with_options,
|
||||
};
|
||||
pub use raster::{
|
||||
DecodedRasterImage, RasterDecodeError, RasterImageFormat, RasterImageMetadata,
|
||||
decode_raster_image, decode_raster_image_with_metadata, probe_raster_image,
|
||||
raster_image_dimensions, supports_image_mime_essence,
|
||||
ColorProfileConversion, DecodedRasterImage, RasterDecodeError, RasterImageFormat,
|
||||
RasterImageMetadata, decode_raster_image, decode_raster_image_with_color_conversion,
|
||||
decode_raster_image_with_metadata, probe_raster_image, raster_image_dimensions,
|
||||
supports_image_mime_essence,
|
||||
};
|
||||
pub use rgba::{RgbaImage, RgbaImageError};
|
||||
pub use svg::{
|
||||
|
||||
+172
-31
@@ -34,6 +34,8 @@ pub enum PngDecodeError {
|
||||
Decoding(#[from] png::DecodingError),
|
||||
#[error(transparent)]
|
||||
InvalidOutput(#[from] RgbaImageError),
|
||||
#[error(transparent)]
|
||||
Color(#[from] crate::ColorDecodeError),
|
||||
#[error("PNG decoder produced unsupported color type {color_type:?}")]
|
||||
UnsupportedColorType { color_type: png::ColorType },
|
||||
}
|
||||
@@ -109,51 +111,91 @@ pub fn encode_png_rgba8_with_options(
|
||||
}
|
||||
|
||||
pub fn decode_png(bytes: &[u8]) -> Result<RgbaImage, PngDecodeError> {
|
||||
decode_png_colors(bytes, true).map(|(image, _)| image)
|
||||
}
|
||||
|
||||
pub(crate) fn decode_png_colors(
|
||||
bytes: &[u8],
|
||||
convert_profile: bool,
|
||||
) -> Result<crate::color::DecodedColorPixels, PngDecodeError> {
|
||||
use crate::color::{ColorSamples, RawColorImage};
|
||||
let mut decoder = png::Decoder::new_with_limits(
|
||||
Cursor::new(bytes),
|
||||
png::Limits {
|
||||
bytes: MAX_DECODED_RGBA_BYTES,
|
||||
},
|
||||
);
|
||||
decoder.set_transformations(png::Transformations::EXPAND | png::Transformations::STRIP_16);
|
||||
decoder.set_transformations(png::Transformations::EXPAND);
|
||||
let mut reader = decoder.read_info()?;
|
||||
let (width, height) = (reader.info().width, reader.info().height);
|
||||
checked_rgba_len(width, height)?;
|
||||
let output_buffer_size = reader
|
||||
let cicp = cicp_profile(reader.info());
|
||||
let expand_gray = cicp.is_some();
|
||||
let profile = cicp.or_else(|| color_profile(reader.info()));
|
||||
let size = reader
|
||||
.output_buffer_size()
|
||||
.ok_or(RgbaImageError::BufferLengthOverflow { width, height })?;
|
||||
let mut decoded = vec![0; output_buffer_size];
|
||||
let mut decoded = vec![0; size];
|
||||
let output = reader.next_frame(&mut decoded)?;
|
||||
decoded.truncate(output.buffer_size());
|
||||
|
||||
let rgba = match output.color_type {
|
||||
png::ColorType::Rgba => decoded,
|
||||
png::ColorType::Rgb => decoded
|
||||
.as_chunks::<3>()
|
||||
.0
|
||||
.iter()
|
||||
.flat_map(|pixel| [pixel[0], pixel[1], pixel[2], 255])
|
||||
.collect(),
|
||||
png::ColorType::Grayscale => decoded
|
||||
.into_iter()
|
||||
.flat_map(|value| [value, value, value, 255])
|
||||
.collect(),
|
||||
png::ColorType::GrayscaleAlpha => decoded
|
||||
.as_chunks::<2>()
|
||||
.0
|
||||
.iter()
|
||||
.flat_map(|pixel| [pixel[0], pixel[0], pixel[0], pixel[1]])
|
||||
.collect(),
|
||||
png::ColorType::Indexed => {
|
||||
return Err(PngDecodeError::UnsupportedColorType {
|
||||
color_type: output.color_type,
|
||||
});
|
||||
let mut layout = match output.color_type {
|
||||
png::ColorType::Rgb => moxcms::Layout::Rgb,
|
||||
png::ColorType::Rgba => moxcms::Layout::Rgba,
|
||||
png::ColorType::Grayscale => moxcms::Layout::Gray,
|
||||
png::ColorType::GrayscaleAlpha => moxcms::Layout::GrayAlpha,
|
||||
color_type @ png::ColorType::Indexed => {
|
||||
return Err(PngDecodeError::UnsupportedColorType { color_type });
|
||||
}
|
||||
};
|
||||
RgbaImage::try_new(output.width, output.height, rgba).map_err(Into::into)
|
||||
let mut samples = if output.bit_depth == png::BitDepth::Sixteen {
|
||||
ColorSamples::U16(
|
||||
decoded
|
||||
.as_chunks::<2>()
|
||||
.0
|
||||
.iter()
|
||||
.map(|value| u16::from_be_bytes(*value))
|
||||
.collect(),
|
||||
)
|
||||
} else {
|
||||
ColorSamples::U8(decoded)
|
||||
};
|
||||
if expand_gray && matches!(layout, moxcms::Layout::Gray | moxcms::Layout::GrayAlpha) {
|
||||
// cICP describes RGB colorimetry even when PNG stores neutral gray.
|
||||
let channels = if layout == moxcms::Layout::GrayAlpha {
|
||||
2
|
||||
} else {
|
||||
1
|
||||
};
|
||||
samples = match samples {
|
||||
ColorSamples::U8(values) => ColorSamples::U8(
|
||||
values
|
||||
.chunks_exact(channels)
|
||||
.flat_map(|p| [p[0], p[0], p[0], if channels == 2 { p[1] } else { 255 }])
|
||||
.collect(),
|
||||
),
|
||||
ColorSamples::U16(values) => ColorSamples::U16(
|
||||
values
|
||||
.chunks_exact(channels)
|
||||
.flat_map(|p| [p[0], p[0], p[0], if channels == 2 { p[1] } else { 65535 }])
|
||||
.collect(),
|
||||
),
|
||||
};
|
||||
layout = moxcms::Layout::Rgba;
|
||||
}
|
||||
Ok(RawColorImage {
|
||||
width,
|
||||
height,
|
||||
samples,
|
||||
layout,
|
||||
profile,
|
||||
cmyk: false,
|
||||
}
|
||||
.finish(convert_profile)?)
|
||||
}
|
||||
|
||||
pub(crate) fn png_dimensions(bytes: &[u8]) -> Result<(u32, u32), PngDecodeError> {
|
||||
pub(crate) fn png_metadata(
|
||||
bytes: &[u8],
|
||||
) -> Result<(u32, u32, crate::RasterColorPrecision), PngDecodeError> {
|
||||
let decoder = png::Decoder::new_with_limits(
|
||||
Cursor::new(bytes),
|
||||
png::Limits {
|
||||
@@ -161,9 +203,108 @@ pub(crate) fn png_dimensions(bytes: &[u8]) -> Result<(u32, u32), PngDecodeError>
|
||||
},
|
||||
);
|
||||
let reader = decoder.read_info()?;
|
||||
let dimensions = (reader.info().width, reader.info().height);
|
||||
checked_rgba_len(dimensions.0, dimensions.1)?;
|
||||
Ok(dimensions)
|
||||
let info = reader.info();
|
||||
checked_rgba_len(info.width, info.height)?;
|
||||
let high_precision = info.bit_depth == png::BitDepth::Sixteen
|
||||
|| info.icc_profile.is_some()
|
||||
|| info.coding_independent_code_points.is_some()
|
||||
|| (info.srgb.is_none() && (info.gama_chunk.is_some() || info.chrm_chunk.is_some()));
|
||||
Ok((
|
||||
info.width,
|
||||
info.height,
|
||||
if high_precision {
|
||||
crate::RasterColorPrecision::Float32
|
||||
} else {
|
||||
crate::RasterColorPrecision::Unorm8
|
||||
},
|
||||
))
|
||||
}
|
||||
|
||||
fn cicp_profile(info: &png::Info<'_>) -> Option<moxcms::ColorProfile> {
|
||||
if let Some(cicp) = info.coding_independent_code_points
|
||||
&& cicp.matrix_coefficients == 0
|
||||
&& cicp.is_video_full_range_image
|
||||
&& let (Ok(color_primaries), Ok(transfer_characteristics), Ok(matrix_coefficients)) = (
|
||||
moxcms::CicpColorPrimaries::try_from(cicp.color_primaries),
|
||||
moxcms::TransferCharacteristics::try_from(cicp.transfer_function),
|
||||
moxcms::MatrixCoefficients::try_from(cicp.matrix_coefficients),
|
||||
)
|
||||
&& moxcms::ColorPrimaries::try_from(color_primaries).is_ok()
|
||||
&& moxcms::ToneReprCurve::try_from(transfer_characteristics).is_ok()
|
||||
{
|
||||
return Some(moxcms::ColorProfile::new_from_cicp(moxcms::CicpProfile {
|
||||
color_primaries,
|
||||
transfer_characteristics,
|
||||
matrix_coefficients,
|
||||
full_range: true,
|
||||
}));
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
fn color_profile(info: &png::Info<'_>) -> Option<moxcms::ColorProfile> {
|
||||
use moxcms::{Chromaticity, ColorPrimaries, ColorProfile, ToneReprCurve, XyY};
|
||||
if let Some(profile) = info
|
||||
.icc_profile
|
||||
.as_deref()
|
||||
.and_then(|bytes| ColorProfile::new_from_slice(bytes).ok())
|
||||
{
|
||||
return Some(profile);
|
||||
}
|
||||
if info.srgb.is_some() || (info.gama_chunk.is_none() && info.chrm_chunk.is_none()) {
|
||||
return None;
|
||||
}
|
||||
let gray = matches!(
|
||||
info.color_type,
|
||||
png::ColorType::Grayscale | png::ColorType::GrayscaleAlpha
|
||||
);
|
||||
let mut profile = if gray {
|
||||
let mut profile = ColorProfile::new_gray_with_gamma(1.0);
|
||||
profile.gray_trc = ColorProfile::new_srgb().red_trc;
|
||||
profile
|
||||
} else {
|
||||
ColorProfile::new_srgb()
|
||||
};
|
||||
profile.cicp = None;
|
||||
if let Some(gamma) = info
|
||||
.gama_chunk
|
||||
.map(png::ScaledFloat::into_value)
|
||||
.filter(|gamma| *gamma > 0.0)
|
||||
{
|
||||
let curve = ToneReprCurve::Parametric(vec![1.0 / gamma]);
|
||||
if gray {
|
||||
profile.gray_trc = Some(curve);
|
||||
} else {
|
||||
profile.red_trc = Some(curve.clone());
|
||||
profile.green_trc = Some(curve.clone());
|
||||
profile.blue_trc = Some(curve);
|
||||
}
|
||||
}
|
||||
if !gray && let Some(chroma) = info.chrm_chunk {
|
||||
let point = |(x, y): (png::ScaledFloat, png::ScaledFloat)| {
|
||||
Chromaticity::new(x.into_value(), y.into_value())
|
||||
};
|
||||
let white = point(chroma.white);
|
||||
if white.y > 0.0
|
||||
&& [chroma.red, chroma.green, chroma.blue]
|
||||
.iter()
|
||||
.all(|(_, y)| y.into_value() > 0.0)
|
||||
{
|
||||
let white = XyY::new(f64::from(white.x), f64::from(white.y), 1.0);
|
||||
let primaries = ColorPrimaries {
|
||||
red: point(chroma.red),
|
||||
green: point(chroma.green),
|
||||
blue: point(chroma.blue),
|
||||
};
|
||||
if ColorProfile::colorants_matrix(white, primaries)
|
||||
.determinant()
|
||||
.is_some_and(|value| value.is_finite())
|
||||
{
|
||||
profile.update_rgb_colorimetry(white, primaries);
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(profile)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
|
||||
+95
-22
@@ -1,10 +1,12 @@
|
||||
use std::io::{BufReader, Cursor};
|
||||
|
||||
use image::{ImageFormat, ImageReader, Limits};
|
||||
use image::{DynamicImage, ImageDecoder, ImageFormat, ImageReader, Limits};
|
||||
|
||||
use crate::{
|
||||
JpegDecodeError, MAX_DECODED_RGBA_BYTES, MAX_ENCODED_IMAGE_BYTES, PngDecodeError, RgbaImage,
|
||||
RgbaImageError, decode_jpeg, decode_png, jpeg::jpeg_dimensions, png::png_dimensions,
|
||||
ColorManagedRasterImage, JpegDecodeError, MAX_DECODED_RGBA_BYTES, MAX_ENCODED_IMAGE_BYTES,
|
||||
PngDecodeError, RasterColorPrecision, RgbaImage, RgbaImageError,
|
||||
jpeg::{decode_jpeg_colors, jpeg_metadata},
|
||||
png::{decode_png_colors, png_metadata},
|
||||
};
|
||||
|
||||
/// Static raster formats admitted by the HTML image pipeline.
|
||||
@@ -55,20 +57,30 @@ pub struct RasterImageMetadata {
|
||||
pub format: RasterImageFormat,
|
||||
pub width: u32,
|
||||
pub height: u32,
|
||||
pub color_precision: RasterColorPrecision,
|
||||
}
|
||||
|
||||
impl RasterImageMetadata {
|
||||
/// Returns the validated RGBA8 allocation required by this image.
|
||||
/// Retained layout pixels plus any high precision Canvas colors.
|
||||
pub fn decoded_byte_len(self) -> usize {
|
||||
crate::rgba::checked_rgba_len(self.width, self.height)
|
||||
.expect("raster metadata is validated before construction")
|
||||
let rgba = crate::rgba::checked_rgba_len(self.width, self.height)
|
||||
.expect("raster metadata is validated before construction");
|
||||
rgba / 4 * self.color_precision.retained_bytes_per_pixel()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
#[derive(Clone, Debug, PartialEq)]
|
||||
pub struct DecodedRasterImage {
|
||||
pub metadata: RasterImageMetadata,
|
||||
pub image: RgbaImage,
|
||||
pub color_managed: Option<ColorManagedRasterImage>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Default)]
|
||||
pub enum ColorProfileConversion {
|
||||
#[default]
|
||||
Apply,
|
||||
Ignore,
|
||||
}
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
@@ -102,6 +114,8 @@ pub enum RasterDecodeError {
|
||||
},
|
||||
#[error(transparent)]
|
||||
InvalidOutput(#[from] RgbaImageError),
|
||||
#[error(transparent)]
|
||||
Color(#[from] crate::ColorDecodeError),
|
||||
}
|
||||
|
||||
pub fn probe_raster_image(bytes: &[u8]) -> Result<RasterImageMetadata, RasterDecodeError> {
|
||||
@@ -109,23 +123,43 @@ pub fn probe_raster_image(bytes: &[u8]) -> Result<RasterImageMetadata, RasterDec
|
||||
let format = image::guess_format(bytes).map_err(|_| RasterDecodeError::UnknownFormat)?;
|
||||
let admitted = RasterImageFormat::from_image_format(format)
|
||||
.ok_or(RasterDecodeError::UnsupportedFormat(format))?;
|
||||
let (width, height) = match admitted {
|
||||
RasterImageFormat::Png => png_dimensions(bytes)?,
|
||||
RasterImageFormat::Jpeg => jpeg_dimensions(bytes)?,
|
||||
let (width, height, color_precision) = match admitted {
|
||||
RasterImageFormat::Png => png_metadata(bytes)?,
|
||||
RasterImageFormat::Jpeg => jpeg_metadata(bytes)?,
|
||||
RasterImageFormat::Gif | RasterImageFormat::WebP | RasterImageFormat::Bmp => {
|
||||
let mut reader = ImageReader::with_format(BufReader::new(Cursor::new(bytes)), format);
|
||||
reader.limits(decode_limits());
|
||||
reader
|
||||
.into_dimensions()
|
||||
let mut decoder = reader.into_decoder().map_err(RasterDecodeError::Metadata)?;
|
||||
let (width, height) = decoder.dimensions();
|
||||
let color_precision = if decoder
|
||||
.icc_profile()
|
||||
.map_err(RasterDecodeError::Metadata)?
|
||||
.is_some()
|
||||
{
|
||||
RasterColorPrecision::Float32
|
||||
} else {
|
||||
RasterColorPrecision::Unorm8
|
||||
};
|
||||
(width, height, color_precision)
|
||||
}
|
||||
};
|
||||
crate::rgba::checked_rgba_len(width, height)?;
|
||||
Ok(RasterImageMetadata {
|
||||
let metadata = RasterImageMetadata {
|
||||
format: admitted,
|
||||
width,
|
||||
height,
|
||||
})
|
||||
color_precision,
|
||||
};
|
||||
crate::rgba::checked_rgba_len(width, height)?;
|
||||
if metadata.decoded_byte_len() > crate::budget::MAX_DECODED_COLOR_BYTES {
|
||||
return Err(RgbaImageError::BufferBudgetExceeded {
|
||||
width,
|
||||
height,
|
||||
required: metadata.decoded_byte_len(),
|
||||
max: crate::budget::MAX_DECODED_COLOR_BYTES,
|
||||
}
|
||||
.into());
|
||||
}
|
||||
Ok(metadata)
|
||||
}
|
||||
|
||||
/// Reads encoded raster dimensions without allocating or decoding an RGBA surface.
|
||||
@@ -143,17 +177,33 @@ pub fn decode_raster_image(bytes: &[u8]) -> Result<DecodedRasterImage, RasterDec
|
||||
decode_raster_image_with_metadata(bytes, metadata)
|
||||
}
|
||||
|
||||
pub fn decode_raster_image_with_color_conversion(
|
||||
bytes: &[u8],
|
||||
conversion: ColorProfileConversion,
|
||||
) -> Result<DecodedRasterImage, RasterDecodeError> {
|
||||
decode_raster(bytes, probe_raster_image(bytes)?, conversion)
|
||||
}
|
||||
|
||||
/// Decodes pixels after a caller has already probed the same immutable byte
|
||||
/// buffer. This lets resource owners reserve aggregate decoded-byte budget
|
||||
/// before allocating RGBA without parsing metadata a second time.
|
||||
pub fn decode_raster_image_with_metadata(
|
||||
bytes: &[u8],
|
||||
metadata: RasterImageMetadata,
|
||||
) -> Result<DecodedRasterImage, RasterDecodeError> {
|
||||
decode_raster(bytes, metadata, ColorProfileConversion::Apply)
|
||||
}
|
||||
|
||||
fn decode_raster(
|
||||
bytes: &[u8],
|
||||
metadata: RasterImageMetadata,
|
||||
conversion: ColorProfileConversion,
|
||||
) -> Result<DecodedRasterImage, RasterDecodeError> {
|
||||
check_encoded_budget(bytes)?;
|
||||
let image = match metadata.format {
|
||||
RasterImageFormat::Png => decode_png(bytes)?,
|
||||
RasterImageFormat::Jpeg => decode_jpeg(bytes)?,
|
||||
let convert_profile = matches!(conversion, ColorProfileConversion::Apply);
|
||||
let (image, color_managed) = match metadata.format {
|
||||
RasterImageFormat::Png => decode_png_colors(bytes, convert_profile)?,
|
||||
RasterImageFormat::Jpeg => decode_jpeg_colors(bytes, convert_profile)?,
|
||||
format @ (RasterImageFormat::Gif | RasterImageFormat::WebP | RasterImageFormat::Bmp) => {
|
||||
let image_format = match format {
|
||||
RasterImageFormat::Gif => ImageFormat::Gif,
|
||||
@@ -164,11 +214,25 @@ pub fn decode_raster_image_with_metadata(
|
||||
let mut reader =
|
||||
ImageReader::with_format(BufReader::new(Cursor::new(bytes)), image_format);
|
||||
reader.limits(decode_limits());
|
||||
let decoded = reader
|
||||
.decode()
|
||||
let mut decoder = reader
|
||||
.into_decoder()
|
||||
.map_err(|source| RasterDecodeError::Image { format, source })?;
|
||||
let profile = decoder
|
||||
.icc_profile()
|
||||
.map_err(|source| RasterDecodeError::Image { format, source })?
|
||||
.and_then(|bytes| moxcms::ColorProfile::new_from_slice(&bytes).ok());
|
||||
let decoded = DynamicImage::from_decoder(decoder)
|
||||
.map_err(|source| RasterDecodeError::Image { format, source })?;
|
||||
let rgba = decoded.into_rgba8();
|
||||
RgbaImage::try_new(rgba.width(), rgba.height(), rgba.into_raw())?
|
||||
crate::color::RawColorImage {
|
||||
width: rgba.width(),
|
||||
height: rgba.height(),
|
||||
samples: crate::color::ColorSamples::U8(rgba.into_raw()),
|
||||
layout: moxcms::Layout::Rgba,
|
||||
profile,
|
||||
cmyk: false,
|
||||
}
|
||||
.finish(convert_profile)?
|
||||
}
|
||||
};
|
||||
if (image.width, image.height) != (metadata.width, metadata.height) {
|
||||
@@ -179,7 +243,11 @@ pub fn decode_raster_image_with_metadata(
|
||||
actual_height: image.height,
|
||||
});
|
||||
}
|
||||
Ok(DecodedRasterImage { metadata, image })
|
||||
Ok(DecodedRasterImage {
|
||||
metadata,
|
||||
image,
|
||||
color_managed,
|
||||
})
|
||||
}
|
||||
|
||||
fn check_encoded_budget(bytes: &[u8]) -> Result<(), RasterDecodeError> {
|
||||
@@ -223,6 +291,7 @@ mod tests {
|
||||
format: RasterImageFormat::Png,
|
||||
width: 2,
|
||||
height: 2,
|
||||
color_precision: RasterColorPrecision::Unorm8,
|
||||
}
|
||||
);
|
||||
assert_eq!(raster_image_dimensions(&encoded.bytes).unwrap(), (2, 2));
|
||||
@@ -314,6 +383,7 @@ mod tests {
|
||||
format: RasterImageFormat::Bmp,
|
||||
width: 2,
|
||||
height: 2,
|
||||
color_precision: RasterColorPrecision::Unorm8,
|
||||
}
|
||||
);
|
||||
assert_eq!(metadata.decoded_byte_len(), 16);
|
||||
@@ -388,6 +458,7 @@ mod tests {
|
||||
format: RasterImageFormat::Jpeg,
|
||||
width: 2,
|
||||
height: 2,
|
||||
color_precision: RasterColorPrecision::Unorm8,
|
||||
}
|
||||
);
|
||||
let decoded_jpeg = decode_raster_image(&jpeg.bytes).unwrap();
|
||||
@@ -403,6 +474,7 @@ mod tests {
|
||||
format: RasterImageFormat::Gif,
|
||||
width: 1,
|
||||
height: 1,
|
||||
color_precision: RasterColorPrecision::Unorm8,
|
||||
}
|
||||
);
|
||||
let decoded_gif = decode_raster_image(gif).unwrap();
|
||||
@@ -418,6 +490,7 @@ mod tests {
|
||||
format: RasterImageFormat::WebP,
|
||||
width: 2,
|
||||
height: 2,
|
||||
color_precision: RasterColorPrecision::Unorm8,
|
||||
}
|
||||
);
|
||||
assert_eq!(decode_raster_image(&webp).unwrap().image, sample());
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 562 B |
Binary file not shown.
|
After Width: | Height: | Size: 620 B |
Binary file not shown.
|
After Width: | Height: | Size: 605 B |
Binary file not shown.
|
After Width: | Height: | Size: 607 B |
Binary file not shown.
|
After Width: | Height: | Size: 54 KiB |
@@ -0,0 +1,11 @@
|
||||
# The 3-Clause BSD License
|
||||
|
||||
Copyright © web-platform-tests contributors
|
||||
|
||||
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
|
||||
|
||||
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
|
||||
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
|
||||
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
@@ -0,0 +1,15 @@
|
||||
# Color profile fixtures
|
||||
|
||||
Copied without changes from web-platform-tests commit `db95fafd1fcef8428805e41eb5705d444e8c67ce`.
|
||||
|
||||
Upstream directory: `html/canvas/element/manual/wide-gamut-canvas/resources/`.
|
||||
The BSD 3-Clause license is preserved in LICENSE-WPT.md.
|
||||
|
||||
| File | SHA-256 |
|
||||
| --- | --- |
|
||||
| Display-P3-FF0000FF.png | f139c78a9d160a3b8fb81f859e508432a0b3ef223af9ccf46981723ffde79e8d |
|
||||
| Display-P3-BB0000CC.png | cc3d8a74a2ff5f1ff7e9298979a1c9972065f8dd860d714469384435f443a28d |
|
||||
| Display-P3-BBBC00000000CCCC.png | 5fbdaaf4720871a4ac55cee84df1aa80645f1fb0545fc70d77941ab6328283ef |
|
||||
| Adobe-RGB-FF0000FF.png | c8d91055ff37d287e4f1f41767b58552a4ff5660793214045b572c61c164094a |
|
||||
| sRGB-BB0000FF.png | e79074833885f4d00a66422ab8d9bcab74000fbe481c6c97b1359a5fc367fba1 |
|
||||
| Generic-CMYK-FF000000.jpg | aa9b574863a7c16a2a757c79d68cc0d52d32c0d1e580d5b7a665156bcf549ba7 |
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 230 B |
@@ -92,7 +92,7 @@ pub(crate) fn create_image_bitmap_callback<'s>(
|
||||
// detach the source. No V8 handles or live DOM state cross the task boundary.
|
||||
let (input, exception) = {
|
||||
v8::tc_scope!(let tc, scope);
|
||||
let input = snapshot_bitmap_source(tc, source);
|
||||
let input = snapshot_bitmap_source(tc, source, parameters.options.color_conversion());
|
||||
let exception = tc.exception();
|
||||
if exception.is_some() {
|
||||
tc.reset();
|
||||
@@ -119,26 +119,31 @@ pub(crate) fn create_image_bitmap_callback<'s>(
|
||||
BitmapInput::Pixels(image) => {
|
||||
producer.send(parameters.apply(image));
|
||||
}
|
||||
BitmapInput::Blob(bytes) => {
|
||||
let decode = move || {
|
||||
let result = moli_image::decode_raster_image(&bytes)
|
||||
.map_err(|_| BitmapRejection::InvalidState)
|
||||
.and_then(|decoded| {
|
||||
CanvasImage::rgba8(
|
||||
decoded.image.width,
|
||||
decoded.image.height,
|
||||
decoded.image.rgba,
|
||||
)
|
||||
.ok_or(BitmapRejection::InvalidState)
|
||||
})
|
||||
.and_then(|image| parameters.apply(image));
|
||||
producer.send(result);
|
||||
};
|
||||
if let Ok(handle) = tokio::runtime::Handle::try_current() {
|
||||
handle.spawn_blocking(decode);
|
||||
} else {
|
||||
std::thread::spawn(decode);
|
||||
}
|
||||
BitmapInput::Blob(data) => {
|
||||
data.read_all(Box::new(move |bytes| {
|
||||
let decode = move || {
|
||||
let result = bytes
|
||||
.map_err(|_| BitmapRejection::InvalidState)
|
||||
.and_then(|bytes| {
|
||||
moli_image::decode_raster_image_with_color_conversion(
|
||||
&bytes,
|
||||
parameters.options.color_conversion(),
|
||||
)
|
||||
.map_err(|_| BitmapRejection::InvalidState)
|
||||
})
|
||||
.and_then(|decoded| {
|
||||
CanvasImage::from_decoded_raster(decoded)
|
||||
.ok_or(BitmapRejection::InvalidState)
|
||||
})
|
||||
.and_then(|image| parameters.apply(image));
|
||||
producer.send(result);
|
||||
};
|
||||
if let Ok(handle) = tokio::runtime::Handle::try_current() {
|
||||
handle.spawn_blocking(decode);
|
||||
} else {
|
||||
std::thread::spawn(decode);
|
||||
}
|
||||
}));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -217,22 +222,23 @@ struct BitmapCropArgs<'s> {
|
||||
}
|
||||
|
||||
enum BitmapInput {
|
||||
Blob(Vec<u8>),
|
||||
Blob(moli_file_api::BlobData),
|
||||
Pixels(CanvasImage),
|
||||
}
|
||||
|
||||
fn snapshot_bitmap_source<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
source: v8::Local<'s, v8::Object>,
|
||||
conversion: moli_image::ColorProfileConversion,
|
||||
) -> Option<BitmapInput> {
|
||||
let source = moli_webapi_declare::web_api_object_target(scope, source)?;
|
||||
if web_api_interfaces::Blob::is_instance(scope, source) {
|
||||
return crate::blob::blob_bytes_from_object(scope, source).map(BitmapInput::Blob);
|
||||
return crate::blob::blob_data_from_object(scope, source).map(BitmapInput::Blob);
|
||||
}
|
||||
let image = if web_api_interfaces::ImageData::is_instance(scope, source) {
|
||||
crate::context_bootstrap::image_data::image_data_pixel_snapshot(scope, source)?
|
||||
} else {
|
||||
super::image_source::image_source_image(scope, source)?
|
||||
super::image_source::image_source_image_with_conversion(scope, source, conversion)?
|
||||
};
|
||||
if image.pixels.width() == 0 || image.pixels.height() == 0 {
|
||||
return None;
|
||||
|
||||
@@ -71,10 +71,9 @@ impl ResizeQuality {
|
||||
#[derive(Default, webidl::WebIdlDictionary)]
|
||||
#[webidl(prefix = "ImageBitmapOptions")]
|
||||
pub(super) struct BitmapOptions {
|
||||
// The raster backend leaves color profiles unconverted in both modes.
|
||||
// Members are declared in Web IDL's observable lexicographic read order.
|
||||
#[webidl(name = "colorSpaceConversion", converter = "enum", default = ColorSpaceConversion::Default)]
|
||||
_color_space_conversion: ColorSpaceConversion,
|
||||
color_space_conversion: ColorSpaceConversion,
|
||||
#[webidl(converter = "enum", default = ImageOrientation::FromImage)]
|
||||
image_orientation: ImageOrientation,
|
||||
#[webidl(converter = "enum", default = PremultiplyAlpha::Default)]
|
||||
@@ -87,6 +86,15 @@ pub(super) struct BitmapOptions {
|
||||
pub(super) resize_width: Option<u32>,
|
||||
}
|
||||
|
||||
impl BitmapOptions {
|
||||
pub(super) fn color_conversion(&self) -> moli_image::ColorProfileConversion {
|
||||
match self.color_space_conversion {
|
||||
ColorSpaceConversion::Default => moli_image::ColorProfileConversion::Apply,
|
||||
ColorSpaceConversion::None => moli_image::ColorProfileConversion::Ignore,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) struct BitmapParameters {
|
||||
pub(super) crop: Option<[i32; 4]>,
|
||||
pub(super) options: BitmapOptions,
|
||||
|
||||
@@ -46,6 +46,14 @@ pub(in crate::context_bootstrap) fn image_source_pixels<'s>(
|
||||
pub(super) fn image_source_image<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
source: v8::Local<'s, v8::Object>,
|
||||
) -> Option<CanvasImage> {
|
||||
image_source_image_with_conversion(scope, source, moli_image::ColorProfileConversion::Apply)
|
||||
}
|
||||
|
||||
pub(super) fn image_source_image_with_conversion<'s>(
|
||||
scope: &mut v8::PinScope<'s, '_>,
|
||||
source: v8::Local<'s, v8::Object>,
|
||||
conversion: moli_image::ColorProfileConversion,
|
||||
) -> Option<CanvasImage> {
|
||||
if web_api_interfaces::VideoFrame::is_instance(scope, source) {
|
||||
let (bytes, width, height) = super::super::video_frame::rendered_pixels(scope, source)?;
|
||||
@@ -66,10 +74,18 @@ pub(super) fn image_source_image<'s>(
|
||||
.ok()?;
|
||||
let host = unsafe { &*host };
|
||||
match host.raster_image_for_canvas(handle) {
|
||||
Ok(Some(pixels)) => Some(
|
||||
CanvasImage::rgba8(pixels.width, pixels.height, pixels.rgba.clone())?
|
||||
.with_origin_clean(host.image_resource_origin_clean(handle)?),
|
||||
),
|
||||
Ok(Some(pixels)) => {
|
||||
let image = if let Some(managed) = pixels.color_managed {
|
||||
CanvasImage::from_color_managed(&managed, conversion)
|
||||
} else {
|
||||
CanvasImage::rgba8(
|
||||
pixels.srgb.width,
|
||||
pixels.srgb.height,
|
||||
pixels.srgb.rgba.clone(),
|
||||
)
|
||||
}?;
|
||||
Some(image.with_origin_clean(host.image_resource_origin_clean(handle)?))
|
||||
}
|
||||
Ok(None) => None,
|
||||
Err(_) => {
|
||||
throw_dom_exception_value(
|
||||
|
||||
@@ -25,6 +25,51 @@ impl CanvasImage {
|
||||
})
|
||||
}
|
||||
|
||||
pub(super) fn from_decoded_raster(decoded: moli_image::DecodedRasterImage) -> Option<Self> {
|
||||
if let Some(managed) = decoded.color_managed {
|
||||
Self::from_color_managed(&managed, moli_image::ColorProfileConversion::Apply)
|
||||
} else {
|
||||
Self::rgba8(
|
||||
decoded.image.width,
|
||||
decoded.image.height,
|
||||
decoded.image.rgba,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) fn from_color_managed(
|
||||
image: &moli_image::ColorManagedRasterImage,
|
||||
conversion: moli_image::ColorProfileConversion,
|
||||
) -> Option<Self> {
|
||||
if matches!(conversion, moli_image::ColorProfileConversion::Ignore)
|
||||
&& let Some(moli_image::UnconvertedRasterPixels::Unorm8(rgba)) = &image.unconverted
|
||||
{
|
||||
return Self::rgba8(image.width, image.height, rgba.clone());
|
||||
}
|
||||
let mut bytes = Vec::new();
|
||||
bytes
|
||||
.try_reserve_exact(moli_canvas::byte_len(image.width, image.height)?.checked_mul(2)?)
|
||||
.ok()?;
|
||||
if matches!(conversion, moli_image::ColorProfileConversion::Ignore)
|
||||
&& let Some(moli_image::UnconvertedRasterPixels::Unorm16(rgba)) = &image.unconverted
|
||||
{
|
||||
for value in rgba {
|
||||
bytes.extend_from_slice(
|
||||
&half::f16::from_f32(f32::from(*value) / 65535.0).to_ne_bytes(),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
for value in &image.rgba {
|
||||
bytes.extend_from_slice(&half::f16::from_f32(*value).to_ne_bytes());
|
||||
}
|
||||
}
|
||||
Some(Self {
|
||||
pixels: PixelBuffer::new(image.width, image.height, PixelFormat::Float16, bytes)?,
|
||||
color_space: ImageDataColorSpace::Srgb,
|
||||
origin_clean: true,
|
||||
})
|
||||
}
|
||||
|
||||
pub(in crate::context_bootstrap) fn from_view(
|
||||
view: v8::Local<'_, v8::ArrayBufferView>,
|
||||
width: u32,
|
||||
|
||||
@@ -331,9 +331,10 @@ mod tests {
|
||||
fn ready_red_pixel() -> ReadyDecodedImage {
|
||||
let budget = super::super::budget::SharedImageResourceBudget::default();
|
||||
ReadyDecodedImage {
|
||||
content: DecodedImageContent::Raster(Arc::new(
|
||||
moli_image::RgbaImage::try_new(1, 1, vec![255, 0, 0, 255]).unwrap(),
|
||||
)),
|
||||
content: DecodedImageContent::Raster(super::super::decode::RasterImagePixels {
|
||||
srgb: Arc::new(moli_image::RgbaImage::try_new(1, 1, vec![255, 0, 0, 255]).unwrap()),
|
||||
color_managed: None,
|
||||
}),
|
||||
decoded_bytes_permit: budget.reserve_decoded(4).unwrap(),
|
||||
}
|
||||
}
|
||||
@@ -345,6 +346,7 @@ mod tests {
|
||||
CssImageResourceStore::new(ReadyImageResourceIndex::default(), generation.clone());
|
||||
let descriptor = ImageResponseDescriptor::raster(moli_image::RasterImageMetadata {
|
||||
format: moli_image::RasterImageFormat::Png,
|
||||
color_precision: moli_image::RasterColorPrecision::Unorm8,
|
||||
width: 1,
|
||||
height: 1,
|
||||
});
|
||||
@@ -376,6 +378,7 @@ mod tests {
|
||||
let url = "https://example.test/shared.png";
|
||||
let descriptor = ImageResponseDescriptor::raster(moli_image::RasterImageMetadata {
|
||||
format: moli_image::RasterImageFormat::Png,
|
||||
color_precision: moli_image::RasterColorPrecision::Unorm8,
|
||||
width: 1,
|
||||
height: 1,
|
||||
});
|
||||
|
||||
@@ -55,10 +55,25 @@ pub(super) struct ReadyDecodedImage {
|
||||
}
|
||||
|
||||
pub(super) enum DecodedImageContent {
|
||||
Raster(Arc<moli_image::RgbaImage>),
|
||||
Raster(RasterImagePixels),
|
||||
Svg(Arc<moli_image::SvgImage>),
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub(crate) struct RasterImagePixels {
|
||||
pub(crate) srgb: Arc<moli_image::RgbaImage>,
|
||||
pub(crate) color_managed: Option<Arc<moli_image::ColorManagedRasterImage>>,
|
||||
}
|
||||
|
||||
impl From<moli_image::DecodedRasterImage> for RasterImagePixels {
|
||||
fn from(decoded: moli_image::DecodedRasterImage) -> Self {
|
||||
Self {
|
||||
srgb: Arc::new(decoded.image),
|
||||
color_managed: decoded.color_managed.map(Arc::new),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub(super) enum ImageDecodeResult {
|
||||
Ready(ReadyDecodedImage),
|
||||
Failed(String),
|
||||
@@ -281,7 +296,7 @@ fn decode_image(
|
||||
ImageDecodeMetadata::Raster(metadata) => {
|
||||
let decoded = moli_image::decode_raster_image_with_metadata(snapshot, metadata)
|
||||
.map_err(|error| error.to_string())?;
|
||||
DecodedImageContent::Raster(Arc::new(decoded.image))
|
||||
DecodedImageContent::Raster(decoded.into())
|
||||
}
|
||||
ImageDecodeMetadata::Svg(metadata) => {
|
||||
let decoded = moli_image::decode_svg_image_with_metadata(snapshot, metadata)
|
||||
|
||||
@@ -188,7 +188,7 @@ impl super::PageScriptHost {
|
||||
pub(crate) fn raster_image_for_canvas(
|
||||
&self,
|
||||
element: DomHandle,
|
||||
) -> Result<Option<std::sync::Arc<moli_image::RgbaImage>>, state::CanvasImageReadError> {
|
||||
) -> Result<Option<decode::RasterImagePixels>, state::CanvasImageReadError> {
|
||||
let selected = crate::native_bridge::element::image_selected_request_key(self, element);
|
||||
if self
|
||||
.image_resources
|
||||
|
||||
@@ -10,7 +10,7 @@ use super::{
|
||||
ImageResourceRequestIdentity, ImageResponseDescriptor,
|
||||
budget::ImageDecodedBytesPermit,
|
||||
css::CssImageResourceStore,
|
||||
decode::{DecodedImageContent, ImageDecodeCoordinator, ReadyDecodedImage},
|
||||
decode::{DecodedImageContent, ImageDecodeCoordinator, RasterImagePixels, ReadyDecodedImage},
|
||||
preload::{ScannedImagePreloadStore, SharedScannedImagePreloadLoad},
|
||||
};
|
||||
use crate::document_runtime::DomHandle;
|
||||
@@ -60,7 +60,7 @@ enum ImageResourceState {
|
||||
pub(super) struct ReadyImageResource {
|
||||
pub(super) descriptor: ImageResponseDescriptor,
|
||||
pub(super) density: f64,
|
||||
pub(super) pixels: Option<Arc<moli_image::RgbaImage>>,
|
||||
pub(super) pixels: Option<RasterImagePixels>,
|
||||
pub(super) svg: Option<Arc<moli_image::SvgImage>>,
|
||||
pub(super) canvas_source: Option<CanvasImageSource>,
|
||||
pub(super) _decoded_bytes_permit: Option<ImageDecodedBytesPermit>,
|
||||
@@ -78,6 +78,10 @@ pub(crate) struct CanvasImageReadError;
|
||||
|
||||
impl ReadyImageResource {
|
||||
pub(super) fn raster_pixels(&self) -> Option<Arc<moli_image::RgbaImage>> {
|
||||
self.raster_source().map(|source| source.srgb)
|
||||
}
|
||||
|
||||
fn raster_source(&self) -> Option<RasterImagePixels> {
|
||||
self.pixels.clone().or_else(|| {
|
||||
let ready = self.canvas_source.as_ref()?.decoded.get()?;
|
||||
match &ready.content {
|
||||
@@ -399,7 +403,7 @@ impl ImageResourceStore {
|
||||
pub(super) fn raster_for_canvas(
|
||||
&self,
|
||||
element: DomHandle,
|
||||
) -> Result<Option<Arc<moli_image::RgbaImage>>, CanvasImageReadError> {
|
||||
) -> Result<Option<RasterImagePixels>, CanvasImageReadError> {
|
||||
let Some(slot) = self.slots.get(&element) else {
|
||||
return Ok(None);
|
||||
};
|
||||
@@ -409,7 +413,7 @@ impl ImageResourceStore {
|
||||
let Some(resource) = slot.current_ready() else {
|
||||
return Ok(None);
|
||||
};
|
||||
if let Some(pixels) = resource.raster_pixels() {
|
||||
if let Some(pixels) = resource.raster_source() {
|
||||
return Ok(Some(pixels));
|
||||
}
|
||||
let Some(source) = resource.canvas_source.as_ref() else {
|
||||
@@ -424,7 +428,7 @@ impl ImageResourceStore {
|
||||
})?;
|
||||
let _ = source.decoded.set(decoded);
|
||||
self.visual_generation.bump();
|
||||
Ok(resource.raster_pixels())
|
||||
Ok(resource.raster_source())
|
||||
}
|
||||
|
||||
pub(super) fn is_ready(&self, element: DomHandle) -> bool {
|
||||
@@ -577,6 +581,7 @@ mod tests {
|
||||
.raster_for_canvas(same_document.element)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.srgb
|
||||
.rgba,
|
||||
[255, 0, 0, 255]
|
||||
);
|
||||
@@ -601,6 +606,7 @@ mod tests {
|
||||
.raster_for_canvas(other_document.element)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.srgb
|
||||
.rgba,
|
||||
[0, 255, 0, 255]
|
||||
);
|
||||
@@ -610,11 +616,64 @@ mod tests {
|
||||
.raster_for_canvas(first.element)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.srgb
|
||||
.rgba,
|
||||
[255, 0, 0, 255]
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn profiled_image_budget_admission_cache_and_retirement_cover_all_color_planes() {
|
||||
let bytes = include_bytes!(concat!(
|
||||
env!("CARGO_MANIFEST_DIR"),
|
||||
"/../moli-image/tests/data/color_profiles/Display-P3-FF0000FF.png"
|
||||
));
|
||||
let metadata = moli_image::probe_raster_image(bytes).unwrap();
|
||||
let retained = metadata.decoded_byte_len();
|
||||
let mut store = ImageResourceStore::default();
|
||||
let budget = store.decode.budget_for_test();
|
||||
let identity = image_identity(20, 1, "https://images.test/profile.png");
|
||||
let manager = moli_parkable_image::ParkableImageManager::new(
|
||||
None,
|
||||
moli_parkable_image::ParkableImagePolicy::default(),
|
||||
);
|
||||
store.begin(identity.clone(), false);
|
||||
assert!(store.complete_metadata(
|
||||
&identity,
|
||||
ImageResponseDescriptor::raster(metadata),
|
||||
manager.from_frozen_bytes(bytes.to_vec())
|
||||
));
|
||||
let blocked = budget
|
||||
.reserve_decoded(super::super::budget::MAX_RETAINED_IMAGE_DECODED_BYTES - retained + 1)
|
||||
.unwrap();
|
||||
assert!(store.raster_for_canvas(identity.element).is_err());
|
||||
drop(blocked);
|
||||
assert_eq!(budget.counters(), (0, 0, 0));
|
||||
|
||||
let first = store.raster_for_canvas(identity.element).unwrap().unwrap();
|
||||
let again = store.raster_for_canvas(identity.element).unwrap().unwrap();
|
||||
assert!(Arc::ptr_eq(&first.srgb, &again.srgb));
|
||||
assert!(Arc::ptr_eq(
|
||||
first.color_managed.as_ref().unwrap(),
|
||||
again.color_managed.as_ref().unwrap()
|
||||
));
|
||||
assert_eq!(budget.counters(), (0, 0, retained));
|
||||
let same_document = image_identity(21, 2, identity.request_key.url());
|
||||
store.begin(same_document.clone(), false);
|
||||
let shared = store
|
||||
.raster_for_canvas(same_document.element)
|
||||
.unwrap()
|
||||
.unwrap();
|
||||
assert!(Arc::ptr_eq(
|
||||
first.color_managed.as_ref().unwrap(),
|
||||
shared.color_managed.as_ref().unwrap()
|
||||
));
|
||||
assert_eq!(budget.counters(), (0, 0, retained));
|
||||
drop((first, again, shared));
|
||||
assert_eq!(store.retire_document(identity.document_handle), 2);
|
||||
assert_eq!(budget.counters(), (0, 0, 0));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn canvas_demand_shares_one_decode_and_releases_its_owner_budget() {
|
||||
let mut store = ImageResourceStore::default();
|
||||
@@ -633,11 +692,11 @@ mod tests {
|
||||
assert_eq!(budget.counters(), (0, 0, 0));
|
||||
let before = store.visual_generation();
|
||||
let first = store.raster_for_canvas(identity.element).unwrap().unwrap();
|
||||
assert_eq!(first.rgba, [0, 255, 0, 255]);
|
||||
assert_eq!(first.srgb.rgba, [0, 255, 0, 255]);
|
||||
assert_eq!(budget.counters(), (0, 0, 4));
|
||||
assert_eq!(store.visual_generation(), before + 1);
|
||||
let again = store.raster_for_canvas(identity.element).unwrap().unwrap();
|
||||
assert!(Arc::ptr_eq(&first, &again));
|
||||
assert!(Arc::ptr_eq(&first.srgb, &again.srgb));
|
||||
assert_eq!(store.visual_generation(), before + 1);
|
||||
assert!(
|
||||
store
|
||||
@@ -649,7 +708,7 @@ mod tests {
|
||||
let other = image_identity(11, 2, identity.request_key.url());
|
||||
store.begin(other.clone(), false);
|
||||
let shared = store.raster_for_canvas(other.element).unwrap().unwrap();
|
||||
assert!(Arc::ptr_eq(&first, &shared));
|
||||
assert!(Arc::ptr_eq(&first.srgb, &shared.srgb));
|
||||
assert_eq!(budget.counters(), (0, 0, 4));
|
||||
drop((first, again, shared));
|
||||
assert!(store.retire_element(identity.element));
|
||||
@@ -681,6 +740,7 @@ mod tests {
|
||||
.raster_for_canvas(first.element)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.srgb
|
||||
.rgba,
|
||||
[0, 255, 0, 255]
|
||||
);
|
||||
@@ -694,6 +754,7 @@ mod tests {
|
||||
.raster_for_canvas(first.element)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.srgb
|
||||
.rgba,
|
||||
[0, 255, 0, 255]
|
||||
);
|
||||
@@ -706,6 +767,7 @@ mod tests {
|
||||
.raster_for_canvas(first.element)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.srgb
|
||||
.rgba,
|
||||
[0, 0, 255, 255]
|
||||
);
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
async function probe(bytes) {
|
||||
const rows = [];
|
||||
const blob = new Blob([new Uint8Array(bytes)], {type: 'image/png'});
|
||||
const image = new Image();
|
||||
const ready = new Promise((resolve, reject) => { image.onload = resolve; image.onerror = reject; });
|
||||
image.src = 'data:image/png;base64,' + btoa(String.fromCharCode(...bytes));
|
||||
await ready;
|
||||
const seed = await createImageBitmap(image);
|
||||
for (const [sourceName, source] of [['image', image], ['blob', blob], ['bitmap', seed]]) {
|
||||
for (const conversion of ['default', 'none']) {
|
||||
const bitmap = await createImageBitmap(source, {colorSpaceConversion: conversion});
|
||||
for (const kind of ['element', 'offscreen']) {
|
||||
for (const colorSpace of ['srgb', 'display-p3']) {
|
||||
for (const paint of ['drawImage', 'pattern']) {
|
||||
const canvas = kind === 'element' ? document.createElement('canvas') : new OffscreenCanvas(2, 2);
|
||||
canvas.width = canvas.height = 2;
|
||||
const context = canvas.getContext('2d', {colorSpace});
|
||||
context.imageSmoothingEnabled = false;
|
||||
if (paint === 'drawImage') context.drawImage(bitmap, 0, 0);
|
||||
else { context.fillStyle = context.createPattern(bitmap, 'repeat'); context.fillRect(0, 0, 2, 2); }
|
||||
const converted = conversion === 'default' || sourceName === 'bitmap';
|
||||
const expected = converted ? (colorSpace === 'srgb' ? [205, 0, 0, 204] : [187, 0, 0, 204])
|
||||
: (colorSpace === 'srgb' ? [187, 0, 0, 204] : [171, 35, 23, 204]);
|
||||
const actual = Array.from(context.getImageData(0, 0, 1, 1, {colorSpace}).data);
|
||||
rows.push({name: [sourceName, conversion, kind, colorSpace, paint].join('/'), expected, actual,
|
||||
passed: actual.every((value, i) => Math.abs(value - expected[i]) <= 2)});
|
||||
}
|
||||
}
|
||||
}
|
||||
bitmap.close();
|
||||
}
|
||||
}
|
||||
seed.close();
|
||||
return rows;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
use super::*;
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
#[tokio::test(flavor = "current_thread")]
|
||||
async fn canvas_image_profiles_preserve_gamut_and_bitmap_color_conversion_options() {
|
||||
let bytes = include_bytes!(concat!(
|
||||
env!("CARGO_MANIFEST_DIR"),
|
||||
"/../moli-image/tests/data/color_profiles/Display-P3-BB0000CC.png"
|
||||
));
|
||||
let loader = ResourceRequestClient::new(&moli_fetch::FetchConfig::default()).unwrap();
|
||||
let mut vm = new_storage_page_task_executor_test_vm("https://canvas-image-color.test/");
|
||||
vm.eval(&format!("const probe={}; probe({bytes:?}).then(rows => globalThis.__imageColors=rows, error => globalThis.__imageColors=String(error));",include_str!("canvas_image_color.js"))).unwrap();
|
||||
let deadline = Instant::now() + Duration::from_secs(15);
|
||||
while vm.eval("typeof __imageColors").unwrap() == "undefined" {
|
||||
vm.run_one_oldest_ready_page_task_executor_turn(&loader)
|
||||
.await
|
||||
.unwrap();
|
||||
assert!(
|
||||
Instant::now() < deadline,
|
||||
"image color conversion timed out"
|
||||
);
|
||||
tokio::time::sleep(Duration::from_millis(1)).await;
|
||||
}
|
||||
assert_eq!(
|
||||
vm.eval("Array.isArray(__imageColors)").unwrap(),
|
||||
"true",
|
||||
"{}",
|
||||
vm.eval("JSON.stringify(__imageColors)").unwrap()
|
||||
);
|
||||
assert_eq!(vm.eval("__imageColors.length").unwrap(), "48");
|
||||
assert_eq!(
|
||||
vm.eval("JSON.stringify(__imageColors.filter(row=>!row.passed))")
|
||||
.unwrap(),
|
||||
"[]"
|
||||
);
|
||||
}
|
||||
@@ -2310,6 +2310,7 @@ mod canvas_drawing_state;
|
||||
mod canvas_fill_rect;
|
||||
mod canvas_gradients;
|
||||
mod canvas_hdr_pixels;
|
||||
mod canvas_image_color;
|
||||
mod canvas_image_proxies;
|
||||
mod canvas_paths;
|
||||
mod canvas_transfer_to_image_bitmap;
|
||||
|
||||
Reference in New Issue
Block a user