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
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
|
/*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <tqobject.h>
#include <tqapplication.h>
#include <clipboard.h>
#include <tqcolor.h>
#include <tqcursor.h>
#include <kdebug.h>
#include <kaction.h>
#include <klocale.h>
#include <kstdaction.h>
#include <KoDocument.h>
#include <KoMainWindow.h>
#include <KoQueryTrader.h>
#include "kis_cursor.h"
#include "kis_part_layer.h"
#include "kis_adjustment_layer.h"
#include "kis_clipboard.h"
#include "kis_types.h"
#include "kis_view.h"
#include "kis_doc.h"
#include "kis_image.h"
#include "kis_selection.h"
#include "kis_selection_manager.h"
#include "kis_painter.h"
#include "kis_iterators_pixel.h"
#include "kis_iteratorpixeltrait.h"
#include "kis_layer.h"
#include "kis_group_layer.h"
#include "kis_paint_layer.h"
#include "kis_paint_device.h"
#include "kis_channelinfo.h"
#include "kis_dlg_apply_profile.h"
#include "kis_config.h"
#include "kis_debug_areas.h"
#include "kis_transaction.h"
#include "kis_undo_adapter.h"
#include "kis_selected_transaction.h"
#include "kis_convolution_painter.h"
#include "kis_integer_maths.h"
#include "kis_fill_painter.h"
#include "kis_canvas.h"
KisSelectionManager::KisSelectionManager(KisView * parent, KisDoc * doc)
: m_parent(parent),
m_doc(doc),
m_copy(0),
m_cut(0),
m_paste(0),
m_pasteNew(0),
m_cutToNewLayer(0),
m_selectAll(0),
m_deselect(0),
m_clear(0),
m_reselect(0),
m_invert(0),
m_toNewLayer(0),
m_feather(0),
m_border(0),
m_expand(0),
m_smooth(0),
m_contract(0),
m_similar(0),
m_transform(0),
m_load(0),
m_save(0),
m_fillForegroundColor(0),
m_fillBackgroundColor(0),
m_fillPattern(0)
{
m_pluginActions.setAutoDelete(true);
m_clipboard = KisClipboard::instance();
}
KisSelectionManager::~KisSelectionManager()
{
m_pluginActions.clear();
}
void KisSelectionManager::setup(KActionCollection * collection)
{
// XXX: setup shortcuts!
m_cut = KStdAction::cut(this,
TQT_SLOT(cut()),
collection,
"cut");
m_copy = KStdAction::copy(this,
TQT_SLOT(copy()),
collection,
"copy");
m_paste = KStdAction::paste(this,
TQT_SLOT(paste()),
collection,
"paste");
m_pasteNew = new KAction(i18n("Paste into &New Image"),
0, 0,
this, TQT_SLOT(pasteNew()),
collection,
"paste_new");
m_selectAll = KStdAction::selectAll(this,
TQT_SLOT(selectAll()),
collection,
"select_all");
m_deselect = KStdAction::deselect(this,
TQT_SLOT(deselect()),
collection,
"deselect");
m_clear = KStdAction::clear(this,
TQT_SLOT(clear()),
collection,
"clear");
m_reselect = new KAction(i18n("&Reselect"),
0, "Ctrl+Shift+D",
this, TQT_SLOT(reselect()),
collection, "reselect");
m_invert = new KAction(i18n("&Invert"),
0, "Ctrl+I",
this, TQT_SLOT(invert()),
collection, "invert");
m_toNewLayer = new KAction(i18n("Copy Selection to New Layer"),
0, "Ctrl+J",
this, TQT_SLOT(copySelectionToNewLayer()),
collection, "copy_selection_to_new_layer");
m_cutToNewLayer = new KAction(i18n("Cut Selection to New Layer"),
0, "Ctrl+Shift+J",
this, TQT_SLOT(cutToNewLayer()),
collection, "cut_selection_to_new_layer");
m_feather = new KAction(i18n("Feather"),
0, "Ctrl+Alt+D",
this, TQT_SLOT(feather()),
collection, "feather");
m_fillForegroundColor = new KAction(i18n("Fill with Foreground Color"),
"Alt+backspace", this,
TQT_SLOT(fillForegroundColor()),
collection,
"fill_selection_foreground_color");
m_fillBackgroundColor = new KAction(i18n("Fill with Background Color"),
"backspace", this,
TQT_SLOT(fillBackgroundColor()),
collection,
"fill_selection_background_color");
m_fillPattern = new KAction(i18n("Fill with Pattern"),
0, this,
TQT_SLOT(fillPattern()),
collection,
"fill_selection_pattern");
m_toggleDisplaySelection = new KToggleAction(i18n("Display Selection"), "Ctrl+h", this, TQT_SLOT(toggleDisplaySelection()), collection, "toggle_display_selection");
m_toggleDisplaySelection->setCheckedState(KGuiItem(i18n("Hide Selection")));
m_toggleDisplaySelection->setChecked(true);
m_border =
new KAction(i18n("Border..."),
0, 0,
this, TQT_SLOT(border()),
collection, "border");
m_expand =
new KAction(i18n("Expand..."),
0, 0,
this, TQT_SLOT(expand()),
collection, "expand");
m_smooth =
new KAction(i18n("Smooth..."),
0, 0,
this, TQT_SLOT(smooth()),
collection, "smooth");
m_contract =
new KAction(i18n("Contract..."),
0, 0,
this, TQT_SLOT(contract()),
collection, "contract");
m_similar =
new KAction(i18n("Similar"),
0, 0,
this, TQT_SLOT(similar()),
collection, "similar");
m_transform
= new KAction(i18n("Transform..."),
0, 0,
this, TQT_SLOT(transform()),
collection, "transform_selection");
// m_load
// = new KAction(i18n("Load..."),
// 0, 0,
// this, TQT_SLOT(load()),
// collection, "load_selection");
//
//
// m_save
// = new KAction(i18n("Save As..."),
// 0, 0,
// this, TQT_SLOT(save()),
// collection, "save_selection");
TQClipboard *cb = TQApplication::clipboard();
connect(cb, TQT_SIGNAL(dataChanged()), TQT_SLOT(clipboardDataChanged()));
}
void KisSelectionManager::clipboardDataChanged()
{
updateGUI();
}
void KisSelectionManager::addSelectionAction(KAction * action)
{
m_pluginActions.append(action);
}
void KisSelectionManager::updateGUI()
{
Q_ASSERT(m_parent);
Q_ASSERT(m_clipboard);
if (m_parent == 0) {
// "Eek, no parent!
return;
}
if (m_clipboard == 0) {
// Eek, no clipboard!
return;
}
KisImageSP img = m_parent->currentImg();
KisLayerSP l = 0;
KisPaintDeviceSP dev = 0;
bool enable = false;
if (img && img->activeDevice() && img->activeLayer()) {
l = img->activeLayer();
dev = img->activeDevice();
KisPartLayer * partLayer = dynamic_cast<KisPartLayer*>(l.data());
KisAdjustmentLayer * adjLayer = dynamic_cast<KisAdjustmentLayer*>(l.data());
enable = l && dev&& dev->hasSelection() && !l->locked() && l->visible() && (partLayer==0);
if(dev && !adjLayer)
m_reselect->setEnabled( dev->selectionDeselected() );
if (adjLayer) // There's no reselect for adjustment layers
m_reselect->setEnabled(false);
}
m_cut->setEnabled(enable);
m_cutToNewLayer->setEnabled(enable);
m_selectAll->setEnabled(img != 0);
m_deselect->setEnabled(enable);
m_clear->setEnabled(enable);
m_fillForegroundColor->setEnabled(enable);
m_fillBackgroundColor->setEnabled(enable);
m_fillPattern->setEnabled(enable);
m_invert->setEnabled(enable);
m_feather->setEnabled(enable);
m_border->setEnabled(enable);
m_expand->setEnabled(enable);
m_smooth->setEnabled(enable);
m_contract->setEnabled(enable);
m_similar->setEnabled(enable);
m_transform->setEnabled(enable);
// m_load->setEnabled(enable);
// m_save->setEnabled(enable);
KAction * a;
for (a = m_pluginActions.first(); a; a = m_pluginActions.next()) {
a->setEnabled(img != 0);
}
// You can copy from locked layers and paste the clip into a new layer, even when
// the current layer is locked.
enable = false;
if (img && l && dev) {
enable = dev->hasSelection() && l->visible();
}
m_copy->setEnabled(enable);
m_paste->setEnabled(img != 0 && m_clipboard->hasClip());
m_pasteNew->setEnabled(img != 0 && m_clipboard->hasClip());
m_toNewLayer->setEnabled(enable);
m_parent->updateStatusBarSelectionLabel();
}
void KisSelectionManager::imgSelectionChanged(KisImageSP img)
{
if (img == m_parent->currentImg()) {
updateGUI();
}
}
void KisSelectionManager::cut()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
if (!dev->hasSelection()) return;
copy();
KisSelectedTransaction *t = 0;
if (img->undo()) {
t = new KisSelectedTransaction(i18n("Cut"), dev);
Q_CHECK_PTR(t);
}
dev->clearSelection();
dev->deselect();
dev->emitSelectionChanged();
if (img->undo()) {
img->undoAdapter()->addCommand(t);
}
}
void KisSelectionManager::copy()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
if (!dev->hasSelection()) return;
KisSelectionSP selection = dev->selection();
TQRect r = selection->selectedExactRect();
KisPaintDeviceSP clip = new KisPaintDevice(dev->colorSpace(), "clip");
Q_CHECK_PTR(clip);
KisColorSpace * cs = clip->colorSpace();
// TODO if the source is linked... copy from all linked layers?!?
// Copy image data
KisPainter gc;
gc.begin(clip);
gc.bitBlt(0, 0, COMPOSITE_COPY, dev, r.x(), r.y(), r.width(), r.height());
gc.end();
// Apply selection mask.
for (TQ_INT32 y = 0; y < r.height(); y++) {
KisHLineIteratorPixel layerIt = clip->createHLineIterator(0, y, r.width(), true);
KisHLineIteratorPixel selectionIt = selection->createHLineIterator(r.x(), r.y() + y, r.width(), false);
while (!layerIt.isDone()) {
cs->applyAlphaU8Mask( layerIt.rawData(), selectionIt.rawData(), 1 );
++layerIt;
++selectionIt;
}
}
m_clipboard->setClip(clip);
imgSelectionChanged(m_parent->currentImg());
}
KisLayerSP KisSelectionManager::paste()
{
KisImageSP img = m_parent->currentImg();
if (!img) return 0;
KisPaintDeviceSP clip = m_clipboard->clip();
if (clip) {
TQApplication::setOverrideCursor(KisCursor::waitCursor());
KisPaintLayer *layer = new KisPaintLayer(img, img->nextLayerName() + i18n("(pasted)"), OPACITY_OPAQUE);
Q_CHECK_PTR(layer);
TQRect r = clip->exactBounds();
KisPainter gc;
gc.begin(layer->paintDevice());
gc.bitBlt(0, 0, COMPOSITE_COPY, clip, r.x(), r.y(), r.width(), r.height());
gc.end();
//figure out where to position the clip
KisCanvasController *cc = m_parent->getCanvasController();
TQPoint center = cc->viewToWindow(TQPoint(cc->kiscanvas()->width()/2, cc->kiscanvas()->height()/2));
TQPoint bottomright = cc->viewToWindow(TQPoint(cc->kiscanvas()->width(), cc->kiscanvas()->height()));
if(bottomright.x() > img->width())
center.setX(img->width()/2);
if(bottomright.y() > img->height())
center.setY(img->height()/2);
center -= TQPoint(r.width()/2, r.height()/2);
layer->setX(center.x());
layer->setY(center.y());
/*XXX CBR have an idea of asking the user if he is about to paste a clip ion another cs than that of
the image if that is what he want rather than silently converting
if (clip->colorSpace != img ->colorSpace())
if (dlg->exec() == TQDialog::Accepted)
layer->convertTo(img->colorSpace());
*/
TQApplication::restoreOverrideCursor();
if(img->addLayer(layer, img->activeLayer()->parent(), img->activeLayer()))
{
return layer;
} else {
return 0;
}
}
return 0;
}
void KisSelectionManager::pasteNew()
{
KisPaintDeviceSP clip = m_clipboard->clip();
if (!clip) return;
TQRect r = clip->exactBounds();
if (r.width() < 1 && r.height() < 1) {
// Don't paste empty clips
return;
}
const TQCString mimetype = KoDocument::readNativeFormatMimeType();
KoDocumentEntry entry = KoDocumentEntry::queryByMimeType( mimetype );
KisDoc * doc = (KisDoc*) entry.createDoc();
Q_ASSERT(doc->undoAdapter() != 0);
doc->undoAdapter()->setUndo(false);
KisImageSP img = new KisImage(doc->undoAdapter(), r.width(), r.height(), clip->colorSpace(), "Pasted");
KisPaintLayer *layer = new KisPaintLayer(img, clip->name(), OPACITY_OPAQUE, clip->colorSpace());
KisPainter p(layer->paintDevice());
p.bitBlt(0, 0, COMPOSITE_COPY, clip, OPACITY_OPAQUE, r.x(), r.y(), r.width(), r.height());
p.end();
img->addLayer(layer, img->rootLayer(), 0);
doc->setCurrentImage(img);
doc->undoAdapter()->setUndo(true);
KoMainWindow *win = new KoMainWindow( doc->instance() );
win->show();
win->setRootDocument( doc );
}
void KisSelectionManager::selectAll()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
KisSelectedTransaction * t = 0;
if (img->undo()) t = new KisSelectedTransaction(i18n("Select All"), dev);
Q_CHECK_PTR(t);
// Make adjustment layers behave better
KisAdjustmentLayer* adj = dynamic_cast<KisAdjustmentLayer*>(img->activeLayer().data());
if (adj) {
adj->clearSelection();
adj->selection()->invert();
} else {
dev->selection()->clear();
dev->selection()->invert();
}
dev->setDirty();
dev->emitSelectionChanged();
if (img->undo())
img->undoAdapter()->addCommand(t);
}
void KisSelectionManager::deselect()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
KisSelectedTransaction * t = 0;
if (img->undo()) t = new KisSelectedTransaction(i18n("Deselect"), dev);
Q_CHECK_PTR(t);
// Make adjustment layers behave almost the same (except no reselect)
KisAdjustmentLayer* adj = dynamic_cast<KisAdjustmentLayer*>(img->activeLayer().data());
if (adj) {
adj->clearSelection();
} else {
dev->deselect();
}
dev->setDirty();
dev->emitSelectionChanged();
if (img->undo())
img->undoAdapter()->addCommand(t);
}
void KisSelectionManager::clear()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
if (!dev->hasSelection()) return;
KisTransaction * t = 0;
if (img->undo()) {
t = new KisTransaction(i18n("Clear"), dev);
}
dev->clearSelection();
dev->setDirty();
dev->emitSelectionChanged();
if (img->undo()) img->undoAdapter()->addCommand(t);
}
void KisSelectionManager::fill(const KisColor& color, bool fillWithPattern, const TQString& transactionText)
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
if (!dev->hasSelection()) return;
KisSelectionSP selection = dev->selection();
KisPaintDeviceSP filled = new KisPaintDevice(dev->colorSpace());
KisFillPainter painter(filled);
if (fillWithPattern) {
painter.fillRect(0, 0, img->width(), img->height(),
m_parent->currentPattern());
} else {
painter.fillRect(0, 0, img->width(), img->height(), color);
}
painter.end();
KisPainter painter2(dev);
if (img->undo()) painter2.beginTransaction(transactionText);
painter2.bltSelection(0, 0, COMPOSITE_OVER, filled, OPACITY_OPAQUE,
0, 0, img->width(), img->height());
dev->setDirty();
dev->emitSelectionChanged();
if (img->undo()) {
img->undoAdapter()->addCommand(painter2.endTransaction());
}
}
void KisSelectionManager::fillForegroundColor()
{
fill(m_parent->fgColor(), false, i18n("Fill with Foreground Color"));
}
void KisSelectionManager::fillBackgroundColor()
{
fill(m_parent->bgColor(), false, i18n("Fill with Background Color"));
}
void KisSelectionManager::fillPattern()
{
fill(KisColor(), true, i18n("Fill with Pattern"));
}
void KisSelectionManager::reselect()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img ->activeDevice();
if (!dev) return;
KisSelectedTransaction * t = 0;
if (img->undo()) t = new KisSelectedTransaction(i18n("Reselect"), dev);
Q_CHECK_PTR(t);
dev->reselect(); // sets hasSelection=true
dev->setDirty();
dev->emitSelectionChanged();
if (img->undo())
img->undoAdapter()->addCommand(t);
}
void KisSelectionManager::invert()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
if (dev->hasSelection()) {
KisSelectionSP s = dev->selection();
KisSelectedTransaction * t = 0;
if (img->undo())
{
t = new KisSelectedTransaction(i18n("Invert"), dev);
Q_CHECK_PTR(t);
}
s->invert();
dev->setDirty();
dev->emitSelectionChanged();
if (t) {
img->undoAdapter()->addCommand(t);
}
}
}
void KisSelectionManager::copySelectionToNewLayer()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
copy();
paste();
}
void KisSelectionManager::cutToNewLayer()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
cut();
paste();
}
void KisSelectionManager::feather()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
if (!dev->hasSelection()) {
// activate it, but don't do anything with it
dev->selection();
return;
}
KisSelectionSP selection = dev->selection();
KisSelectedTransaction * t = 0;
if (img->undo()) t = new KisSelectedTransaction(i18n("Feather..."), dev);
Q_CHECK_PTR(t);
// XXX: we should let gaussian blur & others influence alpha channels as well
// (on demand of the caller)
KisConvolutionPainter painter(selection.data());
KisKernelSP k = new KisKernel();
k->width = 3;
k->height = 3;
k->factor = 16;
k->offset = 0;
k->data = new TQ_INT32[9];
k->data[0] = 1;
k->data[1] = 2;
k->data[2] = 1;
k->data[3] = 2;
k->data[4] = 4;
k->data[5] = 2;
k->data[6] = 1;
k->data[7] = 2;
k->data[8] = 1;
TQRect rect = selection->selectedRect();
// Make sure we've got enough space around the edges.
rect = TQRect(rect.x() - 3, rect.y() - 3, rect.width() + 6, rect.height() + 6);
rect &= TQRect(0, 0, img->width(), img->height());
painter.applyMatrix(k, rect.x(), rect.y(), rect.width(), rect.height(), BORDER_AVOID, KisChannelInfo::FLAG_ALPHA);
painter.end();
dev->setDirty(rect);
dev->emitSelectionChanged();
if (img->undo())
img->undoAdapter()->addCommand(t);
}
void KisSelectionManager::toggleDisplaySelection()
{
m_parent->selectionDisplayToggled(displaySelection());
}
bool KisSelectionManager::displaySelection()
{
return m_toggleDisplaySelection->isChecked();
}
// XXX: Maybe move these esoteric functions to plugins?
void KisSelectionManager::border() {}
void KisSelectionManager::expand() {}
void KisSelectionManager::contract() {}
void KisSelectionManager::similar() {}
void KisSelectionManager::transform() {}
void KisSelectionManager::load() {}
void KisSelectionManager::save() {}
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
void KisSelectionManager::grow (TQ_INT32 xradius, TQ_INT32 yradius)
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
if (!dev->hasSelection()) return;
KisSelectionSP selection = dev->selection();
//determine the layerSize
TQRect layerSize = dev->exactBounds();
/*
Any bugs in this fuction are probably also in thin_region
Blame all bugs in this function on jaycox@gimp.org
*/
TQ_UINT8 **buf; // caches the region's pixel data
TQ_UINT8 **max; // caches the largest values for each column
if (xradius <= 0 || yradius <= 0)
return;
KisSelectedTransaction *t = 0;
if (img->undo()) {
t = new KisSelectedTransaction(i18n("Grow"), dev);
Q_CHECK_PTR(t);
}
max = new TQ_UINT8* [layerSize.width() + 2 * xradius];
buf = new TQ_UINT8* [yradius + 1];
for (TQ_INT32 i = 0; i < yradius + 1; i++)
{
buf[i] = new TQ_UINT8[layerSize.width()];
}
TQ_UINT8* buffer = new TQ_UINT8[ ( layerSize.width() + 2 * xradius ) * ( yradius + 1 ) ];
for (TQ_INT32 i = 0; i < layerSize.width() + 2 * xradius; i++)
{
if (i < xradius)
max[i] = buffer;
else if (i < layerSize.width() + xradius)
max[i] = &buffer[(yradius + 1) * (i - xradius)];
else
max[i] = &buffer[(yradius + 1) * (layerSize.width() + xradius - 1)];
for (TQ_INT32 j = 0; j < xradius + 1; j++)
max[i][j] = 0;
}
/* offset the max pointer by xradius so the range of the array
is [-xradius] to [region->w + xradius] */
max += xradius;
TQ_UINT8* out = new TQ_UINT8[ layerSize.width() ]; // holds the new scan line we are computing
TQ_INT32* circ = new TQ_INT32[ 2 * xradius + 1 ]; // holds the y coords of the filter's mask
computeBorder (circ, xradius, yradius);
/* offset the circ pointer by xradius so the range of the array
is [-xradius] to [xradius] */
circ += xradius;
memset (buf[0], 0, layerSize.width());
for (TQ_INT32 i = 0; i < yradius && i < layerSize.height(); i++) // load top of image
{
selection->readBytes(buf[i + 1], layerSize.x(), layerSize.y() + i, layerSize.width(), 1);
}
for (TQ_INT32 x = 0; x < layerSize.width() ; x++) // set up max for top of image
{
max[x][0] = 0; // buf[0][x] is always 0
max[x][1] = buf[1][x]; // MAX (buf[1][x], max[x][0]) always = buf[1][x]
for (TQ_INT32 j = 2; j < yradius + 1; j++)
{
max[x][j] = MAX(buf[j][x], max[x][j-1]);
}
}
for (TQ_INT32 y = 0; y < layerSize.height(); y++)
{
rotatePointers (buf, yradius + 1);
if (y < layerSize.height() - (yradius))
selection->readBytes(buf[yradius], layerSize.x(), layerSize.y() + y + yradius, layerSize.width(), 1);
else
memset (buf[yradius], 0, layerSize.width());
for (TQ_INT32 x = 0; x < layerSize.width(); x++) /* update max array */
{
for (TQ_INT32 i = yradius; i > 0; i--)
{
max[x][i] = MAX (MAX (max[x][i - 1], buf[i - 1][x]), buf[i][x]);
}
max[x][0] = buf[0][x];
}
TQ_INT32 last_max = max[0][circ[-1]];
TQ_INT32 last_index = 1;
for (TQ_INT32 x = 0; x < layerSize.width(); x++) /* render scan line */
{
last_index--;
if (last_index >= 0)
{
if (last_max == 255)
out[x] = 255;
else
{
last_max = 0;
for (TQ_INT32 i = xradius; i >= 0; i--)
if (last_max < max[x + i][circ[i]])
{
last_max = max[x + i][circ[i]];
last_index = i;
}
out[x] = last_max;
}
}
else
{
last_index = xradius;
last_max = max[x + xradius][circ[xradius]];
for (TQ_INT32 i = xradius - 1; i >= -xradius; i--)
if (last_max < max[x + i][circ[i]])
{
last_max = max[x + i][circ[i]];
last_index = i;
}
out[x] = last_max;
}
}
selection->writeBytes(out, layerSize.x(), layerSize.y() + y, layerSize.width(), 1);
}
/* undo the offsets to the pointers so we can free the malloced memmory */
circ -= xradius;
max -= xradius;
//XXXX: replace delete by delete[] where it is necessary to avoid memory leaks!
delete[] circ;
delete[] buffer;
delete[] max;
for (TQ_INT32 i = 0; i < yradius + 1; i++)
delete[] buf[i];
delete[] buf;
delete[] out;
dev->setDirty();
dev->emitSelectionChanged();
if (t) {
img->undoAdapter()->addCommand(t);
}
}
void KisSelectionManager::shrink (TQ_INT32 xradius, TQ_INT32 yradius, bool edge_lock)
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
if (!dev->hasSelection()) return;
KisSelectionSP selection = dev->selection();
//determine the layerSize
TQRect layerSize = dev->exactBounds();
/*
pretty much the same as fatten_region only different
blame all bugs in this function on jaycox@gimp.org
*/
/* If edge_lock is true we assume that pixels outside the region
we are passed are identical to the edge pixels.
If edge_lock is false, we assume that pixels outside the region are 0
*/
TQ_UINT8 **buf; // caches the the region's pixels
TQ_UINT8 **max; // caches the smallest values for each column
TQ_INT32 last_max, last_index;
if (xradius <= 0 || yradius <= 0)
return;
max = new TQ_UINT8* [layerSize.width() + 2 * xradius];
buf = new TQ_UINT8* [yradius + 1];
for (TQ_INT32 i = 0; i < yradius + 1; i++)
{
buf[i] = new TQ_UINT8[layerSize.width()];
}
TQ_INT32 buffer_size = (layerSize.width() + 2 * xradius + 1) * (yradius + 1);
TQ_UINT8* buffer = new TQ_UINT8[buffer_size];
if (edge_lock)
memset(buffer, 255, buffer_size);
else
memset(buffer, 0, buffer_size);
for (TQ_INT32 i = 0; i < layerSize.width() + 2 * xradius; i++)
{
if (i < xradius)
if (edge_lock)
max[i] = buffer;
else
max[i] = &buffer[(yradius + 1) * (layerSize.width() + xradius)];
else if (i < layerSize.width() + xradius)
max[i] = &buffer[(yradius + 1) * (i - xradius)];
else
if (edge_lock)
max[i] = &buffer[(yradius + 1) * (layerSize.width() + xradius - 1)];
else
max[i] = &buffer[(yradius + 1) * (layerSize.width() + xradius)];
}
if (!edge_lock)
for (TQ_INT32 j = 0 ; j < xradius + 1; j++) max[0][j] = 0;
// offset the max pointer by xradius so the range of the array is [-xradius] to [region->w + xradius]
max += xradius;
TQ_UINT8* out = new TQ_UINT8[layerSize.width()]; // holds the new scan line we are computing
TQ_INT32* circ = new TQ_INT32[2 * xradius + 1]; // holds the y coords of the filter's mask
computeBorder (circ, xradius, yradius);
// offset the circ pointer by xradius so the range of the array is [-xradius] to [xradius]
circ += xradius;
for (TQ_INT32 i = 0; i < yradius && i < layerSize.height(); i++) // load top of image
selection->readBytes(buf[i + 1], layerSize.x(), layerSize.y() + i, layerSize.width(), 1);
if (edge_lock)
memcpy (buf[0], buf[1], layerSize.width());
else
memset (buf[0], 0, layerSize.width());
for (TQ_INT32 x = 0; x < layerSize.width(); x++) // set up max for top of image
{
max[x][0] = buf[0][x];
for (TQ_INT32 j = 1; j < yradius + 1; j++)
max[x][j] = MIN(buf[j][x], max[x][j-1]);
}
for (TQ_INT32 y = 0; y < layerSize.height(); y++)
{
rotatePointers (buf, yradius + 1);
if (y < layerSize.height() - yradius)
selection->readBytes(buf[yradius], layerSize.x(), layerSize.y() + y + yradius, layerSize.width(), 1);
else if (edge_lock)
memcpy (buf[yradius], buf[yradius - 1], layerSize.width());
else
memset (buf[yradius], 0, layerSize.width());
for (TQ_INT32 x = 0 ; x < layerSize.width(); x++) // update max array
{
for (TQ_INT32 i = yradius; i > 0; i--)
{
max[x][i] = MIN (MIN (max[x][i - 1], buf[i - 1][x]), buf[i][x]);
}
max[x][0] = buf[0][x];
}
last_max = max[0][circ[-1]];
last_index = 0;
for (TQ_INT32 x = 0 ; x < layerSize.width(); x++) // render scan line
{
last_index--;
if (last_index >= 0)
{
if (last_max == 0)
out[x] = 0;
else
{
last_max = 255;
for (TQ_INT32 i = xradius; i >= 0; i--)
if (last_max > max[x + i][circ[i]])
{
last_max = max[x + i][circ[i]];
last_index = i;
}
out[x] = last_max;
}
}
else
{
last_index = xradius;
last_max = max[x + xradius][circ[xradius]];
for (TQ_INT32 i = xradius - 1; i >= -xradius; i--)
if (last_max > max[x + i][circ[i]])
{
last_max = max[x + i][circ[i]];
last_index = i;
}
out[x] = last_max;
}
}
selection->writeBytes(out, layerSize.x(), layerSize.y() + y, layerSize.width(), 1);
}
// undo the offsets to the pointers so we can free the malloced memmory
circ -= xradius;
max -= xradius;
//free the memmory
//XXXX: replace delete by delete[] where it is necessary to avoid memory leaks!
delete[] circ;
delete[] buffer;
delete[] max;
for (TQ_INT32 i = 0; i < yradius + 1; i++)
delete buf[i];
delete[] buf;
delete[] out;
dev->setDirty(layerSize);
dev->emitSelectionChanged();
}
//Simple convolution filter to smooth a mask (1bpp)
void KisSelectionManager::smooth()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
if (!dev->hasSelection()) return;
KisSelectionSP selection = dev->selection();
//determine the layerSize
TQRect layerSize = dev->exactBounds();
TQ_UINT8 *buf[3];
TQ_INT32 width = layerSize.width();
for (TQ_INT32 i = 0; i < 3; i++) buf[i] = new TQ_UINT8[width + 2];
TQ_UINT8* out = new TQ_UINT8[width];
// load top of image
selection->readBytes(buf[0] + 1, layerSize.x(), layerSize.y(), width, 1);
buf[0][0] = buf[0][1];
buf[0][width + 1] = buf[0][width];
memcpy (buf[1], buf[0], width + 2);
for (TQ_INT32 y = 0; y < layerSize.height(); y++)
{
if (y + 1 < layerSize.height())
{
selection->readBytes(buf[2] + 1, layerSize.x(), layerSize.y() + y + 1, width, 1);
buf[2][0] = buf[2][1];
buf[2][width + 1] = buf[2][width];
}
else
{
memcpy (buf[2], buf[1], width + 2);
}
for (TQ_INT32 x = 0 ; x < width; x++)
{
TQ_INT32 value = (buf[0][x] + buf[0][x+1] + buf[0][x+2] +
buf[1][x] + buf[2][x+1] + buf[1][x+2] +
buf[2][x] + buf[1][x+1] + buf[2][x+2]);
out[x] = value / 9;
}
selection->writeBytes(out, layerSize.x(), layerSize.y() + y, width, 1);
rotatePointers (buf, 3);
}
for (TQ_INT32 i = 0; i < 3; i++)
delete[] buf[i];
delete[] out;
dev->setDirty();
dev->emitSelectionChanged();
}
// Erode (radius 1 pixel) a mask (1bpp)
void KisSelectionManager::erode()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
if (!dev->hasSelection()) return;
KisSelectionSP selection = dev->selection();
//determine the layerSize
TQRect layerSize = dev->exactBounds();
TQ_UINT8* buf[3];
TQ_INT32 width = layerSize.width();
for (TQ_INT32 i = 0; i < 3; i++)
buf[i] = new TQ_UINT8[width + 2];
TQ_UINT8* out = new TQ_UINT8[width];
// load top of image
selection->readBytes(buf[0] + 1, layerSize.x(), layerSize.y(), width, 1);
buf[0][0] = buf[0][1];
buf[0][width + 1] = buf[0][width];
memcpy (buf[1], buf[0], width + 2);
for (TQ_INT32 y = 0; y < layerSize.height(); y++)
{
if (y + 1 < layerSize.height())
{
selection->readBytes(buf[2] + 1, layerSize.x(), layerSize.y() + y + 1, width, 1);
buf[2][0] = buf[2][1];
buf[2][width + 1] = buf[2][width];
}
else
{
memcpy (buf[2], buf[1], width + 2);
}
for (TQ_INT32 x = 0 ; x < width; x++)
{
TQ_INT32 min = 255;
if (buf[0][x+1] < min) min = buf[0][x+1];
if (buf[1][x] < min) min = buf[1][x];
if (buf[1][x+1] < min) min = buf[1][x+1];
if (buf[1][x+2] < min) min = buf[1][x+2];
if (buf[2][x+1] < min) min = buf[2][x+1];
out[x] = min;
}
selection->writeBytes(out, layerSize.x(), layerSize.y() + y, width, 1);
rotatePointers (buf, 3);
}
for (TQ_INT32 i = 0; i < 3; i++)
delete[] buf[i];
delete[] out;
dev->setDirty();
dev->emitSelectionChanged();
}
// dilate (radius 1 pixel) a mask (1bpp)
void KisSelectionManager::dilate()
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
if (!dev->hasSelection()) return;
KisSelectionSP selection = dev->selection();
//determine the layerSize
TQRect layerSize = dev->exactBounds();
TQ_UINT8* buf[3];
TQ_INT32 width = layerSize.width();
for (TQ_INT32 i = 0; i < 3; i++)
buf[i] = new TQ_UINT8[width + 2];
TQ_UINT8* out = new TQ_UINT8[width];
// load top of image
selection->readBytes(buf[0] + 1, layerSize.x(), layerSize.y(), width, 1);
buf[0][0] = buf[0][1];
buf[0][width + 1] = buf[0][width];
memcpy (buf[1], buf[0], width + 2);
for (TQ_INT32 y = 0; y < layerSize.height(); y++)
{
if (y + 1 < layerSize.height())
{
selection->readBytes(buf[2] + 1, layerSize.x(), layerSize.y() + y + 1, width, 1);
buf[2][0] = buf[2][1];
buf[2][width + 1] = buf[2][width];
}
else
{
memcpy (buf[2], buf[1], width + 2);
}
for (TQ_INT32 x = 0 ; x < width; x++)
{
TQ_INT32 max = 0;
if (buf[0][x+1] > max) max = buf[0][x+1];
if (buf[1][x] > max) max = buf[1][x];
if (buf[1][x+1] > max) max = buf[1][x+1];
if (buf[1][x+2] > max) max = buf[1][x+2];
if (buf[2][x+1] > max) max = buf[2][x+1];
out[x] = max;
}
selection->writeBytes(out, layerSize.x(), layerSize.y() + y, width, 1);
rotatePointers (buf, 3);
}
for (TQ_INT32 i = 0; i < 3; i++)
delete[] buf[i];
delete[] out;
dev->setDirty();
dev->emitSelectionChanged();
}
void KisSelectionManager::border(TQ_INT32 xradius, TQ_INT32 yradius)
{
KisImageSP img = m_parent->currentImg();
if (!img) return;
KisPaintDeviceSP dev = img->activeDevice();
if (!dev) return;
if (!dev->hasSelection()) return;
KisSelectionSP selection = dev->selection();
//determine the layerSize
TQRect layerSize = dev->exactBounds();
/*
This function has no bugs, but if you imagine some you can
blame them on jaycox@gimp.org
*/
TQ_UINT8 *buf[3];
TQ_UINT8 **density;
TQ_UINT8 **transition;
if (xradius == 1 && yradius == 1) // optimize this case specifically
{
TQ_UINT8* source[3];
for (TQ_INT32 i = 0; i < 3; i++)
source[i] = new TQ_UINT8[layerSize.width()];
TQ_UINT8* transition = new TQ_UINT8[layerSize.width()];
selection->readBytes(source[0], layerSize.x(), layerSize.y(), layerSize.width(), 1);
memcpy (source[1], source[0], layerSize.width());
if (layerSize.height() > 1)
selection->readBytes(source[2], layerSize.x(), layerSize.y() + 1, layerSize.width(), 1);
else
memcpy (source[2], source[1], layerSize.width());
computeTransition (transition, source, layerSize.width());
selection->writeBytes(transition, layerSize.x(), layerSize.y(), layerSize.width(), 1);
for (TQ_INT32 y = 1; y < layerSize.height(); y++)
{
rotatePointers (source, 3);
if (y + 1 < layerSize.height())
selection->readBytes(source[2], layerSize.x(), layerSize.y() + y + 1, layerSize.width(), 1);
else
memcpy(source[2], source[1], layerSize.width());
computeTransition (transition, source, layerSize.width());
selection->writeBytes(transition, layerSize.x(), layerSize.y() + y, layerSize.width(), 1);
}
for (TQ_INT32 i = 0; i < 3; i++)
delete[] source[i];
delete[] transition;
return;
}
TQ_INT32* max = new TQ_INT32[layerSize.width() + 2 * xradius];
for (TQ_INT32 i = 0; i < (layerSize.width() + 2 * xradius); i++)
max[i] = yradius + 2;
max += xradius;
for (TQ_INT32 i = 0; i < 3; i++)
buf[i] = new TQ_UINT8[layerSize.width()];
transition = new TQ_UINT8*[yradius + 1];
for (TQ_INT32 i = 0; i < yradius + 1; i++)
{
transition[i] = new TQ_UINT8[layerSize.width() + 2 * xradius];
memset(transition[i], 0, layerSize.width() + 2 * xradius);
transition[i] += xradius;
}
TQ_UINT8* out = new TQ_UINT8[layerSize.width()];
density = new TQ_UINT8*[2 * xradius + 1];
density += xradius;
for (TQ_INT32 x = 0; x < (xradius + 1); x++) // allocate density[][]
{
density[ x] = new TQ_UINT8[2 * yradius + 1];
density[ x] += yradius;
density[-x] = density[x];
}
for (TQ_INT32 x = 0; x < (xradius + 1); x++) // compute density[][]
{
double tmpx, tmpy, dist;
TQ_UINT8 a;
if (x > 0)
tmpx = x - 0.5;
else if (x < 0)
tmpx = x + 0.5;
else
tmpx = 0.0;
for (TQ_INT32 y = 0; y < (yradius + 1); y++)
{
if (y > 0)
tmpy = y - 0.5;
else if (y < 0)
tmpy = y + 0.5;
else
tmpy = 0.0;
dist = ((tmpy * tmpy) / (yradius * yradius) +
(tmpx * tmpx) / (xradius * xradius));
if (dist < 1.0)
a = 255 * (TQ_UINT8)(1.0 - sqrt (dist));
else
a = 0;
density[ x][ y] = a;
density[ x][-y] = a;
density[-x][ y] = a;
density[-x][-y] = a;
}
}
selection->readBytes(buf[0], layerSize.x(), layerSize.y(), layerSize.width(), 1);
memcpy (buf[1], buf[0], layerSize.width());
if (layerSize.height() > 1)
selection->readBytes(buf[2], layerSize.x(), layerSize.y() + 1, layerSize.width(), 1);
else
memcpy (buf[2], buf[1], layerSize.width());
computeTransition (transition[1], buf, layerSize.width());
for (TQ_INT32 y = 1; y < yradius && y + 1 < layerSize.height(); y++) // set up top of image
{
rotatePointers (buf, 3);
selection->readBytes(buf[2], layerSize.x(), layerSize.y() + y + 1, layerSize.width(), 1);
computeTransition (transition[y + 1], buf, layerSize.width());
}
for (TQ_INT32 x = 0; x < layerSize.width(); x++) // set up max[] for top of image
{
max[x] = -(yradius + 7);
for (TQ_INT32 j = 1; j < yradius + 1; j++)
if (transition[j][x])
{
max[x] = j;
break;
}
}
for (TQ_INT32 y = 0; y < layerSize.height(); y++) // main calculation loop
{
rotatePointers (buf, 3);
rotatePointers (transition, yradius + 1);
if (y < layerSize.height() - (yradius + 1))
{
selection->readBytes(buf[2], layerSize.x(), layerSize.y() + y + yradius + 1, layerSize.width(), 1);
computeTransition (transition[yradius], buf, layerSize.width());
}
else
memcpy (transition[yradius], transition[yradius - 1], layerSize.width());
for (TQ_INT32 x = 0; x < layerSize.width(); x++) // update max array
{
if (max[x] < 1)
{
if (max[x] <= -yradius)
{
if (transition[yradius][x])
max[x] = yradius;
else
max[x]--;
}
else
if (transition[-max[x]][x])
max[x] = -max[x];
else if (transition[-max[x] + 1][x])
max[x] = -max[x] + 1;
else
max[x]--;
}
else
max[x]--;
if (max[x] < -yradius - 1)
max[x] = -yradius - 1;
}
TQ_UINT8 last_max = max[0][density[-1]];
TQ_INT32 last_index = 1;
for (TQ_INT32 x = 0 ; x < layerSize.width(); x++) // render scan line
{
last_index--;
if (last_index >= 0)
{
last_max = 0;
for (TQ_INT32 i = xradius; i >= 0; i--)
if (max[x + i] <= yradius && max[x + i] >= -yradius && density[i][max[x+i]] > last_max)
{
last_max = density[i][max[x + i]];
last_index = i;
}
out[x] = last_max;
}
else
{
last_max = 0;
for (TQ_INT32 i = xradius; i >= -xradius; i--)
if (max[x + i] <= yradius && max[x + i] >= -yradius && density[i][max[x + i]] > last_max)
{
last_max = density[i][max[x + i]];
last_index = i;
}
out[x] = last_max;
}
if (last_max == 0)
{
TQ_INT32 i;
for (i = x + 1; i < layerSize.width(); i++)
{
if (max[i] >= -yradius)
break;
}
if (i - x > xradius)
{
for (; x < i - xradius; x++)
out[x] = 0;
x--;
}
last_index = xradius;
}
}
selection->writeBytes(out, layerSize.x(), layerSize.y() + y, layerSize.width(), 1);
}
delete [] out;
for (TQ_INT32 i = 0; i < 3; i++)
delete buf[i];
max -= xradius;
delete[] max;
for (TQ_INT32 i = 0; i < yradius + 1; i++)
{
transition[i] -= xradius;
delete transition[i];
}
delete[] transition;
for (TQ_INT32 i = 0; i < xradius + 1 ; i++)
{
density[i] -= yradius;
delete density[i];
}
density -= xradius;
delete[] density;
dev->setDirty();
dev->emitSelectionChanged();
}
#define RINT(x) floor ((x) + 0.5)
void KisSelectionManager::computeBorder (TQ_INT32 *circ, TQ_INT32 xradius, TQ_INT32 yradius)
{
Q_ASSERT(xradius != 0);
TQ_INT32 i;
TQ_INT32 diameter = xradius * 2 + 1;
double tmp;
for (i = 0; i < diameter; i++)
{
if (i > xradius)
tmp = (i - xradius) - 0.5;
else if (i < xradius)
tmp = (xradius - i) - 0.5;
else
tmp = 0.0;
circ[i] = (TQ_INT32) RINT (yradius / (double) xradius * sqrt (xradius * xradius - tmp * tmp));
}
}
void KisSelectionManager::rotatePointers (TQ_UINT8 **p, TQ_UINT32 n)
{
TQ_UINT32 i;
TQ_UINT8 *tmp;
tmp = p[0];
for (i = 0; i < n - 1; i++) p[i] = p[i + 1];
p[i] = tmp;
}
void KisSelectionManager::computeTransition (TQ_UINT8* transition, TQ_UINT8** buf, TQ_INT32 width)
{
TQ_INT32 x = 0;
if (width == 1)
{
if (buf[1][x] > 127 && (buf[0][x] < 128 || buf[2][x] < 128))
transition[x] = 255;
else
transition[x] = 0;
return;
}
if (buf[1][x] > 127)
{
if ( buf[0][x] < 128 || buf[0][x + 1] < 128 ||
buf[1][x + 1] < 128 ||
buf[2][x] < 128 || buf[2][x + 1] < 128 )
transition[x] = 255;
else
transition[x] = 0;
}
else
transition[x] = 0;
for (TQ_INT32 x = 1; x < width - 1; x++)
{
if (buf[1][x] >= 128)
{
if (buf[0][x - 1] < 128 || buf[0][x] < 128 || buf[0][x + 1] < 128 ||
buf[1][x - 1] < 128 || buf[1][x + 1] < 128 ||
buf[2][x - 1] < 128 || buf[2][x] < 128 || buf[2][x + 1] < 128)
transition[x] = 255;
else
transition[x] = 0;
}
else
transition[x] = 0;
}
if (buf[1][x] >= 128)
{
if (buf[0][x - 1] < 128 || buf[0][x] < 128 ||
buf[1][x - 1] < 128 ||
buf[2][x - 1] < 128 || buf[2][x] < 128)
transition[x] = 255;
else
transition[x] = 0;
}
else
transition[x] = 0;
}
#include "kis_selection_manager.moc"
|