Skip to content

ImageAdapter interface

  • Provides image per-pixel access to underlying image resource.
  • Stores image virtual viewport.
  • Creates blank images for distorted output images.
  • Stores image settings: background color, quantum range.

There is built-in ImageAdapter for use in browser environment Canvas, which underlying image Resource is HTMLCanvasElement or OffscreenCanvas.

For use in Node.js with Jimp there is Lens-Jimp ImageAdapter implementation.

ts
interface ImageAdapter<ResourceType> {
  readonly width: number;
  readonly height: number;
  getViewport(): Viewport;
  setViewport(viewport: Viewport): void;
  getBackgroundColor(): Color;
  setBackgroundColor(color: Color): void;
  getQuantumRange(): number;
  getPixelColor(x: number, y: number): Color;
  setPixelColor(x: number, y: number, color: Color): void;
  getAverageColor(): Color;
  getBlank(viewport: Viewport): ImageAdapter<ResourceType>;
  scale(scale: number): ImageAdapter<ResourceType>;
  getResource(): ResourceType;
  commit(): void;
}

Properties

width

  • Type: number

Read-only image width.


height

  • Type: number

Read-only image height.

Methods

getViewport()

  • Returns: Viewport

Returns image virtual viewport.

WARNING

Returned viewport is mutable! Do not modify it, unless you know what you do.


setViewport()

  • Param: viewport: Viewport - Viewport instance

Sets image virtual viewport to given Viewport instance.


getBackgroundColor()

  • Returns: Color

Returns image background color.


setBackgroundColor()

  • Param: color: Color - color

Sets image background color. It doesn't change anything on image - this color is used in virtual pixel methods and in color interpolation.


getQuantumRange()

  • Returns: number

Returns image quantum range.


getPixelColor()

  • Params: x: number, y: number - pixel coordinates.
  • Returns: Color

Returns image pixel color at given image coordinates.


setPixelColor()

  • Params:
    • x: number, y: number - pixel coordinates
    • color: Color - color

Sets image pixel color at given coordinates to given color.


getAverageColor()

  • Returns: Color

Returns average color of whole image.


getBlank()

  • Param: viewport: Viewport - Viewport class instance.
  • Returns: ImageInterface<ResourceType>

Returns new instance of ImageInterface with empty underlying image resource, which will be used as 'canvas' for drawing distorted image pixel by pixel.


scale()

  • Param: scale: number - scale factor.
  • Returns: ImageInteface<ResourceType>

Returns new image adapter instance with underlying image resource and virtual viewport scaled by given scale factor.


getResource()

  • Returns: ResourceType

Returns underlying image resource, which depends on concrete implementation.

TIP

Built-in browser image adapter Canvas will return either OffscreenCanvas if offscreen canvas was passed to constructor, or HTMLCanvasElement if canvas element was passed to constructor.

Lens-Jimp adapter will return Jimp.


commit()

  • Returns: void

This method is called after image distortion is processed, and used to sync changes to image pixel data and underlying image resource.

Type Guard

isImageAdapter() function checks if given argument is ImageAdapter and can be used as type guard:

ts
import { isImageAdapter, type ImageAdapter } from "@alxcube/lens";

function getResource<T>(mayBeAdapter: ImageAdapter<T> | T): T {
  if (isImageAdapter(mayBeAdapter)) {
    return mayBeAdapter.getResource();
  }
  return mayBeAdapter;
}