Repository: LadybirdBrowser/ladybird
File: Libraries/LibWeb/SVG/SVGFilterElement.cpp (C++)

Code before:
                size_t count = 0;

                StringView sv = values_value;
                auto skip_leading_whitespace = [&] {
                    sv = sv.trim_whitespace(AK::TrimMode::Left);
                };
                auto consume_comma_and_whitespace = [&] {
                    if (!sv.is_empty() && sv[0] == ',')
                        sv = sv.substring_view(1);
                    skip_leading_whitespace();
                };

                skip_leading_whitespace();
                while (!sv.is_empty() && count < 20) {
                    // Parse the next number without trimming (we already trimmed on the left).
                    auto result = AK::parse_first_number<float>(sv, AK::TrimWhitespace::No);
                    if (!result.has_value())
                        break;
                    matrix[count++] = result->value;
                    // Advance exactly past the number just parsed, then consume optional comma + whitespace.
                    sv = sv.substring_view(result->characters_parsed);
                    consume_comma_and_whitespace();
                }

                if (count == 20) {
                    // Detect identity to avoid creating a filter that might behave differently
                    // than a no-op in some pipelines.
                    static constexpr float identity[20] = {
                        1, 0, 0, 0, 0,
                        0, 1, 0, 0, 0,
                        0, 0, 1, 0, 0,
                        0, 0, 0, 1, 0
                    };
                    bool is_identity = true;
                    for (size_t i = 0; i < 20; ++i) {
                        if (matrix[i] != identity[i]) {
                            is_identity = false;
                            break;
                        }
                    }

                    if (is_identity) {
                        if (input.has_value()) {
                            root_filter = input;
                            update_result_map(*colormatrix_primitive);
                        }
                        // If there is no input (SourceGraphic), skip producing a filter,
                        // so the pipeline defaults to painting the source as-is.
                    } else {
                        // Skia's color matrix uses 0..255 range for the bias (5th column).
                        matrix[4] *= 255.0f;

Review comment (suggestion):
(consider creating separate `Gfx::Filter` for that? but probably not strictly necessary; still could be `static constexpr`?)

Code after:
                // values: single number s (1 = original)
                float s = 1.0f;
                if (!values_value.is_empty()) {
                    if (auto parsed = AK::parse_number<float>(values_value, AK::TrimWhitespace::Yes); parsed.has_value())
                        s = *parsed;
                }
                root_filter = Gfx::Filter::saturate(s, input);
                update_result_map(*colormatrix_primitive);
            } else if (type_value.equals_ignoring_ascii_case("hueRotate"sv)) {
                // values: angle in degrees
                float angle_degrees = 0.0f;
                if (!values_value.is_empty()) {
                    if (auto parsed = AK::parse_number<float>(values_value, AK::TrimWhitespace::Yes); parsed.has_value())
                        angle_degrees = *parsed;
                }
                root_filter = Gfx::Filter::hue_rotate(angle_degrees, input);
                update_result_map(*colormatrix_primitive);
            } else if (type_value.equals_ignoring_ascii_case("luminanceToAlpha"sv)) {
                // values ignored; convert luminance to alpha and zero RGB.
                float matrix[20] = {
                    0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0,
                    0, 0, 0, 0, 0,
                    0.2126f, 0.7152f, 0.0722f, 0, 0
                };
                root_filter = Gfx::Filter::color_matrix(matrix, input);
                update_result_map(*colormatrix_primitive);
            } else {
                // Unknown 'type' value on feColorMatrix; skip creating a filter and log.
                dbgln("SVGFEColorMatrixElement: Unknown type '{}' — skipping filter primitive", type_value);
            }
        } else if (auto* image_primitive = as_if<SVGFEImageElement>(node)) {
            auto bitmap = image_primitive->current_image_bitmap({});
            if (!bitmap)
                return IterationDecision::Continue;

            auto src_rect = image_primitive->content_rect();
            if (!src_rect.has_value())
                return IterationDecision::Continue;

            auto* dom_node = referenced_node.dom_node();
            if (!dom_node)
                return IterationDecision::Continue;

            auto* paintable_box = dom_node->paintable_box();
            if (!paintable_box)
                return IterationDecision::Continue;

            auto dest_rect = Gfx::enclosing_int_rect(paintable_box->absolute_rect().to_type<float>());
            auto scaling_mode = CSS::to_gfx_scaling_mode(paintable_box->computed_values().image_rendering(), *src_rect, dest_rect);
            root_filter = Gfx::Filter::image(*bitmap, *src_rect, dest_rect, scaling_mode);