Coverage Report

Created: 2026-12-01 06:38

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/dng_sdk/source/dng_camera_profile.cpp
Line
Count
Source
1
/*****************************************************************************/
2
// Copyright 2006-2008 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
/* $Id: //mondo/dng_sdk_1_4/dng_sdk/source/dng_camera_profile.cpp#1 $ */ 
10
/* $DateTime: 2012/05/30 13:28:51 $ */
11
/* $Change: 832332 $ */
12
/* $Author: tknoll $ */
13
14
#include "dng_camera_profile.h"
15
16
#include "dng_1d_table.h"
17
#include "dng_assertions.h"
18
#include "dng_color_space.h"
19
#include "dng_host.h"
20
#include "dng_exceptions.h"
21
#include "dng_image_writer.h"
22
#include "dng_info.h"
23
#include "dng_parse_utils.h"
24
#include "dng_safe_arithmetic.h"
25
#include "dng_tag_codes.h"
26
#include "dng_tag_types.h"
27
#include "dng_temperature.h"
28
#include "dng_xy_coord.h"
29
30
/*****************************************************************************/
31
32
const char * kProfileName_Embedded = "Embedded";
33
34
const char * kAdobeCalibrationSignature = "com.adobe";
35
36
/*****************************************************************************/
37
38
dng_camera_profile::dng_camera_profile ()
39
40
323k
  : fName ()
41
323k
  , fCalibrationIlluminant1 (lsUnknown)
42
323k
  , fCalibrationIlluminant2 (lsUnknown)
43
323k
  , fColorMatrix1 ()
44
323k
  , fColorMatrix2 ()
45
323k
  , fForwardMatrix1 ()
46
323k
  , fForwardMatrix2 ()
47
323k
  , fReductionMatrix1 ()
48
323k
  , fReductionMatrix2 ()
49
323k
  , fFingerprint ()
50
323k
  , fCopyright ()
51
323k
  , fEmbedPolicy (pepAllowCopying)
52
323k
  , fHueSatDeltas1 ()
53
323k
  , fHueSatDeltas2 ()
54
323k
  , fHueSatMapEncoding (encoding_Linear)
55
323k
  , fLookTable ()
56
323k
  , fLookTableEncoding (encoding_Linear)
57
323k
  , fBaselineExposureOffset (0, 100)
58
323k
  , fDefaultBlackRender (defaultBlackRender_Auto)
59
323k
  , fToneCurve ()
60
323k
  , fProfileCalibrationSignature ()
61
323k
  , fUniqueCameraModelRestriction ()
62
323k
  , fWasReadFromDNG (false)
63
323k
  , fWasReadFromDisk (false)
64
323k
  , fWasBuiltinMatrix (false)
65
323k
  , fWasStubbed (false)
66
  
67
323k
  {
68
69
323k
  fToneCurve.SetInvalid ();
70
71
323k
  }
72
73
/*****************************************************************************/
74
75
dng_camera_profile::~dng_camera_profile ()
76
323k
  {
77
  
78
323k
  }
79
80
/*****************************************************************************/
81
82
real64 dng_camera_profile::IlluminantToTemperature (uint32 light)
83
6.00k
  {
84
  
85
6.00k
  switch (light)
86
6.00k
    {
87
    
88
1.35k
    case lsStandardLightA:
89
1.36k
    case lsTungsten:
90
1.36k
      {
91
1.36k
      return 2850.0;
92
1.35k
      }
93
      
94
26
    case lsISOStudioTungsten:
95
26
      {
96
26
      return 3200.0;
97
1.35k
      }
98
      
99
4
    case lsD50:
100
4
      {
101
4
      return 5000.0;
102
1.35k
      }
103
      
104
12
    case lsD55:
105
44
    case lsDaylight:
106
57
    case lsFineWeather:
107
85
    case lsFlash:
108
101
    case lsStandardLightB:
109
101
      {
110
101
      return 5500.0;
111
85
      }
112
      
113
149
    case lsD65:
114
168
    case lsStandardLightC:
115
184
    case lsCloudyWeather:
116
184
      {
117
184
      return 6500.0;
118
168
      }
119
      
120
10
    case lsD75:
121
20
    case lsShade:
122
20
      {
123
20
      return 7500.0;
124
10
      }
125
      
126
4
    case lsDaylightFluorescent:
127
4
      {
128
4
      return (5700.0 + 7100.0) * 0.5;
129
10
      }
130
      
131
8
    case lsDayWhiteFluorescent:
132
8
      {
133
8
      return (4600.0 + 5500.0) * 0.5;
134
10
      }
135
      
136
11
    case lsCoolWhiteFluorescent:
137
23
    case lsFluorescent:
138
23
      {
139
23
      return (3800.0 + 4500.0) * 0.5;
140
11
      }
141
      
142
10
    case lsWhiteFluorescent:
143
10
      {
144
10
      return (3250.0 + 3800.0) * 0.5;
145
11
      }
146
      
147
10
    case lsWarmWhiteFluorescent:
148
10
      {
149
10
      return (2600.0 + 3250.0) * 0.5;
150
11
      }
151
      
152
4.24k
    default:
153
4.24k
      {
154
4.24k
      return 0.0;
155
11
      }
156
      
157
6.00k
    }
158
  
159
6.00k
  }
160
161
/******************************************************************************/
162
163
void dng_camera_profile::NormalizeColorMatrix (dng_matrix &m)
164
291k
  {
165
  
166
291k
  if (m.NotEmpty ())
167
289k
    {
168
  
169
    // Find scale factor to normalize the matrix.
170
    
171
289k
    dng_vector coord = m * PCStoXYZ ();
172
    
173
289k
    real64 maxCoord = coord.MaxEntry ();
174
    
175
289k
    if (maxCoord > 0.0 && (maxCoord < 0.99 || maxCoord > 1.01))
176
266k
      {
177
      
178
266k
      m.Scale (1.0 / maxCoord);
179
      
180
266k
      }
181
      
182
    // Round to four decimal places.
183
    
184
289k
    m.Round (10000);
185
    
186
289k
    }
187
      
188
291k
  }
189
190
/******************************************************************************/
191
192
void dng_camera_profile::SetColorMatrix1 (const dng_matrix &m)
193
211k
  {
194
  
195
211k
  fColorMatrix1 = m;
196
  
197
211k
  NormalizeColorMatrix (fColorMatrix1);
198
199
211k
  ClearFingerprint ();
200
  
201
211k
  }
202
203
/******************************************************************************/
204
205
void dng_camera_profile::SetColorMatrix2 (const dng_matrix &m)
206
80.9k
  {
207
  
208
80.9k
  fColorMatrix2 = m;
209
  
210
80.9k
  NormalizeColorMatrix (fColorMatrix2);
211
  
212
80.9k
  ClearFingerprint ();
213
214
80.9k
  }
215
    
216
/******************************************************************************/
217
218
// Make sure the forward matrix maps to exactly the PCS.
219
220
void dng_camera_profile::NormalizeForwardMatrix (dng_matrix &m)
221
3.17k
  {
222
  
223
3.17k
  if (m.NotEmpty ())
224
39
    {
225
    
226
39
    dng_vector cameraOne;
227
    
228
39
    cameraOne.SetIdentity (m.Cols ());
229
    
230
39
    dng_vector xyz = m * cameraOne;
231
    
232
39
    m = PCStoXYZ ().AsDiagonal () *
233
39
      Invert (xyz.AsDiagonal ()) *
234
39
      m;
235
    
236
39
    }
237
  
238
3.17k
  }
239
240
/******************************************************************************/
241
242
void dng_camera_profile::SetForwardMatrix1 (const dng_matrix &m)
243
6.05k
  {
244
  
245
6.05k
  fForwardMatrix1 = m;
246
  
247
6.05k
  fForwardMatrix1.Round (10000);
248
  
249
6.05k
  ClearFingerprint ();
250
  
251
6.05k
  }
252
253
/******************************************************************************/
254
255
void dng_camera_profile::SetForwardMatrix2 (const dng_matrix &m)
256
7.52k
  {
257
  
258
7.52k
  fForwardMatrix2 = m;
259
  
260
7.52k
  fForwardMatrix2.Round (10000);
261
  
262
7.52k
  ClearFingerprint ();
263
  
264
7.52k
  }
265
266
/*****************************************************************************/
267
268
void dng_camera_profile::SetReductionMatrix1 (const dng_matrix &m)
269
7.26k
  {
270
  
271
7.26k
  fReductionMatrix1 = m;
272
  
273
7.26k
  fReductionMatrix1.Round (10000);
274
  
275
7.26k
  ClearFingerprint ();
276
277
7.26k
  }
278
279
/******************************************************************************/
280
281
void dng_camera_profile::SetReductionMatrix2 (const dng_matrix &m)
282
20.0k
  {
283
  
284
20.0k
  fReductionMatrix2 = m;
285
  
286
20.0k
  fReductionMatrix2.Round (10000);
287
  
288
20.0k
  ClearFingerprint ();
289
290
20.0k
  }
291
292
/*****************************************************************************/
293
294
bool dng_camera_profile::HasColorMatrix1 () const
295
128k
  {
296
  
297
128k
  return fColorMatrix1.Cols () == 3 &&
298
108k
       fColorMatrix1.Rows ()  > 1;
299
  
300
128k
  }
301
    
302
/*****************************************************************************/
303
304
bool dng_camera_profile::HasColorMatrix2 () const
305
112k
  {
306
307
112k
  return fColorMatrix2.Cols () == 3 &&
308
68.9k
       fColorMatrix2.Rows () == fColorMatrix1.Rows ();
309
  
310
112k
  }
311
    
312
/*****************************************************************************/
313
314
void dng_camera_profile::SetHueSatDeltas1 (const dng_hue_sat_map &deltas1)
315
0
  {
316
317
0
  fHueSatDeltas1 = deltas1;
318
319
0
  ClearFingerprint ();
320
321
0
  }
322
323
/*****************************************************************************/
324
325
void dng_camera_profile::SetHueSatDeltas2 (const dng_hue_sat_map &deltas2)
326
0
  {
327
328
0
  fHueSatDeltas2 = deltas2;
329
330
0
  ClearFingerprint ();
331
332
0
  }
333
334
/*****************************************************************************/
335
336
void dng_camera_profile::SetLookTable (const dng_hue_sat_map &table)
337
0
  {
338
339
0
  fLookTable = table;
340
341
0
  ClearFingerprint ();
342
343
0
  }
344
345
/*****************************************************************************/
346
347
static void FingerprintMatrix (dng_md5_printer_stream &printer,
348
                 const dng_matrix &matrix)
349
44.7k
  {
350
351
44.7k
  tag_matrix tag (0, matrix);
352
  
353
  // Tag's Put routine doesn't write the header, only the data
354
355
44.7k
  tag.Put (printer);
356
357
44.7k
  }
358
359
/*****************************************************************************/
360
361
static void FingerprintHueSatMap (dng_md5_printer_stream &printer,
362
                  const dng_hue_sat_map &map)
