MCPcopy Create free account
hub / github.com/ChromeDevTools/devtools-frontend / imageDiff

Function imageDiff

test/shared/screenshots.ts:234–258  ·  view source on GitHub ↗
(golden: string, generated: string)

Source from the content-addressed store, hash-verified

232}
233
234async 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
260async function execImageDiffCommand(cmd: string) {
261 return await new Promise<string>((resolve, reject) => {

Callers 1

compareFunction · 0.85

Calls 3

execImageDiffCommandFunction · 0.85
replaceMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected