diff --git a/Userland/Libraries/LibPDF/ColorSpace.cpp b/Userland/Libraries/LibPDF/ColorSpace.cpp index fb71d60c3e..7e89ff3c09 100644 --- a/Userland/Libraries/LibPDF/ColorSpace.cpp +++ b/Userland/Libraries/LibPDF/ColorSpace.cpp @@ -102,10 +102,10 @@ NonnullRefPtr DeviceGrayColorSpace::the() return instance; } -PDFErrorOr DeviceGrayColorSpace::style(ReadonlySpan arguments) const +PDFErrorOr DeviceGrayColorSpace::style(ReadonlySpan arguments) const { VERIFY(arguments.size() == 1); - auto gray = static_cast(arguments[0].to_float() * 255.0f); + auto gray = static_cast(arguments[0] * 255.0f); return Color(gray, gray, gray); } @@ -120,12 +120,12 @@ NonnullRefPtr DeviceRGBColorSpace::the() return instance; } -PDFErrorOr DeviceRGBColorSpace::style(ReadonlySpan arguments) const +PDFErrorOr DeviceRGBColorSpace::style(ReadonlySpan arguments) const { VERIFY(arguments.size() == 3); - auto r = static_cast(arguments[0].to_float() * 255.0f); - auto g = static_cast(arguments[1].to_float() * 255.0f); - auto b = static_cast(arguments[2].to_float() * 255.0f); + auto r = static_cast(arguments[0] * 255.0f); + auto g = static_cast(arguments[1] * 255.0f); + auto b = static_cast(arguments[2] * 255.0f); return Color(r, g, b); } @@ -140,13 +140,13 @@ NonnullRefPtr DeviceCMYKColorSpace::the() return instance; } -PDFErrorOr DeviceCMYKColorSpace::style(ReadonlySpan arguments) const +PDFErrorOr DeviceCMYKColorSpace::style(ReadonlySpan arguments) const { VERIFY(arguments.size() == 4); - auto c = arguments[0].to_float(); - auto m = arguments[1].to_float(); - auto y = arguments[2].to_float(); - auto k = arguments[3].to_float(); + auto c = arguments[0]; + auto m = arguments[1]; + auto y = arguments[2]; + auto k = arguments[3]; return Color::from_cmyk(c, m, y, k); } @@ -198,15 +198,11 @@ DeviceNColorSpace::DeviceNColorSpace(NonnullRefPtr alternate_space, { } -PDFErrorOr DeviceNColorSpace::style(ReadonlySpan arguments) const +PDFErrorOr DeviceNColorSpace::style(ReadonlySpan arguments) const { // FIXME: Does this need handling for the special colorant name "None"? // FIXME: When drawing to a printer, do something else. - m_tint_input_values.resize(arguments.size()); - for (size_t i = 0; i < arguments.size(); ++i) - m_tint_input_values[i] = arguments[i].to_float(); - - auto tint_output = TRY(m_tint_transform->evaluate(m_tint_input_values.span())); + auto tint_output = TRY(m_tint_transform->evaluate(arguments)); m_tint_output_values.resize(tint_output.size()); for (size_t i = 0; i < tint_output.size(); ++i) @@ -351,10 +347,10 @@ PDFErrorOr> CalGrayColorSpace::create(Document* return color_space; } -PDFErrorOr CalGrayColorSpace::style(ReadonlySpan arguments) const +PDFErrorOr CalGrayColorSpace::style(ReadonlySpan arguments) const { VERIFY(arguments.size() == 1); - auto a = clamp(arguments[0].to_float(), 0.0f, 1.0f); + auto a = clamp(arguments[0], 0.0f, 1.0f); auto ag = powf(a, m_gamma); @@ -437,12 +433,12 @@ PDFErrorOr> CalRGBColorSpace::create(Document* d return color_space; } -PDFErrorOr CalRGBColorSpace::style(ReadonlySpan arguments) const +PDFErrorOr CalRGBColorSpace::style(ReadonlySpan arguments) const { VERIFY(arguments.size() == 3); - auto a = clamp(arguments[0].to_float(), 0.0f, 1.0f); - auto b = clamp(arguments[1].to_float(), 0.0f, 1.0f); - auto c = clamp(arguments[2].to_float(), 0.0f, 1.0f); + auto a = clamp(arguments[0], 0.0f, 1.0f); + auto b = clamp(arguments[1], 0.0f, 1.0f); + auto c = clamp(arguments[2], 0.0f, 1.0f); auto agr = powf(a, m_gamma[0]); auto bgg = powf(b, m_gamma[1]); @@ -498,32 +494,31 @@ ICCBasedColorSpace::ICCBasedColorSpace(NonnullRefPtr profile) m_map = sRGB()->matrix_matrix_conversion(profile); } -PDFErrorOr ICCBasedColorSpace::style(ReadonlySpan arguments) const +PDFErrorOr ICCBasedColorSpace::style(ReadonlySpan arguments) const { - m_components.resize(arguments.size()); - for (size_t i = 0; i < arguments.size(); ++i) { - auto const& arg = arguments[i]; - VERIFY(arg.has_number()); - float number = arg.to_float(); + if (m_profile->data_color_space() == Gfx::ICC::ColorSpace::CIELAB) { + m_components.resize(arguments.size()); + for (size_t i = 0; i < arguments.size(); ++i) { + float number = arguments[i]; - if (m_profile->data_color_space() == Gfx::ICC::ColorSpace::CIELAB) { // CIELAB channels go from 0..100 and -128..127 instead of from 0..1. // FIXME: We should probably have an API on Gfx::ICC::Profile that takes floats instead of bytes and that does this internally instead. if (i == 0) number /= 100.0f; else number = (number + 128.0f) / 255.0f; - } - m_components[i] = number; + m_components[i] = number; + } + arguments = m_components; } if (m_map.has_value()) - return m_map->map(FloatVector3 { m_components[0], m_components[1], m_components[2] }); + return m_map->map(FloatVector3 { arguments[0], arguments[1], arguments[2] }); m_bytes.resize(arguments.size()); for (size_t i = 0; i < arguments.size(); ++i) - m_bytes[i] = static_cast(m_components[i] * 255.0f); + m_bytes[i] = static_cast(arguments[i] * 255.0f); auto pcs = TRY(m_profile->to_pcs(m_bytes)); Array output; @@ -609,12 +604,12 @@ PDFErrorOr> LabColorSpace::create(Document* documen return color_space; } -PDFErrorOr LabColorSpace::style(ReadonlySpan arguments) const +PDFErrorOr LabColorSpace::style(ReadonlySpan arguments) const { VERIFY(arguments.size() == 3); - auto L_star = clamp(arguments[0].to_float(), 0.0f, 100.0f); - auto a_star = clamp(arguments[1].to_float(), m_range[0], m_range[1]); - auto b_star = clamp(arguments[2].to_float(), m_range[2], m_range[3]); + auto L_star = clamp(arguments[0], 0.0f, 100.0f); + auto a_star = clamp(arguments[1], m_range[0], m_range[1]); + auto b_star = clamp(arguments[2], m_range[2], m_range[3]); auto L = (L_star + 16) / 116 + a_star / 500; auto M = (L_star + 16) / 116; @@ -708,11 +703,11 @@ IndexedColorSpace::IndexedColorSpace(NonnullRefPtr base) { } -PDFErrorOr IndexedColorSpace::style(ReadonlySpan arguments) const +PDFErrorOr IndexedColorSpace::style(ReadonlySpan arguments) const { VERIFY(arguments.size() == 1); - auto index = arguments[0].to_int(); + auto index = static_cast(arguments[0]); if (index < 0 || index > m_hival) return Error { Error::Type::MalformedPDF, "Indexed color space index out of range" }; @@ -764,7 +759,7 @@ SeparationColorSpace::SeparationColorSpace(NonnullRefPtr alternate_s { } -PDFErrorOr SeparationColorSpace::style(ReadonlySpan arguments) const +PDFErrorOr SeparationColorSpace::style(ReadonlySpan arguments) const { // "For an additive device such as a computer display, a Separation color space never applies a process colorant directly; // it always reverts to the alternate color space as described below." @@ -773,7 +768,7 @@ PDFErrorOr SeparationColorSpace::style(ReadonlySpan argumen // FIXME: Does this need handling for the special colorant names "All" and "None"? // FIXME: When drawing to a printer, do something else. VERIFY(arguments.size() == 1); - auto a = arguments[0].to_float(); + auto a = arguments[0]; auto tint_output = TRY(m_tint_transform->evaluate(ReadonlySpan { &a, 1 })); diff --git a/Userland/Libraries/LibPDF/ColorSpace.h b/Userland/Libraries/LibPDF/ColorSpace.h index fb592558d1..c3f9329d53 100644 --- a/Userland/Libraries/LibPDF/ColorSpace.h +++ b/Userland/Libraries/LibPDF/ColorSpace.h @@ -66,7 +66,15 @@ public: virtual ~ColorSpace() = default; - virtual PDFErrorOr style(ReadonlySpan arguments) const = 0; + virtual PDFErrorOr style(ReadonlySpan arguments) const = 0; + virtual PDFErrorOr style(ReadonlySpan arguments) const + { + Vector float_arguments; + for (auto& argument : arguments) + float_arguments.append(argument.to_float()); + return style(float_arguments); + } + virtual int number_of_components() const = 0; virtual Vector default_decode() const = 0; // "TABLE 4.40 Default Decode arrays" virtual ColorSpaceFamily const& family() const = 0; @@ -78,7 +86,7 @@ public: ~DeviceGrayColorSpace() override = default; - PDFErrorOr style(ReadonlySpan arguments) const override; + PDFErrorOr style(ReadonlySpan arguments) const override; int number_of_components() const override { return 1; } Vector default_decode() const override; ColorSpaceFamily const& family() const override { return ColorSpaceFamily::DeviceGray; } @@ -93,7 +101,7 @@ public: ~DeviceRGBColorSpace() override = default; - PDFErrorOr style(ReadonlySpan arguments) const override; + PDFErrorOr style(ReadonlySpan arguments) const override; int number_of_components() const override { return 3; } Vector default_decode() const override; ColorSpaceFamily const& family() const override { return ColorSpaceFamily::DeviceRGB; } @@ -108,7 +116,7 @@ public: ~DeviceCMYKColorSpace() override = default; - PDFErrorOr style(ReadonlySpan arguments) const override; + PDFErrorOr style(ReadonlySpan arguments) const override; int number_of_components() const override { return 4; } Vector default_decode() const override; ColorSpaceFamily const& family() const override { return ColorSpaceFamily::DeviceCMYK; } @@ -123,7 +131,7 @@ public: ~DeviceNColorSpace() override = default; - PDFErrorOr style(ReadonlySpan arguments) const override; + PDFErrorOr style(ReadonlySpan arguments) const override; int number_of_components() const override; Vector default_decode() const override; ColorSpaceFamily const& family() const override { return ColorSpaceFamily::DeviceN; } @@ -134,7 +142,6 @@ private: Vector m_names; NonnullRefPtr m_alternate_space; NonnullRefPtr m_tint_transform; - Vector mutable m_tint_input_values; Vector mutable m_tint_output_values; }; @@ -144,7 +151,7 @@ public: ~CalGrayColorSpace() override = default; - PDFErrorOr style(ReadonlySpan arguments) const override; + PDFErrorOr style(ReadonlySpan arguments) const override; int number_of_components() const override { return 1; } Vector default_decode() const override; ColorSpaceFamily const& family() const override { return ColorSpaceFamily::CalGray; } @@ -163,7 +170,7 @@ public: ~CalRGBColorSpace() override = default; - PDFErrorOr style(ReadonlySpan arguments) const override; + PDFErrorOr style(ReadonlySpan arguments) const override; int number_of_components() const override { return 3; } Vector default_decode() const override; ColorSpaceFamily const& family() const override { return ColorSpaceFamily::CalRGB; } @@ -183,7 +190,7 @@ public: ~ICCBasedColorSpace() override = default; - PDFErrorOr style(ReadonlySpan arguments) const override; + PDFErrorOr style(ReadonlySpan arguments) const override; int number_of_components() const override; Vector default_decode() const override; ColorSpaceFamily const& family() const override { return ColorSpaceFamily::ICCBased; } @@ -206,7 +213,7 @@ public: ~LabColorSpace() override = default; - PDFErrorOr style(ReadonlySpan arguments) const override; + PDFErrorOr style(ReadonlySpan arguments) const override; int number_of_components() const override { return 3; } Vector default_decode() const override; ColorSpaceFamily const& family() const override { return ColorSpaceFamily::Lab; } @@ -225,7 +232,7 @@ public: ~IndexedColorSpace() override = default; - PDFErrorOr style(ReadonlySpan arguments) const override; + PDFErrorOr style(ReadonlySpan arguments) const override; int number_of_components() const override { return 1; } Vector default_decode() const override; ColorSpaceFamily const& family() const override { return ColorSpaceFamily::Indexed; } @@ -244,7 +251,7 @@ public: ~SeparationColorSpace() override = default; - PDFErrorOr style(ReadonlySpan arguments) const override; + PDFErrorOr style(ReadonlySpan arguments) const override; int number_of_components() const override { return 1; } Vector default_decode() const override; ColorSpaceFamily const& family() const override { return ColorSpaceFamily::Separation; } diff --git a/Userland/Libraries/LibPDF/Renderer.cpp b/Userland/Libraries/LibPDF/Renderer.cpp index 15047dc66f..07cac390ef 100644 --- a/Userland/Libraries/LibPDF/Renderer.cpp +++ b/Userland/Libraries/LibPDF/Renderer.cpp @@ -1129,7 +1129,7 @@ PDFErrorOr Renderer::load_image(NonnullRefPtr component_values; + Vector component_values; component_values.resize(n_components); while (!content.is_empty() && y < height) { auto sample = content.slice(0, bytes_per_component * n_components); @@ -1137,7 +1137,7 @@ PDFErrorOr Renderer::load_image(NonnullRefPtrstyle(component_values)).get(); bitmap->set_pixel(x, y, color);