You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-dev@xmlgraphics.apache.org by Mukt <do...@yahoo.com> on 2007/11/15 06:48:24 UTC

Problem: Getting node Image from SVG Image

I have written code to retrieve Image of a single node from entire image..the
code works fine.
The problem is it is too slow  
possibly due to following statement
 builder.build(bridgeContext, thisDocument);

I am calling the getKeyBufferedImage() function several times and it becomes
very very slow.

My question is : Is there any need to call  builder.build(bridgeContext,
thisDocument); each time?
I am not able to get the result each time if I call the abve statement only
once(initially).
Also I have to initialize and reset the objects all the time.

Can anybody review the code and help me find the solution?
Also explain me..why I need to call builder.build each time?

<code>
  protected void initObjects()
    {
        userAgentAdapter = new UserAgentAdapter();
        bridgeContext = new BridgeContext(userAgentAdapter);
        builder = new GVTBuilder();
    }
   
    /**
     * Reset the objects. (What were you expecting?)
     */
    protected void resetObjects()
    {
        userAgentAdapter = null;
        bridgeContext = null;
        builder = null;
    }
public Image getKeyBufferedImage(final String keyString)
    {
        resetObjects();
        initObjects();
       
        GraphicsNode graphicsNode = null;


        builder.build(bridgeContext, thisDocument);
       
        try
        {
            graphicsNode = builder.build(bridgeContext,
                            thisDocument.getElementById(keyString));
        }
        catch (final Exception e)
        {
            logSVG.warning("Error creating graphics node. Key might be
absent. Key = " + keyString);
            e.printStackTrace();
            return null;
        }
           
        // Size up the rectangle in 2D space.
        final Rectangle2D bounds = graphicsNode.getBounds();

        final int margin = 3;
        // Convert the 2D space into a rectangular region within the
document.
        final Rectangle areaOfInterest = new Rectangle((int) bounds.getX() -
1,
                (int) bounds.getY() - 1, (int) bounds.getWidth() + margin,
                (int) bounds.getHeight() + margin);
       
       
        //Assume that SVG is max of 1000 X 1000
        final int size1000 = 1000;
        Image imag = new BufferedImage(size1000, size1000,
BufferedImage.TYPE_4BYTE_ABGR);
        try
        {
            //Get Image graphics
            final Graphics2D g = (Graphics2D) imag.getGraphics();
           
//g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, 0.5f));
            //Because the SVG is rendered AntiAliased
            //Set same for current graphics object
            g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, //
Anti-alias!
                    RenderingHints.VALUE_ANTIALIAS_ON);
            //Pass the graphics object to node's paint method
            graphicsNode.paint((Graphics2D) g);

        }
        catch (final Exception e1)
        {
            e1.printStackTrace();
        }
        //Crop the key Image..as it is painted at the actual location
        imag = createImage(new FilteredImageSource(imag.getSource(),
                new CropImageFilter(
                        areaOfInterest.x , areaOfInterest.y ,
areaOfInterest.width , areaOfInterest.height)));
        //return the resulting Image
        return imag;

    }
</code>
-- 
View this message in context: http://www.nabble.com/Problem%3A-Getting-node-Image-from-SVG-Image-tf4809632.html#a13761337
Sent from the Batik - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-dev-help@xmlgraphics.apache.org


Re: Problem: Getting node Image from SVG Image

Posted by Mukt <do...@yahoo.com>.
After calling build for the whole document
       i.e. builder.build(bridgeContext, thisDocument)
only the call of build to the SVG element
       i.e. builder.build(bridgeContext,
thisDocument.getElementById(keyString))
works and returns proper GraphicsNode. Otherwise it throws exception.
I am not sure why?
-- 
View this message in context: http://www.nabble.com/Problem%3A-Getting-node-Image-from-SVG-Image-tf4809941.html#a13768009
Sent from the Batik - Dev mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe, e-mail: batik-dev-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-dev-help@xmlgraphics.apache.org