image-js
Version:
Image processing and manipulation in JavaScript
553 lines • 21.2 kB
TypeScript
import type { RgbColor } from 'colord';
import type { Mask } from './Mask.js';
import type { DivideOptions } from './compare/divide.js';
import type { SubtractImageOptions } from './compare/index.js';
import type { MultiplyOptions } from './compare/multiply.js';
import type { HistogramOptions, MeanOptions, MedianOptions, VarianceOptions } from './compute/index.js';
import type { DrawCircleOnImageOptions, DrawLineOnImageOptions, DrawMarkerOptions, DrawPointsOptions, DrawPolygonOnImageOptions, DrawPolylineOnImageOptions, DrawRectangleOptions } from './draw/index.js';
import type { BlurOptions, ConvolutionOptions, DerivativeFilterOptions, FlipOptions, GaussianBlurOptions, GradientFilterOptions, HypotenuseOptions, IncreaseContrastOptions, InvertOptions, LevelOptions, MedianFilterOptions, PixelateOptions } from './filters/index.js';
import type { Point, ResizeOptions, RotateAngle, TransformOptions, TransformRotateOptions } from './geometry/index.js';
import type { ImageMetadata, Resolution } from './load/load.types.js';
import type { BottomHatOptions, CannyEdgeOptions, CloseOptions, DilateOptions, ErodeOptions, MorphologicalGradientOptions, OpenOptions, TopHatOptions } from './morphology/index.js';
import type { ConvertBitDepthOptions, ConvertColorOptions, CopyToOptions, CropAlphaOptions, CropOptions, CropRectangleOptions, ExtractOptions, GreyOptions, PaintMaskOnImageOptions, ThresholdOptions } from './operations/index.js';
import type { ImageColorModel } from './utils/constants/colorModels.js';
export type ImageDataArray = Uint8Array | Uint16Array | Uint8ClampedArray;
/**
* Bit depth of the image (nb of bits that encode each value in the image).
*/
export type BitDepth = 1 | 8 | 16;
export declare const ImageCoordinates: {
readonly CENTER: "center";
readonly TOP_LEFT: "top-left";
readonly TOP_RIGHT: "top-right";
readonly BOTTOM_LEFT: "bottom-left";
readonly BOTTOM_RIGHT: "bottom-right";
};
export type ImageCoordinates = (typeof ImageCoordinates)[keyof typeof ImageCoordinates];
export interface ImageOptions {
/**
* Number of bits per value in each channel.
* @default `8`.
*/
bitDepth?: BitDepth;
/**
* Typed array holding the image data.
*/
data?: ImageDataArray;
/**
* Color model of the created image.
* @default `'RGB'`.
*/
colorModel?: ImageColorModel;
/**
* Origin of the image relative to a parent image (top-left corner).
* @default `{row: 0, column: 0}`
*/
origin?: Point;
/**
* Original resolution decoded from the image.
*/
resolution?: Resolution;
meta?: ImageMetadata;
}
export interface CreateFromOptions extends ImageOptions {
width?: number;
height?: number;
}
export type ImageValues = [number, number, number, number];
export declare class Image {
/**
* The number of columns of the image.
*/
readonly width: number;
/**
* The number of rows of the image.
*/
readonly height: number;
/**
* The total number of pixels in the image (width × height).
*/
readonly size: number;
/**
* The number of bits per value in each channel.
*/
readonly bitDepth: BitDepth;
/**
* The color model of the image.
*/
readonly colorModel: ImageColorModel;
/**
* The number of color channels in the image, excluding the alpha channel.
* A GREY image has 1 component. An RGB image has 3 components.
*/
readonly components: number;
/**
* The total number of channels in the image, including the alpha channel.
*/
readonly channels: number;
/**
* Whether the image has an alpha channel or not.
*/
readonly alpha: boolean;
/**
* The maximum value that a pixel channel can have.
*/
readonly maxValue: number;
/**
* Origin of the image relative to a the parent image.
*/
readonly origin: Point;
/**
* Original image resolution.
*/
readonly originalResolution: Resolution | undefined;
readonly meta?: ImageMetadata;
/**
* Typed array holding the image data.
*/
private readonly data;
/**
* Construct a new Image knowing its dimensions.
* @param width - Image width.
* @param height - Image height.
* @param options - Image options.
*/
constructor(width: number, height: number, options?: ImageOptions);
/**
* Returns normalized resolution in pixels per centimeter. If resolution unit is unknown, return null.
* @returns Object with x and y resolutions in pixel/cm.
*/
get normalizedResolution(): {
x: number;
y: number;
} | null | undefined;
/**
* Create a new Image based on the properties of an existing one.
* @param other - Reference image.
* @param options - Image options.
* @returns New image.
*/
static createFrom(other: Image | Mask, options?: CreateFromOptions): Image;
/**
* Get all the channels of a pixel.
* @param column - Column index.
* @param row - Row index.
* @returns Channels of the pixel.
*/
getPixel(column: number, row: number): number[];
getColumn(column: number): number[][];
getRow(row: number): number[][];
/**
* Set all the channels of a pixel.
* @param column - Column index.
* @param row - Row index.
* @param value - New color of the pixel to set.
*/
setPixel(column: number, row: number, value: number[]): void;
/**
* Set all the channels of a pixel if the coordinates are inside the image.
* @param column - Column index.
* @param row - Row index.
* @param value - New color of the pixel to set.
*/
setVisiblePixel(column: number, row: number, value: number[]): void;
/**
* Get all the channels of a pixel using its index.
* @param index - Index of the pixel.
* @returns Channels of the pixel.
*/
getPixelByIndex(index: number): number[];
/**
* Set all the channels of a pixel using its index.
* @param index - Index of the pixel.
* @param value - New channel values of the pixel to set.
*/
setPixelByIndex(index: number, value: number[]): void;
/**
* Get the value of a specific pixel channel. Select pixel using coordinates.
* @param column - Column index.
* @param row - Row index.
* @param channel - Channel index.
* @returns Value of the specified channel of one pixel.
*/
getValue(column: number, row: number, channel: number): number;
/**
* Set the value of a specific pixel channel. Select pixel using coordinates.
* @param column - Column index.
* @param row - Row index.
* @param channel - Channel index.
* @param value - Value to set.
*/
setValue(column: number, row: number, channel: number, value: number): void;
/**
* Set the value of a specific pixel channel. Select pixel using coordinates.
* If the value is out of range it is set to the closest extremety.
* @param column - Column index.
* @param row - Row index.
* @param channel - Channel index.
* @param value - Value to set.
*/
setClampedValue(column: number, row: number, channel: number, value: number): void;
/**
* Get the value of a specific pixel channel. Select pixel using index.
* @param index - Index of the pixel.
* @param channel - Channel index.
* @returns Value of the channel of the pixel.
*/
getValueByIndex(index: number, channel: number): number;
/**
* Set the value of a specific pixel channel. Select pixel using index.
* @param index - Index of the pixel.
* @param channel - Channel index.
* @param value - Value to set.
*/
setValueByIndex(index: number, channel: number, value: number): void;
/**
* Set the value of a specific pixel channel. Select pixel using index.
* If the value is out of range it is set to the closest extremety.
* @param index - Index of the pixel.
* @param channel - Channel index.
* @param value - Value to set.
*/
setClampedValueByIndex(index: number, channel: number, value: number): void;
/**
* Get the value of a specific pixel channel. Select pixel using a point.
* @param point - Coordinates of the desired pixel.
* @param channel - Channel index.
* @returns Value of the channel of the pixel.
*/
getValueByPoint(point: Point, channel: number): number;
/**
* Set the value of a specific pixel channel. Select pixel using a point.
* @param point - Coordinates of the pixel.
* @param channel - Channel index.
* @param value - Value to set.
*/
setValueByPoint(point: Point, channel: number, value: number): void;
/**
* Find the min and max values of each channel of the image.
* @returns An object with arrays of the min and max values.
*/
minMax(): {
min: number[];
max: number[];
};
/**
* Return the raw image data.
* @returns The raw data.
*/
getRawImage(): {
width: number;
height: number;
data: ImageDataArray;
channels: number;
bitDepth: BitDepth;
};
/**
* Fill the image with a value or a color.
* @param value - Value or color.
* @returns The image instance.
*/
fill(value: number | number[]): this;
/**
* Fill one channel with a value.
* @param channel - The channel to fill.
* @param value - The new value.
* @returns The image instance.
*/
fillChannel(channel: number, value: number): this;
/**
* Get one channel of the image as an array.
* @param channel - The channel to fill.
* @returns Array with the channel values.
*/
getChannel(channel: number): number[];
/**
* Fill the alpha channel with the specified value.
* @param value - New channel value.
* @returns The image instance.
*/
fillAlpha(value: number): this;
/**
* Create a copy of this image.
* @returns The image clone.
*/
clone(): Image;
/**
* Modify all the values of the image using the given callback.
* @param cb - Callback that modifies a given value.
*/
changeEach(cb: (value: number) => number): void;
/**
* Get the coordinates of a point in the image. The reference is the top-left corner.
* @param coordinates - The point for which you want the coordinates.
* @param round - Whether the coordinates should be rounded. This is useful when you want the center of the image.
* @returns Coordinates of the point in the format [column, row].
*/
getCoordinates(coordinates: ImageCoordinates, round?: boolean): Point;
/**
* Subtract other from an image.
* @param other - Image to subtract.
* @param options - Inversion options.
* @returns The subtracted image.
*/
subtract(other: Image, options?: SubtractImageOptions): Image;
add(other: Image): Image;
/**
* Multiply image pixels by a constant.
* @param value - Value which pixels will be multiplied to.
* @param options - Multiply options.
* @returns Multiplied image.
*/
multiply(value: number, options?: MultiplyOptions): Image;
/**
* Divide image pixels by a constant.
* @param value - Value which pixels will be divided to.
* @param options - Divide options.
* @returns Divided image.
*/
divide(value: number, options?: DivideOptions): Image;
histogram(options?: HistogramOptions): Uint32Array;
/**
* Compute the mean pixel of an image.
* @param options - Mean options.
* @returns The mean pixel.
*/
mean(options?: MeanOptions): number[];
/**
* Compute the median pixel of an image.
* @param options - Median options.
* @returns The median pixel.
*/
median(options?: MedianOptions): number[];
/**
* Compute the variance of each channel of an image.
* @param options - Variance options.
* @returns The variance of the channels of the image.
*/
variance(options?: VarianceOptions): number[];
/**
* Draw a set of points on an image.
* @param points - Array of points.
* @param options - Draw points on Image options.
* @returns New mask.
*/
drawPoints(points: Point[], options?: DrawPointsOptions): Image;
/**
* Draw a line defined by two points onto an image.
* @param from - Line starting point.
* @param to - Line ending point.
* @param options - Draw Line options.
* @returns The mask with the line drawing.
*/
drawLine(from: Point, to: Point, options?: DrawLineOnImageOptions): Image;
/**
* Draw a rectangle defined by position of the top-left corner, width and height.
* @param options - Draw rectangle options.
* @returns The image with the rectangle drawing.
*/
drawRectangle(options?: DrawRectangleOptions<Image>): Image;
/**
* Draw a polyline defined by an array of points on an image.
* @param points - Polyline array of points.
* @param options - Draw polyline options.
* @returns The image with the polyline drawing.
*/
drawPolyline(points: Point[], options?: DrawPolylineOnImageOptions): Image;
/**
* Draw a polygon defined by an array of points onto an image.
* @param points - Polygon vertices.
* @param options - Draw Line options.
* @returns The image with the polygon drawing.
*/
drawPolygon(points: Point[], options?: DrawPolygonOnImageOptions): Image;
/**
* Draw a circle defined by center and radius onto an image.
* @param center - Circle center.
* @param radius - Circle radius.
* @param options - Draw circle options.
* @returns The image with the circle drawing.
*/
drawCircle(center: Point, radius: number, options?: DrawCircleOnImageOptions): Image;
/**
* Draw a marker on the image.
* @param point - Marker center point.
* @param options - Draw marker options.
* @returns The image with the marker drawing.
*/
drawMarker(point: Point, options?: DrawMarkerOptions): Image;
/**
* Draw markers on the image.
* @param points - Markers center points.
* @param options - Draw marker options.
* @returns The image with the markers drawing.
*/
drawMarkers(points: Point[], options?: DrawMarkerOptions): Image;
split(): Image[];
convertColor(colorModel: ImageColorModel, options?: ConvertColorOptions): Image;
convertBitDepth(newDepth: BitDepth, options?: ConvertBitDepthOptions): Image;
grey(options?: GreyOptions): Image;
copyTo(target: Image, options?: CopyToOptions<Image>): Image;
threshold(options?: ThresholdOptions): Mask;
/**
* Crop the input image to a desired size.
* @param [options] - Crop options.
* @returns The new cropped image.
*/
crop(options?: CropOptions): Image;
/**
* Crop an oriented rectangle from the image.
* If the rectangle's length or width are not an integers, its dimension is expanded in both directions such as the length and width are integers.
* @param points - The points of the rectangle. Points must be circling around the rectangle (clockwise or anti-clockwise)
* @param options - Crop options, see {@link CropRectangleOptions}
* @returns The cropped image. The orientation of the image is the one closest to the rectangle passed as input.
*/
cropRectangle(points: Point[], options?: CropRectangleOptions): Image;
/**
* Crops the image based on the alpha channel
* This removes lines and columns where the alpha channel is lower than a threshold value.
* @param options - Crop alpha options.
* @returns The cropped image.
*/
cropAlpha(options?: CropAlphaOptions): Image;
/**
* Extract the pixels of an image, as specified in a mask.
* @param mask - The mask defining which pixels to keep.
* @param options - Extract options.
* @returns The extracted image.
*/
extract(mask: Mask, options?: ExtractOptions): Image;
/**
* Paint a mask onto an image and the given position and with the given color.
* @param mask - Mask to paint on the image.
* @param options - Paint mask options.
* @returns The painted image.
*/
paintMask(mask: Mask, options?: PaintMaskOnImageOptions): Image;
blur(options: BlurOptions): Image;
pixelate(options: PixelateOptions): Image;
directConvolution(kernel: number[][], options?: ConvolutionOptions): Image;
/**
* Compute direct convolution of an image and return an array with the raw values.
* @param kernel - Kernel used for the convolution.
* @param options - Convolution options.
* @returns Array with the raw convoluted values.
*/
rawDirectConvolution(kernel: number[][], options?: ConvolutionOptions): Float64Array;
separableConvolution(kernelX: number[], kernelY: number[], options?: ConvolutionOptions): Image;
/**
* Apply a gaussian filter to an image.
* @param options - Gaussian blur options.
* @returns The blurred image.
*/
gaussianBlur(options: GaussianBlurOptions): Image;
/**
* Flip the image.
* @param options - Flip options.
* @returns The flipped image.
*/
flip(options?: FlipOptions): Image;
/**
* Invert the colors of the image.
* @param options - Inversion options.
* @returns The inverted image.
*/
invert(options?: InvertOptions): Image;
/**
* Calculate a new image that is the hypotenuse between the current image and the other.
* @param other - Other image.
* @param options - Hypotenuse options.
* @returns Hypotenuse of the two images.
*/
hypotenuse(other: Image, options?: HypotenuseOptions): Image;
/**
* Apply a gradient filter to an image.
* @param options - Gradient filter options.
* @returns The gradient image.
*/
gradientFilter(options: GradientFilterOptions): Image;
/**
* Apply a derivative filter to an image.
* @param options - Derivative filter options.
* @returns The processed image.
*/
derivativeFilter(options?: DerivativeFilterOptions): Image;
/**
* Level the image using the optional input and output value. This function allows you to enhance the image's contrast.
* @param options - Level options.
* @returns The levelled image.
*/
level(options?: LevelOptions): Image;
/**
* Increase the contrast of an image by spanning each channel on the range [0, image.maxValue].
* @param options - Increase contrast options.
* @returns The enhanced image.
*/
increaseContrast(options?: IncreaseContrastOptions): Image;
/**
* Correct the colors in an image using the reference colors.
* @param measuredColors - Colors from the image, which will be compared to the reference.
* @param referenceColors - Reference colors.
* @returns Image with the colors corrected.
*/
correctColor(measuredColors: RgbColor[], referenceColors: RgbColor[]): Image;
/**
* Apply a median filter to the image.
* @param options - Options to apply for median filter.
* @returns Image after median filter.
*/
medianFilter(options?: MedianFilterOptions): Image;
resize(options: ResizeOptions): Image;
rotate(angle: RotateAngle): Image;
transform(transformMatrix: number[][], options?: TransformOptions): Image;
transformRotate(angle: number, options?: TransformRotateOptions): Image;
/**
* Erode an image.
* @param options - Erode options.
* @returns The eroded image.
*/
erode(options?: ErodeOptions): Image;
/**
* Dilate an image.
* @param options - Dilate options.
* @returns The dilated image.
*/
dilate(options?: DilateOptions): Image;
/**
* Open an image.
* @param options - Open options.
* @returns The opened image.
*/
open(options?: OpenOptions): Image;
/**
* Close an image.
* @param options - Close options.
* @returns The closed image.
*/
close(options?: CloseOptions): Image;
/**
* Top hat of an image.
* @param options - Top hat options.
* @returns The top-hatted image.
*/
topHat(options?: TopHatOptions): Image;
/**
* Bottom hat of an image.
* @param options - Bottom hat options.
* @returns The bottom-hatted image.
*/
bottomHat(options?: BottomHatOptions): Image;
/**
* Apply morphological gradient to an image.
* @param options - Morphological gradient options.
* @returns The processed image.
*/
morphologicalGradient(options?: MorphologicalGradientOptions): Image;
/**
* Apply Canny edge detection to an image.
* @param options - Canny edge detection options.
* @returns The processed image.
*/
cannyEdgeDetector(options?: CannyEdgeOptions): Mask;
}
//# sourceMappingURL=Image.d.ts.map