Skip to main content

[JAVA2D] BufferedImage to Image and problems with PixelGrabber

1 reply [Last post]
Anonymous

We are attempting to convert a BufferedImage, to a standard Image. In
the code below, mSecondImage is a BufferedImage and imagea is an Image:

imagea = toImage(mSecondImage);

This calls the toImage method, which is

public static Image toImage(BufferedImage bufferedImage){
return Toolkit.getDefaultToolkit().createImage
(bufferedImage.getSource());
}

The toImage appears to complete the conversion, as we have previously
successfully written the resulting image imagea to jpg files.

The problem comes when we try to perform an operation on the pixel
array by getting the pixels with PixelGrabber. W do this with a
call to linearizeImage

Image mSecondImageNotBuffered = linearizeImage(imagea, abVector);

linearizeImage is as follows:

private Image linearizeImage(Image i, Vector abVector)
{

try {

int width = i.getWidth(this);
int height = i.getHeight(this);
int pixels[] = new int [width * height];
PixelGrabber pg = new PixelGrabber (i, 0, 0, width, height,
pixels, 0, width);
if (pg.grabPixels() && ((pg.status() & ImageObserver.ALLBITS) !=0))
{
i = createImage (new MemoryImageSource (width, height,
linearizePixels
(pixels, width, height, abVector), 0, width));
}
else
{
System.out.println("Problem with pg.status()?" + pg.status() + " " +
ImageObserver.ALLBITS);
}
} catch (InterruptedException e)
{
e.printStackTrace();
}
return i;//return changed image
}

The height and width are coming back as valid numbers, as we've
succesfully
printed them. However, in the PixelGrabber function, pixels seems to
be getting filled with 0's. This may or
may not be the cause of our main error

Problem with pg.status()=27

From what I understand, pg.status() returns the bitwise OR of all
the status
flags of the pixel grabber, and thus the following bit string 0001
1011 (27).
32 is the flag for ImageObserver.ALLBITS and corresponds to the
bitstring 0010
0000. Therefore 0001 1011 & 0010 0000 = 0000 0000, presenting us with a
problem. I guess we need the ALLBITS flag to be set in order for
this to pass.
Any clue as to what is causing this behavior and possible solutions?

Thanks,
Tom

Thomas Busey, PhD
Associate Professor
Department of Psychology and Program in Cognitive Science
Indiana University, Bloomington
1101 E. 10th St
Bloomington, IN, 47405
(812) 855-4261
busey@indiana.edu
www.indiana.edu/~busey
AIM / IChat AV: tombusey@mac.com (video feed)

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Kevin Weiner

BufferedImage is already an Image. No need to convert it.

If the BufferedImage is one of the standard types based on a linear
pixel array, it's much easier to work on it directly instead of dealing
with all that MIP nonsense from a bygone era.

Thomas Busey wrote:

>
> We are attempting to convert a BufferedImage, to a standard Image. In
> the code below, mSecondImage is a BufferedImage and imagea is an Image:
>
---------------------------------------------------------------------
Kevin Weiner FM Software 610-997-3930 http://www.fmsware.com

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".