PImage bgImg; PImage paintImg; void setup() { size(500, 340); //load the background image bgImg = loadImage("letter.jpg"); //load the brush image paintImg = loadImage("stamp.png"); //load a font, set as current, change alignment PFont font = loadFont("Helvetica-Bold-12.vlw"); textFont(font, 12); textAlign(CENTER); //switch off the mouse cursor noCursor(); } void draw() { //draw the bgImg image(bgImg, 0, 0); //write some black text to mouse position fill(0); text("stampIt!",mouseX, mouseY); } void mouseReleased() { //paste the paintImg on bgImg pasteImage(bgImg, paintImg, mouseX-paintImg.width/2, mouseY-paintImg.height/2); } /** * pastes one image into an other one * the images may have alpha! * * paramters: * canvas: the PImage in the background * brush: the PImage in the front (the one you want to paste in) * x: xPosition (upper left corner of brush) * y: yPosition (upper left corner of brush) **/ void pasteImage(PImage canvas, PImage brush, int x, int y) { //loading the pixels arrays of the input images canvas.loadPixels(); brush.loadPixels(); //cycle through the pixels of the brush for (int iB=0; iB < brush.pixels.length; iB++) { //current x and y pixel of the brush image int xB, yB; xB = (iB % brush.width) +x; //continue with the NEXT cycle of the loop //if this pixel is right or left of the canvas image if (xB >= canvas.width || xB < 0) continue; yB = (iB / brush.width) +y; int iC = xB +yB*canvas.width; //continue with the NEXT cycle of the loop //if the index iC is not in canvas.pixels[] if (iC >= canvas.pixels.length || iC < 0) continue; //current color of pixel in brush image color cB = brush.pixels[iB]; //current color of pixel in canvas image color cC = canvas.pixels[iC]; //blend the to colors and overwrite the pixel in canvas canvas.pixels[iC] = blendColor(canvas.pixels[iC], brush.pixels[iB], BLEND); } //always update to change the image for display canvas.updatePixels(); }