@midscene/shared 0.4.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/LICENSE +21 -0
- package/README.md +7 -0
- package/dist/es/constants.js +15 -0
- package/dist/es/img.js +239 -0
- package/dist/es/index.js +7 -0
- package/dist/lib/constants.js +42 -0
- package/dist/lib/img.js +284 -0
- package/dist/lib/index.js +28 -0
- package/dist/types/constants.d.ts +10 -0
- package/dist/types/img.d.ts +111 -0
- package/dist/types/index.d.ts +3 -0
- package/package.json +74 -0
- package/src/constants/index.ts +10 -0
- package/src/img/box-select.ts +179 -0
- package/src/img/index.ts +9 -0
- package/src/img/info.ts +71 -0
- package/src/img/transform.ts +240 -0
- package/src/index.ts +3 -0
- package/src/modern-app-env.d.ts +1 -0
- package/src/types/index.ts +11 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024-present Bytedance, Inc. and its affiliates.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
// src/constants/index.ts
|
|
2
|
+
var TEXT_SIZE_THRESHOLD = 9;
|
|
3
|
+
var TEXT_MAX_SIZE = 40;
|
|
4
|
+
var NodeType = /* @__PURE__ */ ((NodeType2) => {
|
|
5
|
+
NodeType2["FORM_ITEM"] = "FORM_ITEM Node";
|
|
6
|
+
NodeType2["BUTTON"] = "BUTTON Node";
|
|
7
|
+
NodeType2["IMG"] = "IMG Node";
|
|
8
|
+
NodeType2["TEXT"] = "TEXT Node";
|
|
9
|
+
return NodeType2;
|
|
10
|
+
})(NodeType || {});
|
|
11
|
+
export {
|
|
12
|
+
NodeType,
|
|
13
|
+
TEXT_MAX_SIZE,
|
|
14
|
+
TEXT_SIZE_THRESHOLD
|
|
15
|
+
};
|
package/dist/es/img.js
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
// src/img/info.ts
|
|
2
|
+
import assert from "assert";
|
|
3
|
+
import { Buffer } from "buffer";
|
|
4
|
+
import { readFileSync } from "fs";
|
|
5
|
+
import Jimp from "jimp";
|
|
6
|
+
async function imageInfo(image) {
|
|
7
|
+
let jimpImage;
|
|
8
|
+
if (typeof image === "string") {
|
|
9
|
+
jimpImage = await Jimp.read(image);
|
|
10
|
+
} else if (Buffer.isBuffer(image)) {
|
|
11
|
+
jimpImage = await Jimp.read(image);
|
|
12
|
+
} else {
|
|
13
|
+
throw new Error("Invalid image input: must be a string path or a Buffer");
|
|
14
|
+
}
|
|
15
|
+
const { width, height } = jimpImage.bitmap;
|
|
16
|
+
assert(
|
|
17
|
+
width && height,
|
|
18
|
+
`Invalid image: ${typeof image === "string" ? image : "Buffer"}`
|
|
19
|
+
);
|
|
20
|
+
return { width, height };
|
|
21
|
+
}
|
|
22
|
+
async function imageInfoOfBase64(imageBase64) {
|
|
23
|
+
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, "");
|
|
24
|
+
return imageInfo(Buffer.from(base64Data, "base64"));
|
|
25
|
+
}
|
|
26
|
+
function base64Encoded(image, withHeader = true) {
|
|
27
|
+
const imageBuffer = readFileSync(image);
|
|
28
|
+
if (!withHeader) {
|
|
29
|
+
return imageBuffer.toString("base64");
|
|
30
|
+
}
|
|
31
|
+
if (image.endsWith("png")) {
|
|
32
|
+
return `data:image/png;base64,${imageBuffer.toString("base64")}`;
|
|
33
|
+
}
|
|
34
|
+
if (image.endsWith("jpg") || image.endsWith("jpeg")) {
|
|
35
|
+
return `data:image/jpeg;base64,${imageBuffer.toString("base64")}`;
|
|
36
|
+
}
|
|
37
|
+
throw new Error("unsupported image type");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// src/img/transform.ts
|
|
41
|
+
import { Buffer as Buffer2 } from "buffer";
|
|
42
|
+
import Jimp2 from "jimp";
|
|
43
|
+
async function saveBase64Image(options) {
|
|
44
|
+
const { base64Data, outputPath } = options;
|
|
45
|
+
const base64Image = base64Data.split(";base64,").pop() || base64Data;
|
|
46
|
+
const imageBuffer = Buffer2.from(base64Image, "base64");
|
|
47
|
+
const image = await Jimp2.read(imageBuffer);
|
|
48
|
+
await image.writeAsync(outputPath);
|
|
49
|
+
console.log("Image successfully written to file.");
|
|
50
|
+
}
|
|
51
|
+
async function transformImgPathToBase64(inputPath) {
|
|
52
|
+
const image = await Jimp2.read(inputPath);
|
|
53
|
+
const buffer = await image.getBufferAsync(Jimp2.MIME_PNG);
|
|
54
|
+
return buffer.toString("base64");
|
|
55
|
+
}
|
|
56
|
+
async function resizeImg(base64Data) {
|
|
57
|
+
const base64Image = base64Data.split(";base64,").pop() || base64Data;
|
|
58
|
+
const imageBuffer = Buffer2.from(base64Image, "base64");
|
|
59
|
+
const image = await Jimp2.read(imageBuffer);
|
|
60
|
+
const { width, height } = image.bitmap;
|
|
61
|
+
if (!width || !height) {
|
|
62
|
+
throw Error("undefined width or height with url");
|
|
63
|
+
}
|
|
64
|
+
const newSize = calculateNewDimensions(width, height);
|
|
65
|
+
image.resize(newSize.width, newSize.height);
|
|
66
|
+
const buffer = await image.getBufferAsync(Jimp2.MIME_PNG);
|
|
67
|
+
return buffer.toString("base64");
|
|
68
|
+
}
|
|
69
|
+
function calculateNewDimensions(originalWidth, originalHeight) {
|
|
70
|
+
const maxWidth = 768;
|
|
71
|
+
const maxHeight = 2048;
|
|
72
|
+
let newWidth = originalWidth;
|
|
73
|
+
let newHeight = originalHeight;
|
|
74
|
+
const aspectRatio = originalWidth / originalHeight;
|
|
75
|
+
if (originalWidth > maxWidth) {
|
|
76
|
+
newWidth = maxWidth;
|
|
77
|
+
newHeight = newWidth / aspectRatio;
|
|
78
|
+
}
|
|
79
|
+
if (newHeight > maxHeight) {
|
|
80
|
+
newHeight = maxHeight;
|
|
81
|
+
newWidth = newHeight * aspectRatio;
|
|
82
|
+
}
|
|
83
|
+
return {
|
|
84
|
+
width: Math.round(newWidth),
|
|
85
|
+
height: Math.round(newHeight)
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
async function trimImage(image) {
|
|
89
|
+
const jimpImage = await Jimp2.read(
|
|
90
|
+
Buffer2.isBuffer(image) ? image : Buffer2.from(image)
|
|
91
|
+
);
|
|
92
|
+
const { width, height } = jimpImage.bitmap;
|
|
93
|
+
if (width <= 3 || height <= 3) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
const trimmedImage = jimpImage.autocrop();
|
|
97
|
+
const { width: trimmedWidth, height: trimmedHeight } = trimmedImage.bitmap;
|
|
98
|
+
const trimOffsetLeft = (width - trimmedWidth) / 2;
|
|
99
|
+
const trimOffsetTop = (height - trimmedHeight) / 2;
|
|
100
|
+
if (trimOffsetLeft === 0 && trimOffsetTop === 0) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
return {
|
|
104
|
+
trimOffsetLeft: -trimOffsetLeft,
|
|
105
|
+
trimOffsetTop: -trimOffsetTop,
|
|
106
|
+
width: trimmedWidth,
|
|
107
|
+
height: trimmedHeight
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// src/img/box-select.ts
|
|
112
|
+
import assert2 from "assert";
|
|
113
|
+
import { Buffer as Buffer3 } from "buffer";
|
|
114
|
+
import Jimp3 from "jimp";
|
|
115
|
+
var createSvgOverlay = (elements, imageWidth, imageHeight) => {
|
|
116
|
+
const createPngOverlay = async (elements2, imageWidth2, imageHeight2) => {
|
|
117
|
+
const image = new Jimp3(imageWidth2, imageHeight2, 0);
|
|
118
|
+
const colors = [
|
|
119
|
+
{ rect: 65535, text: 4294967295 },
|
|
120
|
+
// blue, white
|
|
121
|
+
{ rect: 2336560127, text: 4294967295 }
|
|
122
|
+
// brown, white
|
|
123
|
+
];
|
|
124
|
+
for (let index = 0; index < elements2.length; index++) {
|
|
125
|
+
const element = elements2[index];
|
|
126
|
+
const color = colors[index % colors.length];
|
|
127
|
+
image.scan(
|
|
128
|
+
element.x,
|
|
129
|
+
element.y,
|
|
130
|
+
element.width,
|
|
131
|
+
element.height,
|
|
132
|
+
function(x, y, idx) {
|
|
133
|
+
if (x === element.x || x === element.x + element.width - 1 || y === element.y || y === element.y + element.height - 1) {
|
|
134
|
+
this.bitmap.data[idx + 0] = color.rect >> 24 & 255;
|
|
135
|
+
this.bitmap.data[idx + 1] = color.rect >> 16 & 255;
|
|
136
|
+
this.bitmap.data[idx + 2] = color.rect >> 8 & 255;
|
|
137
|
+
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
const textWidth = element.label.length * 8;
|
|
142
|
+
const textHeight = 12;
|
|
143
|
+
const rectWidth = textWidth + 5;
|
|
144
|
+
const rectHeight = textHeight + 4;
|
|
145
|
+
let rectX = element.x - rectWidth;
|
|
146
|
+
let rectY = element.y + element.height / 2 - textHeight / 2 - 2;
|
|
147
|
+
if (rectX < 0) {
|
|
148
|
+
rectX = element.x;
|
|
149
|
+
rectY = element.y - rectHeight;
|
|
150
|
+
}
|
|
151
|
+
image.scan(rectX, rectY, rectWidth, rectHeight, function(x, y, idx) {
|
|
152
|
+
this.bitmap.data[idx + 0] = color.rect >> 24 & 255;
|
|
153
|
+
this.bitmap.data[idx + 1] = color.rect >> 16 & 255;
|
|
154
|
+
this.bitmap.data[idx + 2] = color.rect >> 8 & 255;
|
|
155
|
+
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
156
|
+
});
|
|
157
|
+
const font = await Jimp3.loadFont(Jimp3.FONT_SANS_16_WHITE);
|
|
158
|
+
image.print(
|
|
159
|
+
font,
|
|
160
|
+
rectX,
|
|
161
|
+
rectY,
|
|
162
|
+
{
|
|
163
|
+
text: element.label,
|
|
164
|
+
alignmentX: Jimp3.HORIZONTAL_ALIGN_CENTER,
|
|
165
|
+
alignmentY: Jimp3.VERTICAL_ALIGN_MIDDLE
|
|
166
|
+
},
|
|
167
|
+
rectWidth,
|
|
168
|
+
rectHeight
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
return image.getBufferAsync(Jimp3.MIME_PNG);
|
|
172
|
+
};
|
|
173
|
+
return createPngOverlay(elements, imageWidth, imageHeight);
|
|
174
|
+
};
|
|
175
|
+
var processImageElementInfo = async (options) => {
|
|
176
|
+
const base64Image = options.inputImgBase64.split(";base64,").pop();
|
|
177
|
+
assert2(base64Image, "base64Image is undefined");
|
|
178
|
+
const imageBuffer = Buffer3.from(base64Image, "base64");
|
|
179
|
+
const image = await Jimp3.read(imageBuffer);
|
|
180
|
+
const { width, height } = image.bitmap;
|
|
181
|
+
if (width && height) {
|
|
182
|
+
const svgOverlay = await createSvgOverlay(
|
|
183
|
+
options.elementsPositionInfo,
|
|
184
|
+
width,
|
|
185
|
+
height
|
|
186
|
+
);
|
|
187
|
+
const svgOverlayWithoutText = await createSvgOverlay(
|
|
188
|
+
options.elementsPositionInfoWithoutText,
|
|
189
|
+
width,
|
|
190
|
+
height
|
|
191
|
+
);
|
|
192
|
+
const compositeElementInfoImgBase64 = await Jimp3.read(imageBuffer).then(async (image2) => {
|
|
193
|
+
const svgImage = await Jimp3.read(svgOverlay);
|
|
194
|
+
return image2.composite(svgImage, 0, 0, {
|
|
195
|
+
mode: Jimp3.BLEND_SOURCE_OVER,
|
|
196
|
+
opacitySource: 1,
|
|
197
|
+
opacityDest: 1
|
|
198
|
+
});
|
|
199
|
+
}).then((compositeImage) => {
|
|
200
|
+
return compositeImage.getBufferAsync(Jimp3.MIME_PNG);
|
|
201
|
+
}).then((buffer) => {
|
|
202
|
+
return buffer.toString("base64");
|
|
203
|
+
}).catch((error) => {
|
|
204
|
+
throw error;
|
|
205
|
+
});
|
|
206
|
+
const compositeElementInfoImgWithoutTextBase64 = await Jimp3.read(
|
|
207
|
+
imageBuffer
|
|
208
|
+
).then(async (image2) => {
|
|
209
|
+
const svgImage = await Jimp3.read(svgOverlayWithoutText);
|
|
210
|
+
return image2.composite(svgImage, 0, 0, {
|
|
211
|
+
mode: Jimp3.BLEND_SOURCE_OVER,
|
|
212
|
+
opacitySource: 1,
|
|
213
|
+
opacityDest: 1
|
|
214
|
+
});
|
|
215
|
+
}).then((compositeImage) => {
|
|
216
|
+
return compositeImage.getBufferAsync(Jimp3.MIME_PNG);
|
|
217
|
+
}).then((buffer) => {
|
|
218
|
+
return buffer.toString("base64");
|
|
219
|
+
}).catch((error) => {
|
|
220
|
+
throw error;
|
|
221
|
+
});
|
|
222
|
+
return {
|
|
223
|
+
compositeElementInfoImgBase64,
|
|
224
|
+
compositeElementInfoImgWithoutTextBase64
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
throw Error("Image processing failed because width or height is undefined");
|
|
228
|
+
};
|
|
229
|
+
export {
|
|
230
|
+
base64Encoded,
|
|
231
|
+
calculateNewDimensions,
|
|
232
|
+
imageInfo,
|
|
233
|
+
imageInfoOfBase64,
|
|
234
|
+
processImageElementInfo,
|
|
235
|
+
resizeImg,
|
|
236
|
+
saveBase64Image,
|
|
237
|
+
transformImgPathToBase64,
|
|
238
|
+
trimImage
|
|
239
|
+
};
|
package/dist/es/index.js
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
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/constants/index.ts
|
|
21
|
+
var constants_exports = {};
|
|
22
|
+
__export(constants_exports, {
|
|
23
|
+
NodeType: () => NodeType,
|
|
24
|
+
TEXT_MAX_SIZE: () => TEXT_MAX_SIZE,
|
|
25
|
+
TEXT_SIZE_THRESHOLD: () => TEXT_SIZE_THRESHOLD
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(constants_exports);
|
|
28
|
+
var TEXT_SIZE_THRESHOLD = 9;
|
|
29
|
+
var TEXT_MAX_SIZE = 40;
|
|
30
|
+
var NodeType = /* @__PURE__ */ ((NodeType2) => {
|
|
31
|
+
NodeType2["FORM_ITEM"] = "FORM_ITEM Node";
|
|
32
|
+
NodeType2["BUTTON"] = "BUTTON Node";
|
|
33
|
+
NodeType2["IMG"] = "IMG Node";
|
|
34
|
+
NodeType2["TEXT"] = "TEXT Node";
|
|
35
|
+
return NodeType2;
|
|
36
|
+
})(NodeType || {});
|
|
37
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
38
|
+
0 && (module.exports = {
|
|
39
|
+
NodeType,
|
|
40
|
+
TEXT_MAX_SIZE,
|
|
41
|
+
TEXT_SIZE_THRESHOLD
|
|
42
|
+
});
|
package/dist/lib/img.js
ADDED
|
@@ -0,0 +1,284 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/img/index.ts
|
|
31
|
+
var img_exports = {};
|
|
32
|
+
__export(img_exports, {
|
|
33
|
+
base64Encoded: () => base64Encoded,
|
|
34
|
+
calculateNewDimensions: () => calculateNewDimensions,
|
|
35
|
+
imageInfo: () => imageInfo,
|
|
36
|
+
imageInfoOfBase64: () => imageInfoOfBase64,
|
|
37
|
+
processImageElementInfo: () => processImageElementInfo,
|
|
38
|
+
resizeImg: () => resizeImg,
|
|
39
|
+
saveBase64Image: () => saveBase64Image,
|
|
40
|
+
transformImgPathToBase64: () => transformImgPathToBase64,
|
|
41
|
+
trimImage: () => trimImage
|
|
42
|
+
});
|
|
43
|
+
module.exports = __toCommonJS(img_exports);
|
|
44
|
+
|
|
45
|
+
// src/img/info.ts
|
|
46
|
+
var import_node_assert = __toESM(require("assert"));
|
|
47
|
+
var import_node_buffer = require("buffer");
|
|
48
|
+
var import_node_fs = require("fs");
|
|
49
|
+
var import_jimp = __toESM(require("jimp"));
|
|
50
|
+
async function imageInfo(image) {
|
|
51
|
+
let jimpImage;
|
|
52
|
+
if (typeof image === "string") {
|
|
53
|
+
jimpImage = await import_jimp.default.read(image);
|
|
54
|
+
} else if (import_node_buffer.Buffer.isBuffer(image)) {
|
|
55
|
+
jimpImage = await import_jimp.default.read(image);
|
|
56
|
+
} else {
|
|
57
|
+
throw new Error("Invalid image input: must be a string path or a Buffer");
|
|
58
|
+
}
|
|
59
|
+
const { width, height } = jimpImage.bitmap;
|
|
60
|
+
(0, import_node_assert.default)(
|
|
61
|
+
width && height,
|
|
62
|
+
`Invalid image: ${typeof image === "string" ? image : "Buffer"}`
|
|
63
|
+
);
|
|
64
|
+
return { width, height };
|
|
65
|
+
}
|
|
66
|
+
async function imageInfoOfBase64(imageBase64) {
|
|
67
|
+
const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, "");
|
|
68
|
+
return imageInfo(import_node_buffer.Buffer.from(base64Data, "base64"));
|
|
69
|
+
}
|
|
70
|
+
function base64Encoded(image, withHeader = true) {
|
|
71
|
+
const imageBuffer = (0, import_node_fs.readFileSync)(image);
|
|
72
|
+
if (!withHeader) {
|
|
73
|
+
return imageBuffer.toString("base64");
|
|
74
|
+
}
|
|
75
|
+
if (image.endsWith("png")) {
|
|
76
|
+
return `data:image/png;base64,${imageBuffer.toString("base64")}`;
|
|
77
|
+
}
|
|
78
|
+
if (image.endsWith("jpg") || image.endsWith("jpeg")) {
|
|
79
|
+
return `data:image/jpeg;base64,${imageBuffer.toString("base64")}`;
|
|
80
|
+
}
|
|
81
|
+
throw new Error("unsupported image type");
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
// src/img/transform.ts
|
|
85
|
+
var import_node_buffer2 = require("buffer");
|
|
86
|
+
var import_jimp2 = __toESM(require("jimp"));
|
|
87
|
+
async function saveBase64Image(options) {
|
|
88
|
+
const { base64Data, outputPath } = options;
|
|
89
|
+
const base64Image = base64Data.split(";base64,").pop() || base64Data;
|
|
90
|
+
const imageBuffer = import_node_buffer2.Buffer.from(base64Image, "base64");
|
|
91
|
+
const image = await import_jimp2.default.read(imageBuffer);
|
|
92
|
+
await image.writeAsync(outputPath);
|
|
93
|
+
console.log("Image successfully written to file.");
|
|
94
|
+
}
|
|
95
|
+
async function transformImgPathToBase64(inputPath) {
|
|
96
|
+
const image = await import_jimp2.default.read(inputPath);
|
|
97
|
+
const buffer = await image.getBufferAsync(import_jimp2.default.MIME_PNG);
|
|
98
|
+
return buffer.toString("base64");
|
|
99
|
+
}
|
|
100
|
+
async function resizeImg(base64Data) {
|
|
101
|
+
const base64Image = base64Data.split(";base64,").pop() || base64Data;
|
|
102
|
+
const imageBuffer = import_node_buffer2.Buffer.from(base64Image, "base64");
|
|
103
|
+
const image = await import_jimp2.default.read(imageBuffer);
|
|
104
|
+
const { width, height } = image.bitmap;
|
|
105
|
+
if (!width || !height) {
|
|
106
|
+
throw Error("undefined width or height with url");
|
|
107
|
+
}
|
|
108
|
+
const newSize = calculateNewDimensions(width, height);
|
|
109
|
+
image.resize(newSize.width, newSize.height);
|
|
110
|
+
const buffer = await image.getBufferAsync(import_jimp2.default.MIME_PNG);
|
|
111
|
+
return buffer.toString("base64");
|
|
112
|
+
}
|
|
113
|
+
function calculateNewDimensions(originalWidth, originalHeight) {
|
|
114
|
+
const maxWidth = 768;
|
|
115
|
+
const maxHeight = 2048;
|
|
116
|
+
let newWidth = originalWidth;
|
|
117
|
+
let newHeight = originalHeight;
|
|
118
|
+
const aspectRatio = originalWidth / originalHeight;
|
|
119
|
+
if (originalWidth > maxWidth) {
|
|
120
|
+
newWidth = maxWidth;
|
|
121
|
+
newHeight = newWidth / aspectRatio;
|
|
122
|
+
}
|
|
123
|
+
if (newHeight > maxHeight) {
|
|
124
|
+
newHeight = maxHeight;
|
|
125
|
+
newWidth = newHeight * aspectRatio;
|
|
126
|
+
}
|
|
127
|
+
return {
|
|
128
|
+
width: Math.round(newWidth),
|
|
129
|
+
height: Math.round(newHeight)
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
async function trimImage(image) {
|
|
133
|
+
const jimpImage = await import_jimp2.default.read(
|
|
134
|
+
import_node_buffer2.Buffer.isBuffer(image) ? image : import_node_buffer2.Buffer.from(image)
|
|
135
|
+
);
|
|
136
|
+
const { width, height } = jimpImage.bitmap;
|
|
137
|
+
if (width <= 3 || height <= 3) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
const trimmedImage = jimpImage.autocrop();
|
|
141
|
+
const { width: trimmedWidth, height: trimmedHeight } = trimmedImage.bitmap;
|
|
142
|
+
const trimOffsetLeft = (width - trimmedWidth) / 2;
|
|
143
|
+
const trimOffsetTop = (height - trimmedHeight) / 2;
|
|
144
|
+
if (trimOffsetLeft === 0 && trimOffsetTop === 0) {
|
|
145
|
+
return null;
|
|
146
|
+
}
|
|
147
|
+
return {
|
|
148
|
+
trimOffsetLeft: -trimOffsetLeft,
|
|
149
|
+
trimOffsetTop: -trimOffsetTop,
|
|
150
|
+
width: trimmedWidth,
|
|
151
|
+
height: trimmedHeight
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
// src/img/box-select.ts
|
|
156
|
+
var import_node_assert2 = __toESM(require("assert"));
|
|
157
|
+
var import_node_buffer3 = require("buffer");
|
|
158
|
+
var import_jimp3 = __toESM(require("jimp"));
|
|
159
|
+
var createSvgOverlay = (elements, imageWidth, imageHeight) => {
|
|
160
|
+
const createPngOverlay = async (elements2, imageWidth2, imageHeight2) => {
|
|
161
|
+
const image = new import_jimp3.default(imageWidth2, imageHeight2, 0);
|
|
162
|
+
const colors = [
|
|
163
|
+
{ rect: 65535, text: 4294967295 },
|
|
164
|
+
// blue, white
|
|
165
|
+
{ rect: 2336560127, text: 4294967295 }
|
|
166
|
+
// brown, white
|
|
167
|
+
];
|
|
168
|
+
for (let index = 0; index < elements2.length; index++) {
|
|
169
|
+
const element = elements2[index];
|
|
170
|
+
const color = colors[index % colors.length];
|
|
171
|
+
image.scan(
|
|
172
|
+
element.x,
|
|
173
|
+
element.y,
|
|
174
|
+
element.width,
|
|
175
|
+
element.height,
|
|
176
|
+
function(x, y, idx) {
|
|
177
|
+
if (x === element.x || x === element.x + element.width - 1 || y === element.y || y === element.y + element.height - 1) {
|
|
178
|
+
this.bitmap.data[idx + 0] = color.rect >> 24 & 255;
|
|
179
|
+
this.bitmap.data[idx + 1] = color.rect >> 16 & 255;
|
|
180
|
+
this.bitmap.data[idx + 2] = color.rect >> 8 & 255;
|
|
181
|
+
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
);
|
|
185
|
+
const textWidth = element.label.length * 8;
|
|
186
|
+
const textHeight = 12;
|
|
187
|
+
const rectWidth = textWidth + 5;
|
|
188
|
+
const rectHeight = textHeight + 4;
|
|
189
|
+
let rectX = element.x - rectWidth;
|
|
190
|
+
let rectY = element.y + element.height / 2 - textHeight / 2 - 2;
|
|
191
|
+
if (rectX < 0) {
|
|
192
|
+
rectX = element.x;
|
|
193
|
+
rectY = element.y - rectHeight;
|
|
194
|
+
}
|
|
195
|
+
image.scan(rectX, rectY, rectWidth, rectHeight, function(x, y, idx) {
|
|
196
|
+
this.bitmap.data[idx + 0] = color.rect >> 24 & 255;
|
|
197
|
+
this.bitmap.data[idx + 1] = color.rect >> 16 & 255;
|
|
198
|
+
this.bitmap.data[idx + 2] = color.rect >> 8 & 255;
|
|
199
|
+
this.bitmap.data[idx + 3] = color.rect & 255;
|
|
200
|
+
});
|
|
201
|
+
const font = await import_jimp3.default.loadFont(import_jimp3.default.FONT_SANS_16_WHITE);
|
|
202
|
+
image.print(
|
|
203
|
+
font,
|
|
204
|
+
rectX,
|
|
205
|
+
rectY,
|
|
206
|
+
{
|
|
207
|
+
text: element.label,
|
|
208
|
+
alignmentX: import_jimp3.default.HORIZONTAL_ALIGN_CENTER,
|
|
209
|
+
alignmentY: import_jimp3.default.VERTICAL_ALIGN_MIDDLE
|
|
210
|
+
},
|
|
211
|
+
rectWidth,
|
|
212
|
+
rectHeight
|
|
213
|
+
);
|
|
214
|
+
}
|
|
215
|
+
return image.getBufferAsync(import_jimp3.default.MIME_PNG);
|
|
216
|
+
};
|
|
217
|
+
return createPngOverlay(elements, imageWidth, imageHeight);
|
|
218
|
+
};
|
|
219
|
+
var processImageElementInfo = async (options) => {
|
|
220
|
+
const base64Image = options.inputImgBase64.split(";base64,").pop();
|
|
221
|
+
(0, import_node_assert2.default)(base64Image, "base64Image is undefined");
|
|
222
|
+
const imageBuffer = import_node_buffer3.Buffer.from(base64Image, "base64");
|
|
223
|
+
const image = await import_jimp3.default.read(imageBuffer);
|
|
224
|
+
const { width, height } = image.bitmap;
|
|
225
|
+
if (width && height) {
|
|
226
|
+
const svgOverlay = await createSvgOverlay(
|
|
227
|
+
options.elementsPositionInfo,
|
|
228
|
+
width,
|
|
229
|
+
height
|
|
230
|
+
);
|
|
231
|
+
const svgOverlayWithoutText = await createSvgOverlay(
|
|
232
|
+
options.elementsPositionInfoWithoutText,
|
|
233
|
+
width,
|
|
234
|
+
height
|
|
235
|
+
);
|
|
236
|
+
const compositeElementInfoImgBase64 = await import_jimp3.default.read(imageBuffer).then(async (image2) => {
|
|
237
|
+
const svgImage = await import_jimp3.default.read(svgOverlay);
|
|
238
|
+
return image2.composite(svgImage, 0, 0, {
|
|
239
|
+
mode: import_jimp3.default.BLEND_SOURCE_OVER,
|
|
240
|
+
opacitySource: 1,
|
|
241
|
+
opacityDest: 1
|
|
242
|
+
});
|
|
243
|
+
}).then((compositeImage) => {
|
|
244
|
+
return compositeImage.getBufferAsync(import_jimp3.default.MIME_PNG);
|
|
245
|
+
}).then((buffer) => {
|
|
246
|
+
return buffer.toString("base64");
|
|
247
|
+
}).catch((error) => {
|
|
248
|
+
throw error;
|
|
249
|
+
});
|
|
250
|
+
const compositeElementInfoImgWithoutTextBase64 = await import_jimp3.default.read(
|
|
251
|
+
imageBuffer
|
|
252
|
+
).then(async (image2) => {
|
|
253
|
+
const svgImage = await import_jimp3.default.read(svgOverlayWithoutText);
|
|
254
|
+
return image2.composite(svgImage, 0, 0, {
|
|
255
|
+
mode: import_jimp3.default.BLEND_SOURCE_OVER,
|
|
256
|
+
opacitySource: 1,
|
|
257
|
+
opacityDest: 1
|
|
258
|
+
});
|
|
259
|
+
}).then((compositeImage) => {
|
|
260
|
+
return compositeImage.getBufferAsync(import_jimp3.default.MIME_PNG);
|
|
261
|
+
}).then((buffer) => {
|
|
262
|
+
return buffer.toString("base64");
|
|
263
|
+
}).catch((error) => {
|
|
264
|
+
throw error;
|
|
265
|
+
});
|
|
266
|
+
return {
|
|
267
|
+
compositeElementInfoImgBase64,
|
|
268
|
+
compositeElementInfoImgWithoutTextBase64
|
|
269
|
+
};
|
|
270
|
+
}
|
|
271
|
+
throw Error("Image processing failed because width or height is undefined");
|
|
272
|
+
};
|
|
273
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
274
|
+
0 && (module.exports = {
|
|
275
|
+
base64Encoded,
|
|
276
|
+
calculateNewDimensions,
|
|
277
|
+
imageInfo,
|
|
278
|
+
imageInfoOfBase64,
|
|
279
|
+
processImageElementInfo,
|
|
280
|
+
resizeImg,
|
|
281
|
+
saveBase64Image,
|
|
282
|
+
transformImgPathToBase64,
|
|
283
|
+
trimImage
|
|
284
|
+
});
|
|
@@ -0,0 +1,28 @@
|
|
|
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/index.ts
|
|
21
|
+
var src_exports = {};
|
|
22
|
+
__export(src_exports, {
|
|
23
|
+
default: () => src_default
|
|
24
|
+
});
|
|
25
|
+
module.exports = __toCommonJS(src_exports);
|
|
26
|
+
function src_default() {
|
|
27
|
+
return "hello world";
|
|
28
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
declare const TEXT_SIZE_THRESHOLD = 9;
|
|
2
|
+
declare const TEXT_MAX_SIZE = 40;
|
|
3
|
+
declare enum NodeType {
|
|
4
|
+
FORM_ITEM = "FORM_ITEM Node",
|
|
5
|
+
BUTTON = "BUTTON Node",
|
|
6
|
+
IMG = "IMG Node",
|
|
7
|
+
TEXT = "TEXT Node"
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export { NodeType, TEXT_MAX_SIZE, TEXT_SIZE_THRESHOLD };
|