@loaders.gl/images
Advanced tools
Comparing version 1.0.3 to 1.1.0-alpha.1
@@ -1,1 +0,1 @@ | ||
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var r in n)("object"==typeof exports?exports:e)[r]=n[r]}}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=3)}([function(e,t){},function(e,t){},function(e,t){},function(e,t,n){(function(t){const r=n(5),o="undefined"==typeof window?t:window;o.loaders=o.loaders||{},e.exports=Object.assign(o.loaders,r)}).call(this,n(4))},function(e,t){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(e){"object"==typeof window&&(n=window)}e.exports=n},function(e,t,n){"use strict";n.r(t);var r=n(0);const o=r.parseImageNode||"undefined"!=typeof ImageBitmap;function i(e,t){return r.parseImageNode?Object(r.parseImageNode)(e,t):u(e)}async function a(e,t){if("undefined"==typeof Image){const n=await fetch(e,t);return i(await n.arrayBuffer())}return await f(e,t)}function u(e){if("undefined"==typeof createImageBitmap)throw new Error("parseImage");const t=new Blob([new Uint8Array(e)]);return createImageBitmap(t)}async function f(e,t){let n;if(/\.svg((\?|#).*)?$/.test(e)){const r=await fetch(e,t),o=await r.text();n=`data:image/svg+xml;base64,${btoa(o)}`}else n=await e;return await new Promise((r,o)=>{try{const i=new Image;i.onload=(()=>r(i)),i.onerror=(t=>o(new Error(`Could not load image ${e}: ${t}`))),i.crossOrigin=t&&t.crossOrigin||"anonymous",i.src=n}catch(e){o(e)}})}var c={name:"Images",extension:[],parse:o&&i,loadAndParse:!o&&a};const s={parse:u},d={loadAndParse:f};var g=n(1);var l={name:"Images",extensions:["jpeg"],encode:function(e,t){if(g.encodeImageNode)return Object(g.encodeImageNode)(e,t);if(e instanceof HTMLCanvasElement)return e.toDataURL(t);!function(e,t){if(!e)throw new Error(t||"loader assertion failed.")}(e instanceof Image,"getImageData accepts image or canvas");const n=document.createElement("canvas");n.width=e.width,n.height=e.height,n.getContext("2d").drawImage(e,0,0);const r=n.toDataURL(t||"png").replace(/^data:image\/(png|jpg);base64,/,"");return Promise.resolve(r)},DEFAULT_OPTIONS:{type:"png"}},m=n(2);const p=!1,w=!0,y=new Map([["image/png",function(e){const t=j(e);if(t.byteLength<24||2303741511!==t.getUint32(0,p))return null;return{width:t.getUint32(16,p),height:t.getUint32(20,p)}}],["image/jpeg",function(e){const t=j(e);if(t.byteLength<2||65496!==t.getUint16(0,p))return null;const{tableMarkers:n,sofMarkers:r}=function(){const e=new Set([65499,65476,65484,65501,65534]);for(let t=65504;t<65520;++t)e.add(t);const t=new Set([65472,65473,65474,65475,65477,65478,65479,65481,65482,65483,65485,65486,65487,65502]);return{tableMarkers:e,sofMarkers:t}}();let o=2;for(;o<t.byteLength;){const e=t.getUint16(o,p);if(r.has(e))return{height:t.getUint16(o+5,p),width:t.getUint16(o+7,p)};if(!n.has(e))return null;o+=2,o+=t.getUint16(o,p)}return null}],["image/gif",function(e){const t=j(e);if(t.byteLength<10||1195984440!==t.getUint32(0,p))return null;return{width:t.getUint16(6,w),height:t.getUint16(8,w)}}],["image/bmp",function(e){const t=j(e);if(16973!==t.getUint16(0,p))return null;return{width:t.getUint32(18,w),height:t.getUint32(22,w)}}]]),b=`Invalid MIME type. Supported MIME types are: ${Array.from(y.keys()).join(", ")}`;function h(e){const t=v(e);return!!t&&t.mimeType}function I(e,t=null){if(t){const n=y.get(t);if(!n)throw new Error(b);const r=n(e);if(!r)throw new Error(`invalid image data for type: ${t}`);return r}const n=v(e,t);if(!n)throw new Error(b);return n}function v(e,t){for(const[t,n]of y.entries()){const r=n(e);if(r)return r.mimeType=t,r}return null}function j(e){if(m.bufferToArrayBuffer&&(e=Object(m.bufferToArrayBuffer)(e)),e instanceof ArrayBuffer||ArrayBuffer.isView(e))return new DataView(e.buffer||e);throw new Error("toDataView")}function U(e,t=null){return I(e)}function x(e,{mimeType:t="image/jpeg"}){const n=new Blob([e],{type:t}),r=(window.URL||window.webkitURL).createObjectURL(n);return new Promise((e,t)=>{const n=new Image;return n.onload=(()=>e(n)),n.onerror=t,n.src=r,n})}n.d(t,"ImageLoader",function(){return c}),n.d(t,"HTMLImageLoader",function(){return d}),n.d(t,"ImageBitmapLoader",function(){return s}),n.d(t,"ImageWriter",function(){return l}),n.d(t,"loadImage",function(){return a}),n.d(t,"isImage",function(){return h}),n.d(t,"getImageMetadata",function(){return I}),n.d(t,"decodeImage",function(){return x}),n.d(t,"getImageSize",function(){return U})}])}); | ||
!function(e,t){if("object"==typeof exports&&"object"==typeof module)module.exports=t();else if("function"==typeof define&&define.amd)define([],t);else{var n=t();for(var r in n)("object"==typeof exports?exports:e)[r]=n[r]}}(window,function(){return function(e){var t={};function n(r){if(t[r])return t[r].exports;var o=t[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,n),o.l=!0,o.exports}return n.m=e,n.c=t,n.d=function(e,t,r){n.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:r})},n.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},n.t=function(e,t){if(1&t&&(e=n(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var r=Object.create(null);if(n.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)n.d(r,o,function(t){return e[t]}.bind(null,o));return r},n.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return n.d(t,"a",t),t},n.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},n.p="",n(n.s=2)}([function(e,t,n){"use strict";(function(e,r){n.d(t,"a",function(){return i});"object"!=typeof e||"[object process]"!==String(e)||e.browser;const o={self:"undefined"!=typeof self&&self,window:"undefined"!=typeof window&&window,global:void 0!==r&&r},i=o.global||o.self||o.window}).call(this,n(3),n(1))},function(e,t){var n;n=function(){return this}();try{n=n||new Function("return this")()}catch(e){"object"==typeof window&&(n=window)}e.exports=n},function(e,t,n){(function(t){const r=n(4),o="undefined"==typeof window?t:window;o.loaders=o.loaders||{},e.exports=Object.assign(o.loaders,r)}).call(this,n(1))},function(e,t){var n,r,o=e.exports={};function i(){throw new Error("setTimeout has not been defined")}function a(){throw new Error("clearTimeout has not been defined")}function u(e){if(n===setTimeout)return setTimeout(e,0);if((n===i||!n)&&setTimeout)return n=setTimeout,setTimeout(e,0);try{return n(e,0)}catch(t){try{return n.call(null,e,0)}catch(t){return n.call(this,e,0)}}}!function(){try{n="function"==typeof setTimeout?setTimeout:i}catch(e){n=i}try{r="function"==typeof clearTimeout?clearTimeout:a}catch(e){r=a}}();var c,f=[],s=!1,l=-1;function d(){s&&c&&(s=!1,c.length?f=c.concat(f):l=-1,f.length&&g())}function g(){if(!s){var e=u(d);s=!0;for(var t=f.length;t;){for(c=f,f=[];++l<t;)c&&c[l].run();l=-1,t=f.length}c=null,s=!1,function(e){if(r===clearTimeout)return clearTimeout(e);if((r===a||!r)&&clearTimeout)return r=clearTimeout,clearTimeout(e);try{r(e)}catch(t){try{return r.call(null,e)}catch(t){return r.call(this,e)}}}(e)}}function m(e,t){this.fun=e,this.array=t}function p(){}o.nextTick=function(e){var t=new Array(arguments.length-1);if(arguments.length>1)for(var n=1;n<arguments.length;n++)t[n-1]=arguments[n];f.push(new m(e,t)),1!==f.length||s||u(g)},m.prototype.run=function(){this.fun.apply(null,this.array)},o.title="browser",o.browser=!0,o.env={},o.argv=[],o.version="",o.versions={},o.on=p,o.addListener=p,o.once=p,o.off=p,o.removeListener=p,o.removeAllListeners=p,o.emit=p,o.prependListener=p,o.prependOnceListener=p,o.listeners=function(e){return[]},o.binding=function(e){throw new Error("process.binding is not supported")},o.cwd=function(){return"/"},o.chdir=function(e){throw new Error("process.chdir is not supported")},o.umask=function(){return 0}},function(e,t,n){"use strict";n.r(t);var r=n(0);const o=!1,i=!0,a=new Map([["image/png",{test:function(e){return e.byteLength>=24&&2303741511===e.getUint32(0,o)},getSize:function(e){return{width:e.getUint32(16,o),height:e.getUint32(20,o)}}}],["image/jpeg",{test:function(e){return e.byteLength>=2&&65496===e.getUint16(0,o)},getSize:function(e){if(e.byteLength<2||65496!==e.getUint16(0,o))return null;const{tableMarkers:t,sofMarkers:n}=function(){const e=new Set([65499,65476,65484,65501,65534]);for(let t=65504;t<65520;++t)e.add(t);const t=new Set([65472,65473,65474,65475,65477,65478,65479,65481,65482,65483,65485,65486,65487,65502]);return{tableMarkers:e,sofMarkers:t}}();let r=2;for(;r<e.byteLength;){const i=e.getUint16(r,o);if(n.has(i))return{height:e.getUint16(r+5,o),width:e.getUint16(r+7,o)};if(!t.has(i))return null;r+=2,r+=e.getUint16(r,o)}return null}}],["image/gif",{test:function(e){return e.byteLength>=10&&1195984440===e.getUint32(0,o)},getSize:function(e){return{width:e.getUint16(6,i),height:e.getUint16(8,i)}}}],["image/bmp",{test:function(e){return e.byteLength>=2&&16973===e.getUint16(0,o)},getSize:function(e){return{width:e.getUint32(18,i),height:e.getUint32(22,i)}}}]]);const u=`Invalid MIME type. Supported MIME types are: ${Array.from(a.keys()).join(", ")}`;function c(e,t){if(t){const{test:n}=d(t);return n(g(e))}return Boolean(f(e))}function f(e){const t=g(e);for(const[e,{test:n}]of a.entries())if(n(t))return e;return null}function s(e,t=null){t=t||f(e);const{getSize:n}=d(t),r=n(g(e));if(!r)throw new Error(`invalid image data for type: ${t}`);return r}function l(e,t=null){const n=s(e,t=t||f(e));return n.mimeType=t,n}function d(e){const t=a.get(e);if(!t)throw new Error(u);return t}function g(e){if(e instanceof ArrayBuffer||ArrayBuffer.isView(e))return new DataView(e.buffer||e);throw new Error("toDataView")}const m=r.a._parseImageNode||"undefined"!=typeof ImageBitmap;function p(e,t){if(r.a._parseImageNode){const{mimeType:n}=l(e);return r.a._parseImageNode(e,n,t)}return h(e)}async function w(e,t){if("undefined"==typeof Image){const n=await fetch(e,t);return p(await n.arrayBuffer())}return await y(e,t)}function h(e){if("undefined"==typeof createImageBitmap)throw new Error("parseImage");const t=new Blob([new Uint8Array(e)]);return createImageBitmap(t)}async function y(e,t){let n;if(/\.svg((\?|#).*)?$/.test(e)){const r=await fetch(e,t),o=await r.text();n=`data:image/svg+xml;base64,${btoa(o)}`}else n=await e;return await new Promise((r,o)=>{try{const i=new Image;i.onload=(()=>r(i)),i.onerror=(t=>o(new Error(`Could not load image ${e}: ${t}`))),i.crossOrigin=t&&t.crossOrigin||"anonymous",i.src=n}catch(e){o(e)}})}var b={name:"Images",extension:[],parse:m&&p,loadAndParse:!m&&w};const v={parse:h},I={loadAndParse:y};var T={name:"Images",extensions:["jpeg"],encode:function(e,t){if(r.a._encodeImageNode)return r.a._encodeImageNode(e,t);if(e instanceof HTMLCanvasElement)return e.toDataURL(t);!function(e,t){if(!e)throw new Error(t||"@loaders.gl/images assertion failed.")}(e instanceof Image,"getImageData accepts image or canvas");const n=document.createElement("canvas");n.width=e.width,n.height=e.height,n.getContext("2d").drawImage(e,0,0);const o=n.toDataURL(t||"png").replace(/^data:image\/(png|jpg);base64,/,"");return Promise.resolve(o)},DEFAULT_OPTIONS:{type:"png"}};function L(e,{mimeType:t="image/jpeg"}){const n=new Blob([e],{type:t}),r=(window.URL||window.webkitURL).createObjectURL(n);return new Promise((e,t)=>{const n=new Image;return n.onload=(()=>e(n)),n.onerror=t,n.src=r,n})}n.d(t,"ImageLoader",function(){return b}),n.d(t,"HTMLImageLoader",function(){return I}),n.d(t,"ImageBitmapLoader",function(){return v}),n.d(t,"ImageWriter",function(){return T}),n.d(t,"loadImage",function(){return w}),n.d(t,"isImage",function(){return c}),n.d(t,"getImageMetadata",function(){return l}),n.d(t,"getImageMIMEType",function(){return f}),n.d(t,"getImageSize",function(){return s}),n.d(t,"decodeImage",function(){return L})}])}); |
@@ -52,2 +52,8 @@ "use strict"; | ||
}); | ||
Object.defineProperty(exports, "getImageMIMEType", { | ||
enumerable: true, | ||
get: function get() { | ||
return _getImageMetadata.getImageMIMEType; | ||
} | ||
}); | ||
Object.defineProperty(exports, "getImageSize", { | ||
@@ -54,0 +60,0 @@ enumerable: true, |
"use strict"; | ||
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); | ||
Object.defineProperty(exports, "__esModule", { | ||
@@ -8,9 +10,9 @@ value: true | ||
var _core = require("@loaders.gl/core"); | ||
var _assert = _interopRequireDefault(require("../utils/assert")); | ||
var _encodeImageNode = require("../node/encode-image-node"); | ||
var _globals = require("../utils/globals"); | ||
function encodeImage(image, type) { | ||
if (_encodeImageNode.encodeImageNode) { | ||
return (0, _encodeImageNode.encodeImageNode)(image, type); | ||
if (_globals.global._encodeImageNode) { | ||
return _globals.global._encodeImageNode(image, type); | ||
} | ||
@@ -23,3 +25,3 @@ | ||
(0, _core.assert)(image instanceof Image, 'getImageData accepts image or canvas'); | ||
(0, _assert["default"])(image instanceof Image, 'getImageData accepts image or canvas'); | ||
var canvas = document.createElement('canvas'); | ||
@@ -26,0 +28,0 @@ canvas.width = image.width; |
@@ -9,48 +9,26 @@ "use strict"; | ||
exports.isImage = isImage; | ||
exports.getImageMIMEType = getImageMIMEType; | ||
exports.getImageSize = getImageSize; | ||
exports.getImageMetadata = getImageMetadata; | ||
exports.getImageSize = getImageSize; | ||
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray")); | ||
var _bufferToArrayBuffer = require("../node/buffer-to-array-buffer"); | ||
var _imageParsers = require("./image-parsers"); | ||
var BIG_ENDIAN = false; | ||
var LITTLE_ENDIAN = true; | ||
var mimeTypeMap = new Map([['image/png', getPngSize], ['image/jpeg', getJpegSize], ['image/gif', getGifSize], ['image/bmp', getBmpSize]]); | ||
var ERR_INVALID_TYPE = "Invalid MIME type. Supported MIME types are: ".concat(Array.from(mimeTypeMap.keys()).join(', ')); | ||
var ERR_INVALID_MIME_TYPE = "Invalid MIME type. Supported MIME types are: ".concat(Array.from(_imageParsers.mimeTypeMap.keys()).join(', ')); | ||
function isImage(arrayBuffer) { | ||
var result = guessImageMetadata(arrayBuffer); | ||
return result ? result.mimeType : false; | ||
} | ||
function getImageMetadata(arrayBuffer) { | ||
var mimeType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
function isImage(arrayBuffer, mimeType) { | ||
if (mimeType) { | ||
var handler = mimeTypeMap.get(mimeType); | ||
var _getImageTypeHandlers = getImageTypeHandlers(mimeType), | ||
test = _getImageTypeHandlers.test; | ||
if (!handler) { | ||
throw new Error(ERR_INVALID_TYPE); | ||
} | ||
var _result = handler(arrayBuffer); | ||
if (!_result) { | ||
throw new Error("invalid image data for type: ".concat(mimeType)); | ||
} | ||
return _result; | ||
var dataView = toDataView(arrayBuffer); | ||
return test(dataView); | ||
} | ||
var result = guessImageMetadata(arrayBuffer, mimeType); | ||
if (!result) { | ||
throw new Error(ERR_INVALID_TYPE); | ||
} | ||
return result; | ||
return Boolean(getImageMIMEType(arrayBuffer)); | ||
} | ||
function guessImageMetadata(arrayBuffer, mimeType) { | ||
function getImageMIMEType(arrayBuffer) { | ||
var dataView = toDataView(arrayBuffer); | ||
var _iteratorNormalCompletion = true; | ||
@@ -61,12 +39,9 @@ var _didIteratorError = false; | ||
try { | ||
for (var _iterator = mimeTypeMap.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
for (var _iterator = _imageParsers.mimeTypeMap.entries()[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
var _step$value = (0, _slicedToArray2["default"])(_step.value, 2), | ||
supportedMimeType = _step$value[0], | ||
handler = _step$value[1]; | ||
mimeType = _step$value[0], | ||
test = _step$value[1].test; | ||
var result = handler(arrayBuffer); | ||
if (result) { | ||
result.mimeType = supportedMimeType; | ||
return result; | ||
if (test(dataView)) { | ||
return mimeType; | ||
} | ||
@@ -92,94 +67,38 @@ } | ||
function getPngSize(arrayBuffer) { | ||
var dataView = toDataView(arrayBuffer); | ||
function getImageSize(arrayBuffer) { | ||
var mimeType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
mimeType = mimeType || getImageMIMEType(arrayBuffer); | ||
if (dataView.byteLength < 24 || dataView.getUint32(0, BIG_ENDIAN) !== 0x89504e47) { | ||
return null; | ||
} | ||
var _getImageTypeHandlers2 = getImageTypeHandlers(mimeType), | ||
getSize = _getImageTypeHandlers2.getSize; | ||
return { | ||
width: dataView.getUint32(16, BIG_ENDIAN), | ||
height: dataView.getUint32(20, BIG_ENDIAN) | ||
}; | ||
} | ||
function getGifSize(arrayBuffer) { | ||
var dataView = toDataView(arrayBuffer); | ||
var size = getSize(dataView); | ||
if (dataView.byteLength < 10 || dataView.getUint32(0, BIG_ENDIAN) !== 0x47494638) { | ||
return null; | ||
if (!size) { | ||
throw new Error("invalid image data for type: ".concat(mimeType)); | ||
} | ||
return { | ||
width: dataView.getUint16(6, LITTLE_ENDIAN), | ||
height: dataView.getUint16(8, LITTLE_ENDIAN) | ||
}; | ||
return size; | ||
} | ||
function getBmpSize(arrayBuffer) { | ||
var dataView = toDataView(arrayBuffer); | ||
if (dataView.getUint16(0, BIG_ENDIAN) !== 0x424d) { | ||
return null; | ||
} | ||
return { | ||
width: dataView.getUint32(18, LITTLE_ENDIAN), | ||
height: dataView.getUint32(22, LITTLE_ENDIAN) | ||
}; | ||
function getImageMetadata(arrayBuffer) { | ||
var mimeType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
mimeType = mimeType || getImageMIMEType(arrayBuffer); | ||
var metadata = getImageSize(arrayBuffer, mimeType); | ||
metadata.mimeType = mimeType; | ||
return metadata; | ||
} | ||
function getJpegSize(arrayBuffer) { | ||
var dataView = toDataView(arrayBuffer); | ||
function getImageTypeHandlers(mimeType) { | ||
var handlers = _imageParsers.mimeTypeMap.get(mimeType); | ||
if (dataView.byteLength < 2 || dataView.getUint16(0, BIG_ENDIAN) !== 0xffd8) { | ||
return null; | ||
if (!handlers) { | ||
throw new Error(ERR_INVALID_MIME_TYPE); | ||
} | ||
var _getJpegMarkers = getJpegMarkers(), | ||
tableMarkers = _getJpegMarkers.tableMarkers, | ||
sofMarkers = _getJpegMarkers.sofMarkers; | ||
var i = 2; | ||
while (i < dataView.byteLength) { | ||
var marker = dataView.getUint16(i, BIG_ENDIAN); | ||
if (sofMarkers.has(marker)) { | ||
return { | ||
height: dataView.getUint16(i + 5, BIG_ENDIAN), | ||
width: dataView.getUint16(i + 7, BIG_ENDIAN) | ||
}; | ||
} | ||
if (!tableMarkers.has(marker)) { | ||
return null; | ||
} | ||
i += 2; | ||
i += dataView.getUint16(i, BIG_ENDIAN); | ||
} | ||
return null; | ||
return handlers; | ||
} | ||
function getJpegMarkers() { | ||
var tableMarkers = new Set([0xffdb, 0xffc4, 0xffcc, 0xffdd, 0xfffe]); | ||
for (var i = 0xffe0; i < 0xfff0; ++i) { | ||
tableMarkers.add(i); | ||
} | ||
var sofMarkers = new Set([0xffc0, 0xffc1, 0xffc2, 0xffc3, 0xffc5, 0xffc6, 0xffc7, 0xffc9, 0xffca, 0xffcb, 0xffcd, 0xffce, 0xffcf, 0xffde]); | ||
return { | ||
tableMarkers: tableMarkers, | ||
sofMarkers: sofMarkers | ||
}; | ||
} | ||
function toDataView(data) { | ||
if (_bufferToArrayBuffer.bufferToArrayBuffer) { | ||
data = (0, _bufferToArrayBuffer.bufferToArrayBuffer)(data); | ||
} | ||
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) { | ||
@@ -191,7 +110,2 @@ return new DataView(data.buffer || data); | ||
} | ||
function getImageSize(arrayBuffer) { | ||
var mimeType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
return getImageMetadata(arrayBuffer); | ||
} | ||
//# sourceMappingURL=get-image-metadata.js.map |
@@ -18,10 +18,15 @@ "use strict"; | ||
var _parseImageNode = require("../node/parse-image-node"); | ||
var _globals = require("../utils/globals"); | ||
var canParseImage = _parseImageNode.parseImageNode || typeof ImageBitmap !== 'undefined'; | ||
var _getImageMetadata2 = require("./get-image-metadata"); | ||
var canParseImage = _globals.global._parseImageNode || typeof ImageBitmap !== 'undefined'; | ||
exports.canParseImage = canParseImage; | ||
function parseImage(arrayBuffer, options) { | ||
if (_parseImageNode.parseImageNode) { | ||
return (0, _parseImageNode.parseImageNode)(arrayBuffer, options); | ||
if (_globals.global._parseImageNode) { | ||
var _getImageMetadata = (0, _getImageMetadata2.getImageMetadata)(arrayBuffer), | ||
mimeType = _getImageMetadata.mimeType; | ||
return _globals.global._parseImageNode(arrayBuffer, mimeType, options); | ||
} | ||
@@ -28,0 +33,0 @@ |
export { default as ImageLoader, HTMLImageLoader, ImageBitmapLoader } from './image-loader'; | ||
export { default as ImageWriter } from './image-writer'; | ||
export { loadImage } from './lib/parse-image'; | ||
export { isImage, getImageMetadata } from './lib/get-image-metadata'; | ||
export { isImage, getImageMetadata, getImageMIMEType, getImageSize } from './lib/get-image-metadata'; | ||
export { decodeImage } from './lib/image-utils-browser'; | ||
export { getImageSize } from './lib/get-image-metadata'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,6 +0,6 @@ | ||
import { assert } from '@loaders.gl/core'; | ||
import { encodeImageNode } from '../node/encode-image-node'; | ||
import assert from '../utils/assert'; | ||
import { global } from '../utils/globals'; | ||
export function encodeImage(image, type) { | ||
if (encodeImageNode) { | ||
return encodeImageNode(image, type); | ||
if (global._encodeImageNode) { | ||
return global._encodeImageNode(image, type); | ||
} | ||
@@ -7,0 +7,0 @@ |
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; | ||
import { bufferToArrayBuffer } from '../node/buffer-to-array-buffer'; | ||
const BIG_ENDIAN = false; | ||
const LITTLE_ENDIAN = true; | ||
const mimeTypeMap = new Map([['image/png', getPngSize], ['image/jpeg', getJpegSize], ['image/gif', getGifSize], ['image/bmp', getBmpSize]]); | ||
const ERR_INVALID_TYPE = "Invalid MIME type. Supported MIME types are: ".concat(Array.from(mimeTypeMap.keys()).join(', ')); | ||
export function isImage(arrayBuffer) { | ||
const result = guessImageMetadata(arrayBuffer); | ||
return result ? result.mimeType : false; | ||
} | ||
export function getImageMetadata(arrayBuffer) { | ||
let mimeType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
import { mimeTypeMap } from './image-parsers'; | ||
const ERR_INVALID_MIME_TYPE = "Invalid MIME type. Supported MIME types are: ".concat(Array.from(mimeTypeMap.keys()).join(', ')); | ||
export function isImage(arrayBuffer, mimeType) { | ||
if (mimeType) { | ||
const handler = mimeTypeMap.get(mimeType); | ||
const _getImageTypeHandlers = getImageTypeHandlers(mimeType), | ||
test = _getImageTypeHandlers.test; | ||
if (!handler) { | ||
throw new Error(ERR_INVALID_TYPE); | ||
} | ||
const result = handler(arrayBuffer); | ||
if (!result) { | ||
throw new Error("invalid image data for type: ".concat(mimeType)); | ||
} | ||
return result; | ||
const dataView = toDataView(arrayBuffer); | ||
return test(dataView); | ||
} | ||
const result = guessImageMetadata(arrayBuffer, mimeType); | ||
if (!result) { | ||
throw new Error(ERR_INVALID_TYPE); | ||
} | ||
return result; | ||
return Boolean(getImageMIMEType(arrayBuffer)); | ||
} | ||
export function getImageMIMEType(arrayBuffer) { | ||
const dataView = toDataView(arrayBuffer); | ||
function guessImageMetadata(arrayBuffer, mimeType) { | ||
for (const _ref of mimeTypeMap.entries()) { | ||
var _ref2 = _slicedToArray(_ref, 2); | ||
const supportedMimeType = _ref2[0]; | ||
const handler = _ref2[1]; | ||
const result = handler(arrayBuffer); | ||
const mimeType = _ref2[0]; | ||
const test = _ref2[1].test; | ||
if (result) { | ||
result.mimeType = supportedMimeType; | ||
return result; | ||
if (test(dataView)) { | ||
return mimeType; | ||
} | ||
@@ -55,95 +31,37 @@ } | ||
} | ||
export function getImageSize(arrayBuffer) { | ||
let mimeType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
mimeType = mimeType || getImageMIMEType(arrayBuffer); | ||
function getPngSize(arrayBuffer) { | ||
const dataView = toDataView(arrayBuffer); | ||
const _getImageTypeHandlers2 = getImageTypeHandlers(mimeType), | ||
getSize = _getImageTypeHandlers2.getSize; | ||
if (dataView.byteLength < 24 || dataView.getUint32(0, BIG_ENDIAN) !== 0x89504e47) { | ||
return null; | ||
} | ||
return { | ||
width: dataView.getUint32(16, BIG_ENDIAN), | ||
height: dataView.getUint32(20, BIG_ENDIAN) | ||
}; | ||
} | ||
function getGifSize(arrayBuffer) { | ||
const dataView = toDataView(arrayBuffer); | ||
const size = getSize(dataView); | ||
if (dataView.byteLength < 10 || dataView.getUint32(0, BIG_ENDIAN) !== 0x47494638) { | ||
return null; | ||
if (!size) { | ||
throw new Error("invalid image data for type: ".concat(mimeType)); | ||
} | ||
return { | ||
width: dataView.getUint16(6, LITTLE_ENDIAN), | ||
height: dataView.getUint16(8, LITTLE_ENDIAN) | ||
}; | ||
return size; | ||
} | ||
function getBmpSize(arrayBuffer) { | ||
const dataView = toDataView(arrayBuffer); | ||
if (dataView.getUint16(0, BIG_ENDIAN) !== 0x424d) { | ||
return null; | ||
} | ||
return { | ||
width: dataView.getUint32(18, LITTLE_ENDIAN), | ||
height: dataView.getUint32(22, LITTLE_ENDIAN) | ||
}; | ||
export function getImageMetadata(arrayBuffer) { | ||
let mimeType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
mimeType = mimeType || getImageMIMEType(arrayBuffer); | ||
const metadata = getImageSize(arrayBuffer, mimeType); | ||
metadata.mimeType = mimeType; | ||
return metadata; | ||
} | ||
function getJpegSize(arrayBuffer) { | ||
const dataView = toDataView(arrayBuffer); | ||
function getImageTypeHandlers(mimeType) { | ||
const handlers = mimeTypeMap.get(mimeType); | ||
if (dataView.byteLength < 2 || dataView.getUint16(0, BIG_ENDIAN) !== 0xffd8) { | ||
return null; | ||
if (!handlers) { | ||
throw new Error(ERR_INVALID_MIME_TYPE); | ||
} | ||
const _getJpegMarkers = getJpegMarkers(), | ||
tableMarkers = _getJpegMarkers.tableMarkers, | ||
sofMarkers = _getJpegMarkers.sofMarkers; | ||
let i = 2; | ||
while (i < dataView.byteLength) { | ||
const marker = dataView.getUint16(i, BIG_ENDIAN); | ||
if (sofMarkers.has(marker)) { | ||
return { | ||
height: dataView.getUint16(i + 5, BIG_ENDIAN), | ||
width: dataView.getUint16(i + 7, BIG_ENDIAN) | ||
}; | ||
} | ||
if (!tableMarkers.has(marker)) { | ||
return null; | ||
} | ||
i += 2; | ||
i += dataView.getUint16(i, BIG_ENDIAN); | ||
} | ||
return null; | ||
return handlers; | ||
} | ||
function getJpegMarkers() { | ||
const tableMarkers = new Set([0xffdb, 0xffc4, 0xffcc, 0xffdd, 0xfffe]); | ||
for (let i = 0xffe0; i < 0xfff0; ++i) { | ||
tableMarkers.add(i); | ||
} | ||
const sofMarkers = new Set([0xffc0, 0xffc1, 0xffc2, 0xffc3, 0xffc5, 0xffc6, 0xffc7, 0xffc9, 0xffca, 0xffcb, 0xffcd, 0xffce, 0xffcf, 0xffde]); | ||
return { | ||
tableMarkers, | ||
sofMarkers | ||
}; | ||
} | ||
function toDataView(data) { | ||
if (bufferToArrayBuffer) { | ||
data = bufferToArrayBuffer(data); | ||
} | ||
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) { | ||
@@ -155,7 +73,2 @@ return new DataView(data.buffer || data); | ||
} | ||
export function getImageSize(arrayBuffer) { | ||
let mimeType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
return getImageMetadata(arrayBuffer); | ||
} | ||
//# sourceMappingURL=get-image-metadata.js.map |
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; | ||
import { parseImageNode } from '../node/parse-image-node'; | ||
export const canParseImage = parseImageNode || typeof ImageBitmap !== 'undefined'; | ||
import { global } from '../utils/globals'; | ||
import { getImageMetadata } from './get-image-metadata'; | ||
export const canParseImage = global._parseImageNode || typeof ImageBitmap !== 'undefined'; | ||
export function parseImage(arrayBuffer, options) { | ||
if (parseImageNode) { | ||
return parseImageNode(arrayBuffer, options); | ||
if (global._parseImageNode) { | ||
const _getImageMetadata = getImageMetadata(arrayBuffer), | ||
mimeType = _getImageMetadata.mimeType; | ||
return global._parseImageNode(arrayBuffer, mimeType, options); | ||
} | ||
@@ -8,0 +12,0 @@ |
export { default as ImageLoader, HTMLImageLoader, ImageBitmapLoader } from './image-loader'; | ||
export { default as ImageWriter } from './image-writer'; | ||
export { loadImage } from './lib/parse-image'; | ||
export { isImage, getImageMetadata } from './lib/get-image-metadata'; | ||
export { isImage, getImageMetadata, getImageMIMEType, getImageSize } from './lib/get-image-metadata'; | ||
export { decodeImage } from './lib/image-utils-browser'; | ||
export { getImageSize } from './lib/get-image-metadata'; | ||
//# sourceMappingURL=index.js.map |
@@ -1,6 +0,6 @@ | ||
import { assert } from '@loaders.gl/core'; | ||
import { encodeImageNode } from '../node/encode-image-node'; | ||
import assert from '../utils/assert'; | ||
import { global } from '../utils/globals'; | ||
export function encodeImage(image, type) { | ||
if (encodeImageNode) { | ||
return encodeImageNode(image, type); | ||
if (global._encodeImageNode) { | ||
return global._encodeImageNode(image, type); | ||
} | ||
@@ -7,0 +7,0 @@ |
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray"; | ||
import { bufferToArrayBuffer } from '../node/buffer-to-array-buffer'; | ||
var BIG_ENDIAN = false; | ||
var LITTLE_ENDIAN = true; | ||
var mimeTypeMap = new Map([['image/png', getPngSize], ['image/jpeg', getJpegSize], ['image/gif', getGifSize], ['image/bmp', getBmpSize]]); | ||
var ERR_INVALID_TYPE = "Invalid MIME type. Supported MIME types are: ".concat(Array.from(mimeTypeMap.keys()).join(', ')); | ||
export function isImage(arrayBuffer) { | ||
var result = guessImageMetadata(arrayBuffer); | ||
return result ? result.mimeType : false; | ||
} | ||
export function getImageMetadata(arrayBuffer) { | ||
var mimeType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
import { mimeTypeMap } from './image-parsers'; | ||
var ERR_INVALID_MIME_TYPE = "Invalid MIME type. Supported MIME types are: ".concat(Array.from(mimeTypeMap.keys()).join(', ')); | ||
export function isImage(arrayBuffer, mimeType) { | ||
if (mimeType) { | ||
var handler = mimeTypeMap.get(mimeType); | ||
var _getImageTypeHandlers = getImageTypeHandlers(mimeType), | ||
test = _getImageTypeHandlers.test; | ||
if (!handler) { | ||
throw new Error(ERR_INVALID_TYPE); | ||
} | ||
var _result = handler(arrayBuffer); | ||
if (!_result) { | ||
throw new Error("invalid image data for type: ".concat(mimeType)); | ||
} | ||
return _result; | ||
var dataView = toDataView(arrayBuffer); | ||
return test(dataView); | ||
} | ||
var result = guessImageMetadata(arrayBuffer, mimeType); | ||
if (!result) { | ||
throw new Error(ERR_INVALID_TYPE); | ||
} | ||
return result; | ||
return Boolean(getImageMIMEType(arrayBuffer)); | ||
} | ||
function guessImageMetadata(arrayBuffer, mimeType) { | ||
export function getImageMIMEType(arrayBuffer) { | ||
var dataView = toDataView(arrayBuffer); | ||
var _iteratorNormalCompletion = true; | ||
@@ -47,10 +24,7 @@ var _didIteratorError = false; | ||
var _step$value = _slicedToArray(_step.value, 2), | ||
supportedMimeType = _step$value[0], | ||
handler = _step$value[1]; | ||
mimeType = _step$value[0], | ||
test = _step$value[1].test; | ||
var result = handler(arrayBuffer); | ||
if (result) { | ||
result.mimeType = supportedMimeType; | ||
return result; | ||
if (test(dataView)) { | ||
return mimeType; | ||
} | ||
@@ -75,95 +49,37 @@ } | ||
} | ||
export function getImageSize(arrayBuffer) { | ||
var mimeType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
mimeType = mimeType || getImageMIMEType(arrayBuffer); | ||
function getPngSize(arrayBuffer) { | ||
var dataView = toDataView(arrayBuffer); | ||
var _getImageTypeHandlers2 = getImageTypeHandlers(mimeType), | ||
getSize = _getImageTypeHandlers2.getSize; | ||
if (dataView.byteLength < 24 || dataView.getUint32(0, BIG_ENDIAN) !== 0x89504e47) { | ||
return null; | ||
} | ||
return { | ||
width: dataView.getUint32(16, BIG_ENDIAN), | ||
height: dataView.getUint32(20, BIG_ENDIAN) | ||
}; | ||
} | ||
function getGifSize(arrayBuffer) { | ||
var dataView = toDataView(arrayBuffer); | ||
var size = getSize(dataView); | ||
if (dataView.byteLength < 10 || dataView.getUint32(0, BIG_ENDIAN) !== 0x47494638) { | ||
return null; | ||
if (!size) { | ||
throw new Error("invalid image data for type: ".concat(mimeType)); | ||
} | ||
return { | ||
width: dataView.getUint16(6, LITTLE_ENDIAN), | ||
height: dataView.getUint16(8, LITTLE_ENDIAN) | ||
}; | ||
return size; | ||
} | ||
function getBmpSize(arrayBuffer) { | ||
var dataView = toDataView(arrayBuffer); | ||
if (dataView.getUint16(0, BIG_ENDIAN) !== 0x424d) { | ||
return null; | ||
} | ||
return { | ||
width: dataView.getUint32(18, LITTLE_ENDIAN), | ||
height: dataView.getUint32(22, LITTLE_ENDIAN) | ||
}; | ||
export function getImageMetadata(arrayBuffer) { | ||
var mimeType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
mimeType = mimeType || getImageMIMEType(arrayBuffer); | ||
var metadata = getImageSize(arrayBuffer, mimeType); | ||
metadata.mimeType = mimeType; | ||
return metadata; | ||
} | ||
function getJpegSize(arrayBuffer) { | ||
var dataView = toDataView(arrayBuffer); | ||
function getImageTypeHandlers(mimeType) { | ||
var handlers = mimeTypeMap.get(mimeType); | ||
if (dataView.byteLength < 2 || dataView.getUint16(0, BIG_ENDIAN) !== 0xffd8) { | ||
return null; | ||
if (!handlers) { | ||
throw new Error(ERR_INVALID_MIME_TYPE); | ||
} | ||
var _getJpegMarkers = getJpegMarkers(), | ||
tableMarkers = _getJpegMarkers.tableMarkers, | ||
sofMarkers = _getJpegMarkers.sofMarkers; | ||
var i = 2; | ||
while (i < dataView.byteLength) { | ||
var marker = dataView.getUint16(i, BIG_ENDIAN); | ||
if (sofMarkers.has(marker)) { | ||
return { | ||
height: dataView.getUint16(i + 5, BIG_ENDIAN), | ||
width: dataView.getUint16(i + 7, BIG_ENDIAN) | ||
}; | ||
} | ||
if (!tableMarkers.has(marker)) { | ||
return null; | ||
} | ||
i += 2; | ||
i += dataView.getUint16(i, BIG_ENDIAN); | ||
} | ||
return null; | ||
return handlers; | ||
} | ||
function getJpegMarkers() { | ||
var tableMarkers = new Set([0xffdb, 0xffc4, 0xffcc, 0xffdd, 0xfffe]); | ||
for (var i = 0xffe0; i < 0xfff0; ++i) { | ||
tableMarkers.add(i); | ||
} | ||
var sofMarkers = new Set([0xffc0, 0xffc1, 0xffc2, 0xffc3, 0xffc5, 0xffc6, 0xffc7, 0xffc9, 0xffca, 0xffcb, 0xffcd, 0xffce, 0xffcf, 0xffde]); | ||
return { | ||
tableMarkers: tableMarkers, | ||
sofMarkers: sofMarkers | ||
}; | ||
} | ||
function toDataView(data) { | ||
if (bufferToArrayBuffer) { | ||
data = bufferToArrayBuffer(data); | ||
} | ||
if (data instanceof ArrayBuffer || ArrayBuffer.isView(data)) { | ||
@@ -175,7 +91,2 @@ return new DataView(data.buffer || data); | ||
} | ||
export function getImageSize(arrayBuffer) { | ||
var mimeType = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null; | ||
return getImageMetadata(arrayBuffer); | ||
} | ||
//# sourceMappingURL=get-image-metadata.js.map |
import _regeneratorRuntime from "@babel/runtime/regenerator"; | ||
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator"; | ||
import { parseImageNode } from '../node/parse-image-node'; | ||
export var canParseImage = parseImageNode || typeof ImageBitmap !== 'undefined'; | ||
import { global } from '../utils/globals'; | ||
import { getImageMetadata } from './get-image-metadata'; | ||
export var canParseImage = global._parseImageNode || typeof ImageBitmap !== 'undefined'; | ||
export function parseImage(arrayBuffer, options) { | ||
if (parseImageNode) { | ||
return parseImageNode(arrayBuffer, options); | ||
if (global._parseImageNode) { | ||
var _getImageMetadata = getImageMetadata(arrayBuffer), | ||
mimeType = _getImageMetadata.mimeType; | ||
return global._parseImageNode(arrayBuffer, mimeType, options); | ||
} | ||
@@ -9,0 +13,0 @@ |
{ | ||
"name": "@loaders.gl/images", | ||
"version": "1.0.3", | ||
"version": "1.1.0-alpha.1", | ||
"description": "Framework-independent loaders and writers for images (PNG, JPG, ...)", | ||
@@ -33,20 +33,3 @@ "license": "MIT", | ||
"build-bundle": "webpack --display=minimal --config ../../scripts/bundle.config.js" | ||
}, | ||
"browser": { | ||
"./src/node/buffer-to-array-buffer": false, | ||
"./src/node/encode-image-node": false, | ||
"./src/node/parse-image-node": false, | ||
"get-pixels": false, | ||
"ndarray": false, | ||
"save-pixels": false, | ||
"fs": false, | ||
"util": false | ||
}, | ||
"dependencies": { | ||
"canvas-to-blob": "0.0.0", | ||
"get-pixels": "^3.3.2", | ||
"ndarray": "^1.0.18", | ||
"save-pixels": "^2.3.2", | ||
"through": "^2.3.8" | ||
} | ||
} |
@@ -7,4 +7,2 @@ # @loaders.gl/images | ||
More advanced IO support that handles e.g. stream support in browsers is being developed in this separate module. | ||
> These IO functions require dependencies that can signficantly increase the size of an application bundle. | ||
For documentation please visit the [website](https://loaders.gl). |
@@ -7,8 +7,5 @@ export {default as ImageLoader, HTMLImageLoader, ImageBitmapLoader} from './image-loader'; | ||
// UTILS | ||
export {isImage, getImageMetadata} from './lib/get-image-metadata'; | ||
export {isImage, getImageMetadata, getImageMIMEType, getImageSize} from './lib/get-image-metadata'; | ||
// Experimental | ||
export {decodeImage} from './lib/image-utils-browser'; | ||
// Deprecated | ||
export {getImageSize} from './lib/get-image-metadata'; |
// Image loading/saving for browser | ||
/* global document, HTMLCanvasElement, Image */ | ||
import {assert} from '@loaders.gl/core'; | ||
import {encodeImageNode} from '../node/encode-image-node'; | ||
import assert from '../utils/assert'; | ||
import {global} from '../utils/globals'; | ||
/* | ||
* Returns data bytes representing a compressed image in PNG or JPG format, | ||
* This data can be saved using file system (f) methods or | ||
* used in a request. | ||
* @param {Image} image - Image or Canvas | ||
* @param {String} opt.type='png' - png, jpg or image/png, image/jpg are valid | ||
* @param {String} opt.dataURI= - Whether to include a data URI header | ||
*/ | ||
// Returns data bytes representing a compressed image in PNG or JPG format, | ||
// This data can be saved using file system (f) methods or | ||
// used in a request. | ||
// @param {Image} image - Image or Canvas | ||
// @param {String} opt.type='png' - png, jpg or image/png, image/jpg are valid | ||
// @param {String} opt.dataURI= - Whether to include a data URI header | ||
export function encodeImage(image, type) { | ||
if (encodeImageNode) { | ||
return encodeImageNode(image, type); | ||
if (global._encodeImageNode) { | ||
return global._encodeImageNode(image, type); | ||
} | ||
@@ -19,0 +18,0 @@ |
// Attributions | ||
// * Based on binary-gltf-utils under MIT license: Copyright (c) 2016-17 Karl Cheng | ||
// TODO: make these functions work for Node.js buffers? | ||
// Quarantine references to Buffer to prevent bundler from adding big polyfills | ||
import {bufferToArrayBuffer} from '../node/buffer-to-array-buffer'; | ||
// import {bufferToArrayBuffer} from '../node/buffer-to-array-buffer'; | ||
// TODO - this should be handled in @loaders.gl/polyfills | ||
const BIG_ENDIAN = false; | ||
const LITTLE_ENDIAN = true; | ||
import {mimeTypeMap} from './image-parsers'; | ||
const mimeTypeMap = new Map([ | ||
['image/png', getPngSize], | ||
['image/jpeg', getJpegSize], | ||
['image/gif', getGifSize], | ||
['image/bmp', getBmpSize] | ||
]); | ||
const ERR_INVALID_TYPE = `Invalid MIME type. Supported MIME types are: ${Array.from( | ||
const ERR_INVALID_MIME_TYPE = `Invalid MIME type. Supported MIME types are: ${Array.from( | ||
mimeTypeMap.keys() | ||
).join(', ')}`; | ||
/** | ||
* Sniffs the contents of a file to attempt to deduce the image type and extract image type. | ||
* Supported image types are PNG, JPEG, GIF and BMP. | ||
*/ | ||
export function isImage(arrayBuffer) { | ||
const result = guessImageMetadata(arrayBuffer); | ||
return result ? result.mimeType : false; | ||
} | ||
/** | ||
* Sniffs the contents of a file to attempt to deduce the image type and size. | ||
* Supported image types are PNG, JPEG, GIF and BMP. | ||
* @param {ArrayBuffer} arrayBuffer | ||
* @param {string} [mimeType] | ||
*/ | ||
export function getImageMetadata(arrayBuffer, mimeType = null) { | ||
// Looking for only a specific MIME type. | ||
// Supported image types are PNG, JPEG, GIF and BMP. | ||
export function isImage(arrayBuffer, mimeType) { | ||
if (mimeType) { | ||
const handler = mimeTypeMap.get(mimeType); | ||
if (!handler) { | ||
throw new Error(ERR_INVALID_TYPE); | ||
} | ||
const result = handler(arrayBuffer); | ||
if (!result) { | ||
throw new Error(`invalid image data for type: ${mimeType}`); | ||
} | ||
return result; | ||
const {test} = getImageTypeHandlers(mimeType); | ||
const dataView = toDataView(arrayBuffer); | ||
return test(dataView); | ||
} | ||
const result = guessImageMetadata(arrayBuffer, mimeType); | ||
if (!result) { | ||
// Seems not :( | ||
throw new Error(ERR_INVALID_TYPE); | ||
} | ||
return result; | ||
// check if known type | ||
return Boolean(getImageMIMEType(arrayBuffer)); | ||
} | ||
function guessImageMetadata(arrayBuffer, mimeType) { | ||
// Sniffs the contents of a file to attempt to deduce the image type | ||
export function getImageMIMEType(arrayBuffer) { | ||
const dataView = toDataView(arrayBuffer); | ||
// Loop through each file type and see if they work. | ||
for (const [supportedMimeType, handler] of mimeTypeMap.entries()) { | ||
const result = handler(arrayBuffer); | ||
if (result) { | ||
result.mimeType = supportedMimeType; | ||
return result; | ||
for (const [mimeType, {test}] of mimeTypeMap.entries()) { | ||
if (test(dataView)) { | ||
return mimeType; | ||
} | ||
@@ -72,133 +40,40 @@ } | ||
/** | ||
* Extract size from a binary PNG file | ||
* @param {Buffer} contents | ||
*/ | ||
function getPngSize(arrayBuffer) { | ||
const dataView = toDataView(arrayBuffer); | ||
export function getImageSize(arrayBuffer, mimeType = null) { | ||
mimeType = mimeType || getImageMIMEType(arrayBuffer); | ||
// Check file contains the first 4 bytes of the PNG signature. | ||
if (dataView.byteLength < 24 || dataView.getUint32(0, BIG_ENDIAN) !== 0x89504e47) { | ||
return null; | ||
} | ||
const {getSize} = getImageTypeHandlers(mimeType); | ||
return { | ||
width: dataView.getUint32(16, BIG_ENDIAN), | ||
height: dataView.getUint32(20, BIG_ENDIAN) | ||
}; | ||
} | ||
/** | ||
* Extract size from a binary GIF file | ||
* @param {Buffer} contents | ||
* TODO: GIF is not this simple | ||
*/ | ||
function getGifSize(arrayBuffer) { | ||
const dataView = toDataView(arrayBuffer); | ||
const size = getSize(dataView); | ||
// Check first 4 bytes of the GIF signature ("GIF8"). | ||
if (dataView.byteLength < 10 || dataView.getUint32(0, BIG_ENDIAN) !== 0x47494638) { | ||
return null; | ||
if (!size) { | ||
throw new Error(`invalid image data for type: ${mimeType}`); | ||
} | ||
// GIF is little endian. | ||
return { | ||
width: dataView.getUint16(6, LITTLE_ENDIAN), | ||
height: dataView.getUint16(8, LITTLE_ENDIAN) | ||
}; | ||
return size; | ||
} | ||
/** | ||
* @param {Buffer} contents | ||
* TODO: BMP is not this simple | ||
*/ | ||
function getBmpSize(arrayBuffer) { | ||
const dataView = toDataView(arrayBuffer); | ||
// Sniffs the contents of a file to attempt to deduce the image type and size. | ||
// Supported image types are PNG, JPEG, GIF and BMP. | ||
export function getImageMetadata(arrayBuffer, mimeType = null) { | ||
mimeType = mimeType || getImageMIMEType(arrayBuffer); | ||
// Check magic number is valid (first 2 characters should be "BM"). | ||
if (dataView.getUint16(0, BIG_ENDIAN) !== 0x424d) { | ||
return null; | ||
} | ||
// BMP is little endian. | ||
return { | ||
width: dataView.getUint32(18, LITTLE_ENDIAN), | ||
height: dataView.getUint32(22, LITTLE_ENDIAN) | ||
}; | ||
const metadata = getImageSize(arrayBuffer, mimeType); | ||
metadata.mimeType = mimeType; | ||
return metadata; | ||
} | ||
/** | ||
* Extract size from a binary JPEG file | ||
* @param {Buffer} contents | ||
*/ | ||
function getJpegSize(arrayBuffer) { | ||
const dataView = toDataView(arrayBuffer); | ||
// Check file contains the JPEG "start of image" (SOI) marker. | ||
if (dataView.byteLength < 2 || dataView.getUint16(0, BIG_ENDIAN) !== 0xffd8) { | ||
return null; | ||
function getImageTypeHandlers(mimeType) { | ||
const handlers = mimeTypeMap.get(mimeType); | ||
if (!handlers) { | ||
throw new Error(ERR_INVALID_MIME_TYPE); | ||
} | ||
const {tableMarkers, sofMarkers} = getJpegMarkers(); | ||
// Exclude the two byte SOI marker. | ||
let i = 2; | ||
while (i < dataView.byteLength) { | ||
const marker = dataView.getUint16(i, BIG_ENDIAN); | ||
// The frame that contains the width and height of the JPEG image. | ||
if (sofMarkers.has(marker)) { | ||
return { | ||
height: dataView.getUint16(i + 5, BIG_ENDIAN), // Number of lines | ||
width: dataView.getUint16(i + 7, BIG_ENDIAN) // Number of pixels per line | ||
}; | ||
} | ||
// Miscellaneous tables/data preceding the frame header. | ||
if (!tableMarkers.has(marker)) { | ||
return null; | ||
} | ||
// Length includes size of length parameter but not the two byte header. | ||
i += 2; | ||
i += dataView.getUint16(i, BIG_ENDIAN); | ||
} | ||
return null; | ||
return handlers; | ||
} | ||
function getJpegMarkers() { | ||
// Tables/misc header markers. | ||
// DQT, DHT, DAC, DRI, COM, APP_n | ||
const tableMarkers = new Set([0xffdb, 0xffc4, 0xffcc, 0xffdd, 0xfffe]); | ||
for (let i = 0xffe0; i < 0xfff0; ++i) { | ||
tableMarkers.add(i); | ||
} | ||
// SOF markers and DHP marker. | ||
// These markers are after tables/misc data. | ||
const sofMarkers = new Set([ | ||
0xffc0, | ||
0xffc1, | ||
0xffc2, | ||
0xffc3, | ||
0xffc5, | ||
0xffc6, | ||
0xffc7, | ||
0xffc9, | ||
0xffca, | ||
0xffcb, | ||
0xffcd, | ||
0xffce, | ||
0xffcf, | ||
0xffde | ||
]); | ||
return {tableMarkers, sofMarkers}; | ||
} | ||
function toDataView(data) { | ||
if (bufferToArrayBuffer) { | ||
data = bufferToArrayBuffer(data); | ||
} | ||
// TODO: make these functions work for Node.js buffers? | ||
// if (bufferToArrayBuffer) { | ||
// data = bufferToArrayBuffer(data); | ||
// } | ||
@@ -212,7 +87,1 @@ // Careful - Node Buffers will look like ArrayBuffers (keep after isBuffer) | ||
} | ||
// DEPRECEATED | ||
export function getImageSize(arrayBuffer, mimeType = null) { | ||
return getImageMetadata(arrayBuffer); | ||
} |
/* global Image, Blob, createImageBitmap, btoa, fetch */ | ||
import {parseImageNode} from '../node/parse-image-node'; | ||
import {global} from '../utils/globals'; | ||
import {getImageMetadata} from './get-image-metadata'; | ||
export const canParseImage = parseImageNode || typeof ImageBitmap !== 'undefined'; | ||
export const canParseImage = global._parseImageNode || typeof ImageBitmap !== 'undefined'; | ||
// Parse to platform defined type (ndarray on node, ImageBitmap on browser) | ||
export function parseImage(arrayBuffer, options) { | ||
if (parseImageNode) { | ||
return parseImageNode(arrayBuffer, options); | ||
if (global._parseImageNode) { | ||
const {mimeType} = getImageMetadata(arrayBuffer); | ||
return global._parseImageNode(arrayBuffer, mimeType, options); | ||
} | ||
@@ -11,0 +13,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
0
12
180334
2296
2
8
9
49
- Removedcanvas-to-blob@0.0.0
- Removedget-pixels@^3.3.2
- Removedndarray@^1.0.18
- Removedsave-pixels@^2.3.2
- Removedthrough@^2.3.8
- Removedajv@6.12.6(transitive)
- Removedasn1@0.2.6(transitive)
- Removedassert-plus@1.0.0(transitive)
- Removedasynckit@0.4.0(transitive)
- Removedaws-sign2@0.7.0(transitive)
- Removedaws4@1.13.2(transitive)
- Removedbcrypt-pbkdf@1.0.2(transitive)
- Removedcanvas-to-blob@0.0.0(transitive)
- Removedcaseless@0.12.0(transitive)
- Removedcombined-stream@1.0.8(transitive)
- Removedcontentstream@1.0.0(transitive)
- Removedcore-util-is@1.0.21.0.3(transitive)
- Removedcwise-compiler@1.1.3(transitive)
- Removeddashdash@1.14.1(transitive)
- Removeddata-uri-to-buffer@0.0.3(transitive)
- Removeddelayed-stream@1.0.0(transitive)
- Removedecc-jsbn@0.1.2(transitive)
- Removedextend@3.0.2(transitive)
- Removedextsprintf@1.3.0(transitive)
- Removedfast-deep-equal@3.1.3(transitive)
- Removedfast-json-stable-stringify@2.1.0(transitive)
- Removedforever-agent@0.6.1(transitive)
- Removedform-data@2.3.3(transitive)
- Removedget-pixels@3.3.3(transitive)
- Removedgetpass@0.1.7(transitive)
- Removedgif-encoder@0.4.3(transitive)
- Removedhar-schema@2.0.0(transitive)
- Removedhar-validator@5.1.5(transitive)
- Removedhttp-signature@1.2.0(transitive)
- Removedinherits@2.0.4(transitive)
- Removediota-array@1.0.0(transitive)
- Removedis-buffer@1.1.6(transitive)
- Removedis-typedarray@1.0.0(transitive)
- Removedisarray@0.0.1(transitive)
- Removedisstream@0.1.2(transitive)
- Removedjpeg-js@0.4.4(transitive)
- Removedjsbn@0.1.1(transitive)
- Removedjson-schema@0.4.0(transitive)
- Removedjson-schema-traverse@0.4.1(transitive)
- Removedjson-stringify-safe@5.0.1(transitive)
- Removedjsprim@1.4.2(transitive)
- Removedmime-db@1.52.0(transitive)
- Removedmime-types@2.1.35(transitive)
- Removedndarray@1.0.19(transitive)
- Removedndarray-ops@1.2.2(transitive)
- Removedndarray-pack@1.2.1(transitive)
- Removednode-bitmap@0.0.1(transitive)
- Removedoauth-sign@0.9.0(transitive)
- Removedomggif@1.0.10(transitive)
- Removedparse-data-uri@0.2.0(transitive)
- Removedperformance-now@2.1.0(transitive)
- Removedpngjs@3.4.0(transitive)
- Removedpngjs-nozlib@1.0.0(transitive)
- Removedpsl@1.10.0(transitive)
- Removedpunycode@2.3.1(transitive)
- Removedqs@6.5.3(transitive)
- Removedreadable-stream@1.0.341.1.14(transitive)
- Removedrequest@2.88.2(transitive)
- Removedsafe-buffer@5.2.1(transitive)
- Removedsafer-buffer@2.1.2(transitive)
- Removedsave-pixels@2.3.6(transitive)
- Removedsshpk@1.18.0(transitive)
- Removedstring_decoder@0.10.31(transitive)
- Removedthrough@2.3.8(transitive)
- Removedtough-cookie@2.5.0(transitive)
- Removedtunnel-agent@0.6.0(transitive)
- Removedtweetnacl@0.14.5(transitive)
- Removeduniq@1.0.1(transitive)
- Removeduri-js@4.4.1(transitive)
- Removeduuid@3.4.0(transitive)
- Removedverror@1.10.0(transitive)