-
Notifications
You must be signed in to change notification settings - Fork 0
/
Outline.as
110 lines (88 loc) · 2.45 KB
/
Outline.as
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
package rahil {
import flash.display.BitmapData;
import flash.geom.Point;
/**
* Returns the points of each pixel that has a neighboring transparent pixel
* Also it is worth noting that this code treats out-of-bound pixels as being non-transparent.
* @author moagrius, http://upshots.org/actionscript-3/get-outline-from-bitmapdata
*/
public class Outline {
private static const SPOTS:Array = [[-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1]];
private var bitmapData:BitmapData;
private var pixelTable:Array;
private var rows:uint;
private var cols:uint;
public var points:Array;
private var _outlineBitmapData:BitmapData;
public function Outline(bmd:BitmapData):void {
points = [];
bitmapData = bmd;
initialize();
}
private function initialize():void {
rows = bitmapData.height;
cols = bitmapData.width;
pixelTable = new Array(rows);
var row:Array;
for (var i:int = 0; i < rows; i++){
row = [];
for (var j:int = 0; j < cols; j++){
row[j] = bitmapData.getPixel32(j, i) >> 24 & 0xFF;
}
pixelTable[i] = row;
}
populate();
//TODO: added by me
setOutlineBitmapData();
}
public function hasTransparentNeighbor(rowIndex:uint, cellIndex:uint):Boolean {
var spot:Array;
var rowNum:int;
var cellNum:int;
var row:Array;
for (var i:int = 0; i < 8; i++){
spot = SPOTS[i];
rowNum = rowIndex + spot[0];
if (rowNum > -1){
if (rowNum < rows){
cellNum = cellIndex + spot[1];
if (cellNum > -1){
row = pixelTable[rowNum];
if (cellNum < cols){
if (!row[cellNum]){
return true;
}
}
}
}
}
}
return false;
}
private function populate():void {
for (var i:int = 0; i < rows; i++){
for (var j:int = 0; j < cols; j++){
if (pixelTable[i][j]){
if (hasTransparentNeighbor(i, j)){
points.push(new Point(j, i));
}
}
}
}
}
//added by Rahil
private function setOutlineBitmapData():void {
_outlineBitmapData = new BitmapData(bitmapData.width, bitmapData.height, true, 0);
for (var i:int = 0; i < points.length; i++) {
_outlineBitmapData.setPixel32((points[i] as Point).x, (points[i] as Point).y, 0xFF0000FF);
}
}
/**
* Returns a bitmap with a blue outline
* added by Rahil
*/
public function get outlineBitmapData():BitmapData {
return _outlineBitmapData;
}
}
}