-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
develop.c
3565 lines (3065 loc) · 118 KB
/
develop.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of darktable,
Copyright (C) 2009-2023 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <glib/gprintf.h>
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <unistd.h>
#include "common/atomic.h"
#include "common/debug.h"
#include "common/history.h"
#include "common/image_cache.h"
#include "common/mipmap_cache.h"
#include "common/opencl.h"
#include "common/tags.h"
#include "common/presets.h"
#include "control/conf.h"
#include "control/control.h"
#include "control/jobs.h"
#include "develop/blend.h"
#include "develop/develop.h"
#include "develop/imageop.h"
#include "develop/lightroom.h"
#include "develop/masks.h"
#include "gui/gtk.h"
#include "gui/presets.h"
#include "imageio/imageio_common.h"
#ifdef USE_LUA
#include "lua/call.h"
#endif
#define DT_DEV_AVERAGE_DELAY_COUNT 5
void dt_dev_init(dt_develop_t *dev,
const gboolean gui_attached)
{
memset(dev, 0, sizeof(dt_develop_t));
dev->full_preview = FALSE;
dev->gui_module = NULL;
dev->timestamp = 0;
dev->gui_leaving = FALSE;
dev->gui_synch = FALSE;
dt_pthread_mutex_init(&dev->history_mutex, NULL);
dev->snapshot_id = -1;
dev->history_end = 0;
dev->history = NULL; // empty list
dev->history_postpone_invalidate = FALSE;
dev->module_filter_out = NULL;
dev->gui_attached = gui_attached;
dev->full.width = -1;
dev->full.height = -1;
dt_image_init(&dev->image_storage);
dev->history_updating = dev->image_force_reload = FALSE;
dev->autosaving = FALSE;
dev->autosave_time = 0.0;
dev->image_invalid_cnt = 0;
dev->full.pipe = dev->preview_pipe = dev->preview2.pipe = NULL;
dev->histogram_pre_tonecurve = NULL;
dev->histogram_pre_levels = NULL;
dev->forms = NULL;
dev->form_visible = NULL;
dev->form_gui = NULL;
dev->allforms = NULL;
if(dev->gui_attached)
{
dev->full.pipe = (dt_dev_pixelpipe_t *)malloc(sizeof(dt_dev_pixelpipe_t));
dev->preview_pipe = (dt_dev_pixelpipe_t *)malloc(sizeof(dt_dev_pixelpipe_t));
dev->preview2.pipe = (dt_dev_pixelpipe_t *)malloc(sizeof(dt_dev_pixelpipe_t));
dt_dev_pixelpipe_init(dev->full.pipe);
dt_dev_pixelpipe_init_preview(dev->preview_pipe);
dt_dev_pixelpipe_init_preview2(dev->preview2.pipe);
dev->histogram_pre_tonecurve = (uint32_t *)calloc(4 * 256, sizeof(uint32_t));
dev->histogram_pre_levels = (uint32_t *)calloc(4 * 256, sizeof(uint32_t));
// FIXME: these are uint32_t, setting to -1 is confusing
dev->histogram_pre_tonecurve_max = -1;
dev->histogram_pre_levels_max = -1;
dev->darkroom_mouse_in_center_area = FALSE;
dev->darkroom_skip_mouse_events = FALSE;
if(darktable.gui)
{
dev->full.ppd = darktable.gui->ppd;
dev->full.dpi = darktable.gui->dpi;
dev->full.dpi_factor = darktable.gui->dpi_factor;
dev->full.widget = dt_ui_center(darktable.gui->ui);
}
}
dev->iop_instance = 0;
dev->iop = NULL;
dev->alliop = NULL;
dev->allprofile_info = NULL;
dev->iop_order_version = 0;
dev->iop_order_list = NULL;
dev->proxy.exposure.module = NULL;
dt_dev_init_chroma(dev);
dev->rawoverexposed.enabled = FALSE;
dev->rawoverexposed.mode =
dt_conf_get_int("darkroom/ui/rawoverexposed/mode");
dev->rawoverexposed.colorscheme =
dt_conf_get_int("darkroom/ui/rawoverexposed/colorscheme");
dev->rawoverexposed.threshold =
dt_conf_get_float("darkroom/ui/rawoverexposed/threshold");
dev->overexposed.enabled = FALSE;
dev->overexposed.mode = dt_conf_get_int("darkroom/ui/overexposed/mode");
dev->overexposed.colorscheme = dt_conf_get_int("darkroom/ui/overexposed/colorscheme");
dev->overexposed.lower = dt_conf_get_float("darkroom/ui/overexposed/lower");
dev->overexposed.upper = dt_conf_get_float("darkroom/ui/overexposed/upper");
dev->full.iso_12646 = FALSE;
dev->preview2.iso_12646 = dt_conf_get_bool("second_window/iso_12646");
dev->full.zoom = dev->preview2.zoom = DT_ZOOM_FIT;
dev->full.closeup = dev->preview2.closeup = 0;
dev->full.zoom_x = dev->full.zoom_y = dev->preview2.zoom_x = dev->preview2.zoom_y = 0.0f;
dev->full.zoom_scale = dev->preview2.zoom_scale = 1.0f;
}
void dt_dev_cleanup(dt_develop_t *dev)
{
if(!dev) return;
// image_cache does not have to be unref'd, this is done outside develop module.
dt_dev_init_chroma(dev);
if(dev->full.pipe)
{
dt_dev_pixelpipe_cleanup(dev->full.pipe);
free(dev->full.pipe);
}
if(dev->preview_pipe)
{
dt_dev_pixelpipe_cleanup(dev->preview_pipe);
free(dev->preview_pipe);
}
if(dev->preview2.pipe)
{
dt_dev_pixelpipe_cleanup(dev->preview2.pipe);
free(dev->preview2.pipe);
}
while(dev->history)
{
dt_dev_free_history_item(((dt_dev_history_item_t *)dev->history->data));
dev->history = g_list_delete_link(dev->history, dev->history);
}
while(dev->iop)
{
dt_iop_cleanup_module((dt_iop_module_t *)dev->iop->data);
free(dev->iop->data);
dev->iop = g_list_delete_link(dev->iop, dev->iop);
}
while(dev->alliop)
{
dt_iop_cleanup_module((dt_iop_module_t *)dev->alliop->data);
free(dev->alliop->data);
dev->alliop = g_list_delete_link(dev->alliop, dev->alliop);
}
g_list_free_full(dev->iop_order_list, free);
while(dev->allprofile_info)
{
dt_ioppr_cleanup_profile_info
((dt_iop_order_iccprofile_info_t *)dev->allprofile_info->data);
dt_free_align(dev->allprofile_info->data);
dev->allprofile_info = g_list_delete_link(dev->allprofile_info, dev->allprofile_info);
}
dt_pthread_mutex_destroy(&dev->history_mutex);
free(dev->histogram_pre_tonecurve);
free(dev->histogram_pre_levels);
g_list_free_full(dev->forms, (void (*)(void *))dt_masks_free_form);
g_list_free_full(dev->allforms, (void (*)(void *))dt_masks_free_form);
dt_conf_set_int("darkroom/ui/rawoverexposed/mode",
dev->rawoverexposed.mode);
dt_conf_set_int("darkroom/ui/rawoverexposed/colorscheme",
dev->rawoverexposed.colorscheme);
dt_conf_set_float("darkroom/ui/rawoverexposed/threshold",
dev->rawoverexposed.threshold);
dt_conf_set_int("darkroom/ui/overexposed/mode", dev->overexposed.mode);
dt_conf_set_int("darkroom/ui/overexposed/colorscheme", dev->overexposed.colorscheme);
dt_conf_set_float("darkroom/ui/overexposed/lower", dev->overexposed.lower);
dt_conf_set_float("darkroom/ui/overexposed/upper", dev->overexposed.upper);
g_list_free(dev->module_filter_out);
}
void dt_dev_process_image(dt_develop_t *dev)
{
if(!dev->gui_attached || dev->full.pipe->processing) return;
const gboolean err
= dt_control_add_job_res(darktable.control,
dt_dev_process_image_job_create(dev), DT_CTL_WORKER_ZOOM_1);
if(err) dt_print(DT_DEBUG_ALWAYS, "[dev_process_image] job queue exceeded!\n");
}
void dt_dev_process_preview(dt_develop_t *dev)
{
if(!dev->gui_attached) return;
const gboolean err = dt_control_add_job_res(darktable.control,
dt_dev_process_preview_job_create(dev),
DT_CTL_WORKER_ZOOM_FILL);
if(err) dt_print(DT_DEBUG_ALWAYS, "[dev_process_preview] job queue exceeded!\n");
}
void dt_dev_process_preview2(dt_develop_t *dev)
{
const gboolean err = dt_control_add_job_res(darktable.control,
dt_dev_process_preview2_job_create(dev),
DT_CTL_WORKER_ZOOM_2);
if(err) dt_print(DT_DEBUG_ALWAYS, "[dev_process_preview2] job queue exceeded!\n");
}
void dt_dev_invalidate(dt_develop_t *dev)
{
dev->full.pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dev->timestamp++;
if(dev->preview_pipe)
dev->preview_pipe->input_timestamp = dev->timestamp;
if(dev->preview2.pipe)
dev->preview2.pipe->input_timestamp = dev->timestamp;
}
void dt_dev_invalidate_all(dt_develop_t *dev)
{
if(dev->full.pipe)
dev->full.pipe->status = DT_DEV_PIXELPIPE_DIRTY;
if(dev->preview_pipe)
dev->preview_pipe->status = DT_DEV_PIXELPIPE_DIRTY;
if(dev->preview2.pipe)
dev->preview2.pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dev->timestamp++;
}
void dt_dev_invalidate_preview(dt_develop_t *dev)
{
dev->preview_pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dev->timestamp++;
if(dev->full.pipe)
dev->full.pipe->input_timestamp = dev->timestamp;
if(dev->preview2.pipe)
dev->preview2.pipe->input_timestamp = dev->timestamp;
}
void dt_dev_process_image_job(dt_develop_t *dev,
dt_dev_viewport_t *port,
dt_dev_pixelpipe_t *pipe,
dt_signal_t signal,
const int devid)
{
if(dev->full.pipe->loading && pipe != dev->full.pipe)
{
// raw is already loading, no use starting another file access, we wait.
return;
}
if(port == &dev->preview2 && !(port->widget && GTK_IS_WIDGET(port->widget)))
{
return;
}
dt_pthread_mutex_lock(&pipe->mutex);
if(dev->gui_leaving)
{
dt_pthread_mutex_unlock(&pipe->mutex);
return;
}
dt_control_log_busy_enter();
dt_control_toast_busy_enter();
pipe->input_timestamp = dev->timestamp;
// let gui know to draw preview instead of us, if it's there:
pipe->status = DT_DEV_PIXELPIPE_RUNNING;
dt_times_t start;
dt_get_perf_times(&start);
dt_mipmap_buffer_t buf;
dt_mipmap_cache_get(darktable.mipmap_cache,
&buf, dev->image_storage.id,
port ? DT_MIPMAP_FULL : DT_MIPMAP_F,
port ? DT_MIPMAP_BLOCKING : DT_MIPMAP_BEST_EFFORT,
'r');
dt_show_times(&start, "[dt_dev_process_image_job] loading image.");
// failed to load raw?
if(!buf.buf)
{
dt_control_log_busy_leave();
dt_control_toast_busy_leave();
pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dt_pthread_mutex_unlock(&pipe->mutex);
dev->image_invalid_cnt++;
return; // not loaded yet. load will issue a gtk redraw on
// completion, which in turn will trigger us again later.
}
dt_dev_pixelpipe_set_input(pipe, dev, (float *)buf.buf, buf.width, buf.height,
port ? 1.0 : buf.iscale);
if(pipe->loading)
{
// init pixel pipeline
dt_pthread_mutex_lock(&dev->history_mutex);
dt_dev_pixelpipe_cleanup_nodes(pipe);
dt_dev_pixelpipe_create_nodes(pipe, dev);
dt_pthread_mutex_unlock(&dev->history_mutex);
if(pipe == dev->full.pipe)
{
if(dev->image_force_reload) dt_dev_pixelpipe_cache_flush(pipe);
dev->image_force_reload = FALSE;
if(dev->gui_attached)
{
// during load, a mipf update could have been issued.
dev->preview_pipe->input_changed = TRUE;
dev->preview_pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dev->preview_pipe->changed |= DT_DEV_PIPE_SYNCH;
dev->preview2.pipe->input_changed = TRUE;
dev->preview2.pipe->status = DT_DEV_PIXELPIPE_DIRTY;
dev->preview2.pipe->changed |= DT_DEV_PIPE_SYNCH;
dev->gui_synch = TRUE; // notify gui thread we want to synch
// (call gui_update on the modules)
}
pipe->changed |= DT_DEV_PIPE_SYNCH;
}
else
{
dt_dev_pixelpipe_cache_flush(pipe);
pipe->loading = FALSE;
}
}
if(port != &dev->full && pipe->input_changed)
{
dt_dev_pixelpipe_cache_flush(pipe);
pipe->input_changed = FALSE;
}
// adjust pipeline according to changed flag set by {add,pop}_history_item.
restart:
if(dev->gui_leaving)
{
dt_mipmap_cache_release(darktable.mipmap_cache, &buf);
dt_control_log_busy_leave();
dt_control_toast_busy_leave();
pipe->status = DT_DEV_PIXELPIPE_INVALID;
dt_pthread_mutex_unlock(&pipe->mutex);
return;
}
if(port == &dev->full)
pipe->input_timestamp = dev->timestamp;
// dt_dev_pixelpipe_change() will clear the changed value
const dt_dev_pixelpipe_change_t pipe_changed = pipe->changed;
// this locks dev->history_mutex.
dt_dev_pixelpipe_change(pipe, dev);
float scale = 1.0f;
int window_width = G_MAXINT;
int window_height = G_MAXINT;
float zoom_x = 0;
float zoom_y = 0;
if(port)
{
// if just changed to an image with a different aspect ratio or
// altered image orientation, the prior zoom xy could now be beyond
// the image boundary
if(port->pipe->loading || pipe_changed != DT_DEV_PIPE_UNCHANGED)
dt_dev_zoom_move(port, DT_ZOOM_MOVE, 0.0f, 0, 0.0f, 0.0f, TRUE);
// determine scale according to new dimensions
dt_dev_zoom_t zoom;
int closeup;
dt_dev_get_viewport_params(port, &zoom, &closeup, &zoom_x, &zoom_y);
scale = dt_dev_get_zoom_scale(port, zoom, 1.0f, 0) * port->ppd;
window_width = port->width * port->ppd / (1<<closeup);
window_height = port->height * port->ppd / (1<<closeup);
}
// else
// {
// // FIXME full pipe may be busy, so update processed sizes here
// // make sure preview pipe is newer than full/preview2 pipes
// dev->full.pipe->processed_width = pipe->processed_width * pipe->iscale;
// dev->full.pipe->processed_height = pipe->processed_height * pipe->iscale;
// }
const int wd = MIN(window_width, scale * pipe->processed_width);
const int ht = MIN(window_height, scale * pipe->processed_height);
const int x = port ? MAX(0, scale * pipe->processed_width * (.5 + zoom_x) - wd / 2) : 0;
const int y = port ? MAX(0, scale * pipe->processed_height * (.5 + zoom_y) - ht / 2) : 0;
dt_get_times(&start);
// disable some modules if needed
for(GList *m = dev->module_filter_out;
m;
m = g_list_next(m))
{
char *mod = (char *)(m->data);
dt_dev_pixelpipe_module_enabled(port->pipe, mod, FALSE);
}
if(dt_dev_pixelpipe_process(pipe, dev, x, y, wd, ht, scale, devid))
{
// interrupted because image changed?
if(dev->image_force_reload || pipe->loading || pipe->input_changed)
{
dt_mipmap_cache_release(darktable.mipmap_cache, &buf);
dt_control_log_busy_leave();
dt_control_toast_busy_leave();
pipe->status = DT_DEV_PIXELPIPE_INVALID;
dt_pthread_mutex_unlock(&pipe->mutex);
return;
}
// or because the pipeline changed?
else
{
if(port && port->widget) dt_control_queue_redraw_widget(port->widget);
goto restart;
}
}
dt_show_times_f(&start,
"[dev_process_image] pixel pipeline", "processing `%s'",
dev->image_storage.filename);
dt_dev_average_delay_update(&start, &pipe->average_delay);
// maybe we got zoomed/panned in the meantime?
if(port && pipe->changed != DT_DEV_PIPE_UNCHANGED) goto restart;
pipe->status = DT_DEV_PIXELPIPE_VALID;
pipe->loading = FALSE;
dev->image_invalid_cnt = 0;
dt_mipmap_cache_release(darktable.mipmap_cache, &buf);
// if a widget needs to be redraw there's the DT_SIGNAL_*_PIPE_FINISHED signals
dt_control_log_busy_leave();
dt_control_toast_busy_leave();
dt_pthread_mutex_unlock(&pipe->mutex);
if(dev->gui_attached && !dev->gui_leaving && signal != -1)
DT_DEBUG_CONTROL_SIGNAL_RAISE(darktable.signals, signal);
if(port) return;
// preview pipe only
if(!dev->history_postpone_invalidate)
dt_image_update_final_size(dev->preview_pipe->output_imgid);
dev->gui_previous_pipe_time = dt_get_wtime();
#ifdef USE_LUA
dt_lua_async_call_alien(dt_lua_event_trigger_wrapper,
0, NULL, NULL,
LUA_ASYNC_TYPENAME, "const char*", "pixelpipe-processing-complete",
LUA_ASYNC_TYPENAME, "dt_lua_image_t", GINT_TO_POINTER(dev->image_storage.id),
LUA_ASYNC_DONE);
#endif
}
static inline void _dt_dev_load_pipeline_defaults(dt_develop_t *dev)
{
for(const GList *modules = g_list_last(dev->iop);
modules;
modules = g_list_previous(modules))
{
dt_iop_module_t *module = (dt_iop_module_t *)(modules->data);
dt_iop_reload_defaults(module);
}
}
// load the raw and get the new image struct, blocking in gui thread
static inline void _dt_dev_load_raw(dt_develop_t *dev,
const dt_imgid_t imgid)
{
// first load the raw, to make sure dt_image_t will contain all and correct data.
dt_mipmap_buffer_t buf;
dt_times_t start;
dt_get_perf_times(&start);
dt_mipmap_cache_get(darktable.mipmap_cache, &buf, imgid, DT_MIPMAP_FULL,
DT_MIPMAP_BLOCKING, 'r');
dt_mipmap_cache_release(darktable.mipmap_cache, &buf);
dt_show_times(&start, "[dt_dev_load_raw] loading the image.");
const dt_image_t *image = dt_image_cache_get(darktable.image_cache, imgid, 'r');
dev->image_storage = *image;
dt_image_cache_read_release(darktable.image_cache, image);
dev->requested_id = dev->image_storage.id;
}
void dt_dev_reload_image(dt_develop_t *dev,
const dt_imgid_t imgid)
{
_dt_dev_load_raw(dev, imgid);
dev->image_force_reload = TRUE;
dev->full.pipe->loading = dev->preview_pipe->loading = dev->preview2.pipe->loading = TRUE;
dev->full.pipe->changed |= DT_DEV_PIPE_SYNCH;
dt_dev_invalidate(dev); // only invalidate image, preview will follow once it's loaded.
}
float dt_dev_get_zoom_scale(dt_dev_viewport_t *port,
dt_dev_zoom_t zoom,
const int closeup_factor,
const int preview)
{
float zoom_scale;
int procw, proch;
dt_dev_get_processed_size(port, &procw, &proch);
const float w = (float)port->width / procw;
const float h = (float)port->height / proch;
switch(zoom)
{
case DT_ZOOM_FIT:
zoom_scale = fminf(w, h);
break;
case DT_ZOOM_FILL:
zoom_scale = fmaxf(w, h);
break;
case DT_ZOOM_1:
zoom_scale = closeup_factor;
break;
default: // DT_ZOOM_FREE
zoom_scale = port->zoom_scale;
break;
}
if(preview) zoom_scale *= (float)darktable.develop->full.pipe->processed_width
/ darktable.develop->preview_pipe->processed_width;
return zoom_scale;
}
float dt_dev_get_zoom_scale_full(void)
{
dt_dev_zoom_t zoom;
int closeup;
dt_dev_get_viewport_params(&darktable.develop->full, &zoom, &closeup, NULL, NULL);
const float zoom_scale = dt_dev_get_zoom_scale(&darktable.develop->full, zoom, 1 << closeup, 1);
return zoom_scale;
}
float dt_dev_get_zoomed_in(void)
{
dt_dev_zoom_t zoom;
int closeup;
dt_dev_get_viewport_params(&darktable.develop->full, &zoom, &closeup, NULL, NULL);
const float min_scale = dt_dev_get_zoom_scale(&darktable.develop->full, DT_ZOOM_FIT, 1<<closeup, 0);
const float cur_scale = dt_dev_get_zoom_scale(&darktable.develop->full, zoom, 1<<closeup, 0);
return cur_scale / min_scale;
}
void dt_dev_load_image(dt_develop_t *dev,
const dt_imgid_t imgid)
{
dt_lock_image(imgid);
_dt_dev_load_raw(dev, imgid);
if(dev->full.pipe)
{
dev->full.pipe->processed_width = 0;
dev->full.pipe->processed_height = 0;
dev->full.pipe->loading = dev->preview_pipe->loading = dev->preview2.pipe->loading = TRUE;
dev->full.pipe->status = dev->preview_pipe->status = dev->preview2.pipe->status = DT_DEV_PIXELPIPE_DIRTY;
}
dev->first_load = TRUE;
// we need a global lock as the dev->iop set must not be changed
// until read history is terminated
dt_pthread_mutex_lock(&darktable.dev_threadsafe);
dev->iop = dt_iop_load_modules(dev);
dt_dev_read_history_ext(dev, dev->image_storage.id, FALSE);
dt_pthread_mutex_unlock(&darktable.dev_threadsafe);
dev->first_load = FALSE;
dt_unlock_image(imgid);
}
void dt_dev_configure(dt_dev_viewport_t *port)
{
int32_t tb = 0;
if(port->iso_12646)
{
// the border size is taken from conf as an absolute in cm
// and uses dpi and ppd for an absolute size
const int bsize = port->dpi * port->ppd * dt_conf_get_float("darkroom/ui/iso12464_border") / 2.54f;
// for safety, at least 2 pixels and at least 40% for content
tb = MIN(MAX(2, bsize), 0.3f * MIN(port->orig_width, port->orig_height));
}
else if(port == &darktable.develop->full)
{
// Reset border size from config
tb = DT_PIXEL_APPLY_DPI(dt_conf_get_int("plugins/darkroom/ui/border_size"));
}
port->border_size = tb;
// fixed border on every side
int32_t wd = port->orig_width - 2*tb;
int32_t ht = port->orig_height - 2*tb;
if(port->width != wd || port->height != ht)
{
port->width = wd;
port->height = ht;
port->pipe->changed |= DT_DEV_PIPE_ZOOMED;
dt_dev_zoom_move(port, DT_ZOOM_MOVE, 0.0f, TRUE, 0.0f, 0.0f, TRUE);
}
}
// helper used to synch a single history item with db
static void _dev_write_history_item(const dt_imgid_t imgid,
dt_dev_history_item_t *h,
const int32_t num)
{
sqlite3_stmt *stmt;
DT_DEBUG_SQLITE3_PREPARE_V2
(dt_database_get(darktable.db),
"SELECT num FROM main.history WHERE imgid = ?1 AND num = ?2", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, num);
if(sqlite3_step(stmt) != SQLITE_ROW)
{
sqlite3_finalize(stmt);
DT_DEBUG_SQLITE3_PREPARE_V2
(dt_database_get(darktable.db),
"INSERT INTO main.history (imgid, num) VALUES (?1, ?2)", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, num);
sqlite3_step(stmt);
}
sqlite3_finalize(stmt);
// clang-format off
DT_DEBUG_SQLITE3_PREPARE_V2
(dt_database_get(darktable.db),
"UPDATE main.history"
" SET operation = ?1, op_params = ?2, module = ?3, enabled = ?4, "
" blendop_params = ?7, blendop_version = ?8, multi_priority = ?9,"
" multi_name = ?10, multi_name_hand_edited = ?11"
" WHERE imgid = ?5 AND num = ?6",
-1, &stmt, NULL);
// clang-format on
DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 1, h->module->op, -1, SQLITE_TRANSIENT);
DT_DEBUG_SQLITE3_BIND_BLOB(stmt, 2, h->params, h->module->params_size, SQLITE_TRANSIENT);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 3, h->module->version());
DT_DEBUG_SQLITE3_BIND_INT(stmt, 4, h->enabled);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 5, imgid);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 6, num);
DT_DEBUG_SQLITE3_BIND_BLOB(stmt, 7, h->blend_params,
sizeof(dt_develop_blend_params_t), SQLITE_TRANSIENT);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 8, dt_develop_blend_version());
DT_DEBUG_SQLITE3_BIND_INT(stmt, 9, h->multi_priority);
DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 10, h->multi_name, -1, SQLITE_TRANSIENT);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 11, h->multi_name_hand_edited);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
// write masks (if any)
for(GList *forms = h->forms; forms; forms = g_list_next(forms))
{
dt_masks_form_t *form = (dt_masks_form_t *)forms->data;
if(form)
dt_masks_write_masks_history_item(imgid, num, form);
}
}
static void _dev_auto_save(dt_develop_t *dev)
{
const double user_delay = (double)dt_conf_get_int("autosave_interval");
const dt_imgid_t imgid = dev->image_storage.id;
/* We can only autosave database & xmp while we have a valid image id
and we are not currently loading or changing it in main darkroom
*/
const double start = dt_get_wtime();
const gboolean saving = (user_delay >= 1.0)
&& ((start - dev->autosave_time) > user_delay)
&& !dev->full.pipe->loading
&& dev->requested_id == imgid
&& dt_is_valid_imgid(imgid);
if(saving)
{
dt_dev_write_history(dev);
dt_image_write_sidecar_file(imgid);
const double after = dt_get_wtime();
dev->autosave_time = after;
// if writing to database and the xmp took too long we disable
// autosaving mode for this image
if((after - start) > 0.5)
{
dev->autosaving = FALSE;
dt_print(DT_DEBUG_DEV, "autosave history disabled, took %.3fs\n", after - start);
dt_control_log(_("autosaving history has been disabled for this image"
" because of a very large history or a slow drive being used"));
}
}
}
static void _dev_auto_module_label(dt_develop_t *dev,
dt_iop_module_t *module)
{
// adjust the label to match presets if possible or otherwise the default
// multi_name for this module.
if(!dt_iop_is_hidden(module)
&& !module->multi_name_hand_edited
&& dt_conf_get_bool("darkroom/ui/auto_module_name_update"))
{
const gboolean is_default_params =
memcmp(module->params, module->default_params, module->params_size) == 0;
char *preset_name = dt_presets_get_module_label
(module->op,
module->params, module->params_size, is_default_params,
module->blend_params, sizeof(dt_develop_blend_params_t));
// if we have a preset-name, use it. otherwise set the label to the multi-priority
// except for 0 where the multi-name is cleared.
if(preset_name)
snprintf(module->multi_name,
sizeof(module->multi_name), "%s", preset_name);
else if(module->multi_priority != 0)
snprintf(module->multi_name,
sizeof(module->multi_name), "%d", module->multi_priority);
else
g_strlcpy(module->multi_name, "", sizeof(module->multi_name));
g_free(preset_name);
if(dev->gui_attached)
dt_iop_gui_update_header(module);
}
}
static void _dev_add_history_item_ext(dt_develop_t *dev,
dt_iop_module_t *module,
const gboolean enable,
const gboolean new_item,
const gboolean no_image,
const gboolean include_masks,
const gboolean auto_name_module)
{
// try to auto-name the module based on the presets if possible
if(auto_name_module)
{
_dev_auto_module_label(dev, module);
}
int kept_module = 0;
GList *history = g_list_nth(dev->history, dev->history_end);
// look for leaks on top of history in two steps
// first remove obsolete items above history_end
// but keep the always-on modules
while(history)
{
GList *next = g_list_next(history);
dt_dev_history_item_t *hist = (dt_dev_history_item_t *)(history->data);
// printf("removing obsoleted history item: %s\n", hist->module->op);
//check if an earlier instance of the module exists
gboolean earlier_entry = FALSE;
GList *prior_history = g_list_nth(dev->history, dev->history_end - 1);
while(prior_history)
{
dt_dev_history_item_t *prior_hist = (dt_dev_history_item_t *)(prior_history->data);
if(prior_hist->module->so == hist->module->so)
{
earlier_entry = TRUE;
break;
}
prior_history = g_list_previous(prior_history);
}
if((!hist->module->hide_enable_button && !hist->module->default_enabled)
|| earlier_entry)
{
dt_dev_free_history_item(hist);
dev->history = g_list_delete_link(dev->history, history);
}
else
kept_module++;
history = next;
}
// then remove NIL items there
while((dev->history_end>0) && (! g_list_nth(dev->history, dev->history_end - 1)))
dev->history_end--;
dev->history_end += kept_module;
history = g_list_nth(dev->history, dev->history_end - 1);
dt_dev_history_item_t *hist = history ? (dt_dev_history_item_t *)(history->data) : 0;
// if module should be enabled, do it now
if(enable)
{
module->enabled = TRUE;
if(!no_image)
{
if(module->off)
{
++darktable.gui->reset;
dt_iop_gui_set_enable_button(module);
--darktable.gui->reset;
}
}
}
if(!history // no history yet, push new item
|| new_item // a new item is requested
|| module != hist->module
|| module->instance != hist->module->instance // add new item for different op
|| module->multi_priority != hist->module->multi_priority // or instance
|| ((dev->focus_hash != hist->focus_hash) // or if focused out and in
// but only add item if there is a difference at all for the same module
&& ((module->params_size != hist->module->params_size)
|| include_masks
|| (module->params_size == hist->module->params_size
&& memcmp(hist->params, module->params, module->params_size)))))
{
// new operation, push new item
dev->history_end++;
hist = (dt_dev_history_item_t *)calloc(1, sizeof(dt_dev_history_item_t));
g_strlcpy(hist->op_name, module->op, sizeof(hist->op_name));
hist->focus_hash = dev->focus_hash;
hist->enabled = module->enabled;
hist->module = module;
hist->params = malloc(module->params_size);
hist->iop_order = module->iop_order;
hist->multi_priority = module->multi_priority;
hist->multi_name_hand_edited = module->multi_name_hand_edited;
g_strlcpy(hist->multi_name, module->multi_name, sizeof(hist->multi_name));
/* allocate and set hist blend_params */
hist->blend_params = malloc(sizeof(dt_develop_blend_params_t));
memcpy(hist->params, module->params, module->params_size);
memcpy(hist->blend_params, module->blend_params, sizeof(dt_develop_blend_params_t));
if(include_masks)
hist->forms = dt_masks_dup_forms_deep(dev->forms, NULL);
else
hist->forms = NULL;
dev->history = g_list_append(dev->history, hist);
if(!no_image)
{
dev->full.pipe->changed |= DT_DEV_PIPE_SYNCH;
// topology remains, as modules are fixed for now:
dev->preview_pipe->changed |= DT_DEV_PIPE_SYNCH;
dev->preview2.pipe->changed |= DT_DEV_PIPE_SYNCH;
}
}
else
{
// same operation, change params
hist = (dt_dev_history_item_t *)history->data;
memcpy(hist->params, module->params, module->params_size);
if(module->flags() & IOP_FLAGS_SUPPORTS_BLENDING)
memcpy(hist->blend_params, module->blend_params, sizeof(dt_develop_blend_params_t));
hist->iop_order = module->iop_order;
hist->multi_priority = module->multi_priority;
hist->multi_name_hand_edited = module->multi_name_hand_edited;
memcpy(hist->multi_name, module->multi_name, sizeof(module->multi_name));
hist->enabled = module->enabled;
if(include_masks)
{
g_list_free_full(hist->forms, (void (*)(void *))dt_masks_free_form);
hist->forms = dt_masks_dup_forms_deep(dev->forms, NULL);
}
if(!no_image)
{
dev->full.pipe->changed |= DT_DEV_PIPE_TOP_CHANGED;
dev->preview_pipe->changed |= DT_DEV_PIPE_TOP_CHANGED;
dev->preview2.pipe->changed |= DT_DEV_PIPE_TOP_CHANGED;
}
}
if((module->enabled) && (!no_image))
dev->history_last_module = module;
// possibly save database and sidecar file
if(dev->autosaving)
_dev_auto_save(dev);
}
const dt_dev_history_item_t *dt_dev_get_history_item(dt_develop_t *dev, const char *op)
{
for(GList *l = g_list_last(dev->history); l; l = g_list_previous(l))
{
dt_dev_history_item_t *item = (dt_dev_history_item_t *)l->data;
if(!g_strcmp0(item->op_name, op))
{
return item;
break;
}
}
return NULL;
}
void dt_dev_add_history_item_ext(dt_develop_t *dev,
dt_iop_module_t *module,
const gboolean enable,
const int no_image)
{
_dev_add_history_item_ext(dev, module, enable, FALSE, no_image, FALSE, TRUE);
}
static gboolean _dev_undo_start_record_target(dt_develop_t *dev, gpointer target)
{
const double this_time = dt_get_wtime();
const double merge_time =
dev->gui_previous_time + dt_conf_get_float("darkroom/undo/merge_same_secs");
const double review_time =
dev->gui_previous_pipe_time + dt_conf_get_float("darkroom/undo/review_secs");
dev->gui_previous_pipe_time = merge_time;
if(target && target == dev->gui_previous_target
&& this_time < MIN(merge_time, review_time))
{
return FALSE;
}
dt_dev_undo_start_record(dev);
dev->gui_previous_target = target;
dev->gui_previous_time = this_time;
return TRUE;
}
static void _dev_add_history_item(dt_develop_t *dev,
dt_iop_module_t *module,
const gboolean enable,
const gboolean new_item,
const gpointer target)
{
if(!darktable.gui || darktable.gui->reset) return;
// record current name, needed to ensure we do an undo record
// if the module name is changed.
gchar *saved_name = g_strdup(module->multi_name);
_dev_auto_module_label(dev, module);
const gboolean multi_name_changed = strcmp(saved_name, module->multi_name) != 0;
const gboolean need_end_record =
_dev_undo_start_record_target(dev, multi_name_changed ? NULL : target);
g_free(saved_name);
dt_pthread_mutex_lock(&dev->history_mutex);
if(dev->gui_attached)
{
_dev_add_history_item_ext(dev, module, enable, new_item, FALSE, FALSE, FALSE);
}