Files
GenerateMe/mirrorimage/mirrorimage.pde
2015-03-26 00:07:03 +01:00

201 lines
5.2 KiB
Plaintext
Executable File

/* Mirror image in several ways, works well on rectangular images
* there are 24 modes for rectangular and 12 in square
* Author: Tomasz Sulej, generateme.blog@gmail.com, 2015, <http://unlicense.org>
* Thanks Gus Holiday for inspiration <https://www.facebook.com/gus.graphics>
*/
// Configure
// - set image filename
// - put list of actions
// Actions (hard to explain, just experiment):
// UL - mirror upper left triangle onto down right
// UR - ^ upper right -> down left
// DL, DR - similar
// L - mirror left part to right
// R, U, D - similar (right, up, down)
// version 2 - copy (flip and flop)
// S* - versions for rectangle, choose the right or down side of the image
// R* - use rectangles diagonals to copy
// Usage:
// click to do one more actions on current result
// ENTER/RETURN to reset
// SPACE to save
// filename here:
String filename = "test.jpg";
// actions here (comma separated in curly braces):
int[] order = { UL, L, DL };
// do you want order of operations choosen by machine? If true, script ignores what you set up
boolean automatic = true;
// mirror
final static int UL = 0;
final static int UR = 1;
final static int DL = 2;
final static int DR = 3;
final static int U = 4;
final static int D = 5;
final static int L = 6;
final static int R = 7;
// mirror 2
final static int UL2 = 8;
final static int UR2 = 9;
final static int DL2 = 10;
final static int DR2 = 11;
// shift mirror
final static int SUL = 12;
final static int SUR = 13;
final static int SDL = 14;
final static int SDR = 15;
// shift mirror2
final static int SUL2 = 16;
final static int SUR2 = 17;
final static int SDL2 = 18;
final static int SDR2 = 19;
// rectangle mirror2
final static int RUL = 20;
final static int RUR = 21;
final static int RDL = 22;
final static int RDR = 23;
int size;
int tx,ty;
PImage img;
void setup() { //<>//
img = loadImage(filename);
size(img.width,img.height);
image(img,0,0);
noStroke();
size = width<height?width:height;
tx=ty=0;
if(width>size) tx = width-size;
if(height>size) ty = height-size;
}
void draw() {}
void mouseClicked() {
if(automatic) {
int n = (int)random(1,16);
order = new int[n];
for(int i=0;i<n;i++) order[i] = (int)random(24);
}
for(int i=0;i<order.length;i++) doMirror(order[i]);
}
void keyPressed() {
if(keyCode == ENTER || keyCode == RETURN) image(img,0,0);
if(keyCode == 32) saveFrame("res_"+(int)random(10000,99999)+"_"+filename);
}
void doMirror(int type) {
switch(type) {
case U: doHorizontal(true); break;
case D: doHorizontal(false); break;
case L: doVertical(true); break;
case R: doVertical(false); break;
case DL: doDiagUL(0,false); break;
case UR: doDiagUL(1,false); break;
case DR: doDiagUR(0,false); break;
case UL: doDiagUR(1,false); break;
case DL2: doDiagUL(2,false); break;
case UR2: doDiagUL(3,false); break;
case DR2: doDiagUR(2,false); break;
case UL2: doDiagUR(3,false); break;
case SDL: doDiagUL(0,true); break;
case SUR: doDiagUL(1,true); break;
case SDR: doDiagUR(0,true); break;
case SUL: doDiagUR(1,true); break;
case SDL2: doDiagUL(2,true); break;
case SUR2: doDiagUL(3,true); break;
case SDR2: doDiagUR(2,true); break;
case SUL2: doDiagUR(3,true); break;
case RDL: doDiagLRect(true); break;
case RUL: doDiagLRect(false); break;
case RUR: doDiagRRect(true); break;
case RDR: doDiagRRect(false); break;
}
}
void doDiagUL(int type, boolean shift) {
for(int y=0;y<size;y++)
for(int x=0;x<=y;x++)
switch(type) {
case 0: drawPoint(x,y,y,x,shift); break;
case 1: drawPoint(y,x,x,y,shift); break;
case 2: drawPoint(x,y,size-x-1,size-y-1,shift); break;
case 3: drawPoint(y,x,size-y-1,size-x-1,shift); break;
default: break;
}
}
void doDiagUR(int type, boolean shift) {
for(int y=0;y<size;y++)
for(int x=size-1;x>=size-y-1;x--)
switch(type) {
case 0: drawPoint(x,y,size-y-1,size-x-1,shift); break;
case 1: drawPoint(size-y-1,size-x-1,x,y,shift); break;
case 2: drawPoint(x,y,size-x-1,size-y-1,shift); break;
case 3: drawPoint(size-x-1,size-y-1,x,y,shift); break;
default: break;
}
}
void doHorizontal(boolean type) {
for(int y=0;y<height/2;y++)
for(int x=0;x<width;x++)
if(type) drawPoint(x,y,x,height-y-1,false);
else drawPoint(x,height-y-1,x,y,false);
}
void doVertical(boolean type) {
for(int x=0;x<width/2;x++)
for(int y=0;y<height;y++)
if(type) drawPoint(x,y,width-x-1,y,false);
else drawPoint(width-x-1,y,x,y,false);
}
void doDiagLRect(boolean m) {
for(int y=0;y<height;y++) {
int d = m?(int)map(y,0,height,0,width):(int)map(y,0,height-1,width-1,0);
for(int x=0;x<d;x++) {
fill(get(x,y));
rect(width-x-1,height-y-1,1,1);
}
}
}
void doDiagRRect(boolean m) {
for(int y=0;y<height;y++) {
int d = m?(int)map(y,0,height,0,width):(int)map(y,0,height-1,width-1,0);
for(int x=0;x<d;x++) {
fill(get(width-x-1,height-y-1));
rect(x,y,1,1);
}
}
}
void drawPoint(int oldx, int oldy, int newx, int newy, boolean shift) {
int sx=0;
int sy=0;
if(shift) { sx = tx; sy = ty; } // fucking rectangles...
fill(get(oldx+sx,oldy+sy));
rect(newx+sx,newy+sy,1,1);
}