KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > externalizer > ImageExternalizer


1 /*
2  * $Id: ImageExternalizer.java,v 1.4 2004/12/01 07:54:08 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.externalizer;
15
16 import Acme.JPM.Encoders.GifEncoder;
17 import com.keypoint.PngEncoder;
18 import org.apache.commons.logging.Log;
19 import org.apache.commons.logging.LogFactory;
20 import org.wings.io.Device;
21 import org.wings.io.DeviceOutputStream;
22
23 import java.awt.*;
24 import java.util.Collection JavaDoc;
25
26 /**
27  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
28  * @author <a HREF="mailto:mreinsch@to.com">Michael Reinsch</a>
29  * @version $Revision: 1.4 $
30  */

31 public class ImageExternalizer implements Externalizer {
32
33     private final transient static Log log = LogFactory.getLog(ImageExternalizer.class);
34
35     public static final String JavaDoc FORMAT_PNG = "png";
36     public static final String JavaDoc FORMAT_GIF = "gif";
37
38     private static final String JavaDoc[] SUPPORTED_FORMATS = {FORMAT_PNG, FORMAT_GIF};
39     private static final Class JavaDoc[] SUPPORTED_CLASSES = {Image.class};
40
41     public static final ImageExternalizer SHARED_GIF_INSTANCE = new ImageExternalizer(FORMAT_GIF);
42     public static final ImageExternalizer SHARED_PNG_INSTANCE = new ImageExternalizer(FORMAT_PNG);
43
44     protected String JavaDoc format;
45
46     protected final String JavaDoc[] supportedMimeTypes = new String JavaDoc[1];
47
48     public ImageExternalizer() {
49         this(FORMAT_PNG);
50     }
51
52     public ImageExternalizer(String JavaDoc format) {
53         this.format = format;
54         checkFormat();
55
56         supportedMimeTypes[0] = getMimeType(null);
57     }
58
59     protected void checkFormat() {
60         for (int i = 0; i < SUPPORTED_FORMATS.length; i++) {
61             if (SUPPORTED_FORMATS[i].equals(format))
62                 return;
63         }
64         throw new IllegalArgumentException JavaDoc("Unsupported Format " + format);
65     }
66
67     public String JavaDoc getExtension(Object JavaDoc obj) {
68         return format;
69     }
70
71     public String JavaDoc getMimeType(Object JavaDoc obj) {
72         return "image/" + format;
73     }
74
75     public int getLength(Object JavaDoc obj) {
76         return -1;
77     }
78
79     public boolean isFinal(Object JavaDoc obj) {
80         return false;
81     }
82
83     public Class JavaDoc[] getSupportedClasses() {
84         return SUPPORTED_CLASSES;
85     }
86
87     public String JavaDoc[] getSupportedMimeTypes() {
88         return supportedMimeTypes;
89     }
90
91     public void write(Object JavaDoc obj, Device out)
92             throws java.io.IOException JavaDoc {
93         Image img = (Image) obj;
94         if (FORMAT_PNG.equals(format))
95             writePNG(img, out);
96         else
97             writeGIF(img, out);
98     }
99
100     /**
101      * writes a image as gif to the OutputStream
102      */

103     public void writeGIF(Image img, Device out)
104             throws java.io.IOException JavaDoc {
105         GifEncoder encoder = new GifEncoder(img, new DeviceOutputStream(out),
106                 true);
107         encoder.encode();
108     }
109
110     /**
111      * writes a image as png to the OutputStream
112      */

113     public void writePNG(Image img, Device out)
114             throws java.io.IOException JavaDoc {
115         PngEncoder png = new PngEncoder(img, PngEncoder.ENCODE_ALPHA,
116                 PngEncoder.FILTER_NONE, 6);
117         byte[] pngbytes = png.pngEncode();
118         if (pngbytes == null) {
119             log.fatal("null image");
120         } else {
121             out.write(pngbytes);
122         }
123         out.flush();
124     }
125
126     public Collection JavaDoc getHeaders(Object JavaDoc obj) {
127         return null;
128     }
129 }
130
131
132
Popular Tags