1 : /******************************************************************************
2 : * $Id: gdaladdo.cpp 18306 2009-12-15 18:57:11Z 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 18306 2009-12-15 18:57:11Z rouault $");
34 :
35 : /************************************************************************/
36 : /* Usage() */
37 : /************************************************************************/
38 :
39 0 : static void Usage()
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 : " filename: The file to build overviews for (or whose overviews must be removed).\n"
51 : " levels: A list of integral overview levels to build. Ignored with -clean option.\n"
52 : "\n"
53 : "Usefull configuration variables :\n"
54 : " --config USE_RRD YES : Use Erdas Imagine format (.aux) as overview format.\n"
55 : "Below, only for external overviews in GeoTIFF format:\n"
56 : " --config COMPRESS_OVERVIEW {JPEG,LZW,PACKBITS,DEFLATE} : TIFF compression\n"
57 : " --config PHOTOMETRIC_OVERVIEW {RGB,YCBCR,...} : TIFF photometric interp.\n"
58 : " --config INTERLEAVE_OVERVIEW {PIXEL|BAND} : TIFF interleaving method\n"
59 : " --config BIGTIFF_OVERVIEW {IF_NEEDED|IF_SAFER|YES|NO} : is BigTIFF used\n"
60 : "\n"
61 : "Examples:\n"
62 : " %% gdaladdo -r average abc.tif 2 4 8 16\n"
63 : " %% gdaladdo --config COMPRESS_OVERVIEW JPEG\n"
64 : " --config PHOTOMETRIC_OVERVIEW YCBCR\n"
65 0 : " --config INTERLEAVE_OVERVIEW PIXEL -ro abc.tif 2 4 8 16\n");
66 0 : exit( 1 );
67 : }
68 :
69 : /************************************************************************/
70 : /* main() */
71 : /************************************************************************/
72 :
73 12 : int main( int nArgc, char ** papszArgv )
74 :
75 : {
76 : GDALDatasetH hDataset;
77 12 : const char *pszResampling = "nearest";
78 12 : const char *pszFilename = NULL;
79 : int anLevels[1024];
80 12 : int nLevelCount = 0;
81 12 : int nResultStatus = 0;
82 12 : int bReadOnly = FALSE;
83 12 : int bClean = FALSE;
84 12 : GDALProgressFunc pfnProgress = GDALTermProgress;
85 :
86 : /* Check that we are running against at least GDAL 1.7 */
87 : /* Note to developers : if we use newer API, please change the requirement */
88 12 : if (atoi(GDALVersionInfo("VERSION_NUM")) < 1700)
89 : {
90 : fprintf(stderr, "At least, GDAL >= 1.7.0 is required for this version of %s, "
91 0 : "which was compiled against GDAL %s\n", papszArgv[0], GDAL_RELEASE_NAME);
92 0 : exit(1);
93 : }
94 :
95 12 : GDALAllRegister();
96 :
97 12 : nArgc = GDALGeneralCmdLineProcessor( nArgc, &papszArgv, 0 );
98 12 : if( nArgc < 1 )
99 0 : exit( -nArgc );
100 :
101 : /* -------------------------------------------------------------------- */
102 : /* Parse commandline. */
103 : /* -------------------------------------------------------------------- */
104 40 : for( int iArg = 1; iArg < nArgc; iArg++ )
105 : {
106 30 : if( EQUAL(papszArgv[iArg], "--utility_version") )
107 : {
108 : printf("%s was compiled against GDAL %s and is running against GDAL %s\n",
109 2 : papszArgv[0], GDAL_RELEASE_NAME, GDALVersionInfo("RELEASE_NAME"));
110 2 : return 0;
111 : }
112 30 : else if( EQUAL(papszArgv[iArg],"-r") && iArg < nArgc-1 )
113 2 : pszResampling = papszArgv[++iArg];
114 26 : else if( EQUAL(papszArgv[iArg],"-ro"))
115 4 : bReadOnly = TRUE;
116 22 : else if( EQUAL(papszArgv[iArg],"-clean"))
117 2 : bClean = TRUE;
118 20 : else if( EQUAL(papszArgv[iArg],"-q") || EQUAL(papszArgv[iArg],"-quiet") )
119 0 : pfnProgress = GDALDummyProgress;
120 20 : else if( pszFilename == NULL )
121 10 : pszFilename = papszArgv[iArg];
122 10 : else if( atoi(papszArgv[iArg]) > 0 )
123 10 : anLevels[nLevelCount++] = atoi(papszArgv[iArg]);
124 : else
125 0 : Usage();
126 : }
127 :
128 10 : if( pszFilename == NULL || (nLevelCount == 0 && !bClean) )
129 0 : Usage();
130 :
131 : /* -------------------------------------------------------------------- */
132 : /* Open data file. */
133 : /* -------------------------------------------------------------------- */
134 10 : if (bReadOnly)
135 4 : hDataset = NULL;
136 : else
137 : {
138 6 : CPLPushErrorHandler( CPLQuietErrorHandler );
139 6 : hDataset = GDALOpen( pszFilename, GA_Update );
140 6 : CPLPopErrorHandler();
141 : }
142 :
143 10 : if( hDataset == NULL )
144 4 : hDataset = GDALOpen( pszFilename, GA_ReadOnly );
145 :
146 10 : if( hDataset == NULL )
147 0 : exit( 2 );
148 :
149 : /* -------------------------------------------------------------------- */
150 : /* Clean overviews. */
151 : /* -------------------------------------------------------------------- */
152 10 : if ( bClean &&
153 : GDALBuildOverviews( hDataset,pszResampling, 0, 0,
154 : 0, NULL, pfnProgress, NULL ) != CE_None )
155 : {
156 0 : printf( "Cleaning overviews failed.\n" );
157 0 : nResultStatus = 200;
158 : }
159 :
160 : /* -------------------------------------------------------------------- */
161 : /* Generate overviews. */
162 : /* -------------------------------------------------------------------- */
163 10 : if (nLevelCount > 0 && nResultStatus == 0 &&
164 : GDALBuildOverviews( hDataset,pszResampling, nLevelCount, anLevels,
165 : 0, NULL, pfnProgress, NULL ) != CE_None )
166 : {
167 0 : printf( "Overview building failed.\n" );
168 0 : nResultStatus = 100;
169 : }
170 :
171 : /* -------------------------------------------------------------------- */
172 : /* Cleanup */
173 : /* -------------------------------------------------------------------- */
174 10 : GDALClose(hDataset);
175 :
176 10 : CSLDestroy( papszArgv );
177 10 : GDALDestroyDriverManager();
178 :
179 10 : return nResultStatus;
180 : }
|