@blazediff/matcher 2.0.7 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +20 -38
- package/dist/index.d.ts +10 -41
- package/dist/index.js +3 -3
- package/dist/index.mjs +3 -3
- package/dist/worker.js +1 -1
- package/dist/worker.mjs +1 -1
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -124,46 +124,22 @@ Main function for snapshot comparison.
|
|
|
124
124
|
<td>Force update snapshots (like running with -u flag)</td>
|
|
125
125
|
</tr>
|
|
126
126
|
<tr>
|
|
127
|
-
<td><code>
|
|
128
|
-
<td>
|
|
129
|
-
<td
|
|
130
|
-
<td>
|
|
131
|
-
</tr>
|
|
132
|
-
<tr>
|
|
133
|
-
<td><code>antialiasing</code></td>
|
|
134
|
-
<td>boolean</td>
|
|
135
|
-
<td>false</td>
|
|
136
|
-
<td>Enable anti-aliasing detection (core-native method)</td>
|
|
137
|
-
</tr>
|
|
138
|
-
<tr>
|
|
139
|
-
<td><code>includeAA</code></td>
|
|
140
|
-
<td>boolean</td>
|
|
141
|
-
<td>false</td>
|
|
142
|
-
<td>Include anti-aliased pixels in diff count (core method)</td>
|
|
143
|
-
</tr>
|
|
144
|
-
<tr>
|
|
145
|
-
<td><code>windowSize</code></td>
|
|
146
|
-
<td>number</td>
|
|
147
|
-
<td>11</td>
|
|
148
|
-
<td>Window size for SSIM variants</td>
|
|
149
|
-
</tr>
|
|
150
|
-
<tr>
|
|
151
|
-
<td><code>k1</code></td>
|
|
152
|
-
<td>number</td>
|
|
153
|
-
<td>0.01</td>
|
|
154
|
-
<td>k1 constant for SSIM</td>
|
|
127
|
+
<td><code>diff</code></td>
|
|
128
|
+
<td>Omit<DiffOptions, "output"></td>
|
|
129
|
+
<td>-</td>
|
|
130
|
+
<td><code>threshold</code>, <code>antialiasing</code> (default true), <code>metric</code>, <code>mask</code>, <code>window</code> and <code>render</code>. See <code>@blazediff/core</code>. Used by core, core-wasm and core-native.</td>
|
|
155
131
|
</tr>
|
|
156
132
|
<tr>
|
|
157
|
-
<td><code>
|
|
158
|
-
<td>
|
|
159
|
-
<td
|
|
160
|
-
<td>k2
|
|
133
|
+
<td><code>ssim</code></td>
|
|
134
|
+
<td>Omit<SsimOptions, "output"></td>
|
|
135
|
+
<td>-</td>
|
|
136
|
+
<td><code>windowSize</code>, <code>k1</code>, <code>k2</code>, <code>bitDepth</code>, <code>msSsim</code> and <code>hitchhikers</code>. See <code>@blazediff/ssim</code>. Used by ssim, msssim and hitchhikers-ssim.</td>
|
|
161
137
|
</tr>
|
|
162
138
|
<tr>
|
|
163
|
-
<td><code>
|
|
164
|
-
<td>
|
|
165
|
-
<td
|
|
166
|
-
<td>
|
|
139
|
+
<td><code>gmsd</code></td>
|
|
140
|
+
<td>Omit<GmsdOptions, "output"></td>
|
|
141
|
+
<td>-</td>
|
|
142
|
+
<td><code>downsample</code> and <code>c</code>. See <code>@blazediff/gmsd</code>. Used by gmsd.</td>
|
|
167
143
|
</tr>
|
|
168
144
|
<tr>
|
|
169
145
|
<td><code>runInWorker</code></td>
|
|
@@ -233,6 +209,7 @@ Main function for snapshot comparison.
|
|
|
233
209
|
```typescript
|
|
234
210
|
type ImageInput =
|
|
235
211
|
| string // File path
|
|
212
|
+
| Uint8Array // Encoded PNG bytes (a Buffer works)
|
|
236
213
|
| {
|
|
237
214
|
data: Uint8Array | Uint8ClampedArray | Buffer;
|
|
238
215
|
width: number;
|
|
@@ -244,10 +221,14 @@ type ImageInput =
|
|
|
244
221
|
|
|
245
222
|
### `core-native`
|
|
246
223
|
Rust-native comparison via N-API bindings. **Fastest method** with native performance.
|
|
247
|
-
- **Input**: File paths
|
|
224
|
+
- **Input**: File paths, PNG buffers or raw RGBA images
|
|
248
225
|
- **Algorithm**: YIQ color space with block-based optimization
|
|
249
226
|
- **Best for**: Production testing, large images
|
|
250
227
|
|
|
228
|
+
### `core-wasm`
|
|
229
|
+
The `core-native` algorithm compiled to WebAssembly. Same results, no native binaries.
|
|
230
|
+
- **Input**: File paths or buffers
|
|
231
|
+
|
|
251
232
|
### `core`
|
|
252
233
|
Pixel-by-pixel comparison in JavaScript. Pure JS implementation with no native dependencies.
|
|
253
234
|
- **Input**: File paths or buffers
|
|
@@ -317,6 +298,7 @@ const result = await getOrCreateSnapshot(
|
|
|
317
298
|
method: 'core',
|
|
318
299
|
failureThreshold: 0.1,
|
|
319
300
|
failureThresholdType: 'percent', // Allow 0.1% difference
|
|
301
|
+
diff: { threshold: 0.05 }, // Stricter per-pixel color threshold
|
|
320
302
|
},
|
|
321
303
|
{ testPath: __filename, testName: 'test name' }
|
|
322
304
|
);
|
|
@@ -325,7 +307,7 @@ const result = await getOrCreateSnapshot(
|
|
|
325
307
|
### Different Comparison Methods
|
|
326
308
|
|
|
327
309
|
```typescript
|
|
328
|
-
// Fastest - Rust native (
|
|
310
|
+
// Fastest - Rust native (paths, PNG buffers or raw images)
|
|
329
311
|
await getOrCreateSnapshot(imagePath, { method: 'core-native' }, context);
|
|
330
312
|
|
|
331
313
|
// Pure JavaScript
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
import { Buffer as Buffer$1 } from 'node:buffer';
|
|
2
|
+
import { DiffOptions } from '@blazediff/core';
|
|
3
|
+
import { GmsdOptions } from '@blazediff/gmsd';
|
|
4
|
+
import { SsimOptions } from '@blazediff/ssim';
|
|
2
5
|
|
|
3
6
|
/**
|
|
4
7
|
* Comparison methods available in blazediff
|
|
@@ -57,42 +60,12 @@ interface MatcherOptions {
|
|
|
57
60
|
* @default '--update'
|
|
58
61
|
*/
|
|
59
62
|
updateCommand?: string;
|
|
60
|
-
/**
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
/**
|
|
67
|
-
* Enable anti-aliasing detection (core-native method)
|
|
68
|
-
* @default false
|
|
69
|
-
*/
|
|
70
|
-
antialiasing?: boolean;
|
|
71
|
-
/**
|
|
72
|
-
* Include anti-aliased pixels in diff count (core method)
|
|
73
|
-
* @default false
|
|
74
|
-
*/
|
|
75
|
-
includeAA?: boolean;
|
|
76
|
-
/**
|
|
77
|
-
* Window size for SSIM variants
|
|
78
|
-
* @default 11
|
|
79
|
-
*/
|
|
80
|
-
windowSize?: number;
|
|
81
|
-
/**
|
|
82
|
-
* k1 constant for SSIM
|
|
83
|
-
* @default 0.01
|
|
84
|
-
*/
|
|
85
|
-
k1?: number;
|
|
86
|
-
/**
|
|
87
|
-
* k2 constant for SSIM
|
|
88
|
-
* @default 0.03
|
|
89
|
-
*/
|
|
90
|
-
k2?: number;
|
|
91
|
-
/**
|
|
92
|
-
* Downsample factor for GMSD (0 or 1)
|
|
93
|
-
* @default 0
|
|
94
|
-
*/
|
|
95
|
-
downsample?: 0 | 1;
|
|
63
|
+
/** Options for the core, core-wasm and core-native methods. The matcher manages `output`. */
|
|
64
|
+
diff?: Omit<DiffOptions, "output">;
|
|
65
|
+
/** Options for the ssim, msssim and hitchhikers-ssim methods. The matcher manages `output`. */
|
|
66
|
+
ssim?: Omit<SsimOptions, "output">;
|
|
67
|
+
/** Options for the gmsd method. The matcher manages `output`. */
|
|
68
|
+
gmsd?: Omit<GmsdOptions, "output">;
|
|
96
69
|
/**
|
|
97
70
|
* Run image I/O and comparison in a worker thread
|
|
98
71
|
* Worker threads provide better performance by avoiding V8 heap contention
|
|
@@ -151,10 +124,6 @@ interface RunComparisonResult {
|
|
|
151
124
|
/** Diff visualization output buffer */
|
|
152
125
|
diffOutput?: Uint8Array;
|
|
153
126
|
}
|
|
154
|
-
/**
|
|
155
|
-
* Validate that the comparison method supports the given input type
|
|
156
|
-
*/
|
|
157
|
-
declare function validateMethodSupportsInput(method: ComparisonMethod, input: ImageInput): void;
|
|
158
127
|
/**
|
|
159
128
|
* Run comparison using the specified method
|
|
160
129
|
*/
|
|
@@ -231,4 +200,4 @@ declare function compareImages(received: ImageInput, baseline: ImageInput, optio
|
|
|
231
200
|
|
|
232
201
|
declare function terminateWorker(): Promise<number>;
|
|
233
202
|
|
|
234
|
-
export { type ComparisonMethod, type ComparisonResult, type FormatOptions, type ImageData, type ImageInput, type MatcherOptions, type TestContext, compareImages, fileExists, formatMessage as formatReport, getOrCreateSnapshot, isFilePath, isImageBuffer, isImageData, isRawPngBuffer, loadPNG, normalizeImageInput, runComparison, savePNG, saveRawPNGBuffer, terminateWorker
|
|
203
|
+
export { type ComparisonMethod, type ComparisonResult, type FormatOptions, type ImageData, type ImageInput, type MatcherOptions, type TestContext, compareImages, fileExists, formatMessage as formatReport, getOrCreateSnapshot, isFilePath, isImageBuffer, isImageData, isRawPngBuffer, loadPNG, normalizeImageInput, runComparison, savePNG, saveRawPNGBuffer, terminateWorker };
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
'use strict';var fs=require('fs'),path=require('path'),codecPngjs=require('@blazediff/codec-pngjs'),core=require('@blazediff/core'),coreNative=require('@blazediff/core-native'),promises=require('fs/promises'),module$1=require('module'),gmsd=require('@blazediff/gmsd'),ssim=require('@blazediff/ssim'),hitchhikersSsim=require('@blazediff/ssim/hitchhikers-ssim'),msssim=require('@blazediff/ssim/msssim'),
|
|
2
|
-
`)}function
|
|
3
|
-
`)}
|
|
1
|
+
'use strict';var fs=require('fs'),path=require('path'),codecPngjs=require('@blazediff/codec-pngjs'),core=require('@blazediff/core'),coreNative=require('@blazediff/core-native'),promises=require('fs/promises'),module$1=require('module'),gmsd=require('@blazediff/gmsd'),ssim=require('@blazediff/ssim'),hitchhikersSsim=require('@blazediff/ssim/hitchhikers-ssim'),msssim=require('@blazediff/ssim/msssim'),i=require('picocolors'),worker_threads=require('worker_threads');function _interopDefault(e){return e&&e.__esModule?e:{default:e}}var i__default=/*#__PURE__*/_interopDefault(i);var De=Object.defineProperty;var y=(e,t)=>()=>(e&&(t=e(e=0)),t);var de=(e,t)=>{for(var r in t)De(e,r,{get:t[r],enumerable:true});};var be,I,m=y(()=>{be=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href,I=be();});var he={};de(he,{ensureDir:()=>ae,fileExists:()=>F,isFilePath:()=>D,isImageBuffer:()=>ce,isImageData:()=>w,isRawPngBuffer:()=>A,loadPNG:()=>z,normalizeImageInput:()=>b,savePNG:()=>P,saveRawPNGBuffer:()=>S});function D(e){return typeof e=="string"}function A(e){return (Buffer.isBuffer(e)||e instanceof Uint8Array)&&!("width"in e)}function ce(e){return typeof e=="object"&&e!==null&&"data"in e&&"width"in e&&"height"in e}function w(e){return typeof e=="object"&&e!==null&&"data"in e&&"width"in e&&"height"in e}async function z(e){if(!fs.existsSync(e))throw new Error(`Image file not found: ${e}`);let t=await codecPngjs.codecPngjs.read(e);return {data:new Uint8Array(t.data),width:t.width,height:t.height}}async function P(e,t,r,a){let n=path.dirname(e);fs.existsSync(n)||fs.mkdirSync(n,{recursive:true}),await codecPngjs.codecPngjs.write({data:t instanceof Uint8Array?t:new Uint8Array(t),width:r,height:a},e);}function S(e,t){let r=path.dirname(e);fs.existsSync(r)||fs.mkdirSync(r,{recursive:true}),fs.writeFileSync(e,t);}async function b(e){if(D(e))return z(e);if(A(e)){let t=Buffer.isBuffer(e)?e:Buffer.from(e),r=await codecPngjs.codecPngjs.read(t);return {data:new Uint8Array(r.data),width:r.width,height:r.height}}return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}function F(e){return fs.existsSync(e)}function ae(e){fs.existsSync(e)||fs.mkdirSync(e,{recursive:true});}var B=y(()=>{m();});function k(e,t){if(e.kind==="dimension-mismatch"){let{a:r,b:a}=e;return {diffCount:Math.max(r.width*r.height,a.width*a.height),diffPercentage:100}}return {diffCount:e.count,diffPercentage:e.percentage,diffOutput:t?.data}}function R({width:e,height:t}){return {data:new Uint8Array(e*t*4),width:e,height:t}}var O=y(()=>{m();});function j(e,t,r,a){let n=r?R(e):void 0;return k(core.diff(e,t,{...a.diff,output:n}),n)}var oe=y(()=>{m();O();});async function _(e,t,r,a){let n=await coreNative.diff(e,t,{...a.diff,output:r});if(n.kind==="io-error")throw new Error(n.path?`Image file not found or unreadable: ${n.path}. ${n.message}`:n.message);return k(n)}var ne=y(()=>{m();O();});function Ue(){return ie||(ie=import('@blazediff/core-wasm').then(async e=>{let t=Te.resolve("@blazediff/core-wasm/wasm/blazediff_bg.wasm");return await e.initBlazediff(await promises.readFile(t)),e})),ie}async function q(e,t,r,a){let n=r?R(e):void 0,{diff:o}=await Ue();return k(await o(e,t,{...a.diff,output:n}),n)}var Te,ie,se=y(()=>{m();O();Te=module$1.createRequire(I);});function L(e,t,r,a){let n=r?R(e):void 0,o=gmsd.gmsd(e,t,{...a.gmsd,output:n});return o.kind==="dimension-mismatch"?{score:1}:{score:o.score,diffOutput:n?.data}}var me=y(()=>{m();O();});function H(e,t,r,a,n){let o=a?R(e):void 0,s=le[r](e,t,{...n.ssim,output:o});return s.kind==="dimension-mismatch"?{score:0}:{score:s.score,diffOutput:o?.data}}function $(e){return e in le}var le,Z=y(()=>{m();O();le={ssim:ssim.ssim,msssim:msssim.msssim,"hitchhikers-ssim":hitchhikersSsim.hitchhikersSSIM};});var ge={};de(ge,{compareCore:()=>j,compareCoreNative:()=>_,compareCoreWasm:()=>q,compareGmsd:()=>L,compareSsim:()=>H,isSsimMethod:()=>$,runComparison:()=>T});async function T(e,t,r,a,n){if(r==="core-native"){let u=await _(e,t,n,a);return {diffCount:u.diffCount,diffPercentage:u.diffPercentage}}let o=w(e)?e:await b(e),s=w(t)?t:await b(t),f=n!==void 0;if($(r)){let u=H(o,s,r,f,a);return {score:u.score,diffOutput:u.diffOutput}}if(r==="gmsd"){let u=L(o,s,f,a);return {score:u.score,diffOutput:u.diffOutput}}if(r==="core-wasm"){let u=await q(o,s,f,a);return {diffCount:u.diffCount,diffPercentage:u.diffPercentage,diffOutput:u.diffOutput}}let x=j(o,s,f,a);return {diffCount:x.diffCount,diffPercentage:x.diffPercentage,diffOutput:x.diffOutput}}var J=y(()=>{m();B();oe();ne();se();me();Z();oe();ne();se();me();Z();});m();J();B();m();var E={success:i__default.default.isColorSupported?"\u2714":"\u221A",error:i__default.default.isColorSupported?"\u2716":"\xD7",info:i__default.default.isColorSupported?"\u2139":"i",arrow:i__default.default.isColorSupported?"\u2514\u2500":"'-"},ze=new Set(["ssim","msssim","hitchhikers-ssim"]);function U(e){return e.snapshotCreated?Be(e.baselinePath):e.pass?Ee():Ge(e)}function Be(e){return [`${i__default.default.green(E.success)} ${i__default.default.green("New snapshot created")}`,` ${i__default.default.dim(E.arrow)} ${i__default.default.dim(e)}`].join(`
|
|
2
|
+
`)}function Ee(){return `${i__default.default.green(E.success)} ${i__default.default.green("Image matches snapshot")}`}function Ge(e){let{method:t,baselinePath:r,receivedPath:a,diffPath:n,diffCount:o=0,diffPercentage:s,score:f=0,threshold:x,thresholdType:u,updateCommand:W}=e,p=[`${i__default.default.red(E.error)} ${i__default.default.red(i__default.default.bold("Image snapshot mismatch"))}`,""],l=12;if(p.push(` ${i__default.default.dim("Method".padEnd(l))}${t}`),p.push(` ${i__default.default.dim("Baseline".padEnd(l))}${i__default.default.dim(r)}`),p.push(` ${i__default.default.dim("Received".padEnd(l))}${i__default.default.dim(a)}`),p.push(` ${i__default.default.dim("Diff".padEnd(l))}${i__default.default.dim(n)}`),p.push(""),ze.has(t)){let N=((1-f)*100).toFixed(2);p.push(` ${i__default.default.dim("SSIM Score".padEnd(l))}${i__default.default.yellow(f.toFixed(4))} ${i__default.default.dim("(1.0 = identical)")}`),p.push(` ${i__default.default.dim("Difference".padEnd(l))}${i__default.default.yellow(`${N}%`)}`);}else if(t==="gmsd")p.push(` ${i__default.default.dim("GMSD Score".padEnd(l))}${i__default.default.yellow(f.toFixed(4))} ${i__default.default.dim("(0.0 = identical)")}`);else {let N=s?.toFixed(2)??"0.00";p.push(` ${i__default.default.dim("Difference".padEnd(l))}${i__default.default.yellow(o.toLocaleString())} pixels ${i__default.default.dim(`(${N}%)`)}`);}let X=u==="percent"?"%":"pixels";return p.push(` ${i__default.default.dim("Threshold".padEnd(l))}${x} ${X}`),p.push(""),p.push(` ${i__default.default.cyan(E.info)} ${i__default.default.cyan(`Run with ${W??"--update"} to update the snapshot`)}`),p.join(`
|
|
3
|
+
`)}m();J();Z();B();m();var C=null,je=0,Pe=false,M=new Map;function _e(){let e=path.join(__dirname,"worker.js");if(fs.existsSync(e))return e;let t=path.join(__dirname,"../dist/worker.js");return fs.existsSync(t)?t:null}function qe(){if(Pe)return null;if(!C){let e=_e();if(!e)return Pe=true,null;C=new worker_threads.Worker(e),C.on("message",t=>{let r=M.get(t.id);r&&(M.delete(t.id),t.success?r.resolve(t.result):r.reject(new Error(t.error)));}),C.on("error",t=>{for(let r of M.values())r.reject(t);M.clear(),C=null;}),C.on("exit",t=>{if(t!==0){let r=new Error(`Worker exited with code ${t}`);for(let a of M.values())a.reject(r);M.clear();}C=null;});}return C}async function fe(e,t){let r=qe();if(!r){let{runComparison:a}=await Promise.resolve().then(()=>(J(),ge)),{loadPNG:n,normalizeImageInput:o}=await Promise.resolve().then(()=>(B(),he));switch(e){case "normalize":return o(t.input);case "loadPNG":return n(t.filePath);case "compare":{let s=t;return a(s.received,s.baseline,s.method,s.options,s.diffOutputPath)}default:throw new Error(`Unknown request type: ${e}`)}}return new Promise((a,n)=>{let o=++je;M.set(o,{resolve:a,reject:n});let s={id:o,type:e,payload:t};r.postMessage(s);})}function ue(e){return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}async function Ce(e){let t=await fe("normalize",{input:e});return ue(t)}async function ye(e){let t=await fe("loadPNG",{filePath:e});return ue(t)}function xe(e,t,r,a,n){let o=ue(e);return fe("compare",{received:o,baseline:t,method:r,options:a,diffOutputPath:n})}function Le(){if(C){let e=C;return C=null,e.terminate()}return Promise.resolve(0)}function Je(e){return e.testName.replace(/[^a-zA-Z0-9-_\s]/g,"").replace(/\s+/g,"-").toLowerCase()||"snapshot"}function Ke(e,t){let r=path.dirname(e.testPath),a=t.snapshotsDir??"__snapshots__",n=path.isAbsolute(a)?a:path.join(r,a),o=t.snapshotIdentifier??Je(e);return {snapshotDir:n,baselinePath:path.join(n,`${o}.png`),receivedPath:path.join(n,`${o}.received.png`),diffPath:path.join(n,`${o}.diff.png`)}}function Re(e,t,r){let a=r.failureThreshold??0,n=r.failureThresholdType??"pixel";if($(e)){let o=t.score??0;return n==="percent"?(1-o)*100<=a:o>=1-a/100}if(e==="gmsd"){let o=t.score??0;return n==="percent"?o*100<=a:o<=a/100}return n==="percent"?(t.diffPercentage??0)<=a:(t.diffCount??0)<=a}async function Qe(e,t,r){let a=Ke(r,t),{snapshotDir:n,baselinePath:o,receivedPath:s,diffPath:f}=a,x=A(e),u=x?e:null,W=t.runInWorker!==false,p=W?Ce:b,l=W?ye:z,X=W?(g,h,ee)=>xe(g,h,t.method,t,ee):(g,h,ee)=>T(g,h,t.method,t,ee);ae(n);let N=F(o),G=t.updateSnapshots===true?"all":t.updateSnapshots===false||t.updateSnapshots===void 0?"new":t.updateSnapshots;if(!N&&G!=="none"&&(G==="new"||G==="all")){if(u)S(o,u);else if(D(e)){let h=await l(e);await P(o,h.data,h.width,h.height);}else if(w(e))await P(o,e.data,e.width,e.height);else {let h=await p(e);await P(o,h.data,h.width,h.height);}return {pass:true,message:U({pass:true,method:t.method,snapshotCreated:true,baselinePath:o,receivedPath:s,diffPath:f,diffCount:0,diffPercentage:0,score:0,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),baselinePath:o,snapshotStatus:"added"}}let c,d;t.method==="core-native"?(c=await T(e,o,t.method,t,f),d=e):(d=x?await p(e):e,c=await X(w(d)?d:await p(d),o,f));let Y=Re(t.method,c,t);if(Y)return fs.existsSync(s)&&fs.unlinkSync(s),fs.existsSync(f)&&fs.unlinkSync(f),{pass:true,message:U({pass:Y,method:t.method,snapshotCreated:false,baselinePath:o,receivedPath:s,diffPath:f,diffCount:c.diffCount,diffPercentage:c.diffPercentage,score:c.score,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),diffCount:c.diffCount,diffPercentage:c.diffPercentage,score:c.score,baselinePath:o,snapshotStatus:"matched"};if(G==="all"){if(u)S(o,u);else if(D(d)){let h=await l(d);await P(o,h.data,h.width,h.height);}else w(d)&&await P(o,d.data,d.width,d.height);return fs.existsSync(s)&&fs.unlinkSync(s),fs.existsSync(f)&&fs.unlinkSync(f),{pass:true,message:U({pass:true,method:t.method,snapshotCreated:true,baselinePath:o,receivedPath:s,diffPath:f,diffCount:0,diffPercentage:0,score:0,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),baselinePath:o,snapshotStatus:"updated"}}if(u)S(s,u);else if(D(d)){let g=await l(d);await P(s,g.data,g.width,g.height);}else w(d)&&await P(s,d.data,d.width,d.height);if(c.diffOutput){let g=w(d)?d:await p(d);await P(f,c.diffOutput,g.width,g.height);}return {pass:false,message:U({pass:Y,method:t.method,snapshotCreated:false,baselinePath:o,receivedPath:s,diffPath:f,diffCount:c.diffCount,diffPercentage:c.diffPercentage,score:c.score,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),diffCount:c.diffCount,diffPercentage:c.diffPercentage,score:c.score,baselinePath:o,receivedPath:s,diffPath:f,snapshotStatus:"failed"}}async function Ve(e,t,r){let a=await T(e,t,r.method,r),n=Re(r.method,a,r);return {pass:n,message:n?"Images match.":`Images differ: ${a.diffCount??a.score} ${a.diffCount!==void 0?"pixels":"score"}`,diffCount:a.diffCount,diffPercentage:a.diffPercentage,score:a.score}}exports.compareImages=Ve;exports.fileExists=F;exports.formatReport=U;exports.getOrCreateSnapshot=Qe;exports.isFilePath=D;exports.isImageBuffer=ce;exports.isImageData=w;exports.isRawPngBuffer=A;exports.loadPNG=z;exports.normalizeImageInput=b;exports.runComparison=T;exports.savePNG=P;exports.saveRawPNGBuffer=S;exports.terminateWorker=Le;
|
package/dist/index.mjs
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import Me,{dirname,isAbsolute,join}from'path';import {fileURLToPath}from'url';import {existsSync,mkdirSync,writeFileSync,unlinkSync}from'fs';import {codecPngjs}from'@blazediff/codec-pngjs';import {diff}from'@blazediff/core';import {
|
|
2
|
-
`)}function
|
|
3
|
-
`)}
|
|
1
|
+
import Me,{dirname,isAbsolute,join}from'path';import {fileURLToPath}from'url';import {existsSync,mkdirSync,writeFileSync,unlinkSync}from'fs';import {codecPngjs}from'@blazediff/codec-pngjs';import {diff as diff$1}from'@blazediff/core';import {diff}from'@blazediff/core-native';import {readFile}from'fs/promises';import {createRequire}from'module';import {gmsd}from'@blazediff/gmsd';import {ssim}from'@blazediff/ssim';import {hitchhikersSSIM}from'@blazediff/ssim/hitchhikers-ssim';import {msssim}from'@blazediff/ssim/msssim';import i from'picocolors';import {Worker}from'worker_threads';var be=Object.defineProperty;var x=(e,t)=>()=>(e&&(t=e(e=0)),t);var pe=(e,t)=>{for(var r in t)be(e,r,{get:t[r],enumerable:true});};var Se,Oe,l,m=x(()=>{Se=()=>fileURLToPath(import.meta.url),Oe=()=>Me.dirname(Se()),l=Oe();});var le={};pe(le,{ensureDir:()=>oe,fileExists:()=>j,isFilePath:()=>b,isImageBuffer:()=>he,isImageData:()=>P,isRawPngBuffer:()=>z,loadPNG:()=>B,normalizeImageInput:()=>M,savePNG:()=>C,saveRawPNGBuffer:()=>S});function b(e){return typeof e=="string"}function z(e){return (Buffer.isBuffer(e)||e instanceof Uint8Array)&&!("width"in e)}function he(e){return typeof e=="object"&&e!==null&&"data"in e&&"width"in e&&"height"in e}function P(e){return typeof e=="object"&&e!==null&&"data"in e&&"width"in e&&"height"in e}async function B(e){if(!existsSync(e))throw new Error(`Image file not found: ${e}`);let t=await codecPngjs.read(e);return {data:new Uint8Array(t.data),width:t.width,height:t.height}}async function C(e,t,r,a){let n=dirname(e);existsSync(n)||mkdirSync(n,{recursive:true}),await codecPngjs.write({data:t instanceof Uint8Array?t:new Uint8Array(t),width:r,height:a},e);}function S(e,t){let r=dirname(e);existsSync(r)||mkdirSync(r,{recursive:true}),writeFileSync(e,t);}async function M(e){if(b(e))return B(e);if(z(e)){let t=Buffer.isBuffer(e)?e:Buffer.from(e),r=await codecPngjs.read(t);return {data:new Uint8Array(r.data),width:r.width,height:r.height}}return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}function j(e){return existsSync(e)}function oe(e){existsSync(e)||mkdirSync(e,{recursive:true});}var E=x(()=>{m();});function O(e,t){if(e.kind==="dimension-mismatch"){let{a:r,b:a}=e;return {diffCount:Math.max(r.width*r.height,a.width*a.height),diffPercentage:100}}return {diffCount:e.count,diffPercentage:e.percentage,diffOutput:t?.data}}function R({width:e,height:t}){return {data:new Uint8Array(e*t*4),width:e,height:t}}var $=x(()=>{m();});function _(e,t,r,a){let n=r?R(e):void 0;return O(diff$1(e,t,{...a.diff,output:n}),n)}var ne=x(()=>{m();$();});async function q(e,t,r,a){let n=await diff(e,t,{...a.diff,output:r});if(n.kind==="io-error")throw new Error(n.path?`Image file not found or unreadable: ${n.path}. ${n.message}`:n.message);return O(n)}var ie=x(()=>{m();$();});function Ae(){return se||(se=import('@blazediff/core-wasm').then(async e=>{let t=ve.resolve("@blazediff/core-wasm/wasm/blazediff_bg.wasm");return await e.initBlazediff(await readFile(t)),e})),se}async function L(e,t,r,a){let n=r?R(e):void 0,{diff:o}=await Ae();return O(await o(e,t,{...a.diff,output:n}),n)}var ve,se,me=x(()=>{m();$();ve=createRequire(import.meta.url);});function H(e,t,r,a){let n=r?R(e):void 0,o=gmsd(e,t,{...a.gmsd,output:n});return o.kind==="dimension-mismatch"?{score:1}:{score:o.score,diffOutput:n?.data}}var fe=x(()=>{m();$();});function Z(e,t,r,a,n){let o=a?R(e):void 0,s=ge[r](e,t,{...n.ssim,output:o});return s.kind==="dimension-mismatch"?{score:0}:{score:s.score,diffOutput:o?.data}}function T(e){return e in ge}var ge,J=x(()=>{m();$();ge={ssim:ssim,msssim:msssim,"hitchhikers-ssim":hitchhikersSSIM};});var Ie={};pe(Ie,{compareCore:()=>_,compareCoreNative:()=>q,compareCoreWasm:()=>L,compareGmsd:()=>H,compareSsim:()=>Z,isSsimMethod:()=>T,runComparison:()=>W});async function W(e,t,r,a,n){if(r==="core-native"){let u=await q(e,t,n,a);return {diffCount:u.diffCount,diffPercentage:u.diffPercentage}}let o=P(e)?e:await M(e),s=P(t)?t:await M(t),f=n!==void 0;if(T(r)){let u=Z(o,s,r,f,a);return {score:u.score,diffOutput:u.diffOutput}}if(r==="gmsd"){let u=H(o,s,f,a);return {score:u.score,diffOutput:u.diffOutput}}if(r==="core-wasm"){let u=await L(o,s,f,a);return {diffCount:u.diffCount,diffPercentage:u.diffPercentage,diffOutput:u.diffOutput}}let D=_(o,s,f,a);return {diffCount:D.diffCount,diffPercentage:D.diffPercentage,diffOutput:D.diffOutput}}var K=x(()=>{m();E();ne();ie();me();fe();J();ne();ie();me();fe();J();});m();K();E();m();var G={success:i.isColorSupported?"\u2714":"\u221A",error:i.isColorSupported?"\u2716":"\xD7",info:i.isColorSupported?"\u2139":"i",arrow:i.isColorSupported?"\u2514\u2500":"'-"},Fe=new Set(["ssim","msssim","hitchhikers-ssim"]);function N(e){return e.snapshotCreated?je(e.baselinePath):e.pass?_e():qe(e)}function je(e){return [`${i.green(G.success)} ${i.green("New snapshot created")}`,` ${i.dim(G.arrow)} ${i.dim(e)}`].join(`
|
|
2
|
+
`)}function _e(){return `${i.green(G.success)} ${i.green("Image matches snapshot")}`}function qe(e){let{method:t,baselinePath:r,receivedPath:a,diffPath:n,diffCount:o=0,diffPercentage:s,score:f=0,threshold:D,thresholdType:u,updateCommand:U}=e,p=[`${i.red(G.error)} ${i.red(i.bold("Image snapshot mismatch"))}`,""],g=12;if(p.push(` ${i.dim("Method".padEnd(g))}${t}`),p.push(` ${i.dim("Baseline".padEnd(g))}${i.dim(r)}`),p.push(` ${i.dim("Received".padEnd(g))}${i.dim(a)}`),p.push(` ${i.dim("Diff".padEnd(g))}${i.dim(n)}`),p.push(""),Fe.has(t)){let v=((1-f)*100).toFixed(2);p.push(` ${i.dim("SSIM Score".padEnd(g))}${i.yellow(f.toFixed(4))} ${i.dim("(1.0 = identical)")}`),p.push(` ${i.dim("Difference".padEnd(g))}${i.yellow(`${v}%`)}`);}else if(t==="gmsd")p.push(` ${i.dim("GMSD Score".padEnd(g))}${i.yellow(f.toFixed(4))} ${i.dim("(0.0 = identical)")}`);else {let v=s?.toFixed(2)??"0.00";p.push(` ${i.dim("Difference".padEnd(g))}${i.yellow(o.toLocaleString())} pixels ${i.dim(`(${v}%)`)}`);}let Y=u==="percent"?"%":"pixels";return p.push(` ${i.dim("Threshold".padEnd(g))}${D} ${Y}`),p.push(""),p.push(` ${i.cyan(G.info)} ${i.cyan(`Run with ${U??"--update"} to update the snapshot`)}`),p.join(`
|
|
3
|
+
`)}m();K();J();E();m();var y=null,He=0,Ce=false,k=new Map;function Ze(){let e=join(l,"worker.js");if(existsSync(e))return e;let t=join(l,"../dist/worker.js");return existsSync(t)?t:null}function Je(){if(Ce)return null;if(!y){let e=Ze();if(!e)return Ce=true,null;y=new Worker(e),y.on("message",t=>{let r=k.get(t.id);r&&(k.delete(t.id),t.success?r.resolve(t.result):r.reject(new Error(t.error)));}),y.on("error",t=>{for(let r of k.values())r.reject(t);k.clear(),y=null;}),y.on("exit",t=>{if(t!==0){let r=new Error(`Worker exited with code ${t}`);for(let a of k.values())a.reject(r);k.clear();}y=null;});}return y}async function ue(e,t){let r=Je();if(!r){let{runComparison:a}=await Promise.resolve().then(()=>(K(),Ie)),{loadPNG:n,normalizeImageInput:o}=await Promise.resolve().then(()=>(E(),le));switch(e){case "normalize":return o(t.input);case "loadPNG":return n(t.filePath);case "compare":{let s=t;return a(s.received,s.baseline,s.method,s.options,s.diffOutputPath)}default:throw new Error(`Unknown request type: ${e}`)}}return new Promise((a,n)=>{let o=++He;k.set(o,{resolve:a,reject:n});let s={id:o,type:e,payload:t};r.postMessage(s);})}function de(e){return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}async function ye(e){let t=await ue("normalize",{input:e});return de(t)}async function xe(e){let t=await ue("loadPNG",{filePath:e});return de(t)}function De(e,t,r,a,n){let o=de(e);return ue("compare",{received:o,baseline:t,method:r,options:a,diffOutputPath:n})}function Ke(){if(y){let e=y;return y=null,e.terminate()}return Promise.resolve(0)}function Xe(e){return e.testName.replace(/[^a-zA-Z0-9-_\s]/g,"").replace(/\s+/g,"-").toLowerCase()||"snapshot"}function Ye(e,t){let r=dirname(e.testPath),a=t.snapshotsDir??"__snapshots__",n=isAbsolute(a)?a:join(r,a),o=t.snapshotIdentifier??Xe(e);return {snapshotDir:n,baselinePath:join(n,`${o}.png`),receivedPath:join(n,`${o}.received.png`),diffPath:join(n,`${o}.diff.png`)}}function Re(e,t,r){let a=r.failureThreshold??0,n=r.failureThresholdType??"pixel";if(T(e)){let o=t.score??0;return n==="percent"?(1-o)*100<=a:o>=1-a/100}if(e==="gmsd"){let o=t.score??0;return n==="percent"?o*100<=a:o<=a/100}return n==="percent"?(t.diffPercentage??0)<=a:(t.diffCount??0)<=a}async function et(e,t,r){let a=Ye(r,t),{snapshotDir:n,baselinePath:o,receivedPath:s,diffPath:f}=a,D=z(e),u=D?e:null,U=t.runInWorker!==false,p=U?ye:M,g=U?xe:B,Y=U?(I,h,te)=>De(I,h,t.method,t,te):(I,h,te)=>W(I,h,t.method,t,te);oe(n);let v=j(o),F=t.updateSnapshots===true?"all":t.updateSnapshots===false||t.updateSnapshots===void 0?"new":t.updateSnapshots;if(!v&&F!=="none"&&(F==="new"||F==="all")){if(u)S(o,u);else if(b(e)){let h=await g(e);await C(o,h.data,h.width,h.height);}else if(P(e))await C(o,e.data,e.width,e.height);else {let h=await p(e);await C(o,h.data,h.width,h.height);}return {pass:true,message:N({pass:true,method:t.method,snapshotCreated:true,baselinePath:o,receivedPath:s,diffPath:f,diffCount:0,diffPercentage:0,score:0,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),baselinePath:o,snapshotStatus:"added"}}let c,d;t.method==="core-native"?(c=await W(e,o,t.method,t,f),d=e):(d=D?await p(e):e,c=await Y(P(d)?d:await p(d),o,f));let ee=Re(t.method,c,t);if(ee)return existsSync(s)&&unlinkSync(s),existsSync(f)&&unlinkSync(f),{pass:true,message:N({pass:ee,method:t.method,snapshotCreated:false,baselinePath:o,receivedPath:s,diffPath:f,diffCount:c.diffCount,diffPercentage:c.diffPercentage,score:c.score,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),diffCount:c.diffCount,diffPercentage:c.diffPercentage,score:c.score,baselinePath:o,snapshotStatus:"matched"};if(F==="all"){if(u)S(o,u);else if(b(d)){let h=await g(d);await C(o,h.data,h.width,h.height);}else P(d)&&await C(o,d.data,d.width,d.height);return existsSync(s)&&unlinkSync(s),existsSync(f)&&unlinkSync(f),{pass:true,message:N({pass:true,method:t.method,snapshotCreated:true,baselinePath:o,receivedPath:s,diffPath:f,diffCount:0,diffPercentage:0,score:0,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),baselinePath:o,snapshotStatus:"updated"}}if(u)S(s,u);else if(b(d)){let I=await g(d);await C(s,I.data,I.width,I.height);}else P(d)&&await C(s,d.data,d.width,d.height);if(c.diffOutput){let I=P(d)?d:await p(d);await C(f,c.diffOutput,I.width,I.height);}return {pass:false,message:N({pass:ee,method:t.method,snapshotCreated:false,baselinePath:o,receivedPath:s,diffPath:f,diffCount:c.diffCount,diffPercentage:c.diffPercentage,score:c.score,threshold:t.failureThreshold??0,thresholdType:t.failureThresholdType??"pixel",updateCommand:t.updateCommand}),diffCount:c.diffCount,diffPercentage:c.diffPercentage,score:c.score,baselinePath:o,receivedPath:s,diffPath:f,snapshotStatus:"failed"}}async function tt(e,t,r){let a=await W(e,t,r.method,r),n=Re(r.method,a,r);return {pass:n,message:n?"Images match.":`Images differ: ${a.diffCount??a.score} ${a.diffCount!==void 0?"pixels":"score"}`,diffCount:a.diffCount,diffPercentage:a.diffPercentage,score:a.score}}export{tt as compareImages,j as fileExists,N as formatReport,et as getOrCreateSnapshot,b as isFilePath,he as isImageBuffer,P as isImageData,z as isRawPngBuffer,B as loadPNG,M as normalizeImageInput,W as runComparison,C as savePNG,S as saveRawPNGBuffer,Ke as terminateWorker};
|
package/dist/worker.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var worker_threads=require('worker_threads'),fs=require('fs');require('path');var codecPngjs=require('@blazediff/codec-pngjs'),core=require('@blazediff/core'),coreNative=require('@blazediff/core-native'),promises=require('fs/promises'),module$1=require('module'),gmsd=require('@blazediff/gmsd'),ssim=require('@blazediff/ssim'),hitchhikersSsim=require('@blazediff/ssim/hitchhikers-ssim'),msssim=require('@blazediff/ssim/msssim');var
|
|
1
|
+
'use strict';var worker_threads=require('worker_threads'),fs=require('fs');require('path');var codecPngjs=require('@blazediff/codec-pngjs'),core=require('@blazediff/core'),coreNative=require('@blazediff/core-native'),promises=require('fs/promises'),module$1=require('module'),gmsd=require('@blazediff/gmsd'),ssim=require('@blazediff/ssim'),hitchhikersSsim=require('@blazediff/ssim/hitchhikers-ssim'),msssim=require('@blazediff/ssim/msssim');var O=()=>typeof document>"u"?new URL(`file:${__filename}`).href:document.currentScript&&document.currentScript.src||new URL("main.js",document.baseURI).href,s=O();function U(e){return typeof e=="string"}function S(e){return (Buffer.isBuffer(e)||e instanceof Uint8Array)&&!("width"in e)}function l(e){return typeof e=="object"&&e!==null&&"data"in e&&"width"in e&&"height"in e}async function h(e){if(!fs.existsSync(e))throw new Error(`Image file not found: ${e}`);let t=await codecPngjs.codecPngjs.read(e);return {data:new Uint8Array(t.data),width:t.width,height:t.height}}async function p(e){if(U(e))return h(e);if(S(e)){let t=Buffer.isBuffer(e)?e:Buffer.from(e),r=await codecPngjs.codecPngjs.read(t);return {data:new Uint8Array(r.data),width:r.width,height:r.height}}return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}function u(e,t){if(e.kind==="dimension-mismatch"){let{a:r,b:a}=e;return {diffCount:Math.max(r.width*r.height,a.width*a.height),diffPercentage:100}}return {diffCount:e.count,diffPercentage:e.percentage,diffOutput:t?.data}}function m({width:e,height:t}){return {data:new Uint8Array(e*t*4),width:e,height:t}}function y(e,t,r,a){let o=r?m(e):void 0;return u(core.diff(e,t,{...a.diff,output:o}),o)}async function I(e,t,r,a){let o=await coreNative.diff(e,t,{...a.diff,output:r});if(o.kind==="io-error")throw new Error(o.path?`Image file not found or unreadable: ${o.path}. ${o.message}`:o.message);return u(o)}var B=module$1.createRequire(s),w;function W(){return w||(w=import('@blazediff/core-wasm').then(async e=>{let t=B.resolve("@blazediff/core-wasm/wasm/blazediff_bg.wasm");return await e.initBlazediff(await promises.readFile(t)),e})),w}async function P(e,t,r,a){let o=r?m(e):void 0,{diff:i}=await W();return u(await i(e,t,{...a.diff,output:o}),o)}function C(e,t,r,a){let o=r?m(e):void 0,i=gmsd.gmsd(e,t,{...a.gmsd,output:o});return i.kind==="dimension-mismatch"?{score:1}:{score:i.score,diffOutput:o?.data}}var b={ssim:ssim.ssim,msssim:msssim.msssim,"hitchhikers-ssim":hitchhikersSsim.hitchhikersSSIM};function x(e,t,r,a,o){let i=a?m(e):void 0,f=b[r](e,t,{...o.ssim,output:i});return f.kind==="dimension-mismatch"?{score:0}:{score:f.score,diffOutput:i?.data}}function R(e){return e in b}async function M(e,t,r,a,o){if(r==="core-native"){let n=await I(e,t,o,a);return {diffCount:n.diffCount,diffPercentage:n.diffPercentage}}let i=l(e)?e:await p(e),f=l(t)?t:await p(t),c=o!==void 0;if(R(r)){let n=x(i,f,r,c,a);return {score:n.score,diffOutput:n.diffOutput}}if(r==="gmsd"){let n=C(i,f,c,a);return {score:n.score,diffOutput:n.diffOutput}}if(r==="core-wasm"){let n=await P(i,f,c,a);return {diffCount:n.diffCount,diffPercentage:n.diffPercentage,diffOutput:n.diffOutput}}let g=y(i,f,c,a);return {diffCount:g.diffCount,diffPercentage:g.diffPercentage,diffOutput:g.diffOutput}}function j(e){return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}async function F(e){switch(e.type){case "normalize":{let{input:t}=e.payload;return p(t)}case "loadPNG":{let{filePath:t}=e.payload;return h(t)}case "compare":{let{received:t,baseline:r,method:a,options:o,diffOutputPath:i}=e.payload;return M(j(t),r,a,o,i)}default:throw new Error(`Unknown request type: ${e.type}`)}}worker_threads.parentPort&&worker_threads.parentPort.on("message",async e=>{try{let t=await F(e);worker_threads.parentPort?.postMessage({id:e.id,success:!0,result:t});}catch(t){worker_threads.parentPort?.postMessage({id:e.id,success:false,error:t instanceof Error?t.message:String(t)});}});
|
package/dist/worker.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import {parentPort}from'worker_threads';import {existsSync}from'fs';import'path';import {codecPngjs}from'@blazediff/codec-pngjs';import {diff}from'@blazediff/core';import {
|
|
1
|
+
import {parentPort}from'worker_threads';import {existsSync}from'fs';import'path';import {codecPngjs}from'@blazediff/codec-pngjs';import {diff as diff$1}from'@blazediff/core';import {diff}from'@blazediff/core-native';import {readFile}from'fs/promises';import {createRequire}from'module';import {gmsd}from'@blazediff/gmsd';import {ssim}from'@blazediff/ssim';import {hitchhikersSSIM}from'@blazediff/ssim/hitchhikers-ssim';import {msssim}from'@blazediff/ssim/msssim';function U(e){return typeof e=="string"}function A(e){return (Buffer.isBuffer(e)||e instanceof Uint8Array)&&!("width"in e)}function h(e){return typeof e=="object"&&e!==null&&"data"in e&&"width"in e&&"height"in e}async function y(e){if(!existsSync(e))throw new Error(`Image file not found: ${e}`);let t=await codecPngjs.read(e);return {data:new Uint8Array(t.data),width:t.width,height:t.height}}async function c(e){if(U(e))return y(e);if(A(e)){let t=Buffer.isBuffer(e)?e:Buffer.from(e),r=await codecPngjs.read(t);return {data:new Uint8Array(r.data),width:r.width,height:r.height}}return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}function p(e,t){if(e.kind==="dimension-mismatch"){let{a:r,b:o}=e;return {diffCount:Math.max(r.width*r.height,o.width*o.height),diffPercentage:100}}return {diffCount:e.count,diffPercentage:e.percentage,diffOutput:t?.data}}function f({width:e,height:t}){return {data:new Uint8Array(e*t*4),width:e,height:t}}function I(e,t,r,o){let a=r?f(e):void 0;return p(diff$1(e,t,{...o.diff,output:a}),a)}async function w(e,t,r,o){let a=await diff(e,t,{...o.diff,output:r});if(a.kind==="io-error")throw new Error(a.path?`Image file not found or unreadable: ${a.path}. ${a.message}`:a.message);return p(a)}var B=createRequire(import.meta.url),P;function W(){return P||(P=import('@blazediff/core-wasm').then(async e=>{let t=B.resolve("@blazediff/core-wasm/wasm/blazediff_bg.wasm");return await e.initBlazediff(await readFile(t)),e})),P}async function C(e,t,r,o){let a=r?f(e):void 0,{diff:i}=await W();return p(await i(e,t,{...o.diff,output:a}),a)}function x(e,t,r,o){let a=r?f(e):void 0,i=gmsd(e,t,{...o.gmsd,output:a});return i.kind==="dimension-mismatch"?{score:1}:{score:i.score,diffOutput:a?.data}}var M={ssim:ssim,msssim:msssim,"hitchhikers-ssim":hitchhikersSSIM};function D(e,t,r,o,a){let i=o?f(e):void 0,u=M[r](e,t,{...a.ssim,output:i});return u.kind==="dimension-mismatch"?{score:0}:{score:u.score,diffOutput:i?.data}}function b(e){return e in M}async function O(e,t,r,o,a){if(r==="core-native"){let n=await w(e,t,a,o);return {diffCount:n.diffCount,diffPercentage:n.diffPercentage}}let i=h(e)?e:await c(e),u=h(t)?t:await c(t),d=a!==void 0;if(b(r)){let n=D(i,u,r,d,o);return {score:n.score,diffOutput:n.diffOutput}}if(r==="gmsd"){let n=x(i,u,d,o);return {score:n.score,diffOutput:n.diffOutput}}if(r==="core-wasm"){let n=await C(i,u,d,o);return {diffCount:n.diffCount,diffPercentage:n.diffPercentage,diffOutput:n.diffOutput}}let l=I(i,u,d,o);return {diffCount:l.diffCount,diffPercentage:l.diffPercentage,diffOutput:l.diffOutput}}function $(e){return e.data instanceof Uint8Array?e:{data:new Uint8Array(e.data),width:e.width,height:e.height}}async function j(e){switch(e.type){case "normalize":{let{input:t}=e.payload;return c(t)}case "loadPNG":{let{filePath:t}=e.payload;return y(t)}case "compare":{let{received:t,baseline:r,method:o,options:a,diffOutputPath:i}=e.payload;return O($(t),r,o,a,i)}default:throw new Error(`Unknown request type: ${e.type}`)}}parentPort&&parentPort.on("message",async e=>{try{let t=await j(e);parentPort?.postMessage({id:e.id,success:!0,result:t});}catch(t){parentPort?.postMessage({id:e.id,success:false,error:t instanceof Error?t.message:String(t)});}});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@blazediff/matcher",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Core matcher logic for visual regression testing with blazediff",
|
|
5
5
|
"private": false,
|
|
6
6
|
"publishConfig": {
|
|
@@ -33,12 +33,12 @@
|
|
|
33
33
|
"license": "MIT",
|
|
34
34
|
"dependencies": {
|
|
35
35
|
"picocolors": "^1.1.1",
|
|
36
|
-
"@blazediff/core
|
|
37
|
-
"@blazediff/core": "
|
|
38
|
-
"@blazediff/
|
|
39
|
-
"@blazediff/
|
|
36
|
+
"@blazediff/core": "2.0.0",
|
|
37
|
+
"@blazediff/core-native": "7.0.0",
|
|
38
|
+
"@blazediff/gmsd": "2.0.0",
|
|
39
|
+
"@blazediff/core-wasm": "7.0.0",
|
|
40
40
|
"@blazediff/codec-pngjs": "4.0.0",
|
|
41
|
-
"@blazediff/ssim": "
|
|
41
|
+
"@blazediff/ssim": "2.0.0"
|
|
42
42
|
},
|
|
43
43
|
"devDependencies": {
|
|
44
44
|
"@types/node": "^24.3.0",
|