@loaders.gl/images 3.1.0-alpha.5 → 3.1.0-beta.4

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 (67) hide show
  1. package/dist/bundle.d.ts +1 -0
  2. package/dist/bundle.d.ts.map +1 -0
  3. package/dist/bundle.js +5 -0
  4. package/dist/dist.min.js +2 -2
  5. package/dist/dist.min.js.map +7 -1
  6. package/dist/es5/lib/utils/version.js +1 -1
  7. package/dist/es5/lib/utils/version.js.map +1 -1
  8. package/dist/esm/lib/utils/version.js +1 -1
  9. package/dist/esm/lib/utils/version.js.map +1 -1
  10. package/dist/image-loader.d.ts +1 -0
  11. package/dist/image-loader.d.ts.map +1 -0
  12. package/dist/image-loader.js +43 -0
  13. package/dist/image-writer.d.ts +1 -0
  14. package/dist/image-writer.d.ts.map +1 -0
  15. package/dist/image-writer.js +20 -0
  16. package/dist/index.d.ts +1 -0
  17. package/dist/index.d.ts.map +1 -0
  18. package/dist/index.js +27 -0
  19. package/dist/lib/category-api/binary-image-api.d.ts +1 -0
  20. package/dist/lib/category-api/binary-image-api.d.ts.map +1 -0
  21. package/dist/lib/category-api/binary-image-api.js +141 -0
  22. package/dist/lib/category-api/image-format.d.ts +1 -0
  23. package/dist/lib/category-api/image-format.d.ts.map +1 -0
  24. package/dist/lib/category-api/image-format.js +63 -0
  25. package/dist/lib/category-api/image-type.d.ts +1 -0
  26. package/dist/lib/category-api/image-type.d.ts.map +1 -0
  27. package/dist/lib/category-api/image-type.js +48 -0
  28. package/dist/lib/category-api/parsed-image-api.d.ts +1 -0
  29. package/dist/lib/category-api/parsed-image-api.d.ts.map +1 -0
  30. package/dist/lib/category-api/parsed-image-api.js +69 -0
  31. package/dist/lib/encoders/encode-image.d.ts +1 -0
  32. package/dist/lib/encoders/encode-image.d.ts.map +1 -0
  33. package/dist/lib/encoders/encode-image.js +82 -0
  34. package/dist/lib/parsers/parse-image.d.ts +1 -0
  35. package/dist/lib/parsers/parse-image.d.ts.map +1 -0
  36. package/dist/lib/parsers/parse-image.js +57 -0
  37. package/dist/lib/parsers/parse-to-image-bitmap.d.ts +1 -0
  38. package/dist/lib/parsers/parse-to-image-bitmap.d.ts.map +1 -0
  39. package/dist/lib/parsers/parse-to-image-bitmap.js +61 -0
  40. package/dist/lib/parsers/parse-to-image.d.ts +1 -0
  41. package/dist/lib/parsers/parse-to-image.d.ts.map +1 -0
  42. package/dist/lib/parsers/parse-to-image.js +47 -0
  43. package/dist/lib/parsers/parse-to-node-image.d.ts +1 -0
  44. package/dist/lib/parsers/parse-to-node-image.d.ts.map +1 -0
  45. package/dist/lib/parsers/parse-to-node-image.js +14 -0
  46. package/dist/lib/parsers/svg-utils.d.ts +1 -0
  47. package/dist/lib/parsers/svg-utils.d.ts.map +1 -0
  48. package/dist/lib/parsers/svg-utils.js +42 -0
  49. package/dist/lib/texture-api/async-deep-map.d.ts +1 -0
  50. package/dist/lib/texture-api/async-deep-map.d.ts.map +1 -0
  51. package/dist/lib/texture-api/async-deep-map.js +56 -0
  52. package/dist/lib/texture-api/deep-load.d.ts +1 -0
  53. package/dist/lib/texture-api/deep-load.d.ts.map +1 -0
  54. package/dist/lib/texture-api/deep-load.js +15 -0
  55. package/dist/lib/texture-api/generate-url.d.ts +1 -0
  56. package/dist/lib/texture-api/generate-url.d.ts.map +1 -0
  57. package/dist/lib/texture-api/generate-url.js +20 -0
  58. package/dist/lib/texture-api/load-image.d.ts +1 -0
  59. package/dist/lib/texture-api/load-image.d.ts.map +1 -0
  60. package/dist/lib/texture-api/load-image.js +47 -0
  61. package/dist/lib/utils/version.d.ts +1 -0
  62. package/dist/lib/utils/version.d.ts.map +1 -0
  63. package/dist/lib/utils/version.js +7 -0
  64. package/dist/types.d.ts +1 -0
  65. package/dist/types.d.ts.map +1 -0
  66. package/dist/types.js +2 -0
  67. package/package.json +4 -6
