1   
2   
3   
4   
5   
6   
7   
8   
9   
10  
11  
12  
13  
14  
15  
16  
17  
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  
36  
37  
38  
39  public class ImageIOConverter implements ConverterPlugin {
40  
41      private final ImageWriter writer;
42  
43      private final int colorModel;
44  
45      
46  
47  
48  
49  
50  
51  
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      
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      
78      public DocumentWithDimension convert(final Node doc,
79              final LayoutContext context) {
80          return null;
81      }
82  
83  }