@midscene/shared 0.8.16 → 0.8.17-beta-20250103010342.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.
@@ -141,4 +141,20 @@ declare const processImageElementInfo: (options: {
141
141
  compositeElementInfoImgWithoutTextBase64: string;
142
142
  }>;
143
143
 
144
- export { base64Encoded, base64ToPngFormat, bufferFromBase64, calculateNewDimensions, compositeElementInfoImg, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, transformImgPathToBase64, trimImage };
144
+ declare function drawBoxOnImage(options: {
145
+ inputImgBase64: string;
146
+ rect: {
147
+ x: number;
148
+ y: number;
149
+ };
150
+ }): Promise<string>;
151
+ declare function savePositionImg(options: {
152
+ inputImgBase64: string;
153
+ rect: {
154
+ x: number;
155
+ y: number;
156
+ };
157
+ outputPath: string;
158
+ }): Promise<void>;
159
+
160
+ export { base64Encoded, base64ToPngFormat, bufferFromBase64, calculateNewDimensions, compositeElementInfoImg, drawBoxOnImage, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, savePositionImg, transformImgPathToBase64, trimImage };
@@ -5,6 +5,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __pow = Math.pow;
8
9
  var __export = (target, all) => {
9
10
  for (var name in all)
10
11
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -55,12 +56,14 @@ __export(img_exports, {
55
56
  bufferFromBase64: () => bufferFromBase64,
56
57
  calculateNewDimensions: () => calculateNewDimensions,
57
58
  compositeElementInfoImg: () => compositeElementInfoImg,
59
+ drawBoxOnImage: () => drawBoxOnImage,
58
60
  imageInfo: () => imageInfo,
59
61
  imageInfoOfBase64: () => imageInfoOfBase64,
60
62
  processImageElementInfo: () => processImageElementInfo,
61
63
  resizeImg: () => resizeImg,
62
64
  resizeImgBase64: () => resizeImgBase64,
63
65
  saveBase64Image: () => saveBase64Image,
66
+ savePositionImg: () => savePositionImg,
64
67
  transformImgPathToBase64: () => transformImgPathToBase64,
65
68
  trimImage: () => trimImage
66
69
  });
@@ -401,3 +404,44 @@ var processImageElementInfo = (options) => __async(void 0, null, function* () {
401
404
  compositeElementInfoImgWithoutTextBase64
402
405
  };
403
406
  });
407
+
408
+ // src/img/draw-box.ts
409
+ function drawBoxOnImage(options) {
410
+ return __async(this, null, function* () {
411
+ const { inputImgBase64, rect } = options;
412
+ const color = { r: 255, g: 0, b: 0, a: 255 };
413
+ const Jimp = yield getJimp();
414
+ const imageBuffer = yield bufferFromBase64(inputImgBase64);
415
+ const image = yield Jimp.read(imageBuffer);
416
+ const centerX = rect.x;
417
+ const centerY = rect.y;
418
+ const radius = 5;
419
+ image.scan(
420
+ Math.floor(centerX - radius),
421
+ Math.floor(centerY - radius),
422
+ radius * 2,
423
+ radius * 2,
424
+ function(x, y, idx) {
425
+ const distance = Math.sqrt(__pow(x - centerX, 2) + __pow(y - centerY, 2));
426
+ if (distance <= radius) {
427
+ this.bitmap.data[idx + 0] = color.r;
428
+ this.bitmap.data[idx + 1] = color.g;
429
+ this.bitmap.data[idx + 2] = color.b;
430
+ this.bitmap.data[idx + 3] = color.a;
431
+ }
432
+ }
433
+ );
434
+ const resultBase64 = yield image.getBase64Async(Jimp.MIME_PNG);
435
+ return resultBase64;
436
+ });
437
+ }
438
+ function savePositionImg(options) {
439
+ return __async(this, null, function* () {
440
+ const { inputImgBase64, rect, outputPath } = options;
441
+ const imgBase64 = yield drawBoxOnImage({ inputImgBase64, rect });
442
+ yield saveBase64Image({
443
+ base64Data: imgBase64,
444
+ outputPath
445
+ });
446
+ });
447
+ }
package/dist/es/img.d.ts CHANGED
@@ -141,4 +141,20 @@ declare const processImageElementInfo: (options: {
141
141
  compositeElementInfoImgWithoutTextBase64: string;
142
142
  }>;
143
143
 
144
- export { base64Encoded, base64ToPngFormat, bufferFromBase64, calculateNewDimensions, compositeElementInfoImg, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, transformImgPathToBase64, trimImage };
144
+ declare function drawBoxOnImage(options: {
145
+ inputImgBase64: string;
146
+ rect: {
147
+ x: number;
148
+ y: number;
149
+ };
150
+ }): Promise<string>;
151
+ declare function savePositionImg(options: {
152
+ inputImgBase64: string;
153
+ rect: {
154
+ x: number;
155
+ y: number;
156
+ };
157
+ outputPath: string;
158
+ }): Promise<void>;
159
+
160
+ export { base64Encoded, base64ToPngFormat, bufferFromBase64, calculateNewDimensions, compositeElementInfoImg, drawBoxOnImage, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, savePositionImg, transformImgPathToBase64, trimImage };
package/dist/es/img.js CHANGED
@@ -1,3 +1,4 @@
1
+ var __pow = Math.pow;
1
2
  var __async = (__this, __arguments, generator) => {
2
3
  return new Promise((resolve, reject) => {
3
4
  var fulfilled = (value) => {
@@ -354,18 +355,61 @@ var processImageElementInfo = (options) => __async(void 0, null, function* () {
354
355
  compositeElementInfoImgWithoutTextBase64
355
356
  };
356
357
  });
358
+
359
+ // src/img/draw-box.ts
360
+ function drawBoxOnImage(options) {
361
+ return __async(this, null, function* () {
362
+ const { inputImgBase64, rect } = options;
363
+ const color = { r: 255, g: 0, b: 0, a: 255 };
364
+ const Jimp = yield getJimp();
365
+ const imageBuffer = yield bufferFromBase64(inputImgBase64);
366
+ const image = yield Jimp.read(imageBuffer);
367
+ const centerX = rect.x;
368
+ const centerY = rect.y;
369
+ const radius = 5;
370
+ image.scan(
371
+ Math.floor(centerX - radius),
372
+ Math.floor(centerY - radius),
373
+ radius * 2,
374
+ radius * 2,
375
+ function(x, y, idx) {
376
+ const distance = Math.sqrt(__pow(x - centerX, 2) + __pow(y - centerY, 2));
377
+ if (distance <= radius) {
378
+ this.bitmap.data[idx + 0] = color.r;
379
+ this.bitmap.data[idx + 1] = color.g;
380
+ this.bitmap.data[idx + 2] = color.b;
381
+ this.bitmap.data[idx + 3] = color.a;
382
+ }
383
+ }
384
+ );
385
+ const resultBase64 = yield image.getBase64Async(Jimp.MIME_PNG);
386
+ return resultBase64;
387
+ });
388
+ }
389
+ function savePositionImg(options) {
390
+ return __async(this, null, function* () {
391
+ const { inputImgBase64, rect, outputPath } = options;
392
+ const imgBase64 = yield drawBoxOnImage({ inputImgBase64, rect });
393
+ yield saveBase64Image({
394
+ base64Data: imgBase64,
395
+ outputPath
396
+ });
397
+ });
398
+ }
357
399
  export {
358
400
  base64Encoded,
359
401
  base64ToPngFormat,
360
402
  bufferFromBase64,
361
403
  calculateNewDimensions,
362
404
  compositeElementInfoImg,
405
+ drawBoxOnImage,
363
406
  imageInfo,
364
407
  imageInfoOfBase64,
365
408
  processImageElementInfo,
366
409
  resizeImg,
367
410
  resizeImgBase64,
368
411
  saveBase64Image,
412
+ savePositionImg,
369
413
  transformImgPathToBase64,
370
414
  trimImage
371
415
  };
package/dist/lib/img.d.ts CHANGED
@@ -141,4 +141,20 @@ declare const processImageElementInfo: (options: {
141
141
  compositeElementInfoImgWithoutTextBase64: string;
142
142
  }>;
143
143
 
144
- export { base64Encoded, base64ToPngFormat, bufferFromBase64, calculateNewDimensions, compositeElementInfoImg, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, transformImgPathToBase64, trimImage };
144
+ declare function drawBoxOnImage(options: {
145
+ inputImgBase64: string;
146
+ rect: {
147
+ x: number;
148
+ y: number;
149
+ };
150
+ }): Promise<string>;
151
+ declare function savePositionImg(options: {
152
+ inputImgBase64: string;
153
+ rect: {
154
+ x: number;
155
+ y: number;
156
+ };
157
+ outputPath: string;
158
+ }): Promise<void>;
159
+
160
+ export { base64Encoded, base64ToPngFormat, bufferFromBase64, calculateNewDimensions, compositeElementInfoImg, drawBoxOnImage, imageInfo, imageInfoOfBase64, processImageElementInfo, resizeImg, resizeImgBase64, saveBase64Image, savePositionImg, transformImgPathToBase64, trimImage };
package/dist/lib/img.js CHANGED
@@ -5,6 +5,7 @@ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
5
  var __getOwnPropNames = Object.getOwnPropertyNames;
6
6
  var __getProtoOf = Object.getPrototypeOf;
7
7
  var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __pow = Math.pow;
8
9
  var __export = (target, all) => {
9
10
  for (var name in all)
10
11
  __defProp(target, name, { get: all[name], enumerable: true });
@@ -55,12 +56,14 @@ __export(img_exports, {
55
56
  bufferFromBase64: () => bufferFromBase64,
56
57
  calculateNewDimensions: () => calculateNewDimensions,
57
58
  compositeElementInfoImg: () => compositeElementInfoImg,
59
+ drawBoxOnImage: () => drawBoxOnImage,
58
60
  imageInfo: () => imageInfo,
59
61
  imageInfoOfBase64: () => imageInfoOfBase64,
60
62
  processImageElementInfo: () => processImageElementInfo,
61
63
  resizeImg: () => resizeImg,
62
64
  resizeImgBase64: () => resizeImgBase64,
63
65
  saveBase64Image: () => saveBase64Image,
66
+ savePositionImg: () => savePositionImg,
64
67
  transformImgPathToBase64: () => transformImgPathToBase64,
65
68
  trimImage: () => trimImage
66
69
  });
@@ -401,6 +404,47 @@ var processImageElementInfo = (options) => __async(void 0, null, function* () {
401
404
  compositeElementInfoImgWithoutTextBase64
402
405
  };
403
406
  });
407
+
408
+ // src/img/draw-box.ts
409
+ function drawBoxOnImage(options) {
410
+ return __async(this, null, function* () {
411
+ const { inputImgBase64, rect } = options;
412
+ const color = { r: 255, g: 0, b: 0, a: 255 };
413
+ const Jimp = yield getJimp();
414
+ const imageBuffer = yield bufferFromBase64(inputImgBase64);
415
+ const image = yield Jimp.read(imageBuffer);
416
+ const centerX = rect.x;
417
+ const centerY = rect.y;
418
+ const radius = 5;
419
+ image.scan(
420
+ Math.floor(centerX - radius),
421
+ Math.floor(centerY - radius),
422
+ radius * 2,
423
+ radius * 2,
424
+ function(x, y, idx) {
425
+ const distance = Math.sqrt(__pow(x - centerX, 2) + __pow(y - centerY, 2));
426
+ if (distance <= radius) {
427
+ this.bitmap.data[idx + 0] = color.r;
428
+ this.bitmap.data[idx + 1] = color.g;
429
+ this.bitmap.data[idx + 2] = color.b;
430
+ this.bitmap.data[idx + 3] = color.a;
431
+ }
432
+ }
433
+ );
434
+ const resultBase64 = yield image.getBase64Async(Jimp.MIME_PNG);
435
+ return resultBase64;
436
+ });
437
+ }
438
+ function savePositionImg(options) {
439
+ return __async(this, null, function* () {
440
+ const { inputImgBase64, rect, outputPath } = options;
441
+ const imgBase64 = yield drawBoxOnImage({ inputImgBase64, rect });
442
+ yield saveBase64Image({
443
+ base64Data: imgBase64,
444
+ outputPath
445
+ });
446
+ });
447
+ }
404
448
  // Annotate the CommonJS export names for ESM import in node:
405
449
  0 && (module.exports = {
406
450
  base64Encoded,
@@ -408,12 +452,14 @@ var processImageElementInfo = (options) => __async(void 0, null, function* () {
408
452
  bufferFromBase64,
409
453
  calculateNewDimensions,
410
454
  compositeElementInfoImg,
455
+ drawBoxOnImage,
411
456
  imageInfo,
412
457
  imageInfoOfBase64,
413
458
  processImageElementInfo,
414
459
  resizeImg,
415
460
  resizeImgBase64,
416
461
  saveBase64Image,
462
+ savePositionImg,
417
463
  transformImgPathToBase64,
418
464
  trimImage
419
465
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@midscene/shared",
3
- "version": "0.8.16",
3
+ "version": "0.8.17-beta-20250103010342.0",
4
4
  "repository": "https://github.com/web-infra-dev/midscene",
5
5
  "homepage": "https://midscenejs.com/",
6
6
  "types": "./src/index.ts",
@@ -0,0 +1,60 @@
1
+ import type { Buffer } from 'node:buffer';
2
+ import type { Rect } from '@/types';
3
+ import getJimp from './get-jimp';
4
+ import { bufferFromBase64 } from './info';
5
+ import { saveBase64Image } from './transform';
6
+
7
+ export async function drawBoxOnImage(options: {
8
+ inputImgBase64: string;
9
+ rect: { x: number; y: number };
10
+ }) {
11
+ const { inputImgBase64, rect } = options;
12
+ const color = { r: 255, g: 0, b: 0, a: 255 }; // Default to red
13
+
14
+ const Jimp = await getJimp();
15
+ const imageBuffer = await bufferFromBase64(inputImgBase64);
16
+ const image = await Jimp.read(imageBuffer);
17
+
18
+ // Draw a circle dot at the center of the rect
19
+ const centerX = rect.x;
20
+ const centerY = rect.y;
21
+ const radius = 5; // Radius of the dot
22
+
23
+ // Scan a square area around the center point
24
+ image.scan(
25
+ Math.floor(centerX - radius),
26
+ Math.floor(centerY - radius),
27
+ radius * 2,
28
+ radius * 2,
29
+ function (x, y, idx) {
30
+ // Calculate distance from current pixel to center
31
+ const distance = Math.sqrt((x - centerX) ** 2 + (y - centerY) ** 2);
32
+
33
+ // If distance is less than radius, color the pixel
34
+ if (distance <= radius) {
35
+ this.bitmap.data[idx + 0] = color.r;
36
+ this.bitmap.data[idx + 1] = color.g;
37
+ this.bitmap.data[idx + 2] = color.b;
38
+ this.bitmap.data[idx + 3] = color.a;
39
+ }
40
+ },
41
+ );
42
+
43
+ // Convert back to base64
44
+ const resultBase64 = await image.getBase64Async(Jimp.MIME_PNG);
45
+ return resultBase64;
46
+ }
47
+
48
+ export async function savePositionImg(options: {
49
+ inputImgBase64: string;
50
+ rect: { x: number; y: number };
51
+ outputPath: string;
52
+ }) {
53
+ const { inputImgBase64, rect, outputPath } = options;
54
+ const imgBase64 = await drawBoxOnImage({ inputImgBase64, rect });
55
+ // console.log('outputPath', outputPath);
56
+ await saveBase64Image({
57
+ base64Data: imgBase64,
58
+ outputPath,
59
+ });
60
+ }
package/src/img/index.ts CHANGED
@@ -14,3 +14,4 @@ export {
14
14
  saveBase64Image,
15
15
  } from './transform';
16
16
  export { processImageElementInfo, compositeElementInfoImg } from './box-select';
17
+ export { drawBoxOnImage, savePositionImg } from './draw-box';