You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Mark Riley <ma...@seriousintegrated.com> on 2013/08/23 23:41:05 UTC

SVG BufferedImage Quality

I have a question about how to improve the quality of the images I'm generating for buffered images.

[cid:image001.png@01CEA00E.CBA3F7B0]

The left image is the image I generated with Batik while the right is the original. How do I generate a Buffered Image of the same quality? My code currently looks like this:

Float width;
        Float height;

        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buf = new byte[image.getStream().available()];
            int n = 0;
            while ((n = image.getStream().read(buf)) >= 0) {
                baos.write(buf, 0, n);
            }
            byte[] content = baos.toByteArray();


            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
            Document doc = f.createDocument(image.getFilename(), new ByteArrayInputStream(content));
            Element element = doc.getDocumentElement();
            if (element.hasAttributeNS(null, "viewBox")) {
                String[] parts = element.getAttribute("viewBox").split("\\ ");
                width = new Float(parts[2]);
                height = new Float(parts[3]);
            } else if (element.hasAttributeNS(null, "height") && element.hasAttributeNS(null, "width")) {
                height = new Float(element.getAttributeNS(null, "height"));
                width = new Float(element.getAttributeNS(null, "width"));
            } else {
                width = new Float(100);
                height = new Float(100);
            }

            ShipTranscoder transcoder = new ShipTranscoder();
            TranscodingHints hints = new TranscodingHints();
            DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
            hints.put(ImageTranscoder.KEY_WIDTH, width);
            hints.put(ImageTranscoder.KEY_HEIGHT, height);
            hints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION, impl);
            hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, SVGConstants.SVG_NAMESPACE_URI);
            hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, SVGConstants.SVG_NAMESPACE_URI);
            hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT, SVGConstants.SVG_SVG_TAG);
            hints.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, false);
            transcoder.setTranscodingHints(hints);
            transcoder.transcode(new TranscoderInput(new ByteArrayInputStream(content)), null);
            image.setImage(transcoder.getImage());

        } catch (Throwable e) {
            Exceptions.printStackTrace(e);
            String error = "Invalid input stream for SVG file type.";
            setError(SHIPPROPKEY.IMAGE_SOURCEFILE, error);
            LOGGER.log(Level.SEVERE, "        {0}\n", error);
            throw new MashException(getClass().getSimpleName()
}

class ShipTranscoder extends ImageTranscoder {

        private BufferedImage image = null;

        @Override
        public BufferedImage createImage(int w, int h) {
            image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            return image;
        }

        @Override
        public void writeImage(BufferedImage img, TranscoderOutput out) {
        }

        public BufferedImage getImage() {
            return image;
        }
    }

Thanks,
Mark

Re: SVG BufferedImage Quality

Posted by jonathan wood <jo...@gmail.com>.
Hi Mark,

  As far as I know, ImageTranscoder does not supply specific antialias
hints.  Maybe you can try a shape-rendering attribute on the svg?

http://www.w3.org/TR/SVG/painting.html#ShapeRenderingProperty




On Tue, Aug 27, 2013 at 6:27 PM, Mark Riley <
mark.riley@seriousintegrated.com> wrote:

> I’m still struggling to figure out a way to improve the quality of my SVG
> images in Batik.****
>
> ** **
>
> It seems this maybe an Antialiasing issue? I can’t seem to find a
> transcoder hint that will improve this.****
>
> ** **
>
> Thanks,****
>
> Mark****
>
> ** **
>
> *From:* Mark Riley [mailto:mark.riley@seriousintegrated.com]
> *Sent:* Friday, August 23, 2013 2:41 PM
> *To:* batik-users@xmlgraphics.apache.org
> *Subject:* SVG BufferedImage Quality****
>
> ** **
>
> I have a question about how to improve the quality of the images I’m
> generating for buffered images. ****
>
> ** **
>
> ****
>
> ** **
>
> The left image is the image I generated with Batik while the right is the
> original. How do I generate a Buffered Image of the same quality? My code
> currently looks like this:****
>
> ** **
>
> Float width;****
>
>         Float height;****
>
> ** **
>
>         try {****
>
>             ByteArrayOutputStream baos = new ByteArrayOutputStream();****
>
>             byte[] buf = new byte[image.getStream().available()];****
>
>             int n = 0;****
>
>             while ((n = image.getStream().read(buf)) >= 0) {****
>
>                 baos.write(buf, 0, n);****
>
>             }****
>
>             byte[] content = baos.toByteArray();****
>
> ** **
>
> ** **
>
>             String parser = XMLResourceDescriptor.getXMLParserClassName();
> ****
>
>             SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);**
> **
>
>             Document doc = f.createDocument(image.getFilename(), new
> ByteArrayInputStream(content));****
>
>             Element element = doc.getDocumentElement();****
>
>             if (element.hasAttributeNS(null, "viewBox")) {****
>
>                 String[] parts = element.getAttribute("viewBox").split("\\
> ");****
>
>                 width = new Float(parts[2]);****
>
>                 height = new Float(parts[3]);****
>
>             } else if (element.hasAttributeNS(null, "height") &&
> element.hasAttributeNS(null, "width")) {****
>
>                 height = new Float(element.getAttributeNS(null, "height"));
> ****
>
>                 width = new Float(element.getAttributeNS(null, "width"));*
> ***
>
>             } else {****
>
>                 width = new Float(100);****
>
>                 height = new Float(100);****
>
>             }****
>
> ** **
>
>             ShipTranscoder transcoder = new ShipTranscoder();****
>
>             TranscodingHints hints = new TranscodingHints();****
>
>             DOMImplementation impl =
> SVGDOMImplementation.getDOMImplementation();****
>
>             hints.put(ImageTranscoder.KEY_WIDTH, width);****
>
>             hints.put(ImageTranscoder.KEY_HEIGHT, height);****
>
>             hints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION, impl);****
>
>             hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI,
> SVGConstants.SVG_NAMESPACE_URI);****
>
>             hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI,
> SVGConstants.SVG_NAMESPACE_URI);****
>
>             hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT,
> SVGConstants.SVG_SVG_TAG);****
>
>             hints.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, false);**
> **
>
>             transcoder.setTranscodingHints(hints);****
>
>             transcoder.transcode(new TranscoderInput(new
> ByteArrayInputStream(content)), null);****
>
>             image.setImage(transcoder.getImage());****
>
> ** **
>
>         } catch (Throwable e) {****
>
>             Exceptions.printStackTrace(e);****
>
>             String error = "Invalid input stream for SVG file type.";****
>
>             setError(SHIPPROPKEY.IMAGE_SOURCEFILE, error);****
>
>             LOGGER.log(Level.SEVERE, "        {0}\n", error);****
>
>             throw new MashException(getClass().getSimpleName()****
>
> }****
>
> ** **
>
> class ShipTranscoder extends ImageTranscoder {****
>
> ** **
>
>         private BufferedImage image = null;****
>
> ** **
>
>         @Override****
>
>         public BufferedImage createImage(int w, int h) {****
>
>             image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);*
> ***
>
>             return image;****
>
>         }****
>
> ** **
>
>         @Override****
>
>         public void writeImage(BufferedImage img, TranscoderOutput out) {*
> ***
>
>         }****
>
> ** **
>
>         public BufferedImage getImage() {****
>
>             return image;****
>
>         }****
>
>     }****
>
> ** **
>
> Thanks,****
>
> Mark****
>

RE: SVG BufferedImage Quality

Posted by Mark Riley <ma...@seriousintegrated.com>.
I'm still struggling to figure out a way to improve the quality of my SVG images in Batik.

It seems this maybe an Antialiasing issue? I can't seem to find a transcoder hint that will improve this.

Thanks,
Mark

From: Mark Riley [mailto:mark.riley@seriousintegrated.com]
Sent: Friday, August 23, 2013 2:41 PM
To: batik-users@xmlgraphics.apache.org
Subject: SVG BufferedImage Quality

I have a question about how to improve the quality of the images I'm generating for buffered images.

[cid:image001.png@01CEA339.F3DD19E0]

The left image is the image I generated with Batik while the right is the original. How do I generate a Buffered Image of the same quality? My code currently looks like this:

Float width;
        Float height;

        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buf = new byte[image.getStream().available()];
            int n = 0;
            while ((n = image.getStream().read(buf)) >= 0) {
                baos.write(buf, 0, n);
            }
            byte[] content = baos.toByteArray();


            String parser = XMLResourceDescriptor.getXMLParserClassName();
            SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
            Document doc = f.createDocument(image.getFilename(), new ByteArrayInputStream(content));
            Element element = doc.getDocumentElement();
            if (element.hasAttributeNS(null, "viewBox")) {
                String[] parts = element.getAttribute("viewBox").split("\\ ");
                width = new Float(parts[2]);
                height = new Float(parts[3]);
            } else if (element.hasAttributeNS(null, "height") && element.hasAttributeNS(null, "width")) {
                height = new Float(element.getAttributeNS(null, "height"));
                width = new Float(element.getAttributeNS(null, "width"));
            } else {
                width = new Float(100);
                height = new Float(100);
            }

            ShipTranscoder transcoder = new ShipTranscoder();
            TranscodingHints hints = new TranscodingHints();
            DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
            hints.put(ImageTranscoder.KEY_WIDTH, width);
            hints.put(ImageTranscoder.KEY_HEIGHT, height);
            hints.put(ImageTranscoder.KEY_DOM_IMPLEMENTATION, impl);
            hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, SVGConstants.SVG_NAMESPACE_URI);
            hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT_NAMESPACE_URI, SVGConstants.SVG_NAMESPACE_URI);
            hints.put(ImageTranscoder.KEY_DOCUMENT_ELEMENT, SVGConstants.SVG_SVG_TAG);
            hints.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, false);
            transcoder.setTranscodingHints(hints);
            transcoder.transcode(new TranscoderInput(new ByteArrayInputStream(content)), null);
            image.setImage(transcoder.getImage());

        } catch (Throwable e) {
            Exceptions.printStackTrace(e);
            String error = "Invalid input stream for SVG file type.";
            setError(SHIPPROPKEY.IMAGE_SOURCEFILE, error);
            LOGGER.log(Level.SEVERE, "        {0}\n", error);
            throw new MashException(getClass().getSimpleName()
}

class ShipTranscoder extends ImageTranscoder {

        private BufferedImage image = null;

        @Override
        public BufferedImage createImage(int w, int h) {
            image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
            return image;
        }

        @Override
        public void writeImage(BufferedImage img, TranscoderOutput out) {
        }

        public BufferedImage getImage() {
            return image;
        }
    }

Thanks,
Mark