Minor fix regarding blue-noise-offsets

This commit is contained in:
Stephen Seo 2021-12-01 18:13:21 +09:00
parent 6e27c41f43
commit de1b0cc2ac

View file

@ -50,6 +50,7 @@ Image::Image()
is_dithered_grayscale_(false), is_dithered_grayscale_(false),
is_dithered_color_(false), is_dithered_color_(false),
is_preserving_blue_noise_offsets_(true) { is_preserving_blue_noise_offsets_(true) {
std::srand(std::time(nullptr));
GenerateBlueNoiseOffsets(); GenerateBlueNoiseOffsets();
} }
@ -91,6 +92,7 @@ Image::Image(const std::string &filename)
return; return;
} }
std::srand(std::time(nullptr));
GenerateBlueNoiseOffsets(); GenerateBlueNoiseOffsets();
} }
@ -696,10 +698,6 @@ std::unique_ptr<Image> Image::ToColorDitheredWithBlueNoise(Image *blue_noise) {
if (!opencl_handle->HasBuffer(color_kernel_name, if (!opencl_handle->HasBuffer(color_kernel_name,
kBufferBlueNoiseOffsetsName)) { kBufferBlueNoiseOffsetsName)) {
if (!is_preserving_blue_noise_offsets_) {
GenerateBlueNoiseOffsets();
}
if (!opencl_handle->CreateKernelBuffer( if (!opencl_handle->CreateKernelBuffer(
color_kernel_name, CL_MEM_READ_ONLY, color_kernel_name, CL_MEM_READ_ONLY,
sizeof(unsigned int) * blue_noise_offsets_.size(), nullptr, sizeof(unsigned int) * blue_noise_offsets_.size(), nullptr,
@ -713,6 +711,10 @@ std::unique_ptr<Image> Image::ToColorDitheredWithBlueNoise(Image *blue_noise) {
} }
} }
if (!is_preserving_blue_noise_offsets_) {
GenerateBlueNoiseOffsets();
}
if (!opencl_handle->SetKernelBufferData( if (!opencl_handle->SetKernelBufferData(
color_kernel_name, kBufferBlueNoiseOffsetsName, color_kernel_name, kBufferBlueNoiseOffsetsName,
blue_noise_offsets_.size() * sizeof(unsigned int), blue_noise_offsets_.size() * sizeof(unsigned int),
@ -1413,12 +1415,11 @@ const std::string &Image::GetColorKernelName() {
} }
void Image::GenerateBlueNoiseOffsets() { void Image::GenerateBlueNoiseOffsets() {
std::srand(std::time(nullptr)); do {
while (DuplicateBlueNoiseOffsetExists()) {
for (unsigned int i = 0; i < blue_noise_offsets_.size(); ++i) { for (unsigned int i = 0; i < blue_noise_offsets_.size(); ++i) {
blue_noise_offsets_.at(i) = rand() % kBlueNoiseOffsetMax; blue_noise_offsets_.at(i) = rand() % kBlueNoiseOffsetMax;
} }
} } while (DuplicateBlueNoiseOffsetExists());
} }
bool Image::DuplicateBlueNoiseOffsetExists() const { bool Image::DuplicateBlueNoiseOffsetExists() const {