-
Notifications
You must be signed in to change notification settings - Fork 8
/
ufraw_preview.c
6031 lines (5506 loc) · 249 KB
/
ufraw_preview.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* UFRaw - Unidentified Flying Raw converter for digital camera images
*
* ufraw_preview.c - GUI for controlling all the image manipulations
* Copyright 2004-2016 by Udi Fuchs
*
* based on the GIMP plug-in by Pawel T. Jochym jochym at ifj edu pl,
*
* based on the GIMP plug-in by Dave Coffin
* http://www.cybercom.net/~dcoffin/
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*/
#include "ufraw.h"
#include "uf_gtk.h"
#include "ufraw_ui.h"
#include "curveeditor_widget.h"
#include <gdk/gdkkeysyms.h>
#include <glib/gi18n.h>
#include <gtkimageview/gtkimagescrollwin.h>
#include <gtkimageview/gtkimageview.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#ifdef _OPENMP
#include <omp.h>
#define uf_omp_get_thread_num() omp_get_thread_num()
#define uf_omp_get_max_threads() omp_get_max_threads()
#else
#define uf_omp_get_thread_num() 0
#define uf_omp_get_max_threads() 1
#endif
#ifdef _WIN32 /* GDK threads are not supported on the Windows platform. */
#define gdk_threads_add_timeout g_timeout_add
#define gdk_threads_add_idle_full g_idle_add_full
#endif
#ifdef __MINGW32__
#ifdef __MINGW64__
#define uf_long gint64
#else
#define uf_long gint32
#endif
#else
#define uf_long long
#endif
void ufraw_chooser_toggle(GtkToggleButton *button, GtkFileChooser *filechooser);
static void update_crop_ranges(preview_data *data, gboolean render);
static void adjustment_update(GtkAdjustment *adj, double *valuep);
static void button_update(GtkWidget *button, gpointer user_data);
extern GtkFileChooser *ufraw_raw_chooser(conf_data *conf, const char *defPath,
const gchar *label, GtkWindow *toplevel, const gchar *cancel,
gboolean multiple);
/* Set to huge number so that the preview size is set by the screen size */
static const int def_preview_width = 9000;
static const int def_preview_height = 9000;
static const int his_max_height = 256;
enum { pixel_format, percent_format };
enum { without_zone, with_zone };
static char *expanderText[] = { N_("Raw histogram with conversion curves"),
N_("Live histogram"), NULL
};
static const GdkCursorType Cursors[cursor_num] = {
GDK_HAND2, GDK_CROSSHAIR,
GDK_LEFT_SIDE, GDK_RIGHT_SIDE, GDK_TOP_SIDE, GDK_BOTTOM_SIDE,
GDK_TOP_LEFT_CORNER, GDK_TOP_RIGHT_CORNER,
GDK_BOTTOM_LEFT_CORNER, GDK_BOTTOM_RIGHT_CORNER,
GDK_FLEUR
};
static const char *grayscaleModeNames[5] = {
N_("None"),
N_("Lightness"),
N_("Luminance"),
N_("Value"),
N_("Channel Mixer")
};
preview_data *get_preview_data(void *object)
{
GtkWidget *widget;
if (GTK_IS_ADJUSTMENT(object)) {
widget = g_object_get_data(object, "Parent-Widget");
} else if (GTK_IS_MENU(object)) {
widget = g_object_get_data(G_OBJECT(object), "Parent-Widget");
} else if (GTK_IS_MENU_ITEM(object)) {
GtkWidget *menu = gtk_widget_get_ancestor(object, GTK_TYPE_MENU);
widget = g_object_get_data(G_OBJECT(menu), "Parent-Widget");
} else {
widget = object;
}
GtkWidget *parentWindow = gtk_widget_get_toplevel(widget);
preview_data *data =
g_object_get_data(G_OBJECT(parentWindow), "Preview-Data");
return data;
}
void ufraw_focus(void *window, gboolean focus)
{
if (focus) {
GtkWindow *parentWindow =
(void *)ufraw_message(UFRAW_SET_PARENT, (char *)window);
g_object_set_data(G_OBJECT(window), "WindowParent", parentWindow);
if (parentWindow != NULL)
gtk_window_set_accept_focus(GTK_WINDOW(parentWindow), FALSE);
} else {
GtkWindow *parentWindow = g_object_get_data(G_OBJECT(window),
"WindowParent");
ufraw_message(UFRAW_SET_PARENT, (char *)parentWindow);
if (parentWindow != NULL)
gtk_window_set_accept_focus(GTK_WINDOW(parentWindow), TRUE);
}
}
void ufraw_messenger(char *message, void *parentWindow)
{
GtkDialog *dialog;
if (parentWindow == NULL) {
ufraw_batch_messenger(message);
} else {
dialog = GTK_DIALOG(gtk_message_dialog_new(GTK_WINDOW(parentWindow),
GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "%s", message));
gtk_window_set_title(GTK_WINDOW(dialog), _("UFRaw Message"));
gtk_dialog_run(dialog);
gtk_widget_destroy(GTK_WIDGET(dialog));
}
}
/*
* Check if a named curve exists in a table of curves
* First cut is to check for name match only.
* Ideally, we should check points defining the curve as well.
* However, if we do that we also need to have some way of
* differentiating them by name in the drop-downs
* and those names must be consistent across reloads,
* regardless of the order in the .ufrawrc file
* returns:
* -1 if curve does not exist in the table of curves
* index into the curve table if the curve is there
* 0 < return value < table.curveCount
* TODO: The various curve tables and their counts, max counts, and current index
* should be combined into a single object/structure which can be passed
* as one param.
*/
static int find_curve_in_conf(CurveData *newCurve, CurveData *curveTbl, int curveCount)
{
int curveIdx = -1;
int i;
for ( i = 0 ; i < curveCount ; i++) {
if (!strcmp(newCurve->name, curveTbl->name)) {
curveIdx = i;
break;
}
curveTbl++;
}
printf("find_curve_in_conf, curveIdx=%d\n", curveIdx);
return curveIdx;
}
static void load_curve(GtkWidget *widget, long curveType)
{
preview_data *data = get_preview_data(widget);
GtkFileChooser *fileChooser;
GtkFileFilter *filter;
GSList *list, *saveList;
CurveData c;
int curveIdx; /* index into curve table; < 0 means not there */
char *cp;
if (data->FreezeDialog) return;
if ((curveType == base_curve && CFG->BaseCurveCount >= max_curves) ||
(curveType == luminosity_curve && CFG->curveCount >= max_curves)) {
ufraw_message(UFRAW_ERROR, _("No more room for new curves."));
return;
}
fileChooser = GTK_FILE_CHOOSER(gtk_file_chooser_dialog_new(_("Load curve"),
GTK_WINDOW(gtk_widget_get_toplevel(widget)),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL));
ufraw_focus(fileChooser, TRUE);
gtk_file_chooser_set_select_multiple(fileChooser, TRUE);
gtk_file_chooser_set_show_hidden(fileChooser, FALSE);
GtkWidget *button = gtk_check_button_new_with_label(_("Show hidden files"));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), FALSE);
g_signal_connect(G_OBJECT(button), "toggled",
G_CALLBACK(ufraw_chooser_toggle), fileChooser);
gtk_file_chooser_set_extra_widget(fileChooser, button);
filter = GTK_FILE_FILTER(gtk_file_filter_new());
gtk_file_filter_set_name(filter, _("All curve formats"));
gtk_file_filter_add_pattern(filter, "*.ntc");
gtk_file_filter_add_pattern(filter, "*.NTC");
gtk_file_filter_add_pattern(filter, "*.ncv");
gtk_file_filter_add_pattern(filter, "*.NCV");
gtk_file_filter_add_pattern(filter, "*.curve");
gtk_file_filter_add_pattern(filter, "*.CURVE");
gtk_file_chooser_add_filter(fileChooser, filter);
filter = GTK_FILE_FILTER(gtk_file_filter_new());
gtk_file_filter_set_name(filter, _("UFRaw curve format"));
gtk_file_filter_add_pattern(filter, "*.curve");
gtk_file_filter_add_pattern(filter, "*.CURVE");
gtk_file_chooser_add_filter(fileChooser, filter);
filter = GTK_FILE_FILTER(gtk_file_filter_new());
gtk_file_filter_set_name(filter, _("Nikon curve format"));
gtk_file_filter_add_pattern(filter, "*.ntc");
gtk_file_filter_add_pattern(filter, "*.NTC");
gtk_file_filter_add_pattern(filter, "*.ncv");
gtk_file_filter_add_pattern(filter, "*.NCV");
gtk_file_chooser_add_filter(fileChooser, filter);
filter = GTK_FILE_FILTER(gtk_file_filter_new());
gtk_file_filter_set_name(filter, _("All files"));
gtk_file_filter_add_pattern(filter, "*");
gtk_file_chooser_add_filter(fileChooser, filter);
if (strlen(CFG->curvePath) > 0)
gtk_file_chooser_set_current_folder(fileChooser, CFG->curvePath);
if (gtk_dialog_run(GTK_DIALOG(fileChooser)) == GTK_RESPONSE_ACCEPT) {
for (list = saveList = gtk_file_chooser_get_filenames(fileChooser);
list != NULL;
list = g_slist_next(list)) {
c = conf_default.curve[0];
if (curve_load(&c, list->data) != UFRAW_SUCCESS) continue;
if (curveType == base_curve) {
if (CFG->BaseCurveCount >= max_curves) break;
curveIdx = find_curve_in_conf(&c, CFG->curve, CFG->curveCount);
if (curveIdx < 0) { /* check if unknown curve */
gtk_combo_box_append_text(data->BaseCurveCombo, c.name);
CFG->BaseCurve[CFG->BaseCurveCount] = c;
CFG->BaseCurveIndex = CFG->BaseCurveCount;
CFG->BaseCurveCount++;
/* Add curve to .ufrawrc but don't make it default */
RC->BaseCurve[RC->BaseCurveCount++] = c;
if (CFG_cameraCurve)
gtk_combo_box_set_active(data->BaseCurveCombo,
CFG->BaseCurveIndex);
else
gtk_combo_box_set_active(data->BaseCurveCombo,
CFG->BaseCurveIndex - 2);
} else { /* change the active curve */
gtk_combo_box_set_active(data->CurveCombo, curveIdx);
}
} else { /* curveType==luminosity_curve */
if (CFG->curveCount >= max_curves) break;
curveIdx = find_curve_in_conf(&c, CFG->curve, CFG->curveCount);
if (curveIdx < 0) {
/* curve is a new one we don't know about, record it */
gtk_combo_box_append_text(data->CurveCombo, c.name);
CFG->curve[CFG->curveCount] = c;
CFG->curveIndex = CFG->curveCount;
CFG->curveCount++;
/* Add curve to .ufrawrc but don't make it default */
RC->curve[RC->curveCount++] = c;
gtk_combo_box_set_active(data->CurveCombo, CFG->curveIndex);
} else { /* change active curve to existing one */
gtk_combo_box_set_active(data->CurveCombo, curveIdx);
}
}
/* Save the curve to the config, as it is the one used by this image
* if it is a new one, also save it to the .ufrawrc file
*/
cp = g_path_get_dirname(list->data);
g_strlcpy(CFG->curvePath, cp, max_path);
if (curveIdx < 0) {
g_strlcpy(RC->curvePath, cp, max_path);
conf_save(RC, NULL, NULL);
}
g_free(cp);
g_free(list->data);
}
if (list != NULL)
ufraw_message(UFRAW_ERROR, _("No more room for new curves."));
g_slist_free(saveList);
}
ufraw_focus(fileChooser, FALSE);
gtk_widget_destroy(GTK_WIDGET(fileChooser));
}
static void save_curve(GtkWidget *widget, long curveType)
{
preview_data *data = get_preview_data(widget);
GtkFileChooser *fileChooser;
GtkFileFilter *filter;
char defFilename[max_name];
char *filename, *cp;
if (data->FreezeDialog) return;
fileChooser = GTK_FILE_CHOOSER(gtk_file_chooser_dialog_new(_("Save curve"),
GTK_WINDOW(gtk_widget_get_toplevel(widget)),
GTK_FILE_CHOOSER_ACTION_SAVE,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT, NULL));
ufraw_focus(fileChooser, TRUE);
gtk_file_chooser_set_show_hidden(fileChooser, FALSE);
GtkWidget *button = gtk_check_button_new_with_label(_("Show hidden files"));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), FALSE);
g_signal_connect(G_OBJECT(button), "toggled",
G_CALLBACK(ufraw_chooser_toggle), fileChooser);
gtk_file_chooser_set_extra_widget(fileChooser, button);
filter = GTK_FILE_FILTER(gtk_file_filter_new());
gtk_file_filter_set_name(filter, _("All curve formats"));
gtk_file_filter_add_pattern(filter, "*.ntc");
gtk_file_filter_add_pattern(filter, "*.NTC");
gtk_file_filter_add_pattern(filter, "*.ncv");
gtk_file_filter_add_pattern(filter, "*.NCV");
gtk_file_filter_add_pattern(filter, "*.curve");
gtk_file_filter_add_pattern(filter, "*.CURVE");
gtk_file_chooser_add_filter(fileChooser, filter);
filter = GTK_FILE_FILTER(gtk_file_filter_new());
gtk_file_filter_set_name(filter, _("UFRaw curve format"));
gtk_file_filter_add_pattern(filter, "*.curve");
gtk_file_filter_add_pattern(filter, "*.CURVE");
gtk_file_chooser_add_filter(fileChooser, filter);
filter = GTK_FILE_FILTER(gtk_file_filter_new());
gtk_file_filter_set_name(filter, _("Nikon curve format"));
gtk_file_filter_add_pattern(filter, "*.ntc");
gtk_file_filter_add_pattern(filter, "*.NTC");
gtk_file_filter_add_pattern(filter, "*.ncv");
gtk_file_filter_add_pattern(filter, "*.NCV");
gtk_file_chooser_add_filter(fileChooser, filter);
filter = GTK_FILE_FILTER(gtk_file_filter_new());
gtk_file_filter_set_name(filter, _("All files"));
gtk_file_filter_add_pattern(filter, "*");
gtk_file_chooser_add_filter(fileChooser, filter);
if (strlen(CFG->curvePath) > 0)
gtk_file_chooser_set_current_folder(fileChooser, CFG->curvePath);
if (curveType == base_curve)
g_snprintf(defFilename, max_name, "%s.curve",
CFG->BaseCurve[CFG->BaseCurveIndex].name);
else
g_snprintf(defFilename, max_name, "%s.curve",
CFG->curve[CFG->curveIndex].name);
gtk_file_chooser_set_current_name(fileChooser, defFilename);
if (gtk_dialog_run(GTK_DIALOG(fileChooser)) == GTK_RESPONSE_ACCEPT) {
filename = gtk_file_chooser_get_filename(fileChooser);
if (curveType == base_curve)
curve_save(&CFG->BaseCurve[CFG->BaseCurveIndex], filename);
else
curve_save(&CFG->curve[CFG->curveIndex], filename);
cp = g_path_get_dirname(filename);
g_strlcpy(CFG->curvePath, cp, max_path);
g_free(cp);
g_free(filename);
}
ufraw_focus(fileChooser, FALSE);
gtk_widget_destroy(GTK_WIDGET(fileChooser));
}
static void load_profile(GtkWidget *widget, long type)
{
preview_data *data = get_preview_data(widget);
GtkFileChooser *fileChooser;
GSList *list, *saveList;
GtkFileFilter *filter;
char *filename, *cp;
profile_data p;
if (data->FreezeDialog) return;
if (CFG->profileCount[type] == max_profiles) {
ufraw_message(UFRAW_ERROR, _("No more room for new profiles."));
return;
}
fileChooser = GTK_FILE_CHOOSER(gtk_file_chooser_dialog_new(
_("Load color profile"),
GTK_WINDOW(gtk_widget_get_toplevel(widget)),
GTK_FILE_CHOOSER_ACTION_OPEN,
GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT, NULL));
ufraw_focus(fileChooser, TRUE);
gtk_file_chooser_set_select_multiple(fileChooser, TRUE);
gtk_file_chooser_set_show_hidden(fileChooser, FALSE);
GtkWidget *button = gtk_check_button_new_with_label(_("Show hidden files"));
gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(button), FALSE);
g_signal_connect(G_OBJECT(button), "toggled",
G_CALLBACK(ufraw_chooser_toggle), fileChooser);
gtk_file_chooser_set_extra_widget(fileChooser, button);
if (strlen(CFG->profilePath) > 0)
gtk_file_chooser_set_current_folder(fileChooser, CFG->profilePath);
filter = GTK_FILE_FILTER(gtk_file_filter_new());
gtk_file_filter_set_name(filter, _("Color Profiles"));
gtk_file_filter_add_pattern(filter, "*.icm");
gtk_file_filter_add_pattern(filter, "*.ICM");
gtk_file_filter_add_pattern(filter, "*.icc");
gtk_file_filter_add_pattern(filter, "*.ICC");
gtk_file_chooser_add_filter(fileChooser, filter);
filter = GTK_FILE_FILTER(gtk_file_filter_new());
gtk_file_filter_set_name(filter, _("All files"));
gtk_file_filter_add_pattern(filter, "*");
gtk_file_chooser_add_filter(fileChooser, filter);
if (gtk_dialog_run(GTK_DIALOG(fileChooser)) == GTK_RESPONSE_ACCEPT) {
for (list = saveList = gtk_file_chooser_get_filenames(fileChooser);
list != NULL && CFG->profileCount[type] < max_profiles;
list = g_slist_next(list)) {
filename = list->data;
p = conf_default.profile[type][conf_default.profileCount[type]];
g_strlcpy(p.file, filename, max_path);
/* Make sure we update product name */
Developer->updateTransform = TRUE;
developer_profile(Developer, type, &p);
if (Developer->profile[type] == NULL) {
g_free(list->data);
continue;
}
char *base = g_path_get_basename(filename);
char *name = uf_file_set_type(base, "");
char *utf8 = g_filename_display_name(name);
g_strlcpy(p.name, utf8, max_name);
g_free(utf8);
g_free(name);
g_free(base);
/* Set some defaults to the profile's curve */
p.gamma = profile_default_gamma(&p);
CFG->profile[type][CFG->profileCount[type]++] = p;
gtk_combo_box_append_text(data->ProfileCombo[type], p.name);
CFG->profileIndex[type] = CFG->profileCount[type] - 1;
cp = g_path_get_dirname(list->data);
g_strlcpy(CFG->profilePath, cp, max_path);
/* Add profile to .ufrawrc but don't make it default */
RC->profile[type][RC->profileCount[type]++] = p;
g_strlcpy(RC->profilePath, cp, max_path);
conf_save(RC, NULL, NULL);
g_free(cp);
g_free(list->data);
}
gtk_combo_box_set_active(data->ProfileCombo[type],
CFG->profileIndex[type]);
if (list != NULL)
ufraw_message(UFRAW_ERROR, _("No more room for new profiles."));
g_slist_free(saveList);
}
ufraw_focus(fileChooser, FALSE);
gtk_widget_destroy(GTK_WIDGET(fileChooser));
}
static colorLabels *color_labels_new(GtkTable *table, int x, int y,
char *label, int format, int zonep)
{
colorLabels *l;
GtkWidget *lbl;
int c, i = 0, numlabels;
l = g_new(colorLabels, 1);
l->format = format;
l->zonep = zonep;
numlabels = (zonep == with_zone ? 5 : 3);
if (label != NULL) {
lbl = gtk_label_new(label);
gtk_misc_set_alignment(GTK_MISC(lbl), 1, 0.5);
gtk_table_attach(table, lbl, x, x + 1, y, y + 1,
GTK_SHRINK | GTK_FILL, GTK_FILL, 0, 0);
i++;
}
/* 0-2 for RGB, 3 for value, 4 for zone */
for (c = 0; c < numlabels; c++, i++) {
l->labels[c] = GTK_LABEL(gtk_label_new(NULL));
GtkWidget *event_box = gtk_event_box_new();
gtk_container_add(GTK_CONTAINER(event_box), GTK_WIDGET(l->labels[c]));
gtk_table_attach(table, event_box, x + i, x + i + 1, y, y + 1,
GTK_EXPAND | GTK_FILL, 0, 0, 0);
switch (c) {
case 0:
case 1:
case 2:
gtk_label_set_width_chars(l->labels[c], 3);
break;
case 3:
gtk_label_set_width_chars(l->labels[c], 5);
gtk_widget_set_tooltip_text(event_box, _("Luminosity (Y value)"));
break;
case 4:
gtk_label_set_width_chars(l->labels[c], 4);
gtk_widget_set_tooltip_text(event_box, _("Adams' zone"));
}
}
return l;
}
/* Return numeric zone representation from 0-1 luminance value.
* Unlike Adams, we use arabic for now. */
static double value2zone(double v)
{
const double zoneV = 0.18;
double z_rel_V;
double zone;
/* Convert to value relative to zone V value. */
z_rel_V = v / zoneV;
/* Convert to log-based value, with zone V 0. */
zone = log(z_rel_V) / log(2.0);
/* Move zone 5 to the proper place. */
zone += 5.0;
return zone;
}
static void color_labels_set(colorLabels *l, double data[])
{
int c;
char buf1[max_name], buf2[max_name];
/* Value black, zone purple, for no good reason. */
const char *colorName[] = {"red", "green", "blue", "black", "purple"};
for (c = 0; c < 3; c++) {
switch (l->format) {
case pixel_format:
g_snprintf(buf1, max_name, "%3.f", data[c]);
break;
case percent_format:
if (data[c] < 10.0)
g_snprintf(buf1, max_name, "%2.1f%%", data[c]);
else
g_snprintf(buf1, max_name, "%2.0f%%", data[c]);
break;
default:
g_snprintf(buf1, max_name, "ERR");
}
g_snprintf(buf2, max_name, "<span foreground='%s'>%s</span>",
colorName[c], buf1);
gtk_label_set_markup(l->labels[c], buf2);
}
/* If value/zone not requested, just return now. */
if (l->zonep == without_zone)
return;
/* Value */
c = 3;
g_snprintf(buf2, max_name, "<span foreground='%s'>%0.3f</span>",
colorName[c], data[3]);
gtk_label_set_markup(l->labels[c], buf2);
/* Zone */
c = 4;
/* 2 decimal places may be excessive */
g_snprintf(buf2, max_name, "<span foreground='%s'>%0.2f</span>",
colorName[c], data[4]);
gtk_label_set_markup(l->labels[c], buf2);
}
static int zoom_to_scale(double zoom)
{
/* Try to setup for shrink instead of resize because of speed.
* We only round down to guarantee that scale-to-fit actually fits.
*/
int percent = zoom;
int mul = 100 / percent;
if (percent * mul <= 100 && (percent + 1) * mul > 100)
return mul;
else
return 0;
}
/* Modify the preview image to mark crop and spot areas.
* Note that all coordinate intervals are semi-inclusive, e.g.
* X1 <= pixels < X2 and Y1 <= pixels < Y2
* This approach makes computing width/height just a matter of
* substracting X1 from X2 or Y1 from Y2.
*/
static void preview_draw_area(preview_data *data,
int x, int y, int width, int height)
{
int pixbufHeight = gdk_pixbuf_get_height(data->PreviewPixbuf);
if (y < 0 || y >= pixbufHeight)
g_error("preview_draw_area(): y:%d out of range 0 <= y < %d",
y, pixbufHeight);
if (y + height > pixbufHeight)
g_error("preview_draw_area(): y+height:%d out of range y+height <= %d",
y + height, pixbufHeight);
if (height == 0) return; // Nothing to do
int pixbufWidth = gdk_pixbuf_get_width(data->PreviewPixbuf);
if (x < 0 || x >= pixbufWidth)
g_error("preview_draw_area(): x:%d out of range 0 <= x < %d",
x, pixbufWidth);
if (x + width > pixbufWidth)
g_error("preview_draw_area(): x+width:%d out of range x+width <= %d",
x + width, pixbufWidth);
if (width == 0) return; // Nothing to do
gboolean blinkOver = CFG->overExp &&
(!CFG->blinkOverUnder || (data->OverUnderTicker & 3) == 1);
gboolean blinkUnder = CFG->underExp &&
(!CFG->blinkOverUnder || (data->OverUnderTicker & 3) == 3);
UFRectangle Crop;
ufraw_get_scaled_crop(data->UF, &Crop);
int CropX2 = Crop.x + Crop.width;
int CropY2 = Crop.y + Crop.height;
int drawLines = CFG->drawLines + 1;
/* Scale spot image coordinates to pixbuf coordinates */
float scale_x = ((float)pixbufWidth) / data->UF->rotatedWidth;
float scale_y = ((float)pixbufHeight) / data->UF->rotatedHeight;
int SpotY1 = floor(MIN(data->SpotY1, data->SpotY2) * scale_y);
int SpotY2 = ceil(MAX(data->SpotY1, data->SpotY2) * scale_y);
int SpotX1 = floor(MIN(data->SpotX1, data->SpotX2) * scale_x);
int SpotX2 = ceil(MAX(data->SpotX1, data->SpotX2) * scale_x);
int rowstride = gdk_pixbuf_get_rowstride(data->PreviewPixbuf);
guint8 *pixies = gdk_pixbuf_get_pixels(data->PreviewPixbuf) + x * 3;
/* This is bad. `img' should have been a parameter because we
* cannot request an up to date buffer but it must be up to
* date to some extend. In theory we could get the wrong buffer */
ufraw_image_data *displayImage = ufraw_get_image(data->UF, ufraw_display_phase, FALSE);
guint8 *displayPixies = displayImage->buffer + x * displayImage->depth;
ufraw_image_data *workingImage = ufraw_get_image(data->UF,
ufraw_develop_phase, FALSE);
guint8 *workingPixies = workingImage->buffer + x * workingImage->depth;
int xx, yy, c;
for (yy = y; yy < y + height; yy++) {
guint8 *p8 = pixies + yy * rowstride;
memcpy(p8, displayPixies + yy * displayImage->rowstride,
width * displayImage->depth);
if (data->ChannelSelect >= 0) {
guint8 *p = p8;
for (xx = 0; xx < width; xx++, p += 3) {
guint8 px = p[data->ChannelSelect];
p[0] = p[1] = p[2] = px;
}
}
guint8 *p8working = workingPixies + yy * workingImage->rowstride;
for (xx = x; xx < x + width; xx++, p8 += 3, p8working += workingImage->depth) {
if (data->SpotDraw &&
(((yy == SpotY1 - 1 || yy == SpotY2) && xx >= SpotX1 - 1 && xx <= SpotX2)
||
((xx == SpotX1 - 1 || xx == SpotX2) && yy >= SpotY1 - 1 && yy <= SpotY2)
)
) {
if (((xx + yy) & 7) >= 4)
p8[0] = p8[1] = p8[2] = 0;
else
p8[0] = p8[1] = p8[2] = 255;
continue;
}
/* Draw white frame around crop area */
if (((yy == Crop.y - 1 || yy == CropY2) && xx >= Crop.x - 1 && xx <= CropX2) ||
((xx == Crop.x - 1 || xx == CropX2) && yy >= Crop.y - 1 && yy <= CropY2)) {
p8[0] = p8[1] = p8[2] = 255;
continue;
}
/* Shade the cropped out area */
else if (yy < Crop.y || yy >= CropY2 || xx < Crop.x || xx >= CropX2) {
for (c = 0; c < 3; c++) p8[c] = p8[c] / 4;
continue;
}
if (data->RenderMode == render_default) {
/* Shade out the alignment lines */
if (CFG->drawLines &&
yy > Crop.y + 1 && yy < CropY2 - 2 &&
xx > Crop.x + 1 && xx < CropX2 - 2) {
int dx = (xx - Crop.x) * drawLines % Crop.width / drawLines;
int dy = (yy - Crop.y) * drawLines % Crop.height / drawLines;
if (dx == 0 || dy == 0) {
p8[0] /= 2;
p8[1] /= 2;
p8[2] /= 2;
} else if (dx == 1 || dy == 1) {
p8[0] = 255 - (255 - p8[0]) / 2;
p8[1] = 255 - (255 - p8[1]) / 2;
p8[2] = 255 - (255 - p8[2]) / 2;
}
}
/* Blink the overexposed/underexposed spots */
if (blinkOver && (p8working[0] == 255 || p8working[1] == 255 || p8working[2] == 255))
p8[0] = p8[1] = p8[2] = 0;
else if (blinkUnder && (p8working[0] == 0 || p8working[1] == 0 || p8working[2] == 0))
p8[0] = p8[1] = p8[2] = 255;
} else if (data->RenderMode == render_overexposed) {
for (c = 0; c < 3; c++) if (p8working[c] != 255) p8[c] = 0;
} else if (data->RenderMode == render_underexposed) {
for (c = 0; c < 3; c++) if (p8working[c] != 0) p8[c] = 255;
}
}
}
/* Redraw the changed areas */
#ifdef _OPENMP
#pragma omp critical
{
#endif
GtkWidget *widget = data->PreviewWidget;
GtkImageView *view = GTK_IMAGE_VIEW(widget);
GdkRectangle rect;
rect.x = x;
rect.y = y;
rect.width = width;
rect.height = height;
gtk_image_view_damage_pixels(view, &rect);
#ifdef _OPENMP
}
#endif
}
static gboolean preview_draw_crop(preview_data *data)
{
UFRectangle Crop;
ufraw_get_scaled_crop(data->UF, &Crop);
preview_draw_area(data, Crop.x, Crop.y, Crop.width, Crop.height);
return FALSE;
}
static gboolean is_rendering(preview_data *data)
{
return data->RenderSubArea >= 0;
}
static gboolean switch_highlights(gpointer ptr)
{
preview_data *data = ptr;
/* Only redraw the highlights in the default rendering mode. */
if (data->RenderMode != render_default || data->FreezeDialog)
return TRUE;
if (!is_rendering(data)) {
/* Set the area to redraw based on the crop rectangle and view port. */
UFRectangle Crop;
ufraw_get_scaled_crop(data->UF, &Crop);
GdkRectangle viewRect;
gtk_image_view_get_viewport(GTK_IMAGE_VIEW(data->PreviewWidget),
&viewRect);
gdouble zoom = gtk_image_view_get_zoom(GTK_IMAGE_VIEW(data->PreviewWidget));
int x1 = MAX(Crop.x, viewRect.x / zoom);
int width = MIN(Crop.width, viewRect.width);
int y1 = MAX(Crop.y, viewRect.y / zoom);
int height = MIN(Crop.height, viewRect.height);
data->OverUnderTicker++;
preview_draw_area(data, x1, y1, width, height);
}
/* If no blinking is needed, disable this timeout function. */
if (!CFG->blinkOverUnder || (!CFG->overExp && !CFG->underExp)) {
data->BlinkTimer = 0;
return FALSE;
}
return TRUE;
}
static void start_blink(preview_data *data)
{
if (CFG->blinkOverUnder && (CFG->overExp || CFG->underExp)) {
if (!data->BlinkTimer) {
data->BlinkTimer = gdk_threads_add_timeout(500,
switch_highlights, data);
}
}
}
static void stop_blink(preview_data *data)
{
if (data->BlinkTimer) {
data->OverUnderTicker = 0;
switch_highlights(data);
g_source_remove(data->BlinkTimer);
data->BlinkTimer = 0;
}
}
static void window_map_event(GtkWidget *widget, GdkEvent *event,
gpointer user_data)
{
preview_data *data = get_preview_data(widget);
start_blink(data);
(void)event;
(void)user_data;
}
static void window_unmap_event(GtkWidget *widget, GdkEvent *event,
gpointer user_data)
{
preview_data *data = get_preview_data(widget);
stop_blink(data);
(void)event;
(void)user_data;
}
static void render_status_text(preview_data *data)
{
if (data->ProgressBar == NULL)
return;
char progressText[max_name];
int scale = zoom_to_scale(CFG->Zoom);
if (scale == 0)
g_snprintf(progressText, max_name, _("size %dx%d, zoom %2.f%%"),
data->UF->rotatedWidth, data->UF->rotatedHeight, CFG->Zoom);
else
g_snprintf(progressText, max_name, _("size %dx%d, scale 1/%d"),
data->UF->rotatedWidth, data->UF->rotatedHeight, scale);
gtk_progress_bar_set_text(data->ProgressBar, progressText);
gtk_progress_bar_set_fraction(data->ProgressBar, 0);
}
static gboolean render_raw_histogram(preview_data *data);
static gboolean render_preview_image(preview_data *data);
static gboolean render_live_histogram(preview_data *data);
static gboolean render_spot(preview_data *data);
static void draw_spot(preview_data *data, gboolean draw);
static void render_special_mode(GtkWidget *widget, long mode)
{
preview_data *data = get_preview_data(widget);
data->RenderMode = mode;
preview_draw_crop(data);
}
static void render_init(preview_data *data)
{
/* Check if we need a new pixbuf */
int width = gdk_pixbuf_get_width(data->PreviewPixbuf);
int height = gdk_pixbuf_get_height(data->PreviewPixbuf);
ufraw_image_data *image = ufraw_get_image(data->UF,
ufraw_display_phase, FALSE);
if (width != image->width || height != image->height) {
/* Calculate current viewport center */
GdkRectangle vp;
gtk_image_view_get_viewport(GTK_IMAGE_VIEW(data->PreviewWidget), &vp);
double xc = (vp.x + vp.width / 2.0) / width * image->width;
double yc = (vp.y + vp.height / 2.0) / height * image->height;
data->PreviewPixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8,
image->width, image->height);
/* Clear the pixbuffer to avoid displaying garbage */
gdk_pixbuf_fill(data->PreviewPixbuf, 0);
gtk_image_view_set_pixbuf(GTK_IMAGE_VIEW(data->PreviewWidget),
data->PreviewPixbuf, FALSE);
g_object_unref(data->PreviewPixbuf);
/* restore the viewport center */
gtk_image_view_set_offset(GTK_IMAGE_VIEW(data->PreviewWidget),
xc - vp.width / 2, yc - vp.height / 2, FALSE);
}
}
static GtkProgressBar *ProgressBar;
static GTimer *ProgressTimer;
static void preview_progress(int what, int ticks)
{
static int last_what, todo, done;
gboolean update = FALSE;
#ifdef _OPENMP
#pragma omp master
#endif
update = TRUE;
#ifdef _OPENMP
#pragma omp critical(preview_progress)
{
#endif
if (ticks < 0) {
todo = -ticks;
done = 0;
last_what = what;
} else {
if (last_what == what)
done += ticks;
else
update = FALSE;
}
#ifdef _OPENMP
}
#endif
if (!update)
return; // wrong thread for GTK or "what" mismatch
if (g_timer_elapsed(ProgressTimer, NULL) < 0.07 && ticks >= 0)
return; // avoid progress bar rendering hog
g_timer_start(ProgressTimer);
gboolean events = TRUE;
double start = 0.0, stop = 1.0, fraction = 0.0;
char *text = NULL;
switch (what) {
case PROGRESS_WAVELET_DENOISE:
text = _("Wavelet denoising");
break;
case PROGRESS_DESPECKLE:
text = _("Despeckling");
break;
case PROGRESS_INTERPOLATE:
text = _("Interpolating");
break;
case PROGRESS_RENDER:
text = _("Rendering");
stop = 0.0;
events = FALSE; // not needed in render_preview_image()
break;
case PROGRESS_LOAD:
text = _("Loading preview");
break;
case PROGRESS_SAVE:
text = _("Saving image");
}
if (ticks < 0 && text)
gtk_progress_bar_set_text(ProgressBar, text);
if (!events)
return;
fraction = todo ? start + (stop - start) * done / todo : 0;
if (fraction > stop)
fraction = stop;
gtk_progress_bar_set_fraction(ProgressBar, fraction);
while (gtk_events_pending())
gtk_main_iteration();
}
static void preview_progress_enable(preview_data *data)
{
ProgressBar = data->ProgressBar;
ProgressTimer = g_timer_new();
ufraw_progress = preview_progress;
}
static void preview_progress_disable(preview_data *data)
{
ufraw_progress = NULL;
if (ProgressTimer != NULL)
g_timer_destroy(ProgressTimer);
render_status_text(data);
}
void resize_canvas(preview_data *data);
static gboolean render_preview_now(preview_data *data)
{
if (data->FreezeDialog)
return FALSE;
while (g_idle_remove_by_data(data))
;
data->RenderSubArea = 0;
if (data->UF->Images[ufraw_transform_phase].invalidate_event)
resize_canvas(data);
if (CFG->autoExposure == apply_state) {
ufraw_invalidate_layer(data->UF, ufraw_develop_phase);
ufraw_auto_expose(data->UF);
gdouble lower, upper;
g_object_get(data->ExposureAdjustment, "lower", &lower,
"upper", &upper, NULL);
/* Clip the exposure to prevent a "changed" signal */
if (CFG->exposure > upper) CFG->exposure = upper;
if (CFG->exposure < lower) CFG->exposure = lower;
gtk_adjustment_set_value(data->ExposureAdjustment, CFG->exposure);
gtk_widget_set_sensitive(data->ResetExposureButton,
fabs(conf_default.exposure - CFG->exposure) > 0.001);
if (CFG->autoBlack == enabled_state)
CFG->autoBlack = apply_state;
}
if (CFG->autoBlack == apply_state) {
ufraw_invalidate_layer(data->UF, ufraw_develop_phase);
ufraw_auto_black(data->UF);
curveeditor_widget_set_curve(data->CurveWidget,
&CFG->curve[CFG->curveIndex]);
gtk_widget_set_sensitive(data->ResetBlackButton,
CFG->curve[CFG->curveIndex].m_anchors[0].x != 0.0 ||
CFG->curve[CFG->curveIndex].m_anchors[0].y != 0.0);
}
char text[max_name];
g_snprintf(text, max_name, _("Black point: %0.3lf"),
CFG->curve[CFG->curveIndex].m_anchors[0].x);
gtk_label_set_text(GTK_LABEL(data->BlackLabel), text);
if (CFG->profileIndex[display_profile] == 0) {
uf_get_display_profile(data->PreviewWidget, &data->UF->displayProfile,
&data->UF->displayProfileSize);
}
if (CFG->autoCrop == apply_state) {
ufraw_invalidate_layer(data->UF, ufraw_transform_phase);
ufraw_get_image_dimensions(data->UF);
CFG->CropX1 = (data->UF->rotatedWidth - data->UF->autoCropWidth) / 2;
CFG->CropX2 = CFG->CropX1 + data->UF->autoCropWidth;
CFG->CropY1 = (data->UF->rotatedHeight - data->UF->autoCropHeight) / 2;
CFG->CropY2 = CFG->CropY1 + data->UF->autoCropHeight;