...master 2026-11-12 (1037e2069+unstaged)

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.

Constructors

class Pixmap(colorspace, bbox, alpha)

class Pixmap(pixmap, mask)

Create a new empty Pixmap whose pixel data is not initialized. Alternatively create a new Pixmap based on an existing Pixmap without alpha and combine it with a single component soft mask of the same dimensions.

var pixmap1 = new mupdf.Pixmap(mupdf.ColorSpace.DeviceRGB, [0, 0, 100, 100], true)
var pixmap2 = new mupdf.Pixmap(
        new mupdf.Image("photo.png").toPixmap(),
        new mupdf.Image("softmask.png").toPixmap()
)

Instance methods

Pixmap.prototype.clear(value)

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

pixmap.clear(255)

Pixmap.prototype.getBounds()

Return the pixmap bounds.

var rect = pixmap.getBounds()

Pixmap.prototype.getWidth()

Get the width of the pixmap.

var w = pixmap.getWidth()

Pixmap.prototype.getHeight()

Get the height of the pixmap.

var h = pixmap.getHeight()

Pixmap.prototype.getNumberOfComponents()

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

var num = pixmap.getNumberOfComponents()

Pixmap.prototype.getAlpha()

Returns whether an alpha channel is present.

var alpha = pixmap.getAlpha()

Pixmap.prototype.getStride()

Number of bytes per row.

var stride = pixmap.getStride()

Pixmap.prototype.getColorSpace()

Returns the colorspace of this pixmap. Returns null if the pixmap has no colors (for example if it is an opacity mask with only an alpha channel).

var cs = pixmap.getColorSpace()

Pixmap.prototype.setResolution(x, y)

Set horizontal and vertical resolution.

pixmap.setResolution(300, 300)

Pixmap.prototype.getX()

Returns the x coordinate of the pixmap.

var x = pixmap.getX()

Pixmap.prototype.getY()

Returns the y coordinate of the pixmap.

var y = pixmap.getY()

Pixmap.prototype.getXResolution()

Returns the horizontal resolution in dots per inch for this pixmap.

var xRes = pixmap.getXResolution()

Pixmap.prototype.getYResolution()

Returns the vertical resolution in dots per inch for this pixmap.

var yRes = pixmap.getYResolution()

Pixmap.prototype.invert()

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

pixmap.invert()

Pixmap.prototype.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.

pixmap.invertLuminance()

Pixmap.prototype.gamma(p)

Apply gamma correction to this pixmap. All components are processed, except alpha which is unchanged.

Values >= 0.1 & < 1 darkens the pixmap, > 1 & < 10 lightens the pixmap.

pixmap.gamma(3.5)

Pixmap.prototype.tint(black, white)

Tint all pixels in RGB, BGR or Gray pixmaps. Map black and white respectively to the given hex RGB values.

pixmap.tint(0xffff00, 0xffff00)

Pixmap.prototype.warp(points, width, height)

Return a warped subsection of this pixmap, where the corner of the input quadrilateral will be “warped” to become the four corner points of the returned pixmap defined by the requested dimensions.

var warpedPixmap = pixmap.warp([[0, 0], [100, 100], [130, 170], [150, 200]], 200, 200)

Pixmap.prototype.autowarp(points)

only mutool run

Same as Pixmap.prototype.warp() except that width and height are automatically determined.

var warpedPixmap = pixmap.autowarp([0,0,100,0,0,100,100,100])

Pixmap.prototype.convertToColorSpace(colorspace, 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).

Pixmap.prototype.getPixels()

Returns an array of pixels for this pixmap.

var pixels = pixmap.getPixels()

Pixmap.prototype.asPNG()

Returns a buffer of this pixmap as a PNG.

var buffer = pixmap.asPNG()

Pixmap.prototype.asPSD()

Returns a buffer of this pixmap as a PSD.

var buffer = pixmap.asPSD()

Pixmap.prototype.asPAM()

Returns a buffer of this pixmap as a PAM.

var buffer = pixmap.asPAM()

Pixmap.prototype.asJPEG(quality, cmyk_method)

Returns a buffer of this pixmap as a JPEG. Note, if this pixmap has an alpha channel then an exception will be thrown.

var buffer = pixmap.asJPEG(80, 2)

Pixmap.prototype.decodeBarcode(rotate)

only mutool run

Decodes a barcode detected in the pixmap, and returns an object with properties for barcode type and contents.

var barcodeInfo = displayList.decodeBarcode([0, 0, 100, 100 ], 0)

Pixmap.prototype.encodeBarcode(barcodeType, contents, size, errorCorrectionLevel, quietZones, humanReadableText)

only mutool run

Encodes a barcode into a pixmap. The supported types of barcode is either one of:

Matrix Linear Product Linear Industrial
String Name String Name String Name
qrcode QR Code upca UPC-A code39 Code 39
microqrcode Micro QR Code upce UPC-E code93 Code 93
rmqrcode rMQR Code ean8 EAN-8 code128 Code 128
aztec Aztec ean13 EAN-13 codabar Codabar
datamatrix DataMatrix databar DataBar databarexpanded DataBar Expanded
pdf417 PDF417 databarlimited DataBar Limited dxfilmedge DX Film Edge
maxicode MaxiCode itf ITF
var pix = Pixmap.encodeBarcode("qrcode", "Hello world!", 100, 2, true, false)

Pixmap.prototype.getSample(x, y, index)

only mutool run

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

// Get green component of pixel at 10, 10
var sample = rgbpixmap.getSample(10, 10, 1)

Pixmap.prototype.saveAsPNG(filename)

only mutool run

Save this Pixmap as a PNG. Only works for gray and RGB images.

pixmap.saveAsPNG("filename.png")

Pixmap.prototype.saveAsJPEG(filename, quality)

only mutool run

Save this Pixmap as a JPEG file. Only works for gray, RGB and CMYK images.

pixmap.saveAsJPEG("filename.jpg", 80)

Pixmap.prototype.saveAsPAM(filename)

only mutool run

Save this Pixmap as a PAM file.

pixmap.saveAsPAM("filename.pam")

Pixmap.prototype.saveAsPNM(filename)

only mutool run

Save this Pixmap as a PNM file. Only works for gray and RGB images without alpha.

pixmap.saveAsPNM("filename.pnm")

Pixmap.prototype.saveAsPBM(filename)

only mutool run

Save this Pixmap as a PBM file. Only works for alpha only, gray and CMYK images without alpha.

pixmap.saveAsPBM("filename.pbm")

Pixmap.prototype.saveAsPKM(filename)

only mutool run

Save this Pixmap as a PKM file. Only works for alpha only, gray and CMYK images without alpha.

pixmap.saveAsPKM("filename.pkm")

Pixmap.prototype.saveAsJPX(filename, quality)

only mutool run

Save this Pixmap as a JPX file.

pixmap.saveAsJPX("filename.jpx", 90)

Pixmap.prototype.detectDocument(points)

only mutool run

Detect a “document” in a Pixmap. Only a grayscale Pixmap without alpha is supported, anything else will cause an exception to be thrown.

Returns null if no document was detected.

var documentLocation = pixmap.detectDocument([0,0,100,0,100,100,0,100])

Pixmap.prototype.detectSkew()

only mutool run

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

var angle = pixmap.detectSkew()

Pixmap.prototype.deskew(angle, border)

only mutool run

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.

var deskewed = pixmap.deskew(angle, 0)

Pixmap.prototype.computeMD5()

only mutool run

Returns the MD5 digest of the pixmap pixel data. The digest is returned as a string of 16 hex digits.

var md5 = pixmap.computeMD5()