]>
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 ******************************************************************************
32 * Revision 1.11 2005/09/13 14:13:42 dron
35 * Revision 1.10 2005/02/24 14:47:11 fwarmerdam
47 static char* usageMsg
[] = {
48 "usage: tiffset [options] filename",
50 " -s <tagname> [count] <value>... set the tag value",
51 " -sf <tagname> <filename> read the tag value from file (for ASCII tags only)",
59 for (i
= 0; usageMsg
[i
]; i
++)
60 fprintf(stderr
, "%s\n", usageMsg
[i
]);
64 static const TIFFFieldInfo
*
65 GetField(TIFF
*tiff
, const char *tagname
)
67 const TIFFFieldInfo
*fip
;
69 if( atoi(tagname
) > 0 )
70 fip
= TIFFFieldWithTag(tiff
, (ttag_t
)atoi(tagname
));
72 fip
= TIFFFieldWithName(tiff
, tagname
);
75 fprintf( stderr
, "Field name %s not recognised.\n", tagname
);
76 return (TIFFFieldInfo
*)NULL
;
83 main(int argc
, char* argv
[])
91 tiff
= TIFFOpen(argv
[argc
-1], "r+");
95 for( arg_index
= 1; arg_index
< argc
-1; arg_index
++ ) {
96 if (strcmp(argv
[arg_index
],"-s") == 0 && arg_index
< argc
-3) {
97 const TIFFFieldInfo
*fip
;
101 tagname
= argv
[arg_index
];
102 fip
= GetField(tiff
, tagname
);
108 if (fip
->field_type
== TIFF_ASCII
) {
109 if (TIFFSetField(tiff
, fip
->field_tag
, argv
[arg_index
]) != 1)
110 fprintf( stderr
, "Failed to set %s=%s\n",
111 fip
->field_name
, argv
[arg_index
] );
112 } else if (fip
->field_writecount
> 0) {
116 if (fip
->field_writecount
== TIFF_VARIABLE
)
117 wc
= atoi(argv
[arg_index
++]);
119 wc
= fip
->field_writecount
;
121 if (argc
- arg_index
< wc
) {
123 "Number of tag values is not enough. "
124 "Expected %d values for %s tag, got %d\n",
125 wc
, fip
->field_name
, argc
- arg_index
);
133 switch (fip
->field_type
) {
135 * XXX: We can't use TIFFDataWidth()
136 * to determine the space needed to store
137 * the value. For TIFF_RATIONAL values
138 * TIFFDataWidth() returns 8, but we use 4-byte
139 * float to represent rationals.
168 array
= _TIFFmalloc(wc
* size
);
170 fprintf(stderr
, "No space for %s tag\n",
175 switch (fip
->field_type
) {
177 for (i
= 0; i
< wc
; i
++)
178 ((uint8
*)array
)[i
] = atoi(argv
[arg_index
+i
]);
181 for (i
= 0; i
< wc
; i
++)
182 ((uint16
*)array
)[i
] = atoi(argv
[arg_index
+i
]);
185 for (i
= 0; i
< wc
; i
++)
186 ((int8
*)array
)[i
] = atoi(argv
[arg_index
+i
]);
189 for (i
= 0; i
< wc
; i
++)
190 ((int16
*)array
)[i
] = atoi(argv
[arg_index
+i
]);
193 for (i
= 0; i
< wc
; i
++)
194 ((uint32
*)array
)[i
] = atol(argv
[arg_index
+i
]);
198 for (i
= 0; i
< wc
; i
++)
199 ((uint32
*)array
)[i
] = atol(argv
[arg_index
+i
]);
202 for (i
= 0; i
< wc
; i
++)
203 ((double *)array
)[i
] = atof(argv
[arg_index
+i
]);
208 for (i
= 0; i
< wc
; i
++)
209 ((float *)array
)[i
] = (float)atof(argv
[arg_index
+i
]);
215 if (fip
->field_passcount
) {
216 ret
= TIFFSetField(tiff
, fip
->field_tag
,
219 ret
= TIFFSetField(tiff
, fip
->field_tag
,
225 switch (fip
->field_type
) {
230 ret
= TIFFSetField(tiff
, fip
->field_tag
,
231 atoi(argv
[arg_index
++]));
236 ret
= TIFFSetField(tiff
, fip
->field_tag
,
237 atol(argv
[arg_index
++]));
240 ret
= TIFFSetField(tiff
, fip
->field_tag
,
241 atof(argv
[arg_index
++]));
246 ret
= TIFFSetField(tiff
, fip
->field_tag
,
247 (float)atof(argv
[arg_index
++]));
255 fprintf(stderr
, "Failed to set %s\n", fip
->field_name
);
258 } else if (strcmp(argv
[arg_index
],"-sf") == 0 && arg_index
< argc
-3) {
260 const TIFFFieldInfo
*fip
;
265 fip
= GetField(tiff
, argv
[arg_index
]);
270 if (fip
->field_type
!= TIFF_ASCII
) {
272 "Only ASCII tags can be set from file. "
273 "%s is not ASCII tag.\n", fip
->field_name
);
278 fp
= fopen( argv
[arg_index
], "rt" );
280 perror( argv
[arg_index
] );
284 text
= (char *) malloc(1000000);
285 len
= fread( text
, 1, 999999, fp
);
290 if(TIFFSetField( tiff
, fip
->field_tag
, text
) != 1) {
291 fprintf(stderr
, "Failed to set %s from file %s\n",
292 fip
->field_name
, argv
[arg_index
]);
298 fprintf(stderr
, "Unrecognised option: %s\n",
304 TIFFRewriteDirectory(tiff
);
309 /* vim: set ts=8 sts=8 sw=8 noet: */