inlaytrace 0.3.1 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +9 -0
- package/README.md +36 -20
- package/bin/inlaytrace.mjs +139 -0
- package/bin/png.mjs +123 -0
- package/dist/index.d.ts +90 -48
- package/dist/index.js +609 -416
- package/package.json +6 -2
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,14 @@
|
|
|
1
1
|
# inlaytrace
|
|
2
2
|
|
|
3
|
+
## 0.4.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- A command line: `npx inlaytrace logo.png -o logo.svg --preset logo`. PNGs are read with Node's own zlib, so the package still has no dependencies; JPEG, WebP, GIF and TIFF use sharp when it's installed beside it. `--help` lists the flags, which follow the trace options.
|
|
8
|
+
- New `grayscale` option for the colour presets (not `pixel`): trace in greys only, `colors` of them, each pixel taken as the grey of its own OKLab lightness.
|
|
9
|
+
- Presets in the package, and a simpler call: `trace(image, { preset: 'logo' })`. `image` is anything shaped like ImageData (`{ data, width, height }`). The seven presets the app uses (`logo`, `illustration`, `outline`, `lineart`, `centerline`, `poster`, `pixel`) each pick their own palette and carry their own defaults, and any option overrides them. `palette` traces in your own colours, `layers` replaces `stacked`, `invert` traces light ink, and `presets` lists every default. Centerline paths are now `{ d, fill, stroke }` like Outline art's lines (`fill` the colour, `stroke` the width). `trace(rgba, width, height, options)`, `traceCenterline` and `centerlineToSvg` still work but are deprecated, and go in 1.0.
|
|
10
|
+
- The package entry no longer exports `mergeLabels`, `otsu`, `staircase` or `colorAt`. They were internals, exported for the app's tests and never documented. Everything left is in the API reference and stays through 1.0.
|
|
11
|
+
|
|
3
12
|
## 0.3.1
|
|
4
13
|
|
|
5
14
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -27,38 +27,54 @@ shows which commit built it.
|
|
|
27
27
|
Versions are 0.x while the API settles: a minor version (0.1 → 0.2) may change
|
|
28
28
|
it, and the [changelog](https://github.com/mtblck/inlaytrace/blob/main/packages/inlay/CHANGELOG.md)
|
|
29
29
|
says how.
|
|
30
|
+
From 1.0, a minor version can change the traced output (different paths or
|
|
31
|
+
colours) but not the API, and never makes the measured quality worse; a major
|
|
32
|
+
version is any change to an export, option, preset or result type. Pin an exact
|
|
33
|
+
version if you need byte-identical SVGs. The [full policy](https://github.com/mtblck/inlaytrace/blob/main/docs/package.md#versions)
|
|
34
|
+
says what counts as what.
|
|
30
35
|
|
|
31
36
|
## Use
|
|
32
37
|
|
|
33
38
|
```ts
|
|
34
|
-
import { trace, toSvg
|
|
39
|
+
import { trace, toSvg } from 'inlaytrace'
|
|
35
40
|
|
|
36
|
-
// RGBA
|
|
37
|
-
const result = trace(
|
|
38
|
-
mode: 'palette',
|
|
39
|
-
palette: peakPalette(rgba, 8), // the image's main colours, as #rrggbb
|
|
40
|
-
speckle: 8, // merge regions smaller than 8 × 8 px into their surroundings
|
|
41
|
-
tolerance: 1, // px the fitted outline may stray from the pixels
|
|
42
|
-
cornerAngle: 45, // turns sharper than this stay corners
|
|
43
|
-
})
|
|
41
|
+
// Anything shaped like ImageData: { data: RGBA bytes, width, height }
|
|
42
|
+
const result = trace(image, { preset: 'logo' })
|
|
44
43
|
|
|
45
44
|
result.paths // [{ d: 'M…Z', fill: '#rrggbb' }, …] largest first, nonzero fill;
|
|
46
|
-
//
|
|
45
|
+
// lines (Outline art, Centerline) are { d, fill, stroke: width }, stroked in `fill`, unfilled
|
|
47
46
|
toSvg(result) // '<svg …>…</svg>'
|
|
48
47
|
```
|
|
49
48
|
|
|
50
|
-
###
|
|
49
|
+
### Command line
|
|
51
50
|
|
|
52
|
-
|
|
51
|
+
```bash
|
|
52
|
+
npx inlaytrace logo.png -o logo.svg
|
|
53
|
+
npx inlaytrace photo.jpg -o photo.svg --preset poster --colors 16
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
PNGs need nothing else. For JPEG, WebP, GIF or TIFF, install
|
|
57
|
+
[sharp](https://sharp.pixelplumbing.com/) beside it (`npm install sharp`). Without
|
|
58
|
+
`-o` the SVG goes to stdout. `npx inlaytrace --help` lists the options.
|
|
59
|
+
|
|
60
|
+
### Presets
|
|
61
|
+
|
|
62
|
+
| Preset | For |
|
|
53
63
|
| --- | --- |
|
|
54
|
-
| `
|
|
55
|
-
| `
|
|
56
|
-
| `
|
|
57
|
-
| `
|
|
58
|
-
| `
|
|
59
|
-
| `
|
|
60
|
-
| `
|
|
61
|
-
|
|
64
|
+
| `logo` (default) | Few flat colours (8), crisp corners, shapes cut out to tile, exact circles and rectangles. |
|
|
65
|
+
| `illustration` | More colours (24), smooth curves, shapes in layers (each whole, a disc behind a bar stays a disc), soft shading as gradients. |
|
|
66
|
+
| `outline` | Illustration, with thin drawn outlines (2.5–8 px; give `scale` if the image was enlarged) traced down their middle as round-capped strokes, and the shapes either side meeting beneath them. |
|
|
67
|
+
| `lineart` | Ink drawings and scans: ink darker than the image's own ink/paper split (Otsu; coloured ink works too), small breaks in lines closed. Only the ink is drawn. |
|
|
68
|
+
| `centerline` | Single strokes down the middle of the ink, at the width measured from it, for pen plotters and engraving. |
|
|
69
|
+
| `poster` | Photos as flat colour bands: colours clustered (k-means in OKLab, deterministic), each shape whole, larger first. |
|
|
70
|
+
| `pixel` | Pixel art: exact colours, no clean-up of lone pixels, no curve fitting; every boundary is its pixel staircase. |
|
|
71
|
+
|
|
72
|
+
Any option overrides the preset: `colors`, `palette` (your own colours, as
|
|
73
|
+
`#rrggbb`), `merge` (`{ '#from': '#into' }`), `speckle`, `tolerance`,
|
|
74
|
+
`cornerAngle`, `layers`, `shapes`, `strokes`, `gradients`, `closeGaps`,
|
|
75
|
+
`threshold`, `invert`, `strokeWidth`, `smoothShading`, `keepOutlines`, `grayscale` and
|
|
76
|
+
`scale`. [Options](https://github.com/mtblck/inlaytrace/blob/main/docs/package/options.md)
|
|
77
|
+
says what each does.
|
|
62
78
|
|
|
63
79
|
### Also exported
|
|
64
80
|
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* The command line (#165): `npx inlaytrace logo.png -o logo.svg --preset logo`.
|
|
4
|
+
*
|
|
5
|
+
* PNGs are decoded here with Node's own zlib, so the package keeps no
|
|
6
|
+
* dependencies. JPEG, WebP and anything else go through sharp when it's
|
|
7
|
+
* installed (`npm install sharp`), and the error says so when it isn't.
|
|
8
|
+
*/
|
|
9
|
+
import { readFile, writeFile } from 'node:fs/promises'
|
|
10
|
+
import { parseArgs } from 'node:util'
|
|
11
|
+
import { presets, toSvg, trace } from '../dist/index.js'
|
|
12
|
+
import { decodePng, isPng } from './png.mjs'
|
|
13
|
+
|
|
14
|
+
const HELP = `Usage: inlaytrace <image> [-o out.svg] [options]
|
|
15
|
+
|
|
16
|
+
Traces a PNG (or, with sharp installed, a JPEG, WebP, GIF or TIFF) into an SVG.
|
|
17
|
+
Writes to stdout without -o.
|
|
18
|
+
|
|
19
|
+
-o, --output <file> where to write the SVG
|
|
20
|
+
-p, --preset <name> ${Object.keys(presets).join(', ')} (default logo)
|
|
21
|
+
-c, --colors <n> at most this many colours
|
|
22
|
+
--palette <list> your own colours, comma-separated: '#111111,#e63946'
|
|
23
|
+
--speckle <px> merge regions smaller than this square
|
|
24
|
+
--tolerance <px> how far the outline may stray from the pixels
|
|
25
|
+
--corner <deg> turns sharper than this stay corners
|
|
26
|
+
--layers shapes whole, in layers (--no-layers: cut out)
|
|
27
|
+
--exact-shapes draw circles and rectangles exactly (--no-exact-shapes)
|
|
28
|
+
--threshold <0-255> lineart and centerline: ink is darker than this
|
|
29
|
+
--invert lineart and centerline: light ink on a dark background
|
|
30
|
+
--grayscale colour presets: greys only, --colors of them
|
|
31
|
+
--stroke-width <px> centerline: the stroke width
|
|
32
|
+
-h, --help
|
|
33
|
+
-v, --version`
|
|
34
|
+
|
|
35
|
+
const { values, positionals } = parseArgs({
|
|
36
|
+
allowPositionals: true,
|
|
37
|
+
allowNegative: true,
|
|
38
|
+
options: {
|
|
39
|
+
output: { type: 'string', short: 'o' },
|
|
40
|
+
preset: { type: 'string', short: 'p' },
|
|
41
|
+
colors: { type: 'string', short: 'c' },
|
|
42
|
+
palette: { type: 'string' },
|
|
43
|
+
speckle: { type: 'string' },
|
|
44
|
+
tolerance: { type: 'string' },
|
|
45
|
+
corner: { type: 'string' },
|
|
46
|
+
layers: { type: 'boolean' },
|
|
47
|
+
'exact-shapes': { type: 'boolean' },
|
|
48
|
+
threshold: { type: 'string' },
|
|
49
|
+
invert: { type: 'boolean' },
|
|
50
|
+
grayscale: { type: 'boolean' },
|
|
51
|
+
'stroke-width': { type: 'string' },
|
|
52
|
+
help: { type: 'boolean', short: 'h' },
|
|
53
|
+
version: { type: 'boolean', short: 'v' },
|
|
54
|
+
},
|
|
55
|
+
})
|
|
56
|
+
|
|
57
|
+
if (values.version) {
|
|
58
|
+
const pkg = JSON.parse(await readFile(new URL('../package.json', import.meta.url), 'utf8'))
|
|
59
|
+
console.log(pkg.version)
|
|
60
|
+
process.exit(0)
|
|
61
|
+
}
|
|
62
|
+
if (values.help || positionals.length !== 1) {
|
|
63
|
+
console.log(HELP)
|
|
64
|
+
process.exit(values.help ? 0 : 1)
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const fail = (message) => {
|
|
68
|
+
console.error(`inlaytrace: ${message}`)
|
|
69
|
+
process.exit(1)
|
|
70
|
+
}
|
|
71
|
+
const num = (name, v) => {
|
|
72
|
+
if (v === undefined) return undefined
|
|
73
|
+
const n = Number(v)
|
|
74
|
+
if (!Number.isFinite(n) || n < 0) fail(`--${name} needs a number, not "${v}"`)
|
|
75
|
+
return n
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
const options = {
|
|
79
|
+
preset: values.preset,
|
|
80
|
+
colors: num('colors', values.colors),
|
|
81
|
+
palette: values.palette?.split(',').map((c) => c.trim().toLowerCase()),
|
|
82
|
+
speckle: num('speckle', values.speckle),
|
|
83
|
+
tolerance: num('tolerance', values.tolerance),
|
|
84
|
+
cornerAngle: num('corner', values.corner),
|
|
85
|
+
layers: values.layers,
|
|
86
|
+
shapes: values['exact-shapes'],
|
|
87
|
+
threshold: values.threshold === undefined ? undefined : values.threshold === 'auto' ? 'auto' : num('threshold', values.threshold),
|
|
88
|
+
invert: values.invert,
|
|
89
|
+
grayscale: values.grayscale,
|
|
90
|
+
strokeWidth: num('stroke-width', values['stroke-width']),
|
|
91
|
+
}
|
|
92
|
+
if (options.preset && !(options.preset in presets)) fail(`no preset "${options.preset}". Use one of: ${Object.keys(presets).join(', ')}`)
|
|
93
|
+
for (const c of options.palette ?? []) if (!/^#[0-9a-f]{6}$/.test(c)) fail(`--palette colours are #rrggbb, not "${c}"`)
|
|
94
|
+
|
|
95
|
+
const [input] = positionals
|
|
96
|
+
let bytes
|
|
97
|
+
try {
|
|
98
|
+
bytes = await readFile(input)
|
|
99
|
+
} catch (e) {
|
|
100
|
+
fail(`can't read ${input}: ${e.code ?? e.message}`)
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let image
|
|
104
|
+
try {
|
|
105
|
+
try {
|
|
106
|
+
image = isPng(bytes) ? decodePng(bytes) : await decodeWithSharp(bytes, input)
|
|
107
|
+
} catch (e) {
|
|
108
|
+
if (!e.interlaced) throw e
|
|
109
|
+
image = await decodeWithSharp(bytes, 'an interlaced PNG')
|
|
110
|
+
}
|
|
111
|
+
} catch (e) {
|
|
112
|
+
fail(`can't decode ${input}: ${e.message}`)
|
|
113
|
+
}
|
|
114
|
+
let svg
|
|
115
|
+
try {
|
|
116
|
+
svg = toSvg(trace(image, options))
|
|
117
|
+
} catch (e) {
|
|
118
|
+
fail(`can't trace ${input}: ${e.message}`)
|
|
119
|
+
}
|
|
120
|
+
if (values.output) {
|
|
121
|
+
try {
|
|
122
|
+
await writeFile(values.output, svg)
|
|
123
|
+
} catch (e) {
|
|
124
|
+
fail(`can't write ${values.output}: ${e.code ?? e.message}`)
|
|
125
|
+
}
|
|
126
|
+
console.error(`inlaytrace: ${input} → ${values.output} (${image.width} × ${image.height}, ${(svg.length / 1024).toFixed(1)} KB)`)
|
|
127
|
+
} else process.stdout.write(svg)
|
|
128
|
+
|
|
129
|
+
async function decodeWithSharp(b, name) {
|
|
130
|
+
let sharp
|
|
131
|
+
try {
|
|
132
|
+
sharp = (await import('sharp')).default
|
|
133
|
+
} catch {
|
|
134
|
+
fail(`${name} isn't a PNG. For JPEG, WebP and other formats, install sharp next to inlaytrace (npm install sharp), or convert it to PNG.`)
|
|
135
|
+
}
|
|
136
|
+
// rotate() with no angle turns a phone photo upright by its EXIF orientation.
|
|
137
|
+
const { data, info } = await sharp(b).rotate().ensureAlpha().raw().toBuffer({ resolveWithObject: true })
|
|
138
|
+
return { data, width: info.width, height: info.height }
|
|
139
|
+
}
|
package/bin/png.mjs
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A small PNG decoder for the command line, on Node's own zlib, so the
|
|
3
|
+
* package needs no dependencies.
|
|
4
|
+
*/
|
|
5
|
+
import { inflateSync } from 'node:zlib'
|
|
6
|
+
|
|
7
|
+
export function isPng(b) {
|
|
8
|
+
return b.length > 8 && b.readUInt32BE(0) === 0x89504e47 && b.readUInt32BE(4) === 0x0d0a1a0a
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* A PNG as RGBA: every colour type, bit depths 1–16, transparency (tRNS).
|
|
13
|
+
* Interlaced files are rare and left to sharp.
|
|
14
|
+
*/
|
|
15
|
+
export function decodePng(b) {
|
|
16
|
+
let pos = 8
|
|
17
|
+
let width = 0
|
|
18
|
+
let height = 0
|
|
19
|
+
let depth = 0
|
|
20
|
+
let type = 0
|
|
21
|
+
let interlace = 0
|
|
22
|
+
let palette = null
|
|
23
|
+
let trns = null
|
|
24
|
+
const idat = []
|
|
25
|
+
while (pos + 8 <= b.length) {
|
|
26
|
+
const len = b.readUInt32BE(pos)
|
|
27
|
+
const kind = b.toString('latin1', pos + 4, pos + 8)
|
|
28
|
+
const body = b.subarray(pos + 8, pos + 8 + len)
|
|
29
|
+
if (kind === 'IHDR') {
|
|
30
|
+
width = body.readUInt32BE(0)
|
|
31
|
+
height = body.readUInt32BE(4)
|
|
32
|
+
depth = body[8]
|
|
33
|
+
type = body[9]
|
|
34
|
+
interlace = body[12]
|
|
35
|
+
} else if (kind === 'PLTE') palette = body
|
|
36
|
+
else if (kind === 'tRNS') trns = body
|
|
37
|
+
else if (kind === 'IDAT') idat.push(body)
|
|
38
|
+
else if (kind === 'IEND') break
|
|
39
|
+
pos += 12 + len
|
|
40
|
+
}
|
|
41
|
+
if (!width || !height) throw new Error('the PNG has no image header')
|
|
42
|
+
if (interlace) throw Object.assign(new Error('interlaced PNGs need sharp'), { interlaced: true })
|
|
43
|
+
const channels = { 0: 1, 2: 3, 3: 1, 4: 2, 6: 4 }[type]
|
|
44
|
+
if (!channels) throw new Error(`unknown PNG colour type ${type}`)
|
|
45
|
+
const bpp = Math.max(1, (channels * depth) >> 3) // bytes per pixel, for filtering
|
|
46
|
+
const stride = Math.ceil((width * channels * depth) / 8)
|
|
47
|
+
const raw = inflateSync(Buffer.concat(idat))
|
|
48
|
+
// Checked before anything is sized from the header, so a cut-off download or a lying header fails here.
|
|
49
|
+
if (raw.length < (stride + 1) * height) throw new Error('the PNG is truncated')
|
|
50
|
+
const rows = Buffer.alloc(stride * height)
|
|
51
|
+
let prev = Buffer.alloc(stride)
|
|
52
|
+
for (let y = 0; y < height; y++) {
|
|
53
|
+
const filter = raw[y * (stride + 1)]
|
|
54
|
+
const line = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1))
|
|
55
|
+
const out = rows.subarray(y * stride, (y + 1) * stride)
|
|
56
|
+
for (let i = 0; i < stride; i++) {
|
|
57
|
+
const a = i >= bpp ? out[i - bpp] : 0
|
|
58
|
+
const up = prev[i]
|
|
59
|
+
const c = i >= bpp ? prev[i - bpp] : 0
|
|
60
|
+
const x = line[i]
|
|
61
|
+
out[i] =
|
|
62
|
+
filter === 0 ? x
|
|
63
|
+
: filter === 1 ? x + a
|
|
64
|
+
: filter === 2 ? x + up
|
|
65
|
+
: filter === 3 ? x + ((a + up) >> 1)
|
|
66
|
+
: x + paeth(a, up, c)
|
|
67
|
+
}
|
|
68
|
+
prev = out
|
|
69
|
+
}
|
|
70
|
+
// Each sample scaled to 0–255; 16-bit keeps its high byte.
|
|
71
|
+
const sample = (row, i) => {
|
|
72
|
+
if (depth === 8) return row[i]
|
|
73
|
+
if (depth === 16) return row[i * 2]
|
|
74
|
+
const bit = i * depth
|
|
75
|
+
const v = (row[bit >> 3] >> (8 - depth - (bit & 7))) & ((1 << depth) - 1)
|
|
76
|
+
return type === 3 ? v : Math.round((v * 255) / ((1 << depth) - 1))
|
|
77
|
+
}
|
|
78
|
+
// A sample as stored, unscaled, to compare with tRNS's key colour.
|
|
79
|
+
const stored = (row, i) => {
|
|
80
|
+
if (depth === 16) return row.readUInt16BE(i * 2)
|
|
81
|
+
if (depth === 8) return row[i]
|
|
82
|
+
const bit = i * depth
|
|
83
|
+
return (row[bit >> 3] >> (8 - depth - (bit & 7))) & ((1 << depth) - 1)
|
|
84
|
+
}
|
|
85
|
+
const data = new Uint8ClampedArray(width * height * 4)
|
|
86
|
+
for (let y = 0; y < height; y++) {
|
|
87
|
+
const row = rows.subarray(y * stride, (y + 1) * stride)
|
|
88
|
+
for (let x = 0; x < width; x++) {
|
|
89
|
+
const o = (y * width + x) * 4
|
|
90
|
+
let r, g, bl, al = 255
|
|
91
|
+
if (type === 3) {
|
|
92
|
+
const k = sample(row, x)
|
|
93
|
+
r = palette[k * 3]
|
|
94
|
+
g = palette[k * 3 + 1]
|
|
95
|
+
bl = palette[k * 3 + 2]
|
|
96
|
+
if (trns && k < trns.length) al = trns[k]
|
|
97
|
+
} else if (type === 0 || type === 4) {
|
|
98
|
+
r = g = bl = sample(row, x * channels)
|
|
99
|
+
if (type === 4) al = sample(row, x * channels + 1)
|
|
100
|
+
else if (trns && stored(row, x) === trns.readUInt16BE(0)) al = 0
|
|
101
|
+
} else {
|
|
102
|
+
r = sample(row, x * channels)
|
|
103
|
+
g = sample(row, x * channels + 1)
|
|
104
|
+
bl = sample(row, x * channels + 2)
|
|
105
|
+
if (type === 6) al = sample(row, x * channels + 3)
|
|
106
|
+
else if (trns && stored(row, x * 3) === trns.readUInt16BE(0) && stored(row, x * 3 + 1) === trns.readUInt16BE(2) && stored(row, x * 3 + 2) === trns.readUInt16BE(4)) al = 0
|
|
107
|
+
}
|
|
108
|
+
data[o] = r
|
|
109
|
+
data[o + 1] = g
|
|
110
|
+
data[o + 2] = bl
|
|
111
|
+
data[o + 3] = al
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return { data, width, height }
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function paeth(a, b, c) {
|
|
118
|
+
const p = a + b - c
|
|
119
|
+
const pa = Math.abs(p - a)
|
|
120
|
+
const pb = Math.abs(p - b)
|
|
121
|
+
const pc = Math.abs(p - c)
|
|
122
|
+
return pa <= pb && pa <= pc ? a : pb <= pc ? b : c
|
|
123
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
|
|
1
|
+
/**
|
|
2
|
+
* Inlay: a raster-to-vector tracer whose shapes share their edges exactly,
|
|
3
|
+
* like inlaid wood. Every boundary between two colours is traced and fitted
|
|
4
|
+
* once and used by both neighbours, so layers never gap or overlap.
|
|
5
|
+
*
|
|
6
|
+
* Pure TypeScript, no DOM: runs in browsers, workers and Node.
|
|
7
|
+
*/
|
|
2
8
|
/**
|
|
3
9
|
* SVG path data as editable nodes, and back.
|
|
4
10
|
*
|
|
@@ -33,8 +39,6 @@ type Subpath = {
|
|
|
33
39
|
export declare function parsePath(d: string): Subpath[];
|
|
34
40
|
export declare function serializePath(subs: Subpath[]): string;
|
|
35
41
|
export declare function mapPoints(subs: Subpath[], f: (p: Pt) => Pt): Subpath[];
|
|
36
|
-
//#endregion
|
|
37
|
-
//#region src/fit.d.ts
|
|
38
42
|
type FitOptions = {
|
|
39
43
|
/** Max distance in pixels between the outline and the fitted path. */
|
|
40
44
|
tolerance: number;
|
|
@@ -66,8 +70,6 @@ export declare function isFlat([p0, c1, c2, p3]: Cubic): boolean;
|
|
|
66
70
|
*/
|
|
67
71
|
export declare function fitChain(points: Pt[], closed: boolean, o: FitOptions, fine?: Pt[]): Cubic[];
|
|
68
72
|
export declare function pointAt(b: Cubic, t: number): Pt;
|
|
69
|
-
//#endregion
|
|
70
|
-
//#region src/palette.d.ts
|
|
71
73
|
/**
|
|
72
74
|
* Palette extraction for flat artwork.
|
|
73
75
|
*
|
|
@@ -131,8 +133,6 @@ export declare function quantizePalette(rgba: ArrayLike<number>, maxColors: numb
|
|
|
131
133
|
* rarest ones take their nearest kept colour when pixels are labelled.
|
|
132
134
|
*/
|
|
133
135
|
export declare function exactPalette(rgba: ArrayLike<number>, maxColors: number): string[];
|
|
134
|
-
//#endregion
|
|
135
|
-
//#region src/gradient.d.ts
|
|
136
136
|
type GradientStop = {
|
|
137
137
|
offset: number;
|
|
138
138
|
color: string;
|
|
@@ -152,10 +152,6 @@ type Gradient = {
|
|
|
152
152
|
r: number;
|
|
153
153
|
stops: GradientStop[];
|
|
154
154
|
};
|
|
155
|
-
/** A gradient's colour at a point. */
|
|
156
|
-
export declare const colorAt: (g: Gradient, x: number, y: number) => Rgb;
|
|
157
|
-
//#endregion
|
|
158
|
-
//#region src/inlay.d.ts
|
|
159
155
|
type InlayOptions = FitOptions & {
|
|
160
156
|
/** Regions smaller than speckle × speckle pixels are merged into their surroundings. */
|
|
161
157
|
speckle: number;
|
|
@@ -267,29 +263,81 @@ type InlayResult = {
|
|
|
267
263
|
width: number;
|
|
268
264
|
height: number;
|
|
269
265
|
paths: InlayPath[];
|
|
266
|
+
/** Centerline only: the stroke width measured from the ink, in px (whatever `strokeWidth` asked for). */
|
|
267
|
+
measuredWidth?: number;
|
|
270
268
|
};
|
|
271
269
|
/** The trace as an SVG document. */
|
|
272
270
|
export declare function toSvg(r: InlayResult): string;
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
271
|
+
type PresetName = 'logo' | 'illustration' | 'outline' | 'lineart' | 'centerline' | 'poster' | 'pixel';
|
|
272
|
+
/** Anything shaped like ImageData: RGBA bytes, row by row, and the size. sharp's `{ data, info }` gives `data` and `info.width`/`info.height`. */
|
|
273
|
+
type ImageLike = {
|
|
274
|
+
data: ArrayLike<number>;
|
|
275
|
+
width: number;
|
|
276
|
+
height: number;
|
|
277
|
+
};
|
|
278
|
+
type TraceOptions = {
|
|
279
|
+
/** What the image is: picks the palette, the steps before tracing and every default below. Default 'logo'. */
|
|
280
|
+
preset?: PresetName;
|
|
281
|
+
/** Colour presets: at most this many colours. */
|
|
282
|
+
colors?: number;
|
|
283
|
+
/** Colour presets: trace with exactly these colours (#rrggbb), instead of picking them from the image. */
|
|
284
|
+
palette?: string[];
|
|
285
|
+
/** Fold colours together before tracing, so the edge between them disappears: { '#from': '#into' }, traced hex to traced hex. */
|
|
286
|
+
merge?: Record<string, string>;
|
|
287
|
+
/** Regions smaller than speckle × speckle px merge into their surroundings. */
|
|
288
|
+
speckle?: number;
|
|
289
|
+
/** How far, in px, the fitted outline may stray from the pixels. */
|
|
290
|
+
tolerance?: number;
|
|
291
|
+
/** Turns sharper than this (degrees) stay corners. */
|
|
292
|
+
cornerAngle?: number;
|
|
293
|
+
/** Half-window in px of the smoothing over the pixel staircase (default 12). */
|
|
294
|
+
smoothing?: number;
|
|
295
|
+
/** Draw circles, ellipses, (rounded) rectangles and regular polygons exactly. */
|
|
296
|
+
shapes?: boolean;
|
|
297
|
+
/** Shapes drawn whole, in layers, instead of cut out to tile (cut out is what cutters want). */
|
|
298
|
+
layers?: boolean;
|
|
299
|
+
/** Thin drawn lines as stroked paths (Outline art). */
|
|
300
|
+
strokes?: boolean;
|
|
301
|
+
/** Soft shading as gradient fills. Off with your own `palette`, whose colours a gradient would leave. */
|
|
302
|
+
gradients?: boolean;
|
|
303
|
+
/** Join lines' ends across small breaks before tracing. */
|
|
304
|
+
closeGaps?: boolean;
|
|
305
|
+
/** How many times the image was enlarged before tracing: line widths and gap sizes grow with it. Default 1. */
|
|
306
|
+
scale?: number;
|
|
307
|
+
/** Ink presets (lineart, centerline): ink is darker than this (0–255), the image's own split when 'auto', adaptive when null. */
|
|
308
|
+
threshold?: number | 'auto' | null;
|
|
309
|
+
/** Ink presets: light ink on a dark background. */
|
|
310
|
+
invert?: boolean;
|
|
311
|
+
/** Centerline: the stroke width in px; measured from the ink when left out. */
|
|
312
|
+
strokeWidth?: number;
|
|
313
|
+
/** Colour presets: even out painted shading (brush and JPEG noise) before picking colours. */
|
|
314
|
+
smoothShading?: boolean;
|
|
315
|
+
/** Colour presets: darken faint painted outlines first, so the shapes they part don't merge. */
|
|
316
|
+
keepOutlines?: boolean;
|
|
317
|
+
/** Colour presets but pixel art: trace in greys only, `colors` of them (each pixel's OKLab lightness, as a grey). */
|
|
318
|
+
grayscale?: boolean;
|
|
319
|
+
};
|
|
320
|
+
/** A preset's defaults: what `trace` uses for every option not given. */
|
|
321
|
+
type PresetDefaults = Readonly<Pick<TraceOptions, 'colors' | 'speckle' | 'tolerance' | 'cornerAngle' | 'shapes' | 'layers' | 'strokes' | 'gradients' | 'closeGaps' | 'threshold'>> & {
|
|
322
|
+
/** Which kind of trace: filled colour shapes, one ink's shapes, or strokes down the ink's middle. */
|
|
323
|
+
readonly kind: 'colour' | 'ink' | 'centerline';
|
|
324
|
+
};
|
|
325
|
+
/** Every preset's defaults, as the app shows them. */
|
|
326
|
+
export declare const presets: Readonly<Record<PresetName, PresetDefaults>>;
|
|
280
327
|
/**
|
|
281
|
-
*
|
|
282
|
-
*
|
|
283
|
-
*
|
|
284
|
-
*
|
|
328
|
+
* Trace an image into filled shapes that share their edges exactly (or, with
|
|
329
|
+
* the centerline preset, strokes down the ink's middle).
|
|
330
|
+
*
|
|
331
|
+
* trace(imageData, { preset: 'logo' })
|
|
332
|
+
* trace({ data, width, height }, { preset: 'poster', colors: 16 })
|
|
285
333
|
*/
|
|
286
|
-
export declare function
|
|
287
|
-
/**
|
|
288
|
-
export declare function
|
|
289
|
-
/**
|
|
290
|
-
export declare function
|
|
291
|
-
|
|
292
|
-
|
|
334
|
+
export declare function trace(image: ImageLike, options?: TraceOptions): InlayResult;
|
|
335
|
+
/** @deprecated Since 0.4: use `trace({ data: rgba, width, height }, { preset, … })`. This form goes in 1.0. */
|
|
336
|
+
export declare function trace(rgba: ArrayLike<number>, width: number, height: number, options: InlayOptions): InlayResult;
|
|
337
|
+
/** `toSvg(trace(…))` in one call, in either form. */
|
|
338
|
+
export declare function traceInlay(image: ImageLike, options?: TraceOptions): string;
|
|
339
|
+
/** @deprecated Since 0.4: use `traceInlay({ data: rgba, width, height }, { preset, … })`. This form goes in 1.0. */
|
|
340
|
+
export declare function traceInlay(rgba: ArrayLike<number>, width: number, height: number, options: InlayOptions): string;
|
|
293
341
|
/**
|
|
294
342
|
* Edge-preserving smoothing for painted art, before its palette is picked.
|
|
295
343
|
*
|
|
@@ -333,8 +381,6 @@ export declare function smoothShading(rgba: ArrayLike<number>, width: number, he
|
|
|
333
381
|
* enough to the dark grey on either side to be folded in as a sliver.
|
|
334
382
|
*/
|
|
335
383
|
export declare function boostLines(rgba: ArrayLike<number>, width: number, height: number, radius?: number, lines?: Uint8Array): Uint8ClampedArray;
|
|
336
|
-
//#endregion
|
|
337
|
-
//#region src/centerline.d.ts
|
|
338
384
|
type CenterlineOptions = {
|
|
339
385
|
/** Ink is darker than this (0–255); 'auto' splits ink from paper by the image's histogram (Otsu); null compares each pixel with its neighbourhood instead. */
|
|
340
386
|
threshold: number | 'auto' | null;
|
|
@@ -353,25 +399,22 @@ type CenterlineOptions = {
|
|
|
353
399
|
/** How many times the drawing was enlarged before tracing: gap closing's pixel limits grow with it. Default 1. */
|
|
354
400
|
scale?: number;
|
|
355
401
|
};
|
|
356
|
-
/**
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
type CenterlineResult = {
|
|
363
|
-
width: number;
|
|
364
|
-
height: number;
|
|
365
|
-
paths: CenterlinePath[];
|
|
366
|
-
/** The stroke width measured from the ink, in pixels (whatever `strokeWidth` asked for). */
|
|
402
|
+
/**
|
|
403
|
+
* A centerline trace: the usual result, its paths stroked (`stroke` is the
|
|
404
|
+
* width, `fill` the colour, as Outline art's lines), and `measuredWidth`.
|
|
405
|
+
* @deprecated Since 0.4: it's `InlayResult`.
|
|
406
|
+
*/
|
|
407
|
+
type CenterlineResult = InlayResult & {
|
|
367
408
|
measuredWidth: number;
|
|
368
409
|
};
|
|
369
|
-
/**
|
|
410
|
+
/**
|
|
411
|
+
* Trace to an SVG string; `centerline` gives the strokes as data.
|
|
412
|
+
* @deprecated Since 0.4: use `toSvg(trace(image, { preset: 'centerline' }))`.
|
|
413
|
+
*/
|
|
370
414
|
export declare const traceCenterline: (rgba: ArrayLike<number>, w: number, h: number, o: CenterlineOptions) => string;
|
|
371
|
-
|
|
415
|
+
/** @deprecated Since 0.4: `toSvg` draws stroked paths the same way. */
|
|
416
|
+
export declare const centerlineToSvg: (r: InlayResult) => string;
|
|
372
417
|
export declare function centerline(rgba: ArrayLike<number>, w: number, h: number, o: CenterlineOptions): CenterlineResult;
|
|
373
|
-
//#endregion
|
|
374
|
-
//#region src/symmetry.d.ts
|
|
375
418
|
/**
|
|
376
419
|
* Symmetry: find a likely mirror axis or rotational order in a raster.
|
|
377
420
|
*
|
|
@@ -419,5 +462,4 @@ type SymmetryOptions = {
|
|
|
419
462
|
* no foreground.
|
|
420
463
|
*/
|
|
421
464
|
export declare function detectSymmetry(rgba: ArrayLike<number>, w: number, h: number, options?: SymmetryOptions): Symmetry[];
|
|
422
|
-
|
|
423
|
-
export type { CenterlineOptions, CenterlinePath, CenterlineResult, Cubic, FitOptions, Gradient, GradientStop, InlayOptions, InlayPath, InlayResult, MirrorSymmetry, PathNode, Pt, Rgb, RotationalSymmetry, Subpath, Symmetry, SymmetryOptions };
|
|
465
|
+
export type { CenterlineOptions, CenterlineResult, Cubic, FitOptions, Gradient, GradientStop, ImageLike, InlayOptions, InlayPath, InlayResult, MirrorSymmetry, PathNode, PresetDefaults, PresetName, Pt, Rgb, RotationalSymmetry, Subpath, Symmetry, SymmetryOptions, TraceOptions };
|