obartra / ssim

🖼🔬 JavaScript Image Comparison
http://obartra.github.io/ssim
MIT License
299 stars 14 forks source link

Argument of type 'Promise<unknown>' is not assignable to parameter of type 'ImageData'. #309

Closed airda2895 closed 2 years ago

airda2895 commented 2 years ago

Hi, I'm getting this error when trying to use ssim on Angular (TypeScript). I don't know if I'm doing something wrong, but it looks like the code which is supposed to load the image, returns a type of object which ssimdoesn't expect.

My code:

@HostListener('document:keydown', ['$event']) onKeyDown = (e: any) => {
    if(e.key === 's') {
      const img1 = this.loadImage("./img1.jpg");
      const img2 = this.loadImage("./img2.jpg");

      const { mssim, performance } = ssim(img1, img2); // Error on this line

      console.log(`SSIM: ${mssim} (${performance}ms)`);
    }
  }

private getLimitDimensions(
    width: number,
    height: number,
    limit?: number
  ) {
    if (limit && width >= limit && height >= limit) {
      const ratio = width / height;

      if (ratio > 1) {
        return { height: limit, width: Math.round(limit / ratio) };
      }
      return { height: Math.round(limit * ratio), width: limit };
    }
    return { width, height };
  }

  private loadImage(url: string, limit = 0) {
    const img = new Image()

    return new Promise((resolve, reject) => {
      img.onload = () => {
        const { width, height } = this.getLimitDimensions(img.width, img.height, limit)

        if (width === 0 || height === 0) {
          return reject('Failed to load image')
        }

        this.gameContentEl.width = width
        this.gameContentEl.height = height

        this.gameContentCX.drawImage(img, 0, 0, img.width, img.height, 0, 0, width, height)
        return resolve(this.gameContentCX.getImageData(0, 0, width, height))
      }
      img.onerror = reject
      img.src = url
    })
  }
Screenshot 2022-02-23 at 15 44 42
obartra commented 2 years ago

Bones Adrià!

This seems more of an issue with the types than the library, the library expect an object with ImageData properties. Here, if getImageData returns an object that matches that criteria (not sure if it does) you can cast it and do resolve(this.gameContentCX.getImageData(0, 0, width, height) as ImageData)

airda2895 commented 2 years ago

Bones Adrià!

This seems more of an issue with the types than the library, the library expect an object with ImageData properties. Here, if getImageData returns an object that matches that criteria (not sure if it does) you can cast it and do resolve(this.gameContentCX.getImageData(0, 0, width, height) as ImageData)

Thanks @obartra!

I've just tried that, but unfortunately, I keep getting the same error:

Screenshot 2022-02-23 at 17 31 59
stale[bot] commented 2 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.