Search code examples
image-processinghalide

How to load_image() in Halide with 4 channels instead of 3?


I want to get a 4 channel image instead of 3 while loading it in Halide, however load_image() gives only 3 channel images. How can I solve it?


Solution

  • Halide's load_image function simply loads an image file off the disk. If it's RGB, it will have three channels, if it's RGBA, it will have four channels. This is working as intended.

    If you want to add a channel to your image during a pipeline, then you can write:

    Buffer<uint8_t> input = load_image(...);
    
    Func alpha_255;
    alpha_255(x, y, c) = select(c == 3, 255, input(x, y, c));
    

    Then you can schedule that func however you want. Most likely you'll end up inlining it into a consumer that has its innermost channel dimension unrolled to get rid of the select.