Class GrayImage

java.lang.Object
dev.avelar.mapnik.GrayImage
All Implemented Interfaces:
AutoCloseable

public final class GrayImage extends Object implements AutoCloseable
An image with one number per pixel: an elevation grid, a temperature field, a count, a grey picture. It is the data a raster layer would give you, held in memory, so that no GDAL is needed. Colour it with a RasterColorizer to draw it, or read the values back as an array.

 try (GrayImage dem = GrayImage.create(GrayImage.Type.FLOAT32, 256, 256)) {
     dem.write(heights);                        // double[256 * 256], row by row from the top left
     Image picture = RasterColorizer.create().stop(0, "blue").stop(2000, "white").colorize(dem);
 }
 

Values go in and out as doubles. The integer types hold whole numbers in their range, and a value that does not fit is refused with an exception instead of wrapping around. Not thread-safe.

  • Method Details

    • create

      public static GrayImage create(GrayImage.Type type, int width, int height)
      A new image with every pixel 0.
    • create

      public static GrayImage create(GrayImage.Type type, int width, int height, double initial)
      A new image with every pixel set to initial.
    • of

      public static GrayImage of(GrayImage.Type type, int width, int height, double[] values)
      A new image of the given size with these values, row by row from the top left.
    • width

      public int width()
    • height

      public int height()
    • type

      public GrayImage.Type type()
    • get

      public double get(int x, int y)
      One pixel. Throws MapnikException if the position is outside the image.
    • set

      public GrayImage set(int x, int y, double value)
      Set one pixel. Throws MapnikException if the position is outside the image or the value does not fit the type.
    • read

      public double[] read()
      Every pixel, row by row from the top left.
    • write

      public GrayImage write(double[] values)
      Replace every pixel. values must hold width times height numbers, row by row from the top left. If any value does not fit the type, nothing changes.
    • range

      public double[] range()
      The smallest and largest value as {min, max}, ignoring NaN. Throws if there is none.
    • range

      public double[] range(double nodata)
      As range(), also ignoring pixels equal to nodata.
    • colorize

      public Image colorize(RasterColorizer colorizer)
      Colour the image with the stops of colorizer. Same as RasterColorizer.colorize(dev.avelar.mapnik.GrayImage).
    • colorize

      public Image colorize(RasterColorizer colorizer, Double nodata)
      As colorize(RasterColorizer); pixels equal to nodata get the colorizer's default colour.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable