View Javadoc

1   /*
2    * Copyright 2007 - 2009 JEuclid, http://jeuclid.sf.net
3    * 
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /* $Id: ImageIOConverter.java,v 916bcda78280 2009/08/28 07:32:36 max $ */
18  
19  package net.sourceforge.jeuclid.converter;
20  
21  import java.awt.Dimension;
22  import java.awt.image.BufferedImage;
23  import java.io.IOException;
24  import java.io.OutputStream;
25  
26  import javax.imageio.ImageWriter;
27  import javax.imageio.stream.ImageOutputStream;
28  import javax.imageio.stream.MemoryCacheImageOutputStream;
29  
30  import net.sourceforge.jeuclid.LayoutContext;
31  
32  import org.w3c.dom.Node;
33  
34  /**
35   * supports conversion to standard Raster formats through ImageIO.
36   * 
37   * @version $Revision: 916bcda78280 $
38   */
39  public class ImageIOConverter implements ConverterPlugin {
40  
41      private final ImageWriter writer;
42  
43      private final int colorModel;
44  
45      /**
46       * Default constructor.
47       * 
48       * @param iw
49       *            ImageWrite instance to use for this converter.
50       * @param noAlpha
51       *            if true, this image format does not support alpha values.
52       */
53      ImageIOConverter(final ImageWriter iw, final boolean noAlpha) {
54          this.writer = iw;
55          if (noAlpha) {
56              this.colorModel = BufferedImage.TYPE_INT_RGB;
57          } else {
58              this.colorModel = BufferedImage.TYPE_INT_ARGB;
59          }
60      }
61  
62      /** {@inheritDoc} */
63      public Dimension convert(final Node doc, final LayoutContext context,
64              final OutputStream outStream) throws IOException {
65          final ImageOutputStream ios = new MemoryCacheImageOutputStream(
66                  outStream);
67          final BufferedImage image = Converter.getInstance().render(doc,
68                  context, this.colorModel);
69          synchronized (this.writer) {
70              this.writer.setOutput(ios);
71              this.writer.write(image);
72          }
73          ios.close();
74          return new Dimension(image.getWidth(), image.getHeight());
75      }
76  
77      /** {@inheritDoc} */
78      public DocumentWithDimension convert(final Node doc,
79              final LayoutContext context) {
80          return null;
81      }
82  
83  }