jankovicsandras / imagetracerjs

Simple raster image tracer and vectorizer written in JavaScript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Can't get the Node CLI to work

JoanCharmant opened this issue · comments

Hi,
I have an issue running imagetracerjs through Node CLI.

Here is the command I use and the result:
> node nodecli ../smiley.png

(node:7248) [DEP0005] DeprecationWarning: Buffer() is deprecated due to security and usability issues. Please use the Buffer.alloc(), Buffer.allocUnsafe(), or Buffer.from() methods instead.

<path>\imagetracerjs\nodecli/../smiley.png.svg was saved!

The file smiley.png.svg is indeed saved, but contains garbage. I attach the output SVG (with txt extension so github accepts it), and a png version for illustration.

smiley.png.svg.txt
smiley png svg

The browser only version works fine, for example using imagetracer_examples.html and the other test files.

I tested this with the latest commit and with v 1.2.3 and I get the same result. Same issue with other inputs, although the final path are different.

Windows 10 x64, Node --version: v10.15.0.

Can anyone confirm that the node cli works for them and what is their setup?
Thanks

Hi Joan,

Thanks for reporting this error! I haven't tested this yet, but as you see, Buffer is not used in imagetracer.js directly, but in nodecli/PNGReader.js .

PNG.js and PNGReader.js are saved on 2016-03-04 from
https://github.com/arian/pngjs
These are used to load an image to ImageData with Node.js, as Node.js has no built-in canvas. They are not a part of the ImageTracer project.

You can try to use another Node.js canvas library or image-to-ImageData loader, (maybe this, I haven't tested: https://github.com/photopea/UPNG.js ) or try to fix the Buffer allocation.

I'll try to find a solution when I'll have some time. :)

Hi,
I dug into this a bit more and the issue is not the Buffer constructor deprecation (Tested with various older Node and still had the issue).

It is actually linked to issue #18. For sources without alpha channel, (like the "smiley.png" in the main directory of imagetracerjs and the test image I was using), PNG.js will produce a buffer with only RGB data, and so it doesn't conform to the ImageData object ImageTracer.imagedataToSVG() is expecting.

The spec at Mozilla says the buffer should contain RGBA values so this could be seen as an issue in PNG.js.

The returned object of PNG.js has an .alpha field that could be tested and if false nodecli could throw an error to the console or maybe even inject the alpha channel in the buffer.

Another check could be done in imagedataToSVG() to verify that the buffer length matches width×height×4 and throw an error otherwise.

Thanks for investigating this issue! ☺

Despite this was not an error in ImageTracer, I decided to add an enhancement: extending an RGB ImageData.data to RGBA in colorquantization() in version 1.2.5 .