Some fixes using dithering kernel

This commit is contained in:
Stephen Seo 2021-11-27 12:27:39 +09:00
parent 732c99403a
commit 5ad6f9d035
2 changed files with 24 additions and 11 deletions

View file

@ -1,4 +1,4 @@
cmake_minimum_required(VERSION 3.18) cmake_minimum_required(VERSION 3.13)
project(EN605.617.81.FA21_StephenSeo_DitheringProject) project(EN605.617.81.FA21_StephenSeo_DitheringProject)
set(Project_SOURCES set(Project_SOURCES

View file

@ -787,23 +787,32 @@ std::unique_ptr<Image> Image::ToDitheredWithBlueNoise(Image *blue_noise) {
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
unsigned int blue_noise_size = blue_noise->GetWidth(); unsigned int blue_noise_width = blue_noise->GetWidth();
if (!opencl_handle->AssignKernelArgument(kid, 5, sizeof(unsigned int), if (!opencl_handle->AssignKernelArgument(kid, 5, sizeof(unsigned int),
&blue_noise_size)) { &blue_noise_width)) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 5" std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 5"
<< std::endl; << std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
std::srand(std::time(nullptr)); unsigned int blue_noise_height = blue_noise->GetHeight();
unsigned int blue_noise_offset = std::rand() % blue_noise_size;
if (!opencl_handle->AssignKernelArgument(kid, 6, sizeof(unsigned int), if (!opencl_handle->AssignKernelArgument(kid, 6, sizeof(unsigned int),
&blue_noise_offset)) { &blue_noise_height)) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 6" std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 6"
<< std::endl; << std::endl;
opencl_handle->CleanupAllKernels(); opencl_handle->CleanupAllKernels();
return {}; return {};
} }
std::srand(std::time(nullptr));
unsigned int blue_noise_offset =
std::rand() % (blue_noise_width * blue_noise_height);
if (!opencl_handle->AssignKernelArgument(kid, 7, sizeof(unsigned int),
&blue_noise_offset)) {
std::cout << "ERROR ToDitheredWithBlueNoise: Failed to set parameter 7"
<< std::endl;
opencl_handle->CleanupAllKernels();
return {};
}
// auto global_work_sizes = opencl_handle->GetGlobalWorkSize(kid); // auto global_work_sizes = opencl_handle->GetGlobalWorkSize(kid);
auto work_group_size = opencl_handle->GetWorkGroupSize(kid); auto work_group_size = opencl_handle->GetWorkGroupSize(kid);
@ -856,8 +865,11 @@ const char *Image::GetDitheringKernel() {
"unsigned int x,\n" "unsigned int x,\n"
"unsigned int y,\n" "unsigned int y,\n"
"unsigned int o,\n" "unsigned int o,\n"
"unsigned int bn_size) {\n" "unsigned int bn_width,\n"
"return (o + x + y * bn_size) % (bn_size * bn_size);\n" "unsigned int bn_height) {\n"
"unsigned int offset_x = (o % bn_width + x) % bn_width;\n"
"unsigned int offset_y = (o / bn_width + y) % bn_height;\n"
"return offset_x + offset_y * bn_width;\n"
"}\n" "}\n"
"\n" "\n"
"__kernel void Dither(\n" "__kernel void Dither(\n"
@ -866,12 +878,13 @@ const char *Image::GetDitheringKernel() {
"__global unsigned char *output,\n" "__global unsigned char *output,\n"
"const unsigned int input_width,\n" "const unsigned int input_width,\n"
"const unsigned int input_height,\n" "const unsigned int input_height,\n"
"const unsigned int blue_noise_size,\n" "const unsigned int blue_noise_width,\n"
"const unsigned int blue_noise_height,\n"
"const unsigned int blue_noise_offset) {\n" "const unsigned int blue_noise_offset) {\n"
"unsigned int idx = get_global_id(0);\n" "unsigned int idx = get_global_id(0);\n"
"unsigned int idy = get_global_id(1);\n" "unsigned int idy = get_global_id(1);\n"
"unsigned int b_i = BN_INDEX(idx, idy, blue_noise_offset, " "unsigned int b_i = BN_INDEX(idx, idy, blue_noise_offset,\n"
"blue_noise_size);\n" "blue_noise_width, blue_noise_height);\n"
"unsigned int input_index = idx + idy * input_width;\n" "unsigned int input_index = idx + idy * input_width;\n"
"output[input_index] = input[input_index] > blue_noise[b_i] ? 255 : " "output[input_index] = input[input_index] > blue_noise[b_i] ? 255 : "
"0;\n" "0;\n"