~ubuntu-branches/ubuntu/quantal/tiff/quantal

« back to all changes in this revision

Viewing changes to tools/tiffset.c

  • Committer: Bazaar Package Importer
  • Author(s): Jay Berkenbilt
  • Date: 2009-08-28 15:44:23 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20090828154423-7oisj77n302jrroa
Tags: 3.9.1-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 * $Id: tiffset.c,v 1.12 2007/02/24 17:14:14 dron Exp $
 
3
 *
 
4
 * Project:  libtiff tools
 
5
 * Purpose:  Mainline for setting metadata in existing TIFF files.
 
6
 * Author:   Frank Warmerdam, warmerdam@pobox.com
 
7
 *
 
8
 ******************************************************************************
 
9
 * Copyright (c) 2000, Frank Warmerdam
 
10
 *
 
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.
 
18
 * 
 
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.  
 
22
 * 
 
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 
 
28
 * OF THIS SOFTWARE.
 
29
 ******************************************************************************
 
30
 *
 
31
 * $Log: tiffset.c,v $
 
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
 
35
 *
 
36
 * Revision 1.11  2005/09/13 14:13:42  dron
 
37
 * Avoid warnings.
 
38
 *
 
39
 * Revision 1.10  2005/02/24 14:47:11  fwarmerdam
 
40
 * Updated header.
 
41
 *
 
42
 */
 
43
 
 
44
 
 
45
#include <stdio.h>
 
46
#include <string.h>
 
47
#include <stdlib.h>
 
48
 
 
49
#include "tiffio.h"
 
50
 
 
51
static char* usageMsg[] = {
 
52
"usage: tiffset [options] filename",
 
53
"where options are:",
 
54
" -s <tagname> [count] <value>...   set the tag value",
 
55
" -sf <tagname> <filename>  read the tag value from file (for ASCII tags only)",
 
56
NULL
 
57
};
 
58
 
 
59
static void
 
60
usage(void)
 
61
{
 
62
        int i;
 
63
        for (i = 0; usageMsg[i]; i++)
 
64
                fprintf(stderr, "%s\n", usageMsg[i]);
 
65
        exit(-1);
 
66
}
 
67
 
 
68
static const TIFFFieldInfo *
 
69
GetField(TIFF *tiff, const char *tagname)
 
70
{
 
71
    const TIFFFieldInfo *fip;
 
72
 
 
73
    if( atoi(tagname) > 0 )
 
74
        fip = TIFFFieldWithTag(tiff, (ttag_t)atoi(tagname));
 
75
    else
 
76
        fip = TIFFFieldWithName(tiff, tagname);
 
77
 
 
78
    if (!fip) {
 
79
        fprintf( stderr, "Field name %s not recognised.\n", tagname );
 
80
        return (TIFFFieldInfo *)NULL;
 
81
    }
 
82
 
 
83
    return fip;
 
84
}
 
85
 
 
86
int
 
87
main(int argc, char* argv[])
 
88
{
 
89
    TIFF *tiff;
 
90
    int  arg_index;
 
91
 
 
92
    if (argc < 2)
 
93
        usage();
 
94
 
 
95
    tiff = TIFFOpen(argv[argc-1], "r+");
 
96
    if (tiff == NULL)
 
97
        return 2;
 
98
 
 
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;
 
102
            const char *tagname;
 
103
 
 
104
            arg_index++;
 
105
            tagname = argv[arg_index];
 
106
            fip = GetField(tiff, tagname);
 
107
 
 
108
            if (!fip)
 
109
                return 3;
 
110
 
 
111
            arg_index++;
 
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) {
 
118
                int     ret = 1;
 
119
                short   wc;
 
120
 
 
121
                if (fip->field_writecount == TIFF_VARIABLE)
 
122
                        wc = atoi(argv[arg_index++]);
 
123
                else
 
124
                        wc = fip->field_writecount;
 
125
 
 
126
                if (argc - arg_index < wc) {
 
127
                    fprintf( stderr,
 
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);
 
131
                    return 4;
 
132
                }
 
133
                    
 
134
                if (wc > 1) {
 
135
                        int     i, size;
 
136
                        void    *array;
 
137
 
 
138
                        switch (fip->field_type) {
 
139
                                /*
 
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.
 
145
                                 */
 
146
                                case TIFF_BYTE:
 
147
                                case TIFF_ASCII:
 
148
                                case TIFF_SBYTE:
 
149
                                case TIFF_UNDEFINED:
 
150
                                default:
 
151
                                    size = 1;
 
152
                                    break;
 
153
 
 
154
                                case TIFF_SHORT:
 
155
                                case TIFF_SSHORT:
 
156
                                    size = 2;
 
157
                                    break;
 
158
 
 
159
                                case TIFF_LONG:
 
160
                                case TIFF_SLONG:
 
161
                                case TIFF_FLOAT:
 
162
                                case TIFF_IFD:
 
163
                                case TIFF_RATIONAL:
 
164
                                case TIFF_SRATIONAL:
 
165
                                    size = 4;
 
166
                                    break;
 
167
 
 
168
                                case TIFF_DOUBLE:
 
169
                                    size = 8;
 
170
                                    break;
 
171
                        }
 
172
 
 
173
                        array = _TIFFmalloc(wc * size);
 
174
                        if (!array) {
 
175
                                fprintf(stderr, "No space for %s tag\n",
 
176
                                        tagname);
 
177
                                return 4;
 
178
                        }
 
179
 
 
180
                        switch (fip->field_type) {
 
181
                            case TIFF_BYTE:
 
182
                                for (i = 0; i < wc; i++)
 
183
                                    ((uint8 *)array)[i] = atoi(argv[arg_index+i]);
 
184
                                break;
 
185
                            case TIFF_SHORT:
 
186
                                for (i = 0; i < wc; i++)
 
187
                                    ((uint16 *)array)[i] = atoi(argv[arg_index+i]);
 
188
                                break;
 
189
                            case TIFF_SBYTE:
 
190
                                for (i = 0; i < wc; i++)
 
191
                                    ((int8 *)array)[i] = atoi(argv[arg_index+i]);
 
192
                                break;
 
193
                            case TIFF_SSHORT:
 
194
                                for (i = 0; i < wc; i++)
 
195
                                    ((int16 *)array)[i] = atoi(argv[arg_index+i]);
 
196
                                break;
 
197
                            case TIFF_LONG:
 
198
                                for (i = 0; i < wc; i++)
 
199
                                    ((uint32 *)array)[i] = atol(argv[arg_index+i]);
 
200
                                break;
 
201
                            case TIFF_SLONG:
 
202
                            case TIFF_IFD:
 
203
                                for (i = 0; i < wc; i++)
 
204
                                    ((uint32 *)array)[i] = atol(argv[arg_index+i]);
 
205
                                break;
 
206
                            case TIFF_DOUBLE:
 
207
                                for (i = 0; i < wc; i++)
 
208
                                    ((double *)array)[i] = atof(argv[arg_index+i]);
 
209
                                break;
 
210
                            case TIFF_RATIONAL:
 
211
                            case TIFF_SRATIONAL:
 
212
                            case TIFF_FLOAT:
 
213
                                for (i = 0; i < wc; i++)
 
214
                                    ((float *)array)[i] = (float)atof(argv[arg_index+i]);
 
215
                                break;
 
216
                            default:
 
217
                                break;
 
218
                        }
 
219
                
 
220
                        if (fip->field_passcount) {
 
221
                                ret = TIFFSetField(tiff, fip->field_tag,
 
222
                                                   wc, array);
 
223
                        } else {
 
224
                                ret = TIFFSetField(tiff, fip->field_tag,
 
225
                                                   array);
 
226
                        }
 
227
 
 
228
                        _TIFFfree(array);
 
229
                } else {
 
230
                        switch (fip->field_type) {
 
231
                            case TIFF_BYTE:
 
232
                            case TIFF_SHORT:
 
233
                            case TIFF_SBYTE:
 
234
                            case TIFF_SSHORT:
 
235
                                ret = TIFFSetField(tiff, fip->field_tag,
 
236
                                                   atoi(argv[arg_index++]));
 
237
                                break;
 
238
                            case TIFF_LONG:
 
239
                            case TIFF_SLONG:
 
240
                            case TIFF_IFD:
 
241
                                ret = TIFFSetField(tiff, fip->field_tag,
 
242
                                                   atol(argv[arg_index++]));
 
243
                                break;
 
244
                            case TIFF_DOUBLE:
 
245
                                ret = TIFFSetField(tiff, fip->field_tag,
 
246
                                                   atof(argv[arg_index++]));
 
247
                                break;
 
248
                            case TIFF_RATIONAL:
 
249
                            case TIFF_SRATIONAL:
 
250
                            case TIFF_FLOAT:
 
251
                                ret = TIFFSetField(tiff, fip->field_tag,
 
252
                                                   (float)atof(argv[arg_index++]));
 
253
                                break;
 
254
                            default:
 
255
                                break;
 
256
                        }
 
257
                }
 
258
 
 
259
                if (ret != 1)
 
260
                    fprintf(stderr, "Failed to set %s\n", fip->field_name);
 
261
                arg_index += wc;
 
262
            }
 
263
        } else if (strcmp(argv[arg_index],"-sf") == 0 && arg_index < argc-3) {
 
264
            FILE    *fp;
 
265
            const TIFFFieldInfo *fip;
 
266
            char    *text;
 
267
            int     len;
 
268
 
 
269
            arg_index++;
 
270
            fip = GetField(tiff, argv[arg_index]);
 
271
 
 
272
            if (!fip)
 
273
                return 3;
 
274
 
 
275
            if (fip->field_type != TIFF_ASCII) {
 
276
                fprintf( stderr,
 
277
                         "Only ASCII tags can be set from file. "
 
278
                         "%s is not ASCII tag.\n", fip->field_name );
 
279
                return 5;
 
280
            }
 
281
 
 
282
            arg_index++;
 
283
            fp = fopen( argv[arg_index], "rt" );
 
284
            if(fp == NULL) {
 
285
                perror( argv[arg_index] );
 
286
                continue;
 
287
            }
 
288
 
 
289
            text = (char *) malloc(1000000);
 
290
            len = fread( text, 1, 999999, fp );
 
291
            text[len] = '\0';
 
292
 
 
293
            fclose( fp );
 
294
 
 
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]);
 
298
            }
 
299
 
 
300
            _TIFFfree( text );
 
301
            arg_index++;
 
302
        } else {
 
303
            fprintf(stderr, "Unrecognised option: %s\n",
 
304
                    argv[arg_index]);
 
305
            usage();
 
306
        }
 
307
    }
 
308
 
 
309
    TIFFRewriteDirectory(tiff);
 
310
    TIFFClose(tiff);
 
311
    return 0;
 
312
}
 
313
 
 
314
/* vim: set ts=8 sts=8 sw=8 noet: */