Ansel 0.0
A darktable fork - bloat + design vision
Loading...
Searching...
No Matches
image_repository.c
Go to the documentation of this file.
1/*
2 This file is part of darktable,
3 Copyright (C) 2025-2026 Aurélien PIERRE.
4 Copyright (C) 2026 Guillaume Stutin.
5
6 darktable is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 darktable is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with darktable. If not, see <http://www.gnu.org/licenses/>.
18*/
19
20#include <math.h> // NAN, the "no colour matrix" sentinel restored below
21#include <string.h>
22
24
26#include "database/database.h"
27#include "common/datetime.h"
28#include "database/sql_debug.h"
29#include "common/image.h"
30#include "common/logging.h"
31#include "system/dtpthread.h"
32#include "system/macros.h"
33
34#include <inttypes.h>
35#include <sqlite3.h>
36
37/* One statement cache for the whole process, guarded by one mutex, exactly as the image cache
38 * held it before the split: these statements are reused on every image load and every write,
39 * and re-preparing them per call was measurable. The mutex is what makes reuse safe, and it
40 * is why the calls below serialise against each other. */
41static sqlite3_stmt *_image_load_stmt = NULL;
42static sqlite3_stmt *_image_write_history_hash_stmt = NULL;
43static sqlite3_stmt *_image_write_timestamp_select_stmt = NULL;
44static sqlite3_stmt *_image_write_timestamp_update_stmt = NULL;
45static sqlite3_stmt *_image_set_flags_stmt = NULL;
46static sqlite3_stmt *_image_set_flags_masked_stmt = NULL;
47static dt_pthread_mutex_t _image_stmt_mutex;
48static gsize _image_stmt_mutex_inited = 0;
49
50static inline void _image_stmt_mutex_ensure(void)
51{
52 if(g_once_init_enter(&_image_stmt_mutex_inited))
53 {
55 g_once_init_leave(&_image_stmt_mutex_inited, 1);
56 }
57}
58
59static sqlite3_stmt *_image_get_stmt(void)
60{
62 {
63 // clang-format off
66 "SELECT i.id, i.group_id, "
67 " (SELECT COUNT(id) FROM main.images WHERE group_id = i.group_id), "
68 " (SELECT COUNT(imgid) FROM main.history WHERE imgid = i.id), "
69 " COALESCE((SELECT current_hash FROM main.history_hash WHERE imgid = i.id), -1), "
70 " COALESCE((SELECT mipmap_hash FROM main.history_hash WHERE imgid = i.id), -1), "
71 " i.film_id, i.version, i.width, i.height, i.orientation, i.flags, "
72 " i.import_timestamp, i.change_timestamp, i.export_timestamp, i.print_timestamp, "
73 " i.exposure, i.exposure_bias, i.aperture, i.iso, i.focal_length, i.focus_distance, "
74 " i.datetime_taken, i.longitude, i.latitude, i.altitude, "
75 " i.filename, f.folder || '" G_DIR_SEPARATOR_S "' || i.filename, "
76 " i.maker, i.model, i.lens, f.folder, "
77 " COALESCE((SELECT SUM(1 << color) FROM main.color_labels WHERE imgid=i.id), 0), "
78 " i.crop, i.raw_parameters, i.color_matrix, i.colorspace, "
79 " i.raw_black, i.raw_maximum, i.aspect_ratio, i.output_width, i.output_height"
80 " FROM main.images AS i"
81 " LEFT JOIN main.film_rolls AS f ON f.id = i.film_id"
82 " WHERE i.id = ?1",
83 -1, &_image_load_stmt, NULL);
84 // clang-format on
85 }
86
87 sqlite3_reset(_image_load_stmt);
88 sqlite3_clear_bindings(_image_load_stmt);
89 return _image_load_stmt;
90}
91
92static void dt_image_from_stmt(dt_image_t *img, sqlite3_stmt *stmt)
93{
94 if(sqlite3_column_type(stmt, 0) != SQLITE_NULL) img->id = sqlite3_column_int(stmt, 0);
95 if(sqlite3_column_type(stmt, 1) != SQLITE_NULL) img->group_id = sqlite3_column_int(stmt, 1);
96 if(sqlite3_column_type(stmt, 2) != SQLITE_NULL) img->group_members = (uint32_t)sqlite3_column_int(stmt, 2);
97 if(sqlite3_column_type(stmt, 3) != SQLITE_NULL) img->history_items = (uint32_t)sqlite3_column_int(stmt, 3);
98 if(sqlite3_column_type(stmt, 4) != SQLITE_NULL) img->history_hash = sqlite3_column_int64(stmt, 4);
99 if(sqlite3_column_type(stmt, 5) != SQLITE_NULL) img->mipmap_hash = sqlite3_column_int64(stmt, 5);
100 if(sqlite3_column_type(stmt, 6) != SQLITE_NULL) img->film_id = sqlite3_column_int(stmt, 6);
101 if(sqlite3_column_type(stmt, 7) != SQLITE_NULL) img->version = sqlite3_column_int(stmt, 7);
102 if(sqlite3_column_type(stmt, 8) != SQLITE_NULL) img->width = sqlite3_column_int(stmt, 8);
103 if(sqlite3_column_type(stmt, 9) != SQLITE_NULL) img->height = sqlite3_column_int(stmt, 9);
104 if(sqlite3_column_type(stmt, 10) != SQLITE_NULL) img->orientation = sqlite3_column_int(stmt, 10);
105 if(sqlite3_column_type(stmt, 11) != SQLITE_NULL) img->flags = sqlite3_column_int(stmt, 11);
106 if(sqlite3_column_type(stmt, 12) != SQLITE_NULL) img->import_timestamp = sqlite3_column_int64(stmt, 12);
107 if(sqlite3_column_type(stmt, 13) != SQLITE_NULL) img->change_timestamp = sqlite3_column_int64(stmt, 13);
108 if(sqlite3_column_type(stmt, 14) != SQLITE_NULL) img->export_timestamp = sqlite3_column_int64(stmt, 14);
109 if(sqlite3_column_type(stmt, 15) != SQLITE_NULL) img->print_timestamp = sqlite3_column_int64(stmt, 15);
110 if(sqlite3_column_type(stmt, 16) != SQLITE_NULL) img->exif_exposure = sqlite3_column_double(stmt, 16);
111 if(sqlite3_column_type(stmt, 17) != SQLITE_NULL) img->exif_exposure_bias = sqlite3_column_double(stmt, 17);
112 if(sqlite3_column_type(stmt, 18) != SQLITE_NULL) img->exif_aperture = sqlite3_column_double(stmt, 18);
113 if(sqlite3_column_type(stmt, 19) != SQLITE_NULL) img->exif_iso = sqlite3_column_double(stmt, 19);
114 if(sqlite3_column_type(stmt, 20) != SQLITE_NULL) img->exif_focal_length = sqlite3_column_double(stmt, 20);
115 if(sqlite3_column_type(stmt, 21) != SQLITE_NULL) img->exif_focus_distance = sqlite3_column_double(stmt, 21);
116 if(sqlite3_column_type(stmt, 22) != SQLITE_NULL) img->exif_datetime_taken = sqlite3_column_int64(stmt, 22);
117 if(sqlite3_column_type(stmt, 23) != SQLITE_NULL) img->geoloc.longitude = sqlite3_column_double(stmt, 23);
118 if(sqlite3_column_type(stmt, 24) != SQLITE_NULL) img->geoloc.latitude = sqlite3_column_double(stmt, 24);
119 if(sqlite3_column_type(stmt, 25) != SQLITE_NULL) img->geoloc.elevation = sqlite3_column_double(stmt, 25);
120
121 const char *filename = (const char *)sqlite3_column_text(stmt, 26);
122 if(filename) g_strlcpy(img->filename, filename, sizeof(img->filename));
123 const char *fullpath = (const char *)sqlite3_column_text(stmt, 27);
124 if(fullpath) g_strlcpy(img->fullpath, fullpath, sizeof(img->fullpath));
125 const char *maker = (const char *)sqlite3_column_text(stmt, 28);
126 if(maker) g_strlcpy(img->exif_maker, maker, sizeof(img->exif_maker));
127 const char *model = (const char *)sqlite3_column_text(stmt, 29);
128 if(model) g_strlcpy(img->exif_model, model, sizeof(img->exif_model));
129 const char *lens = (const char *)sqlite3_column_text(stmt, 30);
130 if(lens) g_strlcpy(img->exif_lens, lens, sizeof(img->exif_lens));
131 const char *folder = (const char *)sqlite3_column_text(stmt, 31);
132 if(folder) g_strlcpy(img->folder, folder, sizeof(img->folder));
133
134 if(sqlite3_column_type(stmt, 32) != SQLITE_NULL) img->color_labels = sqlite3_column_int(stmt, 32);
135
136 if(sqlite3_column_type(stmt, 33) != SQLITE_NULL) img->exif_crop = sqlite3_column_double(stmt, 33);
137 if(sqlite3_column_type(stmt, 34) != SQLITE_NULL)
138 {
139 uint32_t tmp = sqlite3_column_int(stmt, 34);
140 memcpy(&img->legacy_flip, &tmp, sizeof(dt_image_raw_parameters_t));
141 }
142 /* Size the copy from the blob, not from the destination: the writer binds exactly
143 * sizeof(d65_color_matrix) (see dt_image_repository_store()), so anything shorter is a row this
144 * schema did not write so anything else is a row no writer of this schema produces and reading nine
145 * floats out of it is undefined. No null test: sqlite returns NULL only for a NULL or a zero-length
146 * value, so a size of exactly 36 already says the pointer is good. sqlite3_column_bytes() comes AFTER
147 * sqlite3_column_blob() on purpose -- that is the order sqlite3.h documents as safe, a size
148 * asked for first being allowed to describe a value the later call converts.
149 *
150 * The else branch is the one that runs in practice, on the NULL column of a row not yet stored.
151 * It matters because dt_image_cache_get_reload() reloads into an already populated struct
152 * WITHOUT going through dt_image_init(): the matrix from a previous load would otherwise
153 * survive a row that no longer carries one. NAN is the documented "no matrix" sentinel the
154 * readers test (imageio_profile.c, iop/colorin.c, pixel/illuminants.h). */
155 const void *color_matrix = sqlite3_column_blob(stmt, 35);
156 if(sqlite3_column_bytes(stmt, 35) == (int)sizeof(img->d65_color_matrix))
157 memcpy(img->d65_color_matrix, color_matrix, sizeof(img->d65_color_matrix));
158 else
159 img->d65_color_matrix[0] = NAN;
160 if(sqlite3_column_type(stmt, 36) != SQLITE_NULL) img->colorspace = sqlite3_column_int(stmt, 36);
161 if(sqlite3_column_type(stmt, 37) != SQLITE_NULL) img->raw_black_level = sqlite3_column_int(stmt, 37);
162 if(sqlite3_column_type(stmt, 38) != SQLITE_NULL) img->raw_white_point = sqlite3_column_int(stmt, 38);
163
164 if(img->fullpath[0])
166 sizeof(img->local_copy_path), img->local_copy_legacy_path,
167 sizeof(img->local_copy_legacy_path));
168
169 img->exif_inited = (img->exif_focus_distance >= 0 && img->orientation >= 0);
170
171 if(img->folder[0])
172 g_strlcpy(img->filmroll, dt_image_film_roll_name(img->folder), sizeof(img->filmroll));
173
175
176 // img->dsc are written by imageio drivers : never (re)set them from DB,
177 // they are not saved anyway. Until the codec decodes the file, seed a provisional
178 // descriptor from the (extension-derived) pipeline class so the image can be reasoned
179 // about and the first pipeline stage has a usable contract even before decoding.
181
182 /* Everything past the columns -- rating, monochrome and HDR predicates, the
183 * extension cross-check, makermodel -- is DERIVED, not stored, and needs symbols from
184 * imageio/ and views/. A row mapper that reached two layers up for them is what kept this
185 * code in common/. Callers run dt_image_derive_fields() next; see image_repository.h. */
186
187}
188
190{
191 if(IS_NULL_PTR(img) || img->id <= 0) return;
192
196 {
197 // clang-format off
200 "INSERT INTO main.history_hash (imgid, current_hash, basic_hash, auto_hash, mipmap_hash)"
201 " VALUES (?1, ?2, NULL, NULL, ?3)"
202 " ON CONFLICT (imgid)"
203 " DO UPDATE SET current_hash = ?2, basic_hash = NULL, auto_hash = NULL, mipmap_hash = ?3",
205 // clang-format on
206 }
207 sqlite3_stmt *stmt = _image_write_history_hash_stmt;
208 sqlite3_reset(stmt);
209 sqlite3_clear_bindings(stmt);
210 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, img->id);
211 DT_DEBUG_SQLITE3_BIND_INT64(stmt, 2, (sqlite3_int64)img->history_hash);
212 DT_DEBUG_SQLITE3_BIND_INT64(stmt, 3, (sqlite3_int64)img->mipmap_hash);
213 sqlite3_step(stmt);
215}
216
217
218gboolean dt_image_repository_load(const int32_t imgid, dt_image_t *img)
219{
220 if(IS_NULL_PTR(img)) return FALSE;
221
222 gboolean found = FALSE;
225
226 sqlite3_stmt *stmt = _image_get_stmt();
227 sqlite3_reset(stmt);
228 sqlite3_clear_bindings(stmt);
229 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
230 const int rc = sqlite3_step(stmt);
231 if(rc == SQLITE_ROW)
232 {
233 dt_image_from_stmt(img, stmt);
234 found = TRUE;
235 }
236 else if(rc == SQLITE_DONE)
237 {
238 // no such row: the image was removed, which is ordinary and not an error
239 img->id = UNKNOWN_IMAGE;
240 }
241 else
242 {
243 /* The read failed (SQLITE_BUSY while another program holds the library's lock, an I/O
244 * error), which says nothing about the image, so img is left as it was.
245 * dt_image_cache_get_reload() reloads a live entry in place: marking it invalid here would
246 * make dt_image_cache_get() answer NULL for that id for as long as the entry stays cached. */
247 fprintf(stderr, "[image_repository_load] could not read image %" PRId32 " from the database: %s\n",
248 imgid, sqlite3_errstr(rc));
249 }
250
252 return found;
253}
254
256{
257 if(IS_NULL_PTR(cb)) return;
258
259 sqlite3_stmt *stmt = NULL;
260 // clang-format off
263 // Same columns, same order, as the single-image load above -- dt_image_from_stmt() maps
264 // both. Only the source differs: the collection instead of one id.
265 "SELECT i.id, i.group_id, "
266 " (SELECT COUNT(id) FROM main.images WHERE group_id = i.group_id), "
267 " (SELECT COUNT(imgid) FROM main.history WHERE imgid = i.id), "
268 " COALESCE((SELECT current_hash FROM main.history_hash WHERE imgid = i.id), -1), "
269 " COALESCE((SELECT mipmap_hash FROM main.history_hash WHERE imgid = i.id), -1), "
270 " i.film_id, i.version, i.width, i.height, i.orientation, i.flags, "
271 " i.import_timestamp, i.change_timestamp, i.export_timestamp, i.print_timestamp, "
272 " i.exposure, i.exposure_bias, i.aperture, i.iso, i.focal_length, i.focus_distance, "
273 " i.datetime_taken, i.longitude, i.latitude, i.altitude, "
274 " i.filename, f.folder || '" G_DIR_SEPARATOR_S "' || i.filename, "
275 " i.maker, i.model, i.lens, f.folder, "
276 " COALESCE((SELECT SUM(1 << color) FROM main.color_labels WHERE imgid=i.id), 0), "
277 " i.crop, i.raw_parameters, i.color_matrix, i.colorspace, "
278 " i.raw_black, i.raw_maximum, i.aspect_ratio, i.output_width, i.output_height"
279 " FROM main.images AS i"
280 " JOIN memory.collected_images AS c ON i.id = c.imgid"
281 " LEFT JOIN main.film_rolls AS f ON f.id = i.film_id"
282 " ORDER BY c.rowid ASC",
283 -1, &stmt, NULL);
284 // clang-format on
285 if(IS_NULL_PTR(stmt)) return;
286
287 while(sqlite3_step(stmt) == SQLITE_ROW)
288 {
289 dt_image_t info;
290 dt_image_init(&info);
291 dt_image_from_stmt(&info, stmt);
292 // No lock is held here on purpose: see the header. cb() reaches the selection and the
293 // image cache, and either can come back through this function.
294 cb(user_data, &info);
295 }
296 sqlite3_finalize(stmt);
297}
298
299
301{
302 if(IS_NULL_PTR(img) || img->id <= 0) return;
303
304 union {
306 uint32_t u;
307 } flip;
308
309 sqlite3_stmt *stmt;
310 // clang-format off
312 "UPDATE main.images"
313 " SET width = ?1, height = ?2, filename = ?3, maker = ?4, model = ?5,"
314 " lens = ?6, exposure = ?7, aperture = ?8, iso = ?9, focal_length = ?10,"
315 " focus_distance = ?11, film_id = ?12, datetime_taken = ?13, flags = ?14,"
316 " crop = ?15, orientation = ?16, raw_parameters = ?17, group_id = ?18,"
317 " longitude = ?19, latitude = ?20, altitude = ?21, color_matrix = ?22,"
318 " colorspace = ?23, raw_black = ?24, raw_maximum = ?25,"
319 " aspect_ratio = ROUND(?26,1), exposure_bias = ?27,"
320 " import_timestamp = ?28, change_timestamp = ?29, export_timestamp = ?30,"
321 " print_timestamp = ?31, output_width = ?32, output_height = ?33"
322 " WHERE id = ?34",
323 -1, &stmt, NULL);
324 // clang-format on
325 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, img->width);
326 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, img->height);
327 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 3, img->filename, -1, SQLITE_STATIC);
328 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 4, img->exif_maker, -1, SQLITE_STATIC);
329 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 5, img->exif_model, -1, SQLITE_STATIC);
330 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 6, img->exif_lens, -1, SQLITE_STATIC);
336 DT_DEBUG_SQLITE3_BIND_INT(stmt, 12, img->film_id);
337 if(img->exif_datetime_taken)
339 DT_DEBUG_SQLITE3_BIND_INT(stmt, 14, img->flags);
342 flip.s = img->legacy_flip;
343 DT_DEBUG_SQLITE3_BIND_INT(stmt, 17, flip.u);
344 DT_DEBUG_SQLITE3_BIND_INT(stmt, 18, img->group_id);
348 DT_DEBUG_SQLITE3_BIND_BLOB(stmt, 22, &img->d65_color_matrix, sizeof(img->d65_color_matrix), SQLITE_STATIC);
352 DT_DEBUG_SQLITE3_BIND_DOUBLE(stmt, 26, 0.); // img->aspect_ratio deprecated
354 if(img->import_timestamp)
356 if(img->change_timestamp)
358 if(img->export_timestamp)
360 if(img->print_timestamp)
362 DT_DEBUG_SQLITE3_BIND_INT(stmt, 32, 0); // img->final_width deprecated
363 DT_DEBUG_SQLITE3_BIND_INT(stmt, 33, 0); // img->final_height deprecated
364 DT_DEBUG_SQLITE3_BIND_INT(stmt, 34, img->id);
365 const int rc = sqlite3_step(stmt);
366 if(rc != SQLITE_DONE) fprintf(stderr, "[image_cache_write_release] sqlite3 error %d\n", rc);
367 sqlite3_finalize(stmt);
368
369 /* Straight to the table. This used to call dt_colorlabels_set_labels() in common/, i.e.
370 * the persistence layer reaching up into the domain to have it issue the queries the
371 * persistence layer is for -- the only edge in that direction here. */
372 for(int color = 0; color < 5; color++)
373 {
374 if(img->color_labels & (1 << color))
376 else
378 }
380}
381
382
383/* ---------------------------------------------------------------------------------------
384 * Grouping
385 * ------------------------------------------------------------------------------------- */
386
387/* Collect the `id` column of a stepped statement into a list, dropping @p exclude_imgid. */
388static GList *_collect_ids(sqlite3_stmt *stmt, const int32_t exclude_imgid)
389{
390 GList *ids = NULL;
391 while(sqlite3_step(stmt) == SQLITE_ROW)
392 {
393 const int32_t id = sqlite3_column_int(stmt, 0);
394 if(id != exclude_imgid) ids = g_list_prepend(ids, GINT_TO_POINTER(id));
395 }
396 sqlite3_finalize(stmt);
397 return g_list_reverse(ids);
398}
399
400GList *dt_image_repository_get_group_members(const int32_t group_id, const int32_t exclude_imgid)
401{
402 sqlite3_stmt *stmt = NULL;
404 "SELECT id FROM main.images WHERE group_id = ?1", -1, &stmt, NULL);
405 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, group_id);
406 return _collect_ids(stmt, exclude_imgid);
407}
408
410{
412 if(IS_NULL_PTR(member)) return;
413 dt_free(member->filename);
414 dt_free(member);
415}
416
417GList *dt_image_repository_get_group_member_rows(const int32_t group_id)
418{
419 GList *members = NULL;
420 sqlite3_stmt *stmt = NULL;
421 // clang-format off
423 "SELECT id, version, filename"
424 " FROM main.images"
425 " WHERE group_id = ?1", -1, &stmt,
426 NULL);
427 // clang-format on
428 if(IS_NULL_PTR(stmt)) return NULL;
429
430 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, group_id);
431 while(sqlite3_step(stmt) == SQLITE_ROW)
432 {
434 if(IS_NULL_PTR(member)) break;
435 member->imgid = sqlite3_column_int(stmt, 0);
436 member->version = sqlite3_column_int(stmt, 1);
437 const char *filename = (const char *)sqlite3_column_text(stmt, 2);
438 member->filename = filename ? g_strdup(filename) : NULL;
439 members = g_list_prepend(members, member);
440 }
441 sqlite3_finalize(stmt);
442
443 // Row order: the caller walks it once to build a tooltip listing the group, and the order
444 // it reads is the order the rows came in.
445 return g_list_reverse(members);
446}
447
448int dt_image_repository_count_in_id_range(const int32_t min_imgid, const int32_t max_imgid)
449{
450 sqlite3_stmt *stmt = NULL;
452 "SELECT COUNT(*) FROM main.images WHERE id >= ?1 AND id <= ?2", -1,
453 &stmt, 0);
454 if(IS_NULL_PTR(stmt)) return -1;
455
456 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, min_imgid);
457 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, max_imgid);
458 const int count = (sqlite3_step(stmt) == SQLITE_ROW) ? sqlite3_column_int(stmt, 0) : -1;
459 sqlite3_finalize(stmt);
460
461 return count;
462}
463
464void dt_image_repository_foreach_in_id_range(const int32_t min_imgid, const int32_t max_imgid,
466 void *user_data)
467{
468 if(IS_NULL_PTR(cb)) return;
469
470 sqlite3_stmt *stmt = NULL;
472 "SELECT id, filename FROM main.images WHERE id >= ?1 AND id <= ?2", -1,
473 &stmt, 0);
474 if(IS_NULL_PTR(stmt)) return;
475
476 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, min_imgid);
477 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, max_imgid);
478 while(sqlite3_step(stmt) == SQLITE_ROW)
479 cb(sqlite3_column_int(stmt, 0), (const char *)sqlite3_column_text(stmt, 1), user_data);
480 sqlite3_finalize(stmt);
481}
482
483
484void dt_image_repository_reassign_group(const int32_t from_group_id, const int32_t to_group_id,
485 const int32_t exclude_imgid)
486{
487 sqlite3_stmt *stmt = NULL;
488 // clang-format off
490 "UPDATE main.images SET group_id = ?1 WHERE group_id = ?2 AND id != ?3",
491 -1, &stmt, NULL);
492 // clang-format on
493 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, to_group_id);
494 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, from_group_id);
495 DT_DEBUG_SQLITE3_BIND_INT(stmt, 3, exclude_imgid);
496 sqlite3_step(stmt);
497 sqlite3_finalize(stmt);
498}
499
500GList *dt_image_repository_get_ratings(const int32_t imgid)
501{
502 sqlite3_stmt *stmt = NULL;
503
504 if(imgid < 0)
505 {
506 // clang-format off
508 "SELECT flags FROM main.images WHERE id IN "
509 "(SELECT imgid FROM main.selected_images)",
510 -1, &stmt, NULL);
511 // clang-format on
512 }
513 else
514 {
515 // clang-format off
517 "SELECT flags FROM main.images WHERE id = ?1", -1, &stmt, NULL);
518 // clang-format on
519 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
520 }
521
522 GList *result = NULL;
523 while(sqlite3_step(stmt) == SQLITE_ROW)
524 {
525 const int stars = (sqlite3_column_int(stmt, 0) & 0x7) - 1;
526 result = g_list_prepend(result, GINT_TO_POINTER(stars));
527 }
528 sqlite3_finalize(stmt);
529
530 return g_list_reverse(result);
531}
532
533/* ---------------------------------------------------------------------------------------------
534 * Identity lookups
535 * ------------------------------------------------------------------------------------------ */
536
537int32_t dt_image_repository_find_by_film_and_filename(const int32_t film_id, const char *filename)
538{
539 if(IS_NULL_PTR(filename)) return -1;
540
541 int32_t id = -1;
542 sqlite3_stmt *stmt;
544 "SELECT id FROM main.images WHERE film_id = ?1 AND filename = ?2",
545 -1, &stmt, NULL);
546 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, film_id);
547 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 2, filename, -1, SQLITE_TRANSIENT);
548 if(sqlite3_step(stmt) == SQLITE_ROW) id=sqlite3_column_int(stmt, 0);
549 sqlite3_finalize(stmt);
550 return id;
551}
552
553int32_t dt_image_repository_find_by_folder_and_filename(const char *folder, const char *filename)
554{
555 if(IS_NULL_PTR(folder) || IS_NULL_PTR(filename)) return -1;
556
557 int32_t id = -1;
558 sqlite3_stmt *stmt;
559 // clang-format off
561 "SELECT images.id"
562 " FROM main.images, main.film_rolls"
563 " WHERE film_rolls.folder = ?1"
564 " AND images.film_id = film_rolls.id"
565 " AND images.filename = ?2",
566 -1, &stmt, NULL);
567 // clang-format on
568 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 1, folder, -1, SQLITE_STATIC);
569 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 2, filename, -1, SQLITE_STATIC);
570 if(sqlite3_step(stmt) == SQLITE_ROW) id=sqlite3_column_int(stmt, 0);
571 sqlite3_finalize(stmt);
572
573 return id;
574}
575
576
577/* ---------------------------------------------------------------------------------------------
578 * Versions, flags and the write timestamp
579 * ------------------------------------------------------------------------------------------ */
580
581int dt_image_repository_get_version(const int32_t imgid)
582{
583 int version = 0;
584 sqlite3_stmt *stmt;
585 DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get_sqlite3_global(), "SELECT version FROM main.images WHERE id = ?1", -1,
586 &stmt, NULL);
587 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
588
589 if(sqlite3_step(stmt) == SQLITE_ROW) version = sqlite3_column_int(stmt, 0);
590 sqlite3_finalize(stmt);
591 return version;
592}
593
594gboolean dt_image_repository_set_version(const int32_t imgid, const int version)
595{
596 sqlite3_stmt *stmt;
599 "UPDATE main.images SET version=?1, max_version = ?1 WHERE id = ?2", -1, &stmt, NULL);
600 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, version);
601 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, imgid);
602 const gboolean ok = (sqlite3_step(stmt) == SQLITE_DONE);
603 sqlite3_finalize(stmt);
604 return ok;
605}
606
607int dt_image_repository_count_others_with_flag(const int32_t imgid, const int flag)
608{
609 sqlite3_stmt *stmt;
610 int result = 1;
611
612 // clang-format off
614 "SELECT COUNT(*)"
615 " FROM main.images"
616 " WHERE id!=?1 AND flags&?2=?2"
617 " AND film_id=(SELECT film_id"
618 " FROM main.images"
619 " WHERE id=?1)"
620 " AND filename=(SELECT filename"
621 " FROM main.images"
622 " WHERE id=?1);",
623 -1, &stmt, NULL);
624 // clang-format on
625 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
627 if(sqlite3_step(stmt) == SQLITE_ROW) result = sqlite3_column_int(stmt, 0);
628 sqlite3_finalize(stmt);
629
630 return result;
631}
632
637static char *_id_set(GList *imgids)
638{
639 if(IS_NULL_PTR(imgids)) return NULL;
640
641 GString *set = g_string_new(NULL);
642 for(GList *l = imgids; l; l = g_list_next(l))
643 {
644 if(l != imgids) g_string_append_c(set, ',');
645 g_string_append_printf(set, "%d", GPOINTER_TO_INT(l->data));
646 }
647 return g_string_free(set, FALSE);
648}
649
650GList *dt_image_repository_get_ids_with_flag_among(GList *imgids, const int flag)
651{
652 char *set = _id_set(imgids);
653 if(IS_NULL_PTR(set)) return NULL;
654
655 GList *ids = NULL;
656 sqlite3_stmt *stmt;
657 char *query = g_strdup_printf("SELECT id FROM main.images WHERE id IN (%s) AND flags&?1=?1", set);
660 while(sqlite3_step(stmt) == SQLITE_ROW)
661 ids = g_list_prepend(ids, GINT_TO_POINTER(sqlite3_column_int(stmt, 0)));
662 sqlite3_finalize(stmt);
663 dt_free(query);
664 dt_free(set);
665 return g_list_reverse(ids); // row order
666}
667
668gboolean dt_image_repository_set_flag_among(GList *imgids, const int flag)
669{
670 char *set = _id_set(imgids);
671 if(IS_NULL_PTR(set)) return FALSE;
672
673 sqlite3_stmt *stmt;
674 char *query = g_strdup_printf("UPDATE main.images SET flags = (flags|?1) WHERE id IN (%s)", set);
677 const gboolean ok = (sqlite3_step(stmt) == SQLITE_DONE);
678 sqlite3_finalize(stmt);
679 dt_free(query);
680 dt_free(set);
681 return ok;
682}
683
684gboolean dt_image_repository_clear_flag_among(GList *imgids, const int flag)
685{
686 char *set = _id_set(imgids);
687 if(IS_NULL_PTR(set)) return FALSE;
688
689 sqlite3_stmt *stmt;
690 char *query = g_strdup_printf("UPDATE main.images SET flags = (flags & ~?1) WHERE id IN (%s)", set);
693 const gboolean ok = (sqlite3_step(stmt) == SQLITE_DONE);
694 sqlite3_finalize(stmt);
695 dt_free(query);
696 dt_free(set);
697 return ok;
698}
699
701{
702 char *set = _id_set(imgids);
703 if(IS_NULL_PTR(set)) return NULL;
704
705 GList *list = NULL;
706 sqlite3_stmt *stmt;
707 // clang-format off
708 char *query = g_strdup_printf("SELECT DISTINCT folder || '" G_DIR_SEPARATOR_S "' || filename FROM "
709 "main.images i, main.film_rolls f "
710 "ON i.film_id = f.id WHERE i.id IN (%s)", set);
711 // clang-format on
713 while(sqlite3_step(stmt) == SQLITE_ROW)
714 list = g_list_prepend(list, g_strdup((const gchar *)sqlite3_column_text(stmt, 0)));
715 sqlite3_finalize(stmt);
716 dt_free(query);
717 dt_free(set);
718 return g_list_reverse(list); // list was built in reverse order, so un-reverse it
719}
720
722{
723 GList *ids = NULL;
724 sqlite3_stmt *stmt;
725 DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get_sqlite3_global(), "SELECT id FROM main.images WHERE flags&?1=?1", -1,
726 &stmt, NULL);
728
729 while(sqlite3_step(stmt) == SQLITE_ROW)
730 ids = g_list_prepend(ids, GINT_TO_POINTER(sqlite3_column_int(stmt, 0)));
731 sqlite3_finalize(stmt);
732
733 // Row order, NOT reverse-row: the caller filters this list and prepends into its own, and
734 // that second prepend reproduces the single prepend the original did off the cursor.
735 return g_list_reverse(ids);
736}
737
738gboolean dt_image_repository_set_flags(const int32_t imgid, const int flags)
739{
740 if(imgid <= 0) return FALSE;
741
745 {
747 "UPDATE main.images SET flags = ?1 WHERE id = ?2",
748 -1, &_image_set_flags_stmt, NULL);
749 }
751 {
753 return FALSE;
754 }
755
758 const gboolean ok = (sqlite3_step(_image_set_flags_stmt) == SQLITE_DONE);
759 sqlite3_reset(_image_set_flags_stmt);
760 sqlite3_clear_bindings(_image_set_flags_stmt);
762
763 return ok;
764}
765
766gboolean dt_image_repository_set_flags_masked(const int32_t imgid, const int mask, const int value)
767{
768 if(imgid <= 0 || mask == 0) return FALSE;
769
773 {
774 // The row supplies the bits outside the mask, so nothing this caller read earlier can be
775 // written back over them. Same shape as dt_image_repository_set_flag_among()'s
776 // `flags|?1` and its clearing twin, with both halves in one statement.
778 "UPDATE main.images SET flags = (flags & ~?1) | (?2 & ?1)"
779 " WHERE id = ?3",
781 }
783 {
785 return FALSE;
786 }
787
791 const gboolean ok = (sqlite3_step(_image_set_flags_masked_stmt) == SQLITE_DONE);
792 sqlite3_reset(_image_set_flags_masked_stmt);
793 sqlite3_clear_bindings(_image_set_flags_masked_stmt);
795
796 return ok;
797}
798
800{
801 if(IS_NULL_PTR(cb)) return;
802
803 sqlite3_stmt *stmt = NULL;
804 // clang-format off
806 "SELECT i.id, write_timestamp, version,"
807 " folder || '" G_DIR_SEPARATOR_S "' || filename, flags"
808 " FROM main.images i, main.film_rolls f"
809 " ON i.film_id = f.id"
810 " ORDER BY f.id, filename",
811 -1, &stmt, NULL);
812 // clang-format on
813 if(IS_NULL_PTR(stmt)) return;
814
815 while(sqlite3_step(stmt) == SQLITE_ROW)
816 {
817 // No lock is held here on purpose: cb() writes back through this repository.
818 if(!cb(sqlite3_column_int(stmt, 0),
819 sqlite3_column_int64(stmt, 1),
820 sqlite3_column_int(stmt, 2),
821 (const char *)sqlite3_column_text(stmt, 3),
822 sqlite3_column_int(stmt, 4),
823 user_data))
824 break;
825 }
826 sqlite3_finalize(stmt);
827}
828
830{
831 char *set = _id_set(imgids);
832 if(IS_NULL_PTR(set)) return NULL;
833
834 // One subquery per metadata key, each needing the id set again -- nine occurrences in all.
835 // clang-format off
836 char *query = g_strdup_printf("SELECT COUNT(DISTINCT film_id), "
837 "COUNT(DISTINCT film_id), "
838 "2, " // imgid always different
839 "COUNT(DISTINCT group_id), "
840 "COUNT(DISTINCT filename), "
841 "COUNT(DISTINCT version), "
842 "COUNT(DISTINCT film_id || '/' || filename), " //path
843 "COUNT(DISTINCT flags & 2048), " //local copy
844 "COUNT(DISTINCT import_timestamp), "
845 "COUNT(DISTINCT change_timestamp), "
846 "COUNT(DISTINCT export_timestamp), "
847 "COUNT(DISTINCT print_timestamp), "
848 "COUNT(DISTINCT flags), "
849 "COUNT(DISTINCT model), "
850 "COUNT(DISTINCT maker), "
851 "COUNT(DISTINCT lens), "
852 "COUNT(DISTINCT aperture), "
853 "COUNT(DISTINCT exposure), "
854 "COUNT(DISTINCT IFNULL(exposure_bias, '')), "
855 "COUNT(DISTINCT focal_length), "
856 "COUNT(DISTINCT focus_distance), "
857 "COUNT(DISTINCT iso), "
858 "COUNT(DISTINCT datetime_taken), "
859 "COUNT(DISTINCT width), "
860 "COUNT(DISTINCT height), "
861 "COUNT(DISTINCT IFNULL(output_width, '')), " //exported width
862 "COUNT(DISTINCT IFNULL(output_height, '')), " //exported height
863 "(SELECT COUNT(DISTINCT IFNULL(value,'')) FROM images LEFT JOIN meta_data ON meta_data.id = images.id AND key = 2 WHERE images.id in (%s)), " //title
864 "(SELECT COUNT(DISTINCT IFNULL(value,'')) FROM images LEFT JOIN meta_data ON meta_data.id = images.id AND key = 3 WHERE images.id in (%s)), " //description
865 "(SELECT COUNT(DISTINCT IFNULL(value,'')) FROM images LEFT JOIN meta_data ON meta_data.id = images.id AND key = 0 WHERE images.id in (%s)), " //creator
866 "(SELECT COUNT(DISTINCT IFNULL(value,'')) FROM images LEFT JOIN meta_data ON meta_data.id = images.id AND key = 1 WHERE images.id in (%s)), " //publisher
867 "(SELECT COUNT(DISTINCT IFNULL(value,'')) FROM images LEFT JOIN meta_data ON meta_data.id = images.id AND key = 4 WHERE images.id in (%s)), " //rights
868 "(SELECT COUNT(DISTINCT IFNULL(value,'')) FROM images LEFT JOIN meta_data ON meta_data.id = images.id AND key = 5 WHERE images.id in (%s)), " //notes
869 "(SELECT COUNT(DISTINCT IFNULL(value,'')) FROM images LEFT JOIN meta_data ON meta_data.id = images.id AND key = 6 WHERE images.id in (%s)), " //version name
870 "(SELECT COUNT(DISTINCT IFNULL(value,'')) FROM images LEFT JOIN meta_data ON meta_data.id = images.id AND key = 7 WHERE images.id in (%s)), " //image id
871 "COUNT(DISTINCT IFNULL(latitude, '')), "
872 "COUNT(DISTINCT IFNULL(longitude, '')), "
873 "COUNT(DISTINCT IFNULL(altitude, '')) "
874 "FROM main.images "
875 "WHERE id IN (%s)",
876 set, set, set, set, set, set, set, set, set);
877 // clang-format on
878
879 sqlite3_stmt *stmt = NULL;
881 dt_free(query);
882 dt_free(set);
883 if(IS_NULL_PTR(stmt)) return NULL;
884
885 int *counts = NULL;
886 if(sqlite3_step(stmt) == SQLITE_ROW)
887 {
888 const int col_count = sqlite3_column_count(stmt);
889 counts = (int *)calloc(DT_IMAGE_FIELD_COUNT, sizeof(int));
890 if(counts)
891 {
892 for(int i = 0; i < DT_IMAGE_FIELD_COUNT && i < col_count; i++)
893 counts[i] = sqlite3_column_int(stmt, i);
894 }
895 }
896 sqlite3_finalize(stmt);
897
898 return counts;
899}
900
902{
903 if(IS_NULL_PTR(bounds)) return FALSE;
904
905 bounds->min_latitude = INFINITY;
906 bounds->max_latitude = -INFINITY;
907 bounds->min_longitude = INFINITY;
908 bounds->max_longitude = -INFINITY;
909 bounds->count = 0;
910
911 sqlite3_stmt *stmt = NULL;
912 // clang-format off
914 "SELECT MIN(latitude), MAX(latitude),"
915 " MIN(longitude), MAX(longitude), COUNT(*)"
916 " FROM main.images AS i "
917 " JOIN memory.collected_images AS l ON l.imgid = i.id "
918 " WHERE latitude NOT NULL AND longitude NOT NULL",
919 -1, &stmt, NULL);
920 // clang-format on
921 if(IS_NULL_PTR(stmt)) return FALSE;
922
923 const gboolean ok = (sqlite3_step(stmt) == SQLITE_ROW);
924 if(ok)
925 {
926 bounds->min_latitude = sqlite3_column_double(stmt, 0);
927 bounds->max_latitude = sqlite3_column_double(stmt, 1);
928 bounds->min_longitude = sqlite3_column_double(stmt, 2);
929 bounds->max_longitude = sqlite3_column_double(stmt, 3);
930 bounds->count = sqlite3_column_int(stmt, 4);
931 }
932 sqlite3_finalize(stmt);
933
934 return ok;
935}
936
938 const double lat1, const double lat2,
939 int *count)
940{
941 if(IS_NULL_PTR(count)) return NULL;
942 *count = 0;
943
944 sqlite3_stmt *stmt = NULL;
945 // clang-format off
947 "SELECT * FROM"
948 " (SELECT i.id, i.longitude, i.latitude "
949 " FROM main.images i INNER JOIN memory.collected_images c ON i.id = c.imgid"
950 " WHERE longitude >= ?1 AND longitude <= ?2"
951 " AND latitude <= ?3 AND latitude >= ?4 "
952 " AND longitude NOT NULL AND latitude NOT NULL)"
953 " ORDER BY longitude ASC", // critical to make dbscan work
954 -1, &stmt, NULL);
955 // clang-format on
956 if(IS_NULL_PTR(stmt)) return NULL;
957
958 DT_DEBUG_SQLITE3_BIND_DOUBLE(stmt, 1, lon1);
959 DT_DEBUG_SQLITE3_BIND_DOUBLE(stmt, 2, lon2);
960 DT_DEBUG_SQLITE3_BIND_DOUBLE(stmt, 3, lat1);
961 DT_DEBUG_SQLITE3_BIND_DOUBLE(stmt, 4, lat2);
962
963 // One pass into a growing array, where the caller used to step the whole cursor once to
964 // count the rows and a second time to fill an exactly-sized buffer.
965 GArray *points = g_array_new(FALSE, FALSE, sizeof(dt_image_geo_point_t));
966 while(sqlite3_step(stmt) == SQLITE_ROW)
967 {
968 dt_image_geo_point_t p = { .imgid = sqlite3_column_int(stmt, 0),
969 .longitude = sqlite3_column_double(stmt, 1),
970 .latitude = sqlite3_column_double(stmt, 2) };
971 g_array_append_val(points, p);
972 }
973 sqlite3_finalize(stmt);
974
975 *count = (int)points->len;
976 if(points->len == 0)
977 {
978 g_array_free(points, TRUE);
979 return NULL;
980 }
981
982 return (dt_image_geo_point_t *)g_array_free(points, FALSE); // hand over the buffer
983}
984
985int64_t dt_image_repository_get_write_timestamp(const int32_t imgid)
986{
987 if(imgid <= 0) return 0;
988
992 {
994 "SELECT write_timestamp FROM main.images WHERE id = ?1",
996 }
998 {
1000 return 0;
1001 }
1002
1003 int64_t write_timestamp = 0;
1005 if(sqlite3_step(_image_write_timestamp_select_stmt) == SQLITE_ROW)
1006 write_timestamp = sqlite3_column_int64(_image_write_timestamp_select_stmt, 0);
1008 sqlite3_clear_bindings(_image_write_timestamp_select_stmt);
1010
1011 return write_timestamp;
1012}
1013
1015{
1016 if(imgid <= 0) return;
1017
1021 {
1024 "UPDATE main.images SET write_timestamp = STRFTIME('%s', 'now') WHERE id = ?1",
1026 }
1028 {
1032 sqlite3_clear_bindings(_image_write_timestamp_update_stmt);
1033 }
1035}
1036
1037gboolean dt_image_repository_set_write_timestamp(const int32_t imgid, const int64_t timestamp)
1038{
1039 if(imgid <= 0) return FALSE;
1040
1041 sqlite3_stmt *stmt = NULL;
1043 "UPDATE main.images SET write_timestamp = ?2 WHERE id = ?1",
1044 -1, &stmt, NULL);
1045 if(IS_NULL_PTR(stmt)) return FALSE;
1046
1047 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
1048 DT_DEBUG_SQLITE3_BIND_INT64(stmt, 2, timestamp);
1049 const gboolean ok = (sqlite3_step(stmt) == SQLITE_DONE);
1050 sqlite3_finalize(stmt);
1051
1052 return ok;
1053}
1054
1055gboolean dt_image_repository_delete(const int32_t imgid)
1056{
1057 gboolean ok = TRUE;
1058 sqlite3_stmt *stmt;
1059
1060 // due to foreign keys added in db version 33,
1061 // all entries from tables having references to the images are deleted as well
1062 DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get_sqlite3_global(), "DELETE FROM main.images WHERE id = ?1", -1, &stmt,
1063 NULL);
1064 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
1065 ok &= (sqlite3_step(stmt) == SQLITE_DONE);
1066 sqlite3_finalize(stmt); // the original overwrote `stmt` here and leaked this statement
1067
1069 "DELETE FROM main.meta_data WHERE id = ?1", -1, &stmt, NULL);
1070 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
1071 ok &= (sqlite3_step(stmt) == SQLITE_DONE);
1072 sqlite3_finalize(stmt);
1073
1074 return ok;
1075}
1076
1077int32_t dt_image_repository_duplicate(const int32_t imgid, const int32_t newversion)
1078{
1079 sqlite3_stmt *stmt;
1080 int32_t newid = -1;
1081
1082 // clang-format off
1084 "SELECT a.id"
1085 " FROM main.images AS a JOIN main.images AS b"
1086 " WHERE a.film_id = b.film_id AND a.filename = b.filename"
1087 " AND b.id = ?1 AND a.version = ?2"
1088 " ORDER BY a.id DESC",
1089 -1, &stmt, NULL);
1090 // clang-format on
1091 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
1092 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, newversion);
1093 if(sqlite3_step(stmt) == SQLITE_ROW)
1094 {
1095 newid = sqlite3_column_int(stmt, 0);
1096 }
1097 sqlite3_finalize(stmt);
1098
1099 // requested version is already present in DB, so we just return it
1100 if(newid != -1) return newid;
1101
1102 // clang-format off
1105 "INSERT INTO main.images"
1106 " (id, group_id, film_id, width, height, filename, maker, model, lens, exposure,"
1107 " aperture, iso, focal_length, focus_distance, datetime_taken, flags,"
1108 " output_width, output_height, crop, raw_parameters, raw_denoise_threshold,"
1109 " raw_auto_bright_threshold, raw_black, raw_maximum,"
1110 " license, sha1sum, orientation, histogram, lightmap,"
1111 " longitude, latitude, altitude, color_matrix, colorspace, version, max_version, history_end,"
1112 " aspect_ratio, exposure_bias, import_timestamp)"
1113 " SELECT NULL, group_id, film_id, width, height, filename, maker, model, lens,"
1114 " exposure, aperture, iso, focal_length, focus_distance, datetime_taken,"
1115 " flags, output_width, output_height, crop, raw_parameters, raw_denoise_threshold,"
1116 " raw_auto_bright_threshold, raw_black, raw_maximum,"
1117 " license, sha1sum, orientation, histogram, lightmap,"
1118 " longitude, latitude, altitude, color_matrix, colorspace, NULL, NULL, 0,"
1119 " aspect_ratio, exposure_bias, import_timestamp"
1120 " FROM main.images WHERE id = ?1",
1121 -1, &stmt, NULL);
1122 // clang-format on
1123 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
1124 sqlite3_step(stmt);
1125 sqlite3_finalize(stmt);
1126 // clang-format off
1128 "SELECT a.id, a.film_id, a.filename, b.max_version"
1129 " FROM main.images AS a JOIN main.images AS b"
1130 " WHERE a.film_id = b.film_id AND a.filename = b.filename AND b.id = ?1"
1131 " ORDER BY a.id DESC",
1132 -1, &stmt, NULL);
1133 // clang-format on
1134 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
1135
1136 int32_t film_id = UNKNOWN_IMAGE;
1137 int32_t max_version = -1;
1138 gchar *filename = NULL;
1139 if(sqlite3_step(stmt) == SQLITE_ROW)
1140 {
1141 newid = sqlite3_column_int(stmt, 0);
1142 film_id = sqlite3_column_int(stmt, 1);
1143 filename = g_strdup((gchar *)sqlite3_column_text(stmt, 2));
1144 max_version = sqlite3_column_int(stmt, 3);
1145 }
1146 sqlite3_finalize(stmt);
1147
1148 if(newid != -1)
1149 {
1150 // clang-format off
1152 "INSERT INTO main.color_labels (imgid, color)"
1153 " SELECT ?1, color FROM main.color_labels WHERE imgid = ?2",
1154 -1, &stmt, NULL);
1155 // clang-format on
1156 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, newid);
1157 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, imgid);
1158 sqlite3_step(stmt);
1159 sqlite3_finalize(stmt);
1160
1161 // clang-format off
1163 "INSERT INTO main.meta_data (id, key, value)"
1164 " SELECT ?1, key, value FROM main.meta_data WHERE id = ?2",
1165 -1, &stmt, NULL);
1166 // clang-format on
1167 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, newid);
1168 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, imgid);
1169 sqlite3_step(stmt);
1170 sqlite3_finalize(stmt);
1171
1172 // clang-format off
1174 "INSERT INTO main.tagged_images (imgid, tagid, position)"
1175 " SELECT ?1, tagid, "
1176 " (SELECT (IFNULL(MAX(position),0) & 0xFFFFFFFF00000000)"
1177 " FROM main.tagged_images)"
1178 " + (ROW_NUMBER() OVER (ORDER BY imgid) << 32)"
1179 " FROM main.tagged_images AS ti"
1180 " WHERE imgid = ?2",
1181 -1, &stmt, NULL);
1182 // clang-format on
1183 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, newid);
1184 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, imgid);
1185 sqlite3_step(stmt);
1186 sqlite3_finalize(stmt);
1187
1188 // clang-format off
1190 "INSERT INTO main.module_order (imgid, iop_list, version)"
1191 " SELECT ?1, iop_list, version FROM main.module_order WHERE imgid = ?2",
1192 -1, &stmt, NULL);
1193 // clang-format on
1194 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, newid);
1195 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, imgid);
1196 sqlite3_step(stmt);
1197 sqlite3_finalize(stmt);
1198
1199 // set version of new entry and max_version of all involved duplicates (with same film_id and filename)
1200 // this needs to happen before we do anything with the image cache, as version isn't updated through the cache
1201 const int32_t version = (newversion != -1) ? newversion : max_version + 1;
1202 max_version = (newversion != -1) ? MAX(max_version, newversion) : max_version + 1;
1203
1204 DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get_sqlite3_global(), "UPDATE main.images SET version=?1 WHERE id = ?2",
1205 -1, &stmt, NULL);
1206 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, version);
1207 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, newid);
1208 sqlite3_step(stmt);
1209 sqlite3_finalize(stmt);
1210
1211 // clang-format off
1213 "UPDATE main.images SET max_version=?1 WHERE film_id = ?2 AND filename = ?3", -1,
1214 &stmt, NULL);
1215 // clang-format on
1216 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, max_version);
1217 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, film_id);
1218 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 3, filename, -1, SQLITE_TRANSIENT);
1219 sqlite3_step(stmt);
1220 sqlite3_finalize(stmt);
1221
1222 dt_free(filename);
1223 }
1224 return newid;
1225}
1226
1228{
1230 if(IS_NULL_PTR(v)) return;
1231 dt_free(v->version_name);
1232 dt_free(v);
1233}
1234
1235GList *dt_image_repository_get_versions(const int32_t film_id, const char *filename,
1236 const int name_keyid)
1237{
1238 if(IS_NULL_PTR(filename)) return NULL;
1239
1240 sqlite3_stmt *stmt;
1241 // clang-format off
1243 "SELECT i.version, i.id, m.value"
1244 " FROM images AS i"
1245 " LEFT JOIN meta_data AS m ON m.id = i.id AND m.key = ?3"
1246 " WHERE film_id = ?1 AND filename = ?2"
1247 " ORDER BY i.version",
1248 -1, &stmt, NULL);
1249 // clang-format on
1250 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, film_id);
1251 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 2, filename, -1, SQLITE_TRANSIENT);
1252 DT_DEBUG_SQLITE3_BIND_INT(stmt, 3, name_keyid);
1253
1254 GList *versions = NULL;
1255 while(sqlite3_step(stmt) == SQLITE_ROW)
1256 {
1257 dt_image_version_t *v = g_malloc0(sizeof(dt_image_version_t));
1258 if(v)
1259 {
1260 v->version = sqlite3_column_int(stmt, 0);
1261 v->imgid = sqlite3_column_int(stmt, 1);
1262 const char *name = (const char *)sqlite3_column_text(stmt, 2);
1263 v->version_name = name ? g_strdup(name) : NULL;
1264 versions = g_list_prepend(versions, v);
1265 }
1266 }
1267 sqlite3_finalize(stmt);
1268
1269 return g_list_reverse(versions); // version order, as the query returns it
1270}
1271
1272GList *dt_image_repository_get_duplicate_ids(const int32_t imgid)
1273{
1274 GList *ids = NULL;
1275 sqlite3_stmt *stmt;
1276 // statement for getting ids of the image to be moved and its duplicates
1277 // clang-format off
1280 "SELECT id"
1281 " FROM main.images"
1282 " WHERE filename IN (SELECT filename FROM main.images WHERE id = ?1)"
1283 " AND film_id IN (SELECT film_id FROM main.images WHERE id = ?1)",
1284 -1, &stmt, NULL);
1285 // clang-format on
1286 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
1287
1288 while(sqlite3_step(stmt) == SQLITE_ROW)
1289 ids = g_list_prepend(ids, GINT_TO_POINTER(sqlite3_column_int(stmt, 0)));
1290 sqlite3_finalize(stmt);
1291
1292 // Row order: the original prepended off the cursor and then reversed, and its caller walks
1293 // the list without prepending again.
1294 return g_list_reverse(ids);
1295}
1296
1297int32_t dt_image_repository_copy_to_film(const int32_t imgid, const int32_t filmid,
1298 const char *new_filename, const char *old_filename)
1299{
1300 int32_t newid = -1;
1301 gchar *filename = NULL;
1302 sqlite3_stmt *stmt;
1303
1304// update database
1305// clang-format off
1308 "INSERT INTO main.images"
1309 " (id, group_id, film_id, width, height, filename, maker, model, lens, exposure,"
1310 " aperture, iso, focal_length, focus_distance, datetime_taken, flags,"
1311 " output_width, output_height, crop, raw_parameters, raw_denoise_threshold,"
1312 " raw_auto_bright_threshold, raw_black, raw_maximum,"
1313 " license, sha1sum, orientation, histogram, lightmap,"
1314 " longitude, latitude, altitude, color_matrix, colorspace, version, max_version,"
1315 " aspect_ratio, exposure_bias)"
1316 " SELECT NULL, group_id, ?1 as film_id, width, height, ?2 as filename, maker, model, lens,"
1317 " exposure, aperture, iso, focal_length, focus_distance, datetime_taken,"
1318 " flags, width, height, crop, raw_parameters, raw_denoise_threshold,"
1319 " raw_auto_bright_threshold, raw_black, raw_maximum,"
1320 " license, sha1sum, orientation, histogram, lightmap,"
1321 " longitude, latitude, altitude, color_matrix, colorspace, -1, -1,"
1322 " aspect_ratio, exposure_bias"
1323 " FROM main.images"
1324 " WHERE id = ?3",
1325 -1, &stmt, NULL);
1326// clang-format on
1327DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, filmid);
1328DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 2, new_filename, -1, SQLITE_TRANSIENT);
1329DT_DEBUG_SQLITE3_BIND_INT(stmt, 3, imgid);
1330sqlite3_step(stmt);
1331sqlite3_finalize(stmt);
1332// clang-format off
1335 "SELECT a.id, a.filename"
1336 " FROM main.images AS a"
1337 " JOIN main.images AS b"
1338 " WHERE a.film_id = ?1 AND a.filename = ?2 AND b.filename = ?3 AND b.id = ?4"
1339 " ORDER BY a.id DESC",
1340 -1, &stmt, NULL);
1341// clang-format on
1342DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, filmid);
1343DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 2, new_filename, -1, SQLITE_TRANSIENT);
1344DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 3, old_filename, -1, SQLITE_TRANSIENT);
1345DT_DEBUG_SQLITE3_BIND_INT(stmt, 4, imgid);
1346
1347if(sqlite3_step(stmt) == SQLITE_ROW)
1348{
1349 newid = sqlite3_column_int(stmt, 0);
1350 filename = g_strdup((gchar *)sqlite3_column_text(stmt, 1));
1351}
1352sqlite3_finalize(stmt);
1353
1354if(newid != -1)
1355{
1356 // clang-format off
1358 "INSERT INTO main.color_labels (imgid, color)"
1359 " SELECT ?1, color"
1360 " FROM main.color_labels"
1361 " WHERE imgid = ?2",
1362 -1, &stmt, NULL);
1363 // clang-format on
1364 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, newid);
1365 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, imgid);
1366 sqlite3_step(stmt);
1367 sqlite3_finalize(stmt);
1368 // clang-format off
1370 "INSERT INTO main.meta_data (id, key, value)"
1371 " SELECT ?1, key, value"
1372 " FROM main.meta_data"
1373 " WHERE id = ?2",
1374 -1, &stmt, NULL);
1375 // clang-format on
1376 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, newid);
1377 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, imgid);
1378 sqlite3_step(stmt);
1379 sqlite3_finalize(stmt);
1380
1381 // clang-format off
1383 "INSERT INTO main.tagged_images (imgid, tagid, position)"
1384 " SELECT ?1, tagid, "
1385 " (SELECT (IFNULL(MAX(position),0) & 0xFFFFFFFF00000000)"
1386 " FROM main.tagged_images)"
1387 " + (ROW_NUMBER() OVER (ORDER BY imgid) << 32)"
1388 " FROM main.tagged_images AS ti"
1389 " WHERE imgid = ?2",
1390 -1, &stmt, NULL);
1391 // clang-format on
1392 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, newid);
1393 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, imgid);
1394 sqlite3_step(stmt);
1395 sqlite3_finalize(stmt);
1396
1397 // get max_version of image duplicates in destination filmroll
1398 int32_t max_version = -1;
1399 // clang-format off
1402 "SELECT MAX(a.max_version)"
1403 " FROM main.images AS a"
1404 " JOIN main.images AS b"
1405 " WHERE a.film_id = b.film_id AND a.filename = b.filename AND b.id = ?1",
1406 -1, &stmt, NULL);
1407 // clang-format on
1408 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, newid);
1409
1410 if(sqlite3_step(stmt) == SQLITE_ROW) max_version = sqlite3_column_int(stmt, 0);
1411 sqlite3_finalize(stmt);
1412
1413 // set version of new entry and max_version of all involved duplicates (with same film_id and
1414 // filename)
1415 max_version = (max_version >= 0) ? max_version + 1 : 0;
1416 int32_t version = max_version;
1417
1420 "UPDATE main.images SET version=?1 WHERE id = ?2", -1, &stmt, NULL);
1421 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, version);
1422 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, newid);
1423 sqlite3_step(stmt);
1424 sqlite3_finalize(stmt);
1425
1428 "UPDATE main.images SET max_version=?1 WHERE film_id = ?2 AND filename = ?3",
1429 -1, &stmt, NULL);
1430 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, max_version);
1431 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, filmid);
1432 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 3, filename, -1, SQLITE_TRANSIENT);
1433 sqlite3_step(stmt);
1434 sqlite3_finalize(stmt);
1435
1436 // image group handling follows
1437 // get group_id of potential image duplicates in destination filmroll
1438 int32_t new_group_id = -1;
1439 // clang-format off
1442 "SELECT DISTINCT a.group_id"
1443 " FROM main.images AS a"
1444 " JOIN main.images AS b"
1445 " WHERE a.film_id = b.film_id AND a.filename = b.filename"
1446 " AND b.id = ?1 AND a.id != ?1",
1447 -1, &stmt, NULL);
1448 // clang-format on
1449 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, newid);
1450
1451 if(sqlite3_step(stmt) == SQLITE_ROW) new_group_id = sqlite3_column_int(stmt, 0);
1452
1453 // then check if there are further duplicates belonging to different group(s)
1454 if(sqlite3_step(stmt) == SQLITE_ROW) new_group_id = -1;
1455 sqlite3_finalize(stmt);
1456
1457 // rationale:
1458 // if no group exists or if the image duplicates belong to multiple groups, then the
1459 // new image builds a group of its own, else it is added to the (one) existing group
1460 if(new_group_id == -1) new_group_id = newid;
1461
1462 // make copied image belong to a group
1464 "UPDATE main.images SET group_id=?1 WHERE id = ?2", -1, &stmt, NULL);
1465
1466 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, new_group_id);
1467 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, newid);
1468 sqlite3_step(stmt);
1469 sqlite3_finalize(stmt);
1470
1471 }
1472
1473 dt_free(filename);
1474 return newid;
1475}
1476
1477gboolean dt_image_repository_insert_import(const int32_t film_id, const char *filename,
1478 const int flags, const int64_t import_timestamp)
1479{
1480 sqlite3_stmt *stmt;
1481 //insert a v0 record (which may be updated later if no v0 xmp exists)
1482 // clang-format off
1485 "INSERT INTO main.images (id, film_id, filename, license, sha1sum, flags, version, "
1486 " max_version, history_end, position, import_timestamp)"
1487 " SELECT NULL, ?1, ?2, '', '', ?3, 0, 0, 0, (IFNULL(MAX(position),0) & 0xFFFFFFFF00000000) + (1 << 32), ?4 "
1488 " FROM images",
1489 -1, &stmt, NULL);
1490 // clang-format on
1491
1492 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, film_id);
1493 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 2, filename, -1, SQLITE_TRANSIENT);
1495 DT_DEBUG_SQLITE3_BIND_INT64(stmt, 4, import_timestamp);
1496
1497 const int rc = sqlite3_step(stmt);
1498 if(rc != SQLITE_DONE) fprintf(stderr, "sqlite3 error %d\n", rc);
1499 sqlite3_finalize(stmt);
1500 return (rc == SQLITE_DONE);
1501}
1502
1504 const char *filename_pattern,
1505 const gboolean leader_only,
1506 const int32_t exclude_imgid)
1507{
1508 int32_t group_id = -1;
1509 sqlite3_stmt *stmt;
1510
1511 if(leader_only)
1512 {
1513 // clang-format off
1516 "SELECT group_id"
1517 " FROM main.images"
1518 " WHERE film_id = ?1 AND filename LIKE ?2 AND id = group_id", -1, &stmt,
1519 NULL);
1520 // clang-format on
1521 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, film_id);
1522 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 2, filename_pattern, -1, SQLITE_TRANSIENT);
1523 }
1524 else
1525 {
1526 // clang-format off
1529 "SELECT group_id"
1530 " FROM main.images"
1531 " WHERE film_id = ?1 AND filename LIKE ?2 AND id != ?3", -1, &stmt, NULL);
1532 // clang-format on
1533 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, film_id);
1534 DT_DEBUG_SQLITE3_BIND_TEXT(stmt, 2, filename_pattern, -1, SQLITE_TRANSIENT);
1535 DT_DEBUG_SQLITE3_BIND_INT(stmt, 3, exclude_imgid);
1536 }
1537
1538 if(sqlite3_step(stmt) == SQLITE_ROW) group_id = sqlite3_column_int(stmt, 0);
1539 sqlite3_finalize(stmt);
1540 return group_id;
1541}
1542
1543gboolean dt_image_repository_set_group(const int32_t imgid, const int32_t group_id)
1544{
1545 sqlite3_stmt *stmt;
1548 "UPDATE main.images SET group_id = ?1 WHERE id = ?2",
1549 -1, &stmt, NULL);
1550 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, group_id);
1551 DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, imgid);
1552 const gboolean ok = (sqlite3_step(stmt) == SQLITE_DONE);
1553 sqlite3_finalize(stmt);
1554 return ok;
1555}
1556
1558{
1559 if(IS_NULL_PTR(row)) return FALSE;
1560 memset(row, 0, sizeof(*row));
1561
1562 gboolean found = FALSE;
1563 sqlite3_stmt *stmt;
1564 // clang-format off
1566 "SELECT filename, flags, raw_parameters, "
1567 " longitude, latitude, altitude, history_end, datetime_taken"
1568 " FROM main.images"
1569 " WHERE id = ?1",
1570 -1, &stmt, NULL);
1571 // clang-format on
1572 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
1573 if(sqlite3_step(stmt) == SQLITE_ROW)
1574 {
1575 // copied, not borrowed: the caller used to read it before finalising the statement, and
1576 // owning it removes the constraint rather than moving it across a module boundary
1577 const char *f = (const char *)sqlite3_column_text(stmt, 0);
1578 row->filename = f ? g_strdup(f) : NULL;
1579 row->flags = sqlite3_column_int(stmt, 1);
1580 row->raw_parameters = sqlite3_column_int(stmt, 2);
1581 if(sqlite3_column_type(stmt, 3) == SQLITE_FLOAT)
1582 {
1583 row->longitude = sqlite3_column_double(stmt, 3);
1584 row->has_longitude = TRUE;
1585 }
1586 if(sqlite3_column_type(stmt, 4) == SQLITE_FLOAT)
1587 {
1588 row->latitude = sqlite3_column_double(stmt, 4);
1589 row->has_latitude = TRUE;
1590 }
1591 if(sqlite3_column_type(stmt, 5) == SQLITE_FLOAT)
1592 {
1593 row->altitude = sqlite3_column_double(stmt, 5);
1594 row->has_altitude = TRUE;
1595 }
1596 row->history_end = sqlite3_column_int(stmt, 6);
1597 row->datetime_taken = sqlite3_column_int64(stmt, 7);
1598 found = TRUE;
1599 }
1600 sqlite3_finalize(stmt);
1601 return found;
1602}
1603
1605{
1606 if(IS_NULL_PTR(row)) return;
1607 dt_free(row->filename);
1608 row->filename = NULL;
1609}
1610
1612{
1613 if(IS_NULL_PTR(ts)) return FALSE;
1614 memset(ts, 0, sizeof(*ts));
1615
1616 gboolean found = FALSE;
1617 sqlite3_stmt *stmt;
1618 // clang-format off
1621 "SELECT import_timestamp, change_timestamp, export_timestamp, print_timestamp"
1622 " FROM main.images"
1623 " WHERE id = ?1",
1624 -1, &stmt, NULL);
1625 DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
1626 // clang-format on
1627
1628 if(sqlite3_step(stmt) == SQLITE_ROW)
1629 {
1630 if(sqlite3_column_type(stmt, 0) != SQLITE_NULL)
1631 {
1632 ts->import_timestamp = sqlite3_column_int64(stmt, 0);
1633 ts->has_import = TRUE;
1634 }
1635 if(sqlite3_column_type(stmt, 1) != SQLITE_NULL)
1636 {
1637 ts->change_timestamp = sqlite3_column_int64(stmt, 1);
1638 ts->has_change = TRUE;
1639 }
1640 if(sqlite3_column_type(stmt, 2) != SQLITE_NULL)
1641 {
1642 ts->export_timestamp = sqlite3_column_int64(stmt, 2);
1643 ts->has_export = TRUE;
1644 }
1645 if(sqlite3_column_type(stmt, 3) != SQLITE_NULL)
1646 {
1647 ts->print_timestamp = sqlite3_column_int64(stmt, 3);
1648 ts->has_print = TRUE;
1649 }
1650 found = TRUE;
1651 }
1652 sqlite3_finalize(stmt);
1653 return found;
1654}
1655
1657{
1659 {
1660 sqlite3_finalize(_image_load_stmt);
1661 _image_load_stmt = NULL;
1662 }
1664 {
1665 sqlite3_finalize(_image_write_history_hash_stmt);
1667 }
1669 {
1670 sqlite3_finalize(_image_write_timestamp_select_stmt);
1672 }
1674 {
1675 sqlite3_finalize(_image_write_timestamp_update_stmt);
1677 }
1679 {
1680 sqlite3_finalize(_image_set_flags_stmt);
1681 _image_set_flags_stmt = NULL;
1682 }
1684 {
1685 sqlite3_finalize(_image_set_flags_masked_stmt);
1687 }
1688
1690 {
1693 }
1694}
1695
1696// clang-format off
1697// modelines: These editor modelines have been set for all relevant files by tools/update_modelines.py
1698// vim: shiftwidth=2 expandtab tabstop=2 cindent
1699// kate: tab-indents: off; indent-width 2; replace-tabs on; indent-mode cstyle; remove-trailing-spaces modified;
1700// clang-format on
#define TRUE
Definition ashift_lsd.c:162
#define FALSE
Definition ashift_lsd.c:158
void dt_colorlabel_repository_remove(const int32_t imgid, const int color)
Clear colour color on imgid. Clearing one that is not set does nothing.
void dt_colorlabel_repository_set(const int32_t imgid, const int color)
Set colour color on imgid. Setting one that is already set does nothing.
main.color_labels: which of the five colour labels are set on an image.
const float f
const float v
static const int row
void dt_image_init(dt_image_t *img)
const char * dt_image_film_roll_name(const char *path)
void dt_image_set_provisional_dsc(dt_image_t *img)
void dt_image_local_copy_paths_from_fullpath(const char *fullpath, int32_t imgid, char *local_copy_path, size_t local_copy_len, char *local_copy_legacy_path, size_t local_copy_legacy_len)
sqlite3 * dt_database_get_sqlite3_global(void)
Definition database.c:3790
gboolean dt_datetime_gtimespan_to_local(char *local, const size_t local_size, const GTimeSpan gts, const gboolean msec, const gboolean tz)
Definition datetime.c:178
dev roi_request value
GtkWidget * folder
destination folder chooser
static int dt_pthread_mutex_unlock(dt_pthread_mutex_t *mutex) RELEASE(mutex) NO_THREAD_SAFETY_ANALYSIS
Definition dtpthread.h:127
static int dt_pthread_mutex_init(dt_pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr)
Initialise a mutex. With mutexattr NULL – which is how 54 of the 56 call sites in this tree spell it ...
Definition dtpthread.h:104
static int dt_pthread_mutex_destroy(dt_pthread_mutex_t *mutex)
Definition dtpthread.h:132
static int dt_pthread_mutex_lock(dt_pthread_mutex_t *mutex) ACQUIRE(mutex) NO_THREAD_SAFETY_ANALYSIS
Definition dtpthread.h:117
memcpy(roundtrip, origin, sizeof(roundtrip))
void * data
Definition geometry.h:111
GdkRGBA color[]
Definition geotagging.c:541
#define UNKNOWN_IMAGE
Definition image.h:78
const char flag
Definition image.h:320
int dt_image_repository_get_version(const int32_t imgid)
The duplicate version number of imgid, or 0.
void dt_image_repository_touch_write_timestamp(const int32_t imgid)
Set write_timestamp of imgid to now.
static void _image_write_history_hash(const dt_image_t *img)
static gsize _image_stmt_mutex_inited
static sqlite3_stmt * _image_write_timestamp_update_stmt
int dt_image_repository_count_others_with_flag(const int32_t imgid, const int flag)
How many OTHER images share this image's film roll and filename and carry flag.
int dt_image_repository_count_in_id_range(const int32_t min_imgid, const int32_t max_imgid)
How many images have an id in [min_imgid, max_imgid]. -1 when unreadable.
gboolean dt_image_repository_set_flags_masked(const int32_t imgid, const int mask, const int value)
Write only mask's bits of imgid's flags, taking them from value.
GList * dt_image_repository_get_ids_with_flag_among(GList *imgids, const int flag)
Of the images in imgids, those whose flags carry flag, in row order.
void dt_image_repository_cleanup(void)
Finalise the prepared statements. Called at shutdown, after the last cache release and before the dat...
static char * _id_set(GList *imgids)
void dt_image_repository_xmp_row_cleanup(dt_image_xmp_row_t *row)
Release what dt_image_repository_get_xmp_row() allocated.
int64_t dt_image_repository_get_write_timestamp(const int32_t imgid)
main.images.write_timestamp for imgid, or 0.
gboolean dt_image_repository_set_group(const int32_t imgid, const int32_t group_id)
Set imgid's group_id.
static sqlite3_stmt * _image_write_timestamp_select_stmt
gboolean dt_image_repository_set_write_timestamp(const int32_t imgid, const int64_t timestamp)
Set write_timestamp of imgid to timestamp (seconds since the epoch). Bound as a 64-bit integer; the c...
void dt_image_group_member_free(gpointer data)
Free one dt_image_group_member_t. Suits g_list_free_full().
gboolean dt_image_repository_get_collected_geo_bounds(dt_image_geo_bounds_t *bounds)
Bounding box of every collected image carrying coordinates.
void dt_image_repository_foreach_collected(dt_image_repository_collected_cb cb, void *user_data)
Every image of the current collection, in collection order, already mapped.
gboolean dt_image_repository_load(const int32_t imgid, dt_image_t *img)
Fill img from the main.images row for imgid.
static sqlite3_stmt * _image_set_flags_stmt
int32_t dt_image_repository_find_group_for_pattern(const int32_t film_id, const char *filename_pattern, const gboolean leader_only, const int32_t exclude_imgid)
The group leader of an existing image in film_id matching filename_pattern (a LIKE pattern),...
GList * dt_image_repository_get_group_member_rows(const int32_t group_id)
Every member of group_id, in row order, named. Free with g_list_free_full(list, dt_image_group_member...
gboolean dt_image_repository_get_timestamps(const int32_t imgid, dt_image_timestamps_t *ts)
Read imgid's four timestamps. FALSE when there is no such image.
gboolean dt_image_repository_delete(const int32_t imgid)
Delete imgid from main.images and main.meta_data.
int32_t dt_image_repository_copy_to_film(const int32_t imgid, const int32_t filmid, const char *new_filename, const char *old_filename)
Copy imgid into film roll filmid under new_filename, returning the new id.
GList * dt_image_repository_get_versions(const int32_t film_id, const char *filename, const int name_keyid)
Every version of the file (film_id, filename), in version order, each with the name stored under meta...
gboolean dt_image_repository_set_flags(const int32_t imgid, const int flags)
Replace imgid's whole flags word.
static sqlite3_stmt * _image_set_flags_masked_stmt
GList * dt_image_repository_get_duplicate_ids(const int32_t imgid)
Every image id sharing imgid's film roll and filename, itself included, in row order – an image and i...
static sqlite3_stmt * _image_load_stmt
GList * dt_image_repository_get_ids_with_flag(const int flag)
Every image id whose flags carry flag, in row order.
void dt_image_repository_foreach_in_id_range(const int32_t min_imgid, const int32_t max_imgid, dt_image_repository_id_filename_cb cb, void *user_data)
Walk the images whose id falls in [min_imgid, max_imgid], in row order.
gboolean dt_image_repository_get_xmp_row(const int32_t imgid, dt_image_xmp_row_t *row)
Read imgid's sidecar fields. FALSE when there is no such image.
gboolean dt_image_repository_clear_flag_among(GList *imgids, const int flag)
Clear flag on every image in imgids – the counterpart of dt_image_repository_set_flag_among(),...
static dt_pthread_mutex_t _image_stmt_mutex
void dt_image_version_free(gpointer data)
Release one dt_image_version_t. Use with g_list_free_full().
void dt_image_repository_reassign_group(const int32_t from_group_id, const int32_t to_group_id, const int32_t exclude_imgid)
Move every member of from_group_id into to_group_id, except exclude_imgid.
int32_t dt_image_repository_duplicate(const int32_t imgid, const int32_t newversion)
Duplicate imgid as a new version, returning the new image id (-1 on failure).
static void dt_image_from_stmt(dt_image_t *img, sqlite3_stmt *stmt)
static void _image_stmt_mutex_ensure(void)
void dt_image_repository_store(const dt_image_t *img)
Write img back to main.images, plus its colour labels and history hash.
int32_t dt_image_repository_find_by_film_and_filename(const int32_t film_id, const char *filename)
The image id for filename inside film roll film_id, or -1.
static sqlite3_stmt * _image_get_stmt(void)
GList * dt_image_repository_get_ratings(const int32_t imgid)
Star ratings, as main.images.flags encodes them: the low three bits, minus one.
dt_image_geo_point_t * dt_image_repository_get_collected_geo_points(const double lon1, const double lon2, const double lat1, const double lat2, int *count)
Collected images inside the box, ordered by longitude ascending.
void dt_image_repository_foreach_with_path(dt_image_repository_path_row_cb cb, void *user_data)
Walk every image in the library, film roll by film roll, filename within each.
gboolean dt_image_repository_set_version(const int32_t imgid, const int version)
Set both version and max_version of imgid to version.
int32_t dt_image_repository_find_by_folder_and_filename(const char *folder, const char *filename)
The image id for a full path, split into its folder and filename, or -1.
gboolean dt_image_repository_set_flag_among(GList *imgids, const int flag)
Set flag on every image in imgids.
GList * dt_image_repository_get_group_members(const int32_t group_id, const int32_t exclude_imgid)
Image ids in group group_id.
GList * dt_image_repository_get_full_paths(GList *imgids)
"<folder>/<filename>" for every image in imgids, distinct, in row order. Caller owns the strings.
int * dt_image_repository_count_distinct_fields(GList *imgids)
How many DISTINCT values each dt_image_field_t takes over imgids.
static sqlite3_stmt * _image_write_history_hash_stmt
gboolean dt_image_repository_insert_import(const int32_t film_id, const char *filename, const int flags, const int64_t import_timestamp)
Insert a bare row for a file being imported, returning TRUE on success.
static GList * _collect_ids(sqlite3_stmt *stmt, const int32_t exclude_imgid)
Reading and writing one dt_image_t to and from the library database. The SQL half of what used to be ...
@ DT_IMAGE_FIELD_COUNT
gboolean(* dt_image_repository_path_row_cb)(const int32_t imgid, const int64_t write_timestamp, const int version, const char *image_path, const int flags, void *user_data)
One row of dt_image_repository_foreach_with_path(). image_path is borrowed: it lives until the callba...
void(* dt_image_repository_id_filename_cb)(const int32_t imgid, const char *filename, void *user_data)
One row of dt_image_repository_foreach_in_id_range(). filename is borrowed.
void(* dt_image_repository_collected_cb)(void *user_data, const dt_image_t *info)
One image of the current collection. info is borrowed: it lives until the callback returns.
const char * maker
const char * model
#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 macros.h:96
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
Definition mem_alloc.h:171
dt_mipmap_buffer_dsc_flags flags
Definition mipmap_cache.c:4
const char * name
Definition pdf.h:90
Checked wrappers around the sqlite3_* calls: trace the statement, assert the return code,...
#define DT_DEBUG_SQLITE3_BIND_BLOB(a, b, c, d, e)
Definition sql_debug.h:149
#define DT_DEBUG_SQLITE3_PREPARE_V2(a, b, c, d, e)
Definition sql_debug.h:137
#define DT_DEBUG_SQLITE3_BIND_TEXT(a, b, c, d, e)
Definition sql_debug.h:148
#define DT_DEBUG_SQLITE3_BIND_INT(a, b, c)
Definition sql_debug.h:145
#define DT_DEBUG_SQLITE3_BIND_INT64(a, b, c)
Definition sql_debug.h:146
#define DT_DEBUG_SQLITE3_BIND_DOUBLE(a, b, c)
Definition sql_debug.h:147
const float const int flip
The extent of the collection's geotagged images.
One geotagged image, in degrees as stored.
double latitude
Definition image.h:341
double elevation
Definition image.h:341
double longitude
Definition image.h:341
One member of a group, with what a tooltip needs to name it.
float exif_exposure
Definition image.h:376
int32_t height
Definition image.h:406
GTimeSpan export_timestamp
Definition image.h:424
uint64_t history_hash
Definition image.h:413
float exif_focus_distance
Definition image.h:381
GTimeSpan import_timestamp
Definition image.h:424
int32_t group_id
Definition image.h:410
float exif_exposure_bias
Definition image.h:377
uint16_t raw_black_level
Definition image.h:446
float exif_iso
Definition image.h:379
uint32_t raw_white_point
Definition image.h:448
int32_t exif_inited
Definition image.h:374
float exif_aperture
Definition image.h:378
char local_copy_path[DT_PATH_MAX]
Definition image.h:397
int32_t version
Definition image.h:410
uint64_t mipmap_hash
Definition image.h:420
dt_image_geoloc_t geoloc
Definition image.h:443
int32_t flags
Definition image.h:410
dt_image_orientation_t orientation
Definition image.h:375
int32_t width
Definition image.h:406
float exif_focal_length
Definition image.h:380
char exif_maker[64]
Definition image.h:383
GTimeSpan change_timestamp
Definition image.h:424
uint32_t history_items
Definition image.h:412
char exif_lens[128]
Definition image.h:385
GTimeSpan print_timestamp
Definition image.h:424
int32_t film_id
Definition image.h:410
char folder[DT_PATH_MAX]
Definition image.h:399
GTimeSpan exif_datetime_taken
Definition image.h:386
int color_labels
Definition image.h:481
float d65_color_matrix[9]
Definition image.h:430
char filmroll[DT_PATH_MAX]
Definition image.h:400
char exif_model[64]
Definition image.h:384
dt_image_colorspace_t colorspace
Definition image.h:438
char datetime[200]
Definition image.h:401
uint32_t group_members
Definition image.h:411
char local_copy_legacy_path[DT_PATH_MAX]
Definition image.h:398
dt_image_raw_parameters_t legacy_flip
Definition image.h:440
char filename[DT_MAX_FILENAME_LEN]
Definition image.h:395
int32_t id
Definition image.h:410
float exif_crop
Definition image.h:382
char fullpath[DT_PATH_MAX]
Definition image.h:396
The four timestamps, each with whether it is set at all.
One version of a file: which duplicate it is, its id, and the name the user gave it.
The main.images fields an XMP sidecar carries.
#define MAX(a, b)
Definition thinplate.c:29