Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
chart/tonecurve.c
Go to the documentation of this file.
1/*
2 * This file is part of darktable,
3 * Copyright (C) 2016 johannes hanika.
4 * Copyright (C) 2016 Roman Lebedev.
5 * Copyright (C) 2016 Tobias Ellinghaus.
6 * Copyright (C) 2020 Pascal Obry.
7 * Copyright (C) 2022 Martin Baƙinka.
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#include "common/darktable.h"
24#include "chart/tonecurve.h"
25
26#include <assert.h>
27#include <stdint.h>
28#include <stdlib.h>
29
30// apply and undo a tone curve (L channel only),
31// created from the 24 grey input patches from the it8.
32
33void tonecurve_create(tonecurve_t *c, double *Lin, double *Lout, const int32_t num)
34{
35 c->num = num;
36 c->x = Lin;
37 c->y = Lout;
38}
39
41{
42 if(IS_NULL_PTR(c)) return;
43
44 dt_free(c->y);
45 dt_free(c->x);
46}
47
48static inline double _tonecurve_apply(const double *x, const double *y, const int32_t num, const double L)
49{
50 if(L <= 0.0 || L >= 100.0) return L;
51 uint32_t min = 0, max = num;
52 uint32_t t = max / 2;
53 while(t != min)
54 {
55 if(x[t] <= L)
56 min = t;
57 else
58 max = t;
59 t = (min + max) / 2;
60 }
61 assert(t < num);
62 // last step: decide between min and max one more time (min is rounding default),
63 // but consider that if max is still out of bounds, it's invalid.
64 // (L == 1.0 and a x[0]=1, num=1 would break otherwise)
65 if(max < num && x[max] <= L) t = max;
66 const double f = (x[t + 1] - x[t] > 1e-6f) ? (L - x[t]) / (x[t + 1] - x[t]) : 1.0f;
67 if(t == num - 1) return y[t];
68 assert(x[t] <= L);
69 assert(x[t + 1] >= L);
70 return y[t + 1] * f + y[t] * (1.0f - f);
71}
72
73double tonecurve_apply(const tonecurve_t *c, const double L)
74{
75 return _tonecurve_apply(c->x, c->y, c->num, L);
76}
77
78double tonecurve_unapply(const tonecurve_t *c, const double L)
79{
80 return _tonecurve_apply(c->y, c->x, c->num, L);
81}
82
83// clang-format off
84// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
85// vim: shiftwidth=2 expandtab tabstop=2 cindent
86// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
87// clang-format on
88
static double _tonecurve_apply(const double *x, const double *y, const int32_t num, const double L)
double tonecurve_apply(const tonecurve_t *c, const double L)
double tonecurve_unapply(const tonecurve_t *c, const double L)
void tonecurve_delete(tonecurve_t *c)
void tonecurve_create(tonecurve_t *c, double *Lin, double *Lout, const int32_t num)
const dt_aligned_pixel_t f
static const float const float const float min
const float max
#define dt_free(ptr)
Definition darktable.h:456
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
Definition darktable.h:281
static const float x
const int t