mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-02 09:22:15 +00:00
feat(photos): a photo knows its colours and its calm areas
Photo signals move to version 3: a deterministic palette, a busy grid and the point of most detail, stored as measured values, never as image bytes.
This commit is contained in:
@@ -2,7 +2,8 @@
|
||||
* `TrekPhotoSignalsRepository` (PSIGREPO-*): one measurement per photo,
|
||||
* replaced by the next, gone with its photo; read back for many photos at once
|
||||
* past SQLite's bound-value limit; and the gallery facts the book photo picker
|
||||
* starts from, in gallery order and for one journey only.
|
||||
* starts from, in gallery order and for one journey only; the colour signals
|
||||
* (palette, busy grid, salient point) of the newest measurement only.
|
||||
*/
|
||||
import { JourneyPhotos } from '../../../../src/db/entities/JourneyPhotos.entity';
|
||||
import { TrekPhotoSignals } from '../../../../src/db/entities/TrekPhotoSignals.entity';
|
||||
@@ -144,4 +145,17 @@ describe('TrekPhotoSignalsRepository', () => {
|
||||
expect.objectContaining({ photo_id: clip, media_type: 'video' }),
|
||||
]);
|
||||
});
|
||||
|
||||
it('PSIGREPO-005: the colours, the busy grid and the salient point read back, and a later measurement without them clears them', async () => {
|
||||
const { user } = await makeUser(t);
|
||||
const photoId = await trekPhoto(user.id);
|
||||
const colours = { palette: '["#336699:0.6","#f0e0d0:0.4"]', busy: 'f'.repeat(64), sal_x: 0.25, sal_y: 0.75 };
|
||||
await signals.saveSignals(measured(photoId, { algo: 3, ...colours }));
|
||||
expect(await signals.listForPhotos([photoId])).toEqual([expect.objectContaining({ algo: 3, ...colours })]);
|
||||
|
||||
await signals.saveSignals(measured(photoId, { algo: 2 }));
|
||||
expect(await signals.listForPhotos([photoId])).toEqual([
|
||||
expect.objectContaining({ algo: 2, palette: null, busy: null, sal_x: null, sal_y: null }),
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
/**
|
||||
* The colour signals of a photo (PHSIG3-*), on pixels drawn here: a picture
|
||||
* half flat and half textured has its busy cells and its salient point on
|
||||
* the textured side, a two-colour picture has both colours in its palette
|
||||
* with their shares, a flat one has one colour, no salient point and a calm
|
||||
* grid; the measurement of a whole photo carries them, at version 3.
|
||||
*/
|
||||
import { colourSignals, edgeSignals, paletteOf } from '../../../../src/nest/photos/photo-colour-signals';
|
||||
import { measurePhoto, PHOTO_SIGNALS_ALGO } from '../../../../src/nest/photos/photo-signals-runner';
|
||||
|
||||
import { Jimp } from 'jimp';
|
||||
import { describe, expect, it } from 'vitest';
|
||||
|
||||
type Rgb = [number, number, number];
|
||||
|
||||
/** RGBA pixels of `width` x `height`, each drawn by `paint`. */
|
||||
function pixels(width: number, height: number, paint: (x: number, y: number) => Rgb): Uint8Array {
|
||||
const data = new Uint8Array(width * height * 4);
|
||||
for (let y = 0; y < height; y++) {
|
||||
for (let x = 0; x < width; x++) {
|
||||
const [r, g, b] = paint(x, y);
|
||||
data.set([r, g, b, 255], (y * width + x) * 4);
|
||||
}
|
||||
}
|
||||
return data;
|
||||
}
|
||||
|
||||
/** Flat grey on the left half, a two-pixel black and white check on the right. */
|
||||
const halfBusy = (x: number, y: number): Rgb => {
|
||||
if (x < 64) return [128, 128, 128];
|
||||
const light = (Math.floor(x / 2) + Math.floor(y / 2)) % 2 === 0;
|
||||
return light ? [255, 255, 255] : [0, 0, 0];
|
||||
};
|
||||
|
||||
describe('colour signals', () => {
|
||||
it('PHSIG3-001: the busy cells and the salient point sit on the textured half', () => {
|
||||
const { busy, salX, salY } = edgeSignals(pixels(128, 96, halfBusy), 128, 96);
|
||||
expect(busy).toMatch(/^[0-9a-f]{64}$/);
|
||||
const rows = Array.from({ length: 8 }, (_, r) => busy?.slice(r * 8, r * 8 + 8) ?? '');
|
||||
for (const row of rows) {
|
||||
expect(row.slice(0, 3)).toBe('000');
|
||||
expect(row.slice(5)).toBe('fff');
|
||||
}
|
||||
expect(salX).toBeGreaterThan(0.6);
|
||||
expect(salY).toBeGreaterThan(0.4);
|
||||
expect(salY).toBeLessThan(0.6);
|
||||
});
|
||||
|
||||
it('PHSIG3-002: a two-colour picture has both colours with their shares, the larger first', () => {
|
||||
const palette = paletteOf(
|
||||
pixels(100, 100, (x) => (x < 70 ? [200, 30, 30] : [20, 40, 220])),
|
||||
100,
|
||||
100,
|
||||
);
|
||||
expect(palette).toEqual(['#c81e1e:0.70', '#1428dc:0.30']);
|
||||
});
|
||||
|
||||
it('PHSIG3-003: a flat picture has one colour, a calm grid and no salient point', () => {
|
||||
const flat = colourSignals(
|
||||
pixels(40, 30, () => [10, 200, 120]),
|
||||
40,
|
||||
30,
|
||||
);
|
||||
expect(flat).toEqual({ palette: ['#0ac878:1.00'], busy: '0'.repeat(64), salX: null, salY: null });
|
||||
expect(paletteOf(new Uint8Array(0), 0, 0)).toBeNull();
|
||||
});
|
||||
|
||||
it('PHSIG3-004: a measured photo carries its colours, its grid and its salient point, at version 3', async () => {
|
||||
const image = new Jimp({ width: 256, height: 192, color: 0xffffffff });
|
||||
image.bitmap.data.set(pixels(256, 192, (x, y) => halfBusy(Math.floor(x / 2), Math.floor(y / 2))));
|
||||
const measured = await measurePhoto(new Uint8Array(await image.getBuffer('image/png')));
|
||||
expect(PHOTO_SIGNALS_ALGO).toBe(3);
|
||||
expect(measured?.palette?.length).toBeGreaterThan(0);
|
||||
expect(measured?.palette?.[0]).toMatch(/^#[0-9a-f]{6}:(0|1)\.\d{2}$/);
|
||||
expect(measured?.busy).toMatch(/^[0-9a-f]{64}$/);
|
||||
expect(measured?.salX).toBeGreaterThan(0.6);
|
||||
});
|
||||
});
|
||||
@@ -299,4 +299,29 @@ describe('PhotoSignalsService, the line', () => {
|
||||
expect(byPhoto.get(unsized)).toMatchObject({ origWidth: null, origHeight: null });
|
||||
expect(byPhoto.get(unsized)?.sharpness).toEqual(expect.any(Number));
|
||||
});
|
||||
|
||||
it('PHSVC-011: the book engine reads the original size, brightness, colours, busy grid and salient point of a photo', async () => {
|
||||
const { ids } = await gallery(['image', 'image', 'image']);
|
||||
const [measured, older, broken] = ids as [number, number, number];
|
||||
resolver.readForSignals.mockResolvedValue({ kind: 'original', bytes: photo });
|
||||
await main.signals.signalsFor([measured]);
|
||||
await insertRow(orm, TrekPhotoSignals, { photo: older, algo: PHOTO_SIGNALS_ALGO - 1, orig_width: 800, luma: 0.5 });
|
||||
await insertRow(orm, TrekPhotoSignals, {
|
||||
photo: broken,
|
||||
algo: PHOTO_SIGNALS_ALGO,
|
||||
palette: 'not json',
|
||||
busy: 'abc',
|
||||
});
|
||||
|
||||
const book = await main.signals.bookSignals([measured, older, broken, measured]);
|
||||
|
||||
const own = book.get(measured);
|
||||
expect(own).toMatchObject({ width: 300, height: 200, luma: 128, busy: '0'.repeat(64) });
|
||||
expect(own?.palette).toEqual([expect.stringMatching(/^#[0-9a-f]{6}:1\.00$/)]);
|
||||
expect(own?.salient).toBeUndefined();
|
||||
// An older measurement still knows the size, never the colours.
|
||||
expect(book.get(older)).toEqual({ width: 800, height: null, luma: 128 });
|
||||
expect(book.get(broken)).toEqual({ width: null, height: null });
|
||||
expect(await main.signals.bookSignals([])).toEqual(new Map());
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user