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.
IndexProcessing DevelopmentCore,  Processing Development Environment (PDE) › PImage from java.awt.BufferedImage error
Page Index Toggle Pages: 1
PImage from java.awt.BufferedImage error (Read 3158 times)
PImage from java.awt.BufferedImage error
Jun 1st, 2009, 1:36pm
 
Hi,
prior to Processing 1 I was used to build a PImage from a ByteArrayInputStream in this way:

Code:

PImage tmp = tmp;
tmp = new PImage((BufferedImage)ImageIO.read(
          new ByteArrayInputStream(img)));



Now I've noticed errors in my library with Processing 1.0.3 so I investigated and I found in PImage.java

Code:

public PImage(java.awt.Image img) {
      if (img instanceof BufferedImage) {
        BufferedImage bi = (BufferedImage) img;
        width = bi.getWidth();
        height = bi.getHeight();
        pixels = new int[width * height];
        WritableRaster raster = bi.getRaster();
        raster.getDataElements(0, 0, width, height, pixels);


The last line causes for me a

java.lang.ClassCastException: [I
     at sun.awt.image.ByteInterleavedRaster.getDataElements(ByteInterleavedRaster.java:3
47)
     at processing.core.PImage.<init>(PImage.java:182)
     at it.lilik.capturemjpeg.CaptureMJPEG.run(CaptureMJPEG.java:317)



There's some error in this new implementation or it's my fault?

Now I've fixed forcing the old behavior

Code:

PImage tmp = null;
BufferedImage buffI = (BufferedImage)ImageIO.read(
new ByteArrayInputStream(img));

/*
* Bypass Default PImage costructur forceing
* a load like in Processing < 1.0
*
*/

//tmp = new PImage((BufferedImage)ImageIO.read(
//     new ByteArrayInputStream(img)));
BufferedImage bi = (BufferedImage) buffI;

int width = bi.getWidth(null);
int height = bi.getHeight(null);
int[] pixels = new int[width * height];
PixelGrabber pg =
   new PixelGrabber(bi, 0, 0, width, height, pixels, 0, width);
try {
   pg.grabPixels();
} catch (InterruptedException e) { }

tmp = new PImage(width,height);
tmp.loadPixels();
for (int i = 0; i < width * height; i++) {
   tmp.pixels[i] = pixels[i];
}
tmp.updatePixels();


Re: PImage from java.awt.BufferedImage error
Reply #1 - Aug 13th, 2009, 6:12am
 
If you can post an example and the code in the bugs database, I can take a look.
Page Index Toggle Pages: 1