-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
161 lines (137 loc) · 3.03 KB
/
index.js
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
/**
* Module dependencies.
*/
var convolve = require('convolve')
, Segment = require('./segment')
, edges = require('./edges');
/**
* Expose `focus()`.
*/
module.exports = focus;
/**
* Approximate the focal point of `canvas`.
*
* Options:
*
* - `debug` [false]
*
* @param {Canvas} canvas
* @param {Object} options
* @return {Point}
* @api public
*/
function focus(canvas, options){
options = options || {};
var debug = options.debug;
// setup
var w = canvas.width;
var h = canvas.height;
var ctx = canvas.getContext('2d');
var data = ctx.getImageData(0, 0, w, h);
var res = ctx.createImageData(w, h);
// edge detection
convolve(edges)
.width(w)
.height(h)
.apply(data, res);
// edge debugging
if (debug) {
canvas.width *= 2;
ctx.putImageData(res, 0, 0);
}
// single-pass
var segs = segments(res.data, w, h);
// segment debugging
if (debug) {
debugSegments(segs, ctx);
ctx.translate(w, 0);
ctx.putImageData(data, w, 0);
debugSegments(segs, ctx);
}
return focalSegment(segs).midpoint();
}
/**
* Draw debug segments.
*
* @param {Array} segs
* @param {CanvasContext2d} ctx
* @api private
*/
function debugSegments(segs, ctx) {
for (var i = 0; i < segs.length; ++i) segs[i].draw(ctx);
focalSegment(segs).drawFocus(ctx);
}
/**
* Return the most "intense" segment.
*
* TODO: we should check the neighbour segments
* and walk them within a given intensity factor,
* and return a mid-point of that.
*
* @param {Array} sefgs
* @return {Segment}
* @api private
*/
function focalSegment(segs) {
var seg;
var ret = segs[0];
for (var i = 1; i < segs.length; ++i) {
seg = segs[i];
if (seg.intensity > ret.intensity) ret = seg;
}
return ret;
}
/**
* Return segments for `data`.
*
* This algorithm walks every edge
* and averages the intensity in 20x20
* pixel "segments".
*
* TODO: we could easily speed things up
* by skipping N pixels etc...
*
* @param {ImageData} data
* @param {Number} w
* @param {Number} h
* @return {Array}
* @api private
*/
function segments(data, w, h){
var segs = [];
var size = 20;
var hx = w / 2;
var hy = h / 2;
function factor(x, y) {
x = Math.abs(x - hx) / hx;
y = Math.abs(y - hy) / hy;
x /= 2;
y /= 2;
return 1 - (x + y);
}
for (var x = 0; x < w; ++x) {
for (var y = 0; y < h; ++y) {
var focus = factor(x + size / 2, y + size / 2) * .15;
var width = Math.min(w - x, size);
var height = Math.min(h - y, size);
var pixels = size * size;
var sum = 0;
for (var sx = 0; sx < width; ++sx) {
for (var sy = 0; sy < height; ++sy) {
var px = ((y + sy) * w + (x + sx)) * 4;
var r = data[px + 0];
var g = data[px + 1];
var b = data[px + 2];
var n = r + g + b;
var intensity = n / 765;
sum += intensity;
}
}
focus += (sum / pixels) * .85;
segs.push(new Segment(x, y, width, height, focus * 5));
y += size;
}
x += size;
}
return segs;
};