Facemorph.aam
Class FBOwarp

java.lang.Object
  extended by Facemorph.aam.FBOwarp
All Implemented Interfaces:
java.util.EventListener, javax.media.opengl.GLEventListener

public class FBOwarp
extends java.lang.Object
implements javax.media.opengl.GLEventListener

Frame buffer object warping


Constructor Summary
FBOwarp(int width, int height, java.awt.image.BufferedImage bimg, java.util.Vector<java.awt.geom.Point2D.Float> sourcePoints, java.util.Vector<java.awt.geom.Point2D.Float> targetPoints)
          Constructor for FBOwarp
FBOwarp(int width, int height, java.awt.image.BufferedImage bimg, java.util.Vector<java.awt.geom.Point2D.Float> sourcePoints, java.util.Vector<java.awt.geom.Point2D.Float> targetPoints, javax.media.opengl.GLAutoDrawable canvas)
          Constructor for FBOwarp
 
Method Summary
static void addAffineBorderPoints(java.util.Vector<java.awt.geom.Point2D.Float> srcPoints, java.util.Vector<java.awt.geom.Point2D.Float> dstPoints, int width, int height, boolean addBorder)
          Adds affine border points
static void addBorderPoints(java.util.Vector<java.awt.geom.Point2D.Float> points, int width, int height)
          Adds a set of points round the border
 void checkError(javax.media.opengl.GL gl)
          Check for an OpenGL error
 DelaunayTriangle[] computeTriangulation(java.util.Vector<java.awt.geom.Point2D.Float> points)
          Compute the delaunay triangulation of the given points
 void destroy()
          Deletes this (currently empty method)
 void display()
          Force a redraw? (currently empty)
 void display(javax.media.opengl.GLAutoDrawable drawable)
           
 void displayChanged(javax.media.opengl.GLAutoDrawable drawable, boolean modeChanged, boolean deviceChanged)
           
 void dispose()
          Disposes this FBOwarp
static FloatImage getAlpha(java.awt.image.BufferedImage bimg)
          Gets the alpha channel from the image
 int getHeight()
          Get the height of this FBOwarp
 FloatImage[] getResult()
          Get the warped image as an array of 3 FloatImage
 FloatImage[] getResult(boolean alpha)
          Get the warped image as an array of 3 or 4 FloatImage
 int getWidth()
          Get the width of this FBOwarp
 void grab(javax.media.opengl.GL gl, FloatImage[] result)
          Grab the result to the supplied array of float images
 void init(javax.media.opengl.GLAutoDrawable drawable)
           
 void initFBO(javax.media.opengl.GL gl, javax.media.opengl.glu.GLU glu)
          Initialise the FBO
 double jacobianError()
          Calculate the jacobean error (difference from 1)
static void main(java.lang.String[] args)
          Main method for testing
 double overlap(double minArea, boolean adjust)
          Scales the warp so that it doesn't fold
 void postRenderFBO(javax.media.opengl.GL gl, javax.media.opengl.glu.GLU glu)
          Unbinds the FBO
 void preRenderFBO(javax.media.opengl.GL gl, javax.media.opengl.glu.GLU glu)
          Sets up the global rendering state, binds the FBO
 void reshape(javax.media.opengl.GLAutoDrawable drawable, int x, int y, int width, int height)
           
static java.awt.Color sample(java.awt.image.BufferedImage img, float x, float y)
          Non-integer bilinear sampling from a BufferedImage
static java.awt.image.BufferedImage setAlpha(java.awt.image.BufferedImage bimg, FloatImage alpha)
          sets the alpha channel of the image
 void setDrawTriangulation(boolean drawTriangulation)
          Should the warp triangulation be drawn
 void setDrawWarp(boolean drawWarp)
          Should the warped image be drawn
 void setGLAutoDrawable(javax.media.opengl.GLAutoDrawable drawable)
          Sets the OpenGL canvas
 void setImage(java.awt.image.BufferedImage img)
          Set the image to warp
 void setPoints(java.util.Vector<java.awt.geom.Point2D.Float> sourcePoints, java.util.Vector<java.awt.geom.Point2D.Float> targetPoints)
          Sets the start and end warp points
 void setSize(int w, int h)
          Sets the size
 void setTriangulation(DelaunayTriangle[] dtTess)
          Set the triangulation for this warp