363
7.76k
  {
364
365
7.76k
  if (map.IsNull ())
366
0
    return;
367
368
7.76k
  uint32 hues;
369
7.76k
  uint32 sats;
370
7.76k
  uint32 vals;
371
372
7.76k
  map.GetDivisions (hues, sats, vals);
373
374
7.76k
  printer.Put_uint32 (hues);
375
7.76k
  printer.Put_uint32 (sats);
376
7.76k
  printer.Put_uint32 (vals);
377
378
4.57M
  for (uint32 val = 0; val < vals; val++)
379
18.2M
    for (uint32 hue = 0; hue < hues; hue++)
380
249M
      for (uint32 sat = 0; sat < sats; sat++)
381
235M
        {
382
383
235M
        dng_hue_sat_map::HSBModify modify;
384
385
235M
        map.GetDelta (hue, sat, val, modify);
386
387
235M
        printer.Put_real32 (modify.fHueShift);
388
235M
        printer.Put_real32 (modify.fSatScale);
389
235M
        printer.Put_real32 (modify.fValScale);
390
391
235M
        }
392
393
7.76k
  }
394
395
/*****************************************************************************/
396
397
void dng_camera_profile::CalculateFingerprint () const
398
32.6k
  {
399
  
400
32.6k
  DNG_ASSERT (!fWasStubbed, "CalculateFingerprint on stubbed profile");
401
402
32.6k
  dng_md5_printer_stream printer;
403
404
  // MD5 hash is always calculated on little endian data.
405
406
32.6k
  printer.SetLittleEndian ();
407
  
408
  // The data that we fingerprint closely matches that saved
409
  // by the profile_tag_set class in dng_image_writer.cpp, with
410
  // the exception of the fingerprint itself.
411
  
412
32.6k
  if (HasColorMatrix1 ())
413
26.5k
    {
414
415
26.5k
    uint32 colorChannels = ColorMatrix1 ().Rows ();
416
    
417
26.5k
    printer.Put_uint16 ((uint16) fCalibrationIlluminant1);
418
419
26.5k
    FingerprintMatrix (printer, fColorMatrix1);
420
    
421
26.5k
    if (fForwardMatrix1.Rows () == fColorMatrix1.Cols () &&
422
3
      fForwardMatrix1.Cols () == fColorMatrix1.Rows ())
423
3
      {
424
      
425
3
      FingerprintMatrix (printer, fForwardMatrix1);
426
      
427
3
      }
428
    
429
26.5k
    if (colorChannels > 3 && fReductionMatrix1.Rows () *
430
60
                 fReductionMatrix1.Cols () == colorChannels * 3)
431
1
      {
432
      
433
1
      FingerprintMatrix (printer, fReductionMatrix1);
434
      
435
1
      }
436
    
437
26.5k
    if (HasColorMatrix2 ())
438
18.1k
      {
439
      
440
18.1k
      printer.Put_uint16 ((uint16) fCalibrationIlluminant2);
441
      
442
18.1k
      FingerprintMatrix (printer, fColorMatrix2);
443
    
444
18.1k
      if (fForwardMatrix2.Rows () == fColorMatrix2.Cols () &&
445
1
        fForwardMatrix2.Cols () == fColorMatrix2.Rows ())
446
1
        {
447
        
448
1
        FingerprintMatrix (printer, fForwardMatrix2);
449
        
450
1
        }
451
    
452
18.1k
      if (colorChannels > 3 && fReductionMatrix2.Rows () *
453
3
                   fReductionMatrix2.Cols () == colorChannels * 3)
454
1
        {
455
        
456
1
        FingerprintMatrix (printer, fReductionMatrix2);
457
        
458
1
        }
459
        
460
18.1k
      }
461
    
462
26.5k
    printer.Put (fName.Get    (),
463
26.5k
           fName.Length ());
464
465
26.5k
    printer.Put (fProfileCalibrationSignature.Get    (),
466
26.5k
           fProfileCalibrationSignature.Length ());
467
468
26.5k
    printer.Put_uint32 (fEmbedPolicy);
469
    
470
26.5k
    printer.Put (fCopyright.Get    (),
471
26.5k
           fCopyright.Length ());
472
           
473
26.5k
    bool haveHueSat1 = HueSatDeltas1 ().IsValid ();
474
    
475
26.5k
    bool haveHueSat2 = HueSatDeltas2 ().IsValid () &&
476
57
               HasColorMatrix2 ();
477
478
26.5k
    if (haveHueSat1)
479
20
      {
480
      
481
20
      FingerprintHueSatMap (printer, fHueSatDeltas1);
482
      
483
20
      }
484
      
485
26.5k
    if (haveHueSat2)
486
44
      {
487
      
488
44
      FingerprintHueSatMap (printer, fHueSatDeltas2);
489
      
490
44
      }
491
492
26.5k
    if (haveHueSat1 || haveHueSat2)
493
60
      {
494
495
60
      if (fHueSatMapEncoding != 0)
496
49
        {
497
498
49
        printer.Put_uint32 (fHueSatMapEncoding);
499
500
49
        }
501
      
502
60
      }
503
      
504
26.5k
    if (fLookTable.IsValid ())
505
7.69k
      {
506
      
507
7.69k
      FingerprintHueSatMap (printer, fLookTable);
508
509
7.69k
      if (fLookTableEncoding != 0)
510
275
        {
511
512
275
        printer.Put_uint32 (fLookTableEncoding);
513
514
275
        }
515
      
516
7.69k
      }
517
518
26.5k
    if (fBaselineExposureOffset.IsValid ())
519
26.5k
      {
520
      
521
26.5k
      if (fBaselineExposureOffset.As_real64 () != 0.0)
522
12.4k
        {
523
524
12.4k
        printer.Put_real64 (fBaselineExposureOffset.As_real64 ());
525
526
12.4k
        }
527
      
528
26.5k
      }
529
530
26.5k
    if (fDefaultBlackRender != 0)
531
2.26k
      {
532
      
533
2.26k
      printer.Put_int32 (fDefaultBlackRender);
534
      
535
2.26k
      }
536
      
537
26.5k
    if (fToneCurve.IsValid ())
538
2.12k
      {
539
      
540
6.70k
      for (uint32 i = 0; i < fToneCurve.fCoord.size (); i++)
541
4.57k
        {
542
        
543
4.57k
        printer.Put_real32 ((real32) fToneCurve.fCoord [i].h);
544
4.57k
        printer.Put_real32 ((real32) fToneCurve.fCoord [i].v);
545
        
546
4.57k
        }
547
        
548
2.12k
      }
549
      
550
26.5k
    }
551
552
32.6k
  fFingerprint = printer.Result ();
553
554
32.6k
  }
