pnghalide

Not able to save image in png format while using Halide


I tried running the following program on a Windows machine using Visual Studio:

#include <Halide.h>
#include "halide_image_io.h"
#include "png.h"

using namespace Halide;
using namespace Halide::Tools;

int main(int argc, char** argv)
{
    Buffer<uint8_t> in = load_image("images/rgb.png");
    Func blurx, out;
    Var  x, y,c, xi, yi;
    printf("width : %d, height: %d and channels: %d",in.width(),in.height(),in.channels());
    //width = 768, height = 1280
    blurx(x, y,c) = (in(x, y,c) + in(x, y,c) + in(x, y,c)) / 3.0f;
    out(x, y,c) = (blurx(x, y,c) + blurx(x, y,c) + blurx(x, y,c)) / 3.0f;
    out.tile(x, y, xi, yi, 256, 32).vectorize(xi, 8).parallel(y);
    Buffer<uint8_t> result = out.realize(in.width(), in.height(),in.channels());
    save_image(result, "output/output.png");
    return 0;
    }

I am getting the error "Image cannot be saved in this format". The error does not occur when I remove the '/3.0f'. So, division may be causing some pixel values to be an invalid format. Hence, I am not able to save it in .png format. How can I solve this? Please note: the formula should have been (in(x-1, y,c) + in(x, y,c) + in(x+1, y,c)) / 3.0f; .... but, that gives me error"access out of boundaries of input buffer".... I am trying to resolve the division error first, so for the time being, I modified the formula .. which helped me to catch this error.


Solution

  • I made the following changes and it works now:

    #include <Halide.h>
    #include "halide_image_io.h"
    #include "png.h"
    
    using namespace Halide;
    using namespace Halide::Tools;
    
    int main(int argc, char** argv)
    {
        Buffer<uint8_t> in = load_image("images/rgb.png");
        Func blurx, out;
        Var  x, y,c, xi, yi;
        Func in_bounded = BoundaryConditions::repeat_edge(in);
        Func input_16;
        input_16(x, y) = cast<uint16_t>(in_bounded(x, y));
    
        blurx(x, y,c) = (input_16(x, y,c) + input_16(x, y,c) + input_16(x, y,c)) / 3;
        out(x, y,c) =  cast<uint8_t>(blurx(x, y,c) + blurx(x, y,c) + blurx(x, y,c)) / 3);
        out.tile(x, y, xi, yi, 256, 32).vectorize(xi, 8).parallel(y);
        Buffer<uint8_t> result = out.realize(in.width(), in.height(),in.channels());
        save_image(result, "output/output.png");
        return 0;
        }