Skip to content

Commit

Permalink
feat(pixel): add .copy(), update .blitCanvas()
Browse files Browse the repository at this point in the history
  • Loading branch information
postspectacular committed May 19, 2020
1 parent 65929a2 commit f4b2c3e
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions packages/pixel/src/packed.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import {
Fn,
IObjectOf,
Type,
UIntArray
} from "@thi.ng/api";
import { Fn, IObjectOf, Type, UIntArray } from "@thi.ng/api";
import { isNumber } from "@thi.ng/checks";
import { isPremultipliedInt, postmultiplyInt, premultiplyInt } from "@thi.ng/porter-duff";
import {
isPremultipliedInt,
postmultiplyInt,
premultiplyInt,
} from "@thi.ng/porter-duff";
import {
BlendFnInt,
BlitOpts,
Lane,
PackedFormat,
PackedFormatSpec
PackedFormatSpec,
} from "./api";
import { canvasPixels, imageCanvas } from "./canvas";
import { compileGrayFromABGR, compileGrayToABGR } from "./codegen";
Expand All @@ -24,7 +23,7 @@ import {
setChannelConvert,
setChannelSame,
setChannelUni,
transformABGR
transformABGR,
} from "./utils";

interface UIntArrayConstructor {
Expand All @@ -36,7 +35,7 @@ interface UIntArrayConstructor {
const CTORS: IObjectOf<UIntArrayConstructor> = {
[Type.U8]: Uint8Array,
[Type.U16]: Uint16Array,
[Type.U32]: Uint32Array
[Type.U32]: Uint32Array,
};

export class PackedBuffer {
Expand Down Expand Up @@ -92,6 +91,12 @@ export class PackedBuffer {
return this.getRegion(0, 0, this.width, this.height, fmt);
}

copy() {
const dest = new PackedBuffer(this.width, this.height, this.format);
dest.pixels.set(this.pixels);
return dest;
}

getAt(x: number, y: number) {
return x >= 0 && x < this.width && y >= 0 && y < this.height
? this.pixels[(x | 0) + (y | 0) * this.width]
Expand Down Expand Up @@ -190,6 +195,7 @@ export class PackedBuffer {
dest[i] = fmt(src[i]);
}
ctx.putImageData(idata, x, y);
return canvas;
}

getRegion(
Expand All @@ -211,7 +217,7 @@ export class PackedBuffer {
sx,
sy,
w,
h
h,
});
}

Expand All @@ -223,7 +229,7 @@ export class PackedBuffer {
size: chan.size,
channels: [{ size: chan.size, lane: Lane.RED }],
fromABGR: compileGrayFromABGR(chan.size),
toABGR: compileGrayToABGR(chan.size)
toABGR: compileGrayToABGR(chan.size),
});
const src = this.pixels;
const dest = buf.pixels;
Expand Down

0 comments on commit f4b2c3e

Please sign in to comment.