@midscene/shared 0.12.8 → 0.12.9-beta-20250319085532.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/es/img.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Buffer } from 'node:buffer';
2
2
  import Jimp from 'jimp';
3
- import { NodeType } from './constants.js';
4
3
  import { R as Rect } from './index-305e7a7e.js';
4
+ import { NodeType } from './constants.js';
5
5
 
6
6
  interface Size {
7
7
  width: number;
@@ -102,7 +102,10 @@ declare function trimImage(image: string | Buffer): Promise<{
102
102
  width: number;
103
103
  height: number;
104
104
  } | null>;
105
- declare function paddingToMatchBlock(imageBase64: string, blockSize?: number): Promise<string>;
105
+ declare function jimpFromBase64(base64: string): Promise<Jimp>;
106
+ declare function paddingToMatchBlock(image: Jimp, blockSize?: number): Promise<Jimp>;
107
+ declare function cropByRect(image: Jimp, rect: Rect): Promise<void>;
108
+ declare function jimpToBase64(image: Jimp): Promise<string>;
106
109
 
107
110
  type ElementType = {
108
111
  locator?: string;
@@ -149,4 +152,4 @@ declare function savePositionImg(options: {
149
152
  outputPath: string;
150
153
  }): Promise<void>;
151
154
 
152
- export { base64Encoded, bufferFromBase64, compositeElementInfoImg, drawBoxOnImage, imageInfo, imageInfoOfBase64, paddingToMatchBlock, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, savePositionImg, transformImgPathToBase64, trimImage, zoomForGPT4o };
155
+ export { base64Encoded, bufferFromBase64, compositeElementInfoImg, cropByRect, drawBoxOnImage, imageInfo, imageInfoOfBase64, jimpFromBase64, jimpToBase64, paddingToMatchBlock, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, savePositionImg, transformImgPathToBase64, trimImage, zoomForGPT4o };
package/dist/es/img.js CHANGED
@@ -169,22 +169,31 @@ async function trimImage(image) {
169
169
  height: trimmedHeight
170
170
  };
171
171
  }
172
- async function paddingToMatchBlock(imageBase64, blockSize = 28) {
173
- debugImg("paddingToMatchBlock start");
172
+ async function jimpFromBase64(base64) {
174
173
  const Jimp = await getJimp();
175
- const imageBuffer = await bufferFromBase642(imageBase64);
176
- const image = await Jimp.read(imageBuffer);
174
+ const imageBuffer = await bufferFromBase642(base64);
175
+ return Jimp.read(imageBuffer);
176
+ }
177
+ async function paddingToMatchBlock(image, blockSize = 28) {
178
+ debugImg("paddingToMatchBlock start");
177
179
  const { width, height } = image.bitmap;
178
180
  const targetWidth = Math.ceil(width / blockSize) * blockSize;
179
181
  const targetHeight = Math.ceil(height / blockSize) * blockSize;
180
182
  if (targetWidth === width && targetHeight === height) {
181
- return imageBase64;
183
+ return image;
182
184
  }
185
+ const Jimp = await getJimp();
183
186
  const paddedImage = new Jimp(targetWidth, targetHeight, 4294967295);
184
187
  paddedImage.composite(image, 0, 0);
185
- const base64 = await paddedImage.getBase64Async(Jimp.MIME_JPEG);
186
- debugImg("paddingToMatchBlock done");
187
- return base64;
188
+ return paddedImage;
189
+ }
190
+ async function cropByRect(image, rect) {
191
+ const { left, top, width, height } = rect;
192
+ image.crop(left, top, width, height);
193
+ }
194
+ async function jimpToBase64(image) {
195
+ const Jimp = await getJimp();
196
+ return image.getBase64Async(Jimp.MIME_JPEG);
188
197
  }
189
198
 
190
199
  // src/img/box-select.ts
@@ -411,9 +420,12 @@ export {
411
420
  base64Encoded,
412
421
  bufferFromBase64,
413
422
  compositeElementInfoImg,
423
+ cropByRect,
414
424
  drawBoxOnImage,
415
425
  imageInfo,
416
426
  imageInfoOfBase64,
427
+ jimpFromBase64,
428
+ jimpToBase64,
417
429
  paddingToMatchBlock,
418
430
  processImageElementInfo,
419
431
  resizeImg,
@@ -13,5 +13,6 @@ declare function generateHashId(rect: any, content?: string): string;
13
13
  * @throws Error with the provided message if the condition is false
14
14
  */
15
15
  declare function assert(condition: any, message?: string): asserts condition;
16
+ declare function getGlobalScope(): typeof globalThis | undefined;
16
17
 
17
- export { assert, enableDebug, generateHashId, getDebug, ifInBrowser, uuid };
18
+ export { assert, enableDebug, generateHashId, getDebug, getGlobalScope, ifInBrowser, uuid };
package/dist/es/utils.js CHANGED
@@ -44,11 +44,24 @@ function assert(condition, message) {
44
44
  throw new Error(message || "Assertion failed");
45
45
  }
46
46
  }
47
+ function getGlobalScope() {
48
+ if (typeof window !== "undefined") {
49
+ return window;
50
+ }
51
+ if (typeof global !== "undefined") {
52
+ return global;
53
+ }
54
+ if (typeof self !== "undefined") {
55
+ return self;
56
+ }
57
+ return void 0;
58
+ }
47
59
  export {
48
60
  assert,
49
61
  enableDebug,
50
62
  generateHashId,
51
63
  getDebug,
64
+ getGlobalScope,
52
65
  ifInBrowser,
53
66
  uuid
54
67
  };
package/dist/lib/img.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { Buffer } from 'node:buffer';
2
2
  import Jimp from 'jimp';
3
- import { NodeType } from './constants.js';
4
3
  import { R as Rect } from './index-305e7a7e.js';
4
+ import { NodeType } from './constants.js';
5
5
 
6
6
  interface Size {
7
7
  width: number;
@@ -102,7 +102,10 @@ declare function trimImage(image: string | Buffer): Promise<{
102
102
  width: number;
103
103
  height: number;
104
104
  } | null>;
105
- declare function paddingToMatchBlock(imageBase64: string, blockSize?: number): Promise<string>;
105
+ declare function jimpFromBase64(base64: string): Promise<Jimp>;
106
+ declare function paddingToMatchBlock(image: Jimp, blockSize?: number): Promise<Jimp>;
107
+ declare function cropByRect(image: Jimp, rect: Rect): Promise<void>;
108
+ declare function jimpToBase64(image: Jimp): Promise<string>;
106
109
 
107
110
  type ElementType = {
108
111
  locator?: string;
@@ -149,4 +152,4 @@ declare function savePositionImg(options: {
149
152
  outputPath: string;
150
153
  }): Promise<void>;
151
154
 
152
- export { base64Encoded, bufferFromBase64, compositeElementInfoImg, drawBoxOnImage, imageInfo, imageInfoOfBase64, paddingToMatchBlock, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, savePositionImg, transformImgPathToBase64, trimImage, zoomForGPT4o };
155
+ export { base64Encoded, bufferFromBase64, compositeElementInfoImg, cropByRect, drawBoxOnImage, imageInfo, imageInfoOfBase64, jimpFromBase64, jimpToBase64, paddingToMatchBlock, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, savePositionImg, transformImgPathToBase64, trimImage, zoomForGPT4o };
package/dist/lib/img.js CHANGED
@@ -33,9 +33,12 @@ __export(img_exports, {
33
33
  base64Encoded: () => base64Encoded,
34
34
  bufferFromBase64: () => bufferFromBase64,
35
35
  compositeElementInfoImg: () => compositeElementInfoImg,
36
+ cropByRect: () => cropByRect,
36
37
  drawBoxOnImage: () => drawBoxOnImage,
37
38
  imageInfo: () => imageInfo,
38
39
  imageInfoOfBase64: () => imageInfoOfBase64,
40
+ jimpFromBase64: () => jimpFromBase64,
41
+ jimpToBase64: () => jimpToBase64,
39
42
  paddingToMatchBlock: () => paddingToMatchBlock,
40
43
  processImageElementInfo: () => processImageElementInfo,
41
44
  resizeImg: () => resizeImg,
@@ -219,22 +222,31 @@ async function trimImage(image) {
219
222
  height: trimmedHeight
220
223
  };
221
224
  }
222
- async function paddingToMatchBlock(imageBase64, blockSize = 28) {
223
- debugImg("paddingToMatchBlock start");
225
+ async function jimpFromBase64(base64) {
224
226
  const Jimp = await getJimp();
225
- const imageBuffer = await bufferFromBase642(imageBase64);
226
- const image = await Jimp.read(imageBuffer);
227
+ const imageBuffer = await bufferFromBase642(base64);
228
+ return Jimp.read(imageBuffer);
229
+ }
230
+ async function paddingToMatchBlock(image, blockSize = 28) {
231
+ debugImg("paddingToMatchBlock start");
227
232
  const { width, height } = image.bitmap;
228
233
  const targetWidth = Math.ceil(width / blockSize) * blockSize;
229
234
  const targetHeight = Math.ceil(height / blockSize) * blockSize;
230
235
  if (targetWidth === width && targetHeight === height) {
231
- return imageBase64;
236
+ return image;
232
237
  }
238
+ const Jimp = await getJimp();
233
239
  const paddedImage = new Jimp(targetWidth, targetHeight, 4294967295);
234
240
  paddedImage.composite(image, 0, 0);
235
- const base64 = await paddedImage.getBase64Async(Jimp.MIME_JPEG);
236
- debugImg("paddingToMatchBlock done");
237
- return base64;
241
+ return paddedImage;
242
+ }
243
+ async function cropByRect(image, rect) {
244
+ const { left, top, width, height } = rect;
245
+ image.crop(left, top, width, height);
246
+ }
247
+ async function jimpToBase64(image) {
248
+ const Jimp = await getJimp();
249
+ return image.getBase64Async(Jimp.MIME_JPEG);
238
250
  }
239
251
 
240
252
  // src/img/box-select.ts
@@ -462,9 +474,12 @@ async function savePositionImg(options) {
462
474
  base64Encoded,
463
475
  bufferFromBase64,
464
476
  compositeElementInfoImg,
477
+ cropByRect,
465
478
  drawBoxOnImage,
466
479
  imageInfo,
467
480
  imageInfoOfBase64,
481
+ jimpFromBase64,
482
+ jimpToBase64,
468
483
  paddingToMatchBlock,
469
484
  processImageElementInfo,
470
485
  resizeImg,
@@ -13,5 +13,6 @@ declare function generateHashId(rect: any, content?: string): string;
13
13
  * @throws Error with the provided message if the condition is false
14
14
  */
15
15
  declare function assert(condition: any, message?: string): asserts condition;
16
+ declare function getGlobalScope(): typeof globalThis | undefined;
16
17
 
17
- export { assert, enableDebug, generateHashId, getDebug, ifInBrowser, uuid };
18
+ export { assert, enableDebug, generateHashId, getDebug, getGlobalScope, ifInBrowser, uuid };
package/dist/lib/utils.js CHANGED
@@ -34,6 +34,7 @@ __export(utils_exports, {
34
34
  enableDebug: () => enableDebug,
35
35
  generateHashId: () => generateHashId,
36
36
  getDebug: () => getDebug,
37
+ getGlobalScope: () => getGlobalScope,
37
38
  ifInBrowser: () => ifInBrowser,
38
39
  uuid: () => uuid
39
40
  });
@@ -83,12 +84,25 @@ function assert(condition, message) {
83
84
  throw new Error(message || "Assertion failed");
84
85
  }
85
86
  }
87
+ function getGlobalScope() {
88
+ if (typeof window !== "undefined") {
89
+ return window;
90
+ }
91
+ if (typeof global !== "undefined") {
92
+ return global;
93
+ }
94
+ if (typeof self !== "undefined") {
95
+ return self;
96
+ }
97
+ return void 0;
98
+ }
86
99
  // Annotate the CommonJS export names for ESM import in node:
87
100
  0 && (module.exports = {
88
101
  assert,
89
102
  enableDebug,
90
103
  generateHashId,
91
104
  getDebug,
105
+ getGlobalScope,
92
106
  ifInBrowser,
93
107
  uuid
94
108
  });
@@ -1,7 +1,7 @@
1
1
  import { Buffer } from 'node:buffer';
2
2
  import Jimp from 'jimp';
3
- import { NodeType } from './constants.js';
4
3
  import { R as Rect } from './index-305e7a7e.js';
4
+ import { NodeType } from './constants.js';
5
5
 
6
6
  interface Size {
7
7
  width: number;
@@ -102,7 +102,10 @@ declare function trimImage(image: string | Buffer): Promise<{
102
102
  width: number;
103
103
  height: number;
104
104
  } | null>;
105
- declare function paddingToMatchBlock(imageBase64: string, blockSize?: number): Promise<string>;
105
+ declare function jimpFromBase64(base64: string): Promise<Jimp>;
106
+ declare function paddingToMatchBlock(image: Jimp, blockSize?: number): Promise<Jimp>;
107
+ declare function cropByRect(image: Jimp, rect: Rect): Promise<void>;
108
+ declare function jimpToBase64(image: Jimp): Promise<string>;
106
109
 
107
110
  type ElementType = {
108
111
  locator?: string;
@@ -149,4 +152,4 @@ declare function savePositionImg(options: {
149
152
  outputPath: string;
150
153
  }): Promise<void>;
151
154
 
152
- export { base64Encoded, bufferFromBase64, compositeElementInfoImg, drawBoxOnImage, imageInfo, imageInfoOfBase64, paddingToMatchBlock, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, savePositionImg, transformImgPathToBase64, trimImage, zoomForGPT4o };
155
+ export { base64Encoded, bufferFromBase64, compositeElementInfoImg, cropByRect, drawBoxOnImage, imageInfo, imageInfoOfBase64, jimpFromBase64, jimpToBase64, paddingToMatchBlock, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, savePositionImg, transformImgPathToBase64, trimImage, zoomForGPT4o };
@@ -13,5 +13,6 @@ declare function generateHashId(rect: any, content?: string): string;
13
13
  * @throws Error with the provided message if the condition is false
14
14
  */
15
15
  declare function assert(condition: any, message?: string): asserts condition;
16
+ declare function getGlobalScope(): typeof globalThis | undefined;
16
17
 
17
- export { assert, enableDebug, generateHashId, getDebug, ifInBrowser, uuid };
18
+ export { assert, enableDebug, generateHashId, getDebug, getGlobalScope, ifInBrowser, uuid };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midscene/shared",
3
- "version": "0.12.8",
3
+ "version": "0.12.9-beta-20250319085532.0",
4
4
  "repository": "https://github.com/web-infra-dev/midscene",
5
5
  "homepage": "https://midscenejs.com/",
6
6
  "types": "./dist/types/index.d.ts",
package/src/img/index.ts CHANGED
@@ -12,6 +12,9 @@ export {
12
12
  zoomForGPT4o,
13
13
  saveBase64Image,
14
14
  paddingToMatchBlock,
15
+ cropByRect,
16
+ jimpFromBase64,
17
+ jimpToBase64,
15
18
  } from './transform';
16
19
  export { processImageElementInfo, compositeElementInfoImg } from './box-select';
17
20
  export { drawBoxOnImage, savePositionImg } from './draw-box';
@@ -1,8 +1,10 @@
1
1
  import assert from 'node:assert';
2
2
  import { Buffer } from 'node:buffer';
3
+
3
4
  import getDebug from 'debug';
5
+ import type Jimp from 'jimp';
6
+ import type { Rect } from 'src/types';
4
7
  import getJimp from './get-jimp';
5
-
6
8
  const debugImg = getDebug('img');
7
9
  /**
8
10
  /**
@@ -210,26 +212,40 @@ export function prependBase64Header(base64: string, mimeType = 'image/png') {
210
212
  return `data:${mimeType};base64,${base64}`;
211
213
  }
212
214
 
213
- export async function paddingToMatchBlock(imageBase64: string, blockSize = 28) {
214
- debugImg('paddingToMatchBlock start');
215
+ export async function jimpFromBase64(base64: string): Promise<Jimp> {
215
216
  const Jimp = await getJimp();
216
- const imageBuffer = await bufferFromBase64(imageBase64);
217
- const image = await Jimp.read(imageBuffer);
217
+ const imageBuffer = await bufferFromBase64(base64);
218
+ return Jimp.read(imageBuffer);
219
+ }
220
+
221
+ export async function paddingToMatchBlock(
222
+ image: Jimp,
223
+ blockSize = 28,
224
+ ): Promise<Jimp> {
225
+ debugImg('paddingToMatchBlock start');
218
226
  const { width, height } = image.bitmap;
219
227
 
220
228
  const targetWidth = Math.ceil(width / blockSize) * blockSize;
221
229
  const targetHeight = Math.ceil(height / blockSize) * blockSize;
222
230
 
223
231
  if (targetWidth === width && targetHeight === height) {
224
- return imageBase64;
232
+ return image;
225
233
  }
226
234
 
235
+ const Jimp = await getJimp();
227
236
  const paddedImage = new Jimp(targetWidth, targetHeight, 0xffffffff);
228
237
 
229
238
  // Composite the original image onto the new canvas
230
239
  paddedImage.composite(image, 0, 0);
240
+ return paddedImage;
241
+ }
231
242
 
232
- const base64 = await paddedImage.getBase64Async(Jimp.MIME_JPEG);
233
- debugImg('paddingToMatchBlock done');
234
- return base64;
243
+ export async function cropByRect(image: Jimp, rect: Rect): Promise<void> {
244
+ const { left, top, width, height } = rect;
245
+ image.crop(left, top, width, height);
246
+ }
247
+
248
+ export async function jimpToBase64(image: Jimp): Promise<string> {
249
+ const Jimp = await getJimp();
250
+ return image.getBase64Async(Jimp.MIME_JPEG);
235
251
  }
package/src/utils.ts CHANGED
@@ -68,3 +68,18 @@ export function assert(condition: any, message?: string): asserts condition {
68
68
  throw new Error(message || 'Assertion failed');
69
69
  }
70
70
  }
71
+
72
+ export function getGlobalScope() {
73
+ if (typeof window !== 'undefined') {
74
+ return window;
75
+ }
76
+
77
+ if (typeof global !== 'undefined') {
78
+ return global;
79
+ }
80
+
81
+ if (typeof self !== 'undefined') {
82
+ return self;
83
+ }
84
+ return undefined;
85
+ }