File: iptctool.c

package info (click to toggle)
libiptcdata 1.0.2%2Blibtool01-2
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 2,916 kB
  • ctags: 978
  • sloc: sh: 9,445; ansic: 4,092; xml: 2,798; makefile: 123; python: 62; sed: 16
file content (113 lines) | stat: -rw-r--r-- 2,275 bytes parent folder | download | duplicates (9)
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
/* TODO: this is a stripped down iptc utility with display only */
/* TODO: there is no getopt on Windows */

#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

#include <libiptcdata/iptc-data.h>
#include <libiptcdata/iptc-jpeg.h>


unsigned char buf[256*256];
unsigned char outbuf[256*256];

static void
print_usage(char ** argv)
{
	printf("\nUsage: %s FILE\n\n"
			"Tool for showing the contents of IPTC metadata in JPEG images\n",
			argv[0]);
}


static void
print_iptc_data (IptcData * d)
{
	unsigned int i;

	if (d->count) {
		printf("%6.6s %-20.20s %-9.9s %6s  %s\n", "Tag", "Name", "Type",
				"Size", "Value");
		printf(" ----- -------------------- --------- ------  -----\n");
	}
	
	for (i=0; i < d->count; i++) {
		IptcDataSet * e = d->datasets[i];
		char buf[256];

		printf("%2d:%03d %-20.20s %-9.9s %6d  ",
				e->record, e->tag,
				iptc_tag_get_title (e->record, e->tag),
				iptc_format_get_name (iptc_dataset_get_format (e)),
				e->size);
		switch (iptc_dataset_get_format (e)) {
			case IPTC_FORMAT_BYTE:
			case IPTC_FORMAT_SHORT:
			case IPTC_FORMAT_LONG:
				printf("%d\n", iptc_dataset_get_value (e));
				break;
			case IPTC_FORMAT_BINARY:
				iptc_dataset_get_as_str (e, buf, sizeof(buf));
				printf("%s\n", buf);
				break;
			default:
				iptc_dataset_get_data (e, buf, sizeof(buf));
				printf("%s\n", buf);
				break;
		}
	}
}





int
main (int argc, char ** argv)
{
	FILE * infile;
	IptcData * d = NULL;
	IptcDataSet * ds = NULL;
	int ps3_len, iptc_len, iptc_off;

	if (argc < 2) {
		print_usage(argv);
		return 1;
	}

	infile = fopen(argv[1], "rb");
	if (!infile) {
		fprintf(stderr, "Error opening %s\n", argv[1]);
		return 1;
	}

	ps3_len = iptc_jpeg_read_ps3 (infile, buf, sizeof(buf));
	fclose (infile);
	if (ps3_len < 0) {
		fprintf(stderr, "Error: iptc_jpeg_read_ps3: %d\n", ps3_len);
		return 1;
	}

	if (ps3_len) {
		iptc_off = iptc_jpeg_ps3_find_iptc (buf, ps3_len, &iptc_len);
		if (iptc_off < 0) {
			fprintf(stderr, "Error: iptc_jpeg_ps3_find_iptc: %d\n", iptc_off);
			return 2;
		}
		if (iptc_off)
			d = iptc_data_new_from_data (buf + iptc_off, iptc_len);
	}

	if (d)
		print_iptc_data (d);
	else
		printf("No IPTC data found\n");
	
	if (d)
		iptc_data_unref(d);

	return 0;
}