#include "Halide.h"

namespace {

class EdgeDetect : public Halide::Generator<EdgeDetect> {
public:
    ImageParam input{ UInt(8), 2, "input" };

    Func build() {
        Var x, y;

        Func clamped = Halide::BoundaryConditions::repeat_edge(input);

        // Gradients in x and y.
        Func gx("gx");
        Func gy("gy");
        gx(x, y) = (clamped(x + 1, y) - clamped(x - 1, y)) / 2;
        gy(x, y) = (clamped(x, y + 1) - clamped(x, y - 1)) / 2;

        Func result("result");

        result(x, y) = gx(x, y) * gx(x, y) + gy(x, y) * gy(x, y) + 128;

        // CPU schedule:
        //   Parallelize over scan lines, 4 scanlines per task.
        //   Independently, vectorize in x.
        result
            .parallel(y, 4)
            .vectorize(x, natural_vector_size(UInt(8)));

        return result;
    }
};

Halide::RegisterGenerator<EdgeDetect> register_edge_detect{ "edge_detect" };

}  // namespace

#include "Halide.h"

namespace {

class EdgeDetect : public Halide::Generator<EdgeDetect> {
public:
    ImageParam input{ UInt(8), 2, "input" };

    Func build() {
        Var x, y;
        
        Func clamped = Halide::BoundaryConditions::repeat_edge(input);

        // Upcast to 16-bit
        Func in16;
        in16(x, y) = cast<int16_t>(clamped(x, y));
        
        // Gradients in x and y.
        Func gx;
        Func gy;
        gx(x, y) = (in16(x + 1, y) - in16(x - 1, y)) / 2;
        gy(x, y) = (in16(x, y + 1) - in16(x, y - 1)) / 2;

        // Gradient magnitude.
        Func grad_mag;
        grad_mag(x, y) = (gx(x, y) * gx(x, y) + gy(x, y) * gy(x, y));

        // Draw the result
        Func result;
        result(x, y) = cast<uint8_t>(clamp(grad_mag(x, y), 0, 255));
        
        result
            .compute_root()
            .vectorize(x, 8)
            .parallel(y, 8);
        
        return result;
    }
};

Halide::RegisterGenerator<EdgeDetect> register_edge_detect{ "edge_detect" };

}  // namespace

Improve edge detector (fix overflow)
Improve edge detector (fix overflow)

Former-commit-id: 34d9d1e630c8195888530530f806efd1f9132137