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)
278 lines • 7.89 kB
JavaScript
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 { MemoryImageDataUint16 } from './image-data-uint16.js';
export class PixelUint16 {
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() {
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.uint16;
}
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] = Math.trunc(r);
}
}
get g() {
return this.palette === undefined
? this.numChannels > 1
? this.data[this._index + 1]
: 0
: this.palette.getGreen(this.data[this._index]);
}
set g(g) {
if (this.numChannels > 1) {
this.data[this._index + 1] = Math.trunc(g);
}
}
get b() {
return this.palette === undefined
? 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 + 2] = Math.trunc(b);
}
}
get a() {
return this.palette === undefined
? this.numChannels > 3
? this.data[this._index + 3]
: 0
: this.palette.getAlpha(this.data[this._index]);
}
set a(a) {
if (this.numChannels > 3) {
this.data[this._index + 3] = Math.trunc(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 PixelUint16(-1, 0, -image.numChannels, image);
}
static image(image) {
return new PixelUint16(-1, 0, -image.numChannels, image.data instanceof MemoryImageDataUint16
? image.data
: new MemoryImageDataUint16(0, 0, 0));
}
static from(other) {
return new PixelUint16(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] = Math.trunc(value);
}
}
set(color) {
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 PixelUint16) {
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 PixelUint16.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-uint16.js.map