fix(media-proxy): convert embedded icc profiles to srgb (#4193)

This commit is contained in:
Hampus authored and GitHub committed 2026-11-15 22:46:20 +01:00
1 parent e9c375cbb2
commit cce6b40271
7 files changed
+155 -2

No files matched your search

+59 -2
View File
@@ -8,9 +8,12 @@ use crate::{
metrics::transform::TransformMetrics,
native::{self, NativeStatus},
output_format::OutputFormat,
test_fixtures::{synthetic_png, synthetic_png16},
test_fixtures::{
red_jpeg_tagged_with_swapped_red_and_blue_primaries, synthetic_png, synthetic_png16,
},
};
use std::ffi::CString;
use libc::{c_void, size_t};
use std::{ffi::CString, ptr};
fn transformed(input: &[u8], options: ImageOptions) -> Vec<u8> {
transform_image(
@@ -53,6 +56,60 @@ fn sixteen_bit_png_cover_resize_encodes_eight_bit_png() {
assert_eq!(8, png[24]);
}
fn centre_rgba(encoded: &[u8]) -> [u8; 4] {
let options = CString::default();
let raw = unsafe {
native::fluxer_vips_image_new_from_buffer(
encoded.as_ptr().cast(),
encoded.len(),
options.as_ptr(),
)
};
let image = unsafe { native::VipsImageHandle::from_raw_borrowing(raw, encoded) }
.expect("the output decodes");
let width = unsafe { native::fluxer_vips_image_get_width(image.as_ptr()) } as usize;
let height = unsafe { native::fluxer_vips_image_get_height(image.as_ptr()) } as usize;
let mut out_ptr: *mut c_void = ptr::null_mut();
let mut out_size: size_t = 0;
let rc =
unsafe { native::fluxer_vips_extract_rgba(image.as_ptr(), 0, &mut out_ptr, &mut out_size) };
assert_eq!(0, rc);
let rgba = unsafe { native::buffer::NativeBuffer::from_vips_owned(out_ptr, out_size) }
.unwrap()
.try_to_vec()
.unwrap();
let centre = ((height / 2) * width + width / 2) * 4;
rgba[centre..centre + 4].try_into().unwrap()
}
#[test]
fn embedded_icc_profile_is_converted_to_srgb_in_every_transform_path() {
let input = red_jpeg_tagged_with_swapped_red_and_blue_primaries(64, 48);
for (width, height, resize_mode) in [
(None, None, ResizeMode::Fit),
(Some(32), None, ResizeMode::Fit),
(Some(32), Some(32), ResizeMode::Cover),
] {
for format in [OutputFormat::JPEG, OutputFormat::PNG, OutputFormat::WebP] {
let [red, _, blue, _] = centre_rgba(&transformed(
&input,
ImageOptions {
width,
height,
format,
quality: ImageQuality::High,
resize_mode,
..Default::default()
},
));
assert!(
blue > 200 && red < 80,
"{format:?} {width:?}x{height:?} {resize_mode:?} renders the tagged red as blue, got red {red} blue {blue}"
);
}
}
}
#[test]
fn blocked_libvips_loaders_cannot_decode_untrusted_input() {
ensure_vips_init().expect("libvips initialises");
@@ -178,11 +178,32 @@ pub(crate) fn validate_vips_image(
Ok(())
}
fn embedded_icc_to_srgb<'source>(
image: native::VipsImageHandle<'source>,
) -> Result<native::VipsImageHandle<'source>, MediaError> {
let mut converted_raw = ptr::null_mut();
let rc =
unsafe { native::fluxer_vips_embedded_icc_to_srgb(image.as_ptr(), &mut converted_raw) };
let converted = unsafe { image.adopt_derived_raw(converted_raw) };
if let Some(error) = native_status_error(
native::NativeStatus::from_code(rc),
MediaError::MediaTransformFailed,
) {
clear_vips_error();
return Err(error);
}
converted.ok_or_else(|| {
clear_vips_error();
MediaError::MediaTransformFailed
})
}
pub(crate) fn normalize_vips_image_to_uchar<'source>(
media_limits: &MediaLimits,
image: native::VipsImageHandle<'source>,
) -> Result<native::VipsImageHandle<'source>, MediaError> {
validate_vips_image(media_limits, &image)?;
let image = embedded_icc_to_srgb(image)?;
let format = unsafe { native::fluxer_vips_image_get_format(image.as_ptr()) };
if format == unsafe { native::fluxer_vips_format_uchar } {
return Ok(image);
@@ -73,3 +73,25 @@ pub(super) fn optional_thumbhash(
pub(super) fn encoded_placeholder(hash: Option<Vec<u8>>) -> Option<String> {
hash.map(|hash| general_purpose::STANDARD.encode(hash))
}
#[cfg(test)]
mod tests {
use super::encode_thumbhash;
use crate::{
media_limits::MediaLimits,
test_fixtures::red_jpeg_tagged_with_swapped_red_and_blue_primaries,
};
#[test]
fn thumbhash_reads_colours_through_the_embedded_icc_profile() {
let input = red_jpeg_tagged_with_swapped_red_and_blue_primaries(64, 48);
let hash = encode_thumbhash(&MediaLimits::default_from_config(), &input, None)
.expect("the thumbhash encodes");
let header = u32::from(hash[0]) | (u32::from(hash[1]) << 8) | (u32::from(hash[2]) << 16);
let yellow_minus_blue = (header >> 6) & 63;
assert!(
yellow_minus_blue < 8,
"the average colour is blue, got p {yellow_minus_blue}"
);
}
}
+4
View File
@@ -211,6 +211,10 @@ unsafe extern "C" {
) -> c_int;
pub fn fluxer_vips_srgb_without_alpha(input: *mut VipsImage, out: *mut *mut VipsImage)
-> c_int;
pub fn fluxer_vips_embedded_icc_to_srgb(
input: *mut VipsImage,
out: *mut *mut VipsImage,
) -> c_int;
pub fn fluxer_vips_extract_area(
input: *mut VipsImage,
out: *mut *mut VipsImage,
@@ -246,6 +246,26 @@ int fluxer_vips_srgb_without_alpha(VipsImage *in, VipsImage **out) {
return fluxer_vips_operation_status(rc, out);
}
int fluxer_vips_embedded_icc_to_srgb(VipsImage *in, VipsImage **out) {
if (out == NULL) return FLUXER_NATIVE_STATUS_CODEC_FAILURE;
*out = NULL;
if (in == NULL) return FLUXER_NATIVE_STATUS_CODEC_FAILURE;
VipsBandFormat format = vips_image_get_format(in);
if (vips_image_get_typeof(in, VIPS_META_ICC_NAME) == 0 ||
(format != VIPS_FORMAT_UCHAR && format != VIPS_FORMAT_USHORT)) {
g_object_ref(in);
*out = in;
return FLUXER_NATIVE_STATUS_OK;
}
int rc = vips_icc_transform(
in, out, "srgb",
"embedded", TRUE,
"intent", VIPS_INTENT_RELATIVE,
"depth", 8,
NULL);
return fluxer_vips_operation_status(rc, out);
}
int fluxer_vips_extract_area(VipsImage *in, VipsImage **out, int left, int top, int width, int height) {
if (out == NULL) return FLUXER_NATIVE_STATUS_CODEC_FAILURE;
*out = NULL;
+28
View File
@@ -52,6 +52,34 @@ pub fn synthetic_png16(width: u32, height: u32) -> Vec<u8> {
)
}
pub fn red_jpeg_tagged_with_swapped_red_and_blue_primaries(width: u32, height: u32) -> Vec<u8> {
let pixels = [255, 0, 0, 255].repeat(width as usize * height as usize);
let mut jpeg = encode_rgba(
&pixels,
width,
height,
unsafe { native::fluxer_vips_format_uchar },
".jpg[Q=100,profile=srgb]",
);
let marker = b"ICC_PROFILE\0";
let profile = jpeg
.windows(marker.len())
.position(|window| window == marker)
.expect("the jpeg embeds an icc profile")
+ marker.len()
+ 2;
let tag_count = u32::from_be_bytes(jpeg[profile + 128..profile + 132].try_into().unwrap());
for tag in 0..tag_count as usize {
let signature = profile + 132 + tag * 12;
match &jpeg[signature..signature + 4] {
b"rXYZ" => jpeg[signature..signature + 4].copy_from_slice(b"bXYZ"),
b"bXYZ" => jpeg[signature..signature + 4].copy_from_slice(b"rXYZ"),
_ => {}
}
}
jpeg
}
pub fn synthetic_image(width: u32, height: u32, suffix: &str) -> Vec<u8> {
let pixels = synthetic_rgba(width, height);
encode_rgba(
+1
View File
@@ -58,6 +58,7 @@ int fluxer_vips_read_delays_ms(VipsImage *image, int n_pages, int **out_delays,
int fluxer_vips_autorot(
VipsImage *in, long long deadline_monotonic_ms, VipsImage **out);
int fluxer_vips_srgb_without_alpha(VipsImage *in, VipsImage **out);
int fluxer_vips_embedded_icc_to_srgb(VipsImage *in, VipsImage **out);
int fluxer_vips_extract_area(VipsImage *in, VipsImage **out, int left, int top, int width, int height);
int fluxer_vips_resize(VipsImage *in, VipsImage **out, double scale);
int fluxer_vips_join_animation_pages(