import './InvertedLuminanceSource' ; import InvertedLuminanceSource from './InvertedLuminanceSource' ; import LuminanceSource from './LuminanceSource' ; import Exception from './Exception' ; import System from './util/System' ; author dswitkin google author Betaminos
  export default class RGBLuminanceSource extends LuminanceSource { private luminances : Uint8ClampedArray ; public constructor ( luminances , width , height , private dataWidth ? , private dataHeight ? , private left ? , private top ? ) { super ( width , height ) ; if ( luminances . BYTES_PER_ELEMENT === 4 ) { const size = width * height ; const luminancesUint8Array = new Uint8ClampedArray ( size ) ; for ( let offset = 0 ; offset < size ; offset ++ ) { const pixel = luminances [ offset ] ; const r = ( pixel >> 16 ) & 0xff ; const g2 = ( pixel >> 7 ) & 0x1fe ; const b = pixel & 0xff ; luminancesUint8Array [ offset ] = ( ( r + g2 + b ) / 4 ) & 0xFF ; } this . luminances = luminancesUint8Array ; } else { this . luminances = < Uint8ClampedArray > luminances ; } if ( undefined === dataWidth ) { this . dataWidth = width ; } if ( undefined === dataHeight ) { this . dataHeight = height ; } if ( undefined === left ) { this . left = 0 ; } if ( undefined === top ) { this . top = 0 ; } if ( this . left + width > this . dataWidth || this . top + height > this . dataHeight ) { throw new Exception ( Exception . IllegalArgumentException , "Crop rectangle does not fit within image data." ) ; } } public getRow ( y , row ? ) { if ( y < 0 || y >= this . getHeight ( ) ) { throw new Exception ( Exception . IllegalArgumentException , "Requested row is outside the image: " + y ) ; } const width = this . getWidth ( ) ; if ( row === null || row === undefined || row . length < width ) { row = new Uint8ClampedArray ( width ) ; } const offset = ( y + this . top ) * this . dataWidth + this . left ; System . arraycopy ( this . luminances , offset , row , 0 , width ) ; return row ; } public getMatrix ( ) { const width = this . getWidth ( ) ; const height = this . getHeight ( ) ; if ( width === this . dataWidth && height === this . dataHeight ) { return this . luminances ; } const area = width * height ; const matrix = new Uint8ClampedArray ( area ) ; let inputOffset = this . top * this . dataWidth + this . left ; if ( width === this . dataWidth ) { System . arraycopy ( this . luminances , inputOffset , matrix , 0 , area ) ; return matrix ; } for ( let y = 0 ; y < height ; y ++ ) { const outputOffset = y * width ; System . arraycopy ( this . luminances , inputOffset , matrix , outputOffset , width ) ; inputOffset += this . dataWidth ; } return matrix ; } public isCropSupported ( ) { return true ; } public crop ( left , top , width , height ) { return new RGBLuminanceSource ( this . luminances , width , height , this . dataWidth , this . dataHeight , this . left + left , this . top + top ) ; } public invert ( ) { return new InvertedLuminanceSource ( this ) ; } }