555
556
/******************************************************************************/
557
558
bool dng_camera_profile::ValidForwardMatrix (const dng_matrix &m)
559
11.4k
  {
560
  
561
11.4k
  const real64 kThreshold = 0.01;
562
  
563
11.4k
  if (m.NotEmpty ())
564
11.4k
    {
565
    
566
11.4k
    dng_vector cameraOne;
567
    
568
11.4k
    cameraOne.SetIdentity (m.Cols ());
569
    
570
11.4k
    dng_vector xyz = m * cameraOne;
571
    
572
11.4k
    dng_vector pcs = PCStoXYZ ();
573
    
574
11.4k
    if (Abs_real64 (xyz [0] - pcs [0]) > kThreshold ||
575
2.16k
      Abs_real64 (xyz [1] - pcs [1]) > kThreshold ||
576
73
      Abs_real64 (xyz [2] - pcs [2]) > kThreshold)
577
11.3k
      {
578
      
579
11.3k
      return false;
580
      
581
11.3k
      }
582
      
583
11.4k
    }
584
    
585
63
  return true;
586
  
587
11.4k
  }
588
589
/******************************************************************************/
590
591
bool dng_camera_profile::IsValid (uint32 channels) const
592
201k
  {
593
  
594
  // For Monochrome images, we ignore the camera profile.
595
    
596
201k
  if (channels == 1)
597
0
    {
598
    
599
0
    return true;
600
    
601
0
    }
602
    
603
  // ColorMatrix1 is required for all color images.
604
    
605
201k
  if (fColorMatrix1.Cols () != 3 ||
606
198k
    fColorMatrix1.Rows () != channels)
607
4.60k
    {
608
    
609
    #if qDNGValidate
610
  
611
    ReportError ("ColorMatrix1 is wrong size");
612
           
613
    #endif
614
           
615
4.60k
    return false;
616
    
617
4.60k
    }
618
    
619
  // ColorMatrix2 is optional, but it must be valid if present.
620
  
621
196k
  if (fColorMatrix2.Cols () != 0 ||
622
119k
    fColorMatrix2.Rows () != 0)
623
77.6k
    {
624
    
625
77.6k
    if (fColorMatrix2.Cols () != 3 ||
626
77.6k
      fColorMatrix2.Rows () != channels)
627
0
      {
628
      
629
      #if qDNGValidate
630
    
631
      ReportError ("ColorMatrix2 is wrong size");
632
             
633
      #endif
634
           
635
0
      return false;
636
      
637
0
      }
638
    
639
77.6k
    }
640
    
641
  // ForwardMatrix1 is optional, but it must be valid if present.
642
  
643
196k
  if (fForwardMatrix1.Cols () != 0 ||
644
192k
    fForwardMatrix1.Rows () != 0)
645
4.60k
    {
646
    
647
4.60k
    if (fForwardMatrix1.Rows () != 3 ||
648
4.60k
      fForwardMatrix1.Cols () != channels)
649
0
      {
650
      
651
      #if qDNGValidate
652
    
653
      ReportError ("ForwardMatrix1 is wrong size");
654
             
655
      #endif
656
             
657
0
      return false;
658
      
659
0
      }
660
661
    // Make sure ForwardMatrix1 does a valid mapping.
662
    
663
4.60k
    if (!ValidForwardMatrix (fForwardMatrix1))
664
4.55k
      {
665
      
666
      #if qDNGValidate
667
    
668
      ReportError ("ForwardMatrix1 does not map equal camera values to XYZ D50");
669
             
670
      #endif
671
             
672
4.55k
      return false;
673
    
674
4.55k
      }
675
        
676
4.60k
    }
677
678
  // ForwardMatrix2 is optional, but it must be valid if present.
679
  
680
192k
  if (fForwardMatrix2.Cols () != 0 ||
681
185k
    fForwardMatrix2.Rows () != 0)
682
6.82k
    {
683
    
684
6.82k
    if (fForwardMatrix2.Rows () != 3 ||
685
6.82k
      fForwardMatrix2.Cols () != channels)
686
0
      {
687
      
688
      #if qDNGValidate
689
    
690
      ReportError ("ForwardMatrix2 is wrong size");
691
             
692
      #endif
693
             
694
0
      return false;
695
      
696
0
      }
697
698
    // Make sure ForwardMatrix2 does a valid mapping.
699
    
700
6.82k
    if (!ValidForwardMatrix (fForwardMatrix2))
701
6.81k
      {
702
      
703
      #if qDNGValidate
704
    
705
      ReportError ("ForwardMatrix2 does not map equal camera values to XYZ D50");
706
             
707
      #endif
708
             
709
6.81k
      return false;
710
    
711
6.81k
      }
712
        
713
6.82k
    }
714
715
  // ReductionMatrix1 is optional, but it must be valid if present.
716
  
717
185k
  if (fReductionMatrix1.Cols () != 0 ||
718
180k
    fReductionMatrix1.Rows () != 0)
719
4.71k
    {
720
    
721
4.71k
    if (fReductionMatrix1.Cols () != channels ||
722
4.71k
      fReductionMatrix1.Rows () != 3)
723
0
      {
724
      
725
      #if qDNGValidate
726
    
727
      ReportError ("ReductionMatrix1 is wrong size");
728
             
729
      #endif
730
           
731
0
      return false;
732
      
733
0
      }
734
    
735
4.71k
    }
736
  
737
  // ReductionMatrix2 is optional, but it must be valid if present.
738
  
739
185k
  if (fReductionMatrix2.Cols () != 0 ||
740
167k
    fReductionMatrix2.Rows () != 0)
741
17.6k
    {
742
    
743
17.6k
    if (fReductionMatrix2.Cols () != channels ||
744
17.6k
      fReductionMatrix2.Rows () != 3)
745
0
      {
746
      
747
      #if qDNGValidate
748
    
749
      ReportError ("ReductionMatrix2 is wrong size");
750
             
751
      #endif
752
           
753
0
      return false;
754
      
755
0
      }
756
    
757
17.6k
    }
758
    
759
  // Make sure ColorMatrix1 is invertable.
760
  
761
185k
  try
762
185k
    {
763
    
764
185k
    if (fReductionMatrix1.NotEmpty ())
765
4.71k
      {
766
      
767
4.71k
      (void) Invert (fColorMatrix1,
768
4.71k
               fReductionMatrix1);
769
      
770
4.71k
      }
771
      
772
180k
    else
773
180k
      {
774
    
775
180k
      (void) Invert (fColorMatrix1);
776
      
777
180k
      }
778
    
779
185k
    }
780
    
781
185k
  catch (...)
782
185k
    {
783
      
784
    #if qDNGValidate
785
  
786
    ReportError ("ColorMatrix1 is not invertable");
787
           
788
    #endif
789
           
790
3.07k
    return false;
791
  
792
3.07k
    }
793
    
794
  // Make sure ColorMatrix2 is invertable.
795
  
796
182k
  if (fColorMatrix2.NotEmpty ())
797
70.6k
    {
798
            
799
70.6k
    try
800
70.6k
      {
801
      
802
70.6k
      if (fReductionMatrix2.NotEmpty ())
803
17.6k
        {
804
        
805
17.6k
        (void) Invert (fColorMatrix2,
806
17.6k
                 fReductionMatrix2);
807
        
808
17.6k
        }
809
        
810
53.0k
      else
811
53.0k
        {
812
      
813
53.0k
        (void) Invert (fColorMatrix2);
814
        
815
53.0k
        }
816
817
70.6k
      }
818
      
819
70.6k
    catch (...)
820
70.6k
      {
821
        
822
      #if qDNGValidate
823
  
824
      ReportError ("ColorMatrix2 is not invertable");
825
             
826
      #endif
827
             
828
3.86k
      return false;
829
    
830
3.86k
      }
831
      
832
70.6k
    }
833
834
178k
  return true;
835
  
836
182k
  }
