diff options
author | Andrey Kiselev <dron@ak4719.spb.edu> | 2004-06-05 09:05:26 +0000 |
---|---|---|
committer | Andrey Kiselev <dron@ak4719.spb.edu> | 2004-06-05 09:05:26 +0000 |
commit | 7d2f892d06bd3d5268378f63e0397b7d93a0b0a6 (patch) | |
tree | ef6ea9991596ae1309f2e1ec490cfa57c5951f68 /tools/tiffset.c | |
parent | 04c1e2a2852ddd4bc51908423969c57e23dbe053 (diff) | |
download | libtiff-git-7d2f892d06bd3d5268378f63e0397b7d93a0b0a6.tar.gz |
tiffset now can set any libtiff supported tags. Tags can be supplied by the
mnemonic name or number.
Diffstat (limited to 'tools/tiffset.c')
-rw-r--r-- | tools/tiffset.c | 191 |
1 files changed, 130 insertions, 61 deletions
diff --git a/tools/tiffset.c b/tools/tiffset.c index 751d4bb5..d8d3a2d6 100644 --- a/tools/tiffset.c +++ b/tools/tiffset.c @@ -1,5 +1,5 @@ /****************************************************************************** - * $Id: tiffset.c,v 1.4 2004-05-03 16:39:11 warmerda Exp $ + * $Id: tiffset.c,v 1.5 2004-06-05 09:05:26 dron Exp $ * * Project: libtiff tools * Purpose: Mainline for setting metadata in existing TIFF files. @@ -29,7 +29,11 @@ ****************************************************************************** * * $Log: tiffset.c,v $ - * Revision 1.4 2004-05-03 16:39:11 warmerda + * Revision 1.5 2004-06-05 09:05:26 dron + * tiffset now can set any libtiff supported tags. Tags can be supplied by the + * mnemonic name or number. + * + * Revision 1.4 2004/05/03 16:39:11 warmerda * Increase -sf buffer size. * * Revision 1.3 2002/01/16 17:50:05 warmerda @@ -48,13 +52,14 @@ #include <string.h> #include <stdlib.h> -#include "tiffiop.h" - -static ttag_t field_name_to_id( const char * ); +#include "tiffio.h" static char* usageMsg[] = { - "usage: tiffset [-s name value] filename\n", - NULL +"usage: tiffset [options] filename", +"where options are:", +" -s <tagname> <value>... set the tag value", +" -sf <tagname> <filename> read the tag value from file (for ASCII tags only)", +NULL }; static void @@ -66,6 +71,24 @@ usage(void) exit(-1); } +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[]) { @@ -77,57 +100,126 @@ main(int argc, char* argv[]) tiff = TIFFOpen(argv[argc-1], "r+"); if (tiff == NULL) - return (-2); + return -2; for( arg_index = 1; arg_index < argc-1; arg_index++ ) { if( strcmp(argv[arg_index],"-s") == 0 && arg_index < argc-3 ) { - ttag_t id; + const TIFFFieldInfo *fip; - if( atoi(argv[arg_index+1]) > 0 ) - id = atoi(argv[arg_index+1]); - else - id = field_name_to_id(argv[arg_index+1]); + arg_index++; + fip = GetField(tiff, argv[arg_index]); - if( id < 1 ) + if (!fip) + return -3; + + arg_index++; + if (fip->field_type == TIFF_ASCII) { - fprintf( stderr, "Field name %s not recognised.\n", - argv[arg_index+1] ); - exit( -3 ); + TIFFSetField(tiff, fip->field_tag, argv[arg_index]); + { + fprintf( stderr, "Failed to set %s=%s\n", + fip->field_name, argv[arg_index] ); + } } - if( TIFFSetField( tiff, id, argv[arg_index+2] ) != 1 ) + else if (fip->field_writecount > 0) { - fprintf( stderr, "Failed to set %s=%s\n", - argv[arg_index+1], - argv[arg_index+2] ); + int i; + void *array; + + if (argc - arg_index < fip->field_writecount) + { + fprintf( stderr, + "Too few tag values: %d. " + "Expected %d values for %s tag\n", + argc - arg_index, fip->field_writecount, + fip->field_name ); + return -4; + } + + + array = malloc(fip->field_writecount + * TIFFDataWidth(fip->field_type)); + + switch (fip->field_type) + { + case TIFF_BYTE: + for (i = 0; i < fip->field_writecount; i++) + ((uint8 *)array)[i] = atoi(argv[arg_index+i]); + break; + case TIFF_SHORT: + for (i = 0; i < fip->field_writecount; i++) + ((uint16 *)array)[i] = atoi(argv[arg_index+i]); + break; + case TIFF_SBYTE: + for (i = 0; i < fip->field_writecount; i++) + ((int8 *)array)[i] = atoi(argv[arg_index+i]); + break; + case TIFF_SSHORT: + for (i = 0; i < fip->field_writecount; i++) + ((int16 *)array)[i] = atoi(argv[arg_index+i]); + break; + case TIFF_LONG: + for (i = 0; i < fip->field_writecount; i++) + ((uint32 *)array)[i] = atol(argv[arg_index+i]); + break; + case TIFF_SLONG: + case TIFF_IFD: + for (i = 0; i < fip->field_writecount; i++) + ((uint32 *)array)[i] = atol(argv[arg_index+i]); + break; + case TIFF_RATIONAL: + case TIFF_SRATIONAL: + case TIFF_DOUBLE: + for (i = 0; i < fip->field_writecount; i++) + { + ((double *)array)[i] = atof(argv[arg_index+i]); + printf("%f\n", ((double *)array)[i]); + } + break; + case TIFF_FLOAT: + for (i = 0; i < fip->field_writecount; i++) + ((float *)array)[i] = (float)atof(argv[arg_index+i]); + break; + default: + break; + } + + if( TIFFSetField(tiff, fip->field_tag, array) != 1 ) + fprintf( stderr, "Failed to set %s\n", fip->field_name ); + arg_index += fip->field_writecount; + + free(array); } - arg_index += 2; } else if( strcmp(argv[arg_index],"-sf") == 0 && arg_index < argc-3 ) { - ttag_t id; FILE *fp; + const TIFFFieldInfo *fip; char *text; int len; - if( atoi(argv[arg_index+1]) > 0 ) - id = atoi(argv[arg_index+1]); - else - id = field_name_to_id(argv[arg_index+1]); + arg_index++; + fip = GetField(tiff, argv[arg_index]); + + if (!fip) + return -3; - if( id < 1 ) + if (fip->field_type != TIFF_ASCII) { - fprintf( stderr, "Field name %s not recognised.\n", - argv[arg_index+1] ); - exit( -3 ); + fprintf( stderr, + "Only ASCII tags can be set from file. " + "%s is not ASCII tag.\n", fip->field_name ); + return -5; } - fp = fopen( argv[arg_index+2], "rt" ); + arg_index++; + fp = fopen( argv[arg_index], "rt" ); if( fp == NULL ) { - perror( argv[arg_index+2] ); + perror( argv[arg_index] ); continue; } @@ -137,14 +229,14 @@ main(int argc, char* argv[]) fclose( fp ); - if( TIFFSetField( tiff, id, text ) != 1 ) + if( TIFFSetField( tiff, fip->field_tag, text ) != 1 ) { - fprintf( stderr, "Failed to set %s=%s\n", - argv[arg_index+1], - argv[arg_index+2] ); + fprintf( stderr, "Failed to set %s from file %s\n", + fip->field_name, argv[arg_index] ); } + free( text ); - arg_index += 2; + arg_index++; } else { @@ -154,31 +246,8 @@ main(int argc, char* argv[]) } } -#ifdef notdef - tiff->tif_header.tiff_diroff = 0; - tiff->tif_diroff = 0; -#endif - TIFFRewriteDirectory(tiff); TIFFClose(tiff); return (0); } -static ttag_t field_name_to_id( const char * name ) - -{ - if( strstr(name, "DESCRIPTION") != NULL - || strstr(name, "description") != NULL ) - return TIFFTAG_IMAGEDESCRIPTION; - - else if( strstr(name, "SOFTWARE") != NULL - || strstr(name, "software") != NULL ) - return TIFFTAG_SOFTWARE; - - else if( strstr(name, "COPYRIGHT") != NULL - || strstr(name, "copyright") != NULL ) - return TIFFTAG_COPYRIGHT; - - else - return -1; -} |