Running the halide-lang.org sample code
cmake_minimum_required(VERSION 3.16) | |
project(BlogBlur) | |
find_package(Halide REQUIRED) | |
add_executable(blur main.cpp) | |
target_link_libraries(blur PRIVATE Halide::Halide Halide::ImageIO) | |
#include "Halide.h" | |
#include "halide_image_io.h" | |
#include <iostream> | |
#include <utility> | |
using namespace Halide; | |
using namespace Halide::Tools; | |
Func blur3x3(Func input) { | |
Func blur_x, blur_y; | |
Var x, y, xi, yi; | |
// The algorithm - no storage or order | |
blur_x(x, y) = (input(x - 1, y) + input(x, y) + input(x + 1, y)) / 3; | |
blur_y(x, y) = (blur_x(x, y - 1) + blur_x(x, y) + blur_x(x, y + 1)) / 3; | |
// The schedule - defines order, locality; implies storage | |
blur_y.tile(x, y, xi, yi, 256, 32).vectorize(xi, 8).parallel(y); | |
blur_x.compute_at(blur_y, x).vectorize(x, 8); | |
return blur_y; | |
} | |
int main(int argc, char **argv) { | |
if (argc < 3) { | |
std::cout << "Usage: " << argv[0] << " <input-file> <output-file>\n"; | |
return 1; | |
} | |
// 1. Load the image from disk. | |
Buffer<float> input = load_and_convert_image(argv[1]); | |
// 2. Define the points outside the image. | |
Func with_boundary = BoundaryConditions::repeat_edge(input); | |
// 3. Build the blur program. | |
Func output = blur3x3(with_boundary); | |
// 4. JIT compile and run the blur. | |
Buffer<float> result = output.realize(input.width(), input.height()); | |
// 5. Write the code back out. | |
convert_and_save_image(result, argv[2]); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment