Skip to content

Commit

Permalink
feat: MemoryImage.getBytes should convert image if order has differen…
Browse files Browse the repository at this point in the history
…t number of channels
  • Loading branch information
yegor-pelykh committed Feb 3, 2024
1 parent cf6955e commit fbeb46b
Show file tree
Hide file tree
Showing 14 changed files with 201 additions and 765 deletions.
70 changes: 7 additions & 63 deletions src/image/image-data-float16.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {
Expand Down
70 changes: 7 additions & 63 deletions src/image/image-data-float32.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {
Expand Down
70 changes: 7 additions & 63 deletions src/image/image-data-float64.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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 {
Expand Down
70 changes: 7 additions & 63 deletions src/image/image-data-int16.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -234,67 +237,8 @@ export class MemoryImageDataInt16 implements MemoryImageData, Iterable<Pixel> {
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 {
Expand Down
Loading

0 comments on commit fbeb46b

Please sign in to comment.