-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathimageProcessor.ts
214 lines (198 loc) · 6.23 KB
/
imageProcessor.ts
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
import { fromRgba, Pixel } from './pixel';
export type ToSvgStringParams = {
frame: Pixel[][],
pretty?: boolean,
noMetadata?: boolean,
trimAlpha?: boolean,
maxLeftTrim?: number,
maxRightTrim?: number,
maxTopTrim?: number,
maxBottomTrim?: number,
minWidth?: number,
minHeight?: number,
debug?: (str: string) => void
}
export function toSvgString(params: ToSvgStringParams): Promise<string> {
const frame = params.frame;
const pretty = !!params.pretty;
const noMetadata = !!params.noMetadata;
const trimAlpha = !!params.trimAlpha;
const debug: (str: string) => void = params.debug || (() => undefined);
const maxLeftTrim = params.maxLeftTrim == null ? Number.MAX_SAFE_INTEGER : params.maxLeftTrim;
const maxRightTrim = params.maxRightTrim == null ? Number.MAX_SAFE_INTEGER : params.maxRightTrim;
const maxTopTrim = params.maxTopTrim == null ? Number.MAX_SAFE_INTEGER : params.maxTopTrim;
const maxBottomTrim = params.maxBottomTrim == null ? Number.MAX_SAFE_INTEGER : params.maxBottomTrim;
const minWidth = params.minWidth == null ? 0 : params.minWidth;
const maxHorizontalTrim = frame[0].length - minWidth;
const minHeight = params.minHeight == null ? 0 : params.minHeight;
const maxVerticalTrim = frame.length - minHeight;
return new Promise((resolve, reject) => {
try {
const distinctPixels: Map<string, Coordinate[]> = getDistinctPixels(frame, debug);
const leftTrim = trimAlpha ? Math.min(maxLeftTrim, numBlankLinesLeft(frame), maxHorizontalTrim) : 0;
const rightTrim = trimAlpha ? Math.min(maxRightTrim, numBlankLinesRight(frame), maxHorizontalTrim - leftTrim) : 0;
const topTrim = trimAlpha ? Math.min(maxTopTrim, numBlankLinesTop(frame), maxVerticalTrim) : 0;
const bottomTrim = trimAlpha ? Math.min(maxBottomTrim, numBlankLinesBottom(frame), maxVerticalTrim - topTrim) : 0;
if (trimAlpha && (leftTrim !== 0 || rightTrim !== 0 || topTrim != 0 || bottomTrim != 0)) {
debug(`output image will trim (left: ${leftTrim} right: ${rightTrim} top: ${topTrim} bottom: ${bottomTrim}) blank lines`);
}
let str = '<svg xmlns="http://www.w3.org/2000/svg" shape-rendering="crispEdges" ';
const width = frame[0].length - leftTrim - rightTrim;
const height = frame.length - topTrim - bottomTrim;
str += `viewBox="0 -0.5 ${width} ${height}" width="${width}" height="${height}">`;
str = addNewlineIfPretty(str);
if (!noMetadata) {
str += '<metadata>Generated with pixel-perfect-svg https://github.com/kagof/pixel-perfect-svg</metadata>';
str = addNewlineIfPretty(str);
}
let longest = 0;
distinctPixels.forEach((coords, rgba) => {
const pixel = fromRgba(rgba);
if (pixel.opacity() == 0) {
return;
}
str = addIndentIfPretty(str);
str += `<path stroke="${pixel.rgb()}" `;
if (pixel.a < 255) {
str += `opacity="${pixel.opacity()}" `;
}
str += `d="`;
let lineStarted = false;
let numPixels = 0;
coords.forEach((coord, idx) => {
if (!lineStarted) {
if (str.length - str.lastIndexOf('\n') >= 80) {
str = addNewlineIfPretty(str);
str = addIndentIfPretty(str, 2);
}
str += `M${coord.x - leftTrim},${coord.y - topTrim}`;
if (pretty) {
str += ' ';
}
lineStarted = true;
numPixels = 0;
}
const nextCoord = coords[idx + 1];
if (!nextCoord || nextCoord.x !== coord.x + 1 || nextCoord.y !== coord.y) {
str += `h${++numPixels}`;
lineStarted = false;
if (numPixels > longest) {
longest = numPixels;
}
if (pretty && nextCoord) {
str += ' ';
}
} else {
numPixels++;
}
});
str += `"/>`;
str = addNewlineIfPretty(str);
});
str += '</svg>\n';
debug(`output image longest continuous color section: ${longest}`);
resolve(str);
} catch (err) {
reject(err);
}
});
function addNewlineIfPretty(str: string): string {
if (pretty) {
str += '\n';
}
return str;
}
function addIndentIfPretty(str: string, times = 1): string {
if (pretty) {
for (let i = 0; i < times; i++) {
str += ' ';
}
}
return str;
}
}
function getDistinctPixels(frame: Pixel[][], debug: (str: string) => void): Map<string, Coordinate[]> {
const distinctPixels: Map<string, Coordinate[]> = new Map();
for (let y = 0; y < frame.length; y++) {
for (let x = 0; x < frame[y].length; x++) {
if (frame[y][x].a != 0) {
const rgba = frame[y][x].rgba();
if (distinctPixels.has(rgba)) {
distinctPixels.get(rgba)?.push({ x, y });
} else {
distinctPixels.set(rgba, [{ x, y }]);
}
}
}
}
debug(`input image has ${distinctPixels.size} distinct colors`);
return distinctPixels;
}
function numBlankLinesLeft(pixels: Pixel[][]): number {
let x = 0;
for (; x < pixels[0].length; x++) {
let hasData = false;
for (let y = 0; y < pixels.length; y++) {
if (pixels[y][x].opacity() !== 0) {
hasData = true;
break;
}
}
if (hasData) {
break;
}
}
return x;
}
function numBlankLinesRight(pixels: Pixel[][]): number {
let x = pixels[0].length - 1;
for (; x >= 0; x--) {
let hasData = false;
for (let y = 0; y < pixels.length; y++) {
if (pixels[y][x].opacity() || 0 !== 0) {
hasData = true;
break;
}
}
if (hasData) {
break;
}
}
return pixels[0].length - 1 - x;
}
function numBlankLinesTop(pixels: Pixel[][]): number {
let y = 0;
for (; y < pixels.length; y++) {
let hasData = false;
for (let x = 0; x < pixels[y].length; x++) {
if (pixels[y][x].opacity() !== 0) {
hasData = true;
break;
}
}
if (hasData) {
break;
}
}
return y;
}
function numBlankLinesBottom(pixels: Pixel[][]): number {
let y = pixels.length - 1;
for (; y >= 0; y--) {
let hasData = false;
for (let x = 0; x < pixels[y].length; x++) {
if (pixels[y][x].opacity() !== 0) {
hasData = true;
break;
}
}
if (hasData) {
break;
}
}
return pixels.length - 1 - y;
}
type Coordinate = {
x: number,
y: number,
}