import Packer , { PackImpl } from './packer' ; import { clContext as nodenCLContext , OpenCLBuffer , KernelParams } from 'nodencl' ; import { gamma2linearLUT , ycbcr2rgbMatrix , matrixFlatten , rgb2rgbMatrix , linear2gammaLUT , rgb2ycbcrMatrix } from './colourMaths' ; import { ClJobs , JobCB , JobID } from '../clJobQueue' ; export class Loader extends Packer { private readonly gammaArray : Float32Array ; private readonly colMatrixArray : Float32Array | null = null ; private readonly gamutMatrixArray : Float32Array ; private gammaLut : OpenCLBuffer | null = null ; private colMatrix : OpenCLBuffer | null = null ; private gamutMatrix : OpenCLBuffer | null = null ; constructor ( clContext , colSpec , outColSpec , packImpl , clJobs ) { super ( clContext , packImpl , clJobs ) ; this . gammaArray = gamma2linearLUT ( colSpec ) ; if ( ! this . packImpl . getIsRGB ( ) ) { const colMatrix2d = ycbcr2rgbMatrix ( colSpec , this . packImpl . numBits , this . packImpl . lumaBlack , this . packImpl . lumaWhite , this . packImpl . chromaRange ) ; this . colMatrixArray = matrixFlatten ( colMatrix2d ) ; } const gamutMatrix2d = rgb2rgbMatrix ( colSpec , outColSpec ) ; this . gamutMatrixArray = matrixFlatten ( gamutMatrix2d ) ; } async init ( ) { await super . init ( ) ; this . gammaLut = await this . clContext . createBuffer ( this . gammaArray . byteLength , 'readonly' , 'coarse' , undefined , 'loader gammaLut' ) ; await this . gammaLut . hostAccess ( 'writeonly' ) ; Buffer . from ( this . gammaArray . buffer ) . copy ( this . gammaLut ) ; if ( this . colMatrixArray ) { this . colMatrix = await this . clContext . createBuffer ( this . colMatrixArray . byteLength , 'readonly' , 'none' , undefined , 'loader colMatrix' ) ; await this . colMatrix . hostAccess ( 'writeonly' ) ; Buffer . from ( this . colMatrixArray . buffer ) . copy ( this . colMatrix ) ; } this . gamutMatrix = await this . clContext . createBuffer ( this . gamutMatrixArray . byteLength , 'readonly' , 'none' , undefined , 'loader gamutMatrix' ) ; await this . gamutMatrix . hostAccess ( 'writeonly' ) ; Buffer . from ( this . gamutMatrixArray . buffer ) . copy ( this . gamutMatrix ) ; } addRefs ( ) { this . gammaLut ?. addRef ( ) ; this . colMatrix ?. addRef ( ) ; this . gamutMatrix ?. addRef ( ) ; } releaseRefs ( ) { this . gammaLut ?. release ( ) ; this . colMatrix ?. release ( ) ; this . gamutMatrix ?. release ( ) ; } run ( params , id , cb ) { if ( this . program === null ) throw new Error ( 'Loader.run failed with no program available' ) ; this . addRefs ( ) ; const kernelParams = this . packImpl . getKernelParams ( params ) ; kernelParams . gammaLut = this . gammaLut ; kernelParams . gamutMatrix = this . gamutMatrix ; if ( this . colMatrix ) kernelParams . colMatrix = this . colMatrix ; this . clJobs . add ( id , this . packImpl . getName ( ) , this . program , kernelParams , ( ) => { this . releaseRefs ( ) ; cb ( ) ; } ) ; } } export class Saver extends Packer { private readonly gammaArray : Float32Array ; private readonly colMatrixArray : Float32Array | null = null ; private gammaLut : OpenCLBuffer | null = null ; private colMatrix : OpenCLBuffer | null = null ; constructor ( clContext , colSpec , packImpl , clJobs ) { super ( clContext , packImpl , clJobs ) ; this . gammaArray = linear2gammaLUT ( colSpec ) ; if ( ! this . packImpl . getIsRGB ( ) ) { const colMatrix2d = rgb2ycbcrMatrix ( colSpec , this . packImpl . numBits , this . packImpl . lumaBlack , this . packImpl . lumaWhite , this . packImpl . chromaRange ) ; this . colMatrixArray = matrixFlatten ( colMatrix2d ) ; } } async init ( ) { await super . init ( ) ; this . gammaLut = await this . clContext . createBuffer ( this . gammaArray . byteLength , 'readonly' , 'coarse' , undefined , 'saver gammaLut' ) ; await this . gammaLut . hostAccess ( 'writeonly' ) ; Buffer . from ( this . gammaArray . buffer ) . copy ( this . gammaLut ) ; if ( this . colMatrixArray ) { this . colMatrix = await this . clContext . createBuffer ( this . colMatrixArray . byteLength , 'readonly' , 'none' , undefined , 'saver colMatrix' ) ; await this . colMatrix . hostAccess ( 'writeonly' ) ; Buffer . from ( this . colMatrixArray . buffer ) . copy ( this . colMatrix ) ; } } addRefs ( ) { this . gammaLut ?. addRef ( ) ; this . colMatrix ?. addRef ( ) ; } releaseRefs ( ) { this . gammaLut ?. release ( ) ; this . colMatrix ?. release ( ) ; } run ( params , id , cb ) { if ( this . program === null ) throw new Error ( 'Saver.run failed with no program available' ) ; this . addRefs ( ) ; const kernelParams = this . packImpl . getKernelParams ( params ) ; kernelParams . gammaLut = this . gammaLut ; if ( this . colMatrix ) kernelParams . colMatrix = this . colMatrix ; this . clJobs . add ( id , this . packImpl . getName ( ) , this . program , kernelParams , ( ) => { this . releaseRefs ( ) ; cb ( ) ; } ) ; } }