001    /*
002     * Copyright 2007 - 2009 JEuclid, http://jeuclid.sf.net
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    /* $Id: ImageIOConverter.java,v 916bcda78280 2009/08/28 07:32:36 max $ */
018    
019    package net.sourceforge.jeuclid.converter;
020    
021    import java.awt.Dimension;
022    import java.awt.image.BufferedImage;
023    import java.io.IOException;
024    import java.io.OutputStream;
025    
026    import javax.imageio.ImageWriter;
027    import javax.imageio.stream.ImageOutputStream;
028    import javax.imageio.stream.MemoryCacheImageOutputStream;
029    
030    import net.sourceforge.jeuclid.LayoutContext;
031    
032    import org.w3c.dom.Node;
033    
034    /**
035     * supports conversion to standard Raster formats through ImageIO.
036     * 
037     * @version $Revision: 916bcda78280 $
038     */
039    public class ImageIOConverter implements ConverterPlugin {
040    
041        private final ImageWriter writer;
042    
043        private final int colorModel;
044    
045        /**
046         * Default constructor.
047         * 
048         * @param iw
049         *            ImageWrite instance to use for this converter.
050         * @param noAlpha
051         *            if true, this image format does not support alpha values.
052         */
053        ImageIOConverter(final ImageWriter iw, final boolean noAlpha) {
054            this.writer = iw;
055            if (noAlpha) {
056                this.colorModel = BufferedImage.TYPE_INT_RGB;
057            } else {
058                this.colorModel = BufferedImage.TYPE_INT_ARGB;
059            }
060        }
061    
062        /** {@inheritDoc} */
063        public Dimension convert(final Node doc, final LayoutContext context,
064                final OutputStream outStream) throws IOException {
065            final ImageOutputStream ios = new MemoryCacheImageOutputStream(
066                    outStream);
067            final BufferedImage image = Converter.getInstance().render(doc,
068                    context, this.colorModel);
069            synchronized (this.writer) {
070                this.writer.setOutput(ios);
071                this.writer.write(image);
072            }
073            ios.close();
074            return new Dimension(image.getWidth(), image.getHeight());
075        }
076    
077        /** {@inheritDoc} */
078        public DocumentWithDimension convert(final Node doc,
079                final LayoutContext context) {
080            return null;
081        }
082    
083    }