/* This file is part of darktable, Copyright (C) 2010-2025 darktable developers. darktable is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. darktable is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with darktable. If not, see . */ #define __STDC_FORMAT_MACROS #include "bauhaus/bauhaus.h" #include "common/imagebuf.h" #include "control/control.h" #include "develop/develop.h" #include "develop/imageop.h" #include "develop/imageop_math.h" #include "develop/imageop_gui.h" #include "develop/tiling.h" #include "gui/accelerators.h" #include "gui/gtk.h" #include "iop/iop_api.h" #include #include #include #include #include "iop/Permutohedral.h" #include #include G_BEGIN_DECLS /** * implementation of the 5d-color bilateral filter using andrew adams * et al.'s permutohedral lattice, which they kindly provided online * as c++ code, under new bsd license. */ #define MAX_DIRECT_STAMP_RADIUS 6 DT_MODULE_INTROSPECTION(1, dt_iop_bilateral_params_t) typedef struct dt_iop_bilateral_params_t { // standard deviations of the gauss to use for blurring in the dimensions x,y,r,g,b (or L*,a*,b*) float radius; // $MIN: 1.0 $MAX: 50.0 $DEFAULT: 15.0 float reserved; // $DEFAULT: 15.0 float red, green, blue; // $MIN: 0.0001 $MAX: 1.0 $DEFAULT: 0.005 } dt_iop_bilateral_params_t; typedef struct dt_iop_bilateral_gui_data_t { GtkWidget *radius, *red, *green, *blue; } dt_iop_bilateral_gui_data_t; typedef struct dt_iop_bilateral_data_t { float sigma[5]; } dt_iop_bilateral_data_t; typedef struct dt_iop_bilateral_global_data_t { int kernel_insert; int kernel_splat; int kernel_blur; int kernel_slice; int kernel_direct; } dt_iop_bilateral_global_data_t; const char *name() { return _("surface blur"); } const char *aliases() { return _("denoise (bilateral filter)"); } int default_group() { return IOP_GROUP_CORRECT | IOP_GROUP_TECHNICAL; } int flags() { return IOP_FLAGS_ALLOW_TILING | IOP_FLAGS_SUPPORTS_BLENDING; } dt_iop_colorspace_type_t default_colorspace(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece) { return IOP_CS_RGB; } const char **description(dt_iop_module_t *self) { return dt_iop_set_description (self, _("apply edge-aware surface blur\n" "to denoise or smoothen textures"), _("corrective and creative"), _("linear, RGB, scene-referred"), _("linear, RGB"), _("linear, RGB, scene-referred")); } static void _compute_sigmas(float sigma[5], dt_iop_bilateral_data_t *data, float scale, float iscale) { sigma[0] = data->sigma[0] * scale / iscale; sigma[1] = data->sigma[1] * scale / iscale; sigma[2] = data->sigma[2]; sigma[3] = data->sigma[3]; sigma[4] = data->sigma[4]; } void process(dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, const void *const ivoid, void *const ovoid, const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out) { if(!dt_iop_have_required_input_format(4 /*we need full-color pixels*/, self, piece->colors, ivoid, ovoid, roi_in, roi_out)) return; assert(roi_in->width == roi_out->width); assert(roi_in->height == roi_out->height); const size_t width = roi_out->width; const size_t height = roi_out->height; const size_t npixels = width * height; if (npixels > INT_MAX) { dt_iop_set_module_trouble_message(self, _("image too large"), _("this module is unable to process\n" "images with more than 2 gigapixels.\n" "processing has been skipped."), "image too large, processing skipped"); dt_iop_image_copy_by_size((float*)ovoid, (float*)ivoid, roi_out->width, roi_out->height, 4); return; } else { dt_iop_clear_module_trouble_message(self); } dt_iop_bilateral_data_t *data = (dt_iop_bilateral_data_t *)piece->data; float sigma[5]; _compute_sigmas(sigma, data, roi_in->scale, piece->iscale); if(fmaxf(sigma[0], sigma[1]) < 0.1f) { dt_iop_image_copy_by_size((float*)ovoid, (float*)ivoid, roi_out->width, roi_out->height, 4); return; } // if rad <= 6 use naive version! const int prad = (int)(3.0f * fmaxf(sigma[0], sigma[1]) + 1.0f); const int rad = MIN(prad, MIN(roi_out->width, roi_out->height) - 2 * prad); const gboolean thumb = dt_pipe_is_thumb(piece->pipe); if(rad < 1 || (rad <= MAX_DIRECT_STAMP_RADIUS && thumb)) { // no use denoising the thumbnail. takes ages without permutohedral dt_iop_image_copy_by_size((float*)ovoid, (float*)ivoid, roi_out->width, roi_out->height, 4); } else if(rad <= MAX_DIRECT_STAMP_RADIUS) { static const size_t weights_size = 2 * (6 + 1) * 2 * (6 + 1); float mat[weights_size]; const int wd = 2 * rad + 1; float *m = mat + rad * wd + rad; float weight = 0.0f; const dt_aligned_pixel_t isig2col = {1.0f / (2.0f * sigma[2] * sigma[2]), 1.0f / (2.0f * sigma[3] * sigma[3]), 1.0f / (2.0f * sigma[4] * sigma[4]), 0.0f }; // init gaussian kernel for(int l = -rad; l <= rad; l++) for(int k = -rad; k <= rad; k++) weight += m[l * wd + k] = expf(-(l * l + k * k) / (2.f * sigma[0] * sigma[0])); for(int l = -rad; l <= rad; l++) for(int k = -rad; k <= rad; k++) m[l * wd + k] /= weight; DT_OMP_FOR() for(size_t j = 0; j < height; j++) { const float *in = ((float *)ivoid) + 4 * (j * width); float *out = ((float *)ovoid) + 4 * (j * width); if(j < (unsigned)rad || j >= height - rad) { // copy the unprocessed top/bottom border rows for(size_t i = 0; i < width; i++) copy_pixel_nontemporal(out + 4*i, in + 4*i); continue; } for(size_t i = 0; i < (size_t)rad; i++, in += 4) { // copy the unprocessed left border pixels copy_pixel_nontemporal(out + 4*i, in); } // apply blur to main body of image for(size_t i = rad; i < width - rad; i++, in += 4) { float sumw = 0.0f; dt_aligned_pixel_t res = { 0.0f, 0.0f, 0.0f, 0.0f }; dt_aligned_pixel_t pixel; copy_pixel(pixel, in); for(ssize_t l = -rad; l <= rad; l++) { for(ssize_t k = -rad; k <= rad; k++) { const float *inp = in + 4 * (l * width + k); dt_aligned_pixel_t chandiff; for_each_channel(c) chandiff[c] = (pixel[c] - inp[c]) * (pixel[c] - inp[c]) * isig2col[c]; const float diff = chandiff[0] + chandiff[1] + chandiff[2]; const float pix_weight = m[l * wd + k] * expf(-diff); for_each_channel(c) res[c] += inp[c] * pix_weight; sumw += pix_weight; } } for_each_channel(c) res[c] /= sumw; copy_pixel_nontemporal(out + 4*i, res); } for(size_t i = width - rad; i < width; i++, in += 4) { // copy the unprocessed right border pixels copy_pixel_nontemporal(out + 4*i, in); } } dt_omploop_sfence(); } else { for(int k = 0; k < 5; k++) sigma[k] = 1.0f / sigma[k]; const size_t grid_points = (height*sigma[0]) * (width*sigma[1]) * sigma[2] * sigma[3] * sigma[4]; PermutohedralLattice<5, 4> lattice(width * height, dt_get_num_threads(), grid_points); // splat into the lattice DT_OMP_FOR(shared(lattice)) for(size_t j = 0; j < height; j++) { const float *in = (const float *)ivoid + j * width * 4; const int thread = dt_get_thread_num(); const size_t index = j * width; for(size_t i = 0; i < width; i++) { // image coordinates rather than roi ones: the lattice is not // translation invariant, so tiles and zoomed views would otherwise // each get a different blur float pos[5] = { (float)(roi_in->x + (int)i) * sigma[0], (float)(roi_in->y + (int)j) * sigma[1], in[0] * sigma[2], in[1] * sigma[3], in[2] * sigma[4] }; dt_aligned_pixel_t val = { in[0], in[1], in[2], 1.0f }; lattice.splat(pos, val, index + i, thread); in += 4; } } lattice.merge_splat_threads(); // blur the lattice lattice.blur(); // slice from the lattice float *const out = (float*)ovoid; DT_OMP_FOR(shared(lattice)) for(size_t index = 0; index < npixels; index++) { dt_aligned_pixel_t val; lattice.slice(val, index); for_each_channel(k) val[k] /= val[3]; copy_pixel_nontemporal(out + 4*index, val); } dt_omploop_sfence(); } } #ifdef HAVE_OPENCL // the lattice on the device, see data/kernels/permutohedral.cl. sigma holds // the inverted sigmas, as process() has them for the lattice. static cl_int _lattice_cl(const dt_iop_bilateral_global_data_t *gd, const int devid, cl_mem dev_in, cl_mem dev_out, const int width, const int height, const int x0, const int y0, const float sigma[5]) { const size_t npixels = (size_t)width * height; const size_t grid_points = (height*sigma[0]) * (width*sigma[1]) * sigma[2] * sigma[3] * sigma[4]; // the scale factors of the PermutohedralLattice constructor, computed the // same way so the vertices match the CPU's float prm[12]; for(int i = 0; i < 5; i++) { prm[i] = sigma[i]; prm[5 + i] = 1.0f / (sqrtf((float)(i + 1) * (i + 2))); prm[5 + i] *= (5 + 1) * sqrtf(2.0 / 3); } // the roi origin, for the image coordinates process() uses prm[10] = x0; prm[11] = y0; // the CPU's estimate of the vertex count, which it uses to size its // tables. A run that needs more entries starts again with the count it // found. size_t max_entries = PermutohedralLattice<5, 4>::estimatedHashEntries(grid_points, npixels); const int row_width = 4096; cl_int err = DT_OPENCL_DEFAULT_ERROR; for(int attempt = 0; attempt < 3; attempt++) { const size_t capacity = 2 * max_entries; if(capacity > UINT32_MAX || max_entries == 0) return DT_OPENCL_PROCESS_CL; const uint32_t cap = capacity; const uint32_t max_e = max_entries; cl_mem slots = (cl_mem)dt_opencl_alloc_device_buffer(devid, 2 * sizeof(uint32_t) * capacity); cl_mem keys = (cl_mem)dt_opencl_alloc_device_buffer(devid, 3 * sizeof(uint32_t) * max_entries); cl_mem counters = (cl_mem)dt_opencl_alloc_device_buffer(devid, 4 * sizeof(uint32_t)); cl_mem params = (cl_mem)dt_opencl_copy_host_to_device_constant(devid, sizeof(prm), prm); cl_mem values[2] = { NULL, NULL }; gboolean overflow = FALSE; uint32_t cnt[4] = { 0 }; err = DT_OPENCL_DEFAULT_ERROR; if(!slots || !keys || !counters || !params) goto cleanup; err = dt_opencl_fill_buffer(devid, slots, 2 * capacity, 1, 0.0f); if(err == CL_SUCCESS) err = dt_opencl_write_buffer_to_device(devid, cnt, counters, 0, sizeof(cnt), TRUE); // more rounds only for vertices that lost their slot to another vertex // with the same fingerprint for(uint32_t round = 1; err == CL_SUCCESS && round < 8; round++) { err = dt_opencl_enqueue_kernel_2d_args(devid, gd->kernel_insert, width, height, CLARG(dev_in), CLARG(slots), CLARG(keys), CLARG(counters), CLARG(params), CLARG(width), CLARG(height), CLARG(cap), CLARG(max_e), CLARG(round)); if(err == CL_SUCCESS) err = dt_opencl_read_buffer_from_device(devid, cnt, counters, 0, sizeof(cnt), TRUE); if(err != CL_SUCCESS) break; if(cnt[1]) { // too many vertices for the estimate: size the next attempt by the count overflow = TRUE; max_entries = (size_t)cnt[0] + cnt[0] / 8 + 1024; break; } const uint32_t entries = cnt[0]; for(int k = 0; k < 2; k++) { dt_opencl_release_mem_object(values[k]); values[k] = (cl_mem)dt_opencl_alloc_device_buffer(devid, 4 * sizeof(float) * entries); } err = DT_OPENCL_DEFAULT_ERROR; if(!values[0] || !values[1]) break; err = dt_opencl_fill_buffer(devid, values[0], entries, 4, 0.0f); if(err == CL_SUCCESS) err = dt_opencl_enqueue_kernel_2d_args(devid, gd->kernel_splat, width, height, CLARG(dev_in), CLARG(slots), CLARG(keys), CLARG(counters), CLARG(values[0]), CLARG(params), CLARG(width), CLARG(height), CLARG(cap), CLARG(entries)); const int rows = (entries + row_width - 1) / row_width; int src = 0; for(int j = 0; err == CL_SUCCESS && j <= 5; j++) { err = dt_opencl_enqueue_kernel_2d_args(devid, gd->kernel_blur, row_width, rows, CLARG(slots), CLARG(keys), CLARG(values[src]), CLARG(values[1 - src]), CLARG(cap), CLARG(entries), CLARG(row_width), CLARG(j)); src = 1 - src; } if(err == CL_SUCCESS) err = dt_opencl_enqueue_kernel_2d_args(devid, gd->kernel_slice, width, height, CLARG(dev_in), CLARG(dev_out), CLARG(slots), CLARG(keys), CLARG(values[src]), CLARG(params), CLARG(width), CLARG(height), CLARG(cap), CLARG(entries)); if(err == CL_SUCCESS) err = dt_opencl_read_buffer_from_device(devid, cnt, counters, 0, sizeof(cnt), TRUE); if(err != CL_SUCCESS || cnt[2] == 0) break; dt_print(DT_DEBUG_OPENCL, "[bilateral] %u vertices lost their slot, inserting again", cnt[2]); // the next round starts with no missing vertices cnt[2] = 0; err = dt_opencl_write_buffer_to_device(devid, cnt, counters, 0, sizeof(cnt), TRUE); if(round == 7) err = DT_OPENCL_PROCESS_CL; } cleanup: dt_opencl_release_mem_object(values[0]); dt_opencl_release_mem_object(values[1]); dt_opencl_release_mem_object(params); dt_opencl_release_mem_object(counters); dt_opencl_release_mem_object(keys); dt_opencl_release_mem_object(slots); if(!overflow) break; dt_print(DT_DEBUG_OPENCL, "[bilateral] lattice outgrew its estimate, retrying with %zu entries", max_entries); err = DT_OPENCL_PROCESS_CL; } return err; } int process_cl(dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, cl_mem dev_in, cl_mem dev_out, const dt_iop_roi_t *const roi_in, const dt_iop_roi_t *const roi_out) { const dt_iop_bilateral_global_data_t *gd = (dt_iop_bilateral_global_data_t *)self->global_data; if(piece->colors != 4) return DT_OPENCL_PROCESS_CL; if(roi_in->width != roi_out->width || roi_in->height != roi_out->height) return DT_OPENCL_PROCESS_CL; const int devid = piece->pipe->devid; const int width = roi_out->width; const int height = roi_out->height; if((size_t)width * height > INT_MAX) return DT_OPENCL_PROCESS_CL; dt_iop_bilateral_data_t *data = (dt_iop_bilateral_data_t *)piece->data; float sigma[5]; _compute_sigmas(sigma, data, roi_in->scale, piece->iscale); // the branches of process() size_t origin[] = { 0, 0, 0 }; size_t region[] = { (size_t)width, (size_t)height, 1 }; if(fmaxf(sigma[0], sigma[1]) < 0.1f) return dt_opencl_enqueue_copy_image(devid, dev_in, dev_out, origin, origin, region); const int prad = (int)(3.0f * fmaxf(sigma[0], sigma[1]) + 1.0f); const int rad = MIN(prad, MIN(roi_out->width, roi_out->height) - 2 * prad); const gboolean thumb = dt_pipe_is_thumb(piece->pipe); if(rad < 1 || (rad <= MAX_DIRECT_STAMP_RADIUS && thumb)) return dt_opencl_enqueue_copy_image(devid, dev_in, dev_out, origin, origin, region); if(rad <= MAX_DIRECT_STAMP_RADIUS) { const int wd = 2 * rad + 1; float mat[(2 * MAX_DIRECT_STAMP_RADIUS + 1) * (2 * MAX_DIRECT_STAMP_RADIUS + 1)]; float *m = mat + rad * wd + rad; float weight = 0.0f; for(int l = -rad; l <= rad; l++) for(int k = -rad; k <= rad; k++) weight += m[l * wd + k] = expf(-(l * l + k * k) / (2.f * sigma[0] * sigma[0])); for(int l = -rad; l <= rad; l++) for(int k = -rad; k <= rad; k++) m[l * wd + k] /= weight; const dt_aligned_pixel_t isig2col = { 1.0f / (2.0f * sigma[2] * sigma[2]), 1.0f / (2.0f * sigma[3] * sigma[3]), 1.0f / (2.0f * sigma[4] * sigma[4]), 0.0f }; cl_mem dev_m = (cl_mem)dt_opencl_copy_host_to_device_constant(devid, sizeof(float) * wd * wd, mat); cl_int err = DT_OPENCL_DEFAULT_ERROR; if(dev_m) err = dt_opencl_enqueue_kernel_2d_args(devid, gd->kernel_direct, width, height, CLARG(dev_in), CLARG(dev_out), CLARG(dev_m), CLARG(width), CLARG(height), CLARG(rad), CLARG(isig2col)); dt_opencl_release_mem_object(dev_m); return err; } for(int k = 0; k < 5; k++) sigma[k] = 1.0f / sigma[k]; return _lattice_cl(gd, devid, dev_in, dev_out, width, height, roi_in->x, roi_in->y, sigma); } #endif void commit_params(dt_iop_module_t *self, dt_iop_params_t *p1, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece) { dt_iop_bilateral_params_t *p = (dt_iop_bilateral_params_t *)p1; dt_iop_bilateral_data_t *d = (dt_iop_bilateral_data_t *)piece->data; d->sigma[0] = p->radius; d->sigma[1] = p->radius; d->sigma[2] = p->red; d->sigma[3] = p->green; d->sigma[4] = p->blue; } void init_pipe(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece) { piece->data = malloc(sizeof(dt_iop_bilateral_data_t)); } void cleanup_pipe(dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece) { free(piece->data); piece->data = NULL; } void tiling_callback(dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, const dt_iop_roi_t *roi_in, const dt_iop_roi_t *roi_out, dt_develop_tiling_t *tiling) { dt_iop_bilateral_data_t *data = (dt_iop_bilateral_data_t *)piece->data; float sigma[5]; _compute_sigmas(sigma, data, roi_in->scale, piece->iscale); const int prad = (int)(3.0f * fmaxf(sigma[0], sigma[1]) + 1.0f); const int rad = MIN(prad, MIN(roi_out->width, roi_out->height) - 2 * prad); if(rad <= MAX_DIRECT_STAMP_RADIUS) tiling->factor = 2.0f; // direct stamp, no intermediate buffers used else { // permutohedral needs LOTS of memory // start with the fixed memory requirements tiling->factor = 2.0f /*input+output*/ + 52.0f/16.0f /*52 bytes per pixel for ReplayEntry array*/; // now try to estimate the variable needs for the hashtable based // on the current parameters size_t npixels = (size_t)roi_out->height * roi_out->width; size_t grid_points = (roi_out->height/sigma[0]) * (roi_out->width/sigma[1]) / sigma[2] / sigma[3] / sigma[4]; size_t hash_bytes = PermutohedralLattice<5, 4>::estimatedBytes(grid_points, npixels); tiling->factor += (hash_bytes / (16.0f*npixels)); // the OpenCL lattice: two slot words per estimated entry and twice as // many slots, three key words and two float4 values per entry const size_t entries = PermutohedralLattice<5, 4>::estimatedHashEntries(grid_points, npixels); tiling->factor_cl = 2.0f + (entries * (2 * 8 + 12 + 32)) / (16.0f * npixels); dt_print(DT_DEBUG_MEMORY, "[bilateral tiling requirements] " "tiling factor=%f, npixels=%lu, estimated hashbytes=%lu", tiling->factor, npixels, hash_bytes); } tiling->overhead = 0; tiling->overlap = rad; tiling->align = 1; tiling->maxbuf = 1.0f; } void init_global(dt_iop_module_so_t *self) { const int program = 50; // permutohedral.cl, from programs.conf dt_iop_bilateral_global_data_t *gd = (dt_iop_bilateral_global_data_t *)malloc(sizeof(dt_iop_bilateral_global_data_t)); self->data = gd; gd->kernel_insert = dt_opencl_create_kernel(program, "permutohedral_insert"); gd->kernel_splat = dt_opencl_create_kernel(program, "permutohedral_splat"); gd->kernel_blur = dt_opencl_create_kernel(program, "permutohedral_blur"); gd->kernel_slice = dt_opencl_create_kernel(program, "permutohedral_slice"); gd->kernel_direct = dt_opencl_create_kernel(program, "bilateral_direct"); } void cleanup_global(dt_iop_module_so_t *self) { dt_iop_bilateral_global_data_t *gd = (dt_iop_bilateral_global_data_t *)self->data; dt_opencl_free_kernel(gd->kernel_insert); dt_opencl_free_kernel(gd->kernel_splat); dt_opencl_free_kernel(gd->kernel_blur); dt_opencl_free_kernel(gd->kernel_slice); dt_opencl_free_kernel(gd->kernel_direct); free(self->data); self->data = NULL; } void gui_init(dt_iop_module_t *self) { dt_iop_bilateral_gui_data_t *g = IOP_GUI_ALLOC(bilateral); g->radius = dt_bauhaus_slider_from_params(self, N_("radius")); gtk_widget_set_tooltip_text(g->radius, _("spatial extent of the gaussian")); dt_bauhaus_slider_set_soft_range(g->radius, 1.0, 30.0); g->red = dt_bauhaus_slider_from_params(self, N_("red")); gtk_widget_set_tooltip_text(g->red, _("how much to blur red")); dt_bauhaus_slider_set_soft_max(g->red, 0.1); dt_bauhaus_slider_set_digits(g->red, 4); g->green = dt_bauhaus_slider_from_params(self, N_("green")); gtk_widget_set_tooltip_text(g->green, _("how much to blur green")); dt_bauhaus_slider_set_soft_max(g->green, 0.1); dt_bauhaus_slider_set_digits(g->green, 4); g->blue = dt_bauhaus_slider_from_params(self, N_("blue")); gtk_widget_set_tooltip_text(g->blue, _("how much to blur blue")); dt_bauhaus_slider_set_soft_max(g->blue, 0.1); dt_bauhaus_slider_set_digits(g->blue, 4); } G_END_DECLS // clang-format off // modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py // vim: shiftwidth=2 expandtab tabstop=2 cindent // kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified; // clang-format on