summaryrefslogtreecommitdiffstats
path: root/kolourpaint/pixmapfx/kpfloodfill.cpp
blob: 3eef76c314eb7b53d38c2fc8f2b6f77aa0e73b43 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362

/*
   Copyright (c) 2003,2004,2005 Clarence Dang <[email protected]>
   All rights reserved.

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   1. Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
   IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
   IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
   INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
   NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
   THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/


#define DEBUG_KP_FLOOD_FILL 0


#include <kpfloodfill.h>

#include <tqapplication.h>
#include <tqbitmap.h>
#include <tqpainter.h>
#include <tqpixmap.h>

#include <kdebug.h>
#include <kpdefs.h>

#include <kppixmapfx.h>
#include <kptool.h>


kpFloodFill::kpFloodFill (TQPixmap *pixmap, int x, int y,
                         const kpColor &color, int processedColorSimilarity)
    : m_pixmapPtr (pixmap), m_x (x), m_y (y),
      m_color (color), m_processedColorSimilarity (processedColorSimilarity),
      m_initState (0)
{
}

kpFloodFill::~kpFloodFill ()
{
}


// private
int kpFloodFill::fillLinesListSize (const TQValueList <kpFloodFill::FillLine> &fillLines) const
{
    return (fillLines.size () * kpFloodFill::FillLine::size ());
}
    
// public
int kpFloodFill::size () const
{
    int fillLinesCacheSize = 0;
    for (TQValueVector < TQValueList <kpFloodFill::FillLine > >::const_iterator it = m_fillLinesCache.begin ();
         it != m_fillLinesCache.end ();
         it++)
    {
        fillLinesCacheSize += fillLinesListSize (*it);
    }
    
    return fillLinesListSize (m_fillLines) +
           kpPixmapFX::imageSize (m_image) +
           fillLinesCacheSize;
}


TQRect kpFloodFill::boundingRect () const
{
    return m_boundingRect;
}

bool kpFloodFill::fill ()
{
    if (m_initState < 2 && !prepare ())
    {
        kdError () << "kpFloodFill:fill() could not prepare()!" << endl;
        return false;
    }

    // not trying to do a NOP fill
    if (m_boundingRect.isValid ())
    {
        TQApplication::setOverrideCursor (TQt::waitCursor);

        TQPainter painter, maskPainter;
        TQBitmap maskBitmap;

        if (m_pixmapPtr->mask () || m_color.isTransparent ())
        {
            maskBitmap = kpPixmapFX::getNonNullMask (*m_pixmapPtr);
            maskPainter.begin (&maskBitmap);
            maskPainter.setPen (m_color.maskColor ());
        }

        if (m_color.isOpaque ())
        {
            painter.begin (m_pixmapPtr);
            painter.setPen (m_color.toTQColor ());
        }

        const TQValueList <FillLine>::ConstIterator fillLinesEnd = m_fillLines.end ();
        for (TQValueList <FillLine>::ConstIterator it = m_fillLines.begin ();
             it != fillLinesEnd;
             it++)
        {
            TQPoint p1 = TQPoint ((*it).m_x1, (*it).m_y);
            TQPoint p2 = TQPoint ((*it).m_x2, (*it).m_y);

            if (painter.isActive ())
                painter.drawLine (p1, p2);

            if (maskPainter.isActive ())
                maskPainter.drawLine (p1, p2);
        }

        if (painter.isActive ())
            painter.end ();

        if (maskPainter.isActive ())
            maskPainter.end ();

        if (!maskBitmap.isNull ())
            m_pixmapPtr->setMask (maskBitmap);

        TQApplication::restoreOverrideCursor ();
    }
    else
    {
    #if DEBUG_KP_FLOOD_FILL && 1
        kdDebug () << "kpFloodFill::fill() performing NOP fill" << endl;
    #endif
    }

    return true;
}

bool kpFloodFill::prepareColorToChange ()
{
#if DEBUG_KP_FLOOD_FILL && 1
    kdDebug () << "kpFloodFill::prepareColorToChange" << endl;
#endif

    m_colorToChange = kpPixmapFX::getColorAtPixel (*m_pixmapPtr, TQPoint (m_x, m_y));

    if (m_colorToChange.isOpaque ())
    {
    #if DEBUG_KP_FLOOD_FILL && 1
        kdDebug () << "\tcolorToChange: r=" << m_colorToChange.red ()
                   << ", b=" << m_colorToChange.blue ()
                   << ", g=" << m_colorToChange.green ()
                   << endl;
    #endif
    }
    else
    {
    #if DEBUG_KP_FLOOD_FILL && 1
        kdDebug () << "\tcolorToChange: transparent" << endl;
    #endif
    }

    m_initState = 1;
    return true;
}

// Derived from the zSprite2 Graphics Engine

bool kpFloodFill::prepare ()
{
#if DEBUG_KP_FLOOD_FILL && 1
    kdDebug () << "kpFloodFill::prepare()" << endl;
#endif
    m_boundingRect = TQRect ();

    if (m_initState < 1 && !prepareColorToChange ())
    {
        kdError () << "kpFloodFill:prepare() could not prepareColorToChange()!" << endl;
        return false;
    }

#if DEBUG_KP_FLOOD_FILL && 1
    kdDebug () << "\tperforming NOP check" << endl;
#endif

    // get the color we need to replace
    if (m_processedColorSimilarity == 0 && m_color == m_colorToChange)
    {
        // need to do absolutely nothing (this is a significant optimisation
        // for people who randomly click a lot over already-filled areas)
        m_initState = 2;  // sync with all "return true"'s
        return true;
    }

#if DEBUG_KP_FLOOD_FILL && 1
    kdDebug () << "\tconverting to image" << endl;
#endif

    // is this the only way to read pixels?
    m_image = kpPixmapFX::convertToImage (*m_pixmapPtr);
    if (m_image.isNull ())
    {
        kdError () << "kpFloodFill::prepare() could not convert to TQImage" << endl;
        return false;
    }

#if DEBUG_KP_FLOOD_FILL && 1
    kdDebug () << "\tcreating fillLinesCache" << endl;
#endif

    // ready cache
    m_fillLinesCache.resize (m_pixmapPtr->height ());

#if DEBUG_KP_FLOOD_FILL && 1
    kdDebug () << "\tcreating fill lines" << endl;
#endif

    // draw initial line
    addLine (m_y, findMinX (m_y, m_x), findMaxX (m_y, m_x));

    for (TQValueList <FillLine>::ConstIterator it = m_fillLines.begin ();
         it != m_fillLines.end ();
         it++)
    {
    #if DEBUG_KP_FLOOD_FILL && 0
        kdDebug () << "Expanding from y=" << (*it).m_y
                   << " x1=" << (*it).m_x1
                   << " x2=" << (*it).m_x2
                   << endl;
    #endif

        // make more lines above and below current line
        findAndAddLines (*it, -1);
        findAndAddLines (*it, +1);
    }

#if DEBUG_KP_FLOOD_FILL && 1
    kdDebug () << "\tfinalising memory usage" << endl;
#endif

    // finalize memory usage
    m_image.reset ();
    m_fillLinesCache.clear ();

    m_initState = 2;  // sync with all "return true"'s
    return true;
}

void kpFloodFill::addLine (int y, int x1, int x2)
{
#if DEBUG_KP_FLOOD_FILL && 0
    kdDebug () << "kpFillCommand::fillAddLine (" << y << "," << x1 << "," << x2 << ")" << endl;
#endif

    m_fillLines.append (FillLine (y, x1, x2));
    m_fillLinesCache [y].append (FillLine (y /* OPT */, x1, x2));
    m_boundingRect = m_boundingRect.unite (TQRect (TQPoint (x1, y), TQPoint (x2, y)));
}

