-
Notifications
You must be signed in to change notification settings - Fork 8
/
ufraw_lensfun.cc
927 lines (878 loc) · 32.2 KB
/
ufraw_lensfun.cc
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
/*
* UFRaw - Unidentified Flying Raw converter for digital camera images
*
* ufraw_lensfun.cc - define lensfun UFObject settings.
* Copyright 2004-2016 by Udi Fuchs
*
* 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.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "ufraw.h"
#include <glib/gi18n.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#ifdef HAVE_LENSFUN
#include <lensfun.h>
#define UF_LF_TRANSFORM ( \
LF_MODIFY_DISTORTION | LF_MODIFY_GEOMETRY | LF_MODIFY_SCALE)
namespace UFRaw
{
class Lensfun : public UFGroup
{
private:
static lfDatabase *_LensDB;
public:
lfCamera Camera;
// 'Interpolation' represents the lens the user choose from the LensDB.
// Its transformation values are interpolate according to the choice
// of FocalLegth, Aperture and Distance.
lfLens Interpolation;
// 'Transformation' include the transformation the user choose from the
// 'Interpolation' lens.
lfLens Transformation;
double FocalLengthValue;
double ApertureValue;
double DistanceValue;
Lensfun();
#ifdef UFRAW_VALGRIND // Can be useful for valgrind --leak-check=full
~Lensfun() {
if (_LensDB != NULL)
lf_db_destroy(_LensDB);
_LensDB = NULL;
}
#endif
static Lensfun &Parent(UFObject &object) {
if (strcmp(object.Parent().Name(), ufLensfun) == 0)
return static_cast<Lensfun &>(object.Parent());
return Lensfun::Parent(object.Parent());
}
static const Lensfun &Parent(const UFObject &object) {
if (strcmp(object.Parent().Name(), ufLensfun) == 0)
return static_cast<Lensfun &>(object.Parent());
return Lensfun::Parent(object.Parent());
}
static lfDatabase *LensDB() {
/* Load lens database only once */
if (_LensDB == NULL) {
_LensDB = lfDatabase::Create();
_LensDB->Load();
}
return _LensDB;
}
void SetCamera(const lfCamera &camera) {
Camera = camera;
const char *maker = lf_mlstr_get(camera.Maker);
const char *model = lf_mlstr_get(camera.Model);
if (model != NULL) {
char *fm;
if (maker != NULL)
fm = g_strdup_printf("%s, %s", maker, model);
else
fm = g_strdup_printf("%s", model);
UFString &CameraModel = (*this)[ufCameraModel];
CameraModel.Set(fm);
g_free(fm);
// After setting the camera model, we can retry finding the lens.
SetLensInterpolation();
}
}
void SetLensModel(const lfLens &lens);
// Set the 'Interpolation' lens from the LensModel.
void SetLensInterpolation();
// Interpolate the TCA, Vignetting and Distortion models.
void Interpolate();
// Mark settings as manual (not from LensDB).
void Manual() {
char *lens_model = g_strdup_printf("Generic, Crop factor %.4g",
Transformation.CropFactor);
(*this)[ufLensModel].Set(lens_model);
g_free(lens_model);
UFObject::Parent()[ufLensfunAuto].Set("no");
}
void Init(bool reset);
};
static void parse_maker_model(const char *txt, char *make, size_t sz_make,
char *model, size_t sz_model)
{
while (txt[0] != '\0' && isspace(txt[0]))
txt++;
const gchar *sep = strchr(txt, ',');
if (sep != NULL) {
size_t len = sep - txt + 1;
len = MIN(len, sz_make);
g_strlcpy(make, txt, len);
while (*++sep && isspace(sep[0])) { }
g_strlcpy(model, sep, sz_model);
} else {
g_strlcpy(model, txt, sz_model);
}
}
extern "C" {
UFName ufCameraModel = "CameraModel";
}
class CameraModel : public UFString
{
public:
CameraModel() : UFString(ufCameraModel) { }
};
extern "C" {
UFName ufLensModel = "LensModel";
}
class LensModel : public UFString
{
public:
LensModel() : UFString(ufLensModel) { }
void Event(UFEventType type) {
if (type != uf_value_changed)
return UFObject::Event(type);
if (!HasParent())
return UFObject::Event(type);
Lensfun &Lensfun = Lensfun::Parent(*this);
Lensfun.SetLensInterpolation();
Lensfun.Interpolate();
return UFObject::Event(type);
}
void OriginalValueChangedEvent() {
if (!HasParent())
return;
Lensfun &Lensfun = Lensfun::Parent(*this);
// While loading rc/cmd/conf data, do not reset the auto setting
if (ufraw_image_get_data(this) != NULL)
Lensfun.UFObject::Parent()[ufLensfunAuto].Set("no");
}
};
#define _buffer_size 80
char *_StringNumber(char *buffer, double number)
{
/* int precision = 0;
while (floor(number * pow(10, precision))*10 <
floor(number * pow(10, precision+1)))
precision++;*/
// Compute the floating-point precision which is enough for "normal use".
// The criteria is to have 2 or 3 significant digits.
int precision;
if (number > 10.0 && (int)(10 * number) % 10 != 0)
// Support non-integer focal lengths longer than 10mm.
precision = MAX(-floor(log(number) / log(10.0) - 1.99), 0);
else if (number > 0.0)
precision = MAX(-floor(log(number) / log(10.0) - 0.99), 0);
else
precision = 0;
snprintf(buffer, _buffer_size, "%.*f", precision, number);
return buffer;
}
extern "C" {
UFName ufFocalLength = "FocalLength";
}
class FocalLength : public UFArray
{
public:
FocalLength() : UFArray(ufFocalLength) { }
void Event(UFEventType type) {
if (type == uf_default_changed && Index() == -1) {
// Default value is changed during Init. Reset to this default
// value if no other value was set.
Reset();
}
if (type != uf_value_changed)
return UFObject::Event(type);
ufraw_data *uf = ufraw_image_get_data(this);
if (uf == NULL)
return UFObject::Event(type);
if (!HasParent())
return UFObject::Event(type);
double value;
if (sscanf(StringValue(), "%lf", &value) != 1)
return UFObject::Event(type);
Lensfun::Parent(*this).FocalLengthValue = value;
Lensfun::Parent(*this).Interpolate();
ufraw_invalidate_layer(uf, ufraw_transform_phase);
return UFObject::Event(type);
}
void CreatePresets() {
if (!HasParent())
return;
Clear();
lfLens &lens = Lensfun::Parent(*this).Interpolation;
static double focalValues[] = { 4, 5, 6, 7, 8, 10, 12, 14, 17, 21, 24,
28, 35, 43, 50, 70, 85, 105, 135, 200, 300, 400, 600, 800, 0
};
char buffer[_buffer_size];
double min = lens.MinFocal, max = lens.MaxFocal;
if (min > 0)
*this << new UFString(ufPreset, _StringNumber(buffer, min));
int i = 0;
while (focalValues[i] < min && focalValues[i] != 0)
i++;
if (Has(_StringNumber(buffer, focalValues[i])))
i++; // Comparing strings works better than comparing doubles.
while (focalValues[i] < max && focalValues[i] != 0) {
*this << new UFString(ufPreset, _StringNumber(buffer,
focalValues[i]));
i++;
}
if (max > min)
*this << new UFString(ufPreset, _StringNumber(buffer, max));
}
};
extern "C" {
UFName ufAperture = "Aperture";
}
class Aperture : public UFArray
{
public:
Aperture() : UFArray(ufAperture) { }
void Event(UFEventType type) {
if (type == uf_default_changed && Index() == -1) {
// Default value is changed during Init. Reset to this default
// value if no other value was set.
Reset();
}
if (type != uf_value_changed)
return UFObject::Event(type);
ufraw_data *uf = ufraw_image_get_data(this);
if (uf == NULL)
return UFObject::Event(type);
if (!HasParent())
return UFObject::Event(type);
double value;
if (sscanf(StringValue(), "%lf", &value) != 1)
return UFObject::Event(type);
Lensfun::Parent(*this).ApertureValue = value;
Lensfun::Parent(*this).Interpolate();
return UFObject::Event(type);
}
void CreatePresets() {
if (!HasParent())
return;
Clear();
lfLens &lens = Lensfun::Parent(*this).Interpolation;
static double apertureValues[] = { 1, 1.2, 1.4, 1.7, 2, 2.4, 2.8, 3.4,
4, 4.8, 5.6, 6.7, 8, 9.5, 11, 13, 16, 19, 22, 27, 32, 38, 45, 0
};
char buffer[_buffer_size];
double min = lens.MinAperture;
if (min == 0)
return;
*this << new UFString(ufPreset, _StringNumber(buffer, min));
int i = 0;
while (apertureValues[i] < min && apertureValues[i] != 0)
i++;
if (Has(_StringNumber(buffer, apertureValues[i])))
i++; // Comparing strings works better than comparing doubles.
while (apertureValues[i] != 0) {
*this << new UFString(ufPreset, _StringNumber(buffer,
apertureValues[i]));
i++;
}
}
};
extern "C" {
UFName ufDistance = "Distance";
}
class Distance : public UFArray
{
public:
Distance() : UFArray(ufDistance) { }
void Event(UFEventType type) {
if (type == uf_default_changed && Index() == -1) {
// Default value is changed during Init. Reset to this default
// value if no other value was set.
Reset();
}
if (type != uf_value_changed)
return UFObject::Event(type);
ufraw_data *uf = ufraw_image_get_data(this);
if (uf == NULL)
return UFObject::Event(type);
if (!HasParent())
return UFObject::Event(type);
double value;
if (sscanf(StringValue(), "%lf", &value) != 1)
return UFObject::Event(type);
Lensfun::Parent(*this).DistanceValue = value;
Lensfun::Parent(*this).Interpolate();
return UFObject::Event(type);
}
void CreatePresets() {
Clear();
char buffer[_buffer_size];
double value = 0.25;
while (value < 1001) {
*this << new UFString(ufPreset, _StringNumber(buffer, value));
value *= sqrt(2.0);
if (value > 127 && value < 129) value = 125;
}
}
};
extern "C" {
UFName ufModel = "Model";
}
class Param : public UFNumber
{
public:
Param(UFName name, double min, double max, double defaultValue) :
UFNumber(name, min, max, defaultValue) { }
std::string XML(const char *indent) const {
char num[10];
g_snprintf(num, 10, "%.*lf", AccuracyDigits() + 2, DoubleValue());
return (std::string)indent +
"<" + Name() + ">" + num + "</" + Name() + ">\n";
}
void OriginalValueChangedEvent() {
if (!HasParent())
return;
// While loading rc/cmd/conf data, do not reset other settings
if (ufraw_image_get_data(this) == NULL)
return;
Lensfun &Lensfun = Lensfun::Parent(*this);
Lensfun.Manual();
}
};
extern "C" {
UFName ufTCA = "TCA";
}
class TCA : public UFArray
{
public:
TCA() : UFArray(ufTCA,
lfLens::GetTCAModelDesc(LF_TCA_MODEL_NONE, NULL, NULL)) {
for (lfTCAModel model = LF_TCA_MODEL_NONE; ;
model = lfTCAModel(model + 1)) {
const lfParameter **params;
const char *model_name =
lfLens::GetTCAModelDesc(model, NULL, ¶ms);
if (model_name == NULL)
break; // End of loop.
UFGroup &Model = *(new UFGroup(ufModel, model_name));
(*this) << &Model;
assert(params != NULL);
for (int i = 0; params[i] != NULL; i++)
Model << new Param(params[i]->Name, params[i]->Min,
params[i]->Max, params[i]->Default);
}
}
void Event(UFEventType type) {
if (type != uf_value_changed)
return UFObject::Event(type);
ufraw_data *uf = ufraw_image_get_data(this);
if (uf == NULL)
return UFObject::Event(type);
lfLens &lens = Lensfun::Parent(*this).Transformation;
if (lens.CalibTCA != NULL)
while (lens.CalibTCA[0] != NULL)
lens.RemoveCalibTCA(0);
lfLensCalibTCA calib;
calib.Model = static_cast<lfTCAModel>(Index());
calib.Focal = Lensfun::Parent(*this).FocalLengthValue;
const lfParameter **params;
lfLens::GetTCAModelDesc(calib.Model, NULL, ¶ms);
if (params != NULL) {
UFGroup &Model = (*this)[StringValue()];
for (int i = 0; params[i] != NULL; i++) {
UFNumber &Param = Model[params[i]->Name];
calib.Terms[i] = Param.DoubleValue();
}
}
lens.AddCalibTCA(&calib);
ufraw_invalidate_tca_layer(uf);
return UFObject::Event(type);
}
void Interpolate() {
if (!HasParent())
return;
Lensfun &Lensfun = Lensfun::Parent(*this);
lfLensCalibTCA calib;
if (!Lensfun.Interpolation.InterpolateTCA(
Lensfun.FocalLengthValue, calib)) {
return;
}
SetIndex(calib.Model - LF_TCA_MODEL_NONE);
const lfParameter **params;
lfLens::GetTCAModelDesc(calib.Model, NULL, ¶ms);
if (params != NULL) {
UFGroup &Model = (*this)[StringValue()];
for (int i = 0; params[i] != NULL; i++) {
UFNumber &Param = Model[params[i]->Name];
Param.Set(calib.Terms[i]);
}
}
}
};
extern "C" {
UFName ufVignetting = "Vignetting";
}
class Vignetting : public UFArray
{
public:
Vignetting() : UFArray(ufVignetting,
lfLens::GetVignettingModelDesc(LF_VIGNETTING_MODEL_NONE,
NULL, NULL)) {
for (lfVignettingModel model = LF_VIGNETTING_MODEL_NONE; ;
model = lfVignettingModel(model + 1)) {
const lfParameter **params;
const char *model_name =
lfLens::GetVignettingModelDesc(model, NULL, ¶ms);
if (model_name == NULL)
break; // End of loop.
UFGroup &Model = *(new UFGroup(ufModel, model_name));
(*this) << &Model;
assert(params != NULL);
for (int i = 0; params[i] != NULL; i++)
Model << new Param(params[i]->Name, params[i]->Min,
params[i]->Max, params[i]->Default);
}
}
void Event(UFEventType type) {
if (type != uf_value_changed)
return UFObject::Event(type);
ufraw_data *uf = ufraw_image_get_data(this);
if (uf == NULL)
return UFObject::Event(type);
lfLens &lens = Lensfun::Parent(*this).Transformation;
if (lens.CalibVignetting != NULL)
while (lens.CalibVignetting[0] != NULL)
lens.RemoveCalibVignetting(0);
lfLensCalibVignetting calib;
calib.Model = static_cast<lfVignettingModel>(Index());
calib.Focal = Lensfun::Parent(*this).FocalLengthValue;
calib.Aperture = Lensfun::Parent(*this).ApertureValue;
calib.Distance = Lensfun::Parent(*this).DistanceValue;
const lfParameter **params;
lfLens::GetVignettingModelDesc(calib.Model, NULL, ¶ms);
if (params != NULL) {
UFGroup &Model = (*this)[StringValue()];
for (int i = 0; params[i] != NULL; i++) {
UFNumber &Param = Model[params[i]->Name];
calib.Terms[i] = Param.DoubleValue();
}
}
lens.AddCalibVignetting(&calib);
ufraw_invalidate_layer(uf, ufraw_first_phase);
return UFObject::Event(type);
}
void Interpolate() {
ufraw_data *uf = ufraw_image_get_data(this);
if (uf == NULL)
return;
Lensfun &Lensfun = Lensfun::Parent(*this);
lfLensCalibVignetting calib;
if (!Lensfun.Interpolation.InterpolateVignetting(
Lensfun.FocalLengthValue, Lensfun.ApertureValue,
Lensfun.DistanceValue, calib)) {
return;
}
SetIndex(calib.Model - LF_VIGNETTING_MODEL_NONE);
const lfParameter **params;
lfLens::GetVignettingModelDesc(calib.Model, NULL, ¶ms);
if (params != NULL) {
UFGroup &Model = (*this)[StringValue()];
for (int i = 0; params[i] != NULL; i++) {
UFNumber &Param = Model[params[i]->Name];
Param.Set(calib.Terms[i]);
}
}
}
};
extern "C" {
UFName ufDistortion = "Distortion";
}
class Distortion : public UFArray
{
public:
// The elements of Distortion are distortion models:
// UFGroup &Model = (*this)[StringValue()];
// The elements of Model are the model's parameters:
// UFNumber Param = Model[params[i].Name];
// In XML it would look something like this:
// <Distortion Index='PanoTools lens model'>
// <Model Label='5th order polynomial'>
// <k1>0.0100</k1>
// <k2>-0.0100</k2>
// </Model>
// <Model Label='PanoTools lens model'>
// <a>0.0010</a>
// <b>0.0023</b>
// </Model>
// </Distortion>
Distortion() : UFArray(ufDistortion,
lfLens::GetDistortionModelDesc(LF_DIST_MODEL_NONE, NULL, NULL)) {
for (lfDistortionModel model = LF_DIST_MODEL_NONE; ;
model = lfDistortionModel(model + 1)) {
const lfParameter **params;
const char *model_name =
lfLens::GetDistortionModelDesc(model, NULL, ¶ms);
if (model_name == NULL)
break; // End of loop.
UFGroup &Model = *(new UFGroup(ufModel, model_name));
(*this) << &Model;
assert(params != NULL);
for (int i = 0; params[i] != NULL; i++)
Model << new Param(params[i]->Name, params[i]->Min,
params[i]->Max, params[i]->Default);
}
}
void Event(UFEventType type) {
if (type != uf_value_changed)
return UFObject::Event(type);
ufraw_data *uf = ufraw_image_get_data(this);
if (uf == NULL)
return UFObject::Event(type);
lfLens &lens = Lensfun::Parent(*this).Transformation;
if (lens.CalibDistortion != NULL)
while (lens.CalibDistortion[0] != NULL)
lens.RemoveCalibDistortion(0);
lfLensCalibDistortion calib;
calib.Model = static_cast<lfDistortionModel>(Index());
calib.Focal = Lensfun::Parent(*this).FocalLengthValue;
const lfParameter **params;
lfLens::GetDistortionModelDesc(calib.Model, NULL, ¶ms);
if (params != NULL) {
UFGroup &Model = (*this)[StringValue()];
for (int i = 0; params[i] != NULL; i++) {
UFNumber &Param = Model[params[i]->Name];
calib.Terms[i] = Param.DoubleValue();
}
}
lens.AddCalibDistortion(&calib);
ufraw_invalidate_layer(uf, ufraw_transform_phase);
return UFObject::Event(type);
}
void Interpolate() {
if (!HasParent())
return;
Lensfun &Lensfun = Lensfun::Parent(*this);
lfLensCalibDistortion calib;
if (!Lensfun.Interpolation.InterpolateDistortion(
Lensfun.FocalLengthValue, calib)) {
return;
}
SetIndex(calib.Model - LF_DIST_MODEL_NONE);
const lfParameter **params;
lfLens::GetDistortionModelDesc(calib.Model, NULL, ¶ms);
if (params != NULL) {
UFGroup &Model = (*this)[StringValue()];
for (int i = 0; params[i] != NULL; i++) {
UFNumber &Param = Model[params[i]->Name];
Param.Set(calib.Terms[i]);
}
}
}
};
extern "C" {
UFName ufLensGeometry = "LensGeometry";
}
class LensGeometry : public UFArray
{
public:
explicit LensGeometry(UFName name = ufLensGeometry) : UFArray(name,
lfLens::GetLensTypeDesc(LF_UNKNOWN, NULL)) {
for (lfLensType type = LF_UNKNOWN; ; type = lfLensType(type + 1)) {
const char *typeName = lfLens::GetLensTypeDesc(type, NULL);
if (typeName == NULL)
break; // End of loop.
*this << new UFString("Type", typeName);
}
}
void Event(UFEventType type) {
if (type != uf_value_changed)
return UFObject::Event(type);
ufraw_data *uf = ufraw_image_get_data(this);
if (uf == NULL)
return UFObject::Event(type);
Lensfun::Parent(*this).Transformation.Type = lfLensType(Index());
ufraw_invalidate_layer(uf, ufraw_transform_phase);
return UFObject::Event(type);
}
void OriginalValueChangedEvent() {
if (!HasParent())
return;
// While loading rc/cmd/conf data, do not reset other settings
if (ufraw_image_get_data(this) == NULL)
return;
Lensfun &Lensfun = Lensfun::Parent(*this);
Lensfun.Manual();
}
};
extern "C" {
UFName ufTargetLensGeometry = "TargetLensGeometry";
}
class TargetLensGeometry : public UFArray
{
public:
explicit TargetLensGeometry(UFName name = ufTargetLensGeometry) :
UFArray(name, lfLens::GetLensTypeDesc(LF_UNKNOWN, NULL)) {
for (lfLensType type = LF_UNKNOWN; ; type = lfLensType(type + 1)) {
const char *typeName = lfLens::GetLensTypeDesc(type, NULL);
if (typeName == NULL)
break; // End of loop.
*this << new UFString("Type", typeName);
}
}
void OriginalValueChangedEvent() {
ufraw_data *uf = ufraw_image_get_data(this);
if (uf == NULL)
return;
ufraw_invalidate_layer(uf, ufraw_transform_phase);
}
};
extern "C" {
UFName ufLensfun = "Lensfun";
}
Lensfun::Lensfun() : UFGroup(ufLensfun), FocalLengthValue(0.0),
ApertureValue(0.0), DistanceValue(0.0)
{
*this
<< new CameraModel
<< new LensModel
<< new FocalLength
<< new Aperture
<< new Distance
<< new TCA
<< new Vignetting
<< new Distortion
<< new LensGeometry
<< new TargetLensGeometry
;
}
void Lensfun::SetLensInterpolation()
{
char make[200] = "", model[200] = "";
parse_maker_model((*this)[ufLensModel].StringValue(), make, sizeof(make),
model, sizeof(model));
double crop_factor = 1.0;
int count = sscanf(model, "Crop factor %lf", &crop_factor);
if ((strcmp(make, "Generic") == 0 && count == 1) ||
(strcasecmp(make, "") == 0 && strcmp(model, "") == 0)) {
lfLens cropLens;
cropLens.SetMaker(make);
cropLens.SetModel(model);
cropLens.CropFactor = crop_factor;
cropLens.MinFocal = 1.0;
cropLens.MaxFocal = 800.0;
UFArray &LensGeometry = (*this)[ufLensGeometry];
cropLens.Type = lfLensType(LensGeometry.Index());
Interpolation = cropLens;
} else {
const lfLens **lensList = LensDB()->FindLenses(&Camera,
make, model, LF_SEARCH_LOOSE);
if (lensList == NULL || lensList[0] == NULL) {
lfLens emptyLens;
Interpolation = emptyLens;
} else {
Interpolation = *lensList[0];
}
if (lensList != NULL)
lf_free(lensList);
}
Transformation.CropFactor = Interpolation.CropFactor;
UFArray &LensGeometry = (*this)[ufLensGeometry];
LensGeometry.SetIndex(Interpolation.Type);
static_cast<UFRaw::FocalLength &>((*this)[ufFocalLength]).CreatePresets();
static_cast<UFRaw::Aperture &>((*this)[ufAperture]).CreatePresets();
static_cast<UFRaw::Distance &>((*this)[ufDistance]).CreatePresets();
}
void Lensfun::SetLensModel(const lfLens &lens)
{
UFString &LensModel = (*this)[ufLensModel];
const char *maker = lf_mlstr_get(lens.Maker);
const char *model = lf_mlstr_get(lens.Model);
if (model != NULL) {
char *fm;
if (maker != NULL)
fm = g_strdup_printf("%s, %s", maker, model);
else
fm = g_strdup_printf("%s", model);
if (LensModel.IsEqual(fm)) {
// Even if the lens has not changed, we still want to triger
// a change event.
LensModel.Event(uf_value_changed);
} else {
LensModel.Set(fm);
}
g_free(fm);
}
}
void Lensfun::Interpolate()
{
static_cast<TCA &>((*this)[ufTCA]).Interpolate();
static_cast<Vignetting &>((*this)[ufVignetting]).Interpolate();
static_cast<Distortion &>((*this)[ufDistortion]).Interpolate();
}
lfDatabase *Lensfun::_LensDB = NULL;
void Lensfun::Init(bool reset)
{
ufraw_data *uf = ufraw_image_get_data(this);
if (uf == NULL)
return;
UFGroup &Image = UFObject::Parent();
/* Set lens and camera from EXIF info, if possible */
if (uf->conf->real_make[0] || uf->conf->real_model[0]) {
const lfCamera **cams = LensDB()->FindCameras(
uf->conf->real_make, uf->conf->real_model);
if (cams != NULL) {
SetCamera(*cams[0]);
lf_free(cams);
}
}
UFString &CameraModel = (*this)[ufCameraModel];
CameraModel.SetDefault(CameraModel.StringValue());
char buffer[_buffer_size];
UFArray &FocalLength = (*this)[ufFocalLength];
FocalLength.SetDefault(_StringNumber(buffer, uf->conf->focal_len));
UFArray &Aperture = (*this)[ufAperture];
Aperture.SetDefault(_StringNumber(buffer, uf->conf->aperture));
UFArray &Distance = (*this)[ufDistance];
Distance.SetDefault(_StringNumber(buffer, uf->conf->subject_distance));
if (!reset) {
(*this)[ufTCA].Event(uf_value_changed);
(*this)[ufVignetting].Event(uf_value_changed);
(*this)[ufDistortion].Event(uf_value_changed);
return;
}
FocalLength.Reset();
Aperture.Reset();
Distance.Reset();
UFString &LensfunAuto = Image[ufLensfunAuto];
if (LensfunAuto.IsEqual("yes")) {
if (strlen(uf->conf->lensText) > 0) {
const lfLens **lenses = LensDB()->FindLenses(&Camera,
NULL, uf->conf->lensText, LF_SEARCH_LOOSE);
if (!CameraModel.IsEqual("") && lenses != NULL) {
SetLensModel(*lenses[0]);
// Changing the lens reset Auto="no". So set it back.
LensfunAuto.Set("yes");
lf_free(lenses);
// When loading a configuration file nothing changes, so we
// need to manually trigger the uf_value_changed event.
(*this)[ufTCA].Event(uf_value_changed);
(*this)[ufVignetting].Event(uf_value_changed);
(*this)[ufDistortion].Event(uf_value_changed);
return;
}
}
// Try using the "standard" lens of compact cameras.
const lfLens **lenses = LensDB()->FindLenses(&Camera,
NULL, "Standard", LF_SEARCH_LOOSE);
if (!CameraModel.IsEqual("") && lenses != NULL) {
SetLensModel(*lenses[0]);
// Changing the lens reset Auto="no". So set it back.
LensfunAuto.Set("yes");
lf_free(lenses);
// When loading a configuration file nothing changes, so we
// need to manually trigger the uf_value_changed event.
(*this)[ufTCA].Event(uf_value_changed);
(*this)[ufVignetting].Event(uf_value_changed);
(*this)[ufDistortion].Event(uf_value_changed);
return;
}
}
// LensfunAuto == "no"
(*this)[ufTCA].Reset();
(*this)[ufVignetting].Reset();
(*this)[ufDistortion].Reset();
}
extern "C" {
void ufraw_lensfun_init(UFObject *lensfun, UFBoolean reset)
{
static_cast<UFRaw::Lensfun *>(lensfun)->Init(reset);
}
void ufraw_convert_prepare_transform(ufraw_data *uf,
int width, int height, gboolean reverse, float scale)
{
UFGroup &Image = *uf->conf->ufobject;
UFRaw::Lensfun &Lensfun = static_cast<UFRaw::Lensfun &>(Image[ufLensfun]);
if (uf->modifier != NULL)
uf->modifier->Destroy();
uf->modifier = lfModifier::Create(&Lensfun.Transformation,
Lensfun.Camera.CropFactor, width, height);
/* Make sure the Camera is valid;
* Operations can return nan (not-a-number) values if not
* We should instead guarantee a valid camera,
* and if none present, skip everything.
*/
if (uf->modifier == NULL) {
return;
} else if (! Lensfun.Camera.Check()) {
g_warning("ufraw_convert_prepare_transform: Camare check failed, skipping lens correction");
uf->modifier->Destroy();
uf->modifier = NULL;
return;
}
UFArray &targetLensGeometry = Lensfun[ufTargetLensGeometry];
uf->modFlags = uf->modifier->Initialize(&Lensfun.Transformation,
LF_PF_U16, Lensfun.FocalLengthValue, Lensfun.ApertureValue,
Lensfun.DistanceValue, scale,
lfLensType(targetLensGeometry.Index()),
UF_LF_TRANSFORM | LF_MODIFY_VIGNETTING, reverse);
if ((uf->modFlags & (UF_LF_TRANSFORM | LF_MODIFY_VIGNETTING)) == 0) {
uf->modifier->Destroy();
uf->modifier = NULL;
}
}
void ufraw_prepare_tca(ufraw_data *uf)
{
UFGroup &Image = *uf->conf->ufobject;
UFRaw::Lensfun &Lensfun = static_cast<UFRaw::Lensfun &>(Image[ufLensfun]);
ufraw_image_data *img = &uf->Images[ufraw_raw_phase];
if (uf->TCAmodifier != NULL)
uf->TCAmodifier->Destroy();
uf->TCAmodifier = lfModifier::Create(&Lensfun.Transformation,
Lensfun.Camera.CropFactor, img->width, img->height);
/* Make sure the Camera is valid;
* Operations can return nan (not-a-number) values if not
* We should instead guarantee a valid camera,
* and if none present, skip everything.
*/
if (uf->TCAmodifier == NULL) {
return;
} else if (! Lensfun.Camera.Check()) {
g_warning("ufraw_prepare_tca: Camare check failed, skipping lens correction");
uf->TCAmodifier->Destroy();
uf->TCAmodifier = NULL;
return;
}
UFArray &targetLensGeometry = Lensfun[ufTargetLensGeometry];
int modFlags = uf->TCAmodifier->Initialize(&Lensfun.Transformation,
LF_PF_U16, Lensfun.FocalLengthValue, Lensfun.ApertureValue,
Lensfun.DistanceValue, 1.0,
lfLensType(targetLensGeometry.Index()),
LF_MODIFY_TCA, false);
if ((modFlags & LF_MODIFY_TCA) == 0) {
uf->TCAmodifier->Destroy();
uf->TCAmodifier = NULL;
}
}
UFObject *ufraw_lensfun_new()
{
return new Lensfun();
}
const struct lfCamera *ufraw_lensfun_camera(const UFObject *lensfun)
{
return &static_cast<const UFRaw::Lensfun *>(lensfun)->Camera;
}
void ufraw_lensfun_set_camera(UFObject *lensfun, const struct lfCamera *camera)
{
static_cast<UFRaw::Lensfun *>(lensfun)->SetCamera(*camera);
}
const struct lfLens *ufraw_lensfun_interpolation_lens(const UFObject *lensfun)
{
return &static_cast<const UFRaw::Lensfun *>(lensfun)->Interpolation;
}
void ufraw_lensfun_set_lens(UFObject *lensfun, const struct lfLens *lens)
{
dynamic_cast<UFRaw::Lensfun &>(*lensfun).SetLensModel(*lens);
}
lfDatabase *ufraw_lensfun_db()
{
return Lensfun::LensDB();
}
} // extern "C"
} // namespace UFRaw
#endif // HAVE_LENSFUN