UNPKG

latentimg

Version:

Lightweight browser-side encoder/decoder for images using ONNX

3 lines (2 loc) 2.9 kB
!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?e(exports):"function"==typeof define&&define.amd?define(["exports"],e):e((t="undefined"!=typeof globalThis?globalThis:t||self).Latentimg={})}(this,(function(t){"use strict";function e(t,e,n,o){return new(n||(n=Promise))((function(i,r){function a(t){try{d(o.next(t))}catch(t){r(t)}}function c(t){try{d(o.throw(t))}catch(t){r(t)}}function d(t){var e;t.done?i(t.value):(e=t.value,e instanceof n?e:new n((function(t){t(e)}))).then(a,c)}d((o=o.apply(t,e||[])).next())}))}"function"==typeof SuppressedError&&SuppressedError;let n=!1,o=null;function i(){return e(this,void 0,void 0,(function*(){if(n)return;if(o=yield new Function('return import("https://cdn.jsdelivr.net/npm/onnxruntime-web@1.21.0/dist/ort.bundle.min.mjs")')(),!o)throw new Error("ORT could not be loaded");o.env.wasm.numThreads=1,n=!0,console.log("🧠 ORT init complete (esm)")}))}function r(){if(!o)throw new Error("ORT not initialized. Call ensureOrtInitialized() first.");return o}function a(t){return e(this,void 0,void 0,(function*(){const e=r();return new Promise((n=>{const o=new Image;o.crossOrigin="anonymous",o.onload=()=>{const t=1080,i=document.createElement("canvas");i.width=t,i.height=t;const r=i.getContext("2d");r.fillStyle="black",r.fillRect(0,0,t,t);const a=Math.min(t/o.width,t/o.height),c=o.width*a,d=o.height*a,u=(t-c)/2,s=(t-d)/2;r.drawImage(o,u,s,c,d);const l=r.getImageData(0,0,t,t).data,f=new Float32Array(3499200);for(let t=0;t<1166400;t++)f[t]=l[4*t]/255,f[t+1166400]=l[4*t+1]/255,f[t+2332800]=l[4*t+2]/255;n(new e.Tensor("float32",f,[1,3,t,t]))},o.src=t}))}))}function c(t){return e(this,void 0,void 0,(function*(){r();const[e,n,o,i]=t.dims,a=t.data,c=document.createElement("canvas");c.width=i,c.height=o;const d=c.getContext("2d"),u=d.createImageData(i,o);for(let t=0;t<o*i;t++){let e=255*a[t],n=255*a[t+o*i],r=255*a[t+2*o*i];e=Math.max(0,Math.min(255,e)),n=Math.max(0,Math.min(255,n)),r=Math.max(0,Math.min(255,r)),u.data[4*t+0]=e,u.data[4*t+1]=n,u.data[4*t+2]=r,u.data[4*t+3]=255}return d.putImageData(u,0,0),c.toDataURL()}))}function d(t){return new Float32Array(t.data)}t.decodeLatent=function(t,n){return e(this,void 0,void 0,(function*(){const e=r(),o=new e.Tensor("float32",t,[1,t.length]),i=(null==n?void 0:n.decoderUrl)||"https://huggingface.co/devcwPark/latentimg-cae/resolve/main/decoder.quant.onnx",a=yield e.InferenceSession.create(i),d={latent:o};return c((yield a.run(d)).recon)}))},t.encodeImage=function(t,n){return e(this,void 0,void 0,(function*(){yield i();const e=r(),o=yield a(t),c=(null==n?void 0:n.encoderUrl)||"https://huggingface.co/devcwPark/latentimg-cae/resolve/main/encoder.quant.onnx",u=yield e.InferenceSession.create(c),s={input:o};return d((yield u.run(s)).latent)}))},t.ensureOrtInitialized=i,t.getOrt=r,t.imageToTensor=a,t.tensorToImage=c,t.tensorToLatent=d})); //# sourceMappingURL=latentimg.umd.js.map