diff --git a/.npmignore b/.npmignore index 547281a..ffadfa3 100644 --- a/.npmignore +++ b/.npmignore @@ -5,6 +5,8 @@ browser.js bundle.js* *.tgz vectorize-pixelart-bundle.js +vectorize-pixelart.js .travis.yml +.github src -tsconfig.json \ No newline at end of file +tsconfig.json diff --git a/contour-tracing.js b/contour-tracing.js deleted file mode 100644 index c3a554a..0000000 --- a/contour-tracing.js +++ /dev/null @@ -1,3 +0,0 @@ -// Left for backward compatability, please do not rely on it -const { ContourTracing } = require('./lib') -module.exports = ContourTracing diff --git a/package.json b/package.json index 2d2d8b5..fe01a13 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vectorize-pixelart", - "version": "0.2.0", + "version": "1.0.0", "description": "Convert raster pixel art graphics to vector", "main": "lib/index.js", "types": "lib/index.d.ts", diff --git a/src/contour-tracing.ts b/src/contour-tracing.ts index f5e6d4e..8977beb 100644 --- a/src/contour-tracing.ts +++ b/src/contour-tracing.ts @@ -1,7 +1,7 @@ -import { Coord, Path, Pixel, PNGImageData } from './utils' +import { Coord, Path, RGBA, PNGImageData } from './utils' type Direction = Coord -type ContourFoundCb = (contour: Path, pixel: Pixel) => void +type ContourFoundCb = (contour: Path, pixel: RGBA) => void const DIRECTIONS: Direction[] = [ [1, 0], @@ -74,8 +74,7 @@ export class ContourTracing { } addContour (contour: Path, y: number, x: number, startDirection: number, endDirection: number): void { - // console.log("addContour: ", y, x, startDirection, endDirection); - + // console.log("addContour: ", y, x, startDirection, endDirection); if (startDirection === endDirection) { return } for (let direction = (startDirection + D_MOD - 1) % D_MOD, firstRun = true; firstRun || direction !== endDirection; direction = (direction + 1) % D_MOD, firstRun = false) { diff --git a/src/utils.ts b/src/utils.ts index 561a533..e5fb726 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -4,11 +4,11 @@ import type { PNG } from 'pngjs' const DEFAULT_MULTIPLIER = 1 -export type Pixel = [number, number, number, number] +export type RGBA = [number, number, number, number] export type Coord = [number, number] export type Path = Coord[] -abstract class Image { +export abstract class Image { protected readonly height: number protected readonly width: number protected readonly multiplier: number @@ -22,7 +22,7 @@ abstract class Image { abstract header (): string abstract footer (): string // abstract pixel(y: number, x: number, pixel: Pixel): string | undefined - abstract path (contour: Path, pixel: Pixel): string + abstract path (contour: Path, pixel: RGBA): string } export class SVG extends Image { @@ -37,7 +37,7 @@ export class SVG extends Image { return '\n' } - pixel (y: number, x: number, pixel: Pixel): string { + pixel (y: number, x: number, pixel: RGBA): string { if (pixel[3] < 255) { return '' } @@ -49,7 +49,7 @@ width="${1 * this.multiplier}" height="${1 * this.multiplier}" \ style="fill:rgba(${rgba})" />\n` } - path (contour: Path, pixel: Pixel): string { + path (contour: Path, pixel: RGBA): string { const m = this.multiplier const rgba = pixel.join(', ') @@ -94,7 +94,7 @@ showpage ` } - path (contour: Path, pixel: Pixel): string { + path (contour: Path, pixel: RGBA): string { const m = this.multiplier const height = this.height @@ -143,8 +143,8 @@ export class PNGImageData { return true } - getPixel (y: number, x: number): Pixel { + getPixel (y: number, x: number): RGBA { const offset = (y * this.width + x) * BYTES_PER_PIXEL - return Array.prototype.slice.call(this.data, offset, offset + BYTES_PER_PIXEL) as Pixel + return Array.prototype.slice.call(this.data, offset, offset + BYTES_PER_PIXEL) as RGBA } } diff --git a/src/vectorize-pixelart.ts b/src/vectorize-pixelart.ts index d5d8bbb..ef721f5 100755 --- a/src/vectorize-pixelart.ts +++ b/src/vectorize-pixelart.ts @@ -2,7 +2,7 @@ import { createReadStream, createWriteStream } from 'fs' import { PNG } from 'pngjs' -import { SVG, EPS, PNGImageData, Path, Pixel } from './utils' +import { SVG, EPS, PNGImageData, Path, RGBA } from './utils' import * as Process from 'process' import { ContourTracing } from './contour-tracing' @@ -11,7 +11,6 @@ const OutputFileFormats: { [_: string]: any } = { eps: EPS } -const targetSize = 2 ** 23 const inputFileName = Process.argv[2] const outputFileName: string = Process.argv[3] @@ -31,16 +30,14 @@ createReadStream(inputFileName) // TODO check files exists const vectorOut = createWriteStream(outputFileName) - const pixelMultiplier = Math.sqrt(targetSize / (this.height * this.width)) - const image = new PNGImageData(this) - const vectorFormatter = new VectorFormatterClass(this.height, this.width, pixelMultiplier) + const vectorFormatter = new VectorFormatterClass(this.height, this.width) vectorOut.write(vectorFormatter.header()) const tracer = new ContourTracing(image) - tracer.traceContours((contour: Path, pixel: Pixel) => { - vectorOut.write(vectorFormatter.path(contour, pixel)) + tracer.traceContours((contour: Path, color: RGBA) => { + vectorOut.write(vectorFormatter.path(contour, color)) }) vectorOut.write(vectorFormatter.footer()) diff --git a/test/tracing.test.ts b/test/tracing.test.ts index 30428a6..216be86 100644 --- a/test/tracing.test.ts +++ b/test/tracing.test.ts @@ -1,6 +1,6 @@ import * as test from 'tape' import { ContourTracing } from '../src/contour-tracing' -import { Coord, Path, PNGImageData } from '../src/utils' +import { Coord, Path, RGBA, PNGImageData } from '../src/utils' test('trace contours of sample image', (t) => { const imageData = [ @@ -66,7 +66,7 @@ constructor (imageArray: number[], height: number, width: number) { return this.image[this.getOffset(y1, x1)] === this.image[this.getOffset(y2, x2)] } - getPixel (y: number, x: number) { + getPixel (y: number, x: number): RGBA { const gray = 255 * this.image[this.getOffset(y, x)] return [gray, gray, gray, 255] } diff --git a/tsconfig.json b/tsconfig.json index 42ede86..47a7c18 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,5 +8,5 @@ "strict": true }, "include": ["src/**/*"], - "exclude": ["node_modules", "test"] + "exclude": ["node_modules"] } diff --git a/utils.js b/utils.js deleted file mode 100644 index 06d29e2..0000000 --- a/utils.js +++ /dev/null @@ -1,3 +0,0 @@ -// Left for backward compatability, please do not rely on it -const { SVG, EPS, PNGImageData } = require('./lib') -module.exports = { SVG, EPS, PNGImageData }