-
Notifications
You must be signed in to change notification settings - Fork 2
/
magiceye.js
304 lines (252 loc) · 8.93 KB
/
magiceye.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
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
/*! MagicEye.js (https://github.com/peeinears/MagicEye.js)
*
* MIT License (http://www.opensource.org/licenses/mit-license.html)
* Copyright (c) 2014 Ian Pearce
*/
!(function(){
'use strict';
window.MagicEye = function (opts) {
if (!opts) opts = {};
this.el = opts.el;
// set options to specified or default values
for (var property in this.defaultOptions) {
this[property] = (opts && opts[property]) ? opts[property] : this.defaultOptions[property];
}
if (!this.palette) this.palette = this.generatePalette(this.numColors);
return this;
};
MagicEye.prototype.defaultOptions = {
width: 400,
height: 300,
numColors: 10,
depthMap: '0',
adaptToElementSize: false
};
MagicEye.prototype.randomRGB = function () {
return [Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256),
255];
};
MagicEye.prototype.generatePalette = function (numColors) {
var palette = [];
for (var i = 0; i < numColors; i++) {
palette.push(this.randomRGB());
}
return palette;
};
MagicEye.prototype.generatePixelData = function (width, height, depthMap, numColors) {
/*
* This algorithm was published in a paper authored by by Harold W.
* Thimbleby, Stuart Inglis, and Ian H. Witten. The following code was
* translated from the C code that was featured in the article.
* http://www.cs.sfu.ca/CourseCentral/414/li/material/refs/SIRDS-Computer-94.pdf
*/
var x, y, left, right, visible, t, zt, k, sep, z, row,
same, // points to a pixel to the right
dpi = 72, // assuming output of 72 dots per inch
eyeSep = Math.round(2.5 * dpi), // eye separation assumed to be 2.5 inches
mu = (1 / 3), // depth of field (fraction of viewing distance)
pixels = []; // two-dimensional array of pixels to be returned in the form pixels[y][x]
// rather than storing RGB values here, we store a reference to a color in the palette
for (y = 0; y < height; y++) {
row = [];
same = []; // points to a pixel to the right
for (x = 0; x < width; x++) {
same[x] = x; // each pixel is initially linked with itself
}
for (x = 0; x < width; x++) {
z = depthMap[y][x];
// stereo separation corresponding to z
sep = Math.round((1 - (mu * z)) * eyeSep / (2 - (mu * z)));
// x-values corresponding to left and right eyes
left = Math.round(x - ((sep + (sep & y & 1)) / 2));
right = left + sep;
if (0 <= left && right < width) {
// remove hidden surfaces
t = 1;
do {
zt = z + (2 * (2 - (mu * z)) * t / (mu * eyeSep));
visible = (depthMap[y][x-t] < zt) && (depthMap[y][x+t] < zt); // false if obscured
t++;
} while (visible && zt < 1);
if (visible) {
// record that left and right pixels are the same
for (k = same[left]; k !== left && k !== right; k = same[left]) {
if (k < right) {
left = k;
} else {
left = right;
right = k;
}
}
same[left] = right;
}
}
}
for (x = (width - 1); x >= 0; x--) {
if (same[x] === x) {
// set random color
row[x] = Math.floor(Math.random() * numColors);
} else {
// constrained pixel, obey constraint
row[x] = row[same[x]];
}
}
pixels[y] = row;
}
return pixels;
};
MagicEye.prototype.formatDepthMap = function (width, height, template) {
/*
* Resizes and converts depth maps of various formats into the format
* readable by `generatePixelData()`.
*
* These all return the same depth map:
*
* var template = [" ",
* " # ",
* " "];
*
* var template = ["000",
* "010",
* "000"];
*
* var template = " \n # \n ";
*
* var template = "000\n010\n000";
*
* var template = [[0, 0, 0],
* [0, 1, 0],
* [0, 0, 0]];
*
* Of course, you can have varying depths:
*
* var template = ["001",
* "012",
* "123"];
*
* var template = [[0.0, 0.0, 0.3],
* [0.0, 0.3, 0.6],
* [0.3, 0.6, 0.9]];
*
*/
var x, y, highest, templateY,
templateHeight = 0,
templateWidth = 0,
tmpTemplate = [],
depthMap = [];
if (typeof template === 'string') {
template = template.split(/\n/);
}
if (typeof template === 'object' && template.hasOwnProperty(0)) {
templateHeight = template.length;
if (typeof template[0] === 'string') {
for (y = 0; y < templateHeight; y++) {
template[y] = template[y].replace(/ /g, '0'); // replace spaces with 0
template[y] = template[y].replace(/[^\d\n]/g, '1'); // replace non-digits with 1
template[y] = template[y].split('').map( function (v) { return parseInt(v); } ); // turn '0110' into [0, 1, 1, 0]
}
}
if (typeof template[0] === 'object') {
// get template width and highest level
highest = 0;
for (y = 0; y < templateHeight; y++) {
if (template[y].length > templateWidth) {
templateWidth = template[y].length;
}
for (x = 0; x < template[y].length; x++) {
if (template[y][x] > highest) {
highest = template[y][x];
}
}
}
// scale levels down to fractions and add zeros for rows of length less than template width
for (y = 0; y < templateHeight; y++) {
for (x = 0; x < templateWidth; x++) {
if (typeof template[y][x] === 'undefined') {
template[y][x] = 0;
} else {
template[y][x] = template[y][x] / highest;
}
}
}
} else {
throw('Stereogram.formatDepthMap: invalid depth map template format');
}
} else {
throw('Stereogram.formatDepthMap: invalid depth map template format');
}
for (y = 0; y < height; y++) {
depthMap[y] = [];
templateY = Math.floor(y * templateHeight / height);
for (x = 0; x < width; x++) {
depthMap[y].push( template[templateY][Math.floor(x * templateWidth / width)] );
}
}
return depthMap;
};
MagicEye.prototype.renderPNG = function (img) {
var canvas = document.createElement('canvas');
canvas.width = this.width;
canvas.height = this.height;
this.renderToCanvas(canvas);
img.src = canvas.toDataURL("image/png");
return this;
};
MagicEye.prototype.renderToCanvas = function (canvas) {
this.setupRender();
var x, y, i, rgb, yOffset, xOffset,
context = canvas.getContext("2d"),
imageData = context.createImageData(this.width, this.height);
for (y = 0; y < this.height; y++) {
yOffset = y * this.width * 4;
for (x = 0; x < this.width; x++) {
rgb = this.palette[this.pixels[y][x]];
xOffset = x * 4;
for (i = 0; i < 4; i++) {
imageData.data[yOffset + xOffset + i] = rgb[i];
}
}
}
context.putImageData(imageData, 0, 0);
return this;
};
MagicEye.prototype.regeneratePalette = function (numColors) {
this.numColors = numColors || this.numColors;
this.palette = this.generatePalette(this.numColors);
return this;
};
MagicEye.prototype.setupRender = function () {
// generate properly formatted depth map from template
this.rawDepthMap = this.formatDepthMap(this.width, this.height, this.depthMap);
// generate the pixel data for the stereogram
this.pixels = this.generatePixelData(this.width, this.height, this.rawDepthMap, this.numColors);
return this;
};
MagicEye.prototype.render = function () {
var element = (typeof this.el === 'string' ? document.getElementById(this.el) : this.el);
if (!element || !element.tagName) {
throw('MagicEye.render(): invalid element: ' + element);
}
if (this.adaptToElementSize) {
if (!element || !element.tagName) {
throw('Stereogram.render: invalid element: ' + element);
}
if (typeof element.width === 'number' && typeof element.height === 'number') {
this.width = element.width;
this.height = element.height;
} else {
throw("Stereogram.render: adaptToElementSize set to true, but size of element " + element + " is unknown");
}
}
if (element.tagName.toLowerCase() === 'img') {
this.renderPNG(element);
} else if (element.tagName.toLowerCase() === 'canvas') {
this.renderToCanvas(element);
} else {
throw("MagicEye.render(): invalid element, can only render to <img> or <canvas>");
}
return this;
};
})();