summaryrefslogtreecommitdiff
path: root/samples/source/XMPFilesCoverage.cpp
blob: aefd9cf9fd01ac96277d0c3403f2000dc5d4b2fc (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// =================================================================================================
// Copyright 2002 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.
// =================================================================================================

/**
* Demonstrates syntax and usage by exercising most of the API functions of XMPFiles Toolkit SDK component, 
* using a sample XMP Packet that contains all of the different property and attribute types.
*/

#include <cstdio>
#include <vector>
#include <string>
#include <cstring>
#include <stdexcept>
#include <cerrno>
#include <ctime>

#define TXMP_STRING_TYPE std::string
#define XMP_INCLUDE_XMPFILES 1
#include "public/include/XMP.hpp"
#include "public/include/XMP.incl_cpp"

//#define ENABLE_XMP_CPP_INTERFACE 1;

using namespace std;

#if WIN_ENV
	#pragma warning ( disable : 4100 )	// ignore unused variable
	#pragma warning ( disable : 4127 )	// conditional expression is constant
	#pragma warning ( disable : 4505 )	// unreferenced local function has been removed
	#pragma warning ( disable : 4996 )	// '...' was declared deprecated
#endif

// -------------------------------------------------------------------------------------------------

FILE * sLogFile = 0;

// -------------------------------------------------------------------------------------------------

static void WriteMinorLabel ( FILE * log, const char * title )
{

	fprintf ( log, "\n// " );
	for ( size_t i = 0; i < strlen(title); ++i ) fprintf ( log, "-" );
	fprintf ( log, "--\n// %s :\n\n", title );

}	// WriteMinorLabel

// -------------------------------------------------------------------------------------------------

static XMP_Status DumpToFile ( void * refCon, XMP_StringPtr outStr, XMP_StringLen outLen )
{
	XMP_Status	status	= 0; 
	size_t		count;
	FILE *		outFile	= static_cast < FILE * > ( refCon );
	
	count = fwrite ( outStr, 1, outLen, outFile );
	if ( count != outLen ) status = errno;
	return status;
	
}	// DumpToFile

// -------------------------------------------------------------------------------------------------

static XMP_Status DumpToString ( void * refCon, XMP_StringPtr outStr, XMP_StringLen outLen )
{
	std::string * fullStr = static_cast < std::string * > ( refCon );
	
	fullStr->append ( outStr, outLen );
	return 0;
	
}	// DumpToString

// -------------------------------------------------------------------------------------------------

#define DumpOneFormat(fmt)													\
	format = kXMP_ ## fmt ## File;											\
	flags = 0;																\
	ok = SXMPFiles::GetFormatInfo ( format, &flags );						\
	fprintf ( sLogFile, "kXMP_" #fmt "File = \"%.4s\", %s, flags = 0x%X\n",	\
			  &format, (ok ? "smart" : "dumb"), flags );

static void DumpHandlerInfo()
{

	WriteMinorLabel ( sLogFile, "Dump file format constants and handler flags" );

	bool ok;
	XMP_FileFormat format;
	XMP_OptionBits flags;

	DumpOneFormat ( PDF );
	DumpOneFormat ( PostScript );
	DumpOneFormat ( EPS );

	DumpOneFormat ( JPEG );
	DumpOneFormat ( JPEG2K );
	DumpOneFormat ( TIFF );
	DumpOneFormat ( GIF );
	DumpOneFormat ( PNG );

	DumpOneFormat ( MOV );
	DumpOneFormat ( AVI );
	DumpOneFormat ( CIN );
	DumpOneFormat ( WAV );
	DumpOneFormat ( MP3 );
	DumpOneFormat ( SES );
	DumpOneFormat ( CEL );
	DumpOneFormat ( MPEG );
	DumpOneFormat ( MPEG2 );
	DumpOneFormat ( WMAV );

	DumpOneFormat ( HTML );
	DumpOneFormat ( XML );
	DumpOneFormat ( Text );

	DumpOneFormat ( Photoshop );
	DumpOneFormat ( Illustrator );
	DumpOneFormat ( InDesign );
	DumpOneFormat ( AEProject );
	DumpOneFormat ( AEFilterPreset );
	DumpOneFormat ( EncoreProject );
	DumpOneFormat ( PremiereProject );
	DumpOneFormat ( PremiereTitle );

	DumpOneFormat ( Unknown );

}	// DumpHandlerInfo

// -------------------------------------------------------------------------------------------------

static void OpenTestFile ( const char * fileName, XMP_OptionBits rwMode, SXMPMeta* xmpMeta, SXMPFiles* xmpFile )
{
	bool ok;

	XMP_FileFormat format;
	XMP_OptionBits openFlags, handlerFlags;
	XMP_PacketInfo xmpPacket;
	
	bool isUpdate = ((rwMode & kXMPFiles_OpenForUpdate) != 0);
	
	static const char * charForms[] = { "UTF-8", "unknown char form", "UTF-16BE", "UTF-16LE", "UTF-32BE", "UTF-32LE" };

	XMP_OptionBits smartFlags = rwMode | kXMPFiles_OpenUseSmartHandler;
	XMP_OptionBits scanFlags  = rwMode | kXMPFiles_OpenUsePacketScanning;
	
	ok = xmpFile->OpenFile ( fileName, kXMP_UnknownFile, smartFlags );
	if ( ! ok ) {
		fprintf ( sLogFile, "Failed to get a smart handler\n" );
		ok = xmpFile->OpenFile ( fileName, kXMP_UnknownFile, scanFlags );
		if ( ! ok ) return;
	}

	ok = xmpFile->GetFileInfo ( 0, &openFlags, &format, &handlerFlags );
	if ( ! ok ) return;

	fprintf ( sLogFile, "File info : format = \"%.4s\", handler flags = 0x%X, open flags = 0x%X (%s)\n",
			  &format, handlerFlags, openFlags, (isUpdate ? "update" : "read-only") );

	ok = xmpFile->GetXMP ( xmpMeta, 0, &xmpPacket );
	if ( ! ok ) {
		fprintf ( sLogFile, "No XMP\n" );
		return;
	}

	fprintf ( sLogFile, "XMP packet info : file offset = %lld, length = %d, pad = %d",
			  xmpPacket.offset, xmpPacket.length, xmpPacket.padSize );
	fprintf ( sLogFile, ", %s", ((xmpPacket.charForm > 5) ? "bad char form" : charForms[xmpPacket.charForm]) );
	fprintf ( sLogFile, ", %s\n", (xmpPacket.writeable ? "writeable" : "read-only") );
	
	fprintf ( sLogFile, "\n" );

	// Remove extaneous properties to make the dump smaller.
	SXMPUtils::RemoveProperties ( xmpMeta, kXMP_NS_XMP, "Thumbnails", true );
	SXMPUtils::RemoveProperties ( xmpMeta, kXMP_NS_XMP, "PageInfo", true );
	SXMPUtils::RemoveProperties ( xmpMeta, "http://ns.adobe.com/xap/1.0/t/pg/", 0, true );
	SXMPUtils::RemoveProperties ( xmpMeta, "http://ns.adobe.com/xap/1.0/mm/", 0, true );
	#if 1
		SXMPUtils::RemoveProperties ( xmpMeta, "http://ns.adobe.com/camera-raw-settings/1.0/", 0, true );
		SXMPUtils::RemoveProperties ( xmpMeta, "http://ns.adobe.com/tiff/1.0/", 0, true );
		SXMPUtils::RemoveProperties ( xmpMeta, "http://ns.adobe.com/exif/1.0/", 0, true );
		SXMPUtils::RemoveProperties ( xmpMeta, "http://ns.adobe.com/exif/1.0/aux/", 0, true );
		SXMPUtils::RemoveProperties ( xmpMeta, "http://ns.adobe.com/photoshop/1.0/", 0, true );
		SXMPUtils::RemoveProperties ( xmpMeta, "http://ns.adobe.com/pdf/1.3/", 0, true );
	#endif

}	// OpenTestFile

// -------------------------------------------------------------------------------------------------

#ifndef OnlyReadOnly
	#define OnlyReadOnly 0
#endif

static void TestOneFile ( const char * fileName )
{
	char buffer [1000];
	SXMPMeta  xmpMeta;	
	SXMPFiles xmpFile;
	XMP_PacketInfo xmpPacket;
	std::string    roDump, rwDump;
	
	sprintf ( buffer, "Testing %s", fileName );
	WriteMinorLabel ( sLogFile, buffer );
	
	OpenTestFile ( fileName, kXMPFiles_OpenForRead, &xmpMeta, &xmpFile );
	xmpMeta.DumpObject ( DumpToString, &roDump );
	xmpFile.CloseFile();

	if ( OnlyReadOnly ) {
		fprintf ( sLogFile, "Initial XMP from %s\n", fileName );
		xmpMeta.DumpObject ( DumpToFile, sLogFile );
		return;
	}
	
	OpenTestFile ( fileName, kXMPFiles_OpenForUpdate, &xmpMeta, &xmpFile );
	xmpMeta.DumpObject ( DumpToString, &rwDump );
	if ( roDump != rwDump ) {
		fprintf ( sLogFile, "** Initial read-only and update XMP don't match! **\n\n" );
		fprintf ( sLogFile, "Read-only XMP\n%s\nUpdate XMP\n%s\n", roDump.c_str(), rwDump.c_str() );
	}

	fprintf ( sLogFile, "Initial XMP from %s\n", fileName );
	xmpMeta.DumpObject ( DumpToFile, sLogFile );
	
	XMP_DateTime now;
	SXMPUtils::CurrentDateTime ( &now );
	std::string nowStr;
	SXMPUtils::ConvertFromDate ( now, &nowStr );
	if ( xmpMeta.CountArrayItems ( kXMP_NS_XMP, "XMPFileStamps" ) >= 9 ) {
		xmpMeta.DeleteProperty ( kXMP_NS_XMP, "XMPFileStamps" );
	}
	xmpMeta.AppendArrayItem ( kXMP_NS_XMP, "XMPFileStamps", kXMP_PropArrayIsOrdered, "" );
	xmpMeta.SetProperty ( kXMP_NS_XMP, "XMPFileStamps[last()]", nowStr.c_str() );
	
	nowStr.insert ( 0, "Updating dc:description at " );
	xmpMeta.SetLocalizedText ( kXMP_NS_DC, "description", "", "x-default", nowStr.c_str() );

	xmpFile.PutXMP ( xmpMeta );
	XMP_OptionBits closeOptions = 0;
	XMP_OptionBits capabilities = 0;
	XMP_FileFormat fileFormat = 0;
	xmpFile.GetFileInfo( NULL, NULL, &fileFormat, NULL);
	SXMPFiles::GetFormatInfo( fileFormat, &capabilities);

	if ( (xmpMeta.CountArrayItems ( kXMP_NS_XMP, "XMPFileStamps" ) & 1) == 0
			&& ( capabilities & kXMPFiles_AllowsSafeUpdate ) ) 
		closeOptions |= kXMPFiles_UpdateSafely;

	xmpFile.CloseFile ( closeOptions );

	fprintf ( sLogFile, "\n" );
	OpenTestFile ( fileName, kXMPFiles_OpenForRead, &xmpMeta, &xmpFile );
	fprintf ( sLogFile, "Modified XMP from %s\n", fileName );
	xmpMeta.DumpObject ( DumpToFile, sLogFile );
	xmpFile.CloseFile();

	fprintf ( sLogFile, "\nDone testing %s\n", fileName );

}	// TestOneFile

// -------------------------------------------------------------------------------------------------

extern "C" int main ( int /*argc*/, const char * argv[] )
{
	int result = 0;

	char   logName[256];
	int    nameLen = (int) strlen ( argv[0] );
	if ( (nameLen >= 4) && (strcmp ( argv[0]+nameLen-4, ".exe" ) == 0) ) nameLen -= 4;
	memcpy ( logName, argv[0], nameLen );
	memcpy ( &logName[nameLen], "Log.txt", 8 );	// Include final null.
	sLogFile = fopen ( logName, "wb" );

	time_t now = time(0);
	fprintf ( sLogFile, "XMPFilesCoverage starting %s", ctime(&now) );

	XMP_VersionInfo coreVersion, filesVersion;
	SXMPMeta::GetVersionInfo ( &coreVersion );
	SXMPFiles::GetVersionInfo ( &filesVersion );
	fprintf ( sLogFile, "Version :\n   %s\n   %s\n", coreVersion.message, filesVersion.message );

	try {

		if ( ! SXMPMeta::Initialize() ) {
			fprintf ( sLogFile, "## XMPMeta::Initialize failed!\n" );
			return -1;
		}
		XMP_OptionBits options = 0;
		#if UNIX_ENV
			options |= kXMPFiles_ServerMode;
		#endif
		if ( ! SXMPFiles::Initialize ( options ) ) {
			fprintf ( sLogFile, "## SXMPFiles::Initialize failed!\n" );
			return -1;
		}
		
		DumpHandlerInfo();
	
		TestOneFile ( "../../../testfiles/BlueSquare.ai" );
		TestOneFile ( "../../../testfiles/BlueSquare.eps" );
		TestOneFile ( "../../../testfiles/BlueSquare.indd" );
		TestOneFile ( "../../../testfiles/BlueSquare.jpg" );
		TestOneFile ( "../../../testfiles/BlueSquare.pdf" );
		TestOneFile ( "../../../testfiles/BlueSquare.psd" );
		TestOneFile ( "../../../testfiles/BlueSquare.tif" );
		TestOneFile ( "../../../testfiles/BlueSquare.avi" );
		TestOneFile ( "../../../testfiles/BlueSquare.mov" );
		TestOneFile ( "../../../testfiles/BlueSquare.mp3" );
		TestOneFile ( "../../../testfiles/BlueSquare.wav" );
		TestOneFile ( "../../../testfiles/BlueSquare.png" );

	} catch ( XMP_Error & excep ) {

		fprintf ( sLogFile, "\nCaught XMP_Error %d : %s\n", excep.GetID(), excep.GetErrMsg() );
		result = -2;

	} catch ( ... ) {

		fprintf ( sLogFile, "## Caught unknown exception\n" );
		result = -3;

	}

	SXMPFiles::Terminate();
	SXMPMeta::Terminate();

	now = time(0);
	fprintf ( sLogFile, "\nXMPFilesCoverage finished %s", ctime(&now) );
	fprintf ( sLogFile,	"Final status = %d\n", result );
	fclose ( sLogFile );
	
	printf( "\nresults have been logged into %s\n", logName );
	
	return result;

}	// main