Code:
Execution
// _______
// | | _ _
// |_______|___ ___ _ _ _ ___| | |___ ___ _ _ _ ___ ___ _ _
// | | |___| | | |_ | | |___|_ | | | |___| _ | | |
// |___ _______| |___|_|_|_|_|_|_|_|___|_|_|_|_|_|___|___|_|_|
// | |
// |_______| Alpha-map generator
// _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
// | |
// Variables:
// | |
var n= true
// | If true, some pixels will be random. |
// If false, they will all be interpolated.
// | |
var s= 0
// | Smoothness. Increasing by 1 halves the |
// chance of a pixel being random. Increases
// | by 1 each pass. 0 is 50% on the first |
// pass.
// | |
var t= .5
// | Chance that a random pixel will be on, |
// out of 1.
// | |
var r= false
// | If true, all pixels of a different value |
// than surrounding ones will be switched.
// | |
var k= 0
// | Maximum number of neighboring pixels of |
// the same value for a pixel to be
// | switched. Higher values take much longer. |
// Maximum is 3 to avoid an infinite loop.
// | |
// Note: Biggest possible area of 2^n+1 x
// | 2^n+1 will be filled. |
//
// |_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _|
//
s=Math.pow(2,s)
if(k>3) k=3;
var i=Document.RasterImage;
var x=0,y=0;
var p=0;
if(i.sizeX<i.sizeY) p=i.sizeX;
else p=i.sizeY;
p=Math.pow(2,Math.floor(Math.log(p-1)/Math.log(2)));
var q=1;
var c=0,v=0;
if(n){
i.setPixelAlpha(0,0,0,0,(Math.random()<t)*255);
i.setPixelAlpha(0,p,0,0,(Math.random()<t)*255);
i.setPixelAlpha(p,0,0,0,(Math.random()<t)*255);
i.setPixelAlpha(p,p,0,0,(Math.random()<t)*255);
}
else{
i.setPixelAlpha(0,0,0,0,0);
i.setPixelAlpha(0,p,0,0,255);
i.setPixelAlpha(p,0,0,0,255);
i.setPixelAlpha(p,p,0,0,0);
}
while(p>1){
p=p/2;
s=s*2;
q=q*2;
for(x=0;x<=q;x=x+1)for(y=1;y<q;y=y+2){
if(n){if(Math.random()*s<1) c=1;
else c=0;}
else c=0;
if(c==1) i.setPixelAlpha(x*p,y*p,0,0,(Math.random()<t)*255);
else i.setPixelAlpha(x*p,y*p,0,0,i.getPixelAlpha(x*p,(y+Math.floor(Math.random()*2)*2-1)*p,0,0));
}
for(x=1;x<q;x=x+2)for(y=0;y<q+1;y=y+1){
if(n){if(Math.random()*s<1) c=1;
else c=0;}
else c=0;
if(c==1) i.setPixelAlpha(x*p,y*p,0,0,(Math.random()<t)*255);
else i.setPixelAlpha(x*p,y*p,0,0,i.getPixelAlpha((x+Math.floor(Math.random()*2)*2-1)*p,y*p,0,0));
}
for(x=1;x<q;x=x+2)for(y=1;y<q;y=y+2){
if(n){if(Math.random()*s<1) c=1;
else c=0;}
else c=0;
if(c==1) i.setPixelAlpha(x*p,y*p,0,0,(Math.random()<t)*255);
else{
v=i.getPixelAlpha((x-1)*p,y*p,0,0)+i.getPixelAlpha((x+1)*p,y*p,0,0)+i.getPixelAlpha(x*p,(y-1)*p,0,0)+i.getPixelAlpha(x*p, (y+1)*p,0,0)+i.getPixelAlpha((x-1)*p,(y-1)*p,0,0)+i.getPixelAlpha((x-1)*p,(y+1)*p,0,0)+i.getPixelAlpha((x+1)*p,(y- 1)*p,0,0)+i.getPixelAlpha((x+1)*p,(y+1)*p,0,0);
if(v>255*5-1) i.setPixelAlpha(x*p,y*p,0,0,255)
else if(v<255*3+1) i.setPixelAlpha(x*p,y*p,0,0,0)
else i.setPixelAlpha(x*p,y*p,0,0,(Math.random()<.5)*255)
}
}
}
while(r==true){
r=false;
var a=new Array();
for(x=1;x<q;x++){
a.push(new Array());
for(y=1;y<q;y++){
a[x-1].push(i.getPixelAlpha(x,y,0,0));
v=i.getPixelAlpha(x-1,y,0,0)+i.getPixelAlpha(x+1,y,0,0)+i.getPixelAlpha(x,y-1,0,0)+i.getPixelAlpha(x,y +1,0,0)+i.getPixelAlpha(x-1,y-1,0,0)+i.getPixelAlpha(x-1,y+1,0,0)+i.getPixelAlpha(x+1,y-1,0,0)+i.getPixelAlpha(x+1,y +1,0,0);
if(i.getPixelAlpha(x,y,0,0)==0 && v>255*(8-k)-1){
a[x-1][y-1]=255;
r=true;
}
if(i.getPixelAlpha(x,y,0,0)==255 && v<255*k+1){
a[x-1][y-1]=0;
r=true;
}
}
}
for(x=1;x<q;x++)for(y=1;y<q;y++) i.setPixelAlpha(x,y,0,0,a[x-1][y-1]);
if(k==0)r=false;
}
Notes: In the code
Scope: Generates a somewhat random alpha map in the largest area of 2^n+1 x 2^n+1 that can fit in the image. For example: 9x9, 17x17, 33x33, 65x65, etc.
Preview: (with more detailed explanation) imgur.com/a/ieFb5XS