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_matrix.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_matrix.cpp#1 $ */ 
10
/* $DateTime: 2012/05/30 13:28:51 $ */
11
/* $Change: 832332 $ */
12
/* $Author: tknoll $ */
13
14
/*****************************************************************************/
15
16
#include "dng_matrix.h"
17
18
#include "dng_exceptions.h"
19
#include "dng_utils.h"
20
21
/*****************************************************************************/
22
        
23
dng_matrix::dng_matrix ()
24
            
25
77.2M
  : fRows (0)
26
77.2M
  , fCols (0)
27
  
28
77.2M
  {
29
  
30
77.2M
  }
31
          
32
/*****************************************************************************/
33
        
34
dng_matrix::dng_matrix (uint32 rows,
35
            uint32 cols)
36
            
37
961k
  : fRows (0)
38
961k
  , fCols (0)
39
  
40
961k
  {
41
  
42
961k
  if (rows < 1 || rows > kMaxColorPlanes ||
43
961k
    cols < 1 || cols > kMaxColorPlanes)
44
0
    {
45
    
46
0
    ThrowProgramError ();
47
    
48
0
    }
49
  
50
961k
  fRows = rows;
51
961k
  fCols = cols;
52
  
53
3.83M
  for (uint32 row = 0; row < fRows; row++)
54
11.5M
    for (uint32 col = 0; col < fCols; col++)
55
8.67M
      {
56
      
57
8.67M
      fData [row] [col] = 0.0;
58
      
59
8.67M
      }
60
  
61
961k
  }
62
          
63
/*****************************************************************************/
64
        
65
dng_matrix::dng_matrix (const dng_matrix &m)
66
67
10.5M
  : fRows (m.fRows)
68
10.5M
  , fCols (m.fCols)
69
  
70
10.5M
  {
71
  
72
11.7M
  for (uint32 row = 0; row < fRows; row++)
73
5.12M
    for (uint32 col = 0; col < fCols; col++)
74
3.84M
      {
75
      
76
3.84M
      fData [row] [col] = m.fData [row] [col];
77
      
78
3.84M
      }
79
  
80
10.5M
  }
81
          
82
/*****************************************************************************/
83
        
84
void dng_matrix::Clear () 
85
48.5k
  {
86
  
87
48.5k
  fRows = 0;
88
48.5k
  fCols = 0;
89
  
90
48.5k
  }
91
        
92
/*****************************************************************************/
93
        
94
void dng_matrix::SetIdentity (uint32 count) 
95
6.00k
  {
96
  
97
6.00k
  *this = dng_matrix (count, count);
98
  
99
24.4k
  for (uint32 j = 0; j < count; j++)
100
18.3k
    {
101
    
102
18.3k
    fData [j] [j] = 1.0;
103
    
104
18.3k
    }
105
  
106
6.00k
  }
107
        
108
/******************************************************************************/
109
110
bool dng_matrix::operator== (const dng_matrix &m) const
111
46.5M
  {
112
  
113
46.5M
  if (Rows () != m.Rows () ||
114
46.3M
      Cols () != m.Cols ())
115
166k
    {
116
    
117
166k
    return false;
118
    
119
166k
    }
120
  
121
85.0M
  for (uint32 j = 0; j < Rows (); j++)  
122
154M
    for (uint32 k = 0; k < Cols (); k++)
123
116M
      {
124
      
125
116M
      if (fData [j] [k] != m.fData [j] [k])
126
91.8k
        {
127
        
128
91.8k
        return false;
129
        
130
91.8k
        }
131
      
132
116M
      }
133
      
134
46.2M
  return true;
135
  
136
46.3M
  }
137
138
/******************************************************************************/
139
140
bool dng_matrix::IsDiagonal () const
141
0
  {
142
  
143
0
  if (IsEmpty ())
144
0
    {
145
0
    return false;
146
0
    }
147
  
148
0
  if (Rows () != Cols ())
149
0
    {
150
0
    return false;
151
0
    }
152
  
153
0
  for (uint32 j = 0; j < Rows (); j++)
154
0
    for (uint32 k = 0; k < Cols (); k++)
155
0
      {
156
      
157
0
      if (j != k)
158
0
        {
159
        
160
0
        if (fData [j] [k] != 0.0)
161
0
          {
162
0
          return false;
163
0
          }
164
        
165
0
        }
166
      
167
0
      }
168
      
169
0
  return true;
170
  
171
0
  }
172
173
/******************************************************************************/
174
175
real64 dng_matrix::MaxEntry () const
176
0
  {
177
  
178
0
  if (IsEmpty ())
179
0
    {
180
    
181
0
    return 0.0;
182
    
183
0
    }
184
  
185
0
  real64 m = fData [0] [0];
186
  
187
0
  for (uint32 j = 0; j < Rows (); j++)
188
0
    for (uint32 k = 0; k < Cols (); k++)
189
0
      {
190
      
191
0
      m = Max_real64 (m, fData [j] [k]);
192
      
193
0
      }
194
      
195
0
  return m;
196
  
197
0
  }
198
    
199
/******************************************************************************/
200
201
real64 dng_matrix::MinEntry () const
202
0
  {
203
  
204
0
  if (IsEmpty ())
205
0
    {
206
    
207
0
    return 0.0;
208
    
209
0
    }
210
  
211
0
  real64 m = fData [0] [0];
212
  
213
0
  for (uint32 j = 0; j < Rows (); j++)
214
0
    for (uint32 k = 0; k < Cols (); k++)
215
0
      {
216
      
217
0
      m = Min_real64 (m, fData [j] [k]);
218
      
219
0
      }
220
      
221
0
  return m;
222
  
223
0
  }
224
    
225
/*****************************************************************************/
226
227
void dng_matrix::Scale (real64 factor)
228
271k
  {
229
  
230
1.08M
  for (uint32 j = 0; j < Rows (); j++)  
231
3.25M
    for (uint32 k = 0; k < Cols (); k++)
232
2.44M
      {
233
      
234
2.44M
      fData [j] [k] *= factor;
235
      
236
2.44M
      }
237
      
238
271k
  }
239
      
240
/*****************************************************************************/
241
242
void dng_matrix::Round (real64 factor)
243
330k
  {
244
  
245
330k
  real64 invFactor = 1.0 / factor;
246
  
247
1.31M
  for (uint32 j = 0; j < Rows (); j++)  
248
3.94M
    for (uint32 k = 0; k < Cols (); k++)
249
2.95M
      {
250
      
251
2.95M
      fData [j] [k] = Round_int32 (fData [j] [k] * factor) * invFactor;
252
      
253
2.95M
      }
254
      
255
330k
  }
256
  
257
/*****************************************************************************/
258
259
void dng_matrix::SafeRound (real64 factor)
260
0
  {
261
  
262
0
  real64 invFactor = 1.0 / factor;
263
  
264
0
  for (uint32 j = 0; j < Rows (); j++)
265
0
    {
266
    
267
    // Round each row to the specified accuracy, but make sure the
268
    // a rounding does not affect the total of the elements in a row
269
    // more than necessary.
270
    
271
0
    real64 error = 0.0;
272
    
273
0
    for (uint32 k = 0; k < Cols (); k++)
274
0
      {
275
      
276
0
      fData [j] [k] += error;
277
      
278
0
      real64 rounded = Round_int32 (fData [j] [k] * factor) * invFactor;
279
      
280
0
      error = fData [j] [k] - rounded;
281
      
282
0
      fData [j] [k] = rounded;
283
      
284
0
      }
285
    
286
0
    }
287
    
288
0
  }
289
290
/*****************************************************************************/
291
292
dng_matrix_3by3::dng_matrix_3by3 ()
293
294
2.98k
  : dng_matrix (3, 3)
295
  
296
2.98k
  {
297
2.98k
  }
298
    
299
/*****************************************************************************/
300
301
dng_matrix_3by3::dng_matrix_3by3 (const dng_matrix &m)
302
303
2.98k
  : dng_matrix (m)
304
  
305
2.98k
  {
306
  
307
2.98k
  if (Rows () != 3 ||
308
2.98k
    Cols () != 3)
309
0
    {
310
    
311
0
    ThrowMatrixMath ();
312
    
313
0
    }
314
  
315
2.98k
  }
316
    
317
/*****************************************************************************/
318
319
dng_matrix_3by3::dng_matrix_3by3 (real64 a00, real64 a01, real64 a02,
320
                      real64 a10, real64 a11, real64 a12,
321
                      real64 a20, real64 a21, real64 a22)
322
                     
323
324
3.00k
  : dng_matrix (3, 3)
325
  
326
3.00k
  {
327
  
328
3.00k
  fData [0] [0] = a00;
329
3.00k
  fData [0] [1] = a01;
330
3.00k
  fData [0] [2] = a02;
331
  
332
3.00k
  fData [1] [0] = a10;
333
3.00k
  fData [1] [1] = a11;
334
3.00k
  fData [1] [2] = a12;
335
  
336
3.00k
  fData [2] [0] = a20;
337
3.00k
  fData [2] [1] = a21;
338
3.00k
  fData [2] [2] = a22;
339
  
340
3.00k
  }
341
342
/*****************************************************************************/
343
344
dng_matrix_3by3::dng_matrix_3by3 (real64 a00, real64 a11, real64 a22)
345
346
0
  : dng_matrix (3, 3)
347
  
348
0
  {
349
  
350
0
  fData [0] [0] = a00;
351
0
  fData [1] [1] = a11;
352
0
  fData [2] [2] = a22;
353
  
354
0
  }
355
356
/*****************************************************************************/
357
358
dng_matrix_4by3::dng_matrix_4by3 ()
359
360
0
  : dng_matrix (4, 3)
361
  
362
0
  {
363
0
  }
364
    
365
/*****************************************************************************/
366
367
dng_matrix_4by3::dng_matrix_4by3 (const dng_matrix &m)
368
369
0
  : dng_matrix (m)
370
  
371
0
  {
372
  
373
0
  if (Rows () != 4 ||
374
0
    Cols () != 3)
375
0
    {
376
    
377
0
    ThrowMatrixMath ();
378
    
379
0
    }
380
  
381
0
  }
382
    
383
/*****************************************************************************/
384
385
dng_matrix_4by3::dng_matrix_4by3 (real64 a00, real64 a01, real64 a02,
386
                      real64 a10, real64 a11, real64 a12,
387
                      real64 a20, real64 a21, real64 a22,
388
                      real64 a30, real64 a31, real64 a32)
389
                     
390
391
0
  : dng_matrix (4, 3)
392
  
393
0
  {
394
  
395
0
  fData [0] [0] = a00;
396
0
  fData [0] [1] = a01;
397
0
  fData [0] [2] = a02;
398
  
399
0
  fData [1] [0] = a10;
400
0
  fData [1] [1] = a11;
401
0
  fData [1] [2] = a12;
402
  
403
0
  fData [2] [0] = a20;
404
0
  fData [2] [1] = a21;
405
0
  fData [2] [2] = a22;
406
  
407
0
  fData [3] [0] = a30;
408
0
  fData [3] [1] = a31;
409
0
  fData [3] [2] = a32;
410
  
411
0
  }
412
413
/*****************************************************************************/
414
        
415
dng_vector::dng_vector ()
416
417
475k
  : fCount (0)
418
  
419
475k
  {
420
  
421
475k
  }
422
          
423
/*****************************************************************************/
424
        
425
dng_vector::dng_vector (uint32 count)
426
427
670k
  : fCount (0)
428
  
429
670k
  {
430
  
431
670k
  if (count < 1 || count > kMaxColorPlanes)
432
0
    {
433
    
434
0
    ThrowProgramError ();
435
    
436
0
    }
437
  
438
670k
  fCount = count;
439
  
440
2.68M
  for (uint32 index = 0; index < fCount; index++)
441
2.01M
    {
442
    
443
2.01M
    fData [index] = 0.0;
444
    
445
2.01M
    }
446
  
447
670k
  }
448
          
449
/*****************************************************************************/
450
        
451
dng_vector::dng_vector (const dng_vector &v)
452
453
19.2k
  : fCount (v.fCount)
454
  
455
19.2k
  {
456
  
457
77.1k
  for (uint32 index = 0; index < fCount; index++)
458
57.8k
    {
459
    
460
57.8k
    fData [index] = v.fData [index];
461
    
462
57.8k
    }
463
  
464
19.2k
  }
465
          
466
/*****************************************************************************/
467
        
468
void dng_vector::Clear () 
469
4.47k
  {
470
  
471
4.47k
  fCount = 0;
472
  
473
4.47k
  }
474
        
475
/*****************************************************************************/
476
        
477
void dng_vector::SetIdentity (uint32 count) 
478
11.4k
  {
479
  
480
11.4k
  *this = dng_vector (count);
481
  
482
45.8k
  for (uint32 j = 0; j < count; j++)
483
34.4k
    {
484
    
485
34.4k
    fData [j] = 1.0;
486
    
487
34.4k
    }
488
  
489
11.4k
  }
490
        
491
/******************************************************************************/
492
493
bool dng_vector::operator== (const dng_vector &v) const
494
0
  {
495
  
496
0
  if (Count () != v.Count ())
497
0
    {
498
    
499
0
    return false;
500
    
501
0
    }
502
  
503
0
  for (uint32 j = 0; j < Count (); j++) 
504
0
    {
505
    
506
0
    if (fData [j] != v.fData [j])
507
0
      {
508
      
509
0
      return false;
510
      
511
0
      }
512
    
513
0
    }
514
      
515
0
  return true;
516
  
517
0
  }
518
519
/******************************************************************************/
520
521
real64 dng_vector::MaxEntry () const
522
295k
  {
523
  
524
295k
  if (IsEmpty ())
525
0
    {
526
    
527
0
    return 0.0;
528
    
529
0
    }
530
  
531
295k
  real64 m = fData [0];
532
  
533
1.18M
  for (uint32 j = 0; j < Count (); j++)
534
888k
    {
535
    
536
888k
    m = Max_real64 (m, fData [j]);
537
    
538
888k
    }
539
      
540
295k
  return m;
541
  
542
295k
  }
543
    
544
/******************************************************************************/
545
546
real64 dng_vector::MinEntry () const
547
6.59k
  {
548
  
549
6.59k
  if (IsEmpty ())
550
0
    {
551
    
552
0
    return 0.0;
553
    
554
0
    }
555
  
556
6.59k
  real64 m = fData [0];
557
  
558
26.2k
  for (uint32 j = 0; j < Count (); j++)
559
19.6k
    {
560
    
561
19.6k
    m = Min_real64 (m, fData [j]);
562
    
563
19.6k
    }
564
      
565
6.59k
  return m;
566
  
567
6.59k
  }
568
    
569
/*****************************************************************************/
570
571
void dng_vector::Scale (real64 factor)
572
5.05k
  {
573
  
574
20.0k
  for (uint32 j = 0; j < Count (); j++)  
575
15.0k
    {
576
    
577
15.0k
    fData [j] *= factor;
578
    
579
15.0k
    }
580
    
581
5.05k
  }
582
      
583
/*****************************************************************************/
584
585
void dng_vector::Round (real64 factor)
586
5.05k
  {
587
  
588
5.05k
  real64 invFactor = 1.0 / factor;
589
  
590
19.9k
  for (uint32 j = 0; j < Count (); j++)  
591
14.9k
    {
592
    
593
14.9k
    fData [j] = Round_int32 (fData [j] * factor) * invFactor;
594
    
595
14.9k
    }
596
      
597
5.05k
  }
598
  
599
/*****************************************************************************/
600
        
601
dng_matrix dng_vector::AsDiagonal () const
602
7.00k
  {
603
  
604
7.00k
  dng_matrix M (Count (), Count ());
605
  
606
27.9k
  for (uint32 j = 0; j < Count (); j++)
607
20.8k
    {
608
    
609
20.8k
    M [j] [j] = fData [j];
610
    
611
20.8k
    }
612
    
613
7.00k
  return M;
614
  
615
7.00k
  }
616
617
/*****************************************************************************/
618
        
619
dng_matrix dng_vector::AsColumn () const
620
3
  {
621
  
622
3
  dng_matrix M (Count (), 1);
623
  
624
12
  for (uint32 j = 0; j < Count (); j++)
625
9
    {
626
    
627
9
    M [j] [0] = fData [j];
628
    
629
9
    }
630
    
631
3
  return M;
632
  
633
3
  }
634
635
/******************************************************************************/
636
637
dng_vector_3::dng_vector_3 ()
638
639
0
  : dng_vector (3)
