Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Publish TS version #9

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ browser.js
bundle.js*
*.tgz
vectorize-pixelart-bundle.js
vectorize-pixelart.js
.travis.yml
.github
src
tsconfig.json
tsconfig.json
3 changes: 0 additions & 3 deletions contour-tracing.js

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
7 changes: 3 additions & 4 deletions src/contour-tracing.ts
Original file line number Diff line number Diff line change
@@ -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],
Expand Down Expand Up @@ -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) {
Expand Down
16 changes: 8 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -37,7 +37,7 @@ export class SVG extends Image {
return '</svg>\n'
}

pixel (y: number, x: number, pixel: Pixel): string {
pixel (y: number, x: number, pixel: RGBA): string {
if (pixel[3] < 255) {
return ''
}
Expand All @@ -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(', ')

Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
}
11 changes: 4 additions & 7 deletions src/vectorize-pixelart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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]

Expand All @@ -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())
Expand Down
4 changes: 2 additions & 2 deletions test/tracing.test.ts
Original file line number Diff line number Diff line change
@@ -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 = [
Expand Down Expand Up @@ -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]
}
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
"strict": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "test"]
"exclude": ["node_modules"]
}
3 changes: 0 additions & 3 deletions utils.js

This file was deleted.