forked from svaarala/duktape
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandom_image.js
More file actions
35 lines (30 loc) · 820 Bytes
/
Copy pathrandom_image.js
File metadata and controls
35 lines (30 loc) · 820 Bytes
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
/*
* Output a 1024x1024 image generated using Math.random().
*/
function test(filename, threshold) {
var i, j, t;
var res = [];
res.push('P2\n1024 1024\n255\n');
for (i = 0; i < 1024; i++) {
for (j = 0; j < 1024; j++) {
t = Math.random();
if (typeof threshold === 'number') {
res.push(t >= threshold ? '255 ' : '0 ');
} else {
res.push(String(Math.floor(t * 256.0)) + ' ');
}
}
res.push('\n');
}
res = res.join('');
writeFile(filename, res);
print('Wrote', filename);
}
try {
test('rnd-threshold-01.pgm', 0.1);
test('rnd-threshold-05.pgm', 0.5);
test('rnd-threshold-09.pgm', 0.9);
test('rnd-threshold-none.pgm', null);
} catch (e) {
print(e.stack || e);
}