Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_camera_profile.cpp

raw
1 /*****************************************************************************/
2 // Copyright 2006-2023 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 #include "dng_camera_profile.h"
10
11 #include "dng_1d_table.h"
12 #include "dng_assertions.h"
13 #include "dng_color_space.h"
14 #include "dng_gain_map.h"
15 #include "dng_host.h"
16 #include "dng_exceptions.h"
17 #include "dng_image_writer.h"
18 #include "dng_info.h"
19 #include "dng_parse_utils.h"
20 #include "dng_safe_arithmetic.h"
21 #include "dng_shared.h"
22 #include "dng_tag_codes.h"
23 #include "dng_tag_types.h"
24 #include "dng_temperature.h"
25 #include "dng_xy_coord.h"
26
27 /*****************************************************************************/
28
29 const char * kProfileName_Embedded = "Embedded";
30
31 const char * kAdobeCalibrationSignature = "com.adobe";
32
33 /*****************************************************************************/
34
35 const char * kProfileName_GroupPrefix = "Group: ";
36
37 /*****************************************************************************/
38
39 bool HasProfileGroupPrefix (const dng_string &name)
40 {
41
42 return name.StartsWith (kProfileName_GroupPrefix, true) &&
43 name.Length () > strlen (kProfileName_GroupPrefix);
44
45 }
46
47 /*****************************************************************************/
48
49 dng_string StripProfileGroupPrefix (const dng_string &name)
50 {
51
52 if (HasProfileGroupPrefix (name))
53 {
54
55 dng_string result (name.Get () + strlen (kProfileName_GroupPrefix));
56
57 return result;
58
59 }
60
61 return name;
62
63 }
64
65 /*****************************************************************************/
66 /*****************************************************************************/
67 /*****************************************************************************/
68
69 void dng_camera_profile_id::AddDigest (dng_md5_printer &printer) const
70 {
71
72 printer.Process ("DCPI", 4);
73
74 if (Name ().NotEmpty ())
75 {
76
77 printer.Process (Name ().Get (),
78 Name ().Length ());
79
80 }
81
82 if (Fingerprint ().IsValid ())
83 {
84
85 printer.Process (fFingerprint.data,
86 uint32 (sizeof (fFingerprint.data)));
87
88 }
89
90 }
91
92 /*****************************************************************************/
93 /*****************************************************************************/
94 /*****************************************************************************/
95
96 dng_camera_profile::dng_camera_profile ()
97
98 : fName ()
99 , fCalibrationIlluminant1 (lsUnknown)
100 , fCalibrationIlluminant2 (lsUnknown)
101 , fCalibrationIlluminant3 (lsUnknown)
102 , fColorMatrix1 ()
103 , fColorMatrix2 ()
104 , fColorMatrix3 ()
105 , fForwardMatrix1 ()
106 , fForwardMatrix2 ()
107 , fForwardMatrix3 ()
108 , fReductionMatrix1 ()
109 , fReductionMatrix2 ()
110 , fReductionMatrix3 ()
111 , fFingerprint ()
112 , fRenderDataFingerprint ()
113 , fCopyright ()
114 , fEmbedPolicy (pepAllowCopying)
115 , fHueSatDeltas1 ()
116 , fHueSatDeltas2 ()
117 , fHueSatDeltas3 ()
118 , fHueSatMapEncoding (encoding_Linear)
119 , fLookTable ()
120 , fLookTableEncoding (encoding_Linear)
121 , fBaselineExposureOffset (0, 100)
122 , fDefaultBlackRender (defaultBlackRender_Auto)
123 , fToneCurve ()
124 , fToneMethod (profileToneMethod_Unspecified)
125 , fProfileCalibrationSignature ()
126 , fUniqueCameraModelRestriction ()
127 , fWasReadFromDNG (false)
128 , fWasReadFromDisk (false)
129 , fWasStubbed (false)
130
131 {
132
133 fToneCurve.SetInvalid ();
134
135 }
136
137 /*****************************************************************************/
138
139 dng_camera_profile::~dng_camera_profile ()
140 {
141
142 }
143
144 /*****************************************************************************/
145
146 uint32 dng_camera_profile::IlluminantModel () const
147 {
148
149 // Start by assuming we have a single-illuminant model.
150
151 uint32 model = 1;
152
153 // If we have a 2nd calibration illuminant and 2nd color matrix, then
154 // assume we are using the dual-illuminnt model.
155
156 if ((CalibrationIlluminant2 () != lsUnknown) && HasColorMatrix2 ())
157 {
158
159 model = 2;
160
161 // If we also have a 3rd calibration illuminant and 3rd color matrix,
162 // then assume we are using the triple-illuminnt model.
163
164 if ((CalibrationIlluminant3 () != lsUnknown) && HasColorMatrix3 ())
165 {
166
167 model = 3;
168
169 }
170
171 }
172
173 return model;
174
175 }
176
177 /*****************************************************************************/
178
179 real64 dng_camera_profile::IlluminantToTemperature (uint32 light,
180 const dng_illuminant_data &data)
181 {
182
183 switch (light)
184 {
185
186 case lsStandardLightA:
187 case lsTungsten:
188 {
189 return 2850.0;
190 }
191
192 case lsISOStudioTungsten:
193 {
194 return 3200.0;
195 }
196
197 case lsD50:
198 {
199 return 5000.0;
200 }
201
202 case lsD55:
203 case lsDaylight:
204 case lsFineWeather:
205 case lsFlash:
206 case lsStandardLightB:
207 {
208 return 5500.0;
209 }
210
211 case lsD65:
212 case lsStandardLightC:
213 case lsCloudyWeather:
214 {
215 return 6500.0;
216 }
217
218 case lsD75:
219 case lsShade:
220 {
221 return 7500.0;
222 }
223
224 case lsDaylightFluorescent:
225 {
226 return (5700.0 + 7100.0) * 0.5;
227 }
228
229 case lsDayWhiteFluorescent:
230 {
231 return (4600.0 + 5500.0) * 0.5;
232 }
233
234 case lsCoolWhiteFluorescent:
235 case lsFluorescent:
236 {
237 return (3800.0 + 4500.0) * 0.5;
238 }
239
240 case lsWhiteFluorescent:
241 {
242 return (3250.0 + 3800.0) * 0.5;
243 }
244
245 case lsWarmWhiteFluorescent:
246 {
247 return (2600.0 + 3250.0) * 0.5;
248 }
249
250 case lsOther:
251 {
252 return dng_temperature (data.WhiteXY ()).Temperature ();
253 }
254
255 default:
256 {
257 return 0.0;
258 }
259
260 }
261
262 }
263
264 /******************************************************************************/
265
266 void dng_camera_profile::NormalizeColorMatrix (dng_matrix &m)
267 {
268
269 if (m.NotEmpty ())
270 {
271
272 // Find scale factor to normalize the matrix.
273
274 dng_vector coord = m * PCStoXYZ ();
275
276 real64 maxCoord = coord.MaxEntry ();
277
278 if (maxCoord > 0.0 && (maxCoord < 0.99 || maxCoord > 1.01))
279 {
280
281 m.Scale (1.0 / maxCoord);
282
283 }
284
285 // Round to four decimal places.
286
287 m.Round (10000);
288
289 }
290
291 }
292
293 /******************************************************************************/
294
295 void dng_camera_profile::SetColorMatrix1 (const dng_matrix &m)
296 {
297
298 fColorMatrix1 = m;
299
300 NormalizeColorMatrix (fColorMatrix1);
301
302 ClearFingerprint ();
303
304 }
305
306 /******************************************************************************/
307
308 void dng_camera_profile::SetColorMatrix2 (const dng_matrix &m)
309 {
310
311 fColorMatrix2 = m;
312
313 NormalizeColorMatrix (fColorMatrix2);
314
315 ClearFingerprint ();
316
317 }
318
319 /******************************************************************************/
320
321 void dng_camera_profile::SetColorMatrix3 (const dng_matrix &m)
322 {
323
324 fColorMatrix3 = m;
325
326 NormalizeColorMatrix (fColorMatrix3);
327
328 ClearFingerprint ();
329
330 }
331
332 /******************************************************************************/
333
334 // Make sure the forward matrix maps to exactly the PCS.
335
336 void dng_camera_profile::NormalizeForwardMatrix (dng_matrix &m)
337 {
338
339 if (m.NotEmpty ())
340 {
341
342 dng_vector cameraOne;
343
344 cameraOne.SetIdentity (m.Cols ());
345
346 dng_vector xyz = m * cameraOne;
347
348 m = PCStoXYZ ().AsDiagonal () *
349 Invert (xyz.AsDiagonal ()) *
350 m;
351
352 }
353
354 }
355
356 /******************************************************************************/
357
358 void dng_camera_profile::SetForwardMatrix1 (const dng_matrix &m)
359 {
360
361 fForwardMatrix1 = m;
362
363 fForwardMatrix1.Round (10000);
364
365 ClearFingerprint ();
366
367 }
368
369 /******************************************************************************/
370
371 void dng_camera_profile::SetForwardMatrix2 (const dng_matrix &m)
372 {
373
374 fForwardMatrix2 = m;
375
376 fForwardMatrix2.Round (10000);
377
378 ClearFingerprint ();
379
380 }
381
382 /*****************************************************************************/
383
384 void dng_camera_profile::SetForwardMatrix3 (const dng_matrix &m)
385 {
386
387 fForwardMatrix3 = m;
388
389 fForwardMatrix3.Round (10000);
390
391 ClearFingerprint ();
392
393 }
394
395 /*****************************************************************************/
396
397 void dng_camera_profile::SetReductionMatrix1 (const dng_matrix &m)
398 {
399
400 fReductionMatrix1 = m;
401
402 fReductionMatrix1.Round (10000);
403
404 ClearFingerprint ();
405
406 }
407
408 /******************************************************************************/
409
410 void dng_camera_profile::SetReductionMatrix2 (const dng_matrix &m)
411 {
412
413 fReductionMatrix2 = m;
414
415 fReductionMatrix2.Round (10000);
416
417 ClearFingerprint ();
418
419 }
420
421 /*****************************************************************************/
422
423 void dng_camera_profile::SetReductionMatrix3 (const dng_matrix &m)
424 {
425
426 fReductionMatrix3 = m;
427
428 fReductionMatrix3.Round (10000);
429
430 ClearFingerprint ();
431
432 }
433
434 /*****************************************************************************/
435
436 bool dng_camera_profile::HasColorMatrix1 () const
437 {
438
439 return fColorMatrix1.Cols () == 3 &&
440 fColorMatrix1.Rows () > 1;
441
442 }
443
444 /*****************************************************************************/
445
446 bool dng_camera_profile::HasColorMatrix2 () const
447 {
448
449 return fColorMatrix2.Cols () == 3 &&
450 fColorMatrix2.Rows () == fColorMatrix1.Rows ();
451
452 }
453
454 /*****************************************************************************/
455
456 bool dng_camera_profile::HasColorMatrix3 () const
457 {
458
459 return fColorMatrix3.Cols () == 3 &&
460 fColorMatrix3.Rows () == fColorMatrix2.Rows () &&
461 fColorMatrix3.Rows () == fColorMatrix1.Rows ();
462
463 }
464
465 /*****************************************************************************/
466
467 void dng_camera_profile::SetHueSatDeltas1 (const dng_hue_sat_map &deltas1)
468 {
469
470 fHueSatDeltas1 = deltas1;
471
472 ClearFingerprint ();
473
474 }
475
476 /*****************************************************************************/
477
478 void dng_camera_profile::SetHueSatDeltas2 (const dng_hue_sat_map &deltas2)
479 {
480
481 fHueSatDeltas2 = deltas2;
482
483 ClearFingerprint ();
484
485 }
486
487 /*****************************************************************************/
488
489 void dng_camera_profile::SetHueSatDeltas3 (const dng_hue_sat_map &deltas3)
490 {
491
492 fHueSatDeltas3 = deltas3;
493
494 ClearFingerprint ();
495
496 }
497
498 /*****************************************************************************/
499
500 void dng_camera_profile::SetLookTable (const dng_hue_sat_map &table)
501 {
502
503 fLookTable = table;
504
505 ClearFingerprint ();
506
507 }
508
509 /*****************************************************************************/
510
511 static void FingerprintMatrix (dng_md5_printer_stream &printer,
512 const dng_matrix &matrix)
513 {
514
515 tag_matrix tag (0, matrix);
516
517 // Tag's Put routine doesn't write the header, only the data
518
519 tag.Put (printer);
520
521 }
522
523 /*****************************************************************************/
524
525 static void FingerprintHueSatMap (dng_md5_printer_stream &printer,
526 const dng_hue_sat_map &map)
527 {
528
529 if (map.IsNull ())
530 return;
531
532 uint32 hues;
533 uint32 sats;
534 uint32 vals;
535
536 map.GetDivisions (hues, sats, vals);
537
538 printer.Put_uint32 (hues);
539 printer.Put_uint32 (sats);
540 printer.Put_uint32 (vals);
541
542 for (uint32 val = 0; val < vals; val++)
543 for (uint32 hue = 0; hue < hues; hue++)
544 for (uint32 sat = 0; sat < sats; sat++)
545 {
546
547 dng_hue_sat_map::HSBModify modify;
548
549 map.GetDelta (hue, sat, val, modify);
550
551 printer.Put_real32 (modify.fHueShift);
552 printer.Put_real32 (modify.fSatScale);
553 printer.Put_real32 (modify.fValScale);
554
555 }
556
557 }
558
559 /*****************************************************************************/
560
561 dng_fingerprint dng_camera_profile::CalculateFingerprint (bool renderDataOnly) const
562 {
563
564 DNG_ASSERT (!fWasStubbed, "CalculateFingerprint on stubbed profile");
565
566 dng_md5_printer_stream printer;
567
568 // MD5 hash is always calculated on little endian data.
569
570 printer.SetLittleEndian ();
571
572 // The data that we fingerprint closely matches that saved
573 // by the profile_tag_set class in dng_image_writer.cpp, with
574 // the exception of the fingerprint itself.
575
576 if (HasColorMatrix1 ())
577 {
578
579 uint32 colorChannels = ColorMatrix1 ().Rows ();
580
581 printer.Put_uint16 ((uint16) fCalibrationIlluminant1);
582
583 FingerprintMatrix (printer, fColorMatrix1);
584
585 if (fForwardMatrix1.Rows () == fColorMatrix1.Cols () &&
586 fForwardMatrix1.Cols () == fColorMatrix1.Rows ())
587 {
588
589 FingerprintMatrix (printer, fForwardMatrix1);
590
591 }
592
593 if (colorChannels > 3 && fReductionMatrix1.Rows () *
594 fReductionMatrix1.Cols () == colorChannels * 3)
595 {
596
597 FingerprintMatrix (printer, fReductionMatrix1);
598
599 }
600
601 // Only include 2nd color matrix if we have 1st color matrix.
602
603 if (HasColorMatrix2 ())
604 {
605
606 printer.Put_uint16 ((uint16) fCalibrationIlluminant2);
607
608 FingerprintMatrix (printer, fColorMatrix2);
609
610 if (fForwardMatrix2.Rows () == fColorMatrix2.Cols () &&
611 fForwardMatrix2.Cols () == fColorMatrix2.Rows ())
612 {
613
614 FingerprintMatrix (printer, fForwardMatrix2);
615
616 }
617
618 if (colorChannels > 3 && fReductionMatrix2.Rows () *
619 fReductionMatrix2.Cols () == colorChannels * 3)
620 {
621
622 FingerprintMatrix (printer, fReductionMatrix2);
623
624 }
625
626 // Only include 3rd color matrix if we have 2nd color matrix
627 // (which means we also have the 1st color matrix).
628
629 if (HasColorMatrix3 ())
630 {
631
632 printer.Put_uint16 ((uint16) fCalibrationIlluminant3);
633
634 FingerprintMatrix (printer, fColorMatrix3);
635
636 if (fForwardMatrix3.Rows () == fColorMatrix3.Cols () &&
637 fForwardMatrix3.Cols () == fColorMatrix3.Rows ())
638 {
639
640 FingerprintMatrix (printer, fForwardMatrix3);
641
642 }
643
644 if (colorChannels > 3 && fReductionMatrix3.Rows () *
645 fReductionMatrix3.Cols () == colorChannels * 3)
646 {
647
648 FingerprintMatrix (printer, fReductionMatrix3);
649
650 }
651
652 } // has color matrix 3
653
654 } // has color matrix 2
655
656 if (!renderDataOnly)
657 {
658
659 printer.Put (fName.Get (),
660 fName.Length ());
661
662 printer.Put (fGroupName.Get (),
663 fGroupName.Length ());
664
665 }
666
667 printer.Put (fProfileCalibrationSignature.Get (),
668 fProfileCalibrationSignature.Length ());
669
670 if (!renderDataOnly)
671 {
672
673 printer.Put_uint32 (fEmbedPolicy);
674
675 printer.Put (fCopyright.Get (),
676 fCopyright.Length ());
677
678 }
679
680 bool haveHueSat1 = HueSatDeltas1 ().IsValid ();
681
682 bool haveHueSat2 = HueSatDeltas2 ().IsValid () &&
683 HasColorMatrix2 ();
684
685 bool haveHueSat3 = HueSatDeltas3 ().IsValid () &&
686 HasColorMatrix3 ();
687
688 if (haveHueSat1)
689 {
690
691 FingerprintHueSatMap (printer, fHueSatDeltas1);
692
693 }
694
695 if (haveHueSat2)
696 {
697
698 FingerprintHueSatMap (printer, fHueSatDeltas2);
699
700 }
701
702 if (haveHueSat3)
703 {
704
705 FingerprintHueSatMap (printer, fHueSatDeltas3);
706
707 }
708
709 if (haveHueSat1 || haveHueSat2 || haveHueSat3)
710 {
711
712 if (fHueSatMapEncoding != 0)
713 {
714
715 printer.Put_uint32 (fHueSatMapEncoding);
716
717 }
718
719 }
720
721 if (fLookTable.IsValid ())
722 {
723
724 FingerprintHueSatMap (printer, fLookTable);
725
726 if (fLookTableEncoding != 0)
727 {
728
729 printer.Put_uint32 (fLookTableEncoding);
730
731 }
732
733 }
734
735 if (fBaselineExposureOffset.IsValid ())
736 {
737
738 if (fBaselineExposureOffset.As_real64 () != 0.0)
739 {
740
741 printer.Put_real64 (fBaselineExposureOffset.As_real64 ());
742
743 }
744
745 }
746
747 if (fDefaultBlackRender != 0)
748 {
749
750 printer.Put_int32 (fDefaultBlackRender);
751
752 }
753
754 if (fToneCurve.IsValid ())
755 {
756
757 for (uint32 i = 0; i < fToneCurve.fCoord.size (); i++)
758 {
759
760 printer.Put_real32 ((real32) fToneCurve.fCoord [i].h);
761 printer.Put_real32 ((real32) fToneCurve.fCoord [i].v);
762
763 }
764
765 }
766
767 if (fToneMethod != profileToneMethod_Unspecified)
768 {
769
770 printer.Put_int32 (fToneMethod);
771
772 }
773
774 }
775
776 // ProfileGainTableMap.
777
778 {
779
780 auto pgtm = ShareProfileGainTableMap ();
781
782 if (pgtm)
783 {
784
785 dng_fingerprint digest = pgtm->GetFingerprint ();
786
787 printer.Put (digest.data,
788 uint32 (sizeof (digest.data)));
789
790 }
791
792 }
793
794 // ProfileDynamicRange.
795
796 {
797
798 const auto &range = DynamicRangeInfo ();
799
800 if (range.IsHDR ())
801 {
802
803 printer.Put ("hdr", 3);
804
805 if (range.fHintMaxOutputValue != 1.0f)
806 printer.Put (&range.fHintMaxOutputValue,
807 uint32 (sizeof (range.fHintMaxOutputValue)));
808
809 }
810
811 }
812
813 // RGBTables.
814
815 if (HasMaskedRGBTables ())
816 {
817
818 dng_md5_printer rgbTablesPrinter;
819
820 MaskedRGBTables ().AddDigest (rgbTablesPrinter);
821
822 auto rgbTableDigest = rgbTablesPrinter.Result ();
823
824 printer.Put (rgbTableDigest.data,
825 uint32 (sizeof (rgbTableDigest.data)));
826
827 }
828
829 return printer.Result ();
830
831 }
832
833 /******************************************************************************/
834
835 dng_fingerprint dng_camera_profile::UniqueID () const
836 {
837
838 dng_md5_printer_stream printer;
839
840 // MD5 hash is always calculated on little endian data.
841
842 printer.SetLittleEndian ();
843
844 // Start with the existing fingerprint.
845
846 dng_fingerprint fingerprint = Fingerprint ();
847
848 printer.Put (fingerprint.data,
849 (uint32) sizeof (fingerprint.data));
850
851 // Also include the UniqueCameraModelRestriction tag.
852
853 printer.Put (fUniqueCameraModelRestriction.Get (),
854 fUniqueCameraModelRestriction.Length ());
855
856 // Add any other needed fields here.
857
858 // ...
859
860 return printer.Result ();
861
862 }
863
864 /******************************************************************************/
865
866 bool dng_camera_profile::ValidForwardMatrix (const dng_matrix &m)
867 {
868
869 const real64 kThreshold = 0.01;
870
871 if (m.NotEmpty ())
872 {
873
874 dng_vector cameraOne;
875
876 cameraOne.SetIdentity (m.Cols ());
877
878 dng_vector xyz = m * cameraOne;
879
880 dng_vector pcs = PCStoXYZ ();
881
882 if (Abs_real64 (xyz [0] - pcs [0]) > kThreshold ||
883 Abs_real64 (xyz [1] - pcs [1]) > kThreshold ||
884 Abs_real64 (xyz [2] - pcs [2]) > kThreshold)
885 {
886
887 return false;
888
889 }
890
891 }
892
893 return true;
894
895 }
896
897 /******************************************************************************/
898
899 bool dng_camera_profile::IsValid (uint32 channels) const
900 {
901
902 bool hasFirstTwoColorMatrices = false;
903
904 bool hasThreeColorMatrices = false;
905
906 // For Monochrome images, we ignore the camera profile.
907
908 if (channels == 1)
909 {
910
911 return true;
912
913 }
914
915 // ColorMatrix1 is required for all color images.
916
917 if (fColorMatrix1.Cols () != 3 ||
918 fColorMatrix1.Rows () != channels)
919 {
920
921 #if qDNGValidate
922
923 ReportError ("ColorMatrix1 is wrong size");
924
925 #endif
926
927 return false;
928
929 }
930
931 // ColorMatrix2 is optional, but it must be valid if present.
932
933 if (fColorMatrix2.Cols () != 0 ||
934 fColorMatrix2.Rows () != 0)
935 {
936
937 if (fColorMatrix2.Cols () != 3 ||
938 fColorMatrix2.Rows () != channels)
939 {
940
941 #if qDNGValidate
942
943 ReportError ("ColorMatrix2 is wrong size");
944
945 #endif
946
947 return false;
948
949 }
950
951 // If we reached here, it means we have ColorMatrix1 and ColorMatrix2.
952
953 hasFirstTwoColorMatrices = true;
954
955 }
956
957 // ColorMatrix3 is optional, but it must be valid if present.
958
959 if (fColorMatrix3.Cols () != 0 ||
960 fColorMatrix3.Rows () != 0)
961 {
962
963 if (fColorMatrix3.Cols () != 3 ||
964 fColorMatrix3.Rows () != channels)
965 {
966
967 #if qDNGValidate
968
969 ReportError ("ColorMatrix3 is wrong size");
970
971 #endif
972
973 return false;
974
975 }
976
977 // Furthermore, the first two color matrices must also be present.
978
979 if (!hasFirstTwoColorMatrices)
980 {
981
982 #if qDNGValidate
983
984 ReportError ("ColorMatrix3 present without ColorMatrix1/ColorMatrix2");
985
986 #endif
987
988 return false;
989
990 }
991
992 // If we reached here, it means we have all three color matrices defined.
993
994 hasThreeColorMatrices = true;
995
996 }
997
998 // ForwardMatrix1 is optional, but it must be valid if present.
999
1000 if (fForwardMatrix1.Cols () != 0 ||
1001 fForwardMatrix1.Rows () != 0)
1002 {
1003
1004 if (fForwardMatrix1.Rows () != 3 ||
1005 fForwardMatrix1.Cols () != channels)
1006 {
1007
1008 #if qDNGValidate
1009
1010 ReportError ("ForwardMatrix1 is wrong size");
1011
1012 #endif
1013
1014 return false;
1015
1016 }
1017
1018 // Make sure ForwardMatrix1 does a valid mapping.
1019
1020 if (!ValidForwardMatrix (fForwardMatrix1))
1021 {
1022
1023 #if qDNGValidate
1024
1025 ReportError ("ForwardMatrix1 does not map equal camera values to XYZ D50");
1026
1027 #endif
1028
1029 return false;
1030
1031 }
1032
1033 }
1034
1035 // ForwardMatrix2 is optional, but it must be valid if present.
1036
1037 if (fForwardMatrix2.Cols () != 0 ||
1038 fForwardMatrix2.Rows () != 0)
1039 {
1040
1041 if (fForwardMatrix2.Rows () != 3 ||
1042 fForwardMatrix2.Cols () != channels)
1043 {
1044
1045 #if qDNGValidate
1046
1047 ReportError ("ForwardMatrix2 is wrong size");
1048
1049 #endif
1050
1051 return false;
1052
1053 }
1054
1055 // Make sure ForwardMatrix2 does a valid mapping.
1056
1057 if (!ValidForwardMatrix (fForwardMatrix2))
1058 {
1059
1060 #if qDNGValidate
1061
1062 ReportError ("ForwardMatrix2 does not map equal camera values to XYZ D50");
1063
1064 #endif
1065
1066 return false;
1067
1068 }
1069
1070 }
1071
1072 // ForwardMatrix3 is optional, but it must be valid if present.
1073
1074 if (fForwardMatrix3.Cols () != 0 ||
1075 fForwardMatrix3.Rows () != 0)
1076 {
1077
1078 if (fForwardMatrix3.Rows () != 3 ||
1079 fForwardMatrix3.Cols () != channels)
1080 {
1081
1082 #if qDNGValidate
1083
1084 ReportError ("ForwardMatrix3 is wrong size");
1085
1086 #endif
1087
1088 return false;
1089
1090 }
1091
1092 // Make sure ForwardMatrix3 does a valid mapping.
1093
1094 if (!ValidForwardMatrix (fForwardMatrix3))
1095 {
1096
1097 #if qDNGValidate
1098
1099 ReportError ("ForwardMatrix3 does not map equal camera values to XYZ D50");
1100
1101 #endif
1102
1103 return false;
1104
1105 }
1106
1107 // ForwardMatrix3 is only allowed if we have three color matrices.
1108
1109 if (!hasThreeColorMatrices)
1110 {
1111
1112 #if qDNGValidate
1113
1114 ReportError ("ForwardMatrix3 present without three color matrices");
1115
1116 #endif
1117
1118 return false;
1119
1120 }
1121
1122 }
1123
1124 // ReductionMatrix1 is optional, but it must be valid if present.
1125
1126 if (fReductionMatrix1.Cols () != 0 ||
1127 fReductionMatrix1.Rows () != 0)
1128 {
1129
1130 if (fReductionMatrix1.Cols () != channels ||
1131 fReductionMatrix1.Rows () != 3)
1132 {
1133
1134 #if qDNGValidate
1135
1136 ReportError ("ReductionMatrix1 is wrong size");
1137
1138 #endif
1139
1140 return false;
1141
1142 }
1143
1144 }
1145
1146 // ReductionMatrix2 is optional, but it must be valid if present.
1147
1148 if (fReductionMatrix2.Cols () != 0 ||
1149 fReductionMatrix2.Rows () != 0)
1150 {
1151
1152 if (fReductionMatrix2.Cols () != channels ||
1153 fReductionMatrix2.Rows () != 3)
1154 {
1155
1156 #if qDNGValidate
1157
1158 ReportError ("ReductionMatrix2 is wrong size");
1159
1160 #endif
1161
1162 return false;
1163
1164 }
1165
1166 }
1167
1168 // ReductionMatrix3 is optional, but it must be valid if present.
1169
1170 if (fReductionMatrix3.Cols () != 0 ||
1171 fReductionMatrix3.Rows () != 0)
1172 {
1173
1174 if (fReductionMatrix3.Cols () != channels ||
1175 fReductionMatrix3.Rows () != 3)
1176 {
1177
1178 #if qDNGValidate
1179
1180 ReportError ("ReductionMatrix3 is wrong size");
1181
1182 #endif
1183
1184 return false;
1185
1186 }
1187
1188 // ReductionMatrix3 is only allowed if we have three color matrices.
1189
1190 if (!hasThreeColorMatrices)
1191 {
1192
1193 #if qDNGValidate
1194
1195 ReportError ("ReductionMatrix3 present without three color matrices");
1196
1197 #endif
1198
1199 return false;
1200
1201 }
1202
1203 }
1204
1205 // Make sure ColorMatrix1 is invertible.
1206
1207 try
1208 {
1209
1210 if (fReductionMatrix1.NotEmpty ())
1211 {
1212
1213 (void) Invert (fColorMatrix1,
1214 fReductionMatrix1);
1215
1216 }
1217
1218 else
1219 {
1220
1221 (void) Invert (fColorMatrix1);
1222
1223 }
1224
1225 }
1226
1227 catch (...)
1228 {
1229
1230 #if qDNGValidate
1231
1232 ReportError ("ColorMatrix1 is not invertible");
1233
1234 #endif
1235
1236 return false;
1237
1238 }
1239
1240 // Make sure ColorMatrix2 is invertible.
1241
1242 if (fColorMatrix2.NotEmpty ())
1243 {
1244
1245 try
1246 {
1247
1248 if (fReductionMatrix2.NotEmpty ())
1249 {
1250
1251 (void) Invert (fColorMatrix2,
1252 fReductionMatrix2);
1253
1254 }
1255
1256 else
1257 {
1258
1259 (void) Invert (fColorMatrix2);
1260
1261 }
1262
1263 }
1264
1265 catch (...)
1266 {
1267
1268 #if qDNGValidate
1269
1270 ReportError ("ColorMatrix2 is not invertible");
1271
1272 #endif
1273
1274 return false;
1275
1276 }
1277
1278 }
1279
1280 // Make sure ColorMatrix3 is invertible.
1281
1282 if (fColorMatrix3.NotEmpty ())
1283 {
1284
1285 try
1286 {
1287
1288 if (fReductionMatrix3.NotEmpty ())
1289 {
1290
1291 (void) Invert (fColorMatrix3,
1292 fReductionMatrix3);
1293
1294 }
1295
1296 else
1297 {
1298
1299 (void) Invert (fColorMatrix3);
1300
1301 }
1302
1303 }
1304
1305 catch (...)
1306 {
1307
1308 #if qDNGValidate
1309
1310 ReportError ("ColorMatrix3 is not invertible");
1311
1312 #endif
1313
1314 return false;
1315
1316 }
1317
1318 }
1319
1320 // If this is a triple-illuminant profile, then we have some extra
1321 // requirements.
1322
1323 if (IlluminantModel () == 3)
1324 {
1325
1326 // None of the illuminants can be unknown.
1327
1328 if (CalibrationIlluminant1 () == lsUnknown ||
1329 CalibrationIlluminant2 () == lsUnknown ||
1330 CalibrationIlluminant3 () == lsUnknown)
1331 {
1332
1333 #if qDNGValidate
1334
1335 ReportError ("CalibrationIlluminant1/2/3 cannot be unknown for "
1336 "a triple-illuminant profile");
1337
1338 #endif
1339
1340 return false;
1341
1342 }
1343
1344 // All of the illuminants must be distinct.
1345
1346 dng_illuminant_data light1 (CalibrationIlluminant1 (), &IlluminantData1 ());
1347 dng_illuminant_data light2 (CalibrationIlluminant2 (), &IlluminantData2 ());
1348 dng_illuminant_data light3 (CalibrationIlluminant3 (), &IlluminantData3 ());
1349
1350 dng_xy_coord white1 = light1.WhiteXY ();
1351 dng_xy_coord white2 = light2.WhiteXY ();
1352 dng_xy_coord white3 = light3.WhiteXY ();
1353
1354 if (white1 == white2 ||
1355 white1 == white3 ||
1356 white2 == white3)
1357 {
1358
1359 #if qDNGValidate
1360
1361 ReportError ("In a triple-illuminant profile all three illuminants "
1362 "must be distinct");
1363
1364 #endif
1365
1366 return false;
1367
1368 }
1369
1370 // We must have all three color matrices.
1371
1372 if (!HasColorMatrix1 () ||
1373 !HasColorMatrix2 () ||
1374 !HasColorMatrix3 ())
1375 {
1376
1377 #if qDNGValidate
1378
1379 ReportError ("ColorMatrix1/2/3 must all be present and valid for "
1380 "a triple-illuminant profile");
1381
1382 #endif
1383
1384 return false;
1385
1386 }
1387
1388 // ForwardMatrix must be present for all or absent for all.
1389
1390 if (ForwardMatrix1 ().NotEmpty () ||
1391 ForwardMatrix2 ().NotEmpty () ||
1392 ForwardMatrix3 ().NotEmpty ())
1393 {
1394
1395 if (ForwardMatrix1 ().IsEmpty () ||
1396 ForwardMatrix2 ().IsEmpty () ||
1397 ForwardMatrix3 ().IsEmpty ())
1398 {
1399
1400 #if qDNGValidate
1401
1402 ReportError ("For a triple-illuminant profile, ForwardMatrix "
1403 "must be absent for all three illuminants, or "
1404 "present for all three illuminants");
1405
1406 #endif
1407
1408 return false;
1409
1410 }
1411
1412 }
1413
1414 // ReductionMatrix must be present for all or absent for all.
1415
1416 if (ReductionMatrix1 ().NotEmpty () ||
1417 ReductionMatrix2 ().NotEmpty () ||
1418 ReductionMatrix3 ().NotEmpty ())
1419 {
1420
1421 if (ReductionMatrix1 ().IsEmpty () ||
1422 ReductionMatrix2 ().IsEmpty () ||
1423 ReductionMatrix3 ().IsEmpty ())
1424 {
1425
1426 #if qDNGValidate
1427
1428 ReportError ("For a triple-illuminant profile, ReductionMatrix "
1429 "must be absent for all three illuminants, or "
1430 "present for all three illuminants");
1431
1432 #endif
1433
1434 return false;
1435
1436 }
1437
1438 }
1439
1440 // Hue sat map must be present for all or absent for all.
1441
1442 if (HueSatDeltas1 ().IsValid () ||
1443 HueSatDeltas2 ().IsValid () ||
1444 HueSatDeltas3 ().IsValid ())
1445 {
1446
1447 if (HueSatDeltas1 ().IsNull () ||
1448 HueSatDeltas2 ().IsNull () ||
1449 HueSatDeltas3 ().IsNull ())
1450 {
1451
1452 #if qDNGValidate
1453
1454 ReportError ("For a triple-illuminant profile, HueSatDeltas "
1455 "must be absent for all three illuminants, or "
1456 "present for all three illuminants");
1457
1458 #endif
1459
1460 return false;
1461
1462 }
1463
1464 }
1465
1466 }
1467
1468 return true;
1469
1470 }
1471
1472 /*****************************************************************************/
1473
1474 void dng_camera_profile::ReadHueSatMap (dng_stream &stream,
1475 dng_hue_sat_map &hueSatMap,
1476 uint32 hues,
1477 uint32 sats,
1478 uint32 vals,
1479 bool skipSat0)
1480 {
1481
1482 hueSatMap.SetDivisions (hues, sats, vals);
1483
1484 for (uint32 val = 0; val < vals; val++)
1485 {
1486
1487 for (uint32 hue = 0; hue < hues; hue++)
1488 {
1489
1490 for (uint32 sat = skipSat0 ? 1 : 0; sat < sats; sat++)
1491 {
1492
1493 dng_hue_sat_map::HSBModify modify;
1494
1495 modify.fHueShift = stream.Get_real32 ();
1496 modify.fSatScale = stream.Get_real32 ();
1497 modify.fValScale = stream.Get_real32 ();
1498
1499 hueSatMap.SetDelta (hue, sat, val, modify);
1500
1501 }
1502
1503 }
1504
1505 }
1506
1507 hueSatMap.AssignNewUniqueRuntimeFingerprint ();
1508
1509 }
1510
1511 /*****************************************************************************/
1512
1513 void dng_camera_profile::Parse (dng_stream &stream,
1514 dng_camera_profile_info &profileInfo)
1515 {
1516
1517 SetUniqueCameraModelRestriction (profileInfo.fUniqueCameraModel.Get ());
1518
1519 if (profileInfo.fProfileName.NotEmpty ())
1520 {
1521
1522 SetName (profileInfo.fProfileName.Get ());
1523
1524 }
1525
1526 SetCopyright (profileInfo.fProfileCopyright.Get ());
1527
1528 SetEmbedPolicy (profileInfo.fEmbedPolicy);
1529
1530 SetCalibrationIlluminant1 (profileInfo.fCalibrationIlluminant1);
1531
1532 SetColorMatrix1 (profileInfo.fColorMatrix1);
1533
1534 if (profileInfo.fForwardMatrix1.NotEmpty ())
1535 {
1536
1537 SetForwardMatrix1 (profileInfo.fForwardMatrix1);
1538
1539 }
1540
1541 if (profileInfo.fReductionMatrix1.NotEmpty ())
1542 {
1543
1544 SetReductionMatrix1 (profileInfo.fReductionMatrix1);
1545
1546 }
1547
1548 if (CalibrationIlluminant1 () == lsOther)
1549 {
1550
1551 SetIlluminantData1 (profileInfo.fIlluminantData1);
1552
1553 }
1554
1555 // Deal with 2nd illuminant.
1556
1557 if (profileInfo.fColorMatrix2.NotEmpty ())
1558 {
1559
1560 SetCalibrationIlluminant2 (profileInfo.fCalibrationIlluminant2);
1561
1562 SetColorMatrix2 (profileInfo.fColorMatrix2);
1563
1564 if (profileInfo.fForwardMatrix2.NotEmpty ())
1565 {
1566
1567 SetForwardMatrix2 (profileInfo.fForwardMatrix2);
1568
1569 }
1570
1571 if (profileInfo.fReductionMatrix2.NotEmpty ())
1572 {
1573
1574 SetReductionMatrix2 (profileInfo.fReductionMatrix2);
1575
1576 }
1577
1578 if (CalibrationIlluminant2 () == lsOther)
1579 {
1580
1581 SetIlluminantData2 (profileInfo.fIlluminantData2);
1582
1583 }
1584
1585 }
1586
1587 // Deal with 3rd illuminant.
1588
1589 if (profileInfo.fColorMatrix3.NotEmpty ())
1590 {
1591
1592 SetCalibrationIlluminant3 (profileInfo.fCalibrationIlluminant3);
1593
1594 SetColorMatrix3 (profileInfo.fColorMatrix3);
1595
1596 if (profileInfo.fForwardMatrix3.NotEmpty ())
1597 {
1598
1599 SetForwardMatrix3 (profileInfo.fForwardMatrix3);
1600
1601 }
1602
1603 if (profileInfo.fReductionMatrix3.NotEmpty ())
1604 {
1605
1606 SetReductionMatrix3 (profileInfo.fReductionMatrix3);
1607
1608 }
1609
1610 if (CalibrationIlluminant3 () == lsOther)
1611 {
1612
1613 SetIlluminantData3 (profileInfo.fIlluminantData3);
1614
1615 }
1616
1617 }
1618
1619 SetProfileCalibrationSignature (profileInfo.fProfileCalibrationSignature.Get ());
1620
1621 if (profileInfo.fHueSatDeltas1Offset != 0 &&
1622 profileInfo.fHueSatDeltas1Count != 0)
1623 {
1624
1625 TempBigEndian setEndianness (stream, profileInfo.fBigEndian);
1626
1627 stream.SetReadPosition (profileInfo.fHueSatDeltas1Offset);
1628
1629 bool skipSat0 = (profileInfo.fHueSatDeltas1Count ==
1630 SafeUint32Mult (profileInfo.fProfileHues,
1631 SafeUint32Sub (profileInfo.fProfileSats, 1),
1632 profileInfo.fProfileVals, 3));
1633
1634 ReadHueSatMap (stream,
1635 fHueSatDeltas1,
1636 profileInfo.fProfileHues,
1637 profileInfo.fProfileSats,
1638 profileInfo.fProfileVals,
1639 skipSat0);
1640
1641 }
1642
1643 if (profileInfo.fHueSatDeltas2Offset != 0 &&
1644 profileInfo.fHueSatDeltas2Count != 0)
1645 {
1646
1647 TempBigEndian setEndianness (stream, profileInfo.fBigEndian);
1648
1649 stream.SetReadPosition (profileInfo.fHueSatDeltas2Offset);
1650
1651 bool skipSat0 = (profileInfo.fHueSatDeltas2Count ==
1652 SafeUint32Mult (profileInfo.fProfileHues,
1653 SafeUint32Sub (profileInfo.fProfileSats, 1),
1654 profileInfo.fProfileVals, 3));
1655
1656 ReadHueSatMap (stream,
1657 fHueSatDeltas2,
1658 profileInfo.fProfileHues,
1659 profileInfo.fProfileSats,
1660 profileInfo.fProfileVals,
1661 skipSat0);
1662
1663 }
1664
1665 if (profileInfo.fHueSatDeltas3Offset != 0 &&
1666 profileInfo.fHueSatDeltas3Count != 0)
1667 {
1668
1669 TempBigEndian setEndianness (stream, profileInfo.fBigEndian);
1670
1671 stream.SetReadPosition (profileInfo.fHueSatDeltas3Offset);
1672
1673 bool skipSat0 = (profileInfo.fHueSatDeltas3Count == profileInfo.fProfileHues *
1674 (profileInfo.fProfileSats - 1) *
1675 profileInfo.fProfileVals * 3);
1676
1677 ReadHueSatMap (stream,
1678 fHueSatDeltas3,
1679 profileInfo.fProfileHues,
1680 profileInfo.fProfileSats,
1681 profileInfo.fProfileVals,
1682 skipSat0);
1683
1684 }
1685
1686 if (profileInfo.fLookTableOffset != 0 &&
1687 profileInfo.fLookTableCount != 0)
1688 {
1689
1690 TempBigEndian setEndianness (stream, profileInfo.fBigEndian);
1691
1692 stream.SetReadPosition (profileInfo.fLookTableOffset);
1693
1694 bool skipSat0 = (profileInfo.fLookTableCount ==
1695 SafeUint32Mult (profileInfo.fLookTableHues,
1696 SafeUint32Sub (profileInfo.fLookTableSats, 1),
1697 profileInfo.fLookTableVals, 3));
1698
1699 ReadHueSatMap (stream,
1700 fLookTable,
1701 profileInfo.fLookTableHues,
1702 profileInfo.fLookTableSats,
1703 profileInfo.fLookTableVals,
1704 skipSat0);
1705
1706 }
1707
1708 if ((profileInfo.fToneCurveCount & 1) == 0)
1709 {
1710
1711 TempBigEndian setEndianness (stream, profileInfo.fBigEndian);
1712
1713 stream.SetReadPosition (profileInfo.fToneCurveOffset);
1714
1715 uint32 points = profileInfo.fToneCurveCount / 2;
1716
1717 if (points > kMaxToneCurvePoints)
1718 {
1719 ThrowProgramError ("Too many tone curve points");
1720 }
1721
1722 fToneCurve.fCoord.resize (points);
1723
1724 for (size_t i = 0; i < points; i++)
1725 {
1726
1727 dng_point_real64 point;
1728
1729 point.h = stream.Get_real32 ();
1730 point.v = stream.Get_real32 ();
1731
1732 fToneCurve.fCoord [i] = point;
1733
1734 }
1735
1736 }
1737
1738 SetToneMethod (profileInfo.fToneMethod);
1739
1740 SetHueSatMapEncoding (profileInfo.fHueSatMapEncoding);
1741
1742 SetLookTableEncoding (profileInfo.fLookTableEncoding);
1743
1744 SetBaselineExposureOffset (profileInfo.fBaselineExposureOffset.As_real64 ());
1745
1746 SetDefaultBlackRender (profileInfo.fDefaultBlackRender);
1747
1748 SetProfileGainTableMap (profileInfo.fProfileGainTableMap);
1749
1750 SetGroupName (profileInfo.fProfileGroupName);
1751
1752 SetDynamicRangeInfo (profileInfo.fProfileDynamicRange);
1753
1754 SetMaskedRGBTables (profileInfo.fMaskedRGBTables);
1755
1756 }
1757
1758 /*****************************************************************************/
1759
1760 bool dng_camera_profile::ParseExtended (dng_stream &stream)
1761 {
1762
1763 try
1764 {
1765
1766 dng_camera_profile_info profileInfo;
1767
1768 if (!profileInfo.ParseExtended (stream))
1769 {
1770 return false;
1771 }
1772
1773 Parse (stream, profileInfo);
1774
1775 return true;
1776
1777 }
1778
1779 catch (...)
1780 {
1781
1782 // Eat parsing errors.
1783
1784 }
1785
1786 return false;
1787
1788 }
1789
1790 /*****************************************************************************/
1791
1792 void dng_camera_profile::SetFourColorBayer ()
1793 {
1794
1795 uint32 j;
1796
1797 if (!IsValid (3))
1798 {
1799 ThrowProgramError ();
1800 }
1801
1802 if (fColorMatrix1.NotEmpty ())
1803 {
1804
1805 dng_matrix m (4, 3);
1806
1807 for (j = 0; j < 3; j++)
1808 {
1809 m [0] [j] = fColorMatrix1 [0] [j];
1810 m [1] [j] = fColorMatrix1 [1] [j];
1811 m [2] [j] = fColorMatrix1 [2] [j];
1812 m [3] [j] = fColorMatrix1 [1] [j];
1813 }
1814
1815 fColorMatrix1 = m;
1816
1817 }
1818
1819 if (fColorMatrix2.NotEmpty ())
1820 {
1821
1822 dng_matrix m (4, 3);
1823
1824 for (j = 0; j < 3; j++)
1825 {
1826 m [0] [j] = fColorMatrix2 [0] [j];
1827 m [1] [j] = fColorMatrix2 [1] [j];
1828 m [2] [j] = fColorMatrix2 [2] [j];
1829 m [3] [j] = fColorMatrix2 [1] [j];
1830 }
1831
1832 fColorMatrix2 = m;
1833
1834 }
1835
1836 if (fColorMatrix3.NotEmpty ())
1837 {
1838
1839 dng_matrix m (4, 3);
1840
1841 for (j = 0; j < 3; j++)
1842 {
1843 m [0] [j] = fColorMatrix3 [0] [j];
1844 m [1] [j] = fColorMatrix3 [1] [j];
1845 m [2] [j] = fColorMatrix3 [2] [j];
1846 m [3] [j] = fColorMatrix3 [1] [j];
1847 }
1848
1849 fColorMatrix3 = m;
1850
1851 }
1852
1853 fReductionMatrix1.Clear ();
1854 fReductionMatrix2.Clear ();
1855 fReductionMatrix3.Clear ();
1856
1857 fForwardMatrix1.Clear ();
1858 fForwardMatrix2.Clear ();
1859 fForwardMatrix3.Clear ();
1860
1861 }
1862
1863 /*****************************************************************************/
1864
1865 dng_hue_sat_map * dng_camera_profile::HueSatMapForWhite (const dng_xy_coord &white) const
1866 {
1867
1868 if (fHueSatDeltas1.IsValid ())
1869 {
1870
1871 // If we only have the first table, just use it for any color temperature.
1872
1873 if (!fHueSatDeltas2.IsValid ())
1874 {
1875
1876 return new dng_hue_sat_map (fHueSatDeltas1);
1877
1878 }
1879
1880 // We have table 1 and table 2.
1881
1882 if (IlluminantModel () == 3)
1883 {
1884
1885 // This means we also have a 3rd hue sat map, since all
1886 // three are required to be present or absent for a
1887 // triple-illuminant profile.
1888
1889 return HueSatMapForWhite_Triple (white);
1890
1891 }
1892
1893 else
1894 {
1895
1896 // Dual-illuminant model.
1897
1898 return HueSatMapForWhite_Dual (white);
1899
1900 }
1901
1902 }
1903
1904 return nullptr;
1905
1906 }
1907
1908 /*****************************************************************************/
1909
1910 dng_hue_sat_map *
1911 dng_camera_profile::HueSatMapForWhite_Dual (const dng_xy_coord &white) const
1912 {
1913
1914 DNG_REQUIRE (fHueSatDeltas1.IsValid () &&
1915 fHueSatDeltas2.IsValid (),
1916 "Bad hue sat map deltas 1 or 2");
1917
1918 // Interpolate based on color temperature.
1919
1920 real64 temperature1 = CalibrationTemperature1 ();
1921 real64 temperature2 = CalibrationTemperature2 ();
1922
1923 if (temperature1 <= 0.0 ||
1924 temperature2 <= 0.0 ||
1925 temperature1 == temperature2)
1926 {
1927
1928 return new dng_hue_sat_map (fHueSatDeltas1);
1929
1930 }
1931
1932 bool reverseOrder = temperature1 > temperature2;
1933
1934 if (reverseOrder)
1935 {
1936 real64 temp = temperature1;
1937 temperature1 = temperature2;
1938 temperature2 = temp;
1939 }
1940
1941 // Convert to temperature/offset space.
1942
1943 dng_temperature td (white);
1944
1945 // Find fraction to weight the first calibration.
1946
1947 real64 g;
1948
1949 if (td.Temperature () <= temperature1)
1950 g = 1.0;
1951
1952 else if (td.Temperature () >= temperature2)
1953 g = 0.0;
1954
1955 else
1956 {
1957
1958 real64 invT = 1.0 / td.Temperature ();
1959
1960 g = (invT - (1.0 / temperature2)) /
1961 ((1.0 / temperature1) - (1.0 / temperature2));
1962
1963 }
1964
1965 // Fix up if we swapped the order.
1966
1967 if (reverseOrder)
1968 {
1969 g = 1.0 - g;
1970 }
1971
1972 // Do the interpolation.
1973
1974 return dng_hue_sat_map::Interpolate (HueSatDeltas1 (),
1975 HueSatDeltas2 (),
1976 g);
1977
1978 }
1979
1980 /*****************************************************************************/
1981
1982 dng_hue_sat_map *
1983 dng_camera_profile::HueSatMapForWhite_Triple (const dng_xy_coord &white) const
1984 {
1985
1986 DNG_REQUIRE (fHueSatDeltas1.IsValid () &&
1987 fHueSatDeltas2.IsValid () &&
1988 fHueSatDeltas3.IsValid (),
1989 "Bad hue sat map deltas 1 or 2 or 3");
1990
1991 real64 w1, w2, w3;
1992
1993 CalculateTripleIlluminantWeights
1994 (white,
1995 dng_illuminant_data (CalibrationIlluminant1 (), &IlluminantData1 ()),
1996 dng_illuminant_data (CalibrationIlluminant2 (), &IlluminantData2 ()),
1997 dng_illuminant_data (CalibrationIlluminant3 (), &IlluminantData3 ()),
1998 w1,
1999 w2,
2000 w3);
2001
2002 return dng_hue_sat_map::Interpolate (HueSatDeltas1 (),
2003 HueSatDeltas2 (),
2004 HueSatDeltas3 (),
2005 w1,
2006 w2);
2007
2008 }
2009
2010 /*****************************************************************************/
2011
2012 void dng_camera_profile::Stub ()
2013 {
2014
2015 (void) Fingerprint ();
2016
2017 (void) RenderDataFingerprint ();
2018
2019 dng_hue_sat_map nullTable;
2020
2021 fHueSatDeltas1 = nullTable;
2022 fHueSatDeltas2 = nullTable;
2023 fHueSatDeltas3 = nullTable;
2024
2025 fLookTable = nullTable;
2026
2027 fToneCurve.SetInvalid ();
2028
2029 fWasStubbed = true;
2030
2031 }
2032
2033 /*****************************************************************************/
2034
2035 bool dng_camera_profile::HasProfileGainTableMap () const
2036 {
2037
2038 return fProfileGainTableMap != nullptr;
2039
2040 }
2041
2042 /*****************************************************************************/
2043
2044 void dng_camera_profile::SetProfileGainTableMap
2045 (const std::shared_ptr<const dng_gain_table_map> &gainTableMap)
2046 {
2047
2048 fProfileGainTableMap = gainTableMap;
2049
2050 }
2051
2052 /*****************************************************************************/
2053
2054 const dng_camera_profile_dynamic_range & dng_camera_profile::DynamicRangeInfo () const
2055 {
2056
2057 if (fDynamicRangeInfo)
2058 return *fDynamicRangeInfo;
2059
2060 static const dng_camera_profile_dynamic_range sNoInfo;
2061
2062 return sNoInfo;
2063
2064 }
2065
2066 /*****************************************************************************/
2067
2068 bool dng_camera_profile::IsSDR () const
2069 {
2070
2071 return !IsHDR ();
2072
2073 }
2074
2075 /*****************************************************************************/
2076
2077 bool dng_camera_profile::IsHDR () const
2078 {
2079
2080 return DynamicRangeInfo ().IsHDR ();
2081
2082 }
2083
2084 /*****************************************************************************/
2085
2086 void dng_camera_profile::SetDynamicRangeInfo (const dng_camera_profile_dynamic_range &info)
2087 {
2088
2089 fDynamicRangeInfo.reset (new dng_camera_profile_dynamic_range (info));
2090
2091 ClearFingerprint ();
2092
2093 }
2094
2095 /*****************************************************************************/
2096
2097 bool dng_camera_profile::HasMaskedRGBTables () const
2098 {
2099
2100 return fMaskedRGBTables != nullptr;
2101
2102 }
2103
2104 /*****************************************************************************/
2105
2106 const dng_masked_rgb_tables & dng_camera_profile::MaskedRGBTables () const
2107 {
2108
2109 DNG_REQUIRE (HasMaskedRGBTables (), "Missing masked RGBTables");
2110
2111 return *fMaskedRGBTables;
2112
2113 }
2114
2115 /*****************************************************************************/
2116
2117 void dng_camera_profile::SetMaskedRGBTables
2118 (const std::shared_ptr<const dng_masked_rgb_tables> &maskedRGBTables)
2119 {
2120
2121 fMaskedRGBTables = maskedRGBTables;
2122
2123 }
2124
2125 /*****************************************************************************/
2126
2127 void dng_camera_profile::SetMaskedRGBTables
2128 (AutoPtr<dng_masked_rgb_tables> &maskedRGBTables)
2129 {
2130
2131 fMaskedRGBTables.reset (maskedRGBTables.Release ());
2132
2133 }
2134
2135 /*****************************************************************************/
2136
2137 bool dng_camera_profile::Uses_1_6_Features () const
2138 {
2139
2140 // If we require a DNG 1.6 reader, then we're obviously using 1.6 features.
2141
2142 if (Requires_1_6_Reader ())
2143 {
2144 return true;
2145 }
2146
2147 // Is this a triple-illuminant model?
2148
2149 if (IlluminantModel () == 3)
2150 {
2151 return true;
2152 }
2153
2154 // Don't bother checking the individual tags like ForwardMatrix3. If we
2155 // consider it a triple-illuminant profile, then we'll already have returned
2156 // true -- see above. If we don't, then the extra tags don't matter.
2157
2158 return false;
2159
2160 }
2161
2162 /******************************************************************************/
2163
2164 bool dng_camera_profile::Requires_1_6_Reader () const
2165 {
2166
2167 // The only change in DNG 1.6 that breaks compatibility with older readers
2168 // is the ability to specify custom data for illuminants 1 and 2.
2169
2170 if (CalibrationIlluminant1 () == lsOther &&
2171 IlluminantData1 ().WhiteXY ().IsValid ())
2172 {
2173 return true;
2174 }
2175
2176 if (CalibrationIlluminant2 () == lsOther &&
2177 IlluminantData2 ().WhiteXY ().IsValid ())
2178 {
2179 return true;
2180 }
2181
2182 return false;
2183
2184 }
2185
2186 /*****************************************************************************/
2187
2188 bool dng_camera_profile::Uses_1_7_Features () const
2189 {
2190
2191 // Do we have a ProfileGainTableMap?
2192
2193 if (HasProfileGainTableMap ())
2194 return true;
2195
2196 // Do we have RGBTables?
2197
2198 if (HasMaskedRGBTables ())
2199 return true;
2200
2201 // Is this a HDR profile?
2202
2203 if (DynamicRangeInfo ().IsValid () &&
2204 DynamicRangeInfo ().IsHDR ())
2205 return true;
2206
2207 return false;
2208
2209 }
2210
2211 /******************************************************************************/
2212
2213 dng_camera_profile_metadata::dng_camera_profile_metadata
2214 (const dng_camera_profile &profile,
2215 int32 index)
2216
2217 : fProfileID (profile.ProfileID ())
2218
2219 , fGroupName (profile.GroupName ())
2220
2221 , fHDR (profile.DynamicRangeInfo ().IsHDR ())
2222
2223 , fRenderDataFingerprint (profile.RenderDataFingerprint ())
2224
2225 , fIsLegalToEmbed (profile.IsLegalToEmbed ())
2226
2227 , fWasReadFromDNG (profile.WasReadFromDNG ())
2228
2229 , fWasReadFromDisk (profile.WasReadFromDisk ())
2230
2231 , fUniqueID ()
2232
2233 , fFilePath ()
2234
2235 , fReadOnly (true)
2236
2237 , fIndex (index)
2238
2239 {
2240
2241 if (fWasReadFromDisk)
2242 {
2243 fUniqueID = profile.UniqueID ();
2244 }
2245
2246 }
2247
2248 /*****************************************************************************/
2249
2250 bool dng_camera_profile_metadata::operator==
2251 (const dng_camera_profile_metadata &metadata) const
2252 {
2253
2254 return fProfileID == metadata.fProfileID &&
2255 fGroupName == metadata.fGroupName &&
2256 fHDR == metadata.fHDR &&
2257 fRenderDataFingerprint == metadata.fRenderDataFingerprint &&
2258 fIsLegalToEmbed == metadata.fIsLegalToEmbed &&
2259 fWasReadFromDNG == metadata.fWasReadFromDNG &&
2260 fWasReadFromDisk == metadata.fWasReadFromDisk &&
2261 fUniqueID == metadata.fUniqueID &&
2262 fFilePath == metadata.fFilePath &&
2263 fReadOnly == metadata.fReadOnly &&
2264 fIndex == metadata.fIndex;
2265
2266 }
2267
2268 /*****************************************************************************/
2269
2270 void SplitCameraProfileName (const dng_string &name,
2271 dng_string &baseName,
2272 int32 &version)
2273 {
2274
2275 baseName = name;
2276
2277 version = 0;
2278
2279 uint32 len = baseName.Length ();
2280
2281 if (len == 7 && baseName.StartsWith ("ACR ", true))
2282 {
2283
2284 if (name.Get () [len - 3] >= '0' &&
2285 name.Get () [len - 3] <= '9' &&
2286 name.Get () [len - 2] == '.' &&
2287 name.Get () [len - 1] >= '0' &&
2288 name.Get () [len - 1] <= '9')
2289
2290 baseName.Truncate (3);
2291
2292 version = ((int32) (name.Get () [len - 3] - '0')) * 10 +
2293 ((int32) (name.Get () [len - 1] - '0'));
2294
2295 return;
2296
2297 }
2298
2299 if (len > 5 && baseName.EndsWith (" beta"))
2300 {
2301
2302 baseName.Truncate (len - 5);
2303
2304 version += -10;
2305
2306 }
2307
2308 else if (len > 7)
2309 {
2310
2311 char lastChar = name.Get () [len - 1];
2312
2313 if (lastChar >= '0' && lastChar <= '9')
2314 {
2315
2316 dng_string temp = name;
2317
2318 temp.Truncate (len - 1);
2319
2320 if (temp.EndsWith (" beta "))
2321 {
2322
2323 baseName.Truncate (len - 7);
2324
2325 version += ((int32) (lastChar - '0')) - 10;
2326
2327 }
2328
2329 }
2330
2331 }
2332
2333 len = baseName.Length ();
2334
2335 if (len > 3)
2336 {
2337
2338 char lastChar = name.Get () [len - 1];
2339
2340 if (lastChar >= '0' && lastChar <= '9')
2341 {
2342
2343 dng_string temp = name;
2344
2345 temp.Truncate (len - 1);
2346
2347 if (temp.EndsWith (" v"))
2348 {
2349
2350 baseName.Truncate (len - 3);
2351
2352 version += ((int32) (lastChar - '0')) * 100;
2353
2354 }
2355
2356 }
2357
2358 }
2359
2360 }
2361
2362 /*****************************************************************************/
2363
2364 void BuildHueSatMapEncodingTable (dng_memory_allocator &allocator,
2365 uint32 encoding,
2366 AutoPtr<dng_1d_table> &encodeTable,
2367 AutoPtr<dng_1d_table> &decodeTable,
2368 bool subSample)
2369 {
2370
2371 encodeTable.Reset ();
2372 decodeTable.Reset ();
2373
2374 switch (encoding)
2375 {
2376
2377 case encoding_Linear:
2378 {
2379
2380 break;
2381
2382 }
2383
2384 case encoding_sRGB:
2385 {
2386
2387 encodeTable.Reset (new dng_1d_table);
2388 decodeTable.Reset (new dng_1d_table);
2389
2390 const dng_1d_function & curve = dng_function_GammaEncode_sRGB::Get ();
2391
2392 encodeTable->Initialize (allocator,
2393 curve,
2394 subSample);
2395
2396 const dng_1d_inverse inverse (curve);
2397
2398 decodeTable->Initialize (allocator,
2399 inverse,
2400 subSample);
2401
2402 break;
2403
2404 }
2405
2406 default:
2407 {
2408
2409 DNG_REPORT ("Unsupported hue sat map / look table encoding.");
2410
2411 break;
2412
2413 }
2414
2415 }
2416
2417 }
2418
2419 /*****************************************************************************/