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)
156 lines • 4.73 kB
JavaScript
import { Format, FormatType } from '../color/format.js';
import { getImageDataBytes, } from './image-data.js';
import { PixelFloat16 } from './pixel-float16.js';
import { PixelRangeIterator } from './pixel-range-iterator.js';
import { ColorFloat16 } from '../color/color-float16.js';
import { Float16 } from '../common/float16.js';
export class MemoryImageDataFloat16 {
get width() {
return this._width;
}
set width(v) {
this._width = v;
}
get height() {
return this._height;
}
set height(v) {
this._height = v;
}
get data() {
return this._data;
}
get numChannels() {
return this._numChannels;
}
get format() {
return Format.float16;
}
get formatType() {
return FormatType.float;
}
get buffer() {
return this._data.buffer;
}
get rowStride() {
return this._width * this._numChannels * 2;
}
get iterator() {
return PixelFloat16.imageData(this);
}
get byteLength() {
return this._data.byteLength;
}
get length() {
return this._data.byteLength;
}
get maxChannelValue() {
return 1;
}
get maxIndexValue() {
return 1;
}
get hasPalette() {
return this.palette !== undefined;
}
get palette() {
return undefined;
}
get isHdrFormat() {
return true;
}
get isLdrFormat() {
return !this.isHdrFormat;
}
get bitsPerChannel() {
return 16;
}
constructor(width, height, numChannels, data) {
this._width = width;
this._height = height;
this._numChannels = numChannels;
this._data =
data !== null && data !== void 0 ? data : new Uint16Array(this._width * this._height * this._numChannels);
}
static from(other, skipPixels = false) {
const data = skipPixels
? new Uint16Array(other.data.length)
: other.data.slice();
return new MemoryImageDataFloat16(other.width, other.height, other._numChannels, data);
}
getRange(x, y, width, height) {
return new PixelRangeIterator(PixelFloat16.imageData(this), x, y, width, height);
}
getColor(r, g, b, a) {
return a === undefined
? ColorFloat16.rgb(r, g, b)
: ColorFloat16.rgba(r, g, b, a);
}
getPixel(x, y, pixel) {
let p = pixel;
if (p === undefined || !(p instanceof PixelFloat16) || p.image !== this) {
p = PixelFloat16.imageData(this);
}
p.setPosition(x, y);
return p;
}
setPixel(x, y, p) {
this.setPixelRgba(x, y, p.r, p.g, p.b, p.a);
}
setPixelR(x, y, r) {
const index = y * this._width * this._numChannels + x * this.numChannels;
this.data[index] = Float16.doubleToFloat16(r);
}
setPixelRgb(x, y, r, g, b) {
const index = y * this._width * this._numChannels + x * this._numChannels;
this._data[index] = Float16.doubleToFloat16(r);
if (this._numChannels > 1) {
this._data[index + 1] = Float16.doubleToFloat16(g);
if (this._numChannels > 2) {
this._data[index + 2] = Float16.doubleToFloat16(b);
}
}
}
setPixelRgba(x, y, r, g, b, a) {
const index = y * this._width * this._numChannels + x * this._numChannels;
this._data[index] = Float16.doubleToFloat16(r);
if (this._numChannels > 1) {
this._data[index + 1] = Float16.doubleToFloat16(g);
if (this._numChannels > 2) {
this._data[index + 2] = Float16.doubleToFloat16(b);
if (this._numChannels > 3) {
this._data[index + 3] = Float16.doubleToFloat16(a);
}
}
}
}
setPixelRgbSafe(x, y, r, g, b) {
if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
return;
}
this.setPixelRgb(x, y, r, g, b);
}
setPixelRgbaSafe(x, y, r, g, b, a) {
if (x < 0 || x >= this.width || y < 0 || y >= this.height) {
return;
}
this.setPixelRgba(x, y, r, g, b, a);
}
clear(_c) { }
clone(skipPixels = false) {
return MemoryImageDataFloat16.from(this, skipPixels);
}
toUint8Array() {
return new Uint8Array(this.buffer);
}
getBytes(opt) {
return getImageDataBytes(this, opt);
}
toString() {
return `${this.constructor.name} (w: ${this._width}, h: ${this._height}, ch: ${this._numChannels})`;
}
[Symbol.iterator]() {
return PixelFloat16.imageData(this);
}
}
//# sourceMappingURL=image-data-float16.js.map