Debian Codegraph @

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

raw
1 /*****************************************************************************/
2 // Copyright 2006-2019 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_ifd.h"
10
11 #include "dng_big_table.h"
12 #include "dng_exceptions.h"
13 #include "dng_flags.h"
14 #include "dng_gain_map.h"
15 #include "dng_globals.h"
16 #include "dng_host.h"
17 #include "dng_ifd.h"
18 #include "dng_types.h"
19 #include "dng_parse_utils.h"
20 #include "dng_read_image.h"
21 #include "dng_stream.h"
22 #include "dng_tag_codes.h"
23 #include "dng_tag_types.h"
24 #include "dng_tag_values.h"
25 #include "dng_utils.h"
26
27 /*****************************************************************************/
28
29 dng_preview_info::dng_preview_info ()
30
31 : fIsPrimary (true)
32 , fApplicationName ()
33 , fApplicationVersion ()
34 , fSettingsName ()
35 , fSettingsDigest ()
36 , fColorSpace (previewColorSpace_MaxEnum)
37 , fDateTime ()
38 , fRawToPreviewGain (1.0)
39 , fCacheVersion (0)
40
41 {
42
43 }
44
45 /*****************************************************************************/
46
47 dng_preview_info::~dng_preview_info ()
48 {
49
50 }
51
52 /*****************************************************************************/
53
54 dng_ifd::dng_ifd ()
55
56 : fUsesNewSubFileType (false)
57 , fNewSubFileType (0)
58
59 , fImageWidth (0)
60 , fImageLength (0)
61
62 , fCompression (ccUncompressed)
63 , fPredictor (cpNullPredictor)
64
65 , fPhotometricInterpretation (0xFFFFFFFF)
66
67 , fFillOrder (1)
68
69 , fOrientation (0)
70 , fOrientationType (0)
71 , fOrientationOffset (kDNGStreamInvalidOffset)
72 , fOrientationBigEndian (false)
73
74 , fSamplesPerPixel (1)
75
76 , fPlanarConfiguration (pcInterleaved)
77
78 , fXResolution (0.0)
79 , fYResolution (0.0)
80 , fResolutionUnit (0)
81
82 , fUsesStrips (false)
83 , fUsesTiles (false)
84
85 , fTileWidth (0)
86 , fTileLength (0)
87
88 , fTileOffsetsType (0)
89 , fTileOffsetsCount (0)
90 , fTileOffsetsOffset (0)
91
92 , fTileByteCountsType (0)
93 , fTileByteCountsCount (0)
94 , fTileByteCountsOffset (0)
95
96 , fSubIFDsType (0)
97 , fSubIFDsCount (0)
98 , fSubIFDsOffset (0)
99
100 , fExtraSamplesCount (0)
101
102 , fJPEGTablesCount (0)
103 , fJPEGTablesOffset (0)
104
105 , fJPEGInterchangeFormat (0)
106 , fJPEGInterchangeFormatLength (0)
107
108 , fYCbCrCoefficientR (0.0)
109 , fYCbCrCoefficientG (0.0)
110 , fYCbCrCoefficientB (0.0)
111
112 , fYCbCrSubSampleH (0)
113 , fYCbCrSubSampleV (0)
114
115 , fYCbCrPositioning (0)
116
117 , fCFARepeatPatternRows (0)
118 , fCFARepeatPatternCols (0)
119
120 , fCFALayout (1)
121
122 , fLinearizationTableType (0)
123 , fLinearizationTableCount (0)
124 , fLinearizationTableOffset (0)
125
126 , fBlackLevelRepeatRows (1)
127 , fBlackLevelRepeatCols (1)
128
129 , fBlackLevelDeltaHType (0)
130 , fBlackLevelDeltaHCount (0)
131 , fBlackLevelDeltaHOffset (0)
132
133 , fBlackLevelDeltaVType (0)
134 , fBlackLevelDeltaVCount (0)
135 , fBlackLevelDeltaVOffset (0)
136
137 , fDefaultScaleH (1, 1)
138 , fDefaultScaleV (1, 1)
139
140 , fBestQualityScale (1, 1)
141
142 , fDefaultCropOriginH (0, 1)
143 , fDefaultCropOriginV (0, 1)
144
145 , fDefaultCropSizeH ()
146 , fDefaultCropSizeV ()
147
148 , fDefaultUserCropT (0, 1)
149 , fDefaultUserCropL (0, 1)
150 , fDefaultUserCropB (1, 1)
151 , fDefaultUserCropR (1, 1)
152
153 , fBayerGreenSplit (0)
154
155 , fChromaBlurRadius ()
156
157 , fAntiAliasStrength (1, 1)
158
159 , fActiveArea ()
160
161 , fMaskedAreaCount (0)
162
163 , fRowInterleaveFactor (1)
164 , fColumnInterleaveFactor (1)
165
166 , fSubTileBlockRows (1)
167 , fSubTileBlockCols (1)
168
169 , fPreviewInfo ()
170
171 , fOpcodeList1Count (0)
172 , fOpcodeList1Offset (0)
173
174 , fOpcodeList2Count (0)
175 , fOpcodeList2Offset (0)
176
177 , fOpcodeList3Count (0)
178 , fOpcodeList3Offset (0)
179
180 , fNoiseProfile ()
181
182 , fEnhanceParams ()
183
184 , fBaselineSharpness (0, 0)
185
186 , fNoiseReductionApplied (0, 0)
187
188 , fLosslessJPEGBug16 (false)
189
190 , fSampleBitShift (0)
191
192 , fThisIFD (0)
193 , fNextIFD (0)
194
195 , fCompressionQuality (-1)
196
197 , fPatchFirstJPEGByte (false)
198
199 {
200
201 uint32 j;
202 uint32 k;
203 uint32 n;
204
205 for (j = 0; j < kMaxSamplesPerPixel; j++)
206 {
207 fBitsPerSample [j] = 0;
208 }
209
210 for (j = 0; j < kMaxTileInfo; j++)
211 {
212 fTileOffset [j] = 0;
213 fTileByteCount [j] = 0;
214 }
215
216 for (j = 0; j < kMaxSamplesPerPixel; j++)
217 {
218 fExtraSamples [j] = esUnspecified;
219 }
220
221 for (j = 0; j < kMaxSamplesPerPixel; j++)
222 {
223 fSampleFormat [j] = sfUnsignedInteger;
224 }
225
226 for (j = 0; j < 6; j++)
227 {
228 fReferenceBlackWhite [j] = 0.0;
229 }
230
231 for (j = 0; j < kMaxCFAPattern; j++)
232 for (k = 0; k < kMaxCFAPattern; k++)
233 {
234 fCFAPattern [j] [k] = 255;
235 }
236
237 for (j = 0; j < kMaxColorPlanes; j++)
238 {
239 fCFAPlaneColor [j] = (uint8) (j < 3 ? j : 255);
240 }
241
242 for (j = 0; j < kMaxBlackPattern; j++)
243 for (k = 0; k < kMaxBlackPattern; k++)
244 for (n = 0; n < kMaxColorPlanes; n++)
245 {
246 fBlackLevel [j] [k] [n] = 0.0;
247 }
248
249 for (j = 0; j < kMaxColorPlanes; j++)
250 {
251 fWhiteLevel [j] = -1.0; // Don't know real default yet.
252 }
253
254 memset (fMaskSubArea, 0, sizeof (fMaskSubArea));
255
256 }
257
258 /*****************************************************************************/
259
260 dng_ifd::~dng_ifd ()
261 {
262
263 }
264
265 /*****************************************************************************/
266
267 dng_ifd * dng_ifd::Clone () const
268 {
269
270 return new dng_ifd (*this);
271
272 }
273
274 /*****************************************************************************/
275
276 // Parses tags that should only appear in IFDs that contain images.
277
278 bool dng_ifd::ParseTag (dng_host &host,
279 dng_stream &stream,
280 uint32 parentCode,
281 uint32 tagCode,
282 uint32 tagType,
283 uint32 tagCount,
284 uint64 tagOffset)
285 {
286
287 uint32 j;
288 uint32 k;
289 uint32 n;
290
291 switch (tagCode)
292 {
293
294 case tcNewSubFileType:
295 {
296
297 CheckTagType (parentCode, tagCode, tagType, ttLong);
298
299 CheckTagCount (parentCode, tagCode, tagCount, 1);
300
301 fUsesNewSubFileType = true;
302
303 fNewSubFileType = stream.TagValue_uint32 (tagType);
304
305 fPreviewInfo.fIsPrimary = (fNewSubFileType == sfPreviewImage);
306
307 if (fNewSubFileType == sfEnhancedImage)
308 {
309
310 // Enhanced IFDs different defaults for some tags.
311
312 fDefaultScaleH.Clear ();
313 fDefaultScaleV.Clear ();
314
315 fBestQualityScale.Clear ();
316
317 fDefaultCropOriginH.Clear ();
318 fDefaultCropOriginV.Clear ();
319
320 }
321
322 #if qDNGValidate
323
324 if (gVerbose)
325 {
326
327 printf ("NewSubFileType: %s\n",
328 LookupNewSubFileType (fNewSubFileType));
329
330 }
331
332 #endif
333
334 break;
335
336 }
337
338 case tcImageWidth:
339 {
340
341 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong);
342
343 CheckTagCount (parentCode, tagCode, tagCount, 1);
344
345 fImageWidth = stream.TagValue_uint32 (tagType);
346
347 #if qDNGValidate
348
349 if (gVerbose)
350 {
351 printf ("ImageWidth: %u\n", (unsigned) fImageWidth);
352 }
353
354 #endif
355
356 break;
357
358 }
359
360 case tcImageLength:
361 {
362
363 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong);
364
365 CheckTagCount (parentCode, tagCode, tagCount, 1);
366
367 fImageLength = stream.TagValue_uint32 (tagType);
368
369 #if qDNGValidate
370
371 if (gVerbose)
372 {
373 printf ("ImageLength: %u\n", (unsigned) fImageLength);
374 }
375
376 #endif
377
378 break;
379
380 }
381
382 case tcBitsPerSample:
383 {
384
385 CheckTagType (parentCode, tagCode, tagType, ttShort);
386
387 if (!CheckTagCount (parentCode, tagCode, tagCount, 1, 0x0FFFF))
388 return false;
389
390 #if qDNGValidate
391
392 if (gVerbose)
393 {
394 printf ("BitsPerSample:");
395 }
396
397 #endif
398
399 bool extrasMatch = true;
400
401 for (j = 0; j < tagCount; j++)
402 {
403
404 uint32 x = stream.TagValue_uint32 (tagType);
405
406 const uint32 maxBitsPerSample = 32;
407
408 if (j < kMaxSamplesPerPixel)
409 {
410
411 if (x > maxBitsPerSample)
412 {
413 //ThrowBadFormat ("BitsPerSample out of bounds");
414 DNG_REPORT ("BitsPerSample > 32");
415 x = maxBitsPerSample;
416 }
417
418 fBitsPerSample [j] = x;
419
420 }
421
422 else if (x != fBitsPerSample [kMaxSamplesPerPixel - 1])
423 {
424 extrasMatch = false;
425 }
426
427 #if qDNGValidate
428
429 if (gVerbose)
430 {
431 printf (" %u", (unsigned) x);
432 }
433
434 #endif
435
436 }
437
438 #if qDNGValidate
439
440 if (gVerbose)
441 {
442 printf ("\n");
443 }
444
445 #endif
446
447 if (!extrasMatch)
448 {
449
450 #if qDNGValidate
451
452 ReportError ("BitsPerSample not constant");
453
454 #endif
455
456 ThrowBadFormat ();
457
458 }
459
460 break;
461
462 }
463
464 case tcCompression:
465 {
466
467 CheckTagType (parentCode, tagCode, tagType, ttShort);
468
469 CheckTagCount (parentCode, tagCode, tagCount, 1);
470
471 fCompression = stream.TagValue_uint32 (tagType);
472
473 #if qDNGValidate
474
475 if (gVerbose)
476 {
477
478 printf ("Compression: %s\n",
479 LookupCompression (fCompression));
480
481 }
482
483 #endif
484
485 // Correct a common TIFF writer mistake.
486
487 if (fCompression == 0)
488 {
489
490 #if qDNGValidate
491
492 {
493
494 char message [256];
495
496 snprintf (message,
497 256,
498 "%s has invalid zero compression code",
499 LookupParentCode (parentCode));
500
501 ReportWarning (message);
502
503 }
504
505 #endif
506
507 fCompression = ccUncompressed;
508
509 }
510
511 break;
512
513 }
514
515 case tcPhotometricInterpretation:
516 {
517
518 CheckTagType (parentCode, tagCode, tagType, ttShort);
519
520 CheckTagCount (parentCode, tagCode, tagCount, 1);
521
522 fPhotometricInterpretation = stream.TagValue_uint32 (tagType);
523
524 #if qDNGValidate
525
526 if (gVerbose)
527 {
528
529 printf ("PhotometricInterpretation: %s\n",
530 LookupPhotometricInterpretation (fPhotometricInterpretation));
531
532 }
533
534 #endif
535
536 break;
537
538 }
539
540 case tcFillOrder:
541 {
542
543 CheckTagType (parentCode, tagCode, tagType, ttShort);
544
545 CheckTagCount (parentCode, tagCode, tagCount, 1);
546
547 fFillOrder = stream.TagValue_uint32 (tagType);
548
549 #if qDNGValidate
550
551 if (gVerbose)
552 {
553 printf ("FillOrder: %u\n", (unsigned) fFillOrder);
554 }
555
556 #endif
557
558 break;
559
560 }
561
562 case tcStripOffsets:
563 {
564
565 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong, ttLong8);
566
567 fUsesStrips = true;
568
569 fTileOffsetsType = tagType;
570 fTileOffsetsCount = tagCount;
571 fTileOffsetsOffset = tagOffset;
572
573 if (tagCount <= kMaxTileInfo)
574 {
575
576 for (j = 0; j < tagCount; j++)
577 {
578
579 fTileOffset [j] = stream.TagValue_uint64 (tagType);
580
581 }
582
583 }
584
585 #if qDNGValidate
586
587 if (gVerbose)
588 {
589
590 stream.SetReadPosition (tagOffset);
591
592 DumpTagValues (stream,
593 "Offset",
594 parentCode,
595 tagCode,
596 tagType,
597 tagCount);
598
599 }
600
601 #endif
602
603 break;
604
605 }
606
607 case tcOrientation:
608 {
609
610 CheckTagType (parentCode, tagCode, tagType, ttShort);
611
612 CheckTagCount (parentCode, tagCode, tagCount, 1);
613
614 fOrientationType = tagType;
615 fOrientationOffset = stream.PositionInOriginalFile ();
616 fOrientationBigEndian = stream.BigEndian ();
617
618 fOrientation = stream.TagValue_uint32 (tagType);
619
620 #if qDNGValidate
621
622 if (gVerbose)
623 {
624
625 printf ("Orientation: %s\n",
626 LookupOrientation (fOrientation));
627
628 }
629
630 #endif
631
632 break;
633
634 }
635
636 case tcSamplesPerPixel:
637 {
638
639 CheckTagType (parentCode, tagCode, tagType, ttShort);
640
641 CheckTagCount (parentCode, tagCode, tagCount, 1);
642
643 fSamplesPerPixel = stream.TagValue_uint32 (tagType);
644
645 #if qDNGValidate
646
647 if (gVerbose)
648 {
649 printf ("SamplesPerPixel: %u\n", (unsigned) fSamplesPerPixel);
650 }
651
652 #endif
653
654 break;
655
656 }
657
658 case tcRowsPerStrip:
659 {
660
661 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong);
662
663 CheckTagCount (parentCode, tagCode, tagCount, 1);
664
665 fUsesStrips = true;
666
667 fTileLength = stream.TagValue_uint32 (tagType);
668
669 #if qDNGValidate
670
671 if (gVerbose)
672 {
673 printf ("RowsPerStrip: %u\n", (unsigned) fTileLength);
674 }
675
676 #endif
677
678 break;
679
680 }
681
682 case tcStripByteCounts:
683 {
684
685 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong, ttLong8);
686
687 fUsesStrips = true;
688
689 fTileByteCountsType = tagType;
690 fTileByteCountsCount = tagCount;
691 fTileByteCountsOffset = tagOffset;
692
693 if (tagCount <= kMaxTileInfo)
694 {
695
696 for (j = 0; j < tagCount; j++)
697 {
698
699 fTileByteCount [j] = stream.TagValue_uint64 (tagType);
700
701 }
702
703 }
704
705 #if qDNGValidate
706
707 if (gVerbose)
708 {
709
710 stream.SetReadPosition (tagOffset);
711
712 DumpTagValues (stream,
713 "Count",
714 parentCode,
715 tagCode,
716 tagType,
717 tagCount);
718
719 stream.SetReadPosition (tagOffset);
720
721 uint64 totalCount = 0ULL;
722
723 for (j = 0; j < tagCount; j++)
724 totalCount += stream.TagValue_uint64 (tagType);
725
726 printf ("TotalByteCount %u: %llu\n",
727 (unsigned) fNewSubFileType,
728 (unsigned long long) totalCount);
729
730 }
731
732 #endif
733
734 break;
735
736 }
737
738 case tcXResolution:
739 {
740
741 CheckTagType (parentCode, tagCode, tagType, ttRational);
742
743 CheckTagCount (parentCode, tagCode, tagCount, 1);
744
745 fXResolution = stream.TagValue_real64 (tagType);
746
747 #if qDNGValidate
748
749 if (gVerbose)
750 {
751 printf ("XResolution: %0.2f\n", fXResolution);
752 }
753
754 #endif
755
756 break;
757
758 }
759
760 case tcYResolution:
761 {
762
763 CheckTagType (parentCode, tagCode, tagType, ttRational);
764
765 CheckTagCount (parentCode, tagCode, tagCount, 1);
766
767 fYResolution = stream.TagValue_real64 (tagType);
768
769 #if qDNGValidate
770
771 if (gVerbose)
772 {
773 printf ("YResolution: %0.2f\n", fYResolution);
774 }
775
776 #endif
777
778 break;
779
780 }
781
782 case tcPlanarConfiguration:
783 {
784
785 CheckTagType (parentCode, tagCode, tagType, ttShort);
786
787 CheckTagCount (parentCode, tagCode, tagCount, 1);
788
789 fPlanarConfiguration = stream.TagValue_uint32 (tagType);
790
791 #if qDNGValidate
792
793 if (gVerbose)
794 {
795 printf ("PlanarConfiguration: %u\n", (unsigned) fPlanarConfiguration);
796 }
797
798 #endif
799
800 break;
801
802 }
803
804 case tcResolutionUnit:
805 {
806
807 CheckTagType (parentCode, tagCode, tagType, ttShort);
808
809 CheckTagCount (parentCode, tagCode, tagCount, 1);
810
811 fResolutionUnit = stream.TagValue_uint32 (tagType);
812
813 #if qDNGValidate
814
815 if (gVerbose)
816 {
817
818 printf ("ResolutionUnit: %s\n",
819 LookupResolutionUnit (fResolutionUnit));
820
821 }
822
823 #endif
824
825 break;
826
827 }
828
829 case tcPredictor:
830 {
831
832 CheckTagType (parentCode, tagCode, tagType, ttShort);
833
834 CheckTagCount (parentCode, tagCode, tagCount, 1);
835
836 fPredictor = stream.TagValue_uint32 (tagType);
837
838 #if qDNGValidate
839
840 if (gVerbose)
841 {
842
843 printf ("Predictor: %s\n",
844 LookupPredictor (fPredictor));
845
846 }
847
848 #endif
849
850 break;
851
852 }
853
854 case tcTileWidth:
855 {
856
857 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong);
858
859 CheckTagCount (parentCode, tagCode, tagCount, 1);
860
861 fUsesTiles = true;
862
863 fTileWidth = stream.TagValue_uint32 (tagType);
864
865 #if qDNGValidate
866
867 if (gVerbose)
868 {
869 printf ("TileWidth: %u\n", (unsigned) fTileWidth);
870 }
871
872 #endif
873
874 break;
875
876 }
877
878 case tcTileLength:
879 {
880
881 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong);
882
883 CheckTagCount (parentCode, tagCode, tagCount, 1);
884
885 fUsesTiles = true;
886
887 fTileLength = stream.TagValue_uint32 (tagType);
888
889 #if qDNGValidate
890
891 if (gVerbose)
892 {
893 printf ("TileLength: %u\n", (unsigned) fTileLength);
894 }
895
896 #endif
897
898 break;
899
900 }
901
902 case tcTileOffsets:
903 {
904
905 CheckTagType (parentCode, tagCode, tagType, ttLong, ttLong8);
906
907 fUsesTiles = true;
908
909 fTileOffsetsType = tagType;
910 fTileOffsetsCount = tagCount;
911 fTileOffsetsOffset = tagOffset;
912
913 if (tagCount <= kMaxTileInfo)
914 {
915
916 for (j = 0; j < tagCount; j++)
917 {
918
919 fTileOffset [j] = stream.TagValue_uint64 (tagType);
920
921 }
922
923 }
924
925 #if qDNGValidate
926
927 if (gVerbose)
928 {
929
930 stream.SetReadPosition (tagOffset);
931
932 DumpTagValues (stream,
933 "Offset",
934 parentCode,
935 tagCode,
936 tagType,
937 tagCount);
938
939 }
940
941 #endif
942
943 break;
944
945 }
946
947 case tcTileByteCounts:
948 {
949
950 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong, ttLong8);
951
952 fUsesTiles = true;
953
954 fTileByteCountsType = tagType;
955 fTileByteCountsCount = tagCount;
956 fTileByteCountsOffset = tagOffset;
957
958 if (tagCount <= kMaxTileInfo)
959 {
960
961 for (j = 0; j < tagCount; j++)
962 {
963
964 fTileByteCount [j] = stream.TagValue_uint64 (tagType);
965
966 }
967
968 }
969
970 #if qDNGValidate
971
972 if (gVerbose)
973 {
974
975 stream.SetReadPosition (tagOffset);
976
977 DumpTagValues (stream,
978 "Count",
979 parentCode,
980 tagCode,
981 tagType,
982 tagCount);
983
984 stream.SetReadPosition (tagOffset);
985
986 uint64 totalCount = 0ULL;
987
988 for (j = 0; j < tagCount; j++)
989 totalCount += stream.TagValue_uint64 (tagType);
990
991 printf ("TotalByteCount %u: %llu\n",
992 (unsigned) fNewSubFileType,
993 (unsigned long long) totalCount);
994
995 }
996
997 #endif
998
999 break;
1000
1001 }
1002
1003 case tcSubIFDs:
1004 {
1005
1006 CheckTagType (parentCode, tagCode, tagType, ttLong, ttIFD, ttLong8, ttIFD8);
1007
1008 fSubIFDsType = tagType;
1009 fSubIFDsCount = tagCount;
1010 fSubIFDsOffset = tagOffset;
1011
1012 #if qDNGValidate
1013
1014 if (gVerbose)
1015 {
1016
1017 DumpTagValues (stream,
1018 "IFD",
1019 parentCode,
1020 tagCode,
1021 tagType,
1022 tagCount);
1023
1024 }
1025
1026 #endif
1027
1028 break;
1029
1030 }
1031
1032 case tcExtraSamples:
1033 {
1034
1035 CheckTagType (parentCode, tagCode, tagType, ttShort);
1036
1037 if (!CheckTagCount (parentCode, tagCode, tagCount, 1, fSamplesPerPixel))
1038 return false;
1039
1040 #if qDNGValidate
1041
1042 if (gVerbose)
1043 {
1044 printf ("ExtraSamples:");
1045 }
1046
1047 #endif
1048
1049 fExtraSamplesCount = tagCount;
1050
1051 for (j = 0; j < tagCount; j++)
1052 {
1053
1054 uint32 x = stream.TagValue_uint32 (tagType);
1055
1056 if (j < kMaxSamplesPerPixel)
1057 {
1058 fExtraSamples [j] = x;
1059 }
1060
1061 #if qDNGValidate
1062
1063 if (gVerbose)
1064 {
1065 printf (" %u", (unsigned) x);
1066 }
1067
1068 #endif
1069
1070 }
1071
1072 #if qDNGValidate
1073
1074 if (gVerbose)
1075 {
1076 printf ("\n");
1077 }
1078
1079 #endif
1080
1081 break;
1082
1083 }
1084
1085 case tcSampleFormat:
1086 {
1087
1088 CheckTagType (parentCode, tagCode, tagType, ttShort);
1089
1090 if (!CheckTagCount (parentCode, tagCode, tagCount, fSamplesPerPixel))
1091 return false;
1092
1093 #if qDNGValidate
1094
1095 if (gVerbose)
1096 {
1097 printf ("SampleFormat:");
1098 }
1099
1100 #endif
1101
1102 bool extrasMatch = true;
1103
1104 for (j = 0; j < tagCount; j++)
1105 {
1106
1107 uint32 x = stream.TagValue_uint32 (tagType);
1108
1109 if (j < kMaxSamplesPerPixel)
1110 {
1111 fSampleFormat [j] = x;
1112 }
1113
1114 else if (x != fSampleFormat [kMaxSamplesPerPixel - 1])
1115 {
1116 extrasMatch = false;
1117 }
1118
1119 #if qDNGValidate
1120
1121 if (gVerbose)
1122 {
1123 printf (" %s", LookupSampleFormat (x));
1124 }
1125
1126 #endif
1127
1128 }
1129
1130 #if qDNGValidate
1131
1132 if (gVerbose)
1133 {
1134 printf ("\n");
1135 }
1136
1137 #endif
1138
1139 if (!extrasMatch)
1140 {
1141
1142 #if qDNGValidate
1143
1144 ReportError ("SampleFormat not constant");
1145
1146 #endif
1147
1148 ThrowBadFormat ();
1149
1150 }
1151
1152 break;
1153
1154 }
1155
1156 case tcJPEGTables:
1157 {
1158
1159 CheckTagType (parentCode, tagCode, tagType, ttUndefined);
1160
1161 fJPEGTablesCount = tagCount;
1162 fJPEGTablesOffset = tagOffset;
1163
1164 #if qDNGValidate
1165
1166 if (gVerbose)
1167 {
1168
1169 printf ("JPEGTables: count = %u, offset = %u\n",
1170 (unsigned) fJPEGTablesCount,
1171 (unsigned) fJPEGTablesOffset);
1172
1173 }
1174
1175 #endif
1176
1177 break;
1178
1179 }
1180
1181 case tcJPEGInterchangeFormat:
1182 {
1183
1184 CheckTagType (parentCode, tagCode, tagType, ttLong);
1185
1186 CheckTagCount (parentCode, tagCode, tagCount, 1);
1187
1188 fJPEGInterchangeFormat = stream.TagValue_uint32 (tagType);
1189
1190 #if qDNGValidate
1191
1192 if (gVerbose)
1193 {
1194 printf ("JPEGInterchangeFormat: %u\n",
1195 (unsigned) fJPEGInterchangeFormat);
1196 }
1197
1198 #endif
1199
1200 break;
1201
1202 }
1203
1204 case tcJPEGInterchangeFormatLength:
1205 {
1206
1207 CheckTagType (parentCode, tagCode, tagType, ttLong);
1208
1209 CheckTagCount (parentCode, tagCode, tagCount, 1);
1210
1211 fJPEGInterchangeFormatLength = stream.TagValue_uint32 (tagType);
1212
1213 #if qDNGValidate
1214
1215 if (gVerbose)
1216 {
1217 printf ("JPEGInterchangeFormatLength: %u\n",
1218 (unsigned) fJPEGInterchangeFormatLength);
1219 }
1220
1221 #endif
1222
1223 break;
1224
1225 }
1226
1227 case tcYCbCrCoefficients:
1228 {
1229
1230 CheckTagType (parentCode, tagCode, tagType, ttRational);
1231
1232 if (!CheckTagCount (parentCode, tagCode, tagCount, 3))
1233 {
1234 return false;
1235 }
1236
1237 fYCbCrCoefficientR = stream.TagValue_real64 (tagType);
1238 fYCbCrCoefficientG = stream.TagValue_real64 (tagType);
1239 fYCbCrCoefficientB = stream.TagValue_real64 (tagType);
1240
1241 #if qDNGValidate
1242
1243 if (gVerbose)
1244 {
1245
1246 printf ("YCbCrCoefficients: R = %0.3f, G = %0.3f, B = %0.3f\n",
1247 fYCbCrCoefficientR,
1248 fYCbCrCoefficientG,
1249 fYCbCrCoefficientB);
1250
1251 }
1252
1253 #endif
1254
1255 break;
1256
1257 }
1258
1259 case tcYCbCrSubSampling:
1260 {
1261
1262 CheckTagType (parentCode, tagCode, tagType, ttShort);
1263
1264 if (!CheckTagCount (parentCode, tagCode, tagCount, 2))
1265 {
1266 return false;
1267 }
1268
1269 fYCbCrSubSampleH = stream.TagValue_uint32 (tagType);
1270 fYCbCrSubSampleV = stream.TagValue_uint32 (tagType);
1271
1272 #if qDNGValidate
1273
1274 if (gVerbose)
1275 {
1276
1277 printf ("YCbCrSubSampling: H = %u, V = %u\n",
1278 (unsigned) fYCbCrSubSampleH,
1279 (unsigned) fYCbCrSubSampleV);
1280
1281 }
1282
1283 #endif
1284
1285 break;
1286
1287 }
1288
1289 case tcYCbCrPositioning:
1290 {
1291
1292 CheckTagType (parentCode, tagCode, tagType, ttShort);
1293
1294 CheckTagCount (parentCode, tagCode, tagCount, 1);
1295
1296 fYCbCrPositioning = stream.TagValue_uint32 (tagType);
1297
1298 #if qDNGValidate
1299
1300 if (gVerbose)
1301 {
1302
1303 printf ("YCbCrPositioning: %u\n",
1304 (unsigned) fYCbCrPositioning);
1305
1306 }
1307
1308 #endif
1309
1310 break;
1311
1312 }
1313
1314 case tcReferenceBlackWhite:
1315 {
1316
1317 CheckTagType (parentCode, tagCode, tagType, ttRational);
1318
1319 if (!CheckTagCount (parentCode, tagCode, tagCount, 6))
1320 {
1321 return false;
1322 }
1323
1324 for (j = 0; j < 6; j++)
1325 {
1326 fReferenceBlackWhite [j] = stream.TagValue_real64 (tagType);
1327 }
1328
1329 #if qDNGValidate
1330
1331 if (gVerbose)
1332 {
1333
1334 printf ("ReferenceBlackWhite: %0.1f %0.1f %0.1f %0.1f %0.1f %0.1f\n",
1335 fReferenceBlackWhite [0],
1336 fReferenceBlackWhite [1],
1337 fReferenceBlackWhite [2],
1338 fReferenceBlackWhite [3],
1339 fReferenceBlackWhite [4],
1340 fReferenceBlackWhite [5]);
1341
1342 }
1343
1344 #endif
1345
1346 break;
1347
1348 }
1349
1350 case tcCFARepeatPatternDim:
1351 {
1352
1353 CheckCFA (parentCode, tagCode, fPhotometricInterpretation);
1354
1355 CheckTagType (parentCode, tagCode, tagType, ttShort);
1356
1357 if (!CheckTagCount (parentCode, tagCode, tagCount, 2))
1358 {
1359 return false;
1360 }
1361
1362 fCFARepeatPatternRows = stream.TagValue_uint32 (tagType);
1363 fCFARepeatPatternCols = stream.TagValue_uint32 (tagType);
1364
1365 #if qDNGValidate
1366
1367 if (gVerbose)
1368 {
1369
1370 printf ("CFARepeatPatternDim: Rows = %u, Cols = %u\n",
1371 (unsigned) fCFARepeatPatternRows,
1372 (unsigned) fCFARepeatPatternCols);
1373
1374 }
1375
1376 #endif
1377
1378 break;
1379
1380 }
1381
1382 case tcCFAPattern:
1383 {
1384
1385 CheckCFA (parentCode, tagCode, fPhotometricInterpretation);
1386
1387 if (!CheckTagType (parentCode, tagCode, tagType, ttByte))
1388 {
1389 return false;
1390 }
1391
1392 if (!CheckTagCount (parentCode,
1393 tagCode,
1394 tagCount,
1395 SafeUint32Mult (fCFARepeatPatternRows,
1396 fCFARepeatPatternCols)))
1397 {
1398 return false;
1399 }
1400
1401 if (fCFARepeatPatternRows < 1 || fCFARepeatPatternRows > kMaxCFAPattern ||
1402 fCFARepeatPatternCols < 1 || fCFARepeatPatternCols > kMaxCFAPattern)
1403 {
1404 return false;
1405 }
1406
1407 // Note that the Exif spec stores this array in a different
1408 // scan order than the TIFF-EP spec.
1409
1410 for (j = 0; j < fCFARepeatPatternRows; j++)
1411 for (k = 0; k < fCFARepeatPatternCols; k++)
1412 {
1413
1414 fCFAPattern [j] [k] = stream.Get_uint8 ();
1415
1416 }
1417
1418 #if qDNGValidate
1419
1420 if (gVerbose)
1421 {
1422
1423 printf ("CFAPattern:\n");
1424
1425 for (j = 0; j < fCFARepeatPatternRows; j++)
1426 {
1427
1428 int32 spaces = 4;
1429
1430 for (k = 0; k < fCFARepeatPatternCols; k++)
1431 {
1432
1433 while (spaces-- > 0)
1434 {
1435 printf (" ");
1436 }
1437
1438 const char *name = LookupCFAColor (fCFAPattern [j] [k]);
1439
1440 spaces = 9 - (int32) strlen (name);
1441
1442 printf ("%s", name);
1443
1444 }
1445
1446 printf ("\n");
1447
1448 }
1449
1450 }
1451
1452 #endif
1453
1454 break;
1455
1456 }
1457
1458 case tcCFAPlaneColor:
1459 {
1460
1461 CheckCFA (parentCode, tagCode, fPhotometricInterpretation);
1462
1463 if (!CheckTagType (parentCode, tagCode, tagType, ttByte))
1464 {
1465 return false;
1466 }
1467
1468 if (!CheckTagCount (parentCode, tagCode, tagCount, 3, kMaxColorPlanes))
1469 {
1470 return false;
1471 }
1472
1473 for (j = 0; j < kMaxColorPlanes; j++)
1474 {
1475
1476 if (j < tagCount)
1477 fCFAPlaneColor [j] = stream.Get_uint8 ();
1478
1479 else
1480 fCFAPlaneColor [j] = 255;
1481
1482 }
1483
1484 #if qDNGValidate
1485
1486 if (gVerbose)
1487 {
1488
1489 printf ("CFAPlaneColor:");
1490
1491 for (j = 0; j < tagCount; j++)
1492 {
1493
1494 printf (" %s", LookupCFAColor (fCFAPlaneColor [j]));
1495
1496 }
1497
1498 printf ("\n");
1499
1500 }
1501
1502 #endif
1503
1504 break;
1505
1506 }
1507
1508 case tcCFALayout:
1509 {
1510
1511 CheckCFA (parentCode, tagCode, fPhotometricInterpretation);
1512
1513 CheckTagType (parentCode, tagCode, tagType, ttShort);
1514
1515 CheckTagCount (parentCode, tagCode, tagCount, 1);
1516
1517 fCFALayout = stream.TagValue_uint32 (tagType);
1518
1519 #if qDNGValidate
1520
1521 if (gVerbose)
1522 {
1523
1524 printf ("CFALayout: %s\n",
1525 LookupCFALayout (fCFALayout));
1526
1527 }
1528
1529 #endif
1530
1531 break;
1532
1533 }
1534
1535 case tcLinearizationTable:
1536 {
1537
1538 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
1539
1540 CheckTagType (parentCode, tagCode, tagType, ttShort);
1541
1542 fLinearizationTableType = tagType;
1543 fLinearizationTableCount = tagCount;
1544 fLinearizationTableOffset = tagOffset;
1545
1546 #if qDNGValidate
1547
1548 if (gVerbose)
1549 {
1550
1551 DumpTagValues (stream,
1552 "Table",
1553 parentCode,
1554 tagCode,
1555 tagType,
1556 tagCount);
1557
1558 }
1559
1560 #endif
1561
1562 break;
1563
1564 }
1565
1566 case tcBlackLevelRepeatDim:
1567 {
1568
1569 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
1570
1571 CheckTagType (parentCode, tagCode, tagType, ttShort);
1572
1573 if (!CheckTagCount (parentCode, tagCode, tagCount, 2))
1574 {
1575 return false;
1576 }
1577
1578 fBlackLevelRepeatRows = stream.TagValue_uint32 (tagType);
1579 fBlackLevelRepeatCols = stream.TagValue_uint32 (tagType);
1580
1581 #if qDNGValidate
1582
1583 if (gVerbose)
1584 {
1585
1586 printf ("BlackLevelRepeatDim: Rows = %u, Cols = %u\n",
1587 (unsigned) fBlackLevelRepeatRows,
1588 (unsigned) fBlackLevelRepeatCols);
1589
1590 }
1591
1592 #endif
1593
1594 break;
1595
1596 }
1597
1598 case tcBlackLevel:
1599 {
1600
1601 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
1602
1603 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong, ttRational);
1604
1605 if (!CheckTagCount (parentCode,
1606 tagCode,
1607 tagCount,
1608 SafeUint32Mult (fBlackLevelRepeatRows,
1609 fBlackLevelRepeatCols,
1610 fSamplesPerPixel)))
1611 {
1612 return false;
1613 }
1614
1615 if (fBlackLevelRepeatRows < 1 || fBlackLevelRepeatRows > kMaxBlackPattern ||
1616 fBlackLevelRepeatCols < 1 || fBlackLevelRepeatCols > kMaxBlackPattern ||
1617 fSamplesPerPixel < 1 || fSamplesPerPixel > kMaxColorPlanes)
1618 {
1619 return false;
1620 }
1621
1622 for (j = 0; j < fBlackLevelRepeatRows; j++)
1623 for (k = 0; k < fBlackLevelRepeatCols; k++)
1624 for (n = 0; n < fSamplesPerPixel; n++)
1625 {
1626
1627 fBlackLevel [j] [k] [n] = stream.TagValue_real64 (tagType);
1628
1629 }
1630
1631 #if qDNGValidate
1632
1633 if (gVerbose)
1634 {
1635
1636 printf ("BlackLevel:");
1637
1638 if (fBlackLevelRepeatRows == 1 &&
1639 fBlackLevelRepeatCols == 1)
1640 {
1641
1642 for (n = 0; n < fSamplesPerPixel; n++)
1643 {
1644 printf (" %0.2f", fBlackLevel [0] [0] [n]);
1645 }
1646
1647 printf ("\n");
1648
1649 }
1650
1651 else
1652 {
1653
1654 printf ("\n");
1655
1656 for (n = 0; n < fSamplesPerPixel; n++)
1657 {
1658
1659 if (fSamplesPerPixel > 1)
1660 {
1661 printf ("\tSample: %u\n", (unsigned) n);
1662 }
1663
1664 for (j = 0; j < fBlackLevelRepeatRows; j++)
1665 {
1666
1667 printf ("\t");
1668
1669 for (k = 0; k < fBlackLevelRepeatCols; k++)
1670 {
1671
1672 printf (" %8.2f", fBlackLevel [j] [k] [n]);
1673
1674 }
1675
1676 printf ("\n");
1677
1678 }
1679
1680 }
1681
1682 }
1683
1684 }
1685
1686 #endif
1687
1688 break;
1689
1690 }
1691
1692 case tcBlackLevelDeltaH:
1693 {
1694
1695 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
1696
1697 CheckTagType (parentCode, tagCode, tagType, ttSRational);
1698
1699 fBlackLevelDeltaHType = tagType;
1700 fBlackLevelDeltaHCount = tagCount;
1701 fBlackLevelDeltaHOffset = tagOffset;
1702
1703 #if qDNGValidate
1704
1705 if (gVerbose)
1706 {
1707
1708 DumpTagValues (stream,
1709 "Delta",
1710 parentCode,
1711 tagCode,
1712 tagType,
1713 tagCount);
1714
1715 }
1716
1717 #endif
1718
1719 break;
1720
1721 }
1722
1723 case tcBlackLevelDeltaV:
1724 {
1725
1726 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
1727
1728 CheckTagType (parentCode, tagCode, tagType, ttSRational);
1729
1730 fBlackLevelDeltaVType = tagType;
1731 fBlackLevelDeltaVCount = tagCount;
1732 fBlackLevelDeltaVOffset = tagOffset;
1733
1734 #if qDNGValidate
1735
1736 if (gVerbose)
1737 {
1738
1739 DumpTagValues (stream,
1740 "Delta",
1741 parentCode,
1742 tagCode,
1743 tagType,
1744 tagCount);
1745
1746 }
1747
1748 #endif
1749
1750 break;
1751
1752 }
1753
1754 case tcWhiteLevel:
1755 {
1756
1757 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
1758
1759 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong);
1760
1761 if (!CheckTagCount (parentCode, tagCode, tagCount, fSamplesPerPixel))
1762 return false;
1763
1764 for (j = 0; j < tagCount && j < kMaxColorPlanes; j++)
1765 {
1766
1767 fWhiteLevel [j] = stream.TagValue_real64 (tagType);
1768
1769 }
1770
1771 #if qDNGValidate
1772
1773 if (gVerbose)
1774 {
1775
1776 printf ("WhiteLevel:");
1777
1778 for (j = 0; j < tagCount && j < kMaxColorPlanes; j++)
1779 {
1780
1781 printf (" %0.0f", fWhiteLevel [j]);
1782
1783 }
1784
1785 printf ("\n");
1786
1787 }
1788
1789 #endif
1790
1791 break;
1792
1793 }
1794
1795 case tcDefaultScale:
1796 {
1797
1798 if (fNewSubFileType != sfEnhancedImage)
1799 {
1800 CheckMainIFD (parentCode, tagCode, fNewSubFileType);
1801 }
1802
1803 CheckTagType (parentCode, tagCode, tagType, ttRational);
1804
1805 if (!CheckTagCount (parentCode, tagCode, tagCount, 2))
1806 return false;
1807
1808 fDefaultScaleH = stream.TagValue_urational (tagType);
1809 fDefaultScaleV = stream.TagValue_urational (tagType);
1810
1811 #if qDNGValidate
1812
1813 if (gVerbose)
1814 {
1815
1816 printf ("DefaultScale: H = %0.4f V = %0.4f\n",
1817 fDefaultScaleH.As_real64 (),
1818 fDefaultScaleV.As_real64 ());
1819
1820 }
1821
1822 #endif
1823
1824 break;
1825
1826 }
1827
1828 case tcDefaultCropOrigin:
1829 {
1830
1831 CheckMainOrEnhancedIFD (parentCode, tagCode, fNewSubFileType);
1832
1833 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong, ttRational);
1834
1835 if (!CheckTagCount (parentCode, tagCode, tagCount, 2))
1836 return false;
1837
1838 fDefaultCropOriginH = stream.TagValue_urational (tagType);
1839 fDefaultCropOriginV = stream.TagValue_urational (tagType);
1840
1841 #if qDNGValidate
1842
1843 if (gVerbose)
1844 {
1845
1846 printf ("DefaultCropOrigin: H = %0.2f V = %0.2f\n",
1847 fDefaultCropOriginH.As_real64 (),
1848 fDefaultCropOriginV.As_real64 ());
1849
1850 }
1851
1852 #endif
1853
1854 break;
1855
1856 }
1857
1858 case tcDefaultCropSize:
1859 {
1860
1861 CheckMainOrEnhancedIFD (parentCode, tagCode, fNewSubFileType);
1862
1863 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong, ttRational);
1864
1865 if (!CheckTagCount (parentCode, tagCode, tagCount, 2))
1866 return false;
1867
1868 fDefaultCropSizeH = stream.TagValue_urational (tagType);
1869 fDefaultCropSizeV = stream.TagValue_urational (tagType);
1870
1871 #if qDNGValidate
1872
1873 if (gVerbose)
1874 {
1875
1876 printf ("DefaultCropSize: H = %0.2f V = %0.2f\n",
1877 fDefaultCropSizeH.As_real64 (),
1878 fDefaultCropSizeV.As_real64 ());
1879
1880 }
1881
1882 #endif
1883
1884 break;
1885
1886 }
1887
1888 case tcDefaultUserCrop:
1889 {
1890
1891 CheckMainIFD (parentCode, tagCode, fNewSubFileType);
1892
1893 CheckTagType (parentCode, tagCode, tagType, ttRational);
1894
1895 if (!CheckTagCount (parentCode, tagCode, tagCount, 4))
1896 return false;
1897
1898 fDefaultUserCropT = stream.TagValue_urational (tagType);
1899 fDefaultUserCropL = stream.TagValue_urational (tagType);
1900 fDefaultUserCropB = stream.TagValue_urational (tagType);
1901 fDefaultUserCropR = stream.TagValue_urational (tagType);
1902
1903 #if qDNGValidate
1904
1905 if (gVerbose)
1906 {
1907
1908 printf ("DefaultUserCrop: T = %0.2lf L = %0.2lf B = %0.2lf R = %0.2lf\n",
1909 (double) fDefaultUserCropT.As_real64 (),
1910 (double) fDefaultUserCropL.As_real64 (),
1911 (double) fDefaultUserCropB.As_real64 (),
1912 (double) fDefaultUserCropR.As_real64 ());
1913
1914
1915 }
1916
1917 #endif // qDNGValidate
1918
1919 break;
1920
1921 }
1922
1923 case tcBayerGreenSplit:
1924 {
1925
1926 CheckCFA (parentCode, tagCode, fPhotometricInterpretation);
1927
1928 CheckTagType (parentCode, tagCode, tagType, ttLong);
1929
1930 CheckTagCount (parentCode, tagCode, tagCount, 1);
1931
1932 fBayerGreenSplit = stream.TagValue_uint32 (tagType);
1933
1934 #if qDNGValidate
1935
1936 if (gVerbose)
1937 {
1938 printf ("BayerGreenSplit: %u\n", (unsigned) fBayerGreenSplit);
1939 }
1940
1941 #endif
1942
1943 break;
1944
1945 }
1946
1947 case tcChromaBlurRadius:
1948 {
1949
1950 CheckMainIFD (parentCode, tagCode, fNewSubFileType);
1951
1952 CheckTagType (parentCode, tagCode, tagType, ttRational);
1953
1954 CheckTagCount (parentCode, tagCode, tagCount, 1);
1955
1956 fChromaBlurRadius = stream.TagValue_urational (tagType);
1957
1958 #if qDNGValidate
1959
1960 if (gVerbose)
1961 {
1962
1963 printf ("ChromaBlurRadius: %0.2f\n",
1964 fChromaBlurRadius.As_real64 ());
1965
1966 }
1967
1968 #endif
1969
1970 break;
1971
1972 }
1973
1974 case tcAntiAliasStrength:
1975 {
1976
1977 CheckMainIFD (parentCode, tagCode, fNewSubFileType);
1978
1979 CheckTagType (parentCode, tagCode, tagType, ttRational);
1980
1981 CheckTagCount (parentCode, tagCode, tagCount, 1);
1982
1983 fAntiAliasStrength = stream.TagValue_urational (tagType);
1984
1985 #if qDNGValidate
1986
1987 if (gVerbose)
1988 {
1989
1990 printf ("AntiAliasStrength: %0.2f\n",
1991 fAntiAliasStrength.As_real64 ());
1992
1993 }
1994
1995 #endif
1996
1997 break;
1998
1999 }
2000
2001 case tcBestQualityScale:
2002 {
2003
2004 if (fNewSubFileType != sfEnhancedImage)
2005 {
2006 CheckMainIFD (parentCode, tagCode, fNewSubFileType);
2007 }
2008
2009 CheckTagType (parentCode, tagCode, tagType, ttRational);
2010
2011 CheckTagCount (parentCode, tagCode, tagCount, 1);
2012
2013 fBestQualityScale = stream.TagValue_urational (tagType);
2014
2015 #if qDNGValidate
2016
2017 if (gVerbose)
2018 {
2019
2020 printf ("BestQualityScale: %0.4f\n",
2021 fBestQualityScale.As_real64 ());
2022
2023 }
2024
2025 #endif
2026
2027 break;
2028
2029 }
2030
2031 case tcActiveArea:
2032 {
2033
2034 CheckMainIFD (parentCode, tagCode, fNewSubFileType);
2035
2036 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong);
2037
2038 if (!CheckTagCount (parentCode, tagCode, tagCount, 4))
2039 return false;
2040
2041 fActiveArea.t = stream.TagValue_int32 (tagType);
2042 fActiveArea.l = stream.TagValue_int32 (tagType);
2043 fActiveArea.b = stream.TagValue_int32 (tagType);
2044 fActiveArea.r = stream.TagValue_int32 (tagType);
2045
2046 #if qDNGValidate
2047
2048 if (gVerbose)
2049 {
2050
2051 printf ("ActiveArea: T = %d L = %d B = %d R = %d\n",
2052 (int) fActiveArea.t,
2053 (int) fActiveArea.l,
2054 (int) fActiveArea.b,
2055 (int) fActiveArea.r);
2056
2057 }
2058
2059 #endif
2060
2061 break;
2062
2063 }
2064
2065 case tcMaskedAreas:
2066 {
2067
2068 CheckMainIFD (parentCode, tagCode, fNewSubFileType);
2069
2070 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong);
2071
2072 uint32 rect_count = tagCount / 4;
2073
2074 if (!CheckTagCount (parentCode, tagCode, tagCount, rect_count * 4))
2075 return false;
2076
2077 fMaskedAreaCount = rect_count;
2078
2079 if (fMaskedAreaCount > kMaxMaskedAreas)
2080 fMaskedAreaCount = kMaxMaskedAreas;
2081
2082 for (j = 0; j < fMaskedAreaCount; j++)
2083 {
2084
2085 fMaskedArea [j].t = stream.TagValue_int32 (tagType);
2086 fMaskedArea [j].l = stream.TagValue_int32 (tagType);
2087 fMaskedArea [j].b = stream.TagValue_int32 (tagType);
2088 fMaskedArea [j].r = stream.TagValue_int32 (tagType);
2089
2090 }
2091
2092 #if qDNGValidate
2093
2094 if (gVerbose)
2095 {
2096
2097 printf ("MaskedAreas: %u\n", (unsigned) fMaskedAreaCount);
2098
2099 for (j = 0; j < fMaskedAreaCount; j++)
2100 {
2101
2102 printf ("\tArea [%u]: T = %d L = %d B = %d R = %d\n",
2103 (unsigned) j,
2104 (int) fMaskedArea [j].t,
2105 (int) fMaskedArea [j].l,
2106 (int) fMaskedArea [j].b,
2107 (int) fMaskedArea [j].r);
2108
2109 }
2110
2111 }
2112
2113 #endif
2114
2115 break;
2116
2117 }
2118
2119 case tcPreviewApplicationName:
2120 {
2121
2122 CheckTagType (parentCode, tagCode, tagType, ttAscii, ttByte);
2123
2124 ParseStringTag (stream,
2125 parentCode,
2126 tagCode,
2127 tagCount,
2128 fPreviewInfo.fApplicationName,
2129 false);
2130
2131 #if qDNGValidate
2132
2133 if (gVerbose)
2134 {
2135
2136 printf ("PreviewApplicationName: ");
2137
2138 DumpString (fPreviewInfo.fApplicationName);
2139
2140 printf ("\n");
2141
2142 }
2143
2144 #endif
2145
2146 break;
2147
2148 }
2149
2150 case tcPreviewApplicationVersion:
2151 {
2152
2153 CheckTagType (parentCode, tagCode, tagType, ttAscii, ttByte);
2154
2155 ParseStringTag (stream,
2156 parentCode,
2157 tagCode,
2158 tagCount,
2159 fPreviewInfo.fApplicationVersion,
2160 false);
2161
2162 #if qDNGValidate
2163
2164 if (gVerbose)
2165 {
2166
2167 printf ("PreviewApplicationVersion: ");
2168
2169 DumpString (fPreviewInfo.fApplicationVersion);
2170
2171 printf ("\n");
2172
2173 }
2174
2175 #endif
2176
2177 break;
2178
2179 }
2180
2181 case tcPreviewSettingsName:
2182 {
2183
2184 CheckTagType (parentCode, tagCode, tagType, ttAscii, ttByte);
2185
2186 ParseStringTag (stream,
2187 parentCode,
2188 tagCode,
2189 tagCount,
2190 fPreviewInfo.fSettingsName,
2191 false);
2192
2193 #if qDNGValidate
2194
2195 if (gVerbose)
2196 {
2197
2198 printf ("PreviewSettingsName: ");
2199
2200 DumpString (fPreviewInfo.fSettingsName);
2201
2202 printf ("\n");
2203
2204 }
2205
2206 #endif
2207
2208 break;
2209
2210 }
2211
2212 case tcPreviewSettingsDigest:
2213 {
2214
2215 if (!CheckTagType (parentCode, tagCode, tagType, ttByte))
2216 return false;
2217
2218 if (!CheckTagCount (parentCode, tagCode, tagCount, 16))
2219 return false;
2220
2221 stream.Get (fPreviewInfo.fSettingsDigest.data, 16);
2222
2223 #if qDNGValidate
2224
2225 if (gVerbose)
2226 {
2227
2228 printf ("PreviewSettingsDigest: ");
2229
2230 DumpFingerprint (fPreviewInfo.fSettingsDigest);
2231
2232 printf ("\n");
2233
2234 }
2235
2236 #endif
2237
2238 break;
2239
2240 }
2241
2242 case tcPreviewColorSpace:
2243 {
2244
2245 CheckTagType (parentCode, tagCode, tagType, ttLong);
2246
2247 CheckTagCount (parentCode, tagCode, tagCount, 1);
2248
2249 fPreviewInfo.fColorSpace = (PreviewColorSpaceEnum)
2250 stream.TagValue_uint32 (tagType);
2251
2252 #if qDNGValidate
2253
2254 if (gVerbose)
2255 {
2256
2257 printf ("PreviewColorSpace: %s\n",
2258 LookupPreviewColorSpace ((uint32) fPreviewInfo.fColorSpace));
2259
2260 }
2261
2262 #endif
2263
2264 break;
2265
2266 }
2267
2268 case tcPreviewDateTime:
2269 {
2270
2271 CheckTagType (parentCode, tagCode, tagType, ttAscii);
2272
2273 ParseStringTag (stream,
2274 parentCode,
2275 tagCode,
2276 tagCount,
2277 fPreviewInfo.fDateTime,
2278 false);
2279
2280 #if qDNGValidate
2281
2282 if (gVerbose)
2283 {
2284
2285 printf ("PreviewDateTime: ");
2286
2287 DumpString (fPreviewInfo.fDateTime);
2288
2289 printf ("\n");
2290
2291 }
2292
2293 #endif
2294
2295 break;
2296
2297 }
2298
2299 case tcRowInterleaveFactor:
2300 {
2301
2302 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong);
2303
2304 if (!CheckTagCount (parentCode, tagCode, tagCount, 1))
2305 return false;
2306
2307 fRowInterleaveFactor = stream.TagValue_uint32 (tagType);
2308
2309 #if qDNGValidate
2310
2311 if (gVerbose)
2312 {
2313
2314 printf ("RowInterleaveFactor: %u\n",
2315 (unsigned) fRowInterleaveFactor);
2316
2317 }
2318
2319 #endif
2320
2321 break;
2322
2323 }
2324
2325 case tcColumnInterleaveFactor:
2326 {
2327
2328 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong);
2329
2330 if (!CheckTagCount (parentCode, tagCode, tagCount, 1))
2331 return false;
2332
2333 fColumnInterleaveFactor = stream.TagValue_uint32 (tagType);
2334
2335 #if qDNGValidate
2336
2337 if (gVerbose)
2338 {
2339
2340 printf ("ColumnInterleaveFactor: %u\n",
2341 (unsigned) fColumnInterleaveFactor);
2342
2343 }
2344
2345 #endif
2346
2347 break;
2348
2349 }
2350
2351 case tcSubTileBlockSize:
2352 {
2353
2354 CheckTagType (parentCode, tagCode, tagType, ttShort, ttLong);
2355
2356 if (!CheckTagCount (parentCode, tagCode, tagCount, 2))
2357 return false;
2358
2359 fSubTileBlockRows = stream.TagValue_uint32 (tagType);
2360 fSubTileBlockCols = stream.TagValue_uint32 (tagType);
2361
2362 #if qDNGValidate
2363
2364 if (gVerbose)
2365 {
2366
2367 printf ("SubTileBlockSize: rows = %u, cols = %u\n",
2368 (unsigned) fSubTileBlockRows,
2369 (unsigned) fSubTileBlockCols);
2370
2371 }
2372
2373 #endif
2374
2375 break;
2376
2377 }
2378
2379 case tcOpcodeList1:
2380 {
2381
2382 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
2383
2384 CheckTagType (parentCode, tagCode, tagType, ttUndefined);
2385
2386 fOpcodeList1Count = tagCount;
2387 fOpcodeList1Offset = tagOffset;
2388
2389 #if qDNGValidate
2390
2391 if (gVerbose)
2392 {
2393
2394 printf ("OpcodeList1: count = %u, offset = %u\n",
2395 (unsigned) fOpcodeList1Count,
2396 (unsigned) fOpcodeList1Offset);
2397
2398 }
2399
2400 #endif
2401
2402 break;
2403
2404 }
2405
2406 case tcOpcodeList2:
2407 {
2408
2409 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
2410
2411 CheckTagType (parentCode, tagCode, tagType, ttUndefined);
2412
2413 fOpcodeList2Count = tagCount;
2414 fOpcodeList2Offset = tagOffset;
2415
2416 #if qDNGValidate
2417
2418 if (gVerbose)
2419 {
2420
2421 printf ("OpcodeList2: count = %u, offset = %u\n",
2422 (unsigned) fOpcodeList2Count,
2423 (unsigned) fOpcodeList2Offset);
2424
2425 }
2426
2427 #endif
2428
2429 break;
2430
2431 }
2432
2433 case tcOpcodeList3:
2434 {
2435
2436 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
2437
2438 CheckTagType (parentCode, tagCode, tagType, ttUndefined);
2439
2440 fOpcodeList3Count = tagCount;
2441 fOpcodeList3Offset = tagOffset;
2442
2443 #if qDNGValidate
2444
2445 if (gVerbose)
2446 {
2447
2448 printf ("OpcodeList3: count = %u, offset = %u\n",
2449 (unsigned) fOpcodeList3Count,
2450 (unsigned) fOpcodeList3Offset);
2451
2452 }
2453
2454 #endif
2455
2456 break;
2457
2458 }
2459
2460 case tcRawToPreviewGain:
2461 {
2462
2463 #if qDNGValidate
2464
2465 if (fNewSubFileType != sfPreviewImage)
2466 {
2467
2468 char message [256];
2469
2470 snprintf (message,
2471 256,
2472 "%s %s is not allowed IFDs with NewSubFileType != PreviewImage",
2473 LookupParentCode (parentCode),
2474 LookupTagCode (parentCode, tagCode));
2475
2476 ReportWarning (message);
2477
2478 }
2479
2480 #endif
2481
2482 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
2483
2484 CheckTagType (parentCode, tagCode, tagType, ttDouble);
2485
2486 if (!CheckTagCount (parentCode, tagCode, tagCount, 1))
2487 return false;
2488
2489 fPreviewInfo.fRawToPreviewGain = stream.TagValue_real64 (tagType);
2490
2491 #if qDNGValidate
2492
2493 if (gVerbose)
2494 {
2495
2496 printf ("RawToPreviewGain = %f\n",
2497 fPreviewInfo.fRawToPreviewGain);
2498
2499 }
2500
2501 #endif
2502
2503 break;
2504
2505 }
2506
2507 case tcNoiseProfile:
2508 {
2509
2510 if (!CheckTagType (parentCode, tagCode, tagType, ttDouble))
2511 return false;
2512
2513 // This tag will be parsed even in non-raw IFDs (such as
2514 // thumbnails, previews, etc.) to support legacy DNGs that have
2515 // the tag in the wrong IFD, but we'll now issue a warning.
2516 // (Turn off the warning for IFD0 since we are writing it
2517 // there for backward compatibility).
2518
2519 if (parentCode != 0)
2520 {
2521
2522 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
2523
2524 }
2525
2526 // Must be an even, positive number of doubles in a noise profile.
2527
2528 if (!tagCount || (tagCount & 1))
2529 return false;
2530
2531 // Determine number of planes (i.e., half the number of doubles).
2532
2533 const uint32 numPlanes = Pin_uint32 (0,
2534 tagCount >> 1,
2535 kMaxColorPlanes);
2536
2537 // Parse the noise function parameters.
2538
2539 dng_std_vector<dng_noise_function> noiseFunctions;
2540
2541 for (uint32 i = 0; i < numPlanes; i++)
2542 {
2543
2544 const real64 scale = stream.TagValue_real64 (tagType);
2545 const real64 offset = stream.TagValue_real64 (tagType);
2546
2547 noiseFunctions.push_back (dng_noise_function (scale, offset));
2548
2549 }
2550
2551 // Store the noise profile.
2552
2553 fNoiseProfile = dng_noise_profile (noiseFunctions);
2554
2555 // Debug.
2556
2557 #if qDNGValidate
2558
2559 if (gVerbose)
2560 {
2561
2562 printf ("NoiseProfile:\n");
2563
2564 printf (" Planes: %u\n", (unsigned) numPlanes);
2565
2566 for (uint32 plane = 0; plane < numPlanes; plane++)
2567 {
2568
2569 printf (" Noise function for plane %u: scale = %.20lf, offset = %.20lf\n",
2570 (unsigned) plane,
2571 noiseFunctions [plane].Scale (),
2572 noiseFunctions [plane].Offset ());
2573
2574 }
2575
2576 }
2577
2578 #endif
2579
2580 break;
2581
2582 }
2583
2584 case tcCacheVersion:
2585 {
2586
2587 #if qDNGValidate
2588
2589 if (fNewSubFileType != sfPreviewImage)
2590 {
2591
2592 char message [256];
2593
2594 snprintf (message,
2595 256,
2596 "%s %s is not allowed IFDs with NewSubFileType != PreviewImage",
2597 LookupParentCode (parentCode),
2598 LookupTagCode (parentCode, tagCode));
2599
2600 ReportWarning (message);
2601
2602 }
2603
2604 #endif
2605
2606 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
2607
2608 CheckTagType (parentCode, tagCode, tagType, ttLong);
2609
2610 if (!CheckTagCount (parentCode, tagCode, tagCount, 1))
2611 return false;
2612
2613 fPreviewInfo.fCacheVersion = stream.TagValue_uint32 (tagType);
2614
2615 #if qDNGValidate
2616
2617 if (gVerbose)
2618 {
2619
2620 printf ("CacheVersion = 0x%x\n",
2621 (unsigned) fPreviewInfo.fCacheVersion);
2622
2623 }
2624
2625 #endif
2626
2627 break;
2628
2629 }
2630
2631 case tcEnhanceParams:
2632 {
2633
2634 #if qDNGValidate
2635
2636 if (fNewSubFileType != sfEnhancedImage)
2637 {
2638
2639 char message [256];
2640
2641 snprintf (message,
2642 256,
2643 "%s %s is not allowed IFDs with NewSubFileType != EnhancedImage",
2644 LookupParentCode (parentCode),
2645 LookupTagCode (parentCode, tagCode));
2646
2647 ReportWarning (message);
2648
2649 }
2650
2651 #endif
2652
2653 CheckTagType (parentCode, tagCode, tagType, ttAscii, ttByte);
2654
2655 ParseStringTag (stream,
2656 parentCode,
2657 tagCode,
2658 tagCount,
2659 fEnhanceParams,
2660 false);
2661
2662 #if qDNGValidate
2663
2664 if (gVerbose)
2665 {
2666
2667 printf ("EnhanceParams: ");
2668
2669 DumpString (fEnhanceParams);
2670
2671 printf ("\n");
2672
2673 }
2674
2675 #endif
2676
2677 break;
2678
2679 }
2680
2681 case tcBaselineSharpness:
2682 {
2683
2684 if (fNewSubFileType != sfEnhancedImage)
2685 {
2686 return false;
2687 }
2688
2689 CheckTagType (parentCode, tagCode, tagType, ttRational);
2690
2691 CheckTagCount (parentCode, tagCode, tagCount, 1);
2692
2693 fBaselineSharpness = stream.TagValue_urational (tagType);
2694
2695 #if qDNGValidate
2696
2697 if (gVerbose)
2698 {
2699
2700 printf ("BaselineSharpness (EnhancedImage): %0.2f\n",
2701 fBaselineSharpness.As_real64 ());
2702
2703 }
2704
2705 #endif
2706
2707 break;
2708
2709 }
2710
2711 case tcNoiseReductionApplied:
2712 {
2713
2714 if (!CheckTagType (parentCode, tagCode, tagType, ttRational))
2715 return false;
2716
2717 if (!CheckTagCount (parentCode, tagCode, tagCount, 1))
2718 return false;
2719
2720 // This tag will be parsed even in non-raw IFDs (such as
2721 // thumbnails, previews, etc.) to support legacy DNGs that have
2722 // the tag in the wrong IFD, but we'll now issue a warning.
2723 // (Turn off the warning for IFD0 since we are writing it
2724 // there for backward compatibility).
2725
2726 if (parentCode != 0)
2727 {
2728
2729 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
2730
2731 }
2732
2733 fNoiseReductionApplied = stream.TagValue_urational (tagType);
2734
2735 #if qDNGValidate
2736
2737 if (gVerbose)
2738 {
2739
2740 printf ("NoiseReductionApplied: %u/%u\n",
2741 (unsigned) fNoiseReductionApplied.n,
2742 (unsigned) fNoiseReductionApplied.d);
2743
2744 }
2745
2746 #endif
2747
2748 break;
2749
2750 }
2751
2752 // ProfileGainTableMap is specified for Raw IFD.
2753 //
2754 // ProfileGainTableMap2 is specified for IFD 0, but this implementation
2755 // supports reading the tag from Raw IFD, too.
2756
2757 case tcProfileGainTableMap:
2758 case tcProfileGainTableMap2:
2759 {
2760
2761 if (!CheckTagType (parentCode, tagCode, tagType, ttUndefined))
2762 return false;
2763
2764 if (tagCode == tcProfileGainTableMap)
2765 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
2766
2767 const bool useVersion2format = (tagCode == tcProfileGainTableMap2);
2768
2769 std::shared_ptr<dng_gain_table_map> pgtm
2770 (dng_gain_table_map::GetStream (host,
2771 stream,
2772 useVersion2format));
2773
2774 // If both PGTM and PGTM2 tags are present, then the latter
2775 // supersedes the former.
2776
2777 if (pgtm && (useVersion2format ||
2778 !fProfileGainTableMap ||
2779 fProfileGainTableMap_TagVersion == 1))
2780 {
2781
2782 fProfileGainTableMap = pgtm;
2783
2784 fProfileGainTableMap_TagVersion = useVersion2format ? 2 : 1;
2785
2786 }
2787
2788 #if qDNGValidate
2789
2790 if (pgtm && gVerbose)
2791 {
2792
2793 dng_md5_printer printer;
2794
2795 pgtm->AddDigest (printer);
2796
2797 auto digest = printer.Result ();
2798
2799 char str [2 * dng_fingerprint::kDNGFingerprintSize + 1];
2800
2801 digest.ToUtf8HexString (str);
2802
2803 printf ("%s (digest): %s\n",
2804 useVersion2format ? "ProfileGainTableMap2"
2805 : "ProfileGainTableMap",
2806 str);
2807
2808 }
2809
2810 #endif // qDNGValidate
2811
2812 if (stream.Position () > tagOffset + (uint64) tagCount)
2813 {
2814
2815 if (useVersion2format)
2816 ThrowBadFormat ("tcProfileGainTableMap2 parse error");
2817
2818 else
2819 ThrowBadFormat ("tcProfileGainTableMap parse error");
2820
2821 }
2822
2823 break;
2824
2825 }
2826
2827 case tcSemanticName:
2828 {
2829
2830 #if qDNGValidate
2831
2832 if (fNewSubFileType != sfSemanticMask)
2833 {
2834
2835 char message [256];
2836
2837 snprintf (message,
2838 256,
2839 "%s %s is not allowed in IFDs with NewSubFileType != SemanticMask",
2840 LookupParentCode (parentCode),
2841 LookupTagCode (parentCode, tagCode));
2842
2843 ReportWarning (message);
2844
2845 }
2846
2847 #endif
2848
2849 CheckTagType (parentCode, tagCode, tagType, ttAscii, ttByte);
2850
2851 ParseStringTag (stream,
2852 parentCode,
2853 tagCode,
2854 tagCount,
2855 fSemanticName,
2856 false);
2857
2858 #if qDNGValidate
2859
2860 if (gVerbose)
2861 {
2862
2863 printf ("SemanticName: ");
2864
2865 DumpString (fSemanticName);
2866
2867 printf ("\n");
2868
2869 }
2870
2871 #endif
2872
2873 break;
2874
2875 }
2876
2877 case tcSemanticInstanceID:
2878 {
2879
2880 #if qDNGValidate
2881
2882 if (fNewSubFileType != sfSemanticMask)
2883 {
2884
2885 char message [256];
2886
2887 snprintf (message,
2888 256,
2889 "%s %s is not allowed in IFDs with NewSubFileType != SemanticMask",
2890 LookupParentCode (parentCode),
2891 LookupTagCode (parentCode, tagCode));
2892
2893 ReportWarning (message);
2894
2895 }
2896
2897 #endif
2898
2899 CheckTagType (parentCode, tagCode, tagType, ttAscii, ttByte);
2900
2901 ParseStringTag (stream,
2902 parentCode,
2903 tagCode,
2904 tagCount,
2905 fSemanticInstanceID,
2906 false);
2907
2908 #if qDNGValidate
2909
2910 if (gVerbose)
2911 {
2912
2913 printf ("SemanticInstanceID: ");
2914
2915 DumpString (fSemanticInstanceID);
2916
2917 printf ("\n");
2918
2919 }
2920
2921 #endif
2922
2923 break;
2924
2925 }
2926
2927 case tcXMP:
2928 {
2929
2930 if (fNewSubFileType == sfSemanticMask)
2931 {
2932
2933 const uint32 bytes = tagCount;
2934
2935 // Fuzz: Limit to some reasonable size.
2936
2937 const uint32 kMaxBytes = 16 * 1024 * 1024;
2938
2939 if (bytes <= kMaxBytes)
2940 {
2941
2942 AutoPtr<dng_memory_block> block (host.Allocate (bytes));
2943
2944 stream.Get (block->Buffer (),
2945 bytes);
2946
2947 fSemanticXMP.reset (block.Release ());
2948
2949 #if qDNGValidate
2950
2951 if (gVerbose)
2952 {
2953
2954 uint64 offset = bytes ? tagOffset : 0;
2955
2956 printf ("SemanticMaskXMP: Count = %u, Offset = %u\n",
2957 (unsigned) bytes,
2958 (unsigned) offset);
2959
2960 if (bytes)
2961 {
2962
2963 stream.SetReadPosition (offset);
2964
2965 DumpXMP (stream, bytes);
2966
2967 }
2968
2969 }
2970
2971 #endif // qDNGValidate
2972
2973 return true;
2974
2975 }
2976
2977 }
2978
2979 return false;
2980
2981 }
2982
2983 case tcMaskSubArea:
2984 {
2985
2986 #if qDNGValidate
2987
2988 if (fNewSubFileType != sfSemanticMask)
2989 {
2990
2991 char message [256];
2992
2993 snprintf (message,
2994 256,
2995 "%s %s is not allowed in IFDs with NewSubFileType != SemanticMask",
2996 LookupParentCode (parentCode),
2997 LookupTagCode (parentCode, tagCode));
2998
2999 ReportWarning (message);
3000
3001 }
3002
3003 #endif
3004
3005 CheckTagType (parentCode, tagCode, tagType, ttLong);
3006
3007 fMaskSubArea [0] = stream.TagValue_uint32 (tagType);
3008 fMaskSubArea [1] = stream.TagValue_uint32 (tagType);
3009 fMaskSubArea [2] = stream.TagValue_uint32 (tagType);
3010 fMaskSubArea [3] = stream.TagValue_uint32 (tagType);
3011
3012 #if qDNGValidate
3013
3014 if (gVerbose)
3015 {
3016
3017 printf ("MaskSubArea: "
3018 "origin (t=%u, l=%u), "
3019 "whole size (w=%u, h=%u)\n",
3020 fMaskSubArea [0],
3021 fMaskSubArea [1],
3022 fMaskSubArea [2],
3023 fMaskSubArea [3]);
3024
3025 }
3026
3027 #endif // qDNGValidate
3028
3029 break;
3030
3031 }
3032
3033 case tcImageStats:
3034 {
3035
3036 if (!CheckTagType (parentCode, tagCode, tagType, ttUndefined))
3037 return false;
3038
3039 CheckRawIFD (parentCode, tagCode, fPhotometricInterpretation);
3040
3041 fImageStats.Parse (stream);
3042
3043 #if qDNGValidate
3044
3045 if (gVerbose)
3046 fImageStats.Dump ();
3047
3048 #endif // qDNGValidate
3049
3050 break;
3051
3052 }
3053
3054 case tcJXLDistance:
3055 {
3056
3057 if (!CheckTagType (parentCode, tagCode, tagType, ttFloat))
3058 return false;
3059
3060 fJXLDistance = (real32) stream.TagValue_real64 (tagType);
3061
3062 #if qDNGValidate
3063
3064 if (fCompression != ccJXL)
3065 {
3066 ReportWarning ("JXL compression expected");
3067 }
3068
3069 if (fJXLDistance < 0.0f)
3070 {
3071 ReportWarning ("Invalid JXL distance");
3072 }
3073
3074 if (gVerbose)
3075 {
3076 printf ("JXLDistance: %.2f\n", fJXLDistance);
3077 }
3078
3079 #endif
3080
3081 break;
3082
3083 }
3084
3085 case tcJXLEffort:
3086 {
3087
3088 if (!CheckTagType (parentCode, tagCode, tagType, ttLong))
3089 return false;
3090
3091 fJXLEffort = stream.TagValue_int32 (tagType);
3092
3093 #if qDNGValidate
3094
3095 if (fCompression != ccJXL)
3096 {
3097 ReportWarning ("JXL compression expected");
3098 }
3099
3100 if (fJXLEffort < 1 || fJXLEffort > 9)
3101 {
3102 ReportWarning ("Invalid JXL effort");
3103 }
3104
3105 if (gVerbose)
3106 {
3107 printf ("JXLEffort: %d\n", fJXLEffort);
3108 }
3109
3110 #endif
3111
3112 break;
3113
3114 }
3115
3116 case tcJXLDecodeSpeed:
3117 {
3118
3119 if (!CheckTagType (parentCode, tagCode, tagType, ttLong))
3120 return false;
3121
3122 fJXLDecodeSpeed = stream.TagValue_int32 (tagType);
3123
3124 #if qDNGValidate
3125
3126 if (fCompression != ccJXL)
3127 {
3128 ReportWarning ("JXL compression expected");
3129 }
3130
3131 if (fJXLDecodeSpeed < 1 || fJXLDecodeSpeed > 4)
3132 {
3133 ReportWarning ("Invalid JXL decode speed");
3134 }
3135
3136 if (gVerbose)
3137 {
3138 printf ("JXLDecodeSpeed: %d\n", fJXLDecodeSpeed);
3139 }
3140
3141 #endif
3142
3143 break;
3144
3145 }
3146
3147 default:
3148 {
3149
3150 return false;
3151
3152 }
3153
3154 }
3155
3156 return true;
3157
3158 }
3159
3160 /*****************************************************************************/
3161
3162 void dng_ifd::PostParse ()
3163 {
3164
3165 uint32 j;
3166 uint32 k;
3167
3168 // There is only one PlanarConfiguration for single sample imaages.
3169
3170 if (fSamplesPerPixel == 1)
3171 {
3172 fPlanarConfiguration = pcInterleaved;
3173 }
3174
3175 // Default tile size.
3176
3177 if (fTileWidth == 0)
3178 {
3179 fTileWidth = fImageWidth;
3180 }
3181
3182 if (fTileLength == 0)
3183 {
3184 fTileLength = fImageLength;
3185 }
3186
3187 // Default ActiveArea.
3188
3189 dng_rect imageArea (0, 0, fImageLength, fImageWidth);
3190
3191 if (fActiveArea.IsZero ())
3192 {
3193 fActiveArea = imageArea;
3194 }
3195
3196 // Default crop size.
3197
3198 if (fNewSubFileType != sfEnhancedImage)
3199 {
3200
3201 if (fDefaultCropSizeH.d == 0)
3202 {
3203 fDefaultCropSizeH = dng_urational (fActiveArea.W (), 1);
3204 }
3205
3206 if (fDefaultCropSizeV.d == 0)
3207 {
3208 fDefaultCropSizeV = dng_urational (fActiveArea.H (), 1);
3209 }
3210
3211 }
3212
3213 // Default white level.
3214
3215 uint32 defaultWhite = (fSampleFormat [0] == sfFloatingPoint) ?
3216 1 :
3217 (uint32) ((((uint64) 1) << fBitsPerSample [0]) - 1);
3218
3219 for (j = 0; j < kMaxColorPlanes; j++)
3220 {
3221
3222 if (fWhiteLevel [j] < 0.0)
3223 {
3224 fWhiteLevel [j] = (real64) defaultWhite;
3225 }
3226
3227 }
3228
3229 // Check AntiAliasStrength.
3230
3231 if (fAntiAliasStrength.As_real64 () < 0.0 ||
3232 fAntiAliasStrength.As_real64 () > 1.0)
3233 {
3234
3235 #if qDNGValidate
3236
3237 ReportWarning ("Invalid AntiAliasStrength");
3238
3239 #endif
3240
3241 fAntiAliasStrength = dng_urational (1, 1);
3242
3243 }
3244
3245 // Check MaskedAreas.
3246
3247 for (j = 0; j < fMaskedAreaCount; j++)
3248 {
3249
3250 const dng_rect &r = fMaskedArea [j];
3251
3252 if (r.IsEmpty () || ((r & imageArea) != r))
3253 {
3254
3255 #if qDNGValidate
3256
3257 ReportWarning ("Invalid MaskedArea");
3258
3259 #endif
3260
3261 fMaskedAreaCount = 0;
3262
3263 break;
3264
3265 }
3266
3267 if ((r & fActiveArea).NotEmpty ())
3268 {
3269
3270 #if qDNGValidate
3271
3272 ReportWarning ("MaskedArea overlaps ActiveArea");
3273
3274 #endif
3275
3276 fMaskedAreaCount = 0;
3277
3278 break;
3279
3280 }
3281
3282 for (k = 0; k < j; k++)
3283 {
3284
3285 if ((r & fMaskedArea [k]).NotEmpty ())
3286 {
3287
3288 #if qDNGValidate
3289
3290 ReportWarning ("MaskedAreas overlap each other");
3291
3292 #endif
3293
3294 fMaskedAreaCount = 0;
3295
3296 break;
3297
3298 }
3299
3300 }
3301
3302 }
3303
3304 // Check NoiseProfile.
3305
3306 if (!fNoiseProfile.IsValid () && fNoiseProfile.NumFunctions () != 0)
3307 {
3308
3309 #if qDNGValidate
3310
3311 ReportWarning ("Invalid NoiseProfile");
3312
3313 #endif
3314
3315 fNoiseProfile = dng_noise_profile ();
3316
3317 }
3318
3319 }
3320
3321 /*****************************************************************************/
3322
3323 bool dng_ifd::IsValidCFA (dng_shared &shared,
3324 uint32 parentCode)
3325 {
3326
3327 uint32 j;
3328 uint32 k;
3329 uint32 n;
3330
3331 #if !qDNGValidate
3332
3333 (void) parentCode; // Unused
3334
3335 #endif
3336
3337 if (fCFARepeatPatternRows < 1 || fCFARepeatPatternRows > kMaxCFAPattern ||
3338 fCFARepeatPatternCols < 1 || fCFARepeatPatternCols > kMaxCFAPattern)
3339 {
3340
3341 #if qDNGValidate
3342
3343 ReportError ("Missing or invalid CFAPatternRepeatDim",
3344 LookupParentCode (parentCode));
3345
3346 #endif
3347
3348 return false;
3349
3350 }
3351
3352 uint32 count [kMaxColorPlanes];
3353
3354 for (n = 0; n < shared.fCameraProfile.fColorPlanes; n++)
3355 {
3356 count [n] = 0;
3357 }
3358
3359 for (j = 0; j < fCFARepeatPatternRows; j++)
3360 {
3361
3362 for (k = 0; k < fCFARepeatPatternCols; k++)
3363 {
3364
3365 bool found = false;
3366
3367 for (n = 0; n < shared.fCameraProfile.fColorPlanes; n++)
3368 {
3369
3370 if (fCFAPattern [j] [k] == fCFAPlaneColor [n])
3371 {
3372 found = true;
3373 count [n] ++;
3374 break;
3375 }
3376
3377 }
3378
3379 if (!found)
3380 {
3381
3382 #if qDNGValidate
3383
3384 ReportError ("CFAPattern contains colors not included in the CFAPlaneColor tag",
3385 LookupParentCode (parentCode));
3386
3387 #endif
3388
3389 return false;
3390
3391 }
3392
3393 }
3394
3395 }
3396
3397 for (n = 0; n < shared.fCameraProfile.fColorPlanes; n++)
3398 {
3399
3400 if (count [n] == 0)
3401 {
3402
3403 #if qDNGValidate
3404
3405 ReportError ("CFAPattern does not contain all the colors in the CFAPlaneColor tag",
3406 LookupParentCode (parentCode));
3407
3408 #endif
3409
3410 return false;
3411
3412 }
3413
3414 }
3415
3416 if (fCFALayout < 1 || fCFALayout > 9)
3417 {
3418
3419 #if qDNGValidate
3420
3421 ReportError ("Invalid CFALayout",
3422 LookupParentCode (parentCode));
3423
3424 #endif
3425
3426 return false;
3427
3428 }
3429
3430 return true;
3431
3432 }
3433
3434 /*****************************************************************************/
3435
3436 bool dng_ifd::IsValidDNG (dng_shared &shared,
3437 uint32 parentCode)
3438 {
3439
3440 uint32 j;
3441
3442 bool isFloatingPoint = (fSampleFormat [0] == sfFloatingPoint);
3443
3444 dng_rect imageArea (0, 0, fImageLength, fImageWidth);
3445
3446 uint32 defaultWhite = isFloatingPoint ?
3447 1 :
3448 (uint32) ((((uint64) 1) << fBitsPerSample [0]) - 1);
3449
3450 bool isMonochrome = (shared.fCameraProfile.fColorPlanes == 1);
3451 bool isColor = !isMonochrome;
3452
3453 bool isMainIFD = (fNewSubFileType == sfMainImage);
3454
3455 bool isEnhancedIFD = (fNewSubFileType == sfEnhancedImage);
3456
3457 bool isMainOrEnhancedIFD = isMainIFD || isEnhancedIFD;
3458
3459 bool isGainMapIFD = (fNewSubFileType == sfGainMap);
3460
3461 // Check NewSubFileType.
3462
3463 if (!fUsesNewSubFileType)
3464 {
3465
3466 #if qDNGValidate
3467
3468 ReportError ("Missing NewSubFileType",
3469 LookupParentCode (parentCode));
3470
3471 #endif
3472
3473 return false;
3474
3475 }
3476
3477 if (fNewSubFileType != sfMainImage &&
3478 fNewSubFileType != sfPreviewImage &&
3479 fNewSubFileType != sfTransparencyMask &&
3480 fNewSubFileType != sfPreviewMask &&
3481 fNewSubFileType != sfDepthMap &&
3482 fNewSubFileType != sfPreviewDepthMap &&
3483 fNewSubFileType != sfEnhancedImage &&
3484 fNewSubFileType != sfAltPreviewImage &&
3485 fNewSubFileType != sfGainMap &&
3486 fNewSubFileType != sfSemanticMask)
3487 {
3488
3489 #if qDNGValidate
3490
3491 ReportError ("Unexpected NewSubFileType",
3492 LookupParentCode (parentCode));
3493
3494 #endif
3495
3496 return false;
3497
3498 }
3499
3500 // Check ImageWidth and ImageLength.
3501
3502 if (fImageWidth < 1)
3503 {
3504
3505 #if qDNGValidate
3506
3507 ReportError ("Missing or invalid ImageWidth",
3508 LookupParentCode (parentCode));
3509
3510 #endif
3511
3512 return false;
3513
3514 }
3515
3516 if (fImageLength < 1)
3517 {
3518
3519 #if qDNGValidate
3520
3521 ReportError ("Missing or invalid ImageLength",
3522 LookupParentCode (parentCode));
3523
3524 #endif
3525
3526 return false;
3527
3528 }
3529
3530 if (fImageWidth > kMaxImageSide ||
3531 fImageLength > kMaxImageSide)
3532 {
3533
3534 #if qDNGValidate
3535
3536 ReportWarning ("Image size is larger than supported");
3537
3538 #endif
3539
3540 return false;
3541
3542 }
3543
3544 // Check PhotometricInterpretation.
3545
3546 if (fNewSubFileType == sfTransparencyMask ||
3547 fNewSubFileType == sfPreviewMask)
3548 {
3549
3550 if (fPhotometricInterpretation != piTransparencyMask)
3551 {
3552
3553 #if qDNGValidate
3554
3555 ReportError ("NewSubFileType requires PhotometricInterpretation = TransparencyMask",
3556 LookupParentCode (parentCode));
3557
3558 #endif
3559
3560 return false;
3561
3562 }
3563
3564 }
3565
3566 else if (fNewSubFileType == sfDepthMap ||
3567 fNewSubFileType == sfPreviewDepthMap)
3568 {
3569
3570 if (fPhotometricInterpretation != piDepth)
3571 {
3572
3573 #if qDNGValidate
3574
3575 ReportError ("NewSubFileType requires PhotometricInterpretation = Depth",
3576 LookupParentCode (parentCode));
3577
3578 #endif
3579
3580 return false;
3581
3582 }
3583
3584 }
3585
3586 else if (fNewSubFileType == sfSemanticMask)
3587 {
3588
3589 if (fPhotometricInterpretation != piPhotometricMask)
3590 {
3591
3592 #if qDNGValidate
3593
3594 ReportError ("NewSubFileType requires PhotometricInterpretation = PhotometricMask",
3595 LookupParentCode (parentCode));
3596
3597 #endif
3598
3599 return false;
3600
3601 }
3602
3603 if (fSamplesPerPixel != 1)
3604 {
3605
3606 #if qDNGValidate
3607
3608 ReportError ("NewSubFileType requires SamplesPerPixel = 1",
3609 LookupParentCode (parentCode));
3610
3611 #endif
3612
3613 return false;
3614
3615 }
3616
3617 if (fBitsPerSample [0] != 8)
3618 {
3619
3620 #if qDNGValidate
3621
3622 ReportError ("NewSubFileType requires 8 bits per sample",
3623 LookupParentCode (parentCode));
3624
3625 #endif
3626
3627 return false;
3628
3629 }
3630
3631 }
3632
3633 else if (fNewSubFileType == sfGainMap)
3634 {
3635
3636 if (fPhotometricInterpretation != piGainMap)
3637 {
3638
3639 #if qDNGValidate
3640
3641 ReportError ("NewSubFileType Gain Map requires PhotometricInterpretation "
3642 "= Gain Map",
3643 LookupParentCode (parentCode));
3644
3645 #endif
3646
3647 return false;
3648
3649 }
3650
3651 if (fSamplesPerPixel != 1 &&
3652 fSamplesPerPixel != 3)
3653 {
3654
3655 #if qDNGValidate
3656
3657 ReportError ("NewSubFileType requires SamplesPerPixel = 1 or 3",
3658 LookupParentCode (parentCode));
3659
3660 #endif
3661
3662 return false;
3663
3664 }
3665
3666 }
3667
3668 else
3669 {
3670
3671 switch (fPhotometricInterpretation)
3672 {
3673
3674 case piBlackIsZero:
3675 case piRGB:
3676 case piYCbCr:
3677 {
3678
3679 if (isMainIFD)
3680 {
3681
3682 #if qDNGValidate
3683
3684 ReportError ("PhotometricInterpretation requires NewSubFileType = 1",
3685 LookupParentCode (parentCode));
3686
3687 #endif
3688
3689 return false;
3690
3691 }
3692
3693 break;
3694
3695 }
3696
3697 case piCFA:
3698 {
3699
3700 if (!isMainIFD)
3701 {
3702
3703 #if qDNGValidate
3704
3705 ReportError ("PhotometricInterpretation requires NewSubFileType = 0",
3706 LookupParentCode (parentCode));
3707
3708 #endif
3709
3710 return false;
3711
3712 }
3713
3714 break;
3715
3716 }
3717
3718 case piLinearRaw:
3719 break;
3720
3721 default:
3722 {
3723
3724 #if qDNGValidate
3725
3726 ReportError ("Missing or invalid PhotometricInterpretation",
3727 LookupParentCode (parentCode));
3728
3729 #endif
3730
3731 return false;
3732
3733 }
3734
3735 }
3736
3737 }
3738
3739 switch (fPhotometricInterpretation)
3740 {
3741
3742 case piBlackIsZero:
3743 {
3744
3745 // Allow black in white previews even in color images since the
3746 // raw processing software may be converting to grayscale.
3747
3748 if (isColor && isMainIFD)
3749 {
3750
3751 #if qDNGValidate
3752
3753 ReportError ("PhotometricInterpretation forbids use of ColorMatrix1 tag",
3754 LookupParentCode (parentCode));
3755
3756 #endif
3757
3758 return false;
3759
3760 }
3761
3762 break;
3763
3764 }
3765
3766 case piRGB:
3767 case piYCbCr:
3768 {
3769
3770 // Allow color previews even in monochrome DNG files, since the
3771 // raw procesing software may be adding color effects.
3772
3773 break;
3774
3775 }
3776
3777 case piCFA:
3778 {
3779
3780 if (isMonochrome)
3781 {
3782
3783 #if qDNGValidate
3784
3785 ReportError ("PhotometricInterpretation requires use of ColorMatrix1 tag",
3786 LookupParentCode (parentCode));
3787
3788 #endif
3789
3790 return false;
3791
3792 }
3793
3794 break;
3795
3796 }
3797
3798 }
3799
3800 // Check ColorimetricReference.
3801
3802 if (isGainMapIFD &&
3803 (shared.fColorimetricReference == crSceneReferred))
3804 {
3805
3806 #if qDNGValidate
3807
3808 ReportError ("Gain Maps can only be used in output-referred images",
3809 LookupParentCode (parentCode));
3810
3811 #endif
3812
3813 return false;
3814
3815 }
3816
3817 // Check floating-point support.
3818
3819 if (isFloatingPoint)
3820 {
3821
3822 if (fPhotometricInterpretation != piCFA &&
3823 fPhotometricInterpretation != piLinearRaw &&
3824 fPhotometricInterpretation != piTransparencyMask)
3825 {
3826
3827 #if qDNGValidate
3828
3829 ReportError ("Floating point data requires PhotometricInterpretation "
3830 "CFA or LinearRaw or TransparencyMask",
3831 LookupParentCode (parentCode));
3832
3833 #endif
3834
3835 return false;
3836
3837 }
3838
3839 }
3840
3841 // Check SamplesPerPixel and BitsPerSample.
3842
3843 uint32 minSamplesPerPixel = 1;
3844 uint32 maxSamplesPerPixel = 1;
3845
3846 uint32 minBitsPerSample = 8;
3847 uint32 maxBitsPerSample = 16;
3848
3849 switch (fPhotometricInterpretation)
3850 {
3851
3852 case piBlackIsZero:
3853 break;
3854
3855 case piRGB:
3856 case piYCbCr:
3857 {
3858 minSamplesPerPixel = 3;
3859 maxSamplesPerPixel = 3;
3860 break;
3861 }
3862
3863 case piCFA:
3864 {
3865 maxSamplesPerPixel = kMaxSamplesPerPixel;
3866 maxBitsPerSample = 32;
3867 break;
3868 }
3869
3870 case piGainMap:
3871 {
3872 minSamplesPerPixel = 1;
3873 maxSamplesPerPixel = 3;
3874 minBitsPerSample = 8;
3875 maxBitsPerSample = 32;
3876 break;
3877 }
3878
3879 case piLinearRaw:
3880 {
3881 minSamplesPerPixel = shared.fCameraProfile.fColorPlanes;
3882 maxSamplesPerPixel = shared.fCameraProfile.fColorPlanes;
3883 maxBitsPerSample = 32;
3884 break;
3885 }
3886
3887 case piTransparencyMask:
3888 {
3889 minBitsPerSample = 8;
3890 maxBitsPerSample = 16;
3891 break;
3892 }
3893
3894 case piDepth:
3895 {
3896 minBitsPerSample = 8;
3897 maxBitsPerSample = 16;
3898 break;
3899 }
3900
3901 }
3902
3903 if (isFloatingPoint)
3904 {
3905 minBitsPerSample = 16;
3906 maxBitsPerSample = 32;
3907 }
3908
3909 if (fSamplesPerPixel < minSamplesPerPixel ||
3910 fSamplesPerPixel > maxSamplesPerPixel)
3911 {
3912
3913 #if qDNGValidate
3914
3915 ReportError ("Missing or invalid SamplesPerPixel",
3916 LookupParentCode (parentCode));
3917
3918 #endif
3919
3920 return false;
3921
3922 }
3923
3924 for (j = 0; j < kMaxSamplesPerPixel; j++)
3925 {
3926
3927 if (j < fSamplesPerPixel)
3928 {
3929
3930 if (fBitsPerSample [j] < minBitsPerSample ||
3931 fBitsPerSample [j] > maxBitsPerSample)
3932 {
3933
3934 #if qDNGValidate
3935
3936 ReportError ("Missing or invalid BitsPerSample",
3937 LookupParentCode (parentCode));
3938
3939 #endif
3940
3941 return false;
3942
3943 }
3944
3945 if (isFloatingPoint &&
3946 fBitsPerSample [j] != 16 &&
3947 fBitsPerSample [j] != 24 &&
3948 fBitsPerSample [j] != 32)
3949 {
3950
3951 #if qDNGValidate
3952
3953 ReportError ("Invalid BitsPerSample for floating point",
3954 LookupParentCode (parentCode));
3955
3956 #endif
3957
3958 return false;
3959
3960 }
3961
3962 if (minBitsPerSample == 8 &&
3963 maxBitsPerSample == 16 &&
3964 fBitsPerSample [j] != 8 &&
3965 fBitsPerSample [j] != 16)
3966 {
3967
3968 #if qDNGValidate
3969
3970 ReportError ("Rendered previews and integer masks require 8 or 16 bits per sample",
3971 LookupParentCode (parentCode));
3972
3973 #endif
3974
3975 return false;
3976
3977 }
3978
3979 if (j > 0 && fBitsPerSample [j] != fBitsPerSample [0])
3980 {
3981
3982 #if qDNGValidate
3983
3984 ReportError ("BitsPerSample not equal for all samples",
3985 LookupParentCode (parentCode));
3986
3987 #endif
3988
3989 return false;
3990
3991 }
3992
3993 }
3994
3995 else
3996 {
3997
3998 if (fBitsPerSample [j] != 0)
3999 {
4000
4001 #if qDNGValidate
4002
4003 ReportError ("Too many values specified in BitsPerSample",
4004 LookupParentCode (parentCode));
4005
4006 #endif
4007
4008 return false;
4009
4010 }
4011
4012 }
4013
4014 }
4015
4016 // Check Compression.
4017
4018 switch (fCompression)
4019 {
4020
4021 case ccUncompressed:
4022 break;
4023
4024 #if qDNGSupportVC5
4025
4026 case ccVc5:
4027 break;
4028
4029 #endif // qDNGSupportVC5
4030
4031 case ccJPEG:
4032 {
4033
4034 if (fPhotometricInterpretation == piRGB)
4035 {
4036
4037 #if qDNGValidate
4038
4039 ReportError ("JPEG previews should use PhotometricInterpretation = YCbYb",
4040 LookupParentCode (parentCode));
4041
4042 #endif
4043
4044 return false;
4045
4046 }
4047
4048 if (fBitsPerSample [0] > 16)
4049 {
4050
4051 #if qDNGValidate
4052
4053 ReportError ("JPEG compression is limited to 16 bits/sample",
4054 LookupParentCode (parentCode));
4055
4056 #endif
4057
4058 return false;
4059
4060 }
4061
4062 break;
4063
4064 }
4065
4066 case ccLossyJPEG:
4067 {
4068
4069 if (fPhotometricInterpretation != piLinearRaw &&
4070 fPhotometricInterpretation != piPhotometricMask &&
4071 fPhotometricInterpretation != piGainMap)
4072 {
4073
4074 #if qDNGValidate
4075
4076 ReportError ("Lossy JPEG compression code requires PhotometricInterpretation = LinearRaw or PhotometricMask",
4077 LookupParentCode (parentCode));
4078
4079 #endif
4080
4081 return false;
4082
4083 }
4084
4085 if (fBitsPerSample [0] != 8)
4086 {
4087
4088 #if qDNGValidate
4089
4090 ReportError ("Lossy JPEG compression is limited to 8 bits/sample",
4091 LookupParentCode (parentCode));
4092
4093 #endif
4094
4095 return false;
4096
4097 }
4098
4099 break;
4100
4101 }
4102
4103 case ccJXL:
4104 {
4105
4106 if (fPhotometricInterpretation != piCFA &&
4107 fPhotometricInterpretation != piRGB &&
4108 fPhotometricInterpretation != piBlackIsZero &&
4109 fPhotometricInterpretation != piDepth &&
4110 fPhotometricInterpretation != piLinearRaw &&
4111 fPhotometricInterpretation != piGainMap &&
4112 fPhotometricInterpretation != piPhotometricMask &&
4113 fPhotometricInterpretation != piTransparencyMask)
4114 {
4115
4116 #if qDNGValidate
4117
4118 ReportError ("JXL compression code requires "
4119 "PhotometricInterpretation = CFA, RGB, BlackIsZero, "
4120 "Depth, LinearRaw, PhotometricMask, or "
4121 "Transparency",
4122 LookupParentCode (parentCode));
4123
4124 #endif
4125
4126 return false;
4127
4128 }
4129
4130 if (fBitsPerSample [0] < 8 ||
4131 fBitsPerSample [0] > 16)
4132 {
4133
4134 #if qDNGValidate
4135
4136 ReportError ("JXL compression is limited to 8 to 16 bits/sample",
4137 LookupParentCode (parentCode));
4138
4139 #endif
4140
4141 return false;
4142
4143 }
4144
4145 break;
4146
4147 }
4148
4149 case ccDeflate:
4150 {
4151
4152 if (!isFloatingPoint &&
4153 !isGainMapIFD &&
4154 fBitsPerSample [0] != 32 &&
4155 fPhotometricInterpretation != piTransparencyMask &&
4156 fPhotometricInterpretation != piPhotometricMask &&
4157 fPhotometricInterpretation != piGainMap &&
4158 fPhotometricInterpretation != piDepth)
4159 {
4160
4161 #if qDNGValidate
4162
4163 ReportError ("ZIP compression is limited to floating point, 32-bit integer,"
4164 " transparency masks, semantic masks, gain maps, and depth maps",
4165 LookupParentCode (parentCode));
4166
4167 #endif
4168
4169 }
4170
4171 break;
4172
4173 }
4174
4175 default:
4176 {
4177
4178 #if qDNGValidate
4179
4180 ReportError ("Unsupported Compression",
4181 LookupParentCode (parentCode));
4182
4183 #endif
4184
4185 return false;
4186
4187 }
4188
4189 }
4190
4191 // Check Predictor.
4192
4193 if (isFloatingPoint && fCompression == ccDeflate &&
4194 (fPredictor == cpFloatingPoint ||
4195 fPredictor == cpFloatingPointX2 ||
4196 fPredictor == cpFloatingPointX4))
4197 {
4198
4199 // These combinations are supported.
4200
4201 }
4202
4203 else if (!isFloatingPoint && fCompression == ccDeflate &&
4204 (fPredictor == cpHorizontalDifference ||
4205 fPredictor == cpHorizontalDifferenceX2 ||
4206 fPredictor == cpHorizontalDifferenceX4))
4207 {
4208
4209 // These combinations are supported.
4210
4211 }
4212
4213 else if (fPredictor != cpNullPredictor)
4214 {
4215
4216 #if qDNGValidate
4217
4218 ReportError ("Unsupported Predictor",
4219 LookupParentCode (parentCode));
4220
4221 #endif
4222
4223 return false;
4224
4225 }
4226
4227 // Check FillOrder.
4228
4229 if (fFillOrder != 1)
4230 {
4231
4232 #if qDNGValidate
4233
4234 ReportError ("Unsupported FillOrder",
4235 LookupParentCode (parentCode));
4236
4237 #endif
4238
4239 return false;
4240
4241 }
4242
4243 // Check PlanarConfiguration.
4244
4245 if (fPlanarConfiguration != pcInterleaved)
4246 {
4247
4248 #if qDNGValidate
4249
4250 ReportError ("Unsupported PlanarConfiguration",
4251 LookupParentCode (parentCode));
4252
4253 #endif
4254
4255 return false;
4256
4257 }
4258
4259 // Check ExtraSamples.
4260
4261 if (fExtraSamplesCount != 0)
4262 {
4263
4264 #if qDNGValidate
4265
4266 ReportError ("Unsupported ExtraSamples",
4267 LookupParentCode (parentCode));
4268
4269 #endif
4270
4271 return false;
4272
4273 }
4274
4275 // Check SampleFormat.
4276
4277 for (j = 0; j < fSamplesPerPixel; j++)
4278 {
4279
4280 if (fSampleFormat [j] != (isFloatingPoint ? sfFloatingPoint : sfUnsignedInteger))
4281 {
4282
4283 #if qDNGValidate
4284
4285 ReportError ("Unsupported SampleFormat",
4286 LookupParentCode (parentCode));
4287
4288 #endif
4289
4290 return false;
4291
4292 }
4293
4294 }
4295
4296 // Check Orientation.
4297
4298 if (fOrientation > 9)
4299 {
4300
4301 #if qDNGValidate
4302
4303 ReportError ("Unknown Orientation",
4304 LookupParentCode (parentCode));
4305
4306 #endif
4307
4308 return false;
4309
4310 }
4311
4312 #if qDNGValidate
4313
4314 if (fOrientation != 0 && parentCode != 0)
4315 {
4316
4317 ReportWarning ("Unexpected Orientation tag",
4318 LookupParentCode (parentCode));
4319
4320 }
4321
4322 if (fOrientation == 0 && parentCode == 0)
4323 {
4324
4325 ReportWarning ("Missing Orientation tag",
4326 LookupParentCode (parentCode));
4327
4328 }
4329
4330 #endif
4331
4332 // Check Strips vs. Tiles.
4333
4334 if (!fUsesStrips && !fUsesTiles)
4335 {
4336
4337 #if qDNGValidate
4338
4339 ReportError ("IFD uses neither strips nor tiles",
4340 LookupParentCode (parentCode));
4341
4342 #endif
4343
4344 return false;
4345
4346 }
4347
4348 if (fUsesStrips && fUsesTiles)
4349 {
4350
4351 #if qDNGValidate
4352
4353 ReportError ("IFD uses both strips and tiles",
4354 LookupParentCode (parentCode));
4355
4356 #endif
4357
4358 return false;
4359
4360 }
4361
4362 // Check tile info.
4363
4364 uint32 tilesWide = SafeUint32DivideUp (fImageWidth, fTileWidth);
4365 uint32 tilesHigh = SafeUint32DivideUp (fImageLength, fTileLength);
4366
4367 uint32 tileCount = tilesWide * tilesHigh;
4368
4369 if (fTileOffsetsCount != tileCount)
4370 {
4371
4372 #if qDNGValidate
4373
4374 ReportError ("Missing or invalid Strip/TileOffsets",
4375 LookupParentCode (parentCode));
4376
4377 #endif
4378
4379 return false;
4380
4381 }
4382
4383 if (fTileByteCountsCount != tileCount)
4384 {
4385
4386 #if qDNGValidate
4387
4388 ReportError ("Missing or invalid Strip/TileByteCounts",
4389 LookupParentCode (parentCode));
4390
4391 #endif
4392
4393 return false;
4394
4395 }
4396
4397 // Check CFA pattern.
4398
4399 if (fPhotometricInterpretation == piCFA)
4400 {
4401
4402 if (!IsValidCFA (shared, parentCode))
4403 {
4404
4405 return false;
4406
4407 }
4408
4409 }
4410
4411 // Check ActiveArea.
4412
4413 if (((fActiveArea & imageArea) != fActiveArea) || fActiveArea.IsEmpty ())
4414 {
4415
4416 #if qDNGValidate
4417
4418 ReportError ("Invalid ActiveArea",
4419 LookupParentCode (parentCode));
4420
4421 #endif
4422
4423 return false;
4424
4425 }
4426
4427 if (fActiveArea != imageArea)
4428 {
4429
4430 if (shared.fDNGBackwardVersion < dngVersion_1_1_0_0)
4431 {
4432
4433 #if qDNGValidate
4434
4435 ReportError ("Non-default ActiveArea tag not allowed in this DNG version",
4436 LookupParentCode (parentCode));
4437
4438 #endif
4439
4440 return false;
4441
4442 }
4443
4444 }
4445
4446 // Check LinearizationTable.
4447
4448 if (fLinearizationTableCount)
4449 {
4450
4451 if (fLinearizationTableType != ttShort)
4452 {
4453
4454 #if qDNGValidate
4455
4456 ReportError ("Invalidate LinearizationTable type",
4457 LookupParentCode (parentCode));
4458
4459 #endif
4460
4461 return false;
4462
4463 }
4464
4465 if (fLinearizationTableCount < 2 ||
4466 fLinearizationTableCount > 65536)
4467 {
4468
4469 #if qDNGValidate
4470
4471 ReportError ("Invalidate LinearizationTable count",
4472 LookupParentCode (parentCode));
4473
4474 #endif
4475
4476 return false;
4477
4478 }
4479
4480 if (isFloatingPoint || fBitsPerSample [0] > 16)
4481 {
4482
4483 #if qDNGValidate
4484
4485 ReportError ("Linearization table not allowed for this data type",
4486 LookupParentCode (parentCode));
4487
4488 #endif
4489
4490 return false;
4491
4492 }
4493
4494 }
4495
4496 // Check BlackLevelRepeatDim.
4497
4498 if (fBlackLevelRepeatRows < 1 || fBlackLevelRepeatRows > kMaxBlackPattern ||
4499 fBlackLevelRepeatCols < 1 || fBlackLevelRepeatCols > kMaxBlackPattern)
4500 {
4501
4502 #if qDNGValidate
4503
4504 ReportError ("Invalid BlackLevelRepeatDim",
4505 LookupParentCode (parentCode));
4506
4507 #endif
4508
4509 return false;
4510
4511 }
4512
4513 // Check BlackLevelDeltaH.
4514
4515 if (fBlackLevelDeltaHCount != 0 &&
4516 fBlackLevelDeltaHCount != fActiveArea.W ())
4517 {
4518
4519 #if qDNGValidate
4520
4521 ReportError ("Invalid BlackLevelDeltaH count",
4522 LookupParentCode (parentCode));
4523
4524 #endif
4525
4526 return false;
4527
4528 }
4529
4530 // Check BlackLevelDeltaV.
4531
4532 if (fBlackLevelDeltaVCount != 0 &&
4533 fBlackLevelDeltaVCount != fActiveArea.H ())
4534 {
4535
4536 #if qDNGValidate
4537
4538 ReportError ("Invalid BlackLevelDeltaV count",
4539 LookupParentCode (parentCode));
4540
4541 #endif
4542
4543 return false;
4544
4545 }
4546
4547 // Check WhiteLevel.
4548
4549 real64 maxWhite = fLinearizationTableCount ? 65535.0
4550 : (real64) defaultWhite;
4551
4552 for (j = 0; j < fSamplesPerPixel; j++)
4553 {
4554
4555 if (fWhiteLevel [j] < 1.0 || (fWhiteLevel [j] > maxWhite && !isFloatingPoint))
4556 {
4557
4558 #if qDNGValidate
4559
4560 ReportError ("Invalid WhiteLevel",
4561 LookupParentCode (parentCode));
4562
4563 #endif
4564
4565 return false;
4566
4567 }
4568
4569 }
4570
4571 // Check BlackLevel.
4572
4573 for (j = 0; j < kMaxBlackPattern; j++)
4574 {
4575
4576 for (uint32 k = 0; k < kMaxBlackPattern; k++)
4577 {
4578
4579 for (uint32 s = 0; s < kMaxColorPlanes; s++)
4580 {
4581
4582 const real64 black = fBlackLevel [j][k][s];
4583
4584 if (black >= fWhiteLevel [s])
4585 {
4586
4587 #if qDNGValidate
4588
4589 ReportError ("Invalid BlackLevel",
4590 LookupParentCode (parentCode));
4591
4592 #endif
4593
4594 return false;
4595
4596 }
4597
4598 }
4599
4600 }
4601
4602 }
4603
4604 // Check DefaultScaleH. Enhanced IFDs default to invalid, so allow that.
4605
4606 if ((fNewSubFileType != sfEnhancedImage || fDefaultScaleH.IsValid ()) &&
4607 fDefaultScaleH.As_real64 () <= 0.0)
4608 {
4609
4610 #if qDNGValidate
4611
4612 ReportError ("Invalid DefaultScaleH");
4613
4614 #endif
4615
4616 return false;
4617
4618 }
4619
4620 // Check DefaultScaleV. Enhanced IFDs default to invalid, so allow that.
4621
4622 if ((fNewSubFileType != sfEnhancedImage || fDefaultScaleV.IsValid ()) &&
4623 fDefaultScaleV.As_real64 () <= 0.0)
4624 {
4625
4626 #if qDNGValidate
4627
4628 ReportError ("Invalid DefaultScaleV");
4629
4630 #endif
4631
4632 return false;
4633
4634 }
4635
4636 // Check BestQualityScale. Enhanced IFDs default to invalid, so allow that.
4637
4638 if ((fNewSubFileType != sfEnhancedImage || fBestQualityScale.IsValid ()) &&
4639 fBestQualityScale.As_real64 () < 1.0)
4640 {
4641
4642 #if qDNGValidate
4643
4644 ReportError ("Invalid BestQualityScale");
4645
4646 #endif
4647
4648 return false;
4649
4650 }
4651
4652 // Check DefaultCropOrigin.
4653
4654 if (fDefaultCropOriginH.As_real64 () < 0.0 ||
4655 fDefaultCropOriginV.As_real64 () < 0.0 ||
4656 fDefaultCropOriginH.As_real64 () >= (real64) fActiveArea.W () ||
4657 fDefaultCropOriginV.As_real64 () >= (real64) fActiveArea.H ())
4658 {
4659
4660 #if qDNGValidate
4661
4662 ReportError ("Invalid DefaultCropOrigin");
4663
4664 #endif
4665
4666 return false;
4667
4668 }
4669
4670 // Check DefaultCropSize. Enhanced IFDs default to invalid, so allow that.
4671
4672 if (fNewSubFileType != sfEnhancedImage ||
4673 fDefaultCropSizeH.IsValid () ||
4674 fDefaultCropSizeV.IsValid ())
4675 {
4676
4677 if (fDefaultCropSizeH.As_real64 () <= 0.0 ||
4678 fDefaultCropSizeV.As_real64 () <= 0.0 ||
4679 fDefaultCropSizeH.As_real64 () > (real64) fActiveArea.W () ||
4680 fDefaultCropSizeV.As_real64 () > (real64) fActiveArea.H ())
4681 {
4682
4683 #if qDNGValidate
4684
4685 ReportError ("Invalid DefaultCropSize");
4686
4687 #endif
4688
4689 return false;
4690
4691 }
4692
4693 }
4694
4695 // Check DefaultCrop area.
4696
4697 if (fDefaultCropOriginH.As_real64 () +
4698 fDefaultCropSizeH .As_real64 () > (real64) fActiveArea.W () ||
4699 fDefaultCropOriginV.As_real64 () +
4700 fDefaultCropSizeV .As_real64 () > (real64) fActiveArea.H ())
4701 {
4702
4703 #if qDNGValidate
4704
4705 ReportError ("Default crop extends outside ActiveArea");
4706
4707 #endif
4708
4709 return false;
4710
4711 }
4712
4713 // Check DefaultUserCrop.
4714
4715 if (fDefaultUserCropT.As_real64 () < 0.0 ||
4716 fDefaultUserCropL.As_real64 () < 0.0 ||
4717 fDefaultUserCropB.As_real64 () > 1.0 ||
4718 fDefaultUserCropR.As_real64 () > 1.0 ||
4719 fDefaultUserCropT.As_real64 () >= fDefaultUserCropB.As_real64 () ||
4720 fDefaultUserCropL.As_real64 () >= fDefaultUserCropR.As_real64 ())
4721 {
4722
4723 #if qDNGValidate
4724
4725 ReportError ("Invalid DefaultUserCrop");
4726
4727 #endif // qDNGValidate
4728
4729 return false;
4730
4731 }
4732
4733 // The default crop tags are restricted to the main and enhanced IFDs. If
4734 // found elsewhere, at least require that they be NOPs.
4735
4736 if (!isMainOrEnhancedIFD)
4737 {
4738
4739 if (Round_int32 (fDefaultCropOriginH.As_real64 ()) != 0 ||
4740 Round_int32 (fDefaultCropOriginV.As_real64 ()) != 0)
4741 {
4742
4743 #if qDNGValidate
4744
4745 ReportError ("non-default DefaultCropOrigin on non-main image");
4746
4747 #endif
4748
4749 return false;
4750
4751 }
4752
4753 if (Round_int32 (fDefaultCropSizeH.As_real64 ()) != (int32) fImageWidth ||
4754 Round_int32 (fDefaultCropSizeV.As_real64 ()) != (int32) fImageLength)
4755 {
4756
4757 #if qDNGValidate
4758
4759 ReportError ("non-default DefaultCropSize on non-main image");
4760
4761 #endif
4762
4763 return false;
4764
4765 }
4766
4767 }
4768
4769 // The default user crop tag is restricted to the main IFD. If found
4770 // elsewhere, at least require that it be a NOP.
4771
4772 if (!isMainIFD)
4773 {
4774
4775 if (fDefaultUserCropT.As_real64 () != 0.0 ||
4776 fDefaultUserCropL.As_real64 () != 0.0 ||
4777 fDefaultUserCropB.As_real64 () != 1.0 ||
4778 fDefaultUserCropR.As_real64 () != 1.0)
4779 {
4780
4781 #if qDNGValidate
4782
4783 ReportError ("non-default DefaultCUserCrop on non-main image");
4784
4785 #endif // qDNGValidate
4786
4787 return false;
4788
4789 }
4790
4791 }
4792
4793 // Warning if too little padding on CFA image.
4794
4795 #if qDNGValidate
4796
4797 if (fPhotometricInterpretation == piCFA)
4798 {
4799
4800 const real64 kMinPad = 1.9;
4801
4802 if (fDefaultCropOriginH.As_real64 () < kMinPad)
4803 {
4804
4805 ReportWarning ("Too little padding on left edge of CFA image",
4806 "possible interpolation artifacts");
4807
4808 }
4809
4810 if (fDefaultCropOriginV.As_real64 () < kMinPad)
4811 {
4812
4813 ReportWarning ("Too little padding on top edge of CFA image",
4814 "possible interpolation artifacts");
4815
4816 }
4817
4818 if (fDefaultCropOriginH.As_real64 () +
4819 fDefaultCropSizeH .As_real64 () > (real64) fActiveArea.W () - kMinPad)
4820 {
4821
4822 ReportWarning ("Too little padding on right edge of CFA image",
4823 "possible interpolation artifacts");
4824
4825 }
4826
4827 if (fDefaultCropOriginV.As_real64 () +
4828 fDefaultCropSizeV .As_real64 () > (real64) fActiveArea.H () - kMinPad)
4829 {
4830
4831 ReportWarning ("Too little padding on bottom edge of CFA image",
4832 "possible interpolation artifacts");
4833
4834 }
4835
4836 }
4837
4838 #endif
4839
4840 // Check RowInterleaveFactor
4841
4842 if (fRowInterleaveFactor != 1)
4843 {
4844
4845 if (fRowInterleaveFactor < 1 ||
4846 fRowInterleaveFactor > fImageLength)
4847 {
4848
4849 #if qDNGValidate
4850
4851 ReportError ("RowInterleaveFactor out of valid range",
4852 LookupParentCode (parentCode));
4853
4854 #endif
4855
4856 return false;
4857
4858 }
4859
4860 if (shared.fDNGBackwardVersion < dngVersion_1_2_0_0)
4861 {
4862
4863 #if qDNGValidate
4864
4865 ReportError ("Non-default RowInterleaveFactor tag not allowed in this DNG version",
4866 LookupParentCode (parentCode));
4867
4868 #endif
4869
4870 return false;
4871
4872 }
4873
4874 }
4875
4876 // Check ColumnInterleaveFactor
4877
4878 if (fColumnInterleaveFactor != 1)
4879 {
4880
4881 if (fColumnInterleaveFactor < 1 ||
4882 fColumnInterleaveFactor > fImageWidth)
4883 {
4884
4885 #if qDNGValidate
4886
4887 ReportError ("ColumnInterleaveFactor out of valid range",
4888 LookupParentCode (parentCode));
4889
4890 #endif
4891
4892 return false;
4893
4894 }
4895
4896 if (shared.fDNGBackwardVersion < dngVersion_1_7_0_0)
4897 {
4898
4899 #if qDNGValidate
4900
4901 ReportError ("Non-default ColumnInterleaveFactor tag not allowed in this DNG version",
4902 LookupParentCode (parentCode));
4903
4904 #endif
4905
4906 return false;
4907
4908 }
4909
4910 #if qDNGValidate
4911
4912 // Only warn if column interleave factor is used before DNG 1.7.1 since the
4913 // original published 1.7 spec allowed it.
4914
4915 if (shared.fDNGBackwardVersion < dngVersion_1_7_1_0)
4916 {
4917
4918 ReportWarning ("Non-default ColumnInterleaveFactor tag not allowed in this DNG version",
4919 LookupParentCode (parentCode));
4920
4921 }
4922
4923 #endif
4924
4925 }
4926
4927 // Check SubTileBlockSize
4928
4929 if (fSubTileBlockRows != 1 || fSubTileBlockCols != 1)
4930 {
4931
4932 if (fSubTileBlockRows < 2 || fSubTileBlockRows > fTileLength ||
4933 fSubTileBlockCols < 1 || fSubTileBlockCols > fTileWidth)
4934 {
4935
4936 #if qDNGValidate
4937
4938 ReportError ("SubTileBlockSize out of valid range",
4939 LookupParentCode (parentCode));
4940
4941 #endif
4942
4943 return false;
4944
4945 }
4946
4947 if ((fTileLength % fSubTileBlockRows) != 0 ||
4948 (fTileWidth % fSubTileBlockCols) != 0)
4949 {
4950
4951 #if qDNGValidate
4952
4953 ReportError ("TileSize not exact multiple of SubTileBlockSize",
4954 LookupParentCode (parentCode));
4955
4956 #endif
4957
4958 return false;
4959
4960 }
4961
4962 if (shared.fDNGBackwardVersion < dngVersion_1_2_0_0)
4963 {
4964
4965 #if qDNGValidate
4966
4967 ReportError ("Non-default SubTileBlockSize tag not allowed in this DNG version",
4968 LookupParentCode (parentCode));
4969
4970 #endif
4971
4972 return false;
4973
4974 }
4975
4976 }
4977
4978 return true;
4979
4980 }
4981
4982 /*****************************************************************************/
4983
4984 uint32 dng_ifd::TilesAcross () const
4985 {
4986
4987 if (fTileWidth)
4988 {
4989
4990 uint64 width64 = (uint64) fTileWidth;
4991
4992 return (uint32) (((fImageWidth + width64) - 1) / width64);
4993
4994 }
4995
4996 return 0;
4997
4998 }
4999
5000 /*****************************************************************************/
5001
5002 uint32 dng_ifd::TilesDown () const
5003 {
5004
5005 if (fTileLength)
5006 {
5007
5008 // Use 64-bit math to prevent overflow. RowsPerStrip (assigned to
5009 // fImageLength during parsing) may be 2^32 - 1, indicating a single
5010 // strip.
5011
5012 uint64 length64 = (uint64) fTileLength;
5013
5014 return (uint32) (((fImageLength + length64) - 1) / length64);
5015
5016 }
5017
5018 return 0;
5019
5020 }
5021
5022 /*****************************************************************************/
5023
5024 uint32 dng_ifd::TilesPerImage () const
5025 {
5026
5027 uint32 total = TilesAcross () * TilesDown ();
5028
5029 if (fPlanarConfiguration == pcPlanar)
5030 {
5031
5032 total *= fSamplesPerPixel;
5033
5034 }
5035
5036 return total;
5037
5038 }
5039
5040 /*****************************************************************************/
5041
5042 dng_rect dng_ifd::TileArea (uint32 rowIndex,
5043 uint32 colIndex) const
5044 {
5045
5046 dng_rect r;
5047
5048 r.t = rowIndex * fTileLength;
5049 r.b = r.t + fTileLength;
5050
5051 r.l = colIndex * fTileWidth;
5052 r.r = r.l + fTileWidth;
5053
5054 // If this IFD is using strips rather than tiles, the last strip
5055 // is trimmed so it does not extend beyond the end of the image.
5056
5057 if (fUsesStrips)
5058 {
5059
5060 r.b = Min_uint32 (r.b, fImageLength);
5061
5062 }
5063
5064 return r;
5065
5066 }
5067
5068 /*****************************************************************************/
5069
5070 uint32 dng_ifd::TileByteCount (const dng_rect &tile) const
5071 {
5072
5073 if (fCompression == ccUncompressed)
5074 {
5075
5076 uint32 bitsPerRow = SafeUint32Mult (tile.W (),
5077 fBitsPerSample [0]);
5078
5079 if (fPlanarConfiguration == pcInterleaved)
5080 {
5081
5082 bitsPerRow = SafeUint32Mult (bitsPerRow,
5083 fSamplesPerPixel);
5084
5085 }
5086
5087 uint32 bytesPerRow = SafeUint32DivideUp (bitsPerRow, 8);
5088
5089 if (fPlanarConfiguration == pcRowInterleaved)
5090 {
5091
5092 bytesPerRow = SafeUint32Mult (bytesPerRow,
5093 fSamplesPerPixel);
5094
5095 }
5096
5097 return SafeUint32Mult (bytesPerRow, tile.H ());
5098
5099 }
5100
5101 return 0;
5102
5103 }
5104
5105 /*****************************************************************************/
5106
5107 uint64 dng_ifd::MaxImageDataByteCount () const
5108 {
5109
5110 uint64 bitsPerRow = (uint64) fTileWidth *
5111 (uint64) fSamplesPerPixel *
5112 (uint64) fBitsPerSample [0];
5113
5114 uint64 bytesPerRow = (bitsPerRow + 7) >> 3;
5115
5116 uint64 bytesPerTile = bytesPerRow * fTileLength;
5117
5118 // Round up for TIFF format tile data alignment.
5119
5120 if (bytesPerTile & 1) bytesPerTile++;
5121
5122 // Deal with possible compression expansion.
5123
5124 if (fCompression != ccUncompressed)
5125 {
5126
5127 if (fCompression == ccDeflate)
5128 {
5129
5130 // ZLib says maximum is source size + 0.1% + 12 bytes.
5131
5132 bytesPerTile += (bytesPerTile >> 8) + 12;
5133
5134 }
5135
5136 else
5137 {
5138
5139 // Add a slop factor for compression expansion.
5140
5141 bytesPerTile += (bytesPerTile >> 2) + 1024;
5142
5143 }
5144
5145 }
5146
5147 return bytesPerTile * TilesPerImage ();
5148
5149 }
5150
5151 /*****************************************************************************/
5152
5153 void dng_ifd::SetSingleStrip ()
5154 {
5155
5156 fTileWidth = fImageWidth;
5157 fTileLength = fImageLength;
5158
5159 fUsesTiles = false;
5160 fUsesStrips = true;
5161
5162 }
5163
5164 /*****************************************************************************/
5165
5166 void dng_ifd::FindTileSize (uint32 bytesPerTile,
5167 uint32 cellH,
5168 uint32 cellV)
5169 {
5170
5171 uint32 bytesPerSample = fSamplesPerPixel *
5172 ((fBitsPerSample [0] + 7) >> 3);
5173
5174 uint32 samplesPerTile = bytesPerTile / bytesPerSample;
5175
5176 uint32 tileSide = Round_uint32 (sqrt ((real64) samplesPerTile));
5177
5178 fTileWidth = Min_uint32 (fImageWidth, tileSide);
5179
5180 fTileWidth = Min_uint32 (fTileWidth, 32 * 1024);
5181
5182 uint32 across = TilesAcross ();
5183
5184 DNG_REQUIRE (across > 0, "Bad number of tiles across in dng_ifd::FindTileSize");
5185
5186 fTileWidth = (fImageWidth + across - 1) / across;
5187
5188 fTileWidth = ((fTileWidth + cellH - 1) / cellH) * cellH;
5189
5190 fTileWidth = Min_uint32 (fTileWidth, fImageWidth);
5191
5192 fTileLength = Pin_uint32 (1,
5193 samplesPerTile / fTileWidth,
5194 fImageLength);
5195
5196 fTileLength = Min_uint32 (fTileLength, 32 * 1024);
5197
5198 uint32 down = TilesDown ();
5199
5200 fTileLength = (fImageLength + down - 1) / down;
5201
5202 fTileLength = ((fTileLength + cellV - 1) / cellV) * cellV;
5203
5204 fTileLength = Min_uint32 (fTileLength, fImageLength);
5205
5206 fUsesTiles = true;
5207 fUsesStrips = false;
5208
5209 }
5210
5211 /*****************************************************************************/
5212
5213 void dng_ifd::FindStripSize (uint32 bytesPerStrip,
5214 uint32 cellV)
5215 {
5216
5217 uint32 bytesPerSample = fSamplesPerPixel *
5218 ((fBitsPerSample [0] + 7) >> 3);
5219
5220 uint32 samplesPerStrip = bytesPerStrip / bytesPerSample;
5221
5222 fTileWidth = fImageWidth;
5223
5224 fTileLength = Pin_uint32 (1,
5225 samplesPerStrip / fTileWidth,
5226 fImageLength);
5227
5228 uint32 down = TilesDown ();
5229
5230 fTileLength = (fImageLength + down - 1) / down;
5231
5232 fTileLength = ((fTileLength + cellV - 1) / cellV) * cellV;
5233
5234 fTileLength = Min_uint32 (fTileLength, fImageLength);
5235
5236 fUsesTiles = false;
5237 fUsesStrips = true;
5238
5239 }
5240
5241 /*****************************************************************************/
5242
5243 uint32 dng_ifd::PixelType () const
5244 {
5245
5246 if (fSampleFormat [0] == sfFloatingPoint)
5247 {
5248 return ttFloat;
5249 }
5250
5251 if (fBitsPerSample [0] <= 8)
5252 {
5253 return ttByte;
5254 }
5255
5256 else if (fBitsPerSample [0] <= 16)
5257 {
5258 return ttShort;
5259 }
5260
5261 return ttLong;
5262
5263 }
5264
5265 /*****************************************************************************/
5266
5267 bool dng_ifd::IsBaselineJPEG () const
5268 {
5269
5270 if (fBitsPerSample [0] != 8)
5271 {
5272 return false;
5273 }
5274
5275 if (fSampleFormat [0] != sfUnsignedInteger)
5276 {
5277 return false;
5278 }
5279
5280 if (fCompression == ccLossyJPEG)
5281 {
5282 return true;
5283 }
5284
5285 if (fCompression != ccJPEG)
5286 {
5287 return false;
5288 }
5289
5290 switch (fPhotometricInterpretation)
5291 {
5292
5293 case piBlackIsZero:
5294 {
5295 return (fSamplesPerPixel == 1);
5296 }
5297
5298 case piYCbCr:
5299 {
5300 return (fSamplesPerPixel == 3 ) &&
5301 (fPlanarConfiguration == pcInterleaved);
5302 }
5303
5304 default:
5305 break;
5306
5307 }
5308
5309 return false;
5310
5311 }
5312
5313 /*****************************************************************************/
5314
5315 bool dng_ifd::CanRead () const
5316 {
5317
5318 dng_read_image reader;
5319
5320 return reader.CanRead (*this);
5321
5322 }
5323
5324 /*****************************************************************************/
5325
5326 void dng_ifd::ReadImage (dng_host &host,
5327 dng_stream &stream,
5328 dng_image &image,
5329 dng_lossy_compressed_image *lossyImage,
5330 dng_fingerprint *lossyDigest) const
5331 {
5332
5333 dng_read_image reader;
5334
5335 reader.Read (host,
5336 *this,
5337 stream,
5338 image,
5339 lossyImage,
5340 lossyDigest);
5341
5342 }
5343
5344 /*****************************************************************************/