NASA World Wind

gov.nasa.worldwind.util
Class ImageUtil

java.lang.Object
  extended by gov.nasa.worldwind.util.ImageUtil

public class ImageUtil
extends Object


Nested Class Summary
static class ImageUtil.AlignedImage
           
 
Field Summary
static int BILINEAR_INTERPOLATION
           
static int IMAGE_TILE_SIZE
           
static int NEAREST_NEIGHBOR_INTERPOLATION
           
static Color TRANSPARENT
           
 
Constructor Summary
ImageUtil()
           
 
Method Summary
static ImageUtil.AlignedImage alignImage(BufferedImage sourceImage, float[] latitudes, float[] longitudes)
          Reprojects an image into an aligned image, one with edges of constant latitude and longitude.
static ImageUtil.AlignedImage alignImage(BufferedImage sourceImage, float[] latitudes, float[] longitudes, Sector sector)
          Reprojects an image into an aligned image, one with edges of constant latitude and longitude.
static BufferedImage[] buildMipmaps(BufferedImage image)
          Builds a sequence of mipmaps for the specified image.
static BufferedImage[] buildMipmaps(BufferedImage image, int mipmapImageType, int maxLevel)
          Builds a sequence of mipmaps for the specified image.
static void computeBestFittingControlPoints4(Point2D[] imagePoints, LatLon[] geoPoints, Point2D[] outImagePoints, LatLon[] outGeoPoints)
          Computes which three control points out of four provide the best estimate an image's geographic location.
static List<LatLon> computeImageCorners(int imageWidth, int imageHeight, Matrix imageToGeographic)
          Returns the geographic corners of an image with the specified dimensions, and a transform that maps image coordinates to geographic coordinates.
static long computeSizeInBytes(BufferedImage image)
          Returns the size in bytes of the specified image.
static BufferedImage convertToPowerOfTwoImage(BufferedImage image, boolean scaleToFit)
          Returns a copy of the specified image such that the new dimensions are powers of two.
static void drawImageOnCanvas(BufferedImage image, BufferedImage canvas)
          Draws the specified image onto the canvas, scaling or streching the image to fit the canvas.
static int getMaxMipmapLevel(int width, int height)
          Returns the maximum desired mip level for an image with dimensions width and height.
static int interpolateColor(double x, double y, int c0, int c1, int c2, int c3)
          Performs bilinear interpolation of 32-bit colors over a convex quadrilateral.
static void mergeImage(Sector canvasSector, Sector imageSector, double aspectRatio, BufferedImage image, BufferedImage canvas)
          Merge an image into another image.
static AVList openSpatialImage(File imageFile)
          Opens a spatial image.
static AVList openSpatialImage(File imageFile, int interpolation_mode)
          Opens a spatial image.
static Sector positionImage(BufferedImage sourceImage, Point[] imagePoints, LatLon[] geoPoints, BufferedImage destImage)
           
static Sector positionImage3(BufferedImage sourceImage, Point[] imagePoints, LatLon[] geoPoints, BufferedImage destImage)
           
static Sector positionImage4(BufferedImage sourceImage, Point[] imagePoints, LatLon[] geoPoints, BufferedImage destImage)
           
static void reprojectUtmToGeographic(AVList values, int mode)
          Reprojects an imge in UTM projection to Geo/WGS84.
static Sector warpImageWithControlPoints(BufferedImage sourceImage, Point2D[] imagePoints, LatLon[] geoPoints, BufferedImage destImage)
          Convenience method for transforming a georeferenced source image into a geographically aligned destination image.
static Sector warpImageWithControlPoints3(BufferedImage sourceImage, Point2D[] imagePoints, LatLon[] geoPoints, BufferedImage destImage)
          Transforms a georeferenced source image into a geographically aligned destination image.
static Sector warpImageWithControlPoints4(BufferedImage sourceImage, Point2D[] imagePoints, LatLon[] geoPoints, BufferedImage destImage)
          Transforms a georeferenced source image into a geographically aligned destination image.
static void warpImageWithTransform(BufferedImage image, BufferedImage canvas, Matrix canvasToImageTransform)
          Rasterizes the image into the canvas, given a transform that maps canvas coordinates to image coordinates.
static Sector warpImageWithWorldFile(BufferedImage sourceImage, AVList worldFileParams, BufferedImage destImage)
          Transforms a georeferenced source image into a geographically aligned destination image.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BILINEAR_INTERPOLATION

public static int BILINEAR_INTERPOLATION

IMAGE_TILE_SIZE

public static int IMAGE_TILE_SIZE

NEAREST_NEIGHBOR_INTERPOLATION

public static int NEAREST_NEIGHBOR_INTERPOLATION

TRANSPARENT

