Search code examples
c++halide

How to modify color channels individually in Halide?


I am interested in modifying individual color channels of Halide pixels. For example, how could I brighten the red channel but keep the green and blue channels unchanged? Brightening all colors for all pixels would look like this:

Halide::Expr value = input(x, y, c);
value = value * 2.0f;
brighter(x, y, c) = value;

But when attempting to brighten a single channel with the method below, an error is received.

Halide::Expr value = input(x, y, c);
value(x,y,0) = value(x,y,0) * 2.0f; // brighten red
value(x,y,1) = value(x,y,2);        // keep green the same
value(x,y,2) = value(x,y,3);        // keep blue the same
brighter(x, y, c) = value;

The error:

ImgPipe_Halide.cpp:88:14: error: no match for call to ‘(Halide::Expr) (Halide::Var&, Halide::Var&, int)’

So, how can I read individual channel values from pixels, and how can I modify them?


Solution

  • One way would be to use halide's select function

    Example:

    value = Halide::select(c == 0, input(x,y,c) * 2.0f,
                           input(x,y,c));