summaryrefslogtreecommitdiff
path: root/XMPFiles/source/FormatSupport/ReconcileLegacy.cpp
blob: 7d1c830beed6c9e069a7902325c9ddf6bb8d2bce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// =================================================================================================
// ADOBE SYSTEMS INCORPORATED
// Copyright 2006 Adobe Systems Incorporated
// All Rights Reserved
//
// NOTICE: Adobe permits you to use, modify, and distribute this file in accordance with the terms
// of the Adobe license agreement accompanying it.
// =================================================================================================

#include "public/include/XMP_Environment.h"	// ! XMP_Environment.h must be the first included header.
#include "public/include/XMP_Const.h"

#include "XMPFiles/source/FormatSupport/ReconcileLegacy.hpp"
#include "XMPFiles/source/FormatSupport/Reconcile_Impl.hpp"
#include "source/XIO.hpp"

// =================================================================================================
/// \file ReconcileLegacy.cpp
/// \brief Top level parts of utilities to reconcile between XMP and legacy metadata forms such as
/// TIFF/Exif and IPTC.
///
// =================================================================================================

// =================================================================================================
// ImportPhotoData
// ===============
//
// Import legacy metadata for JPEG, TIFF, and Photoshop files into the XMP. The caller must have
// already done the file specific processing to select the appropriate sources of the TIFF stream,
// the Photoshop image resources, and the IPTC.

#define SaveExifTag(ns,prop)	\
	if ( xmp->DoesPropertyExist ( ns, prop ) ) SXMPUtils::DuplicateSubtree ( *xmp, &savedExif, ns, prop )
#define RestoreExifTag(ns,prop)	\
	if ( savedExif.DoesPropertyExist ( ns, prop ) ) SXMPUtils::DuplicateSubtree ( savedExif, xmp, ns, prop )

