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-2005, 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, by Richard Atkinson and Contributors.
  31:  *
  32:  * Original Author:  Richard Atkinson;
  33:  * Contributor(s):   -;
  34:  *
  35:  * $Id: EncoderUtil.java,v 1.3.2.1 2005/10/25 20:41:46 mungady Exp $
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 01-Aug-2004 : Initial version (RA);
  40:  *
  41:  */
  42: 
  43: package org.jfree.chart.encoders;
  44: 
  45: import java.awt.image.BufferedImage;
  46: import java.io.IOException;
  47: import java.io.OutputStream;
  48: 
  49: /**
  50:  * A collection of utility methods for encoding images and returning them as a 
  51:  * byte[] or writing them directly to an OutputStream.
  52:  *
  53:  * @author Richard Atkinson
  54:  */
  55: public class EncoderUtil {
  56: 
  57:     /**
  58:      * Encode the image in a specific format.
  59:      *
  60:      * @param image  The image to be encoded.
  61:      * @param format  The {@link ImageFormat} to use.
  62:      * 
  63:      * @return The byte[] that is the encoded image.
  64:      * @throws IOException
  65:      */
  66:     public static byte[] encode(BufferedImage image, String format) 
  67:         throws IOException {
  68:         ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format);
  69:         return imageEncoder.encode(image);
  70:     }
  71: 
  72:     /**
  73:      * Encode the image in a specific format.
  74:      *
  75:      * @param image  The image to be encoded.
  76:      * @param format  The {@link ImageFormat} to use.
  77:      * @param encodeAlpha  Whether to encode alpha transparency (not supported 
  78:      *                     by all ImageEncoders).
  79:      * @return The byte[] that is the encoded image.
  80:      * @throws IOException
  81:      */
  82:     public static byte[] encode(BufferedImage image, String format,
  83:                                 boolean encodeAlpha) throws IOException {
  84:         ImageEncoder imageEncoder 
  85:             = ImageEncoderFactory.newInstance(format, encodeAlpha);
  86:         return imageEncoder.encode(image);
  87:     }
  88: 
  89:     /**
  90:      * Encode the image in a specific format.
  91:      *
  92:      * @param image  The image to be encoded.
  93:      * @param format  The {@link ImageFormat} to use.
  94:      * @param quality  The quality to use for the image encoding (not supported
  95:      *                 by all ImageEncoders).
  96:      * @return The byte[] that is the encoded image.
  97:      * @throws IOException
  98:      */
  99:     public static byte[] encode(BufferedImage image, String format,
 100:                                 float quality) throws IOException {
 101:         ImageEncoder imageEncoder 
 102:             = ImageEncoderFactory.newInstance(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) 
 120:         throws IOException {
 121:         ImageEncoder imageEncoder 
 122:             = ImageEncoderFactory.newInstance(format, quality, encodeAlpha);
 123:         return imageEncoder.encode(image);
 124:     }
 125: 
 126:     /**
 127:      * Encode the image in a specific format and write it to an OutputStream.
 128:      *
 129:      * @param image  The image to be encoded.
 130:      * @param format  The {@link ImageFormat} to use.
 131:      * @param outputStream  The OutputStream to write the encoded image to.
 132:      * @throws IOException
 133:      */
 134:     public static void writeBufferedImage(BufferedImage image, String format, 
 135:         OutputStream outputStream) throws IOException {
 136:         ImageEncoder imageEncoder = ImageEncoderFactory.newInstance(format);
 137:         imageEncoder.encode(image, outputStream);
 138:     }
 139: 
 140:     /**
 141:      * Encode the image in a specific format and write it to an OutputStream.
 142:      *
 143:      * @param image  The image to be encoded.
 144:      * @param format  The {@link ImageFormat} to use.
 145:      * @param outputStream  The OutputStream to write the encoded image to.
 146:      * @param quality  The quality to use for the image encoding (not 
 147:      *                 supported by all ImageEncoders).
 148:      * @throws IOException
 149:      */
 150:     public static void writeBufferedImage(BufferedImage image, String format, 
 151:         OutputStream outputStream, float quality) throws IOException {
 152:         ImageEncoder imageEncoder 
 153:             = ImageEncoderFactory.newInstance(format, quality);
 154:         imageEncoder.encode(image, outputStream);
 155:     }
 156: 
 157:     /**
 158:      * Encode the image in a specific format and write it to an OutputStream.
 159:      *
 160:      * @param image  The image to be encoded.
 161:      * @param format  The {@link ImageFormat} to use.
 162:      * @param outputStream  The OutputStream to write the encoded image to.
 163:      * @param encodeAlpha  Whether to encode alpha transparency (not 
 164:      *                     supported by all ImageEncoders).
 165:      * @throws IOException
 166:      */
 167:     public static void writeBufferedImage(BufferedImage image, String format, 
 168:         OutputStream outputStream, boolean encodeAlpha) throws IOException {
 169:         ImageEncoder imageEncoder 
 170:             = ImageEncoderFactory.newInstance(format, encodeAlpha);
 171:         imageEncoder.encode(image, outputStream);
 172:     }
 173: 
 174:     /**
 175:      * Encode the image in a specific format and write it to an OutputStream.
 176:      *
 177:      * @param image  The image to be encoded.
 178:      * @param format  The {@link ImageFormat} to use.
 179:      * @param outputStream  The OutputStream to write the encoded image to.
 180:      * @param quality  The quality to use for the image encoding (not 
 181:      *                 supported by all ImageEncoders).
 182:      * @param encodeAlpha  Whether to encode alpha transparency (not supported 
 183:      *                     by all ImageEncoders).
 184:      * @throws IOException
 185:      */
 186:     public static void writeBufferedImage(BufferedImage image, String format, 
 187:         OutputStream outputStream, float quality, boolean encodeAlpha) 
 188:         throws IOException {
 189:         ImageEncoder imageEncoder 
 190:             = ImageEncoderFactory.newInstance(format, quality, encodeAlpha);
 191:         imageEncoder.encode(image, outputStream);
 192:     }
 193: 
 194: }