We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › pasting an Image --> Code + Q
Page Index Toggle Pages: 1
pasting an Image --> Code + Q (Read 482 times)
pasting an Image --> Code + Q
Oct 16th, 2006, 12:49am
 
Looking through PImage  docs I saw that set() method is generally considered slower for a paste operation then then dealing with pixel arrays. So for a bit of a personal excersise-type-thing I wrote the folowing:

<code>
//Simple "paste" operation
//Open up an image file --> canvas and then bring in another image file to be placed or
//"pasted" at specific (x,y) coordinates.

PImage canvasImage, pasteImage;

void generateCanvas(int img_width, int img_height)
{
 canvasImage=createImage(img_width,img_height,RGB);
 println("generated canvas image:"+ img_width+ " by "+ img_height);
}

void saveCanvas(String imgName)
{
 canvasImage.save(imgName);  
 println("saved out canvas image");
}

void pasteImage(String imgName, int x_offset, int y_offset)
{
 int x;          //horizontal
 int y;          //vertical
 
 int H_LIMIT, V_LIMIT;
 
 pasteImage=loadImage(imgName);

 //---------------
 //Figure out horizontal and vertical limits of the operation

 if((pasteImage.height+y_offset)>canvasImage.height)
 {
   V_LIMIT=canvasImage.height;
 }
 else
 {
   V_LIMIT=pasteImage.height+y_offset;
 }
 
 if((pasteImage.width+x_offset)>canvasImage.width)
 {
   H_LIMIT=canvasImage.width;
 }
 else
 {
   H_LIMIT=pasteImage.width+x_offset;
 }
 
 //---------------  
 
 y=0;
 
 for(;y_offset<V_LIMIT;y_offset++)
 {
   for(x=x_offset;x<H_LIMIT;x++)
   {
    canvasImage.pixels[y_offset*canvasImage.width+x]=pasteImage.pixels[pasteImage.width*y+x-(x_offset)];
   }
   y++;
 }
 
}

void setup()
{
 generateCanvas(500,500);
 
 pasteImage("img_cat.jpg",70,450);
 pasteImage("img_cat.jpg",50,50);  
 
 saveCanvas("test.png");
}

</code>

I haven't looked under the hood so to speak since I don't know much about Java. For those in the know, is this code a considerable time saver compared to set()?


Page Index Toggle Pages: 1