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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
|
/*
* Copyright (c) 2004 Adrian Page <[email protected]>
* Copyright (c) 2004 Bart Coppens <[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 <stdlib.h>
#include <string.h>
#include <cfloat>
#include <stack>
#include "tqbrush.h"
#include "tqfontinfo.h"
#include "tqfontmetrics.h"
#include "tqpen.h"
#include "tqregion.h"
#include "tqwmatrix.h"
#include <tqimage.h>
#include <tqmap.h>
#include <tqpainter.h>
#include <tqpixmap.h>
#include <tqpointarray.h>
#include <tqrect.h>
#include <tqstring.h>
#include <kdebug.h>
#include <kcommand.h>
#include <klocale.h>
#include "kis_brush.h"
#include "kis_debug_areas.h"
#include "kis_image.h"
#include "kis_layer.h"
#include "kis_paint_device.h"
#include "kis_painter.h"
#include "kis_pattern.h"
#include "kis_rect.h"
#include "kis_colorspace.h"
#include "kis_transaction.h"
#include "kis_types.h"
#include "kis_vec.h"
#include "kis_selection.h"
#include "kis_fill_painter.h"
#include "kis_iterators_pixel.h"
#include "kis_iterator.h"
#include "kis_color.h"
#include "kis_selection.h"
namespace {
}
KisFillPainter::KisFillPainter()
: super()
{
m_width = m_height = -1;
m_sampleMerged = false;
m_careForSelection = false;
m_fuzzy = false;
}
KisFillPainter::KisFillPainter(KisPaintDeviceSP device) : super(device)
{
m_width = m_height = -1;
m_sampleMerged = false;
m_careForSelection = false;
m_fuzzy = false;
}
// 'regular' filling
// XXX: This also needs renaming, since filling ought to keep the opacity and the composite op in mind,
// this is more eraseToColor.
void KisFillPainter::fillRect(TQ_INT32 x1, TQ_INT32 y1, TQ_INT32 w, TQ_INT32 h, const KisColor& kc, TQ_UINT8 opacity)
{
if (w > 0 && h > 0) {
// Make sure we're in the right colorspace
KisColor kc2(kc); // get rid of const
kc2.convertTo(m_device->colorSpace());
TQ_UINT8 * data = kc2.data();
m_device->colorSpace()->setAlpha(data, opacity, 1);
m_device->fill(x1, y1, w, h, data);
addDirtyRect(TQRect(x1, y1, w, h));
}
}
void KisFillPainter::fillRect(TQ_INT32 x1, TQ_INT32 y1, TQ_INT32 w, TQ_INT32 h, KisPattern * pattern) {
if (!pattern) return;
if (!pattern->valid()) return;
if (!m_device) return;
KisPaintDeviceSP patternLayer = pattern->image(m_device->colorSpace());
int sx, sy, sw, sh;
int y = y1;
if (y >= 0) {
sy = y % pattern->height();
} else {
sy = pattern->height() - (((-y - 1) % pattern->height()) + 1);
}
while (y < y1 + h) {
sh = TQMIN((y1 + h) - y, pattern->height() - sy);
int x = x1;
if (x >= 0) {
sx = x % pattern->width();
} else {
sx = pattern->width() - (((-x - 1) % pattern->width()) + 1);
}
while (x < x1 + w) {
sw = TQMIN((x1 + w) - x, pattern->width() - sx);
bitBlt(x, y, m_compositeOp, patternLayer.data(), m_opacity, sx, sy, sw, sh);
x += sw; sx = 0;
}
y+=sh; sy = 0;
}
addDirtyRect(TQRect(x1, y1, w, h));
}
// flood filling
void KisFillPainter::fillColor(int startX, int startY) {
genericFillStart(startX, startY);
// Now create a layer and fill it
KisPaintDeviceSP filled = new KisPaintDevice(m_device->colorSpace(), "filled");
Q_CHECK_PTR(filled);
KisFillPainter painter(filled.data());
painter.fillRect(0, 0, m_width, m_height, m_paintColor);
painter.end();
genericFillEnd(filled);
}
void KisFillPainter::fillPattern(int startX, int startY) {
genericFillStart(startX, startY);
// Now create a layer and fill it
KisPaintDeviceSP filled = new KisPaintDevice(m_device->colorSpace(), "filled");
Q_CHECK_PTR(filled);
KisFillPainter painter(filled.data());
painter.fillRect(0, 0, m_width, m_height, m_pattern);
painter.end();
genericFillEnd(filled);
}
void KisFillPainter::genericFillStart(int startX, int startY) {
m_cancelRequested = false;
if (m_width < 0 || m_height < 0) {
if (m_device->image()) {
m_width = m_device->image()->width();
m_height = m_device->image()->height();
} else {
m_width = m_height = 500;
}
}
m_size = m_width * m_height;
// Create a selection from the surrounding area
m_selection = createFloodSelection(startX, startY);
}
void KisFillPainter::genericFillEnd(KisPaintDeviceSP filled) {
if (m_cancelRequested) {
m_width = m_height = -1;
return;
}
TQRect rc = m_selection->selectedRect();
bltSelection(rc.x(), rc.y(), m_compositeOp, filled, m_selection, m_opacity,
rc.x(), rc.y(), rc.width(), rc.height());
emit notifyProgressDone();
m_width = m_height = -1;
}
struct FillSegment {
FillSegment(int x, int y/*, FillSegment* tqparent*/) : x(x), y(y)/*, tqparent(tqparent)*/ {}
int x;
int y;
// FillSegment* tqparent;
};
typedef enum { None = 0, Added = 1, Checked = 2 } tqStatus;
KisSelectionSP KisFillPainter::createFloodSelection(int startX, int startY) {
if (m_width < 0 || m_height < 0) {
if (m_device->hasSelection() && m_careForSelection) {
TQRect rc = m_device->selection()->selectedRect();
m_width = rc.width() - (startX - rc.x());
m_height = rc.height() - (startY - rc.y());
} else if (m_device->image()) {
m_width = m_device->image()->width();
m_height = m_device->image()->height();
} else {
m_width = m_height = 500;
}
}
// Don't try to fill if we start outside the borders, just return an empty 'fill'
if (startX < 0 || startY < 0 || startX >= m_width || startY >= m_height)
return new KisSelection(m_device);
KisPaintDeviceSP sourceDevice = 0;
// sample merged?
if (m_sampleMerged) {
if (!m_device->image()) {
return new KisSelection(m_device);
}
sourceDevice = m_device->image()->mergedImage();
} else {
sourceDevice = m_device;
}
m_size = m_width * m_height;
KisSelectionSP selection = new KisSelection(m_device);
KisColorSpace * colorSpace = selection->colorSpace();
KisColorSpace * devColorSpace = sourceDevice->colorSpace();
TQ_UINT8* source = new TQ_UINT8[sourceDevice->pixelSize()];
KisHLineIteratorPixel pixelIt = sourceDevice->createHLineIterator(startX, startY, startX+1, false);
memcpy(source, pixelIt.rawData(), sourceDevice->pixelSize());
std::stack<FillSegment*> stack;
stack.push(new FillSegment(startX, startY/*, 0*/));
tqStatus* map = new tqStatus[m_size];
memset(map, None, m_size * sizeof(tqStatus));
int progressPercent = 0; int pixelsDone = 0; int currentPercent = 0;
emit notifyProgressStage(i18n("Making fill outline..."), 0);
bool hasSelection = m_careForSelection && sourceDevice->hasSelection();
KisSelectionSP srcSel = 0;
if (hasSelection)
srcSel = sourceDevice->selection();
while(!stack.empty()) {
FillSegment* segment = stack.top();
stack.pop();
if (map[m_width * segment->y + segment->x] == Checked) {
delete segment;
continue;
}
map[m_width * segment->y + segment->x] = Checked;
int x = segment->x;
int y = segment->y;
/* We need an iterator that is valid in the range (0,y) - (width,y). Therefore,
it is needed to start the iterator at the first position, and then skip to (x,y). */
pixelIt = sourceDevice->createHLineIterator(0, y, m_width, false);
pixelIt += x;
TQ_UINT8 diff = devColorSpace->difference(source, pixelIt.rawData());
if (diff >= m_threshold
|| (hasSelection && srcSel->selected(pixelIt.x(), pixelIt.y()) == MIN_SELECTED)) {
delete segment;
continue;
}
// Here as well: start the iterator at (0,y)
KisHLineIteratorPixel selIt = selection->createHLineIterator(0, y, m_width, true);
selIt += x;
if (m_fuzzy)
colorSpace->fromTQColor(TQt::white, MAX_SELECTED - diff, selIt.rawData());
else
colorSpace->fromTQColor(TQt::white, MAX_SELECTED, selIt.rawData());
if (y > 0 && (map[m_width * (y - 1) + x] == None)) {
map[m_width * (y - 1) + x] = Added;
stack.push(new FillSegment(x, y-1));
}
if (y < (m_height - 1) && (map[m_width * (y + 1) + x] == None)) {
map[m_width * (y + 1) + x] = Added;
stack.push(new FillSegment(x, y+1));
}
++pixelsDone;
bool stop = false;
--pixelIt;
--selIt;
--x;
// go to the left
while(!stop && x >= 0 && (map[m_width * y + x] != Checked) ) { // FIXME optimizeable?
map[m_width * y + x] = Checked;
diff = devColorSpace->difference(source, pixelIt.rawData());
if (diff >= m_threshold
|| (hasSelection && srcSel->selected(pixelIt.x(), pixelIt.y()) == MIN_SELECTED)) {
stop = true;
continue;
}
if (m_fuzzy)
colorSpace->fromTQColor(TQt::white, MAX_SELECTED - diff, selIt.rawData());
else
colorSpace->fromTQColor(TQt::white, MAX_SELECTED, selIt.rawData());
if (y > 0 && (map[m_width * (y - 1) + x] == None)) {
map[m_width * (y - 1) + x] = Added;
stack.push(new FillSegment(x, y-1));
}
if (y < (m_height - 1) && (map[m_width * (y + 1) + x] == None)) {
map[m_width * (y + 1) + x] = Added;
stack.push(new FillSegment(x, y+1));
}
++pixelsDone;
--pixelIt;
--selIt;
--x;
}
x = segment->x + 1;
delete segment;
if (map[m_width * y + x] == Checked)
continue;
// and go to the right
pixelIt = sourceDevice->createHLineIterator(x, y, m_width, false);
selIt = selection->createHLineIterator(x, y, m_width, true);
stop = false;
while(!stop && x < m_width && (map[m_width * y + x] != Checked) ) {
diff = devColorSpace->difference(source, pixelIt.rawData());
map[m_width * y + x] = Checked;
if (diff >= m_threshold
|| (hasSelection && srcSel->selected(pixelIt.x(), pixelIt.y()) == MIN_SELECTED) ) {
stop = true;
continue;
}
if (m_fuzzy)
colorSpace->fromTQColor(TQt::white, MAX_SELECTED - diff, selIt.rawData());
else
colorSpace->fromTQColor(TQt::white, MAX_SELECTED, selIt.rawData());
if (y > 0 && (map[m_width * (y - 1) + x] == None)) {
map[m_width * (y - 1) + x] = Added;
stack.push(new FillSegment(x, y-1));
}
if (y < (m_height - 1) && (map[m_width * (y + 1) + x] == None)) {
map[m_width * (y + 1) + x] = Added;
stack.push(new FillSegment(x, y+1));
}
++pixelsDone;
++pixelIt;
++selIt;
++x;
}
if (m_size > 0) {
progressPercent = (pixelsDone * 100) / m_size;
if (progressPercent > currentPercent) {
emit notifyProgress(progressPercent);
currentPercent = progressPercent;
}
}
}
delete[] map;
delete[] source;
return selection;
}
|