Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
imagebuf.h
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2020-2021 Ralf Brown.
4 Copyright (C) 2021 Pascal Obry.
5 Copyright (C) 2022, 2025-2026 Aurélien PIERRE.
6 Copyright (C) 2022 Martin Bařinka.
7 Copyright (C) 2025 Alynx Zhou.
8
9 darktable is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 3 of the License, or
12 (at your option) any later version.
13
14 darktable is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with darktable. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#pragma once
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
28#include "develop/imageop.h" // for dt_iop_roi_t
29
30#ifdef __cplusplus
31extern "C" {
32#endif
33
34// Allocate a 64-byte aligned buffer for an image of the given dimensions and channels.
35// The return value must be freed with dt_pixelpipe_cache_free_align().
36static inline float *__restrict__ dt_iop_image_alloc(const size_t width, const size_t height, const size_t ch)
37{
39}
40
41// Allocate one or more buffers as detailed in the given parameters. If any allocation fails, free all of them,
42// set the module's trouble flag, and return FALSE.
43// The variable arguments take the form SIZE, PTR-to-floatPTR, SIZE, PTR-to-floatPTR, etc. except that if the SIZE
44// indicates a per-thread allocation, a second pointer is passed: SIZE, PTR-to-floatPTR, PTR-to-size_t, SIZE, etc.
45// SIZE is the number of floats per pixel, ORed with appropriate flags from the list following below
46int dt_iop_alloc_image_buffers(struct dt_iop_module_t *const module,
47 const struct dt_iop_roi_t *const roi_in,
48 const struct dt_iop_roi_t *const roi_out, ...);
49// Optional flags to add to size request. Default is to allocate N channels per pixel according to
50// the dimensions of roi_out
51#define DT_IMGSZ_CH_MASK 0x000FFFF // isolate just the number of floats per pixel
52
53#define DT_IMGSZ_ROI_MASK 0x0100000 // isolate just input/output selection
54#define DT_IMGSZ_OUTPUT 0x0000000 // read image dimensions from roi_out
55#define DT_IMGSZ_INPUT 0x0100000 // read image dimensions from roi_in
56
57#define DT_IMGSZ_PERTHREAD 0x0200000 // allocate a separate buffer for each thread
58#define DT_IMGSZ_CLEARBUF 0x0400000 // zero the allocated buffer
59
60#define DT_IMGSZ_DIM_MASK 0x00F0000 // isolate the requested image dimension(s)
61#define DT_IMGSZ_FULL 0x0000000 // full height times width
62#define DT_IMGSZ_HEIGHT 0x0010000 // buffer equal to one column of the image
63#define DT_IMGSZ_WIDTH 0x0020000 // buffer equal to one row of the image
64#define DT_IMGSZ_LONGEST 0x0030000 // buffer equal to larger of one row/one column
65
66
68static inline void dt_simd_memcpy(const float *const __restrict__ in,
69 float *const __restrict__ out,
70 const size_t num_elem)
71{
72 // Perform a parallel vectorized memcpy on 64-bits aligned
73 // contiguous buffers. This is several times faster than the original memcpy
74
75#ifdef _OPENMP
76#pragma omp parallel for simd default(none) \
77dt_omp_firstprivate(in, out, num_elem) \
78schedule(simd:static) aligned(in, out:64)
79#endif
80 for(size_t k = 0; k < num_elem; k++)
81 out[k] = in[k];
82}
83
84// Copy an image buffer, specifying the number of floats it contains. Use of this function is to be preferred
85// over a bare memcpy both because it helps document the purpose of the code and because it gives us a single
86// point where we can optimize performance on different architectures.
87void dt_iop_image_copy(float *const __restrict__ out, const float *const __restrict__ in, const size_t nfloats);
88
89// Copy an image buffer, specifying its dimensions and number of channels. Use of this function is to be
90// preferred over a bare memcpy both because it helps document the purpose of the code and because it gives us
91// a single point where we can optimize performance on different architectures.
92static inline void dt_iop_image_copy_by_size(float *const __restrict__ out, const float *const __restrict__ in,
93 const size_t width, const size_t height, const size_t ch)
94{
95 dt_iop_image_copy(out, in, width * height * ch);
96}
97
98// Copy an image buffer, specifying the region of interest. The output RoI may be larger than the input RoI,
99// in which case the result is optionally padded with zeros. If the output RoI is smaller than the input RoI,
100// only a portion of the input buffer will be copied.
101void dt_iop_copy_image_roi(float *const __restrict__ out, const float *const __restrict__ in, const size_t ch,
102 const dt_iop_roi_t *const __restrict__ roi_in,
103 const dt_iop_roi_t *const __restrict__ roi_out, const int zero_pad);
104
105// Copy one image buffer to another, multiplying each element by the specified scale factor
106void dt_iop_image_scaled_copy(float *const __restrict__ buf, const float *const __restrict__ src, const float scale,
107 const size_t width, const size_t height, const size_t ch);
108
109// Fill an image buffer with a specified value.
110void dt_iop_image_fill(float *const buf, const float fill_value, const size_t width, const size_t height,
111 const size_t ch);
112
113// Add a specified constant value to every element of the image buffer
114void dt_iop_image_add_const(float *const buf, const float add_value, const size_t width, const size_t height,
115 const size_t ch);
116
117// Add the contents of another image buffer to the given buffer, element-wise
118void dt_iop_image_add_image(float *const buf, const float *const other_buf, const size_t width, const size_t height,
119 const size_t ch);
120
121// Subtract the contents of another image buffer from the given buffer, element-wise
122void dt_iop_image_sub_image(float *const buf, const float *const other_buf, const size_t width, const size_t height,
123 const size_t ch);
124
125// Subtract each element of the image buffer from the given constant value
126void dt_iop_image_invert(float *const buf, const float max_value, const size_t width, const size_t height,
127 const size_t ch);
128
129// Multiply every element of the image buffer by a specified constant value
130void dt_iop_image_mul_const(float *const buf, const float mul_value, const size_t width, const size_t height,
131 const size_t ch);
132
133// Divide every element of the image buffer by a specified constant value
134void dt_iop_image_div_const(float *const buf, const float div_value, const size_t width, const size_t height,
135 const size_t ch);
136
137// elementwise: buf = lammda*buf + (1-lambda)*other
138void dt_iop_image_linear_blend(float *const __restrict__ buf, const float lambda, const float *const __restrict__ other_buf,
139 const size_t width, const size_t height, const size_t ch);
140
141#ifdef __cplusplus
142}
143#endif
144
145// clang-format off
146// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
147// vim: shiftwidth=2 expandtab tabstop=2 cindent
148// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
149// clang-format on
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
static const dt_colormatrix_t dt_aligned_pixel_t out
Definition colorspaces_inline_conversions.h:184
#define dt_pixelpipe_cache_alloc_align_float_cache(pixels, id)
Definition darktable.h:371
#define __DT_CLONE_TARGETS__
Definition darktable.h:291
void dt_iop_image_div_const(float *const buf, const float div_value, const size_t width, const size_t height, const size_t ch)
Definition imagebuf.c:384
int dt_iop_alloc_image_buffers(struct dt_iop_module_t *const module, const struct dt_iop_roi_t *const roi_in, const struct dt_iop_roi_t *const roi_out,...)
Definition imagebuf.c:31
static __DT_CLONE_TARGETS__ void dt_simd_memcpy(const float *const __restrict__ in, float *const __restrict__ out, const size_t num_elem)
Definition imagebuf.h:68
void dt_iop_image_scaled_copy(float *const __restrict__ buf, const float *const __restrict__ src, const float scale, const size_t width, const size_t height, const size_t ch)
void dt_iop_image_fill(float *const buf, const float fill_value, const size_t width, const size_t height, const size_t ch)
Definition imagebuf.c:216
void dt_iop_image_mul_const(float *const buf, const float mul_value, const size_t width, const size_t height, const size_t ch)
Definition imagebuf.c:358
static void dt_iop_image_copy_by_size(float *const __restrict__ out, const float *const __restrict__ in, const size_t width, const size_t height, const size_t ch)
Definition imagebuf.h:92
void dt_iop_copy_image_roi(float *const __restrict__ out, const float *const __restrict__ in, const size_t ch, const dt_iop_roi_t *const __restrict__ roi_in, const dt_iop_roi_t *const __restrict__ roi_out, const int zero_pad)
Definition imagebuf.c:160
void dt_iop_image_add_image(float *const buf, const float *const other_buf, const size_t width, const size_t height, const size_t ch)
Definition imagebuf.c:280
void dt_iop_image_invert(float *const buf, const float max_value, const size_t width, const size_t height, const size_t ch)
Definition imagebuf.c:332
void dt_iop_image_sub_image(float *const buf, const float *const other_buf, const size_t width, const size_t height, const size_t ch)
Definition imagebuf.c:306
static float *__restrict__ dt_iop_image_alloc(const size_t width, const size_t height, const size_t ch)
Definition imagebuf.h:36
void dt_iop_image_add_const(float *const buf, const float add_value, const size_t width, const size_t height, const size_t ch)
Definition imagebuf.c:254
void dt_iop_image_copy(float *const __restrict__ out, const float *const __restrict__ in, const size_t nfloats)
Definition imagebuf.c:138
void dt_iop_image_linear_blend(float *const __restrict__ buf, const float lambda, const float *const __restrict__ other_buf, const size_t width, const size_t height, const size_t ch)
Definition imageop.h:216
Definition imageop.h:67