837
    
838
/*****************************************************************************/
839
840
bool dng_camera_profile::EqualData (const dng_camera_profile &profile) const
841
7.97M
  {
842
843
7.97M
  return fCalibrationIlluminant1       == profile.fCalibrationIlluminant1        &&
844
7.95M
       fCalibrationIlluminant2       == profile.fCalibrationIlluminant2        &&
845
7.95M
       fColorMatrix1           == profile.fColorMatrix1            &&
846
7.74M
       fColorMatrix2           == profile.fColorMatrix2            &&
847
7.70M
       fForwardMatrix1           == profile.fForwardMatrix1            &&
848
7.70M
       fForwardMatrix2           == profile.fForwardMatrix2            &&
849
7.70M
       fReductionMatrix1         == profile.fReductionMatrix1          &&
850
7.69M
       fReductionMatrix2         == profile.fReductionMatrix2          &&
851
7.69M
       fHueSatDeltas1            == profile.fHueSatDeltas1            &&
852
7.69M
       fHueSatDeltas2            == profile.fHueSatDeltas2            &&
853
7.69M
       fHueSatMapEncoding          == profile.fHueSatMapEncoding          &&
854
7.69M
       fLookTable              == profile.fLookTable              &&
855
7.68M
       fLookTableEncoding          == profile.fLookTableEncoding          &&
856
7.68M
       fDefaultBlackRender         == profile.fDefaultBlackRender          &&
857
7.67M
       fToneCurve              == profile.fToneCurve              &&
858
42.2k
       fBaselineExposureOffset.As_real64 () == profile.fBaselineExposureOffset.As_real64 () &&
859
39.4k
       fProfileCalibrationSignature      == profile.fProfileCalibrationSignature;
860
861
7.97M
  }
862
    
863
/*****************************************************************************/
864
865
void dng_camera_profile::ReadHueSatMap (dng_stream &stream,
866
                    dng_hue_sat_map &hueSatMap,
867
                    uint32 hues,
868
                    uint32 sats,
869
                    uint32 vals,
870
                    bool skipSat0)
871
59.9k
  {
872
873
59.9k
  hueSatMap.SetDivisions (hues, sats, vals);
874
875
9.78G
  for (uint32 val = 0; val < vals; val++)
876
9.78G
    {
877
878
9.78G
    for (uint32 hue = 0; hue < hues; hue++)
879
1.37M
      {
880
881
7.61M
      for (uint32 sat = skipSat0 ? 1 : 0; sat < sats; sat++)
882
6.23M
        {
883
884
6.23M
        dng_hue_sat_map::HSBModify modify;
885
886
6.23M
        modify.fHueShift = stream.Get_real32 ();
887
6.23M
        modify.fSatScale = stream.Get_real32 ();
888
6.23M
        modify.fValScale = stream.Get_real32 ();
889
890
6.23M
        hueSatMap.SetDelta (hue, sat, val, modify);
891
        
892
6.23M
        }
893
        
894
1.37M
      }
895
      
896
9.78G
    }
897
  
898
59.9k
  }
