@loaders.gl/images 4.0.0-alpha.5 → 4.0.0-alpha.7

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 (152) hide show
  1. package/dist/bundle.js +2 -2
  2. package/dist/dist.min.js +1 -1
  3. package/dist/dist.min.js.map +3 -3
  4. package/dist/es5/bundle.js +6 -0
  5. package/dist/es5/bundle.js.map +1 -0
  6. package/dist/es5/image-loader.js +33 -0
  7. package/dist/es5/image-loader.js.map +1 -0
  8. package/dist/es5/image-writer.js +24 -0
  9. package/dist/es5/image-writer.js.map +1 -0
  10. package/dist/es5/index.js +85 -0
  11. package/dist/es5/index.js.map +1 -0
  12. package/dist/es5/lib/category-api/binary-image-api.js +113 -0
  13. package/dist/es5/lib/category-api/binary-image-api.js.map +1 -0
  14. package/dist/es5/lib/category-api/image-format.js +181 -0
  15. package/dist/es5/lib/category-api/image-format.js.map +1 -0
  16. package/dist/es5/lib/category-api/image-type.js +40 -0
  17. package/dist/es5/lib/category-api/image-type.js.map +1 -0
  18. package/dist/es5/lib/category-api/parse-isobmff-binary.js +50 -0
  19. package/dist/es5/lib/category-api/parse-isobmff-binary.js.map +1 -0
  20. package/dist/es5/lib/category-api/parsed-image-api.js +65 -0
  21. package/dist/es5/lib/category-api/parsed-image-api.js.map +1 -0
  22. package/dist/es5/lib/encoders/encode-image.js +101 -0
  23. package/dist/es5/lib/encoders/encode-image.js.map +1 -0
  24. package/dist/es5/lib/parsers/parse-image.js +76 -0
  25. package/dist/es5/lib/parsers/parse-image.js.map +1 -0
  26. package/dist/es5/lib/parsers/parse-to-image-bitmap.js +97 -0
  27. package/dist/es5/lib/parsers/parse-to-image-bitmap.js.map +1 -0
  28. package/dist/es5/lib/parsers/parse-to-image.js +86 -0
  29. package/dist/es5/lib/parsers/parse-to-image.js.map +1 -0
  30. package/dist/es5/lib/parsers/parse-to-node-image.js +36 -0
  31. package/dist/es5/lib/parsers/parse-to-node-image.js.map +1 -0
  32. package/dist/es5/lib/parsers/svg-utils.js +36 -0
  33. package/dist/es5/lib/parsers/svg-utils.js.map +1 -0
  34. package/dist/es5/lib/texture-api/async-deep-map.js +156 -0
  35. package/dist/es5/lib/texture-api/async-deep-map.js.map +1 -0
  36. package/dist/es5/lib/texture-api/deep-load.js +63 -0
  37. package/dist/es5/lib/texture-api/deep-load.js.map +1 -0
  38. package/dist/es5/lib/texture-api/generate-url.js +24 -0
  39. package/dist/es5/lib/texture-api/generate-url.js.map +1 -0
  40. package/dist/es5/lib/texture-api/load-image.js +132 -0
  41. package/dist/es5/lib/texture-api/load-image.js.map +1 -0
  42. package/dist/es5/lib/utils/version.js +9 -0
  43. package/dist/es5/lib/utils/version.js.map +1 -0
  44. package/dist/es5/types.js +2 -0
  45. package/dist/es5/types.js.map +1 -0
  46. package/dist/esm/bundle.js +4 -0
  47. package/dist/esm/bundle.js.map +1 -0
  48. package/dist/esm/image-loader.js +23 -0
  49. package/dist/esm/image-loader.js.map +1 -0
  50. package/dist/esm/image-writer.js +17 -0
  51. package/dist/esm/image-writer.js.map +1 -0
  52. package/dist/esm/index.js +9 -0
  53. package/dist/esm/index.js.map +1 -0
  54. package/dist/esm/lib/category-api/binary-image-api.js +107 -0
  55. package/dist/esm/lib/category-api/binary-image-api.js.map +1 -0
  56. package/dist/esm/lib/category-api/image-format.js +67 -0
  57. package/dist/esm/lib/category-api/image-format.js.map +1 -0
  58. package/dist/esm/lib/category-api/image-type.js +35 -0
  59. package/dist/esm/lib/category-api/image-type.js.map +1 -0
  60. package/dist/esm/lib/category-api/parse-isobmff-binary.js +39 -0
  61. package/dist/esm/lib/category-api/parse-isobmff-binary.js.map +1 -0
  62. package/dist/esm/lib/category-api/parsed-image-api.js +53 -0
  63. package/dist/esm/lib/category-api/parsed-image-api.js.map +1 -0
  64. package/dist/esm/lib/encoders/encode-image.js +62 -0
  65. package/dist/esm/lib/encoders/encode-image.js.map +1 -0
  66. package/dist/esm/lib/parsers/parse-image.js +44 -0
  67. package/dist/esm/lib/parsers/parse-image.js.map +1 -0
  68. package/dist/esm/lib/parsers/parse-to-image-bitmap.js +37 -0
  69. package/dist/esm/lib/parsers/parse-to-image-bitmap.js.map +1 -0
  70. package/dist/esm/lib/parsers/parse-to-image.js +30 -0
  71. package/dist/esm/lib/parsers/parse-to-image.js.map +1 -0
  72. package/dist/esm/lib/parsers/parse-to-node-image.js +11 -0
  73. package/dist/esm/lib/parsers/parse-to-node-image.js.map +1 -0
  74. package/dist/esm/lib/parsers/svg-utils.js +28 -0
  75. package/dist/esm/lib/parsers/svg-utils.js.map +1 -0
  76. package/dist/esm/lib/texture-api/async-deep-map.js +34 -0
  77. package/dist/esm/lib/texture-api/async-deep-map.js.map +1 -0
  78. package/dist/esm/lib/texture-api/deep-load.js +10 -0
  79. package/dist/esm/lib/texture-api/deep-load.js.map +1 -0
  80. package/dist/esm/lib/texture-api/generate-url.js +19 -0
  81. package/dist/esm/lib/texture-api/generate-url.js.map +1 -0
  82. package/dist/esm/lib/texture-api/load-image.js +51 -0
  83. package/dist/esm/lib/texture-api/load-image.js.map +1 -0
  84. package/dist/esm/lib/utils/version.js +2 -0
  85. package/dist/esm/lib/utils/version.js.map +1 -0
  86. package/dist/esm/types.js +2 -0
  87. package/dist/esm/types.js.map +1 -0
  88. package/dist/image-loader.d.ts +3 -14
  89. package/dist/image-loader.d.ts.map +1 -1
  90. package/dist/image-loader.js +41 -21
  91. package/dist/image-writer.d.ts +8 -13
  92. package/dist/image-writer.d.ts.map +1 -1
  93. package/dist/image-writer.js +20 -16
  94. package/dist/index.d.ts +2 -1
  95. package/dist/index.d.ts.map +1 -1
  96. package/dist/index.js +29 -8
  97. package/dist/lib/category-api/binary-image-api.d.ts +3 -7
  98. package/dist/lib/category-api/binary-image-api.d.ts.map +1 -1
  99. package/dist/lib/category-api/binary-image-api.js +145 -102
  100. package/dist/lib/category-api/image-format.d.ts +4 -2
  101. package/dist/lib/category-api/image-format.d.ts.map +1 -1
  102. package/dist/lib/category-api/image-format.js +107 -41
  103. package/dist/lib/category-api/image-type.js +44 -38
  104. package/dist/lib/category-api/parse-isobmff-binary.d.ts +19 -0
  105. package/dist/lib/category-api/parse-isobmff-binary.d.ts.map +1 -0
  106. package/dist/lib/category-api/parse-isobmff-binary.js +94 -0
  107. package/dist/lib/category-api/parsed-image-api.js +62 -57
  108. package/dist/lib/encoders/encode-image.d.ts +2 -1
  109. package/dist/lib/encoders/encode-image.d.ts.map +1 -1
  110. package/dist/lib/encoders/encode-image.js +72 -60
  111. package/dist/lib/parsers/parse-image.js +54 -49
  112. package/dist/lib/parsers/parse-to-image-bitmap.js +53 -35
  113. package/dist/lib/parsers/parse-to-image.js +43 -30
  114. package/dist/lib/parsers/parse-to-node-image.js +13 -10
  115. package/dist/lib/parsers/svg-utils.js +36 -26
  116. package/dist/lib/texture-api/async-deep-map.js +49 -32
  117. package/dist/lib/texture-api/deep-load.js +13 -8
  118. package/dist/lib/texture-api/generate-url.js +19 -21
  119. package/dist/lib/texture-api/load-image.js +42 -47
  120. package/dist/lib/utils/version.js +7 -2
  121. package/dist/types.d.ts +3 -3
  122. package/dist/types.d.ts.map +1 -1
  123. package/dist/types.js +2 -2
  124. package/package.json +8 -5
  125. package/src/image-loader.ts +5 -4
  126. package/src/image-writer.ts +13 -2
  127. package/src/index.ts +3 -2
  128. package/src/lib/category-api/binary-image-api.ts +25 -6
  129. package/src/lib/category-api/image-format.ts +92 -39
  130. package/src/lib/category-api/parse-isobmff-binary.ts +105 -0
  131. package/src/lib/encoders/encode-image.ts +2 -1
  132. package/src/types.ts +1 -1
  133. package/dist/bundle.js.map +0 -1
  134. package/dist/image-loader.js.map +0 -1
  135. package/dist/image-writer.js.map +0 -1
  136. package/dist/index.js.map +0 -1
  137. package/dist/lib/category-api/binary-image-api.js.map +0 -1
  138. package/dist/lib/category-api/image-format.js.map +0 -1
  139. package/dist/lib/category-api/image-type.js.map +0 -1
  140. package/dist/lib/category-api/parsed-image-api.js.map +0 -1
  141. package/dist/lib/encoders/encode-image.js.map +0 -1
  142. package/dist/lib/parsers/parse-image.js.map +0 -1
  143. package/dist/lib/parsers/parse-to-image-bitmap.js.map +0 -1
  144. package/dist/lib/parsers/parse-to-image.js.map +0 -1
  145. package/dist/lib/parsers/parse-to-node-image.js.map +0 -1
  146. package/dist/lib/parsers/svg-utils.js.map +0 -1
  147. package/dist/lib/texture-api/async-deep-map.js.map +0 -1
  148. package/dist/lib/texture-api/deep-load.js.map +0 -1
  149. package/dist/lib/texture-api/generate-url.js.map +0 -1
  150. package/dist/lib/texture-api/load-image.js.map +0 -1
  151. package/dist/lib/utils/version.js.map +0 -1
  152. package/dist/types.js.map +0 -1
