All files / src Image.ts

96.97% Statements 32/33
75% Branches 3/4
100% Functions 4/4
96.88% Lines 31/32

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 725x 5x 5x   5x             4x 4x 4x 4x 4x     4x 4x 4x               4x                 213x 213x 213x 213x 213x 213x 42577743x 42577743x 42577743x 42577743x   213x               213x       217x 215x 215x       213x 211x 211x      
import { CppObject } from './CppObject';
import { Symbol } from './Symbol';
import { getInstance } from './instance';
 
export class Image extends CppObject {
  static async createFromGrayBuffer(
    width: number,
    height: number,
    dataBuf: ArrayBuffer,
    sequence_num: number = 0
  ): Promise<Image> {
    const inst = await getInstance();
    const heap = new Uint8Array(inst.memory.buffer);
    const data = new Uint8Array(dataBuf);
    const len = width * height;
    Iif (len !== data.byteLength) {
      throw Error('dataBuf does not match width and height');
    }
    const buf = inst.malloc(len);
    heap.set(data, buf);
    const ptr = inst.Image_create(
      width,
      height,
      0x30303859 /* Y800 */,
      buf,
      len,
      sequence_num
    );
    return new this(ptr, inst);
  }
 
  static async createFromRGBABuffer(
    width: number,
    height: number,
    dataBuf: ArrayBuffer,
    sequence_num: number = 0
  ): Promise<Image> {
    const inst = await getInstance();
    const heap = new Uint8Array(inst.memory.buffer);
    const data = new Uint8Array(dataBuf);
    const len = width * height;
    const buf = inst.malloc(len);
    for (let i = 0; i < len; ++i) {
      const r = data[i * 4];
      const g = data[i * 4 + 1];
      const b = data[i * 4 + 2];
      heap[buf + i] = (r * 19595 + g * 38469 + b * 7472) >> 16;
    }
    const ptr = inst.Image_create(
      width,
      height,
      0x30303859 /* Y800 */,
      buf,
      len,
      sequence_num
    );
    return new this(ptr, inst);
  }
 
  destory(): void {
    this.checkAlive();
    this.inst.Image_destory(this.ptr);
    this.ptr = 0;
  }
 
  getSymbols(): Array<Symbol> {
    this.checkAlive();
    const res = this.inst.Image_get_symbols(this.ptr);
    return Symbol.createSymbolsFromPtr(res, this.inst.memory.buffer);
  }
}