899
900
/*****************************************************************************/
901
902
void dng_camera_profile::Parse (dng_stream &stream,
903
                dng_camera_profile_info &profileInfo)
904
211k
  {
905
  
906
211k
  SetUniqueCameraModelRestriction (profileInfo.fUniqueCameraModel.Get ());
907
908
211k
  if (profileInfo.fProfileName.NotEmpty ())
909
16.4k
    {
910
    
911
16.4k
    SetName (profileInfo.fProfileName.Get ());
912
    
913
16.4k
    }
914
  
915
211k
  SetCopyright (profileInfo.fProfileCopyright.Get ());
916
917
211k
  SetEmbedPolicy (profileInfo.fEmbedPolicy);
918
919
211k
  SetCalibrationIlluminant1 (profileInfo.fCalibrationIlluminant1);
920
      
921
211k
  SetColorMatrix1 (profileInfo.fColorMatrix1);
922
      
923
211k
  if (profileInfo.fForwardMatrix1.NotEmpty ())
924
6.05k
    {
925
    
926
6.05k
    SetForwardMatrix1 (profileInfo.fForwardMatrix1);
927
    
928
6.05k
    }
929
    
930
211k
  if (profileInfo.fReductionMatrix1.NotEmpty ())
931
7.26k
    {
932
    
933
7.26k
    SetReductionMatrix1 (profileInfo.fReductionMatrix1);
934
    
935
7.26k
    }
936
    
937
211k
  if (profileInfo.fColorMatrix2.NotEmpty ())
938
80.9k
    {
939
    
940
80.9k
    SetCalibrationIlluminant2 (profileInfo.fCalibrationIlluminant2);
941
    
942
80.9k
    SetColorMatrix2 (profileInfo.fColorMatrix2);
943
          
944
80.9k
    if (profileInfo.fForwardMatrix2.NotEmpty ())
945
7.52k
      {
946
      
947
7.52k
      SetForwardMatrix2 (profileInfo.fForwardMatrix2);
948
      
949
7.52k
      }
950
    
951
80.9k
    if (profileInfo.fReductionMatrix2.NotEmpty ())
952
20.0k
      {
953
      
954
20.0k
      SetReductionMatrix2 (profileInfo.fReductionMatrix2);
955
      
956
20.0k
      }
957
    
958
80.9k
    }
959
960
211k
  SetProfileCalibrationSignature (profileInfo.fProfileCalibrationSignature.Get ());
961
962
211k
  if (profileInfo.fHueSatDeltas1Offset != 0 &&
963
14.3k
    profileInfo.fHueSatDeltas1Count  != 0)
964
7.68k
    {
965
966
7.68k
    TempBigEndian setEndianness (stream, profileInfo.fBigEndian);
967
968
7.68k
    stream.SetReadPosition (profileInfo.fHueSatDeltas1Offset);
969
    
970
7.68k
    bool skipSat0 = (profileInfo.fHueSatDeltas1Count == SafeUint32Mult(
971
7.68k
                               profileInfo.fProfileHues,
972
7.68k
                               SafeUint32Sub(profileInfo.fProfileSats, 1),
973
7.68k
                               profileInfo.fProfileVals, 3));
974
975
7.68k
    ReadHueSatMap (stream,
976
7.68k
             fHueSatDeltas1,
977
7.68k
             profileInfo.fProfileHues,
978
7.68k
             profileInfo.fProfileSats,
979
7.68k
             profileInfo.fProfileVals,
980
7.68k
             skipSat0);
981
982
7.68k
    }
983
984
211k
  if (profileInfo.fHueSatDeltas2Offset != 0 &&
985
106k
    profileInfo.fHueSatDeltas2Count  != 0)
986
7.25k
    {
987
988
7.25k
    TempBigEndian setEndianness (stream, profileInfo.fBigEndian);
989
990
7.25k
    stream.SetReadPosition (profileInfo.fHueSatDeltas2Offset);
991
992
7.25k
    bool skipSat0 = (profileInfo.fHueSatDeltas2Count == SafeUint32Mult(
993
7.25k
                               profileInfo.fProfileHues,
994
7.25k
                               SafeUint32Sub(profileInfo.fProfileSats, 1),
995
7.25k
                               profileInfo.fProfileVals, 3));
996
997
7.25k
    ReadHueSatMap (stream,
998
7.25k
             fHueSatDeltas2,
999
7.25k
             profileInfo.fProfileHues,
1000
7.25k
             profileInfo.fProfileSats,
1001
7.25k
             profileInfo.fProfileVals,
1002
7.25k
             skipSat0);
1003
1004
7.25k
    }
1005
1006
211k
  if (profileInfo.fLookTableOffset != 0 &&
1007
51.2k
    profileInfo.fLookTableCount  != 0)
1008
49.3k
    {
1009
1010
49.3k
    TempBigEndian setEndianness (stream, profileInfo.fBigEndian);
1011
1012
49.3k
    stream.SetReadPosition (profileInfo.fLookTableOffset);
1013
1014
49.3k
    bool skipSat0 = (profileInfo.fLookTableCount == SafeUint32Mult(
1015
49.3k
                             profileInfo.fLookTableHues,
1016
49.3k
                             SafeUint32Sub(profileInfo.fLookTableSats, 1),
1017
49.3k
                             profileInfo.fLookTableVals, 3));
1018
1019
49.3k
    ReadHueSatMap (stream,
1020
49.3k
             fLookTable,
1021
49.3k
             profileInfo.fLookTableHues,
1022
49.3k
             profileInfo.fLookTableSats,
1023
49.3k
             profileInfo.fLookTableVals,
1024
49.3k
             skipSat0);
1025
1026
49.3k
    }
1027
1028
211k
  if ((profileInfo.fToneCurveCount & 1) == 0)
1029
194k
    {
1030
1031
194k
    TempBigEndian setEndianness (stream, profileInfo.fBigEndian);
1032
1033
194k
    stream.SetReadPosition (profileInfo.fToneCurveOffset);
1034
1035
194k
    uint32 points = profileInfo.fToneCurveCount / 2;
1036
1037
194k
    fToneCurve.fCoord.resize (points);
1038
1039
3.80M
    for (size_t i = 0; i < points; i++)
1040
3.61M
      {
1041
1042
3.61M
      dng_point_real64 point;
1043
1044
3.61M
      point.h = stream.Get_real32 ();
1045
3.61M
      point.v = stream.Get_real32 ();
1046
1047
3.61M
      fToneCurve.fCoord [i] = point;
1048
1049
3.61M
      }
1050
      
1051
194k
    }
1052
1053
211k
  SetHueSatMapEncoding (profileInfo.fHueSatMapEncoding);
1054
    
1055
211k
  SetLookTableEncoding (profileInfo.fLookTableEncoding);
1056
1057
211k
  SetBaselineExposureOffset (profileInfo.fBaselineExposureOffset.As_real64 ());
1058
1059
211k
  SetDefaultBlackRender (profileInfo.fDefaultBlackRender);
1060
    
1061
211k
  }
