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 | 34x 34x 44x 34x 593x 31x 31x 2x 29x 34x 2x 25x 2x 23x 1x 22x 1x 21x 1x 20x 1x 19x 1x 18x 1x 17x 1x 16x 16x 31x 1x 15x 43x 5x 10x 1x 9x 1x 8x 7x 6x 6x 5x 5x 1x 1x 1x 6x 5x 5x 5x 5x 5x 5x 5x | import { open, readFile, unlink } from 'node:fs/promises';
import { extname } from 'node:path';
import { lampSetIndex, explainLampSetError } from './options.js';
import { inputFormat } from './photometry.js';
import { CHART_TYPES, chartPhotometry, type ChartOptions } from './chart.js';
import { imageDimensions, renderPhotometry, type RenderOptions } from './render.js';
export interface ImageCommandOptions extends RenderOptions, ChartOptions {
input: string;
output: string;
json: boolean;
}
/** Arguments as yargs parses them for `ies render` and `ies chart`. */
export interface ImageArguments extends Omit<RenderOptions, 'lampSetIndex' | 'inputFormat'> {
file: string;
output: string;
json?: boolean;
lampSet?: number;
format?: 'ies' | 'ldt';
type?: string;
axis?: string;
upAxis?: string;
angle?: number;
mountingHeight?: number;
extent?: number;
resolution?: number;
maxDistance?: number;
unit?: string;
maxColumns?: number;
maxRows?: number;
logScale?: boolean;
contours?: string;
minContour?: number;
colorScale?: string;
}
/** Range checks yargs cannot express, applied before any output file is created. */
export function imageOptions(command: 'render' | 'chart', argv: ImageArguments): ImageCommandOptions {
const { file, lampSet, format, json, contours, ...rest } = argv;
// Choices are checked below; yargs types them as plain strings.
const options = {
...(rest as Omit<ImageCommandOptions, 'input' | 'json'>),
input: file,
json: json ?? false,
inputFormat: format,
lampSetIndex: lampSetIndex(lampSet),
contours: contours === undefined ? undefined : contours.split(',').map((level) => Number(level.trim())),
};
for (const [name, value] of Object.entries(options))
if (typeof value === 'number' && !Number.isFinite(value)) throw new Error(`--${name} must be a finite number`);
const extension = extname(options.output).toLowerCase();
if (command === 'render' ? extension !== '.png' : extension !== '.png' && extension !== '.svg')
throw new Error(command === 'render' ? 'Render output must use .png' : 'Chart output must use .svg or .png');
imageDimensions(
options.width ?? (command === 'chart' ? 900 : 1024),
options.height ?? (command === 'chart' ? 700 : 768),
);
if (options.exposure !== undefined && (options.exposure < -6 || options.exposure > 20))
throw new Error('Exposure must be an EV100 from -6 to 20');
if (options.timeout !== undefined && (options.timeout <= 0 || options.timeout > 600))
throw new Error('Timeout must be between 0 and 600 seconds');
if (options.type !== undefined && !(CHART_TYPES as readonly string[]).includes(options.type))
throw new Error(`Chart type must be one of ${CHART_TYPES.join(', ')}`);
if (options.axis !== undefined && !['vertical', 'horizontal'].includes(options.axis))
throw new Error('Axis must be vertical or horizontal');
if (options.upAxis !== undefined && !['x', 'y', 'z'].includes(options.upAxis))
throw new Error('Up axis must be x, y or z');
if (options.mountingHeight !== undefined && (options.mountingHeight < 0.01 || options.mountingHeight > 10000))
throw new Error('Mounting height must be within 0.01–10000 meters');
if (options.extent !== undefined && (options.extent < 0.01 || options.extent > 100000))
throw new Error('Ground extent must be within 0.01–100000 meters');
if (
options.resolution !== undefined &&
(!Number.isInteger(options.resolution) || options.resolution < 2 || options.resolution > 201)
)
throw new Error('Grid resolution must be an integer from 2 to 201');
if (
options.maxDistance !== undefined &&
(!Number.isInteger(options.maxDistance) || options.maxDistance < 1 || options.maxDistance > 50)
)
throw new Error('Maximum distance must be an integer from 1 to 50');
Iif (options.unit !== undefined && !['cd', 'cd/klm'].includes(options.unit))
throw new Error('Unit must be cd or cd/klm');
for (const [name, value] of [
['columns', options.maxColumns],
['rows', options.maxRows],
] as const)
if (value !== undefined && (!Number.isInteger(value) || value < 2))
throw new Error(`Maximum ${name} must be an integer of at least 2`);
if (
options.contours?.some(
(level, i, levels) => !(level >= 0) || !Number.isFinite(level) || (i > 0 && level <= levels[i - 1]!),
) ||
(options.contours && options.contours.length > 32)
)
throw new Error('Contours must be at most 32 comma-separated, strictly increasing, nonnegative lux values');
if (options.minContour !== undefined && !(options.minContour > 0))
throw new Error('Minimum contour must be a positive lux value');
if (options.colorScale !== undefined && !['log', 'linear'].includes(options.colorScale))
throw new Error('Colour scale must be log or linear');
return options;
}
/** Reserve the output before expensive processing; remove only the newly-created placeholder on failure. */
export async function writeImageExclusively<T extends { data: Buffer }>(
output: string,
produce: () => Promise<T>,
): Promise<T> {
const file = await open(output, 'wx');
try {
const image = await produce();
await file.writeFile(image.data);
return image;
} catch (error) {
await file.close();
await unlink(output);
throw error;
} finally {
await file.close();
}
}
export async function runImageCommand(command: 'render' | 'chart', options: ImageCommandOptions): Promise<void> {
const text = await readFile(options.input, 'utf8');
options.inputFormat = inputFormat(text, options.input, options.inputFormat);
Iif (command === 'render') {
const image = await writeImageExclusively(options.output, async () => {
const rendered = await renderPhotometry(text, options).catch(explainLampSetError);
return { ...rendered, data: rendered.png };
});
const report = { output: options.output, width: image.width, height: image.height, gpu: image.gpu };
console.log(
options.json
? JSON.stringify(report, null, 2)
: `Wrote ${options.output} (${image.width} × ${image.height}; ${image.gpu.renderer})`,
);
} else {
const image = await writeImageExclusively(options.output, () =>
chartPhotometry(text, {
...options,
format: extname(options.output).toLowerCase() === '.png' ? 'png' : 'svg',
}).catch(explainLampSetError),
);
const report = {
output: options.output,
type: image.type,
format: image.format,
width: image.width,
height: image.height,
...(image.values !== undefined && { values: image.values }),
};
console.log(
options.json
? JSON.stringify(report, null, 2)
: `Wrote ${options.output} (${image.width} × ${image.height}; ${image.type})`,
);
}
}
|