Path: blob/master/SLICK_HOME/src/org/newdawn/slick/ImageBuffer.java
1456 views
package org.newdawn.slick;12import java.nio.ByteBuffer;3import java.nio.ByteOrder;45import org.lwjgl.BufferUtils;6import org.newdawn.slick.opengl.ImageData;78/**9* A utility for creating images from pixel operations10*11* Expected usage is:12* <code>13* ImageBuffer buffer = new ImageBuffer(320,200);14* buffer.setRGBA(100,100,50,50,20,255);15* ..16* Image image = buffer.getImage();17* </code>18*19* @author kevin20*/21public class ImageBuffer implements ImageData {22/** The width of the image */23private int width;24/** The height of the image */25private int height;26/** The width of the texture */27private int texWidth;28/** The height of the texture */29private int texHeight;30/** The raw data generated for the image */31private byte[] rawData;3233/**34*35* @param width36* @param height37*/38public ImageBuffer(int width, int height) {39this.width = width;40this.height = height;4142texWidth = get2Fold(width);43texHeight = get2Fold(height);4445rawData = new byte[texWidth * texHeight * 4];46}4748/**49* Retrieve the raw data stored within the image buffer50*51* @return The raw data in RGBA packed format from within the image buffer52*/53public byte[] getRGBA() {54return rawData;55}5657/**58* @see org.newdawn.slick.opengl.ImageData#getDepth()59*/60public int getDepth() {61return 32;62}6364/**65* @see org.newdawn.slick.opengl.ImageData#getHeight()66*/67public int getHeight() {68return height;69}7071/**72* @see org.newdawn.slick.opengl.ImageData#getTexHeight()73*/74public int getTexHeight() {75return texHeight;76}7778/**79* @see org.newdawn.slick.opengl.ImageData#getTexWidth()80*/81public int getTexWidth() {82return texWidth;83}8485/**86* @see org.newdawn.slick.opengl.ImageData#getWidth()87*/88public int getWidth() {89return width;90}9192/**93* @see org.newdawn.slick.opengl.ImageData#getImageBufferData()94*/95public ByteBuffer getImageBufferData() {96ByteBuffer scratch = BufferUtils.createByteBuffer(rawData.length);97scratch.put(rawData);98scratch.flip();99100return scratch;101}102103/**104* Set a pixel in the image buffer105*106* @param x The x position of the pixel to set107* @param y The y position of the pixel to set108* @param r The red component to set (0->255)109* @param g The green component to set (0->255)110* @param b The blue component to set (0->255)111* @param a The alpha component to set (0->255)112*/113public void setRGBA(int x, int y, int r, int g, int b, int a) {114if ((x < 0) || (x >= width) || (y < 0) || (y >= height)) {115throw new RuntimeException("Specified location: "+x+","+y+" outside of image");116}117118int ofs = ((x + (y * texWidth)) * 4);119120if (ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN) {121rawData[ofs] = (byte) r;122rawData[ofs + 1] = (byte) g;123rawData[ofs + 2] = (byte) b;124rawData[ofs + 3] = (byte) a;125} else {126rawData[ofs] = (byte) r;127rawData[ofs + 1] = (byte) g;128rawData[ofs + 2] = (byte) b;129rawData[ofs + 3] = (byte) a;130}131}132133/**134* Get an image generated based on this buffer135*136* @return The image generated from this buffer137*/138public Image getImage() {139return new Image(this);140}141142/**143* Get an image generated based on this buffer144*145* @param filter The filtering method to use when scaling this image146* @return The image generated from this buffer147*/148public Image getImage(int filter) {149return new Image(this, filter);150}151152/**153* Get the closest greater power of 2 to the fold number154*155* @param fold The target number156* @return The power of 2157*/158private int get2Fold(int fold) {159int ret = 2;160while (ret < fold) {161ret *= 2;162}163return ret;164}165166}167168169