/** * @monogrid/gainmap-js v3.4.0 * With ❤️, by MONOGRID */ !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).libultrahdr={})}(this,function(t){"use strict";const e=216,n=225,a=226,r=218,i={SIGNATURE:new Uint8Array([77,80,70,0]),BIG_ENDIAN:new Uint8Array([77,77]),TIFF_MAGIC:42,NUM_PICTURES:2,TAG_COUNT:3,TAG_SIZE:12,MP_ENTRY_SIZE:16},s=45056,p=45057,o=45058,h=7,g=4,d=0,u=536870912,c=new Uint8Array([48,49,48,48]);function m(){return i.SIGNATURE.length+2+2+4+2+i.TAG_COUNT*i.TAG_SIZE+4+i.NUM_PICTURES*i.MP_ENTRY_SIZE}const f="Primary",l="GainMap",M="image/jpeg";function U(t){return String(t).replace(/&/g,"&").replace(//g,">").replace(/"/g,""").replace(/'/g,"'")}function x(t,e,n,a){const r=new DataView(t.buffer,t.byteOffset,t.byteLength);if(r.setUint8(e++,255),r.setUint8(e++,n),a&&a.length>0){const n=a.length+2;r.setUint16(e,n,!1),e+=2,t.set(a,e),e+=a.length}return e}function y(t){const{sdr:y,gainMap:w,metadata:T,exif:E,icc:I}=t;if("image/jpeg"!==y.mimeType)throw new Error("SDR image must be JPEG format");if("image/jpeg"!==w.mimeType)throw new Error("Gain map image must be JPEG format");const A=function(t){const a=new DataView(t.buffer,t.byteOffset,t.byteLength);if(255!==a.getUint8(0)||a.getUint8(1)!==e)return null;let i=2;const s="Exif\0\0";for(;i=t.length||t[n+e]!==s.charCodeAt(e)){r=!1;break}if(r){const a=e-2;return{data:t.slice(n,n+a),pos:i,size:e+2}}}i+=2+a.getUint16(i+2,!1)}return null}(y.data);if(A&&E)throw new Error("Primary image already contains EXIF data, cannot add external EXIF");let b=y.data,C=E;A&&(b=function(t,e,n){const a=t.length-n,r=new Uint8Array(a);return r.set(t.subarray(0,e),0),r.set(t.subarray(e+n),e),r}(y.data,A.pos,A.size),C=A.data);const S=function(t){const e=[],n=t.hdrCapacityMin,a=t.hdrCapacityMax,r=t=>Array.isArray(t)?t.reduce((t,e)=>t+e,0)/t.length:t,i=r(t.gainMapMin),s=r(t.gainMapMax),p=r(t.gamma),o=r(t.offsetSdr),h=r(t.offsetHdr);return e.push(''),e.push(''),e.push(' '),e.push(" '),e.push(" "),e.push(""),e.push(''),e.join("\n")}(T),R=(new TextEncoder).encode(S),G=(new TextEncoder).encode("http://ns.adobe.com/xap/1.0/\0"),D=6+G.length+R.length+(w.data.length-2),N=function(t,e){const n=[];return n.push(''),n.push(''),n.push(' '),n.push(" '),n.push(" "),n.push(" "),n.push(' '),n.push(" `),n.push(" "),n.push(' '),n.push(" `),n.push(" "),n.push(" "),n.push(" "),n.push(" "),n.push(" "),n.push(""),n.push(''),n.join("\n")}(D,T),_=(new TextEncoder).encode(N),P=new Uint8Array(G.length+_.length);P.set(G,0),P.set(_,G.length);const $=m();let F=2;C&&(F+=4+C.length),F+=4+P.length,I&&(F+=4+I.length),F+=4+$,F+=b.length-2,F+=D;const j=F-D,k=function(t,e,n,a){const r=m(),f=new ArrayBuffer(r),l=new DataView(f),M=new Uint8Array(f);let U=0;M.set(i.SIGNATURE,U),U+=i.SIGNATURE.length,M.set(i.BIG_ENDIAN,U);const x=!1;U+=2,l.setUint16(U,i.TIFF_MAGIC,x),U+=2,l.setUint32(U,8,x),U+=4,l.setUint16(U,i.TAG_COUNT,x),U+=2,l.setUint16(U,s,x),U+=2,l.setUint16(U,h,x),U+=2,l.setUint32(U,c.length,x),U+=4,M.set(c,U),U+=4,l.setUint16(U,p,x),U+=2,l.setUint16(U,g,x),U+=2,l.setUint32(U,1,x),U+=4,l.setUint32(U,i.NUM_PICTURES,x),U+=4,l.setUint16(U,o,x),U+=2,l.setUint16(U,h,x),U+=2,l.setUint32(U,i.MP_ENTRY_SIZE*i.NUM_PICTURES,x),U+=4;const y=U-i.SIGNATURE.length+4+4;return l.setUint32(U,y,x),U+=4,l.setUint32(U,0,x),U+=4,l.setUint32(U,d|u,x),U+=4,l.setUint32(U,t,x),U+=4,l.setUint32(U,e,x),U+=4,l.setUint16(U,0,x),U+=2,l.setUint16(U,0,x),U+=2,l.setUint32(U,d,x),U+=4,l.setUint32(U,n,x),U+=4,l.setUint32(U,a,x),U+=4,l.setUint16(U,0,x),U+=2,l.setUint16(U,0,x),M}(j,0,D,j-(2+(C?4+C.length:0)+2+2+P.length+(I?4+I.length:0)+2+2+4)),H=new Uint8Array(F);let z=0;z=x(H,z,e),C&&(z=x(H,z,n,C)),z=x(H,z,n,P),I&&(z=x(H,z,a,I)),z=x(H,z,a,k),H.set(b.subarray(2),z),z+=b.length-2,z=x(H,z,e);const O=new Uint8Array(G.length+R.length);return O.set(G,0),O.set(R,G.length),z=x(H,z,n,O),H.set(w.data.subarray(2),z),H}t.encodeJPEGMetadata=t=>{if("image/jpeg"!==t.sdr.mimeType)throw new Error("This function expects an SDR image compressed in jpeg");if("image/jpeg"!==t.gainMap.mimeType)throw new Error("This function expects a GainMap image compressed in jpeg");const e={version:"1.0",gainMapMin:t.gainMapMin,gainMapMax:t.gainMapMax,gamma:t.gamma,offsetSdr:t.offsetSdr,offsetHdr:t.offsetHdr,hdrCapacityMin:t.hdrCapacityMin,hdrCapacityMax:t.hdrCapacityMax,minContentBoost:Array.isArray(t.gainMapMin)?Math.pow(2,t.gainMapMin.reduce((t,e)=>t+e,0)/t.gainMapMin.length):Math.pow(2,t.gainMapMin),maxContentBoost:Array.isArray(t.gainMapMax)?Math.pow(2,t.gainMapMax.reduce((t,e)=>t+e,0)/t.gainMapMax.length):Math.pow(2,t.gainMapMax)};return y({sdr:t.sdr,gainMap:t.gainMap,metadata:e})}});