Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace gobi.flash {
- import GLTexture = gobi.glCore.GLTexture;
- import BaseTexture = gobi.core.BaseTexture;
- export class BitmapSymbolResource implements gobi.core.ITextureResource, gobi.core.IDisposable {
- constructor(public symbol: Shumway.flash.display.BitmapSymbol,
- public imageType: Shumway.ImageType, public data: Uint8Array, public alphaData: Uint8Array) {
- this.load();
- }
- image: HTMLImageElement = new Image();
- imageData: ImageData = null;
- baseTexture: BaseTexture = null;
- _load: Promise<BitmapSymbolResource>;
- loaded = false;
- load() {
- if (this._load) {
- return this._load;
- }
- this._load = new Promise<any>((resolve: any) => {
- const image = this.image;
- image.src = URL.createObjectURL(new Blob([this.data], {type: Shumway.getMIMETypeForImageType(this.imageType)}));
- let stuff = () => {
- this.loaded = true;
- image.onload = null;
- image.onerror = null;
- if (this.baseTexture) {
- this.baseTexture.setRealSize(this.width, this.height);
- }
- if (this.alphaData) {
- this.moveToImageData();
- this.applyAlpha();
- }
- //TODO: use createImageBitmap when possible
- resolve(this);
- };
- if (image.complete && image.src) {
- stuff();
- } else {
- image.onload = stuff;
- }
- });
- return this._load;
- }
- moveToImageData() {
- const image = this.image;
- const canvas = document.createElement("canvas");
- canvas.width = image.width;
- canvas.height = image.height;
- const context = canvas.getContext("2d");
- context.drawImage(image, 0, 0);
- this.imageData = context.getImageData(0, 0, canvas.width, canvas.height);
- //reset the image
- image.src = "";
- }
- applyAlpha() {
- let alphaValues = this.alphaData;
- let imageData = this.imageData;
- let pixels = new Int32Array((<any>imageData.data).buffer);
- let T = Shumway.ColorUtilities.getUnpremultiplyTable();
- for (let i = 0; i < alphaValues.length; i++) {
- let a = alphaValues[i];
- if (a === 0) {
- pixels[i] = 0;
- continue;
- }
- if (a === 0xff) {
- continue;
- }
- let pixel = pixels[i];
- let r = (pixel >> 0) & 0xff;
- let g = (pixel >> 8) & 0xff;
- let b = (pixel >> 16) & 0xff;
- let o = a << 8;
- r = T[o + Math.min(r, a)];
- g = T[o + Math.min(g, a)];
- b = T[o + Math.min(b, a)];
- pixels[i] = a << 24 | b << 16 | g << 8 | r;
- }
- }
- onTextureNew(baseTexture: BaseTexture) {
- if (!this.baseTexture) {
- this.baseTexture = baseTexture;
- }
- if (this.loaded) {
- baseTexture.setRealSize(this.width, this.height);
- }
- }
- onTextureUpload(renderer: pixi.WebGLRenderer, baseTexture: BaseTexture, glTexture: GLTexture): boolean {
- if (this.imageData) {
- glTexture.uploadData(this.imageData.data, this.imageData.width, this.imageData.height);
- } else {
- glTexture.upload(this.image);
- }
- return true;
- }
- destroyed = false;
- destroy(options?: any) {
- this.imageData = null;
- this.baseTexture = null;
- this.destroyed = true;
- }
- onTextureDestroy(baseTexture: BaseTexture) {
- if (this.baseTexture === baseTexture && !this.destroyed) {
- this.destroy();
- }
- return true;
- }
- get width(): number {
- return this.imageData ? this.imageData.width : this.image.width;
- }
- get height(): number {
- return this.imageData ? this.imageData.height : this.image.height;
- }
- // 2D patterns
- _cachePattern: Array<CanvasPattern> = null;
- ensure2DPattern(context: CanvasRenderingContext2D, repeat: boolean): CanvasPattern {
- if (!this._cachePattern) {
- this._cachePattern = [null, null];
- }
- let index = repeat ? 1 : 0;
- if (this._cachePattern[index]) {
- return this._cachePattern[index];
- }
- const w = this.width, h = this.height;
- let pat: CanvasPattern = null;
- if (this.imageData) {
- const canvas = document.createElement("canvas");
- canvas.width = w;
- canvas.height = h;
- const tempContext = canvas.getContext("2d");
- tempContext.putImageData(this.imageData, 0, 0, w, h);
- pat = context.createPattern(canvas, repeat ? 'repeat' : 'no-repeat');
- } else {
- pat = context.createPattern(this.image, repeat ? 'repeat' : 'no-repeat');
- }
- this._cachePattern[index] = pat;
- return pat;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement