29 #ifndef GDAL_CACHED_PIXEL_ACCESSOR_INCLUDED
30 #define GDAL_CACHED_PIXEL_ACCESSOR_INCLUDED
51 template <
class Type,
int TILE_SIZE,
int CACHED_TILE_COUNT = 4>
58 std::vector<Type> m_data{};
61 bool m_bModified =
false;
64 int m_nCachedTileCount = 0;
65 std::array<CachedTile, CACHED_TILE_COUNT> m_aCachedTiles{};
67 bool LoadTile(
int nTileX,
int nTileY);
68 bool FlushTile(
int iSlot);
70 Type GetSlowPath(
int nTileX,
int nTileY,
int nXInTile,
int nYInTile,
72 bool SetSlowPath(
int nTileX,
int nTileY,
int nXInTile,
int nYInTile,
89 Type
Get(
int nX,
int nY,
bool *pbSuccess =
nullptr);
90 bool Set(
int nX,
int nY, Type val);
112 template <
class Type,
int TILE_SIZE,
int CACHED_TILE_COUNT>
127 template <
class Type,
int TILE_SIZE,
int CACHED_TILE_COUNT>
129 CACHED_TILE_COUNT>::~GDALCachedPixelAccessor()
147 template <
class Type,
int TILE_SIZE,
int CACHED_TILE_COUNT>
149 int nX,
int nY,
bool *pbSuccess)
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)
160 return m_aCachedTiles[0].m_data[nYInTile * TILE_SIZE + nXInTile];
162 return GetSlowPath(nTileX, nTileY, nXInTile, nYInTile, pbSuccess);
169 template <
class Type,
int TILE_SIZE,
int CACHED_TILE_COUNT>
171 int nTileX,
int nTileY,
int nXInTile,
int nYInTile,
bool *pbSuccess)
173 for (
int i = 1; i < m_nCachedTileCount; ++i)
175 const auto &cachedTile = m_aCachedTiles[i];
176 if (cachedTile.m_nTileX == nTileX && cachedTile.m_nTileY == nTileY)
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);
188 if (!LoadTile(nTileX, nTileY))
196 return m_aCachedTiles[0].m_data[nYInTile * TILE_SIZE + nXInTile];
219 template <
class Type,
int TILE_SIZE,
int CACHED_TILE_COUNT>
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)
231 m_aCachedTiles[0].m_data[nYInTile * TILE_SIZE + nXInTile] = val;
232 m_aCachedTiles[0].m_bModified =
true;
235 return SetSlowPath(nTileX, nTileY, nXInTile, nYInTile, val);
242 template <
class Type,
int TILE_SIZE,
int CACHED_TILE_COUNT>
244 int nTileX,
int nTileY,
int nXInTile,
int nYInTile, Type val)
246 for (
int i = 1; i < m_nCachedTileCount; ++i)
248 auto &cachedTile = m_aCachedTiles[i];
249 if (cachedTile.m_nTileX == nTileX && cachedTile.m_nTileY == nTileY)
251 cachedTile.m_data[nYInTile * TILE_SIZE + nXInTile] = val;
252 cachedTile.m_bModified =
true;
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);
263 if (!LoadTile(nTileX, nTileY))
267 m_aCachedTiles[0].m_data[nYInTile * TILE_SIZE + nXInTile] = val;
268 m_aCachedTiles[0].m_bModified =
true;
280 template <
class Type,
int TILE_SIZE,
int CACHED_TILE_COUNT>
284 for (
int i = 0; i < m_nCachedTileCount; ++i)
288 m_aCachedTiles[i].m_nTileX = -1;
289 m_aCachedTiles[i].m_nTileY = -1;
300 template <
class Type,
int TILE_SIZE,
int CACHED_TILE_COUNT>
302 CACHED_TILE_COUNT>::ResetModifiedFlag()
304 for (
int i = 0; i < m_nCachedTileCount; ++i)
306 m_aCachedTiles[i].m_bModified =
false;
315 template <
class T>
struct GDALCachedPixelAccessorGetDataType
319 template <>
struct GDALCachedPixelAccessorGetDataType<
GByte>
323 template <>
struct GDALCachedPixelAccessorGetDataType<
GInt8>
327 template <>
struct GDALCachedPixelAccessorGetDataType<
GUInt16>
331 template <>
struct GDALCachedPixelAccessorGetDataType<
GInt16>
335 template <>
struct GDALCachedPixelAccessorGetDataType<
GUInt32>
339 template <>
struct GDALCachedPixelAccessorGetDataType<
GInt32>
343 #if SIZEOF_UNSIGNED_LONG == 8
345 template <>
struct GDALCachedPixelAccessorGetDataType<unsigned long>
349 template <>
struct GDALCachedPixelAccessorGetDataType<long>
354 template <>
struct GDALCachedPixelAccessorGetDataType<
GUInt64>
358 template <>
struct GDALCachedPixelAccessorGetDataType<
GInt64>
362 template <>
struct GDALCachedPixelAccessorGetDataType<float>
366 template <>
struct GDALCachedPixelAccessorGetDataType<double>
376 template <
class Type,
int TILE_SIZE,
int CACHED_TILE_COUNT>
378 int nTileX,
int nTileY)
380 if (m_nCachedTileCount == CACHED_TILE_COUNT)
382 if (!FlushTile(CACHED_TILE_COUNT - 1))
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);
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++;
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)");
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)
413 m_aCachedTiles[0].m_nTileX = -1;
414 m_aCachedTiles[0].m_nTileY = -1;
417 m_aCachedTiles[0].m_nTileX = nTileX;
418 m_aCachedTiles[0].m_nTileY = nTileY;
426 template <
class Type,
int TILE_SIZE,
int CACHED_TILE_COUNT>
430 if (!m_aCachedTiles[iSlot].m_bModified)
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;
445 #endif // GDAL_PIXEL_ACCESSOR_INCLUDED