001: /* ===========================================================
002: * JFreeChart : a free chart library for the Java(tm) platform
003: * ===========================================================
004: *
005: * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
006: *
007: * Project Info: http://www.jfree.org/jfreechart/index.html
008: *
009: * This library is free software; you can redistribute it and/or modify it
010: * under the terms of the GNU Lesser General Public License as published by
011: * the Free Software Foundation; either version 2.1 of the License, or
012: * (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but
015: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
016: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
017: * License for more details.
018: *
019: * You should have received a copy of the GNU Lesser General Public
020: * License along with this library; if not, write to the Free Software
021: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
022: * USA.
023: *
024: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
025: * in the United States and other countries.]
026: *
027: * ----------------
028: * EncoderUtil.java
029: * ----------------
030: * (C) Copyright 2004-2007, by Richard Atkinson and Contributors.
031: *
032: * Original Author: Richard Atkinson;
033: * Contributor(s): -;
034: *
035: * $Id: EncoderUtil.java,v 1.3.2.2 2007/02/02 14:51:22 mungady Exp $
036: *
037: * Changes
038: * -------
039: * 01-Aug-2004 : Initial version (RA);
040: * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
041: *
042: */
043:
044: package org.jfree.chart.encoders;
045:
046: import java.awt.image.BufferedImage;
047: import java.io.IOException;
048: import java.io.OutputStream;
049:
050: /**
051: * A collection of utility methods for encoding images and returning them as a
052: * byte[] or writing them directly to an OutputStream.
053: */
054: public class EncoderUtil {
055:
056: /**
057: * Encode the image in a specific format.
058: *
059: * @param image The image to be encoded.
060: * @param format The {@link ImageFormat} to use.
061: *
062: * @return The byte[] that is the encoded image.
063: * @throws IOException
064: */
065: public static byte[] encode(BufferedImage image, String format)
066: throws IOException {
067: ImageEncoder imageEncoder = ImageEncoderFactory
068: .newInstance(format);
069: return imageEncoder.encode(image);
070: }
071:
072: /**
073: * Encode the image in a specific format.
074: *
075: * @param image The image to be encoded.
076: * @param format The {@link ImageFormat} to use.
077: * @param encodeAlpha Whether to encode alpha transparency (not supported
078: * by all ImageEncoders).
079: * @return The byte[] that is the encoded image.
080: * @throws IOException
081: */
082: public static byte[] encode(BufferedImage image, String format,
083: boolean encodeAlpha) throws IOException {
084: ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(
085: format, encodeAlpha);
086: return imageEncoder.encode(image);
087: }
088:
089: /**
090: * Encode the image in a specific format.
091: *
092: * @param image The image to be encoded.
093: * @param format The {@link ImageFormat} to use.
094: * @param quality The quality to use for the image encoding (not supported
095: * by all ImageEncoders).
096: * @return The byte[] that is the encoded image.
097: * @throws IOException
098: */
099: public static byte[] encode(BufferedImage image, String format,
100: float quality) throws IOException {
101: ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(
102: format, quality);
103: return imageEncoder.encode(image);
104: }
105:
106: /**
107: * Encode the image in a specific format.
108: *
109: * @param image The image to be encoded.
110: * @param format The {@link ImageFormat} to use.
111: * @param quality The quality to use for the image encoding (not supported
112: * by all ImageEncoders).
113: * @param encodeAlpha Whether to encode alpha transparency (not supported
114: * by all ImageEncoders).
115: * @return The byte[] that is the encoded image.
116: * @throws IOException
117: */
118: public static byte[] encode(BufferedImage image, String format,
119: float quality, boolean encodeAlpha) throws IOException {
120: ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(
121: format, quality, encodeAlpha);
122: return imageEncoder.encode(image);
123: }
124:
125: /**
126: * Encode the image in a specific format and write it to an OutputStream.
127: *
128: * @param image The image to be encoded.
129: * @param format The {@link ImageFormat} to use.
130: * @param outputStream The OutputStream to write the encoded image to.
131: * @throws IOException
132: */
133: public static void writeBufferedImage(BufferedImage image,
134: String format, OutputStream outputStream)
135: throws IOException {
136: ImageEncoder imageEncoder = ImageEncoderFactory
137: .newInstance(format);
138: imageEncoder.encode(image, outputStream);
139: }
140:
141: /**
142: * Encode the image in a specific format and write it to an OutputStream.
143: *
144: * @param image The image to be encoded.
145: * @param format The {@link ImageFormat} to use.
146: * @param outputStream The OutputStream to write the encoded image to.
147: * @param quality The quality to use for the image encoding (not
148: * supported by all ImageEncoders).
149: * @throws IOException
150: */
151: public static void writeBufferedImage(BufferedImage image,
152: String format, OutputStream outputStream, float quality)
153: throws IOException {
154: ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(
155: format, quality);
156: imageEncoder.encode(image, outputStream);
157: }
158:
159: /**
160: * Encode the image in a specific format and write it to an OutputStream.
161: *
162: * @param image The image to be encoded.
163: * @param format The {@link ImageFormat} to use.
164: * @param outputStream The OutputStream to write the encoded image to.
165: * @param encodeAlpha Whether to encode alpha transparency (not
166: * supported by all ImageEncoders).
167: * @throws IOException
168: */
169: public static void writeBufferedImage(BufferedImage image,
170: String format, OutputStream outputStream,
171: boolean encodeAlpha) throws IOException {
172: ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(
173: format, encodeAlpha);
174: imageEncoder.encode(image, outputStream);
175: }
176:
177: /**
178: * Encode the image in a specific format and write it to an OutputStream.
179: *
180: * @param image The image to be encoded.
181: * @param format The {@link ImageFormat} to use.
182: * @param outputStream The OutputStream to write the encoded image to.
183: * @param quality The quality to use for the image encoding (not
184: * supported by all ImageEncoders).
185: * @param encodeAlpha Whether to encode alpha transparency (not supported
186: * by all ImageEncoders).
187: * @throws IOException
188: */
189: public static void writeBufferedImage(BufferedImage image,
190: String format, OutputStream outputStream, float quality,
191: boolean encodeAlpha) throws IOException {
192: ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(
193: format, quality, encodeAlpha);
194: imageEncoder.encode(image, outputStream);
195: }
196:
197: }
|