-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
temperature.c
2213 lines (1880 loc) · 73.8 KB
/
temperature.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
This file is part of darktable,
Copyright (C) 2009-2023 darktable developers.
darktable is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
darktable is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with darktable. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <assert.h>
#include <lcms2.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "bauhaus/bauhaus.h"
#include "common/colorspaces_inline_conversions.h"
#include "common/darktable.h"
#include "common/opencl.h"
#include "common/wb_presets.h"
#include "control/control.h"
#include "control/conf.h"
#include "develop/develop.h"
#include "develop/imageop_gui.h"
#include "develop/imageop_math.h"
#include "develop/tiling.h"
#include "dtgtk/expander.h"
#include "gui/accelerators.h"
#include "gui/gtk.h"
#include "gui/color_picker_proxy.h"
#include "iop/iop_api.h"
// for Kelvin temperature and bogus WB
#include "common/colorspaces.h"
#include "external/cie_colorimetric_tables.c"
DT_MODULE_INTROSPECTION(3, dt_iop_temperature_params_t)
#define INITIALBLACKBODYTEMPERATURE 4000
#define DT_IOP_LOWEST_TEMPERATURE 1901
#define DT_IOP_HIGHEST_TEMPERATURE 25000
#define DT_IOP_LOWEST_TINT 0.135
#define DT_IOP_HIGHEST_TINT 2.326
#define DT_COEFF_EPS 0.00001f
#define DT_IOP_NUM_OF_STD_TEMP_PRESETS 4
// If you reorder presets combo, change this consts
#define DT_IOP_TEMP_AS_SHOT 0
#define DT_IOP_TEMP_SPOT 1
#define DT_IOP_TEMP_USER 2
#define DT_IOP_TEMP_D65 3
static void gui_sliders_update(struct dt_iop_module_t *self);
typedef struct dt_iop_temperature_params_t
{
float red; // $MIN: 0.0 $MAX: 8.0
float green; // $MIN: 0.0 $MAX: 8.0
float blue; // $MIN: 0.0 $MAX: 8.0
float g2; // $MIN: 0.0 $MAX: 8.0 $DESCRIPTION: "emerald"
} dt_iop_temperature_params_t;
typedef struct dt_iop_temperature_gui_data_t
{
GtkWidget *scale_k, *scale_tint, *scale_r, *scale_g, *scale_b, *scale_g2;
GtkWidget *presets;
GtkWidget *finetune;
GtkWidget *buttonbar;
GtkWidget *colorpicker;
GtkWidget *btn_asshot; //As Shot
GtkWidget *btn_user;
GtkWidget *btn_d65;
GtkWidget *temp_label;
GtkWidget *balance_label;
int preset_cnt;
int preset_num[54];
double daylight_wb[4];
double as_shot_wb[4];
double mod_coeff[4];
float mod_temp, mod_tint;
double XYZ_to_CAM[4][3], CAM_to_XYZ[3][4];
int colored_sliders;
int blackbody_is_confusing;
gboolean button_bar_visible;
dt_gui_collapsible_section_t cs;
} dt_iop_temperature_gui_data_t;
typedef struct dt_iop_temperature_data_t
{
float coeffs[4];
} dt_iop_temperature_data_t;
typedef struct dt_iop_temperature_global_data_t
{
int kernel_whitebalance_4f;
int kernel_whitebalance_1f;
int kernel_whitebalance_1f_xtrans;
} dt_iop_temperature_global_data_t;
typedef struct dt_iop_temperature_preset_data_t
{
int no_ft_pos;
int min_ft_pos;
int max_ft_pos;
} dt_iop_temperature_preset_data_t;
int legacy_params(dt_iop_module_t *self,
const void *const old_params,
const int old_version,
void **new_params,
int32_t *new_params_size,
int *new_version)
{
typedef struct dt_iop_temperature_params_v3_t
{
float red;
float green;
float blue;
float g2;
} dt_iop_temperature_params_v3_t;
if(old_version == 2)
{
typedef struct dt_iop_temperature_params_v2_t
{
float temp_out;
float coeffs[3];
} dt_iop_temperature_params_v2_t;
const dt_iop_temperature_params_v2_t *o = (dt_iop_temperature_params_v2_t *)old_params;
dt_iop_temperature_params_v3_t *n =
(dt_iop_temperature_params_v3_t *)malloc(sizeof(dt_iop_temperature_params_v3_t));
n->red = o->coeffs[0];
n->green = o->coeffs[1];
n->blue = o->coeffs[2];
n->g2 = NAN;
*new_params = n;
*new_params_size = sizeof(dt_iop_temperature_params_v3_t);
*new_version = 3;
return 0;
}
return 1;
}
static inline void _temp_params_from_array(dt_iop_temperature_params_t *p,
const double a[4])
{
p->red = a[0];
p->green = a[1];
p->blue = a[2];
p->g2 = a[3];
}
static inline void _temp_array_from_params(double a[4],
const dt_iop_temperature_params_t *p)
{
a[0] = p->red;
a[1] = p->green;
a[2] = p->blue;
a[3] = p->g2;
}
static gboolean _ignore_missing_wb(dt_image_t *img)
{
// Ignore files that end with "-hdr.dng" since these are broken files we
// generated without any proper WB tagged
if(g_str_has_suffix(img->filename,"-hdr.dng"))
return TRUE;
static const char *const ignored_cameras[] = {
"Canon PowerShot A610",
"Canon PowerShot S3 IS",
"Canon PowerShot A620",
"Canon PowerShot A720 IS",
"Canon PowerShot A630",
"Canon PowerShot A640",
"Canon PowerShot A650",
"Canon PowerShot SX110 IS",
"Mamiya ZD",
"Canon EOS D2000C",
"Kodak EOS DCS 1",
"Kodak DCS560C",
"Kodak DCS460D",
"Nikon E5700",
"Sony DSC-F828",
"GITUP GIT2",
};
for(int i=0; i < sizeof(ignored_cameras)/sizeof(ignored_cameras[1]); i++)
if(!strcmp(img->camera_makermodel, ignored_cameras[i]))
return TRUE;
return FALSE;
}
const char *name()
{
return C_("modulename", "white balance");
}
const char **description(struct dt_iop_module_t *self)
{
return dt_iop_set_description
(self,
_("scale raw RGB channels to balance white and help demosaicing"),
_("corrective"),
_("linear, raw, scene-referred"),
_("linear, raw"),
_("linear, raw, scene-referred"));
}
int default_group()
{
return IOP_GROUP_BASIC | IOP_GROUP_GRADING;
}
int flags()
{
return IOP_FLAGS_ALLOW_TILING | IOP_FLAGS_ONE_INSTANCE | IOP_FLAGS_UNSAFE_COPY;
}
dt_iop_colorspace_type_t default_colorspace(dt_iop_module_t *self,
dt_dev_pixelpipe_t *pipe,
dt_dev_pixelpipe_iop_t *piece)
{
// This module may work in RAW or RGB (e.g. for TIFF files)
// depending on the input The module does not change the color space
// between the input and output, therefore implement it here
if(piece && piece->dsc_in.cst != IOP_CS_RAW)
return IOP_CS_RGB;
return IOP_CS_RAW;
}
/*
* Spectral power distribution functions
* https://en.wikipedia.org/wiki/Spectral_power_distribution
*/
typedef double((*spd)(unsigned long int wavelength, double TempK));
/*
* Bruce Lindbloom, "Spectral Power Distribution of a Blackbody Radiator"
* http://www.brucelindbloom.com/Eqn_Blackbody.html
*/
static double spd_blackbody(unsigned long int wavelength, double TempK)
{
// convert wavelength from nm to m
const long double lambda = (double)wavelength * 1e-9;
/*
* these 2 constants were computed using following Sage code:
*
* (from http://physics.nist.gov/cgi-bin/cuu/Value?h)
* h = 6.62606957 * 10^-34 # Planck
* c= 299792458 # speed of light in vacuum
* k = 1.3806488 * 10^-23 # Boltzmann
*
* c_1 = 2 * pi * h * c^2
* c_2 = h * c / k
*
* print 'c_1 = ', c_1, ' ~= ', RealField(128)(c_1)
* print 'c_2 = ', c_2, ' ~= ', RealField(128)(c_2)
*/
#define c1 3.7417715246641281639549488324352159753e-16L
#define c2 0.014387769599838156481252937624049081933L
return (double)(c1 / (powl(lambda, 5) * (expl(c2 / (lambda * TempK)) - 1.0L)));
#undef c2
#undef c1
}
/*
* Bruce Lindbloom, "Spectral Power Distribution of a CIE D-Illuminant"
* http://www.brucelindbloom.com/Eqn_DIlluminant.html
* and https://en.wikipedia.org/wiki/Standard_illuminant#Illuminant_series_D
*/
static double spd_daylight(unsigned long int wavelength, double TempK)
{
cmsCIExyY WhitePoint = { 0.3127, 0.3290, 1.0 };
/*
* Bruce Lindbloom, "TempK to xy"
* http://www.brucelindbloom.com/Eqn_T_to_xy.html
*/
cmsWhitePointFromTemp(&WhitePoint, TempK);
const double M = (0.0241 + 0.2562 * WhitePoint.x - 0.7341 * WhitePoint.y),
m1 = (-1.3515 - 1.7703 * WhitePoint.x + 5.9114 * WhitePoint.y) / M,
m2 = (0.0300 - 31.4424 * WhitePoint.x + 30.0717 * WhitePoint.y) / M;
const unsigned long int j
= ((wavelength - cie_daylight_components[0].wavelength)
/ (cie_daylight_components[1].wavelength
- cie_daylight_components[0].wavelength));
return (cie_daylight_components[j].S[0] + m1 * cie_daylight_components[j].S[1]
+ m2 * cie_daylight_components[j].S[2]);
}
/*
* Bruce Lindbloom, "Computing XYZ From Spectral Data (Emissive Case)"
* http://www.brucelindbloom.com/Eqn_Spect_to_XYZ.html
*/
static cmsCIEXYZ spectrum_to_XYZ(double TempK, spd I)
{
cmsCIEXYZ Source = {.X = 0.0, .Y = 0.0, .Z = 0.0 };
/*
* Color matching functions
* https://en.wikipedia.org/wiki/CIE_1931_color_space#Color_matching_functions
*/
for(size_t i = 0; i < cie_1931_std_colorimetric_observer_count; i++)
{
const unsigned long int lambda =
cie_1931_std_colorimetric_observer[0].wavelength
+ (cie_1931_std_colorimetric_observer[1].wavelength
- cie_1931_std_colorimetric_observer[0].wavelength) * i;
const double P = I(lambda, TempK);
Source.X += P * cie_1931_std_colorimetric_observer[i].xyz.X;
Source.Y += P * cie_1931_std_colorimetric_observer[i].xyz.Y;
Source.Z += P * cie_1931_std_colorimetric_observer[i].xyz.Z;
}
// normalize so that each component is in [0.0, 1.0] range
const double _max = fmax(fmax(Source.X, Source.Y), Source.Z);
Source.X /= _max;
Source.Y /= _max;
Source.Z /= _max;
return Source;
}
// TODO: temperature and tint cannot be disjoined! (here it assumes no tint)
static cmsCIEXYZ temperature_to_XYZ(double TempK)
{
if(TempK < DT_IOP_LOWEST_TEMPERATURE) TempK = DT_IOP_LOWEST_TEMPERATURE;
if(TempK > DT_IOP_HIGHEST_TEMPERATURE) TempK = DT_IOP_HIGHEST_TEMPERATURE;
if(TempK < INITIALBLACKBODYTEMPERATURE)
{
// if temperature is less than 4000K we use blackbody,
// because there will be no Daylight reference below 4000K...
return spectrum_to_XYZ(TempK, spd_blackbody);
}
else
{
return spectrum_to_XYZ(TempK, spd_daylight);
}
}
static cmsCIEXYZ temperature_tint_to_XYZ(double TempK, double tint)
{
cmsCIEXYZ xyz = temperature_to_XYZ(TempK);
xyz.Y /= tint; // TODO: This is baaad!
return xyz;
}
// binary search inversion
static void XYZ_to_temperature(cmsCIEXYZ XYZ, float *TempK, float *tint)
{
double maxtemp = DT_IOP_HIGHEST_TEMPERATURE, mintemp = DT_IOP_LOWEST_TEMPERATURE;
cmsCIEXYZ _xyz;
for(*TempK = (maxtemp + mintemp) / 2.0;
(maxtemp - mintemp) > 1.0;
*TempK = (maxtemp + mintemp) / 2.0)
{
_xyz = temperature_to_XYZ(*TempK);
if(_xyz.Z / _xyz.X > XYZ.Z / XYZ.X)
maxtemp = *TempK;
else
mintemp = *TempK;
}
// TODO: Fix this to move orthogonally to planckian locus
*tint = (_xyz.Y / _xyz.X) / (XYZ.Y / XYZ.X);
if(*TempK < DT_IOP_LOWEST_TEMPERATURE) *TempK = DT_IOP_LOWEST_TEMPERATURE;
if(*TempK > DT_IOP_HIGHEST_TEMPERATURE) *TempK = DT_IOP_HIGHEST_TEMPERATURE;
if(*tint < DT_IOP_LOWEST_TINT) *tint = DT_IOP_LOWEST_TINT;
if(*tint > DT_IOP_HIGHEST_TINT) *tint = DT_IOP_HIGHEST_TINT;
}
static void xyz2mul(dt_iop_module_t *self,
cmsCIEXYZ xyz,
double mul[4])
{
dt_iop_temperature_gui_data_t *g = (dt_iop_temperature_gui_data_t *)self->gui_data;
double XYZ[3] = { xyz.X, xyz.Y, xyz.Z };
double CAM[4];
for(int k = 0; k < 4; k++)
{
CAM[k] = 0.0;
for(int i = 0; i < 3; i++)
{
CAM[k] += g->XYZ_to_CAM[k][i] * XYZ[i];
}
}
for(int k = 0; k < 4; k++) mul[k] = 1.0 / CAM[k];
}
static void temp2mul(dt_iop_module_t *self,
const double TempK,
const double tint,
double mul[4])
{
cmsCIEXYZ xyz = temperature_to_XYZ(TempK);
xyz.Y /= tint; // TODO: This is baaad!
/**
* TODO:
* problem here is that tint as it is is just a nasty hack modyfying Y component
* and therefore changing RGB coefficients in wrong way,
* because modifying only Y in that way doesn’t move XYZ point orthogonally
* to planckian locus. That means it actually changes temperature and thus it lies!
*/
xyz2mul(self, xyz, mul);
}
static cmsCIEXYZ mul2xyz(dt_iop_module_t *self,
const dt_iop_temperature_params_t *p)
{
dt_iop_temperature_gui_data_t *g = (dt_iop_temperature_gui_data_t *)self->gui_data;
double CAM[4];
_temp_array_from_params(CAM, p);
for(int k = 0; k < 4; k++)
CAM[k] = CAM[k] > 0.0f ? 1.0 / CAM[k] : 0.0f;
double XYZ[3];
for(int k = 0; k < 3; k++)
{
XYZ[k] = 0.0;
for(int i = 0; i < 4; i++)
{
XYZ[k] += g->CAM_to_XYZ[k][i] * CAM[i];
}
}
return (cmsCIEXYZ){ XYZ[0], XYZ[1], XYZ[2] };
}
static void mul2temp(dt_iop_module_t *self,
dt_iop_temperature_params_t *p,
float *TempK,
float *tint)
{
XYZ_to_temperature(mul2xyz(self, p), TempK, tint);
}
#ifdef _OPENMP
#pragma omp declare simd aligned(inp,outp)
#endif
static inline void scaled_copy_4wide(float *const outp,
const float *const inp,
const float *const coeffs)
{
// this needs to be in a separate function to make GCC8 vectorize it
// at -O2 as well as -O3
for_four_channels(c, aligned(inp, coeffs, outp))
outp[c] = inp[c] * coeffs[c];
}
void process(struct dt_iop_module_t *self,
dt_dev_pixelpipe_iop_t *piece,
const void *const ivoid,
void *const ovoid,
const dt_iop_roi_t *const roi_in,
const dt_iop_roi_t *const roi_out)
{
const uint32_t filters = piece->pipe->dsc.filters;
const uint8_t(*const xtrans)[6] = (const uint8_t(*const)[6])piece->pipe->dsc.xtrans;
const dt_iop_temperature_data_t *const d = (dt_iop_temperature_data_t *)piece->data;
const float *const in = (const float *const)ivoid;
float *const out = (float *const)ovoid;
const float *const d_coeffs = d->coeffs;
if(filters == 9u)
{ // xtrans float mosaiced
#ifdef _OPENMP
#pragma omp parallel for default(none) \
dt_omp_firstprivate(d_coeffs, in, out, roi_out, xtrans) \
schedule(static)
#endif
for(int j = 0; j < roi_out->height; j++)
{
const float DT_ALIGNED_PIXEL coeffs[3][4] =
{
{ d_coeffs[FCxtrans(j, 0, roi_out, xtrans)],
d_coeffs[FCxtrans(j, 1, roi_out, xtrans)],
d_coeffs[FCxtrans(j, 2, roi_out, xtrans)],
d_coeffs[FCxtrans(j, 3, roi_out, xtrans)] },
{ d_coeffs[FCxtrans(j, 4, roi_out, xtrans)],
d_coeffs[FCxtrans(j, 5, roi_out, xtrans)],
d_coeffs[FCxtrans(j, 6, roi_out, xtrans)],
d_coeffs[FCxtrans(j, 7, roi_out, xtrans)] },
{ d_coeffs[FCxtrans(j, 8, roi_out, xtrans)],
d_coeffs[FCxtrans(j, 9, roi_out, xtrans)],
d_coeffs[FCxtrans(j, 10, roi_out, xtrans)],
d_coeffs[FCxtrans(j, 11, roi_out, xtrans)] },
};
// process sensels four at a time (note that attempting to
//ensure alignment for this main loop actually slowed things
//down marginally)
int i = 0;
for(int coeff = 0; i + 4 < roi_out->width; i += 4, coeff = (coeff+1)%3)
{
const size_t p = (size_t)j * roi_out->width + i;
for_four_channels(c) // in and out are NOT aligned when width is not a multiple of 4
out[p+c] = in[p+c] * coeffs[coeff][c];
}
// process the leftover sensels
for(; i < roi_out->width; i++)
{
const size_t p = (size_t)j * roi_out->width + i;
out[p] = in[p] * d_coeffs[FCxtrans(j, i, roi_out, xtrans)];
}
}
}
else if(filters)
{ // bayer float mosaiced
const int width = roi_out->width;
#ifdef _OPENMP
#pragma omp parallel for default(none) \
dt_omp_firstprivate(d_coeffs, filters, in, out, roi_out, width) \
schedule(static)
#endif
for(int j = 0; j < roi_out->height; j++)
{
int i = 0;
const int alignment = 3 & (4 - ((j*width) & 3));
const int offset_j = j + roi_out->y;
// process the unaligned sensels at the start of the row (when
// width is not a multiple of 4)
for(; i < alignment; i++)
{
const size_t p = (size_t)j * width + i;
out[p] = in[p] * d_coeffs[FC(offset_j, i + roi_out->x, filters)];
}
const dt_aligned_pixel_t coeffs =
{ d_coeffs[FC(offset_j, i + roi_out->x, filters)],
d_coeffs[FC(offset_j, i + roi_out->x + 1,filters)],
d_coeffs[FC(offset_j, i + roi_out->x + 2, filters)],
d_coeffs[FC(offset_j, i + roi_out->x + 3, filters)] };
// process sensels four at a time
for(; i < width - 4; i += 4)
{
const size_t p = (size_t)j * width + i;
scaled_copy_4wide(out + p,in + p, coeffs);
}
// process the leftover sensels
for(; i < width; i++)
{
const size_t p = (size_t)j * width + i;
out[p] = in[p] * d_coeffs[FC(offset_j, i + roi_out->x, filters)];
}
}
}
else
{ // non-mosaiced
const size_t npixels = roi_out->width * (size_t)roi_out->height;
#ifdef _OPENMP
#pragma omp parallel for default(none) \
dt_omp_firstprivate(in, out, npixels) \
dt_omp_sharedconst(d_coeffs) \
schedule(static)
#endif
for(size_t k = 0; k < 4*npixels; k += 4)
{
for_each_channel(c,aligned(in,out))
{
out[k+c] = in[k+c] * d_coeffs[c];
}
}
}
piece->pipe->dsc.temperature.enabled = TRUE;
for(int k = 0; k < 4; k++)
{
piece->pipe->dsc.temperature.coeffs[k] = d->coeffs[k];
piece->pipe->dsc.processed_maximum[k] =
d->coeffs[k] * piece->pipe->dsc.processed_maximum[k];
self->dev->proxy.wb_coeffs[k] = d->coeffs[k];
}
}
#ifdef HAVE_OPENCL
int process_cl(struct dt_iop_module_t *self,
dt_dev_pixelpipe_iop_t *piece,
cl_mem dev_in,
cl_mem dev_out,
const dt_iop_roi_t *const roi_in,
const dt_iop_roi_t *const roi_out)
{
dt_iop_temperature_data_t *d = (dt_iop_temperature_data_t *)piece->data;
dt_iop_temperature_global_data_t *gd = (dt_iop_temperature_global_data_t *)self->global_data;
const int devid = piece->pipe->devid;
const uint32_t filters = piece->pipe->dsc.filters;
cl_mem dev_coeffs = NULL;
cl_mem dev_xtrans = NULL;
cl_int err = DT_OPENCL_DEFAULT_ERROR;
int kernel = -1;
if(filters == 9u)
{
kernel = gd->kernel_whitebalance_1f_xtrans;
}
else if(filters)
{
kernel = gd->kernel_whitebalance_1f;
}
else
{
kernel = gd->kernel_whitebalance_4f;
}
if(filters == 9u)
{
dev_xtrans
= dt_opencl_copy_host_to_device_constant
(devid, sizeof(piece->pipe->dsc.xtrans), piece->pipe->dsc.xtrans);
if(dev_xtrans == NULL) goto error;
}
dev_coeffs = dt_opencl_copy_host_to_device_constant(devid, sizeof(float) * 3, d->coeffs);
if(dev_coeffs == NULL) goto error;
const int width = roi_in->width;
const int height = roi_in->height;
err = dt_opencl_enqueue_kernel_2d_args(devid, kernel, width, height,
CLARG(dev_in), CLARG(dev_out),
CLARG(width), CLARG(height),
CLARG(dev_coeffs), CLARG(filters),
CLARG(roi_out->x), CLARG(roi_out->y), CLARG(dev_xtrans));
if(err != CL_SUCCESS) goto error;
piece->pipe->dsc.temperature.enabled = TRUE;
for(int k = 0; k < 4; k++)
{
piece->pipe->dsc.temperature.coeffs[k] = d->coeffs[k];
piece->pipe->dsc.processed_maximum[k] =
d->coeffs[k] * piece->pipe->dsc.processed_maximum[k];
self->dev->proxy.wb_coeffs[k] = d->coeffs[k];
}
error:
dt_opencl_release_mem_object(dev_coeffs);
dt_opencl_release_mem_object(dev_xtrans);
return err;
}
#endif
void commit_params(struct dt_iop_module_t *self,
dt_iop_params_t *p1,
dt_dev_pixelpipe_t *pipe,
dt_dev_pixelpipe_iop_t *piece)
{
dt_iop_temperature_params_t *p = (dt_iop_temperature_params_t *)p1;
dt_iop_temperature_data_t *d = (dt_iop_temperature_data_t *)piece->data;
dt_iop_temperature_gui_data_t *g = (dt_iop_temperature_gui_data_t *)self->gui_data;
if(self->hide_enable_button)
{
piece->enabled = FALSE;
return;
}
d->coeffs[0] = p->red;
d->coeffs[1] = p->green;
d->coeffs[2] = p->blue;
d->coeffs[3] = p->g2;
// 4Bayer images not implemented in OpenCL yet
if(self->dev->image_storage.flags & DT_IMAGE_4BAYER)
piece->process_cl_ready = FALSE;
if(g)
{
// advertise on the pipe if coeffs are D65 for validity check
gboolean is_D65 = TRUE;
for(int c = 0; c < 3; c++)
if(!feqf(d->coeffs[c], (float)g->daylight_wb[c], DT_COEFF_EPS))
is_D65 = FALSE;
self->dev->proxy.wb_is_D65 = is_D65;
}
}
void init_pipe(struct dt_iop_module_t *self,
dt_dev_pixelpipe_t *pipe,
dt_dev_pixelpipe_iop_t *piece)
{
piece->data = malloc(sizeof(dt_iop_temperature_data_t));
}
void cleanup_pipe(struct dt_iop_module_t *self,
dt_dev_pixelpipe_t *pipe,
dt_dev_pixelpipe_iop_t *piece)
{
free(piece->data);
piece->data = NULL;
}
int generate_preset_combo(struct dt_iop_module_t *self)
{
dt_iop_temperature_gui_data_t *g = (dt_iop_temperature_gui_data_t *)self->gui_data;
int presets_found = 0;
const char *wb_name = NULL;
if(!dt_image_is_ldr(&self->dev->image_storage))
for(int i = 0; i < dt_wb_presets_count(); i++)
{
if(presets_found >= 50) break;
const dt_wb_data *wbp = dt_wb_preset(i);
if(!strcmp(wbp->make, self->dev->image_storage.camera_maker)
&& !strcmp(wbp->model, self->dev->image_storage.camera_model))
{
if(!wb_name) // This is first found preset for maker/model. add section.
{
char *section = g_strdup_printf("%s %s",
self->dev->image_storage.camera_maker,
self->dev->image_storage.camera_model);
dt_bauhaus_combobox_add_section(g->presets, section);
g_free(section);
g->preset_cnt++;
}
if(!wb_name || strcmp(wb_name, wbp->name))
{
// new preset found
dt_iop_temperature_preset_data_t *preset =
malloc(sizeof(dt_iop_temperature_preset_data_t));
wb_name = wbp->name;
preset->no_ft_pos = i;
preset->max_ft_pos = i;
preset->min_ft_pos = i;
if(wbp->tuning != 0)
{
// finetuning found.
// min finetuning is always first, since wb_preset is ordered.
int ft_pos = i;
int last_ft = wbp->tuning;
preset->min_ft_pos = ft_pos++;
while(strcmp(wb_name, dt_wb_preset(ft_pos)->name) == 0)
{
if(dt_wb_preset(ft_pos)->tuning == 0)
{
preset->no_ft_pos = ft_pos;
}
if(dt_wb_preset(ft_pos)->tuning > last_ft)
{
preset->max_ft_pos = ft_pos;
last_ft = dt_wb_preset(ft_pos)->tuning;
}
ft_pos++;
}
}
dt_bauhaus_combobox_add_full(g->presets, _(wbp->name),
DT_BAUHAUS_COMBOBOX_ALIGN_RIGHT, preset, free, TRUE);
g->preset_num[g->preset_cnt] = i;
g->preset_cnt++;
presets_found++;
}
}
}
return presets_found;
}
void color_finetuning_slider(struct dt_iop_module_t *self)
{
dt_iop_temperature_gui_data_t *g = (dt_iop_temperature_gui_data_t *)self->gui_data;
dt_bauhaus_slider_clear_stops(g->finetune);
dt_bauhaus_slider_set_feedback(g->finetune, !g->colored_sliders);
if(!g->colored_sliders) return;
dt_iop_temperature_preset_data_t *preset = dt_bauhaus_combobox_get_data(g->presets);
if(preset != NULL)
{
//we can do realistic/exaggerated.
double min_tune[3] = {0.0};
double no_tune[3] = {0.0};
double max_tune[3] = {0.0};
const dt_wb_data *wb_min = dt_wb_preset(preset->min_ft_pos);
const dt_wb_data *wb_no = dt_wb_preset(preset->no_ft_pos);
const dt_wb_data *wb_max = dt_wb_preset(preset->max_ft_pos);
if(!g->blackbody_is_confusing)
{
//realistic
const double neutral[3] = {
1 / wb_no->channels[0],
1 / wb_no->channels[1],
1 / wb_no->channels[2],
};
for(int ch=0; ch<3; ch++)
{
min_tune[ch] = neutral[ch] * wb_min->channels[ch];
no_tune[ch] = neutral[ch] * wb_no->channels[ch];
max_tune[ch] = neutral[ch] * wb_max->channels[ch];
}
const float maxsRGBmin_tune = fmaxf(fmaxf(min_tune[0], min_tune[1]), min_tune[2]);
const float maxsRGBmax_tune = fmaxf(fmaxf(max_tune[0], max_tune[1]), max_tune[2]);
for(int ch=0; ch<3; ch++)
{
min_tune[ch] = min_tune[ch] / maxsRGBmin_tune;
no_tune[ch] = 1.0;
max_tune[ch] = max_tune[ch] / maxsRGBmax_tune;
}
}
else
{
//exaggerated
for(int ch=0; ch<3; ch++)
{
min_tune[ch] = 0.5;
no_tune[ch] = 0.9;
max_tune[ch] = 0.5;
}
if(wb_min->channels[0] < wb_max->channels[0])
{
// from blue to red
min_tune[0] = 0.1;
min_tune[2] = 0.9;
max_tune[0] = 0.9;
max_tune[2] = 0.1;
}
else
{
//from red to blue
min_tune[0] = 0.9;
min_tune[2] = 0.1;
max_tune[0] = 0.1;
max_tune[2] = 0.9;
}
}
dt_bauhaus_slider_set_stop(g->finetune, 0.0, min_tune[0], min_tune[1], min_tune[2]);
dt_bauhaus_slider_set_stop(g->finetune, 0.5, no_tune[0], no_tune[1], no_tune[2]);
dt_bauhaus_slider_set_stop(g->finetune, 1.0, max_tune[0], max_tune[1], max_tune[2]);
}
if(gtk_widget_get_visible(GTK_WIDGET(g->finetune)))
{
gtk_widget_queue_draw(GTK_WIDGET(g->finetune));
}
}
void color_rgb_sliders(struct dt_iop_module_t *self)
{
dt_iop_temperature_gui_data_t *g = (dt_iop_temperature_gui_data_t *)self->gui_data;
const gboolean color_rgb = g->colored_sliders &&
!(self->dev->image_storage.flags & DT_IMAGE_4BAYER);
dt_bauhaus_slider_clear_stops(g->scale_r);
dt_bauhaus_slider_clear_stops(g->scale_g);
dt_bauhaus_slider_clear_stops(g->scale_b);
dt_bauhaus_slider_clear_stops(g->scale_g2);
dt_bauhaus_slider_set_feedback(g->scale_r, !color_rgb);
dt_bauhaus_slider_set_feedback(g->scale_g, !color_rgb);
dt_bauhaus_slider_set_feedback(g->scale_b, !color_rgb);
dt_bauhaus_slider_set_feedback(g->scale_g2, !color_rgb);
if(!color_rgb) return;
// there are 3 ways to do colored sliders: naive (independent 0->1),
// smart(er) (dependent 0->1) and real (coeff)
if(FALSE)
{
//naive:
dt_bauhaus_slider_set_stop(g->scale_r, 0.0, 0.0, 0.0, 0.0);
dt_bauhaus_slider_set_stop(g->scale_r, 1.0, 1.0, 0.0, 0.0);
dt_bauhaus_slider_set_stop(g->scale_g, 0.0, 0.0, 0.0, 0.0);
dt_bauhaus_slider_set_stop(g->scale_g, 1.0, 0.0, 1.0, 0.0);
dt_bauhaus_slider_set_stop(g->scale_b, 0.0, 0.0, 0.0, 0.0);
dt_bauhaus_slider_set_stop(g->scale_b, 1.0, 0.0, 0.0, 1.0);
dt_bauhaus_slider_set_stop(g->scale_g2, 0.0, 0.0, 0.0, 0.0);
dt_bauhaus_slider_set_stop(g->scale_g2, 1.0, 0.0, 1.0, 0.0);
}
if(!g->blackbody_is_confusing)
{
//smart(er) than naive
const float rchan =
dt_bauhaus_slider_get(g->scale_r) / dt_bauhaus_slider_get_hard_max(g->scale_r);
const float gchan =
dt_bauhaus_slider_get(g->scale_g) / dt_bauhaus_slider_get_hard_max(g->scale_g);
const float bchan =
dt_bauhaus_slider_get(g->scale_b) / dt_bauhaus_slider_get_hard_max(g->scale_b);
dt_bauhaus_slider_set_stop(g->scale_r, 0.0, 0.0, gchan, bchan);
dt_bauhaus_slider_set_stop(g->scale_r, 1.0, 1.0, gchan, bchan);
dt_bauhaus_slider_set_stop(g->scale_g, 0.0, rchan, 0.0, bchan);
dt_bauhaus_slider_set_stop(g->scale_g, 1.0, rchan, 1.0, bchan);
dt_bauhaus_slider_set_stop(g->scale_b, 0.0, rchan, gchan, 0.0);
dt_bauhaus_slider_set_stop(g->scale_b, 1.0, rchan, gchan, 1.0);
}
else
{
//real (ish)
//we consider daylight wb to be "reference white"
const double white[3] = {
1.0/g->daylight_wb[0],
1.0/g->daylight_wb[1],
1.0/g->daylight_wb[2],
};
const float rchanmul = dt_bauhaus_slider_get(g->scale_r);
const float rchanmulmax = dt_bauhaus_slider_get_hard_max(g->scale_r);
const float gchanmul = dt_bauhaus_slider_get(g->scale_g);
const float gchanmulmax = dt_bauhaus_slider_get_hard_max(g->scale_g);
const float bchanmul = dt_bauhaus_slider_get(g->scale_b);
const float bchanmulmax = dt_bauhaus_slider_get_hard_max(g->scale_g);
dt_bauhaus_slider_set_stop
(g->scale_r, 0.0, white[0]*0.0, white[1]*gchanmul, white[2]*bchanmul);
dt_bauhaus_slider_set_stop
(g->scale_r, g->daylight_wb[0]/rchanmulmax,
white[0]*g->daylight_wb[0], white[1]*gchanmul, white[2]*bchanmul);
dt_bauhaus_slider_set_stop
(g->scale_r, 1.0, white[0]*1.0,
white[1]*(gchanmul/gchanmulmax), white[2]*(bchanmul/bchanmulmax));
dt_bauhaus_slider_set_stop
(g->scale_g, 0.0, white[0]*rchanmul, white[1]*0.0, white[2]*bchanmul);
dt_bauhaus_slider_set_stop
(g->scale_g, g->daylight_wb[1]/bchanmulmax,
white[0]*rchanmul, white[1]*g->daylight_wb[1], white[2]*bchanmul);
dt_bauhaus_slider_set_stop
(g->scale_g, 1.0, white[0]*(rchanmul/rchanmulmax),
white[1]*1.0, white[2]*(bchanmul/bchanmulmax));
dt_bauhaus_slider_set_stop
(g->scale_b, 0.0, white[0]*rchanmul, white[1]*gchanmul, white[2]*0.0);
dt_bauhaus_slider_set_stop
(g->scale_b, g->daylight_wb[2]/bchanmulmax,
white[0]*rchanmul, white[1]*gchanmul, white[2]*g->daylight_wb[2]);
dt_bauhaus_slider_set_stop
(g->scale_b, 1.0, white[0]*(rchanmul/rchanmulmax),
white[1]*(gchanmul/gchanmulmax), white[2]*1.0);
}
if(gtk_widget_get_visible(GTK_WIDGET(g->scale_r)))
{
gtk_widget_queue_draw(GTK_WIDGET(g->scale_r));
gtk_widget_queue_draw(GTK_WIDGET(g->scale_g));
gtk_widget_queue_draw(GTK_WIDGET(g->scale_b));
}