001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings.externalizer;
014:
015: import Acme.JPM.Encoders.GifEncoder;
016: import Acme.JPM.Encoders.JpegEncoder;
017:
018: import com.keypoint.PngEncoder;
019: import org.apache.commons.logging.Log;
020: import org.apache.commons.logging.LogFactory;
021: import org.wings.io.Device;
022: import org.wings.io.DeviceOutputStream;
023: import org.wings.resource.HttpHeader;
024:
025: import java.awt.*;
026: import java.util.Collection;
027:
028: /**
029: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
030: * @author <a href="mailto:mreinsch@to.com">Michael Reinsch</a>
031: */
032: public class ImageExternalizer implements Externalizer<Image> {
033:
034: private final static Log log = LogFactory
035: .getLog(ImageExternalizer.class);
036:
037: public static final String FORMAT_PNG = "png";
038: public static final String FORMAT_GIF = "gif";
039: public static final String FORMAT_JPG = "jpeg";
040:
041: public static final String[] SUPPORTED_FORMATS = { FORMAT_PNG,
042: FORMAT_GIF, FORMAT_JPG };
043: private static final Class[] SUPPORTED_CLASSES = { Image.class };
044:
045: public static final ImageExternalizer SHARED_GIF_INSTANCE = new ImageExternalizer(
046: FORMAT_GIF);
047: public static final ImageExternalizer SHARED_PNG_INSTANCE = new ImageExternalizer(
048: FORMAT_PNG);
049: public static final ImageExternalizer SHARED_JPG_INSTANCE = new ImageExternalizer(
050: FORMAT_JPG);
051:
052: protected String format;
053:
054: protected final String[] supportedMimeTypes = new String[1];
055:
056: public ImageExternalizer() {
057: this (FORMAT_PNG);
058: }
059:
060: public ImageExternalizer(String format) {
061: this .format = format;
062: checkFormat();
063:
064: supportedMimeTypes[0] = getMimeType(null);
065: }
066:
067: protected void checkFormat() {
068: for (String aSUPPORTED_FORMATS : SUPPORTED_FORMATS) {
069: if (aSUPPORTED_FORMATS.equals(format)) {
070: return;
071: }
072: }
073: throw new IllegalArgumentException("Unsupported Format "
074: + format);
075: }
076:
077: public String getId(Image obj) {
078: return null;
079: }
080:
081: public String getExtension(Image obj) {
082: return format;
083: }
084:
085: public String getMimeType(Image obj) {
086: return "image/" + format;
087: }
088:
089: public int getLength(Image obj) {
090: return -1;
091: }
092:
093: public boolean isFinal(Image obj) {
094: return false; // images may dynamically change (i.e. status charts)
095: // I guess the static case should cache the encoding results rather
096: // then rerunning it
097: }
098:
099: public Class[] getSupportedClasses() {
100: return SUPPORTED_CLASSES;
101: }
102:
103: public String[] getSupportedMimeTypes() {
104: return supportedMimeTypes;
105: }
106:
107: public void write(Object obj, Device out)
108: throws java.io.IOException {
109: if (FORMAT_PNG.equals(format))
110: writePNG((Image) obj, out);
111: else if (FORMAT_JPG.equals(format))
112: writeJPG((Image) obj, out);
113: else
114: writeGIF((Image) obj, out);
115: }
116:
117: /**
118: * writes a image as gif to the OutputStream
119: */
120: public void writeGIF(Image img, Device out)
121: throws java.io.IOException {
122: GifEncoder encoder = new GifEncoder(img,
123: new DeviceOutputStream(out), true);
124: encoder.encode();
125: }
126:
127: /**
128: * writes a image as jpeg to the OutputStream
129: */
130: public void writeJPG(Image img, Device out)
131: throws java.io.IOException {
132: JpegEncoder encoder = new JpegEncoder(img,
133: new DeviceOutputStream(out));
134: encoder.encode();
135: }
136:
137: /**
138: * writes a image as png to the OutputStream
139: */
140: public void writePNG(Image img, Device out)
141: throws java.io.IOException {
142: PngEncoder png = new PngEncoder(img, PngEncoder.ENCODE_ALPHA,
143: PngEncoder.FILTER_NONE, 6);
144: byte[] pngbytes = png.pngEncode();
145: if (pngbytes == null) {
146: log.fatal("null image");
147: } else {
148: out.write(pngbytes);
149: }
150: out.flush();
151: }
152:
153: public Collection<HttpHeader> getHeaders(Image obj) {
154: return null;
155: }
156: }
|