@midscene/shared 0.7.1 → 0.7.2-beta-20241024094141.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/constants.js +41 -0
- package/dist/{types → browser}/fs.d.ts +2 -2
- package/dist/browser/fs.js +73 -0
- package/dist/{types → browser}/img.d.ts +3 -1
- package/dist/browser/img.js +366 -0
- package/dist/browser/index.d.ts +3 -0
- package/dist/browser/index.js +26 -0
- package/dist/browser/utils.d.ts +4 -0
- package/dist/browser/utils.js +34 -0
- package/dist/es/constants.d.ts +13 -0
- package/dist/es/fs.d.ts +14 -0
- package/dist/es/fs.js +7 -3
- package/dist/es/img.d.ts +131 -0
- package/dist/es/img.js +144 -89
- package/dist/es/index.d.ts +3 -0
- package/dist/es/index.js +1 -3
- package/dist/es/utils.d.ts +4 -0
- package/dist/es/utils.js +13 -0
- package/dist/lib/constants.d.ts +13 -0
- package/dist/lib/fs.d.ts +14 -0
- package/dist/lib/fs.js +8 -4
- package/dist/lib/img.d.ts +131 -0
- package/dist/lib/img.js +142 -87
- package/dist/lib/index.d.ts +3 -0
- package/dist/lib/index.js +1 -3
- package/dist/lib/utils.d.ts +4 -0
- package/dist/lib/utils.js +39 -0
- package/dist/types/index.d.ts +2 -2
- package/package.json +41 -17
- package/src/fs/index.ts +6 -2
- package/src/img/box-select.ts +8 -5
- package/src/img/get-jimp.ts +7 -0
- package/src/img/index.ts +1 -0
- package/src/img/info.ts +11 -4
- package/src/img/jimp.d.ts +4 -0
- package/src/img/transform.ts +6 -1
- package/src/index.ts +1 -3
- package/src/utils.ts +10 -0
- /package/dist/{types → browser}/constants.d.ts +0 -0
package/dist/lib/img.js
CHANGED
|
@@ -26,12 +26,33 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
26
26
|
mod
|
|
27
27
|
));
|
|
28
28
|
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
var __async = (__this, __arguments, generator) => {
|
|
30
|
+
return new Promise((resolve, reject) => {
|
|
31
|
+
var fulfilled = (value) => {
|
|
32
|
+
try {
|
|
33
|
+
step(generator.next(value));
|
|
34
|
+
} catch (e) {
|
|
35
|
+
reject(e);
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
var rejected = (value) => {
|
|
39
|
+
try {
|
|
40
|
+
step(generator.throw(value));
|
|
41
|
+
} catch (e) {
|
|
42
|
+
reject(e);
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
46
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
47
|
+
});
|
|
48
|
+
};
|
|
29
49
|
|
|
30
50
|
// src/img/index.ts
|
|
31
51
|
var img_exports = {};
|
|
32
52
|
__export(img_exports, {
|
|
33
53
|
base64Encoded: () => base64Encoded,
|
|
34
54
|
base64ToPngFormat: () => base64ToPngFormat,
|
|
55
|
+
bufferFromBase64: () => bufferFromBase64,
|
|
35
56
|
calculateNewDimensions: () => calculateNewDimensions,
|
|
36
57
|
compositeElementInfoImg: () => compositeElementInfoImg,
|
|
37
58
|
imageInfo: () => imageInfo,
|
|
@@ -48,26 +69,49 @@ module.exports = __toCommonJS(img_exports);
|
|
|
48
69
|
var import_node_assert = __toESM(require("assert"));
|
|
49
70
|
var import_node_buffer = require("buffer");
|
|
50
71
|
var import_node_fs = require("fs");
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
const { width, height } = jimpImage.bitmap;
|
|
62
|
-
(0, import_node_assert.default)(
|
|
63
|
-
width && height,
|
|
64
|
-
`Invalid image: ${typeof image === "string" ? image : "Buffer"}`
|
|
65
|
-
);
|
|
66
|
-
return { width, height };
|
|
72
|
+
|
|
73
|
+
// src/img/get-jimp.ts
|
|
74
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
75
|
+
function getJimp() {
|
|
76
|
+
return __async(this, null, function* () {
|
|
77
|
+
if (ifInBrowser) {
|
|
78
|
+
return (yield import("jimp/browser/lib/jimp.js")).default;
|
|
79
|
+
}
|
|
80
|
+
return (yield import("jimp")).default;
|
|
81
|
+
});
|
|
67
82
|
}
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
83
|
+
|
|
84
|
+
// src/img/info.ts
|
|
85
|
+
function imageInfo(image) {
|
|
86
|
+
return __async(this, null, function* () {
|
|
87
|
+
const Jimp = yield getJimp();
|
|
88
|
+
let jimpImage;
|
|
89
|
+
if (typeof image === "string") {
|
|
90
|
+
jimpImage = yield Jimp.read(image);
|
|
91
|
+
} else if (import_node_buffer.Buffer.isBuffer(image)) {
|
|
92
|
+
jimpImage = yield Jimp.read(image);
|
|
93
|
+
} else {
|
|
94
|
+
throw new Error("Invalid image input: must be a string path or a Buffer");
|
|
95
|
+
}
|
|
96
|
+
const { width, height } = jimpImage.bitmap;
|
|
97
|
+
(0, import_node_assert.default)(
|
|
98
|
+
width && height,
|
|
99
|
+
`Invalid image: ${typeof image === "string" ? image : "Buffer"}`
|
|
100
|
+
);
|
|
101
|
+
return { width, height };
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
function imageInfoOfBase64(imageBase64) {
|
|
105
|
+
return __async(this, null, function* () {
|
|
106
|
+
const buffer = yield bufferFromBase64(imageBase64);
|
|
107
|
+
return imageInfo(buffer);
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
function bufferFromBase64(imageBase64) {
|
|
111
|
+
return __async(this, null, function* () {
|
|
112
|
+
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, "");
|
|
113
|
+
return import_node_buffer.Buffer.from(base64Data, "base64");
|
|
114
|
+
});
|
|
71
115
|
}
|
|
72
116
|
function base64Encoded(image, withHeader = true) {
|
|
73
117
|
const imageBuffer = (0, import_node_fs.readFileSync)(image);
|
|
@@ -88,31 +132,39 @@ function base64ToPngFormat(base64) {
|
|
|
88
132
|
|
|
89
133
|
// src/img/transform.ts
|
|
90
134
|
var import_node_buffer2 = require("buffer");
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
135
|
+
function saveBase64Image(options) {
|
|
136
|
+
return __async(this, null, function* () {
|
|
137
|
+
const { base64Data, outputPath } = options;
|
|
138
|
+
const base64Image = base64Data.split(";base64,").pop() || base64Data;
|
|
139
|
+
const imageBuffer = import_node_buffer2.Buffer.from(base64Image, "base64");
|
|
140
|
+
const Jimp = yield getJimp();
|
|
141
|
+
const image = yield Jimp.read(imageBuffer);
|
|
142
|
+
yield image.writeAsync(outputPath);
|
|
143
|
+
});
|
|
98
144
|
}
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
145
|
+
function transformImgPathToBase64(inputPath) {
|
|
146
|
+
return __async(this, null, function* () {
|
|
147
|
+
const Jimp = yield getJimp();
|
|
148
|
+
const image = yield Jimp.read(inputPath);
|
|
149
|
+
const buffer = yield image.getBufferAsync(Jimp.MIME_PNG);
|
|
150
|
+
return buffer.toString("base64");
|
|
151
|
+
});
|
|
103
152
|
}
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
153
|
+
function resizeImg(inputData, newSize) {
|
|
154
|
+
return __async(this, null, function* () {
|
|
155
|
+
const isBase64 = typeof inputData === "string";
|
|
156
|
+
const imageBuffer = isBase64 ? import_node_buffer2.Buffer.from(inputData.split(";base64,").pop() || inputData, "base64") : inputData;
|
|
157
|
+
const Jimp = yield getJimp();
|
|
158
|
+
const image = yield Jimp.read(imageBuffer);
|
|
159
|
+
const { width, height } = image.bitmap;
|
|
160
|
+
if (!width || !height) {
|
|
161
|
+
throw Error("Undefined width or height from the input image.");
|
|
162
|
+
}
|
|
163
|
+
const finalNewSize = newSize || calculateNewDimensions(width, height);
|
|
164
|
+
image.resize(finalNewSize.width, finalNewSize.height);
|
|
165
|
+
const resizedBuffer = yield image.getBufferAsync(Jimp.MIME_PNG);
|
|
166
|
+
return isBase64 ? resizedBuffer.toString("base64") : resizedBuffer;
|
|
167
|
+
});
|
|
116
168
|
}
|
|
117
169
|
function calculateNewDimensions(originalWidth, originalHeight) {
|
|
118
170
|
const maxWidth = 2048;
|
|
@@ -133,36 +185,38 @@ function calculateNewDimensions(originalWidth, originalHeight) {
|
|
|
133
185
|
height: Math.round(newHeight)
|
|
134
186
|
};
|
|
135
187
|
}
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
188
|
+
function trimImage(image) {
|
|
189
|
+
return __async(this, null, function* () {
|
|
190
|
+
const Jimp = yield getJimp();
|
|
191
|
+
const jimpImage = yield Jimp.read(
|
|
192
|
+
import_node_buffer2.Buffer.isBuffer(image) ? image : import_node_buffer2.Buffer.from(image)
|
|
193
|
+
);
|
|
194
|
+
const { width, height } = jimpImage.bitmap;
|
|
195
|
+
if (width <= 3 || height <= 3) {
|
|
196
|
+
return null;
|
|
197
|
+
}
|
|
198
|
+
const trimmedImage = jimpImage.autocrop();
|
|
199
|
+
const { width: trimmedWidth, height: trimmedHeight } = trimmedImage.bitmap;
|
|
200
|
+
const trimOffsetLeft = (width - trimmedWidth) / 2;
|
|
201
|
+
const trimOffsetTop = (height - trimmedHeight) / 2;
|
|
202
|
+
if (trimOffsetLeft === 0 && trimOffsetTop === 0) {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
return {
|
|
206
|
+
trimOffsetLeft: -trimOffsetLeft,
|
|
207
|
+
trimOffsetTop: -trimOffsetTop,
|
|
208
|
+
width: trimmedWidth,
|
|
209
|
+
height: trimmedHeight
|
|
210
|
+
};
|
|
211
|
+
});
|
|
157
212
|
}
|
|
158
213
|
|
|
159
214
|
// src/img/box-select.ts
|
|
160
215
|
var import_node_assert2 = __toESM(require("assert"));
|
|
161
|
-
var import_node_buffer3 = require("buffer");
|
|
162
|
-
var import_jimp3 = __toESM(require("jimp"));
|
|
163
216
|
var createSvgOverlay = (elements, imageWidth, imageHeight) => {
|
|
164
|
-
const createPngOverlay =
|
|
165
|
-
const
|
|
217
|
+
const createPngOverlay = (elements2, imageWidth2, imageHeight2) => __async(void 0, null, function* () {
|
|
218
|
+
const Jimp = yield getJimp();
|
|
219
|
+
const image = new Jimp(imageWidth2, imageHeight2, 0);
|
|
166
220
|
const colors = [
|
|
167
221
|
{ rect: 4278190335, text: 4294967295 }
|
|
168
222
|
// red, white
|
|
@@ -243,59 +297,59 @@ var createSvgOverlay = (elements, imageWidth, imageHeight) => {
|
|
|
243
297
|
this.bitmap.data[idx + 2] = color.rect >> 8 & 255;
|
|
244
298
|
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
245
299
|
});
|
|
246
|
-
const font =
|
|
300
|
+
const font = yield Jimp.loadFont(Jimp.FONT_SANS_16_WHITE);
|
|
247
301
|
image.print(
|
|
248
302
|
font,
|
|
249
303
|
rectX,
|
|
250
304
|
rectY,
|
|
251
305
|
{
|
|
252
306
|
text: element.indexId.toString(),
|
|
253
|
-
alignmentX:
|
|
254
|
-
alignmentY:
|
|
307
|
+
alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
|
|
308
|
+
alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE
|
|
255
309
|
},
|
|
256
310
|
rectWidth,
|
|
257
311
|
rectHeight
|
|
258
312
|
);
|
|
259
313
|
}
|
|
260
|
-
return image.getBufferAsync(
|
|
261
|
-
};
|
|
314
|
+
return image.getBufferAsync(Jimp.MIME_PNG);
|
|
315
|
+
});
|
|
262
316
|
return createPngOverlay(elements, imageWidth, imageHeight);
|
|
263
317
|
};
|
|
264
|
-
var compositeElementInfoImg =
|
|
318
|
+
var compositeElementInfoImg = (options) => __async(void 0, null, function* () {
|
|
319
|
+
const Jimp = yield getJimp();
|
|
265
320
|
const { inputImgBase64, elementsPositionInfo } = options;
|
|
266
|
-
const imageBuffer =
|
|
267
|
-
const
|
|
268
|
-
const { width, height } = image.bitmap;
|
|
321
|
+
const imageBuffer = yield bufferFromBase64(inputImgBase64);
|
|
322
|
+
const { width, height } = yield imageInfo(imageBuffer);
|
|
269
323
|
if (!width || !height) {
|
|
270
324
|
throw Error("Image processing failed because width or height is undefined");
|
|
271
325
|
}
|
|
272
|
-
const svgOverlay =
|
|
326
|
+
const svgOverlay = yield createSvgOverlay(
|
|
273
327
|
elementsPositionInfo,
|
|
274
328
|
width,
|
|
275
329
|
height
|
|
276
330
|
);
|
|
277
|
-
return
|
|
278
|
-
const svgImage =
|
|
279
|
-
return
|
|
280
|
-
mode:
|
|
331
|
+
return yield Jimp.read(imageBuffer).then((image) => __async(void 0, null, function* () {
|
|
332
|
+
const svgImage = yield Jimp.read(svgOverlay);
|
|
333
|
+
return image.composite(svgImage, 0, 0, {
|
|
334
|
+
mode: Jimp.BLEND_SOURCE_OVER,
|
|
281
335
|
opacitySource: 1,
|
|
282
336
|
opacityDest: 1
|
|
283
337
|
});
|
|
284
|
-
}).then((compositeImage) => {
|
|
285
|
-
return compositeImage.getBufferAsync(
|
|
338
|
+
})).then((compositeImage) => {
|
|
339
|
+
return compositeImage.getBufferAsync(Jimp.MIME_PNG);
|
|
286
340
|
}).then((buffer) => {
|
|
287
341
|
return buffer.toString("base64");
|
|
288
342
|
}).catch((error) => {
|
|
289
343
|
throw error;
|
|
290
344
|
});
|
|
291
|
-
};
|
|
292
|
-
var processImageElementInfo =
|
|
345
|
+
});
|
|
346
|
+
var processImageElementInfo = (options) => __async(void 0, null, function* () {
|
|
293
347
|
const base64Image = options.inputImgBase64.split(";base64,").pop();
|
|
294
348
|
(0, import_node_assert2.default)(base64Image, "base64Image is undefined");
|
|
295
349
|
const [
|
|
296
350
|
compositeElementInfoImgBase64,
|
|
297
351
|
compositeElementInfoImgWithoutTextBase64
|
|
298
|
-
] =
|
|
352
|
+
] = yield Promise.all([
|
|
299
353
|
compositeElementInfoImg({
|
|
300
354
|
inputImgBase64: options.inputImgBase64,
|
|
301
355
|
elementsPositionInfo: options.elementsPositionInfo
|
|
@@ -309,11 +363,12 @@ var processImageElementInfo = async (options) => {
|
|
|
309
363
|
compositeElementInfoImgBase64,
|
|
310
364
|
compositeElementInfoImgWithoutTextBase64
|
|
311
365
|
};
|
|
312
|
-
};
|
|
366
|
+
});
|
|
313
367
|
// Annotate the CommonJS export names for ESM import in node:
|
|
314
368
|
0 && (module.exports = {
|
|
315
369
|
base64Encoded,
|
|
316
370
|
base64ToPngFormat,
|
|
371
|
+
bufferFromBase64,
|
|
317
372
|
calculateNewDimensions,
|
|
318
373
|
compositeElementInfoImg,
|
|
319
374
|
imageInfo,
|
package/dist/lib/index.js
CHANGED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/utils.ts
|
|
21
|
+
var utils_exports = {};
|
|
22
|
+
__export(utils_exports, {
|
|
23
|
+
ifInBrowser: () => ifInBrowser,
|
|
24
|
+
uuid: () => uuid
|
|
25
|
+
});
|
|
26
|
+
module.exports = __toCommonJS(utils_exports);
|
|
27
|
+
var import_node_crypto = require("crypto");
|
|
28
|
+
var ifInBrowser = typeof window !== "undefined";
|
|
29
|
+
function uuid() {
|
|
30
|
+
if (ifInBrowser) {
|
|
31
|
+
return Math.random().toString(36).substring(2, 15);
|
|
32
|
+
}
|
|
33
|
+
return (0, import_node_crypto.randomUUID)();
|
|
34
|
+
}
|
|
35
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
36
|
+
0 && (module.exports = {
|
|
37
|
+
ifInBrowser,
|
|
38
|
+
uuid
|
|
39
|
+
});
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
declare
|
|
1
|
+
declare const _default: {};
|
|
2
2
|
|
|
3
|
-
export {
|
|
3
|
+
export { _default as default };
|
package/package.json
CHANGED
|
@@ -1,46 +1,70 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@midscene/shared",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.2-beta-20241024094141.0",
|
|
4
4
|
"repository": "https://github.com/web-infra-dev/midscene",
|
|
5
5
|
"homepage": "https://midscenejs.com/",
|
|
6
|
-
"types": "./
|
|
6
|
+
"types": "./src/index.ts",
|
|
7
|
+
"type": "commonjs",
|
|
7
8
|
"main": "./dist/lib/index.js",
|
|
8
9
|
"module": "./dist/es/index.js",
|
|
9
10
|
"exports": {
|
|
10
11
|
".": {
|
|
11
|
-
"types": "./dist/
|
|
12
|
+
"types": "./dist/lib/index.d.ts",
|
|
13
|
+
"require": "./dist/lib/index.js",
|
|
12
14
|
"import": "./dist/es/index.js",
|
|
13
|
-
"
|
|
15
|
+
"browser": "./dist/browser/index.js"
|
|
14
16
|
},
|
|
15
17
|
"./constants": {
|
|
16
|
-
"types": "./dist/
|
|
18
|
+
"types": "./dist/lib/constants.d.ts",
|
|
19
|
+
"require": "./dist/lib/constants.js",
|
|
17
20
|
"import": "./dist/es/constants.js",
|
|
18
|
-
"
|
|
21
|
+
"browser": "./dist/browser/constants.js"
|
|
22
|
+
},
|
|
23
|
+
"./fs": {
|
|
24
|
+
"types": "./dist/lib/fs.d.ts",
|
|
25
|
+
"require": "./dist/lib/fs.js",
|
|
26
|
+
"import": "./dist/es/fs.js",
|
|
27
|
+
"browser": "./dist/browser/fs.js"
|
|
19
28
|
},
|
|
20
29
|
"./img": {
|
|
21
|
-
"types": "./dist/
|
|
30
|
+
"types": "./dist/lib/img.d.ts",
|
|
31
|
+
"require": "./dist/lib/img.js",
|
|
22
32
|
"import": "./dist/es/img.js",
|
|
23
|
-
"
|
|
33
|
+
"browser": "./dist/browser/img.js"
|
|
24
34
|
},
|
|
25
|
-
"./
|
|
26
|
-
"types": "./dist/
|
|
27
|
-
"
|
|
28
|
-
"
|
|
29
|
-
|
|
35
|
+
"./utils": {
|
|
36
|
+
"types": "./dist/lib/utils.d.ts",
|
|
37
|
+
"require": "./dist/lib/utils.js",
|
|
38
|
+
"import": "./dist/es/utils.js",
|
|
39
|
+
"browser": "./dist/browser/utils.js"
|
|
40
|
+
},
|
|
41
|
+
"./browser": "./dist/browser/index.js",
|
|
42
|
+
"./browser/constants": "./dist/browser/constants.js",
|
|
43
|
+
"./browser/fs": "./dist/browser/fs.js",
|
|
44
|
+
"./browser/img": "./dist/browser/img.js",
|
|
45
|
+
"./browser/utils": "./dist/browser/utils.js"
|
|
30
46
|
},
|
|
31
47
|
"typesVersions": {
|
|
32
48
|
"*": {
|
|
33
49
|
".": [
|
|
34
|
-
"./
|
|
50
|
+
"./src/index.ts"
|
|
35
51
|
],
|
|
36
52
|
"constants": [
|
|
37
|
-
"./
|
|
53
|
+
"./src/constants/index.ts"
|
|
38
54
|
],
|
|
39
55
|
"img": [
|
|
40
|
-
"./
|
|
56
|
+
"./src/img/index.ts"
|
|
41
57
|
],
|
|
42
58
|
"fs": [
|
|
43
|
-
"./
|
|
59
|
+
"./src/fs/index.ts"
|
|
60
|
+
],
|
|
61
|
+
"utils": [
|
|
62
|
+
"./src/utils.ts"
|
|
63
|
+
]
|
|
64
|
+
},
|
|
65
|
+
"./browser": {
|
|
66
|
+
"*": [
|
|
67
|
+
"./dist/browser/*"
|
|
44
68
|
]
|
|
45
69
|
}
|
|
46
70
|
},
|
package/src/fs/index.ts
CHANGED
|
@@ -9,7 +9,11 @@ interface PkgInfo {
|
|
|
9
9
|
}
|
|
10
10
|
|
|
11
11
|
let pkg: PkgInfo | undefined;
|
|
12
|
-
|
|
12
|
+
const ifInBrowser = typeof window !== 'undefined';
|
|
13
|
+
export function getRunningPkgInfo(dir?: string): PkgInfo | null {
|
|
14
|
+
if (ifInBrowser) {
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
13
17
|
if (pkg) {
|
|
14
18
|
return pkg;
|
|
15
19
|
}
|
|
@@ -24,7 +28,7 @@ export function getMidscenePkgInfo(dir: string): PkgInfo {
|
|
|
24
28
|
return pkg;
|
|
25
29
|
}
|
|
26
30
|
return {
|
|
27
|
-
name: 'midscene-unknown-
|
|
31
|
+
name: 'midscene-unknown-package-name',
|
|
28
32
|
version: '0.0.0',
|
|
29
33
|
dir: pkgDir,
|
|
30
34
|
};
|
package/src/img/box-select.ts
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
|
-
import { Buffer } from 'node:buffer';
|
|
2
|
+
import type { Buffer } from 'node:buffer';
|
|
3
3
|
import type { Rect } from '@/types';
|
|
4
|
-
import Jimp from 'jimp';
|
|
4
|
+
import type Jimp from 'jimp';
|
|
5
5
|
import type { NodeType } from '../constants';
|
|
6
|
+
import getJimp from './get-jimp';
|
|
7
|
+
import { bufferFromBase64, imageInfo } from './index';
|
|
6
8
|
|
|
7
9
|
// Define picture path
|
|
8
10
|
type ElementType = {
|
|
@@ -32,6 +34,7 @@ const createSvgOverlay = (
|
|
|
32
34
|
imageWidth: number,
|
|
33
35
|
imageHeight: number,
|
|
34
36
|
) => {
|
|
37
|
+
const Jimp = await getJimp();
|
|
35
38
|
const image = new Jimp(imageWidth, imageHeight, 0x00000000);
|
|
36
39
|
|
|
37
40
|
// Define color array
|
|
@@ -192,10 +195,10 @@ export const compositeElementInfoImg = async (options: {
|
|
|
192
195
|
inputImgBase64: string;
|
|
193
196
|
elementsPositionInfo: Array<ElementType>;
|
|
194
197
|
}) => {
|
|
198
|
+
const Jimp = await getJimp();
|
|
195
199
|
const { inputImgBase64, elementsPositionInfo } = options;
|
|
196
|
-
const imageBuffer =
|
|
197
|
-
const
|
|
198
|
-
const { width, height } = image.bitmap;
|
|
200
|
+
const imageBuffer = await bufferFromBase64(inputImgBase64);
|
|
201
|
+
const { width, height } = await imageInfo(imageBuffer);
|
|
199
202
|
|
|
200
203
|
if (!width || !height) {
|
|
201
204
|
throw Error('Image processing failed because width or height is undefined');
|
package/src/img/index.ts
CHANGED
package/src/img/info.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import assert from 'node:assert';
|
|
2
2
|
import { Buffer } from 'node:buffer';
|
|
3
3
|
import { readFileSync } from 'node:fs';
|
|
4
|
-
import
|
|
4
|
+
import getJimp from './get-jimp';
|
|
5
5
|
|
|
6
6
|
export interface Size {
|
|
7
7
|
width: number;
|
|
@@ -16,7 +16,8 @@ export interface Size {
|
|
|
16
16
|
* @throws Error if the image data is invalid
|
|
17
17
|
*/
|
|
18
18
|
export async function imageInfo(image: string | Buffer): Promise<Size> {
|
|
19
|
-
|
|
19
|
+
const Jimp = await getJimp();
|
|
20
|
+
let jimpImage;
|
|
20
21
|
if (typeof image === 'string') {
|
|
21
22
|
jimpImage = await Jimp.read(image);
|
|
22
23
|
} else if (Buffer.isBuffer(image)) {
|
|
@@ -40,9 +41,15 @@ export async function imageInfo(image: string | Buffer): Promise<Size> {
|
|
|
40
41
|
* @throws Error if the image data is invalid
|
|
41
42
|
*/
|
|
42
43
|
export async function imageInfoOfBase64(imageBase64: string): Promise<Size> {
|
|
43
|
-
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, '');
|
|
44
|
+
// const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, '');
|
|
44
45
|
// Call the imageInfo function to get the dimensions of the image
|
|
45
|
-
|
|
46
|
+
const buffer = await bufferFromBase64(imageBase64);
|
|
47
|
+
return imageInfo(buffer);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function bufferFromBase64(imageBase64: string): Promise<Buffer> {
|
|
51
|
+
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, '');
|
|
52
|
+
return Buffer.from(base64Data, 'base64');
|
|
46
53
|
}
|
|
47
54
|
|
|
48
55
|
/**
|
package/src/img/transform.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Buffer } from 'node:buffer';
|
|
2
|
-
import
|
|
2
|
+
import getJimp from './get-jimp';
|
|
3
3
|
|
|
4
|
+
/**
|
|
4
5
|
/**
|
|
5
6
|
* Saves a Base64-encoded image to a file
|
|
6
7
|
*
|
|
@@ -21,6 +22,7 @@ export async function saveBase64Image(options: {
|
|
|
21
22
|
const imageBuffer = Buffer.from(base64Image, 'base64');
|
|
22
23
|
|
|
23
24
|
// Use Jimp to process the image and save it to the specified location
|
|
25
|
+
const Jimp = await getJimp();
|
|
24
26
|
const image = await Jimp.read(imageBuffer);
|
|
25
27
|
await image.writeAsync(outputPath);
|
|
26
28
|
}
|
|
@@ -32,6 +34,7 @@ export async function saveBase64Image(options: {
|
|
|
32
34
|
*/
|
|
33
35
|
export async function transformImgPathToBase64(inputPath: string) {
|
|
34
36
|
// Use Jimp to process images and generate base64 data
|
|
37
|
+
const Jimp = await getJimp();
|
|
35
38
|
const image = await Jimp.read(inputPath);
|
|
36
39
|
const buffer = await image.getBufferAsync(Jimp.MIME_PNG);
|
|
37
40
|
return buffer.toString('base64');
|
|
@@ -56,6 +59,7 @@ export async function resizeImg(
|
|
|
56
59
|
? Buffer.from(inputData.split(';base64,').pop() || inputData, 'base64')
|
|
57
60
|
: inputData;
|
|
58
61
|
|
|
62
|
+
const Jimp = await getJimp();
|
|
59
63
|
const image = await Jimp.read(imageBuffer);
|
|
60
64
|
const { width, height } = image.bitmap;
|
|
61
65
|
|
|
@@ -127,6 +131,7 @@ export async function trimImage(image: string | Buffer): Promise<{
|
|
|
127
131
|
width: number;
|
|
128
132
|
height: number;
|
|
129
133
|
} | null> {
|
|
134
|
+
const Jimp = await getJimp();
|
|
130
135
|
const jimpImage = await Jimp.read(
|
|
131
136
|
Buffer.isBuffer(image) ? image : Buffer.from(image),
|
|
132
137
|
);
|
package/src/index.ts
CHANGED
package/src/utils.ts
ADDED
|
File without changes
|