@midscene/core 0.0.1 → 0.1.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/dist/lib/image.js CHANGED
@@ -45,7 +45,7 @@ module.exports = __toCommonJS(image_exports);
45
45
 
46
46
  // src/image/info.ts
47
47
  var import_node_assert = __toESM(require("assert"));
48
- var import_buffer = require("buffer");
48
+ var import_node_buffer = require("buffer");
49
49
  var import_node_fs = require("fs");
50
50
  var import_sharp = __toESM(require("sharp"));
51
51
  async function imageInfo(image) {
@@ -55,7 +55,7 @@ async function imageInfo(image) {
55
55
  }
56
56
  async function imageInfoOfBase64(imageBase64) {
57
57
  const base64Data = imageBase64.replace(/^data:image\/\w+;base64,/, "");
58
- return imageInfo(import_buffer.Buffer.from(base64Data, "base64"));
58
+ return imageInfo(import_node_buffer.Buffer.from(base64Data, "base64"));
59
59
  }
60
60
  function base64Encoded(image, withHeader = true) {
61
61
  const imageBuffer = (0, import_node_fs.readFileSync)(image);
@@ -71,12 +71,12 @@ function base64Encoded(image, withHeader = true) {
71
71
  }
72
72
 
73
73
  // src/image/transform.ts
74
- var import_node_buffer = require("buffer");
74
+ var import_node_buffer2 = require("buffer");
75
75
  var import_sharp2 = __toESM(require("sharp"));
76
76
  async function saveBase64Image(options) {
77
77
  const { base64Data, outputPath } = options;
78
78
  const base64Image = base64Data.split(";base64,").pop() || base64Data;
79
- const imageBuffer = import_node_buffer.Buffer.from(base64Image, "base64");
79
+ const imageBuffer = import_node_buffer2.Buffer.from(base64Image, "base64");
80
80
  await (0, import_sharp2.default)(imageBuffer).toFile(outputPath);
81
81
  console.log("Image successfully written to file.");
82
82
  }
@@ -88,7 +88,7 @@ async function transformImgPathToBase64(inputPath) {
88
88
  }
89
89
  async function resizeImg(base64Data) {
90
90
  const base64Image = base64Data.split(";base64,").pop() || base64Data;
91
- const imageBuffer = import_node_buffer.Buffer.from(base64Image, "base64");
91
+ const imageBuffer = import_node_buffer2.Buffer.from(base64Image, "base64");
92
92
  const metadata = await (0, import_sharp2.default)(imageBuffer).metadata();
93
93
  const { width, height } = metadata;
94
94
  if (!width || !height) {
@@ -120,7 +120,12 @@ function calculateNewDimensions(originalWidth, originalHeight) {
120
120
  };
121
121
  }
122
122
  async function trimImage(image) {
123
- const { info } = await (0, import_sharp2.default)(image).trim().toBuffer({
123
+ const imgInstance = (0, import_sharp2.default)(image);
124
+ const instanceInfo = await imgInstance.metadata();
125
+ if (!instanceInfo.width || instanceInfo.width <= 3 || !instanceInfo.height || instanceInfo.height <= 3) {
126
+ return null;
127
+ }
128
+ const { info } = await imgInstance.trim().toBuffer({
124
129
  resolveWithObject: true
125
130
  });
126
131
  if (typeof info.trimOffsetLeft === "undefined" || typeof info.trimOffsetTop === "undefined") {
@@ -133,22 +138,31 @@ async function trimImage(image) {
133
138
  height: info.height
134
139
  };
135
140
  }
136
- async function alignCoordByTrim(image, center) {
137
- const img = await (0, import_sharp2.default)(image).extract(center).toBuffer();
138
- const trimInfo = await trimImage(img);
139
- if (!trimInfo) {
140
- return center;
141
+ async function alignCoordByTrim(image, centerRect) {
142
+ const imgInfo = await (0, import_sharp2.default)(image).metadata();
143
+ if (!(imgInfo == null ? void 0 : imgInfo.width) || !imgInfo.height || imgInfo.width <= 3 || imgInfo.height <= 3) {
144
+ return centerRect;
145
+ }
146
+ try {
147
+ const img = await (0, import_sharp2.default)(image).extract(centerRect).toBuffer();
148
+ const trimInfo = await trimImage(img);
149
+ if (!trimInfo) {
150
+ return centerRect;
151
+ }
152
+ return {
153
+ left: centerRect.left - trimInfo.trimOffsetLeft,
154
+ top: centerRect.top - trimInfo.trimOffsetTop,
155
+ width: trimInfo.width,
156
+ height: trimInfo.height
157
+ };
158
+ } catch (e) {
159
+ console.log(imgInfo);
160
+ throw e;
141
161
  }
142
- return {
143
- left: center.left - trimInfo.trimOffsetLeft,
144
- top: center.top - trimInfo.trimOffsetTop,
145
- width: trimInfo.width,
146
- height: trimInfo.height
147
- };
148
162
  }
149
163
 
150
164
  // src/image/visualization.ts
151
- var import_buffer2 = require("buffer");
165
+ var import_buffer = require("buffer");
152
166
  var import_sharp3 = __toESM(require("sharp"));
153
167
 
154
168
  // src/utils.ts
@@ -185,8 +199,8 @@ function getTmpDir() {
185
199
  (0, import_fs.mkdirSync)(path, { recursive: true });
186
200
  return path;
187
201
  }
188
- function getTmpFile(fileExt) {
189
- const filename = `${(0, import_crypto.randomUUID)()}.${fileExt}`;
202
+ function getTmpFile(fileExtWithoutDot) {
203
+ const filename = `${(0, import_crypto.randomUUID)()}.${fileExtWithoutDot}`;
190
204
  return (0, import_path.join)(getTmpDir(), filename);
191
205
  }
192
206
 
@@ -277,7 +291,7 @@ async function composeSectionDiagram(sections, context) {
277
291
  ${rects.join("\n")}
278
292
  </svg>
279
293
  `;
280
- const svgBuffer = import_buffer2.Buffer.from(rectangles);
294
+ const svgBuffer = import_buffer.Buffer.from(rectangles);
281
295
  const file = getTmpFile("png");
282
296
  await (0, import_sharp3.default)({
283
297
  create: {