Changeset View
Changeset View
Standalone View
Standalone View
source/blender/imbuf/intern/filter.c
| /* SPDX-License-Identifier: GPL-2.0-or-later | /* SPDX-License-Identifier: GPL-2.0-or-later | ||||
| * Copyright 2001-2002 NaN Holding BV. All rights reserved. */ | * Copyright 2001-2002 NaN Holding BV. All rights reserved. */ | ||||
| /** \file | /** \file | ||||
| * \ingroup imbuf | * \ingroup imbuf | ||||
| */ | */ | ||||
| #include <math.h> | #include <math.h> | ||||
| #include "MEM_guardedalloc.h" | #include "MEM_guardedalloc.h" | ||||
| #include "BLI_math_base.h" | #include "BLI_math_base.h" | ||||
| #include "BLI_math_color.h" | |||||
| #include "BLI_math_vector.h" | |||||
| #include "BLI_utildefines.h" | #include "BLI_utildefines.h" | ||||
| #include "IMB_filter.h" | #include "IMB_filter.h" | ||||
| #include "IMB_imbuf.h" | #include "IMB_imbuf.h" | ||||
| #include "IMB_imbuf_types.h" | #include "IMB_imbuf_types.h" | ||||
| #include "imbuf.h" | #include "imbuf.h" | ||||
| ▲ Show 20 Lines • Show All 184 Lines • ▼ Show 20 Lines | |||||
| if (ibuf->rect) { | if (ibuf->rect) { | ||||
| IMB_unpremultiply_rect(ibuf->rect, ibuf->planes, ibuf->x, ibuf->y); | IMB_unpremultiply_rect(ibuf->rect, ibuf->planes, ibuf->x, ibuf->y); | ||||
| } | } | ||||
| if (ibuf->rect_float) { | if (ibuf->rect_float) { | ||||
| IMB_unpremultiply_rect_float(ibuf->rect_float, ibuf->channels, ibuf->x, ibuf->y); | IMB_unpremultiply_rect_float(ibuf->rect_float, ibuf->channels, ibuf->x, ibuf->y); | ||||
| } | } | ||||
| } | } | ||||
| static void imb_filter_convolve(const ImBuf *source, | |||||
| ImBuf *dest, | |||||
| const float *kernel, | |||||
| int kernel_width) | |||||
| { | |||||
| unsigned char *input = (unsigned char *)source->rect; | |||||
| unsigned char *output = (unsigned char *)dest->rect; | |||||
| BLI_assert(input && output); | |||||
| /* Note: This is not particularly optimized. */ | |||||
| int width = source->x; | |||||
| int height = source->y; | |||||
| int stride = source->x; | |||||
| /* Vertical convolution step. */ | |||||
| float *temp_row_v = MEM_mallocN(4 * width * sizeof(float), __func__); | |||||
| for (int y = 0; y < height; y++) { | |||||
| memset(temp_row_v, 0, 4 * width * sizeof(float)); | |||||
| for (int dy = max_ii(-kernel_width + 1, -y); dy < min_ii(kernel_width, height - y); dy++) { | |||||
| for (int x = 0; x < width; x++) { | |||||
| float pixel[4]; | |||||
| srgb_to_linearrgb_uchar4(pixel, &input[4 * ((y + dy) * stride + x)]); | |||||
| madd_v4_v4fl(temp_row_v + 4 * x, pixel, kernel[abs(dy)]); | |||||
| } | |||||
| } | |||||
| for (int x = 0; x < width; x++) { | |||||
| linearrgb_to_srgb_uchar4(&output[4 * (y * stride + x)], temp_row_v + 4 * x); | |||||
| } | |||||
| } | |||||
| MEM_freeN(temp_row_v); | |||||
| /* Horizontal convolution step. */ | |||||
| unsigned char *temp_row_h = MEM_mallocN(4 * width * sizeof(unsigned char), __func__); | |||||
| for (int y = 0; y < height; y++) { | |||||
| for (int x = 0; x < width; x++) { | |||||
| float accum[4], pixel[4]; | |||||
| zero_v4(accum); | |||||
| for (int dx = max_ii(-kernel_width + 1, -x); dx < min_ii(kernel_width, width - x); dx++) { | |||||
| srgb_to_linearrgb_uchar4(pixel, &output[4 * (y * stride + x + dx)]); | |||||
| madd_v4_v4fl(accum, pixel, kernel[abs(dx)]); | |||||
| } | |||||
| linearrgb_to_srgb_uchar4(&temp_row_h[4 * x], accum); | |||||
| } | |||||
| memcpy(output + 4 * y * stride, temp_row_h, 4 * width * sizeof(unsigned char)); | |||||
| } | |||||
| MEM_freeN(temp_row_h); | |||||
| } | |||||
| void IMB_filter_gaussian(const struct ImBuf *source, struct ImBuf *dest, float sigma) | |||||
| { | |||||
| int w = 3 * ((int)sigma); | |||||
| float *kernel = MEM_mallocN(sizeof(float) * w, __func__); | |||||
| /* Compute gaussian kernel. */ | |||||
| float fac1 = 0.5f / (sigma * sigma), fac2 = 1.0f / (sqrtf(2.0f * M_PI) * sigma); | |||||
| float scale = 0.0f; | |||||
| for (int i = 0; i < w; i++) { | |||||
| kernel[i] = fac2 * expf(-i * i * fac1); | |||||
| scale += kernel[i]; | |||||
| if (i > 0) { | |||||
| scale += kernel[i]; | |||||
| } | |||||
| } | |||||
| /* Normalize kernel. */ | |||||
| scale = 1.0f / scale; | |||||
| for (int i = 0; i < w; i++) { | |||||
| kernel[i] *= scale; | |||||
| } | |||||
| imb_filter_convolve(source, dest, kernel, w); | |||||
| MEM_freeN(kernel); | |||||
| } | |||||
| Context not available. | |||||