combine.ts
3.74 KB
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
import fs from 'fs';
import path from 'path';
import type { CombineOptions } from '../types';
import { SCREENSHOT_WEBP_QUALITY } from './image-converter';
const DEFAULT_OPTIONS: Required<CombineOptions> = {
leftHeader: 'RENDER (Current)',
rightHeader: 'TARGET (Expected)',
gapWidth: 40,
headerHeight: 60,
};
/**
* Combines two annotated screenshots side-by-side with headers.
* Matches the Python reference implementation from evolt.
*/
export async function combineSideBySide(
renderBase64: string,
targetBase64: string,
outputPath: string,
options?: CombineOptions
): Promise<void> {
const opts = { ...DEFAULT_OPTIONS, ...options };
try {
const sharp = (await import('sharp')).default;
const leftBase64 = extractBase64Data(renderBase64);
const rightBase64 = extractBase64Data(targetBase64);
const leftBuffer = Buffer.from(leftBase64, 'base64');
const rightBuffer = Buffer.from(rightBase64, 'base64');
const leftMeta = await sharp(leftBuffer).metadata();
const rightMeta = await sharp(rightBuffer).metadata();
if (!leftMeta.width || !leftMeta.height || !rightMeta.width || !rightMeta.height) {
throw new Error('Failed to read image dimensions');
}
const scale = leftMeta.height / rightMeta.height;
const rightResizedWidth = Math.round(rightMeta.width * scale);
const rightResized = await sharp(rightBuffer).resize({ height: leftMeta.height, width: rightResizedWidth, fit: 'fill' }).toBuffer();
const combinedWidth = leftMeta.width + opts.gapWidth + rightResizedWidth;
const combinedHeight = opts.headerHeight + leftMeta.height;
const centerX = leftMeta.width + opts.gapWidth / 2;
const leftTextX = leftMeta.width / 2;
const rightTextX = leftMeta.width + opts.gapWidth + rightResizedWidth / 2;
const textY = opts.headerHeight / 2 + 8;
const headerSvg = `
<svg width="${combinedWidth}" height="${opts.headerHeight}">
<rect width="${combinedWidth}" height="${opts.headerHeight}" fill="#F0F0F0"/>
<line x1="${centerX}" y1="0" x2="${centerX}" y2="${combinedHeight}"
stroke="#C8C8C8" stroke-width="2"/>
<text x="${leftTextX}" y="${textY}"
font-family="Arial" font-size="24" font-weight="bold" fill="#C80000"
text-anchor="middle">
${opts.leftHeader}
</text>
<text x="${rightTextX}" y="${textY}"
font-family="Arial" font-size="24" font-weight="bold" fill="#009600"
text-anchor="middle">
${opts.rightHeader}
</text>
</svg>
`;
const headerBuffer = Buffer.from(headerSvg);
const outputDir = path.dirname(outputPath);
if (!fs.existsSync(outputDir)) {
fs.mkdirSync(outputDir, { recursive: true });
}
await sharp({
create: {
width: combinedWidth,
height: combinedHeight,
channels: 3,
background: { r: 255, g: 255, b: 255 },
},
})
.composite([
{ input: headerBuffer, top: 0, left: 0 },
{ input: leftBuffer, top: opts.headerHeight, left: 0 },
{ input: rightResized, top: opts.headerHeight, left: leftMeta.width + opts.gapWidth },
])
.webp({ quality: SCREENSHOT_WEBP_QUALITY })
.toFile(outputPath);
} catch (error) {
throw new Error(`Failed to combine side-by-side screenshots: ${error instanceof Error ? error.message : String(error)}`);
}
}
function extractBase64Data(base64String: string): string {
const dataUriMatch = base64String.match(/^data:image\/[a-z]+;base64,(.+)$/);
if (dataUriMatch && dataUriMatch[1]) {
return dataUriMatch[1];
}
return base64String;
}