Class Resampler

java.lang.Object
com.codename1.backend.image.Resampler

public final class Resampler extends Object

Resampling of interleaved 8-bit band data, the two-pass fixed-point convolution of Pillow's Resample.c.

The results match Pillow byte for byte, which is why the Python runtime's PIL.Image.resize delegates here. Pixels are bands consecutive bytes (L, LA, RGB, RGBA, ...), rows packed with no padding. Images with alpha should be premultiplied first ([#premultiply(byte[], int)]) and unpremultiplied afterwards, as Pillow does, so transparent pixels do not bleed their color into their neighbours.

RasterImage.scale(int, int, int) wraps this for ARGB images.

  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    Cubic interpolation, Pillow's a = -0.5 kernel.
    static final int
    Linear interpolation (a triangle filter).
    static final int
    Box filter: every input pixel under an output pixel weighs the same.
    static final int
    Hamming-windowed sinc with a support of one pixel.
    static final int
    Lanczos, a windowed sinc with a support of three pixels.
    static final int
    Nearest neighbour: each output pixel takes the input pixel under its center.
  • Method Summary

    Modifier and Type
    Method
    Description
    static byte[]
    premultiply(byte[] d, int bands)
    Premultiplies the color bands by the last (alpha) band, Pillow's rounding (RGBA to RGBa).
    static byte[]
    resample(byte[] src, int w, int h, int bands, int nw, int nh, double[] box, int filter)
    Resamples the region box (x0, y0, x1, y1, in source pixels, may be fractional) of a w x h image to nw x nh.
    static byte[]
    unpremultiply(byte[] d, int bands)
    Reverses [#premultiply(byte[], int)] in place, as Pillow's rgbA2rgba.

    Methods inherited from class Object

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

    • NEAREST

      public static final int NEAREST
      Nearest neighbour: each output pixel takes the input pixel under its center.
      See Also:
    • LANCZOS

      public static final int LANCZOS
      Lanczos, a windowed sinc with a support of three pixels.
      See Also:
    • BILINEAR

      public static final int BILINEAR
      Linear interpolation (a triangle filter).
      See Also:
    • BICUBIC

      public static final int BICUBIC
      Cubic interpolation, Pillow's a = -0.5 kernel.
      See Also:
    • BOX

      public static final int BOX
      Box filter: every input pixel under an output pixel weighs the same.
      See Also:
    • HAMMING

      public static final int HAMMING
      Hamming-windowed sinc with a support of one pixel.
      See Also:
  • Method Details

    • resample

      public static byte[] resample(byte[] src, int w, int h, int bands, int nw, int nh, double[] box, int filter)

      Resamples the region box (x0, y0, x1, y1, in source pixels, may be fractional) of a w x h image to nw x nh.

      Parameters
      • src: the source pixels, w * h * bands bytes
      • w: source width
      • h: source height
      • bands: bytes per pixel
      • nw: output width
      • nh: output height
      • box: the source region, four values
      • filter: one of the filter constants of this class
      Returns

      a new array of nw * nh * bands bytes

    • premultiply

      public static byte[] premultiply(byte[] d, int bands)

      Premultiplies the color bands by the last (alpha) band, Pillow's rounding (RGBA to RGBa).

      Parameters
      • d: interleaved pixels whose last band is alpha
      • bands: bytes per pixel
      Returns

      a new premultiplied array

    • unpremultiply

      public static byte[] unpremultiply(byte[] d, int bands)

      Reverses [#premultiply(byte[], int)] in place, as Pillow's rgbA2rgba.

      Parameters
      • d: interleaved premultiplied pixels whose last band is alpha
      • bands: bytes per pixel
      Returns

      d