1062
    
1063
/*****************************************************************************/
1064
1065
bool dng_camera_profile::ParseExtended (dng_stream &stream)
1066
720
  {
1067
1068
720
  try
1069
720
    {
1070
    
1071
720
    dng_camera_profile_info profileInfo;
1072
    
1073
720
    if (!profileInfo.ParseExtended (stream))
1074
335
      {
1075
335
      return false;
1076
335
      }
1077
      
1078
385
    Parse (stream, profileInfo);
1079
1080
385
    return true;
1081
1082
720
    }
1083
    
1084
720
  catch (...)
1085
720
    {
1086
    
1087
    // Eat parsing errors.
1088
    
1089
145
    }
1090
1091
145
  return false;
1092
1093
720
  }
1094
1095
/*****************************************************************************/
1096
1097
void dng_camera_profile::SetFourColorBayer ()
1098
10.4k
  {
1099
  
1100
10.4k
  uint32 j;
1101
  
1102
10.4k
  if (!IsValid (3))
1103
25
    {
1104
25
    ThrowProgramError ();
1105
25
    }
1106
    
1107
10.4k
  if (fColorMatrix1.NotEmpty ())
1108
10.3k
    {
1109
    
1110
10.3k
    dng_matrix m (4, 3);
1111
    
1112
41.5k
    for (j = 0; j < 3; j++)
1113
31.1k
      {
1114
31.1k
      m [0] [j] = fColorMatrix1 [0] [j];
1115
31.1k
      m [1] [j] = fColorMatrix1 [1] [j];
1116
31.1k
      m [2] [j] = fColorMatrix1 [2] [j];
1117
31.1k
      m [3] [j] = fColorMatrix1 [1] [j];
1118
31.1k
      }
1119
      
1120
10.3k
    fColorMatrix1 = m;
1121
    
1122
10.3k
    }
1123
  
1124
10.4k
  if (fColorMatrix2.NotEmpty ())
1125
1.82k
    {
1126
    
1127
1.82k
    dng_matrix m (4, 3);
1128
    
1129
7.29k
    for (j = 0; j < 3; j++)
1130
5.46k
      {
1131
5.46k
      m [0] [j] = fColorMatrix2 [0] [j];
1132
5.46k
      m [1] [j] = fColorMatrix2 [1] [j];
1133
5.46k
      m [2] [j] = fColorMatrix2 [2] [j];
1134
5.46k
      m [3] [j] = fColorMatrix2 [1] [j];
1135
5.46k
      }
1136
      
1137
1.82k
    fColorMatrix2 = m;
1138
    
1139
1.82k
    }
1140
      
1141
10.4k
  fReductionMatrix1.Clear ();
1142
10.4k
  fReductionMatrix2.Clear ();
1143
  
1144
10.4k
  fForwardMatrix1.Clear ();
1145
10.4k
  fForwardMatrix2.Clear ();
1146
  
1147
10.4k
  }
1148
1149
/*****************************************************************************/
1150
1151
dng_hue_sat_map * dng_camera_profile::HueSatMapForWhite (const dng_xy_coord &white) const
1152
2.94k
  {
1153
  
1154
2.94k
  if (fHueSatDeltas1.IsValid ())
1155
0
    {
1156
1157
    // If we only have the first table, just use it for any color temperature.
1158
    
1159
0
    if (!fHueSatDeltas2.IsValid ())
1160
0
      {
1161
      
1162
0
      return new dng_hue_sat_map (fHueSatDeltas1);
1163
      
1164
0
      }
1165
      
1166
    // Else we need to interpolate based on color temperature.
1167
    
1168
0
    real64 temperature1 = CalibrationTemperature1 ();
1169
0
    real64 temperature2 = CalibrationTemperature2 ();
1170
    
1171
0
    if (temperature1 <= 0.0 ||
1172
0
      temperature2 <= 0.0 ||
1173
0
      temperature1 == temperature2)
1174
0
      {
1175
      
1176
0
      return new dng_hue_sat_map (fHueSatDeltas1);
1177
      
1178
0
      }
1179
      
1180
0
    bool reverseOrder = temperature1 > temperature2;
1181
    
1182
0
    if (reverseOrder)
1183
0
      {
1184
0
      real64 temp  = temperature1;
1185
0
      temperature1 = temperature2;
1186
0
      temperature2 = temp;
1187
0
      }
1188
1189
    // Convert to temperature/offset space.
1190
    
1191
0
    dng_temperature td (white);
1192
    
1193
    // Find fraction to weight the first calibration.
1194
    
1195
0
    real64 g;
1196
    
1197
0
    if (td.Temperature () <= temperature1)
1198
0
      g = 1.0;
1199
    
1200
0
    else if (td.Temperature () >= temperature2)
1201
0
      g = 0.0;
1202
    
1203
0
    else
1204
0
      {
1205
      
1206
0
      real64 invT = 1.0 / td.Temperature ();
1207
      
1208
0
      g = (invT                 - (1.0 / temperature2)) /
1209
0
        ((1.0 / temperature1) - (1.0 / temperature2));
1210
        
1211
0
      }
1212
      
1213
    // Fix up if we swapped the order.
1214
    
1215
0
    if (reverseOrder)
1216
0
      {
1217
0
      g = 1.0 - g;
1218
0
      }
1219
    
1220
    // Do the interpolation.
1221
    
1222
0
    return dng_hue_sat_map::Interpolate (HueSatDeltas1 (),
1223
0
                       HueSatDeltas2 (),
1224
0
                       g);
1225
    
1226
0
    }
1227
    
1228
2.94k
  return NULL;
1229
1230
2.94k
  }
