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 › BufferedImage > PImage
Page Index Toggle Pages: 1
BufferedImage > PImage (Read 1341 times)
BufferedImage > PImage
Jul 21st, 2006, 8:15am
 
Is there an easy way to convert a java.awt.image.BufferedImage or Raster object into PImage?

Thanks!
Re: BufferedImage > PImage
Reply #1 - Jul 21st, 2006, 9:40am
 
Try this:

raster.getPixels(0, 0, pimage.width, pimage.height, pimg.pixels);
Re: BufferedImage > PImage
Reply #2 - Jul 23rd, 2006, 7:22am
 
I can't seem to get this to work (getting a ArrayIndexOutofBoundsException)...

I suspect it's because the SampleModel is not compatible - Any suggestions?
Re: BufferedImage > PImage
Reply #3 - Jul 24th, 2006, 4:25pm
 
Have you allocated the pixels array?
I mean this:

PImage pimage = new PImage();
pimage.width = width;
pimage.height = height;
pimage.pixels = new int[width * height];

or just

PImage pimage = new PImage(width, height);

where width and height are the dimensions of your raster.


I don't think there can be problems with the sample model. If you pass an array of ints to getPixels, it just fills it with ints, an element for each element of the Raster. The same thing with an array of floats or doubles. If the dimensions of the arrays correspond the colors can be altered but there shouldn't be exceptions.
Re: BufferedImage > PImage
Reply #4 - Jul 28th, 2006, 10:29am
 
It's probably something crazy I did, but... I tried this and still getting an ArrayIndexOutOfBound exception.

------------
//(src is a WritableRaster)

PImage pimage = new PImage();
pimage.width = src.getWidth();
pimage.height = src.getHeight();
pimage.pixels = new int[src.getWidth() * src.getWidth()];
       
loadPixels();
pimage.pixels = src.getPixels(0, 0, pimage.width, pimage.height, pimage.pixels);
updatePixels();
------------

If I do something like this, it would work (but probably not very efficient)...

-----------------
for (int i=0; i<src.getWidth(); i++ ) {
    for (int j=0; j<src.getHeight(); j++ ) {

               int sum = 0;
               int numBands = src.getNumBands();
               int[] c = new int[numBands];
               for (int k=0; k<numBands; k++) {
                   int p = src.getSample(i,j, k);
                   c[k] = p;
               }

               img.set(i, j, color( c[0], c[1], c[2] ));
     }
}
------------------
Re: BufferedImage > PImage
Reply #5 - Jul 28th, 2006, 3:03pm
 
PImage pimage = new PImage(src);
should be all you need.
Page Index Toggle Pages: 1