APIdia Launch Partner
Top Description Inners Fields Constructors Methods
com.badlogic.gdx.tools.distancefield

public Class DistanceFieldGenerator

extends Object
Class Inheritance
Imports
java.awt.Color, java.awt.image.BufferedImage, java.io.File, .IOException, javax.imageio.ImageIO

Generates a signed distance field image from a binary (black/white) source image.

Signed distance fields are used in Team Fortress 2 by Valve to enable sharp rendering of bitmap fonts even at high magnifications, using nothing but alpha testing so at no extra runtime cost.

The technique is described in the SIGGRAPH 2007 paper "Improved Alpha-Tested Magnification for Vector Textures and Special Effects" by Chris Green: http://www.valvesoftware.com/publications/2007/SIGGRAPH2007_AlphaTestedMagnification.pdf

Author
Thomas ten Cate

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
DistanceFieldGenerator.CommandLineArgumentException

Thrown when the command line contained nonsense.

Field Summary

Modifier and TypeField and Description
private Color
private int
private float

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
private int

Returns:

an ARGB color value suitable for BufferedImage#setRGB.
distanceToRGB
(float
the signed distance of a pixel
signedDistance
)

For a distance as returned by findSignedDistance, returns the corresponding "RGB" (really ARGB) color value.

private float

Returns:

the signed distance
findSignedDistance
(final int
the x coordinate of the center point
centerX
,
final int
the y coordinate of the center point
centerY
,
boolean[][]
the array representation of an image, true representing "inside"
bitmap
)

Returns the signed distance for a given point.

public BufferedImage

Returns:

the distance field image
generateDistanceField
(BufferedImage
the image to process.
inImage
)

Process the image into a distance field.

public Color
public int
public float
private boolean
isInside(int rgb)

Returns true if the color is considered as the "inside" of the image, false if considered "outside".

public static void
main(String[]
command line arguments
args
)

Main function to run the generator as a standalone program.

private static void
run(String[]
command line arguments
args
)

Runs the program.

public void
setColor(Color color)

Sets the color to be used for the output image.

public void
setDownscale(int
a positive integer
downscale
)

Sets the factor by which to downscale the image during processing.

public void
setSpread(float
a positive number
spread
)

Sets the spread of the distance field.

private static int

Returns:

The squared distance between the two points
squareDist
(final int
The x coordinate of the first point
x1
,
final int
The y coordiante of the first point
y1
,
final int
The x coordinate of the second point
x2
,
final int
The y coordinate of the second point
y2
)

Caclulate the squared distance between two points

private static void
usage()

Prints usage information to standard output.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

colorback to summary
private Color color
downscaleback to summary
private int downscale
spreadback to summary
private float spread

Constructor Detail

DistanceFieldGeneratorback to summary
public DistanceFieldGenerator()

Method Detail

distanceToRGBback to summary
private int distanceToRGB(float signedDistance)

For a distance as returned by findSignedDistance, returns the corresponding "RGB" (really ARGB) color value.

Parameters
signedDistance:float

the signed distance of a pixel

Returns:int

an ARGB color value suitable for BufferedImage#setRGB.

findSignedDistanceback to summary
private float findSignedDistance(final int centerX, final int centerY, boolean[][] bitmap)

Returns the signed distance for a given point. For points "inside", this is the distance to the closest "outside" pixel. For points "outside", this is the negative distance to the closest "inside" pixel. If no pixel of different color is found within a radius of spread, returns the -spread or spread, respectively.

Parameters
centerX:int

the x coordinate of the center point

centerY:int

the y coordinate of the center point

bitmap:boolean[][]

the array representation of an image, true representing "inside"

Returns:float

the signed distance

generateDistanceFieldback to summary
public BufferedImage generateDistanceField(BufferedImage inImage)

Process the image into a distance field. The input image should be binary (black/white), but if not, see isInside(int). The returned image is a factor of upscale smaller than inImage. Opaque pixels more than spread away in the output image from white remain opaque; transparent pixels more than spread away in the output image from black remain transparent. In between, we get a smooth transition from opaque to transparent, with an alpha value of 0.5 when we are exactly on the edge.

Parameters
inImage:BufferedImage

the image to process.

Returns:BufferedImage

the distance field image

getColorback to summary
public Color getColor()
See Also
setColor(Color)
getDownscaleback to summary
public int getDownscale()
See Also
setDownscale(int)
getSpreadback to summary
public float getSpread()
See Also
setSpread(float)
isInsideback to summary
private boolean isInside(int rgb)

Returns true if the color is considered as the "inside" of the image, false if considered "outside".

Any color with one of its color channels at least 128 and its alpha channel at least 128 is considered "inside".

mainback to summary
public static void main(String[] args)

Main function to run the generator as a standalone program. Run without arguments for usage instructions (or see usage()).

Parameters
args:String[]

command line arguments

runback to summary
private static void run(String[] args)

Runs the program.

Parameters
args:String[]

command line arguments

Exceptions
CommandLineArgumentException:
if the command line contains an error
setColorback to summary
public void setColor(Color color)

Sets the color to be used for the output image. Its alpha component is ignored. Defaults to white, which is convenient for multiplying by a color value at runtime.

setDownscaleback to summary
public void setDownscale(int downscale)

Sets the factor by which to downscale the image during processing. The output image will be smaller than the input image by this factor, rounded downwards.

For greater accuracy, images to be used as input for a distance field are often generated at higher resolution.

Parameters
downscale:int

a positive integer

Exceptions
IllegalArgumentException:
if downscale is not positive
setSpreadback to summary
public void setSpread(float spread)

Sets the spread of the distance field. The spread is the maximum distance in pixels that we'll scan while for a nearby edge. The resulting distance is also normalized by the spread.

Parameters
spread:float

a positive number

Exceptions
IllegalArgumentException:
if spread is not positive
squareDistback to summary
private static int squareDist(final int x1, final int y1, final int x2, final int y2)

Caclulate the squared distance between two points

Parameters
x1:int

The x coordinate of the first point

y1:int

The y coordiante of the first point

x2:int

The x coordinate of the second point

y2:int

The y coordinate of the second point

Returns:int

The squared distance between the two points

usageback to summary
private static void usage()

Prints usage information to standard output.

com.badlogic.gdx.tools.distancefield back to summary

private Class DistanceFieldGenerator.CommandLineArgumentException

extends IllegalArgumentException
Class Inheritance

Thrown when the command line contained nonsense.

Constructor Summary

AccessConstructor and Description
public

Constructor Detail

CommandLineArgumentExceptionback to summary
public CommandLineArgumentException(String message)