glapp
Class GLImage

java.lang.Object
  extended by glapp.GLImage

public class GLImage
extends java.lang.Object

Loads an image from file, stores pixels as ARGB int array, and RGBA ByteBuffer for use in OpenGL. Can convert images to power-of-2 dimensions for textures.

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

napier at potatoland dot org


Field Summary
 int h
           
 java.nio.ByteBuffer pixelBuffer
           
 int[] pixels
           
 int textureH
           
 int textureHandle
           
 int textureW
           
 int w
           
 
Constructor Summary
GLImage()
           
GLImage(java.nio.ByteBuffer gl_pixels, int w, int h)
          Create GLImage from 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 convertImagePixelsARGB(int[] jpixels, int imgw, int imgh, boolean flipVertically)
          Copy ARGB pixels to a ByteBuffer without changing the ARGB byte order.
static java.nio.ByteBuffer convertImagePixelsRGBA(int[] jpixels, int imgw, int imgh, boolean flipVertically)
          Convert ARGB pixels to a ByteBuffer containing RGBA pixels.
 void convertToPowerOf2()
          Scale this GLImage so width and height are powers of 2.
static java.awt.image.BufferedImage convertToPowerOf2(java.awt.image.BufferedImage bsrc)
          Scale the given BufferedImage to width and height that are powers of two.
 void flipPixels()
          Flip the image pixels vertically
static int[] flipPixels(int[] imgPixels, int imgw, int imgh)
          Flip an array of pixels vertically
static java.awt.image.BufferedImage flipY(java.awt.image.BufferedImage bsrc)
          Flip the given BufferedImage vertically.
static int[] getImagePixels(java.awt.Image image)
          Return the Image pixels in default Java int ARGB format.
 java.nio.ByteBuffer getPixelBytes()
          return ByteBuffer containing pixels in RGBA format (commmonly used in OpenGL).
 int[] getPixelInts()
          return int array containing pixels in ARGB format (default Java byte order).
 boolean isLoaded()
          return true if image has been loaded successfully
 void loadImage(java.lang.String imgName)
          Load an image from the given filename.
static void savePixelsToPNG(int[] pixels, int width, int height, java.lang.String imageFilename, boolean flipY)
          Save an array of ARGB pixels to a PNG file.
static java.awt.image.BufferedImage scale(java.awt.image.BufferedImage bsrc, int width, int height)
          Scale the given BufferedImage to the given width and height.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

h

public int h

w

public int w

pixelBuffer

public java.nio.ByteBuffer pixelBuffer

pixels

public int[] pixels

textureHandle

public int textureHandle

textureW

public int textureW

textureH

public int textureH
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 gl_pixels,
               int w,
               int h)
Create GLImage from 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 from the given filename. Store pixels as ARGB ints in the pixels array and as RGBA bytes in the pixelBuffer ByteBuffer. Hold onto image width/height.

Parameters:
imgName -

getImagePixels

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

Returns:

getPixelInts

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


getPixelBytes

public java.nio.ByteBuffer getPixelBytes()
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[]

convertImagePixelsARGB

public static java.nio.ByteBuffer convertImagePixelsARGB(int[] jpixels,
                                                         int imgw,
                                                         int imgh,
                                                         boolean flipVertically)
Copy ARGB pixels to a ByteBuffer without changing the ARGB byte order. If used to make a texture, the pixel format is GL12.GL_BGRA. With this format we can leave pixels in ARGB order (faster), but unfortunately I had problems building mipmaps in BGRA format (GLU.gluBuild2DMipmaps() did not recognize GL_UNSIGNED_INT_8_8_8_8 and GL_UNSIGNED_INT_8_8_8_8_REV types so screwed up the BGRA/ARGB byte order on Mac).

Returns:
ByteBuffer

convertImagePixelsRGBA

public static java.nio.ByteBuffer convertImagePixelsRGBA(int[] jpixels,
                                                         int imgw,
                                                         int imgh,
                                                         boolean flipVertically)
Convert ARGB pixels to a ByteBuffer containing RGBA pixels. The GL_RGBA format is a default format used in OpenGL 1.0, but requires that we move the Alpha byte for each pixel in the image (slow). Would be better to use OpenGL 1.2 GL_BGRA format and leave pixels in the ARGB format (faster) but this pixel format caused problems when creating mipmaps (see note above). .

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

convertToPowerOf2

public void convertToPowerOf2()
Scale this GLImage so width and height are powers of 2. Recreate pixels and pixelBuffer.


savePixelsToPNG

public static void savePixelsToPNG(int[] pixels,
                                   int width,
                                   int height,
                                   java.lang.String imageFilename,
                                   boolean flipY)
Save an array of ARGB pixels to a PNG file. If flipY is true, flip the pixels on the Y axis before saving.


convertToPowerOf2

public static java.awt.image.BufferedImage convertToPowerOf2(java.awt.image.BufferedImage bsrc)
Scale the given BufferedImage to width and height that are powers of two. Return the new scaled BufferedImage.


scale

public static java.awt.image.BufferedImage scale(java.awt.image.BufferedImage bsrc,
                                                 int width,
                                                 int height)
Scale the given BufferedImage to the given width and height. Return the new scaled BufferedImage.


flipY

public static java.awt.image.BufferedImage flipY(java.awt.image.BufferedImage bsrc)
Flip the given BufferedImage vertically. Return the new flipped BufferedImage.