@@ -0,0 +1,141 @@
1
+ "use strict";
2
+ // Attributions
3
+ // * Based on binary-gltf-utils under MIT license: Copyright (c) 2016-17 Karl Cheng
4
+ Object.defineProperty(exports, "__esModule", { value: true });
5
+ exports.getBmpMetadata = exports.getBinaryImageMetadata = void 0;
6
+ const BIG_ENDIAN = false;
7
+ const LITTLE_ENDIAN = true;
8
+ /**
9
+ * Extracts `{mimeType, width and height}` from a memory buffer containing a known image format
10
+ * Currently supports `image/png`, `image/jpeg`, `image/bmp` and `image/gif`.
11
+ * @param binaryData image file memory to parse
12
+ * @returns metadata or null if memory is not a valid image file format layout.
13
+ */
14
+ function getBinaryImageMetadata(binaryData) {
15
+ const dataView = toDataView(binaryData);
16
+ return (getPngMetadata(dataView) ||
17
+ getJpegMetadata(dataView) ||
18
+ getGifMetadata(dataView) ||
19
+ getBmpMetadata(dataView));
20
+ }
21
+ exports.getBinaryImageMetadata = getBinaryImageMetadata;
22
+ // PNG
23
+ function getPngMetadata(binaryData) {
24
+ const dataView = toDataView(binaryData);
25
+ // Check file contains the first 4 bytes of the PNG signature.
26
+ const isPng = dataView.byteLength >= 24 && dataView.getUint32(0, BIG_ENDIAN) === 0x89504e47;
27
+ if (!isPng) {
28
+ return null;
29
+ }
30
+ // Extract size from a binary PNG file
31
+ return {
32
+ mimeType: 'image/png',
33
+ width: dataView.getUint32(16, BIG_ENDIAN),
34
+ height: dataView.getUint32(20, BIG_ENDIAN)
35
+ };
36
+ }
37
+ // GIF
38
+ // Extract size from a binary GIF file
39
+ // TODO: GIF is not this simple
40
+ function getGifMetadata(binaryData) {
41
+ const dataView = toDataView(binaryData);
42
+ // Check first 4 bytes of the GIF signature ("GIF8").
43
+ const isGif = dataView.byteLength >= 10 && dataView.getUint32(0, BIG_ENDIAN) === 0x47494638;
44
+ if (!isGif) {
45
+ return null;
46
+ }
47
+ // GIF is little endian.
48
+ return {
49
+ mimeType: 'image/gif',
50
+ width: dataView.getUint16(6, LITTLE_ENDIAN),
51
+ height: dataView.getUint16(8, LITTLE_ENDIAN)
52
+ };
53
+ }
54
+ // BMP
55
+ // TODO: BMP is not this simple
56
+ function getBmpMetadata(binaryData) {
57
+ const dataView = toDataView(binaryData);
58
+ // Check magic number is valid (first 2 characters should be "BM").
59
+ // The mandatory bitmap file header is 14 bytes long.
60
+ const isBmp = dataView.byteLength >= 14 &&
61
+ dataView.getUint16(0, BIG_ENDIAN) === 0x424d &&
62
+ dataView.getUint32(2, LITTLE_ENDIAN) === dataView.byteLength;
63
+ if (!isBmp) {
64
+ return null;
65
+ }
66
+ // BMP is little endian.
67
+ return {
68
+ mimeType: 'image/bmp',
69
+ width: dataView.getUint32(18, LITTLE_ENDIAN),
70
+ height: dataView.getUint32(22, LITTLE_ENDIAN)
71
+ };
72
+ }
73
+ exports.getBmpMetadata = getBmpMetadata;
74
+ // JPEG
75
+ // Extract width and height from a binary JPEG file
76
+ function getJpegMetadata(binaryData) {
77
+ const dataView = toDataView(binaryData);
78
+ // Check file contains the JPEG "start of image" (SOI) marker
79
+ // followed by another marker.
80
+ const isJpeg = dataView.byteLength >= 3 &&
81
+ dataView.getUint16(0, BIG_ENDIAN) === 0xffd8 &&
82
+ dataView.getUint8(2) === 0xff;
83
+ if (!isJpeg) {
84
+ return null;
85
+ }
86
+ const { tableMarkers, sofMarkers } = getJpegMarkers();
87
+ // Exclude the two byte SOI marker.
88
+ let i = 2;
89
+ while (i + 9 < dataView.byteLength) {
90
+ const marker = dataView.getUint16(i, BIG_ENDIAN);
91
+ // The frame that contains the width and height of the JPEG image.
92
+ if (sofMarkers.has(marker)) {
93
+ return {
94
+ mimeType: 'image/jpeg',
95
+ height: dataView.getUint16(i + 5, BIG_ENDIAN),
96
+ width: dataView.getUint16(i + 7, BIG_ENDIAN) // Number of pixels per line
97
+ };
98
+ }
99
+ // Miscellaneous tables/data preceding the frame header.
100
+ if (!tableMarkers.has(marker)) {
101
+ return null;
102
+ }
103
+ // Length includes size of length parameter but not the two byte header.
104
+ i += 2;
105
+ i += dataView.getUint16(i, BIG_ENDIAN);
106
+ }
107
+ return null;
108
+ }
109
+ function getJpegMarkers() {
110
+ // Tables/misc header markers.
111
+ // DQT, DHT, DAC, DRI, COM, APP_n
112
+ const tableMarkers = new Set([0xffdb, 0xffc4, 0xffcc, 0xffdd, 0xfffe]);
113
+ for (let i = 0xffe0; i < 0xfff0; ++i) {
114
+ tableMarkers.add(i);
115
+ }
116
+ // SOF markers and DHP marker.
117
+ // These markers are after tables/misc data.
118
+ const sofMarkers = new Set([
119
+ 0xffc0, 0xffc1, 0xffc2, 0xffc3, 0xffc5, 0xffc6, 0xffc7, 0xffc9, 0xffca, 0xffcb, 0xffcd, 0xffce,
120
+ 0xffcf, 0xffde
121
+ ]);
122
+ return { tableMarkers, sofMarkers };
123
+ }
124
+ // TODO - move into image module?
125
+ function toDataView(data) {
126
+ if (data instanceof DataView) {
127
+ return data;
128
+ }
129
+ if (ArrayBuffer.isView(data)) {
130
+ return new DataView(data.buffer);
131
+ }
132
+ // TODO: make these functions work for Node.js buffers?
133
+ // if (bufferToArrayBuffer) {
134
+ // data = bufferToArrayBuffer(data);
135
+ // }
136
+ // Careful - Node Buffers will look like ArrayBuffers (keep after isBuffer)
137
+ if (data instanceof ArrayBuffer) {
138
+ return new DataView(data);
139
+ }
140
+ throw new Error('toDataView');
141
+ }
@@ -2,3 +2,4 @@
2
2
  * Check if image MIME type is supported. Result is cached.
3
3
  */
4
4
  export declare function _isImageFormatSupported(mimeType: string): boolean;
5
+ //# sourceMappingURL=image-format.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"image-format.d.ts","sourceRoot":"","sources":["../../../src/lib/category-api/image-format.ts"],"names":[],"mappings":"AAQA;;GAEG;AACH,wBAAgB,uBAAuB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAKjE"}
@@ -0,0 +1,63 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports._isImageFormatSupported = void 0;
4
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
5
+ // The following formats are supported by loaders.gl polyfills
6
+ const NODE_FORMAT_SUPPORT = ['image/png', 'image/jpeg', 'image/gif'];
7
+ /** Cache values for speed */
8
+ const mimeTypeSupported = {};
9
+ /**
10
+ * Check if image MIME type is supported. Result is cached.
11
+ */
12
+ function _isImageFormatSupported(mimeType) {
13
+ if (mimeTypeSupported[mimeType] === undefined) {
14
+ mimeTypeSupported[mimeType] = checkFormatSupport(mimeType);
15
+ }
16
+ return mimeTypeSupported[mimeType];
17
+ }
18
+ exports._isImageFormatSupported = _isImageFormatSupported;
19
+ /**
20
+ * Check if image MIME type is supported.
21
+ */
22
+ function checkFormatSupport(mimeType) {
23
+ switch (mimeType) {
24
+ case 'image/webp':
25
+ return checkWebPSupport();
26
+ case 'image/svg':
27
+ return loader_utils_1.isBrowser;
28
+ default:
29
+ if (!loader_utils_1.isBrowser) {
30
+ // @ts-ignore
31
+ const { _parseImageNode } = globalThis;
32
+ return Boolean(_parseImageNode) && NODE_FORMAT_SUPPORT.includes(mimeType);
33
+ }
34
+ return true;
35
+ }
36
+ }
37
+ /** Check WebPSupport synchronously */
38
+ function checkWebPSupport() {
39
+ if (!loader_utils_1.isBrowser) {
40
+ return false;
41
+ }
42
+ try {
43
+ const element = document.createElement('canvas');
44
+ return element.toDataURL('image/webp').indexOf('data:image/webp') === 0;
45
+ }
46
+ catch {
47
+ // Probably Safari...
48
+ return false;
49
+ }
50
+ }
51
+ // Note: better test but asynchronous
52
+ // Lossy test image. Support for lossy images doesn't guarantee support for all WebP images.
53
+ // https://stackoverflow.com/questions/5573096/detecting-webp-support
54
+ // const WEBP_TEST_IMAGE = 'data:image/webp;base64,UklGRiIAAABXRUJQVlA4IBYAAAAwAQCdASoBAAEADsD+JaQAA3AAAAAA';
55
+ // Check WebPSupport asynchronously
56
+ // async function isWebPSupported() {
57
+ // return new Promise( resolve => {
58
+ // const image = new Image();
59
+ // image.src = WEBP_TEST_IMAGE;
60
+ // image.onload = image.onerror = function () {
61
+ // resolve( image.height === 1 );
62
+ // }
63
+ // }
@@ -9,3 +9,4 @@ export declare function isImageTypeSupported(type: string): boolean;
9
9
  * @returns image type string
10
10
  */
11
11
  export declare function getDefaultImageType(): ImageTypeEnum;
12
+ //# sourceMappingURL=image-type.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"image-type.d.ts","sourceRoot":"","sources":["../../../src/lib/category-api/image-type.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAU/C;;;GAGG;AACH,wBAAgB,oBAAoB,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,CAgB1D;AAED;;;GAGG;AACH,wBAAgB,mBAAmB,IAAI,aAAa,CAanD"}
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDefaultImageType = exports.isImageTypeSupported = void 0;
4
+ const loader_utils_1 = require("@loaders.gl/loader-utils");
5
+ // @ts-ignore TS2339: Property does not exist on type
6
+ const { _parseImageNode } = globalThis;
7
+ const IMAGE_SUPPORTED = typeof Image !== 'undefined'; // NOTE: "false" positives if jsdom is installed
8
+ const IMAGE_BITMAP_SUPPORTED = typeof ImageBitmap !== 'undefined';
9
+ const NODE_IMAGE_SUPPORTED = Boolean(_parseImageNode);
10
+ const DATA_SUPPORTED = loader_utils_1.isBrowser ? true : NODE_IMAGE_SUPPORTED;
11
+ /**
12
+ * Checks if a loaders.gl image type is supported
13
+ * @param type image type string
14
+ */
15
+ function isImageTypeSupported(type) {
16
+ switch (type) {
17
+ case 'auto':
18
+ // Should only ever be false in Node.js, if polyfills have not been installed...
19
+ return IMAGE_BITMAP_SUPPORTED || IMAGE_SUPPORTED || DATA_SUPPORTED;
20
+ case 'imagebitmap':
21
+ return IMAGE_BITMAP_SUPPORTED;
22
+ case 'image':
23
+ return IMAGE_SUPPORTED;
24
+ case 'data':
25
+ return DATA_SUPPORTED;
26
+ default:
27
+ throw new Error(`@loaders.gl/images: image ${type} not supported in this environment`);
28
+ }
29
+ }
30
+ exports.isImageTypeSupported = isImageTypeSupported;
31
+ /**
32
+ * Returns the "most performant" supported image type on this platform
33
+ * @returns image type string
34
+ */
35
+ function getDefaultImageType() {
36
+ if (IMAGE_BITMAP_SUPPORTED) {
37
+ return 'imagebitmap';
38
+ }
39
+ if (IMAGE_SUPPORTED) {
40
+ return 'image';
41
+ }
42
+ if (DATA_SUPPORTED) {
43
+ return 'data';
44
+ }
45
+ // This should only happen in Node.js
46
+ throw new Error('Install \'@loaders.gl/polyfills\' to parse images under Node.js');
47
+ }
48
+ exports.getDefaultImageType = getDefaultImageType;
@@ -7,3 +7,4 @@ export declare function getImageSize(image: ImageType): {
7
7
  height: number;
8
8
  };
9
9
  export declare function getImageData(image: ImageType): ImageDataType | ImageData;
10
+ //# sourceMappingURL=parsed-image-api.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parsed-image-api.d.ts","sourceRoot":"","sources":["../../../src/lib/category-api/parsed-image-api.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,SAAS,EAAE,aAAa,EAAE,aAAa,EAAC,MAAM,aAAa,CAAC;AAEzE,wBAAgB,OAAO,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAEjD;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,SAAS,GAAG,IAAI,CAQlD;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,GAAG,aAAa,CAM5D;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,GAAG;IAAC,KAAK,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAA;CAAC,CAE9E;AAED,wBAAgB,YAAY,CAAC,KAAK,EAAE,SAAS,GAAG,aAAa,GAAG,SAAS,CA0BxE"}
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getImageData = exports.getImageSize = exports.getImageType = exports.deleteImage = exports.isImage = void 0;
4
+ function isImage(image) {
5
+ return Boolean(getImageTypeOrNull(image));
6
+ }
7
+ exports.isImage = isImage;
8
+ function deleteImage(image) {
9
+ switch (getImageType(image)) {
10
+ case 'imagebitmap':
11
+ image.close();
12
+ break;
13
+ default:
14
+ // Nothing to do for images and image data objects
15
+ }
16
+ }
17
+ exports.deleteImage = deleteImage;
18
+ function getImageType(image) {
19
+ const format = getImageTypeOrNull(image);
20
+ if (!format) {
21
+ throw new Error('Not an image');
22
+ }
23
+ return format;
24
+ }
25
+ exports.getImageType = getImageType;
26
+ function getImageSize(image) {
27
+ return getImageData(image);
28
+ }
29
+ exports.getImageSize = getImageSize;
30
+ function getImageData(image) {
31
+ switch (getImageType(image)) {
32
+ case 'data':
33
+ return image;
34
+ case 'image':
35
+ case 'imagebitmap':
36
+ // Extract the image data from the image via a canvas
37
+ const canvas = document.createElement('canvas');
38
+ // TODO - reuse the canvas?
39
+ const context = canvas.getContext('2d');
40
+ if (!context) {
41
+ throw new Error('getImageData');
42
+ }
43
+ // @ts-ignore
44
+ canvas.width = image.width;
45
+ // @ts-ignore
46
+ canvas.height = image.height;
47
+ // @ts-ignore
48
+ context.drawImage(image, 0, 0);
49
+ // @ts-ignore
50
+ return context.getImageData(0, 0, image.width, image.height);
51
+ default:
52
+ throw new Error('getImageData');
53
+ }
54
+ }
55
+ exports.getImageData = getImageData;
56
+ // PRIVATE
57
+ // eslint-disable-next-line complexity
58
+ function getImageTypeOrNull(image) {
59
+ if (typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap) {
60
+ return 'imagebitmap';
61
+ }
62
+ if (typeof Image !== 'undefined' && image instanceof Image) {
63
+ return 'image';
64
+ }
65
+ if (image && typeof image === 'object' && image.data && image.width && image.height) {
66
+ return 'data';
67
+ }
68
+ return null;
69
+ }
@@ -9,3 +9,4 @@
9
9
  export declare function encodeImage(image: any, options?: {
10
10
  [key: string]: any;
11
11
  }): Promise<ArrayBuffer>;
12
+ //# sourceMappingURL=encode-image.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encode-image.d.ts","sourceRoot":"","sources":["../../../src/lib/encoders/encode-image.ts"],"names":[],"mappings":"AAMA;;;;;;;GAOG;AACH,wBAAsB,WAAW,CAC/B,KAAK,EAAE,GAAG,EACV,OAAO,CAAC,EAAE;IAAC,CAAC,GAAG,EAAE,MAAM,GAAG,GAAG,CAAA;CAAC,GAC7B,OAAO,CAAC,WAAW,CAAC,CAOtB"}
@@ -0,0 +1,82 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encodeImage = void 0;
4
+ // Image loading/saving for browser and Node.js
5
+ const parsed_image_api_1 = require("../category-api/parsed-image-api");
6
+ // @ts-ignore TS2339: Property does not exist on type
7
+ const { _encodeImageNode } = globalThis;
8
+ /**
9
+ * Returns data bytes representing a compressed image in PNG or JPG format,
10
+ * This data can be saved using file system (f) methods or used in a request.
11
+ * @param image - ImageBitmap Image or Canvas
12
+ * @param options
13
+ * param opt.type='png' - png, jpg or image/png, image/jpg are valid
14
+ * param mimeType= - Whether to include a data URI header
15
+ */
16
+ async function encodeImage(image, options) {
17
+ options = options || {};
18
+ options.image = options.image || {};
19
+ return _encodeImageNode
20
+ ? _encodeImageNode(image, { type: options.image.mimeType })
21
+ : encodeImageInBrowser(image, options);
22
+ }
23
+ exports.encodeImage = encodeImage;
24
+ // In case we get exceptions from canvas.toBlob(resolve, type, quality)
25
+ let qualityParamSupported = true;
26
+ /**
27
+ *
28
+ * @param image
29
+ * @param options
30
+ * @note Based on canvas.toBlob
31
+ * @see https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob
32
+ */
33
+ async function encodeImageInBrowser(image, options) {
34
+ const { mimeType, jpegQuality } = options.image;
35
+ const { width, height } = (0, parsed_image_api_1.getImageSize)(image);
36
+ // create a canvas and resize it to the size of our image
37
+ const canvas = document.createElement('canvas');
38
+ canvas.width = width;
39
+ canvas.height = height;
40
+ drawImageToCanvas(image, canvas);
41
+ // The actual encoding is done asynchronously with `canvas.toBlob()`
42
+ const blob = await new Promise((resolve) => {
43
+ // get it back as a Blob
44
+ if (jpegQuality && qualityParamSupported) {
45
+ try {
46
+ canvas.toBlob(resolve, mimeType, jpegQuality);
47
+ return;
48
+ }
49
+ catch (error) {
50
+ qualityParamSupported = false;
51
+ }
52
+ }
53
+ canvas.toBlob(resolve, mimeType);
54
+ });
55
+ if (!blob) {
56
+ throw new Error('image encoding failed');
57
+ }
58
+ return await blob.arrayBuffer();
59
+ }
60
+ function drawImageToCanvas(image, canvas, x = 0, y = 0) {
61
+ // Try optimized path for ImageBitmaps via bitmaprenderer context
62
+ if (x === 0 && y === 0 && typeof ImageBitmap !== 'undefined' && image instanceof ImageBitmap) {
63
+ const context = canvas.getContext('bitmaprenderer');
64
+ if (context) {
65
+ // transfer the ImageBitmap to it
66
+ context.transferFromImageBitmap(image);
67
+ return canvas;
68
+ }
69
+ }
70
+ // Available on most platforms, except IE11 and Andriod WebViews...
71
+ const context = canvas.getContext('2d');
72
+ if (image.data) {
73
+ // ImageData constructor expects clamped array even though getImageData does not return a clamped array...
74
+ const clampedArray = new Uint8ClampedArray(image.data);
75
+ const imageData = new ImageData(clampedArray, image.width, image.height);
76
+ context.putImageData(imageData, 0, 0);
77
+ return canvas;
78
+ }
79
+ // Fall back to generic image/image bitmap rendering path
80
+ context.drawImage(image, 0, 0);
81
+ return canvas;
82
+ }
@@ -2,3 +2,4 @@ import type { LoaderContext } from '@loaders.gl/loader-utils';
2
2
  import type { ImageType } from '../../types';
3
3
  import type { ImageLoaderOptions } from '../../image-loader';
4
4
  export default function parseImage(arrayBuffer: ArrayBuffer, options?: ImageLoaderOptions, context?: LoaderContext): Promise<ImageType>;
5
+ //# sourceMappingURL=parse-image.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-image.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-image.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,0BAA0B,CAAC;AAE5D,OAAO,KAAK,EAAC,SAAS,EAAC,MAAM,aAAa,CAAC;AAC3C,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,oBAAoB,CAAC;AAS3D,wBAA8B,UAAU,CACtC,WAAW,EAAE,WAAW,EACxB,OAAO,CAAC,EAAE,kBAAkB,EAC5B,OAAO,CAAC,EAAE,aAAa,GACtB,OAAO,CAAC,SAAS,CAAC,CAkCpB"}
@@ -0,0 +1,57 @@
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 loader_utils_1 = require("@loaders.gl/loader-utils");
7
+ const image_type_1 = require("../category-api/image-type");
8
+ const parsed_image_api_1 = require("../category-api/parsed-image-api");
9
+ const parse_to_image_1 = __importDefault(require("./parse-to-image"));
10
+ const parse_to_image_bitmap_1 = __importDefault(require("./parse-to-image-bitmap"));
11
+ const parse_to_node_image_1 = __importDefault(require("./parse-to-node-image"));
12
+ // Parse to platform defined image type (data on node, ImageBitmap or HTMLImage on browser)
13
+ // eslint-disable-next-line complexity
14
+ async function parseImage(arrayBuffer, options, context) {
15
+ options = options || {};
16
+ const imageOptions = options.image || {};
17
+ // The user can request a specific output format via `options.image.type`
18
+ const imageType = imageOptions.type || 'auto';
19
+ const { url } = context || {};
20
+ // Note: For options.image.type === `data`, we may still need to load as `image` or `imagebitmap`
21
+ const loadType = getLoadableImageType(imageType);
22
+ let image;
23
+ switch (loadType) {
24
+ case 'imagebitmap':
25
+ image = await (0, parse_to_image_bitmap_1.default)(arrayBuffer, options, url);
26
+ break;
27
+ case 'image':
28
+ image = await (0, parse_to_image_1.default)(arrayBuffer, options, url);
29
+ break;
30
+ case 'data':
31
+ // Node.js loads imagedata directly
32
+ image = await (0, parse_to_node_image_1.default)(arrayBuffer, options);
33
+ break;
34
+ default:
35
+ (0, loader_utils_1.assert)(false);
36
+ }
37
+ // Browser: if options.image.type === 'data', we can now extract data from the loaded image
38
+ if (imageType === 'data') {
39
+ image = (0, parsed_image_api_1.getImageData)(image);
40
+ }
41
+ return image;
42
+ }
43
+ exports.default = parseImage;
44
+ // Get a loadable image type from image type
45
+ function getLoadableImageType(type) {
46
+ switch (type) {
47
+ case 'auto':
48
+ case 'data':
49
+ // Browser: For image data we need still need to load using an image format
50
+ // Node: the default image type is `data`.
51
+ return (0, image_type_1.getDefaultImageType)();
52
+ default:
53
+ // Throw an error if not supported
54
+ (0, image_type_1.isImageTypeSupported)(type);
55
+ return type;
56
+ }
57
+ }
@@ -7,3 +7,4 @@ import type { ImageLoaderOptions } from '../../image-loader';
7
7
  * TODO - createImageBitmap supports source rect (5 param overload), pass through?
8
8
  */
9
9
  export default function parseToImageBitmap(arrayBuffer: ArrayBuffer, options: ImageLoaderOptions, url?: string): Promise<ImageBitmap>;
10
+ //# sourceMappingURL=parse-to-image-bitmap.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-to-image-bitmap.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-to-image-bitmap.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,oBAAoB,CAAC;AAQ3D;;;;;;GAMG;AACH,wBAA8B,kBAAkB,CAC9C,WAAW,EAAE,WAAW,EACxB,OAAO,EAAE,kBAAkB,EAC3B,GAAG,CAAC,EAAE,MAAM,GACX,OAAO,CAAC,WAAW,CAAC,CAgBtB"}
@@ -0,0 +1,61 @@
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"));
8
+ const EMPTY_OBJECT = {};
9
+ let imagebitmapOptionsSupported = true;
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);
31
+ }
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
+ */
39
+ async function safeCreateImageBitmap(blob, imagebitmapOptions = null) {
40
+ if (isEmptyObject(imagebitmapOptions) || !imagebitmapOptionsSupported) {
41
+ imagebitmapOptions = null;
42
+ }
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);
54
+ }
55
+ function isEmptyObject(object) {
56
+ // @ts-ignore
57
+ for (const key in object || EMPTY_OBJECT) {
58
+ return false;
59
+ }
60
+ return true;
61
+ }
@@ -1,3 +1,4 @@
1
1
  import type { ImageLoaderOptions } from '../../image-loader';
2
2
  export default function parseToImage(arrayBuffer: ArrayBuffer, options: ImageLoaderOptions, url?: string): Promise<HTMLImageElement>;
3
3
  export declare function loadToImage(url: any, options: any): Promise<HTMLImageElement>;
4
+ //# sourceMappingURL=parse-to-image.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-to-image.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-to-image.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,oBAAoB,CAAC;AAI3D,wBAA8B,YAAY,CACxC,WAAW,EAAE,WAAW,EACxB,OAAO,EAAE,kBAAkB,EAC3B,GAAG,CAAC,EAAE,MAAM,GACX,OAAO,CAAC,gBAAgB,CAAC,CAe3B;AAED,wBAAsB,WAAW,CAAC,GAAG,KAAA,EAAE,OAAO,KAAA,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAyBzE"}
@@ -0,0 +1,47 @@
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
+ }
20
+ }
21
+ }
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;
35
+ }
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
+ });
46
+ }
47
+ exports.loadToImage = loadToImage;
@@ -1,3 +1,4 @@
1
1
  import type { ImageLoaderOptions } from '../../image-loader';
2
2
  import type { ImageDataType } from '../../types';
3
3
  export default function parseToNodeImage(arrayBuffer: ArrayBuffer, options: ImageLoaderOptions): Promise<ImageDataType>;
4
+ //# sourceMappingURL=parse-to-node-image.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"parse-to-node-image.d.ts","sourceRoot":"","sources":["../../../src/lib/parsers/parse-to-node-image.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAC,kBAAkB,EAAC,MAAM,oBAAoB,CAAC;AAC3D,OAAO,KAAK,EAAC,aAAa,EAAC,MAAM,aAAa,CAAC;AAkB/C,wBAA8B,gBAAgB,CAC5C,WAAW,EAAE,WAAW,EACxB,OAAO,EAAE,kBAAkB,GAC1B,OAAO,CAAC,aAAa,CAAC,CASxB"}