(golden: string, generated: string)
| 232 | } |
| 233 | |
| 234 | async function imageDiff(golden: string, generated: string) { |
| 235 | return await new Promise<ImageDiff>(async (resolve, reject) => { |
| 236 | try { |
| 237 | const imageDiff: ImageDiff = {rawMisMatchPercentage: 0, diffPath: ''}; |
| 238 | const diffText = await execImageDiffCommand(`${IMAGE_DIFF_BINARY} --histogram ${golden} ${generated}`); |
| 239 | |
| 240 | // Parse out the number from the cmd output, i.e. diff: 48.9% failed => 48.9 |
| 241 | imageDiff.rawMisMatchPercentage = Number(diffText.replace(/^diff:\s/, '').replace(/%.*/, '')); |
| 242 | |
| 243 | if (Number.isNaN(imageDiff.rawMisMatchPercentage)) { |
| 244 | reject('Unable to compare images'); |
| 245 | } |
| 246 | |
| 247 | // Only create a diff image if the images are different. |
| 248 | if (imageDiff.rawMisMatchPercentage > 0) { |
| 249 | imageDiff.diffPath = path.join(path.dirname(generated), `${path.basename(generated, '.png')}-diff.png`); |
| 250 | await execImageDiffCommand(`${IMAGE_DIFF_BINARY} --diff ${golden} ${generated} ${imageDiff.diffPath}`); |
| 251 | } |
| 252 | |
| 253 | resolve(imageDiff); |
| 254 | } catch (e) { |
| 255 | reject(new Error(`Error when running image_diff: ${e.stack}`)); |
| 256 | } |
| 257 | }); |
| 258 | } |
| 259 | |
| 260 | async function execImageDiffCommand(cmd: string) { |
| 261 | return await new Promise<string>((resolve, reject) => { |
no test coverage detected