Debian Codegraph @

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

raw
1 /*****************************************************************************/
2 // Copyright 2015-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_big_table.h"
10
11 #include "dng_1d_table.h"
12 #include "dng_bottlenecks.h"
13 #include "dng_abort_sniffer.h"
14 #include "dng_color_space.h"
15 #include "dng_globals.h"
16 #include "dng_host.h"
17 #include "dng_image.h"
18 #include "dng_image_writer.h"
19 #include "dng_info.h"
20 #include "dng_jxl.h"
21 #include "dng_memory_stream.h"
22 #include "dng_mutex.h"
23 #include "dng_negative.h"
24 #include "dng_stream.h"
25
26 #if qDNGUseXMP
27 #include "dng_xmp.h"
28 #endif
29
30 #include "zlib.h"
31
32 #include <unordered_map>
33
34 /*****************************************************************************/
35
36 class dng_big_table_cache
37 {
38
39 private:
40
41 dng_std_mutex fMutex;
42
43 typedef std::pair <dng_fingerprint,
44 int32> RefCountsPair;
45
46 typedef std::map <dng_fingerprint,
47 int32,
48 dng_fingerprint_less_than> RefCountsMap;
49
50 RefCountsMap fRefCounts;
51
52 std::vector<dng_fingerprint> fRecentlyUsed;
53
54 protected:
55
56 enum
57 {
58 kDefaultRecentlyUsedLimit = 5
59 };
60
61 uint32 fRecentlyUsedLimit;
62
63 protected:
64
65 dng_big_table_cache ();
66
67 void UseTable (dng_lock_std_mutex &lock,
68 const dng_fingerprint &fingerprint);
69
70 virtual void CacheIncrement (dng_lock_std_mutex &lock,
71 const dng_fingerprint &fingerprint);
72
73 virtual void CacheDecrement (dng_lock_std_mutex &lock,
74 const dng_fingerprint &fingerprint);
75
76 virtual void CacheAdd (dng_lock_std_mutex &lock,
77 const dng_big_table &table);
78
79 virtual bool CacheExtract (dng_lock_std_mutex &lock,
80 const dng_fingerprint &fingerprint,
81 dng_big_table &table);
82
83 virtual void InsertTableData (dng_lock_std_mutex &lock,
84 const dng_big_table &table) = 0;
85
86 virtual void EraseTableData (dng_lock_std_mutex &lock,
87 const dng_fingerprint &fingerprint) = 0;
88
89 virtual void ExtractTableData (dng_lock_std_mutex &lock,
90 const dng_fingerprint &fingerprint,
91 dng_big_table &table) = 0;
92
93 virtual void Clear ()
94 {
95
96 fRefCounts.clear ();
97
98 fRecentlyUsed.clear ();
99
100 }
101
102 public:
103
104 virtual ~dng_big_table_cache ();
105
106 void FlushRecentlyUsed ();
107
108 static void Increment (dng_big_table_cache *cache,
109 const dng_fingerprint &fingerprint);
110
111 static void Decrement (dng_big_table_cache *cache,
112 const dng_fingerprint &fingerprint);
113
114 static void Add (dng_big_table_cache *cache,
115 const dng_big_table &table);
116
117 static bool Extract (dng_big_table_cache *cache,
118 const dng_fingerprint &fingerprint,
119 dng_big_table &table);
120
121 };
122
123 /*****************************************************************************/
124
125 dng_big_table_cache::dng_big_table_cache ()
126
127 : fMutex ()
128
129 , fRefCounts ()
130
131 , fRecentlyUsed ()
132 , fRecentlyUsedLimit (kDefaultRecentlyUsedLimit)
133
134 {
135
136 }
137
138 /*****************************************************************************/
139
140 dng_big_table_cache::~dng_big_table_cache ()
141 {
142
143 }
144
145 /*****************************************************************************/
146
147 void dng_big_table_cache::UseTable (dng_lock_std_mutex &lock,
148 const dng_fingerprint &fingerprint)
149 {
150
151 // See if fingerprint is in recently used list.
152
153 int32 lastIndex = (int32) fRecentlyUsed.size () - 1;
154
155 for (int32 index = lastIndex; index >= 0; index--)
156 {
157
158 if (fingerprint == fRecentlyUsed [index])
159 {
160
161 // Move to end of list if not already there.
162
163 if (index != lastIndex)
164 {
165
166 fRecentlyUsed.erase (fRecentlyUsed.begin () + index);
167
168 fRecentlyUsed.push_back (fingerprint);
169
170 }
171
172 // Item is in recently used list, so we are done.
173
174 return;
175
176 }
177
178 }
179
180 // Is the recently used list full?
181
182 if (fRecentlyUsed.size () == fRecentlyUsedLimit)
183 {
184
185 CacheDecrement (lock, fRecentlyUsed.front ());
186
187 fRecentlyUsed.erase (fRecentlyUsed.begin ());
188
189 }
190
191 // Add to end of list.
192
193 fRecentlyUsed.push_back (fingerprint);
194
195 CacheIncrement (lock, fingerprint);
196
197 }
198
199 /*****************************************************************************/
200
201 void dng_big_table_cache::FlushRecentlyUsed ()
202 {
203
204 dng_lock_std_mutex lock (fMutex);
205
206 while (!fRecentlyUsed.empty ())
207 {
208
209 CacheDecrement (lock, fRecentlyUsed.back ());
210
211 fRecentlyUsed.pop_back ();
212
213 }
214
215 }
216
217 /*****************************************************************************/
218
219 void dng_big_table_cache::CacheIncrement (dng_lock_std_mutex &lock,
220 const dng_fingerprint &fingerprint)
221 {
222
223 if (fingerprint.IsValid ())
224 {
225
226 RefCountsMap::iterator it = fRefCounts.find (fingerprint);
227
228 if (it == fRefCounts.end ())
229 {
230
231 DNG_REPORT ("dng_big_table_cache::CacheIncrement"
232 "fingerprint not in cache");
233
234 }
235
236 else
237 {
238
239 it->second++;
240
241 UseTable (lock, fingerprint);
242
243 }
244
245 }
246
247 }
248
249 /*****************************************************************************/
250
251 void dng_big_table_cache::CacheDecrement (dng_lock_std_mutex &lock,
252 const dng_fingerprint &fingerprint)
253 {
254
255 if (fingerprint.IsValid ())
256 {
257
258 RefCountsMap::iterator it = fRefCounts.find (fingerprint);
259
260 if (it == fRefCounts.end ())
261 {
262
263 DNG_REPORT ("dng_big_table_cache::CacheDecrement"
264 "fingerprint not in cache");
265
266 }
267
268 else if (--(it->second) == 0)
269 {
270
271 fRefCounts.erase (it);
272
273 EraseTableData (lock, fingerprint);
274
275 }
276
277 }
278
279 }
280
281 /*****************************************************************************/
282
283 void dng_big_table_cache::CacheAdd (dng_lock_std_mutex &lock,
284 const dng_big_table &table)
285 {
286
287 if (table.Fingerprint ().IsValid ())
288 {
289
290 RefCountsMap::iterator it = fRefCounts.find (table.Fingerprint ());
291
292 if (it == fRefCounts.end ())
293 {
294
295 fRefCounts.insert (RefCountsPair (table.Fingerprint (), 1));
296
297 InsertTableData (lock, table);
298
299 }
300
301 else
302 {
303
304 it->second++;
305
306 }
307
308 UseTable (lock, table.Fingerprint ());
309
310 }
311
312 }
313
314 /*****************************************************************************/
315
316 bool dng_big_table_cache::CacheExtract (dng_lock_std_mutex &lock,
317 const dng_fingerprint &fingerprint,
318 dng_big_table &table)
319 {
320
321 if (fingerprint.IsValid ())
322 {
323
324 RefCountsMap::iterator it = fRefCounts.find (fingerprint);
325
326 if (it != fRefCounts.end ())
327 {
328
329 it->second++;
330
331 ExtractTableData (lock, fingerprint, table);
332
333 UseTable (lock, fingerprint);
334
335 return true;
336
337 }
338
339 }
340
341 return false;
342
343 }
344
345 /*****************************************************************************/
346
347 void dng_big_table_cache::Increment (dng_big_table_cache *cache,
348 const dng_fingerprint &fingerprint)
349 {
350
351 if (cache)
352 {
353
354 dng_lock_std_mutex lock (cache->fMutex);
355
356 cache->CacheIncrement (lock, fingerprint);
357
358 }
359
360 }
361
362 /*****************************************************************************/
363
364 void dng_big_table_cache::Decrement (dng_big_table_cache *cache,
365 const dng_fingerprint &fingerprint)
366 {
367
368 if (cache)
369 {
370
371 dng_lock_std_mutex lock (cache->fMutex);
372
373 cache->CacheDecrement (lock, fingerprint);
374
375 }
376
377 }
378
379 /*****************************************************************************/
380
381 void dng_big_table_cache::Add (dng_big_table_cache *cache,
382 const dng_big_table &table)
383 {
384
385 if (cache)
386 {
387
388 dng_lock_std_mutex lock (cache->fMutex);
389
390 cache->CacheAdd (lock, table);
391
392 }
393
394 }
395
396 /*****************************************************************************/
397
398 bool dng_big_table_cache::Extract (dng_big_table_cache *cache,
399 const dng_fingerprint &fingerprint,
400 dng_big_table &table)
401 {
402
403 if (cache)
404 {
405
406 dng_lock_std_mutex lock (cache->fMutex);
407
408 return cache->CacheExtract (lock, fingerprint, table);
409
410 }
411
412 return false;
413
414 }
415
416 /*****************************************************************************/
417
418 class dng_look_table_cache : public dng_big_table_cache
419 {
420
421 private:
422
423 typedef std::pair <dng_fingerprint,
424 dng_look_table::table_data> TableDataPair;
425
426 typedef std::map <dng_fingerprint,
427 dng_look_table::table_data,
428 dng_fingerprint_less_than> TableDataMap;
429
430 TableDataMap fTableData;
431
432 public:
433
434 dng_look_table_cache ()
435
436 : fTableData ()
437
438 {
439 }
440
441 virtual void Clear ()
442 {
443
444 dng_big_table_cache::Clear ();
445
446 fTableData.clear ();
447
448 }
449
450 virtual void InsertTableData (dng_lock_std_mutex & /* lock */,
451 const dng_big_table &table)
452 {
453
454 const dng_look_table *lookTable = static_cast
455 <const dng_look_table *>
456 (&table);
457
458 fTableData.insert (TableDataPair (lookTable->Fingerprint (),
459 lookTable->fData));
460
461 }
462
463 virtual void EraseTableData (dng_lock_std_mutex & /* lock */,
464 const dng_fingerprint &fingerprint)
465 {
466
467 TableDataMap::iterator it = fTableData.find (fingerprint);
468
469 if (it != fTableData.end ())
470 {
471
472 fTableData.erase (it);
473
474 }
475
476 else
477 {
478
479 DNG_REPORT ("dng_look_table_cache::EraseTableData"
480 "fingerprint not in cache");
481
482 }
483
484 }
485
486 virtual void ExtractTableData (dng_lock_std_mutex & /* lock */,
487 const dng_fingerprint &fingerprint,
488 dng_big_table &table)
489 {
490
491 TableDataMap::iterator it = fTableData.find (fingerprint);
492
493 if (it != fTableData.end ())
494 {
495
496 dng_look_table *lookTable = static_cast
497 <dng_look_table *>
498 (&table);
499
500 lookTable->fData = it->second;
501
502 }
503
504 else
505 {
506
507 DNG_REPORT ("dng_look_table_cache::ExtractTableData"
508 "fingerprint not in cache");
509
510 }
511
512 }
513
514 };
515
516 static dng_look_table_cache gLookTableCache;
517
518 /*****************************************************************************/
519
520 class dng_rgb_table_cache : public dng_big_table_cache
521 {
522
523 private:
524
525 typedef std::pair <dng_fingerprint,
526 dng_rgb_table::table_data> TableDataPair;
527
528 typedef std::map <dng_fingerprint,
529 dng_rgb_table::table_data,
530 dng_fingerprint_less_than> TableDataMap;
531
532 TableDataMap fTableData;
533
534 public:
535
536 dng_rgb_table_cache ()
537
538 : fTableData ()
539
540 {
541 }
542
543 virtual void Clear ()
544 {
545
546 dng_big_table_cache::Clear ();
547
548 fTableData.clear ();
549
550 }
551
552 virtual void InsertTableData (dng_lock_std_mutex & /* lock */,
553 const dng_big_table &table)
554 {
555
556 const dng_rgb_table *rgbTable = static_cast
557 <const dng_rgb_table *>
558 (&table);
559
560 fTableData.insert (TableDataPair (rgbTable->Fingerprint (),
561 rgbTable->fData));
562
563 }
564
565 virtual void EraseTableData (dng_lock_std_mutex & /* lock */,
566 const dng_fingerprint &fingerprint)
567 {
568
569 TableDataMap::iterator it = fTableData.find (fingerprint);
570
571 if (it != fTableData.end ())
572 {
573
574 fTableData.erase (it);
575
576 }
577
578 else
579 {
580
581 DNG_REPORT ("dng_rgb_table_cache::EraseTableData"
582 "fingerprint not in cache");
583
584 }
585
586 }
587
588 virtual void ExtractTableData (dng_lock_std_mutex & /* lock */,
589 const dng_fingerprint &fingerprint,
590 dng_big_table &table)
591 {
592
593 TableDataMap::iterator it = fTableData.find (fingerprint);
594
595 if (it != fTableData.end ())
596 {
597
598 dng_rgb_table *rgbTable = static_cast
599 <dng_rgb_table *>
600 (&table);
601
602 rgbTable->fData = it->second;
603
604 }
605
606 else
607 {
608
609 DNG_REPORT ("dng_rgb_table_cache::ExtractTableData"
610 "fingerprint not in cache");
611
612 }
613
614 }
615
616 };
617
618 static dng_rgb_table_cache gRGBTableCache;
619
620 /*****************************************************************************/
621
622 class dng_image_table_cache : public dng_big_table_cache
623 {
624
625 private:
626
627 std::unordered_map <dng_fingerprint,
628 dng_image_table_data,
629 dng_fingerprint_hash> fTableData;
630
631 public:
632
633 void Clear () override
634 {
635
636 dng_big_table_cache::Clear ();
637
638 fTableData.clear ();
639
640 }
641
642 void InsertTableData (dng_lock_std_mutex & /* lock */,
643 const dng_big_table &table) override
644 {
645
646 const dng_image_table *imageTable = static_cast
647 <const dng_image_table *>
648 (&table);
649
650 dng_image_table_data data;
651
652 imageTable->GetData (data);
653
654 fTableData.insert (std::make_pair (imageTable->Fingerprint (),
655 data));
656
657 }
658
659 void EraseTableData (dng_lock_std_mutex & /* lock */,
660 const dng_fingerprint &fingerprint) override
661 {
662
663 auto it = fTableData.find (fingerprint);
664
665 if (it != fTableData.end ())
666 {
667
668 fTableData.erase (it);
669
670 }
671
672 else
673 {
674
675 DNG_REPORT ("dng_image_table_cache::EraseTableData"
676 "fingerprint not in cache");
677
678 }
679
680 }
681
682 void ExtractTableData (dng_lock_std_mutex & /* lock */,
683 const dng_fingerprint &fingerprint,
684 dng_big_table &table) override
685 {
686
687 auto it = fTableData.find (fingerprint);
688
689 if (it != fTableData.end ())
690 {
691
692 dng_image_table *imageTable = static_cast
693 <dng_image_table *>
694 (&table);
695
696 imageTable->SetData (it->second);
697
698 }
699
700 else
701 {
702
703 DNG_REPORT ("dng_image_table_cache::ExtractTableData"
704 "fingerprint not in cache");
705
706 }
707
708 }
709
710 };
711
712 static dng_image_table_cache gImageTableCache;
713
714 /*****************************************************************************/
715
716 class dng_packed_image_table_cache : public dng_big_table_cache
717 {
718
719 private:
720
721 struct entry_t
722 {
723
724 public:
725
726 dng_image_table_data fImageData;
727
728 std::shared_ptr<const dng_memory_block> fBlock;
729
730 // Image properties.
731
732 dng_point fSize;
733
734 uint32 fPlanes = 0;
735
736 uint32 fPixelType = 0;
737
738 };
739
740 std::unordered_map <dng_fingerprint,
741 entry_t,
742 dng_fingerprint_hash> fEntries;
743
744 public:
745
746 void Clear () override
747 {
748
749 dng_big_table_cache::Clear ();
750
751 fEntries.clear ();
752
753 }
754
755 void InsertTableData (dng_lock_std_mutex & /* lock */,
756 const dng_big_table &table) override
757 {
758
759 // printf ("dng_packed_image_table_cache::InsertTableData\n");
760
761 const auto &src = static_cast <const dng_packed_image_table &> (table);
762
763 entry_t entry;
764
765 entry.fBlock = src.ShareBlock ();
766 entry.fSize = src.fSize;
767 entry.fPlanes = src.fPlanes;
768 entry.fPixelType = src.fPixelType;
769
770 if (src.IsValidUnpacked ())
771 src.Table ().GetData (entry.fImageData);
772
773 fEntries.insert (std::make_pair (src.Fingerprint (), entry));
774
775 }
776
777 void EraseTableData (dng_lock_std_mutex & /* lock */,
778 const dng_fingerprint &fingerprint) override
779 {
780
781 // printf ("dng_packed_image_table_cache::EraseTableData\n");
782
783 auto iter = fEntries.find (fingerprint);
784
785 if (iter != fEntries.end ())
786 {
787
788 fEntries.erase (iter);
789
790 }
791
792 else
793 {
794
795 DNG_REPORT ("dng_packed_image_table_cache::EraseTableData"
796 "fingerprint not in cache");
797
798 }
799
800 }
801
802 void ExtractTableData (dng_lock_std_mutex & /* lock */,
803 const dng_fingerprint &fingerprint,
804 dng_big_table &table) override
805 {
806
807 // printf ("dng_packed_image_table_cache::ExtractTableData\n");
808
809 auto iter = fEntries.find (fingerprint);
810
811 if (iter != fEntries.end ())
812 {
813
814 auto &dst = static_cast <dng_packed_image_table &> (table);
815
816 dst.fTableDigest = fingerprint;
817
818 dst.fBlock = iter->second.fBlock;
819 dst.fSize = iter->second.fSize;
820 dst.fPlanes = iter->second.fPlanes;
821 dst.fPixelType = iter->second.fPixelType;
822
823 AutoPtr<dng_image_table> temp (dst.MakeTable ());
824
825 temp->SetData (iter->second.fImageData);
826
827 dst.fTable.reset (temp.Release ());
828
829 }
830
831 else
832 {
833
834 DNG_REPORT ("dng_packed_image_table_cache::ExtractTableData"
835 "fingerprint not in cache");
836
837 }
838
839 }
840
841 };
842
843 static dng_packed_image_table_cache gPackedImageTableCache;
844
845 /*****************************************************************************/
846
847 void dng_big_table_cache_flush ()
848 {
849
850 gLookTableCache.FlushRecentlyUsed ();
851
852 gRGBTableCache.FlushRecentlyUsed ();
853
854 gImageTableCache.FlushRecentlyUsed ();
855
856 gPackedImageTableCache.FlushRecentlyUsed ();
857
858 }
859
860 /*****************************************************************************/
861
862 void dng_big_table_cache_clear ()
863 {
864
865 gLookTableCache.Clear ();
866
867 gRGBTableCache.Clear ();
868
869 gImageTableCache.Clear ();
870
871 gPackedImageTableCache.Clear ();
872
873 }
874
875 /*****************************************************************************/
876
877 dng_big_table::dng_big_table (dng_big_table_cache *cache)
878
879 : fFingerprint ()
880 , fCache (cache)
881 , fIsMissing (false)
882
883 {
884
885 }
886
887 /*****************************************************************************/
888
889 dng_big_table::dng_big_table (const dng_big_table &table)
890
891 : fFingerprint (table.fFingerprint)
892 , fCache (table.fCache )
893 , fIsMissing (false )
894
895 {
896
897 dng_big_table_cache::Increment (fCache, fFingerprint);
898
899 }
900
901 /*****************************************************************************/
902
903 dng_big_table & dng_big_table::operator= (const dng_big_table &table)
904 {
905
906 if (fFingerprint != table.fFingerprint ||
907 fCache != table.fCache )
908 {
909
910 dng_big_table_cache::Decrement (fCache, fFingerprint);
911
912 fFingerprint = table.fFingerprint;
913 fCache = table.fCache;
914
915 dng_big_table_cache::Increment (fCache, fFingerprint);
916
917 }
918
919 return *this;
920
921 }
922
923 /*****************************************************************************/
924
925 dng_big_table::~dng_big_table ()
926 {
927
928 dng_big_table_cache::Decrement (fCache, fFingerprint);
929
930 }
931
932 /*****************************************************************************/
933
934 const dng_fingerprint & dng_big_table::Fingerprint () const
935 {
936
937 return fFingerprint;
938
939 }
940
941 /*****************************************************************************/
942
943 dng_fingerprint dng_big_table::ComputeFingerprint () const
944 {
945
946 dng_md5_printer_stream stream;
947
948 stream.SetLittleEndian ();
949
950 PutStream (stream, true);
951
952 return stream.Result ();
953
954 }
955
956 /*****************************************************************************/
957
958 void dng_big_table::RecomputeFingerprint ()
959 {
960
961 dng_big_table_cache::Decrement (fCache, fFingerprint);
962
963 fFingerprint.Clear ();
964
965 if (IsValid ())
966 {
967
968 fFingerprint = ComputeFingerprint ();
969
970 // Try extract first to force sharing of table data memory if
971 // table data is already in cache.
972
973 if (!dng_big_table_cache::Extract (fCache, fFingerprint, *this))
974 {
975
976 // Otherwise add to cache.
977
978 dng_big_table_cache::Add (fCache, *this);
979
980 }
981
982 }
983
984 }
985
986 /*****************************************************************************/
987
988 bool dng_big_table::DecodeFromBinary (dng_host &host,
989 const uint8 * compressedData,
990 uint32 compressedSize,
991 AutoPtr<dng_memory_block> *uncompressedCache)
992 {
993
994 // Decompress the data, if required.
995
996 if (UseCompression ())
997 {
998
999 if (compressedSize < 5)
1000 {
1001 return false;
1002 }
1003
1004 uint8 *uncompressedData;
1005 uint32 uncompressedSize;
1006
1007 AutoPtr<dng_memory_block> uncompressedBlock;
1008
1009 if (uncompressedCache && uncompressedCache->Get ())
1010 {
1011 uncompressedData = uncompressedCache->Get ()->Buffer_uint8 ();
1012 uncompressedSize = uncompressedCache->Get ()->LogicalSize ();
1013 }
1014
1015 else
1016 {
1017
1018 // Uncompressed size is stored in first four bytes of decoded data,
1019 // little endian order.
1020
1021 uncompressedSize = (((uint32) compressedData [0]) ) +
1022 (((uint32) compressedData [1]) << 8) +
1023 (((uint32) compressedData [2]) << 16) +
1024 (((uint32) compressedData [3]) << 24);
1025
1026 uncompressedBlock.Reset (host.Allocate (uncompressedSize));
1027
1028 uncompressedData = uncompressedBlock->Buffer_uint8 ();
1029
1030 uLongf destLen = uncompressedSize;
1031
1032 int zResult = ::uncompress (uncompressedBlock->Buffer_uint8 (),
1033 &destLen,
1034 compressedData + 4,
1035 compressedSize - 4);
1036
1037 if (zResult != Z_OK)
1038 {
1039 return false;
1040 }
1041
1042 if (uncompressedCache)
1043 {
1044 uncompressedCache->Reset (uncompressedBlock.Release ());
1045 }
1046
1047 }
1048
1049 // Now read in the table data from the uncompressed stream.
1050
1051 try
1052 {
1053
1054 dng_stream stream (uncompressedData,
1055 uncompressedSize);
1056
1057 stream.SetLittleEndian ();
1058
1059 stream.SetSniffer (host.Sniffer ());
1060
1061 if (!GetStream (stream))
1062 {
1063 return false;
1064 }
1065
1066 }
1067
1068 catch (dng_exception &except)
1069 {
1070
1071 if (host.IsTransientError (except.ErrorCode ()))
1072 {
1073 throw;
1074 }
1075
1076 return false;
1077
1078 }
1079
1080 catch (...)
1081 {
1082
1083 return false;
1084
1085 }
1086
1087 }
1088
1089 // Simple uncompressed stream for this big table class.
1090
1091 else
1092 {
1093
1094 try
1095 {
1096
1097 dng_stream stream (compressedData,
1098 compressedSize);
1099
1100 stream.SetLittleEndian ();
1101
1102 stream.SetSniffer (host.Sniffer ());
1103
1104 if (!GetStream (stream))
1105 {
1106 return false;
1107 }
1108
1109 }
1110
1111 catch (dng_exception &except)
1112 {
1113
1114 if (host.IsTransientError (except.ErrorCode ()))
1115 {
1116 throw;
1117 }
1118
1119 return false;
1120
1121 }
1122
1123 catch (...)
1124 {
1125
1126 return false;
1127
1128 }
1129
1130 }
1131
1132 // Force recomputation of fingerprint.
1133
1134 RecomputeFingerprint ();
1135
1136 return true;
1137
1138 }
1139
1140 /*****************************************************************************/
1141
1142 void dng_big_table::ASCIItoBinary (dng_memory_allocator &allocator,
1143 const char *sPtr,
1144 uint32 sCount,
1145 AutoPtr<dng_memory_block> &dBlock,
1146 uint32 &dCount)
1147 {
1148
1149 // This binary to text encoding is very similar to the Z85
1150 // encoding, but the exact character set has been adjusted to
1151 // encode more cleanly into XMP.
1152
1153 static uint8 kDecodeTable [96] =
1154 {
1155
1156 0xFF, // space
1157 0x44, // !
1158 0xFF, // "
1159 0x54, // #
1160 0x53, // $
1161 0x52, // %
1162 0xFF, // &
1163 0x49, // '
1164 0x4B, // (
1165 0x4C, // )
1166 0x46, // *
1167 0x41, // +
1168 0xFF, // ,
1169 0x3F, // -
1170 0x3E, // .
1171 0x45, // /
1172
1173 0x00, 0x01, 0x02, 0x03, 0x04, // 01234
1174 0x05, 0x06, 0x07, 0x08, 0x09, // 56789
1175
1176 0x40, // :
1177 0xFF, // ;
1178 0xFF, // <
1179 0x42, // =
1180 0xFF, // >
1181 0x47, // ?
1182 0x51, // @
1183
1184 0x24, 0x25, 0x26, 0x27, 0x28, // ABCDE
1185 0x29, 0x2A, 0x2B, 0x2C, 0x2D, // FGHIJ
1186 0x2E, 0x2F, 0x30, 0x31, 0x32, // KLMNO
1187 0x33, 0x34, 0x35, 0x36, 0x37, // PQRST
1188 0x38, 0x39, 0x3A, 0x3B, 0x3C, // UVWXY
1189 0x3D, // Z
1190
1191 0x4D, // [
1192 0xFF, // backslash
1193 0x4E, // ]
1194 0x43, // ^
1195 0xFF, // _
1196 0x48, // `
1197
1198 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, // abcde
1199 0x0F, 0x10, 0x11, 0x12, 0x13, // fghij
1200 0x14, 0x15, 0x16, 0x17, 0x18, // klmno
1201 0x19, 0x1A, 0x1B, 0x1C, 0x1D, // pqrst
1202 0x1E, 0x1F, 0x20, 0x21, 0x22, // uvwxy
1203 0x23, // z
1204
1205 0x4F, // {
1206 0x4A, // |
1207 0x50, // }
1208 0xFF, // ~
1209 0xFF // del
1210
1211 };
1212
1213 dCount = 0;
1214
1215 uint32 maxDecodedSize = (sCount + 4) / 5 * 4;
1216
1217 dBlock.Reset (allocator.Allocate (maxDecodedSize));
1218
1219 uint32 phase = 0;
1220 uint32 value;
1221
1222 uint8 *dPtr = dBlock->Buffer_uint8 ();
1223
1224 for (uint32 j = 0; j < sCount; j++)
1225 {
1226
1227 uint8 e = (uint8) sPtr [j];
1228
1229 if (e < 32 || e > 127)
1230 {
1231 continue;
1232 }
1233
1234 uint32 d = kDecodeTable [e - 32];
1235
1236 if (d > 85)
1237 {
1238 continue;
1239 }
1240
1241 phase++;
1242
1243 if (phase == 1)
1244 {
1245 value = d;
1246 }
1247
1248 else if (phase == 2)
1249 {
1250 value += d * 85;
1251 }
1252
1253 else if (phase == 3)
1254 {
1255 value += d * (85 * 85);
1256 }
1257
1258 else if (phase == 4)
1259 {
1260 value += d * (85 * 85 * 85);
1261 }
1262
1263 else
1264 {
1265
1266 value += d * (85 * 85 * 85 * 85);
1267
1268 dPtr [0] = (uint8) (value );
1269 dPtr [1] = (uint8) (value >> 8);
1270 dPtr [2] = (uint8) (value >> 16);
1271 dPtr [3] = (uint8) (value >> 24);
1272
1273 dPtr += 4;
1274
1275 dCount += 4;
1276
1277 phase = 0;
1278
1279 }
1280
1281 }
1282
1283 if (phase > 3)
1284 {
1285
1286 dPtr [2] = (uint8) (value >> 16);
1287
1288 dCount++;
1289
1290 }
1291
1292 if (phase > 2)
1293 {
1294
1295 dPtr [1] = (uint8) (value >> 8);
1296
1297 dCount++;
1298
1299 }
1300
1301 if (phase > 1)
1302 {
1303
1304 dPtr [0] = (uint8) (value);
1305
1306 dCount++;
1307
1308 }
1309
1310 }
1311
1312 /*****************************************************************************/
1313
1314 bool dng_big_table::DecodeFromString (dng_host &host,
1315 const dng_string &block1)
1316 {
1317
1318 // Decode the text to binary.
1319
1320 AutoPtr<dng_memory_block> block2;
1321
1322 uint32 compressedSize = 0;
1323
1324 ASCIItoBinary (host.Allocator (),
1325 block1.Get (),
1326 block1.Length (),
1327 block2,
1328 compressedSize);
1329
1330 // Then decode table from the binary data.
1331
1332 return DecodeFromBinary (host,
1333 block2->Buffer_uint8 (),
1334 compressedSize);
1335
1336 }
1337
1338 /*****************************************************************************/
1339
1340 dng_memory_block* dng_big_table::EncodeAsBinary (dng_memory_allocator &allocator,
1341 uint32 &compressedSize) const
1342 {
1343
1344 // Stream to a binary block.
1345
1346 AutoPtr<dng_memory_block> block1;
1347
1348 {
1349
1350 dng_memory_stream stream (allocator);
1351
1352 stream.SetLittleEndian ();
1353
1354 PutStream (stream, false);
1355
1356 block1.Reset (stream.AsMemoryBlock (allocator));
1357
1358 }
1359
1360 // If we are not compressing this type, we are done.
1361
1362 if (!UseCompression ())
1363 {
1364
1365 compressedSize = block1->LogicalSize ();
1366
1367 return block1.Release ();
1368
1369 }
1370
1371 // Compress the block.
1372
1373 AutoPtr<dng_memory_block> block2;
1374
1375 {
1376
1377 uint32 uncompressedSize = block1->LogicalSize ();
1378
1379 uint32 safeCompressedSize = uncompressedSize + (uncompressedSize >> 8) + 64;
1380
1381 block2.Reset (allocator.Allocate (safeCompressedSize + 4));
1382
1383 // Store uncompressed size in first four bytes of compressed block.
1384
1385 uint8 *dPtr = block2->Buffer_uint8 ();
1386
1387 dPtr [0] = (uint8) (uncompressedSize );
1388 dPtr [1] = (uint8) (uncompressedSize >> 8);
1389 dPtr [2] = (uint8) (uncompressedSize >> 16);
1390 dPtr [3] = (uint8) (uncompressedSize >> 24);
1391
1392 uLongf dCount = safeCompressedSize;
1393
1394 int zResult = ::compress2 (dPtr + 4,
1395 &dCount,
1396 block1->Buffer_uint8 (),
1397 uncompressedSize,
1398 Z_DEFAULT_COMPRESSION);
1399
1400 if (zResult != Z_OK)
1401 {
1402 ThrowMemoryFull ();
1403 }
1404
1405 compressedSize = (uint32) dCount + 4;
1406
1407 block1.Reset ();
1408
1409 }
1410
1411 return block2.Release ();
1412
1413 }
1414
1415 /*****************************************************************************/
1416
1417 dng_memory_block* dng_big_table::EncodeAsString (dng_memory_allocator &allocator) const
1418 {
1419
1420 // Get compressed binary data.
1421
1422 uint32 compressedSize;
1423
1424 AutoPtr<dng_memory_block> block2 (EncodeAsBinary (allocator, compressedSize));
1425
1426 // Encode binary to text.
1427
1428 AutoPtr<dng_memory_block> block3;
1429
1430 {
1431
1432 // This binary to text encoding is very similar to the Z85
1433 // encoding, but the exact character set has been adjusted to
1434 // encode more cleanly into XMP.
1435
1436 static const char *kEncodeTable =
1437 "0123456789"
1438 "abcdefghij"
1439 "klmnopqrst"
1440 "uvwxyzABCD"
1441 "EFGHIJKLMN"
1442 "OPQRSTUVWX"
1443 "YZ.-:+=^!/"
1444 "*?`'|()[]{"
1445 "}@%$#";
1446
1447 uint32 safeEncodedSize = compressedSize +
1448 (compressedSize >> 2) +
1449 (compressedSize >> 6) +
1450 16;
1451
1452 block3.Reset (allocator.Allocate (safeEncodedSize));
1453
1454 uint8 *sPtr = block2->Buffer_uint8 ();
1455
1456 sPtr [compressedSize ] = 0;
1457 sPtr [compressedSize + 1] = 0;
1458 sPtr [compressedSize + 2] = 0;
1459
1460 uint8 *dPtr = block3->Buffer_uint8 ();
1461
1462 while (compressedSize)
1463 {
1464
1465 uint32 x0 = (((uint32) sPtr [0]) ) +
1466 (((uint32) sPtr [1]) << 8) +
1467 (((uint32) sPtr [2]) << 16) +
1468 (((uint32) sPtr [3]) << 24);
1469
1470 sPtr += 4;
1471
1472 uint32 x1 = x0 / 85;
1473
1474 *dPtr++ = kEncodeTable [x0 - x1 * 85];
1475
1476 uint32 x2 = x1 / 85;
1477
1478 *dPtr++ = kEncodeTable [x1 - x2 * 85];
1479
1480 if (!--compressedSize)
1481 break;
1482
1483 uint32 x3 = x2 / 85;
1484
1485 *dPtr++ = kEncodeTable [x2 - x3 * 85];
1486
1487 if (!--compressedSize)
1488 break;
1489
1490 uint32 x4 = x3 / 85;
1491
1492 *dPtr++ = kEncodeTable [x3 - x4 * 85];
1493
1494 if (!--compressedSize)
1495 break;
1496
1497 *dPtr++ = kEncodeTable [x4];
1498
1499 compressedSize--;
1500
1501 }
1502
1503 *dPtr = 0;
1504
1505 block2.Reset ();
1506
1507 }
1508
1509 return block3.Release ();
1510
1511 }
1512
1513 /*****************************************************************************/
1514
1515 bool dng_big_table::ExtractFromCache (const dng_fingerprint &fingerprint)
1516 {
1517
1518 if (dng_big_table_cache::Extract (fCache, fingerprint, *this))
1519 {
1520
1521 fFingerprint = fingerprint;
1522
1523 return true;
1524
1525 }
1526
1527 return false;
1528
1529 }
1530
1531 /*****************************************************************************/
1532
1533 #if qDNGUseXMP
1534
1535 /*****************************************************************************/
1536
1537 bool dng_big_table::ReadTableFromXMP (const dng_xmp &xmp,
1538 const char *ns,
1539 const dng_fingerprint &fingerprint,
1540 dng_big_table_storage *storage,
1541 dng_abort_sniffer *sniffer)
1542 {
1543
1544 // See if we can skip reading the table data, and just grab from cache.
1545
1546 if (ExtractFromCache (fingerprint))
1547 {
1548
1549 return true;
1550
1551 }
1552
1553 // Next see if we can get the table from the storage object.
1554
1555 if (storage && storage->ReadTable (*this, fingerprint, xmp.Allocator ()))
1556 {
1557
1558 return true;
1559
1560 }
1561
1562 // Not in cache nor storage, so we need to read from XMP.
1563
1564 dng_host host (&xmp.Allocator (), sniffer);
1565
1566 host.SniffForAbort ();
1567
1568 dng_string tablePath;
1569
1570 tablePath.Set ("Table_");
1571
1572 tablePath.Append (dng_xmp::EncodeFingerprint (fingerprint).Get ());
1573
1574 dng_string block1;
1575
1576 if (!xmp.GetString (ns,
1577 tablePath.Get (),
1578 block1))
1579 {
1580
1581 DNG_REPORT ("Missing big table data");
1582
1583 return false;
1584
1585 }
1586
1587 host.SniffForAbort ();
1588
1589 bool ok = DecodeFromString (host, block1);
1590
1591 block1.Clear ();
1592
1593 host.SniffForAbort ();
1594
1595 // Validate fingerprint match. Only bother doing this if decoding
1596 // succeeded, otherwise we expect the fingerprint to be wrong and the
1597 // assert message is not helpful.
1598
1599 if (ok)
1600 {
1601
1602 DNG_ASSERT (Fingerprint () == fingerprint,
1603 "dng_big_table fingerprint mismatch");
1604
1605 }
1606
1607 // It worked!
1608
1609 return ok;
1610
1611 }
1612
1613 /*****************************************************************************/
1614
1615 bool dng_big_table::ReadFromXMP (const dng_xmp &xmp,
1616 const char *ns,
1617 const char *path,
1618 dng_big_table_storage &storage,
1619 dng_abort_sniffer *sniffer)
1620 {
1621
1622 dng_fingerprint fingerprint;
1623
1624 if (!xmp.GetFingerprint (ns, path, fingerprint))
1625 {
1626
1627 return false;
1628
1629 }
1630
1631 // See if we can skip reading the table data, and just grab from cache.
1632
1633 if (ExtractFromCache (fingerprint))
1634 {
1635
1636 return true;
1637
1638 }
1639
1640 // Next see if we can get the table from the storage object.
1641
1642 if (storage.ReadTable (*this, fingerprint, xmp.Allocator ()))
1643 {
1644
1645 return true;
1646
1647 }
1648
1649 // Read in the table data. We already checked the storage object
1650 // (above), so pass in nullptr for 4th argument.
1651
1652 if (ReadTableFromXMP (xmp, ns, fingerprint, nullptr, sniffer))
1653 {
1654
1655 return true;
1656
1657 }
1658
1659 // Unable to find table data anywhere. Notify storage object.
1660
1661 storage.MissingTable (fingerprint);
1662
1663 // Also make a note that this table is missing.
1664
1665 SetMissing ();
1666
1667 return false;
1668
1669 }
1670
1671 /*****************************************************************************/
1672
1673 void dng_big_table::WriteToXMP (dng_xmp &xmp,
1674 const char *ns,
1675 const char *path,
1676 dng_big_table_storage &storage) const
1677 {
1678
1679 const dng_fingerprint &fingerprint = Fingerprint ();
1680
1681 if (!fingerprint.IsValid () || IsMissing ())
1682 {
1683
1684 xmp.Remove (ns, path);
1685
1686 return;
1687
1688 }
1689
1690 xmp.SetFingerprint (ns, path, fingerprint);
1691
1692 // See if we can just use the storage object to store the table.
1693
1694 if (storage.WriteTable (*this, fingerprint, xmp.Allocator ()))
1695 {
1696
1697 return;
1698
1699 }
1700
1701 dng_string tablePath;
1702
1703 tablePath.Set ("Table_");
1704
1705 tablePath.Append (dng_xmp::EncodeFingerprint (fingerprint).Get ());
1706
1707 if (xmp.Exists (ns, tablePath.Get ()))
1708 {
1709
1710 return;
1711
1712 }
1713
1714 AutoPtr<dng_memory_block> block;
1715
1716 block.Reset (EncodeAsString (xmp.Allocator ()));
1717
1718 xmp.Set (ns,
1719 tablePath.Get (),
1720 block->Buffer_char ());
1721
1722 }
1723
1724 /*****************************************************************************/
1725
1726 #endif // qDNGUseXMP
1727
1728 /*****************************************************************************/
1729
1730 #if qDNGValidate
1731
1732 void dng_big_table::WriteUncompressedStream (dng_stream &stream) const
1733 {
1734
1735 stream.SetLittleEndian ();
1736
1737 PutStream (stream, false);
1738
1739 }
1740
1741 #endif // qDNGValidate
1742
1743 /*****************************************************************************/
1744
1745 bool dng_big_table_dictionary::HasTable (const dng_fingerprint &fingerprint) const
1746 {
1747
1748 if (fMap.find (fingerprint) != fMap.end ())
1749 {
1750
1751 return true;
1752
1753 }
1754
1755 return false;
1756
1757 }
1758
1759 /*****************************************************************************/
1760
1761 bool dng_big_table_dictionary::GetTable (const dng_fingerprint &fingerprint,
1762 dng_ref_counted_block &block) const
1763 {
1764
1765 const auto it = fMap.find (fingerprint);
1766
1767 if (it != fMap.end ())
1768 {
1769
1770 block = it->second;
1771
1772 return true;
1773
1774 }
1775
1776 return false;
1777
1778 }
1779
1780 /*****************************************************************************/
1781
1782 void dng_big_table_dictionary::AddTable (const dng_fingerprint &fingerprint,
1783 const dng_ref_counted_block &block)
1784 {
1785
1786 if (fMap.find (fingerprint) == fMap.end ())
1787 {
1788
1789 fMap.insert (std::pair<dng_fingerprint,
1790 dng_ref_counted_block> (fingerprint,
1791 block));
1792
1793 }
1794
1795 }
1796
1797 /*****************************************************************************/
1798
1799 void dng_big_table_dictionary::CopyToDictionary
1800 (dng_big_table_dictionary &dictionary) const
1801
1802 {
1803
1804 for (auto it = fMap.cbegin (); it != fMap.cend (); ++it)
1805 {
1806
1807 dictionary.AddTable (it->first,
1808 it->second);
1809
1810 }
1811
1812 }
1813
1814 /*****************************************************************************/
1815
1816 dng_big_table_index::dng_big_table_index ()
1817
1818 : fMap ()
1819
1820 {
1821
1822 }
1823
1824 /*****************************************************************************/
1825
1826 bool dng_big_table_index::HasEntry (const dng_fingerprint &fingerprint) const
1827 {
1828
1829 return fMap.find (fingerprint) != fMap.end ();
1830
1831 }
1832
1833 /*****************************************************************************/
1834
1835 bool dng_big_table_index::GetEntry (const dng_fingerprint &fingerprint,
1836 uint32 &tableSize,
1837 uint64 &tableOffset) const
1838 {
1839
1840 auto it = fMap.find (fingerprint);
1841
1842 if (it != fMap.end ())
1843 {
1844
1845 tableSize = it->second.fTableSize;
1846 tableOffset = it->second.fTableOffset;
1847
1848 return true;
1849
1850 }
1851
1852 return false;
1853
1854 }
1855
1856 /*****************************************************************************/
1857
1858 void dng_big_table_index::AddEntry (const dng_fingerprint &fingerprint,
1859 uint32 tableSize,
1860 uint64 tableOffset)
1861 {
1862
1863 if (fMap.find (fingerprint) == fMap.end ())
1864 {
1865
1866 struct IndexEntry entry;
1867
1868 entry.fTableSize = tableSize;
1869 entry.fTableOffset = tableOffset;
1870
1871 fMap.insert (std::pair<dng_fingerprint,
1872 IndexEntry> (fingerprint, entry));
1873
1874
1875 }
1876
1877 }
1878
1879 /*****************************************************************************/
1880 /*****************************************************************************/
1881 /*****************************************************************************/
1882
1883 bool dng_big_table_group_index::GetEntry (const dng_fingerprint &groupDigest,
1884 dng_fingerprint &instanceDigest) const
1885 {
1886
1887 auto it = fMap.find (groupDigest);
1888
1889 if (it != fMap.end ())
1890 {
1891
1892 instanceDigest = it->second;
1893
1894 return true;
1895
1896 }
1897
1898 return false;
1899
1900 }
1901
1902 /*****************************************************************************/
1903 /*****************************************************************************/
1904 /*****************************************************************************/
1905
1906 dng_big_table_storage::dng_big_table_storage ()
1907 {
1908
1909 }
1910
1911 /*****************************************************************************/
1912
1913 dng_big_table_storage::~dng_big_table_storage ()
1914 {
1915
1916 }
1917
1918 /*****************************************************************************/
1919
1920 bool dng_big_table_storage::ReadTable (dng_big_table & /* table */,
1921 const dng_fingerprint & /* fingerprint */,
1922 dng_memory_allocator & /* allocator */)
1923 {
1924
1925 return false;
1926
1927 }
1928
1929 /*****************************************************************************/
1930
1931 bool dng_big_table_storage::WriteTable (const dng_big_table &table,
1932 const dng_fingerprint & /* fingerprint */,
1933 dng_memory_allocator & /* allocator */)
1934 {
1935
1936 if (table.IsEmbedNever ())
1937 {
1938 return true;
1939 }
1940
1941 return false;
1942
1943 }
1944
1945 /*****************************************************************************/
1946
1947 void dng_big_table_storage::MissingTable (const dng_fingerprint & /* fingerprint */)
1948 {
1949
1950 }
1951
1952 /*****************************************************************************/
1953
1954 bool dng_big_table_storage::GroupToInstance (const dng_fingerprint & /* groupDigest */,
1955 dng_fingerprint & /* instanceDigest */) const
1956 {
1957
1958 return false;
1959
1960 }
1961
1962 /*****************************************************************************/
1963
1964 dng_look_table::dng_look_table ()
1965
1966 : dng_big_table (&gLookTableCache)
1967
1968 , fData ()
1969 , fAmount (1.0)
1970
1971 {
1972
1973 }
1974
1975 /*****************************************************************************/
1976
1977 dng_look_table::dng_look_table (const dng_look_table &table)
1978
1979 : dng_big_table (table)
1980
1981 , fData (table.fData)
1982 , fAmount (table.fAmount)
1983
1984 {
1985
1986 }
1987
1988 /*****************************************************************************/
1989
1990 dng_look_table & dng_look_table::operator= (const dng_look_table &table)
1991 {
1992
1993 dng_big_table::operator= (table);
1994
1995 fData = table.fData;
1996 fAmount = table.fAmount;
1997
1998 return *this;
1999
2000 }
2001
2002 /*****************************************************************************/
2003
2004 dng_look_table::~dng_look_table ()
2005 {
2006
2007 }
2008
2009 /*****************************************************************************/
2010
2011 void dng_look_table::Set (const dng_hue_sat_map &map,
2012 uint32 encoding)
2013 {
2014
2015 fData.fMap = map;
2016 fData.fEncoding = encoding;
2017
2018 fData.ComputeMonochrome ();
2019
2020 RecomputeFingerprint ();
2021
2022 }
2023
2024 /*****************************************************************************/
2025
2026 bool dng_look_table::IsValid () const
2027 {
2028
2029 if (IsMissing ())
2030 {
2031 return false;
2032 }
2033
2034 return fData.fMap.IsValid ();
2035
2036 }
2037
2038 /*****************************************************************************/
2039
2040 void dng_look_table::SetInvalid ()
2041 {
2042
2043 *this = dng_look_table ();
2044
2045 }
2046
2047 /*****************************************************************************/
2048
2049 bool dng_look_table::GetStream (dng_stream &stream)
2050 {
2051
2052 table_data data;
2053
2054 if (stream.Get_uint32 () != btt_LookTable)
2055 {
2056 return false;
2057 }
2058
2059 uint32 version = stream.Get_uint32 ();
2060
2061 if (version != kLookTableVersion1 &&
2062 version != kLookTableVersion2)
2063 {
2064 ThrowBadFormat ("Unknown look table version");
2065 }
2066
2067 uint32 hueDivisions = stream.Get_uint32 ();
2068 uint32 satDivisions = stream.Get_uint32 ();
2069 uint32 valDivisions = stream.Get_uint32 ();
2070
2071 if (hueDivisions < 1 || hueDivisions > kMaxHueSamples ||
2072 satDivisions < 1 || satDivisions > kMaxSatSamples ||
2073 valDivisions < 1 || valDivisions > kMaxValSamples ||
2074 (dng_safe_uint32 (hueDivisions) *
2075 dng_safe_uint32 (satDivisions) *
2076 dng_safe_uint32 (valDivisions)).Get () > kMaxTotalSamples)
2077 {
2078 ThrowBadFormat ();
2079 }
2080
2081 data.fMap.SetDivisions (hueDivisions,
2082 satDivisions,
2083 valDivisions);
2084
2085 uint32 count = data.fMap.DeltasCount ();
2086
2087 dng_hue_sat_map::HSBModify * deltas = data.fMap.GetDeltas ();
2088
2089 for (uint32 index = 0; index < count; index++)
2090 {
2091
2092 deltas->fHueShift = stream.Get_real32 ();
2093 deltas->fSatScale = stream.Get_real32 ();
2094 deltas->fValScale = stream.Get_real32 ();
2095
2096 deltas++;
2097
2098 }
2099
2100 data.fMap.AssignNewUniqueRuntimeFingerprint ();
2101
2102 data.fEncoding = stream.Get_uint32 ();
2103
2104 if (data.fEncoding != encoding_Linear &&
2105 data.fEncoding != encoding_sRGB)
2106 {
2107 ThrowBadFormat ("Unknown look table encoding");
2108 }
2109
2110 if (version != kLookTableVersion1)
2111 {
2112
2113 data.fMinAmount = stream.Get_real64 ();
2114 data.fMaxAmount = stream.Get_real64 ();
2115
2116 if (data.fMinAmount < 0.0 || data.fMinAmount > 1.0 || data.fMaxAmount < 1.0)
2117 {
2118 ThrowBadFormat ("Invalid min/max amount for look table");
2119 }
2120
2121 }
2122
2123 else
2124 {
2125
2126 data.fMinAmount = 1.0;
2127 data.fMaxAmount = 1.0;
2128
2129 }
2130
2131 data.ComputeMonochrome ();
2132
2133 if (stream.Position () + 4 <= stream.Length ())
2134 {
2135 data.fFlags = stream.Get_uint32 ();
2136 }
2137
2138 fData = data;
2139
2140 return true;
2141
2142 }
2143
2144 /*****************************************************************************/
2145
2146 void dng_look_table::PutStream (dng_stream &stream,
2147 bool /* forFingerprint */) const
2148 {
2149
2150 DNG_REQUIRE (IsValid (), "Invalid Look Table");
2151
2152 stream.Put_uint32 (btt_LookTable);
2153
2154 uint32 version = kLookTableVersion1;
2155
2156 if (fData.fMinAmount != 1.0 ||
2157 fData.fMaxAmount != 1.0)
2158 {
2159 version = kLookTableVersion2;
2160 }
2161
2162 stream.Put_uint32 (version);
2163
2164 uint32 hueDivisions;
2165 uint32 satDivisions;
2166 uint32 valDivisions;
2167
2168 fData.fMap.GetDivisions (hueDivisions,
2169 satDivisions,
2170 valDivisions);
2171
2172 stream.Put_uint32 (hueDivisions);
2173 stream.Put_uint32 (satDivisions);
2174 stream.Put_uint32 (valDivisions);
2175
2176 uint32 count = fData.fMap.DeltasCount ();
2177
2178 const dng_hue_sat_map::HSBModify * deltas = fData.fMap.GetConstDeltas ();
2179
2180 for (uint32 index = 0; index < count; index++)
2181 {
2182
2183 stream.Put_real32 (deltas->fHueShift);
2184 stream.Put_real32 (deltas->fSatScale);
2185 stream.Put_real32 (deltas->fValScale);
2186
2187 deltas++;
2188
2189 }
2190
2191 stream.Put_uint32 (fData.fEncoding);
2192
2193 if (version != kLookTableVersion1)
2194 {
2195
2196 stream.Put_real64 (fData.fMinAmount);
2197 stream.Put_real64 (fData.fMaxAmount);
2198
2199 }
2200
2201 if (fData.fFlags != 0)
2202 {
2203
2204 stream.Put_uint32 (fData.fFlags);
2205
2206 }
2207
2208 }
2209
2210 /*****************************************************************************/
2211
2212 dng_rgb_table::dng_rgb_table ()
2213
2214 : dng_big_table (&gRGBTableCache)
2215
2216 , fData ()
2217 , fAmount (1.0)
2218
2219 {
2220
2221 }
2222
2223 /*****************************************************************************/
2224
2225 dng_rgb_table::dng_rgb_table (const dng_rgb_table &table)
2226
2227 : dng_big_table (table)
2228
2229 , fData (table.fData)
2230 , fAmount (table.fAmount)
2231
2232 {
2233
2234 }
2235
2236 /*****************************************************************************/
2237
2238 dng_rgb_table & dng_rgb_table::operator= (const dng_rgb_table &table)
2239 {
2240
2241 dng_big_table::operator= (table);
2242
2243 fData = table.fData;
2244 fAmount = table.fAmount;
2245
2246 return *this;
2247
2248 }
2249
2250 /*****************************************************************************/
2251
2252 dng_rgb_table::~dng_rgb_table ()
2253 {
2254
2255 }
2256
2257 /*****************************************************************************/
2258
2259 bool dng_rgb_table::IsValid () const
2260 {
2261
2262 if (IsMissing ())
2263 {
2264 return false;
2265 }
2266
2267 // If table itself is invalid, then invalid.
2268
2269 if (fData.fDimensions == 0)
2270 return false;
2271
2272 // If table has some effect, then valid.
2273
2274 if (fAmount > 0.0)
2275 return true;
2276
2277 // Does the matrix itself do any clipping?
2278
2279 if (fData.fPrimaries == primaries_ProPhoto ||
2280 fData.fGamut == gamut_extend )
2281 return false;
2282
2283 // Table is a NOP but there is some gamut clipping.
2284
2285 return true;
2286
2287 }
2288
2289 /*****************************************************************************/
2290
2291 void dng_rgb_table::SetInvalid ()
2292 {
2293
2294 *this = dng_rgb_table ();
2295
2296 }
2297
2298 /*****************************************************************************/
2299
2300 void dng_rgb_table::Set (uint32 dimensions,
2301 uint32 divisions,
2302 dng_ref_counted_block samples)
2303 {
2304
2305 if (dimensions == 1)
2306 {
2307
2308 if (divisions < kMinDivisions1D ||
2309 divisions > kMaxDivisions1D)
2310 {
2311
2312 ThrowProgramError ("Bad 1D divisions");
2313
2314 }
2315
2316 if (samples.LogicalSize () != divisions * 4 * sizeof (uint16))
2317 {
2318
2319 ThrowProgramError ("Bad 1D sample count");
2320
2321 }
2322
2323 }
2324
2325 else if (dimensions == 3)
2326 {
2327
2328 if (divisions < kMinDivisions3D ||
2329 divisions > kMaxDivisions3D_InMemory)
2330 {
2331
2332 ThrowProgramError ("Bad 3D divisions");
2333
2334 }
2335
2336 if (samples.LogicalSize () != divisions *
2337 divisions *
2338 divisions * 4 * sizeof (uint16))
2339 {
2340
2341 ThrowProgramError ("Bad 3D sample count");
2342
2343 }
2344
2345 }
2346
2347 else
2348 {
2349
2350 ThrowProgramError ("Bad dimensions");
2351
2352 }
2353
2354 fData.fDimensions = dimensions;
2355
2356 fData.fDivisions = divisions;
2357
2358 fData.fSamples = samples;
2359
2360 fData.ComputeMonochrome ();
2361
2362 RecomputeFingerprint ();
2363
2364 }
2365
2366 /*****************************************************************************/
2367
2368 bool dng_rgb_table::GetStream (dng_stream &stream)
2369 {
2370
2371 table_data data;
2372
2373 if (stream.Get_uint32 () != btt_RGBTable)
2374 {
2375 return false;
2376 }
2377
2378 if (stream.Get_uint32 () != kRGBTableVersion)
2379 {
2380 ThrowBadFormat ("Unknown RGB table version");
2381 }
2382
2383 data.fDimensions = stream.Get_uint32 ();
2384
2385 data.fDivisions = stream.Get_uint32 ();
2386
2387 if (data.fDimensions == 1)
2388 {
2389
2390 if (data.fDivisions < kMinDivisions1D ||
2391 data.fDivisions > kMaxDivisions1D)
2392 {
2393 ThrowBadFormat ("Invalid 1D divisions");
2394 }
2395
2396 }
2397
2398 else if (data.fDimensions == 3)
2399 {
2400
2401 if (data.fDivisions < kMinDivisions3D ||
2402 data.fDivisions > kMaxDivisions3D)
2403 {
2404 ThrowBadFormat ("Invalid 3D divisions");
2405 }
2406
2407 }
2408
2409 else
2410 {
2411 ThrowBadFormat ("Invalid dimensions");
2412 }
2413
2414 uint16 nopValue [kMaxDivisions1D > kMaxDivisions3D ? kMaxDivisions1D
2415 : kMaxDivisions3D];
2416
2417 for (uint32 index = 0; index < data.fDivisions; index++)
2418 {
2419
2420 nopValue [index] = (uint16)
2421 ((index * 0x0FFFF + (data.fDivisions >> 1)) /
2422 (data.fDivisions - 1));
2423
2424 }
2425
2426 if (data.fDimensions == 1)
2427 {
2428
2429 data.fSamples.Allocate (data.fDivisions * 4 * sizeof (uint16));
2430
2431 uint16 *samples = data.fSamples.Buffer_uint16 ();
2432
2433 for (uint32 index = 0; index < data.fDivisions; index++)
2434 {
2435
2436 samples [0] = stream.Get_uint16 () + nopValue [index];
2437 samples [1] = stream.Get_uint16 () + nopValue [index];
2438 samples [2] = stream.Get_uint16 () + nopValue [index];
2439 samples [3] = 0;
2440
2441 samples += 4;
2442
2443 }
2444
2445 }
2446
2447 else
2448 {
2449
2450 data.fSamples.Allocate (data.fDivisions *
2451 data.fDivisions *
2452 data.fDivisions * 4 * sizeof (uint16));
2453
2454 uint16 *samples = data.fSamples.Buffer_uint16 ();
2455
2456 for (uint32 rIndex = 0; rIndex < data.fDivisions; rIndex++)
2457 {
2458
2459 for (uint32 gIndex = 0; gIndex < data.fDivisions; gIndex++)
2460 {
2461
2462 for (uint32 bIndex = 0; bIndex < data.fDivisions; bIndex++)
2463 {
2464
2465 samples [0] = stream.Get_uint16 () + nopValue [rIndex];
2466 samples [1] = stream.Get_uint16 () + nopValue [gIndex];
2467 samples [2] = stream.Get_uint16 () + nopValue [bIndex];
2468 samples [3] = 0;
2469
2470 samples += 4;
2471
2472 }
2473
2474 }
2475
2476 }
2477
2478 }
2479
2480 uint32 primaries = stream.Get_uint32 ();
2481
2482 if (primaries >= primaries_count)
2483 {
2484 ThrowBadFormat ("Unknown RGB table primaries");
2485 }
2486
2487 data.fPrimaries = (primaries_enum) primaries;
2488
2489 uint32 gamma = stream.Get_uint32 ();
2490
2491 if (gamma >= gamma_count)
2492 {
2493 ThrowBadFormat ("Unknown RGB table gamma");
2494 }
2495
2496 data.fGamma = (gamma_enum) gamma;
2497
2498 uint32 gamut = stream.Get_uint32 ();
2499
2500 if (gamut >= gamut_count)
2501 {
2502 ThrowBadFormat ("Unknown RGB table gamut processing option");
2503 }
2504
2505 data.fGamut = (gamut_enum) gamut;
2506
2507 data.fMinAmount = stream.Get_real64 ();
2508 data.fMaxAmount = stream.Get_real64 ();
2509
2510 if (data.fMinAmount < 0.0 || data.fMinAmount > 1.0 || data.fMaxAmount < 1.0)
2511 {
2512 ThrowBadFormat ("Invalid min/max amount for RGB table");
2513 }
2514
2515 data.ComputeMonochrome ();
2516
2517 if (stream.Position () + 4 <= stream.Length ())
2518 {
2519 data.fFlags = stream.Get_uint32 ();
2520 }
2521
2522 fData = data;
2523
2524 return true;
2525
2526 }
2527
2528 /*****************************************************************************/
2529
2530 void dng_rgb_table::PutStream (dng_stream &stream,
2531 bool /* forFingerprint */) const
2532 {
2533
2534 DNG_REQUIRE (IsValid (), "Invalid RGB Table");
2535
2536 stream.Put_uint32 (btt_RGBTable);
2537
2538 stream.Put_uint32 (kRGBTableVersion);
2539
2540 stream.Put_uint32 (fData.fDimensions);
2541
2542 stream.Put_uint32 (fData.fDivisions);
2543
2544 uint16 nopValue [kMaxDivisions1D > kMaxDivisions3D_InMemory ? kMaxDivisions1D
2545 : kMaxDivisions3D_InMemory];
2546
2547 for (uint32 index = 0; index < fData.fDivisions; index++)
2548 {
2549
2550 nopValue [index] = (uint16)
2551 ((index * 0x0FFFF + (fData.fDivisions >> 1)) /
2552 (fData.fDivisions - 1));
2553
2554 }
2555
2556 const uint16 *samples = fData.fSamples.Buffer_uint16 ();
2557
2558 if (fData.fDimensions == 1)
2559 {
2560
2561 for (uint32 index = 0; index < fData.fDivisions; index++)
2562 {
2563
2564 stream.Put_uint16 (samples [0] - nopValue [index]);
2565 stream.Put_uint16 (samples [1] - nopValue [index]);
2566 stream.Put_uint16 (samples [2] - nopValue [index]);
2567
2568 samples += 4;
2569
2570 }
2571
2572 }
2573
2574 else
2575 {
2576
2577 for (uint32 rIndex = 0; rIndex < fData.fDivisions; rIndex++)
2578 {
2579
2580 for (uint32 gIndex = 0; gIndex < fData.fDivisions; gIndex++)
2581 {
2582
2583 for (uint32 bIndex = 0; bIndex < fData.fDivisions; bIndex++)
2584 {
2585
2586 stream.Put_uint16 (samples [0] - nopValue [rIndex]);
2587 stream.Put_uint16 (samples [1] - nopValue [gIndex]);
2588 stream.Put_uint16 (samples [2] - nopValue [bIndex]);
2589
2590 samples += 4;
2591
2592 }
2593
2594 }
2595
2596 }
2597
2598 }
2599
2600 stream.Put_uint32 ((uint32) fData.fPrimaries);
2601
2602 stream.Put_uint32 ((uint32) fData.fGamma);
2603
2604 stream.Put_uint32 ((uint32) fData.fGamut);
2605
2606 stream.Put_real64 (fData.fMinAmount);
2607 stream.Put_real64 (fData.fMaxAmount);
2608
2609 if (fData.fFlags != 0)
2610 {
2611
2612 stream.Put_uint32 (fData.fFlags);
2613
2614 }
2615
2616 }
2617
2618 /*****************************************************************************/
2619 /*****************************************************************************/
2620 /*****************************************************************************/
2621
2622 dng_image_table_compression_info::~dng_image_table_compression_info ()
2623 {
2624
2625 }
2626
2627 /*****************************************************************************/
2628
2629 void dng_image_table_compression_info::Compress (dng_host &host,
2630 dng_stream &stream,
2631 const dng_image &image) const
2632 {
2633
2634 dng_image_writer writer;
2635
2636 writer.WriteTIFFWithProfile (host,
2637 stream,
2638 image,
2639 image.Planes () >= 3
2640 ? piRGB
2641 : piBlackIsZero,
2642 image.PixelType () == ttShort
2643 ? ccJPEG // Lossless JPEG
2644 : ccDeflate);
2645
2646 }
2647
2648 /*****************************************************************************/
2649 /*****************************************************************************/
2650 /*****************************************************************************/
2651
2652 dng_image_table_jxl_compression_info::dng_image_table_jxl_compression_info ()
2653
2654 : fEncodeSettings (new dng_jxl_encode_settings)
2655
2656 {
2657
2658 }
2659
2660 /*****************************************************************************/
2661
2662 void dng_image_table_jxl_compression_info::Compress (dng_host &host,
2663 dng_stream &stream,
2664 const dng_image &image) const
2665 {
2666
2667 DNG_REQUIRE (fEncodeSettings.Get (),
2668 "Missing encode settings");
2669
2670 #if 1
2671
2672 // Use TIFF container but with JXL compression. This is better for larger
2673 // images since we can encode tiles in parallel.
2674
2675 host.SetJXLEncodeSettings (*fEncodeSettings);
2676
2677 host.SetJXLColorSpaceInfo (fColorSpaceInfo);
2678
2679 dng_image_writer writer;
2680
2681 writer.WriteTIFFWithProfile (host,
2682 stream,
2683 image,
2684 image.Planes () >= 3
2685 ? piRGB
2686 : piBlackIsZero,
2687 ccJXL,
2688 nullptr, // metadata
2689 nullptr, // profile data
2690 0, // profile size,
2691 nullptr, // resolution
2692 nullptr, // thumbnail
2693 nullptr, // image resources
2694 kMetadataSubset_All,
2695 false, // has transparency
2696 true, // allow big tiff
2697 nullptr, // gain map,
2698 fPreferHalfFloat);
2699
2700 #else
2701
2702 // Use JXL directly.
2703
2704 dng_jxl_color_space_info colorSpaceInfo;
2705
2706 PreviewColorSpaceToJXLEncoding (previewColorSpace_MaxEnum,
2707 image.Planes (),
2708 colorSpaceInfo);
2709
2710 EncodeJXL_Tile (host,
2711 stream,
2712 image,
2713 colorSpaceInfo,
2714 *fEncodeSettings);
2715
2716 #endif
2717
2718 }
2719
2720 /*****************************************************************************/
2721 /*****************************************************************************/
2722 /*****************************************************************************/
2723
2724 dng_image_table::dng_image_table ()
2725
2726 : dng_big_table (&gImageTableCache)
2727
2728 {
2729
2730 }
2731
2732 /*****************************************************************************/
2733
2734 dng_image_table::dng_image_table (const dng_image_table &table)
2735
2736 : dng_big_table (table)
2737
2738 , fImage (table.fImage)
2739
2740 , fCompressedData (table.fCompressedData)
2741
2742 , fCompressionType (table.fCompressionType)
2743
2744 {
2745
2746 }
2747
2748 /*****************************************************************************/
2749
2750 dng_image_table & dng_image_table::operator= (const dng_image_table &table)
2751 {
2752
2753 dng_big_table::operator= (table);
2754
2755 fImage = table.fImage;
2756
2757 fCompressedData = table.fCompressedData;
2758
2759 fCompressionType = table.fCompressionType;
2760
2761 return *this;
2762
2763 }
2764
2765 /*****************************************************************************/
2766
2767 dng_image_table::~dng_image_table ()
2768 {
2769
2770 }
2771
2772 /*****************************************************************************/
2773
2774 bool dng_image_table::IsValid () const
2775 {
2776
2777 if (IsMissing ())
2778 {
2779 return false;
2780 }
2781
2782 if (!fImage.get ())
2783 {
2784 return false;
2785 }
2786
2787 return true;
2788
2789 }
2790
2791 /*****************************************************************************/
2792
2793 void dng_image_table::SetInvalid ()
2794 {
2795
2796 *this = dng_image_table ();
2797
2798 RecomputeFingerprint ();
2799
2800 }
2801
2802 /*****************************************************************************/
2803
2804 void dng_image_table::SetImage (const dng_image *image,
2805 const dng_image_table_compression_info *compressionInfo,
2806 dng_abort_sniffer *sniffer)
2807 {
2808
2809 fImage = std::shared_ptr<const dng_image> (image);
2810
2811 fCompressedData.reset ();
2812
2813 if (compressionInfo && (compressionInfo->Type () > 0))
2814 CompressImage (*compressionInfo,
2815 sniffer);
2816
2817 RecomputeFingerprint ();
2818
2819 }
2820
2821 /*****************************************************************************/
2822
2823 void dng_image_table::SetImage (const std::shared_ptr<const dng_image> &image,
2824 const dng_image_table_compression_info *compressionInfo,
2825 dng_abort_sniffer *sniffer)
2826 {
2827
2828 if (fImage != image)
2829 {
2830
2831 fImage = image;
2832
2833 fCompressedData.reset ();
2834
2835 if (compressionInfo && (compressionInfo->Type () > 0))
2836 CompressImage (*compressionInfo,
2837 sniffer);
2838
2839 RecomputeFingerprint ();
2840
2841 }
2842
2843 }
2844
2845 /*****************************************************************************/
2846
2847 dng_host * dng_image_table::MakeHost (dng_abort_sniffer *sniffer) const
2848 {
2849
2850 return new dng_host (nullptr, sniffer);
2851
2852 }
2853
2854 /*****************************************************************************/
2855
2856 dng_fingerprint dng_image_table::ComputeFingerprint () const
2857 {
2858
2859 // If we're using lossy compression, then fingerprint the (compressed)
2860 // stream itself, including the header.
2861
2862 if (fCompressedData)
2863 {
2864
2865 AutoPtr<dng_host> host (MakeHost (nullptr));
2866
2867 dng_memory_stream tempStream (host->Allocator ());
2868
2869 PutStream (tempStream, true);
2870
2871 tempStream.Flush ();
2872
2873 tempStream.SetReadPosition (0);
2874
2875 dng_md5_printer_stream stream;
2876
2877 stream.SetLittleEndian ();
2878
2879 tempStream.CopyToStream (stream,
2880 tempStream.Length ());
2881
2882 auto digest = stream.Result ();
2883
2884 return digest;
2885
2886 }
2887
2888 // Otherwise fingerprint the image itself (uncompressed case).
2889
2890 if (fImage.get ())
2891 {
2892
2893 AutoPtr<dng_host> host (MakeHost (nullptr));
2894
2895 dng_md5_printer_stream stream;
2896
2897 stream.SetLittleEndian ();
2898
2899 stream.Put_uint32 (btt_ImageTable);
2900
2901 stream.Put_uint32 (kImageTableVersion);
2902
2903 stream.Put_int32 (fImage->Bounds ().t);
2904 stream.Put_int32 (fImage->Bounds ().l);
2905 stream.Put_int32 (fImage->Bounds ().b);
2906 stream.Put_int32 (fImage->Bounds ().r);
2907
2908 stream.Put_uint32 (fImage->Planes ());
2909
2910 stream.Put_uint32 (fImage->PixelType ());
2911
2912 dng_fingerprint imageDigest = dng_negative::FindFastImageDigest
2913 (*host,
2914 *fImage,
2915 fImage->PixelType ());
2916
2917 stream.Put (imageDigest.data, 16);
2918
2919 return stream.Result ();
2920
2921 }
2922
2923 return dng_fingerprint ();
2924
2925 }
2926
2927 /*****************************************************************************/
2928
2929 static void CheckImageTableIFD (const dng_ifd &ifd)
2930 {
2931
2932 dng_rect bounds = ifd.Bounds ();
2933
2934 if (bounds.ShortSide () < 1 ||
2935 bounds.LongSide () > kMaxImageSide)
2936 {
2937 ThrowBadFormat ();
2938 }
2939
2940 uint32 planes = ifd.fSamplesPerPixel;
2941
2942 if (planes < 1 || planes > kMaxColorPlanes)
2943 {
2944 ThrowBadFormat ();
2945 }
2946
2947 uint32 pixelType = ifd.PixelType ();
2948
2949 if (pixelType != ttByte &&
2950 pixelType != ttShort &&
2951 pixelType != ttFloat)
2952 {
2953 ThrowBadFormat ();
2954 }
2955
2956 }
2957
2958 /*****************************************************************************/
2959
2960 bool dng_image_table::GetStream (dng_stream &stream)
2961 {
2962
2963 AutoPtr<dng_host> host (MakeHost (stream.Sniffer ()));
2964
2965 if (stream.Get_uint32 () != btt_ImageTable)
2966 {
2967 return false;
2968 }
2969
2970 if (stream.Get_uint32 () != kImageTableVersion)
2971 {
2972 ThrowBadFormat ("Unknown image table version");
2973 }
2974
2975 dng_point imageTL;
2976
2977 imageTL.v = stream.Get_int32 ();
2978 imageTL.h = stream.Get_int32 ();
2979
2980 if (!stream.Data ())
2981 {
2982 ThrowProgramError ("Not a memory stream");
2983 }
2984
2985 dng_stream subStream ((uint8 *) stream.Data () + stream.Position (),
2986 (uint32) (stream.Length () - stream.Position ()));
2987
2988 subStream.SetSniffer (stream.Sniffer ());
2989
2990 AutoPtr<dng_image> image;
2991
2992 dng_info jxlInfo;
2993
2994 if (ParseJXL (*host,
2995 subStream,
2996 jxlInfo,
2997 true, // bare codestream
2998 false)) // container
2999 {
3000
3001 // Read as JXL.
3002
3003 if (jxlInfo.IFDCount () < 1)
3004 {
3005 ThrowBadFormat ();
3006 }
3007
3008 CheckImageTableIFD (*jxlInfo.fIFD [0]);
3009
3010 subStream.SetReadPosition (0);
3011
3012 dng_jxl_decoder decoder;
3013
3014 decoder.fNeedBoxMeta = false;
3015
3016 decoder.Decode (*host, subStream);
3017
3018 image.Reset (decoder.fMainImage.Release ());
3019
3020 fCompressionType = ccJXL;
3021
3022 }
3023
3024 else
3025 {
3026
3027 // Read as TIFF.
3028
3029 dng_info info;
3030
3031 info.Parse (*host, subStream);
3032
3033 info.PostParse (*host);
3034
3035 if (info.fMagic != 42)
3036 {
3037 ThrowBadFormat ();
3038 }
3039
3040 if (info.IFDCount () < 1)
3041 {
3042 ThrowBadFormat ();
3043 }
3044
3045 const dng_ifd &ifd = *info.fIFD [0];
3046
3047 CheckImageTableIFD (ifd);
3048
3049 image.Reset (host->Make_dng_image (ifd.Bounds (),
3050 ifd.fSamplesPerPixel,
3051 ifd.PixelType ()));
3052
3053 ifd.ReadImage (*host,
3054 subStream,
3055 *image);
3056
3057 fCompressionType = ifd.fCompression;
3058
3059 } // JXL vs TIFF
3060
3061 // Grab a copy of the (lossy) compressed data.
3062
3063 if (fCompressionType == ccJXL)
3064 {
3065
3066 subStream.SetReadPosition (0);
3067
3068 fCompressedData.reset (subStream.AsMemoryBlock (host->Allocator ()));
3069
3070 }
3071
3072 if (imageTL != dng_point (0, 0))
3073 {
3074
3075 AutoPtr<dng_image> tempImage (image->Clone ());
3076
3077 tempImage->Offset (imageTL);
3078
3079 image.Reset (tempImage.Release ());
3080
3081 }
3082
3083 fImage = std::shared_ptr<const dng_image> (image.Release ());
3084
3085 return true;
3086
3087 }
3088
3089 /*****************************************************************************/
3090
3091 void dng_image_table::PutStream (dng_stream &stream,
3092 bool forFingerprint) const
3093 {
3094
3095 dng_image_table_compression_info defaultInfo;
3096
3097 PutCompressedStream (stream,
3098 forFingerprint,
3099 defaultInfo);
3100
3101 }
3102
3103 /*****************************************************************************/
3104
3105 void dng_image_table::PutCompressedStream (dng_stream &stream,
3106 bool /* forFingerprint */,
3107 const dng_image_table_compression_info &info) const
3108 {
3109
3110 AutoPtr<dng_host> host (MakeHost (stream.Sniffer ()));
3111
3112 stream.Put_uint32 (btt_ImageTable);
3113
3114 stream.Put_uint32 (kImageTableVersion);
3115
3116 stream.Put_int32 (fImage->Bounds ().t);
3117 stream.Put_int32 (fImage->Bounds ().l);
3118
3119 const dng_image *tiffImage = fImage.get ();
3120
3121 AutoPtr<dng_image> tempImage;
3122
3123 if (tiffImage->Bounds ().TL () != dng_point (0, 0))
3124 {
3125
3126 tempImage.Reset (tiffImage->Clone ());
3127
3128 tempImage->Offset (dng_point (0, 0) - fImage->Bounds ().TL ());
3129
3130 tiffImage = tempImage.Get ();
3131
3132 }
3133
3134 // If we have compressed data, then just write that directly.
3135
3136 if (fCompressedData)
3137 {
3138
3139 // printf ("--- writing compressed\n");
3140
3141 stream.Put (fCompressedData->Buffer (),
3142 fCompressedData->LogicalSize ());
3143
3144 }
3145
3146 // Otherwise use the provided compression info.
3147
3148 else
3149 {
3150
3151 dng_memory_stream tempStream (host->Allocator ());
3152
3153 info.Compress (*host,
3154 tempStream,
3155 *tiffImage);
3156
3157 // Remember the compressed data.
3158
3159 if (info.Type () != 0)
3160 {
3161
3162 tempStream.SetReadPosition (0);
3163
3164 fCompressedData.reset (tempStream.AsMemoryBlock (host->Allocator ()));
3165
3166 }
3167
3168 tempStream.SetReadPosition (0);
3169
3170 tempStream.CopyToStream (stream, tempStream.Length ());
3171
3172 }
3173
3174 }
3175
3176 /*****************************************************************************/
3177
3178 void dng_image_table::CompressImage (const dng_image_table_compression_info &info,
3179 dng_abort_sniffer *sniffer)
3180 {
3181
3182 fCompressionType = info.Type ();
3183
3184 if (!fImage ||
3185 info.Type () == 0 ||
3186 info.Type () == ccUncompressed)
3187 {
3188 return;
3189 }
3190
3191 // Force the image go thru a write-read (encode-decode) cycle so that the
3192 // image stored in this object reflects errors introduced by the lossy
3193 // codec.
3194
3195 AutoPtr<dng_host> host (MakeHost (sniffer));
3196
3197 dng_memory_stream tempStream (host->Allocator ());
3198
3199 tempStream.SetSniffer (sniffer);
3200
3201 PutCompressedStream (tempStream,
3202 false,
3203 info);
3204
3205 // Cannot just reset tempStream read position to 0 and call GetStream on
3206 // it directly because dng_image_table::GetStream implementation currently
3207 // relies on the stream having the data in one contiguous chunk. So make a
3208 // copy of the data and then feed the result to GetStream.
3209
3210 AutoPtr<dng_memory_block> block (tempStream.AsMemoryBlock (host->Allocator ()));
3211
3212 dng_stream readStream (block->Buffer (),
3213 block->LogicalSize ());
3214
3215 readStream.SetSniffer (sniffer);
3216
3217 GetStream (readStream);
3218
3219 fCompressionType = info.Type ();
3220
3221 }
3222
3223 /*****************************************************************************/
3224
3225 void dng_image_table::SetData (const dng_image_table_data &data)
3226 {
3227
3228 fImage = data.fImage;
3229
3230 fCompressedData = data.fCompressedData;
3231
3232 fCompressionType = data.fCompressionType;
3233
3234 }
3235
3236 /*****************************************************************************/
3237
3238 void dng_image_table::GetData (dng_image_table_data &data) const
3239 {
3240
3241 data.fImage = fImage;
3242
3243 data.fCompressedData = fCompressedData;
3244
3245 data.fCompressionType = fCompressionType;
3246
3247 }
3248
3249 /*****************************************************************************/
3250 /*****************************************************************************/
3251 /*****************************************************************************/
3252
3253 dng_packed_image_table::dng_packed_image_table ()
3254
3255 : dng_big_table (&gPackedImageTableCache)
3256
3257 {
3258
3259 }
3260
3261 /*****************************************************************************/
3262
3263 dng_packed_image_table::dng_packed_image_table (const dng_packed_image_table &table)
3264
3265 : dng_big_table (table)
3266
3267 , fTableDigest (table.fTableDigest)
3268 , fTable (table.CloneTable ())
3269 , fBlock (table.fBlock)
3270
3271 , fSize (table.fSize)
3272 , fPlanes (table.fPlanes)
3273 , fPixelType (table.fPixelType)
3274
3275 {
3276
3277 }
3278
3279 /*****************************************************************************/
3280
3281 dng_packed_image_table & dng_packed_image_table::operator= (const dng_packed_image_table &table)
3282 {
3283
3284 dng_big_table::operator= (table);
3285
3286 fTableDigest = table.fTableDigest;
3287 fBlock = table.fBlock;
3288
3289 fTable.reset (table.CloneTable ());
3290
3291 fSize = table.fSize;
3292 fPlanes = table.fPlanes;
3293 fPixelType = table.fPixelType;
3294
3295 return *this;
3296
3297 }
3298
3299 /*****************************************************************************/
3300
3301 const dng_image_table & dng_packed_image_table::Table () const
3302 {
3303
3304 DNG_REQUIRE (fTable, "Invalid table");
3305
3306 return *fTable;
3307
3308 }
3309
3310 /*****************************************************************************/
3311
3312 bool dng_packed_image_table::IsValid () const
3313 {
3314
3315 return (fTableDigest.IsValid () &&
3316 ((fTable && fTable->IsValid ()) || (fBlock != nullptr)));
3317
3318 }
3319
3320 /*****************************************************************************/
3321
3322 uint32 dng_packed_image_table::PackedBytes () const
3323 {
3324
3325 return fBlock ? fBlock->LogicalSize () : 0;
3326
3327 }
3328
3329 /*****************************************************************************/
3330
3331 dng_host * dng_packed_image_table::MakeHost (dng_abort_sniffer *sniffer) const
3332 {
3333
3334 return new dng_host (nullptr, sniffer);
3335
3336 }
3337
3338 /*****************************************************************************/
3339
3340 dng_image_table * dng_packed_image_table::MakeTable () const
3341 {
3342
3343 return new dng_image_table;
3344
3345 }
3346
3347 /*****************************************************************************/
3348
3349 dng_image_table * dng_packed_image_table::CloneTable () const
3350 {
3351
3352 return fTable ? (new dng_image_table (*fTable)) : nullptr;
3353
3354 }
3355
3356 /*****************************************************************************/
3357
3358 void dng_packed_image_table::Clear ()
3359 {
3360
3361 fTableDigest.Clear ();
3362
3363 fTable.reset ();
3364
3365 fBlock.reset ();
3366
3367 fSize = dng_point ();
3368
3369 fPlanes = 0;
3370
3371 fPixelType = 0;
3372
3373 }
3374
3375 /*****************************************************************************/
3376
3377 void dng_packed_image_table::ClearPackedData ()
3378 {
3379
3380 fBlock.reset ();
3381
3382 }
3383
3384 /*****************************************************************************/
3385
3386 void dng_packed_image_table::SetImage (const dng_image *image,
3387 const dng_image_table_compression_info *compressionInfo,
3388 dng_abort_sniffer *sniffer)
3389 {
3390
3391 if (!image)
3392 {
3393 Clear ();
3394 return;
3395 }
3396
3397 AutoPtr<dng_image_table> table (MakeTable ());
3398
3399 fSize = image->Size ();
3400 fPlanes = image->Planes ();
3401 fPixelType = image->PixelType ();
3402
3403 table->SetImage (image, compressionInfo, sniffer);
3404
3405 fTable.reset (table.Release ());
3406
3407 fTableDigest = fTable->Fingerprint ();
3408
3409 ClearPackedData ();
3410
3411 RecomputeFingerprint ();
3412
3413 }
3414
3415 /*****************************************************************************/
3416
3417 void dng_packed_image_table::SetImage (const std::shared_ptr<const dng_image> &image,
3418 const dng_image_table_compression_info *compressionInfo,
3419 dng_abort_sniffer *sniffer)
3420 {
3421
3422 if (!image)
3423 {
3424 Clear ();
3425 return;
3426 }
3427
3428 if (fTable && (fTable->ShareImage () == image))
3429 return;
3430
3431 fSize = image->Size ();
3432 fPlanes = image->Planes ();
3433 fPixelType = image->PixelType ();
3434
3435 AutoPtr<dng_image_table> table (MakeTable ());
3436
3437 table->SetImage (image, compressionInfo, sniffer);
3438
3439 fTable.reset (table.Release ());
3440
3441 fTableDigest = fTable->Fingerprint ();
3442
3443 ClearPackedData ();
3444
3445 RecomputeFingerprint ();
3446
3447 }
3448
3449 /*****************************************************************************/
3450
3451 void dng_packed_image_table::Unpack (dng_abort_sniffer *sniffer)
3452 {
3453
3454 DNG_REQUIRE (fBlock,
3455 "Cannot unpack invalid block");
3456
3457 AutoPtr<dng_host> host (MakeHost (sniffer));
3458
3459 AutoPtr<dng_image_table> table (MakeTable ());
3460
3461 if (!table->DecodeFromBinary (*host,
3462 fBlock->Buffer_uint8 (),
3463 fBlock->LogicalSize ()))
3464 {
3465
3466 ThrowBadFormat ("Could not Unpack block to cr_image_table");
3467
3468 }
3469
3470 fTable.reset (table.Release ());
3471
3472 if (fTable->Fingerprint () != fTableDigest)
3473 {
3474
3475 DNG_REPORT ("fTableDigest does not match table fingerprint");
3476
3477 }
3478
3479 #if 0
3480 fSize = Image ().Size ();
3481 fPlanes = Image ().Planes ();
3482 fPixelType = Image ().Size ();
3483 #endif
3484
3485 ClearPackedData ();
3486
3487 }
3488
3489 /*****************************************************************************/
3490
3491 void dng_packed_image_table::Pack (dng_abort_sniffer *sniffer)
3492 {
3493
3494 DNG_REQUIRE (IsValidUnpacked (),
3495 "Cannot pack invalid table");
3496
3497 AutoPtr<dng_host> host (MakeHost (sniffer));
3498
3499 uint32 compressedSize = 0;
3500
3501 fBlock.reset (fTable->EncodeAsBinary (host->Allocator (),
3502 compressedSize));
3503
3504 fTable.reset ();
3505
3506 }
3507
3508 /*****************************************************************************/
3509
3510 bool dng_packed_image_table::GetStream (dng_stream &stream)
3511 {
3512
3513 if (stream.Get_uint32 () != btt_PackedImageTable)
3514 return false;
3515
3516 if (stream.Get_uint32 () != kPackedImageTableVersion)
3517 ThrowBadFormat ("Unknown packed image table version");
3518
3519 // Clear the table.
3520
3521 fTable.reset ();
3522
3523 // Read digest.
3524
3525 stream.Get (fTableDigest.data, 16);
3526
3527 // Read size.
3528
3529 fSize.h = int32 (stream.Get_uint32 ());
3530 fSize.v = int32 (stream.Get_uint32 ());
3531
3532 if (fSize.h <= 0)
3533 ThrowBadFormat ("Invalid size.h in packed image table");
3534
3535 if (fSize.v <= 0)
3536 ThrowBadFormat ("Invalid size.v in packed image table");
3537
3538 // Read planes.
3539
3540 fPlanes = stream.Get_uint32 ();
3541
3542 if (fPlanes == 0 || fPlanes > kMaxColorPlanes)
3543 ThrowBadFormat ("Invalid planes in packed image table");
3544
3545 // Read pixel type.
3546
3547 fPixelType = stream.Get_uint32 ();
3548
3549 if (fPixelType == 0)
3550 ThrowBadFormat ("Invalid pixel type in packed image table");
3551
3552 // Read bytes of image table. Currently limit compressed image tables
3553 // to 32-bit.
3554
3555 const uint32 bytes = stream.Get_uint32 ();
3556
3557 AutoPtr<dng_host> host (MakeHost (stream.Sniffer ()));
3558
3559 AutoPtr<dng_memory_block> block (host->Allocate (bytes));
3560
3561 stream.Get (block->Buffer (), bytes);
3562
3563 fBlock.reset (block.Release ());
3564
3565 return true;
3566
3567 }
3568
3569 /*****************************************************************************/
3570
3571 void dng_packed_image_table::PutStream (dng_stream &stream,
3572 bool /* forFingerprint */) const
3573 {
3574
3575 DNG_REQUIRE (IsValid (),
3576 "Called PutStream on invalid packed image table");
3577
3578 // Write Big Table Type.
3579
3580 stream.Put_uint32 (btt_PackedImageTable);
3581
3582 // Write Version.
3583
3584 stream.Put_uint32 (kPackedImageTableVersion);
3585
3586 // Write digest.
3587
3588 stream.Put (fTableDigest.data, 16);
3589
3590 // Write properties.
3591
3592 stream.Put_uint32 (uint32 (fSize.h));
3593 stream.Put_uint32 (uint32 (fSize.v));
3594
3595 stream.Put_uint32 (fPlanes);
3596
3597 stream.Put_uint32 (fPixelType);
3598
3599 // If the block is valid, just use that.
3600
3601 std::shared_ptr<const dng_memory_block> block;
3602
3603 if (fBlock)
3604 block = fBlock;
3605
3606 // Otherwise, first encode the table into a temporary block.
3607
3608 else
3609 {
3610
3611 DNG_REQUIRE (fTable, "missing fTable");
3612
3613 AutoPtr<dng_host> host (MakeHost (stream.Sniffer ()));
3614
3615 uint32 compressedSize = 0;
3616
3617 block.reset
3618 (fTable->EncodeAsBinary (host->Allocator (),
3619 compressedSize));
3620
3621 }
3622
3623 DNG_REQUIRE (block,
3624 "Missing block");
3625
3626 // Write byte length.
3627
3628 stream.Put_uint32 (block->LogicalSize ());
3629
3630 // Write data.
3631
3632 stream.Put (block->Buffer (),
3633 block->LogicalSize ());
3634
3635 }
3636
3637 /*****************************************************************************/
3638 /*****************************************************************************/
3639 /*****************************************************************************/
3640
3641 dng_masked_rgb_table::dng_masked_rgb_table ()
3642 {
3643
3644 }
3645
3646 /*****************************************************************************/
3647
3648 dng_masked_rgb_table::dng_masked_rgb_table (dng_string tableSemanticName,
3649 uint32 pixelType,
3650 const dng_rgb_table &table)
3651
3652 : fTableSemanticName (tableSemanticName)
3653
3654 , fPixelType (pixelType)
3655
3656 , fTable (table)
3657
3658 {
3659
3660 Validate ();
3661
3662 }
3663
3664 /*****************************************************************************/
3665
3666 void dng_masked_rgb_table::Validate () const
3667 {
3668
3669 // Name cannot be longer than 16-bit.
3670
3671 if (fTableSemanticName.Length () > 65535)
3672 {
3673 ThrowBadFormat ("TableSemanticName too long in RGBTables");
3674 }
3675
3676 // Check PixelType.
3677
3678 CheckPixelType (fPixelType);
3679
3680 // Check Divisions.
3681
3682 CheckDivisions (fTable.Divisions ());
3683
3684 // Check GammaEncoding.
3685
3686 CheckGammaEncoding (fTable.Gamma ());
3687
3688 // Check ColorPrimaries.
3689
3690 CheckColorPrimaries (fTable.Primaries ());
3691
3692 // Check GamutExtension.
3693
3694 CheckGamutExtension (fTable.Gamut ());
3695
3696 // Must be 3D table.
3697
3698 DNG_REQUIRE (fTable.Dimensions () == 3,
3699 "RGBTables must have dimension value of 3");
3700
3701 // For now don't perform any validation on the table data itself.
3702
3703 }
3704
3705 /*****************************************************************************/
3706
3707 static uint8 ColorPrimariesEnumToRGBTablesTagValue
3708 (dng_rgb_table::primaries_enum value)
3709 {
3710
3711 switch (value)
3712 {
3713
3714 case dng_rgb_table::primaries_sRGB: return 0;
3715 case dng_rgb_table::primaries_Adobe: return 1;
3716 case dng_rgb_table::primaries_P3: return 2;
3717 case dng_rgb_table::primaries_Rec2020: return 3;
3718 case dng_rgb_table::primaries_ProPhoto: return 4;
3719
3720 default:
3721 ThrowProgramError ("unsupported primaries_enum value");
3722 break;
3723
3724 }
3725
3726 return 0; // sRGB
3727
3728 }
3729
3730 /*****************************************************************************/
3731
3732 static dng_rgb_table::primaries_enum ColorPrimariesValueToEnum (uint32 x)
3733 {
3734
3735 switch (x)
3736 {
3737
3738 case 0: return dng_rgb_table::primaries_sRGB;
3739 case 1: return dng_rgb_table::primaries_Adobe;
3740 case 2: return dng_rgb_table::primaries_P3;
3741 case 3: return dng_rgb_table::primaries_Rec2020;
3742 case 4: return dng_rgb_table::primaries_ProPhoto;
3743
3744 default:
3745 {
3746 ThrowProgramError ("unsupported ColorPrimaries value");
3747 break;
3748 }
3749
3750 }
3751
3752 return dng_rgb_table::primaries_sRGB;
3753
3754 }
3755
3756 /*****************************************************************************/
3757
3758 static uint32 GamutExtensionEnumToRGBTablesTagValue (dng_rgb_table::gamut_enum value)
3759 {
3760
3761 switch (value)
3762 {
3763
3764 case dng_rgb_table::gamut_clip: return 0;
3765 case dng_rgb_table::gamut_extend: return 1;
3766
3767 default:
3768 ThrowProgramError ("unsupported gamut_enum value");
3769 break;
3770
3771 }
3772
3773 return 1; // extend
3774
3775 }
3776
3777 /*****************************************************************************/
3778
3779 static dng_rgb_table::gamut_enum GamutValueToEnum (uint32 x)
3780 {
3781
3782 switch (x)
3783 {
3784
3785 case 0: return dng_rgb_table::gamut_clip;
3786 case 1: return dng_rgb_table::gamut_extend;
3787
3788 default:
3789 {
3790 ThrowProgramError ("Unexpected GamutExtension value");
3791 break;
3792 }
3793
3794 }
3795
3796 return dng_rgb_table::gamut_extend;
3797
3798 }
3799
3800 /*****************************************************************************/
3801
3802 static uint8 GammaEnumToRGBTablesTagValue (dng_rgb_table::gamma_enum value)
3803 {
3804
3805 switch (value)
3806 {
3807
3808 case dng_rgb_table::gamma_Linear: return 0;
3809 case dng_rgb_table::gamma_sRGB: return 1;
3810 case dng_rgb_table::gamma_1_8: return 2;
3811 case dng_rgb_table::gamma_2_2: return 3;
3812 case dng_rgb_table::gamma_Rec2020: return 4;
3813
3814 default:
3815 ThrowProgramError ("unsupported gamma_enum value");
3816 break;
3817
3818 }
3819
3820 return 1; // sRGB
3821
3822 }
3823
3824 /*****************************************************************************/
3825
3826 static dng_rgb_table::gamma_enum GammaValueToEnum (uint32 x)
3827 {
3828
3829 switch (x)
3830 {
3831
3832 case 0: return dng_rgb_table::gamma_Linear;
3833 case 1: return dng_rgb_table::gamma_sRGB;
3834 case 2: return dng_rgb_table::gamma_1_8;
3835 case 3: return dng_rgb_table::gamma_2_2;
3836 case 4: return dng_rgb_table::gamma_Rec2020;
3837
3838 default:
3839 {
3840 ThrowProgramError ("Unexpected GammaEncoding value");
3841 break;
3842 }
3843
3844 }
3845
3846 return dng_rgb_table::gamma_sRGB;
3847
3848 }
3849
3850 /*****************************************************************************/
3851
3852 void dng_masked_rgb_table::CheckDivisions (uint32 divisions)
3853 {
3854
3855 if (divisions < 2 || divisions > 32)
3856 {
3857 ThrowBadFormat ("Invalid Divisions in RGBTables");
3858 }
3859
3860 }
3861
3862 /*****************************************************************************/
3863
3864 void dng_masked_rgb_table::CheckPixelType (uint32 pixelType)
3865 {
3866
3867 if (pixelType > 2)
3868 {
3869 ThrowBadFormat ("Invalid PixelType in RGBTables");
3870 }
3871
3872 }
3873
3874 /*****************************************************************************/
3875
3876 void dng_masked_rgb_table::CheckGammaEncoding (dng_rgb_table::gamma_enum gamma)
3877 {
3878
3879 switch (gamma)
3880 {
3881
3882 // Enumerate the ones supported in the tag.
3883
3884 case dng_rgb_table::gamma_Linear:
3885 case dng_rgb_table::gamma_sRGB:
3886 case dng_rgb_table::gamma_1_8:
3887 case dng_rgb_table::gamma_2_2:
3888 case dng_rgb_table::gamma_Rec2020:
3889 break;
3890
3891 // Reject everything else.
3892
3893 default:
3894 ThrowBadFormat ("Unsupported GammaEncoding in RGBTables");
3895
3896 }
3897
3898 }
3899
3900 /*****************************************************************************/
3901
3902 void dng_masked_rgb_table::CheckColorPrimaries (dng_rgb_table::primaries_enum primaries)
3903 {
3904
3905 switch (primaries)
3906 {
3907
3908 // Enumerate the ones supported in the tag.
3909
3910 case dng_rgb_table::primaries_Adobe:
3911 case dng_rgb_table::primaries_P3:
3912 case dng_rgb_table::primaries_ProPhoto:
3913 case dng_rgb_table::primaries_Rec2020:
3914 case dng_rgb_table::primaries_sRGB:
3915 break;
3916
3917 // Reject everything else.
3918
3919 default:
3920 ThrowBadFormat ("Unsupported ColorPrimaries in RGBTables");
3921
3922 }
3923
3924 }
3925
3926 /*****************************************************************************/
3927
3928 void dng_masked_rgb_table::CheckGamutExtension (dng_rgb_table::gamut_enum gamut)
3929 {
3930
3931 switch (gamut)
3932 {
3933
3934 // Enumerate the ones supported in the tag.
3935
3936 case dng_rgb_table::gamut_clip:
3937 case dng_rgb_table::gamut_extend:
3938 break;
3939
3940 // Reject everything else.
3941
3942 default:
3943 ThrowBadFormat ("Unsupported GamutExtension in RGBTables");
3944
3945 }
3946
3947 }
3948
3949 /*****************************************************************************/
3950
3951 void dng_masked_rgb_table::GetStream (dng_host &host,
3952 dng_stream &stream)
3953 {
3954
3955 // Read LengthTableSemanticName and TableSemanticName.
3956
3957 uint16 nameLen = stream.Get_uint16 ();
3958
3959 dng_memory_data nameData (nameLen + 1);
3960
3961 stream.Get (nameData.Buffer (), nameLen);
3962
3963 nameData.Buffer_uint8 () [nameLen] = 0; // add null-term char
3964
3965 fTableSemanticName.Set (nameData.Buffer_char ());
3966
3967 // Read Divisions.
3968
3969 uint32 divisions = (uint32) stream.Get_uint8 ();
3970
3971 CheckDivisions (divisions);
3972
3973 // Read PixelType.
3974
3975 fPixelType = (uint32) stream.Get_uint8 ();
3976
3977 CheckPixelType (fPixelType);
3978
3979 // Read GammaEncoding.
3980
3981 uint32 gamma = (uint32) stream.Get_uint8 ();
3982
3983 auto gammaEnum = GammaValueToEnum (gamma);
3984
3985 CheckGammaEncoding (gammaEnum);
3986
3987 // Read ColorPrimaries.
3988
3989 uint32 primaries = (uint32) stream.Get_uint8 ();
3990
3991 auto primariesEnum = ColorPrimariesValueToEnum (primaries);
3992
3993 CheckColorPrimaries (primariesEnum);
3994
3995 // Read GamutExtension.
3996
3997 uint32 gamut = (uint32) stream.Get_uint8 ();
3998
3999 auto gamutEnum = GamutValueToEnum (gamut);
4000
4001 CheckGamutExtension (gamutEnum);
4002
4003 // Read samples.
4004
4005 uint32 srcPixelSize = 1;
4006
4007 if (fPixelType == 1) srcPixelSize = 2;
4008 else if (fPixelType == 2) srcPixelSize = 4;
4009
4010 uint32 srcBytes = divisions * divisions * divisions * 3 * srcPixelSize;
4011
4012 uint32 dstBytes = divisions * divisions * divisions * 4 * 2;
4013
4014 dng_ref_counted_block samples;
4015
4016 samples.Allocate (dstBytes);
4017
4018 uint16 *dst = samples.Buffer_uint16 ();
4019
4020 const uint32 divs = divisions;
4021
4022 // Read into a stored data block.
4023
4024 fStoredData.reset (host.Allocate (srcBytes));
4025
4026 stream.Get (fStoredData->Buffer (),
4027 srcBytes);
4028
4029 if (fPixelType == 0) // u8
4030 {
4031
4032 const uint8 *src = fStoredData->Buffer_uint8 ();
4033
4034 constexpr uint16 scale8to16 = 257; // 65535 / 255
4035
4036 for (uint32 rIndex = 0; rIndex < divs; rIndex++)
4037 {
4038
4039 for (uint32 gIndex = 0; gIndex < divs; gIndex++)
4040 {
4041
4042 for (uint32 bIndex = 0; bIndex < divs; bIndex++)
4043 {
4044
4045 dst [0] = uint16 (src [0]) * scale8to16;
4046 dst [1] = uint16 (src [1]) * scale8to16;
4047 dst [2] = uint16 (src [2]) * scale8to16;
4048 dst [3] = 0;
4049
4050 src += 3;
4051 dst += 4;
4052
4053 } // b
4054
4055 } // g
4056
4057 } // r
4058
4059 }
4060
4061 else if (fPixelType == 1) // u16
4062 {
4063
4064 const uint16 *src = fStoredData->Buffer_uint16 ();
4065
4066 for (uint32 rIndex = 0; rIndex < divs; rIndex++)
4067 {
4068
4069 for (uint32 gIndex = 0; gIndex < divs; gIndex++)
4070 {
4071
4072 for (uint32 bIndex = 0; bIndex < divs; bIndex++)
4073 {
4074
4075 dst [0] = src [0];
4076 dst [1] = src [1];
4077 dst [2] = src [2];
4078 dst [3] = 0;
4079
4080 src += 3;
4081 dst += 4;
4082
4083 } // b
4084
4085 } // g
4086
4087 } // r
4088
4089 }
4090
4091 else if (fPixelType == 2) // fp32
4092 {
4093
4094 // Map 1.0f to 65535.
4095
4096 const real32 *src = fStoredData->Buffer_real32 ();
4097
4098 const real32 kScale = 65535.0f;
4099
4100 for (uint32 rIndex = 0; rIndex < divs; rIndex++)
4101 {
4102
4103 for (uint32 gIndex = 0; gIndex < divs; gIndex++)
4104 {
4105
4106 for (uint32 bIndex = 0; bIndex < divs; bIndex++)
4107 {
4108
4109 dst [0] = Round_int32 (kScale * src [0]);
4110 dst [1] = Round_int32 (kScale * src [1]);
4111 dst [2] = Round_int32 (kScale * src [2]);
4112 dst [3] = 0;
4113
4114 src += 3;
4115 dst += 4;
4116
4117 } // b
4118
4119 } // g
4120
4121 } // r
4122
4123 }
4124
4125 else
4126 {
4127
4128 ThrowProgramError ("Unexpected fPixelType");
4129
4130 }
4131
4132 const uint32 kDimensions = 3;
4133
4134 fTable.Set (kDimensions,
4135 divisions,
4136 samples);
4137
4138 fTable.SetGamut (gamutEnum);
4139
4140 fTable.SetGamma (gammaEnum);
4141
4142 fTable.SetPrimaries (primariesEnum);
4143
4144 }
4145
4146 /*****************************************************************************/
4147
4148 void dng_masked_rgb_table::PutStream (dng_stream &stream) const
4149 {
4150
4151 // LengthTableSemanticName.
4152
4153 uint32 len = fTableSemanticName.Length ();
4154
4155 stream.Put_uint16 ((uint16) len);
4156
4157 // TableSemanticName.
4158
4159 stream.Put (fTableSemanticName.Get (), len);
4160
4161 // Divisions.
4162
4163 stream.Put_uint8 ((uint8) fTable.Divisions ());
4164
4165 // PixelType.
4166
4167 stream.Put_uint8 ((uint8) fPixelType);
4168
4169 // GammaEncoding.
4170
4171 stream.Put_uint8 (GammaEnumToRGBTablesTagValue (fTable.Gamma ()));
4172
4173 // ColorPrimaries.
4174
4175 stream.Put_uint8 (ColorPrimariesEnumToRGBTablesTagValue (fTable.Primaries ()));
4176
4177 // GamutExtension.
4178
4179 stream.Put_uint8 (GamutExtensionEnumToRGBTablesTagValue (fTable.Gamut ()));
4180
4181 // Table sample data.
4182
4183 // If we have stored data, just write that.
4184
4185 if (fStoredData.get ())
4186 {
4187
4188 stream.Put (fStoredData->Buffer (),
4189 fStoredData->LogicalSize ());
4190
4191 }
4192
4193 // Otherwise derive the data from the internal table representation.
4194
4195 else
4196 {
4197
4198 // The data is stored in R, G, B order (R = outermost, B = innermost). The
4199 // dng_rgb_table class already stores its samples in this order, so just
4200 // walk through the values in linear order.
4201
4202 const uint16 *samples = fTable.Samples ();
4203
4204 const uint32 divs = fTable.Divisions ();
4205
4206 if (fPixelType == 1)
4207 {
4208
4209 // Output format is 16-bit unsigned, which matches our internal
4210 // representation, so no conversion needed.
4211
4212 for (uint32 rIndex = 0; rIndex < divs; rIndex++)
4213 {
4214
4215 for (uint32 gIndex = 0; gIndex < divs; gIndex++)
4216 {
4217
4218 for (uint32 bIndex = 0; bIndex < divs; bIndex++)
4219 {
4220
4221 stream.Put_uint16 (samples [0]);
4222 stream.Put_uint16 (samples [1]);
4223 stream.Put_uint16 (samples [2]);
4224
4225 samples += 4;
4226
4227 } // b
4228
4229 } // g
4230
4231 } // r
4232
4233 }
4234
4235 else if (fPixelType == 0)
4236 {
4237
4238 // Output format is 8-bit unsigned, so we need to convert.
4239
4240 for (uint32 rIndex = 0; rIndex < divs; rIndex++)
4241 {
4242
4243 for (uint32 gIndex = 0; gIndex < divs; gIndex++)
4244 {
4245
4246 for (uint32 bIndex = 0; bIndex < divs; bIndex++)
4247 {
4248
4249 stream.Put_uint8 (uint8 ((int32 (samples [0]) + 128) / 257));
4250 stream.Put_uint8 (uint8 ((int32 (samples [1]) + 128) / 257));
4251 stream.Put_uint8 (uint8 ((int32 (samples [2]) + 128) / 257));
4252
4253 samples += 4;
4254
4255 } // b
4256
4257 } // g
4258
4259 } // r
4260
4261 }
4262
4263 else if (fPixelType == 2)
4264 {
4265
4266 // Output format is fp32, so we need to convert.
4267
4268 const real32 kScale = 1.0f / 65535.0f;
4269
4270 for (uint32 rIndex = 0; rIndex < divs; rIndex++)
4271 {
4272
4273 for (uint32 gIndex = 0; gIndex < divs; gIndex++)
4274 {
4275
4276 for (uint32 bIndex = 0; bIndex < divs; bIndex++)
4277 {
4278
4279 stream.Put_real32 (kScale * samples [0]);
4280 stream.Put_real32 (kScale * samples [1]);
4281 stream.Put_real32 (kScale * samples [2]);
4282
4283 samples += 4;
4284
4285 } // b
4286
4287 } // g
4288
4289 } // r
4290
4291 }
4292
4293 else
4294 {
4295
4296 ThrowProgramError ("Unsupported PixelType in "
4297 "dng_masked_rgb_table::PutStream");
4298
4299 }
4300
4301 }
4302
4303 }
4304
4305 /*****************************************************************************/
4306
4307 void dng_masked_rgb_table::AddDigest (dng_md5_printer &printer) const
4308 {
4309
4310 // Header.
4311
4312 printer.Process ("dng_masked_rgb_table", 20);
4313
4314 // Name.
4315
4316 uint32 nameLen = SemanticName ().Length ();
4317
4318 printer.Process (&nameLen,
4319 (uint32) sizeof (nameLen));
4320
4321 if (nameLen > 0)
4322 {
4323
4324 printer.Process (SemanticName ().Get (), nameLen);
4325
4326 }
4327
4328 // Pixel type.
4329
4330 printer.Process (&fPixelType,
4331 (uint32) sizeof (fPixelType));
4332
4333 // Table digest.
4334
4335 dng_fingerprint tableDigest = fTable.Fingerprint ();
4336
4337 printer.Process (tableDigest.data,
4338 sizeof (tableDigest.data));
4339
4340 }
4341
4342 /*****************************************************************************/
4343 /*****************************************************************************/
4344 /*****************************************************************************/
4345
4346 void dng_masked_rgb_table_render_data::Initialize (const dng_negative &negative,
4347 const dng_camera_profile &profile)
4348 {
4349
4350 if (!profile.HasMaskedRGBTables ())
4351 return;
4352
4353 auto maskedTablesReference = profile.ShareMaskedRGBTables ();
4354
4355 const auto &tables = *maskedTablesReference;
4356
4357 if (tables.IsNOP ())
4358 return;
4359
4360 fUseSequentialMethod = tables.UseSequentialMethod ();
4361
4362 // Find correspondence between RGBTables and SemanticMasks tags. It is
4363 // still possible that we have a NOP situation if every table uses a mask
4364 // name that is not found in SemanticMasks.
4365
4366 // First, make a hashtable of semantic masks, with the name as a key.
4367
4368 std::unordered_map<dng_string,
4369 dng_semantic_mask,
4370 dng_string_hash> smMap;
4371
4372 {
4373
4374 const uint32 numMasks = negative.NumSemanticMasks ();
4375
4376 for (uint32 i = 0; i < numMasks; i++)
4377 {
4378
4379 const auto &mask = negative.SemanticMask (i);
4380
4381 smMap.insert (std::make_pair (mask.fName, mask));
4382
4383 }
4384
4385 }
4386
4387 // Next, walk through all tables in the RGBTables tag and figure out which
4388 // ones are relevant (have a matching SemanticMask label, or have no label
4389 // which means a background table).
4390
4391 int32 debugIndex = 0;
4392
4393 #if !qDebugMaskedRGBTableRender
4394 (void) debugIndex;
4395 #endif
4396
4397 for (const auto &table : tables.Tables ())
4398 {
4399
4400 DNG_REQUIRE (table, "bad table");
4401
4402 const auto &name = table->SemanticName ();
4403
4404 if (name.IsEmpty ())
4405 {
4406
4407 DNG_REQUIRE (fBackgroundTable == nullptr,
4408 "already have a background table");
4409
4410 fBackgroundTable = table;
4411
4412 if (fUseSequentialMethod)
4413 {
4414
4415 dng_semantic_mask emptyMask;
4416
4417 fMaskedTables.push_back
4418 (std::make_pair (table, emptyMask));
4419
4420 }
4421
4422 #if qDebugMaskedRGBTableRender
4423
4424 printf ("table %d will be treated as background table\n",
4425 debugIndex);
4426
4427 #endif
4428
4429 }
4430
4431 else
4432 {
4433
4434 // Check if we have a corresponding semantic mask.
4435
4436 auto iter = smMap.find (name);
4437
4438 if (iter != smMap.end ())
4439 {
4440
4441 // Found it. Add it to the list.
4442
4443 fMaskedTables.push_back
4444 (std::make_pair (table, iter->second));
4445
4446 #if qDebugMaskedRGBTableRender
4447
4448 printf ("table index %d -> semantic name '%s' found\n",
4449 debugIndex,
4450 name.Get ());
4451
4452 #endif
4453
4454 }
4455
4456 #if qDebugMaskedRGBTableRender
4457
4458 else
4459 {
4460
4461 printf ("table index %d -> semantic name '%s' not found among "
4462 "negative's list of semantic masks -- ignoring",
4463 debugIndex,
4464 name.Get ());
4465
4466 }
4467
4468 #endif
4469
4470 }
4471
4472 ++debugIndex;
4473
4474 }
4475
4476 // Find the background table index.
4477
4478 fBackgroundTableIndex = uint32 (fMaskedTables.size ());
4479
4480 if (fUseSequentialMethod)
4481 {
4482
4483 for (size_t i = 0; i < fMaskedTables.size (); i++)
4484 {
4485
4486 const auto &semanticMask = fMaskedTables [i].second;
4487
4488 const_dng_image_sptr baseMask = semanticMask.fMask;
4489
4490 // Empty base mask indicates a background table.
4491
4492 if (!baseMask)
4493 {
4494
4495 fBackgroundTableIndex = (uint32) i;
4496
4497 break;
4498
4499 }
4500
4501 }
4502
4503 DNG_REQUIRE ((!fBackgroundTable) ==
4504 (fBackgroundTableIndex == fMaskedTables.size ()),
4505 "inconsistent background table info for sequential");
4506
4507 }
4508
4509 }
4510
4511 /*****************************************************************************/
4512
4513 void dng_masked_rgb_table_render_data::PrepareRGBtoRGBTableData (dng_host &host)
4514 {
4515
4516 fMaskedTableData.clear ();
4517
4518 fMaskedTableData.reserve (fMaskedTables.size ());
4519
4520 for (const auto &x : fMaskedTables)
4521 {
4522
4523 dng_rgb_to_rgb_table_data_sptr ptr
4524 (host.Make_dng_rgb_to_rgb_table_data (x.first->Table ()));
4525
4526 fMaskedTableData.push_back (ptr);
4527
4528 }
4529
4530 if (fBackgroundTable)
4531 {
4532
4533 fBackgroundTableData.Reset
4534 (host.Make_dng_rgb_to_rgb_table_data (fBackgroundTable->Table ()));
4535
4536 }
4537
4538 }
4539
4540 /*****************************************************************************/
4541 /*****************************************************************************/
4542 /*****************************************************************************/
4543
4544 #if qDNGValidate
4545
4546 void dng_masked_rgb_table::Dump (uint32 indent) const
4547 {
4548
4549 const uint32 kIndentBufLen = 32;
4550
4551 indent = Min_uint32 (indent, kIndentBufLen - 1);
4552
4553 char indentBuf [kIndentBufLen];
4554
4555 snprintf (indentBuf,
4556 kIndentBufLen,
4557 "%*s",
4558 indent,
4559 "");
4560
4561 printf ("%s%-20s: %s\n",
4562 indentBuf,
4563 "SemanticName",
4564 fTableSemanticName.Get ());
4565
4566 printf ("%s%-20s: %u\n",
4567 indentBuf,
4568 "Divisions",
4569 fTable.Divisions ());
4570
4571 const char *pixelTypeStrings [] = { "u8", "u16", "fp32" };
4572
4573 printf ("%s%-20s: %u (%s)\n",
4574 indentBuf,
4575 "PixelType",
4576 fPixelType,
4577 pixelTypeStrings [Pin_uint32 (0, fPixelType, 2)]);
4578
4579 const char *gammaEncodingStrings [] =
4580 {
4581 "linear",
4582 "sRGB",
4583 "1.8",
4584 "2.2",
4585 "Rec. 2020"
4586 };
4587
4588 uint32 gammaTagValue = GammaEnumToRGBTablesTagValue (fTable.Gamma ());
4589
4590 printf ("%s%-20s: %u (%s)\n",
4591 indentBuf,
4592 "GammaEncoding",
4593 gammaTagValue,
4594 gammaEncodingStrings [gammaTagValue]);
4595
4596 const char *primariesStrings [] =
4597 {
4598 "sRGB",
4599 "Adobe RGB",
4600 "Display P3",
4601 "Rec. 2020",
4602 "ProPhoto"
4603 };
4604
4605 uint32 primariesTagValue =
4606 ColorPrimariesEnumToRGBTablesTagValue (fTable.Primaries ());
4607
4608 printf ("%s%-20s: %u (%s)\n",
4609 indentBuf,
4610 "ColorPrimaries",
4611 primariesTagValue,
4612 primariesStrings [primariesTagValue]);
4613
4614 const char *gamutStrings [] =
4615 {
4616 "clip",
4617 "extend",
4618 };
4619
4620 uint32 gamutTagValue =
4621 GamutExtensionEnumToRGBTablesTagValue (fTable.Gamut ());
4622
4623 printf ("%s%-20s: %u (%s)\n",
4624 indentBuf,
4625 "GamutExtension",
4626 gamutTagValue,
4627 gamutStrings [gamutTagValue]);
4628
4629 const uint32 divs = fTable.Divisions ();
4630
4631 const uint16 *samples = fTable.Samples ();
4632
4633 bool doPrint = true;
4634
4635 uint32 linesPrinted = 0;
4636
4637 const uint8 *storedPtr = nullptr;
4638
4639 if (fStoredData.get ())
4640 {
4641 storedPtr = fStoredData->Buffer_uint8 ();
4642 }
4643
4644 for (uint32 rIndex = 0; rIndex < divs && doPrint; rIndex++)
4645 {
4646
4647 for (uint32 gIndex = 0; gIndex < divs && doPrint; gIndex++)
4648 {
4649
4650 for (uint32 bIndex = 0; bIndex < divs; bIndex++)
4651 {
4652
4653 if (linesPrinted >= gDumpLineLimit)
4654 {
4655 doPrint = false;
4656 break;
4657 }
4658
4659 if (!storedPtr || (fPixelType == 1))
4660 {
4661
4662 printf (" table [%3u] [%3u] [%3u] = %6u %6u %6u\n",
4663 rIndex,
4664 gIndex,
4665 bIndex,
4666 (unsigned) samples [0],
4667 (unsigned) samples [1],
4668 (unsigned) samples [2]);
4669
4670 }
4671
4672 else if (fPixelType == 0)
4673 {
4674
4675 printf (" table [%3u] [%3u] [%3u] = %6u %6u %6u (stored as %4u %4u %4u)\n",
4676 rIndex,
4677 gIndex,
4678 bIndex,
4679 (unsigned) samples [0],
4680 (unsigned) samples [1],
4681 (unsigned) samples [2],
4682 (unsigned) storedPtr [0],
4683 (unsigned) storedPtr [1],
4684 (unsigned) storedPtr [2]);
4685
4686 storedPtr += 3;
4687
4688 }
4689
4690 else // fPixelType == 2
4691 {
4692
4693 const real32 *sPtr32 = (const real32 *) storedPtr;
4694
4695 printf (" table [%3u] [%3u] [%3u] = %6u %6u %6u (stored as %6.4f %6.4f %6.4f)\n",
4696 rIndex,
4697 gIndex,
4698 bIndex,
4699 (unsigned) samples [0],
4700 (unsigned) samples [1],
4701 (unsigned) samples [2],
4702 sPtr32 [0],
4703 sPtr32 [1],
4704 sPtr32 [2]);
4705
4706 storedPtr += (3 * 4);
4707
4708 }
4709
4710 samples += 4;
4711
4712 ++linesPrinted;
4713
4714 } // b
4715
4716 } // g
4717
4718 } // r
4719
4720 if (linesPrinted < divs * divs * divs)
4721 {
4722
4723 printf ("%s... %u entries omitted ...\n",
4724 indentBuf,
4725 (divs * divs * divs) - linesPrinted);
4726
4727 }
4728
4729 }
4730
4731 #endif // qDNGValidate
4732
4733 /*****************************************************************************/
4734 /*****************************************************************************/
4735 /*****************************************************************************/
4736
4737 dng_masked_rgb_tables::dng_masked_rgb_tables ()
4738 {
4739
4740 }
4741
4742 /*****************************************************************************/
4743
4744 dng_masked_rgb_tables::dng_masked_rgb_tables
4745 (const std::vector<std::shared_ptr<dng_masked_rgb_table> > &tables,
4746 composite_method compositeMethod)
4747
4748 : fCompositeMethod (compositeMethod)
4749
4750 , fTables (tables)
4751
4752 {
4753
4754 Validate ();
4755
4756 }
4757
4758 /*****************************************************************************/
4759
4760 bool dng_masked_rgb_tables::IsNOP () const
4761 {
4762
4763 if (fTables.empty ())
4764 {
4765 return true;
4766 }
4767
4768 return false;
4769
4770 }
4771
4772 /*****************************************************************************/
4773
4774 void dng_masked_rgb_tables::Validate () const
4775 {
4776
4777 // Check number of tables.
4778
4779 if (fTables.size () > kMaxTables)
4780 {
4781 ThrowBadFormat ("Too many tables in RGBTables");
4782 }
4783
4784 // Check each table and keep track of # of tables with empty semantic
4785 // names.
4786
4787 uint32 numEmptyNames = 0;
4788
4789 for (const auto &t : fTables)
4790 {
4791
4792 DNG_REQUIRE (t, "Invalid table pointer in RGBTables");
4793
4794 t->Validate ();
4795
4796 if (t->SemanticName ().IsEmpty ())
4797 {
4798 ++numEmptyNames;
4799 }
4800
4801 }
4802
4803 // There can be at most 1 table with an empty semantic name (i.e., at most
4804 // one background table).
4805
4806 if (numEmptyNames > 1)
4807 {
4808
4809 ThrowBadFormat ("Only one table in RGBTables can "
4810 "have empty semantic name");
4811
4812 }
4813
4814 }
4815
4816 /*****************************************************************************/
4817
4818 void dng_masked_rgb_tables::AddDigest (dng_md5_printer &printer) const
4819 {
4820
4821 // Header.
4822
4823 printer.Process ("dng_masked_rgb_tables", 21);
4824
4825 // Number of tables.
4826
4827 uint32 numTables = (uint32) fTables.size ();
4828
4829 printer.Process (&numTables,
4830 (uint32) sizeof (numTables));
4831
4832 // Process each table.
4833
4834 for (const auto &t : fTables)
4835 {
4836 t->AddDigest (printer);
4837 }
4838
4839 // Composite method.
4840
4841 printer.Process (&fCompositeMethod,
4842 (uint32) sizeof (fCompositeMethod));
4843
4844 }
4845
4846 /*****************************************************************************/
4847
4848 void dng_masked_rgb_tables::PutStream (dng_stream &stream) const
4849 {
4850
4851 // Write the number of tables.
4852
4853 stream.Put_uint32 ((uint32) fTables.size ());
4854
4855 // Write the composite method.
4856
4857 stream.Put_uint32 ((uint32) fCompositeMethod);
4858
4859 // Then write each table.
4860
4861 for (const auto &t : fTables)
4862 {
4863 t->PutStream (stream);
4864 }
4865
4866 }
4867
4868 /*****************************************************************************/
4869
4870 dng_masked_rgb_tables * dng_masked_rgb_tables::GetStream (dng_host &host,
4871 dng_stream &stream,
4872 const bool isDraft)
4873 {
4874
4875 // Read number of tables.
4876
4877 uint32 numTables = stream.Get_uint32 ();
4878
4879 // A zero value is valid, which simply means the tag is empty and is a
4880 // NOP. Return nullptr in this case.
4881
4882 if (numTables == 0)
4883 {
4884 return nullptr;
4885 }
4886
4887 if (numTables > kMaxTables)
4888 {
4889 ThrowBadFormat ("RGBTables: numTables too large");
4890 }
4891
4892 // In earlier DNG 1.6 drafts, there was no CompositeMethod field. The only
4893 // supported method was Weighted Sum.
4894
4895 composite_method method = kWeightedSum;
4896
4897 // In the final DNG 1.6 revision, there is a CompositeMethod field.
4898
4899 if (!isDraft)
4900 {
4901
4902 method = (composite_method) stream.Get_uint32 ();
4903
4904 if (method != kWeightedSum &&
4905 method != kSequential)
4906 {
4907 ThrowBadFormat ("RGBTables: invalid composite method");
4908 }
4909
4910 }
4911
4912 // Read each table.
4913
4914 std::vector<std::shared_ptr<dng_masked_rgb_table> > tables;
4915
4916 tables.resize (numTables);
4917
4918 for (auto &t : tables)
4919 {
4920
4921 t.reset (new dng_masked_rgb_table);
4922
4923 t->GetStream (host, stream);
4924
4925 }
4926
4927 // Done reading all the tables.
4928
4929 return new dng_masked_rgb_tables (tables,
4930 method);
4931
4932 }
4933
4934 /*****************************************************************************/
4935
4936 #if qDNGValidate
4937
4938 void dng_masked_rgb_tables::Dump () const
4939 {
4940
4941 printf ("Number of tables: %u\n", (unsigned) fTables.size ());
4942
4943 printf ("CompositeMethod: %s\n",
4944 UseWeightedSumMethod () ? "Weighted Sum"
4945 : "Sequential");
4946
4947 for (size_t i = 0; i < fTables.size (); i++)
4948 {
4949
4950 printf ("Table %u:\n", (unsigned) i);
4951
4952 fTables [i]->Dump (4);
4953
4954 }
4955
4956 }
4957
4958 #endif // qDNGValidate
4959
4960 /*****************************************************************************/
4961 /*****************************************************************************/
4962 /*****************************************************************************/
4963
4964 class dng_rgb_to_rgb_1d_function : public dng_1d_function
4965 {
4966
4967 private:
4968
4969 const dng_rgb_table &fTable;
4970
4971 uint32 fPlane;
4972
4973 public:
4974
4975 dng_rgb_to_rgb_1d_function (const dng_rgb_table &table,
4976 uint32 plane)
4977
4978 : fTable (table)
4979 , fPlane (plane)
4980
4981 {
4982
4983 DNG_ASSERT (fTable.Dimensions () == 1, "1D table expected");
4984
4985 }
4986
4987 virtual real64 Evaluate (real64 x) const
4988 {
4989
4990 uint32 divisions = fTable.Divisions ();
4991
4992 real64 scaled = x * (real64) (divisions - 1);
4993
4994 int32 index = Pin_int32 (0,
4995 (int32) scaled,
4996 divisions - 2);
4997
4998 real64 fract = scaled - (real64) index;
4999
5000 const uint16 *table = fTable.Samples () + (index * 4) + fPlane;
5001
5002 real64 y = ((1.0 - fract) * (real64) table [0] +
5003 ( fract) * (real64) table [4]) * (1.0 / 65535.0);
5004
5005 return x + fTable.Amount () * (y - x);
5006
5007 }
5008
5009 };
5010
5011
5012 /*****************************************************************************/
5013 /*****************************************************************************/
5014 /*****************************************************************************/
5015
5016 dng_rgb_to_rgb_table_data::dng_rgb_to_rgb_table_data (dng_host &host,
5017 const dng_rgb_table &table)
5018
5019 : fTable (table)
5020
5021 , fNeedMatrix (false)
5022
5023 , fEncodeMatrix ()
5024 , fDecodeMatrix ()
5025
5026 , fEncodeTable ()
5027 , fDecodeTable ()
5028
5029 {
5030
5031 // Find encode/decode matrix, if any.
5032
5033 {
5034
5035 const dng_color_space *space = NULL;
5036
5037 switch (table.Primaries ())
5038 {
5039
5040 case dng_rgb_table::primaries_sRGB:
5041 {
5042 space = &dng_space_sRGB::Get ();
5043 break;
5044 }
5045
5046 case dng_rgb_table::primaries_Adobe:
5047 {
5048 space = &dng_space_AdobeRGB::Get ();
5049 break;
5050 }
5051
5052 case dng_rgb_table::primaries_ProPhoto:
5053 {
5054 break;
5055 }
5056
5057 case dng_rgb_table::primaries_P3:
5058 {
5059 space = &dng_space_DisplayP3::Get ();
5060 break;
5061 }
5062
5063 case dng_rgb_table::primaries_Rec2020:
5064 {
5065 space = &dng_space_Rec2020::Get ();
5066 break;
5067 }
5068
5069 default:
5070 {
5071 DNG_REPORT ("Unknown RGB table primaries");
5072 }
5073
5074 }
5075
5076 fNeedMatrix = (space != NULL);
5077
5078 if (fNeedMatrix)
5079 {
5080
5081 fEncodeMatrix = space->MatrixFromPCS () *
5082 dng_space_ProPhoto::Get ().MatrixToPCS ();
5083
5084 fDecodeMatrix = dng_space_ProPhoto::Get ().MatrixFromPCS () *
5085 space->MatrixToPCS ();
5086
5087 }
5088
5089 }
5090
5091 // Find encode/decode gamma tables, if any.
5092
5093 {
5094
5095 const dng_1d_function *gamma = NULL;
5096
5097 switch (table.Gamma ())
5098 {
5099
5100 case dng_rgb_table::gamma_Linear:
5101 {
5102 break;
5103 }
5104
5105 case dng_rgb_table::gamma_sRGB:
5106 {
5107 gamma = &dng_function_GammaEncode_sRGB::Get ();
5108 break;
5109 }
5110
5111 case dng_rgb_table::gamma_1_8:
5112 {
5113 gamma = &dng_function_GammaEncode_1_8::Get ();
5114 break;
5115 }
5116
5117 case dng_rgb_table::gamma_2_2:
5118 {
5119 gamma = &dng_function_GammaEncode_2_2::Get ();
5120 break;
5121 }
5122
5123 case dng_rgb_table::gamma_Rec2020:
5124 {
5125 gamma = &dng_function_GammaEncode_Rec709::Get ();
5126 break;
5127 }
5128
5129 default:
5130 {
5131 DNG_REPORT ("Unknown RGB table gamma");
5132 }
5133
5134 }
5135
5136 if (fTable.Dimensions () == 1)
5137 {
5138
5139 for (uint32 plane = 0; plane < 3; plane++)
5140 {
5141
5142 fTable1D [plane].Reset (new dng_1d_table);
5143
5144 dng_rgb_to_rgb_1d_function mapPlane (fTable,
5145 plane);
5146
5147 if (gamma == NULL)
5148 {
5149
5150 fTable1D [plane]->Initialize (host.Allocator (),
5151 mapPlane,
5152 false);
5153
5154 }
5155
5156 else
5157 {
5158
5159 dng_1d_inverse inverse (*gamma);
5160
5161 dng_1d_concatenate firstPart (*gamma,
5162 mapPlane);
5163
5164 dng_1d_concatenate combined (firstPart,
5165 inverse);
5166
5167 fTable1D [plane]->Initialize (host.Allocator (),
5168 combined,
5169 false);
5170
5171 }
5172
5173 }
5174
5175 }
5176
5177 else if (gamma != NULL)
5178 {
5179
5180 fEncodeTable.Reset (new dng_1d_table);
5181 fDecodeTable.Reset (new dng_1d_table);
5182
5183 fEncodeTable->Initialize (host.Allocator (),
5184 *gamma,
5185 false);
5186
5187 dng_1d_inverse inverse (*gamma);
5188
5189 fDecodeTable->Initialize (host.Allocator (),
5190 inverse,
5191 false);
5192
5193 }
5194
5195 }
5196
5197 }
5198
5199 /*****************************************************************************/
5200
5201 dng_rgb_to_rgb_table_data::~dng_rgb_to_rgb_table_data ()
5202 {
5203
5204 }
5205
5206 /*****************************************************************************/
5207
5208 void dng_rgb_to_rgb_table_data::Process_32 (dng_pixel_buffer &buffer,
5209 dng_pixel_buffer *optMaskBuffer,
5210 uint32 optMaskPlane,
5211 const dng_rect &dstArea,
5212 uint32 bufferStartPlane,
5213 const bool needOverrange)
5214 {
5215
5216 uint32 p0 = bufferStartPlane;
5217 uint32 p1 = bufferStartPlane + 1;
5218 uint32 p2 = bufferStartPlane + 2;
5219
5220 const real32 *mPtr = nullptr;
5221
5222 int32 mRowStep = 0;
5223
5224 if (optMaskBuffer)
5225 {
5226
5227 mPtr = optMaskBuffer->ConstPixel_real32 (dstArea.t,
5228 dstArea.l,
5229 optMaskPlane);
5230
5231 mRowStep = optMaskBuffer->RowStep ();
5232
5233 }
5234
5235 if (fTable.Dimensions () == 3)
5236 {
5237
5238 DoRGBtoRGBTable3D (buffer.DirtyPixel_real32 (dstArea.t, dstArea.l, p0),
5239 buffer.DirtyPixel_real32 (dstArea.t, dstArea.l, p1),
5240 buffer.DirtyPixel_real32 (dstArea.t, dstArea.l, p2),
5241 mPtr,
5242 dstArea.H (),
5243 dstArea.W (),
5244 buffer.RowStep (),
5245 mRowStep,
5246 fTable.Divisions (),
5247 fTable.Samples (),
5248 (real32) fTable.Amount (),
5249 (uint32) fTable.Gamut (),
5250 fNeedMatrix ? &fEncodeMatrix : NULL,
5251 fNeedMatrix ? &fDecodeMatrix : NULL,
5252 fEncodeTable.Get (),
5253 fDecodeTable.Get (),
5254 needOverrange);
5255
5256 }
5257
5258 else
5259 {
5260
5261 DoRGBtoRGBTable1D (buffer.DirtyPixel_real32 (dstArea.t, dstArea.l, p0),
5262 buffer.DirtyPixel_real32 (dstArea.t, dstArea.l, p1),
5263 buffer.DirtyPixel_real32 (dstArea.t, dstArea.l, p2),
5264 mPtr,
5265 dstArea.H (),
5266 dstArea.W (),
5267 buffer.RowStep (),
5268 mRowStep,
5269 *fTable1D [0],
5270 *fTable1D [1],
5271 *fTable1D [2],
5272 (uint32) fTable.Gamut (),
5273 fNeedMatrix ? &fEncodeMatrix : NULL,
5274 fNeedMatrix ? &fDecodeMatrix : NULL,
5275 needOverrange);
5276
5277 }
5278
5279 }
5280
5281 /*****************************************************************************/
5282
5283 void dng_rgb_to_rgb_table_data::AddDigest (dng_md5_printer &printer) const
5284 {
5285
5286 {
5287
5288 const dng_fingerprint tableFingerPrint = fTable.Fingerprint ();
5289
5290 printer.Process (tableFingerPrint.data,
5291 dng_fingerprint::kDNGFingerprintSize);
5292
5293 }
5294
5295 if (fNeedMatrix)
5296 {
5297
5298 for (uint32 i = 0; i < 3; i++)
5299 {
5300
5301 printer.Process (fEncodeMatrix [i], 3 * sizeof (fEncodeMatrix [i] [0]));
5302 printer.Process (fDecodeMatrix [i], 3 * sizeof (fEncodeMatrix [i] [0]));
5303
5304 }
5305
5306 }
5307
5308 if (fEncodeTable.Get () && fDecodeTable.Get ())
5309 {
5310
5311 printer.Process (fEncodeTable->Table (),
5312 (2 + fEncodeTable->Count ()) * sizeof (fEncodeTable->Table () [0]));
5313
5314 printer.Process (fDecodeTable->Table (),
5315 (2 + fEncodeTable->Count ()) * sizeof (fEncodeTable->Table () [0]));
5316
5317 }
5318
5319 if (fTable.Dimensions () != 3)
5320 {
5321
5322 for (uint32 i = 0; i < 3; i++)
5323 {
5324
5325 printer.Process (fTable1D [i]->Table (),
5326 (2 + fTable1D [i]->Count ()) * sizeof (fTable1D [i]->Table () [0]));
5327
5328 }
5329
5330 }
5331
5332 }
5333
5334 /*****************************************************************************/
5335 /*****************************************************************************/
5336 /*****************************************************************************/
5337
5338 #if qDNGUseXMP
5339
5340 /*****************************************************************************/
5341
5342 class MoveBigTablesToDictionaryContext
5343 {
5344
5345 public:
5346
5347 dng_xmp &fXMP;
5348
5349 dng_big_table_dictionary &fDictionary;
5350
5351 public:
5352
5353 MoveBigTablesToDictionaryContext (dng_xmp &xmp,
5354 dng_big_table_dictionary &dictionary)
5355
5356 : fXMP (xmp)
5357 , fDictionary (dictionary)
5358
5359 {
5360
5361 }
5362
5363 };
5364
5365 /*****************************************************************************/
5366
5367 static bool MoveBigTablesToDictionaryCallback (const char *ns,
5368 const char *path,
5369 void *callbackData)
5370 {
5371
5372 if (path == NULL || path [0] == 0)
5373 {
5374 return true;
5375 }
5376
5377 dng_string s;
5378
5379 s.Set (path);
5380
5381 // Check if ends with Table_ and 32 characters.
5382
5383 if (s.Length () < 38 ||
5384 strncmp (s.Get () + s.Length () - 38,
5385 "Table_",
5386 6) != 0)
5387 {
5388 return true;
5389 }
5390
5391 // Make sure there is either nothing or a namespace before.
5392
5393 if (s.Length () >= 39 &&
5394 s.Get () [s.Length () - 39] != ':')
5395 {
5396 return true;
5397 }
5398
5399 // See if the last 32 characters are a valid fingerprint.
5400
5401 char fingerString [32];
5402
5403 memcpy (fingerString,
5404 s.Get () + s.Length () - 32,
5405 32);
5406
5407 dng_fingerprint fingerprint;
5408
5409 if (!fingerprint.FromUtf8HexString (fingerString))
5410 {
5411 return true;
5412 }
5413
5414 if (!fingerprint.IsValid ())
5415 {
5416 return true;
5417 }
5418
5419 MoveBigTablesToDictionaryContext *context =
5420 (MoveBigTablesToDictionaryContext *) callbackData;
5421
5422 if (!context->fDictionary.HasTable (fingerprint))
5423 {
5424
5425 dng_string tableString;
5426
5427 if (context->fXMP.GetString (ns, path, tableString))
5428 {
5429
5430 AutoPtr<dng_memory_block> dBlock;
5431
5432 uint32 dCount = 0;
5433
5434 dng_big_table::ASCIItoBinary (context->fXMP.Allocator (),
5435 tableString.Get (),
5436 tableString.Length (),
5437 dBlock,
5438 dCount);
5439
5440 if (dCount)
5441 {
5442
5443 dng_ref_counted_block bigTableBlock (dCount);
5444
5445 memcpy (bigTableBlock.Buffer (),
5446 dBlock->Buffer (),
5447 dCount);
5448
5449 context->fDictionary.AddTable (fingerprint,
5450 bigTableBlock);
5451
5452 }
5453
5454 }
5455
5456 }
5457
5458 // Now remove the big table string from the xmp.
5459
5460 context->fXMP.Remove (ns, path);
5461
5462 return true;
5463
5464 }
5465
5466 /*****************************************************************************/
5467
5468 void MoveBigTablesToDictionary (dng_xmp &xmp,
5469 dng_big_table_dictionary &dictionary)
5470 {
5471
5472 MoveBigTablesToDictionaryContext context (xmp, dictionary);
5473
5474 xmp.IteratePaths (MoveBigTablesToDictionaryCallback,
5475 (void *) &context,
5476 XMP_NS_CRS,
5477 nullptr,
5478 true);
5479
5480 xmp.IteratePaths (MoveBigTablesToDictionaryCallback,
5481 (void *) &context,
5482 XMP_NS_CRSS,
5483 nullptr,
5484 true);
5485
5486 }
5487
5488 /*****************************************************************************/
5489
5490 void DualParseXMP (dng_host &host,
5491 dng_xmp &xmp,
5492 dng_big_table_dictionary &dictionary,
5493 const void *blockData,
5494 uint32 blockSize)
5495 {
5496
5497 // Null pad XMP text so we can use string searching functions.
5498
5499 AutoPtr<dng_memory_block> tempBlock (host.Allocate (blockSize + 1));
5500
5501 memcpy (tempBlock->Buffer (),
5502 blockData,
5503 blockSize);
5504
5505 tempBlock->Buffer_uint8 () [blockSize] = 0;
5506
5507 char *blockString = tempBlock->Buffer_char ();
5508
5509 // Assume default namespace short names. Since this is just
5510 // a performance optimization, don't worry about the rare case
5511 // of non-default namespace short names.
5512
5513 dng_string ns_crs ("crs:");
5514 dng_string ns_crss ("crss:");
5515
5516 // Search for big tables in XMP using fast search.
5517
5518 struct TableEntry
5519 {
5520
5521 // Fingerprint of found big table.
5522
5523 dng_fingerprint fFingerprint;
5524
5525 // Offset and byte count of big table data.
5526
5527 size_t fDataOffset;
5528 size_t fDataBytes;
5529
5530 // Offset and byte count of entire big table tag, including wrapper.
5531
5532 size_t fWrapperOffset;
5533 size_t fWrapperBytes;
5534
5535 };
5536
5537 std::vector<TableEntry> entries;
5538
5539 {
5540
5541 size_t offset = 0;
5542
5543 while (const char *search = strstr (blockString + offset, "Table_"))
5544 {
5545
5546 TableEntry entry;
5547
5548 // Is the search result followed by a valid hex encoded fingerprint?
5549
5550 if (search + 6 + 32 > blockString + blockSize)
5551 {
5552 break; // Too near end of xmp block to even fit fingerprint
5553 }
5554
5555 char fingerString [33];
5556
5557 memcpy (fingerString,
5558 search + 6,
5559 32);
5560
5561 fingerString [32] = 0;
5562
5563 if (!entry.fFingerprint.FromUtf8HexString (fingerString))
5564 {
5565
5566 // We did not find a table, so just start searching again just
5567 // after the "Table_" string.
5568
5569 offset = (search - blockString) + 6;
5570
5571 continue; // Not followed by valid fingerprint
5572
5573 }
5574
5575 // Look for big tables in both the XMP_NS_CRS and XMP_NS_CRSS
5576 // namespaces.
5577
5578 bool foundTable = false;
5579
5580 for (uint32 nsIndex = 0; nsIndex < 2; nsIndex++)
5581 {
5582
5583 dng_string ns = (nsIndex == 0 ? ns_crs : ns_crss);
5584
5585 int32 nsLength = ns.Length ();
5586
5587 // Look for compact mode tables.
5588 // Example:
5589 // crs:Table_fingerprint="data"
5590
5591 if (search >= blockString + nsLength + 1 &&
5592 memcmp (search - nsLength, ns.Get (), nsLength) == 0 &&
5593 (search [-nsLength - 1] == ' ' ||
5594 search [-nsLength - 1] == '\t' ||
5595 search [-nsLength - 1] == '\n' ||
5596 search [-nsLength - 1] == '\r') &&
5597 search + 6 + 32 + 2 < blockString + blockSize &&
5598 memcmp (search + 6 + 32, "=\"", 2) == 0)
5599 {
5600
5601 const char *dataStart = search + 6 + 32 + 2;
5602
5603 // Find termination
5604
5605 const char *endSearch = strstr (dataStart, "\"");
5606
5607 if (endSearch)
5608 {
5609
5610 entry.fDataOffset = dataStart - blockString;
5611 entry.fDataBytes = endSearch - dataStart;
5612
5613 entry.fWrapperOffset = entry.fDataOffset - 2 - 32 - 6 - nsLength;
5614 entry.fWrapperBytes = entry.fDataBytes + nsLength + 6 + 32 + 2 + 1;
5615
5616 foundTable = true;
5617
5618 break;
5619
5620 }
5621
5622 }
5623
5624 // Look for non-compact mode tables.
5625 // Example:
5626 // <crs:Table_fingerprint>data</crs:Table_fingerprint>
5627
5628 if (search >= blockString + nsLength + 1 &&
5629 memcmp (search - nsLength, ns.Get (), nsLength) == 0 &&
5630 search [-nsLength - 1] == '<' &&
5631 search + 6 + 32 + 1 < blockString + blockSize &&
5632 search [6 + 32] == '>')
5633 {
5634
5635 const char *dataStart = search + 6 + 32 + 1;
5636
5637 dng_string endString ("</");
5638
5639 endString.Append (ns.Get ());
5640 endString.Append ("Table_");
5641 endString.Append (fingerString);
5642 endString.Append (">");
5643
5644 // Find termination
5645
5646 const char *endSearch = strstr (dataStart, endString.Get ());
5647
5648 if (endSearch)
5649 {
5650
5651 entry.fDataOffset = dataStart - blockString;
5652 entry.fDataBytes = endSearch - dataStart;
5653
5654 entry.fWrapperOffset = entry.fDataOffset - 2 - 32 - 6 - nsLength;
5655 entry.fWrapperBytes = entry.fDataBytes + (nsLength + 6 + 32 + 2) * 2 + 1;
5656
5657 foundTable = true;
5658
5659 break;
5660
5661 }
5662
5663 }
5664
5665 }
5666
5667 // We found a table, start search again after this table's wrapper.
5668
5669 if (foundTable)
5670 {
5671
5672 entries.push_back (entry);
5673
5674 offset = entry.fWrapperOffset +
5675 entry.fWrapperBytes;
5676
5677 }
5678
5679 // If we did not find a table, just start searching again just
5680 // after the "Table_" string.
5681
5682 else
5683 {
5684
5685 offset = (search - blockString) + 6;
5686
5687 }
5688
5689 }
5690
5691 }
5692
5693 // Did we find any tables in our scan?
5694
5695 if (entries.size ())
5696 {
5697
5698 // Add any new tables to dictionary, and remove from xmp that will be passed
5699 // to parser.
5700
5701 size_t srcOffset = 0;
5702 size_t dstOffset = 0;
5703
5704 for (size_t index = 0; index < entries.size (); index++)
5705 {
5706
5707 if (!dictionary.HasTable (entries [index].fFingerprint))
5708 {
5709
5710 AutoPtr<dng_memory_block> dBlock;
5711
5712 uint32 dCount = 0;
5713
5714 dng_big_table::ASCIItoBinary (host.Allocator (),
5715 ((const char *) blockData) + entries [index].fDataOffset,
5716 (uint32) entries [index].fDataBytes,
5717 dBlock,
5718 dCount);
5719
5720 if (dCount)
5721 {
5722
5723 dng_ref_counted_block bigTableBlock (dCount);
5724
5725 memcpy (bigTableBlock.Buffer (),
5726 dBlock->Buffer (),
5727 dCount);
5728
5729 dictionary.AddTable (entries [index].fFingerprint,
5730 bigTableBlock);
5731
5732 }
5733
5734 }
5735
5736 size_t copyBytes = entries [index].fWrapperOffset - srcOffset;
5737
5738 memcpy (blockString + dstOffset,
5739 ((const char *) blockData) + srcOffset,
5740 copyBytes);
5741
5742 dstOffset += copyBytes;
5743
5744 srcOffset = entries [index].fWrapperOffset +
5745 entries [index].fWrapperBytes;
5746
5747 }
5748
5749 // Copy bytes after last block.
5750
5751 memcpy (blockString + dstOffset,
5752 ((const char *) blockData) + srcOffset,
5753 blockSize - srcOffset);
5754
5755 dstOffset += blockSize - srcOffset;
5756
5757 // Parse the remaining XMP with the big table blocks removed.
5758
5759 xmp.Parse (host,
5760 blockString,
5761 (uint32) dstOffset);
5762
5763 }
5764
5765 // No tables founds, just parse the original XMP data.
5766
5767 else
5768 {
5769
5770 xmp.Parse (host,
5771 blockData,
5772 blockSize);
5773
5774 }
5775
5776 // Slow path, in case fast parser missed something.
5777
5778 MoveBigTablesToDictionary (xmp, dictionary);
5779
5780 }
5781
5782 /*****************************************************************************/
5783
5784 #endif // qDNGUseXMP
5785
5786 /*****************************************************************************/