@etu-wiki/sharp-iiif-shims 0.0.37 → 0.0.39
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.
- package/dist/index.d.ts +23 -0
- package/dist/index.js +1 -1
- package/package.json +15 -15
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import sharp from 'sharp';
|
|
2
|
+
import IiifImageShims from '@etu-wiki/iiif-image-shims';
|
|
3
|
+
type Dims = {
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
};
|
|
7
|
+
export default class SharpIiifImageShims extends IiifImageShims {
|
|
8
|
+
pipeline: sharp.Sharp;
|
|
9
|
+
version: string;
|
|
10
|
+
level: string;
|
|
11
|
+
tileWidth: number;
|
|
12
|
+
tileHeight: number;
|
|
13
|
+
constructor(version: string, level: string, maxArea?: number, maxWidth?: number, maxHeight?: number, tileWidth?: number, tileHeight?: number);
|
|
14
|
+
private regionImpl;
|
|
15
|
+
private sizeImpl;
|
|
16
|
+
private rotationImpl;
|
|
17
|
+
private qualityImpl;
|
|
18
|
+
private formatImpl;
|
|
19
|
+
iiifTransform(path: string, dims: Dims, source: sharp.Sharp): sharp.Sharp;
|
|
20
|
+
generateIiifImage(path: string, sourceImage: any): Promise<any>;
|
|
21
|
+
generateImageInfo(id: string, dims: Dims): any;
|
|
22
|
+
}
|
|
23
|
+
export {};
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
!function(t,i){if("object"==typeof exports&&"object"==typeof module)module.exports=i();else if("function"==typeof define&&define.amd)define([],i);else{var e=i();for(var o in e)("object"==typeof exports?exports:t)[o]=e[o]}}(global,(()=>{return t={958:t=>{var i;global,i=()=>(()=>{"use strict";var t={607:(t,i)=>{Object.defineProperty(i,"__esModule",{value:!0});var e="[1-9][0-9]*",o="[0-9]*",r="(?=.*[1-9])\\d+(\\.\\d+)?",n="\\d+(\\.\\d+)?",a={jpg:"image/jpeg",png:"image/png",webp:"image/webp",tif:"image/tiff",gif:"image/gif",jp2:"image/jp2",pdf:"application/pdf"},s={version:["1.0","1.1","2.0","2.1","3.0"],level:["0","1","2"],region:["full","square","".concat(o,",").concat(o,",").concat(e,",").concat(e),"pct:".concat(n,",").concat(n,",").concat(r,",").concat(r)],rotation:"!?".concat(n),quality:["color","gray","bitonal","default","native","grey"],format:["jpg","png","webp","tif","gif"],size:[]},h={1:["full","".concat(e,","),",".concat(e),"pct:".concat(r),"".concat(e,",").concat(e),"!".concat(e,",").concat(e)],1.1:["full","".concat(e,","),",".concat(e),"pct:".concat(r),"".concat(e,",").concat(e),"!".concat(e,",").concat(e)],2:["full","".concat(e,","),",".concat(e),"pct:".concat(r),"".concat(e,",").concat(e),"!".concat(e,",").concat(e)],2.1:["full","max","".concat(e,","),",".concat(e),"pct:".concat(r),"".concat(e,",").concat(e),"!".concat(e,",").concat(e)],3:["max","".concat(e,","),",".concat(e),"pct:".concat(r),"".concat(e,",").concat(e),"!".concat(e,",").concat(e),"\\^max","\\^".concat(e,","),"\\^,".concat(e),"\\^pct:".concat(r),"\\^".concat(e,",").concat(e),"\\^!".concat(e,",").concat(e)]},c=function(){function t(t,i,e,o,r,n,a,c){void 0===o&&(o=4e6),void 0===r&&(r=2e3),void 0===n&&(n=2e3),void 0===a&&(a=512),void 0===c&&(c=512),this.version=t,this.level=i,this.maxArea=o,this.maxWidth=r,this.maxHeight=n,this.tileWidth=a,this.tileHeight=c,this.normalImageType=e,this.validate("version",t),this.validate("level",i),s.size=h[Number(t)]}return t.prototype.validator=function(t){var i=s[t];return i instanceof Array&&(i=i.join("|")),new RegExp("^("+i+")$")},t.prototype.validate=function(t,i){if(!this.validator(t).test(i))throw Error("invalid IIIF url ".concat(t,": ").concat(i));return!0},t.prototype.normalizeRegionParams=function(t,i){if(this.validate("region",t),"full"===t)return null;if("square"===t)return this._regionSquare(i);if(t.match(/^pct:([\d,]+)/))return this._regionPct(RegExp.$1,i);var e=t.split(",").map((function(t){return Number(t)}));return this._regionXYWH(e,i)},t.prototype._regionSquare=function(t){var i,e,o=t.width,r=t.height;if(o!==r){var n=Math.min(o,r),a=Math.abs(Math.floor((o-r)/2));return o>r?(i=a,e=0):(i=0,e=a),this._regionXYWH([i,e,n,n],t)}},t.prototype._regionPct=function(t,i){var e=t.split(",").map((function(t,e){var o=Number(t)/100;return 0===e||2===e?Math.round(i.width*o):1===e||3===e?Math.round(i.height*o):void 0}));return this._regionXYWH(e,i)},t.prototype._regionXYWH=function(t,i){var e=i.width,o=i.height,r=t[0],n=t[1],a=t[2],s=t[3];return r+a>e&&(a=e-r),n+s>o&&(s=o-n),{x:r,y:n,w:a,h:s}},t.prototype.normalizeSizeParams=function(t,i){var e=t,o=i.width,r=i.height;this.validate("size",e);var n,a=!1;if("^"===e.charAt(0)&&(a=!0,e=e.substring(1)),"full"===e)return null;if("max"===e?n=this._sizeMaxScale(i):e.match(/^pct:([\d]+)/)?n=this._sizePctScale(RegExp.$1):"!"===e.charAt(0)&&(e=e.substring(1),n=this._sizeBangScale(e,a,i)),1===n)return null;if(n){var s=Math.round(o*n),h=Math.round(r*n);return this._sizeWH("".concat(s,",").concat(h),a,i)}return this._sizeWH(e,a,i)},t.prototype._sizeBangScale=function(t,i,e){var o=e.width,r=e.height,n=t.split(",").map((function(t){return""===t?null:Number(t)})),a=n[0],s=n[1],h=Math.min(a/o,s/r);if(a>o&&s>r&&!i)throw Error("should not upscale without ^");return h},t.prototype._sizeMaxScale=function(t){var i=t.width,e=t.height,o=1;return this.maxArea&&this.maxArea<i*e&&(o=Math.sqrt(this.maxArea/(i*e))),this.maxWidth&&this.maxWidth<i&&(o=Math.min(o,this.maxWidth/i)),this.maxHeight&&this.maxHeight<e&&(o=Math.min(o,this.maxHeight/e)),o},t.prototype._sizePctScale=function(t){return Number(t)/100},t.prototype._sizeWH=function(t,i,e){var o=e.width,r=e.height,n=t.split(",").map((function(t){return""===t?null:Number(t)})),a=n[0],s=n[1];if(a?s||(s=Math.round(r*a/o)):a=Math.round(o*s/r),(a>o||s>r)&&!i)throw Error("should not upscale without ^");return{width:a,height:s}},t.prototype.normalizeRotationParams=function(t){this.validate("rotation",t);var i=!1;return"0"===t?null:("!"===t[0]&&(i=!0),{isMirror:i,degree:Number(t.replace(/^!/,""))})},t.prototype.normalizeQualityParams=function(t){return this.validate("quality",t),t},t.prototype.normalizeFormatParams=function(t){return"1.0"!==this.version&&"1.1"!==this.version&&"2.0"!==this.version&&"2.1"!==this.version||t?(this.validate("format",t),t):"png"},t.prototype.getContentType=function(t){return a[t]},t.prototype.generateImageInfoTemp=function(t,i){var e={},o={};e.info=o;var r=this.version;return"1.0"===r?(o.identifier=t,o.width=i.width,o.height=i.height,o.scale_factors=this.factors(i),o.tile_width=this.tileWidth,o.tile_height=this.tileHeight,o.formats=s.format,o.qualities=s.quality,o.profile=this.uriPattern("compliance"),e.contentType="application/json",e.body=JSON.stringify(o)):("1.1"===r?(o["@context"]=this.uriPattern("context"),o["@id"]=t,o.width=i.width,o.height=i.height,o.scale_factors=this.factors(i),o.tile_width=this.tileWidth,o.tile_height=this.tileHeight,o.formats=s.format,o.qualities=s.quality,o.profile=this.uriPattern("compliance")):"2.0"===r||"2.1"===r?(o["@context"]=this.uriPattern("context"),o["@id"]=t,o.protocol="http://iiif.io/api/image",o.width=i.width,o.height=i.height,"0"===this.level?o.sizes=this.sizes(i):o.tiles=[{scaleFactors:this.factors(i),width:this.tileWidth,height:this.tileHeight}],o.profile=[this.uriPattern("compliance")]):"3.0"===r&&(o["@context"]=[this.uriPattern("context")],o.id=t,o.type="ImageService3",o.protocol="http://iiif.io/api/image",o.width=i.width,o.height=i.height,"0"===this.level?o.sizes=this.sizes(i):o.tiles=[{scaleFactors:this.factors(i),width:this.tileWidth,height:this.tileHeight}],o.profile=this.uriPattern("compliance")),e.contentType="application/ld+json"),e},t.prototype.factors=function(t){for(var i=1,e=[1],o=t.width,r=t.height;!(this.tileWidth*i>o&&this.tileHeight*i>r);)i*=2,e.push(i);return e},t.prototype.uriPattern=function(t){var i,e;return"1.0"===this.version?i="http://library.stanford.edu/iiif/image-api/compliance.html#level".concat(this.level):"1.1"===this.version?(i="http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level".concat(this.level),e="http://library.stanford.edu/iiif/image-api/1.1/context.json"):"2.0"===this.version||"2.1"===this.version?(i="http://iiif.io/api/image/2/level".concat(this.level,".json"),e="http://iiif.io/api/image/2/context.json"):"3.0"===this.version&&(i="level".concat(this.level),e="http://iiif.io/api/image/3/context.json"),"compliance"===t?i:"context"===t?e:void 0},t.prototype.sizes=function(t){return this.normalizeSizeParams("!200,200",{width:t.width,height:t.height}),this.normalizeSizeParams("!50,50",{width:t.width,height:t.height}),[{width:t.width,height:t.height}]},t}();i.default=c,t.exports=c}},i={};return function e(o){var r=i[o];if(void 0!==r)return r.exports;var n=i[o]={exports:{}};return t[o](n,n.exports,e),n.exports}(607)})(),t.exports=i()},607:function(t,i,e){"use strict";var o,r=this&&this.__extends||(o=function(t,i){return o=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,i){t.__proto__=i}||function(t,i){for(var e in i)Object.prototype.hasOwnProperty.call(i,e)&&(t[e]=i[e])},o(t,i)},function(t,i){if("function"!=typeof i&&null!==i)throw new TypeError("Class extends value "+String(i)+" is not a constructor or null");function e(){this.constructor=t}o(t,i),t.prototype=null===i?Object.create(i):(e.prototype=i.prototype,new e)}),n=this&&this.__assign||function(){return n=Object.assign||function(t){for(var i,e=1,o=arguments.length;e<o;e++)for(var r in i=arguments[e])Object.prototype.hasOwnProperty.call(i,r)&&(t[r]=i[r]);return t},n.apply(this,arguments)},a=this&&this.__awaiter||function(t,i,e,o){return new(e||(e=Promise))((function(r,n){function a(t){try{h(o.next(t))}catch(t){n(t)}}function s(t){try{h(o.throw(t))}catch(t){n(t)}}function h(t){var i;t.done?r(t.value):(i=t.value,i instanceof e?i:new e((function(t){t(i)}))).then(a,s)}h((o=o.apply(t,i||[])).next())}))},s=this&&this.__generator||function(t,i){var e,o,r,n,a={label:0,sent:function(){if(1&r[0])throw r[1];return r[1]},trys:[],ops:[]};return n={next:s(0),throw:s(1),return:s(2)},"function"==typeof Symbol&&(n[Symbol.iterator]=function(){return this}),n;function s(s){return function(h){return function(s){if(e)throw new TypeError("Generator is already executing.");for(;n&&(n=0,s[0]&&(a=0)),a;)try{if(e=1,o&&(r=2&s[0]?o.return:s[0]?o.throw||((r=o.return)&&r.call(o),0):o.next)&&!(r=r.call(o,s[1])).done)return r;switch(o=0,r&&(s=[2&s[0],r.value]),s[0]){case 0:case 1:r=s;break;case 4:return a.label++,{value:s[1],done:!1};case 5:a.label++,o=s[1],s=[0];continue;case 7:s=a.ops.pop(),a.trys.pop();continue;default:if(!((r=(r=a.trys).length>0&&r[r.length-1])||6!==s[0]&&2!==s[0])){a=0;continue}if(3===s[0]&&(!r||s[1]>r[0]&&s[1]<r[3])){a.label=s[1];break}if(6===s[0]&&a.label<r[1]){a.label=r[1],r=s;break}if(r&&a.label<r[2]){a.label=r[2],a.ops.push(s);break}r[2]&&a.ops.pop(),a.trys.pop();continue}s=i.call(t,a)}catch(t){s=[6,t],o=0}finally{e=r=0}if(5&s[0])throw s[1];return{value:s[0]?s[1]:void 0,done:!0}}([s,h])}}},h=this&&this.__importDefault||function(t){return t&&t.__esModule?t:{default:t}};Object.defineProperty(i,"__esModule",{value:!0});var c=h(e(441)),l=h(e(958)),p={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"]},u=function(t){function i(i,e,o,r,n,a,s,h){return void 0===r&&(r=4e6),void 0===n&&(n=2e3),void 0===a&&(a=2e3),void 0===s&&(s=512),void 0===h&&(h=512),t.call(this,i,e,o,r,n,a,s,h)||this}return r(i,t),i.prototype.regionImpl=function(t){t&&(this.pipeline=this.pipeline.extract({left:t.x,top:t.y,width:t.w,height:t.h}))},i.prototype.sizeImpl=function(t){t&&(this.pipeline=this.pipeline.resize(n(n({},t),{fit:"fill"})))},i.prototype.rotationImpl=function(t){t&&(t.isMirror&&(this.pipeline=this.pipeline.flop()),this.pipeline=this.pipeline.rotate(t.degree))},i.prototype.qualityImpl=function(t){"native"===t||"default"===t||("color"===t?this.pipeline=this.pipeline.toColorspace("srgb"):"gray"===t||"grey"===t?this.pipeline=this.pipeline.grayscale():"bitonal"===t&&(this.pipeline=this.pipeline.threshold()))},i.prototype.formatImpl=function(t){t="tif"===t?"tiff":t,this.pipeline=this.pipeline.toFormat(t)},i.prototype.generateIiifImage=function(i,e){return a(this,void 0,void 0,(function(){var o,r,n,a,h,l,p,u,f,d,g,m,y,v,w,x,b,z,P,_,B,M,I,W,j,H,q,S,R,A,F,T,O,k,L,N,E,C,U,X,Y;return s(this,(function(s){switch(s.label){case 0:return o=(0,c.default)(e,{limitInputPixels:!1,sequentialRead:!0}),r=i.split("/"),n=r.pop(),a=n.split(".")[0],h=n.split(".")[1],l=r.pop(),p=r.pop(),u=r.pop(),[4,this.getMetadata(e)];case 1:return f=s.sent(),d={width:f.width,height:f.height},g={},this.pipeline=o,m=t.prototype.normalizeRegionParams.call(this,u,d),this.regionImpl(m),y=t.prototype.normalizeSizeParams.call(this,p,{width:m?m.w:d.width,height:m?m.h:d.height}),this.sizeImpl(y),w=(v=m||{x:0,y:0,w:d.width,h:d.height}).x,x=v.y,b=v.w,z=v.h,_=(P=y||{width:d.width,height:d.height}).width,B=P.height,b*z>_*B*256&&_<1e4&&B<1e4?(console.log("thumbnail"),M=Math.max(_/b,B/z),I=[Math.round(w*M),Math.round(x*M),Math.round(b*M),Math.round(z*M)],W=I[0],j=I[1],H=I[2],q=I[3],S=[Math.round(d.width*M),Math.round(d.height*M)],R=S[0],A=S[1],F=f.pages-1,[4,(0,c.default)(e,{limitInputPixels:!1,page:F,sequentialRead:!0}).metadata()]):[3,5];case 2:for(T=s.sent(),O=T.width,k=T.height;k<A||O<R;)k*=2,O*=2,F--;return console.log("selectedPage: "+F),L={width:R,height:A,fastShrinkOnLoad:!1},[4,(0,c.default)(e,{limitInputPixels:!1,page:F,sequentialRead:!0}).resize(L).toBuffer()];case 3:return N=s.sent(),this.pipeline=(0,c.default)(N),[4,this.pipeline.metadata()];case 4:E=s.sent(),j+q>E.height&&(q=E.height-j,console.log("adjested sh: "+q)),W+H>E.width&&(H=E.width-W,console.log("adjusted sw: "+H)),C={x:W,y:j,w:H,h:q},this.regionImpl(C),this.sizeImpl(y),console.log(E.width,E.height,L.width,L.height,C,y),s.label=5;case 5:return U=t.prototype.normalizeRotationParams.call(this,l),this.rotationImpl(U),X=t.prototype.normalizeQualityParams.call(this,a),this.qualityImpl(X),Y=t.prototype.normalizeFormatParams.call(this,h),this.formatImpl(Y),g.body=this.pipeline,g.contentType=t.prototype.getContentType.call(this,Y),[2,g]}}))}))},i.prototype.generateImageInfo=function(i,e){var o=this.version,r=this.level,n=t.prototype.generateImageInfoTemp.call(this,i,e),a=n.info;if(("2.0"===o||"2.1"===o)&&(a.profile=[t.prototype.uriPattern.call(this,"compliance")],"2"===r)){var s={formats:["jpg","png","webp","tif"],qualities:["color","gray","bitonal","default","native","grey"]};s.supports=p[Number(o)],a.profile.push(s)}return"3.0"===o&&(a.extraFormats=["webp"],a.preferredFormats=["webp"]),n},i.prototype.getMetadata=function(t){return a(this,void 0,void 0,(function(){return s(this,(function(i){switch(i.label){case 0:return[4,(0,c.default)(t,{limitInputPixels:!1,sequentialRead:!0}).metadata()];case 1:return[2,i.sent()]}}))}))},i.prototype.normalize=function(t){return a(this,void 0,void 0,(function(){var i,e,o,r,n;return s(this,(function(a){switch(a.label){case 0:return[4,(i=(0,c.default)(t,{limitInputPixels:!1,sequentialRead:!0})).metadata()];case 1:return e=a.sent(),o=e.width.toString(),r=e.height.toString(),e&&e.orientation&&(i=i.rotate(),e.orientation>=5&&(n=o,o=r,r=n)),[4,i.tiff({pyramid:!0,tile:!0}).toBuffer()];case 2:return[2,{buffer:a.sent(),width:o,height:r}]}}))}))},i.prototype.derive=function(t,i){return a(this,void 0,void 0,(function(){return s(this,(function(e){switch(e.label){case 0:return[4,(0,c.default)(t,{limitInputPixels:!1,sequentialRead:!0}).tile({size:512,layout:"iiif"}).toFile("/tmp/".concat(i))];case 1:return e.sent(),[2]}}))}))},i}(l.default);i.default=u,t.exports=u},441:t=>{"use strict";t.exports=require("sharp")}},i={},function e(o){var r=i[o];if(void 0!==r)return r.exports;var n=i[o]={exports:{}};return t[o].call(n.exports,n,n.exports,e),n.exports}(607);var t,i}));
|
|
1
|
+
"use strict";var ni=Object.create;var P=Object.defineProperty;var oi=Object.getOwnPropertyDescriptor;var hi=Object.getOwnPropertyNames;var li=Object.getPrototypeOf,pi=Object.prototype.hasOwnProperty;var gi=(e,t)=>()=>(t||e((t={exports:{}}).exports,t),t.exports),ci=(e,t)=>{for(var i in t)P(e,i,{get:t[i],enumerable:!0})},C=(e,t,i,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of hi(t))!pi.call(e,s)&&s!==i&&P(e,s,{get:()=>t[s],enumerable:!(r=oi(t,s))||r.enumerable});return e};var Q=(e,t,i)=>(i=e!=null?ni(li(e)):{},C(t||!e||!e.__esModule?P(i,"default",{value:e,enumerable:!0}):i,e)),mi=e=>C(P({},"__esModule",{value:!0}),e);var K=gi((Bi,J)=>{"use strict";var F=Object.defineProperty,di=Object.getOwnPropertyDescriptor,ui=Object.getOwnPropertyNames,fi=Object.prototype.hasOwnProperty,yi=(e,t)=>{for(var i in t)F(e,i,{get:t[i],enumerable:!0})},zi=(e,t,i,r)=>{if(t&&typeof t=="object"||typeof t=="function")for(let s of ui(t))!fi.call(e,s)&&s!==i&&F(e,s,{get:()=>t[s],enumerable:!(r=di(t,s))||r.enumerable});return e},wi=e=>zi(F({},"__esModule",{value:!0}),e),Y={};yi(Y,{default:()=>$i});J.exports=wi(Y);var n="[1-9][0-9]*",U="[0-9]*",m="(?=.*[1-9])\\d+(\\.\\d+)?",q="\\d+(\\.\\d+)?",G=(e=>(e.jpg="image/jpeg",e.png="image/png",e.webp="image/webp",e.tif="image/tiff",e.gif="image/gif",e.jp2="image/jp2",e.pdf="application/pdf",e))(G||{}),y={version:["1.0","1.1","2.0","2.1","3.0"],level:["0","1","2"],region:["full","square",`${U},${U},${n},${n}`,`pct:${q},${q},${m},${m}`],rotation:`!?${q}`,quality:["color","gray","bitonal","default","native","grey"],format:["jpg","png","webp","tif","gif","jp2","pdf"],size:[""]},vi={1:["full",`${n},`,`,${n}`,`pct:${m}`,`${n},${n}`,`!${n},${n}`],1.1:["full",`${n},`,`,${n}`,`pct:${m}`,`${n},${n}`,`!${n},${n}`],2:["full",`${n},`,`,${n}`,`pct:${m}`,`${n},${n}`,`!${n},${n}`],2.1:["full","max",`${n},`,`,${n}`,`pct:${m}`,`${n},${n}`,`!${n},${n}`],3:["max",`${n},`,`,${n}`,`pct:${m}`,`${n},${n}`,`!${n},${n}`,"\\^max",`\\^${n},`,`\\^,${n}`,`\\^pct:${m}`,`\\^${n},${n}`,`\\^!${n},${n}`]},x=/^pct:([\d,]+)/,$i=class{constructor(e,t,i,r,s,a,o){this.version=e,this.level=t,this.maxArea=i,this.maxWidth=r,this.maxHeight=s,this.tileWidth=a,this.tileHeight=o,this.validate("version",e),this.validate("level",t),y.size=vi[Number(e)]}validator(e){var t=y[e];return t instanceof Array&&(t=t.join("|")),new RegExp("^("+t+")$")}validate(e,t){if(!this.validator(e).test(t))throw Error(`invalid IIIF url ${e}: ${t}`);return!0}normalizeRegionParams(e,t){if(this.validate("region",e),e==="full")return null;if(e==="square")return this._regionSquare(t);if(x.test(e))return this._regionPct(e.match(x)[0],t);{let i=e.split(",").map(r=>Number(r));return this._regionXYWH(i,t)}}_regionSquare(e){let t,i,r=e.width,s=e.height;if(r!==s){let a=Math.min(r,s),o=Math.abs(Math.floor((r-s)/2));return r>s?(t=o,i=0):(t=0,i=o),this._regionXYWH([t,i,a,a],e)}return null}_regionPct(e,t){let i=e.split(",").map((r,s)=>{let a=Number(r)/100;return s===0||s===2?Math.round(t.width*a):s===1||s===3?Math.round(t.height*a):null});return this._regionXYWH(i,t)}_regionXYWH(e,t){let i=t.width,r=t.height,[s,a,o,h]=e;return s+o>i&&(o=i-s),a+h>r&&(h=r-a),{x:s,y:a,w:o,h}}normalizeSizeParams(e,t){let i=e,r=t.width,s=t.height;this.validate("size",i);let a=!1;i.charAt(0)==="^"&&(a=!0,i=i.substring(1));let o=null;if(i==="full"||(i==="max"?o=this._sizeMaxScale(t):x.test(i)?o=this._sizePctScale(i.match(x)[0]):i.charAt(0)==="!"&&(i=i.substring(1),o=this._sizeBangScale(i,a,t)),o===1))return null;if(o){let h=Math.round(r*o),l=Math.round(s*o);return this._sizeWH(`${h},${l}`,a,t)}else return this._sizeWH(i,a,t)}_sizeBangScale(e,t,i){let r=i.width,s=i.height,[a,o]=e.split(",").map(l=>l===""?null:Number(l)),h=Math.min(a/r,o/s);if(a>r&&o>s&&!t)throw Error("should not upscale without ^");return h}_sizeMaxScale(e){let t=e.width,i=e.height,r=1;return this.maxArea&&this.maxArea<t*i&&(r=Math.sqrt(this.maxArea/(t*i))),this.maxWidth&&this.maxWidth<t&&(r=Math.min(r,this.maxWidth/t)),this.maxHeight&&this.maxHeight<i&&(r=Math.min(r,this.maxHeight/i)),r}_sizePctScale(e){return Number(e)/100}_sizeWH(e,t,i){let r=i.width,s=i.height,[a,o]=e.split(",").map(h=>h===""?null:Number(h));if(a?o||(o=Math.round(s*a/r)):a=Math.round(r*o/s),(a>r||o>s)&&!t)throw Error("should not upscale without ^");return{width:a,height:o}}normalizeRotationParams(e){this.validate("rotation",e);let t=!1;if(e==="0")return null;e[0]==="!"&&(t=!0);let i=Number(e.replace(/^!/,""));return{isMirror:t,degree:i}}normalizeQualityParams(e){return this.validate("quality",e),e}normalizeFormatParams(e){return(this.version==="1.0"||this.version==="1.1"||this.version==="2.0"||this.version==="2.1")&&!e?"png":(this.validate("format",e),e)}getContentType(e){return G[e]}generateImageInfoTemp(e,t){let i={},r={};i.info=r;let s=this.version;return s==="1.0"?(r.identifier=e,r.width=t.width,r.height=t.height,r.scale_factors=this.factors(t),r.tile_width=this.tileWidth,r.tile_height=this.tileHeight,r.formats=y.format,r.qualities=y.quality,r.profile=this.uriPattern("compliance"),i.contentType="application/json",i.body=JSON.stringify(r)):(s==="1.1"?(r["@context"]=this.uriPattern("context"),r["@id"]=e,r.width=t.width,r.height=t.height,r.scale_factors=this.factors(t),r.tile_width=this.tileWidth,r.tile_height=this.tileHeight,r.formats=y.format,r.qualities=y.quality,r.profile=this.uriPattern("compliance")):s==="2.0"||s==="2.1"?(r["@context"]=this.uriPattern("context"),r["@id"]=e,r.protocol="http://iiif.io/api/image",r.width=t.width,r.height=t.height,this.level==="0"?r.sizes=this.sizes(t):r.tiles=[{scaleFactors:this.factors(t),width:this.tileWidth,height:this.tileHeight}],r.profile=[this.uriPattern("compliance")]):s==="3.0"&&(r["@context"]=[this.uriPattern("context")],r.id=e,r.type="ImageService3",r.protocol="http://iiif.io/api/image",r.width=t.width,r.height=t.height,this.level==="0"?r.sizes=this.sizes(t):r.tiles=[{scaleFactors:this.factors(t),width:this.tileWidth,height:this.tileHeight}],r.profile=this.uriPattern("compliance")),i.contentType="application/ld+json"),i}factors(e){let t=1,i=[1],r=e.width,s=e.height;for(;!(this.tileWidth*t>r&&this.tileHeight*t>s);)t*=2,i.push(t);return i}uriPattern(e){let t="",i="";return this.version==="1.0"?t=`http://library.stanford.edu/iiif/image-api/compliance.html#level${this.level}`:this.version==="1.1"?(t=`http://library.stanford.edu/iiif/image-api/1.1/compliance.html#level${this.level}`,i="http://library.stanford.edu/iiif/image-api/1.1/context.json"):this.version==="2.0"||this.version==="2.1"?(t=`http://iiif.io/api/image/2/level${this.level}.json`,i="http://iiif.io/api/image/2/context.json"):this.version==="3.0"&&(t=`level${this.level}`,i="http://iiif.io/api/image/3/context.json"),e==="compliance"?t:e==="context"?i:""}sizes(e){return[{width:e.width,height:e.height}]}}});var Mi={};ci(Mi,{default:()=>I});module.exports=mi(Mi);var w=Q(require("sharp")),ii=Q(K()),bi=1e8,V=1e4,Z=1e4,Pi=512,xi=512,Ii=256,Hi={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"]},I=class extends ii.default{constructor(i,r,s=bi,a=V,o=Z,h=Pi,l=xi){super(i,r,s,a,o,h,l)}regionImpl(i){i&&(this.pipeline=this.pipeline.extract({left:i.x,top:i.y,width:i.w,height:i.h}))}sizeImpl(i){i&&(this.pipeline=this.pipeline.resize({...i,fit:"fill"}))}rotationImpl(i){i&&(i.isMirror&&(this.pipeline=this.pipeline.flop()),this.pipeline=this.pipeline.rotate(i.degree))}qualityImpl(i){i==="native"||i==="default"||(i==="color"?this.pipeline=this.pipeline.toColorspace("srgb"):i==="gray"||i==="grey"?this.pipeline=this.pipeline.grayscale():i==="bitonal"&&(this.pipeline=this.pipeline.threshold()))}formatImpl(i){i=i==="tif"?"tiff":i,this.pipeline=this.pipeline.toFormat(i)}iiifTransform(i,r,s){let a=i.split("/"),o=a.pop(),h=o.split(".")[0],l=o.split(".")[1],H=a.pop(),M=a.pop(),W=a.pop();this.pipeline=s;let p=super.normalizeRegionParams(W,r);this.regionImpl(p);let g=super.normalizeSizeParams(M,{width:p?p.w:r.width,height:p?p.h:r.height});this.sizeImpl(g);let z=super.normalizeRotationParams(H);this.rotationImpl(z);let c=super.normalizeQualityParams(h);this.qualityImpl(c);let d=super.normalizeFormatParams(l);return this.formatImpl(d),this.pipeline}async generateIiifImage(i,r){let s=(0,w.default)(r,{limitInputPixels:!1,sequentialRead:!0}),a=i.split("/"),o=a.pop(),h=o.split(".")[0],l=o.split(".")[1],H=a.pop(),M=a.pop(),W=a.pop(),p=await s.metadata(),g={width:p.width,height:p.height},z={};this.pipeline=s;let c=super.normalizeRegionParams(W,g);this.regionImpl(c);let d=super.normalizeSizeParams(M,{width:c?c.w:g.width,height:c?c.h:g.height});this.sizeImpl(d);let{x:ti,y:ei,w:B,h:_}=c||{x:0,y:0,w:g.width,h:g.height},{width:j,height:S}=d||{width:g.width,height:g.height};if(B*_>j*S*Ii&&j<V&&S<Z&&p.pages&&p.pages>1){console.log("thumbnail");let u=Math.max(j/B,S/_),[R,T,v,$]=[Math.round(ti*u),Math.round(ei*u),Math.round(B*u),Math.round(_*u)],[E,N]=[Math.round(g.width*u),Math.round(g.height*u)],b=p.pages-1,O=await(0,w.default)(r,{limitInputPixels:!1,page:b,sequentialRead:!0}).metadata(),D=O.width,k=O.height;for(;k<N||D<E;)k*=2,D*=2,b--;console.log("selectedPage: "+b);let A={width:E,height:N,fastShrinkOnLoad:!1},ai=await(0,w.default)(r,{limitInputPixels:!1,page:b,sequentialRead:!0}).resize(A).toBuffer();this.pipeline=(0,w.default)(ai);let f=await this.pipeline.metadata();T+$>f.height&&($=f.height-T,console.log("adjested sh: "+$)),R+v>f.width&&(v=f.width-R,console.log("adjusted sw: "+v));let X={x:R,y:T,w:v,h:$};this.regionImpl(X),this.sizeImpl(d),console.log(f.width,f.height,A.width,A.height,X,d)}let ri=super.normalizeRotationParams(H);this.rotationImpl(ri);let si=super.normalizeQualityParams(h);this.qualityImpl(si);let L=super.normalizeFormatParams(l);return this.formatImpl(L),z.body=this.pipeline,z.contentType=super.getContentType(L),z}generateImageInfo(i,r){let s=this.version,a=this.level,o=super.generateImageInfoTemp(i,r),h=o.info;if((s==="2.0"||s==="2.1")&&(h.profile=[super.uriPattern("compliance")],a==="2")){let l={};l.formats=["jpg","png","webp","tif","jp2","pdf"],l.qualities=["color","gray","bitonal","default","native","grey"],l.supports=Hi[Number(s)],h.profile.push(l)}return s==="3.0"&&(h.extraFormats=["webp"],h.preferredFormats=["webp"]),o}};
|
package/package.json
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@etu-wiki/sharp-iiif-shims",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.39",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"clean": "rm -rf ./dist",
|
|
8
7
|
"test": "jest",
|
|
9
|
-
"build": "
|
|
10
|
-
"
|
|
8
|
+
"build": "npm run build:common && npm run build:types",
|
|
9
|
+
"build:common": "npx esbuild ./src/index.ts --outdir=dist --bundle --platform=node --minify --external:sharp",
|
|
10
|
+
"build:types": "tsc --emitDeclarationOnly --declaration --outDir dist --project tsconfig.spec.json",
|
|
11
11
|
"prepublishOnly": "npm run build"
|
|
12
12
|
},
|
|
13
13
|
"keywords": [
|
|
@@ -17,21 +17,21 @@
|
|
|
17
17
|
"author": "Joe Song",
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"devDependencies": {
|
|
20
|
-
"@babel/core": "^7.
|
|
21
|
-
"@babel/preset-env": "^7.
|
|
22
|
-
"@babel/preset-typescript": "^7.
|
|
23
|
-
"
|
|
24
|
-
"
|
|
25
|
-
"
|
|
26
|
-
"
|
|
27
|
-
"
|
|
28
|
-
"
|
|
20
|
+
"@babel/core": "^7.21.4",
|
|
21
|
+
"@babel/preset-env": "^7.21.4",
|
|
22
|
+
"@babel/preset-typescript": "^7.21.4",
|
|
23
|
+
"@types/jest": "^29.5.1",
|
|
24
|
+
"@types/node": "^18.16.2",
|
|
25
|
+
"babel-jest": "^29.5.0",
|
|
26
|
+
"esbuild": "^0.17.18",
|
|
27
|
+
"jest": "^29.5.0",
|
|
28
|
+
"typescript": "^5.0.4"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@etu-wiki/iiif-image-shims": "^0.0.
|
|
31
|
+
"@etu-wiki/iiif-image-shims": "^0.0.7",
|
|
32
32
|
"sharp": "^0.32.0"
|
|
33
33
|
},
|
|
34
34
|
"files": [
|
|
35
|
-
"dist
|
|
35
|
+
"dist"
|
|
36
36
|
]
|
|
37
37
|
}
|