summaryrefslogtreecommitdiffstats
path: root/krita/core/tiles/kis_memento.cc
blob: aaec37242c68aa80fb2635a8b523dc4a5d5ec76e (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
/*
 *  Copyright (c) 2005 Casper Boemann <[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.
 */
#include "kis_global.h"
#include "kis_memento.h"
#include "kis_tile.h"
#include "kis_tile_global.h"

KisMemento::KisMemento(TQ_UINT32 pixelSize) : KShared()
{
    m_hashTable = new KisTile * [1024];
    Q_CHECK_PTR(m_hashTable);

    m_redoHashTable = new KisTile * [1024];
    Q_CHECK_PTR(m_redoHashTable);

    for(int i = 0; i < 1024; i++)
    {
        m_hashTable [i] = 0;
        m_redoHashTable [i] = 0;
    }
    m_numTiles = 0;
    m_defPixel = new TQ_UINT8[pixelSize];
    m_redoDefPixel = new TQ_UINT8[pixelSize];
    m_valid = true;
}

KisMemento::~KisMemento()
{
    // Deep delete every tile
    for(int i = 0; i < 1024; i++)
    {
        deleteAll(m_hashTable[i]);
        deleteAll(m_redoHashTable[i]);
    }
    delete [] m_hashTable;
    delete [] m_redoHashTable;

    // Delete defPixel arrays;
    delete [] m_defPixel;
    delete [] m_redoDefPixel;
}

KisMemento::DeletedTileList::~DeletedTileList()
{
    clear();
}

void KisMemento::DeletedTileList::clear()
{
    // They are not tiles just references. The actual tiles have already been deleted,
    // so just delete the references.

    const DeletedTile *deletedTile = m_firstDeletedTile;

    while (deletedTile)
    {
        const DeletedTile *d = deletedTile;
        deletedTile = deletedTile->next();
        delete d;
    }

    m_firstDeletedTile = 0;
}

void KisMemento::deleteAll(KisTile *tile)
{
    while(tile)
    {
        KisTile *deltile = tile;
        tile = tile->getNext();
        delete deltile;
    }
}

void KisMemento::extent(TQ_INT32 &x, TQ_INT32 &y, TQ_INT32 &w, TQ_INT32 &h) const
{
    TQ_INT32 maxX = TQ_INT32_MIN;
    TQ_INT32 maxY = TQ_INT32_MIN;
    x = TQ_INT32_MAX;
    y = TQ_INT32_MAX;

    for(int i = 0; i < 1024; i++)
    {
        KisTile *tile = m_hashTable[i];

        while(tile)
        {
            if(x > tile->getCol() * KisTile::WIDTH)
                x = tile->getCol() * KisTile::WIDTH;
            if(maxX < (tile->getCol() + 1) * KisTile::WIDTH - 1)
                maxX = (tile->getCol() + 1) * KisTile::WIDTH - 1;
            if(y > tile->getRow() * KisTile::HEIGHT)
                y = tile->getRow() * KisTile::HEIGHT;
            if(maxY < (tile->getRow() +1) * KisTile::HEIGHT - 1)
                maxY = (tile->getRow() +1) * KisTile::HEIGHT - 1;

            tile = tile->getNext();
        }
    }

    if(maxX < x)
        w = 0;
    else
        w = maxX - x +1;

    if(maxY < y)
        h = 0;
    else
        h = maxY - y +1;
}

TQRect KisMemento::extent() const
{
    TQ_INT32 x;
    TQ_INT32 y;
    TQ_INT32 w;
    TQ_INT32 h;

    extent(x, y, w, h);

    return TQRect(x, y, w, h);
}

bool KisMemento::containsTile(TQ_INT32 col, TQ_INT32 row, TQ_UINT32 tileHash) const
{
    const KisTile *tile = m_hashTable[tileHash];

    while (tile != 0)
    {
        if (tile->getRow() == row && tile->getCol() == col) {
            return true;
        }

        tile = tile->getNext();
    }

    return false;
}