// Image Processing Ws // Zsofia Ruttkay - ruttkay@mome.hu // Arranging an image in shuffled squares PImage flower1, mixed; // 160 * 4 = 640 // 160 * 3 = 480 int imgScale = 160; // Number of columns and rows in our system int cols, rows; int[] startLoc = { 0, 2, 10, 5, 8, 4, 3, 7, 9, 1, 6, 11 }; int[] endLoc = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; // these are the variables to interpret gesture int sx, sy; // where the mouse is pressed int fx=0, fy=0; // the free square left upper corner void setup() { size(640,480); // Make a new instance of a PImage by loading an image file flower1 = loadImage("narcis.jpg"); flower1.resize( 640, 480); mixed = createImage(width,height, RGB); // Initialize columns and rows cols = width/imgScale; rows = height/imgScale; } void draw() { // flower1.loadPixels(); // Begin loop for rows for (int i = 0; i < rows; i++) { // Begin loop for columns for (int j = 0; j < cols; j++) { // Where are we, pixel-wise, in the original image? int x = j*imgScale; int y = i*imgScale; // where the cut out rectangle will be shown int dx = (startLoc[j +i*cols ]%cols)*imgScale; int dy = ((startLoc[j +i*cols]-startLoc[j +i*cols ]%cols)/rows)*imgScale; mixed.copy(flower1, x, y, width/cols, height/rows, dx,dy, width/cols, height/rows); } } image(mixed, 0, 0, width, height); fill(0); rect(fx, fy, width/cols, height/rows); } void mousePressed() { sx = mouseX; sy= mouseY; } void mouseReleased() { if (inEmpty(mouseX, mouseY) && neighbour(sx, sy)){ shuffle(mouseX, mouseY); fx = compx(sx, sy); fy = compy(sx, sy); } //else println("sx="+sx+"sy="+sy+"mouseX="+mouseX+"mouseY="+ mouseY); } boolean inEmpty(int xx, int yy) { boolean retv = (xx>= fx && xx <= fx + imgScale && // x dir yy>= fy && yy <= fy + imgScale); // y dir //println( xx+" "+yy+" inEmpty="+ retv); return retv; } boolean neighbour(int xx, int yy) { boolean retv = (compx(xx, yy) == compx(fx, fy) && abs(compy(xx, yy) - compy(fx, fy)) == imgScale) || // same col (compy (xx, yy) == compy (fx, fy) && abs(compx(xx, yy) - compx(fx, fy)) == imgScale); // same row // println("Neighbourgh="+retv); return retv; } int compx(int xx, int yy) { int newx = xx-xx%imgScale; return newx; } int compy(int xx, int yy) { int newy = yy-yy%imgScale; return newy; } void shuffle(int x, int y) { mixed.copy(mixed, compx(x, y), compy(x,y), width/cols, height/rows, fx,fy, width/cols, height/rows); image(mixed, 0, 0); }