Page 1 of 1

SVG convert size issue

Posted: 2016-10-04T07:16:28-07:00
by gringo974
Hello,

I'm facing a SVG convert size issue with SVG that uses metric system units (mm, cm, in).

Here is a simple SVG that draws a red square of 10mmx10mm :

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="10mm" height="10mm">
<rect x="0" y="0" width="10mm" height="10mm" style="fill:rgb(255,0,0)" />
</svg>
The 90-dpi convert creates a correct 35x35 pixels image.
But all others dpi give a wrong size!

For example:
  • The 300-dpi convert creates a 393x393 pixels image intead of 118x118 pixels (300*10/25.4)
  • The 600-dpi convert creates a 1573x1573 pixels image intead of 236x236 pixels (600*10/25.4)
The issue is located in the SVG coder, in the svg.c file:

Code: Select all

(line 3003) image->columns=image->resolution.x*dimension_info.width/90.0;
which give image->columns=300*118/90=393 pixels
Actually the resolution is applied twice, one time on the dimension_info and one time on the image->columns.

So to quick fix this issue, I commented the rsvg_handle_set_dpi_x_y call :

Code: Select all

(line 2970), //rsvg_handle_set_dpi_x_y(svg_handle,image->resolution.x,image->resolution.y);
like this the dimension_info.width remains to 35, which gives me:
image->columns=300*35/90=116 pixels (which is better but due to approximation not really accurate)

It could be nice if you could provide a accurate fix!

Re: SVG convert size issue

Posted: 2016-10-04T09:14:19-07:00
by magick
Thanks for the problem report. We can reproduce it and will have a patch to fix it in GIT master branch @ https://github.com/ImageMagick/ImageMagick later today. The patch will be available in the beta releases of ImageMagick @ http://www.imagemagick.org/download/beta/ by sometime tomorrow.

Re: SVG convert size issue

Posted: 2016-10-05T06:54:21-07:00
by gringo974
I just tested your fix and it works fine for SVG that uses metric system units (mm, cm, in)
BUT it doesn't work anymore for SVG that uses pixel units :(!

You can reproduce the issue with this SVG:

Code: Select all

<?xml version="1.0" encoding="utf-8"?>
<svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="100" height="100">
<rect x="0" y="0" width="100" height="100"
  style="fill:rgb(255,0,0);stroke:black;stroke-width:50;" />
</svg>
With your patch, the 90-dpi convert creates a 100x100 pixels image
BUT the 300-dpi convert also creates a 100x100 pixels cropped image! (intead of 333x333 pixels without your patch).

Please fix the patch!

Re: SVG convert size issue

Posted: 2016-10-05T07:55:48-07:00
by magick
We leverage the RSVG delegate library to render SVG. If there is a bug, its likely within the RSVG delegate library. The rsvg-convert program is distiributed with the RSVG library and it returns a 100x100 image as well. It is likely treating the pixel units as absolute and resolution independent.

Code: Select all

$ rsvg-convert -d 300 -p 300 -o out.png pixel.svg

$ identify out.png
out.png PNG 100x100 100x100+0+0 8-bit sRGB 231B 0.000u 0:00.000

$ rsvg-convert -d 300 -p 300 -o out.png metric.svg

$ identify out.png
out.png PNG 118x118 118x118+0+0 8-bit sRGB 354B 0.000u 0:00.009

Re: SVG convert size issue

Posted: 2016-10-05T08:23:20-07:00
by gringo974
Indeed, this is weird...

But before your patch, the IM convert did the job:

Code: Select all

$ convert --version
Version: ImageMagick 7.0.3-0 Q8 x64 2016-09-05 http://www.imagemagick.org

$ convert -density 300 metric.svg out.png
$ identify out.png
out.png PNG 393x393 393x393+0+0 8-bit sRGB 2c 400B 0.000u 0:00.000

$ convert -density 300 pixel.svg out.png
$ identify out.png
out.png PNG 333x333 333x333+0+0 8-bit sRGB 5c 541B 0.000u 0:00.000
Moreover, with your patch, the pixel out.png is cropped. Not with rsvg-convert..

Re: SVG convert size issue

Posted: 2016-10-05T08:47:06-07:00
by magick
> But before your patch, the IM convert did the job:

Not really. If you recall we computed the image size from the value returned by rsvg_handle_get_dimensions(). However, this failed for metric units per your first post. Its still looks like a bug in librsvg. You could try installing 'inkscape'. If its available, ImageMagick uses it to render SVG. It returns expected results for both your unit tests.

Re: SVG convert size issue

Posted: 2016-10-06T07:59:13-07:00
by gringo974
  • Did you see the cropped image issue on magick convert for pixel.svg file (300-dpi)?
    If you don't want to investigate this issue, please revert your patch for metric units, I'd rather the previous behavior before my report (Most of the svg files didn't use metric units.).
  • Your yesterday's commit on svg.c didn't compile: 9>..\..\ImageMagick\coders\svg.c(2984): error C2059: syntax error : ')'
    and it doesn't change anything on my 2 unit tests.
  • I tried to use delegated inkscape to render svg but I encountered several issues:
    First, the ExternalDelegateCommand() call in svg.c returns a status=1 on my Windows 8, so the coder didn't use the inkscape image. I didn't investigate why, I just by-passed it.
    Then, the default inkscape command in delegates.xml uses --export-eps that created me a 500x500 image for my pixel.svg file (300-dpi).

    Finally, I managed to get a 334x334 image if I use the --export-png inkscape parameter instead.

Re: SVG convert size issue

Posted: 2016-10-06T14:47:48-07:00
by dlemstra
Can you check if my latest patch resolves your issue?

Re: SVG convert size issue

Posted: 2016-10-07T07:30:02-07:00
by gringo974
Everything is now resolved for me.
Thank you very much for your support :)