hidekatsu-izuno / wmf2svg

WMF to SVG Converting Tool & Library for Java
http://hidekatsu-izuno.github.io/wmf2svg/
Apache License 2.0
81 stars 27 forks source link

(Harmless?) exceptions when converting certain .wmf files #11

Open hshafie opened 3 years ago

hshafie commented 3 years ago

Hello,

First of all, I'm a long time user of your wmf2svg library and I'm very thankful to you for your hard work. I really wish an equivalent library also would exist for EMF.

When I try to convert this .wmf file as follows:

java -jar ../../tapes/wmf2svg-0.9.11.jar  Dessin3.wmf Dessin3.svg

The conversion works, but I get several of this exception:

java.io.EOFException
        at javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:339)
        at com.sun.imageio.plugins.bmp.BMPImageReader.read1Bit(BMPImageReader.java:940)
        at com.sun.imageio.plugins.bmp.BMPImageReader.read(BMPImageReader.java:787)
        at javax.imageio.ImageIO.read(ImageIO.java:1422)
        at javax.imageio.ImageIO.read(ImageIO.java:1326)
        at net.arnx.wmf2svg.util.ImageUtil$ImageIOConverter.convert(ImageUtil.java:62)
        at net.arnx.wmf2svg.util.ImageUtil.convert(ImageUtil.java:44)
        at net.arnx.wmf2svg.gdi.svg.SvgGdi.bmpToSvg(SvgGdi.java:1567)
        at net.arnx.wmf2svg.gdi.svg.SvgGdi.stretchDIBits(SvgGdi.java:1203)
        at net.arnx.wmf2svg.gdi.svg.SvgGdi.dibStretchBlt(SvgGdi.java:428)
        at net.arnx.wmf2svg.gdi.wmf.WmfParser.parse(WmfParser.java:597)
        at net.arnx.wmf2svg.Main.main(Main.java:141)

This issue may be reproduced whatever the Java platform and is not a regression as it was already there in wmf2svg version 0.9.7.

I made the following quick and dirty change to "src/net/arnx/wmf2svg/gdi/svg/SvgGdi.java" to workaround this issue:

cd src/net/arnx/wmf2svg/gdi/svg/

diff -u SvgGdi.java.ORI SvgGdi.java

--- SvgGdi.java.ORI 2021-07-12 13:37:16.000000000 +0200
+++ SvgGdi.java 2021-09-28 11:07:51.305574647 +0200
@@ -213,6 +213,8 @@
        dc = new SvgDc(this);

        Element root = doc.getDocumentElement();
+                // Added by XMLmind Software.
+                root.setAttribute("version", "1.0");
        root.setAttribute("xmlns", "http://www.w3.org/2000/svg");
        root.setAttribute("xmlns:xlink", "http://www.w3.org/1999/xlink");

@@ -1644,6 +1646,7 @@
        long clrUsed = (dib[32] & 0xff) + ((dib[33] & 0xff) << 8)
                + ((dib[34] & 0xff) << 16) + ((dib[35] & 0xff) << 24);

+                /****************************** Hidekatsu Izuno's ORIGNAL CODE.
        switch (biBitCount) {
        case 1:
            bfOffBits += (clrUsed == 0L ? 2 : clrUsed) * 4;
@@ -1655,7 +1658,39 @@
            bfOffBits += (clrUsed == 0L ? 256 : clrUsed) * 4;
            break;
        }
+                **************************************************************/

+                // XMLmind Software CHANGE.
+       switch (biBitCount) {
+       case 1:
+       case 4:
+       case 8:
+                    if (clrUsed == 0) {
+                        clrUsed = (1 << biBitCount);
+
+                        if (biBitCount == 1) {
+                            // Insert a dummy color palette because
+                            // ImageIO.read requires having it (otherwise
+                            // error "Map size (0) must be >= 1").
+
+                            int biSize2 = (int) biSize;
+                            byte[] dib2 = new byte[dib.length+8];
+                            System.arraycopy(dib, 0, dib2, 0, biSize2);
+                            System.arraycopy(dib, biSize2, dib2, biSize2 + 8, 
+                                             dib.length - biSize2);
+                            dib = dib2;
+
+                            byte[] data2 = new byte[data.length+8];
+                            System.arraycopy(data, 0, data2, 0, 14);
+                            data = data2;
+                        }
+                    }
+                    bfOffBits += clrUsed*4;
+                    break;
+                    
+                // Otherwise, no palette.
+       }
+                
        data[10] = (byte) (bfOffBits & 0xff);
        data[11] = (byte) ((bfOffBits >> 8) & 0xff);
        data[12] = (byte) ((bfOffBits >> 16) & 0xff);

Note that adding the version="1.0" attribute to the svg root is not related to this is issue. It's just nice having it.

Dessin3.wmf.zip

hidekatsu-izuno commented 1 year ago

Did not reproduce on JDK1.7.

hshafie commented 1 year ago

Please find attached to this message image9.wmf which seems to trigger the same bug. This WMF is loaded just fine by MS-Word and LibreOffice. See attached screenshot.

java -jar  ../tapes/wmf2svg-0.9.11.jar image9.wmf  image9.svg 

I get more or less the following stack trace whatever the Java runtime I use on my Linux box: Java 8, Java 11 or the very latest Java 20. (Don't have Java 17 handy, sorry.)

java.io.EOFException
        at java.desktop/javax.imageio.stream.ImageInputStreamImpl.readFully(ImageInputStreamImpl.java:393)
        at java.desktop/com.sun.imageio.plugins.bmp.BMPImageReader.read1Bit(BMPImageReader.java:1119)
        at java.desktop/com.sun.imageio.plugins.bmp.BMPImageReader.read(BMPImageReader.java:958)
        at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1466)
        at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1363)
        at net.arnx.wmf2svg.util.ImageUtil$ImageIOConverter.convert(ImageUtil.java:62)
        at net.arnx.wmf2svg.util.ImageUtil.convert(ImageUtil.java:44)
        at net.arnx.wmf2svg.gdi.svg.SvgGdi.bmpToSvg(SvgGdi.java:1567)
        at net.arnx.wmf2svg.gdi.svg.SvgGdi.stretchDIBits(SvgGdi.java:1203)
        at net.arnx.wmf2svg.gdi.svg.SvgGdi.dibStretchBlt(SvgGdi.java:428)
        at net.arnx.wmf2svg.gdi.wmf.WmfParser.parse(WmfParser.java:597)
        at net.arnx.wmf2svg.Main.main(Main.java:141)

Note that the bug fix I suggested in my first message prevents Java from throwing the above exception, but generates an SVG graphic which is displayed as a black rectangle by Inkscape, Batik, Firefox and as a very, very "fuzzy", unusable, graphic by Chrome. In other words, the bug fix I suggested is not good enough.

Thank you so much for your patience and for your help!

image9.wmf.zip

image9_wmf_in_word_doc