FileDocCategorySizeDatePackage
ImageToBuffer.javaAPI DocJMF 2.1.1e3685Mon May 12 12:20:34 BST 2003javax.media.util

ImageToBuffer

public class ImageToBuffer extends Object
Utility class to convert an AWT Image object to a JMF Buffer object. The output buffer will contain the image data in an array of ints in the default ColorModel of the AWT toolkit. The getFormat method on the returned buffer will contain the exact format that the data is in, including the RGB mask values.

Fields Summary
private static ImageObserver
iobs
Constructors Summary
Methods Summary
public static javax.media.BuffercreateBuffer(java.awt.Image image, float frameRate)
Creates a JMF Buffer object for the given AWT Image and frameRate. If the frameRate parameter of the output format is not relevant to your needs, then any value can be specified as the frameRate. If you are generating live video data that is to be presented by JMF, then you should specify a reasonable value between 1 and 60 for the frameRate.

The output buffer will have unspecified values for the flags, sequenceNumber, header and timeStamp fields. Only the format, data, offset and length will contain valid values.

param
image an AWT Image that is fully prepared and has a known size.
param
frameRate the frameRate at which these buffer objects are being generated.
return
a JMF Buffer object that contains the image data in RGB format, or null if the image could not be converted succesfully.


                                                                                                                                                  
           
	int width, height, scan;
	int [] data;
	int redMask, greenMask, blueMask;
	Dimension size;
	PixelGrabber pg;
	ColorModel cm;
	DirectColorModel dcm;
	Buffer buffer;
	
	if (image == null)
	    return null;

	// Need image observer to get the image size
	if (iobs == null) {
	    iobs = new ImageObserver() {
		public boolean imageUpdate(Image im, int info,
					   int x, int y, int w, int h) {
		    return false;
		}
	    } ;
	}

	// Get the size
	width = image.getWidth(iobs);
	height = image.getHeight(iobs);
	if (width < 1 || height < 1)
	    return null;
	scan = (width + 3) & ~3;
	size = new Dimension(width, height);
	data = new int[scan * height];

	// Get the pixels into an int array
	pg = new PixelGrabber(image, 0, 0, width, height,
			      data, 0, scan);
	try {
	    pg.grabPixels();
	} catch (InterruptedException ie) {
	    return null;
	}

	cm = pg.getColorModel();
	if (!(cm instanceof DirectColorModel)) // Cant do if not DCM
	    return null;
	dcm = (DirectColorModel) cm;
	redMask = dcm.getRedMask();
	greenMask = dcm.getGreenMask();
	blueMask = dcm.getBlueMask();
	if ( (redMask | greenMask | blueMask) != 0xFFFFFF )
	    return null;

	// Format for creating the buffer
	RGBFormat rgb = new RGBFormat(size,
				      scan * height,
				      Format.intArray,
				      frameRate,
				      32,
				      redMask, greenMask, blueMask,
				      1, scan,
				      RGBFormat.FALSE,
				      Format.NOT_SPECIFIED);

	buffer = new Buffer();
	buffer.setOffset(0);
	buffer.setLength(scan * height);
	buffer.setHeader(null);
	buffer.setFlags(0);
	buffer.setData(data);
	buffer.setFormat(rgb);
	buffer.setTimeStamp(-1);
	buffer.setSequenceNumber(0);
	return buffer;