1
/******************************************************************************
2
* $Id: tiffset.c,v 1.12 2007/02/24 17:14:14 dron Exp $
4
* Project: libtiff tools
5
* Purpose: Mainline for setting metadata in existing TIFF files.
6
* Author: Frank Warmerdam, warmerdam@pobox.com
8
******************************************************************************
9
* Copyright (c) 2000, Frank Warmerdam
11
* Permission to use, copy, modify, distribute, and sell this software and
12
* its documentation for any purpose is hereby granted without fee, provided
13
* that (i) the above copyright notices and this permission notice appear in
14
* all copies of the software and related documentation, and (ii) the names of
15
* Sam Leffler and Silicon Graphics may not be used in any advertising or
16
* publicity relating to the software without the specific, prior written
17
* permission of Sam Leffler and Silicon Graphics.
19
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
20
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
21
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
23
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
24
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
25
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
26
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
27
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
29
******************************************************************************
32
* Revision 1.12 2007/02/24 17:14:14 dron
33
* Properly handle tags with TIFF_VARIABLE writecount. As per bug
34
* http://bugzilla.remotesensing.org/show_bug.cgi?id=1350
36
* Revision 1.11 2005/09/13 14:13:42 dron
39
* Revision 1.10 2005/02/24 14:47:11 fwarmerdam
51
static char* usageMsg[] = {
52
"usage: tiffset [options] filename",
54
" -s <tagname> [count] <value>... set the tag value",
55
" -sf <tagname> <filename> read the tag value from file (for ASCII tags only)",
63
for (i = 0; usageMsg[i]; i++)
64
fprintf(stderr, "%s\n", usageMsg[i]);
68
static const TIFFFieldInfo *
69
GetField(TIFF *tiff, const char *tagname)
71
const TIFFFieldInfo *fip;
73
if( atoi(tagname) > 0 )
74
fip = TIFFFieldWithTag(tiff, (ttag_t)atoi(tagname));
76
fip = TIFFFieldWithName(tiff, tagname);
79
fprintf( stderr, "Field name %s not recognised.\n", tagname );
80
return (TIFFFieldInfo *)NULL;
87
main(int argc, char* argv[])
95
tiff = TIFFOpen(argv[argc-1], "r+");
99
for( arg_index = 1; arg_index < argc-1; arg_index++ ) {
100
if (strcmp(argv[arg_index],"-s") == 0 && arg_index < argc-3) {
101
const TIFFFieldInfo *fip;
105
tagname = argv[arg_index];
106
fip = GetField(tiff, tagname);
112
if (fip->field_type == TIFF_ASCII) {
113
if (TIFFSetField(tiff, fip->field_tag, argv[arg_index]) != 1)
114
fprintf( stderr, "Failed to set %s=%s\n",
115
fip->field_name, argv[arg_index] );
116
} else if (fip->field_writecount > 0
117
|| fip->field_writecount == TIFF_VARIABLE) {
121
if (fip->field_writecount == TIFF_VARIABLE)
122
wc = atoi(argv[arg_index++]);
124
wc = fip->field_writecount;
126
if (argc - arg_index < wc) {
128
"Number of tag values is not enough. "
129
"Expected %d values for %s tag, got %d\n",
130
wc, fip->field_name, argc - arg_index);
138
switch (fip->field_type) {
140
* XXX: We can't use TIFFDataWidth()
141
* to determine the space needed to store
142
* the value. For TIFF_RATIONAL values
143
* TIFFDataWidth() returns 8, but we use 4-byte
144
* float to represent rationals.
173
array = _TIFFmalloc(wc * size);
175
fprintf(stderr, "No space for %s tag\n",
180
switch (fip->field_type) {
182
for (i = 0; i < wc; i++)
183
((uint8 *)array)[i] = atoi(argv[arg_index+i]);
186
for (i = 0; i < wc; i++)
187
((uint16 *)array)[i] = atoi(argv[arg_index+i]);
190
for (i = 0; i < wc; i++)
191
((int8 *)array)[i] = atoi(argv[arg_index+i]);
194
for (i = 0; i < wc; i++)
195
((int16 *)array)[i] = atoi(argv[arg_index+i]);
198
for (i = 0; i < wc; i++)
199
((uint32 *)array)[i] = atol(argv[arg_index+i]);
203
for (i = 0; i < wc; i++)
204
((uint32 *)array)[i] = atol(argv[arg_index+i]);
207
for (i = 0; i < wc; i++)
208
((double *)array)[i] = atof(argv[arg_index+i]);
213
for (i = 0; i < wc; i++)
214
((float *)array)[i] = (float)atof(argv[arg_index+i]);
220
if (fip->field_passcount) {
221
ret = TIFFSetField(tiff, fip->field_tag,
224
ret = TIFFSetField(tiff, fip->field_tag,
230
switch (fip->field_type) {
235
ret = TIFFSetField(tiff, fip->field_tag,
236
atoi(argv[arg_index++]));
241
ret = TIFFSetField(tiff, fip->field_tag,
242
atol(argv[arg_index++]));
245
ret = TIFFSetField(tiff, fip->field_tag,
246
atof(argv[arg_index++]));
251
ret = TIFFSetField(tiff, fip->field_tag,
252
(float)atof(argv[arg_index++]));
260
fprintf(stderr, "Failed to set %s\n", fip->field_name);
263
} else if (strcmp(argv[arg_index],"-sf") == 0 && arg_index < argc-3) {
265
const TIFFFieldInfo *fip;
270
fip = GetField(tiff, argv[arg_index]);
275
if (fip->field_type != TIFF_ASCII) {
277
"Only ASCII tags can be set from file. "
278
"%s is not ASCII tag.\n", fip->field_name );
283
fp = fopen( argv[arg_index], "rt" );
285
perror( argv[arg_index] );
289
text = (char *) malloc(1000000);
290
len = fread( text, 1, 999999, fp );
295
if(TIFFSetField( tiff, fip->field_tag, text ) != 1) {
296
fprintf(stderr, "Failed to set %s from file %s\n",
297
fip->field_name, argv[arg_index]);
303
fprintf(stderr, "Unrecognised option: %s\n",
309
TIFFRewriteDirectory(tiff);
314
/* vim: set ts=8 sts=8 sw=8 noet: */