void dt_similarity_match_image(uint32_t imgid,dt_similarity_t *data)
{
sqlite3_stmt *stmt;
gboolean all_ok_for_match = TRUE;
dt_similarity_histogram_t orginal_histogram,test_histogram;
dt_similarity_lightmap_t orginal_lightmap,test_lightmap;
/* create temporary mem table for matches */
DT_DEBUG_SQLITE3_EXEC(dt_database_get(darktable.db), "create temporary table if not exists similar_images (id integer,score real)", NULL, NULL, NULL);
DT_DEBUG_SQLITE3_EXEC(dt_database_get(darktable.db), "delete from similar_images", NULL, NULL, NULL);
/*
* get the histogram and lightmap data for image to match against
*/
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "select histogram,lightmap from images where id = ?1", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, imgid);
if (sqlite3_step(stmt) == SQLITE_ROW)
{
/* get the histogram data */
uint32_t size = sqlite3_column_bytes(stmt,0);
if (size!=sizeof(dt_similarity_histogram_t))
{
all_ok_for_match = FALSE;
dt_control_log(_("this image has not been indexed yet."));
}
else
memcpy(&orginal_histogram, sqlite3_column_blob(stmt, 0), sizeof(dt_similarity_histogram_t));
/* get the lightmap data */
size = sqlite3_column_bytes(stmt,1);
if (size!=sizeof(dt_similarity_lightmap_t))
{
all_ok_for_match = FALSE;
dt_control_log(_("this image has not been indexed yet."));
}
else
memcpy(&orginal_lightmap, sqlite3_column_blob(stmt, 1), sizeof(dt_similarity_lightmap_t));
}
else
{
all_ok_for_match = FALSE;
dt_control_log(_("this image has not been indexed yet."));
}
sqlite3_reset(stmt);
sqlite3_clear_bindings(stmt);
/*
* if all ok lets begin matching
*/
if (all_ok_for_match)
{
char query[4096]={0};
/* add target image with 100.0 in score into result to ensure it always shown in top */
sprintf(query,"insert into similar_images(id,score) values(%d,%f)",imgid,100.0);
DT_DEBUG_SQLITE3_EXEC(dt_database_get(darktable.db), query, NULL, NULL, NULL);
/* set an extended collection query for viewing the result of match */
dt_collection_set_extended_where(darktable.collection, ", similar_images where images.id = similar_images.id order by similar_images.score desc");
dt_collection_set_query_flags( darktable.collection,
dt_collection_get_query_flags(darktable.collection) | COLLECTION_QUERY_USE_ONLY_WHERE_EXT);
dt_collection_update(darktable.collection);
dt_control_signal_raise(darktable.signals, DT_SIGNAL_COLLECTION_CHANGED);
/* loop thru images and generate score table */
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "select id,histogram,lightmap from images", -1, &stmt, NULL);
while (sqlite3_step(stmt) == SQLITE_ROW)
{
float score_histogram=0, score_lightmap=0, score_colormap=0;
/* verify size of histogram and lightmap blob of test image */
if (
(sqlite3_column_bytes(stmt,1) == sizeof(dt_similarity_histogram_t)) &&
(sqlite3_column_bytes(stmt,2) == sizeof(dt_similarity_lightmap_t))
)
{
/*
* Get the histogram blob and calculate the similarity score
*/
memcpy(&test_histogram, sqlite3_column_blob(stmt, 1), sizeof(dt_similarity_histogram_t));
score_histogram = _similarity_match_histogram_rgb(data, &orginal_histogram, &test_histogram);
/*
* Get the lightmap blob and calculate the similarity score
* 1.08 is a tuned constant that works well with threshold
*/
memcpy(&test_lightmap, sqlite3_column_blob(stmt, 2), sizeof(dt_similarity_lightmap_t));
score_lightmap = _similarity_match_lightmap(data, &orginal_lightmap, &test_lightmap);
/*
* then calculate the colormap similarity score
*/
score_colormap = _similarity_match_colormap(data, &orginal_lightmap, &test_lightmap);
/*
//.........这里部分代码省略.........
GList *dt_styles_get_item_list(const char *name, gboolean params, int imgid)
{
GList *result = NULL;
sqlite3_stmt *stmt;
int id = 0;
if((id = dt_styles_get_id_by_name(name)) != 0)
{
if(params)
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"SELECT num, module, operation, enabled, op_params, blendop_params, "
"multi_name FROM data.style_items WHERE styleid=?1 ORDER BY num DESC",
-1, &stmt, NULL);
else if(imgid != -1)
{
// get all items from the style
// UNION
// get all items from history, not in the style : select only the last operation, that is max(num)
DT_DEBUG_SQLITE3_PREPARE_V2(
dt_database_get(darktable.db),
"SELECT num, module, operation, enabled, (SELECT MAX(num) FROM main.history WHERE imgid=?2 AND "
"operation=data.style_items.operation GROUP BY multi_priority),multi_name FROM data.style_items WHERE "
"styleid=?1 UNION SELECT -1,main.history.module,main.history.operation,main.history.enabled, "
"main.history.num,multi_name FROM main.history WHERE imgid=?2 AND main.history.enabled=1 AND "
"(main.history.operation NOT IN (SELECT operation FROM data.style_items WHERE styleid=?1) OR "
"(main.history.op_params NOT IN (SELECT op_params FROM data.style_items WHERE styleid=?1 AND "
"operation=main.history.operation)) OR (main.history.blendop_params NOT IN (SELECT blendop_params FROM "
"data.style_items WHERE styleid=?1 AND operation=main.history.operation))) GROUP BY operation HAVING "
"MAX(num) ORDER BY num DESC", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, imgid);
}
else
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "SELECT num, module, operation, enabled, 0, "
"multi_name FROM data.style_items WHERE "
"styleid=?1 ORDER BY num DESC",
-1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, id);
while(sqlite3_step(stmt) == SQLITE_ROW)
{
// name of current item of style
char iname[512] = { 0 };
dt_style_item_t *item = calloc(1, sizeof(dt_style_item_t));
if(sqlite3_column_type(stmt, 0) == SQLITE_NULL)
item->num = -1;
else
item->num = sqlite3_column_int(stmt, 0);
item->selimg_num = -1;
item->module_version = sqlite3_column_int(stmt, 1);
item->enabled = sqlite3_column_int(stmt, 3);
if(params)
{
// when we get the parameters we do not want to get the operation localized as this
// is used to compare against the internal module name.
const char *multi_name = (const char *)sqlite3_column_text(stmt, 6);
if(!(multi_name && *multi_name))
g_snprintf(iname, sizeof(iname), "%s", sqlite3_column_text(stmt, 2));
else
g_snprintf(iname, sizeof(iname), "%s %s", sqlite3_column_text(stmt, 2), multi_name);
const unsigned char *op_blob = sqlite3_column_blob(stmt, 4);
const int32_t op_len = sqlite3_column_bytes(stmt, 4);
const unsigned char *bop_blob = sqlite3_column_blob(stmt, 5);
const int32_t bop_len = sqlite3_column_bytes(stmt, 5);
item->params = malloc(op_len);
memcpy(item->params, op_blob, op_len);
item->blendop_params = malloc(bop_len);
memcpy(item->blendop_params, bop_blob, bop_len);
}
else
{
const char *multi_name = (const char *)sqlite3_column_text(stmt, 5);
gboolean has_multi_name = FALSE;
if(multi_name && *multi_name && strcmp(multi_name, "0") != 0) has_multi_name = TRUE;
if(has_multi_name)
g_snprintf(iname, sizeof(iname), "%s %s (%s)",
dt_iop_get_localized_name((gchar *)sqlite3_column_text(stmt, 2)), multi_name,
(sqlite3_column_int(stmt, 3) != 0) ? _("on") : _("off"));
else
g_snprintf(iname, sizeof(iname), "%s (%s)",
dt_iop_get_localized_name((gchar *)sqlite3_column_text(stmt, 2)),
(sqlite3_column_int(stmt, 3) != 0) ? _("on") : _("off"));
item->params = NULL;
item->blendop_params = NULL;
if(imgid != -1 && sqlite3_column_type(stmt, 4) != SQLITE_NULL)
item->selimg_num = sqlite3_column_int(stmt, 4);
}
item->name = g_strdup(iname);
result = g_list_append(result, item);
}
sqlite3_finalize(stmt);
//.........这里部分代码省略.........
static void _dt_style_cleanup_multi_instance(int id)
{
sqlite3_stmt *stmt;
GList *list = NULL;
struct _data
{
int rowid;
int mi;
};
char last_operation[128] = { 0 };
int last_mi = 0;
/* let's clean-up the style multi-instance. What we want to do is have a unique multi_priority value for
each iop.
Furthermore this value must start to 0 and increment one by one for each multi-instance of the same
module. On
SQLite there is no notion of ROW_NUMBER, so we use rather resource consuming SQL statement, but as a
style has
never a huge number of items that's not a real issue. */
/* 1. read all data for the style and record multi_instance value. */
DT_DEBUG_SQLITE3_PREPARE_V2(
dt_database_get(darktable.db),
"SELECT rowid,operation FROM data.style_items WHERE styleid=?1 ORDER BY operation, multi_priority ASC", -1,
&stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, id);
while(sqlite3_step(stmt) == SQLITE_ROW)
{
struct _data *d = malloc(sizeof(struct _data));
const char *operation = (const char *)sqlite3_column_text(stmt, 1);
if(strncmp(last_operation, operation, 128) != 0)
{
last_mi = 0;
g_strlcpy(last_operation, operation, sizeof(last_operation));
}
else
last_mi++;
d->rowid = sqlite3_column_int(stmt, 0);
d->mi = last_mi;
list = g_list_append(list, d);
}
sqlite3_finalize(stmt);
/* 2. now update all multi_instance values previously recorded */
list = g_list_first(list);
while(list)
{
struct _data *d = (struct _data *)list->data;
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"UPDATE data.style_items SET multi_priority=?1 WHERE rowid=?2", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, d->mi);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, d->rowid);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
list = g_list_next(list);
}
g_list_free_full(list, free);
}
int
dt_history_copy_and_paste_on_image (int32_t imgid, int32_t dest_imgid, gboolean merge, GList *ops)
{
sqlite3_stmt *stmt;
if(imgid==dest_imgid) return 1;
if(imgid==-1)
{
dt_control_log(_("you need to copy history from an image before you paste it onto another"));
return 1;
}
/* if merge onto history stack, lets find history offest in destination image */
int32_t offs = 0;
if (merge)
{
/* apply on top of history stack */
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "SELECT MAX(num)+1 FROM history WHERE imgid = ?1", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, dest_imgid);
if (sqlite3_step (stmt) == SQLITE_ROW) offs = sqlite3_column_int (stmt, 0);
}
else
{
/* replace history stack */
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "delete from history where imgid = ?1", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, dest_imgid);
sqlite3_step (stmt);
}
sqlite3_finalize (stmt);
// prepare SQL request
char req[2048];
strcpy (req, "insert into history (imgid, num, module, operation, op_params, enabled, blendop_params, blendop_version, multi_name, multi_priority) select ?1, num+?2, module, operation, op_params, enabled, blendop_params, blendop_version, multi_name, multi_priority from history where imgid = ?3");
// Add ops selection if any format: ... and num in (val1, val2)
if (ops)
{
GList *l = ops;
int first = 1;
strcat (req, " and num in (");
while (l)
{
unsigned int value = GPOINTER_TO_UINT(l->data);
char v[30];
if (!first) strcat (req, ",");
snprintf (v, 30, "%u", value);
strcat (req, v);
first=0;
l = g_list_next(l);
}
strcat (req, ")");
}
/* add the history items to stack offest */
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), req, -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, dest_imgid);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, offs);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 3, imgid);
sqlite3_step (stmt);
sqlite3_finalize (stmt);
if (merge && ops)
_dt_history_cleanup_multi_instance(dest_imgid, offs);
//we have to copy masks too
//what to do with existing masks ?
if (merge)
{
//there's very little chance that we will have same shapes id.
//but we may want to handle this case anyway
//and it's not trivial at all !
}
else
{
//let's remove all existing shapes
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "delete from mask where imgid = ?1", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, dest_imgid);
sqlite3_step (stmt);
sqlite3_finalize (stmt);
}
//let's copy now
strcpy (req, "insert into mask (imgid, formid, form, name, version, points, points_count, source) select ?1, formid, form, name, version, points, points_count, source from mask where imgid = ?2");
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), req, -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, dest_imgid);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, imgid);
sqlite3_step (stmt);
sqlite3_finalize (stmt);
/* if current image in develop reload history */
if (dt_dev_is_current_image(darktable.develop, dest_imgid))
{
dt_dev_reload_history_items (darktable.develop);
dt_dev_modulegroups_set(darktable.develop, dt_dev_modulegroups_get(darktable.develop));
}
/* update xmp file */
dt_image_synch_xmp(dest_imgid);
//.........这里部分代码省略.........
}
gboolean dt_tag_new_from_gui(const char *name, guint *tagid)
{
gboolean ret = dt_tag_new(name, tagid);
/* if everything went fine, raise signal of tags change to refresh keywords module in GUI */
if(ret) dt_control_signal_raise(darktable.signals, DT_SIGNAL_TAG_CHANGED);
return ret;
}
guint dt_tag_remove(const guint tagid, gboolean final)
{
int rv, count = -1;
sqlite3_stmt *stmt;
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db),
"SELECT COUNT(*) FROM main.tagged_images WHERE tagid=?1", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, tagid);
rv = sqlite3_step(stmt);
if(rv == SQLITE_ROW) count = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
if(final == TRUE)
{
// let's actually remove the tag
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "DELETE FROM data.tags WHERE id=?1", -1, &stmt,
NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, tagid);
sqlite3_step(stmt);
sqlite3_finalize(stmt);
开发者ID:CChiappa,项目名称:darktable,代码行数:30,代码来源:tags.c
示例12: dt_colorlabels_remove_labels_selection
void dt_colorlabels_remove_labels_selection ()
{
DT_DEBUG_SQLITE3_EXEC(dt_database_get(darktable.db), "delete from color_labels where imgid in (select imgid from selected_images)", NULL, NULL, NULL);
}
GList *
dt_styles_get_item_list (const char *name, gboolean params, int imgid)
{
GList *result=NULL;
sqlite3_stmt *stmt;
int id=0;
if ((id=dt_styles_get_id_by_name(name)) != 0)
{
if (params)
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "select num, operation, enabled, op_params, blendop_params, multi_name from style_items where styleid=?1 order by num desc", -1, &stmt, NULL);
else if (imgid != -1)
{
// get all items from the style
// UNION
// get all items from history, not in the style : select only the last operation, that is max(num)
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "select num, operation, enabled, (select max(num) from history where imgid=?2 and operation=style_items.operation group by multi_priority),multi_name from style_items where styleid=?1 UNION select -1,history.operation,history.enabled,history.num,multi_name from history where imgid=?2 and history.enabled=1 and (history.operation not in (select operation from style_items where styleid=?1) or (history.op_params not in (select op_params from style_items where styleid=?1 and operation=history.operation)) or (history.blendop_params not in (select blendop_params from style_items where styleid=?1 and operation=history.operation))) group by operation having max(num) order by num desc", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 2, imgid);
}
else
DT_DEBUG_SQLITE3_PREPARE_V2(dt_database_get(darktable.db), "select num, operation, enabled, 0, multi_name from style_items where styleid=?1 order by num desc", -1, &stmt, NULL);
DT_DEBUG_SQLITE3_BIND_INT(stmt, 1, id);
while (sqlite3_step(stmt) == SQLITE_ROW)
{
char name[512]= {0};
dt_style_item_t *item=g_malloc (sizeof (dt_style_item_t));
if (sqlite3_column_type(stmt,0)==SQLITE_NULL)
item->num = -1;
else
item->num = sqlite3_column_int (stmt, 0);
item->selimg_num = -1;
if (params)
{
// when we get the parameters we do not want to get the operation localized as this
// is used to compare against the internal module name.
const char *multi_name = (const char *)sqlite3_column_text (stmt, 5);
if (!multi_name || strlen(multi_name))
g_snprintf(name,512,"%s",sqlite3_column_text (stmt, 1));
else
g_snprintf(name,512,"%s %s",sqlite3_column_text (stmt, 1), multi_name);
const unsigned char *op_blob = sqlite3_column_blob(stmt, 3);
const int32_t op_len = sqlite3_column_bytes(stmt, 3);
const unsigned char *bop_blob = sqlite3_column_blob(stmt, 4);
const int32_t bop_len = sqlite3_column_bytes(stmt, 4);
item->params = malloc(op_len);
memcpy(item->params, op_blob, op_len);
item->blendop_params = malloc(bop_len);
memcpy(item->blendop_params, bop_blob, bop_len);
}
else
{
const char *multi_name = (const char *)sqlite3_column_text (stmt, 4);
gboolean has_multi_name = FALSE;
if (multi_name && strlen(multi_name)>0 && strcmp(multi_name,"0")!=0)
has_multi_name = TRUE;
if (has_multi_name)
g_snprintf(name,512,"%s %s (%s)",dt_iop_get_localized_name((gchar *)sqlite3_column_text (stmt, 1)),multi_name,(sqlite3_column_int (stmt, 2)!=0)?_("on"):_("off"));
else
g_snprintf(name,512,"%s (%s)",dt_iop_get_localized_name((gchar *)sqlite3_column_text (stmt, 1)),(sqlite3_column_int (stmt, 2)!=0)?_("on"):_("off"));
item->params = NULL;
item->blendop_params = NULL;
if (imgid != -1 && sqlite3_column_type(stmt,4)!=SQLITE_NULL)
item->selimg_num = sqlite3_column_int (stmt, 4);
}
item->name = g_strdup (name);
result = g_list_append (result,item);
}
sqlite3_finalize(stmt);
}
return result;
}
请发表评论