/
image.c
2696 lines (2339 loc) · 94.2 KB
/
image.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-2021 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 "common/image.h"
#include "common/collection.h"
#include "common/darktable.h"
#include "common/debug.h"
#include "common/exif.h"
#include "common/file_location.h"
#include "common/grouping.h"
#include "common/history.h"
#include "common/history_snapshot.h"
#include "common/image_cache.h"
#include "common/imageio.h"
#include "common/imageio_rawspeed.h"
#include "common/mipmap_cache.h"
#include "common/ratings.h"
#include "common/tags.h"
#include "common/undo.h"
#include "common/history.h"
#include "common/selection.h"
#include "control/conf.h"
#include "control/control.h"
#include "control/jobs.h"
#include "develop/lightroom.h"
#include "win/filepath.h"
#ifdef USE_LUA
#include "lua/image.h"
#endif
#include <assert.h>
#include <ctype.h>
#include <math.h>
#include <sqlite3.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#ifndef _WIN32
#include <glob.h>
#endif
#include <glib/gstdio.h>
typedef struct dt_undo_monochrome_t
{
int32_t imgid;
gboolean before;
gboolean after;
} dt_undo_monochrome_t;
typedef struct dt_undo_datetime_t
{
int32_t imgid;
char before[DT_DATETIME_LENGTH];
char after[DT_DATETIME_LENGTH];
} dt_undo_datetime_t;
typedef struct dt_undo_geotag_t
{
int32_t imgid;
dt_image_geoloc_t before;
dt_image_geoloc_t after;
} dt_undo_geotag_t;
typedef struct dt_undo_duplicate_t
{
int32_t orig_imgid;
int32_t version;
int32_t new_imgid;
} dt_undo_duplicate_t;
static void _pop_undo_execute(const int imgid, const gboolean before, const gboolean after);
static int32_t _image_duplicate_with_version(const int32_t imgid, const int32_t newversion, const gboolean undo);
static void _pop_undo(gpointer user_data, const dt_undo_type_t type, dt_undo_data_t data, const dt_undo_action_t action, GList **imgs);
static int64_t max_image_position()
{
sqlite3_stmt *stmt = NULL;
// get last position
int64_t max_position = 0;
const gchar *max_position_query = "SELECT MAX(position) FROM main.images";
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), max_position_query, -1, &stmt, NULL);
if (sqlite3_step(stmt) == SQLITE_ROW)
{
max_position = sqlite3_column_int64(stmt, 0);
}
sqlite3_finalize(stmt);
return max_position;
}
static int64_t create_next_image_position()
{
/* The sequence pictures come in (import) define the initial sequence.
*
* The upper int32_t of the last image position is increased by one
* while the lower 32 bits are masked out.
*
* Example:
* last image position: (Hex)
* 0000 0002 0000 0001
*
* next image position
* 0000 0003 0000 0000
*/
return (max_image_position() & 0xFFFFFFFF00000000) + (1ll << 32);
}
static void _image_local_copy_full_path(const int imgid, char *pathname, size_t pathname_len);
int dt_image_is_ldr(const dt_image_t *img)
{
const char *c = img->filename + strlen(img->filename);
while(*c != '.' && c > img->filename) c--;
if((img->flags & DT_IMAGE_LDR) || !strcasecmp(c, ".jpg") || !strcasecmp(c, ".png")
|| !strcasecmp(c, ".ppm"))
return 1;
else
return 0;
}
int dt_image_is_hdr(const dt_image_t *img)
{
const char *c = img->filename + strlen(img->filename);
while(*c != '.' && c > img->filename) c--;
if((img->flags & DT_IMAGE_HDR) || !strcasecmp(c, ".exr") || !strcasecmp(c, ".hdr")
|| !strcasecmp(c, ".pfm"))
return 1;
else
return 0;
}
// NULL terminated list of supported non-RAW extensions
// const char *dt_non_raw_extensions[]
// = { ".jpeg", ".jpg", ".pfm", ".hdr", ".exr", ".pxn", ".tif", ".tiff", ".png",
// ".j2c", ".j2k", ".jp2", ".jpc", ".gif", ".jpc", ".jp2", ".bmp", ".dcm",
// ".jng", ".miff", ".mng", ".pbm", ".pnm", ".ppm", ".pgm", NULL };
int dt_image_is_raw(const dt_image_t *img)
{
return (img->flags & DT_IMAGE_RAW);
}
int dt_image_is_monochrome(const dt_image_t *img)
{
return (img->flags & DT_IMAGE_MONOCHROME);
}
static void _image_set_monochrome_flag(const int32_t imgid, gboolean monochrome, gboolean undo_on)
{
dt_image_t *img = NULL;
gboolean changed = FALSE;
img = dt_image_cache_get(darktable.image_cache, imgid, 'r');
if(img)
{
const int mask_bw = dt_image_monochrome_flags(img);
dt_image_cache_read_release(darktable.image_cache, img);
if((!monochrome) && (mask_bw & DT_IMAGE_MONOCHROME_PREVIEW))
{
// wanting it to be color found preview
img = dt_image_cache_get(darktable.image_cache, imgid, 'w');
img->flags &= ~(DT_IMAGE_MONOCHROME_PREVIEW | DT_IMAGE_MONOCHROME_WORKFLOW);
changed = TRUE;
}
if(monochrome && ((mask_bw == 0) || (mask_bw == DT_IMAGE_MONOCHROME_PREVIEW)))
{
// wanting monochrome and found color or just preview without workflow activation
img = dt_image_cache_get(darktable.image_cache, imgid, 'w');
img->flags |= (DT_IMAGE_MONOCHROME_PREVIEW | DT_IMAGE_MONOCHROME_WORKFLOW);
changed = TRUE;
}
if(changed)
{
const int mask = dt_image_monochrome_flags(img);
dt_image_cache_write_release(darktable.image_cache, img, DT_IMAGE_CACHE_RELAXED);
dt_imageio_update_monochrome_workflow_tag(imgid, mask);
if(undo_on)
{
dt_undo_monochrome_t *undomono = (dt_undo_monochrome_t *)malloc(sizeof(dt_undo_monochrome_t));
undomono->imgid = imgid;
undomono->before = mask_bw;
undomono->after = mask;
dt_undo_record(darktable.undo, NULL, DT_UNDO_FLAGS, undomono, _pop_undo, g_free);
}
}
}
else
fprintf(stderr,"[image] could not dt_image_cache_get imgid %i\n", imgid);
}
void dt_image_set_monochrome_flag(const int32_t imgid, gboolean monochrome)
{
_image_set_monochrome_flag(imgid, monochrome, TRUE);
}
static void _pop_undo_execute(const int32_t imgid, const gboolean before, const gboolean after)
{
_image_set_monochrome_flag(imgid, after, FALSE);
}
int dt_image_is_matrix_correction_supported(const dt_image_t *img)
{
return ((img->flags & (DT_IMAGE_RAW | DT_IMAGE_S_RAW )) && !(img->flags & DT_IMAGE_MONOCHROME) );
}
int dt_image_is_rawprepare_supported(const dt_image_t *img)
{
return (img->flags & (DT_IMAGE_RAW | DT_IMAGE_S_RAW));
}
gboolean dt_image_use_monochrome_workflow(const dt_image_t *img)
{
return ((img->flags & (DT_IMAGE_MONOCHROME | DT_IMAGE_MONOCHROME_BAYER)) ||
((img->flags & DT_IMAGE_MONOCHROME_PREVIEW) && (img->flags & DT_IMAGE_MONOCHROME_WORKFLOW)));
}
int dt_image_monochrome_flags(const dt_image_t *img)
{
return (img->flags & (DT_IMAGE_MONOCHROME | DT_IMAGE_MONOCHROME_PREVIEW | DT_IMAGE_MONOCHROME_BAYER));
}
const char *dt_image_film_roll_name(const char *path)
{
const char *folder = path + strlen(path);
const int numparts = CLAMPS(dt_conf_get_int("show_folder_levels"), 1, 5);
int count = 0;
while(folder > path)
{
if(*folder == G_DIR_SEPARATOR)
if(++count >= numparts)
{
++folder;
break;
}
--folder;
}
return folder;
}
void dt_image_film_roll_directory(const dt_image_t *img, char *pathname, size_t pathname_len)
{
sqlite3_stmt *stmt;
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "SELECT folder FROM main.film_rolls WHERE id = ?1",
-1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, img->film_id);
if(sqlite3_step(stmt) == SQLITE_ROW)
{
const char *f = (char *)sqlite3_column_text(stmt, 0);
g_strlcpy(pathname, f, pathname_len);
}
sqlite3_finalize(stmt);
pathname[pathname_len - 1] = '\0';
}
void dt_image_film_roll(const dt_image_t *img, char *pathname, size_t pathname_len)
{
sqlite3_stmt *stmt;
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "SELECT folder FROM main.film_rolls WHERE id = ?1",
-1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, img->film_id);
if(sqlite3_step(stmt) == SQLITE_ROW)
{
const char *f = (char *)sqlite3_column_text(stmt, 0);
const char *c = dt_image_film_roll_name(f);
g_strlcpy(pathname, c, pathname_len);
}
else
{
g_strlcpy(pathname, _("orphaned image"), pathname_len);
}
sqlite3_finalize(stmt);
pathname[pathname_len - 1] = '\0';
}
gboolean dt_image_safe_remove(const int32_t imgid)
{
// always safe to remove if we do not have .xmp
if(!dt_conf_get_bool("write_sidecar_files")) return TRUE;
// check whether the original file is accessible
char pathname[PATH_MAX] = { 0 };
gboolean from_cache = TRUE;
dt_image_full_path(imgid, pathname, sizeof(pathname), &from_cache);
if(!from_cache)
return TRUE;
else
{
// finally check if we have a .xmp for the local copy. If no modification done on the local copy it is safe
// to remove.
g_strlcat(pathname, ".xmp", sizeof(pathname));
return !g_file_test(pathname, G_FILE_TEST_EXISTS);
}
}
void dt_image_full_path(const int32_t imgid, char *pathname, size_t pathname_len, gboolean *from_cache)
{
sqlite3_stmt *stmt;
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"SELECT folder || '" G_DIR_SEPARATOR_S "' || filename FROM main.images i, main.film_rolls f WHERE "
"i.film_id = f.id and i.id = ?1",
-1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
if(sqlite3_step(stmt) == SQLITE_ROW)
{
g_strlcpy(pathname, (char *)sqlite3_column_text(stmt, 0), pathname_len);
}
sqlite3_finalize(stmt);
if(*from_cache)
{
char lc_pathname[PATH_MAX] = { 0 };
_image_local_copy_full_path(imgid, lc_pathname, sizeof(lc_pathname));
if (g_file_test(lc_pathname, G_FILE_TEST_EXISTS))
g_strlcpy(pathname, (char *)lc_pathname, pathname_len);
else
*from_cache = FALSE;
}
}
static void _image_local_copy_full_path(const int32_t imgid, char *pathname, size_t pathname_len)
{
sqlite3_stmt *stmt;
*pathname = '\0';
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"SELECT folder || '" G_DIR_SEPARATOR_S "' || filename FROM main.images i, main.film_rolls f "
"WHERE i.film_id = f.id AND i.id = ?1",
-1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
if(sqlite3_step(stmt) == SQLITE_ROW)
{
char filename[PATH_MAX] = { 0 };
char cachedir[PATH_MAX] = { 0 };
g_strlcpy(filename, (char *)sqlite3_column_text(stmt, 0), pathname_len);
char *md5_filename = g_compute_checksum_for_string(G_CHECKSUM_MD5, filename, strlen(filename));
dt_loc_get_user_cache_dir(cachedir, sizeof(cachedir));
// and finally, add extension, needed as some part of the code is looking for the extension
char *c = filename + strlen(filename);
while(*c != '.' && c > filename) c--;
// cache filename old format: <cachedir>/img-<id>-<MD5>.<ext>
// for upward compatibility we check for the old name, if found we return it
snprintf(pathname, pathname_len, "%s/img-%d-%s%s", cachedir, imgid, md5_filename, c);
// if it does not exist, we return the new naming
if(!g_file_test(pathname, G_FILE_TEST_EXISTS))
{
// cache filename format: <cachedir>/img-<MD5>.<ext>
snprintf(pathname, pathname_len, "%s/img-%s%s", cachedir, md5_filename, c);
}
g_free(md5_filename);
}
sqlite3_finalize(stmt);
}
void dt_image_path_append_version_no_db(int version, char *pathname, size_t pathname_len)
{
// the "first" instance (version zero) does not get a version suffix
if(version > 0)
{
// add version information:
char *filename = g_strdup(pathname);
char *c = pathname + strlen(pathname);
while(*c != '.' && c > pathname) c--;
snprintf(c, pathname + pathname_len - c, "_%02d", version);
c = pathname + strlen(pathname);
char *c2 = filename + strlen(filename);
while(*c2 != '.' && c2 > filename) c2--;
g_strlcpy(c, c2, pathname + pathname_len - c);
g_free(filename);
}
}
void dt_image_path_append_version(const int32_t imgid, char *pathname, size_t pathname_len)
{
// get duplicate suffix
int version = 0;
sqlite3_stmt *stmt;
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "SELECT version FROM main.images WHERE id = ?1", -1,
&stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
if(sqlite3_step(stmt) == SQLITE_ROW) version = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
dt_image_path_append_version_no_db(version, pathname, pathname_len);
}
void dt_image_print_exif(const dt_image_t *img, char *line, size_t line_len)
{
char *exposure_str = dt_util_format_exposure(img->exif_exposure);
snprintf(line, line_len, "%s f/%.1f %dmm ISO %d", exposure_str, img->exif_aperture, (int)img->exif_focal_length,
(int)img->exif_iso);
g_free(exposure_str);
}
int dt_image_get_xmp_rating_from_flags(const int flags)
{
return (flags & DT_IMAGE_REJECTED)
? -1 // rejected image = -1
: (flags & DT_VIEW_RATINGS_MASK); // others = 0 .. 5
}
int dt_image_get_xmp_rating(const dt_image_t *img)
{
return dt_image_get_xmp_rating_from_flags(img->flags);
}
void dt_image_set_xmp_rating(dt_image_t *img, const int rating)
{
// clean flags stars and rejected
img->flags &= ~(DT_IMAGE_REJECTED | DT_VIEW_RATINGS_MASK);
if(rating == -2) // assuming that value -2 cannot be found
{
img->flags |= (DT_VIEW_RATINGS_MASK & dt_conf_get_int("ui_last/import_initial_rating"));
}
else if(rating == -1)
{
img->flags |= DT_IMAGE_REJECTED;
}
else
{
img->flags |= (DT_VIEW_RATINGS_MASK & rating);
}
}
void dt_image_get_location(const int32_t imgid, dt_image_geoloc_t *geoloc)
{
const dt_image_t *img = dt_image_cache_get(darktable.image_cache, imgid, 'r');
geoloc->longitude = img->geoloc.longitude;
geoloc->latitude = img->geoloc.latitude;
geoloc->elevation = img->geoloc.elevation;
dt_image_cache_read_release(darktable.image_cache, img);
}
static void _set_location(const int32_t imgid, const dt_image_geoloc_t *geoloc)
{
/* fetch image from cache */
dt_image_t *image = dt_image_cache_get(darktable.image_cache, imgid, 'w');
memcpy(&image->geoloc, geoloc, sizeof(dt_image_geoloc_t));
dt_image_cache_write_release(darktable.image_cache, image, DT_IMAGE_CACHE_SAFE);
}
static void _set_datetime(const int32_t imgid, const char *datetime)
{
/* fetch image from cache */
dt_image_t *image = dt_image_cache_get(darktable.image_cache, imgid, 'w');
memcpy(&image->exif_datetime_taken, datetime, sizeof(image->exif_datetime_taken));
dt_image_cache_write_release(darktable.image_cache, image, DT_IMAGE_CACHE_SAFE);
}
static void _pop_undo(gpointer user_data, const dt_undo_type_t type, dt_undo_data_t data, const dt_undo_action_t action, GList **imgs)
{
if(type == DT_UNDO_GEOTAG)
{
int i = 0;
for(GList *list = (GList *)data; list; list = g_list_next(list))
{
dt_undo_geotag_t *undogeotag = (dt_undo_geotag_t *)list->data;
const dt_image_geoloc_t *geoloc = (action == DT_ACTION_UNDO) ? &undogeotag->before : &undogeotag->after;
_set_location(undogeotag->imgid, geoloc);
*imgs = g_list_prepend(*imgs, GINT_TO_POINTER(undogeotag->imgid));
i++;
}
if(i > 1) dt_control_log((action == DT_ACTION_UNDO)
? _("geo-location undone for %d images")
: _("geo-location re-applied to %d images"), i);
DT_DEBUG_CONTROL_SIGNAL_RAISE(darktable.signals, DT_SIGNAL_MOUSE_OVER_IMAGE_CHANGE);
DT_DEBUG_CONTROL_SIGNAL_RAISE(darktable.signals, DT_SIGNAL_GEOTAG_CHANGED, g_list_copy(*imgs), 0);
}
else if(type == DT_UNDO_DATETIME)
{
int i = 0;
for(GList *list = (GList *)data; list; list = g_list_next(list))
{
dt_undo_datetime_t *undodatetime = (dt_undo_datetime_t *)list->data;
_set_datetime(undodatetime->imgid, (action == DT_ACTION_UNDO)
? undodatetime->before : undodatetime->after);
*imgs = g_list_prepend(*imgs, GINT_TO_POINTER(undodatetime->imgid));
i++;
}
if(i > 1) dt_control_log((action == DT_ACTION_UNDO)
? _("date/time undone for %d images")
: _("date/time re-applied to %d images"), i);
DT_DEBUG_CONTROL_SIGNAL_RAISE(darktable.signals, DT_SIGNAL_MOUSE_OVER_IMAGE_CHANGE);
DT_DEBUG_CONTROL_SIGNAL_RAISE(darktable.signals, DT_SIGNAL_IMAGE_INFO_CHANGED, g_list_copy(*imgs));
}
else if(type == DT_UNDO_DUPLICATE)
{
dt_undo_duplicate_t *undo = (dt_undo_duplicate_t *)data;
if(action == DT_ACTION_UNDO)
{
// remove image
dt_image_remove(undo->new_imgid);
}
else
{
// restore image, note that we record the new imgid created while
// restoring the duplicate.
undo->new_imgid = _image_duplicate_with_version(undo->orig_imgid, undo->version, FALSE);
*imgs = g_list_prepend(*imgs, GINT_TO_POINTER(undo->new_imgid));
}
}
else if(type == DT_UNDO_FLAGS)
{
dt_undo_monochrome_t *undomono = (dt_undo_monochrome_t *)data;
const gboolean before = (action == DT_ACTION_UNDO) ? undomono->after : undomono->before;
const gboolean after = (action == DT_ACTION_UNDO) ? undomono->before : undomono->after;
_pop_undo_execute(undomono->imgid, before, after);
*imgs = g_list_prepend(*imgs, GINT_TO_POINTER(undomono->imgid));
}
}
static void _geotag_undo_data_free(gpointer data)
{
GList *l = (GList *)data;
g_list_free_full(l, g_free);
}
static void _image_set_location(GList *imgs, const dt_image_geoloc_t *geoloc, GList **undo, const gboolean undo_on)
{
for(GList *images = imgs; images; images = g_list_next(images))
{
const int32_t imgid = GPOINTER_TO_INT(images->data);
if(undo_on)
{
dt_undo_geotag_t *undogeotag = (dt_undo_geotag_t *)malloc(sizeof(dt_undo_geotag_t));
undogeotag->imgid = imgid;
dt_image_get_location(imgid, &undogeotag->before);
memcpy(&undogeotag->after, geoloc, sizeof(dt_image_geoloc_t));
*undo = g_list_append(*undo, undogeotag);
}
_set_location(imgid, geoloc);
}
}
void dt_image_set_locations(const GList *imgs, const dt_image_geoloc_t *geoloc, const gboolean undo_on)
{
if(imgs)
{
GList *undo = NULL;
if(undo_on) dt_undo_start_group(darktable.undo, DT_UNDO_GEOTAG);
_image_set_location((GList *)imgs, geoloc, &undo, undo_on);
if(undo_on)
{
dt_undo_record(darktable.undo, NULL, DT_UNDO_GEOTAG, undo, _pop_undo, _geotag_undo_data_free);
dt_undo_end_group(darktable.undo);
}
DT_DEBUG_CONTROL_SIGNAL_RAISE(darktable.signals, DT_SIGNAL_MOUSE_OVER_IMAGE_CHANGE);
}
}
void dt_image_set_location(const int32_t imgid, const dt_image_geoloc_t *geoloc, const gboolean undo_on, const gboolean group_on)
{
GList *imgs = NULL;
if(imgid == -1)
imgs = g_list_copy((GList *)dt_view_get_images_to_act_on(TRUE, TRUE, FALSE));
else
imgs = g_list_prepend(imgs, GINT_TO_POINTER(imgid));
if(group_on) dt_grouping_add_grouped_images(&imgs);
dt_image_set_locations(imgs, geoloc, undo_on);
g_list_free(imgs);
}
static void _image_set_images_locations(const GList *img, const GArray *gloc,
GList **undo, const gboolean undo_on)
{
int i = 0;
for(GList *imgs = (GList *)img; imgs; imgs = g_list_next(imgs))
{
const int32_t imgid = GPOINTER_TO_INT(imgs->data);
const dt_image_geoloc_t *geoloc = &g_array_index(gloc, dt_image_geoloc_t, i);
if(undo_on)
{
dt_undo_geotag_t *undogeotag = (dt_undo_geotag_t *)malloc(sizeof(dt_undo_geotag_t));
undogeotag->imgid = imgid;
dt_image_get_location(imgid, &undogeotag->before);
memcpy(&undogeotag->after, geoloc, sizeof(dt_image_geoloc_t));
*undo = g_list_prepend(*undo, undogeotag);
}
_set_location(imgid, geoloc);
i++;
}
}
void dt_image_set_images_locations(const GList *imgs, const GArray *gloc, const gboolean undo_on)
{
if(!imgs || !gloc || (g_list_length((GList *)imgs) != gloc->len))
return;
GList *undo = NULL;
if(undo_on) dt_undo_start_group(darktable.undo, DT_UNDO_GEOTAG);
_image_set_images_locations(imgs, gloc, &undo, undo_on);
if(undo_on)
{
dt_undo_record(darktable.undo, NULL, DT_UNDO_GEOTAG, undo, _pop_undo, _geotag_undo_data_free);
dt_undo_end_group(darktable.undo);
}
DT_DEBUG_CONTROL_SIGNAL_RAISE(darktable.signals, DT_SIGNAL_MOUSE_OVER_IMAGE_CHANGE);
}
void dt_image_update_final_size(const int32_t imgid)
{
if(imgid <= 0) return;
int ww = 0, hh = 0;
if(darktable.develop && darktable.develop->pipe && darktable.develop->pipe->output_imgid == imgid)
{
dt_dev_pixelpipe_get_dimensions(darktable.develop->pipe, darktable.develop, darktable.develop->pipe->iwidth,
darktable.develop->pipe->iheight, &ww, &hh);
}
dt_image_t *imgtmp = dt_image_cache_get(darktable.image_cache, imgid, 'w');
imgtmp->final_width = ww;
imgtmp->final_height = hh;
dt_image_cache_write_release(darktable.image_cache, imgtmp, DT_IMAGE_CACHE_RELAXED);
DT_DEBUG_CONTROL_SIGNAL_RAISE(darktable.signals, DT_SIGNAL_METADATA_UPDATE);
}
gboolean dt_image_get_final_size(const int32_t imgid, int *width, int *height)
{
// get the img strcut
dt_image_t *imgtmp = dt_image_cache_get(darktable.image_cache, imgid, 'r');
dt_image_t img = *imgtmp;
dt_image_cache_read_release(darktable.image_cache, imgtmp);
// if we already have computed them
if(img.final_height > 0 && img.final_width > 0)
{
*width = img.final_width;
*height = img.final_height;
return 0;
}
// and now we can do the pipe stuff to get final image size
dt_develop_t dev;
dt_dev_init(&dev, 0);
dt_dev_load_image(&dev, imgid);
dt_dev_pixelpipe_t pipe;
int wd = dev.image_storage.width, ht = dev.image_storage.height;
int res = dt_dev_pixelpipe_init_dummy(&pipe, wd, ht);
if(res)
{
// set mem pointer to 0, won't be used.
dt_dev_pixelpipe_set_input(&pipe, &dev, NULL, wd, ht, 1.0f);
dt_dev_pixelpipe_create_nodes(&pipe, &dev);
dt_dev_pixelpipe_synch_all(&pipe, &dev);
dt_dev_pixelpipe_get_dimensions(&pipe, &dev, pipe.iwidth, pipe.iheight, &pipe.processed_width,
&pipe.processed_height);
wd = pipe.processed_width;
ht = pipe.processed_height;
res = TRUE;
dt_dev_pixelpipe_cleanup(&pipe);
}
dt_dev_cleanup(&dev);
imgtmp = dt_image_cache_get(darktable.image_cache, imgid, 'w');
imgtmp->final_width = *width = wd;
imgtmp->final_height = *height = ht;
dt_image_cache_write_release(darktable.image_cache, imgtmp, DT_IMAGE_CACHE_RELAXED);
return res;
}
void dt_image_set_flip(const int32_t imgid, const dt_image_orientation_t orientation)
{
sqlite3_stmt *stmt;
// push new orientation to sql via additional history entry:
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"SELECT IFNULL(MAX(num)+1, 0) FROM main.history"
" WHERE imgid = ?1", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
const int iop_flip_MODVER = 2;
int num = 0;
if(sqlite3_step(stmt) == SQLITE_ROW) num = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"INSERT INTO main.history"
" (imgid, num, module, operation, op_params, enabled, "
" blendop_params, blendop_version, multi_priority, multi_name)"
" VALUES (?1, ?2, ?3, 'flip', ?4, 1, NULL, 0, 0, '') ",
-1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, num);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 3, iop_flip_MODVER);
DT_DEBUG_SQLITE3_BIND_BLOB(stmt, 4, &orientation, sizeof(int32_t), SQLITE_TRANSIENT);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"UPDATE main.images"
" SET history_end = (SELECT MAX(num) + 1"
" FROM main.history "
" WHERE imgid = ?1) WHERE id = ?1", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
dt_history_hash_write_from_history(imgid, DT_HISTORY_HASH_CURRENT);
dt_mipmap_cache_remove(darktable.mipmap_cache, imgid);
dt_image_update_final_size(imgid);
// write that through to xmp:
dt_image_write_sidecar_file(imgid);
}
dt_image_orientation_t dt_image_get_orientation(const int32_t imgid)
{
// find the flip module -- the pointer stays valid until darktable shuts down
static dt_iop_module_so_t *flip = NULL;
if(flip == NULL)
{
for(const GList *modules = darktable.iop; modules; modules = g_list_next(modules))
{
dt_iop_module_so_t *module = (dt_iop_module_so_t *)(modules->data);
if(!strcmp(module->op, "flip"))
{
flip = module;
break;
}
}
}
dt_image_orientation_t orientation = ORIENTATION_NULL;
// db lookup flip params
if(flip && flip->have_introspection && flip->get_p)
{
sqlite3_stmt *stmt;
DT_DEBUG_SQLITE3_PREPARE_V2(
dt_database_get(darktable.db),
"SELECT op_params, enabled"
" FROM main.history"
" WHERE imgid=?1 AND operation='flip'"
" ORDER BY num DESC LIMIT 1", -1,
&stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
if(sqlite3_step(stmt) == SQLITE_ROW && sqlite3_column_int(stmt, 1) != 0)
{
// use introspection to get the orientation from the binary params blob
const void *params = sqlite3_column_blob(stmt, 0);
orientation = *((dt_image_orientation_t *)flip->get_p(params, "orientation"));
}
sqlite3_finalize(stmt);
}
if(orientation == ORIENTATION_NULL)
{
const dt_image_t *img = dt_image_cache_get(darktable.image_cache, imgid, 'r');
orientation = dt_image_orientation(img);
dt_image_cache_read_release(darktable.image_cache, img);
}
return orientation;
}
void dt_image_flip(const int32_t imgid, const int32_t cw)
{
// this is light table only:
const dt_view_t *cv = dt_view_manager_get_current_view(darktable.view_manager);
if(darktable.develop->image_storage.id == imgid && cv->view((dt_view_t *)cv) == DT_VIEW_DARKROOM) return;
dt_undo_lt_history_t *hist = dt_history_snapshot_item_init();
hist->imgid = imgid;
dt_history_snapshot_undo_create(hist->imgid, &hist->before, &hist->before_history_end);
dt_image_orientation_t orientation = dt_image_get_orientation(imgid);
if(cw == 1)
{
if(orientation & ORIENTATION_SWAP_XY)
orientation ^= ORIENTATION_FLIP_Y;
else
orientation ^= ORIENTATION_FLIP_X;
}
else
{
if(orientation & ORIENTATION_SWAP_XY)
orientation ^= ORIENTATION_FLIP_X;
else
orientation ^= ORIENTATION_FLIP_Y;
}
orientation ^= ORIENTATION_SWAP_XY;
if(cw == 2) orientation = ORIENTATION_NULL;
dt_image_set_flip(imgid, orientation);
dt_history_snapshot_undo_create(hist->imgid, &hist->after, &hist->after_history_end);
dt_undo_record(darktable.undo, NULL, DT_UNDO_LT_HISTORY, (dt_undo_data_t)hist,
dt_history_snapshot_undo_pop, dt_history_snapshot_undo_lt_history_data_free);
}
/* About the image size ratio
It has been calculated from the exif data width&height, this is not exact as we crop
the sensor data in most cases for raws.
This is managed by
rawspeed - knowing about default crops
rawprepare - modify the defaults
The database does **not** hold the cropped width & height so we fill the data
when starting to develop.
*/
float dt_image_get_sensor_ratio(const struct dt_image_t *img)
{
if(img->p_height >0)
return (double)img->p_width / (double)img->p_height;
return (double)img->width / (double)img->height;
}
void dt_image_set_raw_aspect_ratio(const int32_t imgid)
{
/* fetch image from cache */
dt_image_t *image = dt_image_cache_get(darktable.image_cache, imgid, 'w');
/* set image aspect ratio */
if(image->orientation < ORIENTATION_SWAP_XY)
image->aspect_ratio = (float )image->width / (float )image->height;
else
image->aspect_ratio = (float )image->height / (float )image->width;
/* store */
dt_image_cache_write_release(darktable.image_cache, image, DT_IMAGE_CACHE_SAFE);
}
void dt_image_set_aspect_ratio_to(const int32_t imgid, const float aspect_ratio, const gboolean raise)
{
if (aspect_ratio > .0f)
{
/* fetch image from cache */
dt_image_t *image = dt_image_cache_get(darktable.image_cache, imgid, 'w');
/* set image aspect ratio */
image->aspect_ratio = aspect_ratio;
/* store but don't save xmp*/
dt_image_cache_write_release(darktable.image_cache, image, DT_IMAGE_CACHE_RELAXED);
if(raise && darktable.collection->params.sort == DT_COLLECTION_SORT_ASPECT_RATIO)
dt_collection_update_query(darktable.collection, DT_COLLECTION_CHANGE_RELOAD,
DT_COLLECTION_PROP_ASPECT_RATIO, g_list_prepend(NULL, GINT_TO_POINTER(imgid)));
}
}
void dt_image_set_aspect_ratio_if_different(const int32_t imgid, const float aspect_ratio, const gboolean raise)
{
if (aspect_ratio > .0f)
{
/* fetch image from cache */
dt_image_t *image = dt_image_cache_get(darktable.image_cache, imgid, 'r');
/* set image aspect ratio */
if(fabs(image->aspect_ratio - aspect_ratio) > 0.1)
{
dt_image_cache_read_release(darktable.image_cache, image);
dt_image_t *wimage = dt_image_cache_get(darktable.image_cache, imgid, 'w');
wimage->aspect_ratio = aspect_ratio;
dt_image_cache_write_release(darktable.image_cache, wimage, DT_IMAGE_CACHE_SAFE);
}
else
dt_image_cache_read_release(darktable.image_cache, image);
if(raise && darktable.collection->params.sort == DT_COLLECTION_SORT_ASPECT_RATIO)
dt_collection_update_query(darktable.collection, DT_COLLECTION_CHANGE_RELOAD,
DT_COLLECTION_PROP_ASPECT_RATIO, g_list_prepend(NULL, GINT_TO_POINTER(imgid)));
}
}
void dt_image_reset_aspect_ratio(const int32_t imgid, const gboolean raise)
{
/* fetch image from cache */
dt_image_t *image = dt_image_cache_get(darktable.image_cache, imgid, 'w');
/* set image aspect ratio */
image->aspect_ratio = 0.f;
/* store */
dt_image_cache_write_release(darktable.image_cache, image, DT_IMAGE_CACHE_SAFE);
if(raise && darktable.collection->params.sort == DT_COLLECTION_SORT_ASPECT_RATIO)
dt_collection_update_query(darktable.collection, DT_COLLECTION_CHANGE_RELOAD, DT_COLLECTION_PROP_ASPECT_RATIO,
g_list_prepend(NULL, GINT_TO_POINTER(imgid)));
}
float dt_image_set_aspect_ratio(const int32_t imgid, const gboolean raise)
{
dt_mipmap_buffer_t buf;
float aspect_ratio = 0.0;
// mipmap cache must be initialized, otherwise we'll update next call
if(darktable.mipmap_cache)
{
dt_mipmap_cache_get(darktable.mipmap_cache, &buf, imgid, DT_MIPMAP_0, DT_MIPMAP_BLOCKING, 'r');
if(buf.buf && buf.height && buf.width)
{
aspect_ratio = (float)buf.width / (float)buf.height;
dt_image_set_aspect_ratio_to(imgid, aspect_ratio, raise);
}
dt_mipmap_cache_release(darktable.mipmap_cache, &buf);
}
return aspect_ratio;
}
int32_t dt_image_duplicate(const int32_t imgid)
{
return dt_image_duplicate_with_version(imgid, -1);
}
static int32_t _image_duplicate_with_version_ext(const int32_t imgid, const int32_t newversion)
{
sqlite3_stmt *stmt;
int32_t newid = -1;
const int64_t image_position = dt_collection_get_image_position(imgid, 0);
const int64_t new_image_position = (image_position < 0) ? max_image_position() : image_position + 1;
dt_collection_shift_image_positions(1, new_image_position, 0);
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"SELECT a.id"
" FROM main.images AS a JOIN main.images AS b"
" WHERE a.film_id = b.film_id AND a.filename = b.filename"
" AND b.id = ?1 AND a.version = ?2"
" ORDER BY a.id DESC",
-1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, newversion);
if(sqlite3_step(stmt) == SQLITE_ROW)
{
newid = sqlite3_column_int(stmt, 0);
}
sqlite3_finalize(stmt);
// requested version is already present in DB, so we just return it
if(newid != -1) return newid;
DT_DEBUG_SQLITE3_PREPARE_V2
(dt_database_get(darktable.db),
"INSERT INTO main.images"
" (id, group_id, film_id, width, height, filename, maker, model, lens, exposure,"
" aperture, iso, focal_length, focus_distance, datetime_taken, flags,"
" output_width, output_height, crop, raw_parameters, raw_denoise_threshold,"
" raw_auto_bright_threshold, raw_black, raw_maximum,"
" license, sha1sum, orientation, histogram, lightmap,"
" longitude, latitude, altitude, color_matrix, colorspace, version, max_version, history_end,"
" position, aspect_ratio, exposure_bias, import_timestamp)"
" SELECT NULL, group_id, film_id, width, height, filename, maker, model, lens,"