Split a TextureImage2D

Hi everyone:

I have a difficult problem to solve.
I have a Shape3D object (with his TextureImage2D and his Geometry). The TextureImage2D is loaded from an image file. I need to get a BufferedImage to each part of the Shape3D object or to each Triangle of the shape. I only can get the BufferedImage from all the image file.
May be some option to get differents images from the original image file using the texture coordinates?

Thanks


/**
 * Clips the input image to the specified shape
 * 
 * @param image
 *           the input image
 * @param clipVerts
 *           list of x, y pairs defining the clip shape, normalised
 *           to image dimensions (think texture coordinates)
 * @return An image, the same size as the input, but only containing
 *         those pixels that fall inside the clip shape
 */
public static BufferedImage clip( BufferedImage image, float... clipVerts )
{
	assert clipVerts.length >= 6;
	assert clipVerts.length % 2 == 0;

	int[] xp = new int[ clipVerts.length / 2 ];
	int[] yp = new int[ xp.length ];

	for( int j = 0; j < xp.length; j++ )
	{
		xp[ j ] = Math.round( clipVerts[ 2 * j ] * image.getWidth() );
		yp[ j ] = Math.round( clipVerts[ 2 * j + 1 ] * image.getHeight() );
	}

	Polygon clip = new Polygon( xp, yp, xp.length );
	BufferedImage out = new BufferedImage( image.getWidth(), image.getHeight(), image.getType() );
	Graphics g = out.getGraphics();
	g.setClip( clip );

	g.drawImage( image, 0, 0, null );
	g.dispose();

	return out;
}


BufferedImage bi = ImageIO.read( new File( "kitten.jpg" ) );
BufferedImage clipped = clip( bi, 0.2f, 0.2f, 0.05f, 0.8f, 0.6f, 0.6f );
ImageIO.write( clipped, "PNG", new File( "clipped.png" ) );

Thanks a lot for the answer. I don´t have clear one thing. In this example the object BufferedImage out what you return has all image or only the part that you clipped. If it is only the part that you clipped it is what I need.

Thanks again.


/**
 * Clips the input image to the specified shape
 * 
 * @param image
 *           the input image
 * @param clipVerts
 *           list of x, y pairs defining the clip shape, normalised
 *           to image dimensions (think texture coordinates)
 * @return The smallest image containing those pixels that fall
 *         inside the clip shape
 */
public static BufferedImage clip( BufferedImage image, float... clipVerts )
{
	assert clipVerts.length >= 6;
	assert clipVerts.length % 2 == 0;

	int[] xp = new int[ clipVerts.length / 2 ];
	int[] yp = new int[ xp.length ];
	int minX = image.getWidth(), minY = image.getHeight(), maxX = 0, maxY = 0;

	for( int j = 0; j < xp.length; j++ )
	{
		xp[ j ] = Math.round( clipVerts[ 2 * j ] * image.getWidth() );
		yp[ j ] = Math.round( clipVerts[ 2 * j + 1 ] * image.getHeight() );

		minX = Math.min( minX, xp[ j ] );
		minY = Math.min( minY, yp[ j ] );
		maxX = Math.max( maxX, xp[ j ] );
		maxY = Math.max( maxY, yp[ j ] );
	}

	for( int i = 0; i < xp.length; i++ )
	{
		xp[ i ] -= minX;
		yp[ i ] -= minY;
	}

	Polygon clip = new Polygon( xp, yp, xp.length );
	BufferedImage out = new BufferedImage( maxX - minX, maxY - minY, image.getType() );
	Graphics g = out.getGraphics();
	g.setClip( clip );

	g.drawImage( image, -minX, -minY, null );
	g.dispose();

	return out;
}

Thanks again. I´m going to try it ;).