Class ImageBuilder


  • public final class ImageBuilder
    extends java.lang.Object
    A utility class primary intended for storing data obtained by reading image files.
    • Constructor Summary

      Constructors 
      Constructor Description
      ImageBuilder​(int width, int height, boolean hasAlpha)
      Constructs an ImageBuilder instance.
      ImageBuilder​(int width, int height, boolean hasAlpha, boolean isAlphaPremultiplied)
      Constructs an ImageBuilder instance.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private void checkBounds​(int x, int y, int w, int h)
      Performs a check on the specified sub-region to verify that it is within the constraints of the ImageBuilder bounds.
      private void checkDimensions​(int width, int height)
      Checks for valid dimensions and throws RasterFormatException if the inputs are invalid.
      java.awt.image.BufferedImage getBufferedImage()
      Create a BufferedImage using the data stored in the ImageBuilder.
      int getHeight()
      Gets the height of the ImageBuilder pixel field
      int getRgb​(int x, int y)
      Gets the RGB or ARGB value for the pixel at the position (x,y) within the image builder pixel field.
      java.awt.image.BufferedImage getSubimage​(int x, int y, int w, int h)
      Gets a subimage from the ImageBuilder using the specified parameters.
      ImageBuilder getSubset​(int x, int y, int w, int h)
      Gets a subset of the ImageBuilder content using the specified parameters to indicate an area of interest.
      int getWidth()
      Gets the width of the ImageBuilder pixel field
      private java.awt.image.BufferedImage makeBufferedImage​(int[] argb, int w, int h, boolean useAlpha)  
      void setRgb​(int x, int y, int argb)
      Sets the RGB or ARGB value for the pixel at position (x,y) within the image builder pixel field.
      • Methods inherited from class java.lang.Object

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

      • data

        private final int[] data
      • width

        private final int width
      • height

        private final int height
      • hasAlpha

        private final boolean hasAlpha
      • isAlphaPremultiplied

        private final boolean isAlphaPremultiplied
    • Constructor Detail

      • ImageBuilder

        public ImageBuilder​(int width,
                            int height,
                            boolean hasAlpha)
        Constructs an ImageBuilder instance.
        Parameters:
        width - the width of the image to be built
        height - the height of the image to be built
        hasAlpha - indicates whether the image has an alpha channel (the selection of alpha channel does not change the memory requirements for the ImageBuilder or resulting BufferedImage.
        Throws:
        java.awt.image.RasterFormatException - if width or height are equal or less than zero
      • ImageBuilder

        public ImageBuilder​(int width,
                            int height,
                            boolean hasAlpha,
                            boolean isAlphaPremultiplied)
        Constructs an ImageBuilder instance.
        Parameters:
        width - the width of the image to be built
        height - the height of the image to be built
        hasAlpha - indicates whether the image has an alpha channel (the selection of alpha channel does not change the memory requirements for the ImageBuilder or resulting BufferedImage.
        isAlphaPremultiplied - indicates whether alpha values are pre-multiplied; this setting is relevant only if alpha is true.
        Throws:
        java.awt.image.RasterFormatException - if width or height are equal or less than zero
    • Method Detail

      • checkBounds

        private void checkBounds​(int x,
                                 int y,
                                 int w,
                                 int h)
        Performs a check on the specified sub-region to verify that it is within the constraints of the ImageBuilder bounds.
        Parameters:
        x - the X coordinate of the upper-left corner of the specified rectangular region
        y - the Y coordinate of the upper-left corner of the specified rectangular region
        w - the width of the specified rectangular region
        h - the height of the specified rectangular region
        Throws:
        java.awt.image.RasterFormatException - if width or height are equal or less than zero, or if the subimage is outside raster (on x or y axis)
      • checkDimensions

        private void checkDimensions​(int width,
                                     int height)
        Checks for valid dimensions and throws RasterFormatException if the inputs are invalid.
        Parameters:
        width - image width (must be greater than zero)
        height - image height (must be greater than zero)
        Throws:
        java.awt.image.RasterFormatException - if width or height are equal or less than zero
      • getBufferedImage

        public java.awt.image.BufferedImage getBufferedImage()
        Create a BufferedImage using the data stored in the ImageBuilder.
        Returns:
        a valid BufferedImage.
      • getHeight

        public int getHeight()
        Gets the height of the ImageBuilder pixel field
        Returns:
        a positive integer
      • getRgb

        public int getRgb​(int x,
                          int y)
        Gets the RGB or ARGB value for the pixel at the position (x,y) within the image builder pixel field. For performance reasons no bounds checking is applied.
        Parameters:
        x - the X coordinate of the pixel to be read
        y - the Y coordinate of the pixel to be read
        Returns:
        the RGB or ARGB pixel value
      • getSubimage

        public java.awt.image.BufferedImage getSubimage​(int x,
                                                        int y,
                                                        int w,
                                                        int h)
        Gets a subimage from the ImageBuilder using the specified parameters. If the parameters specify a rectangular region that is not entirely contained within the bounds defined by the ImageBuilder, this method will throw a RasterFormatException. This runtime-exception behavior is consistent with the behavior of the getSubimage method provided by BufferedImage.
        Parameters:
        x - the X coordinate of the upper-left corner of the specified rectangular region
        y - the Y coordinate of the upper-left corner of the specified rectangular region
        w - the width of the specified rectangular region
        h - the height of the specified rectangular region
        Returns:
        a BufferedImage that constructed from the data within the specified rectangular region
        Throws:
        java.awt.image.RasterFormatException - f the specified area is not contained within this ImageBuilder
      • getSubset

        public ImageBuilder getSubset​(int x,
                                      int y,
                                      int w,
                                      int h)
        Gets a subset of the ImageBuilder content using the specified parameters to indicate an area of interest. If the parameters specify a rectangular region that is not entirely contained within the bounds defined by the ImageBuilder, this method will throw a RasterFormatException. This run- time exception is consistent with the behavior of the getSubimage method provided by BufferedImage.
        Parameters:
        x - the X coordinate of the upper-left corner of the specified rectangular region
        y - the Y coordinate of the upper-left corner of the specified rectangular region
        w - the width of the specified rectangular region
        h - the height of the specified rectangular region
        Returns:
        a valid instance of the specified width and height.
        Throws:
        java.awt.image.RasterFormatException - if the specified area is not contained within this ImageBuilder
      • getWidth

        public int getWidth()
        Gets the width of the ImageBuilder pixel field
        Returns:
        a positive integer
      • makeBufferedImage

        private java.awt.image.BufferedImage makeBufferedImage​(int[] argb,
                                                               int w,
                                                               int h,
                                                               boolean useAlpha)
      • setRgb

        public void setRgb​(int x,
                           int y,
                           int argb)
        Sets the RGB or ARGB value for the pixel at position (x,y) within the image builder pixel field. For performance reasons, no bounds checking is applied.
        Parameters:
        x - the X coordinate of the pixel to be set.
        y - the Y coordinate of the pixel to be set.
        argb - the RGB or ARGB value to be stored.
        Throws:
        java.lang.ArithmeticException - if the index computation overflows an int.
        java.lang.IllegalArgumentException - if the resulting index is illegal.