xxxxxxxxxx
93
const pix = new Float32Array(65536);
const pixSRP = new Float32Array(65536);
const pixRP = new Float32Array(65536);
const rpFlips = new Float32Array(65536);
const srpFlips = new Float32Array(65536);
function setup() {
createCanvas(512, 512);
background(0);
randomSeed(1234567); //Random number seed value
// Random sign flip array
for (let i = 0; i < rpFlips.length; i++) {
rpFlips[i] = random(-1, 1) < 0 ? -1 : 1;
}
// Sub-Random sign flips
let r = 0;
let rAdd = 0xb55988be; // additive subrandom constant
for (let i = 0; i < srpFlips.length; i++) {
r += rAdd;
r &= 0xffffffff;
srpFlips[i] = (r & 0x80000000) === 0 ? -1 : 1;
if (srpFlips[i] > 0)
set(256 + (i & 255), int(i / 256), color(255, 255, 255));
}
updatePixels();
}
function draw() {
fill(0);
square(0, 0, 255);
fill(255);
let x = constrain(mouseX, 0, 230);
let y = constrain(mouseY, 0, 230);
circle(x, y, 50);
//loadPixels();
getPix();
for (let i = 0; i < pix.length; i++) {
pixRP[i] = rpFlips[i] * pix[i];
pixSRP[i] = srpFlips[i] * pix[i];
}
whtN(pixRP); //Random Projection
whtN(pixSRP); //SubRandom Projection
setImage(0, 0, pix);
setImage(0, 256, pixRP);
setImage(256, 256, pixSRP);
updatePixels();
}
function getPix() {
for (let y = 0, idx = 0; y < 256; y++) {
for (let x = 0; x < 256; x++) {
let c = get(x, y);
pix[idx] = (red(c) + green(c) + blue(c)) * 0.3333 - 127.5;
idx++;
}
}
}
function setImage(x, y, pixArray) {
for (let yi = 0, idx = 0; yi < 256; yi++) {
for (let xi = 0; xi < 256; xi++) {
let c = constrain(pixArray[idx] + 127.5, 0, 255);
idx++;
set(x + xi, y + yi, color(c, c, c));
}
}
}
// Fast Walsh Hadamard transform, Normalized.
// Scaled so that vector length is unchanged.
function whtN(vec) {
const n = vec.length;
let hs = 1;
while (hs < n) {
let i = 0;
while (i < n) {
const j = i + hs;
while (i < j) {
var a = vec[i];
var b = vec[i + hs];
vec[i] = a + b;
vec[i + hs] = a - b;
i += 1;
}
i += hs;
}
hs += hs;
}
const sc = 1.0 / sqrt(n);
for (let i = 0; i < n; i++) {
vec[i] *= sc;
}
}