1231
1232
/*****************************************************************************/
1233
1234
void dng_camera_profile::Stub ()
1235
0
  {
1236
  
1237
0
  (void) Fingerprint ();
1238
  
1239
0
  dng_hue_sat_map nullTable;
1240
  
1241
0
  fHueSatDeltas1 = nullTable;
1242
0
  fHueSatDeltas2 = nullTable;
1243
  
1244
0
  fLookTable = nullTable;
1245
  
1246
0
  fToneCurve.SetInvalid ();
1247
  
1248
0
  fWasStubbed = true;
1249
  
1250
0
  }
1251
1252
/*****************************************************************************/
1253
1254
void SplitCameraProfileName (const dng_string &name,
1255
               dng_string &baseName,
1256
               int32 &version)
1257
5.69k
  {
1258
  
1259
5.69k
  baseName = name;
1260
  
1261
5.69k
  version = 0;
1262
  
1263
5.69k
  uint32 len = baseName.Length ();
1264
  
1265
5.69k
  if (len > 5 && baseName.EndsWith (" beta"))
1266
0
    {
1267
    
1268
0
    baseName.Truncate (len - 5);
1269
    
1270
0
    version += -10;
1271
    
1272
0
    }
1273
    
1274
5.69k
  else if (len > 7)
1275
5.69k
    {
1276
    
1277
5.69k
    char lastChar = name.Get () [len - 1];
1278
    
1279
5.69k
    if (lastChar >= '0' && lastChar <= '9')
1280
50
      {
1281
      
1282
50
      dng_string temp = name;
1283
      
1284
50
      temp.Truncate (len - 1);
1285
      
1286
50
      if (temp.EndsWith (" beta "))
1287
0
        {
1288
        
1289
0
        baseName.Truncate (len - 7);
1290
        
1291
0
        version += ((int32) (lastChar - '0')) - 10;
1292
        
1293
0
        }
1294
        
1295
50
      }
1296
      
1297
5.69k
    }
1298
    
1299
5.69k
  len = baseName.Length ();
1300
  
1301
5.69k
  if (len > 3)
1302
5.69k
    {
1303
    
1304
5.69k
    char lastChar = name.Get () [len - 1];
1305
    
1306
5.69k
    if (lastChar >= '0' && lastChar <= '9')
1307
50
      {
1308
      
1309
50
      dng_string temp = name;
1310
      
1311
50
      temp.Truncate (len - 1);
1312
      
1313
50
      if (temp.EndsWith (" v"))
1314
2
        {
1315
        
1316
2
        baseName.Truncate (len - 3);
1317
        
1318
2
        version += ((int32) (lastChar - '0')) * 100;
1319
        
1320
2
        }
1321
        
1322
50
      }
1323
      
1324
5.69k
    }
1325
1326
5.69k
  }
1327
1328
/*****************************************************************************/
1329
1330
void BuildHueSatMapEncodingTable (dng_memory_allocator &allocator,
1331
                  uint32 encoding,
1332
                  AutoPtr<dng_1d_table> &encodeTable,
1333
                  AutoPtr<dng_1d_table> &decodeTable,
1334
                  bool subSample)
1335
188
  {
1336
1337
188
  encodeTable.Reset ();
1338
188
  decodeTable.Reset ();
1339
  
1340
188
  switch (encoding)
1341
188
    {
1342
    
1343
0
    case encoding_Linear:
1344
0
      {
1345
1346
0
      break;
1347
1348
0
      }
1349
    
1350
94
    case encoding_sRGB:
1351
94
      {
1352
1353
94
      encodeTable.Reset (new dng_1d_table);
1354
94
      decodeTable.Reset (new dng_1d_table);
1355
1356
94
      const dng_1d_function & curve = dng_function_GammaEncode_sRGB::Get ();
1357
1358
94
      encodeTable->Initialize (allocator,
1359
94
                   curve,
1360
94
                   subSample);
1361
1362
94
      const dng_1d_inverse inverse (curve);
1363
1364
94
      decodeTable->Initialize (allocator,
1365
94
                   inverse,
1366
94
                   subSample);
1367
1368
94
      break;
1369
1370
0
      }
1371
1372
94
    default:
1373
94
      {
1374
1375
94
      DNG_REPORT ("Unsupported hue sat map / look table encoding.");
1376
1377
94
      break;
1378
1379
0
      }
1380
    
1381
188
    }
1382
1383
188
  }
1384
              
1385
/*****************************************************************************/