UNPKG

lens-filter-sepia

Version:

Small library to apply a sepia transformation to a image

3 lines (2 loc) 3.5 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e(t.lensFilterSepia={})}(this,function(t){"use strict";var e,n=(function(t,e){!function(){function h(o,i,s){o.addEventListener("message",function(t){var e=t.data,n=e.id;if("RPC"===e.type&&null!=n)if(e.method){var r=i[e.method];null==r?o.postMessage({type:"RPC",id:n,error:"NO_SUCH_METHOD"}):Promise.resolve().then(function(){return r.apply(null,e.params)}).then(function(t){o.postMessage({type:"RPC",id:n,result:t})}).catch(function(t){o.postMessage({type:"RPC",id:n,error:""+t})})}else{var a=s[n];if(null==a)throw Error("Unknown callback "+n);delete s[n],e.error?a[1](Error(e.error)):a[0](e.result)}})}function r(t,e){var n=document.createElement("canvas");return n.width=t,n.height=e,n}e.getCanvas=r,e.convertImageDataToCanvasURL=function(t){var e=window.document.createElement("canvas"),n=e.getContext("2d");return e.width=t.width,e.height=t.height,n.putImageData(t,0,0),e.toDataURL()},e.applyFilter=function(t){var e=t.data,n=t.transform,i=t.options,s=t.nWorkers,c=function(t,e){var n,o,i,r=this,a={},s="__xpo"+Math.random().toString().substring(2)+"__";"function"==typeof t&&(t="("+Function.prototype.toString.call(t)+")("+s+")"),o=s,i=a,n=(n=(n=t).replace(/^(\s*)export\s+default\s+/m,function(t,e){return i.default=!0,""+e+o+".default="})).replace(/^(\s*)export\s+((?:async\s*)?function(?:\s*\*)?|const|let|var)(\s+)([a-zA-Z$_][a-zA-Z0-9$_]*)/gm,function(t,e,n,r,a){return i[a]=!0,""+e+o+"."+a+"="+n+r+a}),t="var "+o+"={};\n"+n+"\n"+o+";\n("+Function.prototype.toString.call(h)+")(self,"+s+",{})";var c,u=URL.createObjectURL(new Blob([t])),l=new Worker(u,void 0),d=l.terminate,f={},p=0;for(c in l.kill=function(t){l.postMessage({type:"KILL",signal:t}),setTimeout(l.terminate)},l.terminate=function(){URL.revokeObjectURL(u),d.call(r)},l.call=function(r,a){return new Promise(function(t,e){var n="rpc"+ ++p;f[n]=[t,e],l.postMessage({type:"RPC",id:n,method:r,params:a})})},l.rpcMethods={},h(l,l.rpcMethods,f),l.expose=function(t){l[c]=function(){return l.call(t,[].slice.call(arguments))}},a)c in l||l.expose(c);return l}("\n var transform = "+n+";\n\n export function execute(canvas, index, length, options) {\n canvas.data = transform({ \n data: canvas.data, \n length: length, \n options: options\n });\n\n return { result: canvas, index: index };\n }\n "),u=r(e.width,e.height),l=u.getContext("2d");l.putImageData(e,0,0),s=s||1;var d=Math.floor(u.height/s);return new Promise(function(e){for(var n=0,t=void 0,r=0;r<s;r++){t=d,r+1===s&&(t=u.height-d*r);var a=l.getImageData(0,d*r,u.width,t),o=t*u.width*4;c.execute(a,r,o,i).then(function(t){l.putImageData(t.result,0,d*t.index),++n===s&&e(l.getImageData(0,0,u.width,u.height))})}})}}()}(e={exports:{}},e.exports),e.exports),r=(n.getCanvas,n.convertImageDataToCanvasURL,n.applyFilter),a=function(t){for(var e=t.data,n=t.length,r=0;r<n;r+=4){var a=e[r],o=e[r+1],i=e[r+2];e[r]=.393*a+.769*o+.189*i,e[r+1]=.349*a+.686*o+.168*i,e[r+2]=.272*a+.534*o+.131*i}return e};t.transform=a,t.default=function(){var t=0<arguments.length&&void 0!==arguments[0]?arguments[0]:{},e=t.data,n=t.nWorkers;if(!e)throw new Error("lens-filter-sepia:: invalid options provided");return r({data:e,transform:a,nWorkers:n})},Object.defineProperty(t,"__esModule",{value:!0})}); //# sourceMappingURL=lens-filter-sepia.umd.js.map