1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
|
/******************************************************************************
* $Id: tiffset.c,v 1.11 2005/09/13 14:13:42 dron Exp $
*
* Project: libtiff tools
* Purpose: Mainline for setting metadata in existing TIFF files.
* Author: Frank Warmerdam, warmerdam@pobox.com
*
******************************************************************************
* Copyright (c) 2000, Frank Warmerdam
*
* Permission to use, copy, modify, distribute, and sell this software and
* its documentation for any purpose is hereby granted without fee, provided
* that (i) the above copyright notices and this permission notice appear in
* all copies of the software and related documentation, and (ii) the names of
* Sam Leffler and Silicon Graphics may not be used in any advertising or
* publicity relating to the software without the specific, prior written
* permission of Sam Leffler and Silicon Graphics.
*
* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
*
* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
* OF THIS SOFTWARE.
******************************************************************************
*
* $Log: tiffset.c,v $
* Revision 1.11 2005/09/13 14:13:42 dron
* Avoid warnings.
*
* Revision 1.10 2005/02/24 14:47:11 fwarmerdam
* Updated header.
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "tiffio.h"
static char* usageMsg[] = {
"usage: tiffset [options] filename",
"where options are:",
" -s <tagname> [count] <value>... set the tag value",
" -sf <tagname> <filename> read the tag value from file (for ASCII tags only)",
NULL
};
static void
usage(void)
{
int i;
for (i = 0; usageMsg[i]; i++)
fprintf(stderr, "%s\n", usageMsg[i]);
exit(-1);
}
static const TIFFFieldInfo *
GetField(TIFF *tiff, const char *tagname)
{
const TIFFFieldInfo *fip;
if( atoi(tagname) > 0 )
fip = TIFFFieldWithTag(tiff, (ttag_t)atoi(tagname));
else
fip = TIFFFieldWithName(tiff, tagname);
if (!fip) {
fprintf( stderr, "Field name %s not recognised.\n", tagname );
return (TIFFFieldInfo *)NULL;
}
return fip;
}
int
main(int argc, char* argv[])
{
TIFF *tiff;
int arg_index;
if (argc < 2)
usage();
tiff = TIFFOpen(argv[argc-1], "r+");
if (tiff == NULL)
return 2;
for( arg_index = 1; arg_index < argc-1; arg_index++ ) {
if (strcmp(argv[arg_index],"-s") == 0 && arg_index < argc-3) {
const TIFFFieldInfo *fip;
const char *tagname;
arg_index++;
tagname = argv[arg_index];
fip = GetField(tiff, tagname);
if (!fip)
return 3;
arg_index++;
if (fip->field_type == TIFF_ASCII) {
if (TIFFSetField(tiff, fip->field_tag, argv[arg_index]) != 1)
fprintf( stderr, "Failed to set %s=%s\n",
fip->field_name, argv[arg_index] );
} else if (fip->field_writecount > 0) {
int ret = 1;
short wc;
if (fip->field_writecount == TIFF_VARIABLE)
wc = atoi(argv[arg_index++]);
else
wc = fip->field_writecount;
if (argc - arg_index < wc) {
fprintf( stderr,
"Number of tag values is not enough. "
"Expected %d values for %s tag, got %d\n",
wc, fip->field_name, argc - arg_index);
return 4;
}
if (wc > 1) {
int i, size;
void *array;
switch (fip->field_type) {
/*
* XXX: We can't use TIFFDataWidth()
* to determine the space needed to store
* the value. For TIFF_RATIONAL values
* TIFFDataWidth() returns 8, but we use 4-byte
* float to represent rationals.
*/
case TIFF_BYTE:
case TIFF_ASCII:
case TIFF_SBYTE:
case TIFF_UNDEFINED:
default:
size = 1;
break;
case TIFF_SHORT:
case TIFF_SSHORT:
size = 2;
break;
case TIFF_LONG:
case TIFF_SLONG:
case TIFF_FLOAT:
case TIFF_IFD:
case TIFF_RATIONAL:
case TIFF_SRATIONAL:
size = 4;
break;
case TIFF_DOUBLE:
size = 8;
break;
}
array = _TIFFmalloc(wc * size);
if (!array) {
fprintf(stderr, "No space for %s tag\n",
tagname);
return 4;
}
switch (fip->field_type) {
case TIFF_BYTE:
for (i = 0; i < wc; i++)
((uint8 *)array)[i] = atoi(argv[arg_index+i]);
break;
case TIFF_SHORT:
for (i = 0; i < wc; i++)
((uint16 *)array)[i] = atoi(argv[arg_index+i]);
break;
case TIFF_SBYTE:
for (i = 0; i < wc; i++)
((int8 *)array)[i] = atoi(argv[arg_index+i]);
break;
case TIFF_SSHORT:
for (i = 0; i < wc; i++)
((int16 *)array)[i] = atoi(argv[arg_index+i]);
break;
case TIFF_LONG:
for (i = 0; i < wc; i++)
((uint32 *)array)[i] = atol(argv[arg_index+i]);
break;
case TIFF_SLONG:
case TIFF_IFD:
for (i = 0; i < wc; i++)
((uint32 *)array)[i] = atol(argv[arg_index+i]);
break;
case TIFF_DOUBLE:
for (i = 0; i < wc; i++)
((double *)array)[i] = atof(argv[arg_index+i]);
break;
case TIFF_RATIONAL:
case TIFF_SRATIONAL:
case TIFF_FLOAT:
for (i = 0; i < wc; i++)
((float *)array)[i] = (float)atof(argv[arg_index+i]);
break;
default:
break;
}
if (fip->field_passcount) {
ret = TIFFSetField(tiff, fip->field_tag,
wc, array);
} else {
ret = TIFFSetField(tiff, fip->field_tag,
array);
}
_TIFFfree(array);
} else {
switch (fip->field_type) {
case TIFF_BYTE:
case TIFF_SHORT:
case TIFF_SBYTE:
case TIFF_SSHORT:
ret = TIFFSetField(tiff, fip->field_tag,
atoi(argv[arg_index++]));
break;
case TIFF_LONG:
case TIFF_SLONG:
case TIFF_IFD:
ret = TIFFSetField(tiff, fip->field_tag,
atol(argv[arg_index++]));
break;
case TIFF_DOUBLE:
ret = TIFFSetField(tiff, fip->field_tag,
atof(argv[arg_index++]));
break;
case TIFF_RATIONAL:
case TIFF_SRATIONAL:
case TIFF_FLOAT:
ret = TIFFSetField(tiff, fip->field_tag,
(float)atof(argv[arg_index++]));
break;
default:
break;
}
}
if (ret != 1)
fprintf(stderr, "Failed to set %s\n", fip->field_name);
arg_index += wc;
}
} else if (strcmp(argv[arg_index],"-sf") == 0 && arg_index < argc-3) {
FILE *fp;
const TIFFFieldInfo *fip;
char *text;
int len;
arg_index++;
fip = GetField(tiff, argv[arg_index]);
if (!fip)
return 3;
if (fip->field_type != TIFF_ASCII) {
fprintf( stderr,
"Only ASCII tags can be set from file. "
"%s is not ASCII tag.\n", fip->field_name );
return 5;
}
arg_index++;
fp = fopen( argv[arg_index], "rt" );
if(fp == NULL) {
perror( argv[arg_index] );
continue;
}
text = (char *) malloc(1000000);
len = fread( text, 1, 999999, fp );
text[len] = '\0';
fclose( fp );
if(TIFFSetField( tiff, fip->field_tag, text ) != 1) {
fprintf(stderr, "Failed to set %s from file %s\n",
fip->field_name, argv[arg_index]);
}
_TIFFfree( text );
arg_index++;
} else {
fprintf(stderr, "Unrecognised option: %s\n",
argv[arg_index]);
usage();
}
}
TIFFRewriteDirectory(tiff);
TIFFClose(tiff);
return 0;
}
/* vim: set ts=8 sts=8 sw=8 noet: */
|