Class GLImage

java.lang.Object
  |
  +--GLImage

public class GLImage
extends java.lang.Object

Loads an Image from file, stores pixels as ARGB int array, and RGBA ByteBuffer. An alternate constructor creates a GLImage from a ByteBuffer containing pixel data.

Static functions are included to load, flip and convert pixel arrays.

napier at potatoland dot org


Field Summary
static int SIZE_BYTE
           
 
Constructor Summary
GLImage()
           
GLImage(java.nio.ByteBuffer pixels, int w, int h)
          Store pixels passed in a ByteBuffer.
GLImage(java.lang.String imgName)
          Load pixels from an image file.
 
Method Summary
static java.nio.ByteBuffer allocBytes(byte[] bytearray)
          Same function as in GLApp.java.
static byte[] convertARGBtoRGBA(int[] jpixels)
          Convert pixels from java default ARGB int format to byte array in RGBA format.
static java.nio.ByteBuffer convertImagePixels(int[] jpixels, int imgw, int imgh, boolean flipVertically)
          Convert ARGB pixels to a ByteBuffer containing RGBA pixels.
Can be drawn in ORTHO mode using:
GL.glDrawPixels(imgW, imgH, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, byteBuffer);
If flipVertically is true, pixels will be flipped vertically (for OpenGL coord system).
 void flipPixels()
          Flip the image pixels vertically
static int[] flipPixels(int[] imgPixels, int imgw, int imgh)
          Flip an array of pixels vertically
static byte[] getBytesFromFile(java.io.File file)
          Given File object, returns the contents of the file as a byte array.
static byte[] getBytesFromFile(java.lang.String filename)
          Given name of file, return entire file as a byte array.
 int[] getImagePixels()
          Return the image pixels in default Java int ARGB format.
 int[] getPixelsARGB()
          return int array containing pixels in ARGB format (default Java byte order).
 java.nio.ByteBuffer getPixelsRGBA()
          return ByteBuffer containing pixels in RGBA format (commmonly used in OpenGL).
 boolean isLoaded()
          return true if image has been loaded successfully
 void loadImage(java.lang.String imgName)
          Load an image file and hold its width/height.
static java.awt.Image loadImageFromFile_ORIG(java.lang.String imgName)
           
static java.awt.Image loadImageFromFile(java.lang.String imgName)
          Load an image from file.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

SIZE_BYTE

public static final int SIZE_BYTE
See Also:
Constant Field Values
Constructor Detail

GLImage

public GLImage()

GLImage

public GLImage(java.lang.String imgName)
Load pixels from an image file.

Parameters:
imgName -

GLImage

public GLImage(java.nio.ByteBuffer pixels,
               int w,
               int h)
Store pixels passed in a ByteBuffer.

Parameters:
pixels -
w -
h -
Method Detail

isLoaded

public boolean isLoaded()
return true if image has been loaded successfully

Returns:

flipPixels

public void flipPixels()
Flip the image pixels vertically


loadImage

public void loadImage(java.lang.String imgName)
Load an image file and hold its width/height.

Parameters:
imgName -

getImagePixels

public int[] getImagePixels()
Return the image pixels in default Java int ARGB format.

Returns:

getPixelsARGB

public int[] getPixelsARGB()
return int array containing pixels in ARGB format (default Java byte order).


getPixelsRGBA

public java.nio.ByteBuffer getPixelsRGBA()
return ByteBuffer containing pixels in RGBA format (commmonly used in OpenGL).


flipPixels

public static int[] flipPixels(int[] imgPixels,
                               int imgw,
                               int imgh)
Flip an array of pixels vertically

Parameters:
imgPixels -
imgw -
imgh -
Returns:
int[]

convertImagePixels

public static java.nio.ByteBuffer convertImagePixels(int[] jpixels,
                                                     int imgw,
                                                     int imgh,
                                                     boolean flipVertically)
Convert ARGB pixels to a ByteBuffer containing RGBA pixels.
Can be drawn in ORTHO mode using:
GL.glDrawPixels(imgW, imgH, GL.GL_RGBA, GL.GL_UNSIGNED_BYTE, byteBuffer);
If flipVertically is true, pixels will be flipped vertically (for OpenGL coord system).

Returns:
ByteBuffer

convertARGBtoRGBA

public static byte[] convertARGBtoRGBA(int[] jpixels)
Convert pixels from java default ARGB int format to byte array in RGBA format.

Parameters:
jpixels -
Returns:

allocBytes

public static java.nio.ByteBuffer allocBytes(byte[] bytearray)
Same function as in GLApp.java. Allocates a ByteBuffer to hold the given array of bytes.

Parameters:
bytearray -
Returns:
ByteBuffer containing the contents of the byte array

loadImageFromFile

public static java.awt.Image loadImageFromFile(java.lang.String imgName)
Load an image from file. Avoids the flaky MediaTracker/ImageObserver headache. Assumes that the file can be loaded quickly from the local filesystem, so does not need to wait in a thread. If it can't find the file in the filesystem, will try loading from jar file. If not found will return null.

Parameters:
imgName -

loadImageFromFile_ORIG

public static java.awt.Image loadImageFromFile_ORIG(java.lang.String imgName)

getBytesFromFile

public static byte[] getBytesFromFile(java.lang.String filename)
Given name of file, return entire file as a byte array.

Parameters:
filename -
Returns:

getBytesFromFile

public static byte[] getBytesFromFile(java.io.File file)
                               throws java.io.IOException
Given File object, returns the contents of the file as a byte array.

java.io.IOException