import { ColorTransform, Matrix, Rectangle, Point, ColorUtils, IAssetAdapter, AssetEvent, IAsset } from '@awayjs/core';
import { UnloadManager, IUnloadable, UnloadService } from './../managers/UnloadManager';
import { Image2D } from './Image2D';
import { BitmapImageChannel } from './BitmapImageChannel';
import { LehmerRng } from '../utils/LehmerRng';
import { Turbulence } from '../utils/Turbulence';
import { Settings } from './../Settings';
/**
* The BitmapImage2D export class lets you work with the data(pixels) of a Bitmap
* object. You can use the methods of the BitmapImage2D export class to create
* arbitrarily sized transparent or opaque bitmap images and manipulate them
* in various ways at runtime. You can also access the BitmapImage2D for a bitmap
* image that you load with the flash.Assets or
* flash.display.Loader classes.
*
*
This export class lets you separate bitmap rendering operations from the * internal display updating routines of flash. By manipulating a * BitmapImage2D object directly, you can create complex images without incurring * the per-frame overhead of constantly redrawing the content from vector * data.
* *The methods of the BitmapImage2D export class support effects that are not * available through the filters available to non-bitmap display objects.
* *A BitmapImage2D object contains an array of pixel data. This data can * represent either a fully opaque bitmap or a transparent bitmap that * contains alpha channel data. Either type of BitmapImage2D object is stored as * a buffer of 32-bit integers. Each 32-bit integer determines the properties * of a single pixel in the bitmap.
* *Each 32-bit integer is a combination of four 8-bit channel values(from * 0 to 255) that describe the alpha transparency and the red, green, and blue * (ARGB) values of the pixel.(For ARGB values, the most significant byte * represents the alpha channel value, followed by red, green, and blue.)
* *The four channels(alpha, red, green, and blue) are represented as
* numbers when you use them with the BitmapImage2D.copyChannel()
* method or the DisplacementMapFilter.componentX and
* DisplacementMapFilter.componentY properties, and these numbers
* are represented by the following constants in the BitmapImage2DChannel
* class:
BitmapImage2DChannel.ALPHABitmapImage2DChannel.REDBitmapImage2DChannel.GREENBitmapImage2DChannel.BLUEYou can attach BitmapImage2D objects to a Bitmap object by using the
* bitmapData property of the Bitmap object.
You can use a BitmapImage2D object to fill a Graphics object by using the
* Graphics.beginBitmapFill() method.
You can also use a BitmapImage2D object to perform batch tile rendering
* using the flash.display.Tilesheet class.
In Flash Player 10, the maximum size for a BitmapImage2D object * is 8,191 pixels in width or height, and the total number of pixels cannot * exceed 16,777,215 pixels.(So, if a BitmapImage2D object is 8,191 pixels wide, * it can only be 2,048 pixels high.) In Flash Player 9 and earlier, the limitation * is 2,880 pixels in height and 2,880 in width.
*/ let HAS_REF = ('WeakRef' in window); let alerted = false; function REF_ENABLED() { if (alerted) return HAS_REF; alerted = true; HAS_REF = HAS_REF && Settings.ENABLE_WEAK_REF; if (HAS_REF) { console.debug('[ImageBitmap2D Experemental] Use WeakRef for ImageBitmap2D'); } return HAS_REF; } function fastARGB_to_ABGR(val: number, hasAlpha = true) { const a = hasAlpha ? (val & 0xff000000) : 0xff000000; return (a | ((val & 0xff) << 16) | (val & 0xff00) | ((val & 0xff0000) >> 16) & 0xff) >>> 0; } interface LazyImageSymbolTag { needParse: boolean; lazyParser(): LazyImageSymbolTag; definition: { width: number; height: number; data: Uint8ClampedArray; isPMA?: boolean; } } export class BitmapImage2D extends Image2D implements IUnloadable { public static UNLOAD_EVENT = 'unload'; public static assetType: string = '[image BitmapImage2D]'; public static _unloadManager: UnloadManagertrue for the transparent parameter of the
* constructor. Then, after you create a BitmapImage2D object, you can check
* whether it supports per-pixel transparency by determining if the value of
* the transparent property is true.
*/
public get transparent(): boolean {
return this._transparent;
}
public set transparent(value: boolean) {
this._transparent = value;
}
/**
* Store a mode, witch should be uploaded to GPU
* not all case PMA is should be enabled
*/
get unpackPMA() {
return this._unpackPMA && this._transparent;
}
/**
* Creates a BitmapImage2D object with a specified width and height. If you
* specify a value for the fillColor parameter, every pixel in
* the bitmap is set to that color.
*
* By default, the bitmap is created as transparent, unless you pass
* the value false for the transparent parameter. After you
* create an opaque bitmap, you cannot change it to a transparent bitmap.
* Every pixel in an opaque bitmap uses only 24 bits of color channel
* information. If you define the bitmap as transparent, every pixel uses 32
* bits of color channel information, including an alpha transparency
* channel.
true
* (transparent). To create a fully transparent bitmap,
* set the value of the transparent
* parameter to true and the value of the
* fillColor parameter to 0x00000000(or to
* 0). Setting the transparent property to
* false can result in minor improvements
* in rendering performance.
* @param fillColor A 32-bit ARGB color value that you use to fill the
* bitmap image area. The default value is
* 0xFFFFFFFF(solid white).
*/
constructor(
width: number, height: number, transparent: boolean = true,
fillColor: number = null, powerOfTwo: boolean = true, stage: Stage = null) {
super(width, height, powerOfTwo);
if (stage) {
// init
BitmapImage2D.getManager(stage);
}
//this._data = new Uint8ClampedArray(4 * this._rect.width * this._rect.height);
this._transparent = transparent;
this._stage = stage;
this._initalFillColor = fillColor;
this._lastUsedFill = fillColor;
}
public addLazySymbol(tag: LazyImageSymbolTag) {
this._lazySymbol = tag;
this._isSymbolSource = true;
// we should reset initial color, because a bitmap symbol has data of its own
this._initalFillColor = null;
this.invalidateGPU();
}
public applySymbol(): boolean {
if (!this._lazySymbol /*|| !this._lazySymbol.needParse*/) {
return false;
}
if (this._lazySymbol.needParse) {
this._lazySymbol.lazyParser();
}
this._data = this._lazySymbol.definition.data;
// disable UNPACK_PREMULTIPLE_ALPHA because already is PMA
this._unpackPMA = !this._lazySymbol.definition.isPMA;
this._lazySymbol = null;
return true;
}
/**
* @description transfer adapter to weak mode
* Reference will dropped, and adapter destroyed after collecting a adapter
*/
public useWeakRef() {
if (!REF_ENABLED() || this._isWeakRef) {
return;
}
this._isWeakRef = true;
if (!this._finalizer) {
this._finalizer = new FinalizationRegistry(this.onAdapterDropped.bind(this));
}
this.adapter = this._adapter;
}
public unuseWeakRef() {
if (!this._isWeakRef) {
return;
}
this._isWeakRef = false;
this._finalizer.unregister(this);
this._weakRefAdapter = null;
this.adapter = this._adapter;
}
get isWeakRef() {
return this._isWeakRef;
}
set adapter(v: IAssetAdapter) {
if (this._isWeakRef) {
this._finalizer.unregister(this);
if (v) {
this._weakRefAdapter = new WeakRefColorTransform object. If the rectangle matches the
* boundaries of the bitmap image, this method transforms the color values of
* the entire image.
*
* @param rect A Rectangle object that defines the area of the
* image in which the ColorTransform object is applied.
* @param colorTransform A ColorTransform object that describes the color
* transformation values to apply.
*/
public colorTransform(rect: Rectangle, colorTransform: ColorTransform): void {
let i: number,
j: number,
index: number;
const data = this.data;
for (i = 0; i < rect.width; ++i) {
for (j = 0; j < rect.height; ++j) {
index = (i + rect.x + (j + rect.y) * this._rect.width) * 4;
data[index] = data[index] * colorTransform.redMultiplier + colorTransform.redOffset;
data[index + 1] = data[index + 1] * colorTransform.greenMultiplier + colorTransform.greenOffset;
data[index + 2] = data[index + 2] * colorTransform.blueMultiplier + colorTransform.blueOffset;
data[index + 3] = data[index + 3] * colorTransform.alphaMultiplier + colorTransform.alphaOffset;
}
}
this.invalidateGPU();
}
/**
* Transfers data from one channel of another BitmapImage2D object or the
* current BitmapImage2D object into a channel of the current BitmapImage2D object.
* All of the data in the other channels in the destination BitmapImage2D object
* are preserved.
*
* The source channel value and destination channel value can be one of * following values:
* *BitmapImage2DChannel.REDBitmapImage2DChannel.GREENBitmapImage2DChannel.BLUEBitmapImage2DChannel.ALPHABitmapImage2DChannel.RED,
* BitmapImage2DChannel.BLUE,
* BitmapImage2DChannel.GREEN,
* BitmapImage2DChannel.ALPHA).
* @param destChannel The destination channel. Use a value from the
* BitmapImage2DChannel class
* (BitmapImage2DChannel.RED,
* BitmapImage2DChannel.BLUE,
* BitmapImage2DChannel.GREEN,
* BitmapImage2DChannel.ALPHA).
* @throws TypeError The sourceBitmapImage2D, sourceRect or destPoint are null.
*/
/* eslint-disable-next-line */
public copyChannel(sourceBitmap: BitmapImage2D, sourceRect: Rectangle, destPoint: Point, sourceChannel: number, destChannel: number): void {
this._lastUsedFill = null;
this.unmarkToUnload();
if (destChannel != 8 || !this._imageDataDirty && !sourceBitmap._imageDataDirty) {
const sourceData: Uint8ClampedArray = sourceBitmap.data;
const destData: Uint8ClampedArray = this.data;
const sourceOffset: number = Math.round(Math.log(sourceChannel) / Math.log(2));
const destOffset: number = Math.round(Math.log(destChannel) / Math.log(2));
const sourceX: number = Math.round(sourceRect.x);
const sourceY: number = Math.round(sourceRect.y);
const destX: number = Math.round(destPoint.x);
const destY: number = Math.round(destPoint.y);
let value: number;
let i: number, j: number, sourceIndex: number, destIndex: number;
for (i = 0; i < sourceRect.width; ++i) {
for (j = 0; j < sourceRect.height; ++j) {
sourceIndex = (i + sourceX + (j + sourceY) * sourceBitmap.width) * 4;
destIndex = (i + destX + (j + destY) * this._rect.width) * 4;
value = (sourceOffset == 3) ? sourceData[sourceIndex + 3] : sourceData[sourceIndex + 3] ? sourceData[sourceIndex + sourceOffset] * 255 / sourceData[sourceIndex + 3] : 0;
if (destOffset == 3) {
destData[destIndex + 0] = destData[sourceIndex + 3] ? destData[destIndex + 0] * value / destData[sourceIndex + 3] : 0;
destData[destIndex + 1] = destData[sourceIndex + 3] ? destData[destIndex + 1] * value / destData[sourceIndex + 3] : 0;
destData[destIndex + 2] = destData[sourceIndex + 3] ? destData[destIndex + 2] * value / destData[sourceIndex + 3] : 0;
destData[destIndex + 3] = value;
} else {
destData[destIndex + destOffset] = value;
}
}
}
this.invalidateGPU();
return;
}
if (this._initalFillColor !== null)
this.fillRect(this._rect, this._initalFillColor);
this._stage.filterManager.copyChannel(
sourceBitmap,
this,
sourceRect,
destPoint,
sourceChannel,
destChannel
);
this._imageDataDirty = true;
this.invalidateOwners();
}
/* eslint-disable-next-line */
public merge(source: BitmapImage2D, sourceRect: Rectangle, destPoint: Point, redMultiplier: number, greenMultiplier: number, blueMultiplier: number, alphaMultiplier: number) {
const dest: Uint8ClampedArray = this.getDataInternal(true);
const src: Uint8ClampedArray = source.data;
redMultiplier = ~~redMultiplier;
greenMultiplier = ~~greenMultiplier;
blueMultiplier = ~~blueMultiplier;
alphaMultiplier = ~~alphaMultiplier;
let i: number, j: number, index: number;
for (i = 0; i < sourceRect.width; ++i) {
for (j = 0; j < sourceRect.height; ++j) {
index = (i + sourceRect.x + (j + sourceRect.y) * this.width) * 4;
/* eslint-disable */
dest[index] = ~~((src[index] * redMultiplier + dest[index] * (0x100 - redMultiplier)) / 0x100);
dest[index + 1] = ~~((src[index + 1] * greenMultiplier + dest[index + 1] * (0x100 - greenMultiplier)) / 0x100);
dest[index + 2] = ~~((src[index + 2] * blueMultiplier + dest[index + 2] * (0x100 - blueMultiplier)) / 0x100);
dest[index + 3] = ~~((src[index + 3] * alphaMultiplier + dest[index + 3] * (0x100 - alphaMultiplier)) / 0x100);
/* eslint-enable */
}
}
this.invalidateGPU();
}
/**
* Frees memory that is used to store the BitmapImage2D object.
*
* When the dispose() method is called on an image, the width
* and height of the image are set to 0. All subsequent calls to methods or
* properties of this BitmapImage2D instance fail, and an exception is thrown.
*
BitmapImage2D.dispose() releases the memory occupied by the
* actual bitmap data, immediately(a bitmap can consume up to 64 MB of
* memory). After using BitmapImage2D.dispose(), the BitmapImage2D
* object is no longer usable and an exception may be thrown if
* you call functions on the BitmapImage2D object. However,
* BitmapImage2D.dispose() does not garbage collect the BitmapImage2D
* object(approximately 128 bytes); the memory occupied by the actual
* BitmapImage2D object is released at the time the BitmapImage2D object is
* collected by the garbage collector.
getPixel() method returns an unmultiplied pixel value. No
* alpha information is returned.
*
* All pixels in a BitmapImage2D object are stored as premultiplied color * values. A premultiplied image pixel has the red, green, and blue color * channel values already multiplied by the alpha data. For example, if the * alpha value is 0, the values for the RGB channels are also 0, independent * of their unmultiplied values. This loss of data can cause some problems * when you perform operations. All BitmapImage2D methods take and return * unmultiplied values. The internal pixel representation is converted from * premultiplied to unmultiplied before it is returned as a value. During a * set operation, the pixel value is premultiplied before the raw image pixel * is set.
* * @param x The x position of the pixel. * @param y The y position of the pixel. * @return A number that represents an RGB pixel value. If the(x, * y) coordinates are outside the bounds of the image, the * method returns 0. */ public getPixel(x, y): number { if (!this._rect.contains(x, y)) return 0x0; const index = (~~x + ~~y * this._rect.width) * 4, data = this._data; const r = data[index + 0], g = data[index + 1], b = data[index + 2], a = data[index + 3]; //returns black if fully transparent if (!a) return 0x0; // PMA storage → unpremul; straight alpha → pack as-is if (!this.unpackPMA) { return ((((r * ((0xFF << 8) / a | 0) + 0x7F) >> 8) << 16) | (((g * ((0xFF << 8) / a | 0) + 0x7F) >> 8) << 8) | ((b * ((0xFF << 8) / a | 0) + 0x7F) >> 8)); } return (r << 16) | (g << 8) | b; } /** * Returns an ARGB color value that contains alpha channel data and RGB data. * This method is similar to thegetPixel() method, which
* returns an RGB color without alpha channel data.
*
* All pixels in a BitmapImage2D object are stored as premultiplied color * values. A premultiplied image pixel has the red, green, and blue color * channel values already multiplied by the alpha data. For example, if the * alpha value is 0, the values for the RGB channels are also 0, independent * of their unmultiplied values. This loss of data can cause some problems * when you perform operations. All BitmapImage2D methods take and return * unmultiplied values. The internal pixel representation is converted from * premultiplied to unmultiplied before it is returned as a value. During a * set operation, the pixel value is premultiplied before the raw image pixel * is set.
* * @param x The x position of the pixel. * @param y The y position of the pixel. * @return A number representing an ARGB pixel value. If the(x, * y) coordinates are outside the bounds of the image, 0 is * returned. */ public getPixel32(x, y): number { if (!this._rect.contains(x, y)) return 0x0; let index: number = (~~x + ~~y * this._rect.width) * 4; const data: Uint8ClampedArray = this.getDataInternal(true); const r = data[index++], g = data[index++], b = data[index++], a = data[index]; if (!a) return 0x0; // PMA storage → unpremul RGB into ARGB; straight alpha → pack without dividing if (!this.unpackPMA) { return ((a << 24) | (((r * ((0xFF << 8) / a | 0) + 0x7F) >> 8) << 16) | (((g * ((0xFF << 8) / a | 0) + 0x7F) >> 8) << 8) | ((b * ((0xFF << 8) / a | 0) + 0x7F) >> 8)); } return ((a << 24) | (r << 16) | (g << 8) | b) >>> 0; } /** * Returns unmultiplied ARGB bytes for a rectangular region: * packed as [A,R,G,B, A,R,G,B, ...] (same channel order as getPixel32 / ByteArray). * Always returns a copy - never aliases internal RGBA storage. */ public getPixels(rect: Rectangle): Uint8ClampedArray { const data = this.getDataInternal(true); const isPMA = !this.unpackPMA; const x = rect.x | 0; const y = rect.y | 0; const width = rect.width | 0; const height = rect.height | 0; // Internal _data stays RGBA (PMA when !_unpackPMA). Copy out as ARGB. const target = new Uint8ClampedArray(width * height * 4); for (let j = 0; j < height; ++j) { let src = (x + (j + y) * this._rect.width) * 4; let dst = j * width * 4; for (let i = 0; i < width; ++i) { const r = data[src]; const g = data[src + 1]; const b = data[src + 2]; const a = data[src + 3]; target[dst] = a; if (isPMA) { target[dst + 1] = (r * ((0xFF << 8) / a | 0) + 0x7F) >> 8; target[dst + 2] = (g * ((0xFF << 8) / a | 0) + 0x7F) >> 8; target[dst + 3] = (b * ((0xFF << 8) / a | 0) + 0x7F) >> 8; } else { target[dst + 1] = r; target[dst + 2] = g; target[dst + 3] = b; } src += 4; dst += 4; } } return target; } /** * Raw storage RGBA for one pixel (PMA when !_unpackPMA). Internal use - * public unmultiplied ARGB reads should use getPixel32 / getPixels. */ public getPixelData(x, y, imagePixel: Uint8ClampedArray): void { let index: number = (x + y * this._rect.width) * 4; const data: Uint8ClampedArray = this.getDataInternal(true); imagePixel[0] = data[index++]; imagePixel[1] = data[index++]; imagePixel[2] = data[index++]; imagePixel[3] = data[index]; } public setPixelData(x, y, imagePixel: Uint8ClampedArray): void { const index: number = (x + y * this._rect.width) * 4; const data: Uint8ClampedArray = this.getDataInternal(true); data[index + 0] = imagePixel[0]; data[index + 1] = imagePixel[1]; data[index + 2] = imagePixel[2]; data[index + 3] = this._transparent ? imagePixel[3] : 0xFF; this.invalidateGPU(); } /** * Locks an image so that any objects that reference the BitmapImage2D object, * such as Bitmap objects, are not updated when this BitmapImage2D object * changes. To improve performance, use this method along with the *unlock() method before and after numerous calls to the
* setPixel() or setPixel32() method.
*
*/
public lock(): void {
if (this._locked)
return;
this._locked = true;
}
/**
* Converts an Array into a rectangular region of pixel data. For each pixel,
* an Array element is read and written into the BitmapImage2D pixel. The data
* in the Array is expected to be 32-bit ARGB pixel values.
*
* @param rect Specifies the rectangular region of the BitmapImage2D
* object.
* @param inputArray An Array that consists of 32-bit unmultiplied pixel
* values to be used in the rectangular region.
* @throws RangeError The vector array is not large enough to read all the
* pixel data.
*/
public setArray(rect: Rectangle, inputArray: ArrayNote: To increase performance, when you use the
* setPixel() or setPixel32() method repeatedly,
* call the lock() method before you call the
* setPixel() or setPixel32() method, and then call
* the unlock() method when you have made all pixel changes.
* This process prevents objects that reference this BitmapImage2D instance from
* updating until you finish making the pixel changes.
setPixel()
* method; the main difference is that the setPixel32() method
* takes an ARGB color value that contains alpha channel information.
*
* All pixels in a BitmapImage2D object are stored as premultiplied color * values. A premultiplied image pixel has the red, green, and blue color * channel values already multiplied by the alpha data. For example, if the * alpha value is 0, the values for the RGB channels are also 0, independent * of their unmultiplied values. This loss of data can cause some problems * when you perform operations. All BitmapImage2D methods take and return * unmultiplied values. The internal pixel representation is converted from * premultiplied to unmultiplied before it is returned as a value. During a * set operation, the pixel value is premultiplied before the raw image pixel * is set.
* *Note: To increase performance, when you use the
* setPixel() or setPixel32() method repeatedly,
* call the lock() method before you call the
* setPixel() or setPixel32() method, and then call
* the unlock() method when you have made all pixel changes.
* This process prevents objects that reference this BitmapImage2D instance from
* updating until you finish making the pixel changes.
ByteArray.readUnsignedInt() method is called and
* the return value is written into the pixel. If the byte array ends before
* the full rectangle is written, the function returns. The data in the byte
* array is expected to be 32-bit ARGB pixel values. No seeking is performed
* on the byte array before or after the pixels are read.
*
* @param rect Specifies the rectangular region of the BitmapImage2D
* object.
* @param input A ByteArray object that consists of 32-bit
* unmultiplied pixel values to be used in the
* rectangular region.
* @throws EOFError The inputByteArray object does not include
* enough data to fill the area of the rect
* rectangle. The method fills as many pixels as possible
* before throwing the exception.
* @throws TypeError The rect or inputByteArray are null.
*/
public setPixels(rect: Rectangle, input: Uint8ClampedArray): void {
const data = this.getDataInternal(true);
// Input is unmultiplied ARGB bytes [A,R,G,B,...]; convert to PMA RGBA storage.
// Raw RGBA/PMA writers should assign into getDataInternal() / _data directly.
const imageWidth: number = this._rect.width;
const inputWidth: number = rect.width | 0;
const inputHeight: number = rect.height | 0;
if (rect.equals(this._rect)) {
for (let i = 0; i < data.length; i += 4) {
const a = input[i];
data[i] = ((input[i + 1] * a + 0x7F) / 0xFF) | 0;
data[i + 1] = ((input[i + 2] * a + 0x7F) / 0xFF) | 0;
data[i + 2] = ((input[i + 3] * a + 0x7F) / 0xFF) | 0;
data[i + 3] = a;
}
} else {
for (let j = 0; j < inputHeight; ++j) {
let src = j * inputWidth * 4;
let dst = (rect.x + (j + rect.y) * imageWidth) * 4;
for (let i = 0; i < inputWidth; ++i) {
const a = input[src];
data[dst] = ((input[src + 1] * a + 0x7F) / 0xFF) | 0;
data[dst + 1] = ((input[src + 2] * a + 0x7F) / 0xFF) | 0;
data[dst + 2] = ((input[src + 3] * a + 0x7F) / 0xFF) | 0;
data[dst + 3] = a;
src += 4;
dst += 4;
}
}
}
this._unpackPMA = false;
this.invalidateGPU();
}
public applyFilter (source: BitmapImage2D, sourceRect: Rectangle, destPoint: Point, filter: any): boolean {
return false;
}
/**
* Unlocks an image so that any objects that reference the BitmapImage2D object,
* such as Bitmap objects, are updated when this BitmapImage2D object changes.
* To improve performance, use this method along with the lock()
* method before and after numerous calls to the setPixel() or
* setPixel32() method.
*
* @param changeRect The area of the BitmapImage2D object that has changed. If
* you do not specify a value for this parameter, the
* entire area of the BitmapImage2D object is considered
* changed.
*/
public unlock(): void {
if (!this._locked)
return;
this._locked = false;
this.invalidate();
}
/**
* @inheritdoc
*/
set alphaChannel(buff: Uint8Array) {
if (!buff) {
return;
}
if (buff.length !== this.width * this.height) {
throw (
'error when trying to merge the alpha channel into the image.' +
'the length of the alpha channel should be 1/4 of the length of the imageData');
}
// if assigments after initialiszation, apply it immediate
if (this._data && this.wasUpload) {
const buff = this._alphaChannel;
for (let i = 0; i < buff.length; i++) {
this._data[i * 4 + 3] = buff[i];
}
this._unpackPMA = false;
this.invalidateGPU();
return;
}
this._alphaChannel = buff;
this._unpackPMA = false;
}
/**
*
* @returns {ImageData}
*/
public get data(): Uint8ClampedArray {
return this.getDataInternal(true);
}
/**
*
* @param width
* @param height
* @private
*/
public _setSize(width: number, height: number): void {
const data: Uint8ClampedArray = this.data;
this._data = new Uint8ClampedArray(4 * width * height);
const inputWidth: number = (this._rect.width < width) ? this._rect.width : width;
const inputHeight: number = (this._rect.height < height) ? this._rect.height : height;
for (let i = 0; i < inputHeight; ++i)
this._data.set(data.subarray(i * inputWidth * 4, (i + 1) * inputWidth * 4), i * width * 4);
super._setSize(width, height);
}
private /*internal */ getDebugCanvas(onlyA = false) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.height = this.height;
canvas.width = this.width;
const data = this.getDataInternal(true, false);
const imageData = new ImageData(new Uint8ClampedArray(data), this.width, this.height);
if (onlyA) {
for (let i = 0; i < imageData.data.length; i += 4) {
const a = imageData.data[i + 3];
imageData.data[i + 0] = a;
imageData.data[i + 1] = a;
imageData.data[i + 2] = a;
}
}
ctx.putImageData(imageData, 0,0);
return canvas;
}
private /*internal*/ getDebugImage(onlyA = false) {
const canvas = this.getDebugCanvas(onlyA);
const pngString = canvas.toDataURL('image/png');
const a = document.createElement('a');
a.href = pngString;
a.download = 'SceneImage_' + this.id + (onlyA ? '_alpha' : '_full');
a.click();
}
}
import { ITexture } from '../base/ITexture';
import { ITextureBase } from '../base/ITextureBase';
import { BitmapImageUtils } from '../utils/BitmapImageUtils';
import { _Stage_Image2D } from './Image2D';
import { ContextWebGL } from '../webgl/ContextWebGL';
import { Stage } from '../Stage';
/**
*
* @class away.pool.ImageObjectBase
*/
export class _Stage_BitmapImage2D extends _Stage_Image2D {
private onUnload = () =>{
if (this._texture) {
this._texture.dispose();
this._texture = null;
this._invalid = true;
(