This source file includes following definitions.
- is_interleaved
- is_planar
- generate
- schedule
#include "Halide.h"
namespace {
using Halide::saturating_cast;
template<typename T>
Halide::Expr is_interleaved(const T &p, int channels = 3) {
return p.dim(0).stride() == channels && p.dim(2).stride() == 1 && p.dim(2).extent() == channels;
}
template<typename T>
Halide::Expr is_planar(const T &p, int channels = 3) {
return p.dim(0).stride() == 1 && p.dim(2).extent() == channels;
}
class TiledBlur : public Halide::Generator<TiledBlur> {
public:
Input<Buffer<uint8_t>> input{ "input", 3 };
Output<Buffer<uint8_t>> output{ "output", 3 };
void generate() {
Expr input_float = cast<float>(input(x, y, c)) / 255.f;
brightened(x, y, c) = input_float * 1.2f;
tiled_blur.define_extern(
"blur2x2",
{ brightened, input.dim(0).extent(), input.dim(1).extent() },
Float(32), 3);
Expr tiled_blur_brightened = tiled_blur(x, y, c) * 1.2f;
output(x, y, c) = saturating_cast<uint8_t>(tiled_blur_brightened * 255.f);
}
void schedule() {
Var xi, yi;
output.reorder(c, x, y).tile(x, y, xi, yi, 32, 32);
tiled_blur.compute_at(output, x);
brightened.compute_at(output, x);
brightened.trace_realizations();
input.dim(0).set_stride(Expr());
output.dim(0).set_stride(Expr());
output.specialize(is_planar(input) && is_planar(output))
.vectorize(xi, natural_vector_size<float>());
output.specialize(is_interleaved(input) && is_interleaved(output));
}
private:
Var x{"x"}, y{"y"}, c{"c"};
Func tiled_blur{"tiled_blur"};
Func brightened{"brightened"};
};
HALIDE_REGISTER_GENERATOR(TiledBlur, "tiled_blur")
}