Options
All
  • Public
  • Public/Protected
  • All
Menu

Class PngImage

Decodes and wraps a PNG image. Will call the native bindings under the hood and provides a high-level access to read- and write operations on the image.

Hierarchy

  • PngImage

Index

Constructors

constructor

  • new PngImage(buffer: Buffer): PngImage

Properties

backgroundColor

backgroundColor: ColorRGB | ColorGrayScale | ColorPalette

Returns the background color of the image if provided in the header.

bitDepth

bitDepth: number

The bit depth of the image. Gathered from png_get_bit_depth.

channels

channels: number

The amount of channels of the image. Gathered from png_get_channels.

colorType

colorType: ColorType

The color type of the image as a string. Gathered from png_get_color_type.

data

data: Buffer

The buffer containing the data of the decoded image.

gamma

gamma: number

The gamma value of the image. Gathered from png_get_gAMA.

height

height: number

The width of the image. Gathered from png_get_image_height.

interlaceType

interlaceType: InterlaceType

The interlace type of the image as a string, gathered from png_get_interlace_type.

offsetX

offsetX: number

The horizontal offset of the image. Gathered from png_get_x_offset_pixels.

offsetY

offsetY: number

The vertical offset of the image. Gathered from png_get_y_offset_pixels.

palette

palette: Palette

Retrieve the palette of this image if the color type is ColorType.PALETTE.

see

ColorType

pixelsPerMeterX

pixelsPerMeterX: number

The horizontal amount of pixels per meter of the image. Gathered from png_get_x_pixels_per_meter.

pixelsPerMeterY

pixelsPerMeterY: number

The vertical amount of pixels per meter of the image. Gathered from png_get_y_pixels_per_meter.

rowBytes

rowBytes: number

The amount of bytes per row of the image. Gathered from png_get_rowbytes.

time

time: Date

Returns the last modification time as returned by png_get_tIME.

width

width: number

The width of the image. Gathered from png_get_image_width.

Accessors

alpha

  • get alpha(): boolean
  • Will be true if the image's color type has an alpha channel and false otherwise.

    Returns boolean

bytesPerPixel

  • get bytesPerPixel(): number
  • Returns the amount of bytes per pixel (depending on the color type) for the image.

    Returns number

Methods

at

  • Retrieves the color in the image's color format at the specified position.

    Parameters

    • x: number

      The x position of the pixel in the image of which to retrieve the color.

    • y: number

      The y position of the pixel in the image of which to retrieve the color.

    Returns ColorAny

    The color at the given pixel in the image's color format.

copyFrom

  • Copies the specified rectangle from the other image (or the whole other image if rectangle is omitted) into this image at the current offset (or to the top left if the offset is omitted). Modifies this image and the underlying buffer.

    Parameters

    • other: PngImage

      The other image which should be copied into this image.

    • Optional offset: XY

      The target position in this image to which the other image should be copied.

    • Optional source: Rect

      The clipping rectangle of the other image which should be copied.

    Returns void

crop

  • crop(clip: Rect): void
  • A convenience wrapper around resizeCanvas. Crops the image to a specified sub-rectangle. Modifies this image and the underlying buffer.

    see

    PngImage.resizeCanvas

    see

    ResizeCanvasArguments

    Parameters

    • clip: Rect

      A sub-rectangle which should be cropped out of the image.

    Returns void

encode

  • encode(): Buffer
  • Will encode this image to a PNG buffer.

    Returns Buffer

fill

  • Fill an area of the image with a specific color. This will change the underlying data of this image. The change is in-place.

    Parameters

    • color: ColorAny

      The color with which the area should be filled.

    • Optional area: Rect

      The area to fill. Can be omitted to fill the whole image.

    Returns void

resizeCanvas

  • resizeCanvas(__namedParameters: object): void
  • Resizes the canvas with while optionally adding padding and cropping regions from the image. Modifies this image and the underlying buffer.

    see

    ResizeCanvasArguments

    Parameters

    • __namedParameters: object
      • clip: Object & object
      • dimensions: Object & object
      • fillColor: Object & object | Object & object | Object & object | Object & object | Object & object
      • offset: Object & object

    Returns void

rgbaAt

  • Retrieves the color in rgba format, converting from the image's color format. This will automatically convert from indexed or grayscale images to rgba. If the image's color format doesn't provide an alpha channel, 255 is returned as alpha.

    Parameters

    • x: number

      The x position of the pixel in the image of which to retrieve the color.

    • y: number

      The y position of the pixel in the image of which to retrieve the color.

    Returns ColorRGBA

    The color at the given pixel in rgba format.

set

  • Set the color of one specific pixel on this image. This will change the underlying data of this image. The change is in-place.

    Parameters

    • color: ColorAny

      The color with which the area should be filled.

    • position: XY

      The position of the pixel to colorize.

    Returns void

toIndex

  • toIndex(x: number, y: number): number
  • Convert a set of coordinates to index in the buffer.

    Parameters

    • x: number
    • y: number

    Returns number

toXY

  • toXY(index: number): XY
  • Convert an index in the buffer to a set of coordinates.

    Parameters

    • index: number

    Returns XY

write

  • Will encode this image and write it to the file at the specified path.

    see

    writePngFile

    Parameters

    • path: string

      Path to the file to which the encoded PNG should be written.

    • Optional callback: WritePngFileCallback

      An optional callback to use instead of the Promise API.

    Returns Promise<void> | void

    A Promise which resolves once the file is written or undefined if a callback was specified.

writeSync

  • writeSync(path: string): void
  • Will encode this image and write it to the file at the specified path synchroneously.

    see

    writePngFileSync

    Parameters

    • path: string

      Path to the file to which the encoded PNG should be written.

    Returns void

Generated using TypeDoc