Class Image

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

public final class Image extends Object implements AutoCloseable
A raster image in 8-bit RGBA, such as a rendered map or a picture loaded from a file. Pixels are exposed as ARGB ints, 0xAARRGGBB, the same as BufferedImage.TYPE_INT_ARGB, with straight (not premultiplied) alpha. Holds native memory: close it.
  • Method Details

    • create

      public static Image create(int width, int height)
      A new fully transparent image.
    • load

      public static Image load(Path file)
      Load a PNG, JPEG, TIFF or WebP file, whichever Mapnik was built to read.
    • load

      public static Image load(byte[] data)
      Decode image data held in memory.
    • width

      public int width()
    • height

      public int height()
    • getArgb

      public int getArgb(int x, int y)
      The pixel at (x, y) as 0xAARRGGBB. Throws IndexOutOfBoundsException if outside the image.
    • setArgb

      public Image setArgb(int x, int y, int argb)
    • fill

      public Image fill(String color)
      Set every pixel: a colour name, "#rrggbb" or "rgba(r,g,b,a)".
    • fill

      public Image fill(Color color)
    • filter

      public Image filter(String... filters)
      Apply Mapnik image filters in place, in order. Build them with ImageFilters or write the image-filters syntax yourself, for example "agg-stack-blur(5,5) invert". Throws MapnikException for a filter Mapnik cannot parse; the image is then unchanged.
    • filter

      public Image filter(double scaleFactor, String... filters)
      As filter(String...), with a scale factor for sizes inside the filters, as in RenderOptions.
    • composite

      public Image composite(Image source, int dx, int dy)
      Draw source onto this image with normal blending, its top left corner at (dx, dy). Parts outside are clipped.
    • composite

      public Image composite(Image source, BlendMode mode, double opacity, int dx, int dy)
      Draw source onto this image at (dx, dy) with a blend mode and an opacity from 0 to 1. The source may be any size and is not changed. Parts outside this image are clipped.
    • scaled

      public Image scaled(int width, int height)
      A resized copy, using bilinear resampling.
    • scaled

      public Image scaled(int width, int height, ScalingMethod method)
      A resized copy. This image is not changed. Close the result.
    • warp

      public Image warp(String sourceSrs, Box2d sourceExtent, String targetSrs, Box2d targetExtent, int width, int height, ScalingMethod method)
      Reproject this image, which covers sourceExtent in sourceSrs, into a new image of width x height pixels that covers targetExtent in targetSrs. Areas of the new image that fall outside the source are transparent. Use a small target extent to zoom in, and ScalingMethod.BILINEAR unless you need hard pixels. Close the result.
    • warp

      public Image warp(String sourceSrs, Box2d sourceExtent, String targetSrs, Box2d targetExtent, int width, int height, ScalingMethod method, int meshSize)
      As above, with the size in pixels of the grid the warp is approximated on (Mapnik's default is 16).
    • crop

      public Image crop(int x, int y, int width, int height)
      A copy of part of this image. Throws MapnikException if the area is not inside the image.
    • copy

      public Image copy()
      An independent copy.
    • applyOpacity

      public Image applyOpacity(double opacity)
      Multiply every pixel's alpha by opacity, from 0 to 1.
    • colorToAlpha

      public Image colorToAlpha(Color color)
      Make pixels of this colour transparent. Pixels that only partly match become partly transparent.
    • probe

      public static Image.Info probe(Path file)
      Read the size and format of an image file without decoding it.
    • probe

      public static Image.Info probe(byte[] data)
      Read the size and format of encoded image data without decoding it.
    • isSolid

      public boolean isSolid()
      True if every pixel has the same value.
    • toArgb

      public int[] toArgb()
      All pixels as 0xAARRGGBB, row by row from the top left.
    • toBufferedImage

      public BufferedImage toBufferedImage()
      A copy as a BufferedImage of type TYPE_INT_ARGB. Needs the java.desktop module, which every full JDK has; a class that never calls this does not load it.
    • fromBufferedImage

      public static Image fromBufferedImage(BufferedImage source)
      A new image with the pixels of any BufferedImage, converted to straight ARGB. Close it. Sets one pixel at a time, so a very large picture takes a moment.
    • toBytes

      public byte[] toBytes(String format)
      Encode the image. format is png, jpeg, webp or tiff, optionally with options, such as png8, png256, jpeg90, png:z=9.
    • toBytes

      public byte[] toBytes(ImageFormat format)
      As toBytes(String), with a format built by ImageFormat.
    • save

      public void save(Path file, ImageFormat format)
    • toPng

      public byte[] toPng()
    • save

      public void save(Path file, String format)
    • close

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