001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, 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 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * -------------------------- 028 * SunJPEGEncoderAdapter.java 029 * -------------------------- 030 * (C) Copyright 2004-2013, by Richard Atkinson and Contributors. 031 * 032 * Original Author: Richard Atkinson; 033 * Contributor(s): David Gilbert (for Object Refinery Limited); 034 * 035 * Changes 036 * ------- 037 * 01-Aug-2004 : Initial version (RA); 038 * 01-Nov-2005 : To remove the dependency on non-supported APIs, use ImageIO 039 * instead of com.sun.image.codec.jpeg.JPEGImageEncoder - this 040 * adapter will only be available on JDK 1.4 or later (DG); 041 * ------------- JFREECHART 1.0.x --------------------------------------------- 042 * 20-Jul-2006 : Pass quality setting to ImageIO. Also increased default 043 * value to 0.95 (DG); 044 * 02-Jul-2013 : Use ParamChecks (DG); 045 * 046 */ 047 048package org.jfree.chart.encoders; 049 050import java.awt.image.BufferedImage; 051import java.io.ByteArrayOutputStream; 052import java.io.IOException; 053import java.io.OutputStream; 054import java.util.Iterator; 055 056import javax.imageio.IIOImage; 057import javax.imageio.ImageIO; 058import javax.imageio.ImageWriteParam; 059import javax.imageio.ImageWriter; 060import javax.imageio.stream.ImageOutputStream; 061import org.jfree.chart.util.ParamChecks; 062 063/** 064 * Adapter class for the Sun JPEG Encoder. The {@link ImageEncoderFactory} 065 * will only return a reference to this class by default if the library has 066 * been compiled under a JDK 1.4+ and is being run using a JRE 1.4+. 067 */ 068public class SunJPEGEncoderAdapter implements ImageEncoder { 069 070 /** The quality setting (in the range 0.0f to 1.0f). */ 071 private float quality = 0.95f; 072 073 /** 074 * Creates a new <code>SunJPEGEncoderAdapter</code> instance. 075 */ 076 public SunJPEGEncoderAdapter() { 077 } 078 079 /** 080 * Returns the quality of the image encoding, which is a number in the 081 * range 0.0f to 1.0f (higher values give better quality output, but larger 082 * file sizes). The default value is 0.95f. 083 * 084 * @return A float representing the quality, in the range 0.0f to 1.0f. 085 * 086 * @see #setQuality(float) 087 */ 088 @Override 089 public float getQuality() { 090 return this.quality; 091 } 092 093 /** 094 * Set the quality of the image encoding. 095 * 096 * @param quality A float representing the quality (in the range 0.0f to 097 * 1.0f). 098 * 099 * @see #getQuality() 100 */ 101 @Override 102 public void setQuality(float quality) { 103 if (quality < 0.0f || quality > 1.0f) { 104 throw new IllegalArgumentException( 105 "The 'quality' must be in the range 0.0f to 1.0f"); 106 } 107 this.quality = quality; 108 } 109 110 /** 111 * Returns <code>false</code> always, indicating that this encoder does not 112 * encode alpha transparency. 113 * 114 * @return <code>false</code>. 115 */ 116 @Override 117 public boolean isEncodingAlpha() { 118 return false; 119 } 120 121 /** 122 * Set whether the encoder should encode alpha transparency (this is not 123 * supported for JPEG, so this method does nothing). 124 * 125 * @param encodingAlpha ignored. 126 */ 127 @Override 128 public void setEncodingAlpha(boolean encodingAlpha) { 129 // No op 130 } 131 132 /** 133 * Encodes an image in JPEG format. 134 * 135 * @param bufferedImage the image to be encoded (<code>null</code> not 136 * permitted). 137 * 138 * @return The byte[] that is the encoded image. 139 * 140 * @throws IOException if there is an I/O problem. 141 * @throws NullPointerException if <code>bufferedImage</code> is 142 * <code>null</code>. 143 */ 144 @Override 145 public byte[] encode(BufferedImage bufferedImage) throws IOException { 146 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 147 encode(bufferedImage, outputStream); 148 return outputStream.toByteArray(); 149 } 150 151 /** 152 * Encodes an image in JPEG format and writes it to an output stream. 153 * 154 * @param bufferedImage the image to be encoded (<code>null</code> not 155 * permitted). 156 * @param outputStream the OutputStream to write the encoded image to 157 * (<code>null</code> not permitted). 158 * 159 * @throws IOException if there is an I/O problem. 160 * @throws NullPointerException if <code>bufferedImage</code> is 161 * <code>null</code>. 162 */ 163 @Override 164 public void encode(BufferedImage bufferedImage, OutputStream outputStream) 165 throws IOException { 166 ParamChecks.nullNotPermitted(bufferedImage, "bufferedImage"); 167 ParamChecks.nullNotPermitted(outputStream, "outputStream"); 168 Iterator iterator = ImageIO.getImageWritersByFormatName("jpeg"); 169 ImageWriter writer = (ImageWriter) iterator.next(); 170 ImageWriteParam p = writer.getDefaultWriteParam(); 171 p.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); 172 p.setCompressionQuality(this.quality); 173 ImageOutputStream ios = ImageIO.createImageOutputStream(outputStream); 174 writer.setOutput(ios); 175 writer.write(null, new IIOImage(bufferedImage, null, null), p); 176 ios.flush(); 177 writer.dispose(); 178 ios.close(); 179 } 180 181}