You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1 line
2.1 KiB
JavaScript

(()=>{"use strict";var t=["0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","#","$","%","*","+",",","-",".",":",";","=","?","@","[","]","^","_","{","|","}","~"],a=(a,e)=>{var r="";for(let h=1;h<=e;h++){let o=Math.floor(a)/Math.pow(83,e-h)%83;r+=t[Math.floor(o)]}return r},e=t=>{let a=t/255;return a<=.04045?a/12.92:Math.pow((a+.055)/1.055,2.4)},r=t=>{let a=Math.max(0,Math.min(1,t));return a<=.0031308?Math.trunc(12.92*a*255+.5):Math.trunc(255*(1.055*Math.pow(a,.4166666666666667)-.055)+.5)},h=(t,a)=>(t=>t<0?-1:1)(t)*Math.pow(Math.abs(t),a),o=class extends Error{constructor(t){super(t),this.name="ValidationError",this.message=t}},s=(t,a,r,h)=>{let o=0,s=0,l=0,n=4*a;for(let M=0;M<a;M++){let a=4*M;for(let i=0;i<r;i++){let r=a+i*n,m=h(M,i);o+=m*e(t[r]),s+=m*e(t[r+1]),l+=m*e(t[r+2])}}let M=1/(a*r);return[o*M,s*M,l*M]},l=(t,e,l,n,M)=>{if(n<1||n>9||M<1||M>9)throw new o("BlurHash must have between 1 and 9 components");if(e*l*4!==t.length)throw new o("Width and height must match the pixels array");let i=[];for(let a=0;a<M;a++)for(let r=0;r<n;r++){let h=0==r&&0==a?1:2,o=s(t,e,l,((t,o)=>h*Math.cos(Math.PI*r*t/e)*Math.cos(Math.PI*a*o/l)));i.push(o)}let m,c=i[0],f=i.slice(1),u="";if(u+=a(n-1+9*(M-1),1),f.length>0){let t=Math.max(...f.map((t=>Math.max(...t)))),e=Math.floor(Math.max(0,Math.min(82,Math.floor(166*t-.5))));m=(e+1)/166,u+=a(e,1)}else m=1,u+=a(0,1);return u+=a((t=>(r(t[0])<<16)+(r(t[1])<<8)+r(t[2]))(c),4),f.forEach((t=>{u+=a(((t,a)=>19*Math.floor(Math.max(0,Math.min(18,Math.floor(9*h(t[0]/a,.5)+9.5))))*19+19*Math.floor(Math.max(0,Math.min(18,Math.floor(9*h(t[1]/a,.5)+9.5))))+Math.floor(Math.max(0,Math.min(18,Math.floor(9*h(t[2]/a,.5)+9.5)))))(t,m),2)})),u};const n=function(t){const{image:a,width:e,height:r,componentX:h,componentY:o}=t.data;if(a)try{const t=l(a,e,r,h,o);postMessage({type:"success",blurHash:t})}catch(t){postMessage({type:"error",error:t})}};self.addEventListener("message",n)})();