Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_utils.h

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 #ifndef __dng_utils__
10 #define __dng_utils__
11
12 /*****************************************************************************/
13
14 #include <cmath>
15 #include <limits>
16 #include <utility>
17
18 #include "dng_classes.h"
19 #include "dng_flags.h"
20 #include "dng_memory.h"
21 #include "dng_safe_arithmetic.h"
22 #include "dng_string.h"
23 #include "dng_types.h"
24 #include "dng_uncopyable.h"
25
26 /*****************************************************************************/
27
28 #if qWinOS
29 #undef min
30 #undef max
31 #endif
32
33 /*****************************************************************************/
34
35 // The unsigned integer overflow is intended here since a wrap around is used to
36 // calculate the abs() in the branchless version.
37
38 DNG_ATTRIB_NO_SANITIZE("unsigned-integer-overflow")
39 inline uint32 Abs_int32 (int32 x)
40 {
41
42 #if 0
43
44 // Reference version.
45
46 return (uint32) (x < 0 ? -x : x);
47
48 #else
49
50 // Branchless version.
51
52 uint32 mask = (uint32) (x >> 31);
53
54 return (uint32) (((uint32) x + mask) ^ mask);
55
56 #endif
57
58 }
59
60 inline int32 Min_int32 (int32 x, int32 y)
61 {
62
63 return (x <= y ? x : y);
64
65 }
66
67 inline int32 Max_int32 (int32 x, int32 y)
68 {
69
70 return (x >= y ? x : y);
71
72 }
73
74 inline int32 Pin_int32 (int32 min, int32 x, int32 max)
75 {
76
77 return Max_int32 (min, Min_int32 (x, max));
78
79 }
80
81 inline int32 Pin_int32_between (int32 a, int32 x, int32 b)
82 {
83
84 int32 min, max;
85 if (a < b) { min = a; max = b; }
86 else { min = b; max = a; }
87
88 return Pin_int32 (min, x, max);
89
90 }
91
92 /*****************************************************************************/
93
94 inline uint16 Min_uint16 (uint16 x, uint16 y)
95 {
96
97 return (x <= y ? x : y);
98
99 }
100
101 inline uint16 Max_uint16 (uint16 x, uint16 y)
102 {
103
104 return (x >= y ? x : y);
105
106 }
107
108 inline int16 Pin_int16 (int32 x)
109 {
110
111 x = Pin_int32 (-32768, x, 32767);
112
113 return (int16) x;
114
115 }
116
117 /*****************************************************************************/
118
119 inline uint32 Min_uint32 (uint32 x, uint32 y)
120 {
121
122 return (x <= y ? x : y);
123
124 }
125
126 inline uint32 Min_uint32 (uint32 x, uint32 y, uint32 z)
127 {
128
129 return Min_uint32 (x, Min_uint32 (y, z));
130
131 }
132
133 inline uint32 Max_uint32 (uint32 x, uint32 y)
134 {
135
136 return (x >= y ? x : y);
137
138 }
139
140 inline uint32 Max_uint32 (uint32 x, uint32 y, uint32 z)
141 {
142
143 return Max_uint32 (x, Max_uint32 (y, z));
144
145 }
146
147 inline uint32 Pin_uint32 (uint32 min, uint32 x, uint32 max)
148 {
149
150 return Max_uint32 (min, Min_uint32 (x, max));
151
152 }
153
154 /*****************************************************************************/
155
156 inline uint16 Pin_uint16 (int32 x)
157 {
158
159 #if 0
160
161 // Reference version.
162
163 x = Pin_int32 (0, x, 0x0FFFF);
164
165 #else
166
167 // Single branch version.
168
169 if (x & ~65535)
170 {
171
172 x = ~x >> 31;
173
174 }
175
176 #endif
177
178 return (uint16) x;
179
180 }
181
182 /*****************************************************************************/
183
184 inline uint32 RoundUp2 (uint32 x)
185 {
186
187 return (x + 1) & (uint32) ~1;
188
189 }
190
191 inline uint32 RoundUp4 (uint32 x)
192 {
193
194 return (x + 3) & (uint32) ~3;
195
196 }
197
198 inline uint32 RoundUp8 (uint32 x)
199 {
200
201 return (x + 7) & (uint32) ~7;
202
203 }
204
205 inline uint32 RoundUp16 (uint32 x)
206 {
207
208 return (x + 15) & (uint32) ~15;
209
210 }
211
212 inline uint32 RoundUp32 (uint32 x)
213 {
214
215 return (x + 31) & (uint32) ~31;
216
217 }
218
219 inline uint32 RoundUpSIMD (uint32 x)
220 {
221
222 #if qDNGAVXSupport
223 return RoundUp32 (x);
224 #else
225 return RoundUp16 (x);
226 #endif
227
228 }
229
230 inline uint32 RoundUp4096 (uint32 x)
231 {
232
233 return (x + 4095) & (uint32) ~4095;
234
235 }
236
237 /******************************************************************************/
238
239 inline uint32 RoundDown2 (uint32 x)
240 {
241
242 return x & (uint32) ~1;
243
244 }
245
246 inline uint32 RoundDown4 (uint32 x)
247 {
248
249 return x & (uint32) ~3;
250
251 }
252
253 inline uint32 RoundDown8 (uint32 x)
254 {
255
256 return x & (uint32) ~7;
257
258 }
259
260 inline uint32 RoundDown16 (uint32 x)
261 {
262
263 return x & (uint32) ~15;
264
265 }
266
267 inline uint32 RoundDown32 (uint32 x)
268 {
269
270 return x & (uint32) ~31;
271
272 }
273
274 inline uint32 RoundDownSIMD (uint32 x)
275 {
276
277 #if qDNGAVXSupport
278 return RoundDown32 (x);
279 #else
280 return RoundDown16 (x);
281 #endif
282
283 }
284
285 /******************************************************************************/
286
287 inline bool RoundUpForPixelSize (uint32 x,
288 uint32 pixelSize,
289 uint32 *result)
290 {
291
292 #if qDNGAVXSupport
293 static const uint32 kTargetMultiple = 32;
294 #else
295 static const uint32 kTargetMultiple = 16;
296 #endif
297
298 uint32 multiple;
299
300 switch (pixelSize)
301 {
302
303 case 1:
304 case 2:
305 case 4:
306 case 8:
307 {
308 multiple = kTargetMultiple / pixelSize;
309 break;
310 }
311
312 default:
313 {
314 multiple = kTargetMultiple;
315 break;
316 }
317
318 }
319
320 return RoundUpUint32ToMultiple (x, multiple, result);
321
322 }
323
324 /******************************************************************************/
325
326 inline uint32 RoundUpForPixelSize (uint32 x,
327 uint32 pixelSize)
328 {
329
330 uint32 result = 0;
331
332 if (!RoundUpForPixelSize (x, pixelSize, &result))
333 {
334 ThrowOverflow ("RoundUpForPixelSize");
335 }
336
337 return result;
338
339 }
340
341 /******************************************************************************/
342
343 inline int32 RoundUpForPixelSizeAsInt32 (uint32 x,
344 uint32 pixelSize)
345 {
346
347 uint32 result = 0;
348
349 if (!RoundUpForPixelSize (x, pixelSize, &result))
350 {
351 ThrowOverflow ("RoundUpForPixelSize");
352 }
353
354 dng_safe_uint32 safeResult (result);
355
356 return dng_safe_int32 (safeResult).Get ();
357
358 }
359
360 /******************************************************************************/
361
362 // Type of padding to be performed by ComputeBufferSize.
363
364 enum PaddingType
365 {
366
367 // Don't perform any padding.
368
369 padNone,
370
371 // Pad each scanline to an integer multiple of SIMD vector width (16 or
372 // 32) bytes (in the same way that RoundUpForPixelSize() does).
373
374 padSIMDBytes
375
376 };
377
378 // Returns the number of bytes required for an image tile with the given pixel
379 // type, tile size, number of image planes, and desired padding. Throws a
380 // dng_exception with dng_error_memory error code if one of the components of
381 // tileSize is negative or if arithmetic overflow occurs during the
382 // computation.
383
384 uint32 ComputeBufferSize (uint32 pixelType,
385 const dng_point &tileSize,
386 uint32 numPlanes,
387 PaddingType paddingType);
388
389 /******************************************************************************/
390
391 inline uint64 Abs_int64 (int64 x)
392 {
393
394 return (uint64) (x < 0 ? -x : x);
395
396 }
397
398 inline int64 Min_int64 (int64 x, int64 y)
399 {
400
401 return (x <= y ? x : y);
402
403 }
404
405 inline int64 Max_int64 (int64 x, int64 y)
406 {
407
408 return (x >= y ? x : y);
409
410 }
411
412 inline int64 Pin_int64 (int64 min, int64 x, int64 max)
413 {
414
415 return Max_int64 (min, Min_int64 (x, max));
416
417 }
418
419 /******************************************************************************/
420
421 inline uint64 Min_uint64 (uint64 x, uint64 y)
422 {
423
424 return (x <= y ? x : y);
425
426 }
427
428 inline uint64 Max_uint64 (uint64 x, uint64 y)
429 {
430
431 return (x >= y ? x : y);
432
433 }
434
435 inline uint64 Pin_uint64 (uint64 min, uint64 x, uint64 max)
436 {
437
438 return Max_uint64 (min, Min_uint64 (x, max));
439
440 }
441
442 /*****************************************************************************/
443
444 DNG_ALWAYS_INLINE
445 real32 Abs_real32 (real32 x)
446 {
447
448 return (x < 0.0f ? -x : x);
449
450 }
451
452 DNG_ALWAYS_INLINE
453 real32 Min_real32 (real32 x, real32 y)
454 {
455
456 return (x < y ? x : y);
457
458 }
459
460 DNG_ALWAYS_INLINE
461 real32 Max_real32 (real32 x, real32 y)
462 {
463
464 return (x > y ? x : y);
465
466 }
467
468 DNG_ALWAYS_INLINE
469 real32 Pin_real32 (real32 min, real32 x, real32 max)
470 {
471
472 return Max_real32 (min, Min_real32 (x, max));
473
474 }
475
476 DNG_ALWAYS_INLINE
477 real32 Pin_real32 (real32 x)
478 {
479
480 return Pin_real32 (0.0f, x, 1.0f);
481
482 }
483
484 DNG_ALWAYS_INLINE
485 real32 Pin_real32_Overrange (real32 min,
486 real32 x,
487 real32 max)
488 {
489
490 // Normal numbers in (min,max). No change.
491
492 if (x > min && x < max)
493 {
494 return x;
495 }
496
497 // Map large numbers (including positive infinity) to max.
498
499 else if (x > min)
500 {
501 return max;
502 }
503
504 // Map everything else (including negative infinity and all NaNs) to min.
505
506 return min;
507
508 }
509
510 DNG_ALWAYS_INLINE
511 real32 Pin_Overrange (real32 x)
512 {
513
514 // Normal in-range numbers, except for plus and minus zero.
515
516 if (x > 0.0f && x <= 1.0f)
517 {
518 return x;
519 }
520
521 // Large numbers, including positive infinity.
522
523 else if (x > 0.5f)
524 {
525 return 1.0f;
526 }
527
528 // Plus and minus zero, negative numbers, negative infinity, and all NaNs.
529
530 return 0.0f;
531
532 }
533
534 DNG_ALWAYS_INLINE
535 real32 Lerp_real32 (real32 a, real32 b, real32 t)
536 {
537
538 return a + t * (b - a);
539
540 }
541
542 /*****************************************************************************/
543
544 inline real64 Abs_real64 (real64 x)
545 {
546
547 return (x < 0.0 ? -x : x);
548
549 }
550
551 inline real64 Min_real64 (real64 x, real64 y)
552 {
553
554 return (x < y ? x : y);
555
556 }
557
558 inline real64 Max_real64 (real64 x, real64 y)
559 {
560
561 return (x > y ? x : y);
562
563 }
564
565 inline real64 Pin_real64 (real64 min, real64 x, real64 max)
566 {
567
568 return Max_real64 (min, Min_real64 (x, max));
569
570 }
571
572 inline real64 Pin_real64 (real64 x)
573 {
574
575 return Pin_real64 (0.0, x, 1.0);
576
577 }
578
579 inline real64 Pin_real64_Overrange (real64 min,
580 real64 x,
581 real64 max)
582 {
583
584 // Normal numbers in (min,max). No change.
585
586 if (x > min && x < max)
587 {
588 return x;
589 }
590
591 // Map large numbers (including positive infinity) to max.
592
593 else if (x > min)
594 {
595 return max;
596 }
597
598 // Map everything else (including negative infinity and all NaNs) to min.
599
600 return min;
601
602 }
603
604 inline real64 Lerp_real64 (real64 a, real64 b, real64 t)
605 {
606
607 return a + t * (b - a);
608
609 }
610
611 /*****************************************************************************/
612
613 inline uint8 Floor_uint8 (real32 x)
614 {
615
616 return (uint8) Max_real32 (0.0f, x);
617
618 }
619
620 inline uint8 Floor_uint8 (real64 x)
621 {
622
623 return (uint8) Max_real64 (0.0, x);
624
625 }
626
627 inline uint8 Round_uint8 (real32 x)
628 {
629
630 return Floor_uint8 (x + 0.5f);
631
632 }
633
634 inline uint8 Round_uint8 (real64 x)
635 {
636
637 return Floor_uint8 (x + 0.5);
638
639 }
640
641 /*****************************************************************************/
642
643 inline int32 Round_int32 (real32 x)
644 {
645
646 return (int32) (x > 0.0f ? x + 0.5f : x - 0.5f);
647
648 }
649
650 /*****************************************************************************/
651
652 inline int32 Round_int32 (real64 x)
653 {
654
655 const real64 temp = x > 0.0 ? x + 0.5 : x - 0.5;
656
657 // NaNs will fail this test (because NaNs compare false against
658 // everything) and will therefore also take the else branch.
659
660 if (temp > real64 (std::numeric_limits<int32>::min ()) - 1.0 &&
661 temp < real64 (std::numeric_limits<int32>::max ()) + 1.0)
662 {
663 return (int32) temp;
664 }
665
666 else
667 {
668 ThrowProgramError ("Overflow in Round_int32");
669 // Dummy return.
670 return 0;
671 }
672
673 }
674
675 /*****************************************************************************/
676
677 inline uint32 Floor_uint32 (real64 x)
678 {
679
680 const real64 temp = Max_real64 (0.0, x);
681
682 // NaNs will fail this test (because NaNs compare false against
683 // everything) and will therefore also take the else branch.
684
685 if (temp < real64 (std::numeric_limits<uint32>::max ()) + 1.0)
686 {
687 return (uint32) temp;
688 }
689
690 else
691 {
692 ThrowProgramError ("Overflow in Floor_uint32");
693 // Dummy return.
694 return 0;
695 }
696
697 }
698
699 /*****************************************************************************/
700
701 inline uint32 Floor_uint32 (real32 x)
702 {
703
704 return Floor_uint32 (static_cast<real64> (x));
705
706 }
707
708 /*****************************************************************************/
709
710 inline uint32 Round_uint32 (real32 x)
711 {
712
713 return Floor_uint32 (x + 0.5f);
714
715 }
716
717 /*****************************************************************************/
718
719 inline uint32 Round_uint32 (real64 x)
720 {
721
722 return Floor_uint32 (x + 0.5);
723
724 }
725
726 /******************************************************************************/
727
728 inline int64 Round_int64 (real64 x)
729 {
730
731 return (int64) (x >= 0.0 ? x + 0.5 : x - 0.5);
732
733 }
734
735 /*****************************************************************************/
736
737 const int64 kFixed64_One = (((int64) 1) << 32);
738 const int64 kFixed64_Half = (((int64) 1) << 31);
739
740 /******************************************************************************/
741
742 inline int64 Real64ToFixed64 (real64 x)
743 {
744
745 return Round_int64 (x * (real64) kFixed64_One);
746
747 }
748
749 /******************************************************************************/
750
751 inline real64 Fixed64ToReal64 (int64 x)
752 {
753
754 return x * (1.0 / (real64) kFixed64_One);
755
756 }
757
758 /*****************************************************************************/
759
760 inline char ForceUppercase (char c)
761 {
762
763 if (c >= 'a' && c <= 'z')
764 {
765
766 c -= 'a' - 'A';
767
768 }
769
770 return c;
771
772 }
773
774 /*****************************************************************************/
775
776 inline uint16 SwapBytes16 (uint16 x)
777 {
778
779 return (uint16) ((x << 8) |
780 (x >> 8));
781
782 }
783
784 inline uint32 SwapBytes32 (uint32 x)
785 {
786
787 return (x << 24) +
788 ((x << 8) & 0x00FF0000) +
789 ((x >> 8) & 0x0000FF00) +
790 (x >> 24);
791
792 }
793
794 /*****************************************************************************/
795
796 inline bool IsAligned16 (const void *p)
797 {
798
799 return (((uintptr) p) & 1) == 0;
800
801 }
802
803 inline bool IsAligned32 (const void *p)
804 {
805
806 return (((uintptr) p) & 3) == 0;
807
808 }
809
810 inline bool IsAligned64 (const void *p)
811 {
812
813 return (((uintptr) p) & 7) == 0;
814
815 }
816
817 inline bool IsAligned128 (const void *p)
818 {
819
820 return (((uintptr) p) & 15) == 0;
821
822 }
823
824 /******************************************************************************/
825
826 // Converts from RGB values (range 0.0 to 1.0) to HSV values (range 0.0 to
827 // 6.0 for hue, and 0.0 to 1.0 for saturation and value).
828 //
829 // INPUT REQUIREMENTS:
830 // All input RGB values are expected to be >= 0.
831 // If any RGB value is < 0 the resulting behavior is undefined and in practice
832 // can generate out of range S values (even NaN) and negative v values.
833
834 inline void DNG_RGBtoHSV (real32 r,
835 real32 g,
836 real32 b,
837 real32 &h,
838 real32 &s,
839 real32 &v)
840 {
841
842 v = Max_real32 (r, Max_real32 (g, b));
843
844 real32 gap = v - Min_real32 (r, Min_real32 (g, b));
845
846 if (gap > 0.0f)
847 {
848
849 if (r == v)
850 {
851
852 h = (g - b) / gap;
853
854 if (h < 0.0f)
855 {
856 h += 6.0f;
857 }
858
859 }
860
861 else if (g == v)
862 {
863 h = 2.0f + (b - r) / gap;
864 }
865
866 else
867 {
868 h = 4.0f + (r - g) / gap;
869 }
870
871 s = gap / v;
872
873 }
874
875 else
876 {
877 h = 0.0f;
878 s = 0.0f;
879 }
880
881 }
882
883 /******************************************************************************/
884
885 // Converts from RGB values (range 0.0 to 1.0) to HSV values (range 0.0 to
886 // 6.0 for hue, and 0.0 to 1.0 for saturation and value).
887 //
888 // This function pins input RGB >= 0 before calling DNG_RGBtoHSV()
889
890 inline void DNG_PinnedNonnegativeRGBtoHSV (real32 r,
891 real32 g,
892 real32 b,
893 real32 &h,
894 real32 &s,
895 real32 &v)
896 {
897
898 // Pin RGB >= 0.
899
900 r = Max_real32 (r, 0.0f);
901 g = Max_real32 (g, 0.0f);
902 b = Max_real32 (b, 0.0f);
903
904 DNG_RGBtoHSV (r, g, b, h, s, v);
905
906 }
907
908 /*****************************************************************************/
909
910 // Converts from HSV values (range 0.0 to 6.0 for hue, and 0.0 to 1.0 for
911 // saturation and value) to RGB values (range 0.0 to 1.0).
912
913 inline void DNG_HSVtoRGB (real32 h,
914 real32 s,
915 real32 v,
916 real32 &r,
917 real32 &g,
918 real32 &b)
919 {
920
921 if (s > 0.0f)
922 {
923
924 if (!std::isfinite (h))
925 ThrowProgramError ("Unexpected NaN or Inf");
926
927 h = std::fmod (h, 6.0f);
928
929 if (h < 0.0f)
930 h += 6.0f;
931
932 int32 i = (int32) h;
933 real32 f = h - (real32) i;
934
935 real32 p = v * (1.0f - s);
936
937 #define q (v * (1.0f - s * f))
938 #define t (v * (1.0f - s * (1.0f - f)))
939
940 // The integer 'i' is normally one of {0,1,2,3,4,5}.
941 // However, in rare cases it is possible that i = 6.
942 // Namely, if the input h is a negative number with very small
943 // magnitude (or if the fmod operation produces such an h value)
944 // then "h += 6.0f" can produce a value of EXACTLY 6.0f,
945 // in which case 'i' will be 6. This edge case is handled by
946 // treating the i=6 case the same as the i=0 case.
947
948 switch (i)
949 {
950 case 0: r = v; g = t; b = p; break;
951 case 1: r = q; g = v; b = p; break;
952 case 2: r = p; g = v; b = t; break;
953 case 3: r = p; g = q; b = v; break;
954 case 4: r = t; g = p; b = v; break;
955 case 5: r = v; g = p; b = q; break;
956 case 6: r = v; g = t; b = p; break; // see comment above
957 }
958
959 #undef q
960 #undef t
961
962 }
963
964 else
965 {
966 r = v;
967 g = v;
968 b = v;
969 }
970
971 }
972
973 /******************************************************************************/
974
975 // High resolution timer, for code profiling.
976
977 real64 TickTimeInSeconds ();
978
979 // Lower resolution timer, but more stable.
980
981 real64 TickCountInSeconds ();
982
983 /******************************************************************************/
984
985 void DNGIncrementTimerLevel ();
986
987 int32 DNGDecrementTimerLevel ();
988
989 /******************************************************************************/
990
991 class dng_timer: private dng_uncopyable
992 {
993
994 public:
995
996 dng_timer (const char *message);
997
998 ~dng_timer ();
999
1000 private:
1001
1002 const char *fMessage;
1003
1004 real64 fStartTime;
1005
1006 };
1007
1008 /*****************************************************************************/
1009
1010 // Returns the maximum squared Euclidean distance from the specified point to the
1011 // specified rectangle rect.
1012
1013 real64 MaxSquaredDistancePointToRect (const dng_point_real64 &point,
1014 const dng_rect_real64 &rect);
1015
1016 /*****************************************************************************/
1017
1018 // Returns the maximum Euclidean distance from the specified point to the specified
1019 // rectangle rect.
1020
1021 real64 MaxDistancePointToRect (const dng_point_real64 &point,
1022 const dng_rect_real64 &rect);
1023
1024 /*****************************************************************************/
1025
1026 inline uint32 DNG_HalfToFloat (uint16 halfValue)
1027 {
1028
1029 int32 sign = (halfValue >> 15) & 0x00000001;
1030 int32 exponent = (halfValue >> 10) & 0x0000001f;
1031 int32 mantissa = halfValue & 0x000003ff;
1032
1033 if (exponent == 0)
1034 {
1035
1036 if (mantissa == 0)
1037 {
1038
1039 // Plus or minus zero
1040
1041 return (uint32) (sign << 31);
1042
1043 }
1044
1045 else
1046 {
1047
1048 // Denormalized number -- renormalize it
1049
1050 while (!(mantissa & 0x00000400))
1051 {
1052 mantissa <<= 1;
1053 exponent -= 1;
1054 }
1055
1056 exponent += 1;
1057 mantissa &= ~0x00000400;
1058
1059 }
1060
1061 }
1062
1063 else if (exponent == 31)
1064 {
1065
1066 if (mantissa == 0)
1067 {
1068
1069 // Positive or negative infinity, convert to maximum (16 bit) values.
1070
1071 return (uint32) ((sign << 31) | ((0x1eL + 127 - 15) << 23) | (0x3ffL << 13));
1072
1073 }
1074
1075 else
1076 {
1077
1078 // Nan -- Just set to zero.
1079
1080 return 0;
1081
1082 }
1083
1084 }
1085
1086 // Normalized number
1087
1088 exponent += (127 - 15);
1089 mantissa <<= 13;
1090
1091 // Assemble sign, exponent and mantissa.
1092
1093 return (uint32) ((sign << 31) | (exponent << 23) | mantissa);
1094
1095 }
1096
1097 /*****************************************************************************/
1098
1099 inline uint16 DNG_FloatToHalf (uint32 i)
1100 {
1101
1102 int32 sign = (i >> 16) & 0x00008000;
1103 int32 exponent = ((i >> 23) & 0x000000ff) - (127 - 15);
1104 int32 mantissa = i & 0x007fffff;
1105
1106 if (exponent <= 0)
1107 {
1108
1109 if (exponent < -10)
1110 {
1111
1112 // Zero or underflow to zero.
1113
1114 return (uint16)sign;
1115
1116 }
1117
1118 // E is between -10 and 0. We convert f to a denormalized half.
1119
1120 mantissa = (mantissa | 0x00800000) >> (1 - exponent);
1121
1122 // Round to nearest, round "0.5" up.
1123 //
1124 // Rounding may cause the significand to overflow and make
1125 // our number normalized. Because of the way a half's bits
1126 // are laid out, we don't have to treat this case separately;
1127 // the code below will handle it correctly.
1128
1129 if (mantissa & 0x00001000)
1130 mantissa += 0x00002000;
1131
1132 // Assemble the half from sign, exponent (zero) and mantissa.
1133
1134 return (uint16)(sign | (mantissa >> 13));
1135
1136 }
1137
1138 else if (exponent == 0xff - (127 - 15))
1139 {
1140
1141 if (mantissa == 0)
1142 {
1143
1144 // F is an infinity; convert f to a half
1145 // infinity with the same sign as f.
1146
1147 return (uint16)(sign | 0x7c00);
1148
1149 }
1150
1151 else
1152 {
1153
1154 // F is a NAN; produce a half NAN that preserves
1155 // the sign bit and the 10 leftmost bits of the
1156 // significand of f.
1157
1158 return (uint16)(sign | 0x7c00 | (mantissa >> 13));
1159
1160 }
1161
1162 }
1163
1164 // E is greater than zero. F is a normalized float.
1165 // We try to convert f to a normalized half.
1166
1167 // Round to nearest, round "0.5" up
1168
1169 if (mantissa & 0x00001000)
1170 {
1171
1172 mantissa += 0x00002000;
1173
1174 if (mantissa & 0x00800000)
1175 {
1176 mantissa = 0; // overflow in significand,
1177 exponent += 1; // adjust exponent
1178 }
1179
1180 }
1181
1182 // Handle exponent overflow
1183
1184 if (exponent > 30)
1185 {
1186 return (uint16)(sign | 0x7c00); // infinity with the same sign as f.
1187 }
1188
1189 // Assemble the half from sign, exponent and mantissa.
1190
1191 return (uint16)(sign | (exponent << 10) | (mantissa >> 13));
1192
1193 }
1194
1195 /*****************************************************************************/
1196
1197 inline uint32 DNG_FP24ToFloat (const uint8 *input)
1198 {
1199
1200 int32 sign = (input [0] >> 7) & 0x01;
1201 int32 exponent = (input [0] ) & 0x7F;
1202 int32 mantissa = (((int32) input [1]) << 8) | input[2];
1203
1204 if (exponent == 0)
1205 {
1206
1207 if (mantissa == 0)
1208 {
1209
1210 // Plus or minus zero
1211
1212 return (uint32) (sign << 31);
1213
1214 }
1215
1216 else
1217 {
1218
1219 // Denormalized number -- renormalize it
1220
1221 while (!(mantissa & 0x00010000))
1222 {
1223 mantissa <<= 1;
1224 exponent -= 1;
1225 }
1226
1227 exponent += 1;
1228 mantissa &= ~0x00010000;
1229
1230 }
1231
1232 }
1233
1234 else if (exponent == 127)
1235 {
1236
1237 if (mantissa == 0)
1238 {
1239
1240 // Positive or negative infinity, convert to maximum (24 bit) values.
1241
1242 return (uint32) ((sign << 31) | ((0x7eL + 128 - 64) << 23) | (0xffffL << 7));
1243
1244 }
1245
1246 else
1247 {
1248
1249 // Nan -- Just set to zero.
1250
1251 return 0;
1252
1253 }
1254
1255 }
1256
1257 // Normalized number
1258
1259 exponent += (128 - 64);
1260 mantissa <<= 7;
1261
1262 // Assemble sign, exponent and mantissa.
1263
1264 return (uint32) ((sign << 31) | (exponent << 23) | mantissa);
1265
1266 }
1267
1268 /*****************************************************************************/
1269
1270 inline void DNG_FloatToFP24 (uint32 input, uint8 *output)
1271 {
1272
1273 int32 exponent = (int32) ((input >> 23) & 0xFF) - 128;
1274 int32 mantissa = input & 0x007FFFFF;
1275
1276 if (exponent == 127) // infinity or NaN
1277 {
1278
1279 // Will the NaN alais to infinity?
1280
1281 if (mantissa != 0x007FFFFF && ((mantissa >> 7) == 0xFFFF))
1282 {
1283
1284 mantissa &= 0x003FFFFF; // knock out msb to make it a NaN
1285
1286 }
1287
1288 }
1289
1290 else if (exponent > 63) // overflow, map to infinity
1291 {
1292
1293 exponent = 63;
1294 mantissa = 0x007FFFFF;
1295
1296 }
1297
1298 else if (exponent <= -64)
1299 {
1300
1301 if (exponent >= -79) // encode as denorm
1302 {
1303 mantissa = (mantissa | 0x00800000) >> (-63 - exponent);
1304 }
1305
1306 else // underflow to zero
1307 {
1308 mantissa = 0;
1309 }
1310
1311 exponent = -64;
1312
1313 }
1314
1315 output [0] = (uint8)(((input >> 24) & 0x80) | (uint32) (exponent + 64));
1316
1317 output [1] = (mantissa >> 15) & 0x00FF;
1318 output [2] = (mantissa >> 7) & 0x00FF;
1319
1320 }
1321
1322 /******************************************************************************/
1323
1324 // The following code was from PSDivide.h in Photoshop.
1325
1326 // High order 32-bits of an unsigned 32 by 32 multiply.
1327
1328 #ifndef MULUH
1329
1330 #if defined(_X86_) && defined(_MSC_VER)
1331
1332 inline uint32 Muluh86 (uint32 x, uint32 y)
1333 {
1334 uint32 result;
1335 __asm
1336 {
1337 MOV EAX, x
1338 MUL y
1339 MOV result, EDX
1340 }
1341 return (result);
1342 }
1343
1344 #define MULUH Muluh86
1345
1346 #else
1347
1348 #define MULUH(x,y) ((uint32) (((x) * (uint64) (y)) >> 32))
1349
1350 #endif
1351
1352 #endif
1353
1354 // High order 32-bits of an signed 32 by 32 multiply.
1355
1356 #ifndef MULSH
1357
1358 #if defined(_X86_) && defined(_MSC_VER)
1359
1360 inline int32 Mulsh86 (int32 x, int32 y)
1361 {
1362 int32 result;
1363 __asm
1364 {
1365 MOV EAX, x
1366 IMUL y
1367 MOV result, EDX
1368 }
1369 return (result);
1370 }
1371
1372 #define MULSH Mulsh86
1373
1374 #else
1375
1376 #define MULSH(x,y) ((int32) (((x) * (int64) (y)) >> 32))
1377
1378 #endif
1379
1380 #endif
1381
1382 /******************************************************************************/
1383
1384 // Random number generator (identical to Apple's) for portable use.
1385
1386 // This implements the "minimal standard random number generator"
1387 // as proposed by Park and Miller in CACM October, 1988.
1388 // It has a period of 2147483647 (0x7fffffff)
1389
1390 // This is the ACM standard 30 bit generator:
1391 // x' = (x * 16807) mod 2^31-1
1392
1393 DNG_ATTRIB_NO_SANITIZE("unsigned-integer-overflow")
1394 inline uint32 DNG_Random (uint32 seed)
1395 {
1396
1397 // high = seed / 127773
1398
1399 uint32 temp = MULUH (0x069C16BD, seed);
1400 uint32 high = (temp + ((seed - temp) >> 1)) >> 16;
1401
1402 // low = seed % 127773
1403
1404 uint32 low = seed - high * 127773;
1405
1406 // seed = (seed * 16807) % 2147483647
1407
1408 seed = 16807 * low - 2836 * high;
1409
1410 if (seed & 0x80000000)
1411 seed += 2147483647;
1412
1413 return seed;
1414
1415 }
1416
1417 /*****************************************************************************/
1418
1419 class dng_dither: private dng_uncopyable
1420 {
1421
1422 public:
1423
1424 static const uint32 kRNGBits = 7;
1425
1426 static const uint32 kRNGSize = 1 << kRNGBits;
1427
1428 static const uint32 kRNGMask = kRNGSize - 1;
1429
1430 static const uint32 kRNGSize2D = kRNGSize * kRNGSize;
1431
1432 private:
1433
1434 dng_memory_data fNoiseBuffer;
1435
1436 private:
1437
1438 dng_dither ();
1439
1440 public:
1441
1442 static const dng_dither & Get ();
1443
1444 public:
1445
1446 const uint16 *NoiseBuffer16 () const
1447 {
1448 return fNoiseBuffer.Buffer_uint16 ();
1449 }
1450
1451 };
1452
1453 /*****************************************************************************/
1454
1455 void HistogramArea (dng_host &host,
1456 const dng_image &image,
1457 const dng_rect &area,
1458 uint32 *hist,
1459 uint32 histLimit,
1460 uint32 plane = 0);
1461
1462 /*****************************************************************************/
1463
1464 void LimitFloatBitDepth (dng_host &host,
1465 const dng_image &srcImage,
1466 dng_image &dstImage,
1467 uint32 bitDepth,
1468 real32 scale = 1.0f);
1469
1470 /*****************************************************************************/
1471
1472 #if qMacOS
1473
1474 /*****************************************************************************/
1475
1476 template<typename T>
1477 class CFReleaseHelper
1478 {
1479
1480 private:
1481
1482 T fRef;
1483
1484 public:
1485
1486 CFReleaseHelper (T ref)
1487 : fRef (ref)
1488 {
1489 }
1490
1491 ~CFReleaseHelper ()
1492 {
1493 if (fRef)
1494 {
1495 CFRelease (fRef);
1496 }
1497 }
1498
1499 T Get () const
1500 {
1501 return fRef;
1502 }
1503
1504 };
1505
1506 /*****************************************************************************/
1507
1508 #endif // qMacOS
1509
1510 /*****************************************************************************/
1511
1512 // x is assumed to be in [0,1].
1513 // Result will also be in [0,1].
1514 //
1515 // Applies smooth cubic function f(x) such that first and second derivatives
1516 // at endpoints are both zero, i.e., f'(x) = 0 and f''(x) = 0 for x = 0 and x
1517 // = 1.
1518
1519 static inline real64 SmoothStep (real64 x)
1520 {
1521
1522 return x * x * (3.0 - 2.0 * x);
1523
1524 }
1525
1526 /*****************************************************************************/
1527
1528 uint32 MinBackwardVersionForCompression (uint32 compression);
1529
1530 /*****************************************************************************/
1531
1532 class dng_line_real32
1533 {
1534
1535 public:
1536
1537 real32 fX;
1538 real32 fY;
1539 real32 fSlope;
1540
1541 public:
1542
1543 dng_line_real32 (real32 x0,
1544 real32 y0,
1545 real32 x1,
1546 real32 y1)
1547
1548 : fX (x0)
1549 , fY (y0)
1550
1551 , fSlope ((x0 == x1) ? 0.0f : ((y0 - y1) / (x0 - x1)))
1552
1553 {
1554
1555 }
1556
1557 DNG_ALWAYS_INLINE real32 Evaluate (real32 x) const
1558 {
1559
1560 return fY + fSlope * (x - fX);
1561
1562 }
1563
1564 };
1565
1566 /*****************************************************************************/
1567
1568 // Implements ImageSequenceInfo tag introduced in DNG 1.7.
1569
1570 class dng_image_sequence_info
1571 {
1572
1573 public:
1574
1575 dng_string fSequenceID;
1576
1577 dng_string fSequenceType;
1578
1579 dng_string fFrameInfo;
1580
1581 uint32 fIndex = 0;
1582
1583 uint32 fCount = 0;
1584
1585 uint8 fIsFinal = 2; // unknown
1586
1587 public:
1588
1589 bool IsValid () const
1590 {
1591 return (fSequenceID .Length () >= 8 &&
1592 fSequenceType.Length () >= 1);
1593 }
1594
1595 uint32 TagCount () const
1596 {
1597
1598 uint32 sum = SafeUint32Add (fSequenceID .Length (),
1599 fSequenceType.Length ());
1600
1601 sum = SafeUint32Add (fFrameInfo.Length (), sum);
1602
1603 sum = SafeUint32Add (sum,
1604 1 + // null-term for sequence ID
1605 1 + // null-term for sequence type
1606 1 + // null-term for frame info
1607 4 + // index
1608 4 + // count
1609 1); // final
1610
1611 return sum;
1612
1613 }
1614
1615 tiff_tag * MakeTag (dng_memory_allocator &allocator) const;
1616
1617 bool operator== (const dng_image_sequence_info &src) const
1618 {
1619 return (fSequenceID == src.fSequenceID &&
1620 fSequenceType == src.fSequenceType &&
1621 fFrameInfo == src.fFrameInfo &&
1622 fIndex == src.fIndex &&
1623 fCount == src.fCount &&
1624 fIsFinal == src.fIsFinal);
1625 }
1626
1627 };
1628
1629 /*****************************************************************************/
1630
1631 // Implements ImageStats tag introduced in DNG 1.7.
1632
1633 class dng_image_stats
1634 {
1635
1636 public:
1637
1638 static constexpr uint32 kTag_WeightedAverage = 1;
1639 static constexpr uint32 kTag_WeightedSamples = 2;
1640 static constexpr uint32 kTag_Weights = 3;
1641 static constexpr uint32 kTag_ColorAverage = 4;
1642 static constexpr uint32 kTag_ColorSamples = 5;
1643
1644 static constexpr size_t kMaxSamples = 1024;
1645
1646 public:
1647
1648 struct weighted_sample
1649 {
1650
1651 real32 fFrac = 0.0f;
1652 real32 fValue = 0.0f;
1653
1654 bool operator== (const weighted_sample &src) const
1655 {
1656 return (fFrac == src.fFrac &&
1657 fValue == src.fValue);
1658 }
1659
1660 };
1661
1662 struct color_sample
1663 {
1664
1665 real32 fFrac = 0.0f;
1666
1667 std::vector<real32> fValues;
1668
1669 bool operator== (const color_sample &src) const
1670 {
1671 return (fFrac == src.fFrac &&
1672 fValues == src.fValues);
1673 }
1674
1675 };
1676
1677 public:
1678
1679 // Empty vectors mean invalid/missing.
1680
1681 std::vector<real32> fWeightedAverage;
1682
1683 std::vector<weighted_sample> fWeightedSamples;
1684
1685 std::vector<real32> fWeights;
1686
1687 std::vector<real32> fColorAverage;
1688
1689 std::vector<color_sample> fColorSamples;
1690
1691 public:
1692
1693 bool IsValidForPlaneCount (uint32 planeCount) const;
1694
1695 uint32 TagCount () const;
1696
1697 tiff_tag * MakeTag (dng_memory_allocator &allocator) const;
1698
1699 bool operator== (const dng_image_stats &src) const;
1700
1701 void Parse (dng_stream &stream);
1702
1703 #if qDNGValidate
1704 void Dump () const;
1705 #endif
1706
1707 };
1708
1709 /*****************************************************************************/
1710
1711 DNG_ALWAYS_INLINE real32 EncodeOverrange (real32 x)
1712 {
1713
1714 x = Max_real32 (x, 0.0f);
1715
1716 return x * (256.0f + x) / (256.0f * (1.0f + x));
1717
1718 }
1719
1720 /*****************************************************************************/
1721
1722 DNG_ALWAYS_INLINE real32 DecodeOverrange (real32 x)
1723 {
1724
1725 x = Max_real32 (x, 0.0f);
1726
1727 return 16.0f * ((8.0f * x) - 8.0f + sqrtf (64.0f * x * x - 127.0f * x + 64.0f));
1728
1729 }
1730
1731 /*****************************************************************************/
1732
1733 #endif // __dng_utils__
1734
1735 /*****************************************************************************/