18 if (
size < 1) size_ = 1;
19 if (size_ % 2 == 0) size_ += 1;
20 values_.resize(
static_cast<size_t>(size_ * size_), 0.0f);
24 void set(
int row,
int col,
float value) {
25 values_[
static_cast<size_t>(row * size_ + col)] = value;
29 float get(
int row,
int col)
const {
30 return values_[
static_cast<size_t>(row * size_ + col)];
34 int size()
const {
return size_; }
37 void apply(uint8_t* pixels,
int width,
int height,
int stride = 0)
const {
38 if (!pixels || width <= 0 || height <= 0 || size_ < 1)
return;
39 if (stride == 0) stride = width * 4;
42 std::vector<uint8_t> temp(pixels, pixels +
static_cast<size_t>(height * stride));
44 for (
int y = 0; y < height; ++y) {
45 for (
int x = 0; x < width; ++x) {
46 float r = 0, g = 0, b = 0;
48 for (
int ky = -half; ky <= half; ++ky) {
49 for (
int kx = -half; kx <= half; ++kx) {
50 int sy = std::clamp(y + ky, 0, height - 1);
51 int sx = std::clamp(x + kx, 0, width - 1);
52 float k = values_[
static_cast<size_t>((ky + half) * size_ + (kx + half))];
54 int idx = sy * stride + sx * 4;
55 r +=
static_cast<float>(pixels[idx]) * k;
56 g +=
static_cast<float>(pixels[idx + 1]) * k;
57 b +=
static_cast<float>(pixels[idx + 2]) * k;
61 int out_idx = y * stride + x * 4;
62 temp[out_idx] =
static_cast<uint8_t
>(std::clamp(r, 0.0f, 255.0f));
63 temp[out_idx + 1] =
static_cast<uint8_t
>(std::clamp(g, 0.0f, 255.0f));
64 temp[out_idx + 2] =
static_cast<uint8_t
>(std::clamp(b, 0.0f, 255.0f));
65 temp[out_idx + 3] = pixels[out_idx + 3];
69 std::copy(temp.begin(), temp.end(), pixels);
77 float v[] = {1, 2, 1, 2, 4, 2, 1, 2, 1};
78 for (
int i = 0; i < 9; ++i) k.values_[i] = v[i] / 16.0f;
92 for (
int i = 0; i < 25; ++i) k.values_[i] = v[i] / 256.0f;
99 float v[] = {0, -1, 0, -1, 5, -1, 0, -1, 0};
100 for (
int i = 0; i < 9; ++i) k.values_[i] = v[i];
107 float v[] = {-1, -1, -1, -1, 8, -1, -1, -1, -1};
108 for (
int i = 0; i < 9; ++i) k.values_[i] = v[i];
115 float v[] = {-2, -1, 0, -1, 1, 1, 0, 1, 2};
116 for (
int i = 0; i < 9; ++i) k.values_[i] = v[i];
122 std::vector<float> values_;
float get(int row, int col) const
Get a kernel value.
Definition image_convolution.hpp:29
static ImageConvolutionKernel gaussian_blur_5()
Gaussian blur (5x5).
Definition image_convolution.hpp:83
static ImageConvolutionKernel edge_detect()
Edge detection (Sobel-like, 3x3).
Definition image_convolution.hpp:105
void apply(uint8_t *pixels, int width, int height, int stride=0) const
Apply the kernel to RGBA image data (in-place).
Definition image_convolution.hpp:37
static ImageConvolutionKernel gaussian_blur_3()
Gaussian blur (3x3).
Definition image_convolution.hpp:75
static ImageConvolutionKernel emboss()
Emboss (3x3).
Definition image_convolution.hpp:113
static ImageConvolutionKernel sharpen()
Sharpen kernel (3x3).
Definition image_convolution.hpp:97
ImageConvolutionKernel(int size)
Create an NxN kernel (must be odd: 3, 5, 7, etc.).
Definition image_convolution.hpp:17
void set(int row, int col, float value)
Set a kernel value at (row, col).
Definition image_convolution.hpp:24
int size() const
Kernel size.
Definition image_convolution.hpp:34
Definition affine_transform.hpp:24