1 /******************************************************************************
2 * $Id: tiffset.c,v 1.12.2.2 2010-07-06 14:26:00 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 ******************************************************************************
39 static char* usageMsg[] = {
40 "usage: tiffset [options] filename",
42 " -s <tagname> [count] <value>... set the tag value",
43 " -sf <tagname> <filename> read the tag value from file (for ASCII tags only)",
51 for (i = 0; usageMsg[i]; i++)
52 fprintf(stderr, "%s\n", usageMsg[i]);
56 static const TIFFFieldInfo *
57 GetField(TIFF *tiff, const char *tagname)
59 const TIFFFieldInfo *fip;
61 if( atoi(tagname) > 0 )
62 fip = TIFFFieldWithTag(tiff, (ttag_t)atoi(tagname));
64 fip = TIFFFieldWithName(tiff, tagname);
67 fprintf( stderr, "Field name \"%s\" is not recognised.\n", tagname );
68 return (TIFFFieldInfo *)NULL;
75 main(int argc, char* argv[])
83 tiff = TIFFOpen(argv[argc-1], "r+");
87 for( arg_index = 1; arg_index < argc-1; arg_index++ ) {
88 if (strcmp(argv[arg_index],"-s") == 0 && arg_index < argc-3) {
89 const TIFFFieldInfo *fip;
93 tagname = argv[arg_index];
94 fip = GetField(tiff, tagname);
100 if (fip->field_type == TIFF_ASCII) {
101 if (TIFFSetField(tiff, fip->field_tag, argv[arg_index]) != 1)
102 fprintf( stderr, "Failed to set %s=%s\n",
103 fip->field_name, argv[arg_index] );
104 } else if (fip->field_writecount > 0
105 || fip->field_writecount == TIFF_VARIABLE) {
109 if (fip->field_writecount == TIFF_VARIABLE)
110 wc = atoi(argv[arg_index++]);
112 wc = fip->field_writecount;
114 if (argc - arg_index < wc) {
116 "Number of tag values is not enough. "
117 "Expected %d values for %s tag, got %d\n",
118 wc, fip->field_name, argc - arg_index);
126 switch (fip->field_type) {
128 * XXX: We can't use TIFFDataWidth()
129 * to determine the space needed to store
130 * the value. For TIFF_RATIONAL values
131 * TIFFDataWidth() returns 8, but we use 4-byte
132 * float to represent rationals.
161 array = _TIFFmalloc(wc * size);
163 fprintf(stderr, "No space for %s tag\n",
168 switch (fip->field_type) {
170 for (i = 0; i < wc; i++)
171 ((uint8 *)array)[i] = atoi(argv[arg_index+i]);
174 for (i = 0; i < wc; i++)
175 ((uint16 *)array)[i] = atoi(argv[arg_index+i]);
178 for (i = 0; i < wc; i++)
179 ((int8 *)array)[i] = atoi(argv[arg_index+i]);
182 for (i = 0; i < wc; i++)
183 ((int16 *)array)[i] = atoi(argv[arg_index+i]);
186 for (i = 0; i < wc; i++)
187 ((uint32 *)array)[i] = atol(argv[arg_index+i]);
191 for (i = 0; i < wc; i++)
192 ((uint32 *)array)[i] = atol(argv[arg_index+i]);
195 for (i = 0; i < wc; i++)
196 ((double *)array)[i] = atof(argv[arg_index+i]);
201 for (i = 0; i < wc; i++)
202 ((float *)array)[i] = (float)atof(argv[arg_index+i]);
208 if (fip->field_passcount) {
209 ret = TIFFSetField(tiff, fip->field_tag,
211 } else if (fip->field_tag == TIFFTAG_PAGENUMBER
212 || fip->field_tag == TIFFTAG_HALFTONEHINTS
213 || fip->field_tag == TIFFTAG_YCBCRSUBSAMPLING
214 || fip->field_tag == TIFFTAG_DOTRANGE) {
215 if (fip->field_type == TIFF_BYTE) {
216 ret = TIFFSetField(tiff, fip->field_tag,
217 ((uint8 *)array)[0], ((uint8 *)array)[1]);
218 } else if (fip->field_type == TIFF_SHORT) {
219 ret = TIFFSetField(tiff, fip->field_tag,
220 ((uint16 *)array)[0], ((uint16 *)array)[1]);
223 ret = TIFFSetField(tiff, fip->field_tag,
229 switch (fip->field_type) {
234 ret = TIFFSetField(tiff, fip->field_tag,
235 atoi(argv[arg_index++]));
240 ret = TIFFSetField(tiff, fip->field_tag,
241 atol(argv[arg_index++]));
244 ret = TIFFSetField(tiff, fip->field_tag,
245 atof(argv[arg_index++]));
250 ret = TIFFSetField(tiff, fip->field_tag,
251 (float)atof(argv[arg_index++]));
259 fprintf(stderr, "Failed to set %s\n", fip->field_name);
262 } else if (strcmp(argv[arg_index],"-sf") == 0 && arg_index < argc-3) {
264 const TIFFFieldInfo *fip;
269 fip = GetField(tiff, argv[arg_index]);
274 if (fip->field_type != TIFF_ASCII) {
276 "Only ASCII tags can be set from file. "
277 "%s is not ASCII tag.\n", fip->field_name );
282 fp = fopen( argv[arg_index], "rt" );
284 perror( argv[arg_index] );
288 text = (char *) malloc(1000000);
289 len = fread( text, 1, 999999, fp );
294 if(TIFFSetField( tiff, fip->field_tag, text ) != 1) {
295 fprintf(stderr, "Failed to set %s from file %s\n",
296 fip->field_name, argv[arg_index]);
302 fprintf(stderr, "Unrecognised option: %s\n",
308 TIFFRewriteDirectory(tiff);
313 /* vim: set ts=8 sts=8 sw=8 noet: */