Advertisement
Guest User

Untitled

a guest
Nov 2nd, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace gobi.flash {
  2.     import GLTexture = gobi.glCore.GLTexture;
  3.     import BaseTexture = gobi.core.BaseTexture;
  4.  
  5.     export class BitmapSymbolResource implements gobi.core.ITextureResource, gobi.core.IDisposable {
  6.         constructor(public symbol: Shumway.flash.display.BitmapSymbol,
  7.                     public imageType: Shumway.ImageType, public data: Uint8Array, public alphaData: Uint8Array) {
  8.  
  9.             this.load();
  10.         }
  11.  
  12.         image: HTMLImageElement = new Image();
  13.         imageData: ImageData = null;
  14.         baseTexture: BaseTexture = null;
  15.         _load: Promise<BitmapSymbolResource>;
  16.         loaded = false;
  17.  
  18.         load() {
  19.             if (this._load) {
  20.                 return this._load;
  21.             }
  22.  
  23.             this._load = new Promise<any>((resolve: any) => {
  24.                 const image = this.image;
  25.  
  26.                 image.src = URL.createObjectURL(new Blob([this.data], {type: Shumway.getMIMETypeForImageType(this.imageType)}));
  27.  
  28.                 let stuff = () => {
  29.                     this.loaded = true;
  30.                     image.onload = null;
  31.                     image.onerror = null;
  32.  
  33.                     if (this.baseTexture) {
  34.                         this.baseTexture.setRealSize(this.width, this.height);
  35.                     }
  36.                     if (this.alphaData) {
  37.                         this.moveToImageData();
  38.                         this.applyAlpha();
  39.                     }
  40.  
  41.                     //TODO: use createImageBitmap when possible
  42.  
  43.                     resolve(this);
  44.                 };
  45.  
  46.                 if (image.complete && image.src) {
  47.                     stuff();
  48.                 } else {
  49.                     image.onload = stuff;
  50.                 }
  51.             });
  52.  
  53.             return this._load;
  54.         }
  55.  
  56.         moveToImageData() {
  57.             const image = this.image;
  58.             const canvas = document.createElement("canvas");
  59.             canvas.width = image.width;
  60.             canvas.height = image.height;
  61.             const context = canvas.getContext("2d");
  62.             context.drawImage(image, 0, 0);
  63.             this.imageData = context.getImageData(0, 0, canvas.width, canvas.height);
  64.             //reset the image
  65.             image.src = "";
  66.         }
  67.  
  68.         applyAlpha() {
  69.             let alphaValues = this.alphaData;
  70.             let imageData = this.imageData;
  71.             let pixels = new Int32Array((<any>imageData.data).buffer);
  72.             let T = Shumway.ColorUtilities.getUnpremultiplyTable();
  73.             for (let i = 0; i < alphaValues.length; i++) {
  74.                 let a = alphaValues[i];
  75.                 if (a === 0) {
  76.                     pixels[i] = 0;
  77.                     continue;
  78.                 }
  79.                 if (a === 0xff) {
  80.                     continue;
  81.                 }
  82.                 let pixel = pixels[i];
  83.                 let r = (pixel >> 0) & 0xff;
  84.                 let g = (pixel >> 8) & 0xff;
  85.                 let b = (pixel >> 16) & 0xff;
  86.                 let o = a << 8;
  87.                 r = T[o + Math.min(r, a)];
  88.                 g = T[o + Math.min(g, a)];
  89.                 b = T[o + Math.min(b, a)];
  90.                 pixels[i] = a << 24 | b << 16 | g << 8 | r;
  91.             }
  92.         }
  93.  
  94.         onTextureNew(baseTexture: BaseTexture) {
  95.             if (!this.baseTexture) {
  96.                 this.baseTexture = baseTexture;
  97.             }
  98.  
  99.             if (this.loaded) {
  100.                 baseTexture.setRealSize(this.width, this.height);
  101.             }
  102.         }
  103.  
  104.  
  105.         onTextureUpload(renderer: pixi.WebGLRenderer, baseTexture: BaseTexture, glTexture: GLTexture): boolean {
  106.             if (this.imageData) {
  107.                 glTexture.uploadData(this.imageData.data, this.imageData.width, this.imageData.height);
  108.             } else {
  109.                 glTexture.upload(this.image);
  110.             }
  111.  
  112.             return true;
  113.         }
  114.  
  115.         destroyed = false;
  116.  
  117.         destroy(options?: any) {
  118.             this.imageData = null;
  119.             this.baseTexture = null;
  120.             this.destroyed = true;
  121.         }
  122.  
  123.         onTextureDestroy(baseTexture: BaseTexture) {
  124.             if (this.baseTexture === baseTexture && !this.destroyed) {
  125.                 this.destroy();
  126.             }
  127.             return true;
  128.         }
  129.  
  130.         get width(): number {
  131.             return this.imageData ? this.imageData.width : this.image.width;
  132.         }
  133.  
  134.         get height(): number {
  135.             return this.imageData ? this.imageData.height : this.image.height;
  136.         }
  137.  
  138.         // 2D patterns
  139.  
  140.         _cachePattern: Array<CanvasPattern> = null;
  141.  
  142.         ensure2DPattern(context: CanvasRenderingContext2D, repeat: boolean): CanvasPattern {
  143.             if (!this._cachePattern) {
  144.                 this._cachePattern = [null, null];
  145.             }
  146.  
  147.             let index = repeat ? 1 : 0;
  148.             if (this._cachePattern[index]) {
  149.                 return this._cachePattern[index];
  150.             }
  151.  
  152.             const w = this.width, h = this.height;
  153.             let pat: CanvasPattern = null;
  154.  
  155.             if (this.imageData) {
  156.                 const canvas = document.createElement("canvas");
  157.                 canvas.width = w;
  158.                 canvas.height = h;
  159.                 const tempContext = canvas.getContext("2d");
  160.                 tempContext.putImageData(this.imageData, 0, 0, w, h);
  161.  
  162.                 pat = context.createPattern(canvas, repeat ? 'repeat' : 'no-repeat');
  163.             } else {
  164.                 pat = context.createPattern(this.image, repeat ? 'repeat' : 'no-repeat');
  165.             }
  166.             this._cachePattern[index] = pat;
  167.             return pat;
  168.         }
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement