LCOV - code coverage report
Current view: top level - gcore - gdalcachedpixelaccessor.h (source / functions) Hit Total Coverage
Test: gdal_filtered.info Lines: 103 115 89.6 %
Date: 2026-12-03 10:33:05 Functions: 119 119 100.0 %

          Line data    Source code
       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             :  * SPDX-License-Identifier: MIT
      11             :  ****************************************************************************/
      12             : 
      13             : #ifndef GDAL_CACHED_PIXEL_ACCESSOR_INCLUDED
      14             : #define GDAL_CACHED_PIXEL_ACCESSOR_INCLUDED
      15             : 
      16             : #include "gdal_priv.h"
      17             : #include "cpl_error.h"
      18             : #include "cpl_float.h"
      19             : 
      20             : #include <algorithm>
      21             : #include <array>
      22             : #include <vector>
      23             : 
      24             : /************************************************************************/
      25             : /*                       GDALCachedPixelAccessor                        */
      26             : /************************************************************************/
      27             : 
      28             : /** Class to have reasonably fast random pixel access to a raster band, when
      29             :  * accessing multiple pixels that are close to each other.
      30             :  *
      31             :  * This gives faster access than using GDALRasterBand::RasterIO() with
      32             :  * a 1x1 window.
      33             :  *
      34             :  * @since GDAL 3.5
      35             :  */
      36             : template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT = 4>
      37             : class GDALCachedPixelAccessor
      38             : {
      39             :     GDALRasterBand *m_poBand = nullptr;
      40             : 
      41             :     struct CachedTile
      42             :     {
      43             :         // Avoid a GCC 13.1 internal compiler error in C++20 aggregate initialization of the array.
      44             :         CachedTile() = default;
      45             : 
      46             :         std::vector<Type> m_data{};
      47             :         int m_nTileX = -1;
      48             :         int m_nTileY = -1;
      49             :         bool m_bModified = false;
      50             :     };
      51             : 
      52             :     int m_nCachedTileCount = 0;
      53             :     std::array<CachedTile, static_cast<size_t>(CACHED_TILE_COUNT)>
      54             :         m_aCachedTiles{};
      55             : 
      56             :     bool LoadTile(int nTileX, int nTileY);
      57             :     bool FlushTile(int iSlot);
      58             : 
      59             :     Type GetSlowPath(int nTileX, int nTileY, int nXInTile, int nYInTile,
      60             :                      bool *pbSuccess);
      61             :     bool SetSlowPath(int nTileX, int nTileY, int nXInTile, int nYInTile,
      62             :                      Type val);
      63             : 
      64             :     GDALCachedPixelAccessor(const GDALCachedPixelAccessor &) = delete;
      65             :     GDALCachedPixelAccessor &
      66             :     operator=(const GDALCachedPixelAccessor &) = delete;
      67             : 
      68             :   public:
      69             :     explicit GDALCachedPixelAccessor(GDALRasterBand *poBand);
      70             :     ~GDALCachedPixelAccessor();
      71             : 
      72             :     /** Assign the raster band if not known at construction time. */
      73          10 :     void SetBand(GDALRasterBand *poBand)
      74             :     {
      75          10 :         m_poBand = poBand;
      76          10 :     }
      77             : 
      78             :     Type Get(int nX, int nY, bool *pbSuccess = nullptr);
      79             :     bool Set(int nX, int nY, Type val);
      80             : 
      81             :     bool FlushCache();
      82             :     void ResetModifiedFlag();
      83             : };
      84             : 
      85             : /************************************************************************/
      86             : /*                      GDALCachedPixelAccessor()                       */
      87             : /************************************************************************/
      88             : 
      89             : /** Constructor.
      90             :  *
      91             :  * The template accepts the following parameters:
      92             :  * - Type: should be one of GByte, GUInt16, GInt16, GUInt32, GInt32, GUInt64,
      93             :  * GInt64, float or double
      94             :  * - TILE_SIZE: the tile size for the cache of GDALCachedPixelAccessor.
      95             :  *              Use a power of two for faster computation.
      96             :  *              It doesn't need to be the same of the underlying raster
      97             :  * - CACHED_TILE_COUNT: number of tiles to cache. Should be >= 1. Defaults to 4.
      98             :  *
      99             :  * @param poBand Raster band.
     100             :  */
     101             : template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
     102          21 : GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::
     103             :     GDALCachedPixelAccessor(GDALRasterBand *poBand)
     104          21 :     : m_poBand(poBand)
     105             : {
     106          21 : }
     107             : 
     108             : /************************************************************************/
     109             : /*                      ~GDALCachedPixelAccessor()                      */
     110             : /************************************************************************/
     111             : 
     112             : /** Destructor.
     113             :  *
     114             :  * Will call FlushCache()
     115             :  */
     116             : template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
     117          21 : GDALCachedPixelAccessor<Type, TILE_SIZE,
     118             :                         CACHED_TILE_COUNT>::~GDALCachedPixelAccessor()
     119             : {
     120          21 :     FlushCache();
     121          21 : }
     122             : 
     123             : /************************************************************************/
     124             : /*                                Get()                                 */
     125             : /************************************************************************/
     126             : 
     127             : /** Get the value of a pixel.
     128             :  *
     129             :  * No bound checking of nX, nY is done.
     130             :  *
     131             :  * @param nX X coordinate (between 0 and GetXSize()-1)
     132             :  * @param nY Y coordinate (between 0 and GetYSize()-1)
     133             :  * @param[out] pbSuccess Optional pointer to a success flag
     134             :  * @return the pixel value
     135             :  */
     136             : template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
     137    16223013 : inline Type GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::Get(
     138             :     int nX, int nY, bool *pbSuccess)
     139             : {
     140    16223013 :     const int nTileX = nX / TILE_SIZE;
     141    16223013 :     const int nTileY = nY / TILE_SIZE;
     142    16223013 :     const int nXInTile = nX % TILE_SIZE;
     143    16223013 :     const int nYInTile = nY % TILE_SIZE;
     144    32424607 :     if (m_aCachedTiles[0].m_nTileX == nTileX &&
     145    16201584 :         m_aCachedTiles[0].m_nTileY == nTileY)
     146             :     {
     147    16186884 :         if (pbSuccess)
     148           0 :             *pbSuccess = true;
     149    16186884 :         return m_aCachedTiles[0].m_data[nYInTile * TILE_SIZE + nXInTile];
     150             :     }
     151       36118 :     return GetSlowPath(nTileX, nTileY, nXInTile, nYInTile, pbSuccess);
     152             : }
     153             : 
     154             : /************************************************************************/
     155             : /*                            GetSlowPath()                             */
     156             : /************************************************************************/
     157             : 
     158             : template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
     159       36118 : Type GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::GetSlowPath(
     160             :     int nTileX, int nTileY, int nXInTile, int nYInTile, bool *pbSuccess)
     161             : {
     162       50580 :     for (int i = 1; i < m_nCachedTileCount; ++i)
     163             :     {
     164       50339 :         const auto &cachedTile = m_aCachedTiles[i];
     165       50339 :         if (cachedTile.m_nTileX == nTileX && cachedTile.m_nTileY == nTileY)
     166             :         {
     167       35877 :             const auto ret = cachedTile.m_data[nYInTile * TILE_SIZE + nXInTile];
     168       35877 :             CachedTile tmp = std::move(m_aCachedTiles[i]);
     169       85442 :             for (int j = i; j >= 1; --j)
     170       49565 :                 m_aCachedTiles[j] = std::move(m_aCachedTiles[j - 1]);
     171       35877 :             m_aCachedTiles[0] = std::move(tmp);
     172       35877 :             if (pbSuccess)
     173           0 :                 *pbSuccess = true;
     174       35877 :             return ret;
     175             :         }
     176             :     }
     177         241 :     if (!LoadTile(nTileX, nTileY))
     178             :     {
     179           0 :         if (pbSuccess)
     180           0 :             *pbSuccess = false;
     181           0 :         return 0;
     182             :     }
     183         241 :     if (pbSuccess)
     184           0 :         *pbSuccess = true;
     185         241 :     return m_aCachedTiles[0].m_data[nYInTile * TILE_SIZE + nXInTile];
     186             : }
     187             : 
     188             : /************************************************************************/
     189             : /*                                Set()                                 */
     190             : /************************************************************************/
     191             : 
     192             : /** Set the value of a pixel.
     193             :  *
     194             :  * The actual modification of the underlying raster is deferred until the tile
     195             :  * is implicit flushed while loading a new tile, or an explicit call to
     196             :  * FlushCache().
     197             :  *
     198             :  * The destructor of GDALCachedPixelAccessor will take care of calling
     199             :  * FlushCache(), if the user hasn't done it explicitly.
     200             :  *
     201             :  * No bound checking of nX, nY is done.
     202             :  *
     203             :  * @param nX X coordinate (between 0 and GetXSize()-1)
     204             :  * @param nY Y coordinate (between 0 and GetYSize()-1)
     205             :  * @param val pixel value
     206             :  * @return true if success
     207             :  */
     208             : template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
     209             : inline bool
     210     1475403 : GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::Set(int nX, int nY,
     211             :                                                                  Type val)
     212             : {
     213     1475403 :     const int nTileX = nX / TILE_SIZE;
     214     1475403 :     const int nTileY = nY / TILE_SIZE;
     215     1475403 :     const int nXInTile = nX % TILE_SIZE;
     216     1475403 :     const int nYInTile = nY % TILE_SIZE;
     217     2948607 :     if (m_aCachedTiles[0].m_nTileX == nTileX &&
     218     1473194 :         m_aCachedTiles[0].m_nTileY == nTileY)
     219             :     {
     220     1473194 :         m_aCachedTiles[0].m_data[nYInTile * TILE_SIZE + nXInTile] = val;
     221     1473194 :         m_aCachedTiles[0].m_bModified = true;
     222     1473194 :         return true;
     223             :     }
     224        2209 :     return SetSlowPath(nTileX, nTileY, nXInTile, nYInTile, val);
     225             : }
     226             : 
     227             : /************************************************************************/
     228             : /*                            SetSlowPath()                             */
     229             : /************************************************************************/
     230             : 
     231             : template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
     232        2209 : bool GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::SetSlowPath(
     233             :     int nTileX, int nTileY, int nXInTile, int nYInTile, Type val)
     234             : {
     235        4792 :     for (int i = 1; i < m_nCachedTileCount; ++i)
     236             :     {
     237        4579 :         auto &cachedTile = m_aCachedTiles[i];
     238        4579 :         if (cachedTile.m_nTileX == nTileX && cachedTile.m_nTileY == nTileY)
     239             :         {
     240        1996 :             cachedTile.m_data[nYInTile * TILE_SIZE + nXInTile] = val;
     241        1996 :             cachedTile.m_bModified = true;
     242        1996 :             CachedTile tmp = std::move(m_aCachedTiles[i]);
     243        6068 :             for (int j = i; j >= 1; --j)
     244        4072 :                 m_aCachedTiles[j] = std::move(m_aCachedTiles[j - 1]);
     245        1996 :             m_aCachedTiles[0] = std::move(tmp);
     246        1996 :             return true;
     247             :         }
     248             :     }
     249         213 :     if (!LoadTile(nTileX, nTileY))
     250             :     {
     251           0 :         return false;
     252             :     }
     253         213 :     m_aCachedTiles[0].m_data[nYInTile * TILE_SIZE + nXInTile] = val;
     254         213 :     m_aCachedTiles[0].m_bModified = true;
     255         213 :     return true;
     256             : }
     257             : 
     258             : /************************************************************************/
     259             : /*                             FlushCache()                             */
     260             : /************************************************************************/
     261             : 
     262             : /** Flush content of modified tiles and drop caches
     263             :  *
     264             :  * @return true if success
     265             :  */
     266             : template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
     267          36 : bool GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::FlushCache()
     268             : {
     269          36 :     bool bRet = true;
     270         202 :     for (int i = 0; i < m_nCachedTileCount; ++i)
     271             :     {
     272         166 :         if (!FlushTile(i))
     273           0 :             bRet = false;
     274         166 :         m_aCachedTiles[i].m_nTileX = -1;
     275         166 :         m_aCachedTiles[i].m_nTileY = -1;
     276             :     }
     277          36 :     return bRet;
     278             : }
     279             : 
     280             : /************************************************************************/
     281             : /*                         ResetModifiedFlag()                          */
     282             : /************************************************************************/
     283             : 
     284             : /** Reset the modified flag for cached tiles.
     285             :  */
     286             : template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
     287          10 : void GDALCachedPixelAccessor<Type, TILE_SIZE,
     288             :                              CACHED_TILE_COUNT>::ResetModifiedFlag()
     289             : {
     290          68 :     for (int i = 0; i < m_nCachedTileCount; ++i)
     291             :     {
     292          58 :         m_aCachedTiles[i].m_bModified = false;
     293             :     }
     294          10 : }
     295             : 
     296             : /************************************************************************/
     297             : /*                  GDALCachedPixelAccessorGetDataType                  */
     298             : /************************************************************************/
     299             : 
     300             : /*! @cond Doxygen_Suppress */
     301             : template <class T> struct GDALCachedPixelAccessorGetDataType
     302             : {
     303             : };
     304             : 
     305             : template <> struct GDALCachedPixelAccessorGetDataType<GByte>
     306             : {
     307             :     static constexpr GDALDataType DataType = GDT_Byte;
     308             : };
     309             : 
     310             : template <> struct GDALCachedPixelAccessorGetDataType<GInt8>
     311             : {
     312             :     static constexpr GDALDataType DataType = GDT_Int8;
     313             : };
     314             : 
     315             : template <> struct GDALCachedPixelAccessorGetDataType<GUInt16>
     316             : {
     317             :     static constexpr GDALDataType DataType = GDT_UInt16;
     318             : };
     319             : 
     320             : template <> struct GDALCachedPixelAccessorGetDataType<GInt16>
     321             : {
     322             :     static constexpr GDALDataType DataType = GDT_Int16;
     323             : };
     324             : 
     325             : template <> struct GDALCachedPixelAccessorGetDataType<GUInt32>
     326             : {
     327             :     static constexpr GDALDataType DataType = GDT_UInt32;
     328             : };
     329             : 
     330             : template <> struct GDALCachedPixelAccessorGetDataType<GInt32>
     331             : {
     332             :     static constexpr GDALDataType DataType = GDT_Int32;
     333             : };
     334             : #if SIZEOF_UNSIGNED_LONG == 8
     335             : // std::uint64_t on Linux 64-bit resolves as unsigned long
     336             : template <> struct GDALCachedPixelAccessorGetDataType<unsigned long>
     337             : {
     338             :     static constexpr GDALDataType DataType = GDT_UInt64;
     339             : };
     340             : 
     341             : template <> struct GDALCachedPixelAccessorGetDataType<long>
     342             : {
     343             :     static constexpr GDALDataType DataType = GDT_Int64;
     344             : };
     345             : #endif
     346             : template <> struct GDALCachedPixelAccessorGetDataType<GUInt64>
     347             : {
     348             :     static constexpr GDALDataType DataType = GDT_UInt64;
     349             : };
     350             : 
     351             : template <> struct GDALCachedPixelAccessorGetDataType<GInt64>
     352             : {
     353             :     static constexpr GDALDataType DataType = GDT_Int64;
     354             : };
     355             : 
     356             : template <> struct GDALCachedPixelAccessorGetDataType<GFloat16>
     357             : {
     358             :     static constexpr GDALDataType DataType = GDT_Float16;
     359             : };
     360             : 
     361             : template <> struct GDALCachedPixelAccessorGetDataType<float>
     362             : {
     363             :     static constexpr GDALDataType DataType = GDT_Float32;
     364             : };
     365             : 
     366             : template <> struct GDALCachedPixelAccessorGetDataType<double>
     367             : {
     368             :     static constexpr GDALDataType DataType = GDT_Float64;
     369             : };
     370             : 
     371             : /*! @endcond */
     372             : 
     373             : /************************************************************************/
     374             : /*                              LoadTile()                              */
     375             : /************************************************************************/
     376             : 
     377             : template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
     378         454 : bool GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::LoadTile(
     379             :     int nTileX, int nTileY)
     380             : {
     381         454 :     if (m_nCachedTileCount == CACHED_TILE_COUNT)
     382             :     {
     383         352 :         if (!FlushTile(CACHED_TILE_COUNT - 1))
     384           0 :             return false;
     385         704 :         CachedTile tmp = std::move(m_aCachedTiles[CACHED_TILE_COUNT - 1]);
     386        1408 :         for (int i = CACHED_TILE_COUNT - 1; i >= 1; --i)
     387        1056 :             m_aCachedTiles[i] = std::move(m_aCachedTiles[i - 1]);
     388         352 :         m_aCachedTiles[0] = std::move(tmp);
     389             :     }
     390             :     else
     391             :     {
     392         102 :         if (m_nCachedTileCount > 0)
     393          81 :             std::swap(m_aCachedTiles[0], m_aCachedTiles[m_nCachedTileCount]);
     394         102 :         m_aCachedTiles[0].m_data.resize(TILE_SIZE * TILE_SIZE);
     395         102 :         m_nCachedTileCount++;
     396             :     }
     397             : 
     398             : #if 0
     399             :     CPLDebug("GDAL", "Load tile(%d, %d) of band %d of dataset %s",
     400             :               nTileX, nTileY, m_poBand->GetBand(),
     401             :               m_poBand->GetDataset() ? m_poBand->GetDataset()->GetDescription() : "(unknown)");
     402             : #endif
     403         454 :     CPLAssert(!m_aCachedTiles[0].m_bModified);
     404         454 :     const int nXOff = nTileX * TILE_SIZE;
     405         454 :     const int nYOff = nTileY * TILE_SIZE;
     406         454 :     const int nReqXSize = std::min(m_poBand->GetXSize() - nXOff, TILE_SIZE);
     407         454 :     const int nReqYSize = std::min(m_poBand->GetYSize() - nYOff, TILE_SIZE);
     408         908 :     if (m_poBand->RasterIO(
     409             :             GF_Read, nXOff, nYOff, nReqXSize, nReqYSize,
     410         454 :             m_aCachedTiles[0].m_data.data(), nReqXSize, nReqYSize,
     411             :             GDALCachedPixelAccessorGetDataType<Type>::DataType, sizeof(Type),
     412         454 :             TILE_SIZE * sizeof(Type), nullptr) != CE_None)
     413             :     {
     414           0 :         m_aCachedTiles[0].m_nTileX = -1;
     415           0 :         m_aCachedTiles[0].m_nTileY = -1;
     416           0 :         return false;
     417             :     }
     418         454 :     m_aCachedTiles[0].m_nTileX = nTileX;
     419         454 :     m_aCachedTiles[0].m_nTileY = nTileY;
     420         454 :     return true;
     421             : }
     422             : 
     423             : /************************************************************************/
     424             : /*                             FlushTile()                              */
     425             : /************************************************************************/
     426             : 
     427             : template <class Type, int TILE_SIZE, int CACHED_TILE_COUNT>
     428         518 : bool GDALCachedPixelAccessor<Type, TILE_SIZE, CACHED_TILE_COUNT>::FlushTile(
     429             :     int iSlot)
     430             : {
     431         518 :     if (!m_aCachedTiles[iSlot].m_bModified)
     432         300 :         return true;
     433             : 
     434         218 :     m_aCachedTiles[iSlot].m_bModified = false;
     435         218 :     const int nXOff = m_aCachedTiles[iSlot].m_nTileX * TILE_SIZE;
     436         218 :     const int nYOff = m_aCachedTiles[iSlot].m_nTileY * TILE_SIZE;
     437         218 :     const int nReqXSize = std::min(m_poBand->GetXSize() - nXOff, TILE_SIZE);
     438         218 :     const int nReqYSize = std::min(m_poBand->GetYSize() - nYOff, TILE_SIZE);
     439         218 :     return m_poBand->RasterIO(
     440             :                GF_Write, nXOff, nYOff, nReqXSize, nReqYSize,
     441         218 :                m_aCachedTiles[iSlot].m_data.data(), nReqXSize, nReqYSize,
     442             :                GDALCachedPixelAccessorGetDataType<Type>::DataType, sizeof(Type),
     443         218 :                TILE_SIZE * sizeof(Type), nullptr) == CE_None;
     444             : }
     445             : 
     446             : #endif  // GDAL_CACHED_PIXEL_ACCESSOR_INCLUDED

Generated by: LCOV version 1.14