@etu-wiki/sharp-iiif-shims 0.0.91 → 0.0.93

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +1 -1
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- "use strict";var lt=Object.create;var _=Object.defineProperty;var gt=Object.getOwnPropertyDescriptor;var ct=Object.getOwnPropertyNames;var pt=Object.getPrototypeOf,ut=Object.prototype.hasOwnProperty;var mt=(i,e)=>()=>(e||i((e={exports:{}}).exports,e),e.exports),ft=(i,e)=>{for(var r in e)_(i,r,{get:e[r],enumerable:!0})},K=(i,e,r,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of ct(e))!ut.call(i,s)&&s!==r&&_(i,s,{get:()=>e[s],enumerable:!(t=gt(e,s))||t.enumerable});return i};var V=(i,e,r)=>(r=i!=null?lt(pt(i)):{},K(e||!i||!i.__esModule?_(r,"default",{value:i,enumerable:!0}):r,i)),dt=i=>K(_({},"__esModule",{value:!0}),i);var et=mt((_t,E)=>{"use strict";var O=Object.defineProperty,yt=Object.getOwnPropertyDescriptor,wt=Object.getOwnPropertyNames,zt=Object.prototype.hasOwnProperty,vt=(i,e)=>{for(var r in e)O(i,r,{get:e[r],enumerable:!0})},$t=(i,e,r,t)=>{if(e&&typeof e=="object"||typeof e=="function")for(let s of wt(e))!zt.call(i,s)&&s!==r&&O(i,s,{get:()=>e[s],enumerable:!(t=yt(e,s))||t.enumerable});return i},bt=i=>$t(O({},"__esModule",{value:!0}),i),tt={};vt(tt,{default:()=>it});E.exports=bt(tt);var o="[1-9][0-9]*",Z="[0-9]*",z="(?=.*[1-9])\\d+(\\.\\d+)?",N="\\d+(\\.\\d+)?",q=(i=>(i.jpg="image/jpeg",i.jpeg="image/jpeg",i.png="image/png",i.webp="image/webp",i.tif="image/tiff",i.tiff="image/tiff",i.gif="image/gif",i.jp2="image/jp2",i.pdf="application/pdf",i))(q||{}),x={version:["1.0","1.1","2.0","2.1","3.0"],level:["0","1","2"],region:["full","square",`${Z},${Z},${o},${o}`,`pct:${N},${N},${z},${z}`],rotation:`!?${N}`,quality:["color","gray","bitonal","default","native","grey"],format:["jpg","png","webp","tif","gif","jp2","pdf"],size:[""]},xt={1:["full",`${o},`,`,${o}`,`pct:${z}`,`${o},${o}`,`!${o},${o}`],1.1:["full",`${o},`,`,${o}`,`pct:${z}`,`${o},${o}`,`!${o},${o}`],2:["full",`${o},`,`,${o}`,`pct:${z}`,`${o},${o}`,`!${o},${o}`],2.1:["full","max",`${o},`,`,${o}`,`pct:${z}`,`${o},${o}`,`!${o},${o}`],3:["max",`${o},`,`,${o}`,`pct:${z}`,`${o},${o}`,`!${o},${o}`,"\\^max",`\\^${o},`,`\\^,${o}`,`\\^pct:${z}`,`\\^${o},${o}`,`\\^!${o},${o}`]},j=/^pct:([\d,]+)/,it=class{constructor(i,e,r,t,s,a,n){this.version=i,this.level=e,this.maxArea=r,this.maxWidth=t,this.maxHeight=s,this.tileWidth=a,this.tileHeight=n,this.validate("version",i),this.validate("level",e),x.size=xt[Number(i)]}validator(i){var e=x[i];return e instanceof Array&&(e=e.join("|")),new RegExp("^("+e+")$")}validate(i,e){if(!this.validator(i).test(e))throw Error(`invalid IIIF url ${i}: ${e}`);return!0}normalizeRegionParams(i,e){if(this.validate("region",i),i==="full")return null;if(i==="square")return this._regionSquare(e);if(j.test(i))return this._regionPct(i.match(j)[1],e);{let r=i.split(",").map(t=>Number(t));return this._regionXYWH(r,e)}}_regionSquare(i){let e,r,t=i.width,s=i.height;if(t!==s){let a=Math.min(t,s),n=Math.abs(Math.floor((t-s)/2));return t>s?(e=n,r=0):(e=0,r=n),this._regionXYWH([e,r,a,a],i)}return null}_regionPct(i,e){let r=i.split(",").map((t,s)=>{let a=Number(t)/100;return s===0||s===2?Math.ceil(e.width*a):s===1||s===3?Math.ceil(e.height*a):null});return this._regionXYWH(r,e)}_regionXYWH(i,e){let r=e.width,t=e.height,[s,a,n,h]=i;return s+n>r&&(n=r-s),a+h>t&&(h=t-a),{x:s,y:a,w:n,h}}normalizeSizeParams(i,e){let r=i,t=e.width,s=e.height;if(!e||!t&&!s)throw Error(`invalid region dimensions: ${JSON.stringify(e,null,2)}`);this.validate("size",r);let a=!1;r.charAt(0)==="^"&&(a=!0,r=r.substring(1));let n=null;if(r==="full"||(r==="max"?n=this._sizeMaxScale(e):j.test(r)?n=this._sizePctScale(r.match(j)[1]):r.charAt(0)==="!"&&(r=r.substring(1),n=this._sizeBangScale(r,a,e)),n===1))return null;if(n){let h=Math.ceil(t*n),g=Math.ceil(s*n);return this._sizeWH(`${h},${g}`,a,e)}else return this._sizeWH(r,a,e)}_sizeBangScale(i,e,r){let t=r.width,s=r.height,[a,n]=i.split(",").map(g=>g===""?null:Number(g)),h=Math.min(a/t,n/s);if(a>t&&n>s&&!e)throw Error("should not upscale without ^");return h}_sizeMaxScale(i){let e=i.width,r=i.height,t=1;return this.maxArea&&this.maxArea<e*r&&(t=Math.sqrt(this.maxArea/(e*r))),this.maxWidth&&this.maxWidth<e&&(t=Math.min(t,this.maxWidth/e)),this.maxHeight&&this.maxHeight<r&&(t=Math.min(t,this.maxHeight/r)),t}_sizePctScale(i){return Number(i)/100}_sizeWH(i,e,r){let t=r.width,s=r.height,[a,n]=i.split(",").map(h=>h===""?null:Number(h));if(a?n||(n=Math.ceil(s*a/t),n=n===0?1:n):(a=Math.ceil(t*n/s),a=a===0?1:a),(a>t||n>s)&&!e)throw Error("should not upscale without ^");return{width:a,height:n}}normalizeRotationParams(i){this.validate("rotation",i);let e=!1;if(i==="0")return null;i[0]==="!"&&(e=!0);let r=Number(i.replace(/^!/,""));return{isMirror:e,degree:r}}normalizeQualityParams(i){return this.validate("quality",i),i==="native"||i==="default"?null:i}normalizeFormatParams(i,e){if((this.version==="1.0"||this.version==="1.1"||this.version==="2.0"||this.version==="2.1")&&!i)return"png";this.validate("format",i);let r=q[i],t=q[e];return r===t?null:i}getContentType(i){return q[i]}generateImageInfoTemp(i,e){let r={},t={};r.info=t;let s=this.version;return s==="1.0"?(t.identifier=i,t.width=e.width,t.height=e.height,t.scale_factors=this.factors(e),t.tile_width=this.tileWidth,t.tile_height=this.tileHeight,t.formats=x.format,t.qualities=x.quality,t.profile=this.uriPattern("compliance"),r.contentType="application/json",r.body=JSON.stringify(t)):(s==="1.1"?(t["@context"]=this.uriPattern("context"),t["@id"]=i,t.width=e.width,t.height=e.height,t.scale_factors=this.factors(e),t.tile_width=this.tileWidth,t.tile_height=this.tileHeight,t.formats=x.format,t.qualities=x.quality,t.profile=this.uriPattern("compliance")):s==="2.0"||s==="2.1"?(t["@context"]=this.uriPattern("context"),t["@id"]=i,t.protocol="http://iiif.io/api/image",t.width=e.width,t.height=e.height,this.level==="0"?t.sizes=this.sizes(e):t.tiles=[{scaleFactors:this.factors(e),width:this.tileWidth,height:this.tileHeight}],t.profile=[this.uriPattern("compliance")]):s==="3.0"&&(t["@context"]=[this.uriPattern("context")],t.id=i,t.type="ImageService3",t.protocol="http://iiif.io/api/image",t.width=e.width,t.height=e.height,this.level==="0"?t.sizes=this.sizes(e):t.tiles=[{scaleFactors:this.factors(e),width:this.tileWidth,height:this.tileHeight}],t.profile=this.uriPattern("compliance")),r.contentType="application/ld+json"),r}factors(i){let e=1,r=[];for(;this.tileWidth*e<=i.width&&this.tileHeight*e<=i.height;)r.push(e),e*=2;return r}uriPattern(i){let e="",r="";return this.version==="1.0"?e=`http://library.stanford.edu/iiif/image-api/compliance.html#level${this.level}`:this.version==="1.1"?(e=`http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level${this.level}`,r="http://library.stanford.edu/iiif/image-api/1.1/context.json"):this.version==="2.0"||this.version==="2.1"?(e=`http://iiif.io/api/image/2/level${this.level}.json`,r="http://iiif.io/api/image/2/context.json"):this.version==="3.0"&&(e=`level${this.level}`,r="http://iiif.io/api/image/3/context.json"),i==="compliance"?e:i==="context"?r:""}sizes(i){return[{width:i.width,height:i.height}]}};E.exports=it});var It={};ft(It,{default:()=>P});module.exports=dt(It);var m=V(require("sharp")),at=V(et()),Pt=1e8,rt=1e4,st=1e4,Ht=512,Wt=512,Bt={2:["baseUriRedirect","canonicalLinkHeader","cors","jsonldMediaType","mirroring","profileLinkHeader","regionByPct","regionByPx","rotationArbitrary","rotationBy90s","sizeAboveFull","sizeByWhListed","sizeByForcedWh","sizeByH","sizeByPct","sizeByW","sizeByWh"],2.1:["baseUriRedirect","canonicalLinkHeader","cors","jsonldMediaType","mirroring","profileLinkHeader","regionByPct","regionByPx","regionSquare","rotationArbitrary","rotationBy90s","sizeAboveFull","sizeByConfinedWh","sizeByDistortedWh","sizeByH","sizeByPct","sizeByW","sizeByWh","sizeByWhListed","sizeByForcedWh"],3:["baseUriRedirect","canonicalLinkHeader","cors","jsonldMediaType","mirroring","profileLinkHeader","regionByPct","regionByPx","regionSquare","rotationArbitrary","rotationBy90s","sizeByConfinedWh","sizeByH","sizeByPct","sizeByW","sizeByWh","sizeUpscaling"]},P=class extends at.default{constructor(r,t,s=!1,a=Pt,n=rt,h=st,g=Ht,p=Wt){super(r,t,a,n,h,g,p);this.enableHD=s}regionImpl(r,t){r&&(t=t.extract({left:r.x,top:r.y,width:r.w,height:r.h}))}sizeImpl(r,t){r&&(t=t.resize({...r,fit:"fill"}))}rotationImpl(r,t){r&&(r.isMirror&&(t=t.flop()),t=t.rotate(r.degree))}qualityFormatImpl(r,t,s,a,n){r&&(r==="color"?s=s.toColorspace("srgb"):r==="gray"||r==="grey"?s=s.grayscale():r==="bitonal"&&(s=s.threshold())),t&&(s=s.toFormat(t)),a==="png"&&(n.paletteBitDepth===1||n.space==="b-w")&&(s=s.toColorspace("b-w").png({colors:2}))}iiifTransform(r,t){let s=this.enableHD?(0,m.default)({limitInputPixels:!1,sequentialRead:!0}):(0,m.default)(),a=r.split("/"),n=a.pop(),h=n.split(".")[0],g=n.split(".")[1],p=a.pop(),y=a.pop(),S=a.pop(),H=!1,l=s,c={width:t.width,height:t.height};t&&t.orientation&&(H=!0,l=l.rotate(),t.orientation>=5&&(c={width:t.height,height:t.width}));let f=super.normalizeRegionParams(S,c);this.regionImpl(f,l);let u=super.normalizeSizeParams(y,{width:f?f.w:c.width,height:f?f.h:c.height});this.sizeImpl(u,l);let d=super.normalizeRotationParams(p);this.rotationImpl(d,l);let w=super.normalizeQualityParams(h),W=super.normalizeFormatParams(g,t.format);return this.qualityFormatImpl(w,W,l,g,t),f||u||d||w||W||H?l:(console.log("skip iiif transform"),null)}async generateIiifImage(r,t,s=256){let a=this.enableHD?(0,m.default)(t,{limitInputPixels:!1,sequentialRead:!0}):(0,m.default)(t),n=r.split("/"),h=n.pop(),g=h.split(".")[0],p=h.split(".")[1],y=n.pop(),S=n.pop(),H=n.pop(),l=await a.metadata(),c={width:l.width,height:l.height},f={},u=a,d=super.normalizeRegionParams(H,c);this.regionImpl(d,u);let w=super.normalizeSizeParams(S,{width:d?d.w:c.width,height:d?d.h:c.height});this.sizeImpl(w,u);let{x:W,y:nt,w:R,h:T}=d||{x:0,y:0,w:c.width,h:c.height},{width:A,height:D}=w||{width:c.width,height:c.height};if(R*T>A*D*s&&A<rt&&D<st&&l.pages&&l.pages>1){console.log("thumbnail");let v=Math.max(A/R,D/T),[F,L,B,I]=[Math.round(W*v),Math.round(nt*v),Math.round(R*v),Math.round(T*v)],[X,C]=[Math.round(c.width*v),Math.round(c.height*v)],$=l.pages-1,U=this.enableHD?await(0,m.default)(t,{limitInputPixels:!1,page:$,sequentialRead:!0}).metadata():await(0,m.default)(t,{page:$}).metadata(),Y=U.width,G=U.height;for(;G<C||Y<X;)G*=2,Y*=2,$--;console.log("selectedPage: "+$);let M={width:X,height:C,fastShrinkOnLoad:!1},J=this.enableHD?await(0,m.default)(t,{limitInputPixels:!1,page:$,sequentialRead:!0}).resize(M).toBuffer():await(0,m.default)(t,{page:$}).resize(M).toBuffer();u=this.enableHD?(0,m.default)(J,{limitInputPixels:!1,sequentialRead:!0}):(0,m.default)(J);let b=await u.metadata();L+I>b.height&&(I=b.height-L,console.log("adjested sh: "+I)),F+B>b.width&&(B=b.width-F,console.log("adjusted sw: "+B));let Q={x:F,y:L,w:B,h:I};this.regionImpl(Q,u),this.sizeImpl(w,u),console.log(b.width,b.height,M.width,M.height,Q,w)}let ot=super.normalizeRotationParams(y);this.rotationImpl(ot,u);let ht=super.normalizeQualityParams(g);console.log(p,l.format);let k=super.normalizeFormatParams(p,l.format);return this.qualityFormatImpl(ht,k,u,p,l),f.body=u,f.contentType=super.getContentType(k),f}generateImageInfo(r,t,s=""){let a=this.version,n=this.level,h={width:t.width,height:t.height};t&&t.orientation&&t.orientation>=5&&(console.log(t.orientation),h={width:t.height,height:t.width});let g=super.generateImageInfoTemp(r,h),p=g.info;if((a==="2.0"||a==="2.1")&&(p.profile=[super.uriPattern("compliance")],n==="2")){let y={};y.formats=["jpg","png","webp","jp2","pdf","tif"],y.qualities=["color","gray","bitonal","default","native","grey"],y.supports=Bt[Number(a)],p.profile.push(y)}return a==="3.0"&&s&&(p.extraFormats=[s],p.preferredFormats=[s]),s&&(p.formats=[s]),g}};module.exports=P;
1
+ "use strict";var lt=Object.create;var j=Object.defineProperty;var gt=Object.getOwnPropertyDescriptor;var ct=Object.getOwnPropertyNames;var pt=Object.getPrototypeOf,ut=Object.prototype.hasOwnProperty;var mt=(e,i)=>()=>(i||e((i={exports:{}}).exports,i),i.exports),ft=(e,i)=>{for(var r in i)j(e,r,{get:i[r],enumerable:!0})},K=(e,i,r,t)=>{if(i&&typeof i=="object"||typeof i=="function")for(let s of ct(i))!ut.call(e,s)&&s!==r&&j(e,s,{get:()=>i[s],enumerable:!(t=gt(i,s))||t.enumerable});return e};var V=(e,i,r)=>(r=e!=null?lt(pt(e)):{},K(i||!e||!e.__esModule?j(r,"default",{value:e,enumerable:!0}):r,e)),dt=e=>K(j({},"__esModule",{value:!0}),e);var it=mt((_t,k)=>{"use strict";var E=Object.defineProperty,yt=Object.getOwnPropertyDescriptor,wt=Object.getOwnPropertyNames,zt=Object.prototype.hasOwnProperty,vt=(e,i)=>{for(var r in i)E(e,r,{get:i[r],enumerable:!0})},bt=(e,i,r,t)=>{if(i&&typeof i=="object"||typeof i=="function")for(let s of wt(i))!zt.call(e,s)&&s!==r&&E(e,s,{get:()=>i[s],enumerable:!(t=yt(i,s))||t.enumerable});return e},$t=e=>bt(E({},"__esModule",{value:!0}),e),tt={};vt(tt,{default:()=>et});k.exports=$t(tt);var n="[1-9][0-9]*",Z="[0-9]*",b="(?=.*[1-9])\\d+(\\.\\d+)?",O="\\d+(\\.\\d+)?",q=(e=>(e.jpg="image/jpeg",e.jpeg="image/jpeg",e.png="image/png",e.webp="image/webp",e.tif="image/tiff",e.tiff="image/tiff",e.gif="image/gif",e.jp2="image/jp2",e.pdf="application/pdf",e))(q||{}),x={version:["1.0","1.1","2.0","2.1","3.0"],level:["0","1","2"],region:["full","square",`${Z},${Z},${n},${n}`,`pct:${O},${O},${b},${b}`],rotation:`!?${O}`,quality:["color","gray","bitonal","default","native","grey"],format:["jpg","png","webp","tif","gif","jp2","pdf"],size:[""]},xt={1:["full",`${n},`,`,${n}`,`pct:${b}`,`${n},${n}`,`!${n},${n}`],1.1:["full",`${n},`,`,${n}`,`pct:${b}`,`${n},${n}`,`!${n},${n}`],2:["full",`${n},`,`,${n}`,`pct:${b}`,`${n},${n}`,`!${n},${n}`],2.1:["full","max",`${n},`,`,${n}`,`pct:${b}`,`${n},${n}`,`!${n},${n}`],3:["max",`${n},`,`,${n}`,`pct:${b}`,`${n},${n}`,`!${n},${n}`,"\\^max",`\\^${n},`,`\\^,${n}`,`\\^pct:${b}`,`\\^${n},${n}`,`\\^!${n},${n}`]},S=/^pct:([\d,]+)/,et=class{constructor(e,i,r,t,s,a,o){this.version=e,this.level=i,this.maxArea=r,this.maxWidth=t,this.maxHeight=s,this.tileWidth=a,this.tileHeight=o,this.validate("version",e),this.validate("level",i),x.size=xt[Number(e)]}validator(e){var i=x[e];return i instanceof Array&&(i=i.join("|")),new RegExp("^("+i+")$")}validate(e,i){if(!this.validator(e).test(i))throw Error(`invalid IIIF url ${e}: ${i}`);return!0}normalizeRegionParams(e,i){if(this.validate("region",e),e==="full")return null;if(e==="square")return this._regionSquare(i);if(S.test(e))return this._regionPct(e.match(S)[1],i);{let r=e.split(",").map(t=>Number(t));return this._regionXYWH(r,i)}}_regionSquare(e){let i,r,t=e.width,s=e.height;if(t!==s){let a=Math.min(t,s),o=Math.abs(Math.floor((t-s)/2));return t>s?(i=o,r=0):(i=0,r=o),this._regionXYWH([i,r,a,a],e)}return null}_regionPct(e,i){let r=e.split(",").map((t,s)=>{let a=Number(t)/100;return s===0||s===2?Math.ceil(i.width*a):s===1||s===3?Math.ceil(i.height*a):null});return this._regionXYWH(r,i)}_regionXYWH(e,i){let r=i.width,t=i.height,[s,a,o,h]=e;return s+o>r&&(o=r-s),a+h>t&&(h=t-a),{x:s,y:a,w:o,h}}normalizeSizeParams(e,i){let r=e,t=i.width,s=i.height;if(!i||!t&&!s)throw Error(`invalid region dimensions: ${JSON.stringify(i,null,2)}`);this.validate("size",r);let a=!1;r.charAt(0)==="^"&&(a=!0,r=r.substring(1));let o=null;if(r==="full"||(r==="max"?o=this._sizeMaxScale(i):S.test(r)?o=this._sizePctScale(r.match(S)[1]):r.charAt(0)==="!"&&(r=r.substring(1),o=this._sizeBangScale(r,a,i)),o===1))return null;if(o){let h=Math.ceil(t*o),g=Math.ceil(s*o);return this._sizeWH(`${h},${g}`,a,i)}else return this._sizeWH(r,a,i)}_sizeBangScale(e,i,r){let t=r.width,s=r.height,[a,o]=e.split(",").map(g=>g===""?null:Number(g)),h=Math.min(a/t,o/s);if(a>t&&o>s&&!i)throw Error("should not upscale without ^");return h}_sizeMaxScale(e){let i=e.width,r=e.height,t=1;return this.maxArea&&this.maxArea<i*r&&(t=Math.sqrt(this.maxArea/(i*r))),this.maxWidth&&this.maxWidth<i&&(t=Math.min(t,this.maxWidth/i)),this.maxHeight&&this.maxHeight<r&&(t=Math.min(t,this.maxHeight/r)),t}_sizePctScale(e){return Number(e)/100}_sizeWH(e,i,r){let t=r.width,s=r.height,[a,o]=e.split(",").map(h=>h===""?null:Number(h));if(a?o||(o=Math.ceil(s*a/t),o=o===0?1:o):(a=Math.ceil(t*o/s),a=a===0?1:a),(a>t||o>s)&&!i)throw Error("should not upscale without ^");return{width:a,height:o}}normalizeRotationParams(e){this.validate("rotation",e);let i=!1;if(e==="0")return null;e[0]==="!"&&(i=!0);let r=Number(e.replace(/^!/,""));return{isMirror:i,degree:r}}normalizeQualityParams(e){return this.validate("quality",e),e==="native"||e==="default"?null:e}normalizeFormatParams(e,i){if((this.version==="1.0"||this.version==="1.1"||this.version==="2.0"||this.version==="2.1")&&!e)return"png";this.validate("format",e);let r=q[e],t=q[i];return r===t?null:e}getContentType(e){return q[e]}generateImageInfoTemp(e,i){let r={},t={};r.info=t;let s=this.version;return s==="1.0"?(t.identifier=e,t.width=i.width,t.height=i.height,t.scale_factors=this.factors(i),t.tile_width=this.tileWidth,t.tile_height=this.tileHeight,t.formats=x.format,t.qualities=x.quality,t.profile=this.uriPattern("compliance"),r.contentType="application/json",r.body=JSON.stringify(t)):(s==="1.1"?(t["@context"]=this.uriPattern("context"),t["@id"]=e,t.width=i.width,t.height=i.height,t.scale_factors=this.factors(i),t.tile_width=this.tileWidth,t.tile_height=this.tileHeight,t.formats=x.format,t.qualities=x.quality,t.profile=this.uriPattern("compliance")):s==="2.0"||s==="2.1"?(t["@context"]=this.uriPattern("context"),t["@id"]=e,t.protocol="http://iiif.io/api/image",t.width=i.width,t.height=i.height,this.level==="0"?t.sizes=this.sizes(i):t.tiles=[{scaleFactors:this.factors(i),width:this.tileWidth,height:this.tileHeight}],t.profile=[this.uriPattern("compliance")]):s==="3.0"&&(t["@context"]=[this.uriPattern("context")],t.id=e,t.type="ImageService3",t.protocol="http://iiif.io/api/image",t.width=i.width,t.height=i.height,this.level==="0"?t.sizes=this.sizes(i):t.tiles=[{scaleFactors:this.factors(i),width:this.tileWidth,height:this.tileHeight}],t.profile=this.uriPattern("compliance")),r.contentType="application/ld+json"),r}factors(e){let i=1,r=[];for(;this.tileWidth*i<=e.width&&this.tileHeight*i<=e.height;)r.push(i),i*=2;return r}uriPattern(e){let i="",r="";return this.version==="1.0"?i=`http://library.stanford.edu/iiif/image-api/compliance.html#level${this.level}`:this.version==="1.1"?(i=`http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level${this.level}`,r="http://library.stanford.edu/iiif/image-api/1.1/context.json"):this.version==="2.0"||this.version==="2.1"?(i=`http://iiif.io/api/image/2/level${this.level}.json`,r="http://iiif.io/api/image/2/context.json"):this.version==="3.0"&&(i=`level${this.level}`,r="http://iiif.io/api/image/3/context.json"),e==="compliance"?i:e==="context"?r:""}sizes(e){return[{width:e.width,height:e.height}]}};k.exports=et});var It={};ft(It,{default:()=>P});module.exports=dt(It);var f=V(require("sharp")),at=V(it()),Pt=1e8,rt=1e4,st=1e4,Ht=512,Mt=512,Wt={2:["baseUriRedirect","canonicalLinkHeader","cors","jsonldMediaType","mirroring","profileLinkHeader","regionByPct","regionByPx","rotationArbitrary","rotationBy90s","sizeAboveFull","sizeByWhListed","sizeByForcedWh","sizeByH","sizeByPct","sizeByW","sizeByWh"],2.1:["baseUriRedirect","canonicalLinkHeader","cors","jsonldMediaType","mirroring","profileLinkHeader","regionByPct","regionByPx","regionSquare","rotationArbitrary","rotationBy90s","sizeAboveFull","sizeByConfinedWh","sizeByDistortedWh","sizeByH","sizeByPct","sizeByW","sizeByWh","sizeByWhListed","sizeByForcedWh"],3:["baseUriRedirect","canonicalLinkHeader","cors","jsonldMediaType","mirroring","profileLinkHeader","regionByPct","regionByPx","regionSquare","rotationArbitrary","rotationBy90s","sizeByConfinedWh","sizeByH","sizeByPct","sizeByW","sizeByWh","sizeUpscaling"]},P=class extends at.default{constructor(r,t,s=!1,a=Pt,o=rt,h=st,g=Ht,p=Mt){super(r,t,a,o,h,g,p);this.enableHD=s}regionImpl(r,t){r&&(t=t.extract({left:r.x,top:r.y,width:r.w,height:r.h}))}sizeImpl(r,t){r&&(t=t.resize({...r,fit:"fill"}))}rotationImpl(r,t){r&&(r.isMirror&&(t=t.flop()),t=t.rotate(r.degree))}qualityFormatImpl(r,t,s,a,o){r&&(r==="color"?s=s.toColorspace("srgb"):r==="gray"||r==="grey"?s=s.grayscale():r==="bitonal"&&(s=s.threshold())),t&&(s=s.toFormat(t)),a==="png"&&(o.paletteBitDepth===1||o.space==="b-w")&&(s=s.toColorspace("b-w").png({colors:2}))}iiifTransform(r,t){let s=this.enableHD?(0,f.default)({limitInputPixels:!1,sequentialRead:!0}):(0,f.default)(),a=r.split("/"),o=a.pop(),h=o.split(".")[0],g=o.split(".")[1],p=a.pop(),w=a.pop(),R=a.pop(),H=!1,l=s,c={width:t.width,height:t.height};t&&t.orientation&&(H=!0,l=l.rotate(),t.orientation>=5&&(c={width:t.height,height:t.width}));let d=super.normalizeRegionParams(R,c);this.regionImpl(d,l);let m=super.normalizeSizeParams(w,{width:d?d.w:c.width,height:d?d.h:c.height});this.sizeImpl(m,l);let y=super.normalizeRotationParams(p);this.rotationImpl(y,l);let z=super.normalizeQualityParams(h),M=super.normalizeFormatParams(g,t.format);return this.qualityFormatImpl(z,M,l,g,t),d||m||y||z||M||H?l:(console.log("skip iiif transform"),null)}async generateIiifImage(r,t,s=256){let a=this.enableHD?(0,f.default)(t,{limitInputPixels:!1,sequentialRead:!0}):(0,f.default)(t),o=r.split("/"),h=o.pop(),g=h.split(".")[0],p=h.split(".")[1],w=o.pop(),R=o.pop(),H=o.pop(),l=await a.metadata(),c={width:l.width,height:l.height},d={},m=a,y=super.normalizeRegionParams(H,c);this.regionImpl(y,m);let z=super.normalizeSizeParams(R,{width:y?y.w:c.width,height:y?y.h:c.height});this.sizeImpl(z,m);let{x:M,y:ot,w:T,h:A}=y||{x:0,y:0,w:c.width,h:c.height},{width:D,height:F}=z||{width:c.width,height:c.height};try{if(T*A>D*F*s&&D<rt&&F<st&&l.pages&&l.pages>1){console.log("thumbnail");let v=Math.max(D/T,F/A),[L,N,W,I]=[Math.round(M*v),Math.round(ot*v),Math.round(T*v),Math.round(A*v)],[C,U]=[Math.round(c.width*v),Math.round(c.height*v)];console.log("rawMeta.pages: "+l.pages);let u=Math.max(0,l.pages-1);console.log("selectedPage1: "+u),u>=l.pages&&(console.log("Warning: Selected page exceeds available pages, using last available page"),u=Math.max(0,l.pages-1));try{let B=this.enableHD?await(0,f.default)(t,{limitInputPixels:!1,page:u,sequentialRead:!0}).metadata():await(0,f.default)(t,{page:u}).metadata(),Y=B.width,G=B.height;for(;u>0&&(G<U||Y<C);)G*=2,Y*=2,u--;(u<0||u>=l.pages)&&(console.log("Warning: Invalid page index after calculation, using page 0"),u=0),console.log("selectedPage2: "+u);let _={width:C,height:U,fastShrinkOnLoad:!1},J=this.enableHD?await(0,f.default)(t,{limitInputPixels:!1,page:u,sequentialRead:!0}).resize(_).toBuffer():await(0,f.default)(t,{page:u}).resize(_).toBuffer();m=this.enableHD?(0,f.default)(J,{limitInputPixels:!1,sequentialRead:!0}):(0,f.default)(J);let $=await m.metadata();N+I>$.height&&(I=$.height-N,console.log("adjested sh: "+I)),L+W>$.width&&(W=$.width-L,console.log("adjusted sw: "+W));let Q={x:L,y:N,w:W,h:I};this.regionImpl(Q,m),this.sizeImpl(z,m),console.log($.width,$.height,_.width,_.height,Q,z)}catch(B){console.log(B)}}}catch(v){console.log(v)}let nt=super.normalizeRotationParams(w);this.rotationImpl(nt,m);let ht=super.normalizeQualityParams(g);console.log(p,l.format);let X=super.normalizeFormatParams(p,l.format);return this.qualityFormatImpl(ht,X,m,p,l),d.body=m,d.contentType=super.getContentType(X),d}generateImageInfo(r,t,s=""){let a=this.version,o=this.level,h={width:t.width,height:t.height};t&&t.orientation&&t.orientation>=5&&(console.log(t.orientation),h={width:t.height,height:t.width});let g=super.generateImageInfoTemp(r,h),p=g.info;if((a==="2.0"||a==="2.1")&&(p.profile=[super.uriPattern("compliance")],o==="2")){let w={};w.formats=["jpg","png","webp","jp2","pdf","tif"],w.qualities=["color","gray","bitonal","default","native","grey"],w.supports=Wt[Number(a)],p.profile.push(w)}return a==="3.0"&&s&&(p.extraFormats=[s],p.preferredFormats=[s]),s&&(p.formats=[s]),g}};module.exports=P;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@etu-wiki/sharp-iiif-shims",
3
- "version": "0.0.91",
3
+ "version": "0.0.93",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {