summaryrefslogtreecommitdiffstats
path: root/chalk/core/tiles/kis_tiledhlineiterator.cpp
blob: cf023c1ed863c98a7939f5b643c8c054127681ae (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
/*
 * This file is part of the KDE project
 *
 * Copyright (c) 2004 Casper Boemann <cbr@boemann.dk>
 *
 *  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 <kdebug.h>

#include "kis_tile_global.h"
#include "kis_tilediterator.h"

KisTiledHLineIterator::KisTiledHLineIterator( KisTiledDataManager *ndevice,  TQ_INT32 x, TQ_INT32 y, TQ_INT32 w, bool writable) :
    KisTiledIterator(ndevice),
    m_right(x+w-1), m_left(x)
{
    Q_ASSERT(ndevice != 0);

    m_writable = writable;
    m_x = x;
    m_y = y;

    // Find tile row,col matching x,y
    m_row = yToRow(m_y);
    m_leftCol = xToCol(m_x);
    m_rightCol = xToCol(m_right);
    m_col = m_leftCol;

    // calc limits within the tile
    m_yInTile = m_y - m_row * KisTile::HEIGHT;
    m_leftInTile = m_x - m_leftCol * KisTile::WIDTH;

    if(m_col == m_rightCol)
        m_rightInTile = m_right - m_rightCol * KisTile::WIDTH;
    else
        m_rightInTile = KisTile::WIDTH - 1;

    m_xInTile = m_leftInTile;

    fetchTileData(m_col, m_row);
    m_offset = m_pixelSize * (m_yInTile * KisTile::WIDTH + m_xInTile);
}

KisTiledHLineIterator::KisTiledHLineIterator(const KisTiledHLineIterator& rhs)
    : KisTiledIterator(rhs)
{
    if (this != &rhs) {
        m_right = rhs.m_right;
        m_left = rhs.m_left;
        m_leftCol = rhs.m_leftCol;
        m_rightCol = rhs.m_rightCol;
        m_xInTile = rhs.m_xInTile;
        m_yInTile = rhs.m_yInTile;
        m_leftInTile = rhs.m_leftInTile;
        m_rightInTile = rhs.m_rightInTile;
    }
}

KisTiledHLineIterator& KisTiledHLineIterator::operator=(const KisTiledHLineIterator& rhs)
{
    if (this != &rhs) {
        KisTiledIterator::operator=(rhs);
        m_right = rhs.m_right;
        m_left = rhs.m_left;
        m_leftCol = rhs.m_leftCol;
        m_rightCol = rhs.m_rightCol;
        m_xInTile = rhs.m_xInTile;
        m_yInTile = rhs.m_yInTile;
        m_leftInTile = rhs.m_leftInTile;
        m_rightInTile = rhs.m_rightInTile;
    }
    return *this;
}

KisTiledHLineIterator::~KisTiledHLineIterator( )
{
}

KisTiledHLineIterator & KisTiledHLineIterator::operator ++ ()
{
    if(m_xInTile >= m_rightInTile)
    {
        nextTile();
        fetchTileData(m_col, m_row);
        m_xInTile =m_leftInTile;
        m_offset = m_pixelSize * (m_yInTile * KisTile::WIDTH + m_xInTile);
    }
    else
    {
        m_xInTile++;
        m_offset += m_pixelSize;
    }
    m_x++;

    return *this;
}

void KisTiledHLineIterator::nextTile()
{
    if(m_col < m_rightCol)
    {
        m_col++;
        m_leftInTile = 0;

        if(m_col == m_rightCol)
            m_rightInTile = m_right - m_rightCol * KisTile::WIDTH;
        else
            m_rightInTile = KisTile::WIDTH - 1;
    }
}

void KisTiledHLineIterator::prevTile()
{
    if(m_col > m_leftCol)
    {
        m_col--;

        if(m_col == m_leftCol) {
            m_leftInTile = m_left - m_leftCol * KisTile::WIDTH;
        } else {
            m_leftInTile = 0;
        }
        // the only place this doesn't apply, is if we're in rightCol, and we can't go there
        m_rightInTile = KisTile::WIDTH - 1;
    }
}

TQ_INT32 KisTiledHLineIterator::nConseqHPixels() const
{
    return m_rightInTile - m_xInTile + 1;
}

KisTiledHLineIterator & KisTiledHLineIterator::operator+=(int n)
{
    // XXX what if outside the valid range of this iterator?
    if(m_xInTile + n > m_rightInTile)
    {
        m_x += n;
        m_col = xToCol(m_x);
        m_xInTile = m_x - m_col * KisTile::WIDTH;
        m_leftInTile = 0;

        if(m_col == m_rightCol)
            m_rightInTile = m_right - m_rightCol * KisTile::WIDTH;
        else
            m_rightInTile = KisTile::WIDTH - 1;

        fetchTileData(m_col, m_row);
    }
    else
    {
        m_xInTile += n;
        m_x += n;
    }
    m_offset = m_pixelSize * (m_yInTile * KisTile::WIDTH + m_xInTile);

    return *this;
}

KisTiledHLineIterator & KisTiledHLineIterator::operator -- ()
{
    if(m_xInTile <= 0)
    {
        prevTile();
        fetchTileData(m_col, m_row);
        m_xInTile = KisTile::WIDTH - 1;
        m_offset = m_pixelSize * (m_yInTile * KisTile::WIDTH + m_xInTile);
    }
    else
    {
        m_xInTile--;
        m_offset -= m_pixelSize;
    }
    m_x--;

    return *this;
}

void KisTiledHLineIterator::nextRow()
{
    m_y++;
    m_yInTile++;
    m_x = m_left;
    m_leftInTile = m_x - m_leftCol * KisTile::WIDTH;
    m_xInTile = m_leftInTile;
    if( m_yInTile >= KisTile::HEIGHT )
    { // Need a new row
        m_yInTile = 0;
        m_row++;
        m_col = m_leftCol;
        fetchTileData(m_col, m_row);
    } else if( m_leftCol != m_col ) {
        m_col = m_leftCol;
        fetchTileData(m_col, m_row);
    }
    if(m_col == m_rightCol)
        m_rightInTile = m_right - m_rightCol * KisTile::WIDTH;
    else
        m_rightInTile = KisTile::WIDTH - 1;
    m_offset = m_pixelSize * (m_yInTile * KisTile::WIDTH + m_xInTile);
}