]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/tools/tiffset.c
1 /******************************************************************************
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 " -d <dirno> set the directory",
44 " -sd <diroff> set the subdirectory",
45 " -sf <tagname> <filename> read the tag value from file (for ASCII tags only)",
53 for (i
= 0; usageMsg
[i
]; i
++)
54 fprintf(stderr
, "%s\n", usageMsg
[i
]);
58 static const TIFFField
*
59 GetField(TIFF
*tiff
, const char *tagname
)
63 if( atoi(tagname
) > 0 )
64 fip
= TIFFFieldWithTag(tiff
, (ttag_t
)atoi(tagname
));
66 fip
= TIFFFieldWithName(tiff
, tagname
);
69 fprintf( stderr
, "Field name \"%s\" is not recognised.\n", tagname
);
70 return (TIFFField
*)NULL
;
77 main(int argc
, char* argv
[])
85 tiff
= TIFFOpen(argv
[argc
-1], "r+");
89 for( arg_index
= 1; arg_index
< argc
-1; arg_index
++ ) {
90 if (strcmp(argv
[arg_index
],"-d") == 0 && arg_index
< argc
-2) {
92 if( TIFFSetDirectory(tiff
, atoi(argv
[arg_index
]) ) != 1 )
94 fprintf( stderr
, "Failed to set directory=%s\n", argv
[arg_index
] );
99 if (strcmp(argv
[arg_index
],"-sd") == 0 && arg_index
< argc
-2) {
101 if( TIFFSetSubDirectory(tiff
, atoi(argv
[arg_index
]) ) != 1 )
103 fprintf( stderr
, "Failed to set sub directory=%s\n", argv
[arg_index
] );
108 if (strcmp(argv
[arg_index
],"-s") == 0 && arg_index
< argc
-3) {
109 const TIFFField
*fip
;
113 tagname
= argv
[arg_index
];
114 fip
= GetField(tiff
, tagname
);
120 if (TIFFFieldDataType(fip
) == TIFF_ASCII
) {
121 if (TIFFSetField(tiff
, TIFFFieldTag(fip
), argv
[arg_index
]) != 1)
122 fprintf( stderr
, "Failed to set %s=%s\n",
123 TIFFFieldName(fip
), argv
[arg_index
] );
124 } else if (TIFFFieldWriteCount(fip
) > 0
125 || TIFFFieldWriteCount(fip
) == TIFF_VARIABLE
) {
129 if (TIFFFieldWriteCount(fip
) == TIFF_VARIABLE
)
130 wc
= atoi(argv
[arg_index
++]);
132 wc
= TIFFFieldWriteCount(fip
);
134 if (argc
- arg_index
< wc
) {
136 "Number of tag values is not enough. "
137 "Expected %d values for %s tag, got %d\n",
138 wc
, TIFFFieldName(fip
), argc
- arg_index
);
146 switch (TIFFFieldDataType(fip
)) {
148 * XXX: We can't use TIFFDataWidth()
149 * to determine the space needed to store
150 * the value. For TIFF_RATIONAL values
151 * TIFFDataWidth() returns 8, but we use 4-byte
152 * float to represent rationals.
181 array
= _TIFFmalloc(wc
* size
);
183 fprintf(stderr
, "No space for %s tag\n",
188 switch (TIFFFieldDataType(fip
)) {
190 for (i
= 0; i
< wc
; i
++)
191 ((uint8
*)array
)[i
] = atoi(argv
[arg_index
+i
]);
194 for (i
= 0; i
< wc
; i
++)
195 ((uint16
*)array
)[i
] = atoi(argv
[arg_index
+i
]);
198 for (i
= 0; i
< wc
; i
++)
199 ((int8
*)array
)[i
] = atoi(argv
[arg_index
+i
]);
202 for (i
= 0; i
< wc
; i
++)
203 ((int16
*)array
)[i
] = atoi(argv
[arg_index
+i
]);
206 for (i
= 0; i
< wc
; i
++)
207 ((uint32
*)array
)[i
] = atol(argv
[arg_index
+i
]);
211 for (i
= 0; i
< wc
; i
++)
212 ((uint32
*)array
)[i
] = atol(argv
[arg_index
+i
]);
215 for (i
= 0; i
< wc
; i
++)
216 ((double *)array
)[i
] = atof(argv
[arg_index
+i
]);
221 for (i
= 0; i
< wc
; i
++)
222 ((float *)array
)[i
] = (float)atof(argv
[arg_index
+i
]);
228 if (TIFFFieldPassCount(fip
)) {
229 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
231 } else if (TIFFFieldTag(fip
) == TIFFTAG_PAGENUMBER
232 || TIFFFieldTag(fip
) == TIFFTAG_HALFTONEHINTS
233 || TIFFFieldTag(fip
) == TIFFTAG_YCBCRSUBSAMPLING
234 || TIFFFieldTag(fip
) == TIFFTAG_DOTRANGE
) {
235 if (TIFFFieldDataType(fip
) == TIFF_BYTE
) {
236 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
237 ((uint8
*)array
)[0], ((uint8
*)array
)[1]);
238 } else if (TIFFFieldDataType(fip
) == TIFF_SHORT
) {
239 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
240 ((uint16
*)array
)[0], ((uint16
*)array
)[1]);
243 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
249 switch (TIFFFieldDataType(fip
)) {
254 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
255 atoi(argv
[arg_index
++]));
260 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
261 atol(argv
[arg_index
++]));
264 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
265 atof(argv
[arg_index
++]));
270 ret
= TIFFSetField(tiff
, TIFFFieldTag(fip
),
271 (float)atof(argv
[arg_index
++]));
279 fprintf(stderr
, "Failed to set %s\n", TIFFFieldName(fip
));
282 } else if (strcmp(argv
[arg_index
],"-sf") == 0 && arg_index
< argc
-3) {
284 const TIFFField
*fip
;
289 fip
= GetField(tiff
, argv
[arg_index
]);
294 if (TIFFFieldDataType(fip
) != TIFF_ASCII
) {
296 "Only ASCII tags can be set from file. "
297 "%s is not ASCII tag.\n", TIFFFieldName(fip
) );
302 fp
= fopen( argv
[arg_index
], "rt" );
304 perror( argv
[arg_index
] );
308 text
= (char *) malloc(1000000);
309 len
= fread( text
, 1, 999999, fp
);
314 if(TIFFSetField( tiff
, TIFFFieldTag(fip
), text
) != 1) {
315 fprintf(stderr
, "Failed to set %s from file %s\n",
316 TIFFFieldName(fip
), argv
[arg_index
]);
322 fprintf(stderr
, "Unrecognised option: %s\n",
328 TIFFRewriteDirectory(tiff
);
333 /* vim: set ts=8 sts=8 sw=8 noet: */