mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-17 00:32:21 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
75fe5b5703 | ||
|
|
b7552fbc64 |
No files matched your search
@@ -0,0 +1,3 @@
|
||||
- Shorten the hitch when the Radio directory loads from about 450 ms to about
|
||||
180 ms. A station's tags and categories are worked out once instead of for
|
||||
every category check (mazy1998, #1709).
|
||||
@@ -1,3 +0,0 @@
|
||||
- Halve the time the satellite cloud image takes to prepare on the 3D Tiles
|
||||
map by applying its transparency curve from a lookup table, in place
|
||||
(mazy1998, #1713).
|
||||
@@ -27,42 +27,52 @@ export function createCategories({
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
function hasTag(station, needles) {
|
||||
const tags = stationTags(station);
|
||||
return needles.some((needle) =>
|
||||
const tagsInclude = (tags, needles) =>
|
||||
needles.some((needle) =>
|
||||
tags.some((tag) => tag === needle || tag.includes(needle)),
|
||||
);
|
||||
|
||||
// A station's tags decide its categories, and neither changes while the
|
||||
// station record lives. Matching used to normalise every tag again for
|
||||
// every needle of every category of every station: about 0.9 s and a
|
||||
// 445 ms freeze per directory load of 750 stations, repeated for the active
|
||||
// filter on every horizon scan. Keyed by the record and its tags value, so
|
||||
// a record given new tags is read again.
|
||||
const membershipByStation = new WeakMap();
|
||||
|
||||
function stationMembership(station) {
|
||||
const keyed = station !== null && typeof station === 'object';
|
||||
const known = keyed ? membershipByStation.get(station) : null;
|
||||
if (known && known.source === station.tags) return known;
|
||||
const tags = stationTags(station);
|
||||
const genres = MUSIC_GENRES.filter(([genre]) =>
|
||||
tagsInclude(tags, [genre]),
|
||||
).map(([genre]) => genre);
|
||||
const categories = new Set(genres.map((genre) => `genre:${genre}`));
|
||||
for (const [id, needles] of Object.entries(CATEGORY_MATCHERS))
|
||||
if (tagsInclude(tags, needles)) categories.add(id);
|
||||
const sorted = categories.size > genres.length;
|
||||
if (genres.length > 0 || tagsInclude(tags, ['music', 'hits', 'songs']))
|
||||
categories.add('music');
|
||||
else if (!sorted) categories.add('other');
|
||||
const membership = { source: station?.tags, tags, genres, categories };
|
||||
if (keyed) membershipByStation.set(station, membership);
|
||||
return membership;
|
||||
}
|
||||
|
||||
function hasTag(station, needles) {
|
||||
return tagsInclude(stationMembership(station).tags, needles);
|
||||
}
|
||||
|
||||
function detectedGenres(station) {
|
||||
return MUSIC_GENRES.filter(([genre]) => hasTag(station, [genre])).map(
|
||||
([genre]) => genre,
|
||||
);
|
||||
return [...stationMembership(station).genres];
|
||||
}
|
||||
|
||||
/** Return whether a station belongs in a station-tag category. */
|
||||
|
||||
function stationMatchesRadioCategory(station, categoryId) {
|
||||
if (categoryId === 'all') return true;
|
||||
if (categoryId.startsWith('genre:')) {
|
||||
return detectedGenres(station).includes(
|
||||
categoryId.slice('genre:'.length),
|
||||
);
|
||||
}
|
||||
if (categoryId === 'music') {
|
||||
return (
|
||||
detectedGenres(station).length > 0 ||
|
||||
hasTag(station, ['music', 'hits', 'songs'])
|
||||
);
|
||||
}
|
||||
if (categoryId === 'other') {
|
||||
return (
|
||||
!Object.entries(CATEGORY_MATCHERS).some(([id]) =>
|
||||
stationMatchesRadioCategory(station, id),
|
||||
) && !stationMatchesRadioCategory(station, 'music')
|
||||
);
|
||||
}
|
||||
return hasTag(station, CATEGORY_MATCHERS[categoryId] || []);
|
||||
return stationMembership(station).categories.has(categoryId);
|
||||
}
|
||||
|
||||
/** Build canonical and detected-genre categories from station-level tags. */
|
||||
|
||||
@@ -0,0 +1,198 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { createCategories } from './categories.js';
|
||||
import { CATEGORY_MATCHERS, MUSIC_GENRES } from './policy.js';
|
||||
|
||||
const categories = createCategories({
|
||||
state: {},
|
||||
services: {},
|
||||
parts: { model: { radioCategoryColor: (id) => `color:${id}` } },
|
||||
source: {},
|
||||
});
|
||||
|
||||
// Category matching as it was before memberships were remembered: every call
|
||||
// normalises the station's tags again. Counts and filters must stay equal.
|
||||
const normalize = (value) =>
|
||||
String(value ?? '')
|
||||
.trim()
|
||||
.toLocaleLowerCase()
|
||||
.replace(/[_-]+/g, ' ')
|
||||
.replace(/\s+/g, ' ')
|
||||
.slice(0, 80);
|
||||
const tagsOf = (station) =>
|
||||
(Array.isArray(station?.tags)
|
||||
? station.tags
|
||||
: String(station?.tags ?? '').split(',')
|
||||
)
|
||||
.map(normalize)
|
||||
.filter(Boolean);
|
||||
const hasTag = (station, needles) => {
|
||||
const tags = tagsOf(station);
|
||||
return needles.some((needle) =>
|
||||
tags.some((tag) => tag === needle || tag.includes(needle)),
|
||||
);
|
||||
};
|
||||
const genresOf = (station) =>
|
||||
MUSIC_GENRES.filter(([genre]) => hasTag(station, [genre])).map(
|
||||
([genre]) => genre,
|
||||
);
|
||||
function matchesByRescan(station, categoryId) {
|
||||
if (categoryId === 'all') return true;
|
||||
if (categoryId.startsWith('genre:'))
|
||||
return genresOf(station).includes(categoryId.slice('genre:'.length));
|
||||
if (categoryId === 'music')
|
||||
return (
|
||||
genresOf(station).length > 0 ||
|
||||
hasTag(station, ['music', 'hits', 'songs'])
|
||||
);
|
||||
if (categoryId === 'other')
|
||||
return (
|
||||
!Object.keys(CATEGORY_MATCHERS).some((id) =>
|
||||
matchesByRescan(station, id),
|
||||
) && !matchesByRescan(station, 'music')
|
||||
);
|
||||
return hasTag(station, CATEGORY_MATCHERS[categoryId] || []);
|
||||
}
|
||||
|
||||
const CATEGORY_IDS = [
|
||||
'all',
|
||||
...Object.keys(CATEGORY_MATCHERS),
|
||||
'music',
|
||||
...MUSIC_GENRES.map(([genre]) => `genre:${genre}`),
|
||||
'other',
|
||||
'genre:not-a-genre',
|
||||
'not-a-category',
|
||||
];
|
||||
|
||||
function seeded(seed) {
|
||||
let state = seed >>> 0;
|
||||
return () => {
|
||||
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
|
||||
return state / 2 ** 32;
|
||||
};
|
||||
}
|
||||
|
||||
/** Stations tagged the way the directory tags them, plus the odd shapes. */
|
||||
function directory(count = 1500) {
|
||||
const random = seeded(11);
|
||||
const vocabulary = [
|
||||
...MUSIC_GENRES.map(([genre]) => genre),
|
||||
...Object.values(CATEGORY_MATCHERS).flat(),
|
||||
'Hip-Hop',
|
||||
'R&B',
|
||||
'Top_Hits',
|
||||
'songs of praise',
|
||||
'community',
|
||||
'college',
|
||||
'varied',
|
||||
'Police Scanner',
|
||||
'FIREFIGHTERS',
|
||||
'metropolitan',
|
||||
'x'.repeat(100),
|
||||
'',
|
||||
' ',
|
||||
];
|
||||
const pick = () => vocabulary[Math.floor(random() * vocabulary.length)];
|
||||
const stations = [];
|
||||
for (let i = 0; i < count; i++) {
|
||||
const tags = Array.from({ length: Math.floor(random() * 6) }, pick);
|
||||
stations.push({
|
||||
id: `station-${i}`,
|
||||
tags: i % 5 === 0 ? tags.join(',') : i % 7 === 0 ? Object.freeze(tags) : tags,
|
||||
});
|
||||
}
|
||||
stations.push(
|
||||
{ id: 'no-tags' },
|
||||
{ id: 'null-tags', tags: null },
|
||||
{ id: 'number-tags', tags: 42 },
|
||||
Object.freeze({ id: 'frozen', tags: Object.freeze(['Jazz', 'News']) }),
|
||||
null,
|
||||
undefined,
|
||||
'pop',
|
||||
);
|
||||
return stations;
|
||||
}
|
||||
|
||||
test('remembered memberships give the counts and filters of a full rescan', () => {
|
||||
const stations = directory();
|
||||
for (const id of CATEGORY_IDS) {
|
||||
const expected = stations.filter((station) => matchesByRescan(station, id));
|
||||
assert.deepEqual(
|
||||
categories.filterRadioStations(stations, id),
|
||||
expected,
|
||||
`filter ${id}`,
|
||||
);
|
||||
for (const station of stations)
|
||||
assert.equal(
|
||||
categories.stationMatchesRadioCategory(station, id),
|
||||
matchesByRescan(station, id),
|
||||
);
|
||||
}
|
||||
const built = categories.buildRadioCategories(stations);
|
||||
assert.deepEqual(
|
||||
built.map(({ id, count }) => [id, count]),
|
||||
CATEGORY_IDS.filter(
|
||||
(id) =>
|
||||
!id.startsWith('genre:') ||
|
||||
stations.some((station) => matchesByRescan(station, id)),
|
||||
)
|
||||
.filter((id) => id !== 'not-a-category')
|
||||
.map((id) => [
|
||||
id,
|
||||
stations.filter((station) => matchesByRescan(station, id)).length,
|
||||
]),
|
||||
);
|
||||
assert.ok(built.find(({ id }) => id === 'other').count > 0);
|
||||
assert.ok(built.find(({ id }) => id === 'genre:hip hop').count > 0);
|
||||
for (const station of stations) {
|
||||
assert.deepEqual(categories.detectedGenres(station), genresOf(station));
|
||||
assert.deepEqual(categories.stationTags(station), tagsOf(station));
|
||||
}
|
||||
});
|
||||
|
||||
test('a station has its tags normalised once, however often it is matched', () => {
|
||||
let reads = 0;
|
||||
const tag = (text) => ({
|
||||
toString() {
|
||||
reads++;
|
||||
return text;
|
||||
},
|
||||
});
|
||||
const stations = [
|
||||
{ id: 'a', tags: [tag('Jazz'), tag('News')] },
|
||||
{ id: 'b', tags: [tag('community')] },
|
||||
];
|
||||
categories.buildRadioCategories(stations);
|
||||
for (const id of CATEGORY_IDS) categories.filterRadioStations(stations, id);
|
||||
// A horizon scan asks once per station for the active filter.
|
||||
for (let scan = 0; scan < 50; scan++)
|
||||
for (const station of stations)
|
||||
categories.stationMatchesRadioCategory(station, 'news');
|
||||
assert.equal(reads, 3, 'one normalisation per tag');
|
||||
assert.deepEqual(
|
||||
categories.filterRadioStations(stations, 'genre:jazz').map(({ id }) => id),
|
||||
['a'],
|
||||
);
|
||||
});
|
||||
|
||||
test('a station record given new tags is matched on the new tags', () => {
|
||||
const station = { id: 'a', tags: ['jazz'] };
|
||||
assert.equal(categories.stationMatchesRadioCategory(station, 'music'), true);
|
||||
assert.equal(categories.stationMatchesRadioCategory(station, 'news'), false);
|
||||
station.tags = ['news'];
|
||||
assert.equal(categories.stationMatchesRadioCategory(station, 'music'), false);
|
||||
assert.equal(categories.stationMatchesRadioCategory(station, 'news'), true);
|
||||
assert.deepEqual(categories.detectedGenres(station), []);
|
||||
station.tags = 'community';
|
||||
assert.equal(categories.stationMatchesRadioCategory(station, 'other'), true);
|
||||
});
|
||||
|
||||
test('callers cannot alter a remembered genre list', () => {
|
||||
const station = { id: 'a', tags: ['jazz', 'soul'] };
|
||||
categories.detectedGenres(station).push('rock');
|
||||
assert.deepEqual(categories.detectedGenres(station), ['jazz', 'soul']);
|
||||
assert.equal(
|
||||
categories.stationMatchesRadioCategory(station, 'genre:rock'),
|
||||
false,
|
||||
);
|
||||
});
|
||||
@@ -1,29 +1,22 @@
|
||||
export const INFRARED_ALPHA_T0 = 0.4;
|
||||
export const INFRARED_ALPHA_T1 = 0.7;
|
||||
|
||||
function infraredAmount(strength) {
|
||||
return strength === 'full'
|
||||
? 0
|
||||
: strength === 'filtered' || !Number.isFinite(strength)
|
||||
? 1
|
||||
: Math.max(0, Math.min(100, strength)) / 100;
|
||||
}
|
||||
|
||||
/** Scale each pixel's alpha in place; RGB is left as it is. The scale depends
|
||||
* only on the brightest channel, so it is worked out once for each of the 256
|
||||
* byte values instead of once per pixel: a 4096×2048 image has 8.4 million
|
||||
* pixels and the power curve per pixel held the page for over 100 ms. The
|
||||
* table holds the same numbers the per-pixel formula produced, so the output
|
||||
* bytes are unchanged.
|
||||
/** Return display pixels without changing RGB or the source RGBA bytes.
|
||||
* `strength` is 0–100: zero is the full image and 100 is the cloud key.
|
||||
*/
|
||||
export function applyInfraredAlpha(pixels, strength = 'filtered') {
|
||||
const amount = infraredAmount(strength);
|
||||
export function infraredAlpha(rgba, strength = 'filtered') {
|
||||
const pixels = new Uint8ClampedArray(rgba);
|
||||
const amount =
|
||||
strength === 'full'
|
||||
? 0
|
||||
: strength === 'filtered' || !Number.isFinite(strength)
|
||||
? 1
|
||||
: Math.max(0, Math.min(100, strength)) / 100;
|
||||
if (amount === 0) return pixels;
|
||||
const scale = new Float64Array(256);
|
||||
for (let value = 0; value < 256; value++) {
|
||||
for (let i = 0; i < pixels.length; i += 4) {
|
||||
// Match Cesium's czm_srgbToLinear conversion in draped imagery.
|
||||
const linearMax = (value / 255) ** 2.2;
|
||||
const linearMax =
|
||||
(Math.max(rgba[i], rgba[i + 1], rgba[i + 2]) / 255) ** 2.2;
|
||||
const t = Math.max(
|
||||
0,
|
||||
Math.min(
|
||||
@@ -33,21 +26,7 @@ export function applyInfraredAlpha(pixels, strength = 'filtered') {
|
||||
),
|
||||
);
|
||||
const filtered = t * t * (3 - 2 * t);
|
||||
scale[value] = 1 - amount + filtered * amount;
|
||||
}
|
||||
for (let i = 0; i < pixels.length; i += 4) {
|
||||
const r = pixels[i];
|
||||
const g = pixels[i + 1];
|
||||
const b = pixels[i + 2];
|
||||
const max = r > g ? (r > b ? r : b) : g > b ? g : b;
|
||||
pixels[i + 3] = pixels[i + 3] * scale[max];
|
||||
pixels[i + 3] = rgba[i + 3] * (1 - amount + filtered * amount);
|
||||
}
|
||||
return pixels;
|
||||
}
|
||||
|
||||
/** Return display pixels without changing RGB or the source RGBA bytes.
|
||||
* `strength` is 0–100: zero is the full image and 100 is the cloud key.
|
||||
*/
|
||||
export function infraredAlpha(rgba, strength = 'filtered') {
|
||||
return applyInfraredAlpha(new Uint8ClampedArray(rgba), strength);
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {
|
||||
applyInfraredAlpha,
|
||||
infraredAlpha,
|
||||
INFRARED_ALPHA_T0,
|
||||
INFRARED_ALPHA_T1,
|
||||
@@ -62,43 +61,3 @@ test('full infrared preserves every channel including partial and zero alpha', (
|
||||
]);
|
||||
assert.deepEqual(infraredAlpha(pixels, 'full'), pixels);
|
||||
});
|
||||
|
||||
test('the lookup table gives the per-pixel curve byte for byte, in place', () => {
|
||||
// The transfer as it was written per pixel, before the table.
|
||||
const perPixel = (rgba, amount) => {
|
||||
const out = new Uint8ClampedArray(rgba);
|
||||
for (let i = 0; i < out.length; i += 4) {
|
||||
const linearMax =
|
||||
(Math.max(rgba[i], rgba[i + 1], rgba[i + 2]) / 255) ** 2.2;
|
||||
const t = Math.max(
|
||||
0,
|
||||
Math.min(
|
||||
1,
|
||||
(linearMax - INFRARED_ALPHA_T0) /
|
||||
(INFRARED_ALPHA_T1 - INFRARED_ALPHA_T0),
|
||||
),
|
||||
);
|
||||
out[i + 3] = rgba[i + 3] * (1 - amount + t * t * (3 - 2 * t) * amount);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
// Every brightest-channel value in each channel, against a spread of alphas.
|
||||
const bytes = [];
|
||||
for (let value = 0; value < 256; value++)
|
||||
for (const alpha of [0, 1, 37, 128, 200, 254, 255]) {
|
||||
bytes.push(value, 0, 0, alpha);
|
||||
bytes.push(value >> 1, value, 0, alpha);
|
||||
bytes.push(0, value >> 2, value, alpha);
|
||||
}
|
||||
const source = new Uint8ClampedArray(bytes);
|
||||
for (const strength of [1, 33, 50, 99, 100, 'filtered', Number.NaN]) {
|
||||
const amount = Number.isFinite(strength) ? strength / 100 : 1;
|
||||
const pixels = source.slice();
|
||||
assert.equal(applyInfraredAlpha(pixels, strength), pixels);
|
||||
assert.deepEqual(pixels, perPixel(source, amount), `strength ${strength}`);
|
||||
}
|
||||
for (const strength of [0, 'full']) {
|
||||
const pixels = source.slice();
|
||||
assert.deepEqual(applyInfraredAlpha(pixels, strength), source);
|
||||
}
|
||||
});
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
drawWeatherPixels,
|
||||
closeWeatherPixels,
|
||||
} from './source.js';
|
||||
import { applyInfraredAlpha } from './infraredAlpha.js';
|
||||
import { infraredAlpha } from './infraredAlpha.js';
|
||||
|
||||
export const MAX_MOSAIC_BYTES = 4 * 1024 * 1024;
|
||||
|
||||
@@ -31,8 +31,7 @@ export function processInfraredImage(
|
||||
|
||||
function applyInfraredTransfer(context, canvas, mode) {
|
||||
const pixels = context.getImageData(0, 0, canvas.width, canvas.height);
|
||||
// In place: a copy of a 4096×2048 image is 33 MB, made and written back.
|
||||
applyInfraredAlpha(pixels.data, mode);
|
||||
pixels.data.set(infraredAlpha(pixels.data, mode));
|
||||
context.putImageData(pixels, 0, 0);
|
||||
}
|
||||
|
||||
|
||||
@@ -26,28 +26,6 @@ function canvas() {
|
||||
return output;
|
||||
}
|
||||
|
||||
test('the transfer is applied to the canvas pixels in place, with no copy written back', () => {
|
||||
let read = null;
|
||||
const output = {};
|
||||
output.getContext = () => ({
|
||||
drawImage() {},
|
||||
getImageData: () => {
|
||||
read = { data: new Uint8ClampedArray([194, 0, 0, 200]) };
|
||||
// A second full-size buffer copied over the first is what this guards.
|
||||
read.data.set = () => {
|
||||
throw new Error('the canvas pixels were overwritten from a copy');
|
||||
};
|
||||
return read;
|
||||
},
|
||||
putImageData: (pixels) => {
|
||||
output.pixels = pixels;
|
||||
},
|
||||
});
|
||||
processInfraredImage({ width: 1, height: 1 }, 'filtered', () => output);
|
||||
assert.equal(output.pixels, read);
|
||||
assert.deepEqual([...read.data], [194, 0, 0, 98]);
|
||||
});
|
||||
|
||||
test('decoded pixels are processed once on a same-sized canvas', () => {
|
||||
const image = { width: 1, height: 1 };
|
||||
const output = processInfraredImage(image, 'filtered', canvas);
|
||||
|
||||
Reference in new issue
Block a user