1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 00:17:46 +00:00

LibGfx/ICC: Cache inverted matrix on profile

Reduces time spent rendering page 3 of 0000849.pdf from 1.85s to 1.45s
on my machine.

As determined by running

    time Build/lagom/bin/pdf --page 3 --render out.png \
        ~/Downloads/0000/0000849.pdf

a few times and eyeballing the min time.

Also reduces the time to run Meta/test_pdf.py on 0000.zip
(without 0000849.pdf) from 1m7s to 58s.
This commit is contained in:
Nico Weber 2024-01-08 18:29:47 -05:00 committed by Sam Atkins
parent 19d434b229
commit 3365a92ead
2 changed files with 10 additions and 5 deletions

View file

@ -1505,7 +1505,7 @@ ErrorOr<void> Profile::from_pcs(Profile const& source_profile, FloatVector3 pcs,
// inverted."
// Convert from XYZ to linear rgb.
// FIXME: Inverting matrix and curve on every call to this function is very inefficient.
// FIXME: Inverting curves on every call to this function is very inefficient.
FloatVector3 linear_rgb = TRY(xyz_to_rgb_matrix()) * pcs;
auto evaluate_curve_inverse = [this](TagSignature curve_tag, float f) {
@ -1619,10 +1619,13 @@ Optional<String> Profile::tag_string_data(TagSignature signature) const
ErrorOr<FloatMatrix3x3> Profile::xyz_to_rgb_matrix() const
{
FloatMatrix3x3 forward_matrix = rgb_to_xyz_matrix();
if (!forward_matrix.is_invertible())
return Error::from_string_literal("ICC::Profile::from_pcs: matrix not invertible");
return forward_matrix.inverse();
if (!m_cached_xyz_to_rgb_matrix.has_value()) {
FloatMatrix3x3 forward_matrix = rgb_to_xyz_matrix();
if (!forward_matrix.is_invertible())
return Error::from_string_literal("ICC::Profile::from_pcs: matrix not invertible");
m_cached_xyz_to_rgb_matrix = forward_matrix.inverse();
}
return m_cached_xyz_to_rgb_matrix.value();
}
FloatMatrix3x3 Profile::rgb_to_xyz_matrix() const

View file

@ -252,6 +252,8 @@ private:
// Only valid for RGB matrix-based profiles.
ErrorOr<FloatMatrix3x3> xyz_to_rgb_matrix() const;
FloatMatrix3x3 rgb_to_xyz_matrix() const;
mutable Optional<FloatMatrix3x3> m_cached_xyz_to_rgb_matrix;
};
}