6 lines
6.6 KiB
JavaScript
6 lines
6.6 KiB
JavaScript
|
|
/**
|
|||
|
|
* @monogrid/gainmap-js v3.4.0
|
|||
|
|
* With ❤️, by MONOGRID <gainmap@monogrid.com>
|
|||
|
|
*/
|
|||
|
|
!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,""").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-1&&255===a.getUint8(i);){const e=a.getUint8(i+1);if(e===r)break;if(e===n){const e=a.getUint16(i+2,!1),n=i+4;let r=!0;for(let e=0;e<6;e++)if(n+e>=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('<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>'),e.push('<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.1.2">'),e.push(' <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'),e.push(" <rdf:Description"),e.push(' xmlns:hdrgm="http://ns.adobe.com/hdr-gain-map/1.0/"'),e.push(` hdrgm:Version="${U(t.version)}"`),e.push(` hdrgm:GainMapMin="${U(i)}"`),e.push(` hdrgm:GainMapMax="${U(s)}"`),e.push(` hdrgm:Gamma="${U(p)}"`),e.push(` hdrgm:OffsetSDR="${U(o)}"`),e.push(` hdrgm:OffsetHDR="${U(h)}"`),e.push(` hdrgm:HDRCapacityMin="${U(n)}"`),e.push(` hdrgm:HDRCapacityMax="${U(a)}"`),e.push(' hdrgm:BaseRenditionIsHDR="False"'),e.push(' rdf:about=""/>'),e.push(" </rdf:RDF>"),e.push("</x:xmpmeta>"),e.push('<?xpacket end="w"?>'),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('<?xpacket begin="" id="W5M0MpCehiHzreSzNTczkc9d"?>'),n.push('<x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 5.1.2">'),n.push(' <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">'),n.push(" <rdf:Description"),n.push(' xmlns:Container="http://ns.google.com/photos/1.0/container/"'),n.push(' xmlns:Item="http://ns.google.com/photos/1.0/container/item/"'),n.push(' xmlns:hdrgm="http://ns.adobe.com/hdr-gain-map/1.0/"'),n.push(` hdrgm:Version="${U(e.version)}"`),n.push(' rdf:about="">'),n.push(" <Container:Directory>"),n.push(" <rdf:Seq>"),n.push(' <rdf:li rdf:parseType="Resource">'),n.push(" <Container:Item"),n.push(` Item:Semantic="${f}"`),n.push(` Item:Mime="${M}"/>`),n.push(" </rdf:li>"),n.push(' <rdf:li rdf:parseType="Resource">'),n.push(" <Container:Item"),n
|