Skip to content

Instantly share code, notes, and snippets.

@masyax
Last active January 5, 2017 14:45
Show Gist options
  • Select an option

  • Save masyax/cd69374d6fd945da8ce00170f7cfdf09 to your computer and use it in GitHub Desktop.

Select an option

Save masyax/cd69374d6fd945da8ce00170f7cfdf09 to your computer and use it in GitHub Desktop.
/*--------------------------------
ポスタリゼーションソフトのコードです。
processing2とprocessing3で動作確認しました。
loadImageの中身をファイル名に変えましょう。
512x512が最大サイズなのでそれ以上あげてもどうなるかわかりません。
左下の8つの棒でをクリックするとその棒が選択され、強さと色を変えることができます。
色を変えるときは右下のバーをクリックしましょう。
設定し終わったらredrawボタンを押して、加工画像を更新しましょう。
saveボタンを押せば、加工後の画像が保存されます。
--------------------------------*/
PImage p;
PImage p2;
int i, j, k;
int [] str=new int [8];
color [] col=new color [8];
int selectc=0;
void setup() {
size(1024, 612);
p=loadImage("moto.jpg");
p2=createImage(p.width,p.height,RGB);
image(p, 0, 0);
loadPixels();
for (i=0; i<8; i++) {
if(i<3)
str[i]=8;
else
str[i]=0;
col[i]=color(0,0,0);
}
col[0]=color(255,0,0);
col[1]=color(0,255,0);
col[2]=color(0,0,255);
}
boolean chcol(int a,int b,color c){//dis(c[a])>dis(b)
float disab=sq(red(col[a])-red(col[b]))+sq(green(col[a])-green(col[b]))+sq(blue(col[a])-blue(col[b]));
float disac=sq(red(col[a])-red(c))+sq(green(col[a])-green(c))+sq(blue(col[a])-blue(c));
float disbc=sq(red(col[b])-red(c))+sq(green(col[b])-green(c))+sq(blue(col[b])-blue(c));
if(disac<disbc&&disbc>disab)
return false;
if(disac>disbc&&disac>disab)
return true;
return (disac/str[a]>disbc/str[b]);
}
void reDraw() {
int ch=0;
for (i=0; i<8; i++)
ch+=str[i];
if (ch==0)
return;
loadPixels();
noStroke();
for (i=0; i<p.height; i++) {
for (j=0; j<p.width; j++) {
int sel=-1;
for (k=0; k<8; k++) {
if(str[k]>0){
if(sel==-1)
sel=k;
else if(chcol(sel,k,pixels[i*width+j]))
sel=k;
}
}
p2.pixels[i*p.width+j]=col[sel];
}
}
p2.updatePixels();
}
void draw() {
background(0);
image(p, 0, 0);
image(p2,512, 0);
for (i=0; i<8; i++) {
stroke(255);
line(32+64*i, 522, 32+64*i, 586);
noStroke();
fill(255);
ellipse(32+64*i, 586-str[i]*4, 8, 8);
if (i==selectc) {
fill(255-red(col[i]), 255-green(col[i]), 255-blue(col[i]));
ellipse(32+64*i, 600, 20, 20);
}
fill(col[i]);
ellipse(32+64*i, 600, 16, 16);
}
for (i=0; i<3; i++) {
stroke(255);
line(608+64*i, 522, 608+64*i, 586);
noStroke();
fill(255);
int c2=0;
if (i==0)c2=(int)(red(col[selectc])*64/255);
if (i==1)c2=(int)(green(col[selectc])*64/255);
if (i==2)c2=(int)(blue(col[selectc])*64/255);
ellipse(608+64*i, 586-c2, 8, 8);
fill((i==0)?255:0, (i==1)?255:0, (i==2)?255:0);
ellipse(608+i*64, 600, 16, 16);
}
stroke(255);
fill(128, 128, 256);
rect(768, 513, 254, 48);
rect(768, 563, 254, 48);
textSize(32);
fill(128, 256, 128);
text("redraw", 778, 548);
text("save", 778, 598);
if (mousePressed == true) {
if ( mouseButton == LEFT ) {
for (i=0; i<12; i++) {
if (i==8)continue;
if (16+i*64<=mouseX&&mouseX<=48+i*64) {
if (520<=mouseY&&mouseY<=586) {
if (i<8) {
selectc=i;
str[i]=(586-mouseY)/4;
}
if (i==9)
col[selectc]=color(min(255, (586-mouseY)*4), green(col[selectc]), blue(col[selectc]));
if (i==10)
col[selectc]=color(red(col[selectc]), min(255, (586-mouseY)*4), blue(col[selectc]));
if (i==11)
col[selectc]=color(red(col[selectc]), green(col[selectc]), min(255, (586-mouseY)*4));
}
}
}
}
}
}
void mousePressed() {
if ( mouseButton == LEFT ) {
if (768<=mouseX&&mouseX<=768+254&&513<=mouseY&&mouseY<=513+48)
reDraw();
if (768<=mouseX&&mouseX<=768+254&&563<=mouseY&&mouseY<=563+48) {
p2.save("editted.png");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment