UNPKG

@omnimedia/omnitool

Version:

open source video processing tools

3 lines (2 loc) 5.77 kB
import{A as m,E as p,g as y}from"./chunk-ERLCDDYH.js";var I;function x(i){let e=p.get().createCanvas(6,1),a=e.getContext("2d");return a.fillStyle=i,a.fillRect(0,0,6,1),e}function M(){if(I!==void 0)return I;try{let i=x("#ff00ff"),e=x("#ffff00"),n=p.get().createCanvas(6,1).getContext("2d");n.globalCompositeOperation="multiply",n.drawImage(i,0,0),n.drawImage(e,2,0);let t=n.getImageData(2,0,1,1);if(!t)I=!1;else{let o=t.data;I=o[0]===255&&o[1]===0&&o[2]===0}}catch{I=!1}return I}var c={canvas:null,convertTintToImage:!1,cacheStepsPerColorChannel:8,canUseMultiply:M(),tintMethod:null,_canvasSourceCache:new WeakMap,_unpremultipliedCache:new WeakMap,getCanvasSource:i=>{let e=i.source,a=e?.resource;if(!a)return null;let n=e.alphaMode==="premultiplied-alpha",t=e.resourceWidth??e.pixelWidth,o=e.resourceHeight??e.pixelHeight,h=t!==e.pixelWidth||o!==e.pixelHeight;if(n){if((a instanceof HTMLCanvasElement||typeof OffscreenCanvas<"u"&&a instanceof OffscreenCanvas)&&!h)return a;let r=c._unpremultipliedCache.get(e);if(r?.resourceId===e._resourceId)return r.canvas}if(a instanceof Uint8Array||a instanceof Uint8ClampedArray||a instanceof Int8Array||a instanceof Uint16Array||a instanceof Int16Array||a instanceof Uint32Array||a instanceof Int32Array||a instanceof Float32Array||a instanceof ArrayBuffer){let r=c._canvasSourceCache.get(e);if(r?.resourceId===e._resourceId)return r.canvas;let s=p.get().createCanvas(e.pixelWidth,e.pixelHeight),g=s.getContext("2d"),l=g.createImageData(e.pixelWidth,e.pixelHeight),d=l.data,f=a instanceof ArrayBuffer?new Uint8Array(a):new Uint8Array(a.buffer,a.byteOffset,a.byteLength);if(e.format==="bgra8unorm")for(let u=0;u<d.length&&u+3<f.length;u+=4)d[u]=f[u+2],d[u+1]=f[u+1],d[u+2]=f[u],d[u+3]=f[u+3];else d.set(f.subarray(0,d.length));return g.putImageData(l,0,0),c._canvasSourceCache.set(e,{canvas:s,resourceId:e._resourceId}),s}if(n){let r=p.get().createCanvas(e.pixelWidth,e.pixelHeight),s=r.getContext("2d",{willReadFrequently:!0});r.width=e.pixelWidth,r.height=e.pixelHeight,s.drawImage(a,0,0);let g=s.getImageData(0,0,r.width,r.height),l=g.data;for(let d=0;d<l.length;d+=4){let f=l[d+3];if(f>0){let u=255/f;l[d]=Math.min(255,l[d]*u+.5),l[d+1]=Math.min(255,l[d+1]*u+.5),l[d+2]=Math.min(255,l[d+2]*u+.5)}}return s.putImageData(g,0,0),c._unpremultipliedCache.set(e,{canvas:r,resourceId:e._resourceId}),r}if(h){let r=c._canvasSourceCache.get(e);if(r?.resourceId===e._resourceId)return r.canvas;let s=p.get().createCanvas(e.pixelWidth,e.pixelHeight),g=s.getContext("2d");return s.width=e.pixelWidth,s.height=e.pixelHeight,g.drawImage(a,0,0),c._canvasSourceCache.set(e,{canvas:s,resourceId:e._resourceId}),s}return a},getTintedCanvas:(i,e)=>{let a=i.texture,n=y.shared.setValue(e).toHex(),t=a.tintCache||(a.tintCache={}),o=t[n],h=a.source._resourceId;if(o?.tintId===h)return o;let r=o&&"getContext"in o?o:p.get().createCanvas();if(c.tintMethod(a,e,r),r.tintId=h,c.convertTintToImage&&r.toDataURL!==void 0){let s=p.get().createImage();s.src=r.toDataURL(),s.tintId=h,t[n]=s}else t[n]=r;return t[n]},getTintedPattern:(i,e)=>{let a=y.shared.setValue(e).toHex(),n=i.patternCache||(i.patternCache={}),t=i.source._resourceId,o=n[a];return o?.tintId===t||(c.canvas||(c.canvas=p.get().createCanvas()),c.tintMethod(i,e,c.canvas),o=c.canvas.getContext("2d").createPattern(c.canvas,"repeat"),o.tintId=t,n[a]=o),o},applyPatternTransform:(i,e,a=!0)=>{if(!e)return;let n=i;if(!n.setTransform)return;let t=globalThis.DOMMatrix;if(!t)return;let o=new t([e.a,e.b,e.c,e.d,e.tx,e.ty]);n.setTransform(a?o.inverse():o)},tintWithMultiply:(i,e,a)=>{let n=a.getContext("2d"),t=i.frame.clone(),o=i.source._resolution??i.source.resolution??1,h=i.rotate;t.x*=o,t.y*=o,t.width*=o,t.height*=o;let r=m.isVertical(h),s=r?t.height:t.width,g=r?t.width:t.height;a.width=Math.ceil(s),a.height=Math.ceil(g),n.save(),n.fillStyle=y.shared.setValue(e).toHex(),n.fillRect(0,0,s,g),n.globalCompositeOperation="multiply";let l=c.getCanvasSource(i);if(!l){n.restore();return}h&&c._applyInverseRotation(n,h,t.width,t.height),n.drawImage(l,t.x,t.y,t.width,t.height,0,0,t.width,t.height),n.globalCompositeOperation="destination-atop",n.drawImage(l,t.x,t.y,t.width,t.height,0,0,t.width,t.height),n.restore()},tintWithOverlay:(i,e,a)=>{let n=a.getContext("2d"),t=i.frame.clone(),o=i.source._resolution??i.source.resolution??1,h=i.rotate;t.x*=o,t.y*=o,t.width*=o,t.height*=o;let r=m.isVertical(h),s=r?t.height:t.width,g=r?t.width:t.height;a.width=Math.ceil(s),a.height=Math.ceil(g),n.save(),n.globalCompositeOperation="copy",n.fillStyle=y.shared.setValue(e).toHex(),n.fillRect(0,0,s,g),n.globalCompositeOperation="destination-atop";let l=c.getCanvasSource(i);if(!l){n.restore();return}h&&c._applyInverseRotation(n,h,t.width,t.height),n.drawImage(l,t.x,t.y,t.width,t.height,0,0,t.width,t.height),n.restore()},tintWithPerPixel:(i,e,a)=>{let n=a.getContext("2d"),t=i.frame.clone(),o=i.source._resolution??i.source.resolution??1,h=i.rotate;t.x*=o,t.y*=o,t.width*=o,t.height*=o;let r=m.isVertical(h),s=r?t.height:t.width,g=r?t.width:t.height;a.width=Math.ceil(s),a.height=Math.ceil(g),n.save(),n.globalCompositeOperation="copy";let l=c.getCanvasSource(i);if(!l){n.restore();return}h&&c._applyInverseRotation(n,h,t.width,t.height),n.drawImage(l,t.x,t.y,t.width,t.height,0,0,t.width,t.height),n.restore();let d=e>>16&255,f=e>>8&255,u=e&255,w=n.getImageData(0,0,s,g),v=w.data;for(let C=0;C<v.length;C+=4)v[C]=v[C]*d/255,v[C+1]=v[C+1]*f/255,v[C+2]=v[C+2]*u/255;n.putImageData(w,0,0)},_applyInverseRotation:(i,e,a,n)=>{let t=m.inv(e),o=m.uX(t),h=m.uY(t),r=m.vX(t),s=m.vY(t),g=-Math.min(0,o*a,r*n,o*a+r*n),l=-Math.min(0,h*a,s*n,h*a+s*n);i.transform(o,h,r,s,g,l)}};c.tintMethod=c.canUseMultiply?c.tintWithMultiply:c.tintWithPerPixel;export{M as a,c as b}; //# sourceMappingURL=chunk-73DDAAII.js.map