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