Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
common.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, 2020 Tobias Ellinghaus.
5 * Copyright (C) 2020 Pascal Obry.
6 * Copyright (C) 2021 Sakari Kapanen.
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 "chart/common.h"
25
26// using SVD to solve the system with h[8] also being 0 would be better, but this seems to be good enough
27int get_homography(const point_t *source, const point_t *target, float *h)
28{
29 // Use double precision internally when solving for the homography
30 // to avoid numerical instabilities.
31 const double x1 = source[0].x;
32 const double y1 = source[0].y;
33 const double x2 = source[1].x;
34 const double y2 = source[1].y;
35 const double x3 = source[2].x;
36 const double y3 = source[2].y;
37 const double x4 = source[3].x;
38 const double y4 = source[3].y;
39
40 const double x_1 = target[0].x;
41 const double y_1 = target[0].y;
42 const double x_2 = target[1].x;
43 const double y_2 = target[1].y;
44 const double x_3 = target[2].x;
45 const double y_3 = target[2].y;
46 const double x_4 = target[3].x;
47 const double y_4 = target[3].y;
48
49 double P[9*9] = { -x1, -y1, -1.0, 0.0, 0.0, 0.0, x1 * x_1, y1 * x_1, x_1,
50 0.0, 0.0, 0.0, -x1, -y1, -1.0, x1 * y_1, y1 * y_1, y_1,
51 -x2, -y2, -1.0, 0.0, 0.0, 0.0, x2 * x_2, y2 * x_2, x_2,
52 0.0, 0.0, 0.0, -x2, -y2, -1.0, x2 * y_2, y2 * y_2, y_2,
53 -x3, -y3, -1.0, 0.0, 0.0, 0.0, x3 * x_3, y3 * x_3, x_3,
54 0.0, 0.0, 0.0, -x3, -y3, -1.0, x3 * y_3, y3 * y_3, y_3,
55 -x4, -y4, -1.0, 0.0, 0.0, 0.0, x4 * x_4, y4 * x_4, x_4,
56 0.0, 0.0, 0.0, -x4, -y4, -1.0, x4 * y_4, y4 * y_4, y_4,
57 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0};
58
59 double h_tmp[9];
60 for(int i = 0; i < 8; i++) h_tmp[i] = 0.0;
61 h_tmp[8] = 1.0;
62
63 int err_code = gauss_solve(P, h_tmp, 9);
64 if(err_code) for(int i = 0; i < 9; i++) h[i] = h_tmp[i];
65 return err_code;
66}
67
69{
70 const float s = p.x * h[2 * 3 + 0] + p.y * h[2 * 3 + 1] + h[2 * 3 + 2];
71 const float x = (p.x * h[0 * 3 + 0] + p.y * h[0 * 3 + 1] + h[0 * 3 + 2]) / s;
72 const float y = (p.x * h[1 * 3 + 0] + p.y * h[1 * 3 + 1] + h[1 * 3 + 2]) / s;
73
74 const point_t result = {.x=x, .y=y};
75
76 return result;
77}
78
79float apply_homography_scaling(point_t p, const float *h)
80{
81 // The local scaling of areas by the homography mapping is given by
82 // the absolute value of its Jacobian determinant at point p.
83 const float x = p.x * h[0 * 3 + 0] + p.y * h[0 * 3 + 1] + h[0 * 3 + 2];
84 const float y = p.x * h[1 * 3 + 0] + p.y * h[1 * 3 + 1] + h[1 * 3 + 2];
85 const float s = p.x * h[2 * 3 + 0] + p.y * h[2 * 3 + 1] + h[2 * 3 + 2];
86
87 // Components of the Jacobian matrix, without division by s^2, which is factored
88 // out and done for the whole determinant.
89 const float J00 = h[0 * 3 + 0] * s - h[2 * 3 + 0] * x;
90 const float J01 = h[0 * 3 + 1] * s - h[2 * 3 + 1] * x;
91 const float J10 = h[1 * 3 + 0] * s - h[2 * 3 + 0] * y;
92 const float J11 = h[1 * 3 + 1] * s - h[2 * 3 + 1] * y;
93 const float s2 = s * s;
94 return fabsf(J00 * J11 - J01 * J10) / (s2 * s2);
95}
96
97// modified;
98// clang-format off
99// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
100// vim: shiftwidth=2 expandtab tabstop=2 cindent
101// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
102// clang-format on
103
static const dt_aligned_pixel_simd_t const dt_adaptation_t const float p
#define P(V, params)
point_t apply_homography(point_t p, const float *h)
Definition common.c:68
float apply_homography_scaling(point_t p, const float *h)
Definition common.c:79
int get_homography(const point_t *source, const point_t *target, float *h)
Definition common.c:27
static int gauss_solve(double *A, double *b, int n)
static const float x
float y
Definition colorchart.h:33
float x
Definition colorchart.h:33