diff --git a/src/image/image-data-float16.ts b/src/image/image-data-float16.ts index eb425ba..395934d 100644 --- a/src/image/image-data-float16.ts +++ b/src/image/image-data-float16.ts @@ -1,9 +1,12 @@ /** @format */ -import { ChannelOrder } from '../color/channel-order'; import { Color } from '../color/color'; import { Format, FormatType } from '../color/format'; -import { MemoryImageData } from './image-data'; +import { + MemoryImageData, + MemoryImageDataGetBytesOptions, + getImageDataBytes, +} from './image-data'; import { Palette } from './palette'; import { Pixel } from './pixel'; import { PixelFloat16 } from './pixel-float16'; @@ -232,67 +235,8 @@ export class MemoryImageDataFloat16 return new Uint8Array(this.buffer); } - public getBytes(order?: ChannelOrder | undefined): Uint8Array { - if (order === undefined) { - return this.toUint8Array(); - } - - if (this.numChannels === 4) { - if ( - order === ChannelOrder.abgr || - order === ChannelOrder.argb || - order === ChannelOrder.bgra - ) { - const tempImage = this.clone(); - if (order === ChannelOrder.abgr) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = b; - p.b = g; - p.a = r; - } - } else if (order === ChannelOrder.argb) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = r; - p.b = g; - p.a = b; - } - } else if (order === ChannelOrder.bgra) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = b; - p.g = g; - p.b = r; - p.a = a; - } - } - return tempImage.toUint8Array(); - } - } else if (this.numChannels === 3) { - if (order === ChannelOrder.bgr) { - const tempImage = this.clone(); - for (const p of tempImage) { - const r = p.r; - p.r = p.b; - p.b = r; - } - return tempImage.toUint8Array(); - } - } - - return this.toUint8Array(); + public getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array { + return getImageDataBytes(this, opt); } public toString(): string { diff --git a/src/image/image-data-float32.ts b/src/image/image-data-float32.ts index a9d842a..1931df4 100644 --- a/src/image/image-data-float32.ts +++ b/src/image/image-data-float32.ts @@ -1,9 +1,12 @@ /** @format */ -import { ChannelOrder } from '../color/channel-order'; import { Color } from '../color/color'; import { Format, FormatType } from '../color/format'; -import { MemoryImageData } from './image-data'; +import { + MemoryImageData, + MemoryImageDataGetBytesOptions, + getImageDataBytes, +} from './image-data'; import { Palette } from './palette'; import { Pixel } from './pixel'; import { PixelFloat32 } from './pixel-float32'; @@ -231,67 +234,8 @@ export class MemoryImageDataFloat32 return new Uint8Array(this.buffer); } - public getBytes(order?: ChannelOrder | undefined): Uint8Array { - if (order === undefined) { - return this.toUint8Array(); - } - - if (this.numChannels === 4) { - if ( - order === ChannelOrder.abgr || - order === ChannelOrder.argb || - order === ChannelOrder.bgra - ) { - const tempImage = this.clone(); - if (order === ChannelOrder.abgr) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = b; - p.b = g; - p.a = r; - } - } else if (order === ChannelOrder.argb) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = r; - p.b = g; - p.a = b; - } - } else if (order === ChannelOrder.bgra) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = b; - p.g = g; - p.b = r; - p.a = a; - } - } - return tempImage.toUint8Array(); - } - } else if (this.numChannels === 3) { - if (order === ChannelOrder.bgr) { - const tempImage = this.clone(); - for (const p of tempImage) { - const r = p.r; - p.r = p.b; - p.b = r; - } - return tempImage.toUint8Array(); - } - } - - return this.toUint8Array(); + public getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array { + return getImageDataBytes(this, opt); } public toString(): string { diff --git a/src/image/image-data-float64.ts b/src/image/image-data-float64.ts index 570b73d..57286a3 100644 --- a/src/image/image-data-float64.ts +++ b/src/image/image-data-float64.ts @@ -1,9 +1,12 @@ /** @format */ -import { ChannelOrder } from '../color/channel-order'; import { Color } from '../color/color'; import { Format, FormatType } from '../color/format'; -import { MemoryImageData } from './image-data'; +import { + MemoryImageData, + MemoryImageDataGetBytesOptions, + getImageDataBytes, +} from './image-data'; import { Palette } from './palette'; import { Pixel } from './pixel'; import { PixelFloat64 } from './pixel-float64'; @@ -232,67 +235,8 @@ export class MemoryImageDataFloat64 return new Uint8Array(this.buffer); } - public getBytes(order?: ChannelOrder | undefined): Uint8Array { - if (order === undefined) { - return this.toUint8Array(); - } - - if (this.numChannels === 4) { - if ( - order === ChannelOrder.abgr || - order === ChannelOrder.argb || - order === ChannelOrder.bgra - ) { - const tempImage = this.clone(); - if (order === ChannelOrder.abgr) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = b; - p.b = g; - p.a = r; - } - } else if (order === ChannelOrder.argb) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = r; - p.b = g; - p.a = b; - } - } else if (order === ChannelOrder.bgra) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = b; - p.g = g; - p.b = r; - p.a = a; - } - } - return tempImage.toUint8Array(); - } - } else if (this.numChannels === 3) { - if (order === ChannelOrder.bgr) { - const tempImage = this.clone(); - for (const p of tempImage) { - const r = p.r; - p.r = p.b; - p.b = r; - } - return tempImage.toUint8Array(); - } - } - - return this.toUint8Array(); + public getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array { + return getImageDataBytes(this, opt); } public toString(): string { diff --git a/src/image/image-data-int16.ts b/src/image/image-data-int16.ts index 626d4e3..4bf149a 100644 --- a/src/image/image-data-int16.ts +++ b/src/image/image-data-int16.ts @@ -1,10 +1,13 @@ /** @format */ -import { ChannelOrder } from '../color/channel-order'; import { Color } from '../color/color'; import { ColorInt16 } from '../color/color-int16'; import { Format, FormatType } from '../color/format'; -import { MemoryImageData } from './image-data'; +import { + MemoryImageData, + MemoryImageDataGetBytesOptions, + getImageDataBytes, +} from './image-data'; import { Palette } from './palette'; import { Pixel } from './pixel'; import { PixelInt16 } from './pixel-int16'; @@ -234,67 +237,8 @@ export class MemoryImageDataInt16 implements MemoryImageData, Iterable { return new Uint8Array(this.buffer); } - public getBytes(order?: ChannelOrder | undefined): Uint8Array { - if (order === undefined) { - return this.toUint8Array(); - } - - if (this.numChannels === 4) { - if ( - order === ChannelOrder.abgr || - order === ChannelOrder.argb || - order === ChannelOrder.bgra - ) { - const tempImage = this.clone(); - if (order === ChannelOrder.abgr) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = b; - p.b = g; - p.a = r; - } - } else if (order === ChannelOrder.argb) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = r; - p.b = g; - p.a = b; - } - } else if (order === ChannelOrder.bgra) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = b; - p.g = g; - p.b = r; - p.a = a; - } - } - return tempImage.toUint8Array(); - } - } else if (this.numChannels === 3) { - if (order === ChannelOrder.bgr) { - const tempImage = this.clone(); - for (const p of tempImage) { - const r = p.r; - p.r = p.b; - p.b = r; - } - return tempImage.toUint8Array(); - } - } - - return this.toUint8Array(); + public getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array { + return getImageDataBytes(this, opt); } public toString(): string { diff --git a/src/image/image-data-int32.ts b/src/image/image-data-int32.ts index 884f47e..0996825 100644 --- a/src/image/image-data-int32.ts +++ b/src/image/image-data-int32.ts @@ -1,11 +1,14 @@ /** @format */ -import { ChannelOrder } from '../color/channel-order'; import { Color } from '../color/color'; import { ColorInt32 } from '../color/color-int32'; import { Format, FormatType } from '../color/format'; import { StringUtils } from '../common/string-utils'; -import { MemoryImageData } from './image-data'; +import { + MemoryImageData, + MemoryImageDataGetBytesOptions, + getImageDataBytes, +} from './image-data'; import { Palette } from './palette'; import { Pixel } from './pixel'; import { PixelInt32 } from './pixel-int32'; @@ -235,67 +238,8 @@ export class MemoryImageDataInt32 implements MemoryImageData, Iterable { return new Uint8Array(this.buffer); } - public getBytes(order?: ChannelOrder | undefined): Uint8Array { - if (order === undefined) { - return this.toUint8Array(); - } - - if (this.numChannels === 4) { - if ( - order === ChannelOrder.abgr || - order === ChannelOrder.argb || - order === ChannelOrder.bgra - ) { - const tempImage = this.clone(); - if (order === ChannelOrder.abgr) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = b; - p.b = g; - p.a = r; - } - } else if (order === ChannelOrder.argb) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = r; - p.b = g; - p.a = b; - } - } else if (order === ChannelOrder.bgra) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = b; - p.g = g; - p.b = r; - p.a = a; - } - } - return tempImage.toUint8Array(); - } - } else if (this.numChannels === 3) { - if (order === ChannelOrder.bgr) { - const tempImage = this.clone(); - for (const p of tempImage) { - const r = p.r; - p.r = p.b; - p.b = r; - } - return tempImage.toUint8Array(); - } - } - - return this.toUint8Array(); + public getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array { + return getImageDataBytes(this, opt); } public toString(): string { diff --git a/src/image/image-data-int8.ts b/src/image/image-data-int8.ts index c27eaa3..0ffb1a9 100644 --- a/src/image/image-data-int8.ts +++ b/src/image/image-data-int8.ts @@ -1,10 +1,13 @@ /** @format */ -import { ChannelOrder } from '../color/channel-order'; import { Color } from '../color/color'; import { ColorInt8 } from '../color/color-int8'; import { Format, FormatType } from '../color/format'; -import { MemoryImageData } from './image-data'; +import { + MemoryImageData, + MemoryImageDataGetBytesOptions, + getImageDataBytes, +} from './image-data'; import { Palette } from './palette'; import { Pixel } from './pixel'; import { PixelInt8 } from './pixel-int8'; @@ -234,71 +237,8 @@ export class MemoryImageDataInt8 implements MemoryImageData, Iterable { return new Uint8Array(this.buffer); } - public getBytes(order?: ChannelOrder | undefined): Uint8Array { - if (order === undefined) { - return this.toUint8Array(); - } - - if (this.numChannels === 4) { - if ( - order === ChannelOrder.abgr || - order === ChannelOrder.argb || - order === ChannelOrder.bgra - ) { - const tempImage = this.clone(); - if (order === ChannelOrder.abgr) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = b; - p.b = g; - p.a = r; - } - } else if (order === ChannelOrder.argb) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = r; - p.b = g; - p.a = b; - } - } else if (order === ChannelOrder.bgra) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = b; - p.g = g; - p.b = r; - p.a = a; - } - } - return tempImage.toUint8Array(); - } - } else if (this.numChannels === 3) { - if (order === ChannelOrder.bgr) { - const tempImage = this.clone(); - for (const p of tempImage) { - const r = p.r; - p.r = p.b; - p.b = r; - } - return tempImage.toUint8Array(); - } - } - - return this.toUint8Array(); - } - - public toString(): string { - return `${this.constructor.name} (w: ${this._width}, h: ${this._height}, ch: ${this._numChannels})`; + public getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array { + return getImageDataBytes(this, opt); } public [Symbol.iterator](): Iterator { diff --git a/src/image/image-data-uint1.ts b/src/image/image-data-uint1.ts index b585b3a..142db48 100644 --- a/src/image/image-data-uint1.ts +++ b/src/image/image-data-uint1.ts @@ -1,9 +1,12 @@ /** @format */ -import { ChannelOrder } from '../color/channel-order'; import { Color } from '../color/color'; import { Format, FormatType } from '../color/format'; -import { MemoryImageData } from './image-data'; +import { + MemoryImageData, + MemoryImageDataGetBytesOptions, + getImageDataBytes, +} from './image-data'; import { Palette } from './palette'; import { Pixel } from './pixel'; import { PixelUint1 } from './pixel-uint1'; @@ -256,67 +259,8 @@ export class MemoryImageDataUint1 implements MemoryImageData, Iterable { return new Uint8Array(this.buffer); } - public getBytes(order?: ChannelOrder | undefined): Uint8Array { - if (order === undefined) { - return this.toUint8Array(); - } - - if (this.numChannels === 4) { - if ( - order === ChannelOrder.abgr || - order === ChannelOrder.argb || - order === ChannelOrder.bgra - ) { - const tempImage = this.clone(); - if (order === ChannelOrder.abgr) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = b; - p.b = g; - p.a = r; - } - } else if (order === ChannelOrder.argb) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = r; - p.b = g; - p.a = b; - } - } else if (order === ChannelOrder.bgra) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = b; - p.g = g; - p.b = r; - p.a = a; - } - } - return tempImage.toUint8Array(); - } - } else if (this.numChannels === 3) { - if (order === ChannelOrder.bgr) { - const tempImage = this.clone(); - for (const p of tempImage) { - const r = p.r; - p.r = p.b; - p.b = r; - } - return tempImage.toUint8Array(); - } - } - - return this.toUint8Array(); + public getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array { + return getImageDataBytes(this, opt); } public toString(): string { diff --git a/src/image/image-data-uint16.ts b/src/image/image-data-uint16.ts index 782e985..16940af 100644 --- a/src/image/image-data-uint16.ts +++ b/src/image/image-data-uint16.ts @@ -1,9 +1,12 @@ /** @format */ -import { ChannelOrder } from '../color/channel-order'; import { Color } from '../color/color'; import { Format, FormatType } from '../color/format'; -import { MemoryImageData } from './image-data'; +import { + MemoryImageData, + MemoryImageDataGetBytesOptions, + getImageDataBytes, +} from './image-data'; import { Palette } from './palette'; import { Pixel } from './pixel'; import { PixelUint16 } from './pixel-uint16'; @@ -234,67 +237,8 @@ export class MemoryImageDataUint16 implements MemoryImageData, Iterable { return new Uint8Array(this.buffer); } - public getBytes(order?: ChannelOrder | undefined): Uint8Array { - if (order === undefined) { - return this.toUint8Array(); - } - - if (this.numChannels === 4) { - if ( - order === ChannelOrder.abgr || - order === ChannelOrder.argb || - order === ChannelOrder.bgra - ) { - const tempImage = this.clone(); - if (order === ChannelOrder.abgr) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = b; - p.b = g; - p.a = r; - } - } else if (order === ChannelOrder.argb) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = r; - p.b = g; - p.a = b; - } - } else if (order === ChannelOrder.bgra) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = b; - p.g = g; - p.b = r; - p.a = a; - } - } - return tempImage.toUint8Array(); - } - } else if (this.numChannels === 3) { - if (order === ChannelOrder.bgr) { - const tempImage = this.clone(); - for (const p of tempImage) { - const r = p.r; - p.r = p.b; - p.b = r; - } - return tempImage.toUint8Array(); - } - } - - return this.toUint8Array(); + public getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array { + return getImageDataBytes(this, opt); } public toString(): string { diff --git a/src/image/image-data-uint2.ts b/src/image/image-data-uint2.ts index 7c281b9..0c7824f 100644 --- a/src/image/image-data-uint2.ts +++ b/src/image/image-data-uint2.ts @@ -1,9 +1,12 @@ /** @format */ -import { ChannelOrder } from '../color/channel-order'; import { Color } from '../color/color'; import { Format, FormatType } from '../color/format'; -import { MemoryImageData } from './image-data'; +import { + MemoryImageData, + MemoryImageDataGetBytesOptions, + getImageDataBytes, +} from './image-data'; import { Palette } from './palette'; import { Pixel } from './pixel'; import { PixelUint2 } from './pixel-uint2'; @@ -256,67 +259,8 @@ export class MemoryImageDataUint2 implements MemoryImageData, Iterable { return new Uint8Array(this.buffer); } - public getBytes(order?: ChannelOrder | undefined): Uint8Array { - if (order === undefined) { - return this.toUint8Array(); - } - - if (this.numChannels === 4) { - if ( - order === ChannelOrder.abgr || - order === ChannelOrder.argb || - order === ChannelOrder.bgra - ) { - const tempImage = this.clone(); - if (order === ChannelOrder.abgr) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = b; - p.b = g; - p.a = r; - } - } else if (order === ChannelOrder.argb) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = r; - p.b = g; - p.a = b; - } - } else if (order === ChannelOrder.bgra) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = b; - p.g = g; - p.b = r; - p.a = a; - } - } - return tempImage.toUint8Array(); - } - } else if (this.numChannels === 3) { - if (order === ChannelOrder.bgr) { - const tempImage = this.clone(); - for (const p of tempImage) { - const r = p.r; - p.r = p.b; - p.b = r; - } - return tempImage.toUint8Array(); - } - } - - return this.toUint8Array(); + public getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array { + return getImageDataBytes(this, opt); } public toString(): string { diff --git a/src/image/image-data-uint32.ts b/src/image/image-data-uint32.ts index 98e50b0..3a4e4df 100644 --- a/src/image/image-data-uint32.ts +++ b/src/image/image-data-uint32.ts @@ -1,9 +1,12 @@ /** @format */ -import { ChannelOrder } from '../color/channel-order'; import { Color } from '../color/color'; import { Format, FormatType } from '../color/format'; -import { MemoryImageData } from './image-data'; +import { + MemoryImageData, + MemoryImageDataGetBytesOptions, + getImageDataBytes, +} from './image-data'; import { Palette } from './palette'; import { Pixel } from './pixel'; import { PixelUint32 } from './pixel-uint32'; @@ -234,67 +237,8 @@ export class MemoryImageDataUint32 implements MemoryImageData, Iterable { return new Uint8Array(this.buffer); } - public getBytes(order?: ChannelOrder | undefined): Uint8Array { - if (order === undefined) { - return this.toUint8Array(); - } - - if (this.numChannels === 4) { - if ( - order === ChannelOrder.abgr || - order === ChannelOrder.argb || - order === ChannelOrder.bgra - ) { - const tempImage = this.clone(); - if (order === ChannelOrder.abgr) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = b; - p.b = g; - p.a = r; - } - } else if (order === ChannelOrder.argb) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = r; - p.b = g; - p.a = b; - } - } else if (order === ChannelOrder.bgra) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = b; - p.g = g; - p.b = r; - p.a = a; - } - } - return tempImage.toUint8Array(); - } - } else if (this.numChannels === 3) { - if (order === ChannelOrder.bgr) { - const tempImage = this.clone(); - for (const p of tempImage) { - const r = p.r; - p.r = p.b; - p.b = r; - } - return tempImage.toUint8Array(); - } - } - - return this.toUint8Array(); + public getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array { + return getImageDataBytes(this, opt); } public toString(): string { diff --git a/src/image/image-data-uint4.ts b/src/image/image-data-uint4.ts index 95b645a..9719972 100644 --- a/src/image/image-data-uint4.ts +++ b/src/image/image-data-uint4.ts @@ -1,9 +1,12 @@ /** @format */ -import { ChannelOrder } from '../color/channel-order'; import { Color } from '../color/color'; import { Format, FormatType } from '../color/format'; -import { MemoryImageData } from './image-data'; +import { + MemoryImageData, + MemoryImageDataGetBytesOptions, + getImageDataBytes, +} from './image-data'; import { Palette } from './palette'; import { Pixel } from './pixel'; import { PixelUint4 } from './pixel-uint4'; @@ -263,67 +266,8 @@ export class MemoryImageDataUint4 implements MemoryImageData, Iterable { return new Uint8Array(this.buffer); } - public getBytes(order?: ChannelOrder | undefined): Uint8Array { - if (order === undefined) { - return this.toUint8Array(); - } - - if (this.numChannels === 4) { - if ( - order === ChannelOrder.abgr || - order === ChannelOrder.argb || - order === ChannelOrder.bgra - ) { - const tempImage = this.clone(); - if (order === ChannelOrder.abgr) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = b; - p.b = g; - p.a = r; - } - } else if (order === ChannelOrder.argb) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = r; - p.b = g; - p.a = b; - } - } else if (order === ChannelOrder.bgra) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = b; - p.g = g; - p.b = r; - p.a = a; - } - } - return tempImage.toUint8Array(); - } - } else if (this.numChannels === 3) { - if (order === ChannelOrder.bgr) { - const tempImage = this.clone(); - for (const p of tempImage) { - const r = p.r; - p.r = p.b; - p.b = r; - } - return tempImage.toUint8Array(); - } - } - - return this.toUint8Array(); + public getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array { + return getImageDataBytes(this, opt); } public toString(): string { diff --git a/src/image/image-data-uint8.ts b/src/image/image-data-uint8.ts index 4894335..508d446 100644 --- a/src/image/image-data-uint8.ts +++ b/src/image/image-data-uint8.ts @@ -1,9 +1,12 @@ /** @format */ -import { ChannelOrder } from '../color/channel-order'; import { Color } from '../color/color'; import { Format, FormatType } from '../color/format'; -import { MemoryImageData } from './image-data'; +import { + MemoryImageData, + MemoryImageDataGetBytesOptions, + getImageDataBytes, +} from './image-data'; import { Palette } from './palette'; import { Pixel } from './pixel'; import { PixelUint8 } from './pixel-uint8'; @@ -290,67 +293,8 @@ export class MemoryImageDataUint8 implements MemoryImageData, Iterable { return new Uint8Array(this.buffer); } - public getBytes(order?: ChannelOrder | undefined): Uint8Array { - if (order === undefined) { - return this.toUint8Array(); - } - - if (this.numChannels === 4) { - if ( - order === ChannelOrder.abgr || - order === ChannelOrder.argb || - order === ChannelOrder.bgra - ) { - const tempImage = this.clone(); - if (order === ChannelOrder.abgr) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = b; - p.b = g; - p.a = r; - } - } else if (order === ChannelOrder.argb) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = a; - p.g = r; - p.b = g; - p.a = b; - } - } else if (order === ChannelOrder.bgra) { - for (const p of tempImage) { - const r = p.r; - const g = p.g; - const b = p.b; - const a = p.a; - p.r = b; - p.g = g; - p.b = r; - p.a = a; - } - } - return tempImage.toUint8Array(); - } - } else if (this.numChannels === 3) { - if (order === ChannelOrder.bgr) { - const tempImage = this.clone(); - for (const p of tempImage) { - const r = p.r; - p.r = p.b; - p.b = r; - } - return tempImage.toUint8Array(); - } - } - - return this.toUint8Array(); + public getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array { + return getImageDataBytes(this, opt); } public toString(): string { diff --git a/src/image/image-data.ts b/src/image/image-data.ts index 4b5ac3d..b034fb5 100644 --- a/src/image/image-data.ts +++ b/src/image/image-data.ts @@ -6,6 +6,11 @@ import { Format, FormatType } from '../color/format'; import { Palette } from './palette'; import { Pixel } from './pixel'; +export interface MemoryImageDataGetBytesOptions { + order?: ChannelOrder; + inPlace?: boolean; +} + export interface MemoryImageData extends Iterable { get width(): number; @@ -180,5 +185,74 @@ export interface MemoryImageData extends Iterable { * to the given **order**. If that happens, the returned bytes will be a copy * and not a direct view of the image data. */ - getBytes(order?: ChannelOrder): Uint8Array; + getBytes(opt?: MemoryImageDataGetBytesOptions): Uint8Array; +} + +export function getImageDataBytes( + data: MemoryImageData, + opt?: MemoryImageDataGetBytesOptions +): Uint8Array { + const order = opt?.order; + const inPlace = opt?.inPlace ?? false; + + if (order === undefined) { + return data.toUint8Array(); + } + + if (data.numChannels === 4) { + if ( + order === ChannelOrder.abgr || + order === ChannelOrder.argb || + order === ChannelOrder.bgra + ) { + const tempImage = inPlace ? data : data.clone(); + if (order === ChannelOrder.abgr) { + for (const p of tempImage) { + const r = p.r; + const g = p.g; + const b = p.b; + const a = p.a; + p.r = a; + p.g = b; + p.b = g; + p.a = r; + } + } else if (order === ChannelOrder.argb) { + for (const p of tempImage) { + const r = p.r; + const g = p.g; + const b = p.b; + const a = p.a; + p.r = a; + p.g = r; + p.b = g; + p.a = b; + } + } else if (order === ChannelOrder.bgra) { + for (const p of tempImage) { + const r = p.r; + const g = p.g; + const b = p.b; + const a = p.a; + p.r = b; + p.g = g; + p.b = r; + p.a = a; + } + } + return tempImage.toUint8Array(); + } + } else if (data.numChannels === 3) { + if (order === ChannelOrder.bgr) { + const tempImage = inPlace ? data : data.clone(); + for (const p of tempImage) { + const r = p.r; + p.r = p.b; + p.b = r; + } + return tempImage.toUint8Array(); + } + } + + return data.toUint8Array(); } diff --git a/src/image/image.ts b/src/image/image.ts index 21d0a3a..f85ebf9 100644 --- a/src/image/image.ts +++ b/src/image/image.ts @@ -7,7 +7,6 @@ import { ColorUtils } from '../color/color-utils'; import { Format, FormatMaxValue, - FormatSize, FormatType, getRowStride, } from '../color/format'; @@ -89,6 +88,11 @@ export interface MemoryImageColorExtremes { max: number; } +export interface MemoryImageGetBytesOptions { + order?: ChannelOrder; + alpha?: number; +} + /** * A MemoryImage is a container for MemoryImageData and other various metadata * representing an image in memory. @@ -850,10 +854,44 @@ export class MemoryImage implements Iterable { /** * Similar to **toUint8Array**, but will convert the channels of the image pixels * to the given **order**. If that happens, the returned bytes will be a copy - * and not a direct view of the image data. + * and not a direct view of the image data. If the number of channels needed + * by **order** differs from what the image has, the bytes will come from a + * converted image. If the converted image needs an alpha channel added, + * then you can use the **alpha** argument to specify the value of the + * added alpha channel. */ - public getBytes(order?: ChannelOrder): Uint8Array { - return this._data?.getBytes(order) ?? this.toUint8Array(); + public getBytes(opt?: MemoryImageGetBytesOptions): Uint8Array { + const order = opt?.order; + const alpha = opt?.alpha; + + if (order !== undefined) { + const length = ChannelOrderLength.get(order); + if (length !== this.numChannels) { + const self = this.convert({ + numChannels: length, + alpha: alpha, + }); + return ( + self.data?.getBytes({ + order: order, + inPlace: true, + }) ?? this.toUint8Array() + ); + } else { + return ( + this.data?.getBytes({ + order: order, + inPlace: false, + }) ?? this.toUint8Array() + ); + } + } else { + return ( + this.data?.getBytes({ + order: order, + }) ?? this.toUint8Array() + ); + } } /**