Debian Codegraph @

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

raw
1 /*****************************************************************************/
2 // Copyright 2006-2023 Adobe Systems Incorporated
3 // All Rights Reserved.
4 //
5 // NOTICE: Adobe permits you to use, modify, and distribute this file in
6 // accordance with the terms of the Adobe license agreement accompanying it.
7 /*****************************************************************************/
8
9 /** \file
10 * Support for writing DNG images to files.
11 */
12
13 /*****************************************************************************/
14
15 #ifndef __dng_image_writer__
16 #define __dng_image_writer__
17
18 /*****************************************************************************/
19
20 #include "dng_area_task.h"
21 #include "dng_auto_ptr.h"
22 #include "dng_classes.h"
23 #include "dng_fingerprint.h"
24 #include "dng_memory.h"
25 #include "dng_mutex.h"
26 #include "dng_point.h"
27 #include "dng_rational.h"
28 #include "dng_safe_arithmetic.h"
29 #include "dng_sdk_limits.h"
30 #include "dng_string.h"
31 #include "dng_tag_types.h"
32 #include "dng_tag_values.h"
33 #include "dng_types.h"
34 #include "dng_uncopyable.h"
35
36 #include <atomic>
37
38 /*****************************************************************************/
39
40 /// @brief Image resolution.
41
42 class dng_resolution
43 {
44
45 public:
46
47 dng_urational fXResolution;
48 dng_urational fYResolution;
49
50 uint16 fResolutionUnit;
51
52 public:
53
54 dng_resolution ();
55
56 };
57
58 /*****************************************************************************/
59
60 class tiff_tag: private dng_uncopyable
61 {
62
63 protected:
64
65 uint16 fCode;
66
67 uint16 fType;
68
69 uint32 fCount;
70
71 protected:
72
73 tiff_tag (uint16 code,
74 uint16 type,
75 uint32 count)
76
77 : fCode (code)
78 , fType (type)
79 , fCount (count)
80
81 {
82 }
83
84 public:
85
86 virtual ~tiff_tag ()
87 {
88 }
89
90 uint16 Code () const
91 {
92 return fCode;
93 }
94
95 uint16 Type () const
96 {
97 return fType;
98 }
99
100 uint32 Count () const
101 {
102 return fCount;
103 }
104
105 void SetCount (uint32 count)
106 {
107 fCount = count;
108 }
109
110 uint32 Size () const
111 {
112 return TagTypeSize (Type ()) * Count ();
113 }
114
115 virtual void SetBigTIFF (bool /* isBigTIFF */)
116 {
117 }
118
119 virtual void Put (dng_stream &stream) const = 0;
120
121 };
122
123 /******************************************************************************/
124
125 class tag_data_ptr: public tiff_tag
126 {
127
128 protected:
129
130 const void *fData;
131
132 public:
133
134 tag_data_ptr (uint16 code,
135 uint16 type,
136 uint32 count,
137 const void *data)
138
139 : tiff_tag (code, type, count)
140
141 , fData (data)
142
143 {
144 }
145
146 void SetData (const void *data)
147 {
148 fData = data;
149 }
150
151 virtual void Put (dng_stream &stream) const;
152
153 };
154
155 /******************************************************************************/
156
157 class tag_owned_data_ptr: public tag_data_ptr
158 {
159
160 protected:
161
162 const_dng_memory_block_sptr fBlock;
163
164 public:
165
166 tag_owned_data_ptr (uint16 code,
167 uint16 type,
168 uint32 count,
169 const_dng_memory_block_sptr block)
170
171 : tag_data_ptr (code,
172 type,
173 count,
174 block ? block->Buffer () : nullptr)
175
176 , fBlock (block)
177
178 {
179 }
180
181 void SetBlock (const_dng_memory_block_sptr block)
182 {
183 fBlock = block;
184 SetData (block ? block->Buffer () : nullptr);
185 }
186
187 };
188
189 /******************************************************************************/
190
191 class tag_string: public tiff_tag
192 {
193
194 protected:
195
196 dng_string fString;
197
198 public:
199
200 tag_string (uint16 code,
201 const dng_string &s,
202 bool forceASCII = true);
203
204 virtual void Put (dng_stream &stream) const;
205
206 };
207
208 /******************************************************************************/
209
210 class tag_encoded_text: public tiff_tag
211 {
212
213 private:
214
215 dng_string fText;
216
217 dng_memory_data fUTF16;
218
219 public:
220
221 tag_encoded_text (uint16 code,
222 const dng_string &text);
223
224 virtual void Put (dng_stream &stream) const;
225
226 };
227
228 /******************************************************************************/
229
230 class tag_uint8: public tag_data_ptr
231 {
232
233 private:
234
235 uint8 fValue;
236
237 public:
238
239 tag_uint8 (uint16 code,
240 uint8 value = 0)
241
242 : tag_data_ptr (code, ttByte, 1, &fValue)
243
244 , fValue (value)
245
246 {
247 }
248
249 void Set (uint8 value)
250 {
251 fValue = value;
252 }
253
254 };
255
256 /******************************************************************************/
257
258 class tag_uint8_ptr: public tag_data_ptr
259 {
260
261 public:
262
263 tag_uint8_ptr (uint16 code,
264 const uint8 *data,
265 uint32 count = 1)
266
267 : tag_data_ptr (code, ttByte, count, data)
268
269 {
270 }
271
272 };
273
274 /******************************************************************************/
275
276 class tag_uint16: public tag_data_ptr
277 {
278
279 private:
280
281 uint16 fValue;
282
283 public:
284
285 tag_uint16 (uint16 code,
286 uint16 value = 0)
287
288 : tag_data_ptr (code, ttShort, 1, &fValue)
289
290 , fValue (value)
291
292 {
293 }
294
295 void Set (uint16 value)
296 {
297 fValue = value;
298 }
299
300 uint16 Get () const
301 {
302 return fValue;
303 }
304
305 };
306
307 /******************************************************************************/
308
309 class tag_int16_ptr: public tag_data_ptr
310 {
311
312 public:
313
314 tag_int16_ptr (uint16 code,
315 const int16 *data,
316 uint32 count = 1)
317
318 : tag_data_ptr (code, ttSShort, count, data)
319
320 {
321 }
322
323 };
324
325 /******************************************************************************/
326
327 class tag_uint16_ptr: public tag_data_ptr
328 {
329
330 public:
331
332 tag_uint16_ptr (uint16 code,
333 const uint16 *data,
334 uint32 count = 1)
335
336 : tag_data_ptr (code, ttShort, count, data)
337
338 {
339 }
340
341 };
342
343 /******************************************************************************/
344
345 class tag_uint32: public tag_data_ptr
346 {
347
348 private:
349
350 uint32 fValue;
351
352 public:
353
354 tag_uint32 (uint16 code,
355 uint32 value = 0)
356
357 : tag_data_ptr (code, ttLong, 1, &fValue)
358
359 , fValue (value)
360
361 {
362 }
363
364 void Set (uint32 value)
365 {
366 fValue = value;
367 }
368
369 };
370
371 /******************************************************************************/
372
373 class tag_uint32_ptr: public tag_data_ptr
374 {
375
376 public:
377
378 tag_uint32_ptr (uint16 code,
379 const uint32 *data,
380 uint32 count = 1)
381
382 : tag_data_ptr (code, ttLong, count, data)
383
384 {
385 }
386
387 };
388
389 /******************************************************************************/
390
391 class tag_big_uint: public tiff_tag
392 {
393
394 private:
395
396 uint64 fValue;
397
398 public:
399
400 tag_big_uint (uint16 code,
401 uint64 value = 0)
402
403 : tiff_tag (code, ttLong, 1)
404
405 , fValue (value)
406
407 {
408 }
409
410 void Set (uint64 value)
411 {
412 fValue = value;
413 }
414
415 virtual void SetBigTIFF (bool isBigTIFF)
416 {
417 fType = (uint16)(isBigTIFF ? ttLong8 : ttLong);
418 }
419
420 virtual void Put (dng_stream &stream) const;
421
422 };
423
424 /******************************************************************************/
425
426 class tag_big_uints: public tiff_tag
427 {
428
429 private:
430
431 dng_memory_data fData;
432
433 bool fAlways32;
434
435 public:
436
437 tag_big_uints (uint16 code,
438 uint32 count = 1,
439 bool always32 = false)
440
441 : tiff_tag (code, ttLong, count)
442
443 , fData (count, 8)
444
445 , fAlways32 (always32)
446
447 {
448 }
449
450 void Set (uint32 index,
451 uint64 value)
452 {
453 fData.Buffer_uint64 () [index] = value;
454 }
455
456 virtual void SetBigTIFF (bool isBigTIFF)
457 {
458 fType = (uint16)(isBigTIFF && !fAlways32 ? ttLong8 : ttLong);
459 }
460
461 virtual void Put (dng_stream &stream) const;
462
463 };
464
465 /******************************************************************************/
466
467 class tag_urational: public tag_data_ptr
468 {
469
470 private:
471
472 const dng_urational fValue;
473
474 public:
475
476 tag_urational (uint16 code,
477 const dng_urational &value)
478
479 : tag_data_ptr (code, ttRational, 1, &fValue)
480
481 , fValue (value)
482
483 {
484 }
485
486 };
487
488 /******************************************************************************/
489
490 class tag_urational_ptr: public tag_data_ptr
491 {
492
493 public:
494
495 tag_urational_ptr (uint16 code,
496 const dng_urational *data = NULL,
497 uint32 count = 1)
498
499 : tag_data_ptr (code, ttRational, count, data)
500
501 {
502 }
503
504 };
505
506 /******************************************************************************/
507
508 class tag_srational: public tag_data_ptr
509 {
510
511 private:
512
513 const dng_srational fValue;
514
515 public:
516
517 tag_srational (uint16 code,
518 const dng_srational &value)
519
520 : tag_data_ptr (code, ttSRational, 1, &fValue)
521
522 , fValue (value)
523
524 {
525 }
526
527 };
528
529 /******************************************************************************/
530
531 class tag_srational_ptr: public tag_data_ptr
532 {
533
534 public:
535
536 tag_srational_ptr (uint16 code,
537 const dng_srational *data = NULL,
538 uint32 count = 1)
539
540 : tag_data_ptr (code, ttSRational, count, data)
541
542 {
543 }
544
545 };
546
547 /******************************************************************************/
548
549 class tag_real32: public tag_data_ptr
550 {
551
552 private:
553
554 real32 fValue;
555
556 public:
557
558 tag_real32 (uint16 code,
559 real32 value = 0.0f)
560
561 : tag_data_ptr (code, ttFloat, 1, &fValue)
562
563 , fValue (value)
564
565 {
566 }
567
568 void Set (real32 value)
569 {
570 fValue = value;
571 }
572
573 };
574
575 /******************************************************************************/
576
577 class tag_real64: public tag_data_ptr
578 {
579
580 private:
581
582 real64 fValue;
583
584 public:
585
586 tag_real64 (uint16 code,
587 real64 value = 0.0)
588
589 : tag_data_ptr (code, ttDouble, 1, &fValue)
590
591 , fValue (value)
592
593 {
594 }
595
596 void Set (real64 value)
597 {
598 fValue = value;
599 }
600
601 };
602
603 /******************************************************************************/
604
605 class tag_matrix: public tag_srational_ptr
606 {
607
608 private:
609
610 dng_srational fEntry [kMaxColorPlanes *
611 kMaxColorPlanes];
612
613 public:
614
615 tag_matrix (uint16 code,
616 const dng_matrix &m);
617
618 };
619
620 /******************************************************************************/
621
622 class tag_icc_profile: public tag_data_ptr
623 {
624
625 public:
626
627 tag_icc_profile (const void *profileData, uint32 profileSize);
628
629 };
630
631 /******************************************************************************/
632
633 class tag_cfa_pattern: public tiff_tag
634 {
635
636 private:
637
638 uint32 fRows;
639 uint32 fCols;
640
641 const uint8 *fPattern;
642
643 public:
644
645 tag_cfa_pattern (uint16 code,
646 uint32 rows,
647 uint32 cols,
648 const uint8 *pattern)
649
650 : tiff_tag (code, ttUndefined, 4 + rows * cols)
651
652 , fRows (rows )
653 , fCols (cols )
654 , fPattern (pattern)
655
656 {
657 }
658
659 virtual void Put (dng_stream &stream) const;
660
661 };
662
663 /******************************************************************************/
664
665 class tag_exif_date_time: public tag_data_ptr
666 {
667
668 private:
669
670 char fData [20];
671
672 public:
673
674 tag_exif_date_time (uint16 code,
675 const dng_date_time &dt);
676
677 };
678
679 /******************************************************************************/
680
681 class tag_iptc: public tiff_tag
682 {
683
684 private:
685
686 const void *fData;
687
688 uint32 fLength;
689
690 public:
691
692 tag_iptc (const void *data,
693 uint32 length);
694
695 virtual void Put (dng_stream &stream) const;
696
697 };
698
699 /******************************************************************************/
700
701 class tag_xmp: public tag_uint8_ptr
702 {
703
704 private:
705
706 AutoPtr<dng_memory_block> fBuffer;
707
708 public:
709
710 tag_xmp (const dng_xmp *xmp);
711
712 };
713
714 /******************************************************************************/
715
716 class dng_tiff_directory: private dng_uncopyable
717 {
718
719 private:
720
721 std::vector<tiff_tag *> fTag;
722
723 uint64 fChained;
724
725 bool fBigTIFF;
726
727 public:
728
729 dng_tiff_directory ()
730
731 : fTag ()
732 , fChained (0)
733 , fBigTIFF (false)
734
735 {
736 }
737
738 virtual ~dng_tiff_directory ()
739 {
740 }
741
742 void Add (tiff_tag *tag);
743
744 void SetChained (uint64 offset)
745 {
746 fChained = offset;
747 }
748
749 void SetBigTIFF (bool isBigTIFF);
750
751 uint32 Size () const;
752
753 enum OffsetsBase
754 {
755 offsetsRelativeToStream = 0,
756 offsetsRelativeToExplicitBase = 1,
757 offsetsRelativeToIFD = 2
758 };
759
760 void Put (dng_stream &stream,
761 OffsetsBase offsetsBase = offsetsRelativeToStream,
762 uint64 explicitBase = 0) const;
763
764 };
765
766 /******************************************************************************/
767
768 class dng_basic_tag_set: private dng_uncopyable
769 {
770
771 private:
772
773 tag_uint32 fNewSubFileType;
774
775 tag_uint32 fImageWidth;
776 tag_uint32 fImageLength;
777
778 tag_uint16 fPhotoInterpretation;
779
780 tag_uint16 fFillOrder;
781
782 tag_uint16 fSamplesPerPixel;
783
784 std::vector<uint16> fBitsPerSampleData;
785
786 tag_uint16_ptr fBitsPerSample;
787
788 bool fStrips;
789
790 tag_uint32 fTileWidth;
791 tag_uint32 fTileLength;
792
793 tag_big_uints fTileOffsets;
794
795 tag_big_uints fTileByteCounts;
796
797 tag_uint16 fPlanarConfiguration;
798
799 tag_uint16 fCompression;
800
801 tag_uint16 fPredictor;
802
803 std::vector<uint16> fExtraSamplesData;
804
805 tag_uint16_ptr fExtraSamples;
806
807 std::vector<uint16> fSampleFormatData;
808
809 tag_uint16_ptr fSampleFormat;
810
811 tag_uint16 fRowInterleaveFactor;
812 tag_uint16 fColumnInterleaveFactor;
813
814 uint16 fSubTileBlockSizeData [2];
815
816 tag_uint16_ptr fSubTileBlockSize;
817
818 tag_real32 fJXLDistance;
819 tag_uint32 fJXLEffort;
820 tag_uint32 fJXLDecodeSpeed;
821
822 public:
823
824 dng_basic_tag_set (dng_tiff_directory &directory,
825 const dng_ifd &info);
826
827 virtual ~dng_basic_tag_set ()
828 {
829 }
830
831 void SetTileOffset (uint32 index,
832 uint64 offset)
833 {
834 fTileOffsets.Set (index, offset);
835 }
836
837 void SetTileByteCount (uint32 index,
838 uint64 count)
839 {
840 fTileByteCounts.Set (index, count);
841 }
842
843 bool WritingStrips () const
844 {
845 return fStrips;
846 }
847
848 uint16 Compression () const
849 {
850 return fCompression.Get ();
851 }
852
853 };
854
855 /******************************************************************************/
856
857 class exif_tag_set: private dng_uncopyable
858 {
859
860 protected:
861
862 dng_tiff_directory fExifIFD;
863 dng_tiff_directory fGPSIFD;
864
865 private:
866
867 tag_big_uint fExifLink;
868 tag_big_uint fGPSLink;
869
870 bool fAddedExifLink;
871 bool fAddedGPSLink;
872
873 uint8 fExifVersionData [4];
874
875 tag_data_ptr fExifVersion;
876
877 tag_urational fExposureTime;
878 tag_srational fShutterSpeedValue;
879
880 tag_urational fFNumber;
881 tag_urational fApertureValue;
882
883 tag_srational fBrightnessValue;
884
885 tag_srational fExposureBiasValue;
886
887 tag_urational fMaxApertureValue;
888
889 tag_urational fSubjectDistance;
890
891 tag_urational fFocalLength;
892
893 tag_uint16 fISOSpeedRatings;
894
895 tag_uint16 fSensitivityType;
896 tag_uint32 fStandardOutputSensitivity;
897 tag_uint32 fRecommendedExposureIndex;
898 tag_uint32 fISOSpeed;
899 tag_uint32 fISOSpeedLatitudeyyy;
900 tag_uint32 fISOSpeedLatitudezzz;
901
902 tag_uint16 fFlash;
903
904 tag_uint16 fExposureProgram;
905
906 tag_uint16 fMeteringMode;
907
908 tag_uint16 fLightSource;
909
910 tag_uint16 fSensingMethod;
911
912 tag_uint16 fFocalLength35mm;
913
914 uint8 fFileSourceData;
915 tag_data_ptr fFileSource;
916
917 uint8 fSceneTypeData;
918 tag_data_ptr fSceneType;
919
920 tag_cfa_pattern fCFAPattern;
921
922 tag_uint16 fCustomRendered;
923 tag_uint16 fExposureMode;
924 tag_uint16 fWhiteBalance;
925 tag_uint16 fSceneCaptureType;
926 tag_uint16 fGainControl;
927 tag_uint16 fContrast;
928 tag_uint16 fSaturation;
929 tag_uint16 fSharpness;
930 tag_uint16 fSubjectDistanceRange;
931
932 tag_urational fDigitalZoomRatio;
933
934 tag_urational fExposureIndex;
935
936 tag_uint32 fImageNumber;
937
938 tag_uint16 fSelfTimerMode;
939
940 tag_string fBatteryLevelA;
941 tag_urational fBatteryLevelR;
942
943 tag_uint16 fColorSpace;
944
945 tag_urational fFocalPlaneXResolution;
946 tag_urational fFocalPlaneYResolution;
947
948 tag_uint16 fFocalPlaneResolutionUnit;
949
950 uint16 fSubjectAreaData [4];
951
952 tag_uint16_ptr fSubjectArea;
953
954 dng_urational fLensInfoData [4];
955
956 tag_urational_ptr fLensInfo;
957
958 tag_exif_date_time fDateTime;
959 tag_exif_date_time fDateTimeOriginal;
960 tag_exif_date_time fDateTimeDigitized;
961
962 tag_string fSubsecTime;
963 tag_string fSubsecTimeOriginal;
964 tag_string fSubsecTimeDigitized;
965
966 tag_string fOffsetTime;
967 tag_string fOffsetTimeOriginal;
968 tag_string fOffsetTimeDigitized;
969
970 tag_string fMake;
971 tag_string fModel;
972 tag_string fArtist;
973 tag_string fSoftware;
974 tag_string fCopyright;
975 tag_string fImageDescription;
976
977 tag_string fSerialNumber;
978
979 tag_uint16 fMakerNoteSafety;
980
981 tag_data_ptr fMakerNote;
982
983 tag_encoded_text fUserComment;
984
985 char fImageUniqueIDData [33];
986
987 tag_data_ptr fImageUniqueID;
988
989 // EXIF 2.3 tags.
990
991 tag_string fCameraOwnerName;
992 tag_string fBodySerialNumber;
993 tag_urational_ptr fLensSpecification;
994 tag_string fLensMake;
995 tag_string fLensModel;
996 tag_string fLensSerialNumber;
997
998 // EXIF 2.3.1 tags.
999
1000 tag_srational fTemperature;
1001 tag_urational fHumidity;
1002 tag_urational fPressure;
1003 tag_srational fWaterDepth;
1004 tag_urational fAcceleration;
1005 tag_srational fCameraElevationAngle;
1006
1007 uint8 fGPSVersionData [4];
1008
1009 tag_uint8_ptr fGPSVersionID;
1010
1011 tag_string fGPSLatitudeRef;
1012 tag_urational_ptr fGPSLatitude;
1013
1014 tag_string fGPSLongitudeRef;
1015 tag_urational_ptr fGPSLongitude;
1016
1017 tag_uint8 fGPSAltitudeRef;
1018 tag_urational fGPSAltitude;
1019
1020 tag_urational_ptr fGPSTimeStamp;
1021
1022 tag_string fGPSSatellites;
1023 tag_string fGPSStatus;
1024 tag_string fGPSMeasureMode;
1025
1026 tag_urational fGPSDOP;
1027
1028 tag_string fGPSSpeedRef;
1029 tag_urational fGPSSpeed;
1030
1031 tag_string fGPSTrackRef;
1032 tag_urational fGPSTrack;
1033
1034 tag_string fGPSImgDirectionRef;
1035 tag_urational fGPSImgDirection;
1036
1037 tag_string fGPSMapDatum;
1038
1039 tag_string fGPSDestLatitudeRef;
1040 tag_urational_ptr fGPSDestLatitude;
1041
1042 tag_string fGPSDestLongitudeRef;
1043 tag_urational_ptr fGPSDestLongitude;
1044
1045 tag_string fGPSDestBearingRef;
1046 tag_urational fGPSDestBearing;
1047
1048 tag_string fGPSDestDistanceRef;
1049 tag_urational fGPSDestDistance;
1050
1051 tag_encoded_text fGPSProcessingMethod;
1052 tag_encoded_text fGPSAreaInformation;
1053
1054 tag_string fGPSDateStamp;
1055
1056 tag_uint16 fGPSDifferential;
1057
1058 tag_urational fGPSHPositioningError;
1059
1060 public:
1061
1062 exif_tag_set (dng_tiff_directory &directory,
1063 const dng_exif &exif,
1064 bool makerNoteSafe = false,
1065 const void *makerNoteData = NULL,
1066 uint32 makerNoteLength = 0,
1067 bool insideDNG = false);
1068
1069 void SetBigTIFF (bool isBigTIFF)
1070 {
1071 fExifIFD.SetBigTIFF (isBigTIFF);
1072 fGPSIFD .SetBigTIFF (isBigTIFF);
1073 }
1074
1075 void Locate (uint64 offset)
1076 {
1077 fExifLink.Set (offset);
1078 fGPSLink .Set (offset + fExifIFD.Size ());
1079 }
1080
1081 uint32 Size () const
1082 {
1083 return fExifIFD.Size () +
1084 fGPSIFD .Size ();
1085 }
1086
1087 void Put (dng_stream &stream) const
1088 {
1089 fExifIFD.Put (stream);
1090 fGPSIFD .Put (stream);
1091 }
1092
1093 protected:
1094
1095 void AddLinks (dng_tiff_directory &directory);
1096
1097 };
1098
1099 /******************************************************************************/
1100
1101 class profile_tag_set;
1102
1103 class tiff_dng_extended_color_profile: private dng_tiff_directory
1104 {
1105
1106 protected:
1107
1108 const dng_camera_profile &fProfile;
1109
1110 AutoPtr<profile_tag_set> fProfileTagSet;
1111
1112 tag_string fCameraModelTag;
1113
1114 public:
1115
1116 tiff_dng_extended_color_profile (const dng_camera_profile &profile,
1117 bool includeModelRestriction = true);
1118
1119 virtual ~tiff_dng_extended_color_profile ();
1120
1121 void Put (dng_host &host,
1122 dng_stream &stream);
1123
1124 uint64 DataSize ();
1125
1126 };
1127
1128 /*****************************************************************************/
1129
1130 class tag_dng_noise_profile: public tag_data_ptr
1131 {
1132
1133 protected:
1134
1135 real64 fValues [2 * kMaxColorPlanes];
1136
1137 public:
1138
1139 explicit tag_dng_noise_profile (const dng_noise_profile &profile);
1140
1141 };
1142
1143 /*****************************************************************************/
1144
1145 // Class to control the subset of metadata to save to a file.
1146
1147 class dng_metadata_subset
1148 {
1149
1150 public:
1151
1152 enum
1153 {
1154
1155 kMask_Copyright = 0x00000001,
1156 kMask_Contact = 0x00000002,
1157 kMask_Location = 0x00000004,
1158 kMask_Exif = 0x00000008,
1159 kMask_Keywords = 0x00000010,
1160 kMask_CameraRaw = 0x00000020,
1161 kMask_Rating = 0x00000040,
1162 kMask_Label = 0x00000080,
1163 kMask_Caption = 0x00000100,
1164 kMask_Title = 0x00000200,
1165 kMask_Regions = 0x00000400, // Includes face info
1166 kMask_ExifDate = 0x00000800,
1167
1168 kMask_Other = 0x80000000,
1169
1170 kMask_All = 0xFFFFFFFF
1171
1172 };
1173
1174 private:
1175
1176 uint32 fMask;
1177
1178 public:
1179
1180 dng_metadata_subset (uint32 mask = kMask_All)
1181
1182 : fMask (mask)
1183
1184 {
1185 }
1186
1187 uint32 Mask () const
1188 {
1189 return fMask;
1190 }
1191
1192 bool operator== (const dng_metadata_subset &subset) const
1193 {
1194 return fMask == subset.fMask;
1195 }
1196
1197 bool operator!= (const dng_metadata_subset &subset) const
1198 {
1199 return !(*this == subset);
1200 }
1201
1202 bool Includes (uint32 mask) const
1203 {
1204 return (fMask & mask) == mask;
1205 }
1206
1207 bool Excludes (uint32 mask) const
1208 {
1209 return (fMask & mask) == 0;
1210 }
1211
1212 };
1213
1214 // Metadata subset mask values for legacy API:
1215
1216 enum
1217 {
1218
1219 kMetadataSubset_CopyrightOnly =
1220 dng_metadata_subset::kMask_Copyright,
1221
1222 kMetadataSubset_CopyrightAndContact =
1223 dng_metadata_subset::kMask_Copyright +
1224 dng_metadata_subset::kMask_Contact +
1225 dng_metadata_subset::kMask_Title,
1226
1227 kMetadataSubset_All =
1228 dng_metadata_subset::kMask_All,
1229
1230 kMetadataSubset_AllExceptLocationInfo =
1231 dng_metadata_subset::kMask_All -
1232 dng_metadata_subset::kMask_Location,
1233
1234 kMetadataSubset_AllExceptCameraInfo =
1235 dng_metadata_subset::kMask_All -
1236 dng_metadata_subset::kMask_Exif -
1237 dng_metadata_subset::kMask_CameraRaw,
1238
1239 kMetadataSubset_AllExceptCameraAndLocation =
1240 dng_metadata_subset::kMask_All -
1241 dng_metadata_subset::kMask_Exif -
1242 dng_metadata_subset::kMask_CameraRaw -
1243 dng_metadata_subset::kMask_Location,
1244
1245 kMetadataSubset_AllExceptCameraRawInfo =
1246 dng_metadata_subset::kMask_All -
1247 dng_metadata_subset::kMask_CameraRaw,
1248
1249 kMetadataSubset_AllExceptCameraRawInfoAndLocation =
1250 dng_metadata_subset::kMask_All -
1251 dng_metadata_subset::kMask_CameraRaw -
1252 dng_metadata_subset::kMask_Location,
1253
1254 kMetadataSubset_ExifDate = dng_metadata_subset::kMask_ExifDate
1255
1256 };
1257
1258 /*****************************************************************************/
1259
1260 /// @brief Support for writing dng_image or dng_negative instances to a
1261 /// dng_stream in TIFF or DNG format.
1262
1263 class dng_image_writer
1264 {
1265
1266 friend class dng_compressed_image_encode_task;
1267 friend class dng_write_tiles_task;
1268
1269 protected:
1270
1271 enum
1272 {
1273
1274 // Target size for buffer used to copy data to the image.
1275
1276 kImageBufferSize = 128 * 1024
1277
1278 };
1279
1280 public:
1281
1282 dng_image_writer ();
1283
1284 virtual ~dng_image_writer ();
1285
1286 virtual void EncodeJPEGPreview (dng_host &host,
1287 const dng_image &image,
1288 dng_jpeg_preview &preview,
1289 int32 quality = -1);
1290
1291 virtual void WriteImage (dng_host &host,
1292 const dng_ifd &ifd,
1293 dng_basic_tag_set &basic,
1294 dng_stream &stream,
1295 const dng_image &image,
1296 uint32 fakeChannels = 1,
1297 dng_fingerprint *outDigest = nullptr);
1298
1299 /// Write a dng_image to a dng_stream in TIFF format.
1300 /// \param host Host interface used for progress updates, abort testing, buffer allocation, etc.
1301 /// \param stream The dng_stream on which to write the TIFF.
1302 /// \param image The actual image data to be written.
1303 /// \param photometricInterpretation Either piBlackIsZero for monochrome or piRGB for RGB images.
1304 /// \param compression Must be ccUncompressed.
1305 /// \param metadata If non-NULL, EXIF, IPTC, and XMP metadata from this is written to TIFF.
1306 /// \param space If non-null and color space has an ICC profile, TIFF will be tagged with this
1307 /// profile. No color space conversion of image data occurs.
1308 /// \param resolution If non-NULL, TIFF will be tagged with this resolution.
1309 /// \param thumbnail If non-NULL, will be stored in TIFF as preview image.
1310 /// \param imageResources If non-NULL, will image resources be stored in TIFF as well.
1311 /// \param metadataSubset The subset of metadata (e.g., copyright only) to include in the TIFF.
1312 /// \param hasTransparency Does the image change a transparency channel?
1313 /// \param allowBigTIFF Automatically write BigTIFF format if required?
1314
1315 void WriteTIFF (dng_host &host,
1316 dng_stream &stream,
1317 const dng_image &image,
1318 uint32 photometricInterpretation = piBlackIsZero,
1319 uint32 compression = ccUncompressed,
1320 const dng_metadata *metadata = NULL,
1321 const dng_color_space *space = NULL,
1322 const dng_resolution *resolution = NULL,
1323 const dng_jpeg_preview *thumbnail = NULL,
1324 const dng_memory_block *imageResources = NULL,
1325 dng_metadata_subset metadataSubset = kMetadataSubset_All,
1326 bool hasTransparency = false,
1327 bool allowBigTIFF = true,
1328 const dng_image *gainMapImage = nullptr,
1329 bool useHalfFloat = false);
1330
1331 /// Write a dng_image to a dng_stream in TIFF format.
1332 /// \param host Host interface used for progress updates, abort testing, buffer allocation, etc.
1333 /// \param stream The dng_stream on which to write the TIFF.
1334 /// \param image The actual image data to be written.
1335 /// \param photometricInterpretation Either piBlackIsZero for monochrome or piRGB for RGB images.
1336 /// \param compression Must be ccUncompressed.
1337 /// \param metadata If non-NULL, EXIF, IPTC, and XMP metadata from this is written to TIFF.
1338 /// \param profileData If non-null, TIFF will be tagged with this profile. No color space conversion
1339 /// of image data occurs.
1340 /// \param profileSize The size for the profile data.
1341 /// \param resolution If non-NULL, TIFF will be tagged with this resolution.
1342 /// \param thumbnail If non-NULL, will be stored in TIFF as preview image.
1343 /// \param imageResources If non-NULL, will image resources be stored in TIFF as well.
1344 /// \param metadataSubset The subset of metadata (e.g., copyright only) to include in the TIFF.
1345 /// \param hasTransparency Does the image change a transparency channel?
1346 /// \param allowBigTIFF Automatically write BigTIFF format if required?
1347
1348 virtual void WriteTIFFWithProfile (dng_host &host,
1349 dng_stream &stream,
1350 const dng_image &image,
1351 uint32 photometricInterpretation = piBlackIsZero,
1352 uint32 compression = ccUncompressed,
1353 const dng_metadata *metadata = NULL,
1354 const void *profileData = NULL,
1355 uint32 profileSize = 0,
1356 const dng_resolution *resolution = NULL,
1357 const dng_jpeg_preview *thumbnail = NULL,
1358 const dng_memory_block *imageResources = NULL,
1359 dng_metadata_subset metadataSubset = kMetadataSubset_All,
1360 bool hasTransparency = false,
1361 bool allowBigTIFF = true,
1362 const dng_image *gainMapImage = nullptr,
1363 bool useHalfFloat = false);
1364
1365 /// Write a dng_image to a dng_stream in DNG format.
1366 /// \param host Host interface used for progress updates, abort testing, buffer allocation, etc.
1367 /// \param stream The dng_stream on which to write the TIFF.
1368 /// \param negative The image data and metadata (EXIF, IPTC, XMP) to be written.
1369 /// \param previewList List of previews (not counting thumbnail) to write to the file. Defaults to empty.
1370 /// \param maxBackwardVersion The DNG file should be readable by readers at least back to this version.
1371 /// \param uncompressed True to force uncompressed images. Otherwise use normal compression.
1372 /// \param gainMapImage Optional gain map image.
1373 /// \param gainMapLossyCompressed Optional lossy compressed gain map image.
1374
1375 void WriteDNG (dng_host &host,
1376 dng_stream &stream,
1377 dng_negative &negative,
1378 const dng_preview_list *previewList = NULL,
1379 uint32 maxBackwardVersion = dngVersion_SaveDefault,
1380 bool uncompressed = false,
1381 bool allowBigTIFF = true,
1382 const dng_image *gainMapImage = nullptr,
1383 const dng_lossy_compressed_image *gainMapLossyCompressed = nullptr);
1384
1385 /// Write a dng_image to a dng_stream in DNG format.
1386 /// \param host Host interface used for progress updates, abort testing, buffer allocation, etc.
1387 /// \param stream The dng_stream on which to write the TIFF.
1388 /// \param negative The image data to be written.
1389 /// \param metadata The metadata (EXIF, IPTC, XMP) to be written.
1390 /// \param previewList List of previews (not counting thumbnail) to write to the file. Defaults to empty.
1391 /// \param maxBackwardVersion The DNG file should be readable by readers at least back to this version.
1392 /// \param uncompressed True to force uncompressed images. Otherwise use normal compression.
1393 /// \param gainMapImage Optional gain map image.
1394 /// \param gainMapLossyCompressed Optional lossy compressed gain map image.
1395
1396 virtual void WriteDNGWithMetadata (dng_host &host,
1397 dng_stream &stream,
1398 const dng_negative &negative,
1399 const dng_metadata &metadata,
1400 const dng_preview_list *previewList = NULL,
1401 uint32 maxBackwardVersion = dngVersion_SaveDefault,
1402 bool uncompressed = false,
1403 bool allowBigTIFF = true,
1404 const dng_image *gainMapImage = nullptr,
1405 const dng_lossy_compressed_image *gainMapLossyCompressed = nullptr);
1406
1407 /// Resolve metadata conflicts and apply metadata policies in keeping
1408 /// with Metadata Working Group (MWG) guidelines.
1409
1410 virtual void CleanUpMetadata (dng_host &host,
1411 dng_metadata &metadata,
1412 dng_metadata_subset metadataSubset,
1413 const char *dstMIME,
1414 const char *software = NULL);
1415
1416 protected:
1417
1418 virtual void UpdateExifColorSpaceTag (dng_metadata &metadata,
1419 const void *profileData,
1420 const uint32 profileSize);
1421
1422 virtual uint32 CompressedBufferSize (const dng_ifd &ifd,
1423 uint32 uncompressedSize);
1424
1425 virtual void EncodePredictor (dng_host &host,
1426 const dng_ifd &ifd,
1427 dng_pixel_buffer &buffer,
1428 AutoPtr<dng_memory_block> &tempBuffer);
1429
1430 virtual void ByteSwapBuffer (dng_host &host,
1431 dng_pixel_buffer &buffer);
1432
1433 void ReorderSubTileBlocks (const dng_ifd &ifd,
1434 dng_pixel_buffer &buffer,
1435 AutoPtr<dng_memory_block> &uncompressedBuffer,
1436 AutoPtr<dng_memory_block> &subTileBlockBuffer);
1437
1438 virtual void WriteData (dng_host &host,
1439 const dng_ifd &ifd,
1440 dng_stream &stream,
1441 dng_pixel_buffer &buffer,
1442 AutoPtr<dng_memory_block> &compressedBuffer,
1443 bool usingMultipleThreads);
1444
1445 virtual void WriteTile (dng_host &host,
1446 const dng_ifd &ifd,
1447 dng_stream &stream,
1448 const dng_image &image,
1449 const dng_rect &tileArea,
1450 uint32 fakeChannels,
1451 AutoPtr<dng_memory_block> &compressedBuffer,
1452 AutoPtr<dng_memory_block> &uncompressedBuffer,
1453 AutoPtr<dng_memory_block> &subTileBlockBuffer,
1454 AutoPtr<dng_memory_block> &tempBuffer,
1455 bool usingMultipleThreads);
1456
1457 virtual void DoWriteTiles (dng_host &host,
1458 const dng_ifd &ifd,
1459 dng_basic_tag_set &basic,
1460 dng_stream &stream,
1461 const dng_image &image,
1462 uint32 fakeChannels,
1463 uint32 tilesDown,
1464 uint32 tilesAcross,
1465 uint32 compressedSize,
1466 const dng_safe_uint32 &uncompressedSize,
1467 dng_fingerprint *outDigest);
1468
1469 };
1470
1471 /*****************************************************************************/
1472
1473 class dng_write_tiles_task : public dng_area_task,
1474 private dng_uncopyable
1475 {
1476
1477 protected:
1478
1479 dng_image_writer &fImageWriter;
1480
1481 dng_host &fHost;
1482
1483 const dng_ifd &fIFD;
1484
1485 dng_basic_tag_set &fBasic;
1486
1487 dng_stream &fStream;
1488
1489 const dng_image &fImage;
1490
1491 uint32 fFakeChannels;
1492
1493 uint32 fTilesDown;
1494
1495 uint32 fTilesAcross;
1496
1497 uint32 fCompressedSize;
1498
1499 uint32 fUncompressedSize;
1500
1501 std::atomic_uint fNextTileIndex;
1502
1503 dng_mutex fMutex;
1504
1505 dng_condition fCondition;
1506
1507 bool fTaskFailed;
1508
1509 uint32 fWriteTileIndex;
1510
1511 const bool fNeedDigest;
1512
1513 mutable dng_md5_printer fOverallPrinter;
1514
1515 public:
1516
1517 dng_write_tiles_task (dng_image_writer &imageWriter,
1518 dng_host &host,
1519 const dng_ifd &ifd,
1520 dng_basic_tag_set &basic,
1521 dng_stream &stream,
1522 const dng_image &image,
1523 uint32 fakeChannels,
1524 uint32 tilesDown,
1525 uint32 tilesAcross,
1526 uint32 compressedSize,
1527 uint32 uncompressedSize,
1528 bool needDigest);
1529
1530 void Process (uint32 threadIndex,
1531 const dng_rect &tile,
1532 dng_abort_sniffer *sniffer);
1533
1534 const dng_fingerprint & ResultDigest () const;
1535
1536 protected:
1537
1538 void ProcessTask (uint32 tileIndex,
1539 AutoPtr<dng_memory_block> &compressedBuffer,
1540 AutoPtr<dng_memory_block> &uncompressedBuffer,
1541 AutoPtr<dng_memory_block> &subTileBlockBuffer,
1542 AutoPtr<dng_memory_block> &tempBuffer,
1543 uint32 &tileByteCount, // output
1544 dng_memory_stream &tileStream, // output
1545 dng_abort_sniffer *sniffer);
1546
1547 void WriteTask (uint32 tileIndex,
1548 uint32 tileByteCount,
1549 dng_memory_stream &tileStream,
1550 dng_abort_sniffer *sniffer);
1551
1552 };
1553
1554 /*****************************************************************************/
1555
1556 #endif
1557
1558 /*****************************************************************************/