Class ImageFormat

java.lang.Object
dev.avelar.mapnik.ImageFormat

public final class ImageFormat extends Object
An image format with its options, built in code instead of written as a string. Mapnik takes a format as text, such as png8:c=64:m=h; this builds that text and checks every value in the range Mapnik accepts, so a typo is an IllegalArgumentException at once instead of an error from deep inside the encoder.

 byte[] png = map.renderToBytes(ImageFormat.png8().colors(64).quantizer(ImageFormat.Quantizer.HEXTREE));
 byte[] jpg = map.renderToBytes(ImageFormat.jpeg(85));
 byte[] webp = map.renderToBytes(ImageFormat.webp().lossless());
 

The plain strings still work wherever a format is taken. option(java.lang.String, java.lang.String) sets any option Mapnik has that this class has no method for.

  • Method Details

    • png

      public static ImageFormat png()
      True-colour PNG with an alpha channel: exact, and larger than png8().
    • png8

      public static ImageFormat png8()
      PNG with a palette of at most 256 colours: much smaller, and fine for maps with flat colours.
    • colors

      public ImageFormat colors(int count)
      PNG8: the number of colours in the palette, from 1 to 256.
    • quantizer

      public ImageFormat quantizer(ImageFormat.Quantizer quantizer)
      PNG8: the algorithm that picks the palette.
    • transparency

      public ImageFormat transparency(ImageFormat.Transparency mode)
      PNG8: what to do with transparency.
    • gamma

      public ImageFormat gamma(double gamma)
      PNG8: the gamma the colours are matched in, 0 or more. Left out, no gamma correction is applied.
    • compression

      public ImageFormat compression(int level)
      PNG and PNG8: zlib compression level, from -1 (zlib's default) or 0 (none) to 9 (smallest, slowest).
    • strategy

      public ImageFormat strategy(ImageFormat.PngStrategy strategy)
      PNG and PNG8: the zlib strategy.
    • jpeg

      public static ImageFormat jpeg(int quality)
      JPEG at a quality from 0 (smallest) to 100 (best). JPEG has no transparency.
    • webp

      public static ImageFormat webp()
      WebP, lossy at quality 90 unless you say otherwise.
    • quality

      public ImageFormat quality(double quality)
      WebP: quality from 0 to 100. Lossy unless lossless().
    • lossless

      public ImageFormat lossless()
      WebP: store the pixels exactly.
    • effort

      public ImageFormat effort(int method)
      WebP: how hard the encoder works, from 0 (fast) to 6 (smallest).
    • alpha

      public ImageFormat alpha(boolean keep)
      WebP: keep (true) or drop (false) the alpha channel.
    • tiff

      public static ImageFormat tiff()
      TIFF, uncompressed unless you say otherwise.
    • compression

      public ImageFormat compression(ImageFormat.TiffCompression compression)
    • layout

      public ImageFormat layout(ImageFormat.TiffLayout layout)
    • zlevel

      public ImageFormat zlevel(int level)
      TIFF with deflate compression: the zlib level, from 0 to 9.
    • rowsPerStrip

      public ImageFormat rowsPerStrip(int rows)
      TIFF, stripped layout: rows in each strip.
    • option

      public ImageFormat option(String key, String value)
      Set an option this class has no method for, by Mapnik's own name, such as image_hint for WebP. The value is passed as it is, and Mapnik rejects what it does not accept when the image is encoded.
    • toString

      public String toString()
      The format as Mapnik writes it, such as png8:c=64:m=h.
      Overrides:
      toString in class Object