640
  
641
0
  {
642
0
  }
643
    
644
/******************************************************************************/
645
646
dng_vector_3::dng_vector_3 (const dng_vector &v)
647
648
7.85k
  : dng_vector (v)
649
  
650
7.85k
  {
651
  
652
7.85k
  if (Count () != 3)
653
0
    {
654
    
655
0
    ThrowMatrixMath ();
656
           
657
0
    }
658
  
659
7.85k
  }
660
    
661
/******************************************************************************/
662
663
dng_vector_3::dng_vector_3 (real64 a0,
664
              real64 a1,
665
                real64 a2)
666
667
312k
  : dng_vector (3)
668
  
669
312k
  {
670
  
671
312k
  fData [0] = a0;
672
312k
  fData [1] = a1;
673
312k
  fData [2] = a2;
674
  
675
312k
  }
676
    
677
/******************************************************************************/
678
679
dng_vector_4::dng_vector_4 ()
680
681
0
  : dng_vector (4)
682
  
683
0
  {
684
0
  }
685
    
686
/******************************************************************************/
687
688
dng_vector_4::dng_vector_4 (const dng_vector &v)
689
690
0
  : dng_vector (v)
691
  
692
0
  {
693
  
694
0
  if (Count () != 4)
695
0
    {
696
    
697
0
    ThrowMatrixMath ();
698
           
699
0
    }
700
  
701
0
  }
702
    
703
/******************************************************************************/
704
705
dng_vector_4::dng_vector_4 (real64 a0,
706
              real64 a1,
707
                real64 a2,
708
                real64 a3)
709
710
0
  : dng_vector (4)
711
  
712
0
  {
713
  
714
0
  fData [0] = a0;
715
0
  fData [1] = a1;
716
0
  fData [2] = a2;
717
0
  fData [3] = a3;
718
  
719
0
  }
720
    
721
/******************************************************************************/
722
723
dng_matrix operator* (const dng_matrix &A,
724
            const dng_matrix &B)
725
43.3k
  {
726
  
727
43.3k
  if (A.Cols () != B.Rows ())
728
0
    {
729
    
730
0
    ThrowMatrixMath ();
731
           
732
0
    }
733
  
734
43.3k
  dng_matrix C (A.Rows (), B.Cols ());
735
  
736
160k
  for (uint32 j = 0; j < C.Rows (); j++)
737
464k
    for (uint32 k = 0; k < C.Cols (); k++)
738
348k
      {
739
      
740
348k
      C [j] [k] = 0.0;
741
      
742
1.39M
      for (uint32 m = 0; m < A.Cols (); m++)
743
1.04M
        {
744
        
745
1.04M
        real64 aa = A [j] [m];
746
        
747
1.04M
        real64 bb = B [m] [k];
748
        
749
1.04M
        C [j] [k] += aa * bb;
750
        
751
1.04M
        }
752
      
753
348k
      }
754
      
755
43.3k
  return C;
756
757
43.3k
  }
758
759
/******************************************************************************/
760
761
dng_vector operator* (const dng_matrix &A,
762
            const dng_vector &B)
763
314k
  {
764
  
765
314k
  if (A.Cols () != B.Count ())
766
0
    {
767
    
768
0
    ThrowMatrixMath ();
769
           
770
0
    }
771
  
772
314k
  dng_vector C (A.Rows ());
773
  
774
1.26M
  for (uint32 j = 0; j < C.Count (); j++)
775
945k
    {
776
    
777
945k
    C [j] = 0.0;
778
    
779
3.78M
    for (uint32 m = 0; m < A.Cols (); m++)
780
2.83M
      {
781
      
782
2.83M
      real64 aa = A [j] [m];
783
      
784
2.83M
      real64 bb = B [m];
785
      
786
2.83M
      C [j] += aa * bb;
787
      
788
2.83M
      }
789
    
790
945k
    }
791
      
792
314k
  return C;
793
794
314k
  }
795
796
/******************************************************************************/
797
798
dng_matrix operator* (real64 scale,
799
            const dng_matrix &A)
800
4.96k
  {
801
  
802
4.96k
  dng_matrix B (A);
803
  
804
4.96k
  B.Scale (scale);
805
  
806
4.96k
  return B;
807
    
808
4.96k
  }
