diff --git a/codecs/wp2/dec/wp2_dec.cpp b/codecs/wp2/dec/wp2_dec.cpp index d661ad9c..5ee45156 100644 --- a/codecs/wp2/dec/wp2_dec.cpp +++ b/codecs/wp2/dec/wp2_dec.cpp @@ -1,40 +1,24 @@ -#include #include #include - +#include #include "src/wp2/decode.h" + using namespace emscripten; -class RawImage { -public: - val buffer; - int width; - int height; +thread_local const val Uint8ClampedArray = val::global("Uint8ClampedArray"); +thread_local const val ImageData = val::global("ImageData"); - RawImage(val b, int w, int h) : buffer(b), width(w), height(h) {} -}; - -WP2::ArgbBuffer *buffer = new WP2::ArgbBuffer(WP2_rgbA_32); - -RawImage decode(std::string image_in) { - WP2Status status; - status = WP2::Decode(image_in, buffer); +val decode(std::string image_in) { + WP2::ArgbBuffer buffer(WP2_rgbA_32); + WP2Status status = WP2::Decode(image_in, &buffer); if (status != WP2_STATUS_OK) { - return RawImage(val::null(), -1, -1); + return val::null(); } - return RawImage(val(typed_memory_view(buffer->width * buffer->height * 4, - (uint8_t *)buffer->GetRow(0))), - buffer->width, buffer->height); + return ImageData.new_( + Uint8ClampedArray.new_(typed_memory_view(buffer.stride * buffer.height, buffer.GetRow8(0))), + buffer.width, buffer.height); } -void free_result() { delete buffer; } - EMSCRIPTEN_BINDINGS(my_module) { - class_("RawImage") - .property("buffer", &RawImage::buffer) - .property("width", &RawImage::width) - .property("height", &RawImage::height); - function("decode", &decode); - function("free_result", &free_result); } diff --git a/codecs/wp2/dec/wp2_dec.d.ts b/codecs/wp2/dec/wp2_dec.d.ts index 128c95a2..877df248 100644 --- a/codecs/wp2/dec/wp2_dec.d.ts +++ b/codecs/wp2/dec/wp2_dec.d.ts @@ -1,13 +1,5 @@ -interface RawImage { - buffer: Uint8Array; - width: number; - height: number; -} - interface WP2Module extends EmscriptenWasm.Module { - decode(data: BufferSource): RawImage; - free_result(): void; + decode(data: BufferSource): ImageData | null; } export default function(opts: EmscriptenWasm.ModuleOpts): Promise; - diff --git a/codecs/wp2/dec/wp2_dec.js b/codecs/wp2/dec/wp2_dec.js index 190be496..4f6e8c9f 100644 --- a/codecs/wp2/dec/wp2_dec.js +++ b/codecs/wp2/dec/wp2_dec.js @@ -7,66 +7,53 @@ function(wp2_dec) { wp2_dec = wp2_dec || {}; -var e;e||(e=typeof wp2_dec !== 'undefined' ? wp2_dec : {});var aa,ba;e.ready=new Promise(function(a,b){aa=a;ba=b});var r={},w;for(w in e)e.hasOwnProperty(w)&&(r[w]=e[w]);var ca=!1,y=!1,da=!1,ea=!1;ca="object"===typeof window;y="function"===typeof importScripts;da="object"===typeof process&&"object"===typeof process.versions&&"string"===typeof process.versions.node;ea=!ca&&!da&&!y;var z="",ha,A,ia,ja; -if(da)z=y?require("path").dirname(z)+"/":__dirname+"/",ha=function(a,b){ia||(ia=require("fs"));ja||(ja=require("path"));a=ja.normalize(a);return ia.readFileSync(a,b?null:"utf8")},A=function(a){a=ha(a,!0);a.buffer||(a=new Uint8Array(a));a.buffer||B("Assertion failed: undefined");return a},1=d);)++c;if(16f?d+=String.fromCharCode(f):(f-=65536,d+=String.fromCharCode(55296|f>>10,56320|f&1023))}}else d+=String.fromCharCode(f)}return d} -function pa(a,b,c){var d=H;if(0=g){var l=a.charCodeAt(++f);g=65536+((g&1023)<<10)|l&1023}if(127>=g){if(b>=c)break;d[b++]=g}else{if(2047>=g){if(b+1>=c)break;d[b++]=192|g>>6}else{if(65535>=g){if(b+2>=c)break;d[b++]=224|g>>12}else{if(b+3>=c)break;d[b++]=240|g>>18;d[b++]=128|g>>12&63}d[b++]=128|g>>6&63}d[b++]=128|g&63}}d[b]=0}}var qa="undefined"!==typeof TextDecoder?new TextDecoder("utf-16le"):void 0; -function ra(a,b){var c=a>>1;for(var d=c+b/2;!(c>=d)&&sa[c];)++c;c<<=1;if(32>1];if(0==f||c==b/2)return d;++c;d+=String.fromCharCode(f)}}function ta(a,b,c){void 0===c&&(c=2147483647);if(2>c)return 0;c-=2;var d=b;c=c<2*a.length?c/2:a.length;for(var f=0;f>1]=a.charCodeAt(f),b+=2;I[b>>1]=0;return b-d}function ua(a){return 2*a.length} -function va(a,b){for(var c=0,d="";!(c>=b/4);){var f=J[a+4*c>>2];if(0==f)break;++c;65536<=f?(f-=65536,d+=String.fromCharCode(55296|f>>10,56320|f&1023)):d+=String.fromCharCode(f)}return d}function wa(a,b,c){void 0===c&&(c=2147483647);if(4>c)return 0;var d=b;c=d+c-4;for(var f=0;f=g){var l=a.charCodeAt(++f);g=65536+((g&1023)<<10)|l&1023}J[b>>2]=g;b+=4;if(b+4>c)break}J[b>>2]=0;return b-d} -function xa(a){for(var b=0,c=0;c=d&&++c;b+=4}return b}var K,ya,H,I,sa,J,L,za,Aa;function Ba(a){K=a;e.HEAP8=ya=new Int8Array(a);e.HEAP16=I=new Int16Array(a);e.HEAP32=J=new Int32Array(a);e.HEAPU8=H=new Uint8Array(a);e.HEAPU16=sa=new Uint16Array(a);e.HEAPU32=L=new Uint32Array(a);e.HEAPF32=za=new Float32Array(a);e.HEAPF64=Aa=new Float64Array(a)}var Ca=e.INITIAL_MEMORY||16777216; -e.wasmMemory?G=e.wasmMemory:G=new WebAssembly.Memory({initial:Ca/65536,maximum:32768});G&&(K=G.buffer);Ca=K.byteLength;Ba(K);J[33036]=5375184;function Da(a){for(;0=b?"_"+a:a} -function Xa(a,b){a=Wa(a);return(new Function("body","return function "+a+'() {\n "use strict"; return body.apply(this, arguments);\n};\n'))(b)}function Ya(a){var b=Error,c=Xa(a,function(d){this.name=a;this.message=d;d=Error(d).stack;void 0!==d&&(this.stack=this.toString()+"\n"+d.replace(/^Error(:[^\n]*)?\n/,""))});c.prototype=Object.create(b.prototype);c.prototype.constructor=c;c.prototype.toString=function(){return void 0===this.message?this.name:this.name+": "+this.message};return c} -var Za=void 0;function S(a){throw new Za(a);}var $a=void 0;function ab(a){throw new $a(a);}function bb(a,b,c){function d(k){k=c(k);k.length!==a.length&&ab("Mismatched type converter count");for(var h=0;h>2])}function wb(a,b,c){if(b===c)return a;if(void 0===c.ea)return null;a=wb(a,b,c.ea);return null===a?null:c.wa(a)}var xb={}; -function yb(a,b){for(void 0===b&&S("ptr should not be undefined");a.ea;)b=a.ma(b),a=a.ea;return xb[b]}function zb(a,b){b.aa&&b.Z||ab("makeClassHandle requires ptr and ptrType");!!b.da!==!!b.ba&&ab("Both smartPtrType and smartPtr must be specified");b.count={value:1};return hb(Object.create(a,{Y:{value:b}}))} -function W(a,b,c,d){this.name=a;this.$=b;this.qa=c;this.na=d;this.oa=!1;this.ia=this.Da=this.Ca=this.ta=this.Ea=this.Ba=void 0;void 0!==b.ea?this.toWireType=rb:(this.toWireType=d?qb:tb,this.ga=null)}function Ab(a,b,c){e.hasOwnProperty(a)||ab("Replacing nonexistant public symbol");void 0!==e[a].fa&&void 0!==c?e[a].fa[c]=b:(e[a]=b,e[a].ua=c)} -function X(a,b){a=P(a);var c=e["dynCall_"+a];for(var d=[],f=1;f>2])};case 3:return function(c){return this.fromWireType(Aa[c>>3])};default:throw new TypeError("Unknown float type: "+a);}}function Kb(a){var b=Function;if(!(b instanceof Function))throw new TypeError("new_ called with constructor type "+typeof b+" which is not a function");var c=Xa(b.name||"unknownFunctionName",function(){});c.prototype=b.prototype;c=new c;a=b.apply(c,a);return a instanceof Object?a:c} -function Lb(a,b){for(var c=[],d=0;d>2)+d]);return c}function Mb(a,b,c){switch(b){case 0:return c?function(d){return ya[d]}:function(d){return H[d]};case 1:return c?function(d){return I[d>>1]}:function(d){return sa[d>>1]};case 2:return c?function(d){return J[d>>2]}:function(d){return L[d>>2]};default:throw new TypeError("Unknown integer type: "+a);}}for(var Nb=[null,[],[]],Ob=Array(256),Pb=0;256>Pb;++Pb)Ob[Pb]=String.fromCharCode(Pb);Ua=Ob;Za=e.BindingError=Ya("BindingError"); -$a=e.InternalError=Ya("InternalError");U.prototype.isAliasOf=function(a){if(!(this instanceof U&&a instanceof U))return!1;var b=this.Y.aa.$,c=this.Y.Z,d=a.Y.aa.$;for(a=a.Y.Z;b.ea;)c=b.ma(c),b=b.ea;for(;d.ea;)a=d.ma(a),d=d.ea;return b===d&&c===a};U.prototype.clone=function(){this.Y.Z||db(this);if(this.Y.la)return this.Y.count.value+=1,this;var a=hb(Object.create(Object.getPrototypeOf(this),{Y:{value:cb(this.Y)}}));a.Y.count.value+=1;a.Y.ja=!1;return a}; -U.prototype["delete"]=function(){this.Y.Z||db(this);this.Y.ja&&!this.Y.la&&S("Object already scheduled for deletion");fb(this);gb(this.Y);this.Y.la||(this.Y.ba=void 0,this.Y.Z=void 0)};U.prototype.isDeleted=function(){return!this.Y.Z};U.prototype.deleteLater=function(){this.Y.Z||db(this);this.Y.ja&&!this.Y.la&&S("Object already scheduled for deletion");jb.push(this);1===jb.length&&ib&&ib(kb);this.Y.ja=!0;return this};W.prototype.za=function(a){this.ta&&(a=this.ta(a));return a}; -W.prototype.sa=function(a){this.ia&&this.ia(a)};W.prototype.argPackAdvance=8;W.prototype.readValueFromPointer=vb;W.prototype.deleteObject=function(a){if(null!==a)a["delete"]()}; -W.prototype.fromWireType=function(a){function b(){return this.oa?zb(this.$.ha,{aa:this.Ba,Z:c,da:this,ba:a}):zb(this.$.ha,{aa:this,Z:a})}var c=this.za(a);if(!c)return this.sa(a),null;var d=yb(this.$,c);if(void 0!==d){if(0===d.Y.count.value)return d.Y.Z=c,d.Y.ba=a,d.clone();d=d.clone();this.sa(a);return d}d=this.$.ya(c);d=lb[d];if(!d)return b.call(this);d=this.na?d.va:d.pointerType;var f=wb(c,this.$,d.$);return null===f?b.call(this):this.oa?zb(d.$.ha,{aa:d,Z:f,da:this,ba:a}):zb(d.$.ha,{aa:d,Z:f})}; -e.getInheritedInstanceCount=function(){return Object.keys(xb).length};e.getLiveInheritedInstances=function(){var a=[],b;for(b in xb)xb.hasOwnProperty(b)&&a.push(xb[b]);return a};e.flushPendingDeletes=kb;e.setDelayFunction=function(a){ib=a;jb.length&&ib&&ib(kb)};Bb=e.UnboundTypeError=Ya("UnboundTypeError");e.count_emval_handles=function(){for(var a=0,b=5;b>g])},ga:null})},u:function(a,b,c,d,f,g,l,k,h,p,m,q,t){m=P(m);g=X(f,g);k&&(k=X(l, -k));p&&(p=X(h,p));t=X(q,t);var u=Wa(m);nb(u,function(){Eb("Cannot construct "+m+" due to unbound types",[d])});bb([a,b,c],d?[d]:[],function(n){n=n[0];if(d){var D=n.$;var v=D.ha}else v=U.prototype;n=Xa(u,function(){if(Object.getPrototypeOf(this)!==x)throw new Za("Use 'new' to construct "+m);if(void 0===C.ka)throw new Za(m+" has no accessible constructor");var ub=C.ka[arguments.length];if(void 0===ub)throw new Za("Tried to invoke ctor of "+m+" with invalid number of parameters ("+arguments.length+") - expected ("+ -Object.keys(C.ka).toString()+") parameters instead!");return ub.apply(this,arguments)});var x=Object.create(v,{constructor:{value:n}});n.prototype=x;var C=new ob(m,n,x,t,D,g,k,p);D=new W(m,C,!0,!1);v=new W(m+"*",C,!1,!1);var fa=new W(m+" const*",C,!1,!0);lb[a]={pointerType:v,va:fa};Ab(u,n);return[D,v,fa]})},f:function(a,b,c,d,f,g,l,k,h,p){b=P(b);f=X(d,f);bb([],[a],function(m){m=m[0];var q=m.name+"."+b,t={get:function(){Eb("Cannot access "+q+" due to unbound types",[c,l])},enumerable:!0,configurable:!0}; -h?t.set=function(){Eb("Cannot access "+q+" due to unbound types",[c,l])}:t.set=function(){S(q+" is a read-only property")};Object.defineProperty(m.$.ha,b,t);bb([],h?[c,l]:[c],function(u){var n=u[0],D={get:function(){var x=Gb(this,m,q+" getter");return n.fromWireType(f(g,x))},enumerable:!0};if(h){h=X(k,h);var v=u[1];D.set=function(x){var C=Gb(this,m,q+" setter"),fa=[];h(p,C,v.toWireType(fa,x));Fb(fa)}}Object.defineProperty(m.$.ha,b,D);return[]});return[]})},x:function(a,b){b=P(b);T(a,{name:b,fromWireType:function(c){var d= -Z[c].value;Ib(c);return d},toWireType:function(c,d){return sb(d)},argPackAdvance:8,readValueFromPointer:vb,ga:null})},n:function(a,b,c){c=Ta(c);b=P(b);T(a,{name:b,fromWireType:function(d){return d},toWireType:function(d,f){if("number"!==typeof f&&"boolean"!==typeof f)throw new TypeError('Cannot convert "'+V(f)+'" to '+this.name);return f},argPackAdvance:8,readValueFromPointer:Jb(b,c),ga:null})},j:function(a,b,c,d,f,g){var l=Lb(b,c);a=P(a);f=X(d,f);nb(a,function(){Eb("Cannot call "+a+" due to unbound types", -l)},b-1);bb([],l,function(k){var h=[k[0],null].concat(k.slice(1)),p=k=a,m=f,q=h.length;2>q&&S("argTypes array size mismatch! Must at least get return value and 'this' types!");for(var t=null!==h[1]&&!1,u=!1,n=1;n>>k}}var h=-1!=b.indexOf("unsigned");T(a,{name:b, -fromWireType:g,toWireType:function(p,m){if("number"!==typeof m&&"boolean"!==typeof m)throw new TypeError('Cannot convert "'+V(m)+'" to '+this.name);if(mf)throw new TypeError('Passing a number "'+V(m)+'" from JS side to C/C++ side to an argument of type "'+b+'", which is outside the valid range ['+d+", "+f+"]!");return h?m>>>0:m|0},argPackAdvance:8,readValueFromPointer:Mb(b,l,0!==d),ga:null})},a:function(a,b,c){function d(g){g>>=2;var l=L;return new f(K,l[g+1],l[g])}var f=[Int8Array,Uint8Array, -Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array][b];c=P(c);T(a,{name:c,fromWireType:d,argPackAdvance:8,readValueFromPointer:d},{Aa:!0})},o:function(a,b){b=P(b);var c="std::string"===b;T(a,{name:b,fromWireType:function(d){var f=L[d>>2];if(c)for(var g=d+4,l=0;l<=f;++l){var k=d+4+l;if(l==f||0==H[k]){g=g?oa(H,g,k-g):"";if(void 0===h)var h=g;else h+=String.fromCharCode(0),h+=g;g=k+1}}else{h=Array(f);for(l=0;l=q&&(q=65536+((q&1023)<<10)|f.charCodeAt(++m)&1023);127>=q?++p:p=2047>=q?p+2:65535>=q?p+3:p+4}return p}:function(){return f.length})(),k=Qb(4+l+1);L[k>>2]=l;if(c&&g)pa(f,k+4,l+1);else if(g)for(g= -0;g>2],m=l(),q,t=h+4,u=0;u<=p;++u){var n=h+4+u*b; -if(u==p||0==m[n>>k])t=d(t,n-t),void 0===q?q=t:(q+=String.fromCharCode(0),q+=t),t=n+b}Y(h);return q},toWireType:function(h,p){"string"!==typeof p&&S("Cannot pass non-string to C++ string type "+c);var m=g(p),q=Qb(4+m+b);L[q>>2]=m>>k;f(p,q+4,m+b);null!==h&&h.push(Y,q);return q},argPackAdvance:8,readValueFromPointer:vb,ga:function(h){Y(h)}})},z:function(a,b){b=P(b);T(a,{Fa:!0,name:b,argPackAdvance:0,fromWireType:function(){},toWireType:function(){}})},d:Ib,h:function(a){4>>=0;var b=H.length;if(2147483648=c;c*=2){var d=b*(1+.2/c);d=Math.min(d,a+100663296);d=Math.max(16777216,a,d);0>>16);Ba(G.buffer);var f=1;break a}catch(g){}f=void 0}if(f)return!0}return!1},m:function(a, -b,c,d){for(var f=0,g=0;g>2],k=J[b+(8*g+4)>>2],h=0;h>2]=f;return 0},memory:G,p:function(){return 0},r:function(){return 0},q:function(){},B:function(){return 6},A:function(){},k:function(a){a=+a;return 0<=a?+Ka(a+.5):+Ja(a-.5)},e:function(a){a=+a;return 0<=a?+Ka(a+.5):+Ja(a-.5)},v:function(){},table:la}; -(function(){function a(f){e.asm=f.exports;M--;e.monitorRunDependencies&&e.monitorRunDependencies(M);0==M&&(null!==La&&(clearInterval(La),La=null),N&&(f=N,N=null,f()))}function b(f){a(f.instance)}function c(f){return Qa().then(function(g){return WebAssembly.instantiate(g,d)}).then(f,function(g){E("failed to asynchronously prepare wasm: "+g);B(g)})}var d={a:Rb};M++;e.monitorRunDependencies&&e.monitorRunDependencies(M);if(e.instantiateWasm)try{return e.instantiateWasm(d,a)}catch(f){return E("Module.instantiateWasm callback failed with error: "+ -f),!1}(function(){if(F||"function"!==typeof WebAssembly.instantiateStreaming||Na()||Ma("file://")||"function"!==typeof fetch)return c(b);fetch(O,{credentials:"same-origin"}).then(function(f){return WebAssembly.instantiateStreaming(f,d).then(b,function(g){E("wasm streaming compile failed: "+g);E("falling back to ArrayBuffer instantiation");return c(b)})})})();return{}})(); -var Ra=e.___wasm_call_ctors=function(){return(Ra=e.___wasm_call_ctors=e.asm.C).apply(null,arguments)},Qb=e._malloc=function(){return(Qb=e._malloc=e.asm.D).apply(null,arguments)},Y=e._free=function(){return(Y=e._free=e.asm.E).apply(null,arguments)},Db=e.___getTypeName=function(){return(Db=e.___getTypeName=e.asm.F).apply(null,arguments)};e.___embind_register_native_and_builtin_types=function(){return(e.___embind_register_native_and_builtin_types=e.asm.G).apply(null,arguments)}; -e.dynCall_ii=function(){return(e.dynCall_ii=e.asm.H).apply(null,arguments)};e.dynCall_vi=function(){return(e.dynCall_vi=e.asm.I).apply(null,arguments)};e.dynCall_iii=function(){return(e.dynCall_iii=e.asm.J).apply(null,arguments)};e.dynCall_viii=function(){return(e.dynCall_viii=e.asm.K).apply(null,arguments)};e.dynCall_vii=function(){return(e.dynCall_vii=e.asm.L).apply(null,arguments)};e.dynCall_v=function(){return(e.dynCall_v=e.asm.M).apply(null,arguments)}; -e.dynCall_viiiiiiii=function(){return(e.dynCall_viiiiiiii=e.asm.N).apply(null,arguments)};e.dynCall_viiiiiii=function(){return(e.dynCall_viiiiiii=e.asm.O).apply(null,arguments)};e.dynCall_viiiiiiiii=function(){return(e.dynCall_viiiiiiiii=e.asm.P).apply(null,arguments)};e.dynCall_viiii=function(){return(e.dynCall_viiii=e.asm.Q).apply(null,arguments)};e.dynCall_viiiiii=function(){return(e.dynCall_viiiiii=e.asm.R).apply(null,arguments)}; -e.dynCall_iidiiii=function(){return(e.dynCall_iidiiii=e.asm.S).apply(null,arguments)};e.dynCall_viiiii=function(){return(e.dynCall_viiiii=e.asm.T).apply(null,arguments)};e.dynCall_iiiiii=function(){return(e.dynCall_iiiiii=e.asm.U).apply(null,arguments)};e.dynCall_iiii=function(){return(e.dynCall_iiii=e.asm.V).apply(null,arguments)};e.dynCall_iiiii=function(){return(e.dynCall_iiiii=e.asm.W).apply(null,arguments)};e.dynCall_jiji=function(){return(e.dynCall_jiji=e.asm.X).apply(null,arguments)};var Sb; -N=function Tb(){Sb||Ub();Sb||(N=Tb)}; -function Ub(){function a(){if(!Sb&&(Sb=!0,e.calledRun=!0,!ma)){Da(Fa);Da(Ga);aa(e);if(e.onRuntimeInitialized)e.onRuntimeInitialized();if(e.postRun)for("function"==typeof e.postRun&&(e.postRun=[e.postRun]);e.postRun.length;){var b=e.postRun.shift();Ha.unshift(b)}Da(Ha)}}if(!(0=e);)++c;if(16f?e+=String.fromCharCode(f):(f-=65536,e+=String.fromCharCode(55296|f>>10,56320|f&1023))}}else e+=String.fromCharCode(f)}return e} +function ma(a,b,c){var e=F;if(0=g){var l=a.charCodeAt(++f);g=65536+((g&1023)<<10)|l&1023}if(127>=g){if(b>=c)break;e[b++]=g}else{if(2047>=g){if(b+1>=c)break;e[b++]=192|g>>6}else{if(65535>=g){if(b+2>=c)break;e[b++]=224|g>>12}else{if(b+3>=c)break;e[b++]=240|g>>18;e[b++]=128|g>>12&63}e[b++]=128|g>>6&63}e[b++]=128|g&63}}e[b]=0}}var na="undefined"!==typeof TextDecoder?new TextDecoder("utf-16le"):void 0; +function oa(a,b){var c=a>>1;for(var e=c+b/2;!(c>=e)&&G[c];)++c;c<<=1;if(32>1];if(0==f||c==b/2)return e;++c;e+=String.fromCharCode(f)}}function pa(a,b,c){void 0===c&&(c=2147483647);if(2>c)return 0;c-=2;var e=b;c=c<2*a.length?c/2:a.length;for(var f=0;f>1]=a.charCodeAt(f),b+=2;H[b>>1]=0;return b-e}function qa(a){return 2*a.length} +function ra(a,b){for(var c=0,e="";!(c>=b/4);){var f=I[a+4*c>>2];if(0==f)break;++c;65536<=f?(f-=65536,e+=String.fromCharCode(55296|f>>10,56320|f&1023)):e+=String.fromCharCode(f)}return e}function sa(a,b,c){void 0===c&&(c=2147483647);if(4>c)return 0;var e=b;c=e+c-4;for(var f=0;f=g){var l=a.charCodeAt(++f);g=65536+((g&1023)<<10)|l&1023}I[b>>2]=g;b+=4;if(b+4>c)break}I[b>>2]=0;return b-e} +function ta(a){for(var b=0,c=0;c=e&&++c;b+=4}return b}var J,ua,F,H,G,I,L,va,wa;function xa(a){J=a;d.HEAP8=ua=new Int8Array(a);d.HEAP16=H=new Int16Array(a);d.HEAP32=I=new Int32Array(a);d.HEAPU8=F=new Uint8Array(a);d.HEAPU16=G=new Uint16Array(a);d.HEAPU32=L=new Uint32Array(a);d.HEAPF32=va=new Float32Array(a);d.HEAPF64=wa=new Float64Array(a)}var ya=d.INITIAL_MEMORY||16777216;d.wasmMemory?E=d.wasmMemory:E=new WebAssembly.Memory({initial:ya/65536,maximum:32768}); +E&&(J=E.buffer);ya=J.byteLength;xa(J);I[32956]=5374864;function M(a){for(;0=b?"_"+a:a} +function Qa(a,b){a=Pa(a);return(new Function("body","return function "+a+'() {\n "use strict"; return body.apply(this, arguments);\n};\n'))(b)}function Ra(a){var b=Error,c=Qa(a,function(e){this.name=a;this.message=e;e=Error(e).stack;void 0!==e&&(this.stack=this.toString()+"\n"+e.replace(/^Error(:[^\n]*)?\n/,""))});c.prototype=Object.create(b.prototype);c.prototype.constructor=c;c.prototype.toString=function(){return void 0===this.message?this.name:this.name+": "+this.message};return c} +var Sa=void 0;function W(a){throw new Sa(a);}var Ta=void 0;function Ua(a,b){function c(k){k=b(k);if(k.length!==e.length)throw new Ta("Mismatched type converter count");for(var h=0;h>2])}function ab(a){if(null===a)return"null";var b=typeof a;return"object"===b||"array"===b||"function"===b?a.toString():""+a} +function bb(a,b){switch(b){case 2:return function(c){return this.fromWireType(va[c>>2])};case 3:return function(c){return this.fromWireType(wa[c>>3])};default:throw new TypeError("Unknown float type: "+a);}}function cb(a){var b=Function;if(!(b instanceof Function))throw new TypeError("new_ called with constructor type "+typeof b+" which is not a function");var c=Qa(b.name||"unknownFunctionName",function(){});c.prototype=b.prototype;c=new c;a=b.apply(c,a);return a instanceof Object?a:c} +function db(a){for(;a.length;){var b=a.pop();a.pop()(b)}}function eb(a,b){var c=d;if(void 0===c[a].Y){var e=c[a];c[a]=function(){c[a].Y.hasOwnProperty(arguments.length)||W("Function '"+b+"' called with an invalid number of arguments ("+arguments.length+") - expects one of ("+c[a].Y+")!");return c[a].Y[arguments.length].apply(this,arguments)};c[a].Y=[];c[a].Y[e.da]=e}} +function fb(a,b,c){d.hasOwnProperty(a)?((void 0===c||void 0!==d[a].Y&&void 0!==d[a].Y[c])&&W("Cannot register public name '"+a+"' twice"),eb(a,a),d.hasOwnProperty(c)&&W("Cannot register multiple overloads of a function with the same number of arguments ("+c+")!"),d[a].Y[c]=b):(d[a]=b,void 0!==c&&(d[a].ha=c))}function gb(a,b){for(var c=[],e=0;e>2)+e]);return c} +function hb(a,b){a=S(a);var c=d["dynCall_"+a];for(var e=[],f=1;f>1]}:function(e){return G[e>>1]};case 2:return c?function(e){return I[e>>2]}:function(e){return L[e>>2]};default:throw new TypeError("Unknown integer type: "+a);}}var nb={}; +function ob(){return"object"===typeof globalThis?globalThis:Function("return this")()}function pb(a,b){var c=U[a];void 0===c&&W(b+" has unknown type "+jb(a));return c}for(var qb={},rb=[null,[],[]],sb=Array(256),tb=0;256>tb;++tb)sb[tb]=String.fromCharCode(tb);Oa=sb;Sa=d.BindingError=Ra("BindingError");Ta=d.InternalError=Ra("InternalError");d.count_emval_handles=function(){for(var a=0,b=5;b>g])},Z:null})},x:function(a,b){b=S(b);X(a,{name:b,fromWireType:function(c){var e= +Y[c].value;Wa(c);return e},toWireType:function(c,e){return Za(e)},argPackAdvance:8,readValueFromPointer:$a,Z:null})},n:function(a,b,c){c=Na(c);b=S(b);X(a,{name:b,fromWireType:function(e){return e},toWireType:function(e,f){if("number"!==typeof f&&"boolean"!==typeof f)throw new TypeError('Cannot convert "'+ab(f)+'" to '+this.name);return f},argPackAdvance:8,readValueFromPointer:bb(b,c),Z:null})},u:function(a,b,c,e,f,g){var l=gb(b,c);a=S(a);f=hb(e,f);fb(a,function(){lb("Cannot call "+a+" due to unbound types", +l)},b-1);Ua(l,function(k){var h=[k[0],null].concat(k.slice(1)),n=k=a,p=f,q=h.length;2>q&&W("argTypes array size mismatch! Must at least get return value and 'this' types!");for(var y=null!==h[1]&&!1,C=!1,m=1;m>>k}}var h=-1!=b.indexOf("unsigned");X(a,{name:b,fromWireType:g,toWireType:function(n,p){if("number"!==typeof p&&"boolean"!==typeof p)throw new TypeError('Cannot convert "'+ab(p)+'" to '+this.name);if(pf)throw new TypeError('Passing a number "'+ab(p)+'" from JS side to C/C++ side to an argument of type "'+b+'", which is outside the valid range ['+e+", "+f+"]!");return h?p>>>0:p|0},argPackAdvance:8,readValueFromPointer:mb(b, +l,0!==e),Z:null})},a:function(a,b,c){function e(g){g>>=2;var l=L;return new f(J,l[g+1],l[g])}var f=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array][b];c=S(c);X(a,{name:c,fromWireType:e,argPackAdvance:8,readValueFromPointer:e},{fa:!0})},o:function(a,b){b=S(b);var c="std::string"===b;X(a,{name:b,fromWireType:function(e){var f=L[e>>2];if(c)for(var g=e+4,l=0;l<=f;++l){var k=e+4+l;if(l==f||0==F[k]){g=g?la(F,g,k-g):"";if(void 0===h)var h=g;else h+=String.fromCharCode(0), +h+=g;g=k+1}}else{h=Array(f);for(l=0;l=q&&(q=65536+((q&1023)<<10)|f.charCodeAt(++p)&1023);127>=q?++n:n=2047>=q?n+ +2:65535>=q?n+3:n+4}return n}:function(){return f.length})(),k=ub(4+l+1);L[k>>2]=l;if(c&&g)ma(f,k+4,l+1);else if(g)for(g=0;g>2],p=l(),q,y=h+4,C=0;C<=n;++C){var m=h+4+C*b;if(C==n||0==p[m>>k])y=e(y,m-y),void 0===q?q=y:(q+=String.fromCharCode(0),q+=y),y=m+b}Z(h);return q},toWireType:function(h,n){"string"!==typeof n&&W("Cannot pass non-string to C++ string type "+c);var p=g(n),q=ub(4+p+b);L[q>>2]=p>>k;f(n,q+4,p+b);null!==h&&h.push(Z,q);return q},argPackAdvance:8,readValueFromPointer:$a,Z:function(h){Z(h)}})},z:function(a,b){b=S(b);X(a,{ga:!0,name:b,argPackAdvance:0, +fromWireType:function(){},toWireType:function(){}})},e:Wa,f:function(a){if(0===a)return Za(ob());var b=nb[a];a=void 0===b?S(a):b;return Za(ob()[a])},i:function(a){4>> 2) + "+ +g+'], "parameter '+g+'");\nvar arg'+g+" = argType"+g+".readValueFromPointer(args);\nargs += argType"+g+"['argPackAdvance'];\n";f=(new Function("requireRegisteredType","Module","__emval_register",l+("var obj = new constructor("+f+");\nreturn __emval_register(obj);\n}\n")))(pb,d,Za);qb[b]=f}return f(a,c,e)},l:function(){A()},w:function(a,b,c){F.copyWithin(a,b,b+c)},c:function(a){a>>>=0;var b=F.length;if(2147483648=c;c*=2){var e=b*(1+.2/c);e=Math.min(e,a+100663296);e=Math.max(16777216, +a,e);0>>16);xa(E.buffer);var f=1;break a}catch(g){}f=void 0}if(f)return!0}return!1},m:function(a,b,c,e){for(var f=0,g=0;g>2],k=I[b+(8*g+4)>>2],h=0;h>2]=f;return 0},memory:E,p:function(){return 0},r:function(){return 0},q:function(){},B:function(){return 6},A:function(){},k:function(a){a=+a;return 0<= +a?+Fa(a+.5):+Ea(a-.5)},d:function(a){a=+a;return 0<=a?+Fa(a+.5):+Ea(a-.5)},v:function(){},table:ia}; +(function(){function a(f){d.asm=f.exports;N--;d.monitorRunDependencies&&d.monitorRunDependencies(N);0==N&&(null!==Ga&&(clearInterval(Ga),Ga=null),O&&(f=O,O=null,f()))}function b(f){a(f.instance)}function c(f){return La().then(function(g){return WebAssembly.instantiate(g,e)}).then(f,function(g){B("failed to asynchronously prepare wasm: "+g);A(g)})}var e={a:vb};N++;d.monitorRunDependencies&&d.monitorRunDependencies(N);if(d.instantiateWasm)try{return d.instantiateWasm(e,a)}catch(f){return B("Module.instantiateWasm callback failed with error: "+ +f),!1}(function(){if(D||"function"!==typeof WebAssembly.instantiateStreaming||Ia()||Ha("file://")||"function"!==typeof fetch)return c(b);fetch(Q,{credentials:"same-origin"}).then(function(f){return WebAssembly.instantiateStreaming(f,e).then(b,function(g){B("wasm streaming compile failed: "+g);B("falling back to ArrayBuffer instantiation");return c(b)})})})();return{}})(); +var Ma=d.___wasm_call_ctors=function(){return(Ma=d.___wasm_call_ctors=d.asm.C).apply(null,arguments)},ub=d._malloc=function(){return(ub=d._malloc=d.asm.D).apply(null,arguments)},Z=d._free=function(){return(Z=d._free=d.asm.E).apply(null,arguments)},kb=d.___getTypeName=function(){return(kb=d.___getTypeName=d.asm.F).apply(null,arguments)};d.___embind_register_native_and_builtin_types=function(){return(d.___embind_register_native_and_builtin_types=d.asm.G).apply(null,arguments)}; +d.dynCall_iii=function(){return(d.dynCall_iii=d.asm.H).apply(null,arguments)};d.dynCall_vii=function(){return(d.dynCall_vii=d.asm.I).apply(null,arguments)};d.dynCall_vi=function(){return(d.dynCall_vi=d.asm.J).apply(null,arguments)};d.dynCall_viii=function(){return(d.dynCall_viii=d.asm.K).apply(null,arguments)};d.dynCall_viiiiiiii=function(){return(d.dynCall_viiiiiiii=d.asm.L).apply(null,arguments)};d.dynCall_ii=function(){return(d.dynCall_ii=d.asm.M).apply(null,arguments)}; +d.dynCall_viiiiiii=function(){return(d.dynCall_viiiiiii=d.asm.N).apply(null,arguments)};d.dynCall_viiiiiiiii=function(){return(d.dynCall_viiiiiiiii=d.asm.O).apply(null,arguments)};d.dynCall_viiii=function(){return(d.dynCall_viiii=d.asm.P).apply(null,arguments)};d.dynCall_viiiiii=function(){return(d.dynCall_viiiiii=d.asm.Q).apply(null,arguments)};d.dynCall_iidiiii=function(){return(d.dynCall_iidiiii=d.asm.R).apply(null,arguments)};d.dynCall_v=function(){return(d.dynCall_v=d.asm.S).apply(null,arguments)}; +d.dynCall_viiiii=function(){return(d.dynCall_viiiii=d.asm.T).apply(null,arguments)};d.dynCall_iiiiii=function(){return(d.dynCall_iiiiii=d.asm.U).apply(null,arguments)};d.dynCall_iiii=function(){return(d.dynCall_iiii=d.asm.V).apply(null,arguments)};d.dynCall_iiiii=function(){return(d.dynCall_iiiii=d.asm.W).apply(null,arguments)};d.dynCall_jiji=function(){return(d.dynCall_jiji=d.asm.X).apply(null,arguments)};var wb;O=function xb(){wb||yb();wb||(O=xb)}; +function yb(){function a(){if(!wb&&(wb=!0,d.calledRun=!0,!ja)){M(Aa);M(Ba);aa(d);if(d.onRuntimeInitialized)d.onRuntimeInitialized();if(d.postRun)for("function"==typeof d.postRun&&(d.postRun=[d.postRun]);d.postRun.length;){var b=d.postRun.shift();Ca.unshift(b)}M(Ca)}}if(!(0 #include #include - +#include #include "src/wp2/encode.h" + using namespace emscripten; -val encode(std::string image_in, int image_width, int image_height, - WP2::EncoderConfig config) { - WP2Status status; - uint8_t *image_buffer = (uint8_t *)image_in.c_str(); +thread_local const val Uint8Array = val::global("Uint8Array"); + +val encode(std::string image_in, int image_width, int image_height, WP2::EncoderConfig config) { + uint8_t* image_buffer = (uint8_t*)image_in.c_str(); WP2::ArgbBuffer src = WP2::ArgbBuffer(); - status = src.Import(WP2_rgbA_32, // Format. WP2_RGBA_32 is the same but NOT - // premultiplied alpha - image_width, image_height, image_buffer, 4 * image_width); + WP2Status status = + src.Import(WP2_rgbA_32, // Format. WP2_RGBA_32 is the same but NOT premultiplied alpha + image_width, image_height, image_buffer, 4 * image_width); if (status != WP2_STATUS_OK) { return val::null(); } @@ -23,8 +23,7 @@ val encode(std::string image_in, int image_width, int image_height, return val::null(); } - return val(typed_memory_view(memory_writer.size_, memory_writer.mem_)); - // Lol I forgot to add the free here. + return Uint8Array.new_(typed_memory_view(memory_writer.size_, memory_writer.mem_)); } EMSCRIPTEN_BINDINGS(my_module) { diff --git a/codecs/wp2/enc/wp2_enc.d.ts b/codecs/wp2/enc/wp2_enc.d.ts index 04949538..76a2cf11 100644 --- a/codecs/wp2/enc/wp2_enc.d.ts +++ b/codecs/wp2/enc/wp2_enc.d.ts @@ -1,8 +1,7 @@ -import { EncodeOptions } from '../../src/codecs/wp2/encoder-meta'; +import { EncodeOptions } from '../../../src/codecs/wp2/encoder-meta'; interface WP2Module extends EmscriptenWasm.Module { - encode(data: BufferSource, width: number, height: number, options: EncodeOptions): Uint8Array; - free_result(): void; + encode(data: BufferSource, width: number, height: number, options: EncodeOptions): Uint8Array | null; } export default function(opts: EmscriptenWasm.ModuleOpts): Promise; diff --git a/codecs/wp2/enc/wp2_enc.js b/codecs/wp2/enc/wp2_enc.js index e59f9e53..55d8a412 100644 --- a/codecs/wp2/enc/wp2_enc.js +++ b/codecs/wp2/enc/wp2_enc.js @@ -11,51 +11,53 @@ var c;c||(c=typeof wp2_enc !== 'undefined' ? wp2_enc : {});var aa,ba;c.ready=new if(ca)w=v?require("path").dirname(w)+"/":__dirname+"/",y=function(a,b){ea||(ea=require("fs"));fa||(fa=require("path"));a=fa.normalize(a);return ea.readFileSync(a,b?null:"utf8")},B=function(a){a=y(a,!0);a.buffer||(a=new Uint8Array(a));a.buffer||C("Assertion failed: undefined");return a},1=e);)++d;if(16f?e+=String.fromCharCode(f):(f-=65536,e+=String.fromCharCode(55296|f>>10,56320|f&1023))}}else e+=String.fromCharCode(f)}return e} function ma(a,b,d){var e=I;if(0=g){var l=a.charCodeAt(++f);g=65536+((g&1023)<<10)|l&1023}if(127>=g){if(b>=d)break;e[b++]=g}else{if(2047>=g){if(b+1>=d)break;e[b++]=192|g>>6}else{if(65535>=g){if(b+2>=d)break;e[b++]=224|g>>12}else{if(b+3>=d)break;e[b++]=240|g>>18;e[b++]=128|g>>12&63}e[b++]=128|g>>6&63}e[b++]=128|g&63}}e[b]=0}}var na="undefined"!==typeof TextDecoder?new TextDecoder("utf-16le"):void 0; function oa(a,b){var d=a>>1;for(var e=d+b/2;!(d>=e)&&J[d];)++d;d<<=1;if(32>1];if(0==f||d==b/2)return e;++d;e+=String.fromCharCode(f)}}function pa(a,b,d){void 0===d&&(d=2147483647);if(2>d)return 0;d-=2;var e=b;d=d<2*a.length?d/2:a.length;for(var f=0;f>1]=a.charCodeAt(f),b+=2;K[b>>1]=0;return b-e}function qa(a){return 2*a.length} function ra(a,b){for(var d=0,e="";!(d>=b/4);){var f=L[a+4*d>>2];if(0==f)break;++d;65536<=f?(f-=65536,e+=String.fromCharCode(55296|f>>10,56320|f&1023)):e+=String.fromCharCode(f)}return e}function sa(a,b,d){void 0===d&&(d=2147483647);if(4>d)return 0;var e=b;d=e+d-4;for(var f=0;f=g){var l=a.charCodeAt(++f);g=65536+((g&1023)<<10)|l&1023}L[b>>2]=g;b+=4;if(b+4>d)break}L[b>>2]=0;return b-e} function ta(a){for(var b=0,d=0;d=e&&++d;b+=4}return b}var M,ua,I,K,J,L,N,va,wa;function xa(a){M=a;c.HEAP8=ua=new Int8Array(a);c.HEAP16=K=new Int16Array(a);c.HEAP32=L=new Int32Array(a);c.HEAPU8=I=new Uint8Array(a);c.HEAPU16=J=new Uint16Array(a);c.HEAPU32=N=new Uint32Array(a);c.HEAPF32=va=new Float32Array(a);c.HEAPF64=wa=new Float64Array(a)}var ya=c.INITIAL_MEMORY||16777216;c.wasmMemory?F=c.wasmMemory:F=new WebAssembly.Memory({initial:ya/65536,maximum:32768}); -F&&(M=F.buffer);ya=M.byteLength;xa(M);L[35012]=5383088;function za(a){for(;0>2])}var R={},S={},Sa={};function Ta(a){if(void 0===a)return"_unknown";a=a.replace(/[^a-zA-Z0-9_]/g,"$");var b=a.charCodeAt(0);return 48<=b&&57>=b?"_"+a:a}function Ua(a,b){a=Ta(a);return(new Function("body","return function "+a+'() {\n "use strict"; return body.apply(this, arguments);\n};\n'))(b)} function Va(a){var b=Error,d=Ua(a,function(e){this.name=a;this.message=e;e=Error(e).stack;void 0!==e&&(this.stack=this.toString()+"\n"+e.replace(/^Error(:[^\n]*)?\n/,""))});d.prototype=Object.create(b.prototype);d.prototype.constructor=d;d.prototype.toString=function(){return void 0===this.message?this.name:this.name+": "+this.message};return d}var Wa=void 0; function Xa(a,b,d){function e(k){k=d(k);if(k.length!==a.length)throw new Wa("Mismatched type converter count");for(var h=0;h>2])};case 3:return function(d){return this.fromWireType(wa[d>>3])};default:throw new TypeError("Unknown float type: "+a);}}function fb(a){var b=Function;if(!(b instanceof Function))throw new TypeError("new_ called with constructor type "+typeof b+" which is not a function");var d=Ua(b.name||"unknownFunctionName",function(){});d.prototype=b.prototype;d=new d;a=b.apply(d,a);return a instanceof Object?a:d} -function gb(a,b){var d=c;if(void 0===d[a].fa){var e=d[a];d[a]=function(){d[a].fa.hasOwnProperty(arguments.length)||V("Function '"+b+"' called with an invalid number of arguments ("+arguments.length+") - expects one of ("+d[a].fa+")!");return d[a].fa[arguments.length].apply(this,arguments)};d[a].fa=[];d[a].fa[e.la]=e}} -function hb(a,b,d){c.hasOwnProperty(a)?((void 0===d||void 0!==c[a].fa&&void 0!==c[a].fa[d])&&V("Cannot register public name '"+a+"' twice"),gb(a,a),c.hasOwnProperty(d)&&V("Cannot register multiple overloads of a function with the same number of arguments ("+d+")!"),c[a].fa[d]=b):(c[a]=b,void 0!==d&&(c[a].ya=d))}function ib(a,b){for(var d=[],e=0;e>2)+e]);return d} +function gb(a,b){var d=c;if(void 0===d[a].ha){var e=d[a];d[a]=function(){d[a].ha.hasOwnProperty(arguments.length)||V("Function '"+b+"' called with an invalid number of arguments ("+arguments.length+") - expects one of ("+d[a].ha+")!");return d[a].ha[arguments.length].apply(this,arguments)};d[a].ha=[];d[a].ha[e.na]=e}} +function hb(a,b,d){c.hasOwnProperty(a)?((void 0===d||void 0!==c[a].ha&&void 0!==c[a].ha[d])&&V("Cannot register public name '"+a+"' twice"),gb(a,a),c.hasOwnProperty(d)&&V("Cannot register multiple overloads of a function with the same number of arguments ("+d+")!"),c[a].ha[d]=b):(c[a]=b,void 0!==d&&(c[a].Aa=d))}function ib(a,b){for(var d=[],e=0;e>2)+e]);return d} function Y(a,b){a=U(a);var d=c["dynCall_"+a];for(var e=[],f=1;f>1]}:function(e){return J[e>>1]};case 2:return d?function(e){return L[e>>2]}:function(e){return N[e>>2]};default:throw new TypeError("Unknown integer type: "+a);}}var ob=[null,[],[]];Wa=c.InternalError=Va("InternalError"); -for(var pb=Array(256),qb=0;256>qb;++qb)pb[qb]=String.fromCharCode(qb);Za=pb;$a=c.BindingError=Va("BindingError");c.count_emval_handles=function(){for(var a=0,b=5;b>g])},ga:null})},x:function(a,b){b=U(b);T(a,{name:b,fromWireType:function(d){var e=X[d].value;bb(d);return e},toWireType:function(d,e){return cb(e)},argPackAdvance:8,readValueFromPointer:Ra,ga:null})},j:function(a,b,d){d=Ya(d);b=U(b);T(a, -{name:b,fromWireType:function(e){return e},toWireType:function(e,f){if("number"!==typeof f&&"boolean"!==typeof f)throw new TypeError('Cannot convert "'+db(f)+'" to '+this.name);return f},argPackAdvance:8,readValueFromPointer:eb(b,d),ga:null})},q:function(a,b,d,e,f,g){var l=ib(b,d);a=U(a);f=Y(e,f);hb(a,function(){mb("Cannot call "+a+" due to unbound types",l)},b-1);Xa([],l,function(k){var h=[k[0],null].concat(k.slice(1)),m=k=a,n=f,q=h.length;2>q&&V("argTypes array size mismatch! Must at least get return value and 'this' types!"); -for(var x=null!==h[1]&&!1,z=!1,p=1;p>>k}}var h=-1!=b.indexOf("unsigned");T(a,{name:b,fromWireType:g,toWireType:function(m, -n){if("number"!==typeof n&&"boolean"!==typeof n)throw new TypeError('Cannot convert "'+db(n)+'" to '+this.name);if(nf)throw new TypeError('Passing a number "'+db(n)+'" from JS side to C/C++ side to an argument of type "'+b+'", which is outside the valid range ['+e+", "+f+"]!");return h?n>>>0:n|0},argPackAdvance:8,readValueFromPointer:nb(b,l,0!==e),ga:null})},a:function(a,b,d){function e(g){g>>=2;var l=N;return new f(M,l[g+1],l[g])}var f=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array, -Uint32Array,Float32Array,Float64Array][b];d=U(d);T(a,{name:d,fromWireType:e,argPackAdvance:8,readValueFromPointer:e},{ra:!0})},k:function(a,b){b=U(b);var d="std::string"===b;T(a,{name:b,fromWireType:function(e){var f=N[e>>2];if(d)for(var g=e+4,l=0;l<=f;++l){var k=e+4+l;if(l==f||0==I[k]){g=g?H(I,g,k-g):"";if(void 0===h)var h=g;else h+=String.fromCharCode(0),h+=g;g=k+1}}else{h=Array(f);for(l=0;l=q&&(q=65536+((q&1023)<<10)|f.charCodeAt(++n)&1023);127>=q?++m:m=2047>=q?m+2:65535>=q?m+3:m+4}return m}:function(){return f.length})(),k=rb(4+l+1);N[k>>2]=l;if(d&&g)ma(f,k+4,l+1);else if(g)for(g=0;g>2],n=l(),q,x=h+4,z=0;z<=m;++z){var p=h+4+z*b;if(z==m||0==n[p>>k])x=e(x,p-x),void 0=== -q?q=x:(q+=String.fromCharCode(0),q+=x),x=p+b}Z(h);return q},toWireType:function(h,m){"string"!==typeof m&&V("Cannot pass non-string to C++ string type "+d);var n=g(m),q=rb(4+n+b);N[q>>2]=n>>k;f(m,q+4,n+b);null!==h&&h.push(Z,q);return q},argPackAdvance:8,readValueFromPointer:Ra,ga:function(h){Z(h)}})},E:function(a,b,d,e,f,g){Pa[a]={name:U(b),sa:Y(d,e),ta:Y(f,g),ka:[]}},e:function(a,b,d,e,f,g,l,k,h,m){Pa[a].ka.push({ma:U(b),qa:d,oa:Y(e,f),pa:g,va:l,ua:Y(k,h),wa:m})},z:function(a,b){b=U(b);T(a,{xa:!0, -name:b,argPackAdvance:0,fromWireType:function(){},toWireType:function(){}})},o:bb,p:function(a){4>>=0;var b=I.length;if(2147483648=d;d*=2){var e=b*(1+.2/d);e=Math.min(e,a+100663296);e=Math.max(16777216,a,e);0>>16);xa(F.buffer);var f=1;break a}catch(g){}f=void 0}if(f)return!0}return!1},v:function(){return 0},r:function(){},i:function(a,b,d,e){for(var f=0,g=0;g>2],k=L[b+(8*g+4)>>2],h=0;h>2]=f;return 0},memory:F,l:function(){return 0},n:function(){return 0},m:function(){},D:function(){return 6},C:function(){},g:function(a){a=+a;return 0<=a?+Ga(a+.5):+Fa(a-.5)},c:function(a){a= -+a;return 0<=a?+Ga(a+.5):+Fa(a-.5)},s:function(){},table:ja}; -(function(){function a(f){c.asm=f.exports;O--;c.monitorRunDependencies&&c.monitorRunDependencies(O);0==O&&(null!==Ha&&(clearInterval(Ha),Ha=null),P&&(f=P,P=null,f()))}function b(f){a(f.instance)}function d(f){return Ma().then(function(g){return WebAssembly.instantiate(g,e)}).then(f,function(g){D("failed to asynchronously prepare wasm: "+g);C(g)})}var e={a:sb};O++;c.monitorRunDependencies&&c.monitorRunDependencies(O);if(c.instantiateWasm)try{return c.instantiateWasm(e,a)}catch(f){return D("Module.instantiateWasm callback failed with error: "+ +function mb(a,b){function d(g){f[g]||S[g]||(Sa[g]?Sa[g].forEach(d):(e.push(g),f[g]=!0))}var e=[],f={};b.forEach(d);throw new jb(a+": "+e.map(kb).join([", "]));}function nb(a,b,d){switch(b){case 0:return d?function(e){return ua[e]}:function(e){return I[e]};case 1:return d?function(e){return K[e>>1]}:function(e){return J[e>>1]};case 2:return d?function(e){return L[e>>2]}:function(e){return N[e>>2]};default:throw new TypeError("Unknown integer type: "+a);}}var ob={}; +function pb(){return"object"===typeof globalThis?globalThis:Function("return this")()}function qb(a,b){var d=S[a];void 0===d&&V(b+" has unknown type "+kb(a));return d}var rb={},sb=[null,[],[]];Wa=c.InternalError=Va("InternalError");for(var tb=Array(256),ub=0;256>ub;++ub)tb[ub]=String.fromCharCode(ub);Za=tb;$a=c.BindingError=Va("BindingError");c.count_emval_handles=function(){for(var a=0,b=5;b>g])},ia:null})},x:function(a,b){b=U(b);T(a,{name:b,fromWireType:function(d){var e=X[d].value;bb(d);return e},toWireType:function(d,e){return cb(e)},argPackAdvance:8,readValueFromPointer:Ra,ia:null})},j:function(a,b,d){d=Ya(d); +b=U(b);T(a,{name:b,fromWireType:function(e){return e},toWireType:function(e,f){if("number"!==typeof f&&"boolean"!==typeof f)throw new TypeError('Cannot convert "'+db(f)+'" to '+this.name);return f},argPackAdvance:8,readValueFromPointer:eb(b,d),ia:null})},o:function(a,b,d,e,f,g){var l=ib(b,d);a=U(a);f=Y(e,f);hb(a,function(){mb("Cannot call "+a+" due to unbound types",l)},b-1);Xa([],l,function(k){var h=[k[0],null].concat(k.slice(1)),m=k=a,n=f,q=h.length;2>q&&V("argTypes array size mismatch! Must at least get return value and 'this' types!"); +for(var x=null!==h[1]&&!1,z=!1,p=1;p>>k}}var h=-1!=b.indexOf("unsigned");T(a,{name:b,fromWireType:g,toWireType:function(m, +n){if("number"!==typeof n&&"boolean"!==typeof n)throw new TypeError('Cannot convert "'+db(n)+'" to '+this.name);if(nf)throw new TypeError('Passing a number "'+db(n)+'" from JS side to C/C++ side to an argument of type "'+b+'", which is outside the valid range ['+e+", "+f+"]!");return h?n>>>0:n|0},argPackAdvance:8,readValueFromPointer:nb(b,l,0!==e),ia:null})},a:function(a,b,d){function e(g){g>>=2;var l=N;return new f(M,l[g+1],l[g])}var f=[Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array, +Uint32Array,Float32Array,Float64Array][b];d=U(d);T(a,{name:d,fromWireType:e,argPackAdvance:8,readValueFromPointer:e},{ta:!0})},l:function(a,b){b=U(b);var d="std::string"===b;T(a,{name:b,fromWireType:function(e){var f=N[e>>2];if(d)for(var g=e+4,l=0;l<=f;++l){var k=e+4+l;if(l==f||0==I[k]){g=g?H(I,g,k-g):"";if(void 0===h)var h=g;else h+=String.fromCharCode(0),h+=g;g=k+1}}else{h=Array(f);for(l=0;l=q&&(q=65536+((q&1023)<<10)|f.charCodeAt(++n)&1023);127>=q?++m:m=2047>=q?m+2:65535>=q?m+3:m+4}return m}:function(){return f.length})(),k=vb(4+l+1);N[k>>2]=l;if(d&&g)ma(f,k+4,l+1);else if(g)for(g=0;g>2],n=l(),q,x=h+4,z=0;z<=m;++z){var p=h+4+z*b;if(z==m||0==n[p>>k])x=e(x,p-x),void 0=== +q?q=x:(q+=String.fromCharCode(0),q+=x),x=p+b}Z(h);return q},toWireType:function(h,m){"string"!==typeof m&&V("Cannot pass non-string to C++ string type "+d);var n=g(m),q=vb(4+n+b);N[q>>2]=n>>k;f(m,q+4,n+b);null!==h&&h.push(Z,q);return q},argPackAdvance:8,readValueFromPointer:Ra,ia:function(h){Z(h)}})},r:function(a,b,d,e,f,g){Pa[a]={name:U(b),ua:Y(d,e),va:Y(f,g),la:[]}},e:function(a,b,d,e,f,g,l,k,h,m){Pa[a].la.push({oa:U(b),sa:d,qa:Y(e,f),ra:g,xa:l,wa:Y(k,h),ya:m})},z:function(a,b){b=U(b);T(a,{za:!0, +name:b,argPackAdvance:0,fromWireType:function(){},toWireType:function(){}})},k:bb,G:function(a){if(0===a)return cb(pb());var b=ob[a];a=void 0===b?U(a):b;return cb(pb()[a])},n:function(a){4>> 2) + "+ +g+'], "parameter '+g+'");\nvar arg'+g+" = argType"+g+".readValueFromPointer(args);\nargs += argType"+g+"['argPackAdvance'];\n";f=(new Function("requireRegisteredType","Module","__emval_register",l+("var obj = new constructor("+f+");\nreturn __emval_register(obj);\n}\n")))(qb,c,cb);rb[b]=f}return f(a,d,e)},h:function(){C()},v:function(a,b,d){I.copyWithin(a,b,b+d)},d:function(a){a>>>=0;var b=I.length;if(2147483648=d;d*=2){var e=b*(1+.2/d);e=Math.min(e,a+100663296);e=Math.max(16777216, +a,e);0>>16);xa(F.buffer);var f=1;break a}catch(g){}f=void 0}if(f)return!0}return!1},w:function(){return 0},s:function(){},i:function(a,b,d,e){for(var f=0,g=0;g>2],k=L[b+(8*g+4)>>2],h=0;h>2]=f;return 0},memory:F,m:function(){return 0},F:function(){return 0},E:function(){},D:function(){return 6}, +C:function(){},g:function(a){a=+a;return 0<=a?+Ga(a+.5):+Fa(a-.5)},c:function(a){a=+a;return 0<=a?+Ga(a+.5):+Fa(a-.5)},t:function(){},table:ia}; +(function(){function a(f){c.asm=f.exports;O--;c.monitorRunDependencies&&c.monitorRunDependencies(O);0==O&&(null!==Ha&&(clearInterval(Ha),Ha=null),P&&(f=P,P=null,f()))}function b(f){a(f.instance)}function d(f){return Ma().then(function(g){return WebAssembly.instantiate(g,e)}).then(f,function(g){D("failed to asynchronously prepare wasm: "+g);C(g)})}var e={a:wb};O++;c.monitorRunDependencies&&c.monitorRunDependencies(O);if(c.instantiateWasm)try{return c.instantiateWasm(e,a)}catch(f){return D("Module.instantiateWasm callback failed with error: "+ f),!1}(function(){if(E||"function"!==typeof WebAssembly.instantiateStreaming||Ja()||Ia("file://")||"function"!==typeof fetch)return d(b);fetch(Q,{credentials:"same-origin"}).then(function(f){return WebAssembly.instantiateStreaming(f,e).then(b,function(g){D("wasm streaming compile failed: "+g);D("falling back to ArrayBuffer instantiation");return d(b)})})})();return{}})(); -var Na=c.___wasm_call_ctors=function(){return(Na=c.___wasm_call_ctors=c.asm.G).apply(null,arguments)},rb=c._malloc=function(){return(rb=c._malloc=c.asm.H).apply(null,arguments)},Z=c._free=function(){return(Z=c._free=c.asm.I).apply(null,arguments)},lb=c.___getTypeName=function(){return(lb=c.___getTypeName=c.asm.J).apply(null,arguments)};c.___embind_register_native_and_builtin_types=function(){return(c.___embind_register_native_and_builtin_types=c.asm.K).apply(null,arguments)}; -c.dynCall_i=function(){return(c.dynCall_i=c.asm.L).apply(null,arguments)};c.dynCall_vi=function(){return(c.dynCall_vi=c.asm.M).apply(null,arguments)};c.dynCall_fii=function(){return(c.dynCall_fii=c.asm.N).apply(null,arguments)};c.dynCall_viif=function(){return(c.dynCall_viif=c.asm.O).apply(null,arguments)};c.dynCall_iii=function(){return(c.dynCall_iii=c.asm.P).apply(null,arguments)};c.dynCall_viii=function(){return(c.dynCall_viii=c.asm.Q).apply(null,arguments)}; -c.dynCall_iiiiii=function(){return(c.dynCall_iiiiii=c.asm.R).apply(null,arguments)};c.dynCall_viiiii=function(){return(c.dynCall_viiiii=c.asm.S).apply(null,arguments)};c.dynCall_vii=function(){return(c.dynCall_vii=c.asm.T).apply(null,arguments)};c.dynCall_viiiiiii=function(){return(c.dynCall_viiiiiii=c.asm.U).apply(null,arguments)};c.dynCall_viiiiiiiii=function(){return(c.dynCall_viiiiiiiii=c.asm.V).apply(null,arguments)};c.dynCall_ii=function(){return(c.dynCall_ii=c.asm.W).apply(null,arguments)}; -c.dynCall_viiiiiiii=function(){return(c.dynCall_viiiiiiii=c.asm.X).apply(null,arguments)};c.dynCall_viiii=function(){return(c.dynCall_viiii=c.asm.Y).apply(null,arguments)};c.dynCall_viiiiii=function(){return(c.dynCall_viiiiii=c.asm.Z).apply(null,arguments)};c.dynCall_iidiiii=function(){return(c.dynCall_iidiiii=c.asm._).apply(null,arguments)};c.dynCall_iiiii=function(){return(c.dynCall_iiiii=c.asm.$).apply(null,arguments)};c.dynCall_v=function(){return(c.dynCall_v=c.asm.aa).apply(null,arguments)}; -c.dynCall_fi=function(){return(c.dynCall_fi=c.asm.ba).apply(null,arguments)};c.dynCall_iiii=function(){return(c.dynCall_iiii=c.asm.ca).apply(null,arguments)};c.dynCall_iiiiiiiiii=function(){return(c.dynCall_iiiiiiiiii=c.asm.da).apply(null,arguments)};c.dynCall_jiji=function(){return(c.dynCall_jiji=c.asm.ea).apply(null,arguments)};var tb;P=function ub(){tb||vb();tb||(P=ub)}; -function vb(){function a(){if(!tb&&(tb=!0,c.calledRun=!0,!ka)){za(Ba);za(Ca);aa(c);if(c.onRuntimeInitialized)c.onRuntimeInitialized();if(c.postRun)for("function"==typeof c.postRun&&(c.postRun=[c.postRun]);c.postRun.length;){var b=c.postRun.shift();Da.unshift(b)}za(Da)}}if(!(0 { if (!emscriptenModule) emscriptenModule = initEmscriptenModule(wp2_dec, wasmUrl); const module = await emscriptenModule; - const rawImage = module.decode(data); - const result = new ImageData( - new Uint8ClampedArray(rawImage.buffer), - rawImage.width, - rawImage.height, - ); - - module.free_result(); + const result = module.decode(data); + if (!result) { + throw new Error('Decoding error'); + } return result; } diff --git a/src/codecs/wp2/encoder.ts b/src/codecs/wp2/encoder.ts index 4e8538a3..2a10dada 100644 --- a/src/codecs/wp2/encoder.ts +++ b/src/codecs/wp2/encoder.ts @@ -9,8 +9,11 @@ export async function encode(data: ImageData, options: EncodeOptions): Promise