game.graphics.image
Class CImageProcessor

java.lang.Object
  extended by game.graphics.image.CImageProcessor
Direct Known Subclasses:
CBitmapFont

public class CImageProcessor
extends java.lang.Object

Gathers standard operations on BufferedImages and pixel operations, such as zoom or bluring.
Any affine transformation matrix (zoom, rotation) or convolve operation (gaussian blur, edge detect) can be applied with provided algos and functions.

Author:
seb
See Also:
CAffineTransform

Field Summary
static int A_MASK
           
static int A_OFFSET
           
static int B_MASK
           
static int B_OFFSET
           
static int BYTE_MASK
           
static int G_MASK
           
static int G_OFFSET
           
 int H
           
protected  int[] m_aBasePixels
           
private  int[] m_aProcessedPixels
           
protected  java.awt.image.BufferedImage m_bufferedImage
           
private  int m_param
           
private  int NB_PIXELS
           
static int R_MASK
           
static int R_OFFSET
           
static int RGB_MASK
           
protected  int SCAN_SIZE
           
 int W
           
 
Constructor Summary
CImageProcessor(CImageProcessor rhs)
          Copy a CImageProcessor (image data is copied too)
CImageProcessor(java.awt.Image img)
          Builds a CImageProcessor from an Image
CImageProcessor(int w, int h)
          Builds a blank CImageProcessor.
CImageProcessor(int w, int h, boolean bCompatible)
           
 
Method Summary
 void affineTransform(CAffineTransform transform)
          Applies an affine transformation (such as zoom or rotation) to the whole image.
 void affineTransformAA(CAffineTransform transform)
          The resulting image is 4x4 anti-aliased.
 void colorAdd(java.awt.Color color, boolean bSavePixels)
          adds a color value to each pixel.
 void colorFilter(java.awt.Color color, boolean bSavePixels)
          Applies a color filter to the image.
private  int colorLerp(int fpX, int color1, int color2)
          Linear interpolation between two colors.
 void convolve(int[][] fpKernel)
          Does a convolve operation given a kernel, be it a gaussian blur or edge enhacement.
static java.awt.image.BufferedImage createARGBImage(int w, int h)
          Creates a translucent BufferedImage can be optimally processed as an int[].
static java.awt.image.BufferedImage createCompatibleImage(int w, int h)
          Creates a translucent BufferedImage that can be optimally blitted to the screen.
 void drawImage(java.awt.Graphics g, int x, int y)
          Draws the image to a Graphics.
 void fourCornersGradient(int topLeftColor, int topRightColor, int bottomRightColor, int bottomLeftColor)
          Generates a gradient interpolated from four colors (one at each corner) covering the whole image.
protected  int getA(int argb)
           
protected static int getARGB(int a, int r, int g, int b)
           
protected  int getB(int argb)
           
static int[][] getBlurKernel()
          Returns a 3x3 blur kernel.
 java.awt.image.BufferedImage getBufferedImage()
           
static int[][] getEdgeKernel()
           
static int[][] getEmbossKernel()
           
protected  int getG(int argb)
           
static int[][] getGaussianBlurKernel()
          Returns a 3x3 gaussian blur kernel.
 java.awt.Graphics getGraphics()
          Allows standard AWT drawing on the buffer.
protected  int[] getImageOffsets()
          Reads the first line of the contained image to determine subimages offsets
protected  int getR(int argb)
           
static int[][] getSharpenKernel()
           
private  void initPixelArrays()
          Saves initial pixels.
 void interleave(CImageProcessor imgProcessor, int[][] mask)
           
 void invert()
          Video inversion of the image.
 void mul(java.awt.Color mulColor)
           
 void mul(int iR, int iG, int iB)
           
 void mul(int iA, int iR, int iG, int iB)
           
 void restablishTransparency()
           
 void rotate(float alpha)
          Comodity function, performs a rotation from the center of the image.
 void rotate(float alpha, float x, float y)
          Rotates the image.
 int safeComponent(int component)
           
 void saveCurrentPixels()
          Saves the current pixels for later use (image restoring or other)
 void scrollH(float dx)
           
 void setAlpha(int alpha)
          Modifies the alpha value of the pixels
 java.awt.image.BufferedImage[] split()
          Splits the contained image in subimmages according to markers contained in the first line.
