summaryrefslogtreecommitdiffstats
path: root/chalk/core/tiles/kis_tileddatamanager.h
blob: 539a9ae13baae28d464e7e352109fda2ace85745 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 *  Copyright (c) 2004 Boudewijn Rempt <[email protected]>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */
#ifndef KIS_TILEDDATAMANAGER_H_
#define KIS_TILEDDATAMANAGER_H_

#include <tqglobal.h>
#include <tqvaluevector.h>

#include <ksharedptr.h>

#include "kis_tile_global.h"
#include "kis_tile.h"
#include "kis_memento.h"

class KisTiledDataManager;
typedef TDESharedPtr<KisTiledDataManager> KisTiledDataManagerSP;

class KisDataManager;
typedef TDESharedPtr<KisDataManager> KisDataManagerSP;

class KisTiledIterator;
class KisTiledRandomAccessor;
class KoStore;

class KisTileDataWrapper : public TDEShared {
    KisTile* m_tile;
    TQ_INT32 m_offset;
public:
    KisTileDataWrapper(KisTile* tile, TQ_INT32 offset);
    virtual ~KisTileDataWrapper();
    TQ_UINT8* data() const { return m_tile->data() + m_offset; }
};

typedef TDESharedPtr<KisTileDataWrapper> KisTileDataWrapperSP;

/**
 * KisTiledDataManager implements the interface that KisDataManager defines
 *
 * The interface definition is enforced by KisDataManager calling all the methods
 * which must also be defined in KisTiledDataManager. It is not allowed to change the interface
 * as other datamangers may also rely on the same interface.
 *
 * * Storing undo/redo data
 * * Offering ordered and unordered iterators over rects of pixels
 * * (eventually) efficiently loading and saving data in a format
 * that may allow deferred loading.
 *
 * A datamanager knows nothing about the type of pixel data except
 * how many TQ_UINT8's a single pixel takes.
 */

class KisTiledDataManager : public TDEShared {

protected:
    KisTiledDataManager(TQ_UINT32 pixelSize, const TQ_UINT8 *defPixel);
    ~KisTiledDataManager();
    KisTiledDataManager(const KisTiledDataManager &dm);
    KisTiledDataManager & operator=(const KisTiledDataManager &dm);


protected:
    // Allow the baseclass of iterators acces to the interior
    // derived iterator classes must go through KisTiledIterator
    friend class KisTiledIterator;
    friend class KisTiledRandomAccessor;

protected:

    void setDefaultPixel(const TQ_UINT8 *defPixel);
    const TQ_UINT8 * defaultPixel() const { return m_defPixel;};

    KisMementoSP getMemento();
    void rollback(KisMementoSP memento);
    void rollforward(KisMementoSP memento);

    // For debugging use.
    bool hasCurrentMemento() const { return m_currentMemento != 0; }

protected:
    /**
     * Reads and writes the tiles from/onto a KoStore (which is simply a file within a zip file)
     *
     */
    bool write(KoStore *store);
    bool read(KoStore *store);

protected:

    TQ_UINT32 pixelSize();

    void extent(TQ_INT32 &x, TQ_INT32 &y, TQ_INT32 &w, TQ_INT32 &h) const;
    TQRect extent() const;

    void setExtent(TQ_INT32 x, TQ_INT32 y, TQ_INT32 w, TQ_INT32 h);

protected:

    void clear(TQ_INT32 x, TQ_INT32 y, TQ_INT32 w, TQ_INT32 h, TQ_UINT8 clearValue);
    void clear(TQ_INT32 x, TQ_INT32 y,  TQ_INT32 w, TQ_INT32 h, const TQ_UINT8 *clearPixel);
    void clear();


protected:

    void paste(KisDataManagerSP data,  TQ_INT32 sx, TQ_INT32 sy, TQ_INT32 dx, TQ_INT32 dy,
                TQ_INT32 w, TQ_INT32 h);


protected:


    /**
     * Get a read-only pointer to pixel (x, y).
     */
    const TQ_UINT8* pixel(TQ_INT32 x, TQ_INT32 y);

    /**
     * Get a read-write pointer to pixel (x, y).
     */
    TQ_UINT8* writablePixel(TQ_INT32 x, TQ_INT32 y);

    /**
     * write the specified data to x, y. There is no checking on pixelSize!
     */
    void setPixel(TQ_INT32 x, TQ_INT32 y, const TQ_UINT8 * data);


    /**
     * Copy the bytes in the specified rect to a vector. The caller is responsible
     * for managing the vector.
     */
    void readBytes(TQ_UINT8 * bytes,
               TQ_INT32 x, TQ_INT32 y,
               TQ_INT32 w, TQ_INT32 h);
    /**
     * Copy the bytes in the vector to the specified rect. If there are bytes left
     * in the vector after filling the rect, they will be ignored. If there are
     * not enough bytes, the rest of the rect will be filled with the default value
     * given (by default, 0);
     */
    void writeBytes(const TQ_UINT8 * bytes,
            TQ_INT32 x, TQ_INT32 y,
            TQ_INT32 w, TQ_INT32 h);

    /// Get the number of contiguous columns starting at x, valid for all values
    /// of y between minY and maxY.
    TQ_INT32 numContiguousColumns(TQ_INT32 x, TQ_INT32 minY, TQ_INT32 maxY);

    /// Get the number of contiguous rows starting at y, valid for all values
    /// of x between minX and maxX.
    TQ_INT32 numContiguousRows(TQ_INT32 y, TQ_INT32 minX, TQ_INT32 maxX);

    /// Get the row stride at pixel (x, y). This is the number of bytes to add to a
    /// pointer to pixel (x, y) to access (x, y + 1).
    TQ_INT32 rowStride(TQ_INT32 x, TQ_INT32 y);

    // For debugging use
    TQ_INT32 numTiles() const;

private:

    TQ_UINT32 m_pixelSize;
    TQ_UINT32 m_numTiles;
    KisTile *m_defaultTile;
    KisTile **m_hashTable;
    KisMementoSP m_currentMemento;
    TQ_INT32 m_extentMinX;
    TQ_INT32 m_extentMinY;
    TQ_INT32 m_extentMaxX;
    TQ_INT32 m_extentMaxY;
    TQ_UINT8 *m_defPixel;

private:

    void ensureTileMementoed(TQ_INT32 col, TQ_INT32 row, TQ_UINT32 tileHash, const KisTile *refTile);
    KisTile *getOldTile(TQ_INT32 col, TQ_INT32 row, KisTile *def);
    KisTile *getTile(TQ_INT32 col, TQ_INT32 row, bool writeAccess);
    TQ_UINT32 calcTileHash(TQ_INT32 col, TQ_INT32 row);
    void updateExtent(TQ_INT32 col, TQ_INT32 row);
    void recalculateExtent();
    void deleteTiles(const KisMemento::DeletedTile *deletedTileList);
    TQ_INT32 xToCol(TQ_INT32 x) const;
    TQ_INT32 yToRow(TQ_INT32 y) const;
    void getContiguousColumnsAndRows(TQ_INT32 x, TQ_INT32 y, TQ_INT32 *columns, TQ_INT32 *rows);
    TQ_UINT8* pixelPtr(TQ_INT32 x, TQ_INT32 y, bool writable);
    KisTileDataWrapperSP pixelPtrSafe(TQ_INT32 x, TQ_INT32 y, bool writable);
};


inline TQ_UINT32 KisTiledDataManager::pixelSize()
{
    return m_pixelSize;
}

inline TQ_INT32 KisTiledDataManager::xToCol(TQ_INT32 x) const
{
    if (x >= 0) {
        return x / KisTile::WIDTH;
    } else {
        return -(((-x - 1) / KisTile::WIDTH) + 1);
    }
}

inline TQ_INT32 KisTiledDataManager::yToRow(TQ_INT32 y) const
{
    if (y >= 0) {
        return y / KisTile::HEIGHT;
    } else {
        return -(((-y - 1) / KisTile::HEIGHT) + 1);
    }
}

// during development the following line helps to check the interface is correct
// it should be safe to keep it here even during normal compilation
#include "kis_datamanager.h"

#endif // KIS_TILEDDATAMANAGER_H_