Class PowerOfTwoPaddedImage


  • public class PowerOfTwoPaddedImage
    extends java.lang.Object
    The PowerOfTwoPaddedImage class converts images with non-power-of-two dimensions to images with power-of-two dimensions. The original image is copied into a power-of-two image, where any pixels not covered by the original image are completely transparent. This is a useful property when converting images to OpenGL textures. Non-power-of-two textures are handled inconsistently by graphics hardware. Not all hardware supports them, and many that do lack full support for the the texturing functionality avialable for power-of-two textures.

    PowerOfTwoPaddedImage provides accessors to the converted power-of-two image, the power-of-two image's width and height, and the original image's width and height.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected int height  
      protected java.awt.image.BufferedImage image  
      protected int width  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      protected PowerOfTwoPaddedImage​(java.awt.image.BufferedImage image, int width, int height)  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      static PowerOfTwoPaddedImage fromBufferedImage​(java.awt.image.BufferedImage image)
      Returns a new PowerOfTwoPaddedImage by converting the specified image to a new image with power-of-two dimensions.
      static PowerOfTwoPaddedImage fromPath​(java.lang.String path)
      Returns a new PowerOfTwoPaddedImage from the specified path, or null if the file referenced by path cannot be read, or is not a readable image.
      int getOriginalHeight()
      Returns the height of the original non-power-of-two image, in pixels.
      int getOriginalWidth()
      Returns the width of the original non-power-of-two image, in pixels.
      int getPowerOfTwoHeight()
      Returns the height of the power-of-two image, in pixels.
      java.awt.image.BufferedImage getPowerOfTwoImage()
      Returns a copy of the original image as a BufferedImage with power-of-two dimensions.
      int getPowerOfTwoWidth()
      Returns the width of the power-of-two image, in pixels.
      • Methods inherited from class java.lang.Object

        clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • image

        protected java.awt.image.BufferedImage image
      • width

        protected int width
      • height

        protected int height
    • Constructor Detail

      • PowerOfTwoPaddedImage

        protected PowerOfTwoPaddedImage​(java.awt.image.BufferedImage image,
                                        int width,
                                        int height)
    • Method Detail

      • fromBufferedImage

        public static PowerOfTwoPaddedImage fromBufferedImage​(java.awt.image.BufferedImage image)
        Returns a new PowerOfTwoPaddedImage by converting the specified image to a new image with power-of-two dimensions. Any pixels not covered by the original image are completely transparent. If the specified image has power-of-two dimensions, this maintains a reference to the original image instead of creating a copy. However, if the specified image has no alpha channel, this creates a copy of the original image with an alpha channel, regardless of the specified image's dimensions. This guarantees that the method getPowerOfTwoImage() always returns a BufferedImage with an alpha channel. This is a useful property when converting images to OpenGL texture's, when both power-of-two and non-power-of-two textures must have an alpha channel for consistent handling.
        Parameters:
        image - the BufferedImage to convert to an image with power-of-two dimensions.
        Returns:
        a new PowerOfTwoPaddedImage representing a power-of-two copy of the specified image.
        Throws:
        java.lang.IllegalArgumentException - if the image is null.
      • fromPath

        public static PowerOfTwoPaddedImage fromPath​(java.lang.String path)
        Returns a new PowerOfTwoPaddedImage from the specified path, or null if the file referenced by path cannot be read, or is not a readable image. The path must be a local file path, or a valid resource on the classpath. This uses ImageIO to read the specified path as a BufferedImage. Otherwise, this treats the resultant BufferedImage exactly as fromBufferedImage(java.awt.image.BufferedImage).
        Parameters:
        path - a local file path, or a valid resource on the classpath.
        Returns:
        a new PowerOfTwoPaddedImage representing a power-of-two copy of the image located at the specified path, or null if the image file reference by path cannot be read.
        Throws:
        java.lang.IllegalArgumentException - if the path is null.
      • getOriginalWidth

        public int getOriginalWidth()
        Returns the width of the original non-power-of-two image, in pixels.
        Returns:
        the original image's width, in pixels.
      • getOriginalHeight

        public int getOriginalHeight()
        Returns the height of the original non-power-of-two image, in pixels.
        Returns:
        the original image's height, in pixels.
      • getPowerOfTwoImage

        public java.awt.image.BufferedImage getPowerOfTwoImage()
        Returns a copy of the original image as a BufferedImage with power-of-two dimensions. Any pixels not covered by the original image are completely transparent.
        Returns:
        a copy of the original image as a BufferedImage.
      • getPowerOfTwoWidth

        public int getPowerOfTwoWidth()
        Returns the width of the power-of-two image, in pixels.
        Returns:
        the power-of-two image's width, in pixels.
      • getPowerOfTwoHeight

        public int getPowerOfTwoHeight()
        Returns the height of the power-of-two image, in pixels.
        Returns:
        the power-of-two image's height, in pixels.