1 /******************************************************************************
2  * $Id: tiffset.c,v 1.3 2002/01/16 17:50:05 warmerda Exp $
3  *
4  * Project:  libtiff tools
5  * Purpose:  Mainline for setting metadata in existing TIFF files.
6  * Author:   Frank Warmerdam, warmerda@home.com
7  *
8  ******************************************************************************
9  * Copyright (c) 2000, Frank Warmerdam
10  *
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.
18  *
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.
22  *
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
28  * OF THIS SOFTWARE.
29  ******************************************************************************
30  *
31  * $Log: tiffset.c,v $
32  * Revision 1.3  2002/01/16 17:50:05  warmerda
33  * Fix bug in error output.
34  *
35  * Revision 1.2  2001/09/26 17:42:18  warmerda
36  * added TIFFRewriteDirectory
37  *
38  * Revision 1.1  2001/03/02 04:58:53  warmerda
39  * New
40  *
41  */
42 
43 
44 #include <stdio.h>
45 #include <string.h>
46 #include <stdlib.h>
47 
48 #include "tiffiop.h"
49 
50 static ttag_t field_name_to_id( const char * );
51 
52 static char* usageMsg[] = {
53     "usage: tiffset [-s name value] filename\n",
54     NULL
55 };
56 
57 static void
usage(void)58 usage(void)
59 {
60 	int i;
61 	for (i = 0; usageMsg[i]; i++)
62 		fprintf(stderr, "%s", usageMsg[i]);
63 	exit(-1);
64 }
65 
66 int
main(int argc,char * argv[])67 main(int argc, char* argv[])
68 {
69     TIFF *tiff;
70     int  arg_index;
71 
72     if (argc < 2)
73         usage();
74 
75     tiff = TIFFOpen(argv[argc-1], "r+");
76     if (tiff == NULL)
77         return (-2);
78 
79     for( arg_index = 1; arg_index < argc-1; arg_index++ )
80     {
81         if( strcmp(argv[arg_index],"-s") == 0 && arg_index < argc-3 )
82         {
83             ttag_t  id;
84 
85             if( atoi(argv[arg_index+1]) > 0 )
86                 id = atoi(argv[arg_index+1]);
87             else
88                 id = field_name_to_id(argv[arg_index+1]);
89 
90             if( id < 1 )
91             {
92                 fprintf( stderr, "Field name %s not recognised.\n",
93                          argv[arg_index+1] );
94                 exit( -3 );
95             }
96 
97             if( TIFFSetField( tiff, id, argv[arg_index+2] ) != 1 )
98             {
99                 fprintf( stderr, "Failed to set %s=%s\n",
100                          argv[arg_index+1],
101                          argv[arg_index+2] );
102             }
103             arg_index += 2;
104         }
105         else if( strcmp(argv[arg_index],"-sf") == 0 && arg_index < argc-3 )
106         {
107             ttag_t  id;
108             FILE    *fp;
109             char    *text;
110             int     len;
111 
112             if( atoi(argv[arg_index+1]) > 0 )
113                 id = atoi(argv[arg_index+1]);
114             else
115                 id = field_name_to_id(argv[arg_index+1]);
116 
117             if( id < 1 )
118             {
119                 fprintf( stderr, "Field name %s not recognised.\n",
120                          argv[arg_index+1] );
121                 exit( -3 );
122             }
123 
124             fp = fopen( argv[arg_index+2], "rt" );
125             if( fp == NULL )
126             {
127                 perror( argv[arg_index+2] );
128                 continue;
129             }
130 
131             text = (char *) malloc(66000);
132             len = fread( text, 1, 65535, fp );
133             text[len] = '\0';
134 
135             fclose( fp );
136 
137             if( TIFFSetField( tiff, id, text ) != 1 )
138             {
139                 fprintf( stderr, "Failed to set %s=%s\n",
140                          argv[arg_index+1],
141                          argv[arg_index+2] );
142             }
143             free( text );
144             arg_index += 2;
145         }
146         else
147         {
148             fprintf( stderr, "Unrecognised option: %s\n",
149                      argv[arg_index] );
150             usage();
151         }
152     }
153 
154 #ifdef notdef
155     tiff->tif_header.tiff_diroff = 0;
156     tiff->tif_diroff = 0;
157 #endif
158 
159     TIFFRewriteDirectory(tiff);
160     TIFFClose(tiff);
161     return (0);
162 }
163 
field_name_to_id(const char * name)164 static ttag_t field_name_to_id( const char * name )
165 
166 {
167     if( strstr(name, "DESCRIPTION") != NULL
168         || strstr(name, "description") != NULL )
169         return TIFFTAG_IMAGEDESCRIPTION;
170 
171     else if( strstr(name, "SOFTWARE") != NULL
172         || strstr(name, "software") != NULL )
173         return TIFFTAG_SOFTWARE;
174 
175     else if( strstr(name, "COPYRIGHT") != NULL
176         || strstr(name, "copyright") != NULL )
177         return TIFFTAG_COPYRIGHT;
178 
179     else
180         return -1;
181 }
182