Skip to content

Instantly share code, notes, and snippets.

@kanryu
Created January 21, 2018 02:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kanryu/e08d00ba672cfe8d658d6b296b6cbe36 to your computer and use it in GitHub Desktop.
Save kanryu/e08d00ba672cfe8d658d6b296b6cbe36 to your computer and use it in GitHub Desktop.
shrink a bitmap into half size with halide-lang
#include "Halide.h"
namespace {
class HalfShrink : public Halide::Generator<HalfShrink> {
public:
Input<Buffer<uint8_t>> input{ "input", 3 };
Output<Buffer<uint8_t>> halfshrink{ "output", 3 };
void generate() {
Var x("x"), y("y"), z("z"), c("c");
Var x1("x1"), y1("y1");
Func clamped, input16;
clamped = Halide::BoundaryConditions::repeat_edge(input,
{{input.dim(0).min(), input.dim(0).extent()},
{input.dim(1).min(), input.dim(1).extent()}});
input16(x, y, c) = cast<uint16_t>(clamped(x, y, c));
Expr px = input16(2*x1, 2*y1, c)
+ input16(2*x1+1, 2*y1, c)
+ input16(2*x1, 2*y1+1, c)
+ input16(2*x1+1, 2*y1+1, c);
px /= 4;
halfshrink(x1, y1, c) = cast<uint8_t>(px);
halfshrink.compute_root().vectorize(x1, 16).parallel(y1); }
// void schedule() {
// }
};
} // namespace
HALIDE_REGISTER_GENERATOR(HalfShrink, halfshrink)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment