]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/tools/tiffset.c
1 /******************************************************************************
3 * Project: libtiff tools
4 * Purpose: Mainline for setting metadata in existing TIFF files.
5 * Author: Frank Warmerdam, warmerdam@pobox.com
7 ******************************************************************************
8 * Copyright (c) 2000, Frank Warmerdam
10 * Permission to use, copy, modify, distribute, and sell this software and
11 * its documentation for any purpose is hereby granted without fee, provided
12 * that (i) the above copyright notices and this permission notice appear in
13 * all copies of the software and related documentation, and (ii) the names of
14 * Sam Leffler and Silicon Graphics may not be used in any advertising or
15 * publicity relating to the software without the specific, prior written
16 * permission of Sam Leffler and Silicon Graphics.
18 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
20 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
22 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
23 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
24 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
25 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
26 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
28 ******************************************************************************
38 static char* usageMsg
[] = {
39 "usage: tiffset [options] filename",
41 " -s <tagname> [count] <value>... set the tag value",
42 " -d <dirno> set the directory",
43 " -sd <diroff> set the subdirectory",
44 " -sf <tagname> <filename> read the tag value from file (for ASCII tags only)",
52 for (i
= 0; usageMsg
[i
]; i
++)
53 fprintf(stderr
, "%s\n", usageMsg
[i
]);
57 static const TIFFField
*
58 GetField(TIFF
*tiff
, const char *tagname
)
62 if( atoi(tagname
) > 0 )
63 fip
= TIFFFieldWithTag(tiff
, (ttag_t
)atoi(tagname
));
65 fip
= TIFFFieldWithName(tiff
, tagname
);
68 fprintf( stderr
, "Field name \"%s\" is not recognised.\n", tagname
);
69 return (TIFFField
*)NULL
;
76 main(int argc
, char* argv
[])
84 tiff
= TIFFOpen(argv
[argc
-1], "r+");
88 for( arg_index
= 1; arg_index
< argc
-1; arg_index
++ ) {
89 if (strcmp(argv
[arg_index
],"-d") == 0 && arg_index
< argc
-2) {
91 if( TIFFSetDirectory(tiff
, atoi(argv
[arg_index
]) ) != 1 )
93 fprintf( stderr
, "Failed to set directory=%s\n", argv
[arg_index
] );
98 if (strcmp(argv
[arg_index
],"-sd") == 0 && arg_index
< argc
-2) {
100 if( TIFFSetSubDirectory(tiff
, atoi(argv
[arg_index
]) ) != 1 )
102 fprintf( stderr
, "Failed to set sub directory=%s\n", argv
[arg_index
] );
107 if (strcmp(argv
[arg_index
],"-s") == 0 && arg_index
< argc
-3) {
108 const TIFFField
*fip
;
112 tagname
= argv
[arg_index
];
113 fip
= GetField(tiff
, tagname
);
119 if (TIFFFieldDataType(fip
) == TIFF_ASCII
) {
120 if (TIFFSetField(tiff
, TIFFFieldTag(fip
), argv
[arg_index
]) != 1)
121 fprintf( stderr
, "Failed to set %s=%s\n",
122 TIFFFieldName(fip
), argv
[arg_index
] );
123 } else if (TIFFFieldWriteCount(fip
) > 0
124 || TIFFFieldWriteCount(fip
) == TIFF_VARIABLE
) {
128 if (TIFFFieldWriteCount(fip
) == TIFF_VARIABLE
)
129 wc
= atoi(argv
[arg_index
++]);
131 wc
= TIFFFieldWriteCount(fip
);
133 if (argc
- arg_index
< wc
) {
135 "Number of tag values is not enough. "
136 "Expected %d values for %s tag, got %d\n",
137 wc
, TIFFFieldName(fip
), argc
- arg_index
);
145 switch (TIFFFieldDataType(fip
)) {
147 * XXX: We can't use TIFFDataWidth()
148 * to determine the space needed to store
149 * the value. For TIFF_RATIONAL values
150 * TIFFDataWidth() returns 8, but we use 4-byte
151 * float to represent rationals.
180 array
= _TIFFmalloc(wc
* size
);
182 fprintf(stderr
, "No space for %s tag\n",
187 switch (TIFFFieldDataType(fip
)) {
189 for (i
= 0; i
< wc
; i
++)
190 ((uint8
*)array
)[i
] = atoi(argv
[arg_index
+i
]);
193 for (i
= 0; i
< wc
; i
++)
194 ((uint16
*)array
)[i
] = atoi(argv
[arg_index
+i
]);
197 for (i
= 0; i
< wc
; i
++)
198 ((int8
*)array
)[i
] = atoi(argv
[arg_index
+i
]);
201 for (i
= 0; i
< wc
; i
++)
202 ((int16
*)array
)[i
] = atoi(argv
[arg_index
+i
]);
205 for (i
= 0; i
< wc
; i
++)
206 ((uint32
*)array
)[i
] = atol(argv
[arg_index
+i
]);
210 for (i
= 0; i
< wc
; i
++)
211 ((uint32
*)array
)[i
] = atol(argv
[arg_index
+i
]);
214 for (i
= 0; i
< wc
; i
++)
215 ((double *)array
)[i
] = atof(argv
[arg_index
+i
]);
220 for (i
= 0; i
< wc
; i
++)
221 ((float *)array
)[i
] = (float)atof(argv
[arg_index
+i
]);
227 if (TIFFFieldPassCount(fip
)) {
228 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
230 } else if (TIFFFieldTag(fip
) == TIFFTAG_PAGENUMBER
231 || TIFFFieldTag(fip
) == TIFFTAG_HALFTONEHINTS
232 || TIFFFieldTag(fip
) == TIFFTAG_YCBCRSUBSAMPLING
233 || TIFFFieldTag(fip
) == TIFFTAG_DOTRANGE
) {
234 if (TIFFFieldDataType(fip
) == TIFF_BYTE
) {
235 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
236 ((uint8
*)array
)[0], ((uint8
*)array
)[1]);
237 } else if (TIFFFieldDataType(fip
) == TIFF_SHORT
) {
238 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
239 ((uint16
*)array
)[0], ((uint16
*)array
)[1]);
242 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
248 switch (TIFFFieldDataType(fip
)) {
253 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
254 atoi(argv
[arg_index
++]));
259 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
260 atol(argv
[arg_index
++]));
263 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
264 atof(argv
[arg_index
++]));
269 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
270 (float)atof(argv
[arg_index
++]));
278 fprintf(stderr
, "Failed to set %s\n", TIFFFieldName(fip
));
281 } else if (strcmp(argv
[arg_index
],"-sf") == 0 && arg_index
< argc
-3) {
283 const TIFFField
*fip
;
288 fip
= GetField(tiff
, argv
[arg_index
]);
293 if (TIFFFieldDataType(fip
) != TIFF_ASCII
) {
295 "Only ASCII tags can be set from file. "
296 "%s is not ASCII tag.\n", TIFFFieldName(fip
) );
301 fp
= fopen( argv
[arg_index
], "rt" );
303 perror( argv
[arg_index
] );
307 text
= (char *) malloc(1000000);
308 len
= fread( text
, 1, 999999, fp
);
313 if(TIFFSetField( tiff
, TIFFFieldTag(fip
), text
) != 1) {
314 fprintf(stderr
, "Failed to set %s from file %s\n",
315 TIFFFieldName(fip
), argv
[arg_index
]);
321 fprintf(stderr
, "Unrecognised option: %s\n",
327 TIFFRewriteDirectory(tiff
);
332 /* vim: set ts=8 sts=8 sw=8 noet: */