Source for org.jfree.chart.encoders.EncoderUtil

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2007, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * ----------------
  28:  * EncoderUtil.java
  29:  * ----------------
  30:  * (C) Copyright 2004-2007, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributor(s):   -;
  34:  *
  35:  * Changes
  36:  * -------
  37:  * 01-Aug-2004 : Initial version (RA);
  38:  * 02-Feb-2007 : Removed author tags all over JFreeChart sources (DG);
  39:  *
  40:  */
  41: 
  42: package org.jfree.chart.encoders;
  43: 
  44: import java.awt.image.BufferedImage;
  45: import java.io.IOException;
  46: import java.io.OutputStream;
  47: 
  48: /**
  49:  * A collection of utility methods for encoding images and returning them as a 
  50:  * byte[] or writing them directly to an OutputStream.
  51:  */
  52: public class EncoderUtil {
  53: 
  54:     /**
  55:      * Encode the image in a specific format.
  56:      *
  57:      * @param image  The image to be encoded.
  58:      * @param format  The {@link ImageFormat} to use.
  59:      * 
  60:      * @return The byte[] that is the encoded image.
  61:      * @throws IOException
  62:      */
  63:     public static byte[] encode(BufferedImage image, String format) 
  64:         throws IOException {
  65:         ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format);
  66:         return imageEncoder.encode(image);
  67:     }
  68: 
  69:     /**
  70:      * Encode the image in a specific format.
  71:      *
  72:      * @param image  The image to be encoded.
  73:      * @param format  The {@link ImageFormat} to use.
  74:      * @param encodeAlpha  Whether to encode alpha transparency (not supported 
  75:      *                     by all ImageEncoders).
  76:      * @return The byte[] that is the encoded image.
  77:      * @throws IOException
  78:      */
  79:     public static byte[] encode(BufferedImage image, String format,
  80:                                 boolean encodeAlpha) throws IOException {
  81:         ImageEncoder imageEncoder 
  82:             = ImageEncoderFactory.newInstance(format, encodeAlpha);
  83:         return imageEncoder.encode(image);
  84:     }
  85: 
  86:     /**
  87:      * Encode the image in a specific format.
  88:      *
  89:      * @param image  The image to be encoded.
  90:      * @param format  The {@link ImageFormat} to use.
  91:      * @param quality  The quality to use for the image encoding (not supported
  92:      *                 by all ImageEncoders).
  93:      * @return The byte[] that is the encoded image.
  94:      * @throws IOException
  95:      */
  96:     public static byte[] encode(BufferedImage image, String format,
  97:                                 float quality) throws IOException {
  98:         ImageEncoder imageEncoder 
  99:             = ImageEncoderFactory.newInstance(format, quality);
 100:         return imageEncoder.encode(image);
 101:     }
 102: 
 103:     /**
 104:      * Encode the image in a specific format.
 105:      *
 106:      * @param image  The image to be encoded.
 107:      * @param format  The {@link ImageFormat} to use.
 108:      * @param quality  The quality to use for the image encoding (not supported 
 109:      *                 by all ImageEncoders).
 110:      * @param encodeAlpha  Whether to encode alpha transparency (not supported 
 111:      *                     by all ImageEncoders).
 112:      * @return The byte[] that is the encoded image.
 113:      * @throws IOException
 114:      */
 115:     public static byte[] encode(BufferedImage image, String format,
 116:                                 float quality, boolean encodeAlpha) 
 117:         throws IOException {
 118:         ImageEncoder imageEncoder 
 119:             = ImageEncoderFactory.newInstance(format, quality, encodeAlpha);
 120:         return imageEncoder.encode(image);
 121:     }
 122: 
 123:     /**
 124:      * Encode the image in a specific format and write it to an OutputStream.
 125:      *
 126:      * @param image  The image to be encoded.
 127:      * @param format  The {@link ImageFormat} to use.
 128:      * @param outputStream  The OutputStream to write the encoded image to.
 129:      * @throws IOException
 130:      */
 131:     public static void writeBufferedImage(BufferedImage image, String format, 
 132:         OutputStream outputStream) throws IOException {
 133:         ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format);
 134:         imageEncoder.encode(image, outputStream);
 135:     }
 136: 
 137:     /**
 138:      * Encode the image in a specific format and write it to an OutputStream.
 139:      *
 140:      * @param image  The image to be encoded.
 141:      * @param format  The {@link ImageFormat} to use.
 142:      * @param outputStream  The OutputStream to write the encoded image to.
 143:      * @param quality  The quality to use for the image encoding (not 
 144:      *                 supported by all ImageEncoders).
 145:      * @throws IOException
 146:      */
 147:     public static void writeBufferedImage(BufferedImage image, String format, 
 148:         OutputStream outputStream, float quality) throws IOException {
 149:         ImageEncoder imageEncoder 
 150:             = ImageEncoderFactory.newInstance(format, quality);
 151:         imageEncoder.encode(image, outputStream);
 152:     }
 153: 
 154:     /**
 155:      * Encode the image in a specific format and write it to an OutputStream.
 156:      *
 157:      * @param image  The image to be encoded.
 158:      * @param format  The {@link ImageFormat} to use.
 159:      * @param outputStream  The OutputStream to write the encoded image to.
 160:      * @param encodeAlpha  Whether to encode alpha transparency (not 
 161:      *                     supported by all ImageEncoders).
 162:      * @throws IOException
 163:      */
 164:     public static void writeBufferedImage(BufferedImage image, String format, 
 165:         OutputStream outputStream, boolean encodeAlpha) throws IOException {
 166:         ImageEncoder imageEncoder 
 167:             = ImageEncoderFactory.newInstance(format, encodeAlpha);
 168:         imageEncoder.encode(image, outputStream);
 169:     }
 170: 
 171:     /**
 172:      * Encode the image in a specific format and write it to an OutputStream.
 173:      *
 174:      * @param image  The image to be encoded.
 175:      * @param format  The {@link ImageFormat} to use.
 176:      * @param outputStream  The OutputStream to write the encoded image to.
 177:      * @param quality  The quality to use for the image encoding (not 
 178:      *                 supported by all ImageEncoders).
 179:      * @param encodeAlpha  Whether to encode alpha transparency (not supported 
 180:      *                     by all ImageEncoders).
 181:      * @throws IOException
 182:      */
 183:     public static void writeBufferedImage(BufferedImage image, String format, 
 184:         OutputStream outputStream, float quality, boolean encodeAlpha) 
 185:         throws IOException {
 186:         ImageEncoder imageEncoder 
 187:             = ImageEncoderFactory.newInstance(format, quality, encodeAlpha);
 188:         imageEncoder.encode(image, outputStream);
 189:     }
 190: 
 191: }