File: ShowMetadata.cpp

package info (click to toggle)
freeimage 3.10.0-1
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 18,864 kB
  • ctags: 21,425
  • sloc: ansic: 150,658; cpp: 57,288; pascal: 2,899; cs: 786; sh: 574; makefile: 394; asm: 284
file content (317 lines) | stat: -rw-r--r-- 10,081 bytes parent folder | download | duplicates (11)
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
// ==========================================================
// Simple metadata reader
//
// Design and implementation by 
// - Herv Drolon
//
// This file is part of FreeImage 3
//
// COVERED CODE IS PROVIDED UNDER THIS LICENSE ON AN "AS IS" BASIS, WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, WITHOUT LIMITATION, WARRANTIES
// THAT THE COVERED CODE IS FREE OF DEFECTS, MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE
// OR NON-INFRINGING. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE COVERED
// CODE IS WITH YOU. SHOULD ANY COVERED CODE PROVE DEFECTIVE IN ANY RESPECT, YOU (NOT
// THE INITIAL DEVELOPER OR ANY OTHER CONTRIBUTOR) ASSUME THE COST OF ANY NECESSARY
// SERVICING, REPAIR OR CORRECTION. THIS DISCLAIMER OF WARRANTY CONSTITUTES AN ESSENTIAL
// PART OF THIS LICENSE. NO USE OF ANY COVERED CODE IS AUTHORIZED HEREUNDER EXCEPT UNDER
// THIS DISCLAIMER.
//
// Use at own risk!
// ==========================================================

//
//  This example shows how to easily parse all metadata 
//  contained in a JPEG, TIFF or PNG image. 
//  Comments, Exif and IPTC/NAA metadata tags are written to a HTML file 
//  for later reading, and Adobe XMP XML packets are written 
//  in a file whose extension is '.xmp'. This file can be later 
//  processed using a XML parser. 
//
//  Metadata functions showed in this sample : 
//  FreeImage_GetMetadataCount, FreeImage_FindFirstMetadata, FreeImage_FindNextMetadata, 
//  FreeImage_FindCloseMetadata, FreeImage_TagToString, FreeImage_GetMetadata
//
// ==========================================================

#include <iostream>
#include <sstream>
#include <fstream>

using namespace std;

#include "FreeImage.h"

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

/** Generic image loader
	@param lpszPathName Pointer to the full file name
	@param flag Optional load flag constant
	@return Returns the loaded dib if successful, returns NULL otherwise
*/
FIBITMAP* GenericLoader(const char* lpszPathName, int flag) {
	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;

	// check the file signature and deduce its format
	// (the second argument is currently not used by FreeImage)
	fif = FreeImage_GetFileType(lpszPathName, 0);
	if(fif == FIF_UNKNOWN) {
		// no signature ?
		// try to guess the file format from the file extension
		fif = FreeImage_GetFIFFromFilename(lpszPathName);
	}
	// check that the plugin has reading capabilities ...
	if((fif != FIF_UNKNOWN) && FreeImage_FIFSupportsReading(fif)) {
		// ok, let's load the file
		FIBITMAP *dib = FreeImage_Load(fif, lpszPathName, flag);
		// unless a bad file format, we are done !
		return dib;
	}
	return NULL;
}

/** Generic image writer
	@param dib Pointer to the dib to be saved
	@param lpszPathName Pointer to the full file name
	@param flag Optional save flag constant
	@return Returns true if successful, returns false otherwise
*/
bool GenericWriter(FIBITMAP* dib, const char* lpszPathName, int flag) {
	FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
	BOOL bSuccess = FALSE;

	if(dib) {
		// try to guess the file format from the file extension
		fif = FreeImage_GetFIFFromFilename(lpszPathName);
		if(fif != FIF_UNKNOWN ) {
			// check that the plugin has sufficient writing and export capabilities ...
			WORD bpp = FreeImage_GetBPP(dib);
			if(FreeImage_FIFSupportsWriting(fif) && FreeImage_FIFSupportsExportBPP(fif, bpp)) {
				// ok, we can save the file
				bSuccess = FreeImage_Save(fif, dib, lpszPathName, flag);
				// unless an abnormal bug, we are done !
			}
		}
	}
	return (bSuccess == TRUE) ? true : false;
}

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

