1 : /******************************************************************************
2 : * $Id: gdaladdo.cpp 25586 2013-01-30 20:26:44Z rouault $
3 : *
4 : * Project: GDAL Utilities
5 : * Purpose: Commandline application to build overviews.
6 : * Author: Frank Warmerdam, warmerdam@pobox.com
7 : *
8 : ******************************************************************************
9 : * Copyright (c) 2000, 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 : #include "gdal_priv.h"
31 : #include "cpl_string.h"
32 :
33 : CPL_CVSID("$Id: gdaladdo.cpp 25586 2013-01-30 20:26:44Z rouault $");
34 :
35 : /************************************************************************/
36 : /* Usage() */
37 : /************************************************************************/
38 :
39 0 : static void Usage(const char* pszErrorMsg = NULL)
40 :
41 : {
42 : printf( "Usage: gdaladdo [-r {nearest,average,gauss,cubic,average_mp,average_magphase,mode}]\n"
43 : " [-ro] [-clean] [-q] [--help-general] filename levels\n"
44 : "\n"
45 : " -r : choice of resampling method (default: nearest)\n"
46 : " -ro : open the dataset in read-only mode, in order to generate\n"
47 : " external overview (for GeoTIFF datasets especially)\n"
48 : " -clean : remove all overviews\n"
49 : " -q : turn off progress display\n"
50 : " -b : band to create overview (if not set overviews will be created for all bands)\n"
51 : " filename: The file to build overviews for (or whose overviews must be removed).\n"
52 : " levels: A list of integral overview levels to build. Ignored with -clean option.\n"
53 : "\n"
54 : "Usefull configuration variables :\n"
55 : " --config USE_RRD YES : Use Erdas Imagine format (.aux) as overview format.\n"
56 : "Below, only for external overviews in GeoTIFF format:\n"
57 : " --config COMPRESS_OVERVIEW {JPEG,LZW,PACKBITS,DEFLATE} : TIFF compression\n"
58 : " --config PHOTOMETRIC_OVERVIEW {RGB,YCBCR,...} : TIFF photometric interp.\n"
59 : " --config INTERLEAVE_OVERVIEW {PIXEL|BAND} : TIFF interleaving method\n"
60 : " --config BIGTIFF_OVERVIEW {IF_NEEDED|IF_SAFER|YES|NO} : is BigTIFF used\n"
61 : "\n"
62 : "Examples:\n"
63 : " %% gdaladdo -r average abc.tif 2 4 8 16\n"
64 : " %% gdaladdo --config COMPRESS_OVERVIEW JPEG\n"
65 : " --config PHOTOMETRIC_OVERVIEW YCBCR\n"
66 0 : " --config INTERLEAVE_OVERVIEW PIXEL -ro abc.tif 2 4 8 16\n");
67 :
68 0 : if( pszErrorMsg != NULL )
69 0 : fprintf(stderr, "\nFAILURE: %s\n", pszErrorMsg);
70 :
71 0 : exit( 1 );
72 : }
73 :
74 : /************************************************************************/
75 : /* main() */
76 : /************************************************************************/
77 :
78 : #define CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(nExtraArg) \
79 : do { if (iArg + nExtraArg >= nArgc) \
80 : Usage(CPLSPrintf("%s option requires %d argument(s)", papszArgv[iArg], nExtraArg)); } while(0)
81 :
82 6 : int main( int nArgc, char ** papszArgv )
83 :
84 : {
85 : GDALDatasetH hDataset;
86 6 : const char *pszResampling = "nearest";
87 6 : const char *pszFilename = NULL;
88 : int anLevels[1024];
89 6 : int nLevelCount = 0;
90 6 : int nResultStatus = 0;
91 6 : int bReadOnly = FALSE;
92 6 : int bClean = FALSE;
93 6 : GDALProgressFunc pfnProgress = GDALTermProgress;
94 6 : int *panBandList = NULL;
95 6 : int nBandCount = 0;
96 :
97 : /* Check that we are running against at least GDAL 1.7 */
98 : /* Note to developers : if we use newer API, please change the requirement */
99 6 : if (atoi(GDALVersionInfo("VERSION_NUM")) < 1700)
100 : {
101 : fprintf(stderr, "At least, GDAL >= 1.7.0 is required for this version of %s, "
102 0 : "which was compiled against GDAL %s\n", papszArgv[0], GDAL_RELEASE_NAME);
103 0 : exit(1);
104 : }
105 :
106 6 : GDALAllRegister();
107 :
108 6 : nArgc = GDALGeneralCmdLineProcessor( nArgc, &papszArgv, 0 );
109 6 : if( nArgc < 1 )
110 0 : exit( -nArgc );
111 :
112 : /* -------------------------------------------------------------------- */
113 : /* Parse commandline. */
114 : /* -------------------------------------------------------------------- */
115 20 : for( int iArg = 1; iArg < nArgc; iArg++ )
116 : {
117 15 : if( EQUAL(papszArgv[iArg], "--utility_version") )
118 : {
119 : printf("%s was compiled against GDAL %s and is running against GDAL %s\n",
120 1 : papszArgv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
121 1 : return 0;
122 : }
123 14 : else if( EQUAL(papszArgv[iArg],"--help") )
124 0 : Usage();
125 14 : else if( EQUAL(papszArgv[iArg],"-r") )
126 : {
127 1 : CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
128 1 : pszResampling = papszArgv[++iArg];
129 : }
130 13 : else if( EQUAL(papszArgv[iArg],"-ro"))
131 2 : bReadOnly = TRUE;
132 11 : else if( EQUAL(papszArgv[iArg],"-clean"))
133 1 : bClean = TRUE;
134 10 : else if( EQUAL(papszArgv[iArg],"-q") || EQUAL(papszArgv[iArg],"-quiet") )
135 0 : pfnProgress = GDALDummyProgress;
136 10 : else if( EQUAL(papszArgv[iArg],"-b"))
137 : {
138 0 : CHECK_HAS_ENOUGH_ADDITIONAL_ARGS(1);
139 0 : const char* pszBand = papszArgv[iArg+1];
140 0 : int nBand = atoi(pszBand);
141 0 : if( nBand < 1 )
142 : {
143 0 : printf( "Unrecognizable band number (%s).\n", papszArgv[iArg+1] );
144 0 : Usage();
145 0 : GDALDestroyDriverManager();
146 0 : exit( 2 );
147 : }
148 0 : iArg++;
149 :
150 0 : nBandCount++;
151 : panBandList = (int *)
152 0 : CPLRealloc(panBandList, sizeof(int) * nBandCount);
153 0 : panBandList[nBandCount-1] = nBand;
154 : }
155 10 : else if( papszArgv[iArg][0] == '-' )
156 0 : Usage(CPLSPrintf("Unkown option name '%s'", papszArgv[iArg]));
157 10 : else if( pszFilename == NULL )
158 5 : pszFilename = papszArgv[iArg];
159 5 : else if( atoi(papszArgv[iArg]) > 0 )
160 5 : anLevels[nLevelCount++] = atoi(papszArgv[iArg]);
161 : else
162 0 : Usage("Too many command options.");
163 : }
164 :
165 5 : if( pszFilename == NULL )
166 0 : Usage("No datasource specified.");
167 :
168 5 : if( nLevelCount == 0 && !bClean )
169 0 : Usage("No overview level specified.");
170 :
171 : /* -------------------------------------------------------------------- */
172 : /* Open data file. */
173 : /* -------------------------------------------------------------------- */
174 5 : if (bReadOnly)
175 2 : hDataset = NULL;
176 : else
177 : {
178 3 : CPLPushErrorHandler( CPLQuietErrorHandler );
179 3 : hDataset = GDALOpen( pszFilename, GA_Update );
180 3 : CPLPopErrorHandler();
181 : }
182 :
183 5 : if( hDataset == NULL )
184 2 : hDataset = GDALOpen( pszFilename, GA_ReadOnly );
185 :
186 5 : if( hDataset == NULL )
187 0 : exit( 2 );
188 :
189 : /* -------------------------------------------------------------------- */
190 : /* Clean overviews. */
191 : /* -------------------------------------------------------------------- */
192 5 : if ( bClean &&
193 : GDALBuildOverviews( hDataset,pszResampling, 0, 0,
194 : 0, NULL, pfnProgress, NULL ) != CE_None )
195 : {
196 0 : printf( "Cleaning overviews failed.\n" );
197 0 : nResultStatus = 200;
198 : }
199 :
200 : /* -------------------------------------------------------------------- */
201 : /* Generate overviews. */
202 : /* -------------------------------------------------------------------- */
203 :
204 : //Only HFA support selected layers
205 5 : if(nBandCount > 0)
206 0 : CPLSetConfigOption( "USE_RRD", "YES" );
207 :
208 5 : if (nLevelCount > 0 && nResultStatus == 0 &&
209 : GDALBuildOverviews( hDataset,pszResampling, nLevelCount, anLevels,
210 : nBandCount, panBandList, pfnProgress, NULL ) != CE_None )
211 : {
212 0 : printf( "Overview building failed.\n" );
213 0 : nResultStatus = 100;
214 : }
215 :
216 : /* -------------------------------------------------------------------- */
217 : /* Cleanup */
218 : /* -------------------------------------------------------------------- */
219 5 : GDALClose(hDataset);
220 :
221 5 : CSLDestroy( papszArgv );
222 5 : CPLFree(panBandList);
223 5 : GDALDestroyDriverManager();
224 :
225 5 : return nResultStatus;
226 : }
|