UNPKG

tav-media

Version:

Cross platform media editing framework

140 lines (139 loc) 5.34 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var PAGImage_1; import { NativeImage } from './core/native-image'; import { wasmAwaitRewind, wasmAsyncMethod, destroyVerify } from './utils/decorators'; import { PAGModule } from './pag-module'; import { Matrix } from './core/matrix'; let PAGImage = PAGImage_1 = class PAGImage { constructor(wasmIns) { this.isDestroyed = false; this.wasmIns = wasmIns; } /** * Create pag image from image file. */ static fromFile(data) { return __awaiter(this, void 0, void 0, function* () { return new Promise((resolve, reject) => { const image = new Image(); image.onload = () => __awaiter(this, void 0, void 0, function* () { resolve(PAGImage_1.fromSource(image)); }); image.onerror = (error) => { reject(error); }; image.src = URL.createObjectURL(data); }); }); } /** * Create pag image from image source or video source. * Make sure the target pixel is shown on the screen. * Like * ``` javascript * Image.onload = async () => { * return await PAGImage.fromSource(Image) * } * ``` */ static fromSource(source) { const nativeImage = new NativeImage(source); const wasmIns = PAGModule._PAGImage._FromNativeImage(nativeImage); if (!wasmIns) throw new Error('Make PAGImage from source fail!'); return new PAGImage_1(wasmIns); } /** * Creates a PAGImage object from an array of pixel data, return null if it's not valid pixels. */ static fromPixels(pixels, width, height, colorType, alphaType) { const rowBytes = width * (colorType === 1 /* ColorType.ALPHA_8 */ ? 1 : 4); const dataPtr = PAGModule._malloc(pixels.byteLength); const dataOnHeap = new Uint8Array(PAGModule.HEAPU8.buffer, dataPtr, pixels.byteLength); dataOnHeap.set(pixels); const wasmIns = PAGModule._PAGImage._FromPixels(dataPtr, width, height, rowBytes, colorType, alphaType); if (!wasmIns) throw new Error('Make PAGImage from pixels fail!'); return new PAGImage_1(wasmIns); } /** * Creates a PAGImage object from the specified backend texture, return null if the texture is * invalid. */ static fromTexture(textureID, width, height, flipY) { const wasmIns = PAGModule._PAGImage._FromTexture(textureID, width, height, flipY); if (!wasmIns) throw new Error('Make PAGImage from texture fail!'); return new PAGImage_1(wasmIns); } /** * Returns the width in pixels. */ width() { return this.wasmIns._width(); } /** * Returns the height in pixels. */ height() { return this.wasmIns._height(); } /** * Returns the current scale mode. The default value is PAGScaleMode::LetterBox. */ scaleMode() { return this.wasmIns._scaleMode(); } /** * Specifies the rule of how to scale the content to fit the image layer's original size. * The matrix changes when this method is called. */ setScaleMode(scaleMode) { this.wasmIns._setScaleMode(scaleMode); } /** * Returns a copy of current matrix. */ matrix() { const wasmIns = this.wasmIns._matrix(); if (!wasmIns) throw new Error('Get matrix fail!'); return new Matrix(wasmIns); } /** * Set the transformation which will be applied to the content. * The scaleMode property will be set to PAGScaleMode::None when this method is called. */ setMatrix(matrix) { this.wasmIns._setMatrix(matrix.wasmIns); } /** * Destroy the pag image. */ destroy() { this.wasmIns.delete(); this.isDestroyed = true; } }; __decorate([ wasmAsyncMethod ], PAGImage, "fromFile", null); PAGImage = PAGImage_1 = __decorate([ destroyVerify, wasmAwaitRewind ], PAGImage); export { PAGImage };