image-js 0.33.1 → 0.34.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.d.ts +23 -3
- package/lib/image/core/load.js +16 -11
- package/package.json +1 -1
- package/src/browser/worker/worker.js +2 -0
- package/src/image/core/export.js +2 -0
- package/src/image/core/load.js +31 -11
- package/src/image/roi/creator/fromMask.js +1 -0
- package/src/image/transform/mask/minimum.js +3 -0
package/index.d.ts
CHANGED
|
@@ -32,7 +32,7 @@ export declare class Image {
|
|
|
32
32
|
static createFrom(other: Image, options: ImageConstructorOptions): Image;
|
|
33
33
|
static load(
|
|
34
34
|
image: string | ArrayBuffer | Uint8Array,
|
|
35
|
-
options?: RequestInit,
|
|
35
|
+
options?: RequestInit & { ignorePalette: boolean },
|
|
36
36
|
): Promise<Image>;
|
|
37
37
|
|
|
38
38
|
getRoiManager(): RoiManager;
|
|
@@ -145,11 +145,31 @@ export declare class Image {
|
|
|
145
145
|
// extract
|
|
146
146
|
// floodFill
|
|
147
147
|
// paintLabels
|
|
148
|
-
|
|
148
|
+
paintMasks(
|
|
149
|
+
masks: Image | Array<Image>,
|
|
150
|
+
options?: {
|
|
151
|
+
color?: Array<number> | string;
|
|
152
|
+
colors?: Array<Array<number>> | Array<string>;
|
|
153
|
+
randomColors?: boolean;
|
|
154
|
+
distinctColors?: boolean;
|
|
155
|
+
alpha?: number;
|
|
156
|
+
labels?: Array<string>;
|
|
157
|
+
labelsPosition?: Array<Array<number>>;
|
|
158
|
+
labelColor?: string;
|
|
159
|
+
labelFont?: string;
|
|
160
|
+
},
|
|
161
|
+
): Image;
|
|
149
162
|
// paintPoints
|
|
150
163
|
// paintPolyline
|
|
151
164
|
// paintPolylines
|
|
152
|
-
|
|
165
|
+
paintPolygon(
|
|
166
|
+
points: Array<Array<number>>,
|
|
167
|
+
options?: {
|
|
168
|
+
color?: Array<number>;
|
|
169
|
+
filled?: boolean;
|
|
170
|
+
},
|
|
171
|
+
): Image;
|
|
172
|
+
|
|
153
173
|
// paintPolygons
|
|
154
174
|
|
|
155
175
|
// countAlphaPixels
|
package/lib/image/core/load.js
CHANGED
|
@@ -34,7 +34,10 @@ const isDataURL = /^data:[a-z]+\/(?:[a-z]+);base64,/;
|
|
|
34
34
|
* @static
|
|
35
35
|
* @param {string|ArrayBuffer|Buffer|Uint8Array} image - URL of the image (browser, can be a dataURL) or path (Node.js)
|
|
36
36
|
* or buffer containing the binary data
|
|
37
|
-
* @param {object} [options] - In the browser, the options object is passed to the underlying `fetch` call
|
|
37
|
+
* @param {object} [options] - In the browser, the options object is passed to the underlying `fetch` call, along with
|
|
38
|
+
* the data URL. For binary data, the option specify decoding options.
|
|
39
|
+
* @param {boolean} [options.ignorePalette] - When set to true and loading a tiff from binary data, if the tiff is of
|
|
40
|
+
* type 3 (palette), load as single channel greyscale rather than as a pseudo-colored RGB.
|
|
38
41
|
* @return {Promise<Image>}
|
|
39
42
|
* @example
|
|
40
43
|
* const image = await Image.load('https://example.com/image.png');
|
|
@@ -44,15 +47,15 @@ function load(image, options) {
|
|
|
44
47
|
if (typeof image === 'string') {
|
|
45
48
|
return loadURL(image, options);
|
|
46
49
|
} else if (image instanceof ArrayBuffer) {
|
|
47
|
-
return Promise.resolve(loadBinary(new Uint8Array(image)));
|
|
50
|
+
return Promise.resolve(loadBinary(new Uint8Array(image), undefined, options && options.ignorePalette));
|
|
48
51
|
} else if (image.buffer) {
|
|
49
|
-
return Promise.resolve(loadBinary(image));
|
|
52
|
+
return Promise.resolve(loadBinary(image, undefined, options && options.ignorePalette));
|
|
50
53
|
} else {
|
|
51
54
|
throw new Error('argument to "load" must be a string or buffer.');
|
|
52
55
|
}
|
|
53
56
|
}
|
|
54
57
|
|
|
55
|
-
function loadBinary(image, base64Url) {
|
|
58
|
+
function loadBinary(image, base64Url, ignorePalette) {
|
|
56
59
|
const type = (0, _imageType.default)(image);
|
|
57
60
|
|
|
58
61
|
if (type) {
|
|
@@ -64,7 +67,7 @@ function loadBinary(image, base64Url) {
|
|
|
64
67
|
return loadJPEG(image);
|
|
65
68
|
|
|
66
69
|
case 'image/tiff':
|
|
67
|
-
return loadTIFF(image);
|
|
70
|
+
return loadTIFF(image, ignorePalette);
|
|
68
71
|
|
|
69
72
|
default:
|
|
70
73
|
return loadGeneric(getBase64(type.mime));
|
|
@@ -94,7 +97,7 @@ function loadURL(url, options) {
|
|
|
94
97
|
|
|
95
98
|
return binaryDataP.then(binaryData => {
|
|
96
99
|
const uint8 = new Uint8Array(binaryData);
|
|
97
|
-
return loadBinary(uint8, dataURL ? url : undefined);
|
|
100
|
+
return loadBinary(uint8, dataURL ? url : undefined, options && options.ignorePalette);
|
|
98
101
|
});
|
|
99
102
|
}
|
|
100
103
|
|
|
@@ -195,13 +198,15 @@ function loadJPEG(data) {
|
|
|
195
198
|
return image;
|
|
196
199
|
}
|
|
197
200
|
|
|
198
|
-
function loadTIFF(data) {
|
|
201
|
+
function loadTIFF(data, ignorePalette) {
|
|
199
202
|
let result = (0, _tiff.decode)(data);
|
|
200
203
|
|
|
201
204
|
if (result.length === 1) {
|
|
202
|
-
return getImageFromIFD(result[0]);
|
|
205
|
+
return getImageFromIFD(result[0], ignorePalette);
|
|
203
206
|
} else {
|
|
204
|
-
return new _Stack.default(result.map(
|
|
207
|
+
return new _Stack.default(result.map(function (image) {
|
|
208
|
+
return getImageFromIFD(image, ignorePalette);
|
|
209
|
+
}));
|
|
205
210
|
}
|
|
206
211
|
}
|
|
207
212
|
|
|
@@ -224,8 +229,8 @@ function getMetadata(image) {
|
|
|
224
229
|
return metadata;
|
|
225
230
|
}
|
|
226
231
|
|
|
227
|
-
function getImageFromIFD(image) {
|
|
228
|
-
if (image.type === 3) {
|
|
232
|
+
function getImageFromIFD(image, ignorePalette) {
|
|
233
|
+
if (!ignorePalette && image.type === 3) {
|
|
229
234
|
// Palette
|
|
230
235
|
const data = new Uint16Array(3 * image.width * image.height);
|
|
231
236
|
const palette = image.palette;
|
package/package.json
CHANGED
|
@@ -26,6 +26,7 @@ class Worker {
|
|
|
26
26
|
let manager;
|
|
27
27
|
let url;
|
|
28
28
|
let runner = {};
|
|
29
|
+
|
|
29
30
|
function run(...args) {
|
|
30
31
|
if (!manager) {
|
|
31
32
|
this.checkUrl();
|
|
@@ -35,6 +36,7 @@ class Worker {
|
|
|
35
36
|
}
|
|
36
37
|
return method.run.call(runner, ...args);
|
|
37
38
|
}
|
|
39
|
+
|
|
38
40
|
run.reset = function () {
|
|
39
41
|
if (manager) {
|
|
40
42
|
manager.terminate();
|
package/src/image/core/export.js
CHANGED
|
@@ -125,10 +125,12 @@ const exportMethods = {
|
|
|
125
125
|
}
|
|
126
126
|
const { useCanvas = false, encoder: encoderOptions = undefined } = options;
|
|
127
127
|
type = getType(type);
|
|
128
|
+
|
|
128
129
|
function dataUrl(encoder, ctx) {
|
|
129
130
|
const u8 = encoder(ctx, encoderOptions);
|
|
130
131
|
return toBase64URL(u8, type);
|
|
131
132
|
}
|
|
133
|
+
|
|
132
134
|
if (type === 'image/bmp') {
|
|
133
135
|
return dataUrl(encodeBmp, this);
|
|
134
136
|
} else if (type === 'image/png' && !useCanvas) {
|
package/src/image/core/load.js
CHANGED
|
@@ -19,7 +19,10 @@ const isDataURL = /^data:[a-z]+\/(?:[a-z]+);base64,/;
|
|
|
19
19
|
* @static
|
|
20
20
|
* @param {string|ArrayBuffer|Buffer|Uint8Array} image - URL of the image (browser, can be a dataURL) or path (Node.js)
|
|
21
21
|
* or buffer containing the binary data
|
|
22
|
-
* @param {object} [options] - In the browser, the options object is passed to the underlying `fetch` call
|
|
22
|
+
* @param {object} [options] - In the browser, the options object is passed to the underlying `fetch` call, along with
|
|
23
|
+
* the data URL. For binary data, the option specify decoding options.
|
|
24
|
+
* @param {boolean} [options.ignorePalette] - When set to true and loading a tiff from binary data, if the tiff is of
|
|
25
|
+
* type 3 (palette), load as single channel greyscale rather than as a pseudo-colored RGB.
|
|
23
26
|
* @return {Promise<Image>}
|
|
24
27
|
* @example
|
|
25
28
|
* const image = await Image.load('https://example.com/image.png');
|
|
@@ -28,15 +31,23 @@ export default function load(image, options) {
|
|
|
28
31
|
if (typeof image === 'string') {
|
|
29
32
|
return loadURL(image, options);
|
|
30
33
|
} else if (image instanceof ArrayBuffer) {
|
|
31
|
-
return Promise.resolve(
|
|
34
|
+
return Promise.resolve(
|
|
35
|
+
loadBinary(
|
|
36
|
+
new Uint8Array(image),
|
|
37
|
+
undefined,
|
|
38
|
+
options && options.ignorePalette,
|
|
39
|
+
),
|
|
40
|
+
);
|
|
32
41
|
} else if (image.buffer) {
|
|
33
|
-
return Promise.resolve(
|
|
42
|
+
return Promise.resolve(
|
|
43
|
+
loadBinary(image, undefined, options && options.ignorePalette),
|
|
44
|
+
);
|
|
34
45
|
} else {
|
|
35
46
|
throw new Error('argument to "load" must be a string or buffer.');
|
|
36
47
|
}
|
|
37
48
|
}
|
|
38
49
|
|
|
39
|
-
function loadBinary(image, base64Url) {
|
|
50
|
+
function loadBinary(image, base64Url, ignorePalette) {
|
|
40
51
|
const type = imageType(image);
|
|
41
52
|
if (type) {
|
|
42
53
|
switch (type.mime) {
|
|
@@ -45,12 +56,13 @@ function loadBinary(image, base64Url) {
|
|
|
45
56
|
case 'image/jpeg':
|
|
46
57
|
return loadJPEG(image);
|
|
47
58
|
case 'image/tiff':
|
|
48
|
-
return loadTIFF(image);
|
|
59
|
+
return loadTIFF(image, ignorePalette);
|
|
49
60
|
default:
|
|
50
61
|
return loadGeneric(getBase64(type.mime));
|
|
51
62
|
}
|
|
52
63
|
}
|
|
53
64
|
return loadGeneric(getBase64('application/octet-stream'));
|
|
65
|
+
|
|
54
66
|
function getBase64(type) {
|
|
55
67
|
if (base64Url) {
|
|
56
68
|
return base64Url;
|
|
@@ -70,7 +82,11 @@ function loadURL(url, options) {
|
|
|
70
82
|
}
|
|
71
83
|
return binaryDataP.then((binaryData) => {
|
|
72
84
|
const uint8 = new Uint8Array(binaryData);
|
|
73
|
-
return loadBinary(
|
|
85
|
+
return loadBinary(
|
|
86
|
+
uint8,
|
|
87
|
+
dataURL ? url : undefined,
|
|
88
|
+
options && options.ignorePalette,
|
|
89
|
+
);
|
|
74
90
|
});
|
|
75
91
|
}
|
|
76
92
|
|
|
@@ -159,12 +175,16 @@ function loadJPEG(data) {
|
|
|
159
175
|
return image;
|
|
160
176
|
}
|
|
161
177
|
|
|
162
|
-
function loadTIFF(data) {
|
|
178
|
+
function loadTIFF(data, ignorePalette) {
|
|
163
179
|
let result = decodeTiff(data);
|
|
164
180
|
if (result.length === 1) {
|
|
165
|
-
return getImageFromIFD(result[0]);
|
|
181
|
+
return getImageFromIFD(result[0], ignorePalette);
|
|
166
182
|
} else {
|
|
167
|
-
return new Stack(
|
|
183
|
+
return new Stack(
|
|
184
|
+
result.map(function (image) {
|
|
185
|
+
return getImageFromIFD(image, ignorePalette);
|
|
186
|
+
}),
|
|
187
|
+
);
|
|
168
188
|
}
|
|
169
189
|
}
|
|
170
190
|
|
|
@@ -184,8 +204,8 @@ function getMetadata(image) {
|
|
|
184
204
|
return metadata;
|
|
185
205
|
}
|
|
186
206
|
|
|
187
|
-
function getImageFromIFD(image) {
|
|
188
|
-
if (image.type === 3) {
|
|
207
|
+
function getImageFromIFD(image, ignorePalette) {
|
|
208
|
+
if (!ignorePalette && image.type === 3) {
|
|
189
209
|
// Palette
|
|
190
210
|
const data = new Uint16Array(3 * image.width * image.height);
|
|
191
211
|
const palette = image.palette;
|
|
@@ -31,6 +31,7 @@ export default function minimum(histogram) {
|
|
|
31
31
|
threshold = minimumBetweenPeeks(histogramCopy, max);
|
|
32
32
|
return threshold;
|
|
33
33
|
}
|
|
34
|
+
|
|
34
35
|
function smoothed(histogram) {
|
|
35
36
|
// Smooth with a 3 point running mean filter
|
|
36
37
|
let auHistogram = new Array(histogram.length); // a copy of the histograma for the smoothing process
|
|
@@ -42,6 +43,7 @@ function smoothed(histogram) {
|
|
|
42
43
|
(histogram[histogram.length - 2] + histogram[histogram.length - 1]) / 3;
|
|
43
44
|
return auHistogram;
|
|
44
45
|
}
|
|
46
|
+
|
|
45
47
|
function minimumBetweenPeeks(histogramBimodal, max) {
|
|
46
48
|
let threshold;
|
|
47
49
|
for (let i = 1; i < max; i++) {
|
|
@@ -55,6 +57,7 @@ function minimumBetweenPeeks(histogramBimodal, max) {
|
|
|
55
57
|
}
|
|
56
58
|
return threshold;
|
|
57
59
|
}
|
|
60
|
+
|
|
58
61
|
function bimodalTest(histogram) {
|
|
59
62
|
// It is responsible for determining if a histogram is bimodal
|
|
60
63
|
let len = histogram.length;
|