1 /******************************************************************************
2  * $Id: addtiffo.c,v 1.8 2015-05-30 20:30:27 bfriesen Exp $
3  *
4  * Project:  GeoTIFF Overview Builder
5  * Purpose:  Mainline for building overviews in a TIFF file.
6  * Author:   Frank Warmerdam, warmerdam@pobox.com
7  *
8  ******************************************************************************
9  * Copyright (c) 1999, Frank Warmerdam
10  *
11  * Permission is hereby granted, free of charge, to any person obtaining a
12  * copy of this software and associated documentation files (the "Software"),
13  * to deal in the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15  * and/or sell copies of the Software, and to permit persons to whom the
16  * Software is furnished to do so, subject to the following conditions:
17  *
18  * The above copyright notice and this permission notice shall be included
19  * in all copies or substantial portions of the Software.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  ******************************************************************************
29  *
30  * $Log: addtiffo.c,v $
31  * Revision 1.8  2015-05-30 20:30:27  bfriesen
32  * * contrib/addtiffo/addtiffo.c (main): Possibly address Coverity
33  * 1024226 "Untrusted value as argument".
34  *
35  * Revision 1.7  2010-06-08 18:55:15  bfriesen
36  * * contrib: Add an emacs formatting mode footer to all source files
37  * so that emacs can be effectively used.
38  *
39  * Revision 1.6  2005/12/16 05:59:55  fwarmerdam
40  * Major upgrade to support YCbCr subsampled jpeg images
41  *
42  * Revision 1.4  2004/09/21 13:31:23  dron
43  * Add missed include string.h.
44  *
45  * Revision 1.3  2000/04/18 22:48:31  warmerda
46  * Added support for averaging resampling
47  *
48  * Revision 1.2  2000/01/28 15:36:38  warmerda
49  * pass TIFF handle instead of filename to overview builder
50  *
51  * Revision 1.1  1999/08/17 01:47:59  warmerda
52  * New
53  *
54  * Revision 1.1  1999/03/12 17:46:32  warmerda
55  * New
56  *
57  * Revision 1.2  1999/02/11 22:27:12  warmerda
58  * Added multi-sample support
59  *
60  * Revision 1.1  1999/02/11 18:12:30  warmerda
61  * New
62  */
63 
64 #include <stdio.h>
65 #include <stdlib.h>
66 #include <string.h>
67 #include "tiffio.h"
68 
69 void TIFFBuildOverviews( TIFF *, int, int *, int, const char *,
70                          int (*)(double,void*), void * );
71 
72 /************************************************************************/
73 /*                                main()                                */
74 /************************************************************************/
75 
main(int argc,char ** argv)76 int main( int argc, char ** argv )
77 
78 {
79     int		anOverviews[100];   /* TODO: un-hardwire array length, flexible allocate */
80     int		nOverviewCount = 0;
81     int		bUseSubIFD = 0;
82     TIFF	*hTIFF;
83     const char  *pszResampling = "nearest";
84 
85 /* -------------------------------------------------------------------- */
86 /*      Usage:                                                          */
87 /* -------------------------------------------------------------------- */
88     if( argc < 2 )
89     {
90         printf( "Usage: addtiffo [-r {nearest,average,mode}]\n"
91                 "                tiff_filename [resolution_reductions]\n"
92                 "\n"
93                 "Example:\n"
94                 " %% addtiffo abc.tif 2 4 8 16\n" );
95         return( 1 );
96     }
97 
98     while( argv[1][0] == '-' )
99     {
100         if( strcmp(argv[1],"-subifd") == 0 )
101         {
102             bUseSubIFD = 1;
103             argv++;
104             argc--;
105         }
106         else if( strcmp(argv[1],"-r") == 0 )
107         {
108             argv += 2;
109             argc -= 2;
110             pszResampling = *argv;
111         }
112         else
113         {
114             fprintf( stderr, "Incorrect parameters\n" );
115             return( 1 );
116         }
117     }
118 
119     /* TODO: resampling mode parameter needs to be encoded in an integer from this point on */
120 
121 /* -------------------------------------------------------------------- */
122 /*      Collect the user requested reduction factors.                   */
123 /* -------------------------------------------------------------------- */
124     while( nOverviewCount < argc - 2 && nOverviewCount < 100 )
125     {
126         anOverviews[nOverviewCount] = atoi(argv[nOverviewCount+2]);
127         if( (anOverviews[nOverviewCount] <= 0) ||
128             ((anOverviews[nOverviewCount] > 1024)))
129         {
130             fprintf( stderr, "Incorrect parameters\n" );
131             return(1);
132         }
133         nOverviewCount++;
134     }
135 
136 /* -------------------------------------------------------------------- */
137 /*      Default to four overview levels.  It would be nicer if it       */
138 /*      defaulted based on the size of the source image.                */
139 /* -------------------------------------------------------------------- */
140     /* TODO: make it default based on the size of the source image */
141     if( nOverviewCount == 0 )
142     {
143         nOverviewCount = 4;
144 
145         anOverviews[0] = 2;
146         anOverviews[1] = 4;
147         anOverviews[2] = 8;
148         anOverviews[3] = 16;
149     }
150 
151 /* -------------------------------------------------------------------- */
152 /*      Build the overview.                                             */
153 /* -------------------------------------------------------------------- */
154     hTIFF = TIFFOpen( argv[1], "r+" );
155     if( hTIFF == NULL )
156     {
157         fprintf( stderr, "TIFFOpen(%s) failed.\n", argv[1] );
158         return( 1 );
159     }
160 
161     TIFFBuildOverviews( hTIFF, nOverviewCount, anOverviews, bUseSubIFD,
162                         pszResampling, NULL, NULL );
163 
164     TIFFClose( hTIFF );
165 
166 /* -------------------------------------------------------------------- */
167 /*      Optionally test for memory leaks.                               */
168 /* -------------------------------------------------------------------- */
169 #ifdef DBMALLOC
170     malloc_dump(1);
171 #endif
172 
173     return( 0 );
174 }
175 /*
176  * Local Variables:
177  * mode: c
178  * c-basic-offset: 4
179  * fill-column: 78
180  * End:
181  */
182