809
810
/******************************************************************************/
811
812
dng_vector operator* (real64 scale,
813
            const dng_vector &A)
814
0
  {
815
  
816
0
  dng_vector B (A);
817
  
818
0
  B.Scale (scale);
819
  
820
0
  return B;
821
    
822
0
  }
823
824
/******************************************************************************/
825
826
dng_matrix operator+ (const dng_matrix &A,
827
            const dng_matrix &B)
828
991
  {
829
  
830
991
  if (A.Cols () != B.Cols () ||
831
991
    A.Rows () != B.Rows ())
832
0
    {
833
    
834
0
    ThrowMatrixMath ();
835
           
836
0
    }
837
  
838
991
  dng_matrix C (A);
839
  
840
4.42k
  for (uint32 j = 0; j < C.Rows (); j++)
841
13.8k
    for (uint32 k = 0; k < C.Cols (); k++)
842
10.4k
      {
843
      
844
10.4k
      C [j] [k] += B [j] [k];
845
            
846
10.4k
      }
847
      
848
991
  return C;
849
850
991
  }
851
852
/******************************************************************************/
853
854
const real64 kNearZero = 1.0E-10; 
855
856
/******************************************************************************/
857
858
// Work around bug #1294195, which may be a hardware problem on a specific machine.
859
// This pragma turns on "improved" floating-point consistency.
860
#ifdef _MSC_VER
861
#pragma optimize ("p", on)
862
#endif
863
864
static dng_matrix Invert3by3 (const dng_matrix &A)
865
271k
  {
866
  
867
271k
  real64 a00 = A [0] [0];
868
271k
  real64 a01 = A [0] [1];
869
271k
  real64 a02 = A [0] [2];
870
271k
  real64 a10 = A [1] [0];
871
271k
  real64 a11 = A [1] [1];
872
271k
  real64 a12 = A [1] [2];
873
271k
  real64 a20 = A [2] [0];
874
271k
  real64 a21 = A [2] [1];
875
271k
  real64 a22 = A [2] [2];
876
  
877
271k
  real64 temp [3] [3];
878
879
271k
  temp [0] [0] = a11 * a22 - a21 * a12;
880
271k
  temp [0] [1] = a21 * a02 - a01 * a22;
881
271k
  temp [0] [2] = a01 * a12 - a11 * a02;
882
271k
  temp [1] [0] = a20 * a12 - a10 * a22;
883
271k
  temp [1] [1] = a00 * a22 - a20 * a02;
884
271k
  temp [1] [2] = a10 * a02 - a00 * a12;
885
271k
  temp [2] [0] = a10 * a21 - a20 * a11;
886
271k
  temp [2] [1] = a20 * a01 - a00 * a21;
887
271k
  temp [2] [2] = a00 * a11 - a10 * a01;
888
889
271k
  real64 det = (a00 * temp [0] [0] +
890
271k
          a01 * temp [1] [0] +
891
271k
          a02 * temp [2] [0]);
892
893
271k
  if (Abs_real64 (det) < kNearZero)
894
7.01k
    {
895
    
896
7.01k
    ThrowMatrixMath ();
897
           
898
7.01k
    }
899
900
271k
  dng_matrix B (3, 3);
901
  
902
1.06M
  for (uint32 j = 0; j < 3; j++)  
903
3.17M
    for (uint32 k = 0; k < 3; k++)
904
2.38M
      {
905
      
906
2.38M
      B [j] [k] = temp [j] [k] / det;
907
      
908
2.38M
      }
909
      
910
271k
  return B;
911
912
271k
  }
913
    