public static Color TRANSPARENT
Constructor Detail

ImageUtil

public ImageUtil()
Method Detail

alignImage

public static ImageUtil.AlignedImage alignImage(BufferedImage sourceImage,
                                                float[] latitudes,
                                                float[] longitudes)
Reprojects an image into an aligned image, one with edges of constant latitude and longitude.

Parameters:
sourceImage - the image to reproject, typically a non-aligned image
latitudes - an array identifying the latitude of each pixels if the source image. There must be an entry in the array for all pixels. The values are taken to be in row-major order relative to the image -- the horizontal component varies fastest.
longitudes - an array identifying the longitude of each pixels if the source image. There must be an entry in the array for all pixels. The values are taken to be in row-major order relative to the image -- the horizontal component varies fastest.
Returns:
a new image containing the original image but reprojected to align to the bounding sector. Pixels in the new image that have no correspondence with the source image are transparent.

alignImage

public static ImageUtil.AlignedImage alignImage(BufferedImage sourceImage,
                                                float[] latitudes,
                                                float[] longitudes,
                                                Sector sector)
Reprojects an image into an aligned image, one with edges of constant latitude and longitude.

Parameters:
sourceImage - the image to reproject, typically a non-aligned image
latitudes - an array identifying the latitude of each pixels if the source image. There must be an entry in the array for all pixels. The values are taken to be in row-major order relative to the image -- the horizontal component varies fastest.
longitudes - an array identifying the longitude of each pixels if the source image. There must be an entry in the array for all pixels. The values are taken to be in row-major order relative to the image -- the horizontal component varies fastest.
sector - the sector to align the image to.
Returns:
a new image containing the original image but reprojected to align to the sector. Pixels in the new image that have no correspondence with the source image are transparent.

buildMipmaps

public static BufferedImage[] buildMipmaps(BufferedImage image)
Builds a sequence of mipmaps for the specified image. This is equivalent to invoking buildMipmaps(BufferedImage, int, int), with mipmapImageType equal to image.getType(), and maxLevel equal to getMaxMipmapLevel(image.getWidth(), image.getHeight()).

Parameters:
image - the BufferedImage to build mipmaps for.
Returns:
array of mipmap levels.
Throws:
IllegalArgumentException - if image is null.

buildMipmaps

public static BufferedImage[] buildMipmaps(BufferedImage image,
                                           int mipmapImageType,
                                           int maxLevel)
Builds a sequence of mipmaps for the specified image. The number of mipmap levels created will be equal to maxLevel + 1, including level 0. The level 0 image will be a reference to the original image, not a copy. Each mipmap level will be created with the specified BufferedImage type mipmapImageType. Each level will have dimensions equal to 1/2 the previous level's dimensions, rownding down, to a minimum width or height of 1.

Parameters:
image - the BufferedImage to build mipmaps for.
mipmapImageType - the BufferedImage type to use when creating each mipmap image.
maxLevel - the maximum mip level to create. Specifying zero will return an array containing the original image.
Returns:
array of mipmap levels, starting at level 0 and stopping at maxLevel. This array will have length maxLevel + 1.
Throws:
IllegalArgumentException - if image is null, or if maxLevel is less than zero.
See Also:
getMaxMipmapLevel(int, int)

computeBestFittingControlPoints4

public static void computeBestFittingControlPoints4(Point2D[] imagePoints,
                                                    LatLon[] geoPoints,
                                                    Point2D[] outImagePoints,
                                                    LatLon[] outGeoPoints)
Computes which three control points out of four provide the best estimate an image's geographic location. The result is placed in the output parameters outImagePoints and outGeoPoints, both of which must be non-null and at least length 3.

Parameters:
imagePoints - four control points in the image.
geoPoints - four geographic locations corresponding to the four imagePoints.
outImagePoints - three control points that best estimate the image's location.
outGeoPoints - three geographic locations correstponding to the three outImagePoints.
Throws:
IllegalArgumentException - if any of imagePoints, geoPoints, outImagePoints or outGeoPoints is null, or if imagePoints or geoPoints have length less than 4, or if outImagePoints or outGeoPoints have length less than 3.

computeImageCorners

public static List<LatLon> computeImageCorners(int imageWidth,
                                               int imageHeight,
                                               Matrix imageToGeographic)
Returns the geographic corners of an image with the specified dimensions, and a transform that maps image coordinates to geographic coordinates.

Parameters:
imageWidth - width of the image grid.
imageHeight - height of the image grid.
imageToGeographic - Matrix that maps image coordinates to geographic coordinates.
Returns:
List of the image's corner locations in geographic coordinates.
Throws:
IllegalArgumentException - if either imageWidth or imageHeight are less than 1, or if imageToGeographic is null.

computeSizeInBytes

