46#include <glib/gstdio.h>
61static const point_t bb_ref[] = {{.x=.0, .y=.0}, {.x=1., .y=0.}, {.x=1., .y=1.}, {.x=0., .y=1.}};
102static void add_column(GtkTreeView *treeview,
const char *title,
int column_id,
int sort_column);
115 int *x_end,
int *y_end);
229 const int prev_corner = (closest_corner + 3) % 4;
230 const int opposite_corner = (closest_corner + 2) % 4;
231 const int next_corner = (closest_corner + 1) % 4;
233 const float x1 = image->
bb[prev_corner].
x;
234 const float y1 = image->
bb[prev_corner].
y;
235 const float x2 = image->
bb[next_corner].
x;
236 const float y2 = image->
bb[next_corner].
y;
237 const float x3 = image->
bb[opposite_corner].
x;
238 const float y3 = image->
bb[opposite_corner].
y;
240 const float denom = (y2 - y3) * (x1 - x3) + (x3 - x2) * (y1 - y3);
241 const float l1 = ((y2 - y3) * (
x - x3) + (x3 - x2) * (y - y3)) / denom;
242 const float l2 = ((y3 - y1) * (
x - x3) + (x1 - x3) * (y - y3)) / denom;
243 const float l3 = 1.0 -
l1 -
l2;
245 if(
l1 < 0.0 ||
l2 < 0.0 || l3 < 0.0)
247 image->
bb[closest_corner].
x =
x;
248 image->
bb[closest_corner].
y = y;
251 gtk_widget_queue_draw(widget);
258 int closest_corner = 0;
259 float distance = G_MAXFLOAT;
260 for(
int i = 0;
i < 4;
i++)
262 const float d_x = (
x - bb[
i].
x);
263 const float d_y = (y - bb[
i].
y);
264 float d = d_x * d_x + d_y * d_y;
272 return closest_corner;
277 guint
width = gtk_widget_get_allocated_width(widget);
278 guint
height = gtk_widget_get_allocated_height(widget);
312 char *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
330 char *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
337 char *last_slash = g_strrstr(filename,
"/");
339 return g_strdup(last_slash + 1);
341 return g_strdup(filename);
382 fprintf(stderr,
"error reading image `%s'\n", filename);
391 if(cairo_surface_status(image_surface) != CAIRO_STATUS_SUCCESS)
393 fprintf(stderr,
"error creating cairo surface from `%s'\n", filename);
394 cairo_surface_destroy(image_surface);
398 image->
surface = image_surface;
399 image->
image = cairo_pattern_create_for_surface(image_surface);
407 guint widget_width = gtk_widget_get_allocated_width(image->
drawing_area);
408 guint widget_height = gtk_widget_get_allocated_height(image->
drawing_area);
418 char *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
437 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(self->
it8_button));
463 const int selected = gtk_combo_box_get_active(widget);
471 g_signal_emit_by_name(self->
it8_button,
"file-set", user_data);
488 char *new_filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(widget));
504 gtk_file_chooser_unselect_all(GTK_FILE_CHOOSER(self->
it8_button));
517 gboolean *basecurve, gboolean *colorchecker, gboolean *colorin, gboolean *tonecurve)
519 GtkWidget *name_entry = NULL, *description_entry = NULL;
521 = gtk_file_chooser_dialog_new(
"save file", GTK_WINDOW(self->
window), GTK_FILE_CHOOSER_ACTION_SAVE,
522 _(
"_cancel"), GTK_RESPONSE_CANCEL, _(
"_save"), GTK_RESPONSE_ACCEPT, NULL);
524 gtk_file_chooser_set_do_overwrite_confirmation(GTK_FILE_CHOOSER(dialog),
TRUE);
527 char *last_dot = g_strrstr(reference_filename,
".");
531 char *new_filename = g_strconcat(reference_filename,
extension, NULL);
532 gtk_file_chooser_set_current_name(GTK_FILE_CHOOSER(dialog), new_filename);
540 gtk_grid_set_row_homogeneous(GTK_GRID(grid),
TRUE);
544 char *name_dot = g_strrstr(*
name,
".");
545 if(name_dot) *name_dot =
'\0';
547 name_entry = gtk_entry_new();
548 description_entry = gtk_entry_new();
550 gtk_entry_set_text(GTK_ENTRY(name_entry), *
name);
551 gtk_entry_set_text(GTK_ENTRY(description_entry), *
description);
558 label = gtk_label_new(
"style name");
559 gtk_widget_set_halign(label, GTK_ALIGN_START);
560 gtk_grid_attach(GTK_GRID(grid), label, 0, 0, 1, 1);
561 gtk_grid_attach(GTK_GRID(grid), name_entry, 1, 0, 1, 1);
562 label = gtk_label_new(
"style description");
563 gtk_widget_set_halign(label, GTK_ALIGN_START);
564 gtk_grid_attach(GTK_GRID(grid), label, 0, 1, 1, 1);
565 gtk_grid_attach(GTK_GRID(grid), description_entry, 1, 1, 1, 1);
568 label = gtk_label_new(
"modules included in the style:");
569 gtk_widget_set_halign(label, GTK_ALIGN_START);
570 g_object_set(label,
"margin-left", 50, NULL);
572 GtkWidget *cb_basecurve = gtk_check_button_new_with_label(
"base curve");
573 GtkWidget *cb_colorchecker = gtk_check_button_new_with_label(
"color look up table");
574 GtkWidget *cb_colorin = gtk_check_button_new_with_label(
"input color profile");
575 GtkWidget *cb_tonecurve = gtk_check_button_new_with_label(
"tone curve");
577 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cb_basecurve),
TRUE);
578 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cb_colorchecker),
TRUE);
579 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cb_colorin),
TRUE);
580 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(cb_tonecurve),
TRUE);
584 gtk_grid_attach(GTK_GRID(grid), label, 2, 0, 1, 1);
585 gtk_grid_attach_next_to(GTK_GRID(grid), cb_basecurve, label, GTK_POS_RIGHT, 1, 1);
586 gtk_grid_attach_next_to(GTK_GRID(grid), cb_colorchecker, cb_basecurve, GTK_POS_BOTTOM, 1, 1);
587 gtk_grid_attach_next_to(GTK_GRID(grid), cb_colorin, cb_colorchecker, GTK_POS_BOTTOM, 1, 1);
588 gtk_grid_attach_next_to(GTK_GRID(grid), cb_tonecurve, cb_colorin, GTK_POS_BOTTOM, 1, 1);
591 gtk_widget_show_all(grid);
593 gtk_file_chooser_set_extra_widget(GTK_FILE_CHOOSER(dialog), grid);
595 char *filename = NULL;
596 int res = gtk_dialog_run(GTK_DIALOG(dialog));
597 if(
res == GTK_RESPONSE_ACCEPT)
599 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
600 *
name = g_strdup(gtk_entry_get_text(GTK_ENTRY(name_entry)));
601 *
description = g_strdup(gtk_entry_get_text(GTK_ENTRY(description_entry)));
605 *basecurve = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb_basecurve));
606 *colorchecker = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb_colorchecker));
607 *colorin = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb_colorin));
608 *tonecurve = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(cb_tonecurve));
611 gtk_widget_destroy(dialog);
618 for(GList *iter = patch_names; iter; iter = g_list_next(iter))
621 char *
key = (
char *)iter->data;
626 fprintf(stderr,
"error: missing patch `%s'\n",
key);
634 fprintf(fd,
"%s",
key);
635 for(
int i = 0;
i < 3;
i++) fprintf(fd,
";%s", g_ascii_dtostr(s,
sizeof(s), source_Lab[
i]));
636 for(
int i = 0;
i < 3;
i++) fprintf(fd,
";%s", g_ascii_dtostr(s,
sizeof(s), reference_Lab[
i]));
641static void print_xml_plugin(FILE *fd,
int num,
int op_version,
const char *operation,
const char *op_params,
644 fprintf(fd,
" <plugin>\n");
645 fprintf(fd,
" <num>%d</num>\n", num);
646 fprintf(fd,
" <module>%d</module>\n", op_version);
647 fprintf(fd,
" <operation>%s</operation>\n", operation);
648 fprintf(fd,
" <op_params>%s</op_params>\n", op_params);
649 fprintf(fd,
" <enabled>%d</enabled>\n",
enabled);
650 fprintf(fd,
" <blendop_params>gz12eJxjYGBgkGAAgRNODESDBnsIHll8ANNSGQM=</blendop_params>\n");
651 fprintf(fd,
" <blendop_version>7</blendop_version>\n");
652 fprintf(fd,
" <multi_priority>0</multi_priority>\n");
653 fprintf(fd,
" <multi_name></multi_name>\n");
654 fprintf(fd,
" </plugin>\n");
658 gboolean include_basecurve, gboolean include_colorchecker, gboolean include_colorin,
659 gboolean include_tonecurve)
663 FILE *fd = g_fopen(filename,
"w");
666 fprintf(fd,
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
667 fprintf(fd,
"<darktable_style version=\"1.0\">\n");
668 fprintf(fd,
"<info>\n");
669 fprintf(fd,
" <name>%s</name>\n",
name);
670 fprintf(fd,
" <description>%s</description>\n",
description);
671 fprintf(fd,
"</info>\n");
672 fprintf(fd,
"<style>\n");
675 if(include_basecurve)
678 "gz09eJxjYIAAM6vnNnqyn22E9n235b6aa3cy6rVdRaK9/Y970fYf95bbMzA0QPEoGEqADYnNhMQGAO0WEJo=",
FALSE);
689 if(include_tonecurve)
694 if(include_colorchecker)
699 fprintf(fd,
"</style>\n");
700 fprintf(fd,
"</darktable_style>\n");
707 GHashTableIter table_iter;
710 FILE *fd = g_fopen(filename,
"w");
713 GList *patch_names = NULL;
715 fprintf(fd,
"name;%s\n",
name);
717 fprintf(fd,
"num_gray; 0\n");
719 fprintf(fd,
"patch;L_source;a_source;b_source;L_reference;a_reference;b_reference\n");
722 while(g_hash_table_iter_next(&table_iter, &
key, &
value))
724 patch_names = (GList *)
value;
749 gboolean include_basecurve, include_colorchecker, include_colorin, include_tonecurve;
751 &include_basecurve, &include_colorchecker, &include_colorin, &include_tonecurve);
753 include_basecurve, include_colorchecker, include_colorin, include_tonecurve);
763 for(GList *iter = patch_names; iter; iter = g_list_next(iter))
765 const char *
key = (
char *)iter->data;
770 fprintf(stderr,
"error: missing patch `%s'\n",
key);
779 target_L[*
i] = reference_Lab[0];
780 target_a[*
i] = reference_Lab[1];
781 target_b[*
i] = reference_Lab[2];
786 fprintf(stderr,
"warning: ignoring patch %s with large difference deltaE %g!\n",
key, deltaE);
787 fprintf(stderr,
" %g %g %g -- %g %g %g\n", source_Lab[0], source_Lab[1], source_Lab[2],
788 reference_Lab[0], reference_Lab[1], reference_Lab[2]);
796static void add_hdr_patches(
int *
N,
double **target_L,
double **target_a,
double **target_b,
799 gboolean need_hdr00 =
TRUE, need_hdr01 =
TRUE;
800 int n_extra_patches = 0;
801 double extra_target_L[2], extra_target_a[2], extra_target_b[2], extra_colorchecker_Lab[2 * 3];
803 for(
int j = 0; j < *
N; j++)
805 if((*target_L)[j] == 100.0 && (*target_a)[j] == 0.0 && (*target_b)[j] == 0.0 && (*
colorchecker_Lab)[j * 3] == 100.0
810 else if((*target_L)[j] == 200.0 && (*target_a)[j] == 0.0 && (*target_b)[j] == 0.0 && (*
colorchecker_Lab)[j * 3] == 200.0
819 extra_target_L[n_extra_patches] = 100.0;
820 extra_target_a[n_extra_patches] = 0.0;
821 extra_target_b[n_extra_patches] = 0.0;
822 extra_colorchecker_Lab[n_extra_patches * 3] = 100.0;
823 extra_colorchecker_Lab[n_extra_patches * 3 + 1] = 0.0;
824 extra_colorchecker_Lab[n_extra_patches * 3 + 2] = 0.0;
830 extra_target_L[n_extra_patches] = 200.0;
831 extra_target_a[n_extra_patches] = 0.0;
832 extra_target_b[n_extra_patches] = 0.0;
833 extra_colorchecker_Lab[n_extra_patches * 3] = 200.0;
834 extra_colorchecker_Lab[n_extra_patches * 3 + 1] = 0.0;
835 extra_colorchecker_Lab[n_extra_patches * 3 + 2] = 0.0;
839 if(n_extra_patches > 0)
841 *target_L = realloc(*target_L,
sizeof(
double) * (*
N + n_extra_patches + 4));
842 *target_a = realloc(*target_a,
sizeof(
double) * (*
N + n_extra_patches + 4));
843 *target_b = realloc(*target_b,
sizeof(
double) * (*
N + n_extra_patches + 4));
849 memmove(&(*target_L)[n_extra_patches], *target_L,
sizeof(
double) * *
N);
850 memmove(&(*target_a)[n_extra_patches], *target_a,
sizeof(
double) * *
N);
851 memmove(&(*target_b)[n_extra_patches], *target_b,
sizeof(
double) * *
N);
854 memcpy(*target_L, extra_target_L,
sizeof(
double) * n_extra_patches);
855 memcpy(*target_a, extra_target_a,
sizeof(
double) * n_extra_patches);
856 memcpy(*target_b, extra_target_b,
sizeof(
double) * n_extra_patches);
859 *
N += n_extra_patches;
884 params.tonecurve_autoscale_ab = 3;
886 params.tonecurve_type[0] = 2;
887 params.tonecurve_nodes[0] = 20;
888 for(
int k = 0;
k < 20;
k++)
890 const double x = (
k / 19.0) * (
k / 19.0);
895 params.tonecurve_type[1] = 2;
896 params.tonecurve_nodes[1] = 2;
897 params.tonecurve[1][0].x = 0.0f;
898 params.tonecurve[1][0].y = 0.0f;
899 params.tonecurve[1][1].x = 1.0f;
900 params.tonecurve[1][1].y = 1.0f;
902 params.tonecurve_type[2] = 2;
903 params.tonecurve_nodes[2] = 2;
904 params.tonecurve[2][0].x = 0.0f;
905 params.tonecurve[2][0].y = 0.0f;
906 params.tonecurve[2][1].x = 1.0f;
907 params.tonecurve[2][1].y = 1.0f;
915#define MAX_PATCHES 49
933 for(
int k = 0;
k < num;
k++)
945 const float tmp = (a); \
950 for(
int k = 0;
k < num - 1;
k++)
951 for(
int j = 0; j < num -
k - 1; j++)
972 const double x = *(
const double *)x_;
973 const double y = *(
const double *)y_;
974 return x < y ? -1 : (
x > y ? +1 : 0);
981 double *cx = malloc(
sizeof(
double)*
N);
982 double *cy = malloc(
sizeof(
double)*
N);
983 double *grays = malloc(
sizeof(
double) * 6 *
N);
985 int num_tonecurve = 0;
991 const double sat_in =
994 const double sat_out =
995 target_a[
i] * target_a[
i] +
996 target_b[
i] * target_b[
i];
998 if(sat_in < 15.0 && sat_out < 15.0)
1004 grays[6*cnt + 3] = target_L[
i];
1005 grays[6*cnt + 4] = target_a[
i];
1006 grays[6*cnt + 5] = target_b[
i];
1009 fprintf(stderr,
"detected %d/%d as gray patches for tonecurve computation\n", cnt,
N);
1014 cx[0] = cy[0] = 0.0;
1015 cx[cnt+1] = cy[cnt+1] = 100.0;
1018 num_tonecurve = cnt + 2;
1019 for(
int k = 0;
k < cnt;
k++) cx[
k + 1] = grays[6*
k+0];
1020 for(
int k = 0;
k < cnt;
k++) cy[
k + 1] = grays[6*
k+3];
1025 for(
int k = 0;
k < num_tonecurve;
k++)
1026 fprintf(stderr,
"L[%g] = %g\n", 100.0 *
k / (num_tonecurve - 1.0),
1036 cx = malloc(
sizeof(
double)*num_tonecurve);
1037 cy = malloc(
sizeof(
double)*num_tonecurve);
1038 cx[0] = cy[0] = 0.0;
1039 cx[num_tonecurve - 1] = cy[num_tonecurve - 1] = 100.0;
1040 for(
int k = 1;
k < num_tonecurve-1;
k++)
1043 Lab[0] = grays[6*
k+0];
1044 dt_Lab_to_prophotorgb(
Lab,
rgb);
1047 dt_Lab_to_prophotorgb(
Lab,
rgb);
1054 for(
int k = 0;
k <
N;
k++)
1057 Lab[0] = target_L[
k];
1058 Lab[1] = target_a[
k];
1059 Lab[2] = target_b[
k];
1060 dt_Lab_to_prophotorgb(
Lab,
rgb);
1064 dt_prophotorgb_to_Lab(
rgb,
Lab);
1065 target_L[
k] =
Lab[0];
1066 target_a[
k] =
Lab[1];
1067 target_b[
k] =
Lab[2];
1072 const double *target[3] = { target_L, target_a, target_b };
1073 double *coeff_L = malloc(
sizeof(
double) * (
N + 4) );
1074 double *coeff_a = malloc(
sizeof(
double) * (
N + 4) );
1075 double *coeff_b = malloc(
sizeof(
double) * (
N + 4) );
1076 double *
coeff[] = { coeff_L, coeff_a, coeff_b };
1077 int *
perm = malloc(
sizeof(
int) * (
N + 4));
1078 double avgerr, maxerr;
1084 char *result_string = g_strdup_printf(_(
"average dE: %.02f\nmax dE: %.02f"), avgerr, maxerr);
1085 gtk_label_set_text(GTK_LABEL(self->
result_label), result_string);
1094 int cperm[300] = { 0 };
1095 for(
int k = 0;
k < sparsity;
k++)
1097 cperm[sp++] =
perm[
k];
1100 fprintf(stderr,
"found %d basis functions:\n", sp);
1101 for(
int k = 0;
k < sp;
k++)
1102 fprintf(stderr,
"perm[%d] = %d source %g %g %g\n",
k, cperm[
k],
colorchecker_Lab[3 * cperm[
k]],
1128 double *target_L = (
double *)calloc(
sizeof(
double), (
N + 4));
1129 double *target_a = (
double *)calloc(
sizeof(
double), (
N + 4));
1130 double *target_b = (
double *)calloc(
sizeof(
double), (
N + 4));
1133 GHashTableIter table_iter;
1134 gpointer set_key,
value;
1137 while(g_hash_table_iter_next(&table_iter, &set_key, &
value))
1139 GList *patch_names = (GList *)
value;
1145 int sparsity = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(self->
number_patches)) + 4;
1162 if(
flags & GTK_STATE_FLAG_INSENSITIVE)
1175 image->
shrink = gtk_range_get_value(range);
1185 gtk_box_pack_start(GTK_BOX(page), hbox,
FALSE,
TRUE, 0);
1187 GtkWidget *image_button = gtk_file_chooser_button_new(
"image of a color chart", GTK_FILE_CHOOSER_ACTION_OPEN);
1191 = gtk_file_chooser_button_new(
"description of a color chart", GTK_FILE_CHOOSER_ACTION_OPEN);
1194 GtkWidget *source_shrink = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, 0.5, 2.0, 0.01);
1195 gtk_scale_set_value_pos(GTK_SCALE(source_shrink), GTK_POS_RIGHT);
1198 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(
"image:"),
FALSE,
TRUE, 0);
1199 gtk_box_pack_start(GTK_BOX(hbox), image_button,
TRUE,
TRUE, 0);
1200 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(
"chart:"),
FALSE,
TRUE, 0);
1201 gtk_box_pack_start(GTK_BOX(hbox), cht_button,
TRUE,
TRUE, 0);
1202 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(
"size:"),
FALSE,
TRUE, 0);
1203 gtk_box_pack_start(GTK_BOX(hbox), source_shrink,
TRUE,
TRUE, 0);
1209 g_signal_connect(cht_button,
"state-flags-changed", G_CALLBACK(
cht_state_callback), self);
1223 gtk_box_pack_start(GTK_BOX(page), hbox,
FALSE,
TRUE, 0);
1225 GtkWidget *reference_mode = gtk_combo_box_text_new();
1226 gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(reference_mode), NULL,
"cie/it8 file");
1227 gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(reference_mode), NULL,
"color chart image");
1228 gtk_combo_box_set_active(GTK_COMBO_BOX(reference_mode), 0);
1232 = gtk_file_chooser_button_new(
"reference data of a color chart", GTK_FILE_CHOOSER_ACTION_OPEN);
1236 = gtk_file_chooser_button_new(
"image of a color chart", GTK_FILE_CHOOSER_ACTION_OPEN);
1239 GtkWidget *reference_shrink = gtk_scale_new_with_range(GTK_ORIENTATION_HORIZONTAL, 0.5, 2.0, 0.01);
1240 gtk_scale_set_value_pos(GTK_SCALE(reference_shrink), GTK_POS_RIGHT);
1243 gtk_box_pack_start(GTK_BOX(hbox), gtk_label_new(
"mode:"),
FALSE,
TRUE, 0);
1244 gtk_box_pack_start(GTK_BOX(hbox), reference_mode,
TRUE,
TRUE, 0);
1247 gtk_box_pack_start(GTK_BOX(reference_it8_box), gtk_label_new(
"reference it8:"),
FALSE,
TRUE, 0);
1248 gtk_box_pack_start(GTK_BOX(reference_it8_box), it8_button,
TRUE,
TRUE, 0);
1249 gtk_box_pack_start(GTK_BOX(hbox), reference_it8_box,
TRUE,
TRUE, 0);
1252 gtk_box_pack_start(GTK_BOX(reference_image_box), gtk_label_new(
"reference image:"),
FALSE,
TRUE, 0);
1253 gtk_box_pack_start(GTK_BOX(reference_image_box), reference_image_button,
TRUE,
TRUE, 0);
1254 gtk_box_pack_start(GTK_BOX(reference_image_box), gtk_label_new(
"size:"),
FALSE,
TRUE, 0);
1255 gtk_box_pack_start(GTK_BOX(reference_image_box), reference_shrink,
TRUE,
TRUE, 0);
1256 gtk_box_pack_start(GTK_BOX(hbox), reference_image_box,
TRUE,
TRUE, 0);
1262 gtk_widget_show_all(reference_it8_box);
1263 gtk_widget_show_all(reference_image_box);
1265 gtk_widget_hide(reference_image_box);
1267 gtk_widget_set_no_show_all(reference_it8_box,
TRUE);
1268 gtk_widget_set_no_show_all(reference_image_box,
TRUE);
1291 GtkWidget *number_patches = gtk_spin_button_new_with_range(0, 49, 1);
1292 gtk_spin_button_set_value(GTK_SPIN_BUTTON(number_patches), 24);
1293 gtk_grid_attach(GTK_GRID(page), gtk_label_new(
"number of final patches"), 0, line, 1, 1);
1294 gtk_grid_attach(GTK_GRID(page), number_patches, 1, line++, 1, 1);
1296 GtkWidget *process_button = gtk_button_new_with_label(
"process");
1297 GtkWidget *export_button = gtk_button_new_with_label(
"export");
1298 GtkWidget *export_raw_button = gtk_button_new_with_label(
"export raw data as csv");
1299 gtk_grid_attach(GTK_GRID(page), process_button, 1, line, 1, 1);
1300 gtk_grid_attach(GTK_GRID(page), export_button, 2, line, 1, 1);
1301 gtk_grid_attach(GTK_GRID(page), export_raw_button, 3, line++, 1, 1);
1304 gtk_widget_set_halign(self->
result_label, GTK_ALIGN_START);
1305 gtk_grid_attach(GTK_GRID(page), self->
result_label, 1, line++, 3, 1);
1326 gtk_label_new(
"source image"));
1330 gtk_label_new(
"reference values"));
1340 GtkWidget *scrolled_window = gtk_scrolled_window_new(NULL, NULL);
1341 gtk_widget_set_size_request(scrolled_window, -1, 15);
1342 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrolled_window), GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
1343 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(scrolled_window), GTK_SHADOW_ETCHED_IN);
1358 gtk_container_add(GTK_CONTAINER(scrolled_window), self->
treeview);
1367 return scrolled_window;
1370static void add_column(GtkTreeView *treeview,
const char *title,
int column_id,
int sort_column)
1372 GtkCellRenderer *renderer;
1373 GtkTreeViewColumn *column;
1374 renderer = gtk_cell_renderer_text_new();
1375 column = gtk_tree_view_column_new_with_attributes(title, renderer,
"text", column_id, NULL);
1376 gtk_tree_view_column_set_sort_column_id(column, sort_column);
1377 gtk_tree_view_append_column(treeview, column);
1384 gboolean valid = gtk_tree_model_get_iter_first(self->
model, &iter);
1396 char *s_Lab_in, *s_RGB_in, *s_deltaE_1976, *s_deltaE_2000;
1397 float deltaE_1976 = 0.0, deltaE_2000 = 0.0;
1406 s_RGB_in = g_strdup_printf(
"%d; %d; %d", (
int)(patch->
rgb[0] * 255 + 0.5),
1407 (
int)(patch->
rgb[1] * 255 + 0.5), (
int)(patch->
rgb[2] * 255 + 0.5));
1408 s_Lab_in = g_strdup_printf(
"%.02f; %.02f; %.02f", in_Lab[0], in_Lab[1], in_Lab[2]);
1411 s_deltaE_1976 = g_strdup_printf(
"%.02f", deltaE_1976);
1412 s_deltaE_2000 = g_strdup_printf(
"%.02f", deltaE_2000);
1416 s_Lab_in = g_strdup(
"?");
1417 s_RGB_in = g_strdup(
"?");
1418 s_deltaE_1976 = g_strdup(
"-");
1419 s_deltaE_2000 = g_strdup(
"-");
1421 char *s_Lab_ref = g_strdup_printf(
"%.02f; %.02f; %.02f",
Lab[0],
Lab[1],
Lab[2]);
1434 valid = gtk_tree_model_iter_next(self->
model, &iter);
1461 gtk_list_store_clear(GTK_LIST_STORE(self->
model));
1465 GList *patch_names = g_hash_table_get_keys(self->
chart->
box_table);
1466 patch_names = g_list_sort(patch_names, (GCompareFunc)g_strcmp0);
1469 gtk_list_store_append(GTK_LIST_STORE(self->
model), &iter);
1472 g_list_free(patch_names);
1519 g_hash_table_insert(table, g_strdup(
key), patch);
1530 int x_start, y_start, x_end, y_end;
1532 xyz[0] = xyz[1] = xyz[2] = 0.0;
1551 double sample_x = 0.0, sample_y = 0.0, sample_z = 0.0;
1552 size_t n_samples = 0;
1554 for(
int y = y_start; y < y_end; y++)
1555 for(
int x = x_start;
x < x_end;
x++)
1562 float *pixel = &image->
xyz[(
x + y * image->
width) * 3];
1563 sample_x += pixel[0];
1564 sample_y += pixel[1];
1565 sample_z += pixel[2];
1570 xyz[0] = sample_x / n_samples;
1571 xyz[1] = sample_y / n_samples;
1572 xyz[2] = sample_z / n_samples;
1577 for(
int i = 0;
i < 4;
i++)
1586 box_t inner_box = *outer_box;
1588 inner_box.
p.
x += x_shrink;
1589 inner_box.
p.
y += y_shrink;
1590 inner_box.
w -= 2.0 * x_shrink;
1591 inner_box.
h -= 2.0 * y_shrink;
1607 int *x_end,
int *y_end)
1641 memset(image, 0x00,
sizeof(
image_t));
1644 gtk_widget_set_size_request(image->
drawing_area, -1, 50);
1646 GDK_POINTER_MOTION_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK);
1649 g_signal_connect(image->
drawing_area,
"motion-notify-event", G_CALLBACK(motion_cb), self);
1656 if(image->
image) cairo_pattern_destroy(image->
image);
1659 image->
image = NULL;
1667 for(
int y = 0; y <
height; y++)
1670 float *pixel = &image[(
x + y *
width) * 3];
1677 gtk_init(&argc, &argv);
1679 char *source_filename = argc >= 2 ? argv[1] : NULL;
1680 char *cht_filename = argc >= 3 ? argv[2] : NULL;
1681 char *it8_filename = NULL;
1682 char *reference_filename = NULL;
1685 char *upper_string = g_ascii_strup(argv[3], -1);
1686 if(g_str_has_suffix(upper_string,
".PFM"))
1687 reference_filename = argv[3];
1689 it8_filename = argv[3];
1696 gtk_window_set_title(GTK_WINDOW(
window),
"darktable LUT tool");
1697 gtk_container_set_border_width(GTK_CONTAINER(
window), 3);
1698 gtk_window_set_default_size(GTK_WINDOW(
window), 800, 600);
1699 g_signal_connect(GTK_WINDOW(
window),
"destroy", G_CALLBACK(gtk_main_quit), NULL);
1702 GtkWidget *vpaned = gtk_paned_new(GTK_ORIENTATION_VERTICAL);
1703 gtk_container_add(GTK_CONTAINER(
window), vpaned);
1718 gtk_widget_show_all(
window);
1723 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(self->
image_button), source_filename);
1724 if(cht_filename &&
open_cht(self, cht_filename))
1726 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(self->
cht_button), cht_filename);
1727 if(it8_filename &&
open_it8(self, it8_filename))
1728 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(self->
it8_button), it8_filename);
1732 gtk_combo_box_set_active(GTK_COMBO_BOX(self->
reference_mode), 1);
1737#ifdef GDK_WINDOWING_QUARTZ
1745static int parse_csv(
dt_lut_t *self,
const char *filename,
double **target_L_ptr,
double **target_a_ptr,
1746 double **target_b_ptr,
double **source_Lab_ptr,
int *num_gray,
char **
name,
char **
description)
1748 *target_L_ptr = NULL;
1749 *target_a_ptr = NULL;
1750 *target_b_ptr = NULL;
1751 *source_Lab_ptr = NULL;
1755 FILE *
f = g_fopen(filename,
"rb");
1758 while(fscanf(
f,
"%*[^\n]\n") != EOF)
N++;
1759 fseek(
f, 0, SEEK_SET);
1768 char key[16] = {0},
value[256] = {0};
1769 int read_res = fscanf(
f,
"%15[^;];%255[^\n]\n",
key,
value);
1770 if(g_strcmp0(
key,
"name") || read_res == EOF)
1772 fprintf(stderr,
"error: expected `name' in the first line\n");
1779 read_res = fscanf(
f,
"%15[^;];%255[^\n]\n",
key,
value);
1780 if(g_strcmp0(
key,
"description") || read_res == EOF)
1782 fprintf(stderr,
"error: expected `description' in the second line\n");
1789 read_res = fscanf(
f,
"%15[^;];%d\n",
key, num_gray);
1790 if(g_strcmp0(
key,
"num_gray") || read_res == EOF)
1792 fprintf(stderr,
"error: missing num_gray in csv\n");
1799 read_res = fscanf(
f,
"%*[^\n]\n");
1802 double *target_L = (
double *)calloc(
sizeof(
double), (
N + 4));
1803 double *target_a = (
double *)calloc(
sizeof(
double), (
N + 4));
1804 double *target_b = (
double *)calloc(
sizeof(
double), (
N + 4));
1805 double *source_Lab = (
double *)calloc(
sizeof(
double) * 3,
N);
1806 *target_L_ptr = target_L;
1807 *target_a_ptr = target_a;
1808 *target_b_ptr = target_b;
1809 *source_Lab_ptr = source_Lab;
1812 for(
int i = 0;
i <
N;
i++)
1814 char *patchname = line, *iter = line, *endptr;
1815 if(fgets(line,
sizeof(line) /
sizeof(*line),
f) == 0)
break;
1816 while(*iter !=
';') iter++;
1819 source_Lab[3 *
i] = g_ascii_strtod(iter, &endptr);
1820 if(iter == endptr || *endptr !=
';')
break;
1822 source_Lab[3 *
i + 1] = g_ascii_strtod(iter, &endptr);
1823 if(iter == endptr || *endptr !=
';')
break;
1825 source_Lab[3 *
i + 2] = g_ascii_strtod(iter, &endptr);
1826 if(iter == endptr || *endptr !=
';')
break;
1828 target_L[
i] = g_ascii_strtod(iter, &endptr);
1829 if(iter == endptr || *endptr !=
';')
break;
1831 target_a[
i] = g_ascii_strtod(iter, &endptr);
1832 if(iter == endptr || *endptr !=
';')
break;
1834 target_b[
i] = g_ascii_strtod(iter, &endptr);
1835 if(iter == endptr || *endptr !=
'\n')
break;
1837 const double d[3] = { target_L[
i], target_a[
i], target_b[
i] };
1838 if(sqrt(
d[0] *
d[0] +
d[1] *
d[1] +
d[2] *
d[2]) >
thrs)
1840 fprintf(stderr,
"warning: ignoring patch %s with large difference deltaE %g!\n", patchname,
1841 sqrt(
d[0] *
d[0] +
d[1] *
d[1] +
d[2] *
d[2]));
1842 fprintf(stderr,
" %g %g %g -- %g %g %g\n", source_Lab[3 *
i + 0], source_Lab[3 *
i + 1],
1843 source_Lab[3 *
i + 2], target_L[
i], target_a[
i], target_b[
i]);
1855 const char *filename_csv = argv[2];
1856 const int num_patches = atoi(argv[3]);
1857 const char *filename_style = argv[4];
1859 const int sparsity = num_patches + 4;
1870 fprintf(stderr,
"error parsing `%s', giving up\n", filename_csv);
1899 fprintf(stderr,
"Usage: %s [<input Lab pfm file>] [<cht file>] [<reference cgats/it8 or Lab pfm file>]\n"
1900 " %s --csv <csv file> <number patches> <output dtstyle file>\n",
1910 SetErrorMode(SEM_FAILCRITICALERRORS);
1914 omp_set_num_threads(omp_get_num_procs());
1921 if(argc >= 2 && !strcmp(argv[1],
"--help"))
1923 else if(argc >= 2 && !g_strcmp0(argv[1],
"--csv"))
static char * get_filename_base(const char *filename)
static void add_patches_to_array(dt_lut_t *self, GList *patch_names, int *N, int *i, double *target_L, double *target_a, double *target_b, double *colorchecker_Lab)
static gboolean handle_motion(GtkWidget *widget, GdkEventMotion *event, dt_lut_t *self, image_t *image)
static int main_gui(dt_lut_t *self, int argc, char *argv[])
static box_t * find_patch(GHashTable *table, gpointer key)
static void cht_state_callback(GtkWidget *widget, GtkStateFlags flags, gpointer user_data)
static void init_image(dt_lut_t *self, image_t *image, GCallback motion_cb)
static void get_Lab_from_box(box_t *box, float *Lab)
static int find_closest_corner(point_t *bb, float x, float y)
static void process_button_clicked_callback(GtkButton *button, gpointer user_data)
static box_t get_sample_box(chart_t *chart, box_t *outer_box, float shrink)
static point_t map_point_to_view(image_t *image, point_t p)
static void export_raw(dt_lut_t *self, char *filename, char *name, char *description)
static void reference_mode_changed_callback(GtkComboBox *widget, gpointer user_data)
static void reset_bb(image_t *image)
static int parse_csv(dt_lut_t *self, const char *filename, double **target_L_ptr, double **target_a_ptr, double **target_b_ptr, double **source_Lab_ptr, int *num_gray, char **name, char **description)
static void print_patches(dt_lut_t *self, FILE *fd, GList *patch_names)
static void get_pixel_region(const image_t *const image, const point_t *const corners, int *x_start, int *y_start, int *x_end, int *y_end)
static void collect_reference_patches(dt_lut_t *self)
static const point_t bb_ref[]
static int main_csv(dt_lut_t *self, int argc, char *argv[])
static char * encode_colorchecker(int num, const double *point, const double **target, int *permutation)
static void map_mouse_to_0_1(GtkWidget *widget, GdkEventMotion *event, image_t *image, float *x, float *y)
static void free_image(image_t *image)
static gboolean open_cht(dt_lut_t *self, const char *filename)
static void get_corners(const float *homography, box_t *box, point_t *corners)
static void export_style(dt_lut_t *self, const char *filename, const char *name, const char *description, gboolean include_basecurve, gboolean include_colorchecker, gboolean include_colorin, gboolean include_tonecurve)
static void collect_reference_patches_foreach(gpointer key, gpointer value, gpointer user_data)
static void get_xyz_sample_from_image(const image_t *const image, float shrink, box_t *box, float *xyz)
static gboolean draw_image_callback(GtkWidget *widget, cairo_t *cr, gpointer user_data)
static void export_button_clicked_callback(GtkButton *button, gpointer user_data)
static void show_usage(const char *exe)
static void export_raw_button_clicked_callback(GtkButton *button, gpointer user_data)
static gboolean open_it8(dt_lut_t *self, const char *filename)
static char * get_export_filename(dt_lut_t *self, const char *extension, char **name, char **description, gboolean *basecurve, gboolean *colorchecker, gboolean *colorin, gboolean *tonecurve)
static gboolean open_image(image_t *image, const char *filename)
static void init_table(dt_lut_t *self)
static void source_image_changed_callback(GtkFileChooserButton *widget, gpointer user_data)
static void size_allocate_callback(GtkWidget *widget, GdkRectangle *allocation, gpointer user_data)
static void print_xml_plugin(FILE *fd, int num, int op_version, const char *operation, const char *op_params, gboolean enabled)
static void add_hdr_patches(int *N, double **target_L, double **target_a, double **target_b, double **colorchecker_Lab)
static void process_data(dt_lut_t *self, double *target_L, double *target_a, double *target_b, double *colorchecker_Lab, int N, int sparsity)
static GtkWidget * create_notebook_page_reference(dt_lut_t *self)
static gboolean motion_notify_callback_reference(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
static gboolean open_reference_image(dt_lut_t *self, const char *filename)
static void add_column(GtkTreeView *treeview, const char *title, int column_id, int sort_column)
static void collect_source_patches(dt_lut_t *self)
static void update_table(dt_lut_t *self)
static GtkWidget * create_notebook_page_process(dt_lut_t *self)
static void collect_source_patches_foreach(gpointer key, gpointer value, gpointer user_data)
static int compare_L_source(const void *x_, const void *y_)
static void ref_image_changed_callback(GtkFileChooserButton *widget, gpointer user_data)
static void cht_changed_callback(GtkFileChooserButton *widget, gpointer user_data)
static void image_lab_to_xyz(float *image, const int width, const int height)
static char * encode_tonecurve(const tonecurve_t *c)
static void update_corner(image_t *image, int which, float *x, float *y)
static GtkWidget * create_table(dt_lut_t *self)
static GtkWidget * create_notebook(dt_lut_t *self)
static void it8_changed_callback(GtkFileChooserButton *widget, gpointer user_data)
static GtkWidget * create_notebook_page_source(dt_lut_t *self)
static void map_boundingbox_to_view(image_t *image, point_t *bb)
static gboolean open_source_image(dt_lut_t *self, const char *filename)
static void shrink_changed_callback(GtkRange *range, gpointer user_data)
static void get_boundingbox(const image_t *const image, point_t *bb)
static gboolean motion_notify_callback_source(GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
float * read_pfm(const char *filename, int *wd, int *ht)
double tonecurve_apply(const tonecurve_t *c, const double L)
double tonecurve_unapply(const tonecurve_t *c, const double L)
void tonecurve_delete(tonecurve_t *c)
void tonecurve_create(tonecurve_t *c, double *Lin, double *Lout, const int32_t num)
const char ** description(struct dt_iop_module_t *self)
static __DT_CLONE_TARGETS__ void homography(float *homograph, const float angle, const float shift_v, const float shift_h, const float shear, const float f_length_kb, const float orthocorr, const float aspect, const int width, const int height, dt_iop_ashift_homodir_t dir)
const char * extension(dt_imageio_module_data_t *data)
chart_t * parse_cht(const char *filename)
void free_chart(chart_t *chart)
int parse_it8(const char *filename, chart_t *chart)
void checker_set_color(box_t *box, dt_colorspaces_color_profile_type_t color_space, float c0, float c1, float c2)
const float *const const float coeff[3]
struct _GtkWidget GtkWidget
GtkWidget, opaque, spelled exactly as GTK spells it.
static dt_aligned_pixel_t rgb
static dt_aligned_pixel_t XYZ
static dt_aligned_pixel_t Lab
float dt_colorspaces_deltaE_1976(dt_aligned_pixel_t Lab0, dt_aligned_pixel_t Lab1)
float dt_colorspaces_deltaE_2000(dt_aligned_pixel_t Lab0, dt_aligned_pixel_t Lab1)
GHashTable * selected
set of checked row labels, mirrored to conf on every change
void draw_image(cairo_t *cr, image_t *image)
void draw_color_boxes_outline(cairo_t *cr, const float *homography, chart_t *chart)
void draw_no_image(cairo_t *cr, GtkWidget *widget)
void center_image(cairo_t *cr, image_t *image)
void stroke_boxes(cairo_t *cr, float line_width)
void clear_background(cairo_t *cr)
void draw_color_boxes_inside(cairo_t *cr, const float *homography, chart_t *chart, float shrink, float line_width, gboolean colored)
cairo_surface_t * cairo_surface_create_from_xyz_data(const float *const image, const int width, const int height)
void draw_f_boxes(cairo_t *cr, const float *homography, chart_t *chart)
void draw_boundingbox(cairo_t *cr, point_t *bb)
void draw_d_boxes(cairo_t *cr, const float *homography, chart_t *chart)
void set_offset_and_scale(image_t *image, float width, float height)
char * dt_exif_xmp_encode_internal(const unsigned char *input, const int len, int *output_len, gboolean do_compress)
What a photograph says about itself: the EXIF, IPTC and XMP tags a camera and a cataloguer write,...
memcpy(roundtrip, origin, sizeof(roundtrip))
point_t apply_homography(point_t p, const float *h)
int get_homography(const point_t *source, const point_t *target, float *h)
static const float colorchecker_Lab[]
static const dt_iop_drawlayer_params_t * params
float *const restrict const size_t k
#define IS_NULL_PTR(p)
C is way too permissive with !=, == and if(var) checks, which can mean too many things depending on w...
static void * dt_calloc_align(size_t size)
dt_alloc_align() followed by a zero fill.
static void dt_free_gpointer(gpointer ptr)
g_free() one pointer, with the signature GDestroyNotify wants.
#define dt_free(ptr)
g_free() ptr and set it to NULL, skipping both if it is already NULL.
dt_mipmap_buffer_dsc_flags flags
#define __OMP_PARALLEL_FOR__(...)
void dt_osx_focus_window()
void dt_osx_prepare_environment()
DT_ALIGNED_PIXEL float dt_aligned_pixel_t[4]
dt_colorspaces_color_profile_type_t color_space
dt_iop_tonecurve_node_t tonecurve[3][20]
int tonecurve_autoscale_ab
char * colorchecker_encoded
GtkWidget * reference_image_box
char * reference_filename
GtkWidget * source_shrink
GtkWidget * reference_mode
GtkWidget * reference_shrink
GtkWidget * process_button
GtkWidget * reference_image_button
GtkWidget * reference_it8_box
GtkWidget * number_patches
GtkWidget * export_raw_button
GtkWidget * export_button
GHashTable * picked_source_patches
cairo_surface_t * surface
float thinplate_color_pos(float L, float a, float b)
int thinplate_match(const tonecurve_t *curve, int dim, int N, const double *point, const double **target, int S, int *permutation, double **coeff, double *avgerr, double *maxerr)