static java.nio.FloatBuffer toFloatBuffer(java.awt.image.BufferedImage bimg)
          Converts the image to a FloatBuffer
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

FBOwarp

public FBOwarp(int width,
               int height,
               java.awt.image.BufferedImage bimg,
               java.util.Vector<java.awt.geom.Point2D.Float> sourcePoints,
               java.util.Vector<java.awt.geom.Point2D.Float> targetPoints)
Constructor for FBOwarp

Parameters:
width - the width
height - the height
bimg - the image to warp
sourcePoints - the start points
targetPoints - the end points

FBOwarp

public FBOwarp(int width,
               int height,
               java.awt.image.BufferedImage bimg,
               java.util.Vector<java.awt.geom.Point2D.Float> sourcePoints,
               java.util.Vector<java.awt.geom.Point2D.Float> targetPoints,
               javax.media.opengl.GLAutoDrawable canvas)
Constructor for FBOwarp

Parameters:
width - the width
height - the height
bimg - the image to warp
sourcePoints - the start points
targetPoints - the end points
canvas - use an existing OpenGL canvas
Method Detail

setSize

public void setSize(int w,
                    int h)
Sets the size

Parameters:
w - the new width
h - the new height

dispose

public void dispose()
Disposes this FBOwarp


setGLAutoDrawable

public void setGLAutoDrawable(javax.media.opengl.GLAutoDrawable drawable)
Sets the OpenGL canvas

Parameters:
drawable - the new OpenGL canvas

getWidth

public int getWidth()
Get the width of this FBOwarp

Returns:
returns the width

getHeight

public int getHeight()
Get the height of this FBOwarp

Returns:
returns the height

getResult

public FloatImage[] getResult()
Get the warped image as an array of 3 FloatImage

Returns:
returns the warped image

getResult

public FloatImage[] getResult(boolean alpha)
Get the warped image as an array of 3 or 4 FloatImage

Parameters:
alpha - indicates if the alpha should be grabbed
Returns:
returns the grabbed images

checkError

public void checkError(javax.media.opengl.GL gl)
Check for an OpenGL error

Parameters:
gl - the OpenGL object to check

destroy

public void destroy()
Deletes this (currently empty method)


setImage

public void setImage(java.awt.image.BufferedImage img)
Set the image to warp

Parameters:
img - the image to warp

setPoints

public void setPoints(java.util.Vector<java.awt.geom.Point2D.Float> sourcePoints,
                      java.util.Vector<java.awt.geom.Point2D.Float> targetPoints)
Sets the start and end warp points

Parameters:
sourcePoints - the start points
targetPoints - the end points

display

public void display()
Force a redraw? (currently empty)


setDrawWarp

public void setDrawWarp(boolean drawWarp)
Should the warped image be drawn

Parameters:
drawWarp - the new value for drawWarp

setDrawTriangulation

public void setDrawTriangulation(boolean drawTriangulation)
Should the warp triangulation be drawn

Parameters:
drawTriangulation - the new value for drawTriangulation

sample

public static java.awt.Color sample(java.awt.image.BufferedImage img,
                                    float x,
                                    float y)
Non-integer bilinear sampling from a BufferedImage

Parameters:
img - the image to sample from
x - the x-coord to sample from
y - the y-coord to sample from
Returns:
the sampled colour

toFloatBuffer

public static java.nio.FloatBuffer toFloatBuffer(java.awt.image.BufferedImage bimg)
Converts the image to a FloatBuffer

Parameters:
bimg - the image to convert
Returns:
returns the FloatBuffer holding the image

computeTriangulation

public DelaunayTriangle[] computeTriangulation(java.util.Vector<java.awt.geom.Point2D.Float> points)
Compute the delaunay triangulation of the given points

Parameters:
points - the points to triangulate
Returns:
returns the Delauay triangulation as an array of triangles

setTriangulation

public void setTriangulation(DelaunayTriangle[] dtTess)
Set the triangulation for this warp

Parameters:
dtTess - the new triangulation

init

public void init(javax.media.opengl.GLAutoDrawable drawable)
Specified by:
init in interface javax.media.opengl.GLEventListener

display

public void display(javax.media.opengl.GLAutoDrawable drawable)
Specified by:
display in interface javax.media.opengl.GLEventListener

grab

public void grab(javax.media.opengl.GL gl,
                 FloatImage[] result)
Grab the result to the supplied array of float images

Parameters:
gl - the OpenGL object
result - for storing the results

reshape

public void reshape(javax.media.opengl.GLAutoDrawable drawable,
                    int x,
                    int y,
                    int width,
                    int height)
Specified by:
reshape in interface javax.media.opengl.GLEventListener

displayChanged

public void displayChanged(javax.media.opengl.GLAutoDrawable drawable,
                           boolean modeChanged,
                           boolean deviceChanged)
Specified by:
displayChanged in interface javax.media.opengl.GLEventListener

preRenderFBO

public void preRenderFBO(javax.media.opengl.GL gl,
                         javax.media.opengl.glu.GLU glu)
Sets up the global rendering state, binds the FBO

Parameters:
gl - the opengl context
glu - the opengl utility object

postRenderFBO

public void postRenderFBO(javax.media.opengl.GL gl,
                          javax.media.opengl.glu.GLU glu)
Unbinds the FBO

Parameters:
gl -
glu -

initFBO

public void initFBO(javax.media.opengl.GL gl,
                    javax.media.opengl.glu.GLU glu)
Initialise the FBO

Parameters:
gl - the opengl context
glu - the opengl utility object

addAffineBorderPoints

public static void addAffineBorderPoints(java.util.Vector<java.awt.geom.Point2D.Float> srcPoints,
                                         java.util.Vector<java.awt.geom.Point2D.Float> dstPoints,
                                         int width,
                                         int height,
                                         boolean addBorder)
Adds affine border points

Parameters:
srcPoints - the source points array
dstPoints - the target point array
width - the output width
height - the output height
addBorder - indicates that the border points should be added

addBorderPoints

public static void addBorderPoints(java.util.Vector<java.awt.geom.Point2D.Float> points,
                                   int width,
                                   int height)
Adds a set of points round the border

Parameters:
points - the point set to add to
width - the image width
height - the image height

jacobianError

public double jacobianError()
Calculate the jacobean error (difference from 1)

Returns:
returns the Jacobean error (basically the change in area)

overlap

public double overlap(double minArea,
                      boolean adjust)
Scales the warp so that it doesn't fold

Parameters:
minArea - the minimum allowed area of each triangle as a fraction of its current area
adjust - if true and any triangles are smaller that the allowed minimum the warp is scaled to ensure it doesn't fold or come close to folding
Returns:
returns the calculated scale factor

setAlpha

public static java.awt.image.BufferedImage setAlpha(java.awt.image.BufferedImage bimg,
                                                    FloatImage alpha)
sets the alpha channel of the image

Parameters:
bimg - the original image
alpha - the new alpha channel
Returns:
returns the original with the new alpha channel

getAlpha

public static FloatImage getAlpha(java.awt.image.BufferedImage bimg)
Gets the alpha channel from the image

Parameters:
bimg - the image to get the alpha channel from
Returns:
the alpha channel as a float image

main

public static void main(java.lang.String[] args)
Main method for testing

Parameters:
args - args[0] = name of input image, args[1] and args[2] start and end templates, args[3] output image name