@@ -1,43 +1,61 @@
1
- import { isSVG, getBlob } from './svg-utils';
2
- import parseToImage from './parse-to-image';
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const svg_utils_1 = require("./svg-utils");
7
+ const parse_to_image_1 = __importDefault(require("./parse-to-image"));
3
8
  const EMPTY_OBJECT = {};
4
9
  let imagebitmapOptionsSupported = true;
5
- export default async function parseToImageBitmap(arrayBuffer, options, url) {
6
- let blob;
7
-
8
- if (isSVG(url)) {
9
- const image = await parseToImage(arrayBuffer, options, url);
10
- blob = image;
11
- } else {
12
- blob = getBlob(arrayBuffer, url);
13
- }
14
-
15
- const imagebitmapOptions = options && options.imagebitmap;
16
- return await safeCreateImageBitmap(blob, imagebitmapOptions);
10
+ /**
11
+ * Asynchronously parses an array buffer into an ImageBitmap - this contains the decoded data
12
+ * ImageBitmaps are supported on worker threads, but not supported on Edge, IE11 and Safari
13
+ * https://developer.mozilla.org/en-US/docs/Web/API/ImageBitmap#Browser_compatibility
14
+ *
15
+ * TODO - createImageBitmap supports source rect (5 param overload), pass through?
16
+ */
17
+ async function parseToImageBitmap(arrayBuffer, options, url) {
18
+ let blob;
19
+ // Cannot parse SVG directly to ImageBitmap, parse to Image first
20
+ if ((0, svg_utils_1.isSVG)(url)) {
21
+ // Note: this only works on main thread
22
+ const image = await (0, parse_to_image_1.default)(arrayBuffer, options, url);
23
+ blob = image;
24
+ }
25
+ else {
26
+ // Create blob from the array buffer
27
+ blob = (0, svg_utils_1.getBlob)(arrayBuffer, url);
28
+ }
29
+ const imagebitmapOptions = options && options.imagebitmap;
30
+ return await safeCreateImageBitmap(blob, imagebitmapOptions);
17
31
  }
18
-
32
+ exports.default = parseToImageBitmap;
33
+ /**
34
+ * Safely creates an imageBitmap with options
35
+ * *
36
+ * Firefox crashes if imagebitmapOptions is supplied
37
+ * Avoid supplying if not provided or supported, remember if not supported
38
+ */
19
39
  async function safeCreateImageBitmap(blob, imagebitmapOptions = null) {
20
- if (isEmptyObject(imagebitmapOptions) || !imagebitmapOptionsSupported) {
21
- imagebitmapOptions = null;
22
- }
23
-
24
- if (imagebitmapOptions) {
25
- try {
26
- return await createImageBitmap(blob, imagebitmapOptions);
27
- } catch (error) {
28
- console.warn(error);
29
- imagebitmapOptionsSupported = false;
40
+ if (isEmptyObject(imagebitmapOptions) || !imagebitmapOptionsSupported) {
41
+ imagebitmapOptions = null;
30
42
  }
31
- }
32
-
33
- return await createImageBitmap(blob);
43
+ if (imagebitmapOptions) {
44
+ try {
45
+ // @ts-ignore Options
46
+ return await createImageBitmap(blob, imagebitmapOptions);
47
+ }
48
+ catch (error) {
49
+ console.warn(error); // eslint-disable-line
50
+ imagebitmapOptionsSupported = false;
51
+ }
52
+ }
53
+ return await createImageBitmap(blob);
34
54
  }
35
-
36
55
  function isEmptyObject(object) {
37
- for (const key in object || EMPTY_OBJECT) {
38
- return false;
39
- }
40
-
41
- return true;
56
+ // @ts-ignore
57
+ for (const key in object || EMPTY_OBJECT) {
58
+ return false;
59
+ }
60
+ return true;
42
61
  }
43
- //# sourceMappingURL=parse-to-image-bitmap.js.map
@@ -1,34 +1,47 @@
1
- import { getBlobOrSVGDataUrl } from './svg-utils';
2
- export default async function parseToImage(arrayBuffer, options, url) {
3
- const blobOrDataUrl = getBlobOrSVGDataUrl(arrayBuffer, url);
4
- const URL = self.URL || self.webkitURL;
5
- const objectUrl = typeof blobOrDataUrl !== 'string' && URL.createObjectURL(blobOrDataUrl);
6
-
7
- try {
8
- return await loadToImage(objectUrl || blobOrDataUrl, options);
9
- } finally {
10
- if (objectUrl) {
11
- URL.revokeObjectURL(objectUrl);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadToImage = void 0;
4
+ const svg_utils_1 = require("./svg-utils");
5
+ // Parses html image from array buffer
6
+ async function parseToImage(arrayBuffer, options, url) {
7
+ // Note: image parsing requires conversion to Blob (for createObjectURL).
8
+ // Potentially inefficient for not using `response.blob()` (and for File / Blob inputs)...
9
+ // But presumably not worth adding 'blob' flag to loader objects?
10
+ const blobOrDataUrl = (0, svg_utils_1.getBlobOrSVGDataUrl)(arrayBuffer, url);
11
+ const URL = self.URL || self.webkitURL;
12
+ const objectUrl = typeof blobOrDataUrl !== 'string' && URL.createObjectURL(blobOrDataUrl);
13
+ try {
14
+ return await loadToImage(objectUrl || blobOrDataUrl, options);
15
+ }
16
+ finally {
17
+ if (objectUrl) {
18
+ URL.revokeObjectURL(objectUrl);
19
+ }
12
20
  }
13
- }
14
21
  }
15
- export async function loadToImage(url, options) {
16
- const image = new Image();
17
- image.src = url;
18
-
19
- if (options.image && options.image.decode && image.decode) {
20
- await image.decode();
21
- return image;
22
- }
23
-
24
- return await new Promise((resolve, reject) => {
25
- try {
26
- image.onload = () => resolve(image);
27
-
28
- image.onerror = err => reject(new Error("Could not load image ".concat(url, ": ").concat(err)));
29
- } catch (error) {
30
- reject(error);
22
+ exports.default = parseToImage;
23
+ async function loadToImage(url, options) {
24
+ const image = new Image();
25
+ image.src = url;
26
+ // The `image.onload()` callback does not guarantee that the image has been decoded
27
+ // so a main thread "freeze" can be incurred when using the image for the first time.
28
+ // `Image.decode()` returns a promise that completes when image is decoded.
29
+ // https://developer.mozilla.org/en-US/docs/Web/API/HTMLImageElement/decode
30
+ // Note: When calling `img.decode()`, we do not need to wait for `img.onload()`
31
+ // Note: `HTMLImageElement.decode()` is not available in Edge and IE11
32
+ if (options.image && options.image.decode && image.decode) {
33
+ await image.decode();
34
+ return image;
31
35
  }
32
- });
36
+ // Create a promise that tracks onload/onerror callbacks
37
+ return await new Promise((resolve, reject) => {
38
+ try {
39
+ image.onload = () => resolve(image);
40
+ image.onerror = (err) => reject(new Error(`Could not load image ${url}: ${err}`));
41
+ }
42
+ catch (error) {
43
+ reject(error);
44
+ }
45
+ });
33
46
  }
34
- //# sourceMappingURL=parse-to-image.js.map
47
+ exports.loadToImage = loadToImage;
@@ -1,11 +1,14 @@
1
- import { assert } from '@loaders.gl/loader-utils';
2
- import { getBinaryImageMetadata } from '../category-api/binary-image-api';
3
- export default async function parseToNodeImage(arrayBuffer, options) {
4
- const {
5
- mimeType
6
- } = getBinaryImageMetadata(arrayBuffer) || {};
7
- const _parseImageNode = globalThis._parseImageNode;
8
- assert(_parseImageNode);
9
- return await _parseImageNode(arrayBuffer, mimeType);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
4
+ const binary_image_api_1 = require("../category-api/binary-image-api");
5
+ // Use polyfills if installed to parsed image using get-pixels
6
+ async function parseToNodeImage(arrayBuffer, options) {
7
+ const { mimeType } = (0, binary_image_api_1.getBinaryImageMetadata)(arrayBuffer) || {};
8
+ // @ts-ignore
9
+ const _parseImageNode = globalThis._parseImageNode;
10
+ (0, loader_utils_1.assert)(_parseImageNode); // '@loaders.gl/polyfills not installed'
11
+ // @ts-expect-error TODO should we throw error in this case?
12
+ return await _parseImageNode(arrayBuffer, mimeType);
10
13
  }
11
- //# sourceMappingURL=parse-to-node-image.js.map
14
+ exports.default = parseToNodeImage;
@@ -1,32 +1,42 @@
1
+ "use strict";
2
+ // SVG parsing has limitations, e.g:
3
+ // https://bugs.chromium.org/p/chromium/issues/detail?id=606319
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.getBlob = exports.getBlobOrSVGDataUrl = exports.isSVG = void 0;
1
6
  const SVG_DATA_URL_PATTERN = /^data:image\/svg\+xml/;
2
7
  const SVG_URL_PATTERN = /\.svg((\?|#).*)?$/;
3
- export function isSVG(url) {
4
- return url && (SVG_DATA_URL_PATTERN.test(url) || SVG_URL_PATTERN.test(url));
8
+ function isSVG(url) {
9
+ return url && (SVG_DATA_URL_PATTERN.test(url) || SVG_URL_PATTERN.test(url));
5
10
  }
6
- export function getBlobOrSVGDataUrl(arrayBuffer, url) {
7
- if (isSVG(url)) {
8
- const textDecoder = new TextDecoder();
9
- let xmlText = textDecoder.decode(arrayBuffer);
10
-
11
- try {
12
- if (typeof unescape === 'function' && typeof encodeURIComponent === 'function') {
13
- xmlText = unescape(encodeURIComponent(xmlText));
14
- }
15
- } catch (error) {
16
- throw new Error(error.message);
11
+ exports.isSVG = isSVG;
12
+ function getBlobOrSVGDataUrl(arrayBuffer, url) {
13
+ if (isSVG(url)) {
14
+ // Prepare a properly tagged data URL, and load using normal mechanism
15
+ const textDecoder = new TextDecoder();
16
+ let xmlText = textDecoder.decode(arrayBuffer);
17
+ // TODO Escape in browser to support e.g. Chinese characters
18
+ try {
19
+ if (typeof unescape === 'function' && typeof encodeURIComponent === 'function') {
20
+ xmlText = unescape(encodeURIComponent(xmlText));
21
+ }
22
+ }
23
+ catch (error) {
24
+ throw new Error(error.message);
25
+ }
26
+ // base64 encoding is safer. utf-8 fails in some browsers
27
+ const src = `data:image/svg+xml;base64,${btoa(xmlText)}`;
28
+ return src;
17
29
  }
18
-
19
- const src = "data:image/svg+xml;base64,".concat(btoa(xmlText));
20
- return src;
21
- }
22
-
23
- return getBlob(arrayBuffer, url);
30
+ return getBlob(arrayBuffer, url);
24
31
  }
25
- export function getBlob(arrayBuffer, url) {
26
- if (isSVG(url)) {
27
- throw new Error('SVG cannot be parsed directly to imagebitmap');
28
- }
29
-
30
- return new Blob([new Uint8Array(arrayBuffer)]);
32
+ exports.getBlobOrSVGDataUrl = getBlobOrSVGDataUrl;
33
+ function getBlob(arrayBuffer, url) {
34
+ if (isSVG(url)) {
35
+ // https://bugs.chromium.org/p/chromium/issues/detail?id=606319
36
+ // return new Blob([new Uint8Array(arrayBuffer)], {type: 'image/svg+xml'});
37
+ throw new Error('SVG cannot be parsed directly to imagebitmap');
38
+ }
39
+ // TODO - how to determine mime type? Param? Sniff here?
40
+ return new Blob([new Uint8Array(arrayBuffer)]); // MIME type not needed?
31
41
  }
32
- //# sourceMappingURL=svg-utils.js.map
42
+ exports.getBlob = getBlob;
@@ -1,39 +1,56 @@
1
- const isObject = value => value && typeof value === 'object';
1
+ "use strict";
2
+ /*
3
+ Asynchronously maps a deep structure of values (e.g. objects and arrays of urls).
2
4
 
3
- export async function asyncDeepMap(tree, func, options = {}) {
4
- return await mapSubtree(tree, func, options);
5
+ E.g. a mipmapped cubemap
6
+ {
7
+ [CUBE_FACE_FRONT]: [
8
+ "image-front-0.jpg",
9
+ "image-front-1.jpg",
10
+ "image-front-2.jpg",
11
+ ],
12
+ [CUBE_MAP_BACK]: [
13
+ ...
14
+ ]
5
15
  }
6
- export async function mapSubtree(object, func, options) {
7
- if (Array.isArray(object)) {
8
- return await mapArray(object, func, options);
9
- }
10
-
11
- if (isObject(object)) {
12
- return await mapObject(object, func, options);
13
- }
14
-
15
- const url = object;
16
- return await func(url, options);
16
+ */
17
+ Object.defineProperty(exports, "__esModule", { value: true });
18
+ exports.mapSubtree = exports.asyncDeepMap = void 0;
19
+ const isObject = (value) => value && typeof value === 'object';
20
+ // Loads a deep structure of urls (objects and arrays of urls)
21
+ // Returns an object with six key-value pairs containing the images (or image mip arrays)
22
+ // for each cube face
23
+ async function asyncDeepMap(tree, func, options = {}) {
24
+ return await mapSubtree(tree, func, options);
17
25
  }
18
-
26
+ exports.asyncDeepMap = asyncDeepMap;
27
+ async function mapSubtree(object, func, options) {
28
+ if (Array.isArray(object)) {
29
+ return await mapArray(object, func, options);
30
+ }
31
+ if (isObject(object)) {
32
+ return await mapObject(object, func, options);
33
+ }
34
+ // TODO - ignore non-urls, non-arraybuffers?
35
+ const url = object;
36
+ return await func(url, options);
37
+ }
38
+ exports.mapSubtree = mapSubtree;
39
+ // HELPERS
19
40
  async function mapObject(object, func, options) {
20
- const promises = [];
21
- const values = {};
22
-
23
- for (const key in object) {
24
- const url = object[key];
25
- const promise = mapSubtree(url, func, options).then(value => {
26
- values[key] = value;
27
- });
28
- promises.push(promise);
29
- }
30
-
31
- await Promise.all(promises);
32
- return values;
41
+ const promises = [];
42
+ const values = {};
43
+ for (const key in object) {
44
+ const url = object[key];
45
+ const promise = mapSubtree(url, func, options).then((value) => {
46
+ values[key] = value;
47
+ });
48
+ promises.push(promise);
49
+ }
50
+ await Promise.all(promises);
51
+ return values;
33
52
  }
34
-
35
53
  async function mapArray(urlArray, func, options = {}) {
36
- const promises = urlArray.map(url => mapSubtree(url, func, options));
37
- return await Promise.all(promises);
54
+ const promises = urlArray.map((url) => mapSubtree(url, func, options));
55
+ return await Promise.all(promises);
38
56
  }
39
- //# sourceMappingURL=async-deep-map.js.map
@@ -1,10 +1,15 @@
1
- import { asyncDeepMap } from './async-deep-map';
2
- export async function deepLoad(urlTree, load, options) {
3
- return await asyncDeepMap(urlTree, url => shallowLoad(url, load, options));
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.shallowLoad = exports.deepLoad = void 0;
4
+ const async_deep_map_1 = require("./async-deep-map");
5
+ async function deepLoad(urlTree, load, options) {
6
+ return await (0, async_deep_map_1.asyncDeepMap)(urlTree, (url) => shallowLoad(url, load, options));
4
7
  }
5
- export async function shallowLoad(url, load, options) {
6
- const response = await fetch(url, options.fetch);
7
- const arrayBuffer = await response.arrayBuffer();
8
- return await load(arrayBuffer, options);
8
+ exports.deepLoad = deepLoad;
9
+ async function shallowLoad(url, load, options) {
10
+ // console.error('loading', url);
11
+ const response = await fetch(url, options.fetch);
12
+ const arrayBuffer = await response.arrayBuffer();
13
+ return await load(arrayBuffer, options);
9
14
  }
10
- //# sourceMappingURL=deep-load.js.map
15
+ exports.shallowLoad = shallowLoad;
@@ -1,22 +1,20 @@
1
- import { resolvePath, assert } from '@loaders.gl/loader-utils';
2
- export function generateUrl(getUrl, options, urlOptions) {
3
- let url = getUrl;
4
-
5
- if (typeof getUrl === 'function') {
6
- url = getUrl({ ...options,
7
- ...urlOptions
8
- });
9
- }
10
-
11
- assert(typeof url === 'string');
12
- const {
13
- baseUrl
14
- } = options;
15
-
16
- if (baseUrl) {
17
- url = baseUrl[baseUrl.length - 1] === '/' ? "".concat(baseUrl).concat(url) : "".concat(baseUrl, "/").concat(url);
18
- }
19
-
20
- return resolvePath(url);
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.generateUrl = void 0;
4
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
5
+ // Generate a url by calling getUrl with mix of options, applying options.baseUrl
6
+ function generateUrl(getUrl, options, urlOptions) {
7
+ // Get url
8
+ let url = getUrl;
9
+ if (typeof getUrl === 'function') {
10
+ url = getUrl({ ...options, ...urlOptions });
11
+ }
12
+ (0, loader_utils_1.assert)(typeof url === 'string');
13
+ // Apply options.baseUrl
14
+ const { baseUrl } = options;
15
+ if (baseUrl) {
16
+ url = baseUrl[baseUrl.length - 1] === '/' ? `${baseUrl}${url}` : `${baseUrl}/${url}`;
17
+ }
18
+ return (0, loader_utils_1.resolvePath)(url);
21
19
  }
22
- //# sourceMappingURL=generate-url.js.map
20
+ exports.generateUrl = generateUrl;
@@ -1,52 +1,47 @@
1
- import { assert } from '@loaders.gl/loader-utils';
2
- import parseImage from '../parsers/parse-image';
3
- import { getImageSize } from '../category-api/parsed-image-api';
4
- import { generateUrl } from './generate-url';
5
- import { deepLoad, shallowLoad } from './deep-load';
6
- export async function loadImage(getUrl, options = {}) {
7
- const imageUrls = await getImageUrls(getUrl, options);
8
- return await deepLoad(imageUrls, parseImage, options);
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.getMipLevels = exports.getImageUrls = exports.loadImage = void 0;
7
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
8
+ const parse_image_1 = __importDefault(require("../parsers/parse-image"));
9
+ const parsed_image_api_1 = require("../category-api/parsed-image-api");
10
+ const generate_url_1 = require("./generate-url");
11
+ const deep_load_1 = require("./deep-load");
12
+ async function loadImage(getUrl, options = {}) {
13
+ const imageUrls = await getImageUrls(getUrl, options);
14
+ return await (0, deep_load_1.deepLoad)(imageUrls, parse_image_1.default, options);
9
15
  }
10
- export async function getImageUrls(getUrl, options, urlOptions = {}) {
11
- const mipLevels = options && options.image && options.image.mipLevels || 0;
12
- return mipLevels !== 0 ? await getMipmappedImageUrls(getUrl, mipLevels, options, urlOptions) : generateUrl(getUrl, options, urlOptions);
16
+ exports.loadImage = loadImage;
17
+ async function getImageUrls(getUrl, options, urlOptions = {}) {
18
+ const mipLevels = (options && options.image && options.image.mipLevels) || 0;
19
+ return mipLevels !== 0
20
+ ? await getMipmappedImageUrls(getUrl, mipLevels, options, urlOptions)
21
+ : (0, generate_url_1.generateUrl)(getUrl, options, urlOptions);
13
22
  }
14
-
23
+ exports.getImageUrls = getImageUrls;
15
24
  async function getMipmappedImageUrls(getUrl, mipLevels, options, urlOptions) {
16
- const urls = [];
17
-
18
- if (mipLevels === 'auto') {
19
- const url = generateUrl(getUrl, options, { ...urlOptions,
20
- lod: 0
21
- });
22
- const image = await shallowLoad(url, parseImage, options);
23
- const {
24
- width,
25
- height
26
- } = getImageSize(image);
27
- mipLevels = getMipLevels({
28
- width,
29
- height
30
- });
31
- urls.push(url);
32
- }
33
-
34
- assert(mipLevels > 0);
35
-
36
- for (let mipLevel = urls.length; mipLevel < mipLevels; ++mipLevel) {
37
- const url = generateUrl(getUrl, options, { ...urlOptions,
38
- lod: mipLevel
39
- });
40
- urls.push(url);
41
- }
42
-
43
- return urls;
25
+ const urls = [];
26
+ // If no mip levels supplied, we need to load the level 0 image and calculate based on size
27
+ if (mipLevels === 'auto') {
28
+ const url = (0, generate_url_1.generateUrl)(getUrl, options, { ...urlOptions, lod: 0 });
29
+ const image = await (0, deep_load_1.shallowLoad)(url, parse_image_1.default, options);
30
+ const { width, height } = (0, parsed_image_api_1.getImageSize)(image);
31
+ mipLevels = getMipLevels({ width, height });
32
+ // TODO - push image and make `deepLoad` pass through non-url values, avoid loading twice?
33
+ urls.push(url);
34
+ }
35
+ // We now know how many mipLevels we need, remaining image urls can now be constructed
36
+ (0, loader_utils_1.assert)(mipLevels > 0);
37
+ for (let mipLevel = urls.length; mipLevel < mipLevels; ++mipLevel) {
38
+ const url = (0, generate_url_1.generateUrl)(getUrl, options, { ...urlOptions, lod: mipLevel });
39
+ urls.push(url);
40
+ }
41
+ return urls;
44
42
  }
45
-
46
- export function getMipLevels({
47
- width,
48
- height
49
- }) {
50
- return 1 + Math.floor(Math.log2(Math.max(width, height)));
43
+ // Calculates number of mipmaps based on texture size (log2)
44
+ function getMipLevels({ width, height }) {
45
+ return 1 + Math.floor(Math.log2(Math.max(width, height)));
51
46
  }
52
- //# sourceMappingURL=load-image.js.map
47
+ exports.getMipLevels = getMipLevels;
@@ -1,2 +1,7 @@
1
- export const VERSION = typeof "4.0.0-alpha.5" !== 'undefined' ? "4.0.0-alpha.5" : 'latest';
2
- //# sourceMappingURL=version.js.map
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.VERSION = void 0;
4
+ // Version constant cannot be imported, it needs to correspond to the build version of **this** module.
5
+ // __VERSION__ is injected by babel-plugin-version-inline
6
+ // @ts-ignore TS2304: Cannot find name '__VERSION__'.
7
+ exports.VERSION = typeof __VERSION__ !== 'undefined' ? __VERSION__ : 'latest';
package/dist/types.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /**
2
2
  * data images
3
3
  */
4
- export declare type ImageDataType = {
4
+ export type ImageDataType = {
5
5
  data: Uint8Array;
6
6
  width: number;
7
7
  height: number;
@@ -10,9 +10,9 @@ export declare type ImageDataType = {
10
10
  /**
11
11
  * Supported Image Types
12
12
  */
13
- export declare type ImageType = ImageBitmap | typeof Image | ImageDataType;
13
+ export type ImageType = ImageBitmap | ImageDataType | HTMLImageElement;
14
14
  /**
15
15
  * Image type string used to control or determine the type of images returned from ImageLoader
16
16
  */
17
- export declare type ImageTypeEnum = 'imagebitmap' | 'image' | 'data';
17
+ export type ImageTypeEnum = 'imagebitmap' | 'image' | 'data';
18
18
  //# sourceMappingURL=types.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,oBAAY,aAAa,GAAG;IAC1B,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AAEF;;GAEG;AACH,oBAAY,SAAS,GAAG,WAAW,GAAG,OAAO,KAAK,GAAG,aAAa,CAAC;AAEnE;;GAEG;AACH,oBAAY,aAAa,GAAG,aAAa,GAAG,OAAO,GAAG,MAAM,CAAC"}
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG;IAC1B,IAAI,EAAE,UAAU,CAAC;IACjB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB,CAAC;AAEF;;GAEG;AACH,MAAM,MAAM,SAAS,GAAG,WAAW,GAAG,aAAa,GAAG,gBAAgB,CAAC;AAEvE;;GAEG;AACH,MAAM,MAAM,aAAa,GAAG,aAAa,GAAG,OAAO,GAAG,MAAM,CAAC"}
package/dist/types.js CHANGED
@@ -1,2 +1,2 @@
1
- export {};
2
- //# sourceMappingURL=types.js.map
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@loaders.gl/images",
3
- "version": "4.0.0-alpha.5",
3
+ "version": "4.0.0-alpha.7",
4
4
  "description": "Framework-independent loaders and writers for images (PNG, JPG, ...)",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -19,8 +19,8 @@
19
19
  "PLY"
20
20
  ],
21
21
  "types": "dist/index.d.ts",
22
- "main": "dist/index.js",
23
- "module": "dist/index.js",
22
+ "main": "dist/es5/index.js",
23
+ "module": "dist/esm/index.js",
24
24
  "sideEffects": false,
25
25
  "files": [
26
26
  "src",
@@ -32,7 +32,10 @@
32
32
  "build-bundle": "esbuild src/bundle.ts --outfile=dist/dist.min.js --bundle --minify --sourcemap"
33
33
  },
34
34
  "dependencies": {
35
- "@loaders.gl/loader-utils": "4.0.0-alpha.5"
35
+ "@loaders.gl/loader-utils": "4.0.0-alpha.7"
36
36
  },
37
- "gitHead": "7a71a54bdf1ddf985cc3af3db90b82e7fa97d025"
37
+ "gitHead": "afb59c4d8e5d8ebb9c28f111cb0c96c5527d0ffd",
38
+ "devDependencies": {
39
+ "@types/get-pixels": "^3.3.2"
40
+ }
38
41
  }
@@ -1,14 +1,17 @@
1
1
  import type {LoaderOptions, LoaderWithParser} from '@loaders.gl/loader-utils';
2
+ import type {ImageType} from './types';
3
+ // import type { ImageType } from '@loaders.gl/schema';
2
4
  import {VERSION} from './lib/utils/version';
3
5
  import parseImage from './lib/parsers/parse-image';
4
6
  import {getBinaryImageMetadata} from './lib/category-api/binary-image-api';
5
7
 
6
- const EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'ico', 'svg'];
8
+ const EXTENSIONS = ['png', 'jpg', 'jpeg', 'gif', 'webp', 'bmp', 'ico', 'svg', 'avif'];
7
9
  const MIME_TYPES = [
8
10
  'image/png',
9
11
  'image/jpeg',
10
12
  'image/gif',
11
13
  'image/webp',
14
+ 'image/avif',
12
15
  'image/bmp',
13
16
  'image/vnd.microsoft.icon',
14
17
  'image/svg+xml'
@@ -34,7 +37,7 @@ const DEFAULT_IMAGE_LOADER_OPTIONS: ImageLoaderOptions = {
34
37
  * Loads a platform-specific image type
35
38
  * Note: This type can be used as input data to WebGL texture creation
36
39
  */
37
- export const ImageLoader = {
40
+ export const ImageLoader: LoaderWithParser<ImageType, never, ImageLoaderOptions> = {
38
41
  id: 'image',
39
42
  module: 'images',
40
43
  name: 'Images',
@@ -46,5 +49,3 @@ export const ImageLoader = {
46
49
  tests: [(arrayBuffer) => Boolean(getBinaryImageMetadata(new DataView(arrayBuffer)))],
47
50
  options: DEFAULT_IMAGE_LOADER_OPTIONS
48
51
  };
49
-
50
- export const _typecheckImageLoader: LoaderWithParser = ImageLoader;