-
Notifications
You must be signed in to change notification settings - Fork 1
/
xiaolinWuLineDemo.html
152 lines (120 loc) · 4.19 KB
/
xiaolinWuLineDemo.html
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
<html>
<head>
<style> </style>
<script type="text/javascript" src="src/image.js"></script>
<script type="text/javascript" src="src/helpers.js"></script>
<script type="text/javascript" src="libs/pixastic/pixastic.core.js"></script>
<script type="text/javascript" src="libs/pixastic/actions/histogram.js"></script>
<script type="text/javascript">
var img;
var imgWidth, imgHeight;
var imgScale = 1.0;
var points = [];
var pixels, bwPixels;
function setup()
{
document.getElementById('imageFile').addEventListener('change', handleImageSelect, false);
document.getElementById('canvas').addEventListener('click', handleCanvasClick, false);
// enable dnd on canvas to load images
var holder = document.getElementById('canvas');
holder.ondragover = function () { this.className = 'hover'; return false; };
holder.ondragend = function () { this.className = ''; return false; };
holder.ondrop = function (e) {
this.className = ''; // what for?
e.preventDefault();
var file = e.dataTransfer.files[0];
asyncLoadImageFromFile(file);
};
asyncLoadImageFromURL("test1.png")
}
function handleImageSelect(evt) {
pixels = undefined;
var files = evt.target.files; // FileList object
var file = files[0];
asyncLoadImageFromFile(file);
}
function handleCanvasClick(evt) {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
if (!pixels) {
pixels = context.getImageData(0,0, canvas.width, canvas.height);
bwPixels = RGBA2A(pixels, context);
}
var coords = canvas.relMouseCoords(event);
var x = coords.x/imgScale;
var y = coords.y/imgScale;
if (x >= 0 && x < imgWidth && y >= 0 && y < imgHeight)
points.push([x, y]);
context.beginPath();
context.arc(x, y, 3, 0, Math.PI*2, true);
context.closePath();
context.fill();
if (points.length == 2)
{
var sum = 0;
var n = 0;
var w = bwPixels.width;
xiaolinWuLineIterator(points[0][0],points[0][1], points[1][0], points[1][1], function(x, y, c) {
n++;
sum += bwPixels[x+w*y];
pixels.data[4*(x+w*y)] = (1-c) * pixels.data[4*(x+w*y)] + c * 0;
pixels.data[4*(x+w*y)+1] = (1-c) * pixels.data[4*(x+w*y)+1] + c * 0;
pixels.data[4*(x+w*y)+2] = (1-c) * pixels.data[4*(x+w*y)+2] + c * 0;
//pixels.data[4*(x+w*y)+3] = 255;
});
/*
lineIterator(points[0][0],points[0][1], points[1][0], points[1][1], function(x, y) {
n++;
sum += bwPixels[x+w*y];
pixels.data[4*(x+w*y)] = 255 - pixels.data[4*(x+w*y)];
pixels.data[4*(x+w*y)+1] = 255 - pixels.data[4*(x+w*y)+1];
pixels.data[4*(x+w*y)+2] = 255 - pixels.data[4*(x+w*y)+2];
});
//*/
//alert("s: " + sum + " n: " + n + " " + (sum/n));
context.putImageData(pixels, 0, 0);
//document.getElementById('outputText').value = "convert ..\\..\\web\\" + fileName + " -virtual-pixel transparent -distort Perspective \"" + s + "\" " + fileName + "out.png";
//handleLoad(img);
points = [];
}
}
function drawImage(img) {
var canvas = document.getElementById('canvas');
canvas.width = img.width;
canvas.height = img.height;
imgWidth = img.width;
imgHeight = img.height;
var context = canvas.getContext('2d');
context.drawImage(img, 0, 0, img.width, img.height);
}
function scaleImage(scale)
{
imgScale *= scale;
var canvas = document.getElementById('canvas');
canvas.style.width = "" + (imgWidth*imgScale) +"px";
canvas.style.height = "" + (imgHeight*imgScale) +"px";
//
//canvas.height = imgHeight;
}
var hist = {};
function handleTestClick()
{
Pixastic.process(document.getElementById("canvas"), "histogram", {
average : false, paint:true,color:"rgba(255,255,255,0.5)",returnValue:hist
});
document.getElementById('debugText').value = hist.values;
}
</script>
</head>
<body onload="setup()">
<form name="controls" action="">
<input type="button" value="Reset" onclick="handleReset();">
<input type="file" value="imageFile" id="imageFile" />
<input type="button" value="scale/1.2" onclick="scaleImage(1/1.2);"><input type="button" value="scale*1.2" onclick="scaleImage(1.2);">
<input type="text" name="outputText" id="outputText" />
<input type="button" value="TestButton" onclick="handleTestClick();">
<input type="text" name="debugText" id="debugText" />
</form>
<canvas id="canvas" width="400" height="300"></canvas>
</body>
</html>