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