File: exiftran.c

package info (click to toggle)
fbi 2.07-6
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,240 kB
  • ctags: 2,357
  • sloc: ansic: 17,896; sh: 59; makefile: 33; perl: 12
file content (269 lines) | stat: -rw-r--r-- 6,198 bytes parent folder | download | duplicates (3)
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
/*
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include <stddef.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>

#include <libexif/exif-data.h>

#include <jpeglib.h>
#include "jpeg/transupp.h"		/* Support routines for jpegtran */
#include "jpegtools.h"
#include "genthumbnail.h"

/* ---------------------------------------------------------------------- */

static void dump_exif(FILE *out, ExifData *ed)
{
    const char *title, *value;
#ifdef HAVE_NEW_EXIF
    char buffer[256];
#endif
    ExifEntry  *ee;
    int tag,i;

    for (i = 0; i < EXIF_IFD_COUNT; i++) {
	fprintf(out,"   ifd %s\n", exif_ifd_get_name (i));
	for (tag = 0; tag < 0xffff; tag++) {
	    title = exif_tag_get_title(tag);
	    if (!title)
		continue;
	    ee = exif_content_get_entry (ed->ifd[i], tag);
	    if (NULL == ee)
		continue;
#ifdef HAVE_NEW_EXIF
	    value = exif_entry_get_value(ee, buffer, sizeof(buffer));
#else
	    value = exif_entry_get_value(ee);
#endif
	    fprintf(out,"      0x%04x  %-30s %s\n", tag, title, value);
	}
    }
    if (ed->data && ed->size)
	fprintf(out,"   thumbnail\n      %d bytes data\n", ed->size);
}

static int dump_file(FILE *out, char *filename)
{
    ExifData   *ed;

    ed = exif_data_new_from_file (filename);
    if (NULL == ed) {
	fprintf(stderr,"%s: no EXIF data\n",filename);
	return -1;
    }

    fprintf(out,"%s\n",filename);
    dump_exif(out,ed);
    fprintf(out,"--\n");
    
    exif_data_unref (ed);
    return 0;
}

/* ---------------------------------------------------------------------- */

#define THUMB_MAX 65536

static void
usage(FILE *fp, char *name)
{
    char *h;

    if (NULL != (h = strrchr(name, '/')))
	name = h+1;
    fprintf(fp,
	    "usage: %s [ options ] file\n"
	    "\n"
	    "transform options:\n"
	    "  -a         automatic (using exif orientation tag)\n"
	    "  -9         rotate by 90 degrees\n"
	    "  -1         rotate by 180 degrees\n"
	    "  -2         rotate by 270 degrees\n"
	    "  -f         flip vertical\n"
	    "  -F         flip horizontal\n"
	    "  -t         transpose\n"
	    "  -T         transverse\n"
	    "\n"
	    "  -nt        don't rotate exif thumbnail\n"
	    "  -ni        don't rotate jpeg image\n"
	    "  -no        don't update the orientation tag\n"
	    "\n"
	    "other options:\n"
	    "  -h         print this text\n"
	    "  -d         dump exif data\n"
	    "  -c <text>  create/update comment\n"
	    "  -g         (re)generate thumbnail\n"
	    "  -o <file>  output file\n"
	    "  -i         change files inplace\n"
	    "    -b       create a backup file (with -i)\n"
	    "    -p       preserve timestamps  (with -i)\n"
	    "\n"
	    "-- \n"
	    "Gerd Hoffmann <kraxel@bytesex.org> [SUSE Labs]\n",
	    name);
}

int main(int argc, char *argv[])
{
    JXFORM_CODE transform = JXFORM_NONE;
    unsigned char *comment = NULL;
    unsigned char *outfile = NULL;
    unsigned char *thumbnail = NULL;
    int tsize = 0;
    int inplace = 0;
    unsigned int flags =
	JFLAG_TRANSFORM_IMAGE     |
	JFLAG_TRANSFORM_THUMBNAIL |
	JFLAG_UPDATE_ORIENTATION;
    int dump = 0;
    int i, c, rc;

    for (;;) {
	c = getopt(argc, argv, "hbpid912fFtTagc:o:n:");
	if (c == -1)
	    break;
	switch (c) {
	case '9':
	    transform = JXFORM_ROT_90;
	    break;
	case '1':
	    transform = JXFORM_ROT_180;
	    break;
	case '2':
	    transform = JXFORM_ROT_270;
	    break;
	case 'f':
	    transform = JXFORM_FLIP_V;
	    break;
	case 'F':
	    transform = JXFORM_FLIP_H;
	    break;
	case 't':
	    transform = JXFORM_TRANSPOSE;
	    break;
	case 'T':
	    transform = JXFORM_TRANSVERSE;
	    break;
	case 'a':
	    transform = -1; /* automagic */
	    break;

	case 'n':
	    /* don't ... */
	    switch (optarg[0]) {
	    case 't':
		flags &= ~JFLAG_TRANSFORM_THUMBNAIL;
		break;
	    case 'i':
		flags &= ~JFLAG_TRANSFORM_IMAGE;
		break;
	    case 'o':
		flags &= ~JFLAG_UPDATE_ORIENTATION;
		break;
	    default:
		fprintf(stderr,"unknown option -n%c\n",optarg[0]);
		exit(1);
	    }
	    break;
	    
	case 'c':
	    flags |= JFLAG_UPDATE_COMMENT;
	    comment = optarg;
	    break;
	case 'g':
	    flags |= JFLAG_UPDATE_THUMBNAIL;
	    break;
	case 'o':
	    outfile = optarg;
	    break;
	case 'd':
	    dump = 1;
	    break;

	case 'b':
	    flags |= JFLAG_FILE_BACKUP;
	    break;
	case 'p':
	    flags |= JFLAG_FILE_KEEP_TIME;
	    break;
	case 'i':
	    inplace = 1;
	    break;

	case 'h':
	    usage(stdout,argv[0]);
	    exit(0);
	default:
	    usage(stderr,argv[0]);
	    exit(1);
	}
    }

    /* sanity checks on the arguments */
    if (optind == argc) {
	fprintf(stderr,
		"no image file specified (try -h for more info)\n");
	exit(1);
    }

    /* read-only stuff */
    if (dump) {
	rc = 0;
	for (i = optind; i < argc; i++) {
	    if (0 != dump_file(stdout,argv[i]))
		rc = 1;
	}
	return rc;
    }

    /* r/w sanity checks */
    if (NULL != outfile && optind+1 > argc) {
	fprintf(stderr,
		"when specifying a output file you can process\n"
		"one file at a time only (try -h for more info).\n");
	exit(1);
    }
    if (NULL == outfile && 0 == inplace) {
	fprintf(stderr,
		"you have to either specify a output file (-o <file>)\n"
		"or enable inplace editing (-i). Try -h for more info.\n");
	exit(1);
    }
    if (JXFORM_NONE == transform &&
	!(flags & JFLAG_UPDATE_COMMENT) &&
	!(flags & JFLAG_UPDATE_THUMBNAIL)) {
	fprintf(stderr,
		"What do you want to do today?  Neither a new comment nor a\n"
		"tranformation operation was specified (try -h for more info).\n");
	exit(1);
    }

    /* do actual update work */
    if (outfile) {
	if (flags & JFLAG_UPDATE_THUMBNAIL) {
	    thumbnail = malloc(THUMB_MAX);
	    tsize = create_thumbnail(argv[optind],thumbnail,THUMB_MAX);
	}
	return jpeg_transform_files(argv[optind], outfile, transform,
				    comment, thumbnail, tsize, flags);
    } else {
	rc = 0;
	for (i = optind; i < argc; i++) {
	    fprintf(stderr,"processing %s\n",argv[i]);
	    if (flags & JFLAG_UPDATE_THUMBNAIL) {
		thumbnail = malloc(THUMB_MAX);
		tsize = create_thumbnail(argv[i],thumbnail,THUMB_MAX);
	    }
	    if (0 != jpeg_transform_inplace(argv[i], transform, comment,
					    thumbnail, tsize, flags))
		rc = 1;
	}
	return rc;
    }
}