void ImportPhotoData ( const TIFF_Manager & exif,
					   const IPTC_Manager & iptc,
					   const PSIR_Manager & psir,
					   int                  iptcDigestState,
					   SXMPMeta *		    xmp,
					   XMP_OptionBits	    options /* = 0 */ )
{
	/*bool haveXMP  = XMP_OptionIsSet ( options, k2XMP_FileHadXMP );*/
	bool haveExif = XMP_OptionIsSet ( options, k2XMP_FileHadExif );
	bool haveIPTC = XMP_OptionIsSet ( options, k2XMP_FileHadIPTC );
	
	// Save some new Exif writebacks that can be XMP-only from older versions, delete all of the
	// XMP's tiff: and exif: namespaces (they should only reflect native Exif), then put back the
	// saved writebacks (which might get replaced by the native Exif values in the Import calls).
	// The value of exif:ISOSpeedRatings is saved for special case handling of ISO over 65535.

	bool haveOldExif = true;	// Default to old Exif if no version tag.
	TIFF_Manager::TagInfo tagInfo;
	bool found = exif.GetTag ( kTIFF_ExifIFD, kTIFF_ExifVersion, &tagInfo );
	if ( found && (tagInfo.type == kTIFF_UndefinedType) && (tagInfo.count == 4) ) {
		haveOldExif = (strncmp ( (char*)tagInfo.dataPtr, "0230", 4 ) < 0);
	}
	
	SXMPMeta savedExif;
	
	SaveExifTag ( kXMP_NS_EXIF, "DateTimeOriginal" );
	SaveExifTag ( kXMP_NS_EXIF, "GPSLatitude" );
	SaveExifTag ( kXMP_NS_EXIF, "GPSLongitude" );
	SaveExifTag ( kXMP_NS_EXIF, "GPSTimeStamp" );
	SaveExifTag ( kXMP_NS_EXIF, "GPSAltitude" );
	SaveExifTag ( kXMP_NS_EXIF, "GPSAltitudeRef" );
	SaveExifTag ( kXMP_NS_EXIF, "ISOSpeedRatings" );
	
	SXMPUtils::RemoveProperties ( xmp, kXMP_NS_TIFF, 0, kXMPUtil_DoAllProperties );
	SXMPUtils::RemoveProperties ( xmp, kXMP_NS_EXIF, 0, kXMPUtil_DoAllProperties );
	if ( ! haveOldExif ) SXMPUtils::RemoveProperties ( xmp, kXMP_NS_ExifEX, 0, kXMPUtil_DoAllProperties );

	RestoreExifTag ( kXMP_NS_EXIF, "DateTimeOriginal" );
	RestoreExifTag ( kXMP_NS_EXIF, "GPSLatitude" );
	RestoreExifTag ( kXMP_NS_EXIF, "GPSLongitude" );
	RestoreExifTag ( kXMP_NS_EXIF, "GPSTimeStamp" );
	RestoreExifTag ( kXMP_NS_EXIF, "GPSAltitude" );
	RestoreExifTag ( kXMP_NS_EXIF, "GPSAltitudeRef" );
	RestoreExifTag ( kXMP_NS_EXIF, "ISOSpeedRatings" );

	// Not obvious here, but the logic in PhotoDataUtils follows the MWG reader guidelines.
	
	PhotoDataUtils::ImportPSIR ( psir, xmp, iptcDigestState );

	if ( haveIPTC ) PhotoDataUtils::Import2WayIPTC ( iptc, xmp, iptcDigestState );
	if ( haveExif ) PhotoDataUtils::Import2WayExif ( exif, xmp, iptcDigestState );

	if ( haveExif | haveIPTC ) PhotoDataUtils::Import3WayItems ( exif, iptc, xmp, iptcDigestState );

	// If photoshop:DateCreated does not exist try to create it from exif:DateTimeOriginal.
	
	if ( ! xmp->DoesPropertyExist ( kXMP_NS_Photoshop, "DateCreated" ) ) {
		std::string exifValue;
		bool haveExifDTO = xmp->GetProperty ( kXMP_NS_EXIF, "DateTimeOriginal", &exifValue, 0 );
		if ( haveExifDTO ) xmp->SetProperty ( kXMP_NS_Photoshop, "DateCreated", exifValue.c_str() );
	}

}	// ImportPhotoData

// =================================================================================================
// ExportPhotoData
// ===============

