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

Create colored svg in c++ code. #5

Merged
merged 10 commits into from
Feb 2, 2022
Merged
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
34 changes: 33 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,33 @@
.DS_Store
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

/.idea/

# dependencies
/**/node_modules
/**/.pnp
/**/.pnp.js

# testing
/**/coverage

# production
/**/build
/**/dist

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*

!/**/.gitkeep

/**/.cache

.vscode
.vs
7 changes: 7 additions & 0 deletions build.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if (Test-Path $dist/index.js) {
Remove-Item dist/index.js
}

em++ -O3 -std=c++20 (ls wasm/*.c) --post-js src/potrace.js -s WASM=1 -s "ALLOW_MEMORY_GROWTH=1" -s "SINGLE_FILE=1" -o dist/index.js

Add-Content -Path dist/index.js -Value "export { potrace, ready as init };"
2 changes: 1 addition & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"

cd $DIR && rm -f lib/*

cd $DIR && emcc -O3 wasm/*.c --post-js src/potrace.js -s WASM=1 -s EXPORTED_FUNCTIONS='["_start"]' -s "ALLOW_MEMORY_GROWTH=1" -s "SINGLE_FILE=1" -o dist/index.js
cd $DIR && em++ -O3 -std=gnu++20 wasm/*.c --post-js src/potrace.js -s WASM=1 -s EXPORTED_FUNCTIONS='["_start"]' -s "ALLOW_MEMORY_GROWTH=1" -s "SINGLE_FILE=1" -o dist/index.js
Binary file added demo/avatar.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
22 changes: 21 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,30 @@
<script src="./script.js" type="module"></script>
</head>
<body>
<img src="./snail.png" width="256" height="256" alt="A snail." />
<h1>ESM Potrace Wasm Demo</h1>

<h2>Raster illustration</h2>

<img src="./logo.png" width="127" height="138" alt="The potrace logo." />

<h3>Color</h3>
<div id="div1"></div>
<pre id="pre1"></pre>

<h3>Black and white</h3>
<div id="div2"></div>
<pre id="pre2"></pre>

<h2>Raster photo</h2>

<img src="./avatar.jpg" width="400" height="400" alt="An avatar." />

<h3>Color</h3>
<div id="div3"></div>
<pre id="pre3"></pre>

<h3>Black and white</h3>
<div id="div4"></div>
<pre id="pre4"></pre>
</body>
</html>
Binary file added demo/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
48 changes: 44 additions & 4 deletions demo/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,76 @@ import { potrace, init } from '../dist/index.js';
(async () => {
const pre1 = document.querySelector('#pre1');
const pre2 = document.querySelector('#pre2');
const pre3 = document.querySelector('#pre3');
const pre4 = document.querySelector('#pre4');
const div1 = document.querySelector('#div1');
const div2 = document.querySelector('#div2');
const div3 = document.querySelector('#div3');
const div4 = document.querySelector('#div4');

const blob = await fetch('./snail.png').then((response) => response.blob());
const blob1 = await fetch('./logo.png').then((response) => response.blob());
const blob2 = await fetch('./avatar.jpg').then((response) => response.blob());

await init();

potrace(blob, {
turdsize: 1,
potrace(blob1, {
turdsize: 2,
turnpolicy: 4,
alphamax: 1,
opticurve: 1,
opttolerance: 0.2,
pathonly: false,
extractcolors: true,
posterizelevel: 2,
posterizationalgorithm: 0, // 0: simple, 1: interpolation
}).then((svg) => {
div1.innerHTML = svg;
svg = svg.replaceAll('><', '>\n<');
pre1.textContent = svg + '\n\n(' + svg.length + ')';
});

potrace(blob, {
potrace(blob1, {
turdsize: 2,
turnpolicy: 4,
alphamax: 1,
opticurve: 1,
opttolerance: 0.2,
pathonly: false,
extractcolors: false,
}).then((svg) => {
div2.innerHTML = svg;
svg = svg.replaceAll('><', '>\n<');
pre2.textContent = svg + '\n\n(' + svg.length + ')';
});

potrace(blob2, {
turdsize: 2,
turnpolicy: 4,
alphamax: 1,
opticurve: 1,
opttolerance: 0.2,
pathonly: false,
extractcolors: true,
posterizelevel: 3,
posterizationalgorithm: 1, // 0: simple, 1: interpolation
}).then((svg) => {
div3.innerHTML = svg;
svg = svg.replaceAll('><', '>\n<');
pre3.textContent = svg + '\n\n(' + svg.length + ')';
});

potrace(blob2, {
turdsize: 2,
turnpolicy: 4,
alphamax: 1,
opticurve: 1,
opttolerance: 0.2,
pathonly: false,
extractcolors: false,
}).then((svg) => {
div4.innerHTML = svg;
svg = svg.replaceAll('><', '>\n<');
pre4.textContent = svg + '\n\n(' + svg.length + ')';
});

})();
Binary file removed demo/snail.png
Binary file not shown.
5 changes: 5 additions & 0 deletions demo/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,8 @@ video {
height: auto;
max-width: 100%;
}

pre {
max-height: 10rem;
overflow: scroll;
}
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion dist/index.min.js

This file was deleted.

Loading