Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_reference.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_reference.h"
10
11 #include "dng_1d_table.h"
12 #include "dng_flags.h"
13 #include "dng_gain_map.h"
14 #include "dng_hue_sat_map.h"
15 #include "dng_matrix.h"
16 #include "dng_resample.h"
17 #include "dng_simd_type.h"
18 #include "dng_utils.h"
19
20 /*****************************************************************************/
21
22 // This module contains routines that should be as fast as possible, even
23 // at the expense of slight code size increases.
24
25 #include "dng_fast_module.h"
26
27 /*****************************************************************************/
28
29 void RefZeroBytes (void *dPtr,
30 uint32 count)
31 {
32
33 memset (dPtr, 0, count);
34
35 }
36
37 /*****************************************************************************/
38
39 void RefCopyBytes (const void *sPtr,
40 void *dPtr,
41 uint32 count)
42 {
43
44 memcpy (dPtr, sPtr, count);
45
46 }
47
48 /*****************************************************************************/
49
50 void RefSwapBytes16 (uint16 *dPtr,
51 uint32 count)
52 {
53
54 for (uint32 j = 0; j < count; j++)
55 {
56
57 dPtr [j] = SwapBytes16 (dPtr [j]);
58
59 }
60
61 }
62
63 /*****************************************************************************/
64
65 void RefSwapBytes32 (uint32 *dPtr,
66 uint32 count)
67 {
68
69 for (uint32 j = 0; j < count; j++)
70 {
71
72 dPtr [j] = SwapBytes32 (dPtr [j]);
73
74 }
75
76 }
77
78 /*****************************************************************************/
79
80 void RefSetArea8 (uint8 *dPtr,
81 uint8 value,
82 uint32 rows,
83 uint32 cols,
84 uint32 planes,
85 int32 rowStep,
86 int32 colStep,
87 int32 planeStep)
88 {
89
90 for (uint32 row = 0; row < rows; row++)
91 {
92
93 uint8 *dPtr1 = dPtr;
94
95 for (uint32 col = 0; col < cols; col++)
96 {
97
98 uint8 *dPtr2 = dPtr1;
99
100 for (uint32 plane = 0; plane < planes; plane++)
101 {
102
103 *dPtr2 = value;
104
105 dPtr2 += planeStep;
106
107 }
108
109 dPtr1 += colStep;
110
111 }
112
113 dPtr += rowStep;
114
115 }
116
117 }
118
119 /*****************************************************************************/
120
121 template <SIMDType simd, typename destType>
122
123 #ifdef __INTEL_LLVM_COMPILER
124 __attribute__((SET_CPU_FEATURE(simd)))
125 #endif // __INTEL_LLVM_COMPILER
126
127 void RefSetArea (destType *dPtr,
128 destType value,
129 uint32 rows,
130 uint32 cols,
131 uint32 planes,
132 int32 rowStep,
133 int32 colStep,
134 int32 planeStep)
135 {
136
137 INTEL_COMPILER_NEEDED_NOTE
138 #ifdef __INTEL_COMPILER
139 SET_CPU_FEATURE(simd);
140 #endif // __INTEL_COMPILER
141
142
143 if ((planeStep == 0) && (colStep == 1))
144 {
145
146 for (uint32 row = 0; row < rows; row++)
147 {
148
149 INTEL_PRAGMA_SIMD_ASSERT
150 for (uint32 col = 0; col < cols; col++)
151 {
152
153 dPtr [col] = value;
154
155 }
156
157 dPtr += rowStep;
158
159 }
160 }
161
162 else if (planeStep == 1)
163 {
164
165 for (uint32 row = 0; row < rows; row++)
166 {
167
168 destType *dPtr1 = dPtr;
169
170 for (uint32 col = 0; col < cols; col++)
171 {
172
173 destType *dPtr2 = dPtr1;
174
175 INTEL_PRAGMA_SIMD_ASSERT
176 for (uint32 plane = 0; plane < planes; plane++)
177 {
178
179 dPtr2 [plane] = value;
180
181 }
182
183 dPtr1 += colStep;
184
185 }
186
187 dPtr += rowStep;
188
189 }
190
191 }
192
193 else
194 {
195
196 for (uint32 row = 0; row < rows; row++)
197 {
198
199 destType *dPtr1 = dPtr;
200
201 for (uint32 col = 0; col < cols; col++)
202 {
203
204 destType *dPtr2 = dPtr1;
205
206 for (uint32 plane = 0; plane < planes; plane++)
207 {
208
209 *dPtr2 = value;
210
211 dPtr2 += planeStep;
212
213 }
214
215 dPtr1 += colStep;
216
217 }
218
219 dPtr += rowStep;
220
221 }
222
223 }
224
225 }
226
227 /*****************************************************************************/
228
229 #if !qDNGIntelCompiler
230 template
231 void RefSetArea<Scalar, uint16>(uint16 *dPtr,
232 uint16 value,
233 uint32 rows,
234 uint32 cols,
235 uint32 planes,
236 int32 rowStep,
237 int32 colStep,
238 int32 planeStep);
239 template
240 void RefSetArea<Scalar, uint32>(uint32 *dPtr,
241 uint32 value,
242 uint32 rows,
243 uint32 cols,
244 uint32 planes,
245 int32 rowStep,
246 int32 colStep,
247 int32 planeStep);
248 #else
249 template SetArea16Proc RefSetArea<Scalar, uint16>;
250 template SetArea16Proc RefSetArea<AVX2, uint16>;
251 template SetArea32Proc RefSetArea<Scalar, uint32>;
252 template SetArea32Proc RefSetArea<AVX2, uint32>;
253 #endif
254
255 /*****************************************************************************/
256
257 void RefCopyArea8 (const uint8 *sPtr,
258 uint8 *dPtr,
259 uint32 rows,
260 uint32 cols,
261 uint32 planes,
262 int32 sRowStep,
263 int32 sColStep,
264 int32 sPlaneStep,
265 int32 dRowStep,
266 int32 dColStep,
267 int32 dPlaneStep)
268 {
269
270 for (uint32 row = 0; row < rows; row++)
271 {
272
273 const uint8 *sPtr1 = sPtr;
274 uint8 *dPtr1 = dPtr;
275
276 for (uint32 col = 0; col < cols; col++)
277 {
278
279 const uint8 *sPtr2 = sPtr1;
280 uint8 *dPtr2 = dPtr1;
281
282 for (uint32 plane = 0; plane < planes; plane++)
283 {
284
285 *dPtr2 = *sPtr2;
286
287 sPtr2 += sPlaneStep;
288 dPtr2 += dPlaneStep;
289
290 }
291
292 sPtr1 += sColStep;
293 dPtr1 += dColStep;
294
295 }
296
297 sPtr += sRowStep;
298 dPtr += dRowStep;
299
300 }
301
302 }
303
304 /*****************************************************************************/
305
306 void RefCopyArea16 (const uint16 *sPtr,
307 uint16 *dPtr,
308 uint32 rows,
309 uint32 cols,
310 uint32 planes,
311 int32 sRowStep,
312 int32 sColStep,
313 int32 sPlaneStep,
314 int32 dRowStep,
315 int32 dColStep,
316 int32 dPlaneStep)
317 {
318
319 for (uint32 row = 0; row < rows; row++)
320 {
321
322 const uint16 *sPtr1 = sPtr;
323 uint16 *dPtr1 = dPtr;
324
325 for (uint32 col = 0; col < cols; col++)
326 {
327
328 const uint16 *sPtr2 = sPtr1;
329 uint16 *dPtr2 = dPtr1;
330
331 for (uint32 plane = 0; plane < planes; plane++)
332 {
333
334 *dPtr2 = *sPtr2;
335
336 sPtr2 += sPlaneStep;
337 dPtr2 += dPlaneStep;
338
339 }
340
341 sPtr1 += sColStep;
342 dPtr1 += dColStep;
343
344 }
345
346 sPtr += sRowStep;
347 dPtr += dRowStep;
348
349 }
350
351 }
352
353 /*****************************************************************************/
354
355 void RefCopyArea32 (const uint32 *sPtr,
356 uint32 *dPtr,
357 uint32 rows,
358 uint32 cols,
359 uint32 planes,
360 int32 sRowStep,
361 int32 sColStep,
362 int32 sPlaneStep,
363 int32 dRowStep,
364 int32 dColStep,
365 int32 dPlaneStep)
366 {
367
368 for (uint32 row = 0; row < rows; row++)
369 {
370
371 const uint32 *sPtr1 = sPtr;
372 uint32 *dPtr1 = dPtr;
373
374 for (uint32 col = 0; col < cols; col++)
375 {
376
377 const uint32 *sPtr2 = sPtr1;
378 uint32 *dPtr2 = dPtr1;
379
380 for (uint32 plane = 0; plane < planes; plane++)
381 {
382
383 *dPtr2 = *sPtr2;
384
385 sPtr2 += sPlaneStep;
386 dPtr2 += dPlaneStep;
387
388 }
389
390 sPtr1 += sColStep;
391 dPtr1 += dColStep;
392
393 }
394
395 sPtr += sRowStep;
396 dPtr += dRowStep;
397
398 }
399
400 }
401
402 /*****************************************************************************/
403
404 void RefCopyArea8_16 (const uint8 *sPtr,
405 uint16 *dPtr,
406 uint32 rows,
407 uint32 cols,
408 uint32 planes,
409 int32 sRowStep,
410 int32 sColStep,
411 int32 sPlaneStep,
412 int32 dRowStep,
413 int32 dColStep,
414 int32 dPlaneStep)
415 {
416
417 for (uint32 row = 0; row < rows; row++)
418 {
419
420 const uint8 *sPtr1 = sPtr;
421 uint16 *dPtr1 = dPtr;
422
423 for (uint32 col = 0; col < cols; col++)
424 {
425
426 const uint8 *sPtr2 = sPtr1;
427 uint16 *dPtr2 = dPtr1;
428
429 for (uint32 plane = 0; plane < planes; plane++)
430 {
431
432 *dPtr2 = *sPtr2;
433
434 sPtr2 += sPlaneStep;
435 dPtr2 += dPlaneStep;
436
437 }
438
439 sPtr1 += sColStep;
440 dPtr1 += dColStep;
441
442 }
443
444 sPtr += sRowStep;
445 dPtr += dRowStep;
446
447 }
448
449 }
450
451 /*****************************************************************************/
452
453 void RefCopyArea8_S16 (const uint8 *sPtr,
454 int16 *dPtr,
455 uint32 rows,
456 uint32 cols,
457 uint32 planes,
458 int32 sRowStep,
459 int32 sColStep,
460 int32 sPlaneStep,
461 int32 dRowStep,
462 int32 dColStep,
463 int32 dPlaneStep)
464 {
465
466 for (uint32 row = 0; row < rows; row++)
467 {
468
469 const uint8 *sPtr1 = sPtr;
470 int16 *dPtr1 = dPtr;
471
472 for (uint32 col = 0; col < cols; col++)
473 {
474
475 const uint8 *sPtr2 = sPtr1;
476 int16 *dPtr2 = dPtr1;
477
478 for (uint32 plane = 0; plane < planes; plane++)
479 {
480
481 int16 x = *sPtr2;
482
483 *dPtr2 = x ^ 0x8000;
484
485 sPtr2 += sPlaneStep;
486 dPtr2 += dPlaneStep;
487
488 }
489
490 sPtr1 += sColStep;
491 dPtr1 += dColStep;
492
493 }
494
495 sPtr += sRowStep;
496 dPtr += dRowStep;
497
498 }
499
500 }
501
502 /*****************************************************************************/
503
504 void RefCopyArea8_32 (const uint8 *sPtr,
505 uint32 *dPtr,
506 uint32 rows,
507 uint32 cols,
508 uint32 planes,
509 int32 sRowStep,
510 int32 sColStep,
511 int32 sPlaneStep,
512 int32 dRowStep,
513 int32 dColStep,
514 int32 dPlaneStep)
515 {
516
517 for (uint32 row = 0; row < rows; row++)
518 {
519
520 const uint8 *sPtr1 = sPtr;
521 uint32 *dPtr1 = dPtr;
522
523 for (uint32 col = 0; col < cols; col++)
524 {
525
526 const uint8 *sPtr2 = sPtr1;
527 uint32 *dPtr2 = dPtr1;
528
529 for (uint32 plane = 0; plane < planes; plane++)
530 {
531
532 *dPtr2 = *sPtr2;
533
534 sPtr2 += sPlaneStep;
535 dPtr2 += dPlaneStep;
536
537 }
538
539 sPtr1 += sColStep;
540 dPtr1 += dColStep;
541
542 }
543
544 sPtr += sRowStep;
545 dPtr += dRowStep;
546
547 }
548
549 }
550
551 /*****************************************************************************/
552
553 template <SIMDType simd>
554
555 #ifdef __INTEL_LLVM_COMPILER
556 __attribute__((SET_CPU_FEATURE(simd)))
557 #endif // __INTEL_LLVM_COMPILER
558
559 void RefCopyArea16_S16 (const uint16 *sPtr,
560 int16 *dPtr,
561 uint32 rows,
562 uint32 cols,
563 uint32 planes,
564 int32 sRowStep,
565 int32 sColStep,
566 int32 sPlaneStep,
567 int32 dRowStep,
568 int32 dColStep,
569 int32 dPlaneStep)
570 {
571
572 INTEL_COMPILER_NEEDED_NOTE
573
574 #ifdef __INTEL_COMPILER
575 SET_CPU_FEATURE(simd);
576 #endif // __INTEL_COMPILER
577
578
579 for (uint32 row = 0; row < rows; row++)
580 {
581
582 const uint16 *sPtr1 = sPtr;
583 int16 *dPtr1 = dPtr;
584
585 for (uint32 col = 0; col < cols; col++)
586 {
587
588 const uint16 *sPtr2 = sPtr1;
589 int16 *dPtr2 = dPtr1;
590
591 // Vectorizing if both sPlaneStep and dPlaneStep are 1. Else,
592 // regular operation is performed.
593
594 if (sPlaneStep == 1 && dPlaneStep == 1)
595 {
596
597 INTEL_PRAGMA_SIMD_ASSERT
598 for (uint32 plane = 0; plane < planes; plane++)
599 {
600
601 *dPtr2 = *sPtr2 ^ 0x8000;
602
603 sPtr2 += sPlaneStep;
604 dPtr2 += dPlaneStep;
605 }
606
607 }
608
609 else
610 {
611
612 for (uint32 plane = 0; plane < planes; plane++)
613 {
614
615 *dPtr2 = *sPtr2 ^ 0x8000;
616
617 sPtr2 += sPlaneStep;
618 dPtr2 += dPlaneStep;
619
620 }
621
622 }
623
624 sPtr1 += sColStep;
625 dPtr1 += dColStep;
626
627 }
628
629 sPtr += sRowStep;
630 dPtr += dRowStep;
631
632 }
633
634 }
635
636 /*****************************************************************************/
637
638 INTEL_COMPILER_NEEDED_NOTE
639 #if !qDNGIntelCompiler
640 template
641 void RefCopyArea16_S16<Scalar> (const uint16 *sPtr,
642 int16 *dPtr,
643 uint32 rows,
644 uint32 cols,
645 uint32 planes,
646 int32 sRowStep,
647 int32 sColStep,
648 int32 sPlaneStep,
649 int32 dRowStep,
650 int32 dColStep,
651 int32 dPlaneStep);
652 #else
653 template CopyArea16_S16Proc RefCopyArea16_S16<Scalar>;
654 template CopyArea16_S16Proc RefCopyArea16_S16<AVX2>;
655 #endif
656
657 /*****************************************************************************/
658
659 void RefCopyArea16_32 (const uint16 *sPtr,
660 uint32 *dPtr,
661 uint32 rows,
662 uint32 cols,
663 uint32 planes,
664 int32 sRowStep,
665 int32 sColStep,
666 int32 sPlaneStep,
667 int32 dRowStep,
668 int32 dColStep,
669 int32 dPlaneStep)
670 {
671
672 for (uint32 row = 0; row < rows; row++)
673 {
674
675 const uint16 *sPtr1 = sPtr;
676 uint32 *dPtr1 = dPtr;
677
678 for (uint32 col = 0; col < cols; col++)
679 {
680
681 const uint16 *sPtr2 = sPtr1;
682 uint32 *dPtr2 = dPtr1;
683
684 for (uint32 plane = 0; plane < planes; plane++)
685 {
686
687 *dPtr2 = *sPtr2;
688
689 sPtr2 += sPlaneStep;
690 dPtr2 += dPlaneStep;
691
692 }
693
694 sPtr1 += sColStep;
695 dPtr1 += dColStep;
696
697 }
698
699 sPtr += sRowStep;
700 dPtr += dRowStep;
701
702 }
703
704 }
705
706 /*****************************************************************************/
707
708 void RefCopyArea8_R32 (const uint8 *sPtr,
709 real32 *dPtr,
710 uint32 rows,
711 uint32 cols,
712 uint32 planes,
713 int32 sRowStep,
714 int32 sColStep,
715 int32 sPlaneStep,
716 int32 dRowStep,
717 int32 dColStep,
718 int32 dPlaneStep,
719 uint32 pixelRange)
720 {
721
722 real32 scale = 1.0f / (real32) pixelRange;
723
724 for (uint32 row = 0; row < rows; row++)
725 {
726
727 const uint8 *sPtr1 = sPtr;
728 real32 *dPtr1 = dPtr;
729
730 for (uint32 col = 0; col < cols; col++)
731 {
732
733 const uint8 *sPtr2 = sPtr1;
734 real32 *dPtr2 = dPtr1;
735
736 for (uint32 plane = 0; plane < planes; plane++)
737 {
738
739 *dPtr2 = scale * (real32) *sPtr2;
740
741 sPtr2 += sPlaneStep;
742 dPtr2 += dPlaneStep;
743
744 }
745
746 sPtr1 += sColStep;
747 dPtr1 += dColStep;
748
749 }
750
751 sPtr += sRowStep;
752 dPtr += dRowStep;
753
754 }
755
756 }
757
758 /*****************************************************************************/
759
760 void RefCopyArea16_R32 (const uint16 *sPtr,
761 real32 *dPtr,
762 uint32 rows,
763 uint32 cols,
764 uint32 planes,
765 int32 sRowStep,
766 int32 sColStep,
767 int32 sPlaneStep,
768 int32 dRowStep,
769 int32 dColStep,
770 int32 dPlaneStep,
771 uint32 pixelRange)
772 {
773
774 real32 scale = 1.0f / (real32) pixelRange;
775
776 for (uint32 row = 0; row < rows; row++)
777 {
778
779 const uint16 *sPtr1 = sPtr;
780 real32 *dPtr1 = dPtr;
781
782 for (uint32 col = 0; col < cols; col++)
783 {
784
785 const uint16 *sPtr2 = sPtr1;
786 real32 *dPtr2 = dPtr1;
787
788 for (uint32 plane = 0; plane < planes; plane++)
789 {
790
791 *dPtr2 = scale * (real32) *sPtr2;
792
793 sPtr2 += sPlaneStep;
794 dPtr2 += dPlaneStep;
795
796 }
797
798 sPtr1 += sColStep;
799 dPtr1 += dColStep;
800
801 }
802
803 sPtr += sRowStep;
804 dPtr += dRowStep;
805
806 }
807
808 }
809
810 /*****************************************************************************/
811
812 void RefCopyAreaS16_R32 (const int16 *sPtr,
813 real32 *dPtr,
814 uint32 rows,
815 uint32 cols,
816 uint32 planes,
817 int32 sRowStep,
818 int32 sColStep,
819 int32 sPlaneStep,
820 int32 dRowStep,
821 int32 dColStep,
822 int32 dPlaneStep,
823 uint32 pixelRange)
824 {
825
826 real32 scale = 1.0f / (real32) pixelRange;
827
828 for (uint32 row = 0; row < rows; row++)
829 {
830
831 const int16 *sPtr1 = sPtr;
832 real32 *dPtr1 = dPtr;
833
834 for (uint32 col = 0; col < cols; col++)
835 {
836
837 const int16 *sPtr2 = sPtr1;
838 real32 *dPtr2 = dPtr1;
839
840 for (uint32 plane = 0; plane < planes; plane++)
841 {
842
843 int32 x = *sPtr2;
844
845 *dPtr2 = scale * (real32) (x + 32768);
846
847 sPtr2 += sPlaneStep;
848 dPtr2 += dPlaneStep;
849
850 }
851
852 sPtr1 += sColStep;
853 dPtr1 += dColStep;
854
855 }
856
857 sPtr += sRowStep;
858 dPtr += dRowStep;
859
860 }
861
862 }
863
864 /*****************************************************************************/
865
866 void RefCopyAreaR32_8 (const real32 *sPtr,
867 uint8 *dPtr,
868 uint32 rows,
869 uint32 cols,
870 uint32 planes,
871 int32 sRowStep,
872 int32 sColStep,
873 int32 sPlaneStep,
874 int32 dRowStep,
875 int32 dColStep,
876 int32 dPlaneStep,
877 uint32 pixelRange)
878 {
879
880 real32 scale = (real32) pixelRange;
881
882 for (uint32 row = 0; row < rows; row++)
883 {
884
885 const real32 *sPtr1 = sPtr;
886 uint8 *dPtr1 = dPtr;
887
888 for (uint32 col = 0; col < cols; col++)
889 {
890
891 const real32 *sPtr2 = sPtr1;
892 uint8 *dPtr2 = dPtr1;
893
894 for (uint32 plane = 0; plane < planes; plane++)
895 {
896
897 *dPtr2 = (uint8) (Pin_Overrange (*sPtr2) * scale + 0.5f);
898
899 sPtr2 += sPlaneStep;
900 dPtr2 += dPlaneStep;
901
902 }
903
904 sPtr1 += sColStep;
905 dPtr1 += dColStep;
906
907 }
908
909 sPtr += sRowStep;
910 dPtr += dRowStep;
911
912 }
913
914 }
915
916 /*****************************************************************************/
917
918 void RefCopyAreaR32_16 (const real32 *sPtr,
919 uint16 *dPtr,
920 uint32 rows,
921 uint32 cols,
922 uint32 planes,
923 int32 sRowStep,
924 int32 sColStep,
925 int32 sPlaneStep,
926 int32 dRowStep,
927 int32 dColStep,
928 int32 dPlaneStep,
929 uint32 pixelRange)
930 {
931
932 real32 scale = (real32) pixelRange;
933
934 for (uint32 row = 0; row < rows; row++)
935 {
936
937 const real32 *sPtr1 = sPtr;
938 uint16 *dPtr1 = dPtr;
939
940 for (uint32 col = 0; col < cols; col++)
941 {
942
943 const real32 *sPtr2 = sPtr1;
944 uint16 *dPtr2 = dPtr1;
945
946 for (uint32 plane = 0; plane < planes; plane++)
947 {
948
949 *dPtr2 = (uint16) (Pin_Overrange (*sPtr2) * scale + 0.5f);
950
951 sPtr2 += sPlaneStep;
952 dPtr2 += dPlaneStep;
953
954 }
955
956 sPtr1 += sColStep;
957 dPtr1 += dColStep;
958
959 }
960
961 sPtr += sRowStep;
962 dPtr += dRowStep;
963
964 }
965
966 }
967
968 /*****************************************************************************/
969
970 void RefCopyAreaR32_S16 (const real32 *sPtr,
971 int16 *dPtr,
972 uint32 rows,
973 uint32 cols,
974 uint32 planes,
975 int32 sRowStep,
976 int32 sColStep,
977 int32 sPlaneStep,
978 int32 dRowStep,
979 int32 dColStep,
980 int32 dPlaneStep,
981 uint32 pixelRange)
982 {
983
984 real32 scale = (real32) pixelRange;
985
986 for (uint32 row = 0; row < rows; row++)
987 {
988
989 const real32 *sPtr1 = sPtr;
990 int16 *dPtr1 = dPtr;
991
992 for (uint32 col = 0; col < cols; col++)
993 {
994
995 const real32 *sPtr2 = sPtr1;
996 int16 *dPtr2 = dPtr1;
997
998 for (uint32 plane = 0; plane < planes; plane++)
999 {
1000
1001 int32 x = (int32) (Pin_Overrange (*sPtr2) * scale + 0.5f);
1002
1003 *dPtr2 = (int16) (x ^ 0x8000);
1004
1005 sPtr2 += sPlaneStep;
1006 dPtr2 += dPlaneStep;
1007
1008 }
1009
1010 sPtr1 += sColStep;
1011 dPtr1 += dColStep;
1012
1013 }
1014
1015 sPtr += sRowStep;
1016 dPtr += dRowStep;
1017
1018 }
1019
1020 }
1021
1022 /*****************************************************************************/
1023
1024 void RefRepeatArea8 (const uint8 *sPtr,
1025 uint8 *dPtr,
1026 uint32 rows,
1027 uint32 cols,
1028 uint32 planes,
1029 int32 rowStep,
1030 int32 colStep,
1031 int32 planeStep,
1032 uint32 repeatV,
1033 uint32 repeatH,
1034 uint32 phaseV,
1035 uint32 phaseH)
1036 {
1037
1038 const uint8 *sPtr0 = sPtr + phaseV * rowStep +
1039 phaseH * colStep;
1040
1041 int32 backStepV = (repeatV - 1) * rowStep;
1042 int32 backStepH = (repeatH - 1) * colStep;
1043
1044 for (uint32 row = 0; row < rows; row++)
1045 {
1046
1047 const uint8 *sPtr1 = sPtr0;
1048 uint8 *dPtr1 = dPtr;
1049
1050 uint32 colPhase = phaseH;
1051
1052 for (uint32 col = 0; col < cols; col++)
1053 {
1054
1055 const uint8 *sPtr2 = sPtr1;
1056 uint8 *dPtr2 = dPtr1;
1057
1058 for (uint32 plane = 0; plane < planes; plane++)
1059 {
1060
1061 *dPtr2 = *sPtr2;
1062
1063 sPtr2 += planeStep;
1064 dPtr2 += planeStep;
1065
1066 }
1067
1068 if (++colPhase == repeatH)
1069 {
1070 colPhase = 0;
1071 sPtr1 -= backStepH;
1072 }
1073 else
1074 {
1075 sPtr1 += colStep;
1076 }
1077
1078 dPtr1 += colStep;
1079
1080 }
1081
1082 if (++phaseV == repeatV)
1083 {
1084 phaseV = 0;
1085 sPtr0 -= backStepV;
1086 }
1087 else
1088 {
1089 sPtr0 += rowStep;
1090 }
1091
1092 dPtr += rowStep;
1093
1094 }
1095
1096 }
1097
1098 /*****************************************************************************/
1099
1100 void RefRepeatArea16 (const uint16 *sPtr,
1101 uint16 *dPtr,
1102 uint32 rows,
1103 uint32 cols,
1104 uint32 planes,
1105 int32 rowStep,
1106 int32 colStep,
1107 int32 planeStep,
1108 uint32 repeatV,
1109 uint32 repeatH,
1110 uint32 phaseV,
1111 uint32 phaseH)
1112 {
1113
1114 const uint16 *sPtr0 = sPtr + phaseV * rowStep +
1115 phaseH * colStep;
1116
1117 int32 backStepV = (repeatV - 1) * rowStep;
1118 int32 backStepH = (repeatH - 1) * colStep;
1119
1120 for (uint32 row = 0; row < rows; row++)
1121 {
1122
1123 const uint16 *sPtr1 = sPtr0;
1124 uint16 *dPtr1 = dPtr;
1125
1126 uint32 colPhase = phaseH;
1127
1128 for (uint32 col = 0; col < cols; col++)
1129 {
1130
1131 const uint16 *sPtr2 = sPtr1;
1132 uint16 *dPtr2 = dPtr1;
1133
1134 for (uint32 plane = 0; plane < planes; plane++)
1135 {
1136
1137 *dPtr2 = *sPtr2;
1138
1139 sPtr2 += planeStep;
1140 dPtr2 += planeStep;
1141
1142 }
1143
1144 if (++colPhase == repeatH)
1145 {
1146 colPhase = 0;
1147 sPtr1 -= backStepH;
1148 }
1149 else
1150 {
1151 sPtr1 += colStep;
1152 }
1153
1154 dPtr1 += colStep;
1155
1156 }
1157
1158 if (++phaseV == repeatV)
1159 {
1160 phaseV = 0;
1161 sPtr0 -= backStepV;
1162 }
1163 else
1164 {
1165 sPtr0 += rowStep;
1166 }
1167
1168 dPtr += rowStep;
1169
1170 }
1171
1172 }
1173
1174 /*****************************************************************************/
1175
1176 void RefRepeatArea32 (const uint32 *sPtr,
1177 uint32 *dPtr,
1178 uint32 rows,
1179 uint32 cols,
1180 uint32 planes,
1181 int32 rowStep,
1182 int32 colStep,
1183 int32 planeStep,
1184 uint32 repeatV,
1185 uint32 repeatH,
1186 uint32 phaseV,
1187 uint32 phaseH)
1188 {
1189
1190 const uint32 *sPtr0 = sPtr + phaseV * rowStep +
1191 phaseH * colStep;
1192
1193 int32 backStepV = (repeatV - 1) * rowStep;
1194 int32 backStepH = (repeatH - 1) * colStep;
1195
1196 for (uint32 row = 0; row < rows; row++)
1197 {
1198
1199 const uint32 *sPtr1 = sPtr0;
1200 uint32 *dPtr1 = dPtr;
1201
1202 uint32 colPhase = phaseH;
1203
1204 for (uint32 col = 0; col < cols; col++)
1205 {
1206
1207 const uint32 *sPtr2 = sPtr1;
1208 uint32 *dPtr2 = dPtr1;
1209
1210 for (uint32 plane = 0; plane < planes; plane++)
1211 {
1212
1213 *dPtr2 = *sPtr2;
1214
1215 sPtr2 += planeStep;
1216 dPtr2 += planeStep;
1217
1218 }
1219
1220 if (++colPhase == repeatH)
1221 {
1222 colPhase = 0;
1223 sPtr1 -= backStepH;
1224 }
1225 else
1226 {
1227 sPtr1 += colStep;
1228 }
1229
1230 dPtr1 += colStep;
1231
1232 }
1233
1234 if (++phaseV == repeatV)
1235 {
1236 phaseV = 0;
1237 sPtr0 -= backStepV;
1238 }
1239 else
1240 {
1241 sPtr0 += rowStep;
1242 }
1243
1244 dPtr += rowStep;
1245
1246 }
1247
1248 }
1249
1250 /*****************************************************************************/
1251
1252 void RefShiftRight16 (uint16 *dPtr,
1253 uint32 rows,
1254 uint32 cols,
1255 uint32 planes,
1256 int32 rowStep,
1257 int32 colStep,
1258 int32 planeStep,
1259 uint32 shift)
1260 {
1261
1262 for (uint32 row = 0; row < rows; row++)
1263 {
1264
1265 uint16 *dPtr1 = dPtr;
1266
1267 for (uint32 col = 0; col < cols; col++)
1268 {
1269
1270 uint16 *dPtr2 = dPtr1;
1271
1272 for (uint32 plane = 0; plane < planes; plane++)
1273 {
1274
1275 *dPtr2 >>= shift;
1276
1277 dPtr2 += planeStep;
1278
1279 }
1280
1281 dPtr1 += colStep;
1282
1283 }
1284
1285 dPtr += rowStep;
1286
1287 }
1288
1289 }
1290
1291 /*****************************************************************************/
1292
1293 void RefBilinearRow16 (const uint16 *sPtr,
1294 uint16 *dPtr,
1295 uint32 cols,
1296 uint32 patPhase,
1297 uint32 patCount,
1298 const uint32 * kernCounts,
1299 const int32 * const * kernOffsets,
1300 const uint16 * const * kernWeights,
1301 uint32 sShift)
1302 {
1303
1304 for (uint32 j = 0; j < cols; j++)
1305 {
1306
1307 const uint16 *p = sPtr + (j >> sShift);
1308
1309 uint32 count = kernCounts [patPhase];
1310
1311 const int32 *offsets = kernOffsets [patPhase];
1312 const uint16 *weights = kernWeights [patPhase];
1313
1314 if (++patPhase == patCount)
1315 {
1316 patPhase = 0;
1317 }
1318
1319 uint32 total = 128;
1320
1321 for (uint32 k = 0; k < count; k++)
1322 {
1323
1324 int32 offset = offsets [k];
1325 uint32 weight = weights [k];
1326
1327 uint32 pixel = p [offset];
1328
1329 total += pixel * weight;
1330
1331 }
1332
1333 dPtr [j] = (uint16) (total >> 8);
1334
1335 }
1336
1337 }
1338
1339 /*****************************************************************************/
1340
1341 void RefBilinearRow32 (const real32 *sPtr,
1342 real32 *dPtr,
1343 uint32 cols,
1344 uint32 patPhase,
1345 uint32 patCount,
1346 const uint32 * kernCounts,
1347 const int32 * const * kernOffsets,
1348 const real32 * const * kernWeights,
1349 uint32 sShift)
1350 {
1351
1352 for (uint32 j = 0; j < cols; j++)
1353 {
1354
1355 const real32 *p = sPtr + (j >> sShift);
1356
1357 uint32 count = kernCounts [patPhase];
1358
1359 const int32 *offsets = kernOffsets [patPhase];
1360 const real32 *weights = kernWeights [patPhase];
1361
1362 if (++patPhase == patCount)
1363 {
1364 patPhase = 0;
1365 }
1366
1367 real32 total = 0.0f;
1368
1369 for (uint32 k = 0; k < count; k++)
1370 {
1371
1372 int32 offset = offsets [k];
1373 real32 weight = weights [k];
1374
1375 real32 pixel = p [offset];
1376
1377 total += pixel * weight;
1378
1379 }
1380
1381 dPtr [j] = total;
1382
1383 }
1384
1385 }
1386
1387 /*****************************************************************************/
1388
1389 void RefBaselineABCtoRGB (const real32 *sPtrA,
1390 const real32 *sPtrB,
1391 const real32 *sPtrC,
1392 real32 *dPtrR,
1393 real32 *dPtrG,
1394 real32 *dPtrB,
1395 uint32 count,
1396 const dng_vector &cameraWhite,
1397 const dng_matrix &cameraToRGB)
1398 {
1399
1400 real32 clipA = (real32) cameraWhite [0];
1401 real32 clipB = (real32) cameraWhite [1];
1402 real32 clipC = (real32) cameraWhite [2];
1403
1404 real32 m00 = (real32) cameraToRGB [0] [0];
1405 real32 m01 = (real32) cameraToRGB [0] [1];
1406 real32 m02 = (real32) cameraToRGB [0] [2];
1407
1408 real32 m10 = (real32) cameraToRGB [1] [0];
1409 real32 m11 = (real32) cameraToRGB [1] [1];
1410 real32 m12 = (real32) cameraToRGB [1] [2];
1411
1412 real32 m20 = (real32) cameraToRGB [2] [0];
1413 real32 m21 = (real32) cameraToRGB [2] [1];
1414 real32 m22 = (real32) cameraToRGB [2] [2];
1415
1416 for (uint32 col = 0; col < count; col++)
1417 {
1418
1419 real32 A = sPtrA [col];
1420 real32 B = sPtrB [col];
1421 real32 C = sPtrC [col];
1422
1423 A = Min_real32 (A, clipA);
1424 B = Min_real32 (B, clipB);
1425 C = Min_real32 (C, clipC);
1426
1427 real32 r = m00 * A + m01 * B + m02 * C;
1428 real32 g = m10 * A + m11 * B + m12 * C;
1429 real32 b = m20 * A + m21 * B + m22 * C;
1430
1431 r = Pin_real32 (0.0f, r, 1.0f);
1432 g = Pin_real32 (0.0f, g, 1.0f);
1433 b = Pin_real32 (0.0f, b, 1.0f);
1434
1435 dPtrR [col] = r;
1436 dPtrG [col] = g;
1437 dPtrB [col] = b;
1438
1439 }
1440
1441 }
1442
1443 /*****************************************************************************/
1444
1445 void RefBaselineABCDtoRGB (const real32 *sPtrA,
1446 const real32 *sPtrB,
1447 const real32 *sPtrC,
1448 const real32 *sPtrD,
1449 real32 *dPtrR,
1450 real32 *dPtrG,
1451 real32 *dPtrB,
1452 uint32 count,
1453 const dng_vector &cameraWhite,
1454 const dng_matrix &cameraToRGB)
1455 {
1456
1457 real32 clipA = (real32) cameraWhite [0];
1458 real32 clipB = (real32) cameraWhite [1];
1459 real32 clipC = (real32) cameraWhite [2];
1460 real32 clipD = (real32) cameraWhite [3];
1461
1462 real32 m00 = (real32) cameraToRGB [0] [0];
1463 real32 m01 = (real32) cameraToRGB [0] [1];
1464 real32 m02 = (real32) cameraToRGB [0] [2];
1465 real32 m03 = (real32) cameraToRGB [0] [3];
1466
1467 real32 m10 = (real32) cameraToRGB [1] [0];
1468 real32 m11 = (real32) cameraToRGB [1] [1];
1469 real32 m12 = (real32) cameraToRGB [1] [2];
1470 real32 m13 = (real32) cameraToRGB [1] [3];
1471
1472 real32 m20 = (real32) cameraToRGB [2] [0];
1473 real32 m21 = (real32) cameraToRGB [2] [1];
1474 real32 m22 = (real32) cameraToRGB [2] [2];
1475 real32 m23 = (real32) cameraToRGB [2] [3];
1476
1477 for (uint32 col = 0; col < count; col++)
1478 {
1479
1480 real32 A = sPtrA [col];
1481 real32 B = sPtrB [col];
1482 real32 C = sPtrC [col];
1483 real32 D = sPtrD [col];
1484
1485 A = Min_real32 (A, clipA);
1486 B = Min_real32 (B, clipB);
1487 C = Min_real32 (C, clipC);
1488 D = Min_real32 (D, clipD);
1489
1490 real32 r = m00 * A + m01 * B + m02 * C + m03 * D;
1491 real32 g = m10 * A + m11 * B + m12 * C + m13 * D;
1492 real32 b = m20 * A + m21 * B + m22 * C + m23 * D;
1493
1494 r = Pin_real32 (0.0f, r, 1.0f);
1495 g = Pin_real32 (0.0f, g, 1.0f);
1496 b = Pin_real32 (0.0f, b, 1.0f);
1497
1498 dPtrR [col] = r;
1499 dPtrG [col] = g;
1500 dPtrB [col] = b;
1501
1502 }
1503
1504 }
1505
1506 /*****************************************************************************/
1507
1508 void RefBaselineHueSatMap (const real32 *sPtrR,
1509 const real32 *sPtrG,
1510 const real32 *sPtrB,
1511 real32 *dPtrR,
1512 real32 *dPtrG,
1513 real32 *dPtrB,
1514 uint32 count,
1515 const dng_hue_sat_map &lut,
1516 const dng_1d_table *encodeTable,
1517 const dng_1d_table *decodeTable,
1518 const bool supportOverrange)
1519 {
1520
1521 uint32 hueDivisions;
1522 uint32 satDivisions;
1523 uint32 valDivisions;
1524
1525 lut.GetDivisions (hueDivisions,
1526 satDivisions,
1527 valDivisions);
1528
1529 real32 hScale = (hueDivisions < 2) ? 0.0f : (hueDivisions * (1.0f / 6.0f));
1530 real32 sScale = (real32) ((int32) satDivisions - 1);
1531 real32 vScale = (real32) ((int32) valDivisions - 1);
1532
1533 int32 maxHueIndex0 = (int32) hueDivisions - 1;
1534 int32 maxSatIndex0 = (int32) satDivisions - 2;
1535 int32 maxValIndex0 = (int32) valDivisions - 2;
1536
1537 const bool hasEncodeTable = ((encodeTable != NULL) && (encodeTable->Table () != NULL));
1538 const bool hasDecodeTable = ((decodeTable != NULL) && (decodeTable->Table () != NULL));
1539
1540 const bool hasTable = hasEncodeTable && hasDecodeTable;
1541
1542 const dng_hue_sat_map::HSBModify *tableBase = lut.GetConstDeltas ();
1543
1544 int32 hueStep = satDivisions;
1545 int32 valStep = hueDivisions * hueStep;
1546
1547 #if 0 // Not required with "2.5D" table optimization.
1548
1549 if (valDivisions < 2)
1550 {
1551 valStep = 0;
1552 maxValIndex0 = 0;
1553 }
1554
1555 #endif
1556
1557 for (uint32 j = 0; j < count; j++)
1558 {
1559
1560 real32 r = sPtrR [j];
1561 real32 g = sPtrG [j];
1562 real32 b = sPtrB [j];
1563
1564 if (supportOverrange)
1565 {
1566
1567 r = Max_real32 (r, 0.0f);
1568 g = Max_real32 (g, 0.0f);
1569 b = Max_real32 (b, 0.0f);
1570
1571 if (valDivisions > 1)
1572 {
1573 r = EncodeOverrange (r);
1574 g = EncodeOverrange (g);
1575 b = EncodeOverrange (b);
1576 }
1577
1578 }
1579
1580 real32 h, s, v;
1581
1582 DNG_RGBtoHSV (r, g, b, h, s, v);
1583
1584 real32 vEncoded = v;
1585
1586 real32 hueShift;
1587 real32 satScale;
1588 real32 valScale;
1589
1590 if (valDivisions < 2) // Optimize most common case of "2.5D" table.
1591 {
1592
1593 real32 hScaled = h * hScale;
1594 real32 sScaled = s * sScale;
1595
1596 int32 hIndex0 = (int32) hScaled;
1597 int32 sIndex0 = (int32) sScaled;
1598
1599 sIndex0 = Min_int32 (sIndex0, maxSatIndex0);
1600
1601 int32 hIndex1 = hIndex0 + 1;
1602
1603 if (hIndex0 >= maxHueIndex0)
1604 {
1605 hIndex0 = maxHueIndex0;
1606 hIndex1 = 0;
1607 }
1608
1609 real32 hFract1 = hScaled - (real32) hIndex0;
1610 real32 sFract1 = sScaled - (real32) sIndex0;
1611
1612 real32 hFract0 = 1.0f - hFract1;
1613 real32 sFract0 = 1.0f - sFract1;
1614
1615 const dng_hue_sat_map::HSBModify *entry00 = tableBase + hIndex0 * hueStep +
1616 sIndex0;
1617
1618 const dng_hue_sat_map::HSBModify *entry01 = entry00 + (hIndex1 - hIndex0) * hueStep;
1619
1620 real32 hueShift0 = hFract0 * entry00->fHueShift +
1621 hFract1 * entry01->fHueShift;
1622
1623 real32 satScale0 = hFract0 * entry00->fSatScale +
1624 hFract1 * entry01->fSatScale;
1625
1626 real32 valScale0 = hFract0 * entry00->fValScale +
1627 hFract1 * entry01->fValScale;
1628
1629 entry00++;
1630 entry01++;
1631
1632 real32 hueShift1 = hFract0 * entry00->fHueShift +
1633 hFract1 * entry01->fHueShift;
1634
1635 real32 satScale1 = hFract0 * entry00->fSatScale +
1636 hFract1 * entry01->fSatScale;
1637
1638 real32 valScale1 = hFract0 * entry00->fValScale +
1639 hFract1 * entry01->fValScale;
1640
1641 hueShift = sFract0 * hueShift0 + sFract1 * hueShift1;
1642 satScale = sFract0 * satScale0 + sFract1 * satScale1;
1643 valScale = sFract0 * valScale0 + sFract1 * valScale1;
1644
1645 }
1646
1647 else
1648 {
1649
1650 if (hasTable)
1651 {
1652 vEncoded = encodeTable->Interpolate (Pin_real32 (v));
1653 }
1654
1655 real32 hScaled = h * hScale;
1656 real32 sScaled = s * sScale;
1657 real32 vScaled = vEncoded * vScale;
1658
1659 int32 hIndex0 = (int32) hScaled;
1660 int32 sIndex0 = (int32) sScaled;
1661 int32 vIndex0 = (int32) vScaled;
1662
1663 sIndex0 = Min_int32 (sIndex0, maxSatIndex0);
1664 vIndex0 = Min_int32 (vIndex0, maxValIndex0);
1665
1666 int32 hIndex1 = hIndex0 + 1;
1667
1668 if (hIndex0 >= maxHueIndex0)
1669 {
1670 hIndex0 = maxHueIndex0;
1671 hIndex1 = 0;
1672 }
1673
1674 real32 hFract1 = hScaled - (real32) hIndex0;
1675 real32 sFract1 = sScaled - (real32) sIndex0;
1676 real32 vFract1 = vScaled - (real32) vIndex0;
1677
1678 real32 hFract0 = 1.0f - hFract1;
1679 real32 sFract0 = 1.0f - sFract1;
1680 real32 vFract0 = 1.0f - vFract1;
1681
1682 const dng_hue_sat_map::HSBModify *entry00 = tableBase + vIndex0 * valStep +
1683 hIndex0 * hueStep +
1684 sIndex0;
1685
1686 const dng_hue_sat_map::HSBModify *entry01 = entry00 + (hIndex1 - hIndex0) * hueStep;
1687
1688 const dng_hue_sat_map::HSBModify *entry10 = entry00 + valStep;
1689 const dng_hue_sat_map::HSBModify *entry11 = entry01 + valStep;
1690
1691 real32 hueShift0 = vFract0 * (hFract0 * entry00->fHueShift +
1692 hFract1 * entry01->fHueShift) +
1693 vFract1 * (hFract0 * entry10->fHueShift +
1694 hFract1 * entry11->fHueShift);
1695
1696 real32 satScale0 = vFract0 * (hFract0 * entry00->fSatScale +
1697 hFract1 * entry01->fSatScale) +
1698 vFract1 * (hFract0 * entry10->fSatScale +
1699 hFract1 * entry11->fSatScale);
1700
1701 real32 valScale0 = vFract0 * (hFract0 * entry00->fValScale +
1702 hFract1 * entry01->fValScale) +
1703 vFract1 * (hFract0 * entry10->fValScale +
1704 hFract1 * entry11->fValScale);
1705
1706 entry00++;
1707 entry01++;
1708 entry10++;
1709 entry11++;
1710
1711 real32 hueShift1 = vFract0 * (hFract0 * entry00->fHueShift +
1712 hFract1 * entry01->fHueShift) +
1713 vFract1 * (hFract0 * entry10->fHueShift +
1714 hFract1 * entry11->fHueShift);
1715
1716 real32 satScale1 = vFract0 * (hFract0 * entry00->fSatScale +
1717 hFract1 * entry01->fSatScale) +
1718 vFract1 * (hFract0 * entry10->fSatScale +
1719 hFract1 * entry11->fSatScale);
1720
1721 real32 valScale1 = vFract0 * (hFract0 * entry00->fValScale +
1722 hFract1 * entry01->fValScale) +
1723 vFract1 * (hFract0 * entry10->fValScale +
1724 hFract1 * entry11->fValScale);
1725
1726 hueShift = sFract0 * hueShift0 + sFract1 * hueShift1;
1727 satScale = sFract0 * satScale0 + sFract1 * satScale1;
1728 valScale = sFract0 * valScale0 + sFract1 * valScale1;
1729
1730 }
1731
1732 hueShift *= (6.0f / 360.0f); // Convert to internal hue range.
1733
1734 h += hueShift;
1735
1736 s = Min_real32 (s * satScale, 1.0f);
1737
1738 vEncoded = Pin_real32 (vEncoded * valScale);
1739
1740 v = hasTable ? decodeTable->Interpolate (vEncoded) : vEncoded;
1741
1742 DNG_HSVtoRGB (h, s, v, r, g, b);
1743
1744 if (supportOverrange && (valDivisions > 1))
1745 {
1746
1747 r = DecodeOverrange (r);
1748 g = DecodeOverrange (g);
1749 b = DecodeOverrange (b);
1750
1751 }
1752
1753 dPtrR [j] = r;
1754 dPtrG [j] = g;
1755 dPtrB [j] = b;
1756
1757 }
1758
1759 }
1760
1761 /*****************************************************************************/
1762
1763 void RefBaselineRGBtoGray (const real32 *sPtrR,
1764 const real32 *sPtrG,
1765 const real32 *sPtrB,
1766 real32 *dPtrG,
1767 uint32 count,
1768 const dng_matrix &matrix,
1769 const bool supportOverrange)
1770 {
1771
1772 real32 m00 = (real32) matrix [0] [0];
1773 real32 m01 = (real32) matrix [0] [1];
1774 real32 m02 = (real32) matrix [0] [2];
1775
1776 for (uint32 col = 0; col < count; col++)
1777 {
1778
1779 real32 R = sPtrR [col];
1780 real32 G = sPtrG [col];
1781 real32 B = sPtrB [col];
1782
1783 real32 g = m00 * R + m01 * G + m02 * B;
1784
1785 if (!supportOverrange)
1786 g = Pin_real32 (0.0f, g, 1.0f);
1787
1788 dPtrG [col] = g;
1789
1790 }
1791
1792 }
1793
1794 /*****************************************************************************/
1795
1796 void RefBaselineRGBtoRGB (const real32 *sPtrR,
1797 const real32 *sPtrG,
1798 const real32 *sPtrB,
1799 real32 *dPtrR,
1800 real32 *dPtrG,
1801 real32 *dPtrB,
1802 uint32 count,
1803 const dng_matrix &matrix,
1804 const bool supportOverrange)
1805 {
1806
1807 real32 m00 = (real32) matrix [0] [0];
1808 real32 m01 = (real32) matrix [0] [1];
1809 real32 m02 = (real32) matrix [0] [2];
1810
1811 real32 m10 = (real32) matrix [1] [0];
1812 real32 m11 = (real32) matrix [1] [1];
1813 real32 m12 = (real32) matrix [1] [2];
1814
1815 real32 m20 = (real32) matrix [2] [0];
1816 real32 m21 = (real32) matrix [2] [1];
1817 real32 m22 = (real32) matrix [2] [2];
1818
1819 for (uint32 col = 0; col < count; col++)
1820 {
1821
1822 real32 R = sPtrR [col];
1823 real32 G = sPtrG [col];
1824 real32 B = sPtrB [col];
1825
1826 real32 r = m00 * R + m01 * G + m02 * B;
1827 real32 g = m10 * R + m11 * G + m12 * B;
1828 real32 b = m20 * R + m21 * G + m22 * B;
1829
1830 if (!supportOverrange)
1831 {
1832 r = Pin_real32 (0.0f, r, 1.0f);
1833 g = Pin_real32 (0.0f, g, 1.0f);
1834 b = Pin_real32 (0.0f, b, 1.0f);
1835 }
1836
1837 dPtrR [col] = r;
1838 dPtrG [col] = g;
1839 dPtrB [col] = b;
1840
1841 }
1842
1843 }
1844
1845 /*****************************************************************************/
1846
1847 void RefBaseline1DTable (const real32 *sPtr,
1848 real32 *dPtr,
1849 uint32 count,
1850 const dng_1d_table &table)
1851 {
1852
1853 for (uint32 col = 0; col < count; col++)
1854 {
1855
1856 real32 x = sPtr [col];
1857
1858 real32 y = table.Interpolate (Pin_real32 (x));
1859
1860 dPtr [col] = y;
1861
1862 }
1863
1864 }
1865
1866 /*****************************************************************************/
1867
1868 void RefBaselineRGBTone (const real32 *sPtrR,
1869 const real32 *sPtrG,
1870 const real32 *sPtrB,
1871 real32 *dPtrR,
1872 real32 *dPtrG,
1873 real32 *dPtrB,
1874 uint32 count,
1875 const dng_1d_table &table)
1876 {
1877
1878 for (uint32 col = 0; col < count; col++)
1879 {
1880
1881 real32 r = sPtrR [col];
1882 real32 g = sPtrG [col];
1883 real32 b = sPtrB [col];
1884
1885 r = Pin_real32 (r);
1886 g = Pin_real32 (g);
1887 b = Pin_real32 (b);
1888
1889 real32 rr;
1890 real32 gg;
1891 real32 bb;
1892
1893 #define RGBTone(r, g, b, rr, gg, bb)\
1894 {\
1895 \
1896 DNG_ASSERT (r >= g && g >= b && r > b, "Logic Error RGBTone");\
1897 \
1898 rr = table.Interpolate (r);\
1899 bb = table.Interpolate (b);\
1900 \
1901 gg = bb + ((rr - bb) * (g - b) / (r - b));\
1902 \
1903 }
1904
1905 if (r >= g)
1906 {
1907
1908 if (g > b)
1909 {
1910
1911 // Case 1: r >= g > b
1912
1913 RGBTone (r, g, b, rr, gg, bb);
1914
1915 }
1916
1917 else if (b > r)
1918 {
1919
1920 // Case 2: b > r >= g
1921
1922 RGBTone (b, r, g, bb, rr, gg);
1923
1924 }
1925
1926 else if (b > g)
1927 {
1928
1929 // Case 3: r >= b > g
1930
1931 RGBTone (r, b, g, rr, bb, gg);
1932
1933 }
1934
1935 else
1936 {
1937
1938 // Case 4: r >= g == b
1939
1940 DNG_ASSERT (r >= g && g == b, "Logic Error 2");
1941
1942 rr = table.Interpolate (r);
1943 gg = table.Interpolate (g);
1944 bb = gg;
1945
1946 }
1947
1948 }
1949
1950 else
1951 {
1952
1953 if (r >= b)
1954 {
1955
1956 // Case 5: g > r >= b
1957
1958 RGBTone (g, r, b, gg, rr, bb);
1959
1960 }
1961
1962 else if (b > g)
1963 {
1964
1965 // Case 6: b > g > r
1966
1967 RGBTone (b, g, r, bb, gg, rr);
1968
1969 }
1970
1971 else
1972 {
1973
1974 // Case 7: g >= b > r
1975
1976 RGBTone (g, b, r, gg, bb, rr);
1977
1978 }
1979
1980 }
1981
1982 #undef RGBTone
1983
1984 dPtrR [col] = rr;
1985 dPtrG [col] = gg;
1986 dPtrB [col] = bb;
1987
1988 }
1989
1990 }
1991
1992 /*****************************************************************************/
1993
1994 void RefResampleDown16 (const uint16 *sPtr,
1995 uint16 *dPtr,
1996 uint32 sCount,
1997 int32 sRowStep,
1998 const int16 *wPtr,
1999 uint32 wCount,
2000 uint32 pixelRange)
2001 {
2002
2003 for (uint32 j = 0; j < sCount; j++)
2004 {
2005
2006 int32 total = 8192;
2007
2008 const uint16 *s = sPtr + j;
2009
2010 for (uint32 k = 0; k < wCount; k++)
2011 {
2012
2013 total += wPtr [k] * (int32) s [0];
2014
2015 s += sRowStep;
2016
2017 }
2018
2019 dPtr [j] = (uint16) Pin_int32 (0,
2020 total >> 14,
2021 pixelRange);
2022
2023 }
2024
2025 }
2026
2027 /*****************************************************************************/
2028
2029 void RefResampleDown32 (const real32 *sPtr,
2030 real32 *dPtr,
2031 uint32 sCount,
2032 int32 sRowStep,
2033 const real32 *wPtr,
2034 uint32 wCount)
2035 {
2036
2037 uint32 col;
2038
2039 // Process first row.
2040
2041 real32 w = wPtr [0];
2042
2043 for (col = 0; col < sCount; col++)
2044 {
2045
2046 dPtr [col] = w * sPtr [col];
2047
2048 }
2049
2050 sPtr += sRowStep;
2051
2052 // Process middle rows.
2053
2054 for (uint32 j = 1; j < wCount - 1; j++)
2055 {
2056
2057 w = wPtr [j];
2058
2059 for (col = 0; col < sCount; col++)
2060 {
2061
2062 dPtr [col] += w * sPtr [col];
2063
2064 }
2065
2066 sPtr += sRowStep;
2067
2068 }
2069
2070 // Process last row.
2071
2072 w = wPtr [wCount - 1];
2073
2074 for (col = 0; col < sCount; col++)
2075 {
2076
2077 dPtr [col] = Pin_real32 (0.0f,
2078 dPtr [col] + w * sPtr [col],
2079 1.0f);
2080
2081 }
2082
2083 }
2084
2085 /******************************************************************************/
2086
2087 void RefResampleAcross16 (const uint16 *sPtr,
2088 uint16 *dPtr,
2089 uint32 dCount,
2090 const int32 *coord,
2091 const int16 *wPtr,
2092 uint32 wCount,
2093 uint32 wStep,
2094 uint32 pixelRange)
2095 {
2096
2097 for (uint32 j = 0; j < dCount; j++)
2098 {
2099
2100 int32 sCoord = coord [j];
2101
2102 int32 sFract = sCoord & kResampleSubsampleMask;
2103 int32 sPixel = sCoord >> kResampleSubsampleBits;
2104
2105 const int16 *w = wPtr + sFract * wStep;
2106 const uint16 *s = sPtr + sPixel;
2107
2108 int32 total = w [0] * (int32) s [0];
2109
2110 for (uint32 k = 1; k < wCount; k++)
2111 {
2112
2113 total += w [k] * (int32) s [k];
2114
2115 }
2116
2117 dPtr [j] = (uint16) Pin_int32 (0,
2118 (total + 8192) >> 14,
2119 pixelRange);
2120
2121 }
2122
2123 }
2124
2125 /******************************************************************************/
2126
2127 void RefResampleAcross32 (const real32 *sPtr,
2128 real32 *dPtr,
2129 uint32 dCount,
2130 const int32 *coord,
2131 const real32 *wPtr,
2132 uint32 wCount,
2133 uint32 wStep)
2134 {
2135
2136 for (uint32 j = 0; j < dCount; j++)
2137 {
2138
2139 int32 sCoord = coord [j];
2140
2141 int32 sFract = sCoord & kResampleSubsampleMask;
2142 int32 sPixel = sCoord >> kResampleSubsampleBits;
2143
2144 const real32 *w = wPtr + sFract * wStep;
2145 const real32 *s = sPtr + sPixel;
2146
2147 real32 total = w [0] * s [0];
2148
2149 for (uint32 k = 1; k < wCount; k++)
2150 {
2151
2152 total += w [k] * s [k];
2153
2154 }
2155
2156 dPtr [j] = Pin_real32 (0.0f, total, 1.0f);
2157
2158 }
2159
2160 }
2161
2162 /*****************************************************************************/
2163
2164 bool RefEqualBytes (const void *sPtr,
2165 const void *dPtr,
2166 uint32 count)
2167 {
2168
2169 return memcmp (dPtr, sPtr, count) == 0;
2170
2171 }
2172
2173 /*****************************************************************************/
2174
2175 bool RefEqualArea8 (const uint8 *sPtr,
2176 const uint8 *dPtr,
2177 uint32 rows,
2178 uint32 cols,
2179 uint32 planes,
2180 int32 sRowStep,
2181 int32 sColStep,
2182 int32 sPlaneStep,
2183 int32 dRowStep,
2184 int32 dColStep,
2185 int32 dPlaneStep)
2186 {
2187
2188 for (uint32 row = 0; row < rows; row++)
2189 {
2190
2191 const uint8 *sPtr1 = sPtr;
2192 const uint8 *dPtr1 = dPtr;
2193
2194 for (uint32 col = 0; col < cols; col++)
2195 {
2196
2197 const uint8 *sPtr2 = sPtr1;
2198 const uint8 *dPtr2 = dPtr1;
2199
2200 for (uint32 plane = 0; plane < planes; plane++)
2201 {
2202
2203 if (*dPtr2 != *sPtr2)
2204 return false;
2205
2206 sPtr2 += sPlaneStep;
2207 dPtr2 += dPlaneStep;
2208
2209 }
2210
2211 sPtr1 += sColStep;
2212 dPtr1 += dColStep;
2213
2214 }
2215
2216 sPtr += sRowStep;
2217 dPtr += dRowStep;
2218
2219 }
2220
2221 return true;
2222
2223 }
2224
2225 /*****************************************************************************/
2226
2227 bool RefEqualArea16 (const uint16 *sPtr,
2228 const uint16 *dPtr,
2229 uint32 rows,
2230 uint32 cols,
2231 uint32 planes,
2232 int32 sRowStep,
2233 int32 sColStep,
2234 int32 sPlaneStep,
2235 int32 dRowStep,
2236 int32 dColStep,
2237 int32 dPlaneStep)
2238 {
2239
2240 for (uint32 row = 0; row < rows; row++)
2241 {
2242
2243 const uint16 *sPtr1 = sPtr;
2244 const uint16 *dPtr1 = dPtr;
2245
2246 for (uint32 col = 0; col < cols; col++)
2247 {
2248
2249 const uint16 *sPtr2 = sPtr1;
2250 const uint16 *dPtr2 = dPtr1;
2251
2252 for (uint32 plane = 0; plane < planes; plane++)
2253 {
2254
2255 if (*dPtr2 != *sPtr2)
2256 return false;
2257
2258 sPtr2 += sPlaneStep;
2259 dPtr2 += dPlaneStep;
2260
2261 }
2262
2263 sPtr1 += sColStep;
2264 dPtr1 += dColStep;
2265
2266 }
2267
2268 sPtr += sRowStep;
2269 dPtr += dRowStep;
2270
2271 }
2272
2273 return true;
2274
2275 }
2276
2277 /*****************************************************************************/
2278
2279 bool RefEqualArea32 (const uint32 *sPtr,
2280 const uint32 *dPtr,
2281 uint32 rows,
2282 uint32 cols,
2283 uint32 planes,
2284 int32 sRowStep,
2285 int32 sColStep,
2286 int32 sPlaneStep,
2287 int32 dRowStep,
2288 int32 dColStep,
2289 int32 dPlaneStep)
2290 {
2291
2292 for (uint32 row = 0; row < rows; row++)
2293 {
2294
2295 const uint32 *sPtr1 = sPtr;
2296 const uint32 *dPtr1 = dPtr;
2297
2298 for (uint32 col = 0; col < cols; col++)
2299 {
2300
2301 const uint32 *sPtr2 = sPtr1;
2302 const uint32 *dPtr2 = dPtr1;
2303
2304 for (uint32 plane = 0; plane < planes; plane++)
2305 {
2306
2307 if (*dPtr2 != *sPtr2)
2308 return false;
2309
2310 sPtr2 += sPlaneStep;
2311 dPtr2 += dPlaneStep;
2312
2313 }
2314
2315 sPtr1 += sColStep;
2316 dPtr1 += dColStep;
2317
2318 }
2319
2320 sPtr += sRowStep;
2321 dPtr += dRowStep;
2322
2323 }
2324
2325 return true;
2326
2327 }
2328
2329 /*****************************************************************************/
2330
2331 void RefVignetteMask16 (uint16 *mPtr,
2332 uint32 rows,
2333 uint32 cols,
2334 int32 rowStep,
2335 int64 offsetH,
2336 int64 offsetV,
2337 int64 stepH,
2338 int64 stepV,
2339 uint32 tBits,
2340 const uint16 *table)
2341 {
2342
2343 uint32 tShift = 32 - tBits;
2344 uint32 tRound = (1 << (tShift - 1));
2345 uint32 tLimit = 1 << tBits;
2346
2347 for (uint32 row = 0; row < rows; row++)
2348 {
2349
2350 int64 baseDelta = (offsetV + 32768) >> 16;
2351
2352 baseDelta = baseDelta * baseDelta + tRound;
2353
2354 int64 deltaH = offsetH + 32768;
2355
2356 for (uint32 col = 0; col < cols; col++)
2357 {
2358
2359 int64 temp = deltaH >> 16;
2360
2361 int64 delta = baseDelta + (temp * temp);
2362
2363 uint32 index = Min_uint32 ((uint32) (delta >> tShift), tLimit);
2364
2365 mPtr [col] = table [index];
2366
2367 deltaH += stepH;
2368
2369 }
2370
2371 offsetV += stepV;
2372
2373 mPtr += rowStep;
2374
2375 }
2376
2377 }
2378
2379 /*****************************************************************************/
2380
2381 void RefVignette16 (int16 *sPtr,
2382 const uint16 *mPtr,
2383 uint32 rows,
2384 uint32 cols,
2385 uint32 planes,
2386 int32 sRowStep,
2387 int32 sPlaneStep,
2388 int32 mRowStep,
2389 uint32 mBits)
2390 {
2391
2392 const uint32 mRound = 1 << (mBits - 1);
2393
2394 switch (planes)
2395 {
2396
2397 case 1:
2398 {
2399
2400 for (uint32 row = 0; row < rows; row++)
2401 {
2402
2403 for (uint32 col = 0; col < cols; col++)
2404 {
2405
2406 uint32 s = sPtr [col] + 32768;
2407
2408 uint32 m = mPtr [col];
2409
2410 s = (s * m + mRound) >> mBits;
2411
2412 s = Min_uint32 (s, 65535);
2413
2414 sPtr [col] = (int16) (s - 32768);
2415
2416 }
2417
2418 sPtr += sRowStep;
2419
2420 mPtr += mRowStep;
2421
2422 }
2423
2424 break;
2425
2426 }
2427
2428 case 3:
2429 {
2430
2431 int16 *rPtr = sPtr;
2432 int16 *gPtr = rPtr + sPlaneStep;
2433 int16 *bPtr = gPtr + sPlaneStep;
2434
2435 for (uint32 row = 0; row < rows; row++)
2436 {
2437
2438 for (uint32 col = 0; col < cols; col++)
2439 {
2440
2441 uint32 r = rPtr [col] + 32768;
2442 uint32 g = gPtr [col] + 32768;
2443 uint32 b = bPtr [col] + 32768;
2444
2445 uint32 m = mPtr [col];
2446
2447 r = (r * m + mRound) >> mBits;
2448 g = (g * m + mRound) >> mBits;
2449 b = (b * m + mRound) >> mBits;
2450
2451 r = Min_uint32 (r, 65535);
2452 g = Min_uint32 (g, 65535);
2453 b = Min_uint32 (b, 65535);
2454
2455 rPtr [col] = (int16) (r - 32768);
2456 gPtr [col] = (int16) (g - 32768);
2457 bPtr [col] = (int16) (b - 32768);
2458
2459 }
2460
2461 rPtr += sRowStep;
2462 gPtr += sRowStep;
2463 bPtr += sRowStep;
2464
2465 mPtr += mRowStep;
2466
2467 }
2468
2469 break;
2470
2471 }
2472
2473 case 4:
2474 {
2475
2476 int16 *aPtr = sPtr;
2477 int16 *bPtr = aPtr + sPlaneStep;
2478 int16 *cPtr = bPtr + sPlaneStep;
2479 int16 *dPtr = cPtr + sPlaneStep;
2480
2481 for (uint32 row = 0; row < rows; row++)
2482 {
2483
2484 for (uint32 col = 0; col < cols; col++)
2485 {
2486
2487 uint32 a = aPtr [col] + 32768;
2488 uint32 b = bPtr [col] + 32768;
2489 uint32 c = cPtr [col] + 32768;
2490 uint32 d = dPtr [col] + 32768;
2491
2492 uint32 m = mPtr [col];
2493
2494 a = (a * m + mRound) >> mBits;
2495 b = (b * m + mRound) >> mBits;
2496 c = (c * m + mRound) >> mBits;
2497 d = (d * m + mRound) >> mBits;
2498
2499 a = Min_uint32 (a, 65535);
2500 b = Min_uint32 (b, 65535);
2501 c = Min_uint32 (c, 65535);
2502 d = Min_uint32 (d, 65535);
2503
2504 aPtr [col] = (int16) (a - 32768);
2505 bPtr [col] = (int16) (b - 32768);
2506 cPtr [col] = (int16) (c - 32768);
2507 dPtr [col] = (int16) (d - 32768);
2508
2509 }
2510
2511 aPtr += sRowStep;
2512 bPtr += sRowStep;
2513 cPtr += sRowStep;
2514 dPtr += sRowStep;
2515
2516 mPtr += mRowStep;
2517
2518 }
2519
2520 break;
2521
2522 }
2523
2524 default:
2525 {
2526
2527 for (uint32 plane = 0; plane < planes; plane++)
2528 {
2529
2530 int16 *planePtr = sPtr;
2531
2532 const uint16 *maskPtr = mPtr;
2533
2534 for (uint32 row = 0; row < rows; row++)
2535 {
2536
2537 for (uint32 col = 0; col < cols; col++)
2538 {
2539
2540 uint32 s = planePtr [col] + 32768;
2541
2542 uint32 m = maskPtr [col];
2543
2544 s = (s * m + mRound) >> mBits;
2545
2546 s = Min_uint32 (s, 65535);
2547
2548 planePtr [col] = (int16) (s - 32768);
2549
2550 }
2551
2552 planePtr += sRowStep;
2553
2554 maskPtr += mRowStep;
2555
2556 }
2557
2558 sPtr += sPlaneStep;
2559
2560 }
2561
2562 break;
2563
2564 }
2565
2566 }
2567
2568 }
2569
2570 /*****************************************************************************/
2571
2572 void RefVignette32 (real32 *sPtr,
2573 const uint16 *mPtr,
2574 uint32 rows,
2575 uint32 cols,
2576 uint32 planes,
2577 int32 sRowStep,
2578 int32 sPlaneStep,
2579 int32 mRowStep,
2580 uint32 mBits,
2581 uint16 blackLevel)
2582 {
2583
2584 real32 *basePtr = sPtr;
2585
2586 real32 blackScale1 = 1.0f;
2587 real32 blackScale2 = 1.0f;
2588 real32 blackOffset1 = 0.0f;
2589 real32 blackOffset2 = 0.0f;
2590
2591 if (blackLevel != 0)
2592 {
2593
2594 blackOffset2 = ((real32) blackLevel) / 65535.0f;
2595 blackScale2 = 1.0f - blackOffset2;
2596 blackScale1 = 1.0f / blackScale2;
2597 blackOffset1 = 1.0f - blackScale1;
2598
2599 for (uint32 plane = 0; plane < planes; plane++)
2600 {
2601
2602 real32 *dPtr = basePtr + plane * sPlaneStep;
2603
2604 for (uint32 row = 0; row < rows; row++)
2605 {
2606
2607 for (uint32 col = 0; col < cols; col++)
2608 {
2609
2610 dPtr [col] = dPtr [col] * blackScale1 + blackOffset1;
2611
2612 }
2613
2614 dPtr += sRowStep;
2615
2616 }
2617
2618 }
2619
2620 }
2621
2622 const real32 kNorm = 1.0f / (1 << mBits);
2623
2624 switch (planes)
2625 {
2626
2627 case 1:
2628 {
2629
2630 for (uint32 row = 0; row < rows; row++)
2631 {
2632
2633 for (uint32 col = 0; col < cols; col++)
2634 {
2635
2636 real32 s = sPtr [col];
2637
2638 uint16 m = mPtr [col];
2639
2640 real32 scale = m * kNorm;
2641
2642 s = Min_real32 (s * scale, 1.0f);
2643
2644 sPtr [col] = s;
2645
2646 }
2647
2648 sPtr += sRowStep;
2649
2650 mPtr += mRowStep;
2651
2652 }
2653
2654 break;
2655
2656 }
2657
2658 case 3:
2659 {
2660
2661 real32 *rPtr = sPtr;
2662 real32 *gPtr = rPtr + sPlaneStep;
2663 real32 *bPtr = gPtr + sPlaneStep;
2664
2665 for (uint32 row = 0; row < rows; row++)
2666 {
2667
2668 for (uint32 col = 0; col < cols; col++)
2669 {
2670
2671 real32 r = rPtr [col];
2672 real32 g = gPtr [col];
2673 real32 b = bPtr [col];
2674
2675 uint16 m = mPtr [col];
2676
2677 real32 scale = m * kNorm;
2678
2679 r = Min_real32 (r * scale, 1.0f);
2680 g = Min_real32 (g * scale, 1.0f);
2681 b = Min_real32 (b * scale, 1.0f);
2682
2683 rPtr [col] = r;
2684 gPtr [col] = g;
2685 bPtr [col] = b;
2686
2687 }
2688
2689 rPtr += sRowStep;
2690 gPtr += sRowStep;
2691 bPtr += sRowStep;
2692
2693 mPtr += mRowStep;
2694
2695 }
2696
2697 break;
2698
2699 }
2700
2701 case 4:
2702 {
2703
2704 real32 *aPtr = sPtr;
2705 real32 *bPtr = aPtr + sPlaneStep;
2706 real32 *cPtr = bPtr + sPlaneStep;
2707 real32 *dPtr = cPtr + sPlaneStep;
2708
2709 for (uint32 row = 0; row < rows; row++)
2710 {
2711
2712 for (uint32 col = 0; col < cols; col++)
2713 {
2714
2715 real32 a = aPtr [col];
2716 real32 b = bPtr [col];
2717 real32 c = cPtr [col];
2718 real32 d = dPtr [col];
2719
2720 uint16 m = mPtr [col];
2721
2722 real32 scale = m * kNorm;
2723
2724 a = Min_real32 (a * scale, 1.0f);
2725 b = Min_real32 (b * scale, 1.0f);
2726 c = Min_real32 (c * scale, 1.0f);
2727 d = Min_real32 (d * scale, 1.0f);
2728
2729 aPtr [col] = a;
2730 bPtr [col] = b;
2731 cPtr [col] = c;
2732 dPtr [col] = d;
2733
2734 }
2735
2736 aPtr += sRowStep;
2737 bPtr += sRowStep;
2738 cPtr += sRowStep;
2739 dPtr += sRowStep;
2740
2741 mPtr += mRowStep;
2742
2743 }
2744
2745 break;
2746
2747 }
2748
2749 default:
2750 {
2751
2752 for (uint32 plane = 0; plane < planes; plane++)
2753 {
2754
2755 real32 *planePtr = sPtr;
2756
2757 const uint16 *maskPtr = mPtr;
2758
2759 for (uint32 row = 0; row < rows; row++)
2760 {
2761
2762 for (uint32 col = 0; col < cols; col++)
2763 {
2764
2765 real32 s = planePtr [col];
2766
2767 uint16 m = maskPtr [col];
2768
2769 real32 scale = m * kNorm;
2770
2771 s = Min_real32 (s * scale, 1.0f);
2772
2773 planePtr [col] = s;
2774
2775 }
2776
2777 planePtr += sRowStep;
2778
2779 maskPtr += mRowStep;
2780
2781 }
2782
2783 sPtr += sPlaneStep;
2784
2785 }
2786
2787 break;
2788
2789 }
2790
2791 }
2792
2793 if (blackLevel != 0)
2794 {
2795
2796 for (uint32 plane = 0; plane < planes; plane++)
2797 {
2798
2799 real32 *dPtr = basePtr + plane * sPlaneStep;
2800
2801 for (uint32 row = 0; row < rows; row++)
2802 {
2803
2804 for (uint32 col = 0; col < cols; col++)
2805 {
2806
2807 dPtr [col] = dPtr [col] * blackScale2 + blackOffset2;
2808
2809 }
2810
2811 dPtr += sRowStep;
2812
2813 }
2814
2815 }
2816
2817 }
2818
2819 }
2820
2821 /******************************************************************************/
2822
2823 void RefMapArea16 (uint16 *dPtr,
2824 uint32 count0,
2825 uint32 count1,
2826 uint32 count2,
2827 int32 step0,
2828 int32 step1,
2829 int32 step2,
2830 const uint16 *map)
2831 {
2832
2833 if (step2 == 1 && count2 >= 32)
2834 {
2835
2836 for (uint32 index0 = 0; index0 < count0; index0++)
2837 {
2838
2839 uint16 *d1 = dPtr;
2840
2841 for (uint32 index1 = 0; index1 < count1; index1++)
2842 {
2843
2844 uint16 *d2 = d1;
2845
2846 uint32 count = count2;
2847
2848 // Get the data 32-bit aligned if it is not.
2849
2850 if (!IsAligned32 (dPtr))
2851 {
2852
2853 d2 [0] = map [d2 [0]];
2854
2855 count--;
2856
2857 d2++;
2858
2859 }
2860
2861 // Use 32-bit reads and writes for bulk processing.
2862
2863 uint32 *dPtr32 = (uint32 *) d2;
2864
2865 // Process in blocks of 16 pixels.
2866
2867 uint32 blocks = count >> 4;
2868
2869 count -= blocks << 4;
2870 d2 += blocks << 4;
2871
2872 while (blocks--)
2873 {
2874
2875 uint32 x0, x1, x2, x3, x4, x5, x6, x7;
2876 uint32 p0, p1, p2, p3, p4, p5, p6, p7;
2877
2878 // Use 32 bit reads & writes, and pack and unpack the 16-bit values.
2879 // This results in slightly higher performance.
2880
2881 // Note that this code runs on both little-endian and big-endian systems,
2882 // since the pixels are either never swapped or double swapped.
2883
2884 x0 = dPtr32 [0];
2885 x1 = dPtr32 [1];
2886 x2 = dPtr32 [2];
2887 x3 = dPtr32 [3];
2888
2889 p0 = map [x0 >> 16 ];
2890 p1 = map [x0 & 0x0FFFF];
2891 p2 = map [x1 >> 16 ];
2892 p3 = map [x1 & 0x0FFFF];
2893 p4 = map [x2 >> 16 ];
2894 p5 = map [x2 & 0x0FFFF];
2895 p6 = map [x3 >> 16 ];
2896 p7 = map [x3 & 0x0FFFF];
2897
2898 x0 = (p0 << 16) | p1;
2899 x1 = (p2 << 16) | p3;
2900 x2 = (p4 << 16) | p5;
2901 x3 = (p6 << 16) | p7;
2902
2903 x4 = dPtr32 [4];
2904 x5 = dPtr32 [5];
2905 x6 = dPtr32 [6];
2906 x7 = dPtr32 [7];
2907
2908 dPtr32 [0] = x0;
2909 dPtr32 [1] = x1;
2910 dPtr32 [2] = x2;
2911 dPtr32 [3] = x3;
2912
2913 p0 = map [x4 >> 16 ];
2914 p1 = map [x4 & 0x0FFFF];
2915 p2 = map [x5 >> 16 ];
2916 p3 = map [x5 & 0x0FFFF];
2917 p4 = map [x6 >> 16 ];
2918 p5 = map [x6 & 0x0FFFF];
2919 p6 = map [x7 >> 16 ];
2920 p7 = map [x7 & 0x0FFFF];
2921
2922 x4 = (p0 << 16) | p1;
2923 x5 = (p2 << 16) | p3;
2924 x6 = (p4 << 16) | p5;
2925 x7 = (p6 << 16) | p7;
2926
2927 dPtr32 [4] = x4;
2928 dPtr32 [5] = x5;
2929 dPtr32 [6] = x6;
2930 dPtr32 [7] = x7;
2931
2932 dPtr32 += 8;
2933
2934 }
2935
2936 // Process remaining columns.
2937
2938 for (uint32 j = 0; j < count; j++)
2939 {
2940
2941 d2 [j] = map [d2 [j]];
2942
2943 }
2944
2945 d1 += step1;
2946
2947 }
2948
2949 dPtr += step0;
2950
2951 }
2952
2953 }
2954
2955 else
2956 {
2957
2958 for (uint32 index0 = 0; index0 < count0; index0++)
2959 {
2960
2961 uint16 *d1 = dPtr;
2962
2963 for (uint32 index1 = 0; index1 < count1; index1++)
2964 {
2965
2966 uint16 *d2 = d1;
2967
2968 for (uint32 index2 = 0; index2 < count2; index2++)
2969 {
2970
2971 d2 [0] = map [d2 [0]];
2972
2973 d2 += step2;
2974
2975 }
2976
2977 d1 += step1;
2978
2979 }
2980
2981 dPtr += step0;
2982
2983 }
2984
2985 }
2986
2987 }
2988
2989 /*****************************************************************************/
2990
2991 void RefBaselineMapPoly32 (real32 *dPtr,
2992 const int32 rowStep,
2993 const uint32 rows,
2994 const uint32 cols,
2995 const uint32 rowPitch,
2996 const uint32 colPitch,
2997 const real32 *coefficients,
2998 const uint32 degree,
2999 uint16 blackLevel)
3000 {
3001
3002 real32 blackScale1 = 1.0f;
3003 real32 blackScale2 = 1.0f;
3004 real32 blackOffset1 = 0.0f;
3005 real32 blackOffset2 = 0.0f;
3006
3007 if (blackLevel != 0)
3008 {
3009
3010 blackOffset2 = ((real32) blackLevel) / 65535.0f;
3011 blackScale2 = 1.0f - blackOffset2;
3012 blackScale1 = 1.0f / blackScale2;
3013 blackOffset1 = 1.0f - blackScale1;
3014
3015 }
3016
3017 for (uint32 row = 0; row < rows; row += rowPitch)
3018 {
3019
3020 if (blackLevel != 0)
3021 {
3022
3023 for (uint32 col = 0; col < cols; col += colPitch)
3024 {
3025
3026 dPtr [col] = dPtr [col] * blackScale1 + blackOffset1;
3027
3028 }
3029
3030 }
3031
3032 switch (degree)
3033 {
3034
3035 case 0:
3036 {
3037
3038 real32 y = Pin_real32 (-1.0f,
3039 coefficients [0],
3040 1.0f);
3041
3042 for (uint32 col = 0; col < cols; col += colPitch)
3043 {
3044
3045 dPtr [col] = y;
3046
3047 }
3048
3049 break;
3050
3051 }
3052
3053 case 1:
3054 {
3055
3056 for (uint32 col = 0; col < cols; col += colPitch)
3057 {
3058
3059 real32 x = dPtr [col];
3060
3061 real32 y = coefficients [0] + x * coefficients [1];
3062
3063 dPtr [col] = Pin_real32 (-1.0f, y, 1.0f);
3064
3065 }
3066
3067 break;
3068
3069 }
3070
3071 case 2:
3072 {
3073
3074 for (uint32 col = 0; col < cols; col += colPitch)
3075 {
3076
3077 real32 x = dPtr [col];
3078
3079 real32 y;
3080
3081 if (x < 0.0f)
3082 {
3083
3084 y = coefficients [0] + x *
3085 (coefficients [1] - x *
3086 (coefficients [2]));
3087
3088 }
3089
3090 else
3091 {
3092
3093 y = coefficients [0] + x *
3094 (coefficients [1] + x *
3095 (coefficients [2]));
3096
3097 }
3098
3099 dPtr [col] = Pin_real32 (-1.0f, y, 1.0f);
3100
3101 }
3102
3103 break;
3104
3105 }
3106
3107 case 3:
3108 {
3109
3110 for (uint32 col = 0; col < cols; col += colPitch)
3111 {
3112
3113 real32 x = dPtr [col];
3114
3115 real32 y;
3116
3117 if (x < 0.0f)
3118 {
3119
3120 y = coefficients [0] + x *
3121 (coefficients [1] - x *
3122 (coefficients [2] - x *
3123 (coefficients [3])));
3124
3125 }
3126
3127 else
3128 {
3129
3130 y = coefficients [0] + x *
3131 (coefficients [1] + x *
3132 (coefficients [2] + x *
3133 (coefficients [3])));
3134
3135 }
3136
3137 dPtr [col] = Pin_real32 (-1.0f, y, 1.0f);
3138
3139 }
3140
3141 break;
3142
3143 }
3144
3145 case 4:
3146 {
3147
3148 for (uint32 col = 0; col < cols; col += colPitch)
3149 {
3150
3151 real32 x = dPtr [col];
3152
3153 real32 y;
3154
3155 if (x < 0.0f)
3156 {
3157
3158 y = coefficients [0] + x *
3159 (coefficients [1] - x *
3160 (coefficients [2] - x *
3161 (coefficients [3] - x *
3162 (coefficients [4]))));
3163
3164 }
3165
3166 else
3167 {
3168
3169 y = coefficients [0] + x *
3170 (coefficients [1] + x *
3171 (coefficients [2] + x *
3172 (coefficients [3] + x *
3173 (coefficients [4]))));
3174
3175 }
3176
3177 dPtr [col] = Pin_real32 (-1.0f, y, 1.0f);
3178
3179 }
3180
3181 break;
3182
3183 }
3184
3185 default:
3186 {
3187
3188 for (uint32 col = 0; col < cols; col += colPitch)
3189 {
3190
3191 real32 x = dPtr [col];
3192
3193 real32 y = coefficients [0];
3194
3195 if (x < 0.0f)
3196 {
3197
3198 x = -x;
3199
3200 real32 xx = x;
3201
3202 for (uint32 j = 1; j <= degree; j++)
3203 {
3204
3205 y -= coefficients [j] * xx;
3206
3207 xx *= x;
3208
3209 }
3210
3211 }
3212
3213 else
3214 {
3215
3216 real32 xx = x;
3217
3218 for (uint32 j = 1; j <= degree; j++)
3219 {
3220
3221 y += coefficients [j] * xx;
3222
3223 xx *= x;
3224
3225 }
3226
3227 }
3228
3229 dPtr [col] = Pin_real32 (-1.0f, y, 1.0f);
3230
3231 }
3232
3233 }
3234
3235 }
3236
3237 if (blackLevel != 0)
3238 {
3239
3240 for (uint32 col = 0; col < cols; col += colPitch)
3241 {
3242
3243 dPtr [col] = dPtr [col] * blackScale2 + blackOffset2;
3244
3245 }
3246
3247 }
3248
3249 // Advance to the next row. Note that rowStep already accounts for the
3250 // row pitch.
3251
3252 dPtr += rowStep;
3253
3254 }
3255
3256 }
3257
3258 /*****************************************************************************/
3259
3260 void RefBaselineProfileGainTableMap (const real32 *rSrcPtr,
3261 const real32 *gSrcPtr,
3262 const real32 *bSrcPtr,
3263 real32 *rDstPtr,
3264 real32 *gDstPtr,
3265 real32 *bDstPtr,
3266 const uint32 cols,
3267 const int32 top,
3268 const int32 left,
3269 const dng_rect &imageArea,
3270 const real32 exposureWeightGain,
3271 const dng_gain_table_map &gainTableMap,
3272 const bool supportOverrange)
3273 {
3274
3275 const auto *mapInputWeights = gainTableMap.MapInputWeights ();
3276
3277 const real32 miw0 = mapInputWeights [0];
3278 const real32 miw1 = mapInputWeights [1];
3279 const real32 miw2 = mapInputWeights [2];
3280 const real32 miw3 = mapInputWeights [3];
3281 const real32 miw4 = mapInputWeights [4];
3282
3283 const dng_point &points = gainTableMap.Points ();
3284
3285 const dng_point_real64 &spacing = gainTableMap.Spacing ();
3286
3287 const dng_point_real64 &origin = gainTableMap.Origin ();
3288
3289 // Origin of the gain table map in normalized coordinates.
3290
3291 const real32 mapOriginH32 = (real32) origin.h;
3292 const real32 mapOriginV32 = (real32) origin.v;
3293
3294 // Size of the gain table map in normalized coordinates.
3295
3296 const real32 mapSpacingH32 = (real32) spacing.h;
3297 const real32 mapSpacingV32 = (real32) spacing.v;
3298
3299 // Minimum and maximum sample positions of the gain table map.
3300
3301 const real32 xLimitLo = 0.0f;
3302 const real32 yLimitLo = 0.0f;
3303
3304 const real32 xLimitHi = points.h - 1.0f;
3305 const real32 yLimitHi = points.v - 1.0f;
3306
3307 // Maximum 2D integer index into the gain table map.
3308
3309 const int32 xPixelLimit = points.h - 1;
3310 const int32 yPixelLimit = points.v - 1;
3311
3312 // Number of table samples at each position of the gain table map.
3313
3314 const int32 tableSize = (int32) gainTableMap.NumTablePoints ();
3315
3316 // Maximum integer table index.
3317
3318 const int32 tableLimit = tableSize - 1;
3319
3320 // Gamma parameter.
3321
3322 const real32 gamma = gainTableMap.Gamma ();
3323
3324 // Initialize sample position. Note the half-pixel offset.
3325
3326 real32 y = top + 0.5f;
3327 real32 x = left + 0.5f;
3328
3329 // Process each pixel in this row.
3330
3331 for (uint32 col = 0; col < cols; col++)
3332 {
3333
3334 // This is an intentionally unoptimized implementation for clarity.
3335
3336 // Transform to image-relative coordinates.
3337
3338 real32 u_image = (x - imageArea.l) / (real32) imageArea.W ();
3339 real32 v_image = (y - imageArea.t) / (real32) imageArea.H ();
3340
3341 // Transform to map-relative coordinates.
3342
3343 real32 x_map = (u_image - mapOriginH32) / mapSpacingH32;
3344 real32 y_map = (v_image - mapOriginV32) / mapSpacingV32;
3345
3346 // Clamp to valid sample positions.
3347
3348 x_map = Pin_real32 (xLimitLo, x_map, xLimitHi);
3349 y_map = Pin_real32 (yLimitLo, y_map, yLimitHi);
3350
3351 // Compute integer 2D indices.
3352
3353 int32 x0 = (int32) x_map;
3354 int32 x1 = Min_int32 (x0 + 1, xPixelLimit);
3355
3356 int32 y0 = (int32) y_map;
3357 int32 y1 = Min_int32 (y0 + 1, yPixelLimit);
3358
3359 // Compute fractional weights.
3360
3361 real32 xf = x_map - (real32) x0;
3362 real32 yf = y_map - (real32) y0;
3363
3364 // Read linear RGB values in RIMM space.
3365
3366 real32 r = rSrcPtr [col];
3367 real32 g = gSrcPtr [col];
3368 real32 b = bSrcPtr [col];
3369
3370 // Apply MapInputWeights (5-element dot product).
3371
3372 real32 minValue = Min_real32 (r, Min_real32 (g, b));
3373 real32 maxValue = Max_real32 (r, Max_real32 (g, b));
3374
3375 real32 weight = ((miw0 * r) +
3376 (miw1 * g) +
3377 (miw2 * b) +
3378 (miw3 * minValue) +
3379 (miw4 * maxValue));
3380
3381 // Since this sample render pipeline applies this processing step
3382 // before the BaselineExposure tag, we must scale the weight by the
3383 // baseline exposure value.
3384
3385 weight = weight * exposureWeightGain;
3386
3387 // Clamp the weight to [0,1].
3388
3389 weight = Pin_real32 (0.0f, weight, 1.0f);
3390
3391 // Apply the gamma parameter.
3392
3393 if (gamma != 1.0f)
3394 weight = powf (weight, gamma);
3395
3396 // Scale the weight by the table size and compute the table indices
3397 // and fractional weight.
3398
3399 real32 weightScaled = weight * tableSize;
3400
3401 int32 w0 = Min_int32 ((int32) weightScaled, tableLimit);
3402 int32 w1 = Min_int32 (w0 + 1, tableLimit);
3403
3404 real32 wf = weightScaled - (real32) w0;
3405
3406 // Look up 8 gains.
3407
3408 real32 gain000 = gainTableMap.Entry (y0, x0, w0);
3409 real32 gain001 = gainTableMap.Entry (y0, x0, w1);
3410 real32 gain010 = gainTableMap.Entry (y0, x1, w0);
3411 real32 gain011 = gainTableMap.Entry (y0, x1, w1);
3412 real32 gain100 = gainTableMap.Entry (y1, x0, w0);
3413 real32 gain101 = gainTableMap.Entry (y1, x0, w1);
3414 real32 gain110 = gainTableMap.Entry (y1, x1, w0);
3415 real32 gain111 = gainTableMap.Entry (y1, x1, w1);
3416
3417 // Interpolate in table (w) direction.
3418
3419 real32 gain00_ = Lerp_real32 (gain000, gain001, wf);
3420 real32 gain01_ = Lerp_real32 (gain010, gain011, wf);
3421 real32 gain10_ = Lerp_real32 (gain100, gain101, wf);
3422 real32 gain11_ = Lerp_real32 (gain110, gain111, wf);
3423
3424 // Interpolate in column (x) direction.
3425
3426 real32 gain0__ = Lerp_real32 (gain00_, gain01_, xf);
3427 real32 gain1__ = Lerp_real32 (gain10_, gain11_, xf);
3428
3429 // Interpolate in row (y) direction.
3430
3431 real32 gain = Lerp_real32 (gain0__, gain1__, yf);
3432
3433 // Apply gain.
3434
3435 r *= gain;
3436 g *= gain;
3437 b *= gain;
3438
3439 // Optionally clamp to [0,1].
3440
3441 if (!supportOverrange)
3442 {
3443 r = Pin_real32 (0.0f, r, 1.0f);
3444 g = Pin_real32 (0.0f, g, 1.0f);
3445 b = Pin_real32 (0.0f, b, 1.0f);
3446 }
3447
3448 // Store the result.
3449
3450 rDstPtr [col] = r;
3451 gDstPtr [col] = g;
3452 bDstPtr [col] = b;
3453
3454 // Increment sample position for next column.
3455
3456 x += 1.0f;
3457
3458 } // for each pixel in this row
3459
3460 }
3461
3462 /*****************************************************************************/
3463
3464 void RefRGBtoRGBTable3D (real32 *rPtr,
3465 real32 *gPtr,
3466 real32 *bPtr,
3467 const real32 *mPtr,
3468 uint32 rows,
3469 uint32 cols,
3470 int32 rowStep,
3471 int32 mRowStep,
3472 uint32 divisions,
3473 const uint16 *samples,
3474 real32 amount,
3475 uint32 gamut,
3476 const dng_matrix *encodeMatrix,
3477 const dng_matrix *decodeMatrix,
3478 const dng_1d_table *encodeGamma,
3479 const dng_1d_table *decodeGamma,
3480 const bool supportOverrange)
3481 {
3482
3483 real32 em00, em01, em02;
3484 real32 em10, em11, em12;
3485 real32 em20, em21, em22;
3486
3487 real32 dm00, dm01, dm02;
3488 real32 dm10, dm11, dm12;
3489 real32 dm20, dm21, dm22;
3490
3491 const bool hasMask = (mPtr != NULL);
3492
3493 bool hasMatrix = ((encodeMatrix != NULL) &&
3494 (decodeMatrix != NULL));
3495
3496 bool hasGamut = (gamut != 0);
3497
3498 if (hasMatrix)
3499 {
3500
3501 em00 = (real32) (*encodeMatrix) [0] [0];
3502 em01 = (real32) (*encodeMatrix) [0] [1];
3503 em02 = (real32) (*encodeMatrix) [0] [2];
3504
3505 em10 = (real32) (*encodeMatrix) [1] [0];
3506 em11 = (real32) (*encodeMatrix) [1] [1];
3507 em12 = (real32) (*encodeMatrix) [1] [2];
3508
3509 em20 = (real32) (*encodeMatrix) [2] [0];
3510 em21 = (real32) (*encodeMatrix) [2] [1];
3511 em22 = (real32) (*encodeMatrix) [2] [2];
3512
3513 dm00 = (real32) (*decodeMatrix) [0] [0];
3514 dm01 = (real32) (*decodeMatrix) [0] [1];
3515 dm02 = (real32) (*decodeMatrix) [0] [2];
3516
3517 dm10 = (real32) (*decodeMatrix) [1] [0];
3518 dm11 = (real32) (*decodeMatrix) [1] [1];
3519 dm12 = (real32) (*decodeMatrix) [1] [2];
3520
3521 dm20 = (real32) (*decodeMatrix) [2] [0];
3522 dm21 = (real32) (*decodeMatrix) [2] [1];
3523 dm22 = (real32) (*decodeMatrix) [2] [2];
3524
3525 }
3526
3527 else if (supportOverrange)
3528 {
3529
3530 hasMatrix = true;
3531 hasGamut = true;
3532
3533 // Identity matrix.
3534
3535 em00 = 1.0f; em01 = 0.0f; em02 = 0.0f;
3536 em10 = 0.0f; em11 = 1.0f; em12 = 0.0f;
3537 em20 = 0.0f; em21 = 0.0f; em22 = 1.0f;
3538
3539 dm00 = 1.0f; dm01 = 0.0f; dm02 = 0.0f;
3540 dm10 = 0.0f; dm11 = 1.0f; dm12 = 0.0f;
3541 dm20 = 0.0f; dm21 = 0.0f; dm22 = 1.0f;
3542
3543 }
3544
3545 const bool hasGamma = (encodeGamma != NULL) &&
3546 (decodeGamma != NULL);
3547
3548 real32 scale = (real32) (divisions - 1);
3549
3550 int32 maxIndex = divisions - 2;
3551
3552 const int32 offset001 = 4;
3553 const int32 offset010 = offset001 * divisions;
3554 const int32 offset100 = offset010 * divisions;
3555
3556 const int32 offset011 = offset010 + offset001;
3557 const int32 offset101 = offset100 + offset001;
3558 const int32 offset110 = offset100 + offset010;
3559 const int32 offset111 = offset110 + offset001;
3560
3561 real32 gamutDeltaR = 0.0f;
3562 real32 gamutDeltaG = 0.0f;
3563 real32 gamutDeltaB = 0.0f;
3564
3565 const bool hasAmount = (amount != 1.0f);
3566
3567 for (uint32 row = 0; row < rows; row++)
3568 {
3569
3570 for (uint32 col = 0; col < cols; col++)
3571 {
3572
3573 real32 r = rPtr [col];
3574 real32 g = gPtr [col];
3575 real32 b = bPtr [col];
3576
3577 real32 m = hasMask ? mPtr [col] : 1.0f;
3578
3579 real32 rSrc = r;
3580 real32 gSrc = g;
3581 real32 bSrc = b;
3582
3583 if (hasMatrix)
3584 {
3585
3586 real32 rr = r * em00 + g * em01 + b * em02;
3587 real32 gg = r * em10 + g * em11 + b * em12;
3588 real32 bb = r * em20 + g * em21 + b * em22;
3589
3590 if (supportOverrange)
3591 {
3592
3593 rr = EncodeOverrange (rr);
3594 gg = EncodeOverrange (gg);
3595 bb = EncodeOverrange (bb);
3596
3597 }
3598
3599 r = Pin_real32 (rr);
3600 g = Pin_real32 (gg);
3601 b = Pin_real32 (bb);
3602
3603 if (hasGamut)
3604 {
3605
3606 gamutDeltaR = rr - r;
3607 gamutDeltaG = gg - g;
3608 gamutDeltaB = bb - b;
3609
3610 }
3611
3612 }
3613
3614 if (hasGamma)
3615 {
3616
3617 r = encodeGamma->Interpolate (r);
3618 g = encodeGamma->Interpolate (g);
3619 b = encodeGamma->Interpolate (b);
3620
3621 }
3622
3623 real32 rScaled = r * scale;
3624 real32 gScaled = g * scale;
3625 real32 bScaled = b * scale;
3626
3627 int32 rIndex = Min_int32 ((int32) rScaled, maxIndex);
3628 int32 gIndex = Min_int32 ((int32) gScaled, maxIndex);
3629 int32 bIndex = Min_int32 ((int32) bScaled, maxIndex);
3630
3631 real32 rFract = rScaled - (real32) rIndex;
3632 real32 gFract = gScaled - (real32) gIndex;
3633 real32 bFract = bScaled - (real32) bIndex;
3634
3635 int32 offset1;
3636 int32 offset2;
3637
3638 real32 f1;
3639 real32 f2;
3640 real32 f3;
3641
3642 if (gFract >= rFract)
3643 {
3644
3645 if (bFract >= gFract)
3646 {
3647
3648 offset1 = offset001;
3649 offset2 = offset011;
3650
3651 f1 = bFract;
3652 f2 = gFract;
3653 f3 = rFract;
3654
3655 }
3656
3657 else if (bFract >= rFract)
3658 {
3659
3660 offset1 = offset010;
3661 offset2 = offset011;
3662
3663 f1 = gFract;
3664 f2 = bFract;
3665 f3 = rFract;
3666
3667 }
3668
3669 else
3670 {
3671
3672 offset1 = offset010;
3673 offset2 = offset110;
3674
3675 f1 = gFract;
3676 f2 = rFract;
3677 f3 = bFract;
3678
3679 }
3680
3681 }
3682
3683 else
3684 {
3685
3686 if (bFract >= rFract)
3687 {
3688
3689 offset1 = offset001;
3690 offset2 = offset101;
3691
3692 f1 = bFract;
3693 f2 = rFract;
3694 f3 = gFract;
3695
3696 }
3697
3698 else if (bFract >= gFract)
3699 {
3700
3701 offset1 = offset100;
3702 offset2 = offset101;
3703
3704 f1 = rFract;
3705 f2 = bFract;
3706 f3 = gFract;
3707
3708 }
3709
3710 else
3711 {
3712
3713 offset1 = offset100;
3714 offset2 = offset110;
3715
3716 f1 = rFract;
3717 f2 = gFract;
3718 f3 = bFract;
3719
3720 }
3721
3722 }
3723
3724 real32 w0 = 1.0f - f1;
3725 real32 w1 = f1 - f2;
3726 real32 w2 = f2 - f3;
3727
3728 const uint16 *table = samples + rIndex * offset100 +
3729 gIndex * offset010 +
3730 bIndex * offset001;
3731
3732 real32 r0 = (w0 * ((real32) table [0 ]) +
3733 w1 * ((real32) table [offset1 ]) +
3734 w2 * ((real32) table [offset2 ]) +
3735 f3 * ((real32) table [offset111 ])) *
3736 ((real32) (1.0 / 65535.0));
3737
3738 real32 g0 = (w0 * ((real32) table [1 ]) +
3739 w1 * ((real32) table [offset1 + 1]) +
3740 w2 * ((real32) table [offset2 + 1]) +
3741 f3 * ((real32) table [offset111 + 1])) *
3742 ((real32) (1.0 / 65535.0));
3743
3744 real32 b0 = (w0 * ((real32) table [2 ]) +
3745 w1 * ((real32) table [offset1 + 2]) +
3746 w2 * ((real32) table [offset2 + 2]) +
3747 f3 * ((real32) table [offset111 + 2])) *
3748 ((real32) (1.0 / 65535.0));
3749
3750 if (hasAmount)
3751 {
3752
3753 r = Pin_real32 (r + amount * (r0 - r));
3754 g = Pin_real32 (g + amount * (g0 - g));
3755 b = Pin_real32 (b + amount * (b0 - b));
3756
3757 }
3758
3759 else
3760 {
3761
3762 r = r0;
3763 g = g0;
3764 b = b0;
3765
3766 }
3767
3768 if (hasGamma)
3769 {
3770
3771 r = decodeGamma->Interpolate (r);
3772 g = decodeGamma->Interpolate (g);
3773 b = decodeGamma->Interpolate (b);
3774
3775 }
3776
3777 if (hasMatrix)
3778 {
3779
3780 if (hasGamut)
3781 {
3782
3783 r += gamutDeltaR;
3784 g += gamutDeltaG;
3785 b += gamutDeltaB;
3786
3787 }
3788
3789 if (supportOverrange)
3790 {
3791
3792 r = DecodeOverrange (r);
3793 g = DecodeOverrange (g);
3794 b = DecodeOverrange (b);
3795
3796 }
3797
3798 real32 rDst = r * dm00 + g * dm01 + b * dm02;
3799 real32 gDst = r * dm10 + g * dm11 + b * dm12;
3800 real32 bDst = r * dm20 + g * dm21 + b * dm22;
3801
3802 if (!supportOverrange)
3803 {
3804
3805 rDst = Pin_real32 (rDst);
3806 gDst = Pin_real32 (gDst);
3807 bDst = Pin_real32 (bDst);
3808
3809 }
3810
3811 rPtr [col] = Lerp_real32 (rSrc, rDst, m);
3812 gPtr [col] = Lerp_real32 (gSrc, gDst, m);
3813 bPtr [col] = Lerp_real32 (bSrc, bDst, m);
3814
3815 }
3816
3817 else
3818 {
3819
3820 rPtr [col] = Lerp_real32 (rSrc, r, m);
3821 gPtr [col] = Lerp_real32 (gSrc, g, m);
3822 bPtr [col] = Lerp_real32 (bSrc, b, m);
3823
3824 }
3825
3826 }
3827
3828 rPtr += rowStep;
3829 gPtr += rowStep;
3830 bPtr += rowStep;
3831
3832 if (hasMask)
3833 mPtr += mRowStep;
3834
3835 }
3836
3837 }
3838
3839 /*****************************************************************************/
3840
3841 void RefRGBtoRGBTable1D (real32 *rPtr,
3842 real32 *gPtr,
3843 real32 *bPtr,
3844 const real32 *mPtr,
3845 uint32 rows,
3846 uint32 cols,
3847 int32 rowStep,
3848 int32 mRowStep,
3849 const dng_1d_table &table0,
3850 const dng_1d_table &table1,
3851 const dng_1d_table &table2,
3852 uint32 gamut,
3853 const dng_matrix *encodeMatrix,
3854 const dng_matrix *decodeMatrix,
3855 const bool supportOverrange)
3856 {
3857
3858 real32 em00, em01, em02;
3859 real32 em10, em11, em12;
3860 real32 em20, em21, em22;
3861
3862 real32 dm00, dm01, dm02;
3863 real32 dm10, dm11, dm12;
3864 real32 dm20, dm21, dm22;
3865
3866 const bool hasMask = (mPtr != NULL);
3867
3868 bool hasMatrix = ((encodeMatrix != NULL) &&
3869 (decodeMatrix != NULL));
3870
3871 bool hasGamut = (gamut != 0);
3872
3873 if (hasMatrix)
3874 {
3875
3876 em00 = (real32) (*encodeMatrix) [0] [0];
3877 em01 = (real32) (*encodeMatrix) [0] [1];
3878 em02 = (real32) (*encodeMatrix) [0] [2];
3879
3880 em10 = (real32) (*encodeMatrix) [1] [0];
3881 em11 = (real32) (*encodeMatrix) [1] [1];
3882 em12 = (real32) (*encodeMatrix) [1] [2];
3883
3884 em20 = (real32) (*encodeMatrix) [2] [0];
3885 em21 = (real32) (*encodeMatrix) [2] [1];
3886 em22 = (real32) (*encodeMatrix) [2] [2];
3887
3888 dm00 = (real32) (*decodeMatrix) [0] [0];
3889 dm01 = (real32) (*decodeMatrix) [0] [1];
3890 dm02 = (real32) (*decodeMatrix) [0] [2];
3891
3892 dm10 = (real32) (*decodeMatrix) [1] [0];
3893 dm11 = (real32) (*decodeMatrix) [1] [1];
3894 dm12 = (real32) (*decodeMatrix) [1] [2];
3895
3896 dm20 = (real32) (*decodeMatrix) [2] [0];
3897 dm21 = (real32) (*decodeMatrix) [2] [1];
3898 dm22 = (real32) (*decodeMatrix) [2] [2];
3899
3900 }
3901
3902 else if (supportOverrange)
3903 {
3904
3905 hasMatrix = true;
3906 hasGamut = true;
3907
3908 // Identity matrix.
3909
3910 em00 = 1.0f; em01 = 0.0f; em02 = 0.0f;
3911 em10 = 0.0f; em11 = 1.0f; em12 = 0.0f;
3912 em20 = 0.0f; em21 = 0.0f; em22 = 1.0f;
3913
3914 dm00 = 1.0f; dm01 = 0.0f; dm02 = 0.0f;
3915 dm10 = 0.0f; dm11 = 1.0f; dm12 = 0.0f;
3916 dm20 = 0.0f; dm21 = 0.0f; dm22 = 1.0f;
3917
3918 }
3919
3920 real32 gamutDeltaR = 0.0f;
3921 real32 gamutDeltaG = 0.0f;
3922 real32 gamutDeltaB = 0.0f;
3923
3924 for (uint32 row = 0; row < rows; row++)
3925 {
3926
3927 for (uint32 col = 0; col < cols; col++)
3928 {
3929
3930 real32 r = rPtr [col];
3931 real32 g = gPtr [col];
3932 real32 b = bPtr [col];
3933
3934 real32 m = hasMask ? mPtr [col] : 1.0f;
3935
3936 real32 rSrc = r;
3937 real32 gSrc = g;
3938 real32 bSrc = b;
3939
3940 if (hasMatrix)
3941 {
3942
3943 real32 rr = r * em00 + g * em01 + b * em02;
3944 real32 gg = r * em10 + g * em11 + b * em12;
3945 real32 bb = r * em20 + g * em21 + b * em22;
3946
3947 if (supportOverrange)
3948 {
3949
3950 rr = EncodeOverrange (rr);
3951 gg = EncodeOverrange (gg);
3952 bb = EncodeOverrange (bb);
3953
3954 }
3955
3956 r = Pin_real32 (rr);
3957 g = Pin_real32 (gg);
3958 b = Pin_real32 (bb);
3959
3960 if (hasGamut)
3961 {
3962
3963 gamutDeltaR = rr - r;
3964 gamutDeltaG = gg - g;
3965 gamutDeltaB = bb - b;
3966
3967 }
3968
3969 }
3970
3971 r = table0.Interpolate (r);
3972 g = table1.Interpolate (g);
3973 b = table2.Interpolate (b);
3974
3975 if (hasMatrix)
3976 {
3977
3978 if (hasGamut)
3979 {
3980
3981 r += gamutDeltaR;
3982 g += gamutDeltaG;
3983 b += gamutDeltaB;
3984
3985 }
3986
3987 if (supportOverrange)
3988 {
3989
3990 r = DecodeOverrange (r);
3991 g = DecodeOverrange (g);
3992 b = DecodeOverrange (b);
3993
3994 }
3995
3996 real32 rDst = r * dm00 + g * dm01 + b * dm02;
3997 real32 gDst = r * dm10 + g * dm11 + b * dm12;
3998 real32 bDst = r * dm20 + g * dm21 + b * dm22;
3999
4000 if (!supportOverrange)
4001 {
4002
4003 rDst = Pin_real32 (rDst);
4004 gDst = Pin_real32 (gDst);
4005 bDst = Pin_real32 (bDst);
4006
4007 }
4008
4009 rPtr [col] = Lerp_real32 (rSrc, rDst, m);
4010 gPtr [col] = Lerp_real32 (gSrc, gDst, m);
4011 bPtr [col] = Lerp_real32 (bSrc, bDst, m);
4012
4013 }
4014
4015 else
4016 {
4017
4018 rPtr [col] = Lerp_real32 (rSrc, r, m);
4019 gPtr [col] = Lerp_real32 (gSrc, g, m);
4020 bPtr [col] = Lerp_real32 (bSrc, b, m);
4021
4022 }
4023
4024 }
4025
4026 rPtr += rowStep;
4027 gPtr += rowStep;
4028 bPtr += rowStep;
4029
4030 if (hasMask)
4031 mPtr += mRowStep;
4032
4033 }
4034
4035 }
4036
4037 /*****************************************************************************/
4038
4039 // Weighted Sum Method.
4040
4041 void RefMaskedRGBTables32 (real32 *ptr0,
4042 real32 *ptr1,
4043 real32 *ptr2,
4044 const real32 *tablePtr0,
4045 const real32 *tablePtr1,
4046 const real32 *tablePtr2,
4047 const real32 *maskPtr,
4048 uint32 numTransforms,
4049 int32 pRowStep,
4050 int32 tRowStep,
4051 int32 tPlaneStep,
4052 uint32 rows,
4053 uint32 cols)
4054 {
4055
4056 const int32 transformStep = 4 * tPlaneStep;
4057
4058 for (uint32 row = 0; row < rows; row++)
4059 {
4060
4061 for (uint32 j = 0; j < cols; j++)
4062 {
4063
4064 const real32 *tp0 = tablePtr0;
4065 const real32 *tp1 = tablePtr1;
4066 const real32 *tp2 = tablePtr2;
4067
4068 const real32 *mPtr = maskPtr;
4069
4070 real32 rSum = 0.0f;
4071 real32 gSum = 0.0f;
4072 real32 bSum = 0.0f;
4073
4074 real32 mSum = 0.0f;
4075
4076 // Walk through all masked transforms and compute weighted sum.
4077
4078 for (uint32 t = 0; t < numTransforms; t++)
4079 {
4080
4081 real32 r = tp0 [j];
4082 real32 g = tp1 [j];
4083 real32 b = tp2 [j];
4084
4085 real32 m = mPtr [j];
4086
4087 // Weighted sum.
4088
4089 rSum += (r * m);
4090 gSum += (g * m);
4091 bSum += (b * m);
4092
4093 mSum += m;
4094
4095 // Next transform.
4096
4097 tp0 += transformStep;
4098 tp1 += transformStep;
4099 tp2 += transformStep;
4100 mPtr += transformStep;
4101
4102 }
4103
4104 // Add weighted sum for background table.
4105 //
4106 // Note:
4107 //
4108 // If there is a background table, then ptr0, ptr1, and ptr2
4109 // contain the result of transforming the source values by the
4110 // background table.
4111 //
4112 // Otherwise (there is no background table), ptr0, ptr1, and ptr2
4113 // contain the source values (i.e., identity transform). In other
4114 // words, the "background" in this case represents the original
4115 // image with no table applied.
4116
4117 real32 bgWeight = 1.0f - Min_real32 (1.0f, mSum);
4118
4119 real32 bg_r = ptr0 [j];
4120 real32 bg_g = ptr1 [j];
4121 real32 bg_b = ptr2 [j];
4122
4123 rSum += (bg_r * bgWeight);
4124 gSum += (bg_g * bgWeight);
4125 bSum += (bg_b * bgWeight);
4126
4127 mSum += bgWeight;
4128
4129 // Normalize.
4130
4131 real32 norm = 1.0f / mSum;
4132
4133 real32 r_dst = rSum * norm;
4134 real32 g_dst = gSum * norm;
4135 real32 b_dst = bSum * norm;
4136
4137 // Store.
4138
4139 #if 1
4140
4141 // Production path: store final composited color.
4142
4143 ptr0 [j] = r_dst;
4144 ptr1 [j] = g_dst;
4145 ptr2 [j] = b_dst;
4146
4147 #elif 1
4148
4149 // Debug path: store something else for vis.
4150
4151 ptr0 [j] = bgWeight;
4152 ptr1 [j] = bgWeight;
4153 ptr2 [j] = bgWeight;
4154
4155 #else
4156
4157 // Debug path: store something else for vis.
4158
4159 ptr0 [j] = mSum;
4160 ptr1 [j] = mSum;
4161 ptr2 [j] = mSum;
4162
4163 #endif
4164
4165 } // cols
4166
4167 // Next row.
4168
4169 ptr0 += pRowStep;
4170 ptr1 += pRowStep;
4171 ptr2 += pRowStep;
4172
4173 tablePtr0 += tRowStep;
4174 tablePtr1 += tRowStep;
4175 tablePtr2 += tRowStep;
4176 maskPtr += tRowStep;
4177
4178 } // rows
4179
4180 }
4181
4182 /*****************************************************************************/