<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html><head><meta name="robots" content="noindex"> <meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>gdaldefaultoverviews.cpp Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> </head><body bgcolor="#ffffff"> <!-- Generated by Doxygen 1.2.3-20001105 on Thu Mar 28 09:47:30 2002 --> <center> <a class="qindex" href="index.html">Main Page</a> <a class="qindex" href="hierarchy.html">Class Hierarchy</a> <a class="qindex" href="annotated.html">Compound List</a> <a class="qindex" href="files.html">File List</a> <a class="qindex" href="functions.html">Compound Members</a> <a class="qindex" href="globals.html">File Members</a> <a class="qindex" href="pages.html">Related Pages</a> </center> <hr><h1>gdaldefaultoverviews.cpp</h1><div class="fragment"><pre>00001 <font class="comment">/******************************************************************************</font> 00002 <font class="comment"> * $Id: gdaldefaultoverviews_cpp-source.html,v 1.10 2002/04/16 13:11:48 warmerda Exp $</font> 00003 <font class="comment"> *</font> 00004 <font class="comment"> * Project: GDAL Core</font> 00005 <font class="comment"> * Purpose: Helper code to implement overview support in different drivers.</font> 00006 <font class="comment"> * Author: Frank Warmerdam, warmerda@home.com</font> 00007 <font class="comment"> *</font> 00008 <font class="comment"> ******************************************************************************</font> 00009 <font class="comment"> * Copyright (c) 2000, Frank Warmerdam</font> 00010 <font class="comment"> *</font> 00011 <font class="comment"> * Permission is hereby granted, free of charge, to any person obtaining a</font> 00012 <font class="comment"> * copy of this software and associated documentation files (the "Software"),</font> 00013 <font class="comment"> * to deal in the Software without restriction, including without limitation</font> 00014 <font class="comment"> * the rights to use, copy, modify, merge, publish, distribute, sublicense,</font> 00015 <font class="comment"> * and/or sell copies of the Software, and to permit persons to whom the</font> 00016 <font class="comment"> * Software is furnished to do so, subject to the following conditions:</font> 00017 <font class="comment"> *</font> 00018 <font class="comment"> * The above copyright notice and this permission notice shall be included</font> 00019 <font class="comment"> * in all copies or substantial portions of the Software.</font> 00020 <font class="comment"> *</font> 00021 <font class="comment"> * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS</font> 00022 <font class="comment"> * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,</font> 00023 <font class="comment"> * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL</font> 00024 <font class="comment"> * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER</font> 00025 <font class="comment"> * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING</font> 00026 <font class="comment"> * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER</font> 00027 <font class="comment"> * DEALINGS IN THE SOFTWARE.</font> 00028 <font class="comment"> ******************************************************************************</font> 00029 <font class="comment"> *</font> 00030 <font class="comment"> * $Log: gdaldefaultoverviews_cpp-source.html,v $ 00030 <font class="comment"> * Revision 1.10 2002/04/16 13:11:48 warmerda 00030 <font class="comment"> * updated 00030 <font class="comment"> *</font> 00031 <font class="comment"> * Revision 1.8 2001/10/18 14:35:22 warmerda</font> 00032 <font class="comment"> * avoid conflicts between parameters and member data</font> 00033 <font class="comment"> *</font> 00034 <font class="comment"> * Revision 1.7 2001/07/18 04:04:30 warmerda</font> 00035 <font class="comment"> * added CPL_CVSID</font> 00036 <font class="comment"> *</font> 00037 <font class="comment"> * Revision 1.6 2001/06/22 21:00:38 warmerda</font> 00038 <font class="comment"> * fixed several problems with regenerating existing overviews</font> 00039 <font class="comment"> *</font> 00040 <font class="comment"> * Revision 1.5 2001/06/22 13:52:03 warmerda</font> 00041 <font class="comment"> * fixed bug when refreshing overviews during build</font> 00042 <font class="comment"> *</font> 00043 <font class="comment"> * Revision 1.4 2001/06/20 16:08:54 warmerda</font> 00044 <font class="comment"> * GDALDefaultOverviews now remembers ovr filename, and allows explicit setting</font> 00045 <font class="comment"> *</font> 00046 <font class="comment"> * Revision 1.3 2000/06/19 18:48:49 warmerda</font> 00047 <font class="comment"> * fixed message</font> 00048 <font class="comment"> *</font> 00049 <font class="comment"> * Revision 1.2 2000/06/19 14:42:27 warmerda</font> 00050 <font class="comment"> * Don't close old overviews till after we have identified which already</font> 00051 <font class="comment"> * exist, otherwise multiple copies of overviews may be created.</font> 00052 <font class="comment"> *</font> 00053 <font class="comment"> * Revision 1.1 2000/04/21 21:54:05 warmerda</font> 00054 <font class="comment"> * New</font> 00055 <font class="comment"> *</font> 00056 <font class="comment"> */</font> 00057 00058 <font class="preprocessor">#include "gdal_priv.h"</font> 00059 00060 CPL_CVSID(<font class="stringliteral">"$Id: gdaldefaultoverviews_cpp-source.html,v 1.10 2002/04/16 13:11:48 warmerda Exp $"</font>); 00061 00062 <font class="comment">/************************************************************************/</font> 00063 <font class="comment">/* GDALDefaultOverviews() */</font> 00064 <font class="comment">/************************************************************************/</font> 00065 00066 GDALDefaultOverviews::GDALDefaultOverviews()<font class="keyword"></font> 00067 <font class="keyword"></font> 00068 <font class="keyword"></font>{ 00069 poDS = NULL; 00070 poODS = NULL; 00071 pszOvrFilename = NULL; 00072 } 00073 00074 <font class="comment">/************************************************************************/</font> 00075 <font class="comment">/* ~GDALDefaultOverviews() */</font> 00076 <font class="comment">/************************************************************************/</font> 00077 00078 GDALDefaultOverviews::~GDALDefaultOverviews()<font class="keyword"></font> 00079 <font class="keyword"></font> 00080 <font class="keyword"></font>{ 00081 <font class="keywordflow">if</font>( poODS != NULL ) 00082 { 00083 poODS->FlushCache(); 00084 <font class="keyword">delete</font> poODS; 00085 } 00086 CPLFree( pszOvrFilename ); 00087 } 00088 00089 <font class="comment">/************************************************************************/</font> 00090 <font class="comment">/* Initialize() */</font> 00091 <font class="comment">/************************************************************************/</font> 00092 00093 <font class="keywordtype">void</font> GDALDefaultOverviews::Initialize( <a class="code" href="class_GDALDataset.html">GDALDataset</a> *poDSIn, 00094 <font class="keyword">const</font> <font class="keywordtype">char</font> * pszBasename, 00095 <font class="keywordtype">int</font> bNameIsOVR )<font class="keyword"></font> 00096 <font class="keyword"></font> 00097 <font class="keyword"></font>{ 00098 VSIStatBuf sStatBuf; 00099 00100 <font class="comment">/* -------------------------------------------------------------------- */</font> 00101 <font class="comment">/* If we were already initialized, destroy the old overview */</font> 00102 <font class="comment">/* file handle. */</font> 00103 <font class="comment">/* -------------------------------------------------------------------- */</font> 00104 <font class="keywordflow">if</font>( poODS != NULL ) 00105 { 00106 <font class="keyword">delete</font> poODS; 00107 } 00108 00109 <font class="comment">/* -------------------------------------------------------------------- */</font> 00110 <font class="comment">/* Open overview dataset if it exists. */</font> 00111 <font class="comment">/* -------------------------------------------------------------------- */</font> 00112 poDS = poDSIn; 00113 00114 <font class="keywordflow">if</font>( pszBasename == NULL ) 00115 pszBasename = poDS->GetDescription(); 00116 00117 CPLFree( pszOvrFilename ); 00118 pszOvrFilename = (<font class="keywordtype">char</font> *) <a class="code" href="cpl_conv_h.html#a3">CPLMalloc</a>(strlen(pszBasename)+5); 00119 <font class="keywordflow">if</font>( bNameIsOVR ) 00120 strcpy( pszOvrFilename, pszBasename ); 00121 <font class="keywordflow">else</font> 00122 sprintf( pszOvrFilename, <font class="stringliteral">"%s.ovr"</font>, pszBasename ); 00123 00124 <font class="keywordflow">if</font>( VSIStat( pszOvrFilename, &sStatBuf ) == 0 ) 00125 poODS = (<a class="code" href="class_GDALDataset.html">GDALDataset</a> *) <a class="code" href="gdal_h.html#a60">GDALOpen</a>( pszOvrFilename, poDS->GetAccess() ); 00126 } 00127 00128 <font class="comment">/************************************************************************/</font> 00129 <font class="comment">/* GetOverviewCount() */</font> 00130 <font class="comment">/************************************************************************/</font> 00131 00132 <font class="keywordtype">int</font> GDALDefaultOverviews::GetOverviewCount( <font class="keywordtype">int</font> nBand )<font class="keyword"></font> 00133 <font class="keyword"></font> 00134 <font class="keyword"></font>{ 00135 <a class="code" href="class_GDALRasterBand.html">GDALRasterBand</a> * poBand; 00136 00137 <font class="keywordflow">if</font>( poODS == NULL || nBand < 1 || nBand > poODS->GetRasterCount() ) 00138 <font class="keywordflow">return</font> 0; 00139 00140 poBand = poODS->GetRasterBand( nBand ); 00141 <font class="keywordflow">if</font>( poBand == NULL ) 00142 <font class="keywordflow">return</font> 0; 00143 <font class="keywordflow">else</font> 00144 <font class="keywordflow">return</font> poBand-><a class="code" href="class_GDALRasterBand.html#a28">GetOverviewCount</a>() + 1; 00145 } 00146 00147 <font class="comment">/************************************************************************/</font> 00148 <font class="comment">/* GetOverview() */</font> 00149 <font class="comment">/************************************************************************/</font> 00150 00151 <a class="code" href="class_GDALRasterBand.html">GDALRasterBand</a> * 00152 GDALDefaultOverviews::GetOverview( <font class="keywordtype">int</font> nBand, <font class="keywordtype">int</font> iOverview )<font class="keyword"></font> 00153 <font class="keyword"></font> 00154 <font class="keyword"></font>{ 00155 <a class="code" href="class_GDALRasterBand.html">GDALRasterBand</a> * poBand; 00156 00157 <font class="keywordflow">if</font>( poODS == NULL || nBand < 1 || nBand > poODS->GetRasterCount() ) 00158 <font class="keywordflow">return</font> NULL; 00159 00160 poBand = poODS->GetRasterBand( nBand ); 00161 <font class="keywordflow">if</font>( poBand == NULL ) 00162 <font class="keywordflow">return</font> NULL; 00163 00164 <font class="keywordflow">if</font>( iOverview == 0 ) 00165 <font class="keywordflow">return</font> poBand; 00166 <font class="keywordflow">else</font> <font class="keywordflow">if</font>( iOverview-1 >= poBand-><a class="code" href="class_GDALRasterBand.html#a28">GetOverviewCount</a>() ) 00167 <font class="keywordflow">return</font> NULL; 00168 <font class="keywordflow">else</font> 00169 <font class="keywordflow">return</font> poBand-><a class="code" href="class_GDALRasterBand.html#a29">GetOverview</a>( iOverview-1 ); 00170 } 00171 00172 <font class="comment">/************************************************************************/</font> 00173 <font class="comment">/* GDALOvLevelAdjust() */</font> 00174 <font class="comment">/* */</font> 00175 <font class="comment">/* Some overview levels cannot be achieved closely enough to be */</font> 00176 <font class="comment">/* recognised as the desired overview level. This function */</font> 00177 <font class="comment">/* will adjust an overview level to one that is achievable on */</font> 00178 <font class="comment">/* the given raster size. */</font> 00179 <font class="comment">/* */</font> 00180 <font class="comment">/* For instance a 1200x1200 image on which a 256 level overview */</font> 00181 <font class="comment">/* is request will end up generating a 5x5 overview. However, */</font> 00182 <font class="comment">/* this will appear to the system be a level 240 overview. */</font> 00183 <font class="comment">/* This function will adjust 256 to 240 based on knowledge of */</font> 00184 <font class="comment">/* the image size. */</font> 00185 <font class="comment">/************************************************************************/</font> 00186 00187 <font class="keyword">static</font> <font class="keywordtype">int</font> GDALOvLevelAdjust( <font class="keywordtype">int</font> nOvLevel, <font class="keywordtype">int</font> nXSize )<font class="keyword"></font> 00188 <font class="keyword"></font> 00189 <font class="keyword"></font>{ 00190 <font class="keywordtype">int</font> nOXSize = (nXSize + nOvLevel - 1) / nOvLevel; 00191 00192 <font class="keywordflow">return</font> (<font class="keywordtype">int</font>) (0.5 + nXSize / (<font class="keywordtype">double</font>) nOXSize); 00193 } 00194 00195 <font class="comment">/************************************************************************/</font> 00196 <font class="comment">/* GDALDefaultBuildOverviews() */</font> 00197 <font class="comment">/************************************************************************/</font> 00198 00199 CPLErr 00200 GDALDefaultOverviews::BuildOverviews( 00201 <font class="keyword">const</font> <font class="keywordtype">char</font> * pszBasename, 00202 <font class="keyword">const</font> <font class="keywordtype">char</font> * pszResampling, 00203 <font class="keywordtype">int</font> nOverviews, <font class="keywordtype">int</font> * panOverviewList, 00204 <font class="keywordtype">int</font> nBands, <font class="keywordtype">int</font> * panBandList, 00205 GDALProgressFunc pfnProgress, <font class="keywordtype">void</font> * pProgressData)<font class="keyword"></font> 00206 <font class="keyword"></font> 00207 <font class="keyword"></font>{ 00208 <a class="code" href="class_GDALRasterBand.html">GDALRasterBand</a> **pahBands; 00209 CPLErr eErr; 00210 <font class="keywordtype">int</font> i; 00211 00212 <font class="comment">/* -------------------------------------------------------------------- */</font> 00213 <font class="comment">/* Our TIFF overview support currently only works safely if all */</font> 00214 <font class="comment">/* bands are handled at the same time. */</font> 00215 <font class="comment">/* -------------------------------------------------------------------- */</font> 00216 <font class="keywordflow">if</font>( nBands != poDS->GetRasterCount() ) 00217 { 00218 <a class="code" href="cpl_error_h.html#a17">CPLError</a>( CE_Failure, CPLE_NotSupported, 00219 <font class="stringliteral">"Generation of overviews in external TIFF currently only"</font> 00220 <font class="stringliteral">" supported when operating on all bands.\n"</font> 00221 <font class="stringliteral">"Operation failed.\n"</font> ); 00222 <font class="keywordflow">return</font> CE_Failure; 00223 } 00224 00225 <font class="comment">/* -------------------------------------------------------------------- */</font> 00226 <font class="comment">/* If a basename is provided, use it to override the internal */</font> 00227 <font class="comment">/* overview filename. */</font> 00228 <font class="comment">/* -------------------------------------------------------------------- */</font> 00229 <font class="keywordflow">if</font>( pszBasename == NULL && pszOvrFilename == NULL ) 00230 pszBasename = poDS->GetDescription(); 00231 00232 <font class="keywordflow">if</font>( pszBasename != NULL ) 00233 { 00234 CPLFree( pszOvrFilename ); 00235 pszOvrFilename = (<font class="keywordtype">char</font> *) <a class="code" href="cpl_conv_h.html#a3">CPLMalloc</a>(strlen(pszBasename)+5); 00236 sprintf( pszOvrFilename, <font class="stringliteral">"%s.ovr"</font>, pszBasename ); 00237 } 00238 00239 <font class="comment">/* -------------------------------------------------------------------- */</font> 00240 <font class="comment">/* Establish which of the overview levels we already have, and */</font> 00241 <font class="comment">/* which are new. We assume that band 1 of the file is */</font> 00242 <font class="comment">/* representative. */</font> 00243 <font class="comment">/* -------------------------------------------------------------------- */</font> 00244 <font class="keywordtype">int</font> nNewOverviews, *panNewOverviewList = NULL; 00245 <a class="code" href="class_GDALRasterBand.html">GDALRasterBand</a> *poBand = poDS->GetRasterBand( 1 ); 00246 00247 nNewOverviews = 0; 00248 panNewOverviewList = (<font class="keywordtype">int</font> *) <a class="code" href="cpl_conv_h.html#a4">CPLCalloc</a>(<font class="keyword">sizeof</font>(<font class="keywordtype">int</font>),nOverviews); 00249 <font class="keywordflow">for</font>( i = 0; i < nOverviews && poBand != NULL; i++ ) 00250 { 00251 <font class="keywordtype">int</font> j; 00252 00253 <font class="keywordflow">for</font>( j = 0; j < poBand->GetOverviewCount(); j++ ) 00254 { 00255 <font class="keywordtype">int</font> nOvFactor; 00256 <a class="code" href="class_GDALRasterBand.html">GDALRasterBand</a> * poOverview = poBand-><a class="code" href="class_GDALRasterBand.html#a29">GetOverview</a>( j ); 00257 00258 nOvFactor = (<font class="keywordtype">int</font>) 00259 (0.5 + poBand-><a class="code" href="class_GDALRasterBand.html#a2">GetXSize</a>() / (<font class="keywordtype">double</font>) poOverview-><a class="code" href="class_GDALRasterBand.html#a2">GetXSize</a>()); 00260 00261 <font class="keywordflow">if</font>( nOvFactor == panOverviewList[i] 00262 || nOvFactor == GDALOvLevelAdjust( panOverviewList[i], 00263 poBand-><a class="code" href="class_GDALRasterBand.html#a2">GetXSize</a>() ) ) 00264 panOverviewList[i] *= -1; 00265 } 00266 00267 <font class="keywordflow">if</font>( panOverviewList[i] > 0 ) 00268 panNewOverviewList[nNewOverviews++] = panOverviewList[i]; 00269 } 00270 00271 <font class="comment">/* -------------------------------------------------------------------- */</font> 00272 <font class="comment">/* If we have an existing overview file open, close it now. */</font> 00273 <font class="comment">/* -------------------------------------------------------------------- */</font> 00274 <font class="keywordflow">if</font>( poODS != NULL ) 00275 { 00276 <font class="keyword">delete</font> poODS; 00277 poODS = NULL; 00278 } 00279 00280 <font class="comment">/* -------------------------------------------------------------------- */</font> 00281 <font class="comment">/* Build band list. */</font> 00282 <font class="comment">/* -------------------------------------------------------------------- */</font> 00283 pahBands = (<a class="code" href="class_GDALRasterBand.html">GDALRasterBand</a> **) <a class="code" href="cpl_conv_h.html#a4">CPLCalloc</a>(<font class="keyword">sizeof</font>(<a class="code" href="class_GDALRasterBand.html">GDALRasterBand</a> *),nBands); 00284 <font class="keywordflow">for</font>( i = 0; i < nBands; i++ ) 00285 pahBands[i] = poDS->GetRasterBand( panBandList[i] ); 00286 00287 <font class="comment">/* -------------------------------------------------------------------- */</font> 00288 <font class="comment">/* Build new overviews. */</font> 00289 <font class="comment">/* -------------------------------------------------------------------- */</font> 00290 00291 eErr = GTIFFBuildOverviews( pszOvrFilename, nBands, pahBands, 00292 nNewOverviews, panNewOverviewList, 00293 pszResampling, pfnProgress, pProgressData ); 00294 00295 <font class="comment">/* -------------------------------------------------------------------- */</font> 00296 <font class="comment">/* Refresh old overviews that were listed. */</font> 00297 <font class="comment">/* -------------------------------------------------------------------- */</font> 00298 <a class="code" href="class_GDALRasterBand.html">GDALRasterBand</a> **papoOverviewBands; 00299 00300 <font class="keywordflow">if</font>( eErr == CE_None ) 00301 { 00302 poODS = (<a class="code" href="class_GDALDataset.html">GDALDataset</a> *) <a class="code" href="gdal_h.html#a60">GDALOpen</a>( pszOvrFilename, GA_Update ); 00303 <font class="keywordflow">if</font>( poODS == NULL ) 00304 eErr = CE_Failure; 00305 } 00306 00307 papoOverviewBands = (<a class="code" href="class_GDALRasterBand.html">GDALRasterBand</a> **) 00308 <a class="code" href="cpl_conv_h.html#a4">CPLCalloc</a>(<font class="keyword">sizeof</font>(<font class="keywordtype">void</font>*),nOverviews); 00309 00310 <font class="keywordflow">for</font>( <font class="keywordtype">int</font> iBand = 0; iBand < nBands && eErr == CE_None; iBand++ ) 00311 { 00312 poBand = poDS->GetRasterBand( panBandList[iBand] ); 00313 00314 nNewOverviews = 0; 00315 <font class="keywordflow">for</font>( i = 0; i < nOverviews && poBand != NULL; i++ ) 00316 { 00317 <font class="keywordtype">int</font> j; 00318 00319 <font class="keywordflow">for</font>( j = 0; j < poBand->GetOverviewCount(); j++ ) 00320 { 00321 <font class="keywordtype">int</font> nOvFactor; 00322 <a class="code" href="class_GDALRasterBand.html">GDALRasterBand</a> * poOverview = poBand-><a class="code" href="class_GDALRasterBand.html#a29">GetOverview</a>( j ); 00323 00324 nOvFactor = (<font class="keywordtype">int</font>) 00325 (0.5 + poBand-><a class="code" href="class_GDALRasterBand.html#a2">GetXSize</a>() / (<font class="keywordtype">double</font>) poOverview-><a class="code" href="class_GDALRasterBand.html#a2">GetXSize</a>()); 00326 00327 <font class="keywordflow">if</font>( nOvFactor == - panOverviewList[i] 00328 || nOvFactor == GDALOvLevelAdjust( -panOverviewList[i], 00329 poBand-><a class="code" href="class_GDALRasterBand.html#a2">GetXSize</a>() ) ) 00330 { 00331 panOverviewList[i] *= -1; 00332 papoOverviewBands[nNewOverviews++] = poOverview; 00333 } 00334 } 00335 } 00336 00337 <font class="keywordflow">if</font>( nNewOverviews > 0 ) 00338 { 00339 eErr = GDALRegenerateOverviews( poBand, 00340 nNewOverviews, papoOverviewBands, 00341 pszResampling, 00342 GDALDummyProgress, NULL ); 00343 } 00344 } 00345 00346 <font class="comment">/* -------------------------------------------------------------------- */</font> 00347 <font class="comment">/* Cleanup */</font> 00348 <font class="comment">/* -------------------------------------------------------------------- */</font> 00349 CPLFree( papoOverviewBands ); 00350 CPLFree( panNewOverviewList ); 00351 CPLFree( pahBands ); 00352 00353 <font class="keywordflow">return</font> eErr; 00354 } 00355 </div></pre><hr><address><small>Generated at Thu Mar 28 09:47:30 2002 for GDAL by <a href="http://www.stack.nl/~dimitri/doxygen/index.html"> <img src="doxygen.gif" alt="doxygen" align="middle" border=0 width=110 height=53></a>1.2.3-20001105 written by <a href="mailto:dimitri@stack.nl">Dimitri van Heesch</a>, © 1997-2000</small></address> </body> </html>