@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/es/image.js CHANGED
@@ -75,7 +75,12 @@ function calculateNewDimensions(originalWidth, originalHeight) {
75
75
  };
76
76
  }
77
77
  async function trimImage(image) {
78
- const { info } = await Sharp2(image).trim().toBuffer({
78
+ const imgInstance = Sharp2(image);
79
+ const instanceInfo = await imgInstance.metadata();
80
+ if (!instanceInfo.width || instanceInfo.width <= 3 || !instanceInfo.height || instanceInfo.height <= 3) {
81
+ return null;
82
+ }
83
+ const { info } = await imgInstance.trim().toBuffer({
79
84
  resolveWithObject: true
80
85
  });
81
86
  if (typeof info.trimOffsetLeft === "undefined" || typeof info.trimOffsetTop === "undefined") {
@@ -88,18 +93,27 @@ async function trimImage(image) {
88
93
  height: info.height
89
94
  };
90
95
  }
91
- async function alignCoordByTrim(image, center) {
92
- const img = await Sharp2(image).extract(center).toBuffer();
93
- const trimInfo = await trimImage(img);
94
- if (!trimInfo) {
95
- return center;
96
+ async function alignCoordByTrim(image, centerRect) {
97
+ const imgInfo = await Sharp2(image).metadata();
98
+ if (!(imgInfo == null ? void 0 : imgInfo.width) || !imgInfo.height || imgInfo.width <= 3 || imgInfo.height <= 3) {
99
+ return centerRect;
100
+ }
101
+ try {
102
+ const img = await Sharp2(image).extract(centerRect).toBuffer();
103
+ const trimInfo = await trimImage(img);
104
+ if (!trimInfo) {
105
+ return centerRect;
106
+ }
107
+ return {
108
+ left: centerRect.left - trimInfo.trimOffsetLeft,
109
+ top: centerRect.top - trimInfo.trimOffsetTop,
110
+ width: trimInfo.width,
111
+ height: trimInfo.height
112
+ };
113
+ } catch (e) {
114
+ console.log(imgInfo);
115
+ throw e;
96
116
  }
97
- return {
98
- left: center.left - trimInfo.trimOffsetLeft,
99
- top: center.top - trimInfo.trimOffsetTop,
100
- width: trimInfo.width,
101
- height: trimInfo.height
102
- };
103
117
  }
104
118
 
105
119
  // src/image/visualization.ts
@@ -140,8 +154,8 @@ function getTmpDir() {
140
154
  mkdirSync(path, { recursive: true });
141
155
  return path;
142
156
  }
143
- function getTmpFile(fileExt) {
144
- const filename = `${randomUUID()}.${fileExt}`;
157
+ function getTmpFile(fileExtWithoutDot) {
158
+ const filename = `${randomUUID()}.${fileExtWithoutDot}`;
145
159
  return join(getTmpDir(), filename);
146
160
  }
147
161