File indexing completed on 2024-05-26 04:28:05

0001 /*
0002  *  SPDX-FileCopyrightText: 2009 Dmitry Kazakov <dimula73@gmail.com>
0003  *
0004  *  SPDX-License-Identifier: GPL-2.0-or-later
0005  */
0006 
0007 #ifndef __KIS_TILE_DATA_WRAPPER_H
0008 #define __KIS_TILE_DATA_WRAPPER_H
0009 
0010 
0011 /**
0012  * KisTileDataWrapper is a special object, that fetches the tile from
0013  * the data manager according to the position, locks it and returns
0014  * a pointer to the needed piece of data
0015  */
0016 class KisTileDataWrapper
0017 {
0018 public:
0019     enum accessType {
0020         READ,
0021         WRITE
0022     };
0023 
0024     /**
0025      * Fetches the tile which contains point (\p x, \p y) from
0026      * the data manager \p dm with access \p type
0027      */
0028     inline KisTileDataWrapper(KisTiledDataManager *dm,
0029                               qint32 x, qint32 y,
0030                               enum KisTileDataWrapper::accessType type)
0031     {
0032         const qint32 col = dm->xToCol(x);
0033         const qint32 row = dm->yToRow(y);
0034 
0035         /* FIXME: Always positive? */
0036         const qint32 xInTile = x - col * KisTileData::WIDTH;
0037         const qint32 yInTile = y - row * KisTileData::HEIGHT;
0038 
0039         const qint32 pixelIndex = xInTile + yInTile * KisTileData::WIDTH;
0040 
0041         KisTileSP tile = dm->getTile(col, row, type == WRITE);
0042 
0043         m_tile = tile;
0044         m_offset = pixelIndex * dm->pixelSize();
0045 
0046         if (type == READ) {
0047             m_tile->lockForRead();
0048         }
0049         else {
0050             m_tile->lockForWrite();
0051         }
0052 
0053         m_type = type;
0054     }
0055 
0056     virtual ~KisTileDataWrapper()
0057     {
0058         if (m_type == READ) {
0059             m_tile->unlockForRead();
0060         } else {
0061             m_tile->unlockForWrite();
0062         }
0063     }
0064 
0065     /**
0066      * Returns the offset of the data in the tile's chunk of memory
0067      *
0068      * \see data()
0069      */
0070     inline qint32 offset() const
0071     {
0072         return m_offset;
0073     }
0074 
0075     /**
0076      * Returns the fetched tile
0077      */
0078     inline KisTileSP& tile()
0079     {
0080         return m_tile;
0081     }
0082 
0083     /**
0084      * Returns the pointer to the pixel, that was passed to
0085      * the constructor. This points to the raw data of the tile,
0086      * so you should think about the borders of the tile yourself.
0087      * When (x,y) is the top-left corner of the tile, the pointer
0088      * will lead to the beginning of the tile's chunk of memory.
0089      */
0090     inline quint8* data() const
0091     {
0092         return m_tile->data() + m_offset;
0093     }
0094 
0095 private:
0096     Q_DISABLE_COPY(KisTileDataWrapper)
0097 
0098     KisTileSP m_tile;
0099     qint32 m_offset;
0100     KisTileDataWrapper::accessType m_type;
0101 };
0102 #endif /* __KIS_TILE_DATA_WRAPPER_H */