1
/******************************************************************************
2
* $Id: gdaldataset.cpp 16796 2009-04-17 23:35:04Z normanb $
5
* Purpose: Implementation of GDALDefaultAsyncReader and the
6
* GDALAsyncReader base class.
7
* Author: Frank Warmerdam, warmerdam@pobox.com
9
******************************************************************************
10
* Copyright (c) 2010, Frank Warmerdam
12
* Permission is hereby granted, free of charge, to any person obtaining a
13
* copy of this software and associated documentation files (the "Software"),
14
* to deal in the Software without restriction, including without limitation
15
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
16
* and/or sell copies of the Software, and to permit persons to whom the
17
* Software is furnished to do so, subject to the following conditions:
19
* The above copyright notice and this permission notice shall be included
20
* in all copies or substantial portions of the Software.
22
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28
* DEALINGS IN THE SOFTWARE.
29
****************************************************************************/
31
#include "gdal_priv.h"
33
CPL_CVSID("$Id: gdaldataset.cpp 16796 2009-04-17 23:35:04Z normanb $");
37
GDALGetDefaultAsyncReader( GDALDataset* poDS,
38
int nXOff, int nYOff, int nXSize, int nYSize,
39
void *pBuf, int nBufXSize, int nBufYSize,
40
GDALDataType eBufType,
41
int nBandCount, int* panBandMap,
42
int nPixelSpace, int nLineSpace,
43
int nBandSpace, char **papszOptions );
46
/************************************************************************/
47
/* ==================================================================== */
49
/* ==================================================================== */
50
/************************************************************************/
52
/************************************************************************/
53
/* GDALAsyncReader() */
54
/************************************************************************/
56
GDALAsyncReader::GDALAsyncReader()
60
/************************************************************************/
61
/* ~GDALAsyncReader() */
62
/************************************************************************/
63
GDALAsyncReader::~GDALAsyncReader()
67
/************************************************************************/
68
/* GetNextUpdatedRegion() */
69
/************************************************************************/
72
* \fn GDALAsyncStatusType GDALAsyncReader::GetNextUpdatedRegion( double dfTimeout, int* pnBufXOff, int* pnBufYOff, int* pnBufXSize, int* pnBufXSize) = 0;
74
* \brief Get async IO update
76
* Provide an opportunity for an asynchronous IO request to update the
77
* image buffer and return an indication of the area of the buffer that
80
* The dfTimeout parameter can be used to wait for additional data to
81
* become available. The timeout does not limit the amount
82
* of time this method may spend actually processing available data.
84
* The following return status are possible.
85
* - GARIO_PENDING: No imagery was altered in the buffer, but there is still
86
* activity pending, and the application should continue to call
87
* GetNextUpdatedRegion() as time permits.
88
* - GARIO_UPDATE: Some of the imagery has been updated, but there is still
90
* - GARIO_ERROR: Something has gone wrong. The asynchronous request should
92
* - GARIO_COMPLETE: An update has occured and there is no more pending work
93
* on this request. The request should be ended and the buffer used.
95
* @param dfTimeout the number of seconds to wait for additional updates. Use
96
* -1 to wait indefinately, or zero to not wait at all if there is no data
98
* @param pnBufXOff location to return the X offset of the area of the
99
* request buffer that has been updated.
100
* @param pnBufYOff location to return the Y offset of the area of the
101
* request buffer that has been updated.
102
* @param pnBufXSize location to return the X size of the area of the
103
* request buffer that has been updated.
104
* @param pnBufYSize location to return the Y size of the area of the
105
* request buffer that has been updated.
107
* @return GARIO_ status, details described above.
110
/************************************************************************/
111
/* GDALARGetNextUpdatedRegion() */
112
/************************************************************************/
114
GDALAsyncStatusType CPL_STDCALL
115
GDALARGetNextUpdatedRegion(GDALAsyncReaderH hARIO, double timeout,
116
int* pnxbufoff, int* pnybufoff,
117
int* pnxbufsize, int* pnybufsize)
119
VALIDATE_POINTER1(hARIO, "GDALARGetNextUpdatedRegion", GARIO_ERROR);
120
return ((GDALAsyncReader *)hARIO)->GetNextUpdatedRegion(
121
timeout, pnxbufoff, pnybufoff, pnxbufsize, pnybufsize);
124
/************************************************************************/
126
/************************************************************************/
129
* \brief Lock image buffer.
131
* Locks the image buffer passed into GDALDataset::BeginAsyncReader().
132
* This is useful to ensure the image buffer is not being modified while
133
* it is being used by the application. UnlockBuffer() should be used
134
* to release this lock when it is no longer needed.
136
* @param dfTimeout the time in seconds to wait attempting to lock the buffer.
137
* -1.0 to wait indefinately and 0 to not wait at all if it can't be
138
* acquired immediately. Default is -1.0 (infinite wait).
140
* @return TRUE if successful, or FALSE on an error.
143
int GDALAsyncReader::LockBuffer( double dfTimeout )
150
/************************************************************************/
151
/* GDALARLockBuffer() */
152
/************************************************************************/
153
int CPL_STDCALL GDALARLockBuffer(GDALAsyncReaderH hARIO, double dfTimeout )
155
VALIDATE_POINTER1(hARIO, "GDALARLockBuffer",FALSE);
156
return ((GDALAsyncReader *)hARIO)->LockBuffer( dfTimeout );
159
/************************************************************************/
161
/************************************************************************/
164
* \brief Unlock image buffer.
166
* Releases a lock on the image buffer previously taken with LockBuffer().
169
void GDALAsyncReader::UnlockBuffer()
174
/************************************************************************/
175
/* GDALARUnlockBuffer() */
176
/************************************************************************/
177
void CPL_STDCALL GDALARUnlockBuffer(GDALAsyncReaderH hARIO)
179
VALIDATE_POINTER0(hARIO, "GDALARUnlockBuffer");
180
((GDALAsyncReader *)hARIO)->UnlockBuffer();
183
/************************************************************************/
184
/* ==================================================================== */
185
/* GDALDefaultAsyncReader */
186
/* ==================================================================== */
187
/************************************************************************/
189
class GDALDefaultAsyncReader : public GDALAsyncReader
192
char ** papszOptions;
195
GDALDefaultAsyncReader(GDALDataset* poDS,
196
int nXOff, int nYOff,
197
int nXSize, int nYSize,
199
int nBufXSize, int nBufYSize,
200
GDALDataType eBufType,
201
int nBandCount, int* panBandMap,
202
int nPixelSpace, int nLineSpace,
203
int nBandSpace, char **papszOptions);
204
~GDALDefaultAsyncReader();
206
virtual GDALAsyncStatusType GetNextUpdatedRegion(double dfTimeout,
213
/************************************************************************/
214
/* GDALGetDefaultAsyncReader() */
215
/************************************************************************/
218
GDALGetDefaultAsyncReader( GDALDataset* poDS,
219
int nXOff, int nYOff,
220
int nXSize, int nYSize,
222
int nBufXSize, int nBufYSize,
223
GDALDataType eBufType,
224
int nBandCount, int* panBandMap,
225
int nPixelSpace, int nLineSpace,
226
int nBandSpace, char **papszOptions)
229
return new GDALDefaultAsyncReader( poDS,
230
nXOff, nYOff, nXSize, nYSize,
231
pBuf, nBufXSize, nBufYSize, eBufType,
232
nBandCount, panBandMap,
233
nPixelSpace, nLineSpace, nBandSpace,
237
/************************************************************************/
238
/* GDALDefaultAsyncReader() */
239
/************************************************************************/
241
GDALDefaultAsyncReader::
242
GDALDefaultAsyncReader( GDALDataset* poDS,
243
int nXOff, int nYOff,
244
int nXSize, int nYSize,
246
int nBufXSize, int nBufYSize,
247
GDALDataType eBufType,
248
int nBandCount, int* panBandMap,
249
int nPixelSpace, int nLineSpace,
250
int nBandSpace, char **papszOptions)
256
this->nXSize = nXSize;
257
this->nYSize = nYSize;
259
this->nBufXSize = nBufXSize;
260
this->nBufYSize = nBufYSize;
261
this->eBufType = eBufType;
262
this->nBandCount = nBandCount;
263
this->panBandMap = (int *) CPLMalloc(sizeof(int)*nBandCount);
265
if( panBandMap != NULL )
266
memcpy( this->panBandMap, panBandMap, sizeof(int)*nBandCount );
269
for( int i = 0; i < nBandCount; i++ )
270
this->panBandMap[i] = i+1;
273
this->nPixelSpace = nPixelSpace;
274
this->nLineSpace = nLineSpace;
275
this->nBandSpace = nBandSpace;
277
this->papszOptions = CSLDuplicate(papszOptions);
280
/************************************************************************/
281
/* ~GDALDefaultAsyncReader() */
282
/************************************************************************/
284
GDALDefaultAsyncReader::~GDALDefaultAsyncReader()
287
CPLFree( panBandMap );
288
CSLDestroy( papszOptions );
291
/************************************************************************/
292
/* GetNextUpdatedRegion() */
293
/************************************************************************/
296
GDALDefaultAsyncReader::GetNextUpdatedRegion(double dfTimeout,
305
eErr = poDS->RasterIO( GF_Read, nXOff, nYOff, nXSize, nYSize,
306
pBuf, nBufXSize, nBufYSize, eBufType,
307
nBandCount, panBandMap,
308
nPixelSpace, nLineSpace, nBandSpace );
312
*pnBufXSize = nBufXSize;
313
*pnBufYSize = nBufYSize;
315
if( eErr == CE_None )
316
return GARIO_COMPLETE;