/**
	FreeImage error handler
	@param fif Format / Plugin responsible for the error 
	@param message Error message
*/
void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message) {
	cout << "\n*** ";
	if(fif != FIF_UNKNOWN) {
		cout << FreeImage_GetFormatFromFIF(fif) << " Format\n";
	}
	cout << message;
	cout << " ***\n";
}

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

/**
Print a basic HTML header
*/
void PrintHTMLHeader(iostream& ios) {
	ios << "<HTML>\n<BODY>\n<CENTER>\n";
	ios << "<FONT FACE = \"Arial\">\n";
}

/**
Print a HTML footer
*/
void PrintHTMLFooter(iostream& ios) {
	ios << "</CENTER>\n</FONT>\n</BODY>\n</HTML>\n";
}

/**
Print a table header
*/
void PrintTableHeader(iostream& ios, const char *title) {
	ios << "<TABLE BORDER=\"1\">\n";
	ios << "<TR><TD ALIGN=CENTER COLSPAN=\"3\" BGCOLOR=\"#CCCCCC\"><B><font face=\"Arial\">" << title << "</font></B></TD></TR>\n";
}

/**
Print a table section
*/
void PrintTableSection(iostream& ios, const char *title) {
	ios << "<TR><TD ALIGN=CENTER COLSPAN=\"3\" BGCOLOR=\"#FFFFCC\"><B><font face=\"Arial\">" << title << "</font></B></TD></TR>\n";
	ios << "<TR><TD><B>Tag name</B></TD><TD><B>Tag value</B></TD><TD><B>Description</B></TD></TR>";
}

/**
Print a table footer
*/
void PrintTableFooter(iostream& ios) {
	ios << "</TABLE>\n";
}


/**
Print the metadata tags to a HTML file
*/
void PrintMetadata(iostream& ios, const char *sectionTitle, FIBITMAP *dib, FREE_IMAGE_MDMODEL model) {
	FITAG *tag = NULL;
	FIMETADATA *mdhandle = NULL;

	mdhandle = FreeImage_FindFirstMetadata(model, dib, &tag);

	if(mdhandle) {
		// Print a table section
		PrintTableSection(ios, sectionTitle);

		do {
			// convert the tag value to a string
			const char *value = FreeImage_TagToString(model, tag);

			// print the tag 
			// note that most tags do not have a description, 
			// especially when the metadata specifications are not available
			if(FreeImage_GetTagDescription(tag)) {
				ios << "<TR><TD>" << FreeImage_GetTagKey(tag) << "</TD><TD>" << value << "</TD><TD>" << FreeImage_GetTagDescription(tag) << "</TD></TR>\n";
			} else {
				ios << "<TR><TD>" << FreeImage_GetTagKey(tag) << "</TD><TD>" << value << "</TD><TD>" << "&nbsp;" << "</TD></TR>\n";
			}

		} while(FreeImage_FindNextMetadata(mdhandle, &tag));
	}

	FreeImage_FindCloseMetadata(mdhandle);
}

int 
main(int argc, char *argv[]) {
	unsigned count;

	// call this ONLY when linking with FreeImage as a static library
#ifdef FREEIMAGE_LIB
	FreeImage_Initialise();
#endif // FREEIMAGE_LIB

	// initialize your own FreeImage error handler

	FreeImage_SetOutputMessage(FreeImageErrorHandler);

	// print version & copyright infos

	cout << "FreeImage " << FreeImage_GetVersion() << "\n";
	cout << FreeImage_GetCopyrightMessage() << "\n\n";

	if(argc != 2) {
		cout << "Usage : ShowMetadata <input file name>\n";
		return 0;
	}

	// Load the bitmap

	FIBITMAP *dib = GenericLoader(argv[1], 0);
	if(!dib)
		return 0;

	// Create a HTML file
	std::string html_file(strtok(argv[1], ".") + std::string(".html"));

	fstream metadataFile(html_file.c_str(), ios::out);

	// Print the header

	PrintHTMLHeader(metadataFile);
	PrintTableHeader(metadataFile, argv[1]);

	// Parse and print metadata

	if(count = FreeImage_GetMetadataCount(FIMD_COMMENTS, dib)) {
		cout << "\nFIMD_COMMENTS (" << count << " data)\n-----------------------------------------\n";

		PrintMetadata(metadataFile, "Comments", dib, FIMD_COMMENTS);
	}	
	if(count = FreeImage_GetMetadataCount(FIMD_EXIF_MAIN, dib)) {
		cout << "\nFIMD_EXIF_MAIN (" << count << " data)\n-----------------------------------------\n";

		PrintMetadata(metadataFile, "Exif - main info", dib, FIMD_EXIF_MAIN);
	}
	if(count = FreeImage_GetMetadataCount(FIMD_EXIF_EXIF, dib)) {
		cout << "\nFIMD_EXIF_EXIF (" << count << " data)\n-----------------------------------------\n";

		PrintMetadata(metadataFile, "Exif - advanced info", dib, FIMD_EXIF_EXIF);
	}
	if(count = FreeImage_GetMetadataCount(FIMD_EXIF_GPS, dib)) {
		cout << "\nFIMD_EXIF_GPS (" << count << " data)\n-----------------------------------------\n";

		PrintMetadata(metadataFile, "Exif GPS", dib, FIMD_EXIF_GPS);
	}
	if(count = FreeImage_GetMetadataCount(FIMD_EXIF_INTEROP, dib)) {
		cout << "\nFIMD_EXIF_INTEROP (" << count << " data)\n-----------------------------------------\n";

		PrintMetadata(metadataFile, "Exif interoperability", dib, FIMD_EXIF_INTEROP);
	}
	if(count = FreeImage_GetMetadataCount(FIMD_EXIF_MAKERNOTE, dib)) {
		cout << "\nFIMD_EXIF_MAKERNOTE (" << count << " data)\n-----------------------------------------\n";

		// Get the camera model
		FITAG *tagMake = NULL;
		FreeImage_GetMetadata(FIMD_EXIF_MAIN, dib, "Make", &tagMake);

		std::string buffer((char*)FreeImage_GetTagValue(tagMake));
		buffer += " Makernote";

		PrintMetadata(metadataFile, buffer.c_str(), dib, FIMD_EXIF_MAKERNOTE);
	}
	if(count = FreeImage_GetMetadataCount(FIMD_IPTC, dib)) {
		cout << "\nFIMD_IPTC (" << count << " data)\n-----------------------------------------\n";

		PrintMetadata(metadataFile, "IPTC/NAA", dib, FIMD_IPTC);
	}
	if(count = FreeImage_GetMetadataCount(FIMD_GEOTIFF, dib)) {
		cout << "\nFIMD_GEOTIFF (" << count << " data)\n-----------------------------------------\n";

		PrintMetadata(metadataFile, "GEOTIFF", dib, FIMD_GEOTIFF);
	}

	// Print the footer

	PrintTableFooter(metadataFile);
	PrintHTMLFooter(metadataFile);

	// close the HTML file

	metadataFile.close();

	// print XMP data

	if(count = FreeImage_GetMetadataCount(FIMD_XMP, dib)) {
		cout << "\nFIMD_XMP (" << count << " packet)\n-----------------------------------------\n";

		std::string xmp_file(strtok(argv[1], ".") + std::string(".xmp"));
		metadataFile.open(xmp_file.c_str(), ios::out);

		FITAG *tag = NULL;
		FreeImage_GetMetadata(FIMD_XMP, dib, "XMLPacket", &tag);
		if(tag) {
			metadataFile << (char*)FreeImage_GetTagValue(tag);
		}

		metadataFile.close();
	}


	// Unload the bitmap

	FreeImage_Unload(dib);


	// call this ONLY when linking with FreeImage as a static library
#ifdef FREEIMAGE_LIB
	FreeImage_DeInitialise();
#endif // FREEIMAGE_LIB

	return 0;
}