Skip to content

Commit

Permalink
feat: ✨ Add HalideTutorial07
Browse files Browse the repository at this point in the history
add halide official tutorial07 with my own code
  • Loading branch information
RookieTars committed Mar 8, 2022
1 parent 1ca0670 commit a80a391
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
5 changes: 5 additions & 0 deletions HalideTutorial07/.vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 1,5 @@
{
"cmake.configureSettings": {
"CMAKE_TOOLCHAIN_FILE": "C:/src/vcpkg/scripts/buildsystems/vcpkg.cmake"
}
}
29 changes: 29 additions & 0 deletions HalideTutorial07/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 1,29 @@
cmake_minimum_required(VERSION 3.21)

project(halide_tutorial07)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED YES)
set(CMAKE_CXX_EXTENSIONS NO)

find_package(Halide REQUIRED)
find_package(PNG REQUIRED)
find_package(JPEG REQUIRED)

add_executable(
${PROJECT_NAME}
main.cpp
)
target_include_directories(
${PROJECT_NAME}
PRIVATE
${JPEG_INCLUDE_DIR}
)
target_link_libraries(
${PROJECT_NAME}
PRIVATE
Halide::Halide
Halide::Tools
PNG::PNG
${JPEG_LIBRARIES}
)
Binary file added HalideTutorial07/images/rgb.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
75 changes: 75 additions & 0 deletions HalideTutorial07/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 1,75 @@
#include "Halide.h"
#include <stdio.h>

using namespace Halide;

#include "halide_image_io.h"

using namespace Halide::Tools;

int main()
{
Var x("x"), y("y"), c("c");
{
Buffer<uint8_t> input = load_image("../images/rgb.png");
Func input_16("input16");
input_16(x, y, c) = cast<uint16_t>(input(x, y, c));

Func blur_x("blur_x");
blur_x(x, y, c) = (input_16(x - 1, y, c)
2 * input_16(x, y, c)
input_16(x 1, y, c)) /
4;

Func blur_y("blur_y");
blur_y(x, y, c) = (blur_x(x, y - 1, c)
2 * blur_x(x, y, c)
blur_x(x, y 1, c)) /
4;

Func output("output");
output(x, y, c) = cast<uint8_t>(blur_y(x, y, c));

// Buffer<uint8_t> result = output.realize({input.width(), input.height(), input.channels()});

Buffer<uint8_t> result(input.width() - 2, input.height() - 2, 3);
result.set_min(1, 1);
output.realize(result);

save_image(result, "blurry_parrot_1.png");
}
{
Buffer<uint8_t> input = load_image("../images/rgb.png");

Func clamped("clamped");

Expr clamped_x = clamp(x, 0, input.width() - 1);
Expr clamped_y = clamp(y, 0, input.height() - 1);

clamped(x, y, c) = input(clamped_x, clamped_y, c);

Func input_16("input_16");
input_16(x, y, c) = cast<uint16_t>(clamped(x, y, c));

Func blur_x("blur_x");
blur_x(x, y, c) = (input_16(x - 1, y, c)
2 * input_16(x, y, c)
input_16(x 1, y, c)) /
4;

Func blur_y("blur_y");
blur_y(x, y, c) = (blur_x(x, y - 1, c)
2 * blur_x(x, y, c)
blur_x(x, y 1, c)) /
4;

Func output("output");
output(x, y, c) = cast<uint8_t>(blur_y(x, y, c));

Buffer<uint8_t> result = output.realize({input.width(), input.height(), input.channels()});

save_image(result, "blurry_parrot_2.png");
}
printf("Success!\n");
return 0;
}

0 comments on commit a80a391

Please sign in to comment.