xxxxxxxxxx
237
var arrin=[];
var org;
var loadphoto=true;
var sd=55;
function preload() {
if(loadphoto)
{
org = createImage(400,400);
org = loadImage('/dino.jpeg');
}
else
{
org= createImage(400,400);
org.loadPixels();
loadStrings('/out5.txt', ldstr);
}
}
function ldstr(instr)
{
for(var i=1; i<org.pixels.length; i+=4)
{
org.pixels[i]=instr[i];
org.pixels[i+1]=instr[i+1];
org.pixels[i+2]=instr[i+2];
org.pixels[i+3]=instr[i+3];
//arrin.push(instr[i]);
}
org.updatePixels();
console.log("Image Loaded From Text File");
}
var x=0;
var y=0;
var thresh=1;
var threshvel=0;
var j=0;
var index1=0;
var index2=0;
function setup() {
cnv=createCanvas(400, 400);
pixelDensity(1);
org.resize(400,400);
img = createImage(400,400);
org.loadPixels();
img.loadPixels();
for(var i=0; i<img.pixels.length; i++)
{
img.pixels[i]=org.pixels[i];
}
img.updatePixels();
image(img,0,0);
//image(org, 0,0);
/* Usded for learning how to access the index correctly.
console.log(img.pixels.length);
var pixind=0;
for(var i=0; i<height; i+=2)
{
for(var j=0; j<width; j++)
{
point(j,i);
img.pixels[pixind]=0;
img.pixels[pixind+1]=0;
img.pixels[pixind+2]=0;
//img.pixels[pixind]=0;
pixind=ind(i,j) *4;
}
}
*/
//save(img.pixels, 'out.txt')
}
function draw() {
//background(0);
}
function keyPressed()
{
//img=org;
//setup();
// console.log(keyCode);
switch(keyCode)
{
case 38: // 'up' increase seed
sd++;
break;
case 40: // 'down' decrease seed
sd--;
break;
case 73: // 'i' SAVE IMAGE FILE
var mt= (minute());
var fname= "out" + mt+ ".jpg"// + mt + ".jpg");
save(img, fname);
break;
case 83: //'s' SAVE text file
var mt= (minute());
var fname= "out" + mt+ ".txt"// + mt + ".txt");
save(img.pixels, fname);
break;
case 69: //'e' ENCRYPT picture
encrypt();
break;
}
}
function ind(r, c) // Returns Array index given a row/ col
{
var arrind=0;
arrind=r * width + c;
return arrind;
}
function encrypt()
{
// noprotect
var temp=[];
var tmp;
var rnd=[];
for(var i=width/2; i<width; i++)
{
rnd.push(i);
}
randomSeed(sd);
for(var m=0; m<200; m++)
{
// console.log(m);
for(var i=0; i<rnd.length; i++)
{
tmp=rnd[i];
rand=floor(random(rnd.length-1));
rnd[i]=rnd[rand];
rnd[rand]=tmp;
}
}
//console.log(rnd);
skip=0
for(var i=0; i<height-skip; i++)
{
for(var j=0; j<width/2; j+=1)
{
index1=ind(i,j)*4;
index2=ind(i, rnd[j])*4;
temp[0]=img.pixels[index1];
temp[1]=img.pixels[index1+1];
temp[2]=img.pixels[index1+2];
temp[3]=img.pixels[index1+3];
img.pixels[index1]=img.pixels[index2];
img.pixels[index1+1]=img.pixels[index2+1];
img.pixels[index1+2]=img.pixels[index2+2];
img.pixels[index1+3]=img.pixels[index2+3];
img.pixels[index2]=temp[0];
img.pixels[index2+1]=temp[1]
img.pixels[index2+2]=temp[2]
img.pixels[index2+3]=temp[3]
}
}
for(var j=0; j<width; j++)
{
for(var i=0; i<height/2; i+=1)
{
index1=ind(rnd[i],j)*4;
index2=ind(i, j)*4;
temp[0]=img.pixels[index1];
temp[1]=img.pixels[index1+1];
temp[2]=img.pixels[index1+2];
temp[3]=img.pixels[index1+3];
img.pixels[index1]=img.pixels[index2];
img.pixels[index1+1]=img.pixels[index2+1];
img.pixels[index1+2]=img.pixels[index2+2];
img.pixels[index1+3]=img.pixels[index2+3];
img.pixels[index2]=temp[0];
img.pixels[index2+1]=temp[1]
img.pixels[index2+2]=temp[2]
img.pixels[index2+3]=temp[3]
}
}
img.updatePixels();
image(img, 0, 0);
}