Problem with porting SVG composition standard in Matlab

65 views Asked by At

I have ported most of the 24 methods from the SVG composition standard (2009, https://www.w3.org/TR/2009/WD-SVGCompositing-20090430/) in Matlab but four methods (like color-dodge) relay on comparing images within an if-statement (like: if Sca == Sa && Dca == 0 ...) but Matlab views this as nonscalar operators.

Sc, Dc are RGB images Sa, Da are gray masks that represents alpha channels Dca, Sca are pre-multiplied images: Dca = Dc .* Da; Sca = Sc .* Sa;

if strcmp(compo_meth, 'color-dodge') == 1; 
    if Sca == Sa && Dca == 0
        Dca = (1 - Da) .* Sca;
    elseif Sca == Sa
        Dca = Sa .* Da + (1 - Da) .* Sca + (1 - Sa) .* Dca;
    elseif Sca < Sa
        Dca = Sa .* Da .* min(1, Dca/Da .* Sa/(Sa - Sca));
    end
    Da  = Sa + Da - Sa .* Da;
end

error in if Sca == Sa && Dca == 0

Operands to the || and && operators must be convertible to logical scalar values

1

There are 1 answers

0
Günter Bachelier On

A freelancer.com project delivered the following proposal, which is about 10+ times faster than an arrayfun version but 5+ times slower than one would expect if Matlab could directly uses the syntax in the SVG standard like in most of the other cases:

if strcmp(compo_meth, 'color-dodge') == 1
    rc = (Sca == Sa) & (Dca == 0);
    Dca(rc) = (1 - Da(rc)) .* Sca(rc);
    rc1 = (Sca == Sa) & (Dca ~= 0);
    Dca(rc1) = Sa(rc1) .* Da(rc1) + (1 - Da(rc1)) .* Sca(rc1) + (1 - Sa(rc1)) .* Dca(rc1);
    rc2 = (Sca < Sa);
    Dca(rc2) = Sa(rc2) .* Da(rc2) .* min(1, Dca(rc2)./Da(rc2) .* Sa(rc2)./(Sa(rc2) - Sca(rc2)));
    Da = Sa + Da - Sa .* Da;
end