Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 162x 162x 24x 138x 138x 138x 138x 138x 138x 262144x 138x 138x 138x 138x 31x 31x 3x 28x 28x 28x 28x 28x 28x 720896x 28x 28x 28x 28x 13x 185x 185x 162x 158x 27x 27x 14x 26x 26x 26624x 26624x 26x 26x 26x 26624x 27x 27x 27x 27x 27x 27x 27x 27x 21x 10x 11x 11x 27x 43x 43x 43x 27x 16x 32x 16x 16x 16x 16x 16x 11x 11x 11x 11x 10x 10x 10x 11x 11x 11x 11x 11x 7x 7x 7x 7x 7x 7x 7x 14x 7x 7x 7x 7x 7x 7x 11x 8x 8x 8x 8x 8x 8x 202x 202x 202x 202x 202x 5050x 5050x 202x 4848x 4848x 202x 202x 202x 202x 202x 202x 202x 202x 4848x 202x | import { getANSIFrameAlpha } from './bga-alpha.ts';
import { applyBMSColorKey, type BmsARGB } from '@be-music/chart-model';
import { readFile } from 'node:fs/promises';
import { extname } from 'node:path';
import { isAbortError, throwIfAborted } from '@be-music/shared-utils/core';
import { resolveFirstExistingPath } from '@be-music/shared-utils/path';
import { decodeVideoFramesStream, decodeVideoFramesToSourceFramesInWorker } from './bga-video.ts';
import { DEFAULT_IMAGE_RESIZE_ALGORITHM, type ImageResizeAlgorithm } from '@be-music/player-engine/display';
import { decodeImageBuffer, type DecodedImage } from './bga-image.ts';
import {
type FrameMode,
type ANSIFrame,
convertImageToSpecFrameOffThread,
convertImageToSourceFrame,
} from './bga-frame.ts';
import {
type TimedANSIFrame,
type VideoFrameSource,
type FrameSource,
createStaticFrameSource,
mapFrameSource,
} from './bga-frame-source.ts';
async function loadImageAsSpecFrame(
imagePath: string,
mode: FrameMode,
resizeAlgorithm = DEFAULT_IMAGE_RESIZE_ALGORITHM,
signal?: AbortSignal,
colorKey?: BmsARGB,
swBGAColorKey?: BmsARGB,
): Promise<ANSIFrame | undefined> {
const extension = extname(imagePath).toLowerCase();
if (extension !== '.bmp' && extension !== '.png' && extension !== '.jpg' && extension !== '.jpeg') {
return undefined;
}
try {
throwIfAborted(signal);
const buffer = await readFile(imagePath, { signal });
throwIfAborted(signal);
const decoded = decodeImageBuffer(buffer, imagePath);
if ((colorKey || swBGAColorKey) && !decoded.hasAlphaChannel) {
for (let offset = 3; offset < decoded.data.length; offset += 4) decoded.data[offset] = 255;
}
if (colorKey) applyImageColorKey(decoded, colorKey);
if (swBGAColorKey) applyBMSColorKey(decoded.data, swBGAColorKey, true);
throwIfAborted(signal);
return await convertImageToSpecFrameOffThread(
decoded,
colorKey || swBGAColorKey ? 'exbmp' : mode,
resizeAlgorithm,
signal,
);
} catch (error) {
if (isAbortError(error)) {
throw error;
}
return undefined;
}
}
async function loadImageAsSourceFrame(
imagePath: string,
mode: FrameMode,
signal?: AbortSignal,
colorKey?: BmsARGB,
swBGAColorKey?: BmsARGB,
): Promise<ANSIFrame | undefined> {
const extension = extname(imagePath).toLowerCase();
if (extension !== '.bmp' && extension !== '.png' && extension !== '.jpg' && extension !== '.jpeg') {
return undefined;
}
try {
throwIfAborted(signal);
const buffer = await readFile(imagePath, { signal });
throwIfAborted(signal);
const decoded = decodeImageBuffer(buffer, imagePath);
if ((colorKey || swBGAColorKey) && !decoded.hasAlphaChannel) {
for (let offset = 3; offset < decoded.data.length; offset += 4) decoded.data[offset] = 255;
}
if (colorKey) applyImageColorKey(decoded, colorKey);
if (swBGAColorKey) applyBMSColorKey(decoded.data, swBGAColorKey, true);
throwIfAborted(signal);
return convertImageToSourceFrame(decoded, colorKey || swBGAColorKey ? 'exbmp' : mode);
} catch (error) {
if (isAbortError(error)) {
throw error;
}
return undefined;
}
}
/** Key native pixels before scaling and before the terminal's binary alpha-mask conversion. */
function applyImageColorKey(image: DecodedImage, colorKey: BmsARGB): void {
applyBMSColorKey(image.data, colorKey, image.hasAlphaChannel === true);
}
export async function loadFrameSource(
resourcePath: string,
mode: FrameMode,
_width: number,
_height: number,
imageResizeAlgorithm: ImageResizeAlgorithm = DEFAULT_IMAGE_RESIZE_ALGORITHM,
videoBGAStreaming = true,
signal?: AbortSignal,
preserveSourceGeometry = false,
colorKey?: BmsARGB,
swBGAColorKey?: BmsARGB,
): Promise<FrameSource | undefined> {
throwIfAborted(signal);
const imageFrame = preserveSourceGeometry
? await loadImageAsSourceFrame(resourcePath, mode, signal, colorKey, swBGAColorKey)
: await loadImageAsSpecFrame(resourcePath, mode, imageResizeAlgorithm, signal, colorKey, swBGAColorKey);
if (imageFrame) {
return createStaticFrameSource(imageFrame);
}
const video = await loadVideoAsFrameSource(
resourcePath,
swBGAColorKey || colorKey ? 'exbmp' : mode,
videoBGAStreaming,
signal,
preserveSourceGeometry || swBGAColorKey !== undefined || colorKey !== undefined,
);
if (!video || (!swBGAColorKey && !colorKey)) return video;
return mapFrameSource(video, (frame) => {
const rgba = new Uint8Array(frame.width * frame.height * 4);
for (let pixel = 0; pixel < frame.opaqueMask.length; pixel++) {
rgba.set(frame.rgb.subarray(pixel * 3, pixel * 3 + 3), pixel * 4);
rgba[pixel * 4 + 3] = getANSIFrameAlpha(frame, pixel);
}
if (colorKey) applyBMSColorKey(rgba, colorKey, false);
if (swBGAColorKey) applyBMSColorKey(rgba, swBGAColorKey, true);
return {
...frame,
opaqueMask: Uint8Array.from(frame.opaqueMask, (_, pixel) => (rgba[pixel * 4 + 3] ? 1 : 0)),
...(frame.alpha ? { alpha: Uint8Array.from(frame.alpha, (_, pixel) => rgba[pixel * 4 + 3]!) } : {}),
};
});
}
async function loadVideoAsFrameSource(
videoPath: string,
mode: FrameMode,
videoBGAStreaming = true,
signal?: AbortSignal,
preserveVideoBorders = false,
): Promise<FrameSource | undefined> {
throwIfAborted(signal);
const frames: TimedANSIFrame[] = [];
const source: VideoFrameSource = {
kind: 'video',
frames,
durationSeconds: undefined,
};
let settleFirstFrame: ((hasFrame: boolean) => void) | undefined;
let rejectFirstFrame: ((error: unknown) => void) | undefined;
let firstFrameSettled = false;
const firstFramePromise = new Promise<boolean>((resolve, reject) => {
settleFirstFrame = resolve;
rejectFirstFrame = reject;
});
const settleHasFirstFrame = (hasFrame: boolean): void => {
if (firstFrameSettled) {
return;
}
firstFrameSettled = true;
settleFirstFrame?.(hasFrame);
};
const appendFrame = (frame: { seconds: number; width: number; height: number; rgba: Uint8Array }): void => {
throwIfAborted(signal);
const sourceFrame = convertImageToSourceFrame(
{
width: frame.width,
height: frame.height,
data: frame.rgba,
format: 'video',
},
mode,
preserveVideoBorders,
);
frames.push({
seconds: frame.seconds,
frame: sourceFrame,
});
};
if (!videoBGAStreaming) {
const decoded = await decodeVideoFramesStream(
videoPath,
(frame) => {
appendFrame(frame);
},
signal,
{
onReady: (info) => {
source.durationSeconds = info.durationSeconds;
},
},
);
Eif (decoded) {
source.durationSeconds = decoded.durationSeconds;
}
throwIfAborted(signal);
return frames.length > 0 ? source : undefined;
}
void decodeVideoFramesStream(
videoPath,
(frame) => {
appendFrame(frame);
settleHasFirstFrame(true);
},
signal,
{
onReady: (info) => {
source.durationSeconds = info.durationSeconds;
},
stopAfterFirstFrame: true,
},
)
.then((decoded) => {
Eif (decoded) {
source.durationSeconds = decoded.durationSeconds;
}
settleHasFirstFrame(frames.length > 0);
})
.catch((error) => {
if (isAbortError(error)) {
if (!firstFrameSettled) {
firstFrameSettled = true;
rejectFirstFrame?.(error);
}
return;
}
settleHasFirstFrame(false);
});
const hasFirstFrame = await firstFramePromise;
throwIfAborted(signal);
Iif (!hasFirstFrame) {
return undefined;
}
let streamingStarted = false;
source.ensureStreaming = () => {
Iif (streamingStarted) {
return;
}
streamingStarted = true;
void (async () => {
let skipFrameCount = frames.length;
try {
throwIfAborted(signal);
const decoded = await decodeVideoFramesToSourceFramesInWorker(
videoPath,
mode,
(frame) => {
if (skipFrameCount > 0) {
skipFrameCount -= 1;
return;
}
frames.push({
seconds: frame.seconds,
frame: {
width: frame.width,
height: frame.height,
rgb: frame.rgb,
opaqueMask: frame.opaqueMask,
...(frame.alpha ? { alpha: frame.alpha } : {}),
},
});
},
signal,
{
preserveVideoBorders,
onReady: (info) => {
source.durationSeconds = info.durationSeconds;
},
},
);
Eif (decoded) {
source.durationSeconds = decoded.durationSeconds;
}
} catch (error) {
if (isAbortError(error)) {
return;
}
}
})();
};
return source;
}
export async function loadTerminalImageSourceFrame(
baseDir: string,
mediaPath: string,
signal?: AbortSignal,
): Promise<FrameSource | undefined> {
throwIfAborted(signal);
const resolved = await resolveMediaPath(baseDir, mediaPath, signal);
Iif (!resolved) {
return undefined;
}
const imageFrame = await loadImageAsSourceFrame(resolved, 'base', signal);
Eif (imageFrame) {
return createStaticFrameSource(imageFrame);
}
return loadVideoAsFrameSource(resolved, 'base', true, signal);
}
export async function resolveMediaPath(
baseDir: string,
mediaPath: string,
signal?: AbortSignal,
): Promise<string | undefined> {
return resolveFirstExistingPath(baseDir, createMediaPathCandidates(mediaPath), signal);
}
function createMediaPathCandidates(mediaPath: string): string[] {
const extCandidates = [
'.bmp',
'.BMP',
'.png',
'.PNG',
'.jpg',
'.JPG',
'.jpeg',
'.JPEG',
'.mpg',
'.MPG',
'.mpeg',
'.MPEG',
'.mp4',
'.MP4',
'.m4v',
'.M4V',
'.avi',
'.AVI',
'.wmv',
'.WMV',
'.mov',
'.MOV',
'.webm',
'.WEBM',
];
const candidates: string[] = [];
const seen = new Set<string>();
const push = (value: string): void => {
const normalized = value.trim();
if (normalized.length === 0 || seen.has(normalized)) {
return;
}
seen.add(normalized);
candidates.push(normalized);
};
const basePaths = [mediaPath];
const slashNormalized = mediaPath.replaceAll('\\', '/');
Iif (slashNormalized !== mediaPath) {
basePaths.push(slashNormalized);
}
for (const basePath of basePaths) {
push(basePath);
const extension = extname(basePath);
const withoutExtension = extension.length > 0 ? basePath.slice(0, -extension.length) : basePath;
for (const candidateExtension of extCandidates) {
push(`${withoutExtension}${candidateExtension}`);
}
}
return candidates;
}
|