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
| Modifier and Type | Class and Description |
|---|---|
| private static class | DistanceFieldGenerator.
Thrown when the command line contained nonsense. |
| Modifier and Type | Field and Description |
|---|---|
| private Color | |
| private int | |
| private float |
| Access | Constructor and Description |
|---|---|
| public |
| Modifier and Type | Method and Description |
|---|---|
| private int | Returns: an ARGB color value suitable forBufferedImage#setRGB.the signed distance of a pixel signedDistance)For a distance as returned by |
| private float | Returns: the signed distancethe x coordinate of the center point centerX, final int the y coordinate of the center point centerY, boolean[][] the array representation of an image, bitmap)true representing "inside"Returns the signed distance for a given point. |
| public BufferedImage | Returns: the distance field imagethe image to process. inImage)Process the image into a distance field. |
| public Color | |
| public int | |
| public float | |
| private boolean | isInside(int rgb)
Returns |
| public static void | |
| private static void | |
| public void | |
| public void | setDownscale(int
a positive integer downscale)Sets the factor by which to downscale the image during processing. |
| public void | |
| private static int | Returns: The squared distance between the two pointsThe 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 |
| color | back to summary |
|---|---|
| private Color color | |
| downscale | back to summary |
|---|---|
| private int downscale | |
| spread | back to summary |
|---|---|
| private float spread | |
| DistanceFieldGenerator | back to summary |
|---|---|
| public DistanceFieldGenerator() | |
| distanceToRGB | back to summary |
|---|---|
| private int distanceToRGB(float signedDistance) For a distance as returned by
| |
| findSignedDistance | back 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
| |
| generateDistanceField | back 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
| |
| getColor | back to summary |
|---|---|
public Color getColor()
| |
| getDownscale | back to summary |
|---|---|
public int getDownscale()
| |
| getSpread | back to summary |
|---|---|
public float getSpread()
| |
| isInside | back to summary |
|---|---|
| private boolean isInside(int rgb) Returns Any color with one of its color channels at least 128 and its alpha channel at least 128 is considered "inside". | |
| main | back 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
| |
| run | back to summary |
|---|---|
| private static void run(String[] args) Runs the program.
| |
| setColor | back 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. | |
| setDownscale | back 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.
| |
| setSpread | back 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.
| |
| squareDist | back 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
| |
| usage | back to summary |
|---|---|
| private static void usage() Prints usage information to standard output. | |
| Access | Constructor and Description |
|---|---|
| public |
| CommandLineArgumentException | back to summary |
|---|---|
| public CommandLineArgumentException(String message) | |