GDAL

gdalcachedpixelaccessor.h
1 /******************************************************************************
2 *
3 * Project: GDAL
4 * Purpose: Fast access to individual pixels in a GDALRasterBand
5 * Author: Even Rouault <even dot rouault at spatialys.com>
6 *
7 ******************************************************************************
8 * Copyright (c) 2022, Planet Labs
9 *
10 * Permission is hereby granted, free of charge, to any person obtaining a
11 * copy of this software and associated documentation files (the "Software"),
12 * to deal in the Software without restriction, including without limitation
13 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14 * and/or sell copies of the Software, and to permit persons to whom the
15 * Software is furnished to do so, subject to the following conditions:
16 *
17 * The above copyright notice and this permission notice shall be included
18 * in all copies or substantial portions of the Software.
19 *
20 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 * DEALINGS IN THE SOFTWARE.
27 ****************************************************************************/
28
29 #ifndef GDAL_CACHED_PIXEL_ACCESSOR_INCLUDED
30 #define GDAL_CACHED_PIXEL_ACCESSOR_INCLUDED
31
32 #include "gdal_priv.h"
33 #include "cpl_error.h"
34
35 #include <algorithm>
36 #include <array>
37 #include <vector>
38
39 /************************************************************************/
40 /* GDALCachedPixelAccessor */
41 /************************************************************************/
42
51 template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT = 4>
52 class GDALCachedPixelAccessor
53 {
54 GDALRasterBand *m_poBand = nullptr;
55
56 struct CachedTile
57 {
58 std::vector<Type> m_data{};
59 int m_nTileX = -1;
60 int m_nTileY = -1;
61 bool m_bModified = false;
62 };
63
64 int m_nCachedTileCount = 0;
65 std::array<CachedTile, CACHED_TILE_COUNT> m_aCachedTiles{};
66
67 bool LoadTile(int nTileX, int nTileY);
68 bool FlushTile(int iSlot);
69
70 Type GetSlowPath(int nTileX, int nTileY, int nXInTile, int nYInTile,
71 bool *pbSuccess);
72 bool SetSlowPath(int nTileX, int nTileY, int nXInTile, int nYInTile,
73 Type val);
74
75 GDALCachedPixelAccessor(const GDALCachedPixelAccessor &) = delete;
76 GDALCachedPixelAccessor &
77 operator=(const GDALCachedPixelAccessor &) = delete;
78
79 public:
80 explicit GDALCachedPixelAccessor(GDALRasterBand *poBand);
81 ~GDALCachedPixelAccessor();
82
84 void SetBand(GDALRasterBand *poBand)
85 {
86 m_poBand = poBand;
87 }
88
89 Type Get(int nX, int nY, bool *pbSuccess = nullptr);
90 bool Set(int nX, int nY, Type val);
91
92 bool FlushCache();
93 void ResetModifiedFlag();
94 };
95
96 /************************************************************************/
97 /* GDALCachedPixelAccessor() */
98 /************************************************************************/
99
112 template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
113 GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::
114 GDALCachedPixelAccessor(GDALRasterBand *poBand)
115 : m_poBand(poBand)
116 {
117 }
118
119 /************************************************************************/
120 /* ~GDALCachedPixelAccessor() */
121 /************************************************************************/
122
127 template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
128 GDALCachedPixelAccessor<Type, TILE_SIZE,
129 CACHED_TILE_COUNT>::~GDALCachedPixelAccessor()
130 {
131 FlushCache();
132 }
133
134 /************************************************************************/
135 /* Get() */
136 /************************************************************************/
137
147 template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
148 inline Type GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::Get(
149 int nX, int nY, bool *pbSuccess)
150 {
151 const int nTileX = nX / TILE_SIZE;
152 const int nTileY = nY / TILE_SIZE;
153 const int nXInTile = nX % TILE_SIZE;
154 const int nYInTile = nY % TILE_SIZE;
155 if (m_aCachedTiles[0].m_nTileX == nTileX &&
156 m_aCachedTiles[0].m_nTileY == nTileY)
157 {
158 if (pbSuccess)
159 *pbSuccess = true;
160 return m_aCachedTiles[0].m_data[nYInTile * TILE_SIZE + nXInTile];
161 }
162 return GetSlowPath(nTileX, nTileY, nXInTile, nYInTile, pbSuccess);
163 }
164
165 /************************************************************************/
166 /* GetSlowPath() */
167 /************************************************************************/
168
169 template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
170 Type GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::GetSlowPath(
171 int nTileX, int nTileY, int nXInTile, int nYInTile, bool *pbSuccess)
172 {
173 for (int i = 1; i < m_nCachedTileCount; ++i)
174 {
175 const auto &cachedTile = m_aCachedTiles[i];
176 if (cachedTile.m_nTileX == nTileX && cachedTile.m_nTileY == nTileY)
177 {
178 const auto ret = cachedTile.m_data[nYInTile * TILE_SIZE + nXInTile];
179 CachedTile tmp = std::move(m_aCachedTiles[i]);
180 for (int j = i; j >= 1; --j)
181 m_aCachedTiles[j] = std::move(m_aCachedTiles[j - 1]);
182 m_aCachedTiles[0] = std::move(tmp);
183 if (pbSuccess)
184 *pbSuccess = true;
185 return ret;
186 }
187 }
188 if (!LoadTile(nTileX, nTileY))
189 {
190 if (pbSuccess)
191 *pbSuccess = false;
192 return 0;
193 }
194 if (pbSuccess)
195 *pbSuccess = true;
196 return m_aCachedTiles[0].m_data[nYInTile * TILE_SIZE + nXInTile];
197 }
198
199 /************************************************************************/
200 /* Set() */
201 /************************************************************************/
202
219 template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
220 inline bool
221 GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::Set(int nX, int nY,
222 Type val)
223 {
224 const int nTileX = nX / TILE_SIZE;
225 const int nTileY = nY / TILE_SIZE;
226 const int nXInTile = nX % TILE_SIZE;
227 const int nYInTile = nY % TILE_SIZE;
228 if (m_aCachedTiles[0].m_nTileX == nTileX &&
229 m_aCachedTiles[0].m_nTileY == nTileY)
230 {
231 m_aCachedTiles[0].m_data[nYInTile * TILE_SIZE + nXInTile] = val;
232 m_aCachedTiles[0].m_bModified = true;
233 return true;
234 }
235 return SetSlowPath(nTileX, nTileY, nXInTile, nYInTile, val);
236 }
237
238 /************************************************************************/
239 /* SetSlowPath() */
240 /************************************************************************/
241
242 template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
243 bool GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::SetSlowPath(
244 int nTileX, int nTileY, int nXInTile, int nYInTile, Type val)
245 {
246 for (int i = 1; i < m_nCachedTileCount; ++i)
247 {
248 auto &cachedTile = m_aCachedTiles[i];
249 if (cachedTile.m_nTileX == nTileX && cachedTile.m_nTileY == nTileY)
250 {
251 cachedTile.m_data[nYInTile * TILE_SIZE + nXInTile] = val;
252 cachedTile.m_bModified = true;
253 if (i > 0)
254 {
255 CachedTile tmp = std::move(m_aCachedTiles[i]);
256 for (int j = i; j >= 1; --j)
257 m_aCachedTiles[j] = std::move(m_aCachedTiles[j - 1]);
258 m_aCachedTiles[0] = std::move(tmp);
259 }
260 return true;
261 }
262 }
263 if (!LoadTile(nTileX, nTileY))
264 {
265 return false;
266 }
267 m_aCachedTiles[0].m_data[nYInTile * TILE_SIZE + nXInTile] = val;
268 m_aCachedTiles[0].m_bModified = true;
269 return true;
270 }
271
272 /************************************************************************/
273 /* FlushCache() */
274 /************************************************************************/
275
280 template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
281 bool GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::FlushCache()
282 {
283 bool bRet = true;
284 for (int i = 0; i < m_nCachedTileCount; ++i)
285 {
286 if (!FlushTile(i))
287 bRet = false;
288 m_aCachedTiles[i].m_nTileX = -1;
289 m_aCachedTiles[i].m_nTileY = -1;
290 }
291 return bRet;
292 }
293
294 /************************************************************************/
295 /* ResetModifiedFlag() */
296 /************************************************************************/
297
300 template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
301 void GDALCachedPixelAccessor<Type, TILE_SIZE,
302 CACHED_TILE_COUNT>::ResetModifiedFlag()
303 {
304 for (int i = 0; i < m_nCachedTileCount; ++i)
305 {
306 m_aCachedTiles[i].m_bModified = false;
307 }
308 }
309
310 /************************************************************************/
311 /* GDALCachedPixelAccessorGetDataType */
312 /************************************************************************/
313
315 template <class T> struct GDALCachedPixelAccessorGetDataType
316 {
317 };
318
319 template <> struct GDALCachedPixelAccessorGetDataType<GByte>
320 {
321 static constexpr GDALDataType DataType = GDT_Byte;
322 };
323 template <> struct GDALCachedPixelAccessorGetDataType<GInt8>
324 {
325 static constexpr GDALDataType DataType = GDT_Int8;
326 };
327 template <> struct GDALCachedPixelAccessorGetDataType<GUInt16>
328 {
329 static constexpr GDALDataType DataType = GDT_UInt16;
330 };
331 template <> struct GDALCachedPixelAccessorGetDataType<GInt16>
332 {
333 static constexpr GDALDataType DataType = GDT_Int16;
334 };
335 template <> struct GDALCachedPixelAccessorGetDataType<GUInt32>
336 {
337 static constexpr GDALDataType DataType = GDT_UInt32;
338 };
339 template <> struct GDALCachedPixelAccessorGetDataType<GInt32>
340 {
341 static constexpr GDALDataType DataType = GDT_Int32;
342 };
343 #if SIZEOF_UNSIGNED_LONG == 8
344 // std::uint64_t on Linux 64-bit resolves as unsigned long
345 template <> struct GDALCachedPixelAccessorGetDataType<unsigned long>
346 {
347 static constexpr GDALDataType DataType = GDT_UInt64;
348 };
349 template <> struct GDALCachedPixelAccessorGetDataType<long>
350 {
351 static constexpr GDALDataType DataType = GDT_Int64;
352 };
353 #endif
354 template <> struct GDALCachedPixelAccessorGetDataType<GUInt64>
355 {
356 static constexpr GDALDataType DataType = GDT_UInt64;
357 };
358 template <> struct GDALCachedPixelAccessorGetDataType<GInt64>
359 {
360 static constexpr GDALDataType DataType = GDT_Int64;
361 };
362 template <> struct GDALCachedPixelAccessorGetDataType<float>
363 {
364 static constexpr GDALDataType DataType = GDT_Float32;
365 };
366 template <> struct GDALCachedPixelAccessorGetDataType<double>
367 {
368 static constexpr GDALDataType DataType = GDT_Float64;
369 };
372 /************************************************************************/
373 /* LoadTile() */
374 /************************************************************************/
375
376 template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
377 bool GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::LoadTile(
378 int nTileX, int nTileY)
379 {
380 if (m_nCachedTileCount == CACHED_TILE_COUNT)
381 {
382 if (!FlushTile(CACHED_TILE_COUNT - 1))
383 return false;
384 CachedTile tmp = std::move(m_aCachedTiles[CACHED_TILE_COUNT - 1]);
385 for (int i = CACHED_TILE_COUNT - 1; i >= 1; --i)
386 m_aCachedTiles[i] = std::move(m_aCachedTiles[i - 1]);
387 m_aCachedTiles[0] = std::move(tmp);
388 }
389 else
390 {
391 if (m_nCachedTileCount > 0)
392 std::swap(m_aCachedTiles[0], m_aCachedTiles[m_nCachedTileCount]);
393 m_aCachedTiles[0].m_data.resize(TILE_SIZE * TILE_SIZE);
394 m_nCachedTileCount++;
395 }
396
397 #if 0
398 CPLDebug("GDAL", "Load tile(%d, %d) of band %d of dataset %s",
399 nTileX, nTileY, m_poBand->GetBand(),
400 m_poBand->GetDataset() ? m_poBand->GetDataset()->GetDescription() : "(unknown)");
401 #endif
402 CPLAssert(!m_aCachedTiles[0].m_bModified);
403 const int nXOff = nTileX * TILE_SIZE;
404 const int nYOff = nTileY * TILE_SIZE;
405 const int nReqXSize = std::min(m_poBand->GetXSize() - nXOff, TILE_SIZE);
406 const int nReqYSize = std::min(m_poBand->GetYSize() - nYOff, TILE_SIZE);
407 if (m_poBand->RasterIO(
408 GF_Read, nXOff, nYOff, nReqXSize, nReqYSize,
409 m_aCachedTiles[0].m_data.data(), nReqXSize, nReqYSize,
410 GDALCachedPixelAccessorGetDataType<Type>::DataType, sizeof(Type),
411 TILE_SIZE * sizeof(Type), nullptr) != CE_None)
412 {
413 m_aCachedTiles[0].m_nTileX = -1;
414 m_aCachedTiles[0].m_nTileY = -1;
415 return false;
416 }
417 m_aCachedTiles[0].m_nTileX = nTileX;
418 m_aCachedTiles[0].m_nTileY = nTileY;
419 return true;
420 }
421
422 /************************************************************************/
423 /* FlushTile() */
424 /************************************************************************/
425
426 template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
427 bool GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::FlushTile(
428 int iSlot)
429 {
430 if (!m_aCachedTiles[iSlot].m_bModified)
431 return true;
432
433 m_aCachedTiles[iSlot].m_bModified = false;
434 const int nXOff = m_aCachedTiles[iSlot].m_nTileX * TILE_SIZE;
435 const int nYOff = m_aCachedTiles[iSlot].m_nTileY * TILE_SIZE;
436 const int nReqXSize = std::min(m_poBand->GetXSize() - nXOff, TILE_SIZE);
437 const int nReqYSize = std::min(m_poBand->GetYSize() - nYOff, TILE_SIZE);
438 return m_poBand->RasterIO(
439 GF_Write, nXOff, nYOff, nReqXSize, nReqYSize,
440 m_aCachedTiles[iSlot].m_data.data(), nReqXSize, nReqYSize,
441 GDALCachedPixelAccessorGetDataType<Type>::DataType, sizeof(Type),
442 TILE_SIZE * sizeof(Type), nullptr) == CE_None;
443 }
444
445 #endif // GDAL_PIXEL_ACCESSOR_INCLUDED
GDT_Int32
@ GDT_Int32
Definition: gdal.h:71
GByte
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:196
GUInt64
GUIntBig GUInt64
Unsigned 64 bit integer type.
Definition: cpl_port.h:249
GDT_UInt64
@ GDT_UInt64
Definition: gdal.h:72
GDALCachedPixelAccessor
Class to have reasonably fast random pixel access to a raster band, when accessing multiple pixels th...
Definition: gdalcachedpixelaccessor.h:52
GInt16
short GInt16
Int16 type.
Definition: cpl_port.h:192
GDT_Float32
@ GDT_Float32
Definition: gdal.h:74
cpl_error.h
GDALCachedPixelAccessor::ResetModifiedFlag
void ResetModifiedFlag()
Reset the modified flag for cached tiles.
Definition: gdalcachedpixelaccessor.h:302
GDT_Int8
@ GDT_Int8
Definition: gdal.h:67
GDT_UInt32
@ GDT_UInt32
Definition: gdal.h:70
GF_Read
@ GF_Read
Definition: gdal.h:133
GDALRasterBand
A single raster band (or channel).
Definition: gdal_priv.h:1270
GInt64
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:247
GInt8
signed char GInt8
Signed int8 type.
Definition: cpl_port.h:198
GDALDataType
GDALDataType
Definition: gdal.h:63
GDALCachedPixelAccessor::~GDALCachedPixelAccessor
~GDALCachedPixelAccessor()
Destructor.
Definition: gdalcachedpixelaccessor.h:129
GDALCachedPixelAccessor::Get
Type Get(int nX, int nY, bool *pbSuccess=nullptr)
Get the value of a pixel.
Definition: gdalcachedpixelaccessor.h:148
GDT_UInt16
@ GDT_UInt16
Definition: gdal.h:68
CPLAssert
#define CPLAssert(expr)
Assert on an expression.
Definition: cpl_error.h:213
GF_Write
@ GF_Write
Definition: gdal.h:134
GDT_Float64
@ GDT_Float64
Definition: gdal.h:75
CPLDebug
void CPLDebug(const char *, const char *,...)
Display a debugging message.
Definition: cpl_error.cpp:617
GDALCachedPixelAccessor::Set
bool Set(int nX, int nY, Type val)
Set the value of a pixel.
Definition: gdalcachedpixelaccessor.h:221
GDALCachedPixelAccessor::FlushCache
bool FlushCache()
Flush content of modified tiles and drop caches.
Definition: gdalcachedpixelaccessor.h:281
gdal_priv.h
GDT_Int64
@ GDT_Int64
Definition: gdal.h:73
GDT_Int16
@ GDT_Int16
Definition: gdal.h:69
GDT_Byte
@ GDT_Byte
Definition: gdal.h:66
GDALCachedPixelAccessor::SetBand
void SetBand(GDALRasterBand *poBand)
Assign the raster band if not known at construction time.
Definition: gdalcachedpixelaccessor.h:110
GUInt16
unsigned short GUInt16
Unsigned int16 type.
Definition: cpl_port.h:194
GInt32
int GInt32
Int32 type.
Definition: cpl_port.h:186
GUInt32
unsigned int GUInt32
Unsigned int32 type.
Definition: cpl_port.h:188