{"version":3,"file":"compare_image.mjs","names":["pixelmatch"],"sources":["../../src/shared/compare_image.ts"],"sourcesContent":["import pixelmatch from 'pixelmatch'\nimport { type Options, ssim } from 'ssim.js'\nimport { convertThresholdUnit } from './convert_threshold_unit.ts'\nimport type { ComparisonMethod, ImageSnapshotCompareOptions } from './types.ts'\n\nexport function compareImage<M extends ComparisonMethod>(\n\timg1: Uint8ClampedArray | Buffer,\n\timg2: Uint8ClampedArray | Buffer,\n\toutput: Uint8ClampedArray | Buffer,\n\twidth: number,\n\theight: number,\n\toptions: ImageSnapshotCompareOptions<M> = {} as any,\n) {\n\tconst pixelDiff =\n\t\toptions.comparisonMethod === 'ssim'\n\t\t\t? compareWithSsim(img1, img2, output, width, height, options.diffOptions)\n\t\t\t: pixelmatch(img1, img2, output, width, height, options.diffOptions)\n\tconst diffAmount = convertThresholdUnit(\n\t\t{ failureThresholdType: options.failureThresholdType ?? 'pixel', width, height },\n\t\tpixelDiff,\n\t)\n\n\treturn {\n\t\tpass: diffAmount <= (options.failureThreshold ?? 0),\n\t\tdiffAmount,\n\t}\n}\n\n/**\n * @author This code is based on `jest-image-snapshot` implementation.\n */\nfunction compareWithSsim(\n\timg1: Uint8ClampedArray | Buffer,\n\timg2: Uint8ClampedArray | Buffer,\n\toutput: Uint8ClampedArray | Buffer,\n\twidth: number,\n\theight: number,\n\tdiffOptions?: Partial<Options>,\n) {\n\tconst newImage = { data: img1, width: width, height: height }\n\tconst baselineImage = { data: img2, width: width, height: height }\n\tconst { ssim_map, mssim } = ssim(newImage as any, baselineImage as any, {\n\t\tssim: 'bezkrovny',\n\t\t...diffOptions,\n\t})\n\t// Converts the SSIM value to different pixels based on image width and height\n\t// conforms to how pixelmatch works.\n\tconst diffPixels = Math.round((1 - mssim) * width * height)\n\tconst diffRgbaPixels = new DataView(output.buffer, output.byteOffset)\n\tfor (let ln = 0; ln !== height; ++ln) {\n\t\tfor (let pos = 0; pos !== width; ++pos) {\n\t\t\tconst rPos = ln * width + pos\n\t\t\t// initial value is transparent.  We'll add in the SSIM offset.\n\t\t\t// red (ff) green (00) blue (00) alpha (00)\n\t\t\tconst diffValue =\n\t\t\t\t0xff000000 +\n\t\t\t\tMath.floor(\n\t\t\t\t\t0xff *\n\t\t\t\t\t\t(1 -\n\t\t\t\t\t\t\tssim_map.data[\n\t\t\t\t\t\t\t\tssim_map.width * Math.round((ssim_map.height * ln) / height) +\n\t\t\t\t\t\t\t\t\tMath.round((ssim_map.width * pos) / width)\n\t\t\t\t\t\t\t]!),\n\t\t\t\t)\n\t\t\tdiffRgbaPixels.setUint32(rPos * 4, diffValue)\n\t\t}\n\t}\n\treturn diffPixels\n}\n"],"mappings":";;;;;AAKA,SAAgB,aACf,MACA,MACA,QACA,OACA,QACA,UAA0C,EAAE,EAC3C;CACD,MAAM,YACL,QAAQ,qBAAqB,SAC1B,gBAAgB,MAAM,MAAM,QAAQ,OAAO,QAAQ,QAAQ,YAAY,GACvEA,WAAW,MAAM,MAAM,QAAQ,OAAO,QAAQ,QAAQ,YAAY;CACtE,MAAM,aAAa,qBAClB;EAAE,sBAAsB,QAAQ,wBAAwB;EAAS;EAAO;EAAQ,EAChF,UACA;AAED,QAAO;EACN,MAAM,eAAe,QAAQ,oBAAoB;EACjD;EACA;;;;;AAMF,SAAS,gBACR,MACA,MACA,QACA,OACA,QACA,aACC;CAGD,MAAM,EAAE,UAAU,UAAU,KAFX;EAAE,MAAM;EAAa;EAAe;EAAQ,EACvC;EAAE,MAAM;EAAa;EAAe;EAAQ,EACM;EACvE,MAAM;EACN,GAAG;EACH,CAAC;CAGF,MAAM,aAAa,KAAK,OAAO,IAAI,SAAS,QAAQ,OAAO;CAC3D,MAAM,iBAAiB,IAAI,SAAS,OAAO,QAAQ,OAAO,WAAW;AACrE,MAAK,IAAI,KAAK,GAAG,OAAO,QAAQ,EAAE,GACjC,MAAK,IAAI,MAAM,GAAG,QAAQ,OAAO,EAAE,KAAK;EACvC,MAAM,OAAO,KAAK,QAAQ;EAG1B,MAAM,YACL,aACA,KAAK,MACJ,OACE,IACA,SAAS,KACR,SAAS,QAAQ,KAAK,MAAO,SAAS,SAAS,KAAM,OAAO,GAC3D,KAAK,MAAO,SAAS,QAAQ,MAAO,MAAM,GAE9C;AACF,iBAAe,UAAU,OAAO,GAAG,UAAU;;AAG/C,QAAO"}