Class ContourPlot.Img

java.lang.Object
gov.nih.mipav.model.algorithms.ContourPlot.Img
All Implemented Interfaces:
ContourPlot.ImgBase<ContourPlot.Pixel>, Iterable<ContourPlot.Pixel>
Enclosing class:
ContourPlot

public class ContourPlot.Img extends Object implements ContourPlot.ImgBase<ContourPlot.Pixel>
Image class with data stored in an int array.

In contrast to BufferedImage the Img class only offers pixel data to be stored as integer values simplifying data retrieval and increasing performance due to less overhead and omitting color model conversions.
However the Img class can be easily used together with BufferedImages offering convenience methods like Img(BufferedImage), ContourPlot.ImgBase.toBufferedImage() or createRemoteImg(BufferedImage).

Moreover the Img class targets lambda expressions introduced in Java 8 useful for per pixel operations by implementing the Iterable interface and providing

Since version 1.1 it is also possible to iterate over a specified area of the Img using

Here is an example of a parallelized per pixel operation:

 
 Img img = new Img(1024, 1024);
 img.forEach(true, px -> {
     double x = (px.getX()-512)/512.0;
     double y = (px.getY()-512)/512.0;
     double len = Math.max(Math.abs(x),Math.abs(y));
     double angle = (Math.atan2(x,y)+Math.PI)*(180/Math.PI);

     double r = 255*Math.max(0,1-Math.abs((angle-120)/120.0));
     double g = 255*Math.max(0, 1-Math.abs((angle-240)/120.0));
     double b = 255*Math.max(0, angle <= 120 ?
          1-Math.abs((angle)/120.0):1-Math.abs((angle-360)/120.0));

     px.setRGB((int)(r*(1-len)), (int)(g*(1-len)), (int)(b*(1-len)));
 });
 ImageSaver.saveImage(img.getRemoteBufferedImage(), "polar_colors.png");
 
Since:
1.0
Author:
hageldave
  • Field Details

    • boundary_mode_zero

      public static final int boundary_mode_zero
      boundary mode that will return 0 for out of bounds positions.
      Since:
      1.0
      See Also:
    • boundary_mode_repeat_edge

      public static final int boundary_mode_repeat_edge
      boundary mode that will repeat the edge of of an Img for out of bounds positions.
      Since:
      1.0
      See Also:
    • boundary_mode_repeat_image

      public static final int boundary_mode_repeat_image
      boundary mode that will repeat the Img for out of bounds positions.
      Since:
      1.0
      See Also:
    • boundary_mode_mirror

      public static final int boundary_mode_mirror
      boundary mode that will mirror the Img for out of bounds positions
      Since:
      1.0
      See Also:
    • data

      private final int[] data
      data array of this Img containing a value for each pixel in row major order
      Since:
      1.0
    • width

      private final int width
      width and height of this image
    • height

      private final int height
      width and height of this image
    • spliteratorMinimumSplitSize

      private int spliteratorMinimumSplitSize
      minimum number of elements this Img's Spliterators can be split to. Default value is 1024.
      Since:
      1.3
  • Constructor Details

    • Img

      public Img(int width, int height)
      Creates a new Img of specified dimensions. Values are initialized to 0.
      Parameters:
      width - of the Img
      height - of the Img
      Since:
      1.0
    • Img

      public Img(Dimension dimension)
      Creates a new Img of specified Dimension. Values are initilaized to 0.
      Parameters:
      dimension - extend of the Img (width and height)
      Since:
      1.0
    • Img

      public Img(BufferedImage bimg)
      Creates a new Img of same dimensions as provided BufferedImage. Values are copied from argument Image
      Parameters:
      bimg - the BufferedImage
      Since:
      1.0
      See Also:
    • Img

      public Img(int width, int height, int[] data)
      Creates a new Img of specified dimensions. Provided data array will be used as this images data.
      Parameters:
      width - of the Img
      height - of the Img
      data - values (pixels) that will be used as the content of this Img
      Throws:
      IllegalArgumentException - when the number of pixels of this Img resulting from width*height does not match the number of provided data values.
      Since:
      1.0
    • Img

      public Img(Dimension dim, int[] data)
      Creates a new Img of specified dimensions. Provided data array will be used as this images data.
      Parameters:
      dim - extend of the image (width and height)
      data - values (pixels) that will be used as the content of this Img
      Throws:
      IllegalArgumentException - when the number of pixels of this Img resulting from width*height does not match the number of provided data values.
      Since:
      1.0
  • Method Details

    • getWidth

      public int getWidth()
      Specified by:
      getWidth in interface ContourPlot.ImgBase<ContourPlot.Pixel>
      Returns:
      width of this Img
      Since:
      1.0
      See Also:
    • getHeight

      public int getHeight()
      Specified by:
      getHeight in interface ContourPlot.ImgBase<ContourPlot.Pixel>
      Returns:
      height of this Img
      Since:
      1.0
      See Also:
    • numValues

      public int numValues()
      Specified by:
      numValues in interface ContourPlot.ImgBase<ContourPlot.Pixel>
      Returns:
      number of values (pixels) of this Img
      Since:
      1.0
      See Also:
    • getData

      public int[] getData()
      Returns:
      data array of this Img
      Since:
      1.0
    • getValue

      public int getValue(int x, int y)
      Returns the value of this Img at the specified position. No bounds checks will be performed, positions outside of this image's dimension can either result in a value for a different position or an ArrayIndexOutOfBoundsException.
      Parameters:
      x - coordinate
      y - coordinate
      Returns:
      value for specified position
      Throws:
      ArrayIndexOutOfBoundsException - if resulting index from x and y is not within the data arrays bounds.
      Since:
      1.0
      See Also:
    • getValue

      public int getValue(int x, int y, int boundaryMode)
      Returns the value of this Img at the specified position. Bounds checks will be performed and positions outside of this image's dimensions will be handled according to the specified boundary mode.

      Boundary Modes
      boundary_mode_zero
      will return 0 for out of bounds positions.
      -boundary_mode_repeat_edge
      will return the same value as the nearest edge value.
      -boundary_mode_repeat_image
      will return a value of the image as if the if the image was repeated on all sides.
      -boundary_mode_mirror
      will return a value of the image as if the image was mirrored on all sides.
      -other values for boundary mode
      will be used as default color for out of bounds positions. It is safe to use opaque colors (0xff000000 - 0xffffffff) and transparent colors above 0x0000000f which will not collide with one of the boundary modes (number of boundary modes is limited to 16 for the future).

      Parameters:
      x - coordinate
      y - coordinate
      boundaryMode - one of the boundary modes e.g. boundary_mode_mirror
      Returns:
      value at specified position or a value depending on the boundary mode for out of bounds positions.
      Since:
      1.0
    • interpolateARGB

      public int interpolateARGB(double xNormalized, double yNormalized)
      Returns a bilinearly interpolated ARGB value of the image for the specified normalized position (x and y within [0,1]). Position {0,0} denotes the image's origin (top left corner), position {1,1} denotes the opposite corner (pixel at {width-1, height-1}).

      An ArrayIndexOutOfBoundsException may be thrown for x and y greater than 1 or less than 0.

      Parameters:
      xNormalized - coordinate within [0,1]
      yNormalized - coordinate within [0,1]
      Returns:
      bilinearly interpolated ARGB value.
      Throws:
      ArrayIndexOutOfBoundsException - when a resulting index is out of the data array's bounds, which can only happen for x and y values less than 0 or greater than 1.
      Since:
      1.0
    • interpolateColors

      private int interpolateColors(int c00, int c01, int c10, int c11, double mx, double my)
    • blend

      private int blend(int channel1, int channel2, double m)
    • getPixel

      public ContourPlot.Pixel getPixel()
      Creates a new Pixel object for this Img with position {0,0}.
      Specified by:
      getPixel in interface ContourPlot.ImgBase<ContourPlot.Pixel>
      Returns:
      a Pixel object for this Img.
      Since:
      1.0
      See Also:
    • getPixel

      public ContourPlot.Pixel getPixel(int x, int y)
      Creates a new Pixel object for this Img at specified position. No bounds checks are performed for x and y.

      Tip:
      Do not use this method repeatedly while iterating the image. Use ContourPlot.Pixel.setPosition(int, int) instead to avoid excessive allocation of Pixel objects.

      You can also use for(Pixel px: img){...} syntax or the ContourPlot.ImgBase.forEach(Consumer) method to iterate this image.

      Specified by:
      getPixel in interface ContourPlot.ImgBase<ContourPlot.Pixel>
      Parameters:
      x - coordinate
      y - coordinate
      Returns:
      a Pixel object for this Img at {x,y}.
      Since:
      1.0
      See Also:
    • copyArea

      public ContourPlot.Img copyArea(int x, int y, int w, int h, ContourPlot.Img dest, int destX, int destY)
      Copies specified area of this Img to the specified destination Img at specified destination coordinates. If destination Img is null a new Img with the areas size will be created and the destination coordinates will be ignored so that the Img will contain all the values of the area.

      The specified area has to be within the bounds of this image or otherwise an IllegalArgumentException will be thrown. Only the intersecting part of the area and the destination image is copied which allows for an out of bounds destination area origin.

      Parameters:
      x - area origin in this image (x-coordinate)
      y - area origin in this image (y-coordinate)
      w - width of area
      h - height of area
      dest - destination Img
      destX - area origin in destination Img (x-coordinate)
      destY - area origin in destination Img (y-coordinate)
      Returns:
      the destination Img
      Throws:
      IllegalArgumentException - if the specified area is not within the bounds of this Img or if the size of the area is not positive.
      Since:
      1.0
    • setValue

      public void setValue(int x, int y, int value)
      Sets value at the specified position. No bounds checks will be performed, positions outside of this images dimension can either result in a value for a different position or an ArrayIndexOutOfBoundsException.
      Parameters:
      x - coordinate
      y - coordinate
      value - to be set at specified position. e.g. 0xff0000ff for blue color
      Throws:
      ArrayIndexOutOfBoundsException - if resulting index from x and y is not within the data arrays bounds.
      Since:
      1.0
      See Also:
    • fill

      public ContourPlot.Img fill(int value)
      Fills the whole image with the specified value.
      Parameters:
      value - for filling image
      Returns:
      this for chaining
      Since:
      1.0
    • copy

      public ContourPlot.Img copy()
      Description copied from interface: ContourPlot.ImgBase
      Returns a deep copy of this image. 'Deep' means that changes made to this image are NOT reflected in the copy.
      Specified by:
      copy in interface ContourPlot.ImgBase<ContourPlot.Pixel>
      Returns:
      a deep copy of this Img.
      Since:
      1.0
    • toBufferedImage

      public BufferedImage toBufferedImage(BufferedImage bimg)
      Description copied from interface: ContourPlot.ImgBase
      Copies this image's data to the specified BufferedImage. This method will preserve the Raster of the specified BufferedImage and will only modify the contents of it.
      Specified by:
      toBufferedImage in interface ContourPlot.ImgBase<ContourPlot.Pixel>
      Parameters:
      bimg - the BufferedImage
      Returns:
      the specified BufferedImage
      See Also:
    • getRemoteBufferedImage

      public BufferedImage getRemoteBufferedImage()
      Creates a BufferedImage that shares the data of this Img. Changes in this Img are reflected in the created BufferedImage and vice versa. The created BufferedImage uses an ARGB DirectColorModel with an underlying DataBufferInt (similar to BufferedImage.TYPE_INT_ARGB)
      Specified by:
      getRemoteBufferedImage in interface ContourPlot.ImgBase<ContourPlot.Pixel>
      Returns:
      BufferedImage sharing this Img's data.
      Since:
      1.0
      See Also:
    • supportsRemoteBufferedImage

      public boolean supportsRemoteBufferedImage()
      Description copied from interface: ContourPlot.ImgBase
      Returns true when this implementation of ContourPlot.ImgBase supports the ContourPlot.ImgBase.getRemoteBufferedImage() method. This by default also indicates the support for the following methods:
      Specified by:
      supportsRemoteBufferedImage in interface ContourPlot.ImgBase<ContourPlot.Pixel>
      Returns:
      true when supported, false otherwise.
    • createRemoteImg

      public ContourPlot.Img createRemoteImg(BufferedImage bimg)
      Creates an Img sharing the specified BufferedImage's data. Changes in the BufferdImage are reflected in the created Img and vice versa.

      Only BufferedImages with DataBuffer of DataBuffer.TYPE_INT can be used since the Img class uses an int[] to store its data. An IllegalArgumentException will be thrown if a BufferedImage with a different DataBufferType is provided.

      Parameters:
      bimg - BufferedImage with TYPE_INT DataBuffer.
      Returns:
      Throws:
      IllegalArgumentException - if a BufferedImage with a DataBufferType other than DataBuffer.TYPE_INT is provided.
      Since:
      1.0
      See Also:
    • getSpliteratorMinimumSplitSize

      public int getSpliteratorMinimumSplitSize()
      Returns the minimum number of elements in a split of a Spliterator of this Img. Spliterators will only split if they contain more elements than specified by this value. Default is 1024.
      Specified by:
      getSpliteratorMinimumSplitSize in interface ContourPlot.ImgBase<ContourPlot.Pixel>
      Returns:
      minimum number of elements of a Spliterator to allow for splitting.
      Since:
      1.3
    • setSpliteratorMinimumSplitSize

      public void setSpliteratorMinimumSplitSize(int size)
      Sets the minimum number of elements in a split of a Spliterator of this Img. Spliterators will only split if they contain more elements than specified by this value. Default is 1024.

      It is advised that this number is chosen carefully and with respect to the Img's size and application of the spliterator, as it can decrease performance of the parallelized methods
      ContourPlot.ImgBase.forEach(boolean parallel, Consumer action),
      ContourPlot.ImgBase.forEach(boolean parallel, int x, int y, int w, int h, Consumer action) or
      ContourPlot.ImgBase.stream(boolean parallel) etc.
      Small values cause a Spliterator to be split more often which will consume more memory compared to higher values. Special applications on small Imgs using sophisticated consumers or stream operations may justify the use of small split sizes. High values cause a Spliterator to be split less often which may cause the work items to be badly apportioned among the threads and lower throughput.

      Parameters:
      size - the minimum number of elements a split covers
      Throws:
      IllegalArgumentException - if specified size is less than 1
      Since:
      1.3