Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
dtcairo.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-2017, 2020 Tobias Ellinghaus.
5 * Copyright (C) 2019 Andreas Schneider.
6 * Copyright (C) 2020-2021 Pascal Obry.
7 * Copyright (C) 2021 Ralf Brown.
8 * Copyright (C) 2021 Sakari Kapanen.
9 * Copyright (C) 2022 Martin Baƙinka.
10 *
11 * darktable is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * darktable is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with darktable. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25#include "chart/dtcairo.h"
26#include "chart/common.h"
27
28void draw_no_image(cairo_t *cr, GtkWidget *widget)
29{
30 guint width = gtk_widget_get_allocated_width(widget);
31 guint height = gtk_widget_get_allocated_height(widget);
32 cairo_set_line_width(cr, 5);
33 cairo_set_source_rgb(cr, 1, 0, 0);
34 cairo_move_to(cr, 0, 0);
35 cairo_line_to(cr, width, height);
36 cairo_move_to(cr, width, 0);
37 cairo_line_to(cr, 0, height);
38 cairo_stroke(cr);
39}
40
41void draw_line(cairo_t *cr, point_t start, point_t end)
42{
43 cairo_move_to(cr, start.x, start.y);
44 cairo_line_to(cr, end.x, end.y);
45}
46
47void draw_cross(cairo_t *cr, point_t center)
48{
49 cairo_move_to(cr, center.x - 10, center.y);
50 cairo_line_to(cr, center.x + 10, center.y);
51 cairo_move_to(cr, center.x, center.y - 10);
52 cairo_line_to(cr, center.x, center.y + 10);
53}
54
55void draw_box(cairo_t *cr, box_t box, const float *homography)
56{
57 point_t p[4];
59 p[TOP_RIGHT].x += box.w;
60 p[BOTTOM_RIGHT].x += box.w;
61 p[BOTTOM_RIGHT].y += box.h;
62 p[BOTTOM_LEFT].y += box.h;
63
64 for(int i = 0; i < 4; i++) p[i] = apply_homography(p[i], homography);
65
66 // cairo_new_sub_path(cr);
67 cairo_move_to(cr, p[TOP_LEFT].x, p[TOP_LEFT].y);
68 for(int i = 1; i < 4; i++)
69 {
70 point_t corner = p[i];
71 cairo_line_to(cr, corner.x, corner.y);
72 }
73 cairo_close_path(cr);
74}
75
76void clear_background(cairo_t *cr)
77{
78 cairo_set_source_rgb(cr, 0, 0, 0);
79 cairo_paint(cr);
80}
81
82void center_image(cairo_t *cr, image_t *image)
83{
84 cairo_translate(cr, image->offset_x, image->offset_y);
85}
86
87void draw_image(cairo_t *cr, image_t *image)
88{
89 cairo_set_source(cr, image->image);
90 cairo_paint(cr);
91}
92
93void draw_boundingbox(cairo_t *cr, point_t *bb)
94{
95 for(int i = 0; i < 4; i++) draw_line(cr, bb[i], bb[(i + 1) % 4]);
96}
97
98void draw_f_boxes(cairo_t *cr, const float *homography, chart_t *chart)
99{
100 for(GList *iter = chart->f_list; iter; iter = g_list_next(iter))
101 {
102 f_line_t *f = iter->data;
103 for(int i = 0; i < 4; i++)
104 {
106 draw_cross(cr, p);
107 }
108 }
109}
110
111static void _draw_boxes(cairo_t *cr, const float *homography, GHashTable *table)
112{
113 GHashTableIter table_iter;
114 gpointer key, value;
115
116 g_hash_table_iter_init(&table_iter, table);
117 while(g_hash_table_iter_next(&table_iter, &key, &value))
118 {
119 box_t *box = (box_t *)value;
120 draw_box(cr, *box, homography);
121 }
122}
123
124void draw_d_boxes(cairo_t *cr, const float *homography, chart_t *chart)
125{
126 _draw_boxes(cr, homography, chart->d_table);
127}
128
129void draw_color_boxes_outline(cairo_t *cr, const float *homography, chart_t *chart)
130{
131 _draw_boxes(cr, homography, chart->box_table);
132}
133
134void draw_color_boxes_inside(cairo_t *cr, const float *homography, chart_t *chart, float shrink, float line_width,
135 gboolean colored)
136{
137 GHashTableIter table_iter;
138 gpointer key, value;
139
140 float x_shrink = shrink * chart->box_shrink / chart->bb_w, y_shrink = shrink * chart->box_shrink / chart->bb_h;
141
142 cairo_set_line_width(cr, line_width);
143 cairo_set_source_rgb(cr, 1.0, 1.0, 1.0);
144
145 g_hash_table_iter_init(&table_iter, chart->box_table);
146 while(g_hash_table_iter_next(&table_iter, &key, &value))
147 {
148 box_t *box = (box_t *)value;
149 box_t inner_box = *box;
150 inner_box.p.x += x_shrink;
151 inner_box.p.y += y_shrink;
152 inner_box.w -= 2.0 * x_shrink;
153 inner_box.h -= 2.0 * y_shrink;
154 draw_box(cr, inner_box, homography);
155
156 if(colored) cairo_set_source_rgb(cr, box->rgb[0], box->rgb[1], box->rgb[2]);
157
158 cairo_stroke(cr);
159 }
160}
161
162void stroke_boxes(cairo_t *cr, float line_width)
163{
164 cairo_set_line_width(cr, line_width * 2.5);
165 cairo_set_source_rgb(cr, 1, 1, 1);
166 cairo_stroke_preserve(cr);
167
168 cairo_set_line_width(cr, line_width);
169 cairo_set_source_rgb(cr, 0, 0, 0);
170 cairo_stroke(cr);
171}
172
173void set_offset_and_scale(image_t *image, float width, float height)
174{
175 if(IS_NULL_PTR(image->image)) return;
176
177 cairo_matrix_t matrix;
178 const float s_w = (float)image->width / width;
179 const float s_h = (float)image->height / height;
180 image->scale = MAX(s_w, s_h);
181 cairo_matrix_init_scale(&matrix, image->scale, image->scale);
182 cairo_pattern_set_matrix(image->image, &matrix);
183
184 image->offset_x = (width - (image->width / image->scale)) / 2.0 + 0.5;
185 image->offset_y = (height - (image->height / image->scale)) / 2.0 + 0.5;
186}
187
188static cairo_user_data_key_t source_data_buffer_key;
189
190cairo_surface_t *cairo_surface_create_from_xyz_data(const float *const image, const int width, const int height)
191{
192 unsigned char *rgbbuf = (unsigned char *)malloc(sizeof(unsigned char) * height * width * 4);
194 for(int y = 0; y < height; y++)
195 {
196 const float *iter = image + y * width * 3;
197 for(int x = 0; x < width; x++, iter += 3)
198 {
200 int32_t pixel = 0;
201 dt_XYZ_to_sRGB_clipped(iter, sRGB);
202 for(int c = 0; c < 3; c++) pixel |= ((int)(sRGB[c] * 255) & 0xff) << (16 - c * 8);
203 *((int *)(&rgbbuf[(x + (size_t)y * width) * 4])) = pixel;
204 }
205 }
206
207 cairo_format_t format = CAIRO_FORMAT_RGB24;
208 const int stride = cairo_format_stride_for_width(format, width);
209 cairo_surface_t *surface = cairo_image_surface_create_for_data(rgbbuf, format, width, height, stride);
210 cairo_surface_set_user_data(surface, &source_data_buffer_key, rgbbuf, free);
211
212 return surface;
213}
214
215// clang-format off
216// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
217// vim: shiftwidth=2 expandtab tabstop=2 cindent
218// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
219// clang-format on
220
static __DT_CLONE_TARGETS__ void homography(float *homograph, const float angle, const float shift_v, const float shift_h, const float shear, const float f_length_kb, const float orthocorr, const float aspect, const int width, const int height, dt_iop_ashift_homodir_t dir)
Definition ashift.c:755
int width
Definition bilateral.h:1
int height
Definition bilateral.h:1
static const dt_aligned_pixel_simd_t const dt_adaptation_t const float p
const dt_aligned_pixel_t f
static dt_aligned_pixel_t sRGB
const dt_colormatrix_t matrix
char * key
point_t apply_homography(point_t p, const float *h)
Definition common.c:68
#define __OMP_PARALLEL_FOR__(...)
Definition darktable.h:258
static const dt_aligned_pixel_simd_t value
Definition darktable.h:577
#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
void draw_image(cairo_t *cr, image_t *image)
Definition dtcairo.c:87
void draw_color_boxes_outline(cairo_t *cr, const float *homography, chart_t *chart)
Definition dtcairo.c:129
void draw_box(cairo_t *cr, box_t box, const float *homography)
Definition dtcairo.c:55
static cairo_user_data_key_t source_data_buffer_key
Definition dtcairo.c:188
void draw_cross(cairo_t *cr, point_t center)
Definition dtcairo.c:47
void draw_no_image(cairo_t *cr, GtkWidget *widget)
Definition dtcairo.c:28
void center_image(cairo_t *cr, image_t *image)
Definition dtcairo.c:82
void stroke_boxes(cairo_t *cr, float line_width)
Definition dtcairo.c:162
void clear_background(cairo_t *cr)
Definition dtcairo.c:76
void draw_color_boxes_inside(cairo_t *cr, const float *homography, chart_t *chart, float shrink, float line_width, gboolean colored)
Definition dtcairo.c:134
void draw_line(cairo_t *cr, point_t start, point_t end)
Definition dtcairo.c:41
cairo_surface_t * cairo_surface_create_from_xyz_data(const float *const image, const int width, const int height)
Definition dtcairo.c:190
void draw_f_boxes(cairo_t *cr, const float *homography, chart_t *chart)
Definition dtcairo.c:98
void draw_boundingbox(cairo_t *cr, point_t *bb)
Definition dtcairo.c:93
void draw_d_boxes(cairo_t *cr, const float *homography, chart_t *chart)
Definition dtcairo.c:124
static void _draw_boxes(cairo_t *cr, const float *homography, GHashTable *table)
Definition dtcairo.c:111
void set_offset_and_scale(image_t *image, float width, float height)
Definition dtcairo.c:173
static const float x
float dt_aligned_pixel_t[4]
struct _GtkWidget GtkWidget
Definition splash.h:29
@ BOTTOM_RIGHT
@ TOP_LEFT
@ BOTTOM_LEFT
@ TOP_RIGHT
float h
Definition colorchart.h:45
float w
Definition colorchart.h:45
dt_aligned_pixel_t rgb
Definition colorchart.h:49
point_t p
Definition colorchart.h:44
float bb_w
Definition colorchart.h:63
float bb_h
Definition colorchart.h:63
float box_shrink
Definition colorchart.h:65
GHashTable * box_table
Definition colorchart.h:57
GHashTable * d_table
Definition colorchart.h:57
GList * f_list
Definition colorchart.h:55
cairo_pattern_t * image
float y
Definition colorchart.h:33
float x
Definition colorchart.h:33
#define MAX(a, b)
Definition thinplate.c:29