001 /* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2005, 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, by Richard Atkinson and Contributors. 031 * 032 * Original Author: Richard Atkinson; 033 * Contributor(s): -; 034 * 035 * $Id: EncoderUtil.java,v 1.3.2.1 2005/10/25 20:41:46 mungady Exp $ 036 * 037 * Changes 038 * ------- 039 * 01-Aug-2004 : Initial version (RA); 040 * 041 */ 042 043 package org.jfree.chart.encoders; 044 045 import java.awt.image.BufferedImage; 046 import java.io.IOException; 047 import java.io.OutputStream; 048 049 /** 050 * A collection of utility methods for encoding images and returning them as a 051 * byte[] or writing them directly to an OutputStream. 052 * 053 * @author Richard Atkinson 054 */ 055 public class EncoderUtil { 056 057 /** 058 * Encode the image in a specific format. 059 * 060 * @param image The image to be encoded. 061 * @param format The {@link ImageFormat} to use. 062 * 063 * @return The byte[] that is the encoded image. 064 * @throws IOException 065 */ 066 public static byte[] encode(BufferedImage image, String format) 067 throws IOException { 068 ImageEncoder imageEncoder = ImageEncoderFactory.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 085 = ImageEncoderFactory.newInstance(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 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 }