public static long computeSizeInBytes(BufferedImage image)
Returns the size in bytes of the specified image. This takes into account only the image's backing DataBuffers, and not the numerous supporting classes a BufferedImage references.

Parameters:
image - the BufferedImage to compute the size of.
Returns:
size of the BufferedImage in bytes.
Throws:
IllegalArgumentException - if image is null.

convertToPowerOfTwoImage

public static BufferedImage convertToPowerOfTwoImage(BufferedImage image,
                                                     boolean scaleToFit)
Returns a copy of the specified image such that the new dimensions are powers of two. The new image dimensions will be equal to or greater the original image. The flag scaleToFit determines whether the original image should be drawn into the new image with no special scalign, or whether the original image should be scaled to fit exactly in the new image. If the original image dimensions are already powers of two, this will simply return the original image.

Parameters:
image - the BufferedImage to convert to a power of two image.
scaleToFit - true if image should be scaled to fit the new image dimensions; false otherwise.s
Returns:
copy of image with power of two dimensions.
Throws:
IllegalArgumentException - if image is null.

drawImageOnCanvas

public static void drawImageOnCanvas(BufferedImage image,
                                     BufferedImage canvas)
Draws the specified image onto the canvas, scaling or streching the image to fit the canvas. This will apply a bilinear filter to the image if any scaling or streching is necessary.

Parameters:
image - the BufferedImage to draw, potentially scaling or streching to fit the canvas.
canvas - the BufferedImage to receive the scaled or streched image.
Throws:
IllegalArgumentException - if either image or canvas is null.

getMaxMipmapLevel

public static int getMaxMipmapLevel(int width,
                                    int height)
Returns the maximum desired mip level for an image with dimensions width and height. The maximum desired level is the number of levels required to reduce the original image dimensions to a 1x1 image.

Parameters:
width - the level 0 image width.
height - the level 0 image height.
Returns:
maximum mip level for the specified width and height.
Throws:
IllegalArgumentException - if either width or height are less than 1.

interpolateColor

public static int interpolateColor(double x,
                                   double y,
                                   int c0,
                                   int c1,
                                   int c2,
                                   int c3)
Performs bilinear interpolation of 32-bit colors over a convex quadrilateral. The four colors are specified in counterclockwise order beginning with the lower left.

Parameters:
x - horizontal coordinate of the interpolation point relative to the lower left corner of the quadrilateral. The value should generally be in the range [0, 1].
y - vertical coordinate of the interpolation point relative to the lower left corner of the quadrilateral. The value should generally be in the range [0, 1].
c0 - color at the lower left corner of the quadrilateral.
c1 - color at the lower right corner of the quadrilateral.
c2 - color at the pixel upper right corner of the quadrilateral.
c3 - color at the pixel upper left corner of the quadrilateral.
Returns:
int the interpolated color.

mergeImage

public static void mergeImage(Sector canvasSector,
                              Sector imageSector,
                              double aspectRatio,
                              BufferedImage image,
                              BufferedImage canvas)
Merge an image into another image. This method is typically used to assemble a composite, seamless image from several individual images. The receiving image, called here the canvas because it's analogous to the Photoshop notion of a canvas, merges the incoming image according to the specified aspect ratio.

Parameters:
canvasSector - the sector defining the canvas' location and range.
imageSector - the sector defining the image's locaion and range.
aspectRatio - the aspect ratio, width/height, of the assembled image. If the aspect ratio is greater than or equal to one, the assembled image uses the full width of the canvas; the height used is proportional to the inverse of the aspect ratio. If the aspect ratio is less than one, the full height of the canvas is used; the width used is proportional to the aspect ratio.

The aspect ratio is typically used to maintain consistent width and height units while assembling multiple images into a canvas of a different aspect ratio than the canvas sector, such as drawing a non-square region into a 1024x1024 canvas. An aspect ratio of 1 causes the incoming images to be stretched as necessary in one dimension to match the aspect ratio of the canvas sector.

image - the image to merge into the canvas.
canvas - the canvas into which the images are merged. The canvas is not changed if the specified image and canvas sectors are disjoint.
Throws:
IllegalArgumentException - if the any of the reference arguments are null or the aspect ratio is less than or equal to zero.

openSpatialImage

public static AVList openSpatialImage(File imageFile)
                               throws IOException
Opens a spatial image. Reprojects the image if it is in UTM projection.

Parameters:
imageFile - source image
Returns:
AVList
Throws:
IOException - if there is a problem opening the file.

openSpatialImage

public static AVList openSpatialImage(File imageFile,
                                      int interpolation_mode)
                               throws IOException
Opens a spatial image. Reprojects the image if it is in UTM projection.

