composeSideBySide function
srcs scaled to height and laid side by side with a white gap, as one PNG at dst; null when no tool could.
Implementation
Future<String?> composeSideBySide(
List<String> srcs,
String dst, {
required int height,
int gap = 16,
Future<ProcessResult> Function(String, List<String>) run = boundedRun,
}) async {
try {
if (!Platform.isMacOS) {
final r = await _runImageTool(run, 'convert', [
...srcs,
'-resize',
'x$height',
'-bordercolor',
'white',
'-border',
'${gap ~/ 2}x0',
'+append',
dst,
]);
return r.exitCode == 0 && File(dst).existsSync() ? dst : null;
}
final parts = <({int width, int height, List<int> rgb})>[];
for (final (i, src) in srcs.indexed) {
final bmp = '$dst-part$i.bmp';
final r =
await _runImageTool(run, 'sips', ['--resampleHeight', '$height', '-s', 'format', 'bmp', src, '--out', bmp]);
if (r.exitCode != 0) return null;
final part = _bmpRgb(File(bmp).readAsBytesSync());
File(bmp).deleteSync();
if (part == null) return null;
parts.add(part);
}
final tall = parts.map((p) => p.height).reduce((a, b) => a > b ? a : b);
final wide = parts.fold(0, (s, p) => s + p.width) + gap * (parts.length - 1);
final canvas = List<int>.filled(wide * tall * 3, 255);
var left = 0;
for (final p in parts) {
for (var y = 0; y < p.height; y++) {
final from = y * p.width * 3;
canvas.setRange((y * wide + left) * 3, (y * wide + left + p.width) * 3, p.rgb, from);
}
left += p.width + gap;
}
final bmp = '$dst.bmp';
File(bmp).writeAsBytesSync(_bmpEncode(wide, tall, canvas));
final r = await _runImageTool(run, 'sips', ['-s', 'format', 'png', bmp, '--out', dst]);
File(bmp).deleteSync();
return r.exitCode == 0 && File(dst).existsSync() ? dst : null;
} on Object {
return null;
}
}