Repository navigation
Expand file tree
/
Copy pathImageUtil.js
More file actions
198 lines (150 loc) · 4.17 KB
/
Copy pathImageUtil.js
File metadata and controls
198 lines (150 loc) · 4.17 KB
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
function BlackTracer(imageData)
{
this.imageData = imageData;
this.points = [];
this.markedPoints = [];
for(var x = 0; x<this.imageData.width; x++)
{
for(var y = 0; y<this.imageData.height; y++)
{
var p = {};
p.x = x;
p.y = y;
p.isMarked = false;
this.points.push(p);
}
}
for(var i=0; i<this.points.length; i++)
{
var p = this.points[i];
var x = p.x;
var y = p.y;
var top = {x:x,y:y-1};
var left = {x:x-1,y:y};
var right = {x:x+1,y:y};
var bottom = {x:x,y:y+1};
if(top.y>=0 && left.x>=0 && right.x<=this.imageData.width && bottom.y<=this.imageData.height)
{
var selfColor = getRGBAt(imageData,x,y);
var topColor = getRGBAt(imageData,top.x,top.y);
var leftColor = getRGBAt(imageData,left.x,left.y)
var rightColor = getRGBAt(imageData,right.x,right.y);
var bottomColor = getRGBAt(imageData,bottom.x,bottom.y);
if(selfColor.r > 250 && selfColor.g >250 && selfColor.b >250 && p.isMarked == false)
{
var isTopBlack = topColor.r == 0 && topColor.g == 0 && topColor.b == 0;
var isLeftBlack = leftColor.r == 0 && leftColor.g == 0 && leftColor.b == 0;
var isRightBlack = rightColor.r == 0 && rightColor.g == 0 && rightColor.b == 0;
var isBottomBlack = bottomColor.r == 0 && bottomColor.g == 0 && bottomColor.b == 0;
if(isTopBlack || isLeftBlack || isRightBlack || isBottomBlack)
{
p.isMarked = true;
this.markedPoints.push(p);
}
}
}
}
}
function getAvarageRGBAt(imageData,x,y,width,height)
{
var xl = width - x;
var yl = height - y;
var avarageColor = {r:0,g:0,b:0}
var colorCount = 0;
for(var ix=x; ix<x+width; ix++)
{
for(var iy=y; iy<y+height; iy++)
{
var color = getRGBAt(imageData,ix,iy);
avarageColor.r += color.r;
avarageColor.g += color.g;
avarageColor.b += color.b;
colorCount++;
}
}
avarageColor = {r:Math.floor(avarageColor.r/colorCount),
g:Math.floor(avarageColor.g/colorCount),
b:Math.floor(avarageColor.b/colorCount)};
return avarageColor;
}
function isRGBDifferent(imageData,x,y,width,height,distanceThreshold,countThreshold)
{
var avarageColor = getAvarageRGBAt(imageData,x,y,width,height);
var colorCount = 0;
var differentColorCount = 0;
var xl = width - x;
var yl = height - y;
// console.log("avarageColor ",avarageColor);
for(var ix=x; ix<x+width; ix++)
{
for(var iy=y; iy<y+height; iy++)
{
var color = getRGBAt(imageData,ix,iy);
var colorDistance = getColorDistance(color,avarageColor);
// console.log(color,avarageColor,colorDistance);
if(colorDistance>distanceThreshold)
{
differentColorCount++;
}
colorCount++;
}
}
if(differentColorCount/colorCount > countThreshold)
{
return true;
}
return false;
}
function getColorDistance(color1,color2)
{
var dr = Math.abs(color2.r-color1.r);
var dg = Math.abs(color2.g-color1.g);
var db = Math.abs(color2.b-color1.b);
var td = dr + dg + db;
var nd = td / (255+255+255);
return nd;
}
function ImageDataLoader ()
{
this.imageDatas = [];
this.canvas = document.createElement('canvas');
this.getImageDataWithURL = function(imageURL,completion)
{
if (this.imageDatas[imageURL])
{
completion(this.imageDatas[imageURL]);
return;
}
var img = new Image();
var canvas = this.canvas;
var context = this.context;
var imageDatas = this.imageDatas;
img.onload = function ()
{
var imageWidth = img.naturalWidth;
var imageHeigth = img.naturalHeight;
canvas.width = imageWidth;
canvas.height = imageHeigth;
context = canvas.getContext('2d');
// console.log(canvas,imageWidth,imageHeigth);
context.drawImage(img, 0, 0 );
var imageData = context.getImageData(0, 0, imageWidth, imageHeigth);
imageDatas[imageURL] = {image:img,data:imageData};
completion(imageDatas[imageURL]);
}
img.src = imageURL;
}
}
function getRGBAt(imageData,x,y)
{
x = Math.floor(x);
y = Math.floor(y);
if(x<imageData.width && y<imageData.height)
{
var red = imageData.data[((imageData.width * y) + x) * 4];
var green = imageData.data[((imageData.width * y) + x) * 4 + 1];
var blue = imageData.data[((imageData.width * y) + x) * 4 + 2];
return {r:red,g:green,b:blue};
}
return null;
}