kpColor kpFloodFill::pixelColor (int x, int y, bool *beenHere) const
{
    if (beenHere)
        *beenHere = false;

    if (y >= (int) m_fillLinesCache.count ())
    {
        kdError () << "kpFloodFill::pixelColor("
                   << x << ","
                   << y << ") y out of range=" << m_pixmapPtr->height () << endl;
        return kpColor::invalid;
    }

    const TQValueList <FillLine>::ConstIterator theEnd = m_fillLinesCache [y].end ();
    for (TQValueList <FillLine>::ConstIterator it = m_fillLinesCache [y].begin ();
         it != theEnd;
         it++)
    {
        if (x >= (*it).m_x1 && x <= (*it).m_x2)
        {
            if (beenHere)
                *beenHere = true;
            return m_color;
        }
    }

    return kpPixmapFX::getColorAtPixel (m_image, TQPoint (x, y));
}

bool kpFloodFill::shouldGoTo (int x, int y) const
{
    bool beenThere;
    const kpColor col = pixelColor (x, y, &beenThere);

    return (!beenThere && col.isSimilarTo (m_colorToChange, m_processedColorSimilarity));
}

void kpFloodFill::findAndAddLines (const FillLine &fillLine, int dy)
{
    // out of bounds?
    if (fillLine.m_y + dy < 0 || fillLine.m_y + dy >= m_pixmapPtr->height ())
        return;

    for (int xnow = fillLine.m_x1; xnow <= fillLine.m_x2; xnow++)
    {
        // At current position, right colour?
        if (shouldGoTo (xnow, fillLine.m_y + dy))
        {
            // Find minimum and maximum x values
            int minxnow = findMinX (fillLine.m_y + dy, xnow);
            int maxxnow = findMaxX (fillLine.m_y + dy, xnow);

            // Draw line
            addLine (fillLine.m_y + dy, minxnow, maxxnow);

            // Move x pointer
            xnow = maxxnow;
        }
    }
}

// finds the minimum x value at a certain line to be filled
int kpFloodFill::findMinX (int y, int x) const
{
    for (;;)
    {
        if (x < 0)
            return 0;

        if (shouldGoTo (x, y))
            x--;
        else
            return x + 1;
    }
}

// finds the maximum x value at a certain line to be filled
int kpFloodFill::findMaxX (int y, int x) const
{
    for (;;)
    {
        if (x > m_pixmapPtr->width () - 1)
            return m_pixmapPtr->width () - 1;

        if (shouldGoTo (x, y))
            x++;
        else
            return x - 1;
    }
}