/
colorin.c
1291 lines (1170 loc) · 45.9 KB
/
colorin.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--2011 johannes hanika.
copyright (c) 2011 henrik andersson
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 "iop/color.h"
#include "develop/develop.h"
#include "control/control.h"
#include "gui/gtk.h"
#include "bauhaus/bauhaus.h"
#include "common/colorspaces.h"
#include "common/colormatrices.c"
#include "common/opencl.h"
#include "common/image_cache.h"
#ifdef HAVE_OPENJPEG
#include "common/imageio_j2k.h"
#endif
#include "common/imageio_jpeg.h"
#include "common/imageio_tiff.h"
#include "common/imageio_png.h"
#include "external/adobe_coeff.c"
#include <xmmintrin.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <string.h>
DT_MODULE_INTROSPECTION(3, dt_iop_colorin_params_t)
static void update_profile_list(dt_iop_module_t *self);
typedef enum dt_iop_color_normalize_t
{
DT_NORMALIZE_OFF,
DT_NORMALIZE_SRGB,
DT_NORMALIZE_ADOBE_RGB,
DT_NORMALIZE_LINEAR_REC709_RGB,
DT_NORMALIZE_LINEAR_REC2020_RGB
}
dt_iop_color_normalize_t;
typedef struct dt_iop_colorin_params_t
{
char iccprofile[DT_IOP_COLOR_ICC_LEN];
dt_iop_color_intent_t intent;
int normalize;
int blue_mapping;
}
dt_iop_colorin_params_t;
typedef struct dt_iop_colorin_gui_data_t
{
GtkWidget *cbox1, *cbox2, *cbox3;
GList *image_profiles, *global_profiles;
int n_image_profiles;
}
dt_iop_colorin_gui_data_t;
typedef struct dt_iop_colorin_global_data_t
{
int kernel_colorin_unbound;
int kernel_colorin_clipping;
}
dt_iop_colorin_global_data_t;
typedef struct dt_iop_colorin_data_t
{
cmsHPROFILE input;
cmsHPROFILE Lab;
cmsHPROFILE nrgb;
cmsHTRANSFORM *xform_cam_Lab;
cmsHTRANSFORM *xform_cam_nrgb;
cmsHTRANSFORM *xform_nrgb_Lab;
float lut[3][LUT_SAMPLES];
float cmatrix[9];
float nmatrix[9];
float lmatrix[9];
float unbounded_coeffs[3][3]; // approximation for extrapolation of shaper curves
int blue_mapping;
}
dt_iop_colorin_data_t;
const char *
name()
{
return _("input color profile");
}
int
groups ()
{
return IOP_GROUP_COLOR;
}
int
flags ()
{
return IOP_FLAGS_ALLOW_TILING | IOP_FLAGS_ONE_INSTANCE;
}
int
legacy_params (dt_iop_module_t *self, const void *const old_params, const int old_version, void *new_params, const int new_version)
{
if (old_version == 1 && new_version == 3)
{
typedef struct dt_iop_colorin_params_v1_t
{
char iccprofile[DT_IOP_COLOR_ICC_LEN];
dt_iop_color_intent_t intent;
}
dt_iop_colorin_params_v1_t;
const dt_iop_colorin_params_v1_t *old = (dt_iop_colorin_params_v1_t *)old_params;
dt_iop_colorin_params_t *new = (dt_iop_colorin_params_t *)new_params;
g_strlcpy(new->iccprofile, old->iccprofile, DT_IOP_COLOR_ICC_LEN);
new->intent = old->intent;
new->normalize = 0;
new->blue_mapping = 1;
return 0;
}
if (old_version == 2 && new_version == 3)
{
typedef struct dt_iop_colorin_params_v2_t
{
char iccprofile[DT_IOP_COLOR_ICC_LEN];
dt_iop_color_intent_t intent;
int normalize;
}
dt_iop_colorin_params_v2_t;
const dt_iop_colorin_params_v2_t *old = (dt_iop_colorin_params_v2_t *)old_params;
dt_iop_colorin_params_t *new = (dt_iop_colorin_params_t *)new_params;
g_strlcpy(new->iccprofile, old->iccprofile, DT_IOP_COLOR_ICC_LEN);
new->intent = old->intent;
new->normalize = old->normalize;
new->blue_mapping = 1;
return 0;
}
return 1;
}
void
init_global(dt_iop_module_so_t *module)
{
const int program = 2; // basic.cl, from programs.conf
dt_iop_colorin_global_data_t *gd = (dt_iop_colorin_global_data_t *)malloc(sizeof(dt_iop_colorin_global_data_t));
module->data = gd;
gd->kernel_colorin_unbound = dt_opencl_create_kernel(program, "colorin_unbound");
gd->kernel_colorin_clipping = dt_opencl_create_kernel(program, "colorin_clipping");
}
void
cleanup_global(dt_iop_module_so_t *module)
{
dt_iop_colorin_global_data_t *gd = (dt_iop_colorin_global_data_t *)module->data;
dt_opencl_free_kernel(gd->kernel_colorin_unbound);
dt_opencl_free_kernel(gd->kernel_colorin_clipping);
free(module->data);
module->data = NULL;
}
#if 0
static void intent_changed (GtkWidget *widget, gpointer user_data)
{
dt_iop_module_t *self = (dt_iop_module_t *)user_data;
if(self->dt->gui->reset) return;
dt_iop_colorin_params_t *p = (dt_iop_colorin_params_t *)self->params;
p->intent = (dt_iop_color_intent_t)dt_bauhaus_combobox_get(widget);
dt_dev_add_history_item(darktable.develop, self, TRUE);
}
#endif
static void
profile_changed (GtkWidget *widget, gpointer user_data)
{
dt_iop_module_t *self = (dt_iop_module_t *)user_data;
if(self->dt->gui->reset) return;
dt_iop_request_focus(self);
dt_iop_colorin_params_t *p = (dt_iop_colorin_params_t *)self->params;
dt_iop_colorin_gui_data_t *g = (dt_iop_colorin_gui_data_t *)self->gui_data;
int pos = dt_bauhaus_combobox_get(widget);
GList *prof;
if(pos < g->n_image_profiles)
prof = g->image_profiles;
else
{
prof = g->global_profiles;
pos -= g->n_image_profiles;
}
while(prof)
{
// could use g_list_nth. this seems safer?
dt_iop_color_profile_t *pp = (dt_iop_color_profile_t *)prof->data;
if(pp->pos == pos)
{
g_strlcpy(p->iccprofile, pp->filename, sizeof(p->iccprofile));
dt_dev_add_history_item(darktable.develop, self, TRUE);
return;
}
prof = g_list_next(prof);
}
// should really never happen.
fprintf(stderr, "[colorin] color profile %s seems to have disappeared!\n", p->iccprofile);
}
static void
normalize_changed (GtkWidget *widget, gpointer user_data)
{
dt_iop_module_t *self = (dt_iop_module_t *)user_data;
if(self->dt->gui->reset) return;
dt_iop_colorin_params_t *p = (dt_iop_colorin_params_t *)self->params;
p->normalize = dt_bauhaus_combobox_get(widget);
dt_dev_add_history_item(darktable.develop, self, TRUE);
}
static float
lerp_lut(const float *const lut, const float v)
{
// TODO: check if optimization is worthwhile!
const float ft = CLAMPS(v*(LUT_SAMPLES-1), 0, LUT_SAMPLES-1);
const int t = ft < LUT_SAMPLES-2 ? ft : LUT_SAMPLES-2;
const float f = ft - t;
const float l1 = lut[t];
const float l2 = lut[t+1];
return l1*(1.0f-f) + l2*f;
}
#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 *roi_in, const dt_iop_roi_t *roi_out)
{
dt_iop_colorin_data_t *d = (dt_iop_colorin_data_t *)piece->data;
dt_iop_colorin_global_data_t *gd = (dt_iop_colorin_global_data_t *)self->data;
cl_mem dev_m = NULL, dev_l = NULL, dev_r = NULL, dev_g = NULL, dev_b = NULL, dev_coeffs = NULL;
int kernel;
float *cmat, *lmat;
if(d->nrgb)
{
kernel = gd->kernel_colorin_clipping;
cmat = d->nmatrix;
lmat = d->lmatrix;
}
else
{
kernel = gd->kernel_colorin_unbound;
cmat = d->cmatrix;
lmat = d->lmatrix;
}
cl_int err = -999;
const int blue_mapping = d->blue_mapping &&
piece->pipe->image.flags & DT_IMAGE_RAW;
const int devid = piece->pipe->devid;
const int width = roi_in->width;
const int height = roi_in->height;
size_t sizes[] = { ROUNDUPWD(width), ROUNDUPHT(height), 1};
dev_m = dt_opencl_copy_host_to_device_constant(devid, sizeof(float)*9, cmat);
if (dev_m == NULL) goto error;
dev_l = dt_opencl_copy_host_to_device_constant(devid, sizeof(float)*9, lmat);
if (dev_l == NULL) goto error;
dev_r = dt_opencl_copy_host_to_device(devid, d->lut[0], 256, 256, sizeof(float));
if (dev_r == NULL) goto error;
dev_g = dt_opencl_copy_host_to_device(devid, d->lut[1], 256, 256, sizeof(float));
if (dev_g == NULL) goto error;
dev_b = dt_opencl_copy_host_to_device(devid, d->lut[2], 256, 256, sizeof(float));
if (dev_b == NULL) goto error;
dev_coeffs = dt_opencl_copy_host_to_device_constant(devid, sizeof(float)*3*3, (float *)d->unbounded_coeffs);
if (dev_coeffs == NULL) goto error;
dt_opencl_set_kernel_arg(devid, kernel, 0, sizeof(cl_mem), (void *)&dev_in);
dt_opencl_set_kernel_arg(devid, kernel, 1, sizeof(cl_mem), (void *)&dev_out);
dt_opencl_set_kernel_arg(devid, kernel, 2, sizeof(int), (void *)&width);
dt_opencl_set_kernel_arg(devid, kernel, 3, sizeof(int), (void *)&height);
dt_opencl_set_kernel_arg(devid, kernel, 4, sizeof(cl_mem), (void *)&dev_m);
dt_opencl_set_kernel_arg(devid, kernel, 5, sizeof(cl_mem), (void *)&dev_l);
dt_opencl_set_kernel_arg(devid, kernel, 6, sizeof(cl_mem), (void *)&dev_r);
dt_opencl_set_kernel_arg(devid, kernel, 7, sizeof(cl_mem), (void *)&dev_g);
dt_opencl_set_kernel_arg(devid, kernel, 8, sizeof(cl_mem), (void *)&dev_b);
dt_opencl_set_kernel_arg(devid, kernel, 9, sizeof(cl_int), (void *)&blue_mapping);
dt_opencl_set_kernel_arg(devid, kernel, 10, sizeof(cl_mem), (void *)&dev_coeffs);
err = dt_opencl_enqueue_kernel_2d(devid, kernel, sizes);
if(err != CL_SUCCESS) goto error;
dt_opencl_release_mem_object(dev_m);
dt_opencl_release_mem_object(dev_l);
dt_opencl_release_mem_object(dev_r);
dt_opencl_release_mem_object(dev_g);
dt_opencl_release_mem_object(dev_b);
dt_opencl_release_mem_object(dev_coeffs);
return TRUE;
error:
if (dev_m != NULL) dt_opencl_release_mem_object(dev_m);
if (dev_l != NULL) dt_opencl_release_mem_object(dev_l);
if (dev_r != NULL) dt_opencl_release_mem_object(dev_r);
if (dev_g != NULL) dt_opencl_release_mem_object(dev_g);
if (dev_b != NULL) dt_opencl_release_mem_object(dev_b);
if (dev_coeffs != NULL) dt_opencl_release_mem_object(dev_coeffs);
dt_print(DT_DEBUG_OPENCL, "[opencl_colorin] couldn't enqueue kernel! %d\n", err);
return FALSE;
}
#endif
static inline __m128
lab_f_m(const __m128 x)
{
const __m128 epsilon = _mm_set1_ps(216.0f/24389.0f);
const __m128 kappa = _mm_set1_ps(24389.0f/27.0f);
// calculate as if x > epsilon : result = cbrtf(x)
// approximate cbrtf(x):
const __m128 a = _mm_castsi128_ps(_mm_add_epi32(_mm_cvtps_epi32(_mm_div_ps(_mm_cvtepi32_ps(_mm_castps_si128(x)),_mm_set1_ps(3.0f))),_mm_set1_epi32(709921077)));
const __m128 a3 = _mm_mul_ps(_mm_mul_ps(a,a),a);
const __m128 res_big = _mm_div_ps(_mm_mul_ps(a,_mm_add_ps(a3,_mm_add_ps(x,x))),_mm_add_ps(_mm_add_ps(a3,a3),x));
// calculate as if x <= epsilon : result = (kappa*x+16)/116
const __m128 res_small = _mm_div_ps(_mm_add_ps(_mm_mul_ps(kappa,x),_mm_set1_ps(16.0f)),_mm_set1_ps(116.0f));
// blend results according to whether each component is > epsilon or not
const __m128 mask = _mm_cmpgt_ps(x,epsilon);
return _mm_or_ps(_mm_and_ps(mask,res_big),_mm_andnot_ps(mask,res_small));
}
static inline __m128
dt_XYZ_to_Lab_SSE(const __m128 XYZ)
{
const __m128 d50_inv = _mm_set_ps(0.0f, 1.0f/0.8249f, 1.0f, 1.0f/0.9642f);
const __m128 coef = _mm_set_ps(0.0f,200.0f,500.0f,116.0f);
const __m128 f = lab_f_m(_mm_mul_ps(XYZ,d50_inv));
// because d50_inv.z is 0.0f, lab_f(0) == 16/116, so Lab[0] = 116*f[0] - 16 equal to 116*(f[0]-f[3])
return _mm_mul_ps(coef,_mm_sub_ps(_mm_shuffle_ps(f,f,_MM_SHUFFLE(3,1,0,1)),_mm_shuffle_ps(f,f,_MM_SHUFFLE(3,2,1,3))));
}
void
process (struct dt_iop_module_t *self, dt_dev_pixelpipe_iop_t *piece, void *ivoid, void *ovoid, const dt_iop_roi_t *roi_in, const dt_iop_roi_t *roi_out)
{
const dt_iop_colorin_data_t *const d = (dt_iop_colorin_data_t *)piece->data;
const int ch = piece->colors;
const int clipping = (d->nrgb != NULL);
const int blue_mapping = d->blue_mapping &&
piece->pipe->image.flags & DT_IMAGE_RAW;
if(!isnan(d->cmatrix[0]))
{
// only color matrix. use our optimized fast path!
const float *const cmat = d->cmatrix;
const float *const nmat = d->nmatrix;
const float *const lmat = d->lmatrix;
float *in = (float *)ivoid;
float *out = (float *)ovoid;
#ifdef _OPENMP
#pragma omp parallel for default(none) shared(roi_in,roi_out, out, in) schedule(static)
#endif
for(int j=0; j<roi_out->height; j++)
{
float *buf_in = in + (size_t)ch*roi_in->width *j;
float *buf_out = out + (size_t)ch*roi_out->width*j;
float cam[3];
const __m128 cm0 = _mm_set_ps(0.0f,cmat[6],cmat[3],cmat[0]);
const __m128 cm1 = _mm_set_ps(0.0f,cmat[7],cmat[4],cmat[1]);
const __m128 cm2 = _mm_set_ps(0.0f,cmat[8],cmat[5],cmat[2]);
const __m128 nm0 = _mm_set_ps(0.0f,nmat[6],nmat[3],nmat[0]);
const __m128 nm1 = _mm_set_ps(0.0f,nmat[7],nmat[4],nmat[1]);
const __m128 nm2 = _mm_set_ps(0.0f,nmat[8],nmat[5],nmat[2]);
const __m128 lm0 = _mm_set_ps(0.0f,lmat[6],lmat[3],lmat[0]);
const __m128 lm1 = _mm_set_ps(0.0f,lmat[7],lmat[4],lmat[1]);
const __m128 lm2 = _mm_set_ps(0.0f,lmat[8],lmat[5],lmat[2]);
for(int i=0; i<roi_out->width; i++, buf_in+=ch, buf_out+=ch )
{
// memcpy(cam, buf_in, sizeof(float)*3);
// avoid calling this for linear profiles (marked with negative entries), assures unbounded
// color management without extrapolation.
for(int i=0; i<3; i++) cam[i] = (d->lut[i][0] >= 0.0f) ?
((buf_in[i] < 1.0f) ? lerp_lut(d->lut[i], buf_in[i])
: dt_iop_eval_exp(d->unbounded_coeffs[i], buf_in[i]))
: buf_in[i];
if(blue_mapping)
{
const float YY = cam[0] + cam[1] + cam[2];
if(YY > 0.0f)
{
// manual gamut mapping. these values cause trouble when converting back from Lab to sRGB.
// deeply saturated blues turn into purple fringes, so dampen them before conversion.
// this is off for non-raw images, which don't seem to have this problem.
// might be caused by too loose clipping bounds during highlight clipping?
const float zz = cam[2] / YY;
// lower amount and higher bound_z make the effect smaller.
// the effect is weakened the darker input values are, saturating at bound_Y
const float bound_z = 0.5f, bound_Y = 0.8f;
const float amount = 0.11f;
if (zz > bound_z)
{
const float t = (zz - bound_z) / (1.0f - bound_z) * fminf(1.0f, YY / bound_Y);
cam[1] += t * amount;
cam[2] -= t * amount;
}
}
}
#if 0
__attribute__((aligned(16))) float XYZ[4];
_mm_store_ps(XYZ,_mm_add_ps(_mm_add_ps( _mm_mul_ps(m0,_mm_set1_ps(cam[0])), _mm_mul_ps(m1,_mm_set1_ps(cam[1]))), _mm_mul_ps(m2,_mm_set1_ps(cam[2]))));
dt_XYZ_to_Lab(XYZ, buf_out);
#endif
if(!clipping)
{
__m128 xyz = _mm_add_ps(_mm_add_ps( _mm_mul_ps(cm0,_mm_set1_ps(cam[0])), _mm_mul_ps(cm1,_mm_set1_ps(cam[1]))), _mm_mul_ps(cm2,_mm_set1_ps(cam[2])));
_mm_stream_ps(buf_out,dt_XYZ_to_Lab_SSE(xyz));
}
else
{
__m128 nrgb = _mm_add_ps(_mm_add_ps( _mm_mul_ps(nm0,_mm_set1_ps(cam[0])), _mm_mul_ps(nm1,_mm_set1_ps(cam[1]))), _mm_mul_ps(nm2,_mm_set1_ps(cam[2])));
__m128 crgb = _mm_min_ps(_mm_max_ps( nrgb, _mm_set1_ps(0.0f)), _mm_set1_ps(1.0f));
__m128 xyz = _mm_add_ps(_mm_add_ps( _mm_mul_ps(lm0,_mm_shuffle_ps(crgb, crgb, _MM_SHUFFLE(0,0,0,0))),
_mm_mul_ps(lm1,_mm_shuffle_ps(crgb, crgb, _MM_SHUFFLE(1,1,1,1)))),
_mm_mul_ps(lm2,_mm_shuffle_ps(crgb, crgb, _MM_SHUFFLE(2,2,2,2))));
_mm_stream_ps(buf_out,dt_XYZ_to_Lab_SSE(xyz));
}
}
}
_mm_sfence();
}
else
{
// use general lcms2 fallback
#ifdef _OPENMP
#pragma omp parallel for schedule(static) default(none) shared(ivoid, ovoid, roi_out)
#endif
for(int k=0; k<roi_out->height; k++)
{
const float *in = ((float *)ivoid) + (size_t)ch*k*roi_out->width;
float *out = ((float *)ovoid) + (size_t)ch*k*roi_out->width;
void *cam = NULL;
const void *input = NULL;
if(blue_mapping)
{
input = cam = dt_alloc_align(16, 4 * sizeof(float) * roi_out->width);
float *camptr = (float *)cam;
for (int j = 0; j < roi_out->width; j++, in += 4, camptr += 4)
{
camptr[0] = in[0];
camptr[1] = in[1];
camptr[2] = in[2];
const float YY = camptr[0] + camptr[1] + camptr[2];
const float zz = camptr[2] / YY;
const float bound_z = 0.5f, bound_Y = 0.5f;
const float amount = 0.11f;
if (zz > bound_z)
{
const float t = (zz - bound_z) / (1.0f - bound_z) * fminf(1.0, YY / bound_Y);
camptr[1] += t * amount;
camptr[2] -= t * amount;
}
}
}
else
{
input = in;
}
// convert to (L,a/L,b/L) to be able to change L without changing saturation.
if(!d->nrgb)
{
cmsDoTransform(d->xform_cam_Lab, input, out, roi_out->width);
if(blue_mapping)
{
dt_free_align(cam);
cam = NULL;
}
} else {
void *rgb = dt_alloc_align(16, 4*sizeof(float)*roi_out->width);
cmsDoTransform(d->xform_cam_nrgb, input, rgb, roi_out->width);
if(blue_mapping)
{
dt_free_align(cam);
cam = NULL;
}
float *rgbptr = (float *)rgb;
for (int j=0; j<roi_out->width; j++,rgbptr+=4)
{
const __m128 min = _mm_setzero_ps();
const __m128 max = _mm_set1_ps(1.0f);
const __m128 input = _mm_load_ps(rgbptr);
const __m128 result = _mm_max_ps(_mm_min_ps(input, max), min);
_mm_store_ps(rgbptr, result);
}
cmsDoTransform(d->xform_nrgb_Lab, rgb, out, roi_out->width);
dt_free_align(rgb);
}
}
}
if(piece->pipe->mask_display)
dt_iop_alpha_copy(ivoid, ovoid, roi_out->width, roi_out->height);
}
static void
mat3mul (float *dst, const float *const m1, const float *const m2)
{
for(int k=0; k<3; k++)
{
for(int i=0; i<3; i++)
{
float x=0.0f;
for(int j=0; j<3; j++) x += m1[3*k+j] * m2[3*j+i];
dst[3*k+i] = x;
}
}
}
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_colorin_params_t *p = (dt_iop_colorin_params_t *)p1;
dt_iop_colorin_data_t *d = (dt_iop_colorin_data_t *)piece->data;
if(d->input) cmsCloseProfile(d->input);
d->input = NULL;
if(d->nrgb) cmsCloseProfile(d->nrgb);
d->nrgb = NULL;
d->blue_mapping = p->blue_mapping;
switch(p->normalize)
{
case DT_NORMALIZE_SRGB:
d->nrgb = dt_colorspaces_create_srgb_profile();
break;
case DT_NORMALIZE_ADOBE_RGB:
d->nrgb = dt_colorspaces_create_adobergb_profile();
break;
case DT_NORMALIZE_LINEAR_REC709_RGB:
d->nrgb = dt_colorspaces_create_linear_rec709_rgb_profile();
break;
case DT_NORMALIZE_LINEAR_REC2020_RGB:
d->nrgb = dt_colorspaces_create_linear_rec2020_rgb_profile();
break;
case DT_NORMALIZE_OFF:
default:
d->nrgb = NULL;
}
if(d->xform_cam_Lab)
{
cmsDeleteTransform(d->xform_cam_Lab);
d->xform_cam_Lab = NULL;
}
if(d->xform_cam_nrgb)
{
cmsDeleteTransform(d->xform_cam_nrgb);
d->xform_cam_nrgb = NULL;
}
if(d->xform_nrgb_Lab)
{
cmsDeleteTransform(d->xform_nrgb_Lab);
d->xform_nrgb_Lab = NULL;
}
d->cmatrix[0] = d->nmatrix[0] = d->lmatrix[0] = NAN;
d->lut[0][0] = -1.0f;
d->lut[1][0] = -1.0f;
d->lut[2][0] = -1.0f;
piece->process_cl_ready = 1;
char datadir[PATH_MAX];
char filename[PATH_MAX];
dt_loc_get_datadir(datadir, sizeof(datadir));
if(!strcmp(p->iccprofile, "Lab"))
{
piece->enabled = 0;
return;
}
piece->enabled = 1;
if(!strcmp(p->iccprofile, "darktable"))
{
char makermodel[1024];
dt_colorspaces_get_makermodel(makermodel, sizeof(makermodel), pipe->image.exif_maker, pipe->image.exif_model);
d->input = dt_colorspaces_create_darktable_profile(makermodel);
if(!d->input) snprintf(p->iccprofile, sizeof(p->iccprofile), "eprofile");
}
if(!strcmp(p->iccprofile, "vendor"))
{
char makermodel[1024];
dt_colorspaces_get_makermodel(makermodel, sizeof(makermodel), pipe->image.exif_maker, pipe->image.exif_model);
d->input = dt_colorspaces_create_vendor_profile(makermodel);
if(!d->input) snprintf(p->iccprofile, sizeof(p->iccprofile), "eprofile");
}
if(!strcmp(p->iccprofile, "alternate"))
{
char makermodel[1024];
dt_colorspaces_get_makermodel(makermodel, sizeof(makermodel), pipe->image.exif_maker, pipe->image.exif_model);
d->input = dt_colorspaces_create_alternate_profile(makermodel);
if(!d->input) snprintf(p->iccprofile, sizeof(p->iccprofile), "eprofile");
}
if(!strcmp(p->iccprofile, "eprofile"))
{
// embedded color profile
const dt_image_t *cimg = dt_image_cache_read_get(darktable.image_cache, pipe->image.id);
if(cimg == NULL || cimg->profile == NULL) snprintf(p->iccprofile, sizeof(p->iccprofile), "ematrix");
else d->input = cmsOpenProfileFromMem(cimg->profile, cimg->profile_size);
dt_image_cache_read_release(darktable.image_cache, cimg);
}
if(!strcmp(p->iccprofile, "ematrix"))
{
// embedded matrix, hopefully D65
if(isnan(pipe->image.d65_color_matrix[0])) snprintf(p->iccprofile, sizeof(p->iccprofile), "cmatrix");
else d->input = dt_colorspaces_create_xyzimatrix_profile((float (*)[3])pipe->image.d65_color_matrix);
}
if(!strcmp(p->iccprofile, "cmatrix"))
{
// color matrix
char makermodel[1024];
dt_colorspaces_get_makermodel(makermodel, sizeof(makermodel), pipe->image.exif_maker, pipe->image.exif_model);
float cam_xyz[12];
cam_xyz[0] = NAN;
dt_dcraw_adobe_coeff(makermodel, (float (*)[12])cam_xyz);
if(isnan(cam_xyz[0]))
{
if(dt_image_is_raw(&pipe->image))
{
fprintf(stderr, "[colorin] `%s' color matrix not found!\n", makermodel);
dt_control_log(_("`%s' color matrix not found!"), makermodel);
}
snprintf(p->iccprofile, sizeof(p->iccprofile), "linear_rec709_rgb");
}
else d->input = dt_colorspaces_create_xyzimatrix_profile((float (*)[3])cam_xyz);
}
if(!strcmp(p->iccprofile, "sRGB"))
{
d->input = dt_colorspaces_create_srgb_profile();
}
else if(!strcmp(p->iccprofile, "infrared"))
{
d->input = dt_colorspaces_create_linear_infrared_profile();
}
else if(!strcmp(p->iccprofile, "XYZ"))
{
d->input = dt_colorspaces_create_xyz_profile();
}
else if(!strcmp(p->iccprofile, "adobergb"))
{
d->input = dt_colorspaces_create_adobergb_profile();
}
else if(!strcmp(p->iccprofile, "linear_rec709_rgb") || !strcmp(p->iccprofile, "linear_rgb"))
{
d->input = dt_colorspaces_create_linear_rec709_rgb_profile();
}
else if(!strcmp(p->iccprofile, "linear_rec2020_rgb"))
{
d->input = dt_colorspaces_create_linear_rec2020_rgb_profile();
}
else if(!d->input)
{
dt_colorspaces_find_profile(filename, sizeof(filename), p->iccprofile, "in");
d->input = cmsOpenProfileFromFile(filename, "r");
}
if(!d->input && strcmp(p->iccprofile, "sRGB"))
{
// use linear_rec709_rgb as fallback for missing non-sRGB profiles:
d->input = dt_colorspaces_create_linear_rec709_rgb_profile();
}
// final resort: sRGB
if(!d->input) d->input = dt_colorspaces_create_srgb_profile();
// should never happen, but catch that case to avoid a crash
if(!d->input)
{
dt_control_log(_("input profile could not be generated!"));
piece->enabled = 0;
return;
}
// prepare transformation matrix or lcms2 transforms as fallback
if(d->nrgb)
{
// user wants us to clip to a given RGB profile
if(dt_colorspaces_get_matrix_from_input_profile (d->input, d->cmatrix, d->lut[0], d->lut[1], d->lut[2], LUT_SAMPLES))
{
piece->process_cl_ready = 0;
d->cmatrix[0] = NAN;
d->xform_cam_Lab = cmsCreateTransform(d->input, TYPE_RGBA_FLT, d->Lab, TYPE_LabA_FLT, p->intent, 0);
d->xform_cam_nrgb = cmsCreateTransform(d->input, TYPE_RGBA_FLT, d->nrgb, TYPE_RGBA_FLT, p->intent, 0);
d->xform_nrgb_Lab = cmsCreateTransform(d->nrgb, TYPE_RGBA_FLT, d->Lab, TYPE_LabA_FLT, p->intent, 0);
}
else
{
float lutr[1], lutg[1], lutb[1];
float omat[9];
dt_colorspaces_get_matrix_from_output_profile (d->nrgb, omat, lutr, lutg, lutb, 1);
mat3mul (d->nmatrix, omat, d->cmatrix);
dt_colorspaces_get_matrix_from_input_profile (d->nrgb, d->lmatrix, lutr, lutg, lutb, 1);
}
}
else
{
// default mode: unbound processing
if(dt_colorspaces_get_matrix_from_input_profile (d->input, d->cmatrix, d->lut[0], d->lut[1], d->lut[2], LUT_SAMPLES))
{
piece->process_cl_ready = 0;
d->cmatrix[0] = NAN;
d->xform_cam_Lab = cmsCreateTransform(d->input, TYPE_RGBA_FLT, d->Lab, TYPE_LabA_FLT, p->intent, 0);
}
}
// we might have failed generating the clipping transformations, check that:
if(d->nrgb && ((!d->xform_cam_nrgb && isnan(d->nmatrix[0])) || (!d->xform_nrgb_Lab && isnan(d->lmatrix[0]))))
{
if(d->xform_cam_nrgb)
{
cmsDeleteTransform(d->xform_cam_nrgb);
d->xform_cam_nrgb = NULL;
}
if(d->xform_nrgb_Lab)
{
cmsDeleteTransform(d->xform_nrgb_Lab);
d->xform_nrgb_Lab = NULL;
}
dt_colorspaces_cleanup_profile(d->nrgb);
d->nrgb = NULL;
}
// user selected a non-supported output profile, check that:
if(!d->xform_cam_Lab && isnan(d->cmatrix[0]))
{
dt_control_log(_("unsupported input profile has been replaced by linear Rec709 RGB!"));
if(d->input) dt_colorspaces_cleanup_profile(d->input);
if(d->nrgb) dt_colorspaces_cleanup_profile(d->nrgb);
d->nrgb = NULL;
d->input = dt_colorspaces_create_linear_rec709_rgb_profile();
if(dt_colorspaces_get_matrix_from_input_profile (d->input, d->cmatrix, d->lut[0], d->lut[1], d->lut[2], LUT_SAMPLES))
{
piece->process_cl_ready = 0;
d->cmatrix[0] = NAN;
d->xform_cam_Lab = cmsCreateTransform(d->input, TYPE_RGBA_FLT, d->Lab, TYPE_LabA_FLT, p->intent, 0);
}
}
// now try to initialize unbounded mode:
// we do a extrapolation for input values above 1.0f.
// unfortunately we can only do this if we got the computation
// in our hands, i.e. for the fast builtin-dt-matrix-profile path.
for(int k=0; k<3; k++)
{
// omit luts marked as linear (negative as marker)
if(d->lut[k][0] >= 0.0f)
{
const float x[4] = {0.7f, 0.8f, 0.9f, 1.0f};
const float y[4] = {lerp_lut(d->lut[k], x[0]),
lerp_lut(d->lut[k], x[1]),
lerp_lut(d->lut[k], x[2]),
lerp_lut(d->lut[k], x[3])
};
dt_iop_estimate_exp(x, y, 4, d->unbounded_coeffs[k]);
}
else d->unbounded_coeffs[k][0] = -1.0f;
}
}
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_colorin_data_t));
dt_iop_colorin_data_t *d = (dt_iop_colorin_data_t *)piece->data;
d->input = NULL;
d->nrgb = NULL;
d->xform_cam_Lab = NULL;
d->xform_cam_nrgb = NULL;
d->xform_nrgb_Lab = NULL;
d->Lab = dt_colorspaces_create_lab_profile();
self->commit_params(self, self->default_params, pipe, piece);
}
void cleanup_pipe (struct dt_iop_module_t *self, dt_dev_pixelpipe_t *pipe, dt_dev_pixelpipe_iop_t *piece)
{
dt_iop_colorin_data_t *d = (dt_iop_colorin_data_t *)piece->data;
if(d->input) dt_colorspaces_cleanup_profile(d->input);
dt_colorspaces_cleanup_profile(d->Lab);
if (d->nrgb) dt_colorspaces_cleanup_profile(d->nrgb);
if(d->xform_cam_Lab)
{
cmsDeleteTransform(d->xform_cam_Lab);
d->xform_cam_Lab = NULL;
}
if(d->xform_cam_nrgb)
{
cmsDeleteTransform(d->xform_cam_nrgb);
d->xform_cam_nrgb = NULL;
}
if(d->xform_nrgb_Lab)
{
cmsDeleteTransform(d->xform_nrgb_Lab);
d->xform_nrgb_Lab = NULL;
}
free(piece->data);
piece->data = NULL;
}
void gui_update(struct dt_iop_module_t *self)
{
dt_iop_module_t *module = (dt_iop_module_t *)self;
dt_iop_colorin_gui_data_t *g = (dt_iop_colorin_gui_data_t *)self->gui_data;
dt_iop_colorin_params_t *p = (dt_iop_colorin_params_t *)module->params;
// dt_bauhaus_combobox_set(g->cbox1, (int)p->intent);
dt_bauhaus_combobox_set(g->cbox3, p->normalize);
update_profile_list(self);
// TODO: merge this into update_profile_list()
GList *prof = g->image_profiles;
while(prof)
{
dt_iop_color_profile_t *pp = (dt_iop_color_profile_t *)prof->data;
if(!strcmp(pp->filename, p->iccprofile))
{
dt_bauhaus_combobox_set(g->cbox2, pp->pos);
return;
}
prof = g_list_next(prof);
}
prof = g->global_profiles;
while(prof)
{
dt_iop_color_profile_t *pp = (dt_iop_color_profile_t *)prof->data;
if(!strcmp(pp->filename, p->iccprofile))
{
dt_bauhaus_combobox_set(g->cbox2, pp->pos + g->n_image_profiles);
return;
}
prof = g_list_next(prof);
}
dt_bauhaus_combobox_set(g->cbox2, 0);
if(strcmp(p->iccprofile, "darktable")) fprintf(stderr, "[colorin] could not find requested profile `%s'!\n", p->iccprofile);
}
// FIXME: update the gui when we add/remove the eprofile or ematrix
void reload_defaults(dt_iop_module_t *module)
{
dt_iop_colorin_params_t tmp = (dt_iop_colorin_params_t)
{
.iccprofile = "darktable",
.intent = DT_INTENT_PERCEPTUAL,
.normalize = DT_NORMALIZE_OFF,
.blue_mapping = 0
};
// we might be called from presets update infrastructure => there is no image
if(!module || !module->dev) goto end;
gboolean use_eprofile = FALSE;
// some file formats like jpeg can have an embedded color profile
// currently we only support jpeg, j2k, tiff and png
const dt_image_t *cimg = dt_image_cache_read_get(darktable.image_cache, module->dev->image_storage.id);
if(!cimg->profile)
{
char filename[PATH_MAX];
gboolean from_cache = TRUE;
dt_image_full_path(cimg->id, filename, sizeof(filename), &from_cache);
const gchar *cc = filename + strlen(filename);
for(; *cc!='.'&&cc>filename; cc--);
gchar *ext = g_ascii_strdown(cc+1, -1);
if(!strcmp(ext, "jpg") || !strcmp(ext, "jpeg"))
{
dt_imageio_jpeg_t jpg;
if(!dt_imageio_jpeg_read_header(filename, &jpg))
{
dt_image_t *img = dt_image_cache_write_get(darktable.image_cache, cimg);
img->profile_size = dt_imageio_jpeg_read_profile(&jpg, &img->profile);
use_eprofile = (img->profile_size > 0);
dt_image_cache_write_release(darktable.image_cache, img, DT_IMAGE_CACHE_RELAXED);
}
}
#ifdef HAVE_OPENJPEG
else if(!strcmp(ext, "jp2") || !strcmp(ext, "j2k") || !strcmp(ext, "j2c") || !strcmp(ext, "jpc"))
{
dt_image_t *img = dt_image_cache_write_get(darktable.image_cache, cimg);
img->profile_size = dt_imageio_j2k_read_profile(filename, &img->profile);
use_eprofile = (img->profile_size > 0);
dt_image_cache_write_release(darktable.image_cache, img, DT_IMAGE_CACHE_RELAXED);
}
#endif
else if(!strcmp(ext, "tif") || !strcmp(ext, "tiff"))
{
dt_image_t *img = dt_image_cache_write_get(darktable.image_cache, cimg);
img->profile_size = dt_imageio_tiff_read_profile(filename, &img->profile);
use_eprofile = (img->profile_size > 0);
dt_image_cache_write_release(darktable.image_cache, img, DT_IMAGE_CACHE_RELAXED);
}
else if(!strcmp(ext, "png"))
{
dt_image_t *img = dt_image_cache_write_get(darktable.image_cache, cimg);
img->profile_size = dt_imageio_png_read_profile(filename, &img->profile);
use_eprofile = (img->profile_size > 0);
dt_image_cache_write_release(darktable.image_cache, img, DT_IMAGE_CACHE_RELAXED);
}
g_free(ext);
}
else
use_eprofile = TRUE; // the image has a profile assigned
dt_image_cache_read_release(darktable.image_cache, cimg);
if(use_eprofile) g_strlcpy(tmp.iccprofile, "eprofile", sizeof(tmp.iccprofile));
else if(module->dev->image_storage.colorspace == DT_IMAGE_COLORSPACE_SRGB) g_strlcpy(tmp.iccprofile, "sRGB", sizeof(tmp.iccprofile));
else if(module->dev->image_storage.colorspace == DT_IMAGE_COLORSPACE_ADOBE_RGB) g_strlcpy(tmp.iccprofile, "adobergb", sizeof(tmp.iccprofile));
else if(dt_image_is_ldr(&module->dev->image_storage)) g_strlcpy(tmp.iccprofile, "sRGB", sizeof(tmp.iccprofile));
end:
memcpy(module->params, &tmp, sizeof(dt_iop_colorin_params_t));
memcpy(module->default_params, &tmp, sizeof(dt_iop_colorin_params_t));
}
void init(dt_iop_module_t *module)
{
// module->data = malloc(sizeof(dt_iop_colorin_data_t));
module->params = malloc(sizeof(dt_iop_colorin_params_t));
module->default_params = malloc(sizeof(dt_iop_colorin_params_t));
module->params_size = sizeof(dt_iop_colorin_params_t);
module->gui_data = NULL;
module->priority = 333; // module order created by iop_dependencies.py, do not edit!
module->hide_enable_button = 1;
module->default_enabled = 1;
}
void cleanup(dt_iop_module_t *module)
{
free(module->gui_data);
module->gui_data = NULL;
free(module->params);
module->params = NULL;
}
static void update_profile_list(dt_iop_module_t *self)
{
dt_iop_colorin_gui_data_t *g = (dt_iop_colorin_gui_data_t *)self->gui_data;
// clear and refill the image profile list
while(g->image_profiles)
{
g_free(g->image_profiles->data);
g->image_profiles = g_list_delete_link(g->image_profiles, g->image_profiles);
}
g->image_profiles = NULL;
g->n_image_profiles = 0;
dt_iop_color_profile_t *prof;
int pos = -1;
// some file formats like jpeg can have an embedded color profile
// currently we only support jpeg, j2k, tiff and png
const dt_image_t *cimg = dt_image_cache_read_get(darktable.image_cache, self->dev->image_storage.id);
if(cimg->profile)
{
prof = (dt_iop_color_profile_t *)g_malloc0(sizeof(dt_iop_color_profile_t));
g_strlcpy(prof->filename, "eprofile", sizeof(prof->filename));
g_strlcpy(prof->name, "eprofile", sizeof(prof->name));
g->image_profiles = g_list_append(g->image_profiles, prof);
prof->pos = ++pos;
}
dt_image_cache_read_release(darktable.image_cache, cimg);
// use the matrix embedded in some DNGs
if(!isnan(self->dev->image_storage.d65_color_matrix[0]))
{
prof = (dt_iop_color_profile_t *)g_malloc0(sizeof(dt_iop_color_profile_t));