Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
Feature("Bitmask tool").tag("@regress");
 
const assert = require("assert");
 
const IMAGE = "https://data.heartex.net/open-images/train_0/mini/0030019819f25b28.jpg";
 
const config = `<View>
  <Image name="img" value="$image" smoothing="false"></Image>
  <BitmaskLabels name="tag" toName="img">
    <Label value="Test" background="orange"></Label>
  </BitmaskLabels>
</View>`;
 
// Add cleanup hook
Before(async ({ I }) => {
  I.amOnPage("/");
});
 
Scenario("Basic Bitmask drawing", async ({ I, LabelStudio, AtImageView, AtLabels }) => {
  const params = {
    config,
    data: { image: IMAGE },
    annotations: [{ id: 1, result: [] }],
  };
 
  I.amOnPage("/");
  LabelStudio.init(params);
  LabelStudio.waitForObjectsReady();
  await AtImageView.lookForStage();
 
  I.say("Select Bitmask tool");
  I.pressKey("B");
  AtLabels.clickLabel("Test");
 
  I.say("Draw a simple mask");
  AtImageView.drawThroughPoints([
    [20, 20],
    [20, 40],
    [40, 40],
    [40, 20],
    [20, 20],
  ]);
 
  I.say("Check if mask was created");
  const result = await LabelStudio.serialize();
  assert.strictEqual(result.length, 1);
  assert.ok(result[0].value.imageDataURL);
});
 
Scenario("Bitmask eraser functionality", async ({ I, LabelStudio, AtImageView, AtLabels }) => {
  const params = {
    config,
    data: { image: IMAGE },
    annotations: [{ id: 1, result: [] }],
  };
 
  I.amOnPage("/");
  LabelStudio.init(params);
  LabelStudio.waitForObjectsReady();
  await AtImageView.lookForStage();
 
  I.say("Select Bitmask tool and draw initial mask");
  I.pressKey("B");
  AtLabels.clickLabel("Test");
  AtImageView.drawThroughPoints([
    [20, 20],
    [20, 40],
    [40, 40],
    [40, 20],
    [20, 20],
  ]);
 
  I.say("Switch to eraser and erase part of the mask");
  I.pressKey("E");
  AtImageView.drawThroughPoints([
    [25, 25],
    [35, 35],
  ]);
 
  I.say("Check if mask was modified");
  const result = await LabelStudio.serialize();
  assert.strictEqual(result.length, 1);
  assert.ok(result[0].value.imageDataURL);
});
 
Scenario("Bitmask size controls", async ({ I, LabelStudio, AtImageView, AtLabels }) => {
  const params = {
    config,
    data: { image: IMAGE },
    annotations: [{ id: 1, result: [] }],
  };
 
  I.amOnPage("/");
  LabelStudio.init(params);
  LabelStudio.waitForObjectsReady();
  await AtImageView.lookForStage();
 
  I.say("Select Bitmask tool");
  I.pressKey("B");
  AtLabels.clickLabel("Test");
 
  I.say("Test size increase shortcut");
  I.pressKey("]");
  AtImageView.drawThroughPoints([[30, 30]]);
 
  I.say("Test size decrease shortcut");
  I.pressKey("[");
  AtImageView.drawThroughPoints([[50, 50]]);
 
  I.say("Check if masks were created with different sizes");
  const result = await LabelStudio.serialize();
  assert.strictEqual(result.length, 1);
  assert.ok(result[0].value.imageDataURL);
});
 
Scenario("Bitmask hover and selection", async ({ I, LabelStudio, AtImageView, AtLabels, AtOutliner }) => {
  const params = {
    config,
    data: { image: IMAGE },
    annotations: [{ id: 1, result: [] }],
  };
 
  I.amOnPage("/");
  LabelStudio.init(params);
  LabelStudio.waitForObjectsReady();
  await AtImageView.lookForStage();
 
  I.say("Create initial mask");
  I.pressKey("B");
  AtLabels.clickLabel("Test");
  AtImageView.drawThroughPoints([
    [20, 20],
    [20, 40],
    [40, 40],
    [40, 20],
    [20, 20],
  ]);
 
  I.say("Verify selection behavior");
  AtOutliner.seeRegions(1);
 
  I.say("Click on the region");
  AtImageView.clickAt(30, 30);
  AtOutliner.seeSelectedRegion();
});
 
