Hide navigation sidebar
Hide table of contents sidebar
Skip to content
Back to top
View this page
Toggle table of contents sidebar

# `Pixmap`

A `Pixmap` object contains a color raster image (short for pixel map). The components in a pixel in the `Pixmap` are all byte values, with the transparency as the last component. A `Pixmap` also has a location (x, y) in addition to its size; so that they can easily be used to represent tiles of a page.

new Pixmap(colorspace, bounds, alpha)

  Constructor method.

  Create a new pixmap. The pixel data is not initialized; and will contain garbage.

  Arguments:

    - colorspace – `ColorSpace`.

    - bounds – `[ulx,uly,lrx,lry]` Rectangle.

    - alpha – `Boolean`.

  Returns:

    `Pixmap`.

  EXAMPLE

      var pixmap = new mupdf.Pixmap(mupdf.ColorSpace.DeviceRGB, [0,0,100,100], true);

Instance methods

clear(value)

  Clear the pixels to the specified value. Pass `255` for white, or omit for transparent.

  Arguments:

    - value – Pixel value.

  EXAMPLE

      pixmap.clear(255);
      pixmap.clear();

getBounds()

  Return the pixmap bounds.

  Returns:

    `[ulx,uly,lrx,lry]` Rectangle.

  EXAMPLE

      var rect = pixmap.getBounds();

getWidth()
  Returns:

    `Int` The width value.

  EXAMPLE

      var w = pixmap.getWidth();

getHeight()
  Returns:

    `Int` The height value.

  EXAMPLE

      var h = pixmap.getHeight();

getNumberOfComponents()

  Number of colors; plus one if an alpha channel is present.

  Returns:

    `Int` Number of color components.

  EXAMPLE

      var num = pixmap.getNumberOfComponents();

getAlpha()

  True if alpha channel is present.

  Returns:

    `Boolean`.

  EXAMPLE

      var alpha = pixmap.getAlpha();

getStride()

  Number of bytes per row.

  Returns:

    `Int`.

  EXAMPLE

      var stride = pixmap.getStride();

getColorSpace()

  Returns the `ColorSpace` for the `Pixmap`.

  Returns:

    `ColorSpace`.

  EXAMPLE

      var cs = pixmap.getColorSpace();

setResolution(xRes, yRes)

  Set `x` & `y` resolution.

  Arguments:

    - xRes – `Int` X resolution in dots per inch.

    - yRes – `Int` Y resolution in dots per inch.

  EXAMPLE

      pixmap.setResolution(300, 300);

getXResolution()

  Returns the `x` resolution for the `Pixmap`.

  Returns:

    `Int` Resolution in dots per inch.

  EXAMPLE

      var xRes = pixmap.getXResolution();

getYResolution()

  Returns the `y` resolution for the `Pixmap`.

  Returns:

    `Int` Resolution in dots per inch.

  EXAMPLE

      var yRes = pixmap.getYResolution();

getSample(x, y, index)

  mutool only

  Get the value of component `index` at position `x`, `y` (relative to the image origin: 0, 0 is the top left pixel).

  Arguments:

    - x – X co-ordinate.

    - y – Y co-ordinate.

    - index – Component index. i.e. For CMYK ColorSpaces 0 = Cyan, for RGB 0 = Red etc.

  Returns:

    `Int`.

  EXAMPLE

      var sample = pixmap.getSample(0,0,0);

saveAsPNG(fileName)

  mutool only

  Save the `Pixmap` as a PNG. Only works for Gray and RGB images.

  Arguments:

    - fileName – `String`.

  EXAMPLE

      pixmap.saveAsPNG("fileName.png");

saveAsJPEG(fileName, quality)

  mutool only

  Save the `Pixmap` as a JPEG. Only works for Gray, RGB and CMYK images.

  Arguments:

    - fileName – `String`.

    - quality – `Int`.

  EXAMPLE

      pixmap.saveAsJPEG("fileName.jpg", 80);

saveAsPAM(fileName)

  mutool only

  Save the `Pixmap` as a PAM.

  Arguments:

    - fileName – `String`.

  EXAMPLE

      pixmap.saveAsPAM("fileName.pam");

saveAsPNM(fileName)

  mutool only

  Save the `Pixmap` as a PNM. Only works for Gray and RGB images without alpha.

  Arguments:

    - fileName – `String`.

  EXAMPLE

      pixmap.saveAsPNM("fileName.pnm");

saveAsPBM(fileName)

  mutool only

  Save the `Pixmap` as a PBM. Only works for Gray and RGB images without alpha.

  Arguments:

    - fileName – `String`.

  EXAMPLE

      pixmap.saveAsPBM("fileName.pbm");

saveAsPKM(fileName)

  mutool only

  Save the `Pixmap` as a PKM. Only works for Gray and RGB images without alpha.

  Arguments:

    - fileName – `String`.

  EXAMPLE

      pixmap.saveAsPKM("fileName.pkm");

invert()

  Invert all pixels. All components are processed, except alpha which is unchanged.

  EXAMPLE

      pixmap.invert();

invertLuminance()

  Transform all pixels so that luminance of each pixel is inverted, and the chrominance remains as unchanged as possible. All components are processed, except alpha which is unchanged.

  EXAMPLE

      pixmap.invertLuminance();

gamma(gamma)

  Apply gamma correction to `Pixmap`. All components are processed, except alpha which is unchanged.

  Values `>= 0.1 & < 1` = darken, `> 1 & < 10` = lighten.

  Arguments:

    - gamma – `Float`.

  EXAMPLE

      pixmap.gamma(3);

tint(black, white)
  Tint all pixels in a RGB, BGR or Gray `Pixmap`.

    Map black and white respectively to the given hex RGB values.

  Arguments:

    - black – `Integer`.

    - white – `Integer`.

  EXAMPLE

      pixmap.tint(0xffff00, 0xffff00);

warp(points, width, height)

  Return a warped subsection of the `Pixmap`, where the result has the requested dimensions.

  Arguments:

    - points – `[x0, y0, x1, y1, x2, y2, x3, y3, x4, y4]`

  Points give the corner points of a convex quadrilateral within the `Pixmap` to be warped. :arg width: `Int`. :arg height: `Int`.

  Returns:

    `Pixmap`.

  EXAMPLE

      var warpedPixmap = pixmap.warp([0,0,100,0,0,100,100,100],200,200);

convertToColorSpace(colorspace, proof, defaultColorSpaces, colorParams, keepAlpha)

  Convert pixmap into a new pixmap of a desired colorspace. A proofing colorspace, a set of default colorspaces and color parameters used during conversion may be specified. Finally a boolean indicates if alpha should be preserved (default is to not preserve alpha).

  Arguments:

    - colorspace – `Colorspace`.

    - proof – `Colorspace`.

    - defaultColorSpaces – `DefaultColorSpaces`.

    - colorParams – `[]`.

    - keepAlpha – `Boolean`.

  Returns:

    `Pixmap`.

getPixels()

  wasm only

  Returns an array of pixels for the `Pixmap`.

  Returns:

    `[...]`.

  EXAMPLE

      var pixels = pixmap.getPixels();

asPNG()

  wasm only

  Returns a buffer of the `Pixmap` as a PNG.

  Returns:

    `Buffer`.

  EXAMPLE

      var buffer = pixmap.asPNG();

asPSD()

  wasm only

  Returns a buffer of the `Pixmap` as a PSD.

  Returns:

    `Buffer`.

  EXAMPLE

      var buffer = pixmap.asPSD();

asPAM()

  wasm only

  Returns a buffer of the `Pixmap` as a PAM.

  Returns:

    `Buffer`.

  EXAMPLE

      var buffer = pixmap.asPAM();

asJPEG(quality)

  wasm only

  Returns a buffer of the `Pixmap` as a JPEG. Note, if the `Pixmap` has an alpha channel then an exception will be thrown.

  Returns:

    `Buffer`.

  EXAMPLE

      var buffer = pixmap.asJPEG(80);

skewDetect()

  wasm only

  Returns the angle of skew detected from `Pixmap`. Note, if the `Pixmap` is not Greyscale with no alpha then an exception will be thrown.

  Returns:

    `Float`.

  EXAMPLE

      var angle = pixmap.skewDetect();

deskew(angle, border)

  wasm only

  Returns a new `Pixmap` being the deskewed version of the supplied `Pixmap`. Note, if a `Pixmap` is supplied that is not RGB or Greyscale, or has alpha then an exception will be thrown.

  Arguments:

    - angle – `Float`. The angle to deskew.

    - border – `String`. “increase” increases the size of the pixmap so no pixels are lost. “maintain” maintains the size of the pixmap. “decrease” decreases the size of the page so no new pixels are shown.

  Returns:

    `Pixmap`.

  EXAMPLE

      var deskewed = pixmap.deskew(angle, 0);