UNPKG

image-in-browser

Version:

Package for encoding / decoding images, transforming images, applying filters, drawing primitives on images on the client side (no need for server Node.js)

301 lines 8.91 kB
import { Channel } from '../color/channel.js'; import { ColorUtils } from '../color/color-utils.js'; import { Format } from '../color/format.js'; import { ArrayUtils } from '../common/array-utils.js'; import { MathUtils } from '../common/math-utils.js'; import { MemoryImageDataUint8 } from './image-data-uint8.js'; export class PixelUint8 { get image() { return this._image; } get x() { return this._x; } get y() { return this._y; } get xNormalized() { return this.width > 1 ? this._x / (this.width - 1) : 0; } get yNormalized() { return this.height > 1 ? this._y / (this.height - 1) : 0; } get index() { return this.data[this._index]; } set index(i) { this.data[this._index] = MathUtils.clampInt255(i); } get data() { return this._image.data; } get isValid() { return (this._x >= 0 && this._x < this._image.width - 1 && this._y >= 0 && this._y < this._image.height - 1); } get width() { return this._image.width; } get height() { return this._image.height; } get length() { var _a, _b; return (_b = (_a = this.palette) === null || _a === void 0 ? void 0 : _a.numChannels) !== null && _b !== void 0 ? _b : this._image.numChannels; } get numChannels() { return this._image.numChannels; } get maxChannelValue() { return this._image.maxChannelValue; } get maxIndexValue() { return this._image.maxIndexValue; } get format() { return Format.uint8; } get isLdrFormat() { return this._image.isLdrFormat; } get isHdrFormat() { return this._image.isLdrFormat; } get hasPalette() { return this._image.hasPalette; } get palette() { return this._image.palette; } get r() { return this.palette === undefined ? this.numChannels > 0 ? this.data[this._index] : 0 : this.palette.getRed(this.data[this._index]); } set r(r) { if (this.numChannels > 0) { this.data[this._index] = MathUtils.clampInt255(r); } } get g() { return this.palette === undefined ? this.numChannels === 2 ? this.data[this._index] : this.numChannels > 1 ? this.data[this._index + 1] : 0 : this.palette.getGreen(this.data[this._index]); } set g(g) { if (this.numChannels === 2) { this.data[this._index] = MathUtils.clampInt255(g); } else if (this.image.numChannels > 1) { this.data[this._index + 1] = MathUtils.clampInt255(g); } } get b() { return this.palette === undefined ? this.numChannels === 2 ? this.data[this._index] : this.numChannels > 2 ? this.data[this._index + 2] : 0 : this.palette.getBlue(this.data[this._index]); } set b(b) { if (this.numChannels === 2) { this.data[this._index] = MathUtils.clampInt255(b); } else if (this.image.numChannels > 2) { this.data[this._index + 2] = MathUtils.clampInt255(b); } } get a() { return this.palette === undefined ? this.numChannels === 2 ? this.data[this._index + 1] : this.numChannels > 3 ? this.data[this._index + 3] : 255 : this.palette.getAlpha(this.data[this._index]); } set a(a) { if (this.numChannels === 2) { this.data[this._index + 1] = MathUtils.clampInt255(a); } else if (this.image.numChannels > 3) { this.data[this._index + 3] = MathUtils.clampInt255(a); } } get rNormalized() { return this.r / this.maxChannelValue; } set rNormalized(v) { this.r = v * this.maxChannelValue; } get gNormalized() { return this.g / this.maxChannelValue; } set gNormalized(v) { this.g = v * this.maxChannelValue; } get bNormalized() { return this.b / this.maxChannelValue; } set bNormalized(v) { this.b = v * this.maxChannelValue; } get aNormalized() { return this.a / this.maxChannelValue; } set aNormalized(v) { this.a = v * this.maxChannelValue; } get luminance() { return this.numChannels === 2 ? this.r : ColorUtils.getLuminance(this); } get luminanceNormalized() { return this.numChannels === 2 ? this.rNormalized : ColorUtils.getLuminanceNormalized(this); } constructor(x, y, index, image) { this._image = image; this._index = index; this._x = x; this._y = y; } static imageData(image) { return new PixelUint8(-1, 0, -image.numChannels, image); } static image(image) { return new PixelUint8(-1, 0, -image.numChannels, image.data instanceof MemoryImageDataUint8 ? image.data : new MemoryImageDataUint8(0, 0, 0)); } static from(other) { return new PixelUint8(other.x, other.y, other._index, other.image); } next() { this._x++; if (this._x === this.width) { this._x = 0; this._y++; if (this._y === this.height) { return { done: true, value: this, }; } } this._index += this.palette === undefined ? this.numChannels : 1; return { done: this._index >= this.image.data.length, value: this, }; } setPosition(x, y) { this._x = x; this._y = y; this._index = this._y * this._image.width * this._image.numChannels + this._x * this._image.numChannels; } setPositionNormalized(x, y) { return this.setPosition(Math.floor(x * (this.width - 1)), Math.floor(y * (this.height - 1))); } getChannel(channel) { if (channel === Channel.luminance) { return this.luminance; } else { if (this.palette !== undefined) { return this.palette.get(this.data[this._index], channel); } else { return channel < this.numChannels ? this.data[this._index + channel] : 0; } } } getChannelNormalized(channel) { return this.getChannel(channel) / this.maxChannelValue; } setChannel(channel, value) { if (channel < this.numChannels) { this.data[this._index + channel] = MathUtils.clampInt255(value); } } set(color) { if (this._image.hasPalette) { this.index = color.index; } else { this.r = color.r; this.g = color.g; this.b = color.b; this.a = color.a; } } setRgb(r, g, b) { if (this.numChannels > 0) { this.data[this._index] = Math.trunc(r); if (this.numChannels > 1) { this.data[this._index + 1] = Math.trunc(g); if (this.numChannels > 2) { this.data[this._index + 2] = Math.trunc(b); } } } } setRgba(r, g, b, a) { if (this.numChannels > 0) { this.data[this._index] = Math.trunc(r); if (this.numChannels > 1) { this.data[this._index + 1] = Math.trunc(g); if (this.numChannels > 2) { this.data[this._index + 2] = Math.trunc(b); if (this.numChannels > 3) { this.data[this._index + 3] = Math.trunc(a); } } } } } equals(other) { if (other instanceof PixelUint8) { return ArrayUtils.equals(this.toArray(), other.toArray()); } if (Array.isArray(other)) { return ArrayUtils.equals(this.toArray(), other); } return false; } toArray() { return ArrayUtils.generate(this.length, (i) => this.getChannel(i)); } clone() { return PixelUint8.from(this); } convert(opt) { return ColorUtils.convertColor({ from: this, format: opt.format, numChannels: opt.numChannels, alpha: opt.alpha, }); } toString() { return `${this.constructor.name} (${this.toArray()})`; } [Symbol.iterator]() { return this; } } //# sourceMappingURL=pixel-uint8.js.map