void ExportPhotoData ( XMP_FileFormat destFormat,
					   SXMPMeta *     xmp,
					   TIFF_Manager * exif, // Pass 0 if not wanted.
					   IPTC_Manager * iptc, // Pass 0 if not wanted.
					   PSIR_Manager * psir, // Pass 0 if not wanted.
					   XMP_OptionBits /* options = 0 */ )
{
	XMP_Assert ( (destFormat == kXMP_JPEGFile) || (destFormat == kXMP_TIFFFile) || (destFormat == kXMP_PhotoshopFile) );

	// Do not write IPTC-IIM or PSIR in DNG files (which are a variant of TIFF).

	if ( (destFormat == kXMP_TIFFFile) && (exif != 0) &&
		 exif->GetTag ( kTIFF_PrimaryIFD, kTIFF_DNGVersion, 0 ) ) {

		iptc = 0;	// These prevent calls to ExportIPTC and ExportPSIR.
		psir = 0;

		exif->DeleteTag ( kTIFF_PrimaryIFD, kTIFF_IPTC );	// These remove any existing IPTC and PSIR.
		exif->DeleteTag ( kTIFF_PrimaryIFD, kTIFF_PSIR );

	}

	// Export the individual metadata items to the non-XMP forms. Set the IPTC digest whether or not
	// it changed, it might not have been present or correct before.

	bool iptcChanged = false;	// Save explicitly, internal flag is reset by UpdateMemoryDataSets.

	void *    iptcPtr = 0;
	XMP_Uns32 iptcLen = 0;
	
	if ( iptc != 0 ) {
		PhotoDataUtils::ExportIPTC ( *xmp, iptc );
		iptcChanged = iptc->IsChanged();
		if ( iptcChanged ) iptc->UpdateMemoryDataSets();
		iptcLen = iptc->GetBlockInfo ( &iptcPtr );
		if ( psir != 0 ) PhotoDataUtils::SetIPTCDigest ( iptcPtr, iptcLen, psir );
	}

	if ( exif != 0 ) PhotoDataUtils::ExportExif ( xmp, exif );
	if ( psir != 0 ) PhotoDataUtils::ExportPSIR ( *xmp, psir );

	// Now update the non-XMP collections of metadata according to the file format. Do not update
	// the XMP here, that is done in the file handlers after deciding if an XMP-only in-place
	// update should be done.
	// - JPEG has the IPTC in PSIR 1028, the Exif and PSIR are marker segments.
	// - TIFF has the IPTC and PSIR in primary IFD tags.
	// - PSD has everything in PSIRs.

	if ( destFormat == kXMP_JPEGFile ) {

		if ( iptcChanged && (psir != 0) ) psir->SetImgRsrc ( kPSIR_IPTC, iptcPtr, iptcLen );

	} else if ( destFormat == kXMP_TIFFFile ) {

		XMP_Assert ( exif != 0 );

		if ( iptcChanged ) exif->SetTag ( kTIFF_PrimaryIFD, kTIFF_IPTC, kTIFF_UndefinedType, iptcLen, iptcPtr );

		if ( (psir != 0) && psir->IsChanged() ) {
			void* psirPtr;
			XMP_Uns32 psirLen = psir->UpdateMemoryResources ( &psirPtr );
			exif->SetTag ( kTIFF_PrimaryIFD, kTIFF_PSIR, kTIFF_UndefinedType, psirLen, psirPtr );
		}

	} else if ( destFormat == kXMP_PhotoshopFile ) {

		XMP_Assert ( psir != 0 );

		if ( iptcChanged ) psir->SetImgRsrc ( kPSIR_IPTC, iptcPtr, iptcLen );

		if ( (exif != 0) && exif->IsChanged() ) {
			void* exifPtr;
			XMP_Uns32 exifLen = exif->UpdateMemoryStream ( &exifPtr );
			psir->SetImgRsrc ( kPSIR_Exif, exifPtr, exifLen );
		}

	}
	
	// Strip the tiff: and exif: namespaces from the XMP, we're done with them. Save the Exif
	// ISOSpeedRatings if any of the values are over 0xFFFF, the native tag is SHORT. Lower level
	// code already kept or stripped the XMP form.

	bool haveOldExif = true;	// Default to old Exif if no version tag.
	if ( exif != 0 ) {
		TIFF_Manager::TagInfo tagInfo;
		bool found = exif->GetTag ( kTIFF_ExifIFD, kTIFF_ExifVersion, &tagInfo );
		if ( found && (tagInfo.type == kTIFF_UndefinedType) && (tagInfo.count == 4) ) {
			haveOldExif = (strncmp ( (char*)tagInfo.dataPtr, "0230", 4 ) < 0);
		}
	}
	
	SXMPMeta savedExif;
	SaveExifTag ( kXMP_NS_EXIF, "ISOSpeedRatings" );
	
	SXMPUtils::RemoveProperties ( xmp, kXMP_NS_TIFF, 0, kXMPUtil_DoAllProperties );
	SXMPUtils::RemoveProperties ( xmp, kXMP_NS_EXIF, 0, kXMPUtil_DoAllProperties );
	if ( ! haveOldExif ) SXMPUtils::RemoveProperties ( xmp, kXMP_NS_ExifEX, 0, kXMPUtil_DoAllProperties );

	RestoreExifTag ( kXMP_NS_EXIF, "ISOSpeedRatings" );

}	// ExportPhotoData