From a8f0ec788acb8b4885d986d6b52d5923a535826f Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 6 Oct 2021 19:54:07 +0900 Subject: [PATCH] Switch to CMake, try to fix grayscale blue-noise --- .clangd | 2 ++ .gitignore | 1 + CMakeLists.txt | 29 +++++++++++++++++++++++++++++ Makefile | 25 ------------------------- src/blue_noise.cpp | 28 +++++++++++++++++----------- 5 files changed, 49 insertions(+), 36 deletions(-) create mode 100644 .clangd create mode 100644 CMakeLists.txt delete mode 100644 Makefile diff --git a/.clangd b/.clangd new file mode 100644 index 0000000..43f14d4 --- /dev/null +++ b/.clangd @@ -0,0 +1,2 @@ +CompileFlags: + Add: [-std=c++17] diff --git a/.gitignore b/.gitignore index 2551958..414513b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ Dithering compile_commands.json .cache/ +build*/ diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..4d18424 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,29 @@ +cmake_minimum_required(VERSION 3.9) +project(Dithering) + +set(Dithering_SOURCES + ${CMAKE_CURRENT_SOURCE_DIR}/src/blue_noise.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/image.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp +) + +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic") +set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3 -DNDEBUG") +set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g") + +if(NOT DEFINED CMAKE_BUILD_TYPE OR NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE "Debug") + message("Set build type to Debug by default") +endif() + +find_package(Threads REQUIRED) +find_package(OpenCL REQUIRED) + +add_executable(Dithering ${Dithering_SOURCES}) +target_compile_features(Dithering PUBLIC cxx_std_17) +target_include_directories(Dithering PUBLIC + Threads::Threads + ${OpenCL_INCLUDE_DIRS}) +target_link_libraries(Dithering PUBLIC + Threads::Threads + ${OpenCL_LIBRARIES}) diff --git a/Makefile b/Makefile deleted file mode 100644 index d4e60a1..0000000 --- a/Makefile +++ /dev/null @@ -1,25 +0,0 @@ - -COMMON_FLAGS=-Wall -Wextra -Wpedantic -std=c++17 -LINKER_FLAGS=-lpthread -lOpenCL -ifdef DEBUG - CPPFLAGS=${COMMON_FLAGS} -g -O0 -else - CPPFLAGS=${COMMON_FLAGS} -O3 -DNDEBUG -endif - -SOURCES= \ - src/main.cpp \ - src/blue_noise.cpp \ - src/image.cpp -OBJECTS=${subst .cpp,.o,${SOURCES}} - -all: Dithering - -Dithering: ${OBJECTS} - ${CXX} ${CPPFLAGS} ${LINKER_FLAGS} -o Dithering $^ - -.PHONY: - -clean: - rm -f Dithering - rm -f ${OBJECTS} diff --git a/src/blue_noise.cpp b/src/blue_noise.cpp index bb30f31..fd90162 100644 --- a/src/blue_noise.cpp +++ b/src/blue_noise.cpp @@ -113,10 +113,8 @@ image::Bl dither::blue_noise_grayscale(int width, int height, int threads) { int filter_size = (width + height) / 2; std::vector precomputed(internal::precompute_gaussian(filter_size)); - int min, max; + int min, max, min2, max2; float tempPixel; - int prevmin = -1; - int prevmax = -1; while(true) { printf("Iteration %d\n", iterations); @@ -126,17 +124,25 @@ image::Bl dither::blue_noise_grayscale(int width, int height, int threads) { &precomputed, threads); std::tie(min, max) = internal::filter_minmax(filter_out); - printf("min == %4d, max == %4d\n", min, max); + printf("min == %4d, max == %4d", min, max); tempPixel = image[max]; - image[max] = image[min]; - image[min] = tempPixel; - if(prevmin >= 0 && prevmax >= 0 - && (utility::dist(min, prevmin, width) < 1.5F - || utility::dist(max, prevmax, width) < 1.5F)) { + image[max] = 0.0F; + + internal::compute_filter_grayscale(image, + width, height, count, + filter_size, filter_out, + &precomputed, threads); + + std::tie(min2, max2) = internal::filter_minmax(filter_out); + printf(", min2 == %4d, max2 == %4d\n", min2, max2); + + if(utility::dist(min, min2, width) < 1.5F) { + image[max] = image[min2]; + image[min2] = tempPixel; + } else { + image[max] = tempPixel; break; } - prevmin = min; - prevmax = max; //#ifndef NDEBUG if(iterations % 20 == 0) {