xxxxxxxxxx
387
var arrin=[];
var org;
var upl;
var input;
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)
{
//console.log("CALLED");
//console.log(instr.length);
for(var i=1; i<upl.pixels.length; i+=4)
{
upl.pixels[i]=instr[i];
upl.pixels[i+1]=instr[i+1];
upl.pixels[i+2]=instr[i+2];
upl.pixels[i+3]=instr[i+3];
//arrin.push(instr[i]);
}
upl.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;
var runonce=true;
function setup()
if(runonce)
{
cnv=createCanvas(400, 400);
input = createFileInput(handleFile);
input.position(0, height+10);
button = createButton('ENCRYPT/DECRYPT');
button.position(0, height+40);
button.mousePressed(encrypt);
saveimg=createButton('SAVE IMAGE');
saveimg.position(0, height+70);
saveimg.mousePressed(sve);
savetxt=createButton("SAVE TXT");
savetxt.position(150, height+70);
savetxt.mousePressed(svetxt);
runonce=false;
}
pixelDensity(1);
org=createImage(400,400);
org.resize(400,400);
if(upl)
{
upl.resize(400,400);
upl.loadPixels();
//image(upl, 0, 0);
img = createImage(400,400);
org.loadPixels();
img.loadPixels();
for(var i=0; i<img.pixels.length; i++)
{
img.pixels[i]=upl.pixels[i];
}
img.updatePixels();
//console.log(img.pixels[1000] +","+ img.pixels[1001] +","+ img.pixels[1002]+"," + img.pixels[1003]);
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);
}
function handleFile(file) {
//print(file);
if (file.type === 'image') {
upl=createImage(400,400);
upl = loadImage(file.data, upl=>setup());
//upl.hide();
}
else if(file.type== 'text')
{
var txtfile=[];
var cleanfile=[];
var fnm="'./" + file.name +"'";
txtfile=file.data;
var tt="";
for(var i=0; i<txtfile.length; i++)
{
if(txtfile[i]!='\n')
{
tt=tt+txtfile[i];
//cleanfile.push(int(txtfile[i]));
}
else
{
cleanfile.push(tt);
tt="";
}
}
upl=createImage(400,400);
upl.loadPixels();
ldstr(cleanfile);
setup();
}
else {
upl = null;
}
}
function sve(){
var mt= (minute());
var fname= "out" + mt+ ".jpg"// + mt + ".jpg");
save(img, fname);
}
function svetxt()
{
var mt= (minute());
var fname= "out" + mt+ ".txt"// + mt + ".txt");
save(img.pixels, fname);
}
function convert(num)
{
var remainder=num;
var quotient=0;
var n=num;
var hexA=[];
var hexn="";
while(n)
{
quotient=floor(n/16)
remainder=n%16;
if(remainder<10)
{
hexA.push(str(remainder))
}
else
{
switch(remainder)
{
case 10: hexA.push("A");
break;
case 11: hexA.push("B");
break;
case 12: hexA.push("C");
break;
case 13: hexA.push("D");
break;
case 14: hexA.push("E");
break;
case 15: hexA.push("F");
break;
}
}
n=quotient;
}
for(var i=0; i<hexA.length; i++)
{
if(i==0)
{
if(hexA[i]!="0")
hexn=hexn+hexA[i];
}
else
{
hexn=hexn+hexA[i];
}
}
return hexn;
}