Scenario("Verify Bitmask drawing content", async ({ I, LabelStudio, AtImageView, AtLabels }) => {
  const params = {
    config,
    data: { image: IMAGE },
    annotations: [{ id: 1, result: [] }],
  };
 
  I.amOnPage("/");
  LabelStudio.init(params);
  LabelStudio.waitForObjectsReady();
  await AtImageView.lookForStage();
 
  I.say("Select Bitmask tool");
  I.pressKey("B");
  AtLabels.clickLabel("Test");
 
  I.say("Draw a rectangle mask");
  AtImageView.drawThroughPoints([
    [20, 20],
    [20, 40],
    [40, 40],
    [40, 20],
    [20, 20],
  ]);
 
  I.say("Verify mask content");
  const result = await LabelStudio.serialize();
  assert.strictEqual(result.length, 1);
  assert.ok(result[0].value.imageDataURL);
 
  // Verify that the imageDataURL contains actual pixel data
  const imageData = result[0].value.imageDataURL;
  assert.ok(imageData.startsWith("data:image/png;base64,"));
 
  // Decode base64 and verify it's not empty
  const base64Data = imageData.replace("data:image/png;base64,", "");
  const decodedData = Buffer.from(base64Data, "base64");
  assert.ok(decodedData.length > 0, "Decoded image data should not be empty");
});
 
Scenario("Verify Bitmask pixel content", async ({ I, LabelStudio, AtImageView, AtLabels }) => {
  const params = {
    config,
    data: { image: IMAGE },
    annotations: [{ id: 1, result: [] }],
  };
 
  I.amOnPage("/");
  LabelStudio.init(params);
  LabelStudio.waitForObjectsReady();
  await AtImageView.lookForStage();
 
  I.say("Select Bitmask tool");
  I.pressKey("B");
  AtLabels.clickLabel("Test");
 
  I.say("Draw a rectangle mask");
  AtImageView.drawThroughPoints([
    [20, 20],
    [20, 40],
    [40, 40],
    [40, 20],
    [20, 20],
  ]);
 
  I.say("Verify mask content and dimensions");
  const result = await LabelStudio.serialize();
  assert.strictEqual(result.length, 1);
  assert.ok(result[0].value.imageDataURL);
 
  // Get all data we need before making assertions
  // Retry mechanism to wait for bbox coordinates to be calculated
  let bbox = null;
  let attempts = 0;
  const maxAttempts = 10;
 
  while (!bbox && attempts < maxAttempts) {
    try {
      bbox = await I.executeScript(() => {
        const region = Htx.annotationStore.selected.regions[0];
        if (!region) return null;
        if (!region.bboxCoordsCanvas) return null;
        return region.bboxCoordsCanvas;
      });
 
      if (!bbox) {
        attempts++;
        await I.wait(100); // Wait 100ms before retrying
      }
    } catch (error) {
      attempts++;
      await I.wait(100); // Wait 100ms before retrying
    }
  }
 
  if (!bbox) {
    throw new Error("Bbox coordinates not available after multiple attempts");
  }
 
  // Define thresholds for assertions
  const THRESHOLD = 5;
  const EXPECTED_SIZE = 40;
 
  // Verify that the bbox exists
  assert.ok(bbox, "Bounding box should exist");
 
  // Calculate actual dimensions
  const width = bbox.right - bbox.left;
  const height = bbox.bottom - bbox.top;
 
  // Verify that the bbox has the expected size
  assert.ok(
    Math.abs(width - EXPECTED_SIZE) <= THRESHOLD,
    `Width should be close to ${EXPECTED_SIZE} pixels (got ${width})`,
  );
 
  assert.ok(
    Math.abs(height - EXPECTED_SIZE) <= THRESHOLD,
    `Height should be close to ${EXPECTED_SIZE} pixels (got ${height})`,
  );
 
  // Verify that the bbox is roughly square
  assert.ok(
    Math.abs(width - height) <= THRESHOLD,
    `Width and height should be similar (got width=${width}, height=${height})`,
  );
});
 