914
// Reset floating-point optimization. See comment above.
915
#ifdef _MSC_VER
916
#pragma optimize ("p", off)
917
#endif
918
919
/******************************************************************************/
920
921
static dng_matrix InvertNbyN (const dng_matrix &A)
922
265
  {
923
  
924
265
  uint32 i;
925
265
  uint32 j;
926
265
  uint32 k;
927
  
928
265
  uint32 n = A.Rows ();
929
  
930
265
  real64 temp [kMaxColorPlanes] [kMaxColorPlanes * 2];
931
  
932
937
  for (i = 0; i < n; i++)
933
2.58k
    for (j = 0; j < n; j++)
934
1.91k
      {
935
      
936
1.91k
      temp [i] [j    ] = A [i] [j];
937
      
938
1.91k
      temp [i] [j + n] = (i == j ? 1.0 : 0.0);
939
      
940
1.91k
      }
941
      
942
872
  for (i = 0; i < n; i++)
943
607
    {
944
    
945
607
    real64 alpha = temp [i] [i];
946
    
947
607
    if (Abs_real64 (alpha) < kNearZero)
948
53
      {
949
      
950
53
      ThrowMatrixMath ();
951
             
952
53
      }
953
      
954
3.55k
    for (j = 0; j < n * 2; j++)
955
2.95k
      {
956
      
957
2.95k
      temp [i] [j] /= alpha;
958
      
959
2.95k
      }
960
      
961
2.08k
    for (k = 0; k < n; k++)
962
1.47k
      {
963
      
964
1.47k
      if (i != k)
965
922
        {
966
        
967
922
        real64 beta = temp [k] [i];
968
        
969
6.81k
        for (j = 0; j < n * 2; j++)
970
5.89k
          {
971
          
972
5.89k
          temp [k] [j] -= beta * temp [i] [j];
973
          
974
5.89k
          }
975
        
976
922
        }
977
      
978
1.47k
      }
979
      
980
607
    }
981
    
982
265
  dng_matrix B (n, n);
983
  
984
757
  for (i = 0; i < n; i++)
985
1.74k
    for (j = 0; j < n; j++)
986
1.25k
      {
987
      
988
1.25k
      B [i] [j] = temp [i] [j + n];
989
      
990
1.25k
      }
991
      
992
265
  return B;
993
994
265
  }
995
    
996
/******************************************************************************/
997
998
dng_matrix Transpose (const dng_matrix &A)
999
5.65k
  {
1000
  
1001
5.65k
  dng_matrix B (A.Cols (), A.Rows ());
1002
  
1003
22.6k
  for (uint32 j = 0; j < B.Rows (); j++)
1004
63.1k
    for (uint32 k = 0; k < B.Cols (); k++)
1005
46.1k
      {
1006
      
1007
46.1k
      B [j] [k] = A [k] [j];
1008
      
1009
46.1k
      }
1010
      
1011
5.65k
  return B; 
1012
  
1013
5.65k
  }
1014
1015
/******************************************************************************/
1016
1017
dng_matrix Invert (const dng_matrix &A)
1018
277k
  {
1019
  
1020
277k
  if (A.Rows () < 2 || A.Cols () < 2)
1021
0
    {
1022
    
1023
0
    ThrowMatrixMath ();
1024
             
1025
0
    }
1026
  
1027
277k
  if (A.Rows () == A.Cols ())
1028
272k
    {
1029
    
1030
272k
    if (A.Rows () == 3)
1031
271k
      {
1032
      
1033
271k
      return Invert3by3 (A);
1034
      
1035
271k
      }
1036
      
1037
265
    return InvertNbyN (A);
1038
    
1039
272k
    }
1040
    
1041
5.65k
  else
1042
5.65k
    {
1043
    
1044
    // Compute the pseudo inverse.
1045
  
1046
5.65k
    dng_matrix B = Transpose (A);
1047
  
1048
5.65k
    return Invert (B * A) * B;
1049
    
1050
5.65k
    }
1051
    
1052
277k
  }
1053
    
1054
/******************************************************************************/
1055
1056
dng_matrix Invert (const dng_matrix &A,
1057
           const dng_matrix &hint)
1058
25.2k
  {
1059
  
1060
25.2k
  if (A.Rows () == A   .Cols () ||
1061
1.16k
    A.Rows () != hint.Cols () ||
1062
46
    A.Cols () != hint.Rows ())
1063
25.2k
    {
1064
    
1065
25.2k
    return Invert (A);
1066
    
1067
25.2k
    }
1068
    
1069
46
  else
1070
46
    {
1071
    
1072
    // Use the specified hint matrix.
1073
    
1074
46
    return Invert (hint * A) * hint;
1075
    
1076
46
    }
1077
  
1078
25.2k
  }
1079
  
1080
/*****************************************************************************/