protected  java.awt.image.BufferedImage[] split(int[] offsets)
          /** Splits the contained image in subimmages according to an array of offsets.
 void wave(float f, float t)
           
 void zoom(float lambda)
          Comodity function, performs a zoom from the center of the image.
 void zoom(float lambda, float x, float y)
          Zooms the image in or out.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

A_OFFSET

public static final int A_OFFSET
See Also:
Constant Field Values

R_OFFSET

public static final int R_OFFSET
See Also:
Constant Field Values

G_OFFSET

public static final int G_OFFSET
See Also:
Constant Field Values

B_OFFSET

public static final int B_OFFSET
See Also:
Constant Field Values

BYTE_MASK

public static final int BYTE_MASK
See Also:
Constant Field Values

RGB_MASK

public static final int RGB_MASK
See Also:
Constant Field Values

A_MASK

public static final int A_MASK
See Also:
Constant Field Values

R_MASK

public static final int R_MASK
See Also:
Constant Field Values

G_MASK

public static final int G_MASK
See Also:
Constant Field Values

B_MASK

public static final int B_MASK
See Also:
Constant Field Values

m_bufferedImage

protected final java.awt.image.BufferedImage m_bufferedImage

m_aBasePixels

protected int[] m_aBasePixels

m_aProcessedPixels

private int[] m_aProcessedPixels

W

public final int W

H

public final int H

SCAN_SIZE

protected final int SCAN_SIZE

NB_PIXELS

private final int NB_PIXELS

m_param

private int m_param
Constructor Detail

CImageProcessor

public CImageProcessor(int w,
                       int h,
                       boolean bCompatible)

CImageProcessor

public CImageProcessor(int w,
                       int h)
Builds a blank CImageProcessor.

Parameters:
w - image width
h - image height

CImageProcessor

public CImageProcessor(java.awt.Image img)
Builds a CImageProcessor from an Image

Parameters:
img - the Image

CImageProcessor

public CImageProcessor(CImageProcessor rhs)
Copy a CImageProcessor (image data is copied too)

Parameters:
rhs -
Method Detail

getBufferedImage

public java.awt.image.BufferedImage getBufferedImage()

createCompatibleImage

public static final java.awt.image.BufferedImage createCompatibleImage(int w,
                                                                       int h)
Creates a translucent BufferedImage that can be optimally blitted to the screen.

Parameters:
w - BufferedImage's width
h - BufferedImage's height
Returns:
the new BufferedImage

createARGBImage

public static final java.awt.image.BufferedImage createARGBImage(int w,
                                                                 int h)
Creates a translucent BufferedImage can be optimally processed as an int[].

Parameters:
w - BufferedImage's width
h - BufferedImage's height
Returns:
the new BufferedImage

getARGB

protected static final int getARGB(int a,
                                   int r,
                                   int g,
                                   int b)

getGraphics

public java.awt.Graphics getGraphics()
Allows standard AWT drawing on the buffer.

Returns:
the BufferedImage's Graphics

saveCurrentPixels

public void saveCurrentPixels()
Saves the current pixels for later use (image restoring or other)


initPixelArrays

private final void initPixelArrays()
Saves initial pixels. Useful to keep the original image.


drawImage

public void drawImage(java.awt.Graphics g,
                      int x,
                      int y)
Draws the image to a Graphics.

Parameters:
g -
x -
y -

setAlpha

public void setAlpha(int alpha)
Modifies the alpha value of the pixels

Parameters:
alpha - the new alpha value

colorFilter

public void colorFilter(java.awt.Color color,
                        boolean bSavePixels)
Applies a color filter to the image.

Parameters:
color -
bSavePixels - wether to save pixels after operation

colorAdd

public void colorAdd(java.awt.Color color,
                     boolean bSavePixels)
adds a color value to each pixel.

Parameters:
color -
bSavePixels -

scrollH

public void scrollH(float dx)
Parameters:
dx -

rotate

public final void rotate(float alpha)
Comodity function, performs a rotation from the center of the image.

Parameters:
alpha - angle by witch to rotate.

rotate

public final void rotate(float alpha,
                         float x,
                         float y)
Rotates the image. Function params are of float type for ease of use, but all inner calculations are on ints for efficiency (fixed point arithmetics).

Parameters:
alpha - angle by witch to rotate.
x - coordinate of the center of the rotation.
y - coordinate of the center of the rotation.

zoom

public final void zoom(float lambda)
Comodity function, performs a zoom from the center of the image.

Parameters:
lambda - zooming factor

zoom

public final void zoom(float lambda,
                       float x,
                       float y)
Zooms the image in or out. Function params are of float type for ease of use, but all inner calculations are on ints for efficiency (fixed point arithmetics).

Parameters:
lambda - zooming factor
x - center point of the zoom
y -

affineTransform

public final void affineTransform(CAffineTransform transform)
Applies an affine transformation (such as zoom or rotation) to the whole image.

Parameters:
transform - the transformation matrix (containing fixed point values)
See Also:
CAffineTransform, CFixedPoint

affineTransformAA

public final void affineTransformAA(CAffineTransform transform)
The resulting image is 4x4 anti-aliased. One property of this operation is that if the transform matrix is the identity matrix, the resulting image is equal to the original.

Parameters:
transform - the transformation matrix to be applied.

convolve

public void convolve(int[][] fpKernel)
Does a convolve operation given a kernel, be it a gaussian blur or edge enhacement.

Parameters:
fpKernel - an (m, n) matrix of fixed point values.

safeComponent

public int safeComponent(int component)

wave

public void wave(float f,
                 float t)
Parameters:
f -
t -

getImageOffsets

protected final int[] getImageOffsets()
Reads the first line of the contained image to determine subimages offsets

Returns:
the offsets corresponding to the subimages

split

protected final java.awt.image.BufferedImage[] split(int[] offsets)
/** Splits the contained image in subimmages according to an array of offsets.

Parameters:
offsets - the said array
Returns:
an array of subimages

split

public final java.awt.image.BufferedImage[] split()
Splits the contained image in subimmages according to markers contained in the first line.

Returns:
an array of subimages

mul

public final void mul(java.awt.Color mulColor)

mul

public final void mul(int iR,
                      int iG,
                      int iB)

mul

public final void mul(int iA,
                      int iR,
                      int iG,
                      int iB)

invert

public final void invert()
Video inversion of the image. Alpha values are left unchanged.


getA

protected final int getA(int argb)

getR

protected final int getR(int argb)

getG

protected final int getG(int argb)

getB

protected final int getB(int argb)

restablishTransparency

public final void restablishTransparency()

getBlurKernel

public static final int[][] getBlurKernel()
Returns a 3x3 blur kernel.

Returns:
a 3x3 blur kernel.

getGaussianBlurKernel

public static final int[][] getGaussianBlurKernel()
Returns a 3x3 gaussian blur kernel.

Returns:
a 3x3 gaussian blur kernel.

getEdgeKernel

public static final int[][] getEdgeKernel()

getEmbossKernel

public static final int[][] getEmbossKernel()

getSharpenKernel

public static final int[][] getSharpenKernel()

colorLerp

private final int colorLerp(int fpX,
                            int color1,
                            int color2)
Linear interpolation between two colors.

Parameters:
fpX - the ratio in fixed point value. Must belongs to [0, 1].
color1 - start color
color2 - end color
Returns:
the interpolated color
See Also:
CFixedPoint

fourCornersGradient

public final void fourCornersGradient(int topLeftColor,
                                      int topRightColor,
                                      int bottomRightColor,
                                      int bottomLeftColor)
Generates a gradient interpolated from four colors (one at each corner) covering the whole image.
Uses linear interpolation and fixed point arithmetics.

Parameters:
topLeftColor -
topRightColor -
bottomRightColor -
bottomLeftColor -
See Also:
CFixedPoint

interleave

public void interleave(CImageProcessor imgProcessor,
                       int[][] mask)