Scenario("Verify Bitmask canvas fit", async ({ I, LabelStudio, AtImageView, AtLabels }) => {
  const params = {
    config,
    data: { image: IMAGE },
    annotations: [{ id: 1, result: [] }],
  };
 
  I.amOnPage("/");
  LabelStudio.init(params);
  LabelStudio.waitForObjectsReady();
  await AtImageView.lookForStage();
 
  I.say("Select Bitmask tool");
  I.pressKey("B");
  AtLabels.clickLabel("Test");
 
  I.say("Draw a mask that covers the entire canvas height");
  AtImageView.drawThroughPoints([
    [10, 0], // Start from top
    [10, 100], // Draw to bottom
    [30, 100], // Complete rectangle
    [30, 0], // Back to top
    [10, 0], // Close the path
  ]);
 
  I.say("Verify mask content and image scaling");
  const result = await LabelStudio.serialize();
  assert.strictEqual(result.length, 1);
  assert.ok(result[0].value.imageDataURL);
 
  // Ensure the region is selected
  I.say("Ensure region is selected");
  AtImageView.clickAt(20, 50); // Click in the middle of our drawn region
 
  // Get canvas dimensions and verify the underlying image scaling
  const canvasInfo = await I.executeScript(() => {
    // Get the canvas element
    const canvas = document.querySelector("canvas");
    if (!canvas) throw new Error("Canvas not found");
 
    // Get canvas dimensions
    const canvasWidth = canvas.width;
    const canvasHeight = canvas.height;
 
    // Get the original image element
    const imageElement = document.querySelector("img");
    if (!imageElement) throw new Error("Image element not found");
 
    // Get the original image dimensions
    const imageWidth = imageElement.naturalWidth;
    const imageHeight = imageElement.naturalHeight;
 
    // Get the region and its imageDataURL for verification
    const region = Htx.annotationStore.selected.regions[0];
    if (!region) throw new Error("Region not found");
 
    // For bitmask regions, imageDataURL is stored directly on the region
    const imageDataURL = region.imageDataURL;
    if (!imageDataURL) throw new Error("Region imageDataURL not available");
 
    return {
      canvasWidth,
      canvasHeight,
      imageWidth,
      imageHeight,
      imageDataURL,
    };
  });
 
  // Verify canvas dimensions exist
  assert.ok(canvasInfo.canvasWidth > 0, "Canvas width should be positive");
  assert.ok(canvasInfo.canvasHeight > 0, "Canvas height should be positive");
  assert.ok(canvasInfo.imageWidth > 0, "Image width should be positive");
  assert.ok(canvasInfo.imageHeight > 0, "Image height should be positive");
  assert.ok(canvasInfo.imageDataURL, "Image data URL should exist");
 
  // Verify that the image is properly scaled to fit the canvas
  // The image should be scaled to match the canvas dimensions
  const widthRatio = canvasInfo.canvasWidth / canvasInfo.imageWidth;
  const heightRatio = canvasInfo.canvasHeight / canvasInfo.imageHeight;
 
  // Check that the scaling ratios are reasonable (image should be scaled to fit canvas)
  assert.ok(widthRatio > 0, "Width scaling ratio should be positive");
  assert.ok(heightRatio > 0, "Height scaling ratio should be positive");
 
  // The image should be scaled to fit the canvas, but we don't require uniform scaling
  // as the image might be displayed with different aspect ratios or zoom levels
  // Instead, verify that both dimensions are scaled appropriately
  assert.ok(widthRatio >= 1 || heightRatio >= 1, "At least one dimension should be scaled up to fit canvas");
 
  // Log the scaling information for debugging
  I.say(`Image scaling - Width: ${widthRatio.toFixed(3)}x, Height: ${heightRatio.toFixed(3)}x`);
  I.say(
    `Canvas: ${canvasInfo.canvasWidth}x${canvasInfo.canvasHeight}, Image: ${canvasInfo.imageWidth}x${canvasInfo.imageHeight}`,
  );
 
  // Get the proper canvas and image frame sizes using the helper functions
  const { width: canvasSizeWidth, height: canvasSizeHeight } = await AtImageView.getCanvasSize();
  const { width: imageFrameWidth, height: imageFrameHeight } = await AtImageView.getImageFrameSize();
 
  // Verify that the stage/canvas dimensions match the image frame dimensions
  // The stage should be sized to fit the image properly
  assert.ok(
    Math.abs(canvasSizeWidth - imageFrameWidth) <= 1,
    `Stage width (${canvasSizeWidth}) should match image frame width (${imageFrameWidth}) within 1 pixel`,
  );
 
  assert.ok(
    Math.abs(canvasSizeHeight - imageFrameHeight) <= 1,
    `Stage height (${canvasSizeHeight}) should match image frame height (${imageFrameHeight}) within 1 pixel`,
  );
});