Created
September 26, 2010 20:08
-
-
Save nicolasH/598278 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* How to create a BufferedImage from a sub rectangle of another BufferedImage, a.k.a. 'clipping'. | |
* inspired by : | |
* http://stackoverflow.com/questions/2825837/java-how-to-do-fast-copy-of-a-bufferedimages-pixels-unit-test-included | |
* | |
* Does an arraycopy of the relevant part of the rasters. | |
* | |
*/ | |
BufferedImage src = ImageIO.read(new File("SomeFile.png")); | |
Rectangle clip = new Rectangle((src.getWidth() - 200) / 2, (src.getHeight() - 200) / 2, 200, 200); | |
BufferedImage dst = new BufferedImage(clip.width, clip.height, src.getType()); | |
Object srcbuf = null; | |
Object dstbuf = null; | |
int mpx = src.getWidth() * src.getHeight(); | |
int factor = 1; | |
DataBuffer buff = src.getRaster().getDataBuffer(); | |
/** | |
* Different type of image have different type of underlying buffer. Each type has a different number of cells | |
* dedicated to a single pixel. | |
*/ | |
if (buff instanceof DataBufferByte) { | |
srcbuf = ((DataBufferByte) buff).getData(); | |
dstbuf = ((DataBufferByte) dst.getRaster().getDataBuffer()).getData(); | |
factor = ((DataBufferByte) buff).getData().length / mpx; | |
} | |
if (buff instanceof DataBufferDouble) { | |
srcbuf = ((DataBufferDouble) buff).getData(); | |
dstbuf = ((DataBufferDouble) dst.getRaster().getDataBuffer()).getData(); | |
factor = ((DataBufferDouble) buff).getData().length / mpx; | |
} | |
if (buff instanceof DataBufferFloat) { | |
srcbuf = ((DataBufferFloat) buff).getData(); | |
dstbuf = ((DataBufferFloat) dst.getRaster().getDataBuffer()).getData(); | |
factor = ((DataBufferFloat) buff).getData().length / mpx; | |
} | |
if (buff instanceof DataBufferInt) { | |
srcbuf = ((DataBufferInt) buff).getData(); | |
dstbuf = ((DataBufferInt) dst.getRaster().getDataBuffer()).getData(); | |
factor = ((DataBufferInt) buff).getData().length / mpx; | |
} | |
if (buff instanceof DataBufferShort) { | |
srcbuf = ((DataBufferShort) buff).getData(); | |
dstbuf = ((DataBufferShort) dst.getRaster().getDataBuffer()).getData(); | |
factor = ((DataBufferShort) buff).getData().length / mpx; | |
} | |
if (buff instanceof DataBufferUShort) { | |
srcbuf = ((DataBufferUShort) buff).getData(); | |
dstbuf = ((DataBufferUShort) dst.getRaster().getDataBuffer()).getData(); | |
factor = ((DataBufferUShort) buff).getData().length / mpx; | |
} | |
int srcOffset = src.getWidth() * clip.y * factor + clip.x * factor; | |
int dstOffset = 0; | |
final int dstLineOffset = clip.width * factor; | |
final int srcLineOffset = src.getWidth() * factor; | |
/** Actually copying the pixel data from src to dst.*/ | |
for (int y = 0; y < clip.height; y++) { | |
System.arraycopy(srcbuf, srcOffset, dstbuf, dstOffset, clip.width * factor); | |
srcOffset += srcLineOffset; | |
dstOffset += dstLineOffset; | |
} | |
//done ! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment