001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wms/GraphicContextFactory.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    package org.deegree.ogcwebservices.wms;
037    
038    import static java.awt.image.DataBuffer.TYPE_BYTE;
039    import static java.awt.image.Raster.createBandedRaster;
040    
041    import java.awt.Graphics;
042    import java.awt.image.BufferedImage;
043    import java.awt.image.ColorModel;
044    
045    import javax.media.jai.PlanarImage;
046    
047    import org.apache.batik.dom.GenericDOMImplementation;
048    import org.apache.batik.svggen.SVGGraphics2D;
049    import org.w3c.dom.DOMImplementation;
050    import org.w3c.dom.Document;
051    
052    /**
053     *
054     *
055     * @version $Revision: 18195 $
056     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
057     */
058    public class GraphicContextFactory {
059    
060        /**
061         * creates a graphic target object for the passed mime type. The target will be the object where to render the to. A
062         * <tt>BufferedImage</tt> for raster image mime types and a DOM <tt>Document</tt> for SVG.
063         *
064         * @param mimeType
065         *            mime type to create a object for
066         * @param width
067         *            width of the desired target object
068         * @param height
069         *            height of the desired target object
070         *
071         * @return object to render to
072         */
073        public static synchronized Object createGraphicTarget( String mimeType, int width, int height ) {
074            // to avoid errors:
075            mimeType = mimeType.toLowerCase();
076    
077            Object o = null;
078            if ( mimeType.equals( "image/jpg" ) || mimeType.equals( "image/jpeg" ) || mimeType.equals( "image/bmp" )
079                 || mimeType.equals( "image/tif" ) || mimeType.equals( "image/tiff" ) ) {
080                o = new BufferedImage( width, height, BufferedImage.TYPE_INT_RGB );
081            } else if ( mimeType.equals( "image/gif" ) || mimeType.equals( "image/png" )
082                        || mimeType.equals( "image/png; mode=24bit" ) ) {
083                o = new BufferedImage( width, height, BufferedImage.TYPE_INT_ARGB );
084            } else if ( mimeType.equals( "image/svg+xml" ) || mimeType.equals( "image/svg xml" ) ) {
085                // Get a DOMImplementation
086                DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
087    
088                // Create an instance of org.w3c.dom.Document
089                o = domImpl.createDocument( null, "svg", null );
090            } else if ( mimeType.equals( "image/png; mode=8bit" ) ) {
091                ColorModel cm = PlanarImage.getDefaultColorModel( TYPE_BYTE, 4 );
092                o = new BufferedImage( cm, createBandedRaster( TYPE_BYTE, width, height, 4, null ), false, null );
093            }
094    
095            return o;
096        }
097    
098        /**
099         * creates a graphic context for the passed target considering the passed mime type
100         *
101         * @param mimeType
102         *            mime type of the graphic (target)
103         * @param target
104         *            object to render to.
105         *
106         * @return graphic context of the target
107         */
108        public static synchronized Graphics createGraphicContext( String mimeType, Object target ) {
109            // to avoid errors:
110            mimeType = mimeType.toLowerCase();
111    
112            Graphics g = null;
113    
114            if ( mimeType.equals( "image/jpg" ) || mimeType.equals( "image/jpeg" ) || mimeType.equals( "image/bmp" )
115                 || mimeType.equals( "image/tif" ) || mimeType.equals( "image/tiff" ) || mimeType.equals( "image/gif" )
116                 || mimeType.equals( "image/png" ) || mimeType.equals( "image/png; mode=8bit" )
117                 || mimeType.equals( "image/png; mode=24bit" ) ) {
118                g = ( (BufferedImage) target ).getGraphics();
119            } else if ( mimeType.equals( "image/svg+xml" ) || mimeType.equals( "image/svg xml" ) ) {
120                // Create an instance of the SVG Generator
121                g = new SVGGraphics2D( (Document) target );
122            }
123    
124            return g;
125        }
126    }