diff --git a/src/images.js b/src/images.js index fd68568..ee0e166 100644 --- a/src/images.js +++ b/src/images.js @@ -119,3 +119,58 @@ export function grabScreenFrame(cap) { canvas.getContext('2d').drawImage(v, 0, 0); return new Promise((resolve, reject) => canvas.toBlob((b) => (b ? resolve(b) : reject(new Error('Could not grab the picture.'))), 'image/png')); } + +// ---- annotation helpers (issue #25), pure ---- +// Marks sit on a frame's picture as fractions of its width and height (0..1), so they +// stay in place however the picture is scaled: +// { type: 'marker', x, y } a numbered circle centred on the point +// { type: 'box', x, y, w, h } a highlight rectangle (x, y = top-left) +// Blur is NOT a mark: it is burned into the picture's pixels (pixelateRegion), so no +// unblurred copy is kept. +export const MARK_TYPES = ['marker', 'box']; +const clamp01 = (v) => Math.min(1, Math.max(0, v)); + +// A rectangle from two corners (any order), clamped to the picture. null if it is +// too small to mean anything (a stray click). +export function normRect(x0, y0, x1, y1, min = 0.01) { + const ax = clamp01(Math.min(x0, x1)), ay = clamp01(Math.min(y0, y1)); + const bx = clamp01(Math.max(x0, x1)), by = clamp01(Math.max(y0, y1)); + const w = bx - ax, h = by - ay; + return w < min || h < min ? null : { x: ax, y: ay, w, h }; +} + +export function isMark(m) { + if (!m || typeof m !== 'object' || !MARK_TYPES.includes(m.type)) return false; + const frac = (v) => typeof v === 'number' && v >= 0 && v <= 1; + if (!frac(m.x) || !frac(m.y)) return false; + if (m.type === 'marker') return true; + return frac(m.w) && frac(m.h) && m.w > 0 && m.h > 0 && m.x + m.w <= 1.0001 && m.y + m.h <= 1.0001; +} + +// Block size for blurring a region: coarse enough that small text cannot be read back. +export function blurBlock(w, h) { + return Math.max(12, Math.round(Math.min(w, h) / 3)); +} + +// Replace each block of rect {x,y,w,h} (pixels) in RGBA `data` (width x height) with its +// average colour, in place. Pixels outside the rectangle are untouched. +export function pixelateRegion(data, width, height, rect, block = blurBlock(rect.w, rect.h)) { + const x0 = Math.max(0, Math.floor(rect.x)), y0 = Math.max(0, Math.floor(rect.y)); + const x1 = Math.min(width, Math.ceil(rect.x + rect.w)), y1 = Math.min(height, Math.ceil(rect.y + rect.h)); + for (let by = y0; by < y1; by += block) { + for (let bx = x0; bx < x1; bx += block) { + const ex = Math.min(bx + block, x1), ey = Math.min(by + block, y1); + let r = 0, g = 0, b = 0, a = 0, n = 0; + for (let y = by; y < ey; y++) for (let x = bx; x < ex; x++) { + const i = (y * width + x) * 4; + r += data[i]; g += data[i + 1]; b += data[i + 2]; a += data[i + 3]; n++; + } + if (!n) continue; + r = Math.round(r / n); g = Math.round(g / n); b = Math.round(b / n); a = Math.round(a / n); + for (let y = by; y < ey; y++) for (let x = bx; x < ex; x++) { + const i = (y * width + x) * 4; + data[i] = r; data[i + 1] = g; data[i + 2] = b; data[i + 3] = a; + } + } + } +} diff --git a/src/model.js b/src/model.js index 0af4931..d56e20a 100644 --- a/src/model.js +++ b/src/model.js @@ -1,4 +1,4 @@ -import { isImageDataUrl, MAX_IMAGE_DATA_CHARS } from './images.js'; +import { isImageDataUrl, MAX_IMAGE_DATA_CHARS, isMark } from './images.js'; // Data model for the document (plaintext JSON before encryption). // Pure functions, no DOM; runs in browsers and Node. build.js strips `export`. @@ -32,6 +32,8 @@ import { isImageDataUrl, MAX_IMAGE_DATA_CHARS } from './images.js'; // Frame: { image: PNG/JPEG data URL or '', text, layout: 'right'|'under'|'none', secret?: boolean, // ifStuck?: string, what to do if the screen looks different or the step fails // stop?: boolean, "stop here and call someone" marker; ifStuck says who/why +// marks?: [Mark] overlay on the picture: { type:'marker', x, y } or +// { type:'box', x, y, w, h }, all fractions 0..1 of the picture // uses?: [{ record: , field: }] credentials this step needs, // shown beside the step with Show/Hide and Copy. A reference // that no longer resolves is shown as missing, never an error. @@ -210,6 +212,10 @@ export function validate(doc) { if (f.secret !== undefined && typeof f.secret !== 'boolean') errs.push(`${fw}: secret must be a boolean`); if (f.ifStuck !== undefined && !isStr(f.ifStuck)) errs.push(`${fw}: ifStuck must be a string`); if (f.stop !== undefined && typeof f.stop !== 'boolean') errs.push(`${fw}: stop must be a boolean`); + if (f.marks !== undefined) { + if (!Array.isArray(f.marks)) errs.push(`${fw}: marks must be an array`); + else f.marks.forEach((m, n) => { if (!isMark(m)) errs.push(`${fw}.marks[${n}]: not a valid marker or box`); }); + } if (f.uses !== undefined) { if (!Array.isArray(f.uses)) errs.push(`${fw}: uses must be an array`); else f.uses.forEach((u, m) => { diff --git a/src/ops.js b/src/ops.js index cad042f..cc37738 100644 --- a/src/ops.js +++ b/src/ops.js @@ -184,7 +184,7 @@ export function allSequences(doc) { // and the secret flags stay). Returns how many values were erased. export function clearSecrets(doc) { let n = 0; - const frames = (q) => { for (const f of q.frames) if (f.secret && (f.image || String(f.text || '').trim() || String(f.ifStuck || '').trim())) { f.image = ''; f.text = ''; if (f.ifStuck) f.ifStuck = ''; n++; } }; + const frames = (q) => { for (const f of q.frames) if (f.secret && (f.image || String(f.text || '').trim() || String(f.ifStuck || '').trim())) { f.image = ''; delete f.marks; f.text = ''; if (f.ifStuck) f.ifStuck = ''; n++; } }; for (const s of doc.sections) { for (const r of s.records || []) { if (isSequence(r)) frames(r); diff --git a/src/version.js b/src/version.js index 2ab639c..247904c 100644 --- a/src/version.js +++ b/src/version.js @@ -2,7 +2,7 @@ // YYMMDD is the date, X the iteration that day (A first, B second, ...). // Bump with `npm run bump` (see bump-version.js). build.js strips `export`. -export const APP_VERSION = '261124D'; +export const APP_VERSION = '261124E'; const VERSION_RE = /^(\d{6})([A-Z])$/; export const isVersion = (v) => typeof v === 'string' && VERSION_RE.test(v); diff --git a/test/images.test.js b/test/images.test.js index 051a365..e3eef05 100644 --- a/test/images.test.js +++ b/test/images.test.js @@ -86,3 +86,41 @@ test('displayValue for images: masked when secret, never the data itself', () => assert.equal(displayValue({ ...img, value: '' }, false), '(no image)'); assert.equal(countSecrets(docWithField({ ...img, secret: true })), 1); }); + +import { normRect, isMark, blurBlock, pixelateRegion } from '../src/images.js'; + +test('normRect orders corners, clamps to the picture, drops stray clicks', () => { + assert.deepEqual(normRect(0.5, 0.6, 0.2, 0.1), { x: 0.2, y: 0.1, w: 0.3, h: 0.5 }); + const c = normRect(-1, -1, 2, 0.5); + assert.deepEqual([c.x, c.y, c.w, c.h], [0, 0, 1, 0.5]); + assert.equal(normRect(0.5, 0.5, 0.503, 0.9), null); +}); + +test('isMark accepts markers and boxes inside the picture only', () => { + assert.equal(isMark({ type: 'marker', x: 0.5, y: 0.5 }), true); + assert.equal(isMark({ type: 'box', x: 0.1, y: 0.1, w: 0.5, h: 0.5 }), true); + assert.equal(isMark({ type: 'box', x: 0.6, y: 0.1, w: 0.5, h: 0.5 }), false); // runs off the right edge + assert.equal(isMark({ type: 'box', x: 0.1, y: 0.1, w: 0, h: 0.5 }), false); + assert.equal(isMark({ type: 'marker', x: 2, y: 0.5 }), false); + assert.equal(isMark({ type: 'blur', x: 0.1, y: 0.1, w: 0.2, h: 0.2 }), false); + assert.equal(isMark(null), false); +}); + +test('pixelateRegion averages blocks inside the rectangle and leaves the rest alone', () => { + const W = 40, H = 30; + const data = new Uint8ClampedArray(W * H * 4); + for (let y = 0; y < H; y++) for (let x = 0; x < W; x++) { // a pattern with detail in every pixel + const i = (y * W + x) * 4; data[i] = (x * 37 + y * 11) % 256; data[i + 1] = (x * 5) % 256; data[i + 2] = (y * 9) % 256; data[i + 3] = 255; + } + const before = Uint8ClampedArray.from(data); + pixelateRegion(data, W, H, { x: 10, y: 5, w: 24, h: 20 }, 12); + const px = (a, x, y) => [...a.slice((y * W + x) * 4, (y * W + x) * 4 + 4)].join(); + assert.equal(px(data, 10, 5), px(data, 21, 16)); // same 12x12 block, same colour + assert.notEqual(px(data, 10, 5), px(data, 22, 5)); // next block differs + assert.notEqual(px(data, 12, 7), px(before, 12, 7)); // detail is gone + assert.equal(px(data, 9, 5), px(before, 9, 5)); // left of the rectangle: untouched + assert.equal(px(data, 34, 5), px(before, 34, 5)); // right of it + assert.equal(px(data, 15, 25), px(before, 15, 25)); // below it + // total of each channel inside a block is preserved up to rounding (it is an average) + assert.ok(blurBlock(300, 14) >= 12 && blurBlock(900, 600) === 200); +}); diff --git a/test/model.test.js b/test/model.test.js index 93297e0..4851391 100644 --- a/test/model.test.js +++ b/test/model.test.js @@ -104,6 +104,10 @@ test('sequence validation: layout, image, text, secret, id, title', () => { bad((x) => { x.frames[0].image = 5; }, /image must be a string/); bad((x) => { x.frames[0].text = null; }, /text must be a string/); bad((x) => { x.frames[0].secret = 'yes'; }, /secret must be a boolean/); + bad((x) => { x.frames[0].marks = 'no'; }, /marks must be an array/); + bad((x) => { x.frames[0].marks = [{ type: 'box', x: 0.9, y: 0, w: 0.5, h: 0.5 }]; }, /not a valid marker or box/); + const okm = structuredClone(d); okm.sections[0].records[1].frames[0].marks = [{ type: 'marker', x: 0.3, y: 0.4 }, { type: 'box', x: 0.1, y: 0.1, w: 0.2, h: 0.2 }]; + assert.deepEqual(validate(okm), []); bad((x) => { x.frames[0].ifStuck = 5; }, /ifStuck must be a string/); bad((x) => { x.frames[0].stop = 'yes'; }, /stop must be a boolean/); bad((x) => { x.captured = 'yesterday'; }, /captured must be YYYY-MM-DD/); diff --git a/test/ops.test.js b/test/ops.test.js index d2f1afe..9c4e004 100644 --- a/test/ops.test.js +++ b/test/ops.test.js @@ -356,8 +356,9 @@ test('addCapture adds a frame and stamps the sequence captured date; clearSecret assert.equal(q.frames.length, 1); assert.equal(f.layout, 'under'); assert.match(q.captured, /^\d{4}-\d{2}-\d{2}$/); - f.secret = true; f.ifStuck = 'call 555-0100'; f.stop = true; + f.secret = true; f.ifStuck = 'call 555-0100'; f.stop = true; f.image = 'data:image/png;base64,AAAA'; f.marks = [{ type: 'marker', x: 0.5, y: 0.5 }]; assert.equal(clearSecrets(doc), 1); + assert.equal(f.marks, undefined); assert.equal(f.ifStuck, ''); assert.equal(f.stop, true); assert.deepEqual(validate(doc), []);