import { LoggerManager } from "@here/harp-utils" ; import * as THREE from "three" ; import { MipMapGenerator } from "./MipMapGenerator" ; /** Any type supported by WebGLRenderingContext.texImage2D() for texture creation */ export type TexturizableImage = HTMLImageElement | HTMLCanvasElement | HTMLVideoElement | ImageData | ImageBitmap ; const logger = LoggerManager . instance . create ( "loadImage" ) ; const mipMapGenerator = new MipMapGenerator ( ) ; /**
 * `ImageItem` is used to identify an image in the  ImageCache .
  export class ImageItem { /** Mip maps for image data */ mipMaps ? : ImageData [ ] ; /** Turns to `true` if the loading has been cancelled. */ cancelled ? : boolean ; /** `loadingPromise` is only used during loading/generating the image. */ private loadingPromise ? : Promise < ImageItem | undefined > ; param url param image constructor ( readonly url , public image ? ) { } get loaded ( ) : boolean { return this . image !== undefined && this . mipMaps !== undefined ; } get loading ( ) : boolean { return this . loadingPromise !== undefined ; } /**
     * Load an  ImageItem .
     *
     *  remarks param imageItem returns An  ImageItem if the image has already been loaded, a promise otherwise.
      loadImage ( ) { if ( this . loaded ) { return Promise . resolve ( this ) ; } if ( this . loading ) { return this . loadingPromise ! ; } this . loadingPromise = new Promise ( ( resolve , reject ) => { if ( this . image ) { const image = this . image ; if ( image instanceof HTMLImageElement && ! image . complete ) { image . addEventListener ( "load" , this . finalizeImage . bind ( this , image , resolve ) ) ; image . addEventListener ( "error" , reject ) ; } else { this . finalizeImage ( this . image , resolve ) ; } return ; } logger . debug ( `Loading image: ${ this . url }` ) ; if ( this . cancelled === true ) { logger . debug ( `Cancelled loading image: ${ this . url }` ) ; resolve ( undefined ) ; } else { new THREE . ImageLoader ( ) . load ( this . url , ( image ) => { if ( this . cancelled === true ) { logger . debug ( `Cancelled loading image: ${ this . url }` ) ; resolve ( undefined ) ; return ; } this . finalizeImage ( image , resolve ) ; } , undefined , errorEvent => { logger . error ( `... loading image failed: ${ this . url } : ${ errorEvent }` ) ; this . loadingPromise = undefined ; reject ( `... loading image failed: ${ this . url } : ${ errorEvent }` ) ; } ) ; } } ) ; return this . loadingPromise ; } private finalizeImage ( image , resolve ) { this . image = image ; this . mipMaps = mipMapGenerator . generateTextureAtlasMipMap ( this ) ; this . loadingPromise = undefined ; resolve ( this ) ; } }