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)

264 lines 7.42 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 { Float16 } from '../common/float16.js'; import { MemoryImageDataFloat16 } from './image-data-float16.js'; export class PixelFloat16 { 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.r; } set index(i) { this.r = 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() { return 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.float16; } get isLdrFormat() { return this._image.isLdrFormat; } get isHdrFormat() { return this._image.isLdrFormat; } get hasPalette() { return this._image.hasPalette; } get palette() { return undefined; } get r() { return this.numChannels > 0 ? Float16.float16ToDouble(this.data[this._index]) : 0; } set r(r) { if (this.numChannels > 0) { this.data[this._index] = Float16.doubleToFloat16(r); } } get g() { return this.numChannels > 1 ? Float16.float16ToDouble(this.data[this._index + 1]) : 0; } set g(g) { if (this.numChannels > 1) { this.data[this._index + 1] = Float16.doubleToFloat16(g); } } get b() { return this.numChannels > 2 ? Float16.float16ToDouble(this.data[this._index + 2]) : 0; } set b(b) { if (this.numChannels > 2) { this.data[this._index + 2] = Float16.doubleToFloat16(b); } } get a() { return this.numChannels > 3 ? Float16.float16ToDouble(this.data[this._index + 3]) : 0; } set a(a) { if (this.numChannels > 3) { this.data[this._index + 3] = Float16.doubleToFloat16(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 ColorUtils.getLuminance(this); } get luminanceNormalized() { return 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 PixelFloat16(-1, 0, -image.numChannels, image); } static image(image) { return new PixelFloat16(-1, 0, -image.numChannels, image.data instanceof MemoryImageDataFloat16 ? image.data : new MemoryImageDataFloat16(0, 0, 0)); } static from(other) { return new PixelFloat16(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.numChannels; 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 { return channel < this.numChannels ? Float16.float16ToDouble(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] = Float16.doubleToFloat16(value); } } set(color) { if (this.numChannels > 0) { 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] = Float16.doubleToFloat16(r); if (this.numChannels > 1) { this.data[this._index + 1] = Float16.doubleToFloat16(g); if (this.numChannels > 2) { this.data[this._index + 2] = Float16.doubleToFloat16(b); } } } } setRgba(r, g, b, a) { if (this.numChannels > 0) { this.data[this._index] = Float16.doubleToFloat16(r); if (this.numChannels > 1) { this.data[this._index + 1] = Float16.doubleToFloat16(g); if (this.numChannels > 2) { this.data[this._index + 2] = Float16.doubleToFloat16(b); if (this.numChannels > 3) { this.data[this._index + 3] = Float16.doubleToFloat16(a); } } } } } toArray() { return ArrayUtils.generate(this.length, (i) => this.getChannel(i)); } equals(other) { if (other instanceof PixelFloat16) { return ArrayUtils.equals(this.toArray(), other.toArray()); } if (Array.isArray(other)) { return ArrayUtils.equals(this.toArray(), other); } return false; } clone() { return PixelFloat16.from(this); } convert(opt) { return ColorUtils.convertColor({ from: this, format: opt.format, numChannels: opt.numChannels, alpha: opt.alpha, }); } [Symbol.iterator]() { return this; } } //# sourceMappingURL=pixel-float16.js.map