Parameters:
imageFile - source image
interpolation_mode - the interpolation mode if the image is reprojected.
Returns:
AVList
Throws:
IOException - if there is a problem opening the file.
WWRuntimeException - if the image type is unsupported.

positionImage

public static Sector positionImage(BufferedImage sourceImage,
                                   Point[] imagePoints,
                                   LatLon[] geoPoints,
                                   BufferedImage destImage)

positionImage3

public static Sector positionImage3(BufferedImage sourceImage,
                                    Point[] imagePoints,
                                    LatLon[] geoPoints,
                                    BufferedImage destImage)

positionImage4

public static Sector positionImage4(BufferedImage sourceImage,
                                    Point[] imagePoints,
                                    LatLon[] geoPoints,
                                    BufferedImage destImage)

reprojectUtmToGeographic

public static void reprojectUtmToGeographic(AVList values,
                                            int mode)
Reprojects an imge in UTM projection to Geo/WGS84.

Parameters:
values - AVList: contains the bufferedimage and the values from the world file. Stores resulting image in values
mode - the interpolation mode if the image is reprojected.

warpImageWithControlPoints

public static Sector warpImageWithControlPoints(BufferedImage sourceImage,
                                                Point2D[] imagePoints,
                                                LatLon[] geoPoints,
                                                BufferedImage destImage)
Convenience method for transforming a georeferenced source image into a geographically aligned destination image. The source image is georeferenced by either three four control points. Each control point maps a location in the source image to a geographic location. This is equivalent to calling {#transformConstrainedImage3} or {#transformConstrainedImage4}, depending on the number of control points.

Parameters:
sourceImage - the source image to transform.
imagePoints - three or four control points in the source image.
geoPoints - three or four geographic locations corresponding to each source control point.
destImage - the destination image to receive the transformed source imnage.
Returns:
bounding sector for the geographically aligned destination image.
Throws:
IllegalArgumentException - if any of sourceImage, destImage, imagePoints or geoPoints is null, or if either imagePoints or geoPoints have length less than 3.

warpImageWithControlPoints3

public static Sector warpImageWithControlPoints3(BufferedImage sourceImage,
                                                 Point2D[] imagePoints,
                                                 LatLon[] geoPoints,
                                                 BufferedImage destImage)
Transforms a georeferenced source image into a geographically aligned destination image. The source image is georeferenced by three control points. Each control point maps a location in the source image to a geographic location.

Parameters:
sourceImage - the source image to transform.
imagePoints - three control points in the source image.
geoPoints - three geographic locations corresponding to each source control point.
destImage - the destination image to receive the transformed source imnage.
Returns:
bounding sector for the geographically aligned destination image.
Throws:
IllegalArgumentException - if any of sourceImage, destImage, imagePoints or geoPoints is null, or if either imagePoints or geoPoints have length less than 3.

warpImageWithControlPoints4

public static Sector warpImageWithControlPoints4(BufferedImage sourceImage,
                                                 Point2D[] imagePoints,
                                                 LatLon[] geoPoints,
                                                 BufferedImage destImage)
Transforms a georeferenced source image into a geographically aligned destination image. The source image is georeferenced by four control points. Each control point maps a location in the source image to a geographic location.

Parameters:
sourceImage - the source image to transform.
imagePoints - four control points in the source image.
geoPoints - four geographic locations corresponding to each source control point.
destImage - the destination image to receive the transformed source imnage.
Returns:
bounding sector for the geographically aligned destination image.
Throws:
IllegalArgumentException - if any of sourceImage, destImage, imagePoints or geoPoints is null, or if either imagePoints or geoPoints have length less than 4.

warpImageWithTransform

public static void warpImageWithTransform(BufferedImage image,
                                          BufferedImage canvas,
                                          Matrix canvasToImageTransform)
Rasterizes the image into the canvas, given a transform that maps canvas coordinates to image coordinates.

Parameters:
image - the source image.
canvas - the image to receive the transformed source image.
canvasToImageTransform - Matrix that maps a canvas coordinates to image coordinates.
Throws:
IllegalArgumentException - if any of image, canvas, or canvasToImageTransform are null.

warpImageWithWorldFile

public static Sector warpImageWithWorldFile(BufferedImage sourceImage,
                                            AVList worldFileParams,
                                            BufferedImage destImage)
Transforms a georeferenced source image into a geographically aligned destination image. The source image is georeferenced by a world file, which defines an affine transform from source coordinates to geographic coordinates.

Parameters:
sourceImage - the source image to transform.
worldFileParams - world file parameters which define an affine transform.
destImage - the destination image to receive the transformed source imnage.
Returns:
bounding sector for the geographically aligned destination image.
Throws:
IllegalArgumentException - if any of sourceImage, destImage or worldFileParams is null.

NASA World Wind