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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
|
/*
* Copyright (c) 2004 Boudewijn Rempt <boud@valdyas.org>
*
* 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., 675 mass ave, cambridge, ma 02139, usa.
*/
#include <tqimage.h>
#include <kdebug.h>
#include <tdelocale.h>
#include <tqcolor.h>
#include "kis_layer.h"
#include "kis_debug_areas.h"
#include "kis_types.h"
#include "kis_colorspace_factory_registry.h"
#include "kis_fill_painter.h"
#include "kis_iterators_pixel.h"
#include "kis_integer_maths.h"
#include "kis_image.h"
#include "kis_datamanager.h"
#include "kis_fill_painter.h"
#include "kis_selection.h"
KisSelection::KisSelection(KisPaintDeviceSP dev)
: super(dev->parentLayer()
, KisMetaRegistry::instance()->csRegistry()->getAlpha8()
, (TQString("selection for ") + dev->name()).latin1())
, m_parentPaintDevice(dev)
, m_doCacheExactRect(false)
, m_dirty(false)
{
Q_ASSERT(dev);
}
KisSelection::KisSelection()
: super(KisMetaRegistry::instance()->csRegistry()->getAlpha8(), "anonymous selection")
, m_parentPaintDevice(0), m_dirty(false)
{
}
KisSelection::KisSelection(const KisSelection& rhs)
: super(rhs), m_parentPaintDevice(rhs.m_parentPaintDevice), m_doCacheExactRect(rhs.m_doCacheExactRect),
m_cachedExactRect(rhs.m_cachedExactRect), m_dirty(rhs.m_dirty)
{
}
KisSelection::~KisSelection()
{
}
TQ_UINT8 KisSelection::selected(TQ_INT32 x, TQ_INT32 y)
{
KisHLineIteratorPixel iter = createHLineIterator(x, y, 1, false);
TQ_UINT8 *pix = iter.rawData();
return *pix;
}
void KisSelection::setSelected(TQ_INT32 x, TQ_INT32 y, TQ_UINT8 s)
{
KisHLineIteratorPixel iter = createHLineIterator(x, y, 1, true);
TQ_UINT8 *pix = iter.rawData();
*pix = s;
}
TQImage KisSelection::maskImage()
{
// If part of a KisAdjustmentLayer, there may be no parent device.
TQImage img;
TQRect bounds;
if (m_parentPaintDevice) {
bounds = m_parentPaintDevice->exactBounds();
bounds = bounds.intersect( m_parentPaintDevice->image()->bounds() );
img = TQImage(bounds.width(), bounds.height(), 32);
}
else {
bounds = TQRect( 0, 0, image()->width(), image()->height());
img = TQImage(bounds.width(), bounds.height(), 32);
}
KisHLineIteratorPixel it = createHLineIterator(bounds.x(), bounds.y(), bounds.width(), false);
for (int y2 = bounds.y(); y2 < bounds.height() - bounds.y(); ++y2) {
int x2 = 0;
while (!it.isDone()) {
TQ_UINT8 s = MAX_SELECTED - *(it.rawData());
TQ_INT32 c = tqRgb(s, s, s);
img.setPixel(x2, y2, c);
++x2;
++it;
}
it.nextRow();
}
return img;
}
void KisSelection::select(TQRect r)
{
KisFillPainter painter(this);
KisColorSpace * cs = KisMetaRegistry::instance()->csRegistry()->getRGB8();
painter.fillRect(r, KisColor(TQt::white, cs), MAX_SELECTED);
TQ_INT32 x, y, w, h;
extent(x, y, w, h);
}
void KisSelection::clear(TQRect r)
{
KisFillPainter painter(this);
KisColorSpace * cs = KisMetaRegistry::instance()->csRegistry()->getRGB8();
painter.fillRect(r, KisColor(TQt::white, cs), MIN_SELECTED);
}
void KisSelection::clear()
{
TQ_UINT8 defPixel = MIN_SELECTED;
m_datamanager->setDefaultPixel(&defPixel);
m_datamanager->clear();
}
void KisSelection::invert()
{
TQ_INT32 x,y,w,h;
extent(x, y, w, h);
KisRectIterator it = createRectIterator(x, y, w, h, true);
while ( ! it.isDone() )
{
// CBR this is wrong only first byte is inverted
// BSAR: But we have always only one byte in this color model :-).
*(it.rawData()) = MAX_SELECTED - *(it.rawData());
++it;
}
TQ_UINT8 defPixel = MAX_SELECTED - *(m_datamanager->defaultPixel());
m_datamanager->setDefaultPixel(&defPixel);
}
bool KisSelection::isTotallyUnselected(TQRect r)
{
if(*(m_datamanager->defaultPixel()) != MIN_SELECTED)
return false;
TQRect sr = selectedExactRect();
return ! r.intersects(sr);
}
bool KisSelection::isProbablyTotallyUnselected(TQRect r)
{
if(*(m_datamanager->defaultPixel()) != MIN_SELECTED)
return false;
TQRect sr = selectedRect();
return ! r.intersects(sr);
}
TQRect KisSelection::selectedRect() const
{
if(*(m_datamanager->defaultPixel()) == MIN_SELECTED || !m_parentPaintDevice)
return extent();
else
return extent().unite(m_parentPaintDevice->extent());
}
TQRect KisSelection::selectedExactRect() const
{
if(m_doCacheExactRect)
return m_cachedExactRect;
else if(*(m_datamanager->defaultPixel()) == MIN_SELECTED || !m_parentPaintDevice)
return exactBounds();
else
return exactBounds().unite(m_parentPaintDevice->exactBounds());
}
void KisSelection::stopCachingExactRect()
{
kdDebug() << "stop caching the exact rect" << endl;
m_doCacheExactRect = false;
}
void KisSelection::startCachingExactRect()
{
kdDebug() << "start caching the exact rect" << endl;
if(*(m_datamanager->defaultPixel()) == MIN_SELECTED || !m_parentPaintDevice)
m_cachedExactRect = exactBounds();
else
m_cachedExactRect = exactBounds().unite(m_parentPaintDevice->exactBounds());
m_doCacheExactRect = true;
}
void KisSelection::paintUniformSelectionRegion(TQImage img, const TQRect& imageRect, const TQRegion& uniformRegion)
{
Q_ASSERT(img.size() == imageRect.size());
Q_ASSERT(imageRect.contains(uniformRegion.boundingRect()));
if (img.isNull() || img.size() != imageRect.size() || !imageRect.contains(uniformRegion.boundingRect())) {
return;
}
if (*m_datamanager->defaultPixel() == MIN_SELECTED) {
TQRegion region = uniformRegion & TQRegion(imageRect);
if (!region.isEmpty()) {
TQMemArray<TQRect> rects = region.rects();
for (unsigned int i = 0; i < rects.count(); i++) {
TQRect r = rects[i];
for (TQ_INT32 y = 0; y < r.height(); ++y) {
TQRgb *imagePixel = reinterpret_cast<TQRgb *>(img.scanLine(r.y() - imageRect.y() + y));
imagePixel += r.x() - imageRect.x();
TQ_INT32 numPixels = r.width();
while (numPixels > 0) {
TQRgb srcPixel = *imagePixel;
TQ_UINT8 srcGrey = (tqRed(srcPixel) + tqGreen(srcPixel) + tqBlue(srcPixel)) / 9;
TQ_UINT8 srcAlpha = tqAlpha(srcPixel);
srcGrey = UINT8_MULT(srcGrey, srcAlpha);
TQ_UINT8 dstAlpha = TQMAX(srcAlpha, 192);
TQRgb dstPixel = tqRgba(128 + srcGrey, 128 + srcGrey, 165 + srcGrey, dstAlpha);
*imagePixel = dstPixel;
++imagePixel;
--numPixels;
}
}
}
}
}
}
void KisSelection::paintSelection(TQImage img, TQ_INT32 imageRectX, TQ_INT32 imageRectY, TQ_INT32 imageRectWidth, TQ_INT32 imageRectHeight)
{
Q_ASSERT(img.size() == TQSize(imageRectWidth, imageRectHeight));
if (img.isNull() || img.size() != TQSize(imageRectWidth, imageRectHeight)) {
return;
}
TQRect imageRect(imageRectX, imageRectY, imageRectWidth, imageRectHeight);
TQRect selectionExtent = extent();
selectionExtent.setLeft(selectionExtent.left() - 1);
selectionExtent.setTop(selectionExtent.top() - 1);
selectionExtent.setWidth(selectionExtent.width() + 2);
selectionExtent.setHeight(selectionExtent.height() + 2);
TQRegion uniformRegion = TQRegion(imageRect);
uniformRegion -= TQRegion(selectionExtent);
if (!uniformRegion.isEmpty()) {
paintUniformSelectionRegion(img, imageRect, uniformRegion);
}
TQRect nonuniformRect = imageRect & selectionExtent;
if (!nonuniformRect.isEmpty()) {
const TQ_INT32 imageRectOffsetX = nonuniformRect.x() - imageRectX;
const TQ_INT32 imageRectOffsetY = nonuniformRect.y() - imageRectY;
imageRectX = nonuniformRect.x();
imageRectY = nonuniformRect.y();
imageRectWidth = nonuniformRect.width();
imageRectHeight = nonuniformRect.height();
const TQ_INT32 NUM_SELECTION_ROWS = 3;
TQ_UINT8 *selectionRow[NUM_SELECTION_ROWS];
TQ_INT32 aboveRowIndex = 0;
TQ_INT32 centreRowIndex = 1;
TQ_INT32 belowRowIndex = 2;
selectionRow[aboveRowIndex] = new TQ_UINT8[imageRectWidth + 2];
selectionRow[centreRowIndex] = new TQ_UINT8[imageRectWidth + 2];
selectionRow[belowRowIndex] = new TQ_UINT8[imageRectWidth + 2];
readBytes(selectionRow[centreRowIndex], imageRectX - 1, imageRectY - 1, imageRectWidth + 2, 1);
readBytes(selectionRow[belowRowIndex], imageRectX - 1, imageRectY, imageRectWidth + 2, 1);
for (TQ_INT32 y = 0; y < imageRectHeight; ++y) {
TQ_INT32 oldAboveRowIndex = aboveRowIndex;
aboveRowIndex = centreRowIndex;
centreRowIndex = belowRowIndex;
belowRowIndex = oldAboveRowIndex;
readBytes(selectionRow[belowRowIndex], imageRectX - 1, imageRectY + y + 1, imageRectWidth + 2, 1);
const TQ_UINT8 *aboveRow = selectionRow[aboveRowIndex] + 1;
const TQ_UINT8 *centreRow = selectionRow[centreRowIndex] + 1;
const TQ_UINT8 *belowRow = selectionRow[belowRowIndex] + 1;
TQRgb *imagePixel = reinterpret_cast<TQRgb *>(img.scanLine(imageRectOffsetY + y));
imagePixel += imageRectOffsetX;
for (TQ_INT32 x = 0; x < imageRectWidth; ++x) {
TQ_UINT8 centre = *centreRow;
if (centre != MAX_SELECTED) {
// this is where we come if the pixels should be blue or bluish
TQRgb srcPixel = *imagePixel;
TQ_UINT8 srcGrey = (tqRed(srcPixel) + tqGreen(srcPixel) + tqBlue(srcPixel)) / 9;
TQ_UINT8 srcAlpha = tqAlpha(srcPixel);
// Colour influence is proportional to alphaPixel.
srcGrey = UINT8_MULT(srcGrey, srcAlpha);
TQRgb dstPixel;
if (centre == MIN_SELECTED) {
//this is where we come if the pixels should be blue (or red outline)
TQ_UINT8 left = *(centreRow - 1);
TQ_UINT8 right = *(centreRow + 1);
TQ_UINT8 above = *aboveRow;
TQ_UINT8 below = *belowRow;
// Stop unselected transparent areas from appearing the same
// as selected transparent areas.
TQ_UINT8 dstAlpha = TQMAX(srcAlpha, 192);
// now for a simple outline based on 4-connectivity
if (left != MIN_SELECTED || right != MIN_SELECTED || above != MIN_SELECTED || below != MIN_SELECTED) {
dstPixel = tqRgba(255, 0, 0, dstAlpha);
} else {
dstPixel = tqRgba(128 + srcGrey, 128 + srcGrey, 165 + srcGrey, dstAlpha);
}
} else {
dstPixel = tqRgba(UINT8_BLEND(tqRed(srcPixel), srcGrey + 128, centre),
UINT8_BLEND(tqGreen(srcPixel), srcGrey + 128, centre),
UINT8_BLEND(tqBlue(srcPixel), srcGrey + 165, centre),
srcAlpha);
}
*imagePixel = dstPixel;
}
aboveRow++;
centreRow++;
belowRow++;
imagePixel++;
}
}
delete [] selectionRow[aboveRowIndex];
delete [] selectionRow[centreRowIndex];
delete [] selectionRow[belowRowIndex];
}
}
void KisSelection::paintSelection(TQImage img, const TQRect& scaledImageRect, const TQSize& scaledImageSize, const TQSize& imageSize)
{
if (img.isNull() || scaledImageRect.isEmpty() || scaledImageSize.isEmpty() || imageSize.isEmpty()) {
return;
}
Q_ASSERT(img.size() == scaledImageRect.size());
if (img.size() != scaledImageRect.size()) {
return;
}
TQ_INT32 imageWidth = imageSize.width();
TQ_INT32 imageHeight = imageSize.height();
TQRect selectionExtent = extent();
selectionExtent.setLeft(selectionExtent.left() - 1);
selectionExtent.setTop(selectionExtent.top() - 1);
selectionExtent.setWidth(selectionExtent.width() + 2);
selectionExtent.setHeight(selectionExtent.height() + 2);
double xScale = static_cast<double>(scaledImageSize.width()) / imageWidth;
double yScale = static_cast<double>(scaledImageSize.height()) / imageHeight;
TQRect scaledSelectionExtent;
scaledSelectionExtent.setLeft(static_cast<int>(selectionExtent.left() * xScale));
scaledSelectionExtent.setRight(static_cast<int>(ceil((selectionExtent.right() + 1) * xScale)) - 1);
scaledSelectionExtent.setTop(static_cast<int>(selectionExtent.top() * yScale));
scaledSelectionExtent.setBottom(static_cast<int>(ceil((selectionExtent.bottom() + 1) * yScale)) - 1);
TQRegion uniformRegion = TQRegion(scaledImageRect);
uniformRegion -= TQRegion(scaledSelectionExtent);
if (!uniformRegion.isEmpty()) {
paintUniformSelectionRegion(img, scaledImageRect, uniformRegion);
}
TQRect nonuniformRect = scaledImageRect & scaledSelectionExtent;
if (!nonuniformRect.isEmpty()) {
const TQ_INT32 scaledImageRectXOffset = nonuniformRect.x() - scaledImageRect.x();
const TQ_INT32 scaledImageRectYOffset = nonuniformRect.y() - scaledImageRect.y();
const TQ_INT32 scaledImageRectX = nonuniformRect.x();
const TQ_INT32 scaledImageRectY = nonuniformRect.y();
const TQ_INT32 scaledImageRectWidth = nonuniformRect.width();
const TQ_INT32 scaledImageRectHeight = nonuniformRect.height();
const TQ_INT32 imageRowLeft = static_cast<TQ_INT32>(scaledImageRectX / xScale);
const TQ_INT32 imageRowRight = static_cast<TQ_INT32>((ceil((scaledImageRectX + scaledImageRectWidth - 1 + 1) / xScale)) - 1);
const TQ_INT32 imageRowWidth = imageRowRight - imageRowLeft + 1;
const TQ_INT32 imageRowStride = imageRowWidth + 2;
const TQ_INT32 NUM_SELECTION_ROWS = 3;
TQ_INT32 aboveRowIndex = 0;
TQ_INT32 centreRowIndex = 1;
TQ_INT32 belowRowIndex = 2;
TQ_INT32 aboveRowSrcY = -3;
TQ_INT32 centreRowSrcY = -3;
TQ_INT32 belowRowSrcY = -3;
TQ_UINT8 *selectionRows = new TQ_UINT8[imageRowStride * NUM_SELECTION_ROWS];
TQ_UINT8 *selectionRow[NUM_SELECTION_ROWS];
selectionRow[0] = selectionRows + 1;
selectionRow[1] = selectionRow[0] + imageRowStride;
selectionRow[2] = selectionRow[0] + (2 * imageRowStride);
for (TQ_INT32 y = 0; y < scaledImageRectHeight; ++y) {
TQ_INT32 scaledY = scaledImageRectY + y;
TQ_INT32 srcY = (scaledY * imageHeight) / scaledImageSize.height();
TQ_UINT8 *aboveRow;
TQ_UINT8 *centreRow;
TQ_UINT8 *belowRow;
if (srcY - 1 == aboveRowSrcY) {
aboveRow = selectionRow[aboveRowIndex];
centreRow = selectionRow[centreRowIndex];
belowRow = selectionRow[belowRowIndex];
} else if (srcY - 1 == centreRowSrcY) {
TQ_INT32 oldAboveRowIndex = aboveRowIndex;
aboveRowIndex = centreRowIndex;
centreRowIndex = belowRowIndex;
belowRowIndex = oldAboveRowIndex;
aboveRow = selectionRow[aboveRowIndex];
centreRow = selectionRow[centreRowIndex];
belowRow = selectionRow[belowRowIndex];
readBytes(belowRow - 1, imageRowLeft - 1, srcY + 1, imageRowStride, 1);
} else if (srcY - 1 == belowRowSrcY) {
TQ_INT32 oldAboveRowIndex = aboveRowIndex;
TQ_INT32 oldCentreRowIndex = centreRowIndex;
aboveRowIndex = belowRowIndex;
centreRowIndex = oldAboveRowIndex;
belowRowIndex = oldCentreRowIndex;
aboveRow = selectionRow[aboveRowIndex];
centreRow = selectionRow[centreRowIndex];
belowRow = selectionRow[belowRowIndex];
if (belowRowIndex == centreRowIndex + 1) {
readBytes(centreRow - 1, imageRowLeft - 1, srcY, imageRowStride, 2);
} else {
readBytes(centreRow - 1, imageRowLeft - 1, srcY, imageRowStride, 1);
readBytes(belowRow - 1, imageRowLeft - 1, srcY + 1, imageRowStride, 1);
}
} else {
aboveRowIndex = 0;
centreRowIndex = 1;
belowRowIndex = 2;
aboveRow = selectionRow[aboveRowIndex];
centreRow = selectionRow[centreRowIndex];
belowRow = selectionRow[belowRowIndex];
readBytes(selectionRows, imageRowLeft - 1, srcY - 1, imageRowStride, NUM_SELECTION_ROWS);
}
aboveRowSrcY = srcY - 1;
centreRowSrcY = aboveRowSrcY + 1;
belowRowSrcY = centreRowSrcY + 1;
TQRgb *imagePixel = reinterpret_cast<TQRgb *>(img.scanLine(scaledImageRectYOffset + y));
imagePixel += scaledImageRectXOffset;
for (TQ_INT32 x = 0; x < scaledImageRectWidth; ++x) {
TQ_INT32 scaledX = scaledImageRectX + x;
TQ_INT32 srcX = (scaledX * imageWidth) / scaledImageSize.width();
TQ_UINT8 centre = *(centreRow + srcX - imageRowLeft);
if (centre != MAX_SELECTED) {
// this is where we come if the pixels should be blue or bluish
TQRgb srcPixel = *imagePixel;
TQ_UINT8 srcGrey = (tqRed(srcPixel) + tqGreen(srcPixel) + tqBlue(srcPixel)) / 9;
TQ_UINT8 srcAlpha = tqAlpha(srcPixel);
// Colour influence is proportional to alphaPixel.
srcGrey = UINT8_MULT(srcGrey, srcAlpha);
TQRgb dstPixel;
if (centre == MIN_SELECTED) {
//this is where we come if the pixels should be blue (or red outline)
TQ_UINT8 left = *(centreRow + (srcX - imageRowLeft) - 1);
TQ_UINT8 right = *(centreRow + (srcX - imageRowLeft) + 1);
TQ_UINT8 above = *(aboveRow + (srcX - imageRowLeft));
TQ_UINT8 below = *(belowRow + (srcX - imageRowLeft));
// Stop unselected transparent areas from appearing the same
// as selected transparent areas.
TQ_UINT8 dstAlpha = TQMAX(srcAlpha, 192);
// now for a simple outline based on 4-connectivity
if (left != MIN_SELECTED || right != MIN_SELECTED || above != MIN_SELECTED || below != MIN_SELECTED) {
dstPixel = tqRgba(255, 0, 0, dstAlpha);
} else {
dstPixel = tqRgba(128 + srcGrey, 128 + srcGrey, 165 + srcGrey, dstAlpha);
}
} else {
dstPixel = tqRgba(UINT8_BLEND(tqRed(srcPixel), srcGrey + 128, centre),
UINT8_BLEND(tqGreen(srcPixel), srcGrey + 128, centre),
UINT8_BLEND(tqBlue(srcPixel), srcGrey + 165, centre),
srcAlpha);
}
*imagePixel = dstPixel;
}
imagePixel++;
}
}
delete [] selectionRows;
}
}
void KisSelection::setDirty(const TQRect& rc)
{
if (m_dirty)
super::setDirty(rc);
}
void KisSelection::setDirty()
{
if (m_dirty)
super::setDirty();
}
|