pixel-buffer-diff-folders
Version:
Pbd-folders ( Pixel Buffer Diff folders ) recursively diff folders of images using pixel buffer diff
1 lines • 1.74 kB
JavaScript
Object.defineProperty(exports,"__esModule",{value:!0}),exports.diffImage=void 0;const{isMainThread:e,parentPort:t}=require("worker_threads"),a=require("fs"),i=require("path"),r=require("jpeg-js"),n=require("pixel-buffer-diff"),d=require("fast-png"),o=async e=>{const t=e.endsWith(".png"),i=await a.promises.readFile(e),n=(t?d.decode:r.decode)(i),o={width:n.width,height:n.height,data:n.data,colorSpace:"srgb"},s=o.data,h=o.width*o.height,c=s.length;if(c===4*h)return o;const f={width:o.width,height:o.height,data:new Uint8ClampedArray(4*h)},g=f.data;for(let e=0,t=0;e<c;)g[t++]=s[e++],g[t++]=s[e++],g[t++]=s[e++],g[t++]=255;return f};exports.diffImage=async e=>{const{baselineFolder:t,candidateFolder:r,diffFolder:s,options:h,sideBySide:c,path:f}=e;try{const e=(0,i.join)(t,f),g=(0,i.join)(r,f),l=(0,i.join)(s,f),[p,w]=(e=>{if(2!==e.length)throw new Error(`Received ${e.length} ImageData instead of 2 in matchImageDataDimensions`);const[t,a]=e,i=t.width,r=t.height,n=a.width,d=a.height;if(i===n&&r===d)return e;const o=[],s=Math.max(i,n),h=Math.max(r,d);for(const t of e){const e={width:s,height:h,data:new Uint8ClampedArray(s*h*4),colorSpace:"srgb"};let a=0,i=0;const r=4*t.width,n=4*s;for(let d=0;d<h;d++)e.data.set(t.data.slice(a,a+r),i),a+=r,i+=n;o.push(e)}return o})(await Promise.all([o(e),o(g)])),m=c?3:1,{width:u,height:y}=p,b={width:u*m,height:y,data:new Uint8ClampedArray(u*m*y*4),colorSpace:"srgb"},j=(0,n.diffImageDatas)(p,w,b,h),x=Object.assign(j,{path:f});if(j.diff>0){const e=d.encode(b);a.mkdirSync((0,i.dirname)(l),{recursive:!0}),a.writeFileSync(l,e)}return x}catch(e){return{path:f,error:`${e}`}}},e||(async()=>{new Promise((()=>{t.on("message",(async e=>{const a=await(0,exports.diffImage)(e);t.postMessage(a)}))}))})();