Debian Codegraph @

core/libs/dngwriter/extra/dng_sdk/dng_xmp_sdk.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_xmp_sdk.h"
10
11 #include "dng_abort_sniffer.h"
12 #include "dng_auto_ptr.h"
13 #include "dng_assertions.h"
14 #include "dng_exceptions.h"
15 #include "dng_flags.h"
16 #include "dng_host.h"
17 #include "dng_local_string.h"
18 #include "dng_memory.h"
19 #include "dng_string.h"
20 #include "dng_string_list.h"
21 #include "dng_utils.h"
22
23 #include <cstdio>
24
25 /*****************************************************************************/
26
27 #if qMacOS
28 #ifndef MAC_ENV
29 #define MAC_ENV 1
30 #endif
31 #endif
32
33 #if qWinOS
34 #ifndef WIN_ENV
35 #define WIN_ENV 1
36 #endif
37 #endif
38
39 #include <new>
40 #include <string>
41
42 #define TXMP_STRING_TYPE std::string
43
44 #define XMP_INCLUDE_XMPFILES qDNGXMPFiles
45
46 #define XMP_StaticBuild 1
47
48 #if qiPhone
49 #undef UNIX_ENV
50 #endif
51
52 #include "XMP.incl_cpp"
53
54 /*****************************************************************************/
55
56 const char *XMP_NS_TIFF = "http://ns.adobe.com/tiff/1.0/";
57 const char *XMP_NS_EXIF = "http://ns.adobe.com/exif/1.0/";
58 const char *XMP_NS_EXIFEX = "http://cipa.jp/exif/1.0/";
59 const char *XMP_NS_PHOTOSHOP = "http://ns.adobe.com/photoshop/1.0/";
60 const char *XMP_NS_XAP = "http://ns.adobe.com/xap/1.0/";
61 const char *XMP_NS_XAP_RIGHTS = "http://ns.adobe.com/xap/1.0/rights/";
62 const char *XMP_NS_DC = "http://purl.org/dc/elements/1.1/";
63 const char *XMP_NS_XMP_NOTE = "http://ns.adobe.com/xmp/note/";
64 const char *XMP_NS_MM = "http://ns.adobe.com/xap/1.0/mm/";
65
66 const char *XMP_NS_CRS = "http://ns.adobe.com/camera-raw-settings/1.0/";
67 const char *XMP_NS_CRSS = "http://ns.adobe.com/camera-raw-saved-settings/1.0/";
68 const char *XMP_NS_CRD = "http://ns.adobe.com/camera-raw-defaults/1.0/";
69 const char *XMP_NS_CRLCP = "http://ns.adobe.com/camera-raw-embedded-lens-profile/1.0/";
70
71 const char *XMP_NS_LR = "http://ns.adobe.com/lightroom/1.0/";
72
73 const char *XMP_NS_LCP = "http://ns.adobe.com/photoshop/1.0/camera-profile";
74
75 const char *XMP_NS_AUX = "http://ns.adobe.com/exif/1.0/aux/";
76
77 const char *XMP_NS_IPTC = "http://iptc.org/std/Iptc4xmpCore/1.0/xmlns/";
78 const char *XMP_NS_IPTC_EXT = "http://iptc.org/std/Iptc4xmpExt/2008-02-29/";
79
80 const char *XMP_NS_CRX = "http://ns.adobe.com/lightroom-settings-experimental/1.0/";
81
82 const char *XMP_NS_DNG = "http://ns.adobe.com/dng/1.0/";
83
84 const char *XMP_NS_PANO = "http://ns.adobe.com/photoshop/1.0/panorama-profile";
85 const char *XMP_NS_GPANO = "http://ns.google.com/photos/1.0/panorama/";
86
87 const char *XMP_NS_REGIONS = "http://www.metadataworkinggroup.com/schemas/regions/";
88
89 const char *XMP_NS_HDRGM = "http://ns.adobe.com/hdr-gain-map/1.0/";
90 const char *XMP_NS_HDR_META = "http://ns.adobe.com/hdr-metadata/1.0/";
91
92 const char *XMP_NS_APPLE_HDRGM = "http://ns.apple.com/HDRGainMap/1.0/";
93 const char *XMP_NS_APPLE_PIXELDATA = "http://ns.apple.com/pixeldatainfo/1.0/";
94
95 /******************************************************************************/
96
97 #define CATCH_XMP_ALT(routine, fatal, silent)\
98 \
99 catch (std::bad_alloc &)\
100 {\
101 DNG_REPORT ("Info: XMP " routine " threw memory exception");\
102 ThrowMemoryFull ();\
103 }\
104 \
105 catch (XMP_Error &error)\
106 {\
107 const char *errMessage = error.GetErrMsg ();\
108 if (errMessage && strlen (errMessage) <= 128)\
109 {\
110 if (!silent)\
111 {\
112 char errBuffer [256];\
113 snprintf (errBuffer, 256, "Info: XMP " routine " threw '%s' exception", errMessage); \
114 DNG_REPORT (errBuffer);\
115 }\
116 }\
117 else\
118 {\
119 DNG_REPORT ("Info: XMP " routine " threw unnamed exception");\
120 }\
121 if (fatal) ThrowProgramError ();\
122 }\
123 \
124 catch (...)\
125 {\
126 DNG_REPORT ("Info: XMP " routine " threw unknown exception");\
127 if (fatal) ThrowProgramError ();\
128 }
129
130 #define CATCH_XMP(routine, fatal) CATCH_XMP_ALT(routine, fatal, false)
131
132 /*****************************************************************************/
133
134 class dng_xmp_private
135 {
136
137 public:
138
139 SXMPMeta *fMeta;
140
141 dng_xmp_private ()
142 : fMeta (NULL)
143 {
144 }
145
146 dng_xmp_private (const dng_xmp_private &xmp);
147
148 ~dng_xmp_private ()
149 {
150 if (fMeta)
151 {
152 delete fMeta;
153 }
154 }
155
156 private:
157
158 // Hidden assignment operator.
159
160 dng_xmp_private & operator= (const dng_xmp_private &xmp);
161
162 };
163
164 /*****************************************************************************/
165
166 dng_xmp_private::dng_xmp_private (const dng_xmp_private &xmp)
167
168 : fMeta (NULL)
169
170 {
171
172 if (xmp.fMeta)
173 {
174
175 fMeta = new SXMPMeta (xmp.fMeta->Clone (0));
176
177 if (!fMeta)
178 {
179 ThrowMemoryFull ();
180 }
181
182 }
183
184 }
185
186 /*****************************************************************************/
187
188 dng_xmp_sdk::dng_xmp_sdk ()
189
190 : fPrivate (NULL)
191
192 {
193
194 fPrivate = new dng_xmp_private;
195
196 if (!fPrivate)
197 {
198 ThrowMemoryFull ();
199 }
200
201 }
202
203 /*****************************************************************************/
204
205 dng_xmp_sdk::dng_xmp_sdk (const dng_xmp_sdk &sdk)
206
207 : fPrivate (NULL)
208
209 {
210
211 fPrivate = new dng_xmp_private (*sdk.fPrivate);
212
213 if (!fPrivate)
214 {
215 ThrowMemoryFull ();
216 }
217
218 }
219
220 /*****************************************************************************/
221
222 dng_xmp_sdk::~dng_xmp_sdk ()
223 {
224
225 if (fPrivate)
226 {
227 delete fPrivate;
228 }
229
230 }
231
232 /*****************************************************************************/
233
234 static bool gInitializedXMP = false;
235
236 /*****************************************************************************/
237
238 void dng_xmp_sdk::InitializeSDK (dng_xmp_namespace * extraNamespaces,
239 const char *software)
240 {
241
242 if (!gInitializedXMP)
243 {
244
245 try
246 {
247
248 if (!SXMPMeta::Initialize ())
249 {
250 ThrowProgramError ();
251 }
252
253 // Register Lightroom beta settings namespace.
254 // We no longer read this, but we do need to register
255 // it so we can clean up this namespace when saving
256 // new settings.
257
258 {
259
260 TXMP_STRING_TYPE ss ("");
261
262 SXMPMeta::RegisterNamespace (XMP_NS_CRX,
263 "crx",
264 &ss);
265
266 }
267
268 // Register CRSS snapshots namespace
269
270 {
271
272 TXMP_STRING_TYPE ss ("");
273
274 SXMPMeta::RegisterNamespace (XMP_NS_CRSS,
275 "crss",
276 &ss);
277
278 }
279
280 // Register CRD defaults namespace
281
282 {
283
284 TXMP_STRING_TYPE ss ("");
285
286 SXMPMeta::RegisterNamespace (XMP_NS_CRD,
287 "crd",
288 &ss);
289
290 }
291
292 // Register CRLCP embedded lens profile namespace
293
294 {
295
296 TXMP_STRING_TYPE ss ("");
297
298 SXMPMeta::RegisterNamespace (XMP_NS_CRLCP,
299 "crlcp",
300 &ss);
301
302 }
303
304 // Register LR namespace
305
306 {
307
308 TXMP_STRING_TYPE ss ("");
309
310 SXMPMeta::RegisterNamespace (XMP_NS_LR,
311 "lr",
312 &ss);
313
314 }
315
316 // Register LCP (lens correction profiles) namespace
317
318 {
319
320 TXMP_STRING_TYPE ss ("");
321
322 SXMPMeta::RegisterNamespace (XMP_NS_LCP,
323 "stCamera",
324 &ss);
325
326 }
327
328 // Register DNG format metadata namespace
329
330 {
331
332 TXMP_STRING_TYPE ss ("");
333
334 SXMPMeta::RegisterNamespace (XMP_NS_DNG,
335 "dng",
336 &ss);
337
338 }
339
340 // Register Adobe panorama metadata namespace
341
342 {
343
344 TXMP_STRING_TYPE ss ("");
345
346 SXMPMeta::RegisterNamespace (XMP_NS_PANO,
347 "panorama",
348 &ss);
349
350 }
351
352 // Register Google panorama metadata namespace
353
354 {
355
356 TXMP_STRING_TYPE ss ("");
357
358 SXMPMeta::RegisterNamespace (XMP_NS_GPANO,
359 "GPano",
360 &ss);
361
362 }
363
364 // Register Metadata Working Group Regions namespace
365
366 {
367
368 TXMP_STRING_TYPE ss ("");
369
370 SXMPMeta::RegisterNamespace (XMP_NS_REGIONS,
371 "mwg-rs",
372 &ss);
373
374 }
375
376 // Register HDR Gain Map namespace
377
378 {
379
380 TXMP_STRING_TYPE ss ("");
381
382 SXMPMeta::RegisterNamespace (XMP_NS_HDRGM,
383 "hdrgm",
384 &ss);
385
386 }
387
388 // Register Apple HDR Gain Map namespace
389
390 {
391
392 TXMP_STRING_TYPE ss ("");
393
394 SXMPMeta::RegisterNamespace (XMP_NS_APPLE_HDRGM,
395 "HDRGainMap",
396 &ss);
397
398 }
399
400 // Register Apple Pixel Data namespace
401
402 {
403
404 TXMP_STRING_TYPE ss ("");
405
406 SXMPMeta::RegisterNamespace (XMP_NS_APPLE_PIXELDATA,
407 "apdi",
408 &ss);
409
410 }
411
412 // Register HDR Metadata namespace
413
414 {
415
416 TXMP_STRING_TYPE ss ("");
417
418 SXMPMeta::RegisterNamespace (XMP_NS_HDR_META,
419 "hdr_metadata",
420 &ss);
421
422 }
423
424 // Register extra namespaces.
425
426 if (extraNamespaces != NULL)
427 {
428
429 for (; extraNamespaces->fullName != NULL; ++extraNamespaces)
430 {
431
432 TXMP_STRING_TYPE ss ("");
433
434 SXMPMeta::RegisterNamespace (extraNamespaces->fullName,
435 extraNamespaces->shortName,
436 &ss);
437
438 }
439
440 }
441
442 #if qDNGXMPFiles
443
444 #if qLinux || qAndroid || qWeb
445 if (!SXMPFiles::Initialize (kXMPFiles_IgnoreLocalText))
446 #else
447 if (!SXMPFiles::Initialize ())
448 #endif
449 {
450 ThrowProgramError ();
451 }
452
453 #endif
454
455 #if qDNGXMPDocOps
456
457 if (software)
458 {
459
460 SXMPDocOps::SetAppName (software);
461
462 }
463
464 #else
465
466 (void) software;
467
468 #endif
469
470 }
471
472 CATCH_XMP ("Initialization", true)
473
474 gInitializedXMP = true;
475
476 }
477
478 }
479
480 /******************************************************************************/
481
482 void dng_xmp_sdk::TerminateSDK ()
483 {
484
485 if (gInitializedXMP)
486 {
487
488 try
489 {
490
491 #if qDNGXMPFiles
492
493 SXMPFiles::Terminate ();
494
495 #endif
496
497 SXMPMeta::Terminate ();
498
499 }
500
501 catch (...)
502 {
503
504 }
505
506 gInitializedXMP = false;
507
508 }
509
510 }
511
512 /******************************************************************************/
513
514 bool dng_xmp_sdk::HasMeta () const
515 {
516
517 if (fPrivate->fMeta)
518 {
519
520 return true;
521
522 }
523
524 return false;
525
526 }
527
528 /******************************************************************************/
529
530 void dng_xmp_sdk::ClearMeta ()
531 {
532
533 if (HasMeta ())
534 {
535
536 delete fPrivate->fMeta;
537
538 fPrivate->fMeta = NULL;
539
540 }
541
542 }
543
544 /******************************************************************************/
545
546 void dng_xmp_sdk::MakeMeta ()
547 {
548
549 ClearMeta ();
550
551 InitializeSDK ();
552
553 try
554 {
555
556 fPrivate->fMeta = new SXMPMeta;
557
558 if (!fPrivate->fMeta)
559 {
560
561 ThrowMemoryFull ();
562
563 }
564
565 }
566
567 CATCH_XMP ("MakeMeta", true)
568
569 }
570
571 /******************************************************************************/
572
573 void dng_xmp_sdk::NeedMeta ()
574 {
575
576 if (!HasMeta ())
577 {
578
579 MakeMeta ();
580
581 }
582
583 }
584
585 /******************************************************************************/
586
587 void * dng_xmp_sdk::GetPrivateMeta ()
588 {
589
590 NeedMeta ();
591
592 return (void *) fPrivate->fMeta;
593
594 }
595
596 /******************************************************************************/
597
598 void dng_xmp_sdk::Parse (dng_host &host,
599 const char *buffer,
600 uint32 count)
601 {
602
603 MakeMeta ();
604
605 try
606 {
607
608 try
609 {
610
611 fPrivate->fMeta->ParseFromBuffer (buffer, count);
612
613 }
614
615 CATCH_XMP ("ParseFromBuffer", true)
616
617 }
618
619 catch (dng_exception &except)
620 {
621
622 ClearMeta ();
623
624 if (host.IsTransientError (except.ErrorCode ()))
625 {
626
627 throw;
628
629 }
630
631 ThrowBadFormat ();
632
633 }
634
635 }
636
637 /*****************************************************************************/
638
639 void dng_xmp_sdk::AppendArrayItem (const char *ns,
640 const char *arrayName,
641 const char *itemValue,
642 bool isBag,
643 bool propIsStruct)
644 {
645
646 NeedMeta();
647
648 try
649 {
650
651 fPrivate->fMeta->AppendArrayItem (ns,
652 arrayName,
653 isBag ? kXMP_PropValueIsArray
654 : kXMP_PropArrayIsOrdered,
655 itemValue,
656 propIsStruct ? kXMP_PropValueIsStruct
657 : 0);
658
659 }
660 CATCH_XMP ("AppendArrayItem", true )
661
662 }
663
664 /*****************************************************************************/
665
666 int32 dng_xmp_sdk::CountArrayItems (const char *ns,
667 const char *path) const
668 {
669
670 if (HasMeta ())
671 {
672
673 try
674 {
675
676 return fPrivate->fMeta->CountArrayItems (ns, path);
677
678 }
679
680 CATCH_XMP ("CountArrayItems", false)
681
682 }
683
684 return 0;
685
686 }
687
688 /*****************************************************************************/
689
690 bool dng_xmp_sdk::Exists (const char *ns,
691 const char *path) const
692 {
693
694 if (HasMeta ())
695 {
696
697 try
698 {
699
700 return fPrivate->fMeta->DoesPropertyExist (ns, path);
701
702 }
703
704 catch (...)
705 {
706
707 // Does not exist...
708
709 }
710
711 }
712
713 return false;
714
715 }
716
717 /*****************************************************************************/
718
719 bool dng_xmp_sdk::HasNameSpace (const char *ns) const
720 {
721
722 bool result = false;
723
724 if (HasMeta ())
725 {
726
727 try
728 {
729
730 SXMPIterator iter (*fPrivate->fMeta, ns);
731
732 TXMP_STRING_TYPE nsTemp ("");
733 TXMP_STRING_TYPE prop ("");
734
735 if (iter.Next (&nsTemp,
736 &prop,
737 NULL,
738 NULL))
739 {
740
741 result = true;
742
743 }
744
745 }
746
747 CATCH_XMP ("HasNameSpace", true)
748
749 }
750
751 return result;
752
753 }
754
755 /*****************************************************************************/
756
757 void dng_xmp_sdk::Remove (const char *ns,
758 const char *path)
759 {
760
761 if (HasMeta ())
762 {
763
764 try
765 {
766
767 fPrivate->fMeta->DeleteProperty (ns, path);
768
769 }
770
771 CATCH_XMP ("DeleteProperty", false)
772
773 }
774
775 }
776
777 /*****************************************************************************/
778
779 void dng_xmp_sdk::RemoveProperties (const char *ns)
780 {
781
782 if (HasMeta ())
783 {
784
785 try
786 {
787
788 SXMPUtils::RemoveProperties (fPrivate->fMeta,
789 ns,
790 NULL,
791 kXMPUtil_DoAllProperties);
792
793 }
794
795 catch (...)
796 {
797
798 }
799
800 }
801
802 }
803
804 /*****************************************************************************/
805
806 bool dng_xmp_sdk::IsEmptyString (const char *ns,
807 const char *path)
808 {
809
810 if (HasMeta ())
811 {
812
813 try
814 {
815
816 TXMP_STRING_TYPE ss ("");
817
818 XMP_OptionBits options = 0;
819
820 if (fPrivate->fMeta->GetProperty (ns,
821 path,
822 &ss,
823 &options))
824 {
825
826 // Item must be simple.
827
828 if (XMP_PropIsSimple (options))
829 {
830
831 // Check for null strings.
832
833 return (ss.c_str () == 0 ||
834 ss.c_str () [0] == 0);
835
836 }
837
838 }
839
840 }
841
842 CATCH_XMP ("IsEmptyString", false)
843
844 }
845
846 return false;
847
848 }
849
850 /*****************************************************************************/
851
852 bool dng_xmp_sdk::IsEmptyArray (const char *ns,
853 const char *path)
854 {
855
856 if (HasMeta ())
857 {
858
859 try
860 {
861
862 TXMP_STRING_TYPE ss ("");
863
864 XMP_OptionBits options = 0;
865
866 if (fPrivate->fMeta->GetProperty (ns,
867 path,
868 &ss,
869 &options))
870 {
871
872 if (XMP_PropIsArray (options))
873 {
874
875 if (fPrivate->fMeta->GetArrayItem (ns,
876 path,
877 1,
878 &ss,
879 &options))
880 {
881
882 // If the first item is a null string...
883
884 if (XMP_PropIsSimple (options))
885 {
886
887 if ((ss.c_str () == 0 ||
888 ss.c_str () [0] == 0))
889 {
890
891 // And there is no second item.
892
893 if (!fPrivate->fMeta->GetArrayItem (ns,
894 path,
895 2,
896 &ss,
897 &options))
898 {
899
900 // Then we have an empty array.
901
902 return true;
903
904 }
905
906 }
907
908 }
909
910 }
911
912 else
913 {
914
915 // Unable to get first item, so array is empty.
916
917 return true;
918
919 }
920
921 }
922
923 }
924
925 }
926
927 CATCH_XMP ("IsEmptyArray", false)
928
929 }
930
931 return false;
932
933 }
934
935 /*****************************************************************************/
936
937 void dng_xmp_sdk::ComposeArrayItemPath (const char *ns,
938 const char *arrayName,
939 int32 index,
940 dng_string &s) const
941 {
942
943 try
944 {
945
946 std::string ss ("");
947
948 SXMPUtils::ComposeArrayItemPath (ns, arrayName, index, &ss);
949
950 s.Set (ss.c_str ());
951
952 return;
953
954 }
955
956 CATCH_XMP ("ComposeArrayItemPath", true)
957
958 }
959
960 /*****************************************************************************/
961
962 void dng_xmp_sdk::ComposeStructFieldPath (const char *ns,
963 const char *structName,
964 const char *fieldNS,
965 const char *fieldName,
966 dng_string &s) const
967 {
968
969 try
970 {
971
972 std::string ss ("");
973
974 SXMPUtils::ComposeStructFieldPath (ns,
975 structName,
976 fieldNS,
977 fieldName,
978 &ss);
979
980 s.Set (ss.c_str ());
981
982 return;
983
984 }
985
986 CATCH_XMP ("ComposeStructFieldPath", true)
987
988 }
989
990 /*****************************************************************************/
991
992 bool dng_xmp_sdk::GetNamespacePrefix (const char *uri,
993 dng_string &s) const
994 {
995
996 bool result = false;
997
998 if (HasMeta ())
999 {
1000
1001 try
1002 {
1003
1004 std::string ss ("");
1005
1006 fPrivate->fMeta->GetNamespacePrefix (uri, &ss);
1007
1008 s.Set (ss.c_str ());
1009
1010 result = true;
1011
1012 }
1013
1014 CATCH_XMP ("GetNamespacePrefix", false)
1015
1016 }
1017
1018 return result;
1019
1020 }
1021
1022 /*****************************************************************************/
1023
1024 bool dng_xmp_sdk::GetString (const char *ns,
1025 const char *path,
1026 dng_string &s) const
1027 {
1028
1029 bool result = false;
1030
1031 if (HasMeta ())
1032 {
1033
1034 try
1035 {
1036
1037 TXMP_STRING_TYPE ss ("");
1038
1039 if (fPrivate->fMeta->GetProperty (ns, path, &ss, NULL))
1040 {
1041
1042 s.Set (ss.c_str ());
1043
1044 result = true;
1045
1046 }
1047
1048 }
1049
1050 CATCH_XMP ("GetProperty", false)
1051
1052 }
1053
1054 return result;
1055
1056 }
1057
1058 /*****************************************************************************/
1059
1060 void dng_xmp_sdk::ValidateStringList (const char *ns,
1061 const char *path)
1062 {
1063
1064 if (Exists (ns, path))
1065 {
1066
1067 bool bogus = true;
1068
1069 try
1070 {
1071
1072 XMP_Index index = 1;
1073
1074 TXMP_STRING_TYPE ss ("");
1075
1076 while (fPrivate->fMeta->GetArrayItem (ns,
1077 path,
1078 index++,
1079 &ss,
1080 NULL))
1081 {
1082
1083 }
1084
1085 bogus = false;
1086
1087 }
1088
1089 catch (...)
1090 {
1091
1092 // Array is probably bogus. Don't need to report.
1093
1094 }
1095
1096 if (bogus)
1097 {
1098
1099 Remove (ns, path);
1100
1101 }
1102
1103 }
1104
1105 }
1106
1107 /*****************************************************************************/
1108
1109 bool dng_xmp_sdk::GetStringList (const char *ns,
1110 const char *path,
1111 dng_string_list &list,
1112 dng_abort_sniffer *sniffer) const
1113 {
1114
1115 bool result = false;
1116
1117 if (HasMeta ())
1118 {
1119
1120 try
1121 {
1122
1123 XMP_Index index = 1;
1124
1125 TXMP_STRING_TYPE ss ("");
1126
1127 while (fPrivate->fMeta->GetArrayItem (ns,
1128 path,
1129 index++,
1130 &ss,
1131 NULL))
1132 {
1133
1134 if ((list.Count () & 0x3FF) == 0)
1135 {
1136 dng_abort_sniffer::SniffForAbort (sniffer);
1137 }
1138
1139 dng_string s;
1140
1141 s.Set (ss.c_str ());
1142
1143 list.Append (s);
1144
1145 result = true;
1146
1147 }
1148
1149 }
1150
1151 CATCH_XMP ("GetStringList", false)
1152
1153 }
1154
1155 return result;
1156
1157 }
1158
1159 /*****************************************************************************/
1160
1161 bool dng_xmp_sdk::GetAltLangDefault (const char *ns,
1162 const char *path,
1163 dng_string &s,
1164 bool silent) const
1165 {
1166
1167 bool result = false;
1168
1169 if (HasMeta ())
1170 {
1171
1172 try
1173 {
1174
1175 TXMP_STRING_TYPE ss ("");
1176
1177 if (fPrivate->fMeta->GetLocalizedText (ns,
1178 path,
1179 "x-default",
1180 "x-default",
1181 NULL,
1182 &ss,
1183 NULL))
1184 {
1185
1186 s.Set (ss.c_str ());
1187
1188 result = true;
1189
1190 }
1191
1192 // Special Case: treat the following two representation equivalently.
1193 // The first is an empty alt lang array; the second is an array with
1194 // an empty item. It seems that xmp lib could be generating both under
1195 // some circumstances!
1196 //
1197 // <dc:description>
1198 // <rdf:Alt/>
1199 // </dc:description>
1200 //
1201 // and
1202 //
1203 // <dc:description>
1204 // <rdf:Alt>
1205 // <rdf:li xml:lang="x-default"/>
1206 // </rdf:Alt>
1207 // </dc:description>
1208
1209 else if (fPrivate->fMeta->GetProperty (ns,
1210 path,
1211 &ss,
1212 NULL))
1213 {
1214
1215 if (ss.empty ())
1216 {
1217
1218 s.Clear ();
1219
1220 result = true;
1221
1222 }
1223
1224 }
1225
1226 }
1227
1228 CATCH_XMP_ALT ("GetLocalizedText", false, silent)
1229
1230 }
1231
1232 return result;
1233
1234 }
1235
1236 /*****************************************************************************/
1237
1238 bool dng_xmp_sdk::GetLocalString (const char *ns,
1239 const char *path,
1240 dng_local_string &s) const
1241 {
1242
1243 dng_string defaultText;
1244
1245 if (GetAltLangDefault (ns, path, defaultText, true))
1246 {
1247
1248 s.SetDefaultText (defaultText);
1249
1250 try
1251 {
1252
1253 int32 count = CountArrayItems (ns, path);
1254
1255 if (count > 1)
1256 {
1257
1258 for (int32 index = 1; index <= count; index++)
1259 {
1260
1261 dng_string arrayItemPath;
1262
1263 ComposeArrayItemPath (ns,
1264 path,
1265 index + 1,
1266 arrayItemPath);
1267
1268 TXMP_STRING_TYPE langS ("");
1269
1270 if (fPrivate->fMeta->GetQualifier (ns,
1271 arrayItemPath.Get (),
1272 kXMP_NS_XML,
1273 "lang",
1274 &langS,
1275 NULL))
1276 {
1277
1278 dng_string language;
1279
1280 language.Set (langS.c_str ());
1281
1282 if (language.IsEmpty () ||
1283 language.Matches ("x-default"))
1284 {
1285 continue;
1286 }
1287
1288 TXMP_STRING_TYPE tranS ("");
1289
1290 if (fPrivate->fMeta->GetProperty (ns,
1291 arrayItemPath.Get (),
1292 &tranS,
1293 NULL))
1294 {
1295
1296 dng_string translation;
1297
1298 translation.Set (tranS.c_str ());
1299
1300 s.AddTranslation (language,
1301 translation);
1302
1303 }
1304
1305 }
1306
1307 }
1308
1309 }
1310
1311 }
1312
1313 CATCH_XMP ("GetLocalString", false)
1314
1315 return true;
1316
1317 }
1318
1319 return false;
1320
1321 }
1322
1323 /*****************************************************************************/
1324
1325 bool dng_xmp_sdk::GetStructField (const char *ns,
1326 const char *path,
1327 const char *fieldNS,
1328 const char *fieldName,
1329 dng_string &s) const
1330 {
1331
1332 bool result = false;
1333
1334 if (HasMeta ())
1335 {
1336
1337 try
1338 {
1339
1340 TXMP_STRING_TYPE ss ("");
1341
1342 if (fPrivate->fMeta->GetStructField (ns,
1343 path,
1344 fieldNS,
1345 fieldName,
1346 &ss,
1347 NULL))
1348 {
1349
1350 s.Set (ss.c_str ());
1351
1352 result = true;
1353
1354 }
1355
1356 }
1357
1358 CATCH_XMP ("GetStructField", false)
1359
1360 }
1361
1362 return result;
1363
1364 }
1365
1366 /*****************************************************************************/
1367
1368 void dng_xmp_sdk::Set (const char *ns,
1369 const char *path,
1370 const char *text)
1371 {
1372
1373 NeedMeta ();
1374
1375 try
1376 {
1377
1378 fPrivate->fMeta->SetProperty (ns, path, text);
1379
1380 return;
1381
1382 }
1383
1384 catch (...)
1385 {
1386
1387 // Failed for some reason.
1388
1389 }
1390
1391 // Remove existing value and try again.
1392
1393 Remove (ns, path);
1394
1395 try
1396 {
1397
1398 fPrivate->fMeta->SetProperty (ns, path, text);
1399
1400 }
1401
1402 CATCH_XMP ("SetProperty", true)
1403
1404 }
1405
1406 /*****************************************************************************/
1407
1408 void dng_xmp_sdk::SetString (const char *ns,
1409 const char *path,
1410 const dng_string &s)
1411 {
1412
1413 dng_string ss (s);
1414
1415 ss.SetLineEndings ('\n');
1416
1417 ss.StripLowASCII ();
1418
1419 Set (ns, path, ss.Get ());
1420
1421 }
1422
1423 /*****************************************************************************/
1424
1425 void dng_xmp_sdk::SetStringList (const char *ns,
1426 const char *path,
1427 const dng_string_list &list,
1428 bool isBag)
1429 {
1430
1431 // Remove any existing structure.
1432
1433 Remove (ns, path);
1434
1435 // If list is not empty, add the items.
1436
1437 if (list.Count ())
1438 {
1439
1440 NeedMeta ();
1441
1442 for (uint32 index = 0; index < list.Count (); index++)
1443 {
1444
1445 dng_string s (list [index]);
1446
1447 s.SetLineEndings ('\n');
1448
1449 s.StripLowASCII ();
1450
1451 try
1452 {
1453
1454 fPrivate->fMeta->AppendArrayItem (ns,
1455 path,
1456 isBag ? kXMP_PropValueIsArray
1457 : kXMP_PropArrayIsOrdered,
1458 s.Get ());
1459
1460 }
1461
1462 CATCH_XMP ("AppendArrayItem", true)
1463
1464 }
1465
1466 }
1467
1468 }
1469
1470 /*****************************************************************************/
1471
1472 void dng_xmp_sdk::SetAltLangDefault (const char *ns,
1473 const char *path,
1474 const dng_string &s)
1475 {
1476
1477 NeedMeta ();
1478
1479 Remove (ns, path);
1480
1481 dng_string ss (s);
1482
1483 ss.SetLineEndings ('\n');
1484
1485 ss.StripLowASCII ();
1486
1487 try
1488 {
1489
1490 fPrivate->fMeta->SetLocalizedText (ns,
1491 path,
1492 "x-default",
1493 "x-default",
1494 ss.Get ());
1495
1496 }
1497
1498 CATCH_XMP ("SetLocalizedText", true)
1499
1500 }
1501
1502 /*****************************************************************************/
1503
1504 void dng_xmp_sdk::SetLocalString (const char *ns,
1505 const char *path,
1506 const dng_local_string &s)
1507 {
1508
1509 SetAltLangDefault (ns, path, s.DefaultText ());
1510
1511 try
1512 {
1513
1514 for (uint32 index = 0; index < s.TranslationCount (); index++)
1515 {
1516
1517 dng_string arrayItemPath;
1518
1519 ComposeArrayItemPath (ns,
1520 path,
1521 index + 2,
1522 arrayItemPath);
1523
1524 fPrivate->fMeta->SetProperty (ns,
1525 arrayItemPath.Get (),
1526 s.Translation (index).Get ());
1527
1528 fPrivate->fMeta->SetQualifier (ns,
1529 arrayItemPath.Get (),
1530 kXMP_NS_XML,
1531 "lang",
1532 s.Language (index).Get (),
1533 0);
1534
1535 }
1536
1537 }
1538
1539 CATCH_XMP ("SetLocalizedText", true)
1540
1541 }
1542
1543 /*****************************************************************************/
1544
1545 void dng_xmp_sdk::SetStructField (const char *ns,
1546 const char *path,
1547 const char *fieldNS,
1548 const char *fieldName,
1549 const char *text)
1550 {
1551
1552 NeedMeta ();
1553
1554 try
1555 {
1556
1557 fPrivate->fMeta->SetStructField (ns,
1558 path,
1559 fieldNS,
1560 fieldName,
1561 text);
1562
1563 }
1564
1565 CATCH_XMP ("SetStructField", true)
1566
1567 }
1568
1569 /*****************************************************************************/
1570
1571 void dng_xmp_sdk::DeleteStructField (const char *ns,
1572 const char *structName,
1573 const char *fieldNS,
1574 const char *fieldName)
1575 {
1576
1577 if (HasMeta ())
1578 {
1579
1580 try
1581 {
1582
1583 fPrivate->fMeta->DeleteStructField (ns, structName, fieldNS, fieldName);
1584
1585 }
1586
1587 catch (...)
1588 {
1589
1590 }
1591
1592 }
1593
1594 }
1595
1596 /*****************************************************************************/
1597
1598 dng_memory_block * dng_xmp_sdk::Serialize (dng_memory_allocator &allocator,
1599 bool asPacket,
1600 uint32 targetBytes,
1601 uint32 padBytes,
1602 bool forJPEG,
1603 bool compact) const
1604 {
1605
1606 // The largest XMP packet you can embed in JPEG using normal methods:
1607
1608 const uint32 kJPEG_XMP_Limit = 65504;
1609
1610 if (HasMeta ())
1611 {
1612
1613 TXMP_STRING_TYPE s ("");
1614
1615 bool havePacket = false;
1616
1617 // Note that the XMP lib is changing its default to compact format
1618 // in the future, so the following line will need to change.
1619
1620 uint32 formatOption = compact ? kXMP_UseCompactFormat : 0;
1621
1622 if (asPacket && targetBytes)
1623 {
1624
1625 try
1626 {
1627
1628 fPrivate->fMeta->SerializeToBuffer (&s,
1629 formatOption | kXMP_ExactPacketLength,
1630 targetBytes,
1631 "",
1632 " ");
1633
1634 havePacket = true;
1635
1636 }
1637
1638 catch (...)
1639 {
1640
1641 // Most likely the packet cannot fit in the target
1642 // byte count. So try again without the limit.
1643
1644 }
1645
1646 }
1647
1648 if (!havePacket)
1649 {
1650
1651 try
1652 {
1653
1654 fPrivate->fMeta->SerializeToBuffer (&s,
1655 formatOption |
1656 (asPacket ? 0
1657 : kXMP_OmitPacketWrapper),
1658 (asPacket ? padBytes
1659 : 0),
1660 "",
1661 " ");
1662
1663 }
1664
1665 CATCH_XMP ("SerializeToBuffer", true)
1666
1667 }
1668
1669 uint32 packetLen = (uint32) s.size ();
1670
1671 if (forJPEG && asPacket && padBytes > 0 && targetBytes <= kJPEG_XMP_Limit &&
1672 packetLen > kJPEG_XMP_Limit)
1673 {
1674
1675 uint32 overLimitCount = packetLen - kJPEG_XMP_Limit;
1676
1677 if (overLimitCount > padBytes)
1678 {
1679 padBytes = 0;
1680 }
1681 else
1682 {
1683 padBytes -= overLimitCount;
1684 }
1685
1686 try
1687 {
1688
1689 fPrivate->fMeta->SerializeToBuffer (&s,
1690 formatOption,
1691 padBytes,
1692 "",
1693 " ");
1694
1695 }
1696
1697 CATCH_XMP ("SerializeToBuffer", true)
1698
1699 packetLen = (uint32) s.size ();
1700
1701 }
1702
1703 if (packetLen)
1704 {
1705
1706 AutoPtr<dng_memory_block> buffer (allocator.Allocate (packetLen));
1707
1708 memcpy (buffer->Buffer (), s.c_str (), packetLen);
1709
1710 return buffer.Release ();
1711
1712 }
1713
1714 }
1715
1716 return NULL;
1717
1718 }
1719
1720 /*****************************************************************************/
1721
1722 void dng_xmp_sdk::PackageForJPEG (dng_memory_allocator &allocator,
1723 AutoPtr<dng_memory_block> &stdBlock,
1724 AutoPtr<dng_memory_block> &extBlock,
1725 dng_string &extDigest) const
1726 {
1727
1728 if (HasMeta ())
1729 {
1730
1731 TXMP_STRING_TYPE stdStr ("");
1732 TXMP_STRING_TYPE extStr ("");
1733 TXMP_STRING_TYPE digestStr ("");
1734
1735 try
1736 {
1737
1738 SXMPUtils::PackageForJPEG (*fPrivate->fMeta,
1739 &stdStr,
1740 &extStr,
1741 &digestStr);
1742
1743 }
1744
1745 CATCH_XMP ("PackageForJPEG", true)
1746
1747 uint32 stdLen = (uint32) stdStr.size ();
1748 uint32 extLen = (uint32) extStr.size ();
1749
1750 if (stdLen)
1751 {
1752
1753 stdBlock.Reset (allocator.Allocate (stdLen));
1754
1755 memcpy (stdBlock->Buffer (), stdStr.c_str (), stdLen);
1756
1757 }
1758
1759 if (extLen)
1760 {
1761
1762 extBlock.Reset (allocator.Allocate (extLen));
1763
1764 memcpy (extBlock->Buffer (), extStr.c_str (), extLen);
1765
1766 if (digestStr.size () != 32)
1767 {
1768 ThrowProgramError ();
1769 }
1770
1771 extDigest.Set (digestStr.c_str ());
1772
1773 }
1774
1775 }
1776
1777 }
1778
1779 /*****************************************************************************/
1780
1781 void dng_xmp_sdk::MergeFromJPEG (const dng_xmp_sdk *xmp)
1782 {
1783
1784 if (xmp && xmp->HasMeta ())
1785 {
1786
1787 NeedMeta ();
1788
1789 try
1790 {
1791
1792 SXMPUtils::MergeFromJPEG (fPrivate->fMeta,
1793 *xmp->fPrivate->fMeta);
1794
1795 }
1796
1797 CATCH_XMP ("MergeFromJPEG", true)
1798
1799 }
1800
1801 }
1802
1803 /*****************************************************************************/
1804
1805 void dng_xmp_sdk::ReplaceXMP (dng_xmp_sdk *xmp)
1806 {
1807
1808 ClearMeta ();
1809
1810 if (xmp && xmp->HasMeta ())
1811 {
1812
1813 fPrivate->fMeta = xmp->fPrivate->fMeta;
1814
1815 xmp->fPrivate->fMeta = NULL;
1816
1817 }
1818
1819 }
1820
1821 /*****************************************************************************/
1822
1823 bool dng_xmp_sdk::IteratePaths (IteratePathsCallback *callback,
1824 void *callbackData,
1825 const char* startingNS,
1826 const char* startingPath,
1827 bool justChildren)
1828 {
1829
1830 if (HasMeta ())
1831 {
1832
1833 try
1834 {
1835
1836 SXMPIterator iter (*fPrivate->fMeta,
1837 startingNS,
1838 startingPath,
1839 justChildren ? kXMP_IterJustChildren
1840 : 0);
1841
1842 TXMP_STRING_TYPE ns ("");
1843 TXMP_STRING_TYPE prop ("");
1844
1845 while (iter.Next (&ns,
1846 &prop,
1847 NULL,
1848 NULL))
1849 {
1850
1851 if (!callback (ns .c_str (),
1852 prop.c_str (),
1853 callbackData))
1854 {
1855
1856 return false;
1857
1858 }
1859
1860 }
1861
1862 }
1863
1864 CATCH_XMP ("IteratePaths", true)
1865
1866 }
1867
1868 return true;
1869
1870 }
1871
1872 /*****************************************************************************/
1873
1874 void dng_xmp_sdk::DuplicateSubtree (const dng_xmp_sdk &source,
1875 const char *sourceNS,
1876 const char *sourceRoot,
1877 const char *destNS,
1878 const char *destRoot)
1879 {
1880
1881 if (source.HasMeta ())
1882 {
1883
1884 NeedMeta ();
1885
1886 try
1887 {
1888
1889 SXMPUtils::DuplicateSubtree (*source.fPrivate->fMeta,
1890 fPrivate->fMeta,
1891 sourceNS,
1892 sourceRoot,
1893 destNS,
1894 destRoot);
1895
1896 }
1897
1898 CATCH_XMP ("DuplicateSubtree", true)
1899
1900 }
1901
1902 }
1903
1904 /*****************************************************************************/
1905
1906 #if qDNGXMPDocOps
1907
1908 /*****************************************************************************/
1909
1910 void dng_xmp_sdk::DocOpsOpenXMP (const char *srcMIME)
1911 {
1912
1913 if (srcMIME [0])
1914 {
1915
1916 NeedMeta ();
1917
1918 try
1919 {
1920
1921 SXMPDocOps docOps;
1922
1923 docOps.OpenXMP (fPrivate->fMeta,
1924 srcMIME);
1925
1926 }
1927
1928 CATCH_XMP ("DocOpsOpenXMP", false)
1929
1930 Set (XMP_NS_DC,
1931 "format",
1932 srcMIME);
1933
1934 }
1935
1936 }
1937
1938 /*****************************************************************************/
1939
1940 void dng_xmp_sdk::DocOpsPrepareForSave (const char *srcMIME,
1941 const char *dstMIME,
1942 bool newPath)
1943 {
1944
1945 NeedMeta ();
1946
1947 try
1948 {
1949
1950 SXMPDocOps docOps;
1951
1952 docOps.OpenXMP (fPrivate->fMeta,
1953 srcMIME,
1954 "old path");
1955
1956 docOps.NoteChange (kXMP_Part_All);
1957
1958 docOps.PrepareForSave (dstMIME,
1959 newPath ? "new path" : "old path");
1960
1961 }
1962
1963 CATCH_XMP ("DocOpsPrepareForSave", false)
1964
1965 Set (XMP_NS_DC,
1966 "format",
1967 dstMIME);
1968
1969 }
1970
1971 /*****************************************************************************/
1972
1973 void dng_xmp_sdk::DocOpsUpdateMetadata (const char *srcMIME)
1974 {
1975
1976 NeedMeta ();
1977
1978 try
1979 {
1980
1981 SXMPDocOps docOps;
1982
1983 docOps.OpenXMP (fPrivate->fMeta,
1984 srcMIME);
1985
1986 docOps.NoteChange (kXMP_Part_Metadata);
1987
1988 docOps.PrepareForSave (srcMIME);
1989
1990 }
1991
1992 CATCH_XMP ("DocOpsUpdateMetadata", false)
1993
1994 }
1995
1996 /*****************************************************************************/
1997
1998 #endif
1999
2000 /*****************************************************************************/