@atlas-viewer/iiif-image-api
Version:
A series of helpers for working with the IIIF Image API.
2 lines (1 loc) • 24.7 kB
JavaScript
var Ce=Object.defineProperty;var Le=(i,e,t)=>e in i?Ce(i,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):i[e]=t;var w=(i,e,t)=>(Le(i,typeof e!="symbol"?e+"":e,t),t);function u(i){return i.endsWith("info.json")?i:i.endsWith("/")?`${i}info.json`:`${i}/info.json`}var Oe="http://library.stanford.edu/iiif/image-api/compliance.html#level0",j="http://library.stanford.edu/iiif/image-api/compliance.html#level1",T="http://library.stanford.edu/iiif/image-api/compliance.html#level2",Pe="http://library.stanford.edu/iiif/image-api/conformance.html#level0",$="http://library.stanford.edu/iiif/image-api/conformance.html#level1",B="http://library.stanford.edu/iiif/image-api/conformance.html#level2",Me="http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level0",N="http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level1",k="http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level2",We="http://library.stanford.edu/iiif/image-api/1.1/conformance.html#level0",G="http://library.stanford.edu/iiif/image-api/1.1/conformance.html#level1",H="http://library.stanford.edu/iiif/image-api/1.1/conformance.html#level2",je="http://iiif.io/api/image/1/level0.json",Te="http://iiif.io/api/image/1/profiles/level0.json",U="http://iiif.io/api/image/1/level1.json",V="http://iiif.io/api/image/1/profiles/level1.json",Q="http://iiif.io/api/image/1/level2.json",D="http://iiif.io/api/image/1/profiles/level2.json",$e="http://iiif.io/api/image/2/level0.json",Be="http://iiif.io/api/image/2/profiles/level0.json",J="http://iiif.io/api/image/2/level1.json",Z="http://iiif.io/api/image/2/profiles/level1.json",K="http://iiif.io/api/image/2/level2.json",X="http://iiif.io/api/image/2/profiles/level2.json",Ne="level0",Y="level1",q="level2",ke="http://iiif.io/api/image/2/level0",ee="http://iiif.io/api/image/2/level1",ie="http://iiif.io/api/image/2/level2",Ge="https://iiif.io/api/image/1.1/compliance/#level0",te="https://iiif.io/api/image/1.1/compliance/#level1",re="https://iiif.io/api/image/1.1/compliance/#level2",R=[ie,T,B,k,H,Q,D,K,X,q,re],_=[...R,ee,j,$,N,G,U,V,J,Z,Y,te],C=[ke,ee,ie,Oe,j,T,Pe,$,B,Me,N,k,We,G,H,je,Te,U,V,Q,D,$e,Be,J,Z,K,X,Ne,Y,q,Ge,te,re],Ve=C,oe={extraFormats:["jpg"],extraQualities:["default"],extraFeatures:["sizeByWhListed"]},ae={extraFormats:["jpg"],extraQualities:["default"],extraFeatures:["baseUriRedirect","cors","jsonldMediaType","regionByPx","regionSquare","sizeByWhListed","sizeByH","sizeByW","sizeByWh"]},ne={extraFormats:["jpg","png"],extraQualities:["default"],extraFeatures:["baseUriRedirect","cors","jsonldMediaType","regionByPct","regionByPx","regionSquare","rotationBy90s","sizeByWhListed","sizeByConfinedWh","sizeByH","sizeByPct","sizeByW","sizeByWh"]},L=["baseUriRedirect","canonicalLinkHeader","cors","jsonldMediaType","mirroring","profileLinkHeader","regionByPct","regionByPx","regionSquare","rotationArbitrary","rotationBy90s","sizeByConfinedWh","sizeByH","sizeByPct","sizeByW","sizeByWh","sizeUpscaling","sizeByWhListed","sizeByDistortedWh","sizeByForcedWh"];function se(i){return R.indexOf(i)!==-1?ne:_.indexOf(i)!==-1?ae:oe}function z(i){let e=i?Array.isArray(i.profile)?i.profile:[i.profile]:[],t={extraQualities:[],extraFormats:[],extraFeatures:[]};for(let r of e)if(typeof r=="string"&&(r=se(r)),!!r){if(r.formats)for(let o of r.formats)t.extraFormats.indexOf(o)===-1&&t.extraFormats.push(o);if(r.qualities)for(let o of r.qualities)t.extraQualities.indexOf(o)===-1&&t.extraQualities.push(o);if(r.supports)for(let o of r.supports)t.extraFeatures.indexOf(o)===-1&&t.extraFeatures.push(o);if(r.maxHeight&&(t.maxHeight=r.maxHeight),r.maxWidth&&(t.maxWidth=r.maxWidth),r.maxArea&&(t.maxArea=r.maxArea),r.extraFormats)for(let o of r.extraFormats)t.extraFormats.indexOf(o)===-1&&t.extraFormats.push(o);if(r.extraQualities)for(let o of r.extraQualities)t.extraQualities.indexOf(o)===-1&&t.extraQualities.push(o);if(r.extraFeatures)for(let o of r.extraFeatures)t.extraFeatures.indexOf(o)===-1&&t.extraFeatures.push(o);r.maxHeight&&(t.maxHeight=r.maxHeight),r.maxWidth&&(t.maxWidth=r.maxWidth),r.maxArea&&(t.maxArea=r.maxArea)}if(i.extraFormats)for(let r of i.extraFormats)t.extraFormats.indexOf(r)===-1&&t.extraFormats.push(r);if(i.extraFeatures)for(let r of i.extraFeatures)t.extraFeatures.indexOf(r)===-1&&t.extraFeatures.push(r);if(i.extraQualities)for(let r of i.extraQualities)t.extraQualities.indexOf(r)===-1&&t.extraQualities.push(r);return t}function fe(i){try{if(i==="full")return{full:!0};if(i==="square")return{square:!0};let e=i.startsWith("pct:"),r=i.substr(e?4:0).split(",").map(o=>parseFloat(o));return{x:r[0],y:r[1],w:r[2],h:r[3],percent:e}}catch{throw new Error("Expected 'full', 'square' or 'x,y,w,h'. Found "+i)}}function me(i){let e={upscaled:!1,max:!1,confined:!1};if(i[0]==="^"&&(e.upscaled=!0,i=i.slice(1)),i==="max"||i==="full")return e.max=!0,e.serialiseAsFull=i==="full",e;if(i[0]==="!"&&(e.confined=!0,i=i.slice(1)),i[0]==="p")return e.percentScale=parseFloat(i.slice(4)),e;let t=i.split(",").map(r=>r.trim());return t.length&&typeof t[0]<"u"&&(t[0]!==""&&(e.width=parseInt(t[0],10)),typeof t[1]<"u"&&t[1]!==""?(e.height=parseInt(t[1],10),e.version=2):e.version=3),e}function le(i){let e={angle:0};if(i[0]==="!"&&(e.mirror=!0,i=i.substr(1)),e.angle=parseFloat(i)%360,Number.isNaN(e.angle))throw new Error(`Invalid rotation ${i}`);return e}function pe(i,e=""){let t=i.match(/^(([a-zA-Z]+):\/\/([^/]+))?((.*)+)/);if(!t)throw new Error(`Invalid or unknown input ${i}`);let r=t[2],o=t[3],a=t[4];if(a[0]==="/"&&(a=a.substring(1)),e.length>0){if(e[0]==="/"&&(e=e.substring(1)),e!==a.substring(0,e.length))throw new Error(`Path does not start with prefix (path: ${a}, prefix: ${e})`);a=a.substring(e.length)}return{scheme:r,server:o,path:a,prefix:e}}function ce(i,e=""){let{path:t,scheme:r,server:o,prefix:a}=pe(i,e),n=(t||"").split("/").reverse(),[s,f,c,m,...h]=n,x=h.reverse().filter(Boolean).join("/");if(n.length===1||s==="")return{type:"base",scheme:r,server:o,prefix:a,identifier:x};if(s==="info.json"){let[,...p]=n;return{type:"info",scheme:r,server:o,prefix:a,identifier:p.reverse().filter(Boolean).join("/")}}let g=(s||"").split(".");if(typeof g[1]>"u"||typeof g[0]>"u"||typeof m>"u"||typeof c>"u"||typeof f>"u")throw new Error("Invalid image request");return{type:"image",scheme:r,server:o,prefix:a,identifier:x,originalPath:t,region:fe(m),size:me(c),rotation:le(f),quality:g[0],format:g[1]}}function he(i){let e=ce(u(i.id));if(e.type!=="info")throw new Error("Invalid service URL");let t=z(i);return{identifier:e.identifier,originalPath:"",server:e.server,prefix:e.prefix,scheme:e.scheme,type:"image",quality:t.extraQualities.indexOf("default")===-1?t.extraQualities[0]:"default",region:{full:!0},size:{max:!0,upscaled:!1,confined:!1},format:"jpg",rotation:{angle:0}}}function ge(i,e,t){let r=t.length,o=[];for(let a=0;a<r;a++){let n=t[a];if(n){let s=n.width;o.push(i/s)}}return o}function ue(i,e,t){let r=t.length,o=[];for(let a=0;a<r;a++){let n=t[a];n&&o.push({width:Math.floor(i/n),height:Math.floor(e/n)})}return o}function l(i){if(i["@id"])return i["@id"];if(i.id)return i.id}function d(i){if(!i||!i.profile||!l(i))return!1;let e=Array.isArray(i.profile)?i.profile:[i.profile];for(let t of e)if(typeof t=="string"&&C.indexOf(t)!==-1)return!0;return!1}function de(i){if(!d(i))return!1;let e=Array.isArray(i.profile)?i.profile:[i.profile];for(let t of e)if(typeof t=="string"){if(_.indexOf(t)!==-1)return!0}else{let r=[...t.supports||[],...t.extraFeatures||[]];if(r.indexOf("regionByPx")!==-1&&(r.indexOf("sizeByW")!==-1||r.indexOf("sizeByWh")!==-1))return!0}return!1}function v(i,e){if(e&&e.profile){let t=e.profile;if(t){let r=Array.isArray(t)?t:[t];return r.includes(`level${i}`)||r.includes(`http://iiif.io/api/image/2/level${i}.json`)||r.includes(`http://iiif.io/api/image/1/level${i}.json`)||r.includes(`http://iiif.io/api/image/1/profiles/level${i}.json`)}}return!1}function I(i){return d(i)?v(0,i)?0:v(1,i)?1:v(2,i)?2:null:null}function y(i){return(i["@context"]?Array.isArray(i["@context"])?i["@context"]:[i["@context"]]:[]).indexOf("http://iiif.io/api/image/3/context.json")!==-1}function xe(i){if(!de(i))return[];let e=[],t=Array.isArray(i.profile)?i.profile:[i.profile],r=t.length;for(let o=0;o<r;o++){let a=t[o];if(a&&typeof a!="string"&&(a.maxHeight||a.maxWidth))return[{id:l(i),type:"variable",minWidth:0,minHeight:0,maxHeight:a.maxHeight||a.maxWidth,maxWidth:a.maxWidth||a.maxHeight,level:I(i),version:i["@context"]==="http://iiif.io/api/image/3/context.json"?3:2}]}if(i.tiles){let o=i.tiles.length;for(let a=0;a<o;a++){let n=i.tiles[a];n&&(n.height||n.width)&&e.push({id:l(i),type:"variable",minHeight:0,minWidth:0,maxHeight:n.height||n.width,maxWidth:n.width,level:I(i),version:y(i)?3:2})}}return e}function O(i){let e=/^.*\/(full)\/(((\d+),(\d+)?)|max)\/(\d+)\/default\.(jpg|png|jpeg)$/,t=i.match(e);if(t&&t[4]&&t[5]){let r=t[1],o=parseInt(t[4],10),a=parseInt(t[5],10),n=t[7];if((r==="max"||r==="full")&&o&&a&&n)return{type:"fixed",id:i,height:a,width:o}}return{type:"unknown",id:i}}function Ie(i){if(i["@type"])return i["@type"];if(i.type)return i.type}function Se(i){if(typeof i=="string")return O(i);let e=Ie(i);if(e!=="Image"&&e!=="sc:Image")return null;let t=i,r=l(t);return r?r&&t.width&&t.height?{id:r,type:"fixed",width:t.width,height:t.height,unsafe:!0}:O(r):null}function ve(i){return d(i)?(i&&i.sizes?i.sizes:[]).map(e=>({id:l(i),type:"fixed-service",height:e.height,width:e.width,level:I(i),version:y(i)?3:2})):[]}function P(i){let e=[],t=i.length;for(let r=0;r<t;r++){let o=i[r];if(!o)continue;let a=ve(o);a.length&&e.push(...a);let n=xe(o);n.length&&e.push(...n)}return e}function b(i){let e=i.service?Array.isArray(i.service)?i.service:[i.service]:[],t=e.length,r=[];for(let o=0;o<t;o++)d(e[o])&&r.push(e[o]);return r}function ye(i,e=!0,t){let r=[],o=Se(i);if(o===null)return r;let a=i;if(r.push(o),e&&a&&a.width&&a.height){let n=[],s=b(a);for(let f of s){let c={id:l(f),width:a.width,height:a.height};if(t.canLoadSync(c)){let m=t.loadServiceSync(c);m&&(m.height||(m.height=a.height),m.width||(m.width=a.width),n.push(...P([m])))}}if(n.length)return r.push(...n),r}return a.service&&r.push(...P(a.service)),r}function Fe({x:i=0,y:e=0,w:t,h:r,full:o,square:a,percent:n}){if(o)return"full";if(a)return"square";if(typeof t>"u"||typeof r>"u")throw new Error("RegionParameter: invalid region");let s=`${i},${e},${t},${r}`;return n?`pct:${s}`:s}function we({max:i,percentScale:e,upscaled:t,confined:r,width:o,height:a,serialiseAsFull:n,version:s}){let f=[];return t&&f.push("^"),i?(f.push(n?"full":"max"),f.join("")):(r&&f.push("!"),e&&f.push(`pct:${e}`),o&&f.push(`${o}`),f.push(","),a&&s===3&&f.push(`${a}`),f.join(""))}function _e(i){return`${i.mirror?"!":""}${(i.angle||0)%360}`}function ze(i,e){let t=i.prefix.startsWith("/")?i.prefix.substr(1):i.prefix,r=`${i.scheme}://${i.server}/${t?`${t}/`:""}${i.identifier}`;if(i.type==="base")return r;if(i.type==="info")return`${r}/info.json`;let{size:o}=i,{region:a,rotation:n,format:s,quality:f}=i;if(e){let c=e["@context"]?Array.isArray(e["@context"])?e["@context"]:[e["@context"]]:[],m=e.profile,h=c.indexOf("http://iiif.io/api/image/2/context.json")!==-1||m==="level2",x=c.indexOf("http://iiif.io/api/image/3/context.json")!==-1;if((o.width===e.width&&!o.height||o.height===e.height&&!o.width||o.width===e.width&&o.height===e.height)&&(o={...o,max:!0}),h&&(o.max&&!o.serialiseAsFull&&(o={...o,serialiseAsFull:!0}),!o.max&&o.width&&o.height&&(o={...o,height:void 0}),o={...o,version:2}),x){if(o.max&&o.serialiseAsFull&&(o={...o,serialiseAsFull:!1}),o.width&&!o.height&&e.width&&e.height){let g=e.height/e.width;o={...o,height:Math.ceil(o.width*g)}}o={...o,version:3}}}return[r,Fe(a),we(o),_e(n),`${f}.${s}`].filter(Boolean).join("/")}function E(i,e,t){let r=he({"@context":i.version===3?"http://iiif.io/api/image/3/context.json":"http://iiif.io/api/image/2/context.json",id:u(l(i)),profile:i.level===null||typeof i.level>"u"?"level0":`level${i.level}`,type:i.version===3?"ImageService3":"ImageService2"});if(r.type!=="image")throw new Error("Invalid service");return r.size.max=!1,r.size.width=e,r.size.height=t,{id:ze(r),type:"fixed",width:e,height:t||i.height/(i.width||1)*e,unsafe:i.width>e}}function S(i){let e=i.replace(/(https?:\/\/)?(www.)?/i,"");return e.indexOf("/")!==-1?e.split("/")[0]:e}function gt(i){if(!i.width||!i.height)return null;if(i.tiles){let e=i.tiles.sort((r,o)=>Math.max(...o.scaleFactors)-Math.max(...r.scaleFactors)),t=e.length;for(let r=0;r<t;r++){let o=e[r];if(!o)continue;let a=o.width;if(!a)continue;let n=o.scaleFactors.length,s=o.scaleFactors.sort();for(let f=0;f<n;f++){let c=s[f];if(c&&i.width/c<=a&&i.height/c<=a)return{id:l(i),type:"fixed-service",width:i.width/c|0,height:i.height/c|0,level:I(i),version:y(i)?3:2}}}}return null}function A(i,e){if(!d(i))return[!1,"Not a valid image service"];e.extraFeatures=e.extraFeatures?e.extraFeatures:[];let t=z(i);if(e.exactSize){let r=!1;if(i.sizes)for(let o of i.sizes)o.width&&o.width===e.exactSize.width&&(L.indexOf("sizeByW")!==-1||o.height&&o.height===e.exactSize.height)&&(r=!0),o.height&&o.height===e.exactSize.height&&(L.indexOf("sizeByH")!==-1||o.width&&o.width===e.exactSize.width)&&(r=!0);r||(e.maxWidth=Math.max(e.maxWidth||0,e.exactSize.width||0)||void 0,e.maxHeight=Math.max(e.maxHeight||0,e.exactSize.height||0)||void 0,e.maxArea=Math.max(e.maxArea||0,(e.exactSize.width&&e.exactSize.height?e.exactSize.width*e.exactSize.height:e.maxArea)||0)||void 0,!e.exactSize.height&&e.exactSize.width?e.extraFeatures.indexOf("sizeByW")===-1&&e.extraFeatures.push("sizeByW"):!e.exactSize.width&&e.exactSize.height&&e.extraFeatures.indexOf("sizeByH")===-1&&e.extraFeatures.push("sizeByH"))}if(e.maxArea&&t.maxArea&&e.maxArea>t.maxArea)return[!1,`Max area is ${t.maxArea}`];if(e.maxWidth&&t.maxWidth&&e.maxWidth>t.maxWidth)return[!1,`Max width is ${t.maxWidth}`];if(e.maxHeight&&t.maxHeight&&e.maxHeight>t.maxHeight)return[!1,`Max height is ${t.maxHeight}`];if(e.extraFeatures){let r=[];for(let o of e.extraFeatures)t.extraFeatures.indexOf(o)===-1&&r.push(o);if(r.length)return[!1,`Missing features: ${r.join(", ")}`]}if(e.extraFormats){let r=[];for(let o of e.extraFormats)t.extraFormats.indexOf(o)===-1&&r.push(o);if(r.length)return[!1,`Missing formats: ${r.join(", ")}`]}if(e.extraQualities){let r=[];for(let o of e.extraQualities)t.extraQualities.indexOf(o)===-1&&r.push(o);if(r.length)return[!1,`Missing qualities: ${r.join(", ")}`]}return[!0]}function Ft(i,e){return A(i,{extraFormats:[e]})}function zt(i,e){if(e.type!=="image")return[!0];let t=[];e.rotation.mirror&&t.push("mirroring"),e.region.percent&&t.push("regionByPct"),e.region.square?t.push("regionSquare"):e.region.full||t.push("regionByPx"),e.rotation.angle&&(e.rotation.angle%90?t.push("rotationArbitrary"):t.push("rotationBy90s")),e.size.confined&&t.push("sizeByConfinedWh"),!e.size.width&&e.size.height&&t.push("sizeByH"),e.size.percentScale&&t.push("sizeByPct"),(i.sizes||[]).find(n=>n.width===e.size.width&&!e.size.height||n.height===e.size.height&&!e.size.width||n.height===e.size.height&&n.width===e.size.width)?t.push("sizeByWhListed"):(e.size.width&&!e.size.height&&t.push("sizeByW"),e.size.width&&e.size.height&&t.push("sizeByWh")),e.size.upscaled&&t.push("sizeUpscaling");let[o,a]=A(i,{extraFeatures:t,extraQualities:[e.quality],extraFormats:[e.format],exactSize:e.size});return o?[!0]:[!1,a]}function be(i,e,t){let r=i.width?i.width:i.maxWidth;return t.height<=i.maxHeight&&t.width<=i.maxWidth&&t.height>=i.minHeight&&t.width>=i.minWidth&&(!e||Math.abs(t.width-r)<Math.abs(e.width-r))}function Ee(i,e){let t=[],r=Object.assign({unsafeImageService:!1,atAnyCost:!0,fallback:!0,minHeight:64,minWidth:64,maxHeight:1/0,maxWidth:1/0,returnAllOptions:!1,preferFixedSize:!1,allowUnsafe:!1,explain:!1,height:0,width:0},i),o=(m,h=0)=>r.explain?t.push(new Array(h).fill(0).map(x=>" ").join("")+m().trim()):void 0,a=[],n=[],s=null;o(()=>`Using configuration: ${JSON.stringify(r,null,2)}`);let f=(m,h)=>{if(o(()=>"Swapping choice",3),be(r,h,m)){if(r.preferFixedSize&&m.unsafe){o(()=>`We found an image that was marked as unsafe, but it was the best size. (${m.id})`,4),n.push(m);return}r.returnAllOptions&&h&&n.push(h),o(()=>`We found a new image that was the best size. (${m.id})`,4),s=m}else r.returnAllOptions&&n.push(m)};o(()=>`The input shows we have ${e.length} list(s) of candidates to choose from.`);let c=e.length;for(let m=0;m<c;m++){let h=e[m]();o(()=>`Candidate group ${m}: ${JSON.stringify(h,null,2)}`,1);let x=h.length;o(()=>`Checking candidate list number ${m} and found ${x} potential ways of creating image(s)`,1);for(let g=0;g<x;g++){let p=h[g];if(o(()=>`-> Checking candidate ${g}`,1),p.type==="unknown"&&r.atAnyCost&&(o(()=>`We've found an unknown image type, adding this to the "last resort" list`,2),a.push(p)),p.type==="fixed"&&(p.unsafe?(o(()=>`We've found an unsafe fixed image type, adding this to the "last resort" list`,2),a.push(p)):(o(()=>"We've found a fixed size image, checking if it matches the request",2),f(p,s))),p.type==="fixed-service")if(r.unsafeImageService){o(()=>"Checking for an image from the tile source, without calculating the right height and width (unsafeImageService)",2);let F=E(p,r.width,r.height);f(F,s)}else{o(()=>"Checking for an image from the tile source 3",2);let F=E(p,p.width,p.height);f(F,s)}if(p.type==="variable"&&p.maxWidth){let F=E({id:p.id,type:"fixed-service",width:p.maxWidth,height:p.maxWidth,level:p.level,version:p.version},p.maxWidth);f(F,s)}}if(s&&!r.returnAllOptions){if(s.unsafe||r.allowUnsafe)continue;o(()=>`We found a match in choice list number ${m}, no searching any more`);break}}return r.atAnyCost&&n.length===0?(o(()=>s?`We found an image! ${s.id} of type ${s.type}`:'We found no images, but "atAnyCost" is set, so returning that'),{best:s||a[0]||null,fallback:a.slice(1),log:t}):r.returnAllOptions?(o(()=>"Returning all options that we have found"),{best:(r.atAnyCost?s||n[0]||a[0]:s||n[0])||null,fallback:[...n,...a],log:t}):(o(()=>"Returning the best image that we found, and a fallback"),{best:s||n[0]||null,fallback:s?n:n.slice(1),log:t})}function Ae(i,e,t){let r=i>e?i:e,o=t.length,a=[];for(let n=0;n<o;n++){let s=t[n];if(!s||s.scaleFactors.length===0)continue;let f=s.scaleFactors[0];if(!f)continue;let c=r/f,m=[f];for(;c>=s.width;)f=f*2,m.push(f),c=c/2;a.push({...s,scaleFactors:m})}return a}function Re(i,e){if(i.length!==e.length)return!1;if(i.length===0&&e.length===0)return!0;let t=i.length,r=!0;for(let a=0;a<t;a++){let n=i[a],s=e[a];if(n.width!==s.width||n.height!==s.height){r=!1;break}}if(r)return!0;let o=0;for(let a=0;a<t;a++)for(let n=0;n<t;n++)if(i[a].width===e[n].width&&i[a].height===e[n].height){o++;break}return o===t}function M(i){return v(0,i)}var W=class{constructor(e={}){w(this,"config",{verificationsRequired:1,approximateServices:!1,enableFetching:!0,disableThrottling:!1});w(this,"fetchingCount",0);w(this,"imageServices",{});w(this,"knownImageServers",{});this.config=Object.assign(this.config,e)}setConfig(e){Object.assign(this.config,e)}sample(e,t,r=!0){let o=S(l(e)),a=u(l(e)),n=this.knownImageServers[o];return this.imageServices[a]=Object.assign(e,{real:!0}),!n&&e.tiles&&!M(e)?(this.knownImageServers[o]={verifications:0,malformed:!1,root:o,preLoaded:r,sampledId:l(e),verified:!1,server:null,result:{context:e["@context"]||[],sampledProfile:e.profile,resourceServiceRatio:t&&e.height?t.height/e.height:1,sampledSizes:e.sizes||[],sizeRatios:ge(e.width,e.height,e.sizes||[]),sampledTiles:e.tiles||[]}},!0):this.verify(e)}preLoad(e,t=!0){this.knownImageServers[e.root]=e,t&&(this.knownImageServers[e.root].malformed=!1,this.knownImageServers[e.root].verifications=this.config.verificationsRequired)}predict(e,t=!1,r=!1){let o=e?.source,a=S(l(e)),n=this.knownImageServers[a],s=u(l(e));return this.imageServices[s]?this.imageServices[s]||null:!this.config.approximateServices||!n||!n.result||!(o?.height||e.height)||!(o?.width||e.width)||!r&&(n.malformed||n.verifications<this.config.verificationsRequired)||M(e.source)?null:(this.imageServices[s]||(this.imageServices[s]={"@context":n.result.context,"@id":l(e),id:l(e),protocol:"http://iiif.io/api/image",tiles:o?.tiles||Ae(e.width,e.height,n.result.sampledTiles),sizes:o?.sizes||ue(Math.round(e.width/n.result.resourceServiceRatio),Math.round(e.height/n.result.resourceServiceRatio),n.result.sizeRatios),profile:o?.profile||n.result.sampledProfile,height:o?.height||e.height,width:o?.width||e.width,real:!1}),this.imageServices[s]||null)}async getThumbnailFromResource(e,t,r=!0,o=[]){let a=e?await this.getImageCandidates(e,r):[];return Ee(t,[()=>o,()=>a])}async getImageCandidates(e,t=!0){let r=e;if(t&&r&&r.height&&r.width){let o=b(r);for(let a of o){let n={id:l(a),width:a.width?a.width:r.width,height:a.height?a.height:r.height,source:a};await this.loadService(n)}}return ye(e,t,this)}async verify(e){let t=this.predict(e,!1,!0),r=await this.fetchService(l(e));if(!t)return!1;let o=t.height===r.height&&t.width===r.width&&t["@context"]===r["@context"]&&Re(t.sizes||[],r.sizes||[]);if(o){let a=S(l(e)),n=this.knownImageServers[a];n&&(n.verifications+=1,n.verifications>=this.config.verificationsRequired&&(n.verified=!0))}return o}canLoadSync(e){let t=typeof e=="string"?e:l(e),r=u(t);if(this.imageServices[r])return!0;let o=this.knownImageServers[S(t)];return!!(o&&!o.malformed&&o.verifications>=this.config.verificationsRequired)}async markAsMalformed(e){return this.knownImageServers[S(l(e))].malformed=!0,this.loadService(e,!0)}async fetchService(e,t=!1){let r=u(e),o=this.imageServices[r];if(o&&(!t||o.real))return o;if(!this.config.enableFetching)throw new Error("Fetching is not enabled");let a=await this.fetch(r).then(n=>n.json());return!a.id&&a["@id"]&&(a.id=a["@id"]),a.id!==e&&(a.id=e,a["@id"]&&(a["@id"]=e)),this.imageServices[r]=Object.assign(a,{real:!0}),this.imageServices[r]}async fetch(e,t){return fetch(e,t)}async loadService(e,t=!1){if(!this.config.disableThrottling){let a=!0;for(;a;)if(this.fetchingCount>=this.config.verificationsRequired)await new Promise(n=>setTimeout(n,500));else{a=!1;break}}let r=this.knownImageServers[S(l(e))];if(r&&!r.malformed&&!t){await r.result;let a=this.loadServiceSync(e);if(a)return a}this.fetchingCount++;let o=await this.fetchService(l(e),t);return this.fetchingCount--,o.real&&this.sample(o,e),o}loadServiceSync(e){let t=u(l(e));return this.imageServices[t]?this.imageServices[t]:this.config.approximateServices?this.predict(e):null}},Qt=new W;export{je as IIIF_1_IMAGE_LEVEL_0,Ge as IIIF_1_IMAGE_LEVEL_0_COMPLIANCE,Te as IIIF_1_IMAGE_LEVEL_0_PROFILE,U as IIIF_1_IMAGE_LEVEL_1,te as IIIF_1_IMAGE_LEVEL_1_COMPLIANCE,V as IIIF_1_IMAGE_LEVEL_1_PROFILE,Q as IIIF_1_IMAGE_LEVEL_2,re as IIIF_1_IMAGE_LEVEL_2_COMPLIANCE,D as IIIF_1_IMAGE_LEVEL_2_PROFILE,$e as IIIF_2_IMAGE_LEVEL_0,ke as IIIF_2_IMAGE_LEVEL_0_NO_JSON,Be as IIIF_2_IMAGE_LEVEL_0_PROFILE,J as IIIF_2_IMAGE_LEVEL_1,ee as IIIF_2_IMAGE_LEVEL_1_NO_JSON,Z as IIIF_2_IMAGE_LEVEL_1_PROFILE,K as IIIF_2_IMAGE_LEVEL_2,ie as IIIF_2_IMAGE_LEVEL_2_NO_JSON,X as IIIF_2_IMAGE_LEVEL_2_PROFILE,Ne as IIIF_3_IMAGE_LEVEL_0,Y as IIIF_3_IMAGE_LEVEL_1,q as IIIF_3_IMAGE_LEVEL_2,W as ImageServiceLoader,Me as STANFORD_IIIF_1_IMAGE_COMPLIANCE_0,N as STANFORD_IIIF_1_IMAGE_COMPLIANCE_1,k as STANFORD_IIIF_1_IMAGE_COMPLIANCE_2,We as STANFORD_IIIF_1_IMAGE_CONFORMANCE_0,G as STANFORD_IIIF_1_IMAGE_CONFORMANCE_1,H as STANFORD_IIIF_1_IMAGE_CONFORMANCE_2,Oe as STANFORD_IIIF_IMAGE_COMPLIANCE_0,j as STANFORD_IIIF_IMAGE_COMPLIANCE_1,T as STANFORD_IIIF_IMAGE_COMPLIANCE_2,Pe as STANFORD_IIIF_IMAGE_CONFORMANCE_0,$ as STANFORD_IIIF_IMAGE_CONFORMANCE_1,B as STANFORD_IIIF_IMAGE_CONFORMANCE_2,u as canonicalServiceUrl,z as combineProfiles,he as createImageServiceRequest,L as extraFeatures,ge as extractFixedSizeScales,ue as fixedSizesFromScales,xe as getCustomSizeFromService,Se as getFixedSizeFromImage,ve as getFixedSizesFromService,l as getId,ye as getImageCandidates,P as getImageCandidatesFromService,E as getImageFromTileSource,S as getImageServerFromId,b as getImageServices,gt as getSmallestScaleFactorAsSingleImage,Ie as getType,Qt as imageServiceLoader,C as imageServiceProfiles,ze as imageServiceRequestToString,Ft as imageServiceSupportsFormat,zt as imageServiceSupportsRequest,O as inferSizeFromUrl,be as isBestMatch,d as isImageService,oe as level0,Ve as level0Support,ae as level1,_ as level1Support,ne as level2,R as level2Support,se as levelToProfile,ce as parseImageServiceRequest,pe as parseImageServiceUrl,fe as parseRegionParameter,le as parseRotationParameter,me as parseSizeParameter,Ee as pickBestFromCandidates,Fe as regionParameterToString,_e as rotationParameterToString,Ae as sampledTilesToTiles,we as sizeParameterToString,Re as sizesMatch,A as supports,de as supportsCustomSizes};