    001/* ===========================================================
    002 * JFreeChart : a free chart library for the Java(tm) platform
    003 * ===========================================================
    004 *
    005 * (C) Copyright 2000-2011, 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 * ChartUtilities.java
    029 * -------------------
    030 * (C) Copyright 2001-2008, by Object Refinery Limited and Contributors.
    031 *
    032 * Original Author:  David Gilbert (for Object Refinery Limited);
    033 * Contributor(s):   Wolfgang Irler;
    034 *                   Richard Atkinson;
    035 *                   Xavier Poinsard;
    036 *
    037 * Changes
    038 * -------
    039 * 11-Dec-2001 : Version 1.  The JPEG method comes from Wolfgang Irler's
    040 *               JFreeChartServletDemo class (DG);
    041 * 23-Jan-2002 : Changed saveChartAsXXX() methods to pass IOExceptions back to
    042 *               caller (DG);
    043 * 26-Jun-2002 : Added image map methods (DG);
    044 * 05-Aug-2002 : Added writeBufferedImage methods
    045 *               Modified writeImageMap method to support flexible image
    046 *               maps (RA);
    047 * 26-Aug-2002 : Added saveChartAsJPEG and writeChartAsJPEG methods with info
    048 *               objects (RA);
    049 * 05-Sep-2002 : Added writeImageMap() method to support OverLIB
    050 *               - http://www.bosrup.com/web/overlib (RA);
    051 * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
    052 * 17-Oct-2002 : Exposed JPEG quality setting and PNG compression level as
    053 *               parameters (DG);
    054 * 25-Oct-2002 : Fixed writeChartAsJPEG() empty method bug (DG);
    055 * 13-Mar-2003 : Updated writeImageMap method as suggested by Xavier Poinsard
    056 *               (see Feature Request 688079) (DG);
    057 * 12-Aug-2003 : Added support for custom image maps using
    058 *               ToolTipTagFragmentGenerator and URLTagFragmentGenerator (RA);
    059 * 02-Sep-2003 : Separated PNG encoding from writing chart to an
    060 *               OutputStream (RA);
    061 * 04-Dec-2003 : Chart draw() method modified to include anchor point (DG);
    062 * 20-Feb-2004 : Edited Javadocs and added argument checking (DG);
    063 * 05-Apr-2004 : Fixed problem with buffered image type (DG);
    064 * 01-Aug-2004 : Modified to use EncoderUtil for all image encoding (RA);
    065 * 02-Aug-2004 : Delegated image map related functionality to ImageMapUtil (RA);
    066 * 13-Jan-2005 : Renamed ImageMapUtil --> ImageMapUtilities, removed method
    067 *               writeImageMap(PrintWriter, String, ChartRenderingInfo) which
    068 *               exists in ImageMapUtilities (DG);
    069 * ------------- JFREECHART 1.0.x ---------------------------------------------
    070 * 06-Feb-2006 : API doc update (DG);
    071 * 19-Mar-2007 : Use try-finally to close output stream in saveChartAsXXX()
    072 *               methods (DG);
    073 * 10-Jan-2008 : Fix bug 1868251 - don't create image with transparency when
    074 *               saving to JPEG format (DG);
    075 *
    076 */
    077
    078package org.jfree.chart;
    079
    080import java.awt.Graphics2D;
    081import java.awt.geom.AffineTransform;
    082import java.awt.geom.Rectangle2D;
    083import java.awt.image.BufferedImage;
    084import java.io.BufferedOutputStream;
    085import java.io.File;
    086import java.io.FileOutputStream;
    087import java.io.IOException;
    088import java.io.OutputStream;
    089import java.io.PrintWriter;
    090
    091import org.jfree.chart.encoders.EncoderUtil;
    092import org.jfree.chart.encoders.ImageFormat;
    093import org.jfree.chart.imagemap.ImageMapUtilities;
    094import org.jfree.chart.imagemap.OverLIBToolTipTagFragmentGenerator;
    095import org.jfree.chart.imagemap.StandardToolTipTagFragmentGenerator;
    096import org.jfree.chart.imagemap.StandardURLTagFragmentGenerator;
    097import org.jfree.chart.imagemap.ToolTipTagFragmentGenerator;
    098import org.jfree.chart.imagemap.URLTagFragmentGenerator;
    099
    100/**
    101 * A collection of utility methods for JFreeChart.  Includes methods for
    102 * converting charts to image formats (PNG and JPEG) plus creating simple HTML
    103 * image maps.
    104 *
    105 * @see ImageMapUtilities
    106 */
    107public abstract class ChartUtilities {
    108
    109    /**
    110     * Applies the current theme to the specified chart.  This method is
    111     * provided for convenience, the theme itself is stored in the
    112     * {@link ChartFactory} class.
    113     *
    114     * @param chart  the chart (<code>null</code> not permitted).
    115     *
    116     * @since 1.0.11
    117     */
    118    public static void applyCurrentTheme(JFreeChart chart) {
    119        ChartFactory.getChartTheme().apply(chart);
    120    }
    121
    122    /**
    123     * Writes a chart to an output stream in PNG format.
    124     *
    125     * @param out  the output stream (<code>null</code> not permitted).
    126     * @param chart  the chart (<code>null</code> not permitted).
    127     * @param width  the image width.
    128     * @param height  the image height.
    129     *
    130     * @throws IOException if there are any I/O errors.
    131     */
    132    public static void writeChartAsPNG(OutputStream out, JFreeChart chart,
    133            int width, int height) throws IOException {
    134
    135        // defer argument checking...
    136        writeChartAsPNG(out, chart, width, height, null);
    137
    138    }
    139
    140    /**
    141     * Writes a chart to an output stream in PNG format.
    142     *
    143     * @param out  the output stream (<code>null</code> not permitted).
    144     * @param chart  the chart (<code>null</code> not permitted).
    145     * @param width  the image width.
    146     * @param height  the image height.
    147     * @param encodeAlpha  encode alpha?
    148     * @param compression  the compression level (0-9).
    149     *
    150     * @throws IOException if there are any I/O errors.
    151     */
    152    public static void writeChartAsPNG(OutputStream out, JFreeChart chart,
    153            int width, int height, boolean encodeAlpha, int compression)
    154            throws IOException {
    155
    156        // defer argument checking...
    157        ChartUtilities.writeChartAsPNG(out, chart, width, height, null,
    158                encodeAlpha, compression);
    159
    160    }
    161
    162    /**
    163     * Writes a chart to an output stream in PNG format.  This method allows
    164     * you to pass in a {@link ChartRenderingInfo} object, to collect
    165     * information about the chart dimensions/entities.  You will need this
    166     * info if you want to create an HTML image map.
    167     *
    168     * @param out  the output stream (<code>null</code> not permitted).
    169     * @param chart  the chart (<code>null</code> not permitted).
    170     * @param width  the image width.
    171     * @param height  the image height.
    172     * @param info  the chart rendering info (<code>null</code> permitted).
    173     *
    174     * @throws IOException if there are any I/O errors.
    175     */
    176    public static void writeChartAsPNG(OutputStream out, JFreeChart chart,
    177            int width, int height,  ChartRenderingInfo info)
    178            throws IOException {
    179
    180        if (chart == null) {
    181            throw new IllegalArgumentException("Null 'chart' argument.");
    182        }
    183        BufferedImage bufferedImage
    184                = chart.createBufferedImage(width, height, info);
    185        EncoderUtil.writeBufferedImage(bufferedImage, ImageFormat.PNG, out);
    186    }
    187
    188    /**
    189     * Writes a chart to an output stream in PNG format.  This method allows
    190     * you to pass in a {@link ChartRenderingInfo} object, to collect
    191     * information about the chart dimensions/entities.  You will need this
    192     * info if you want to create an HTML image map.
    193     *
    194     * @param out  the output stream (<code>null</code> not permitted).
    195     * @param chart  the chart (<code>null</code> not permitted).
    196     * @param width  the image width.
    197     * @param height  the image height.
    198     * @param info  carries back chart rendering info (<code>null</code>
    199     *              permitted).
    200     * @param encodeAlpha  encode alpha?
    201     * @param compression  the PNG compression level (0-9).
    202     *
    203     * @throws IOException if there are any I/O errors.
    204     */
    205    public static void writeChartAsPNG(OutputStream out, JFreeChart chart,
    206            int width, int height, ChartRenderingInfo info,
    207            boolean encodeAlpha, int compression) throws IOException {
    208
    209        if (out == null) {
    210            throw new IllegalArgumentException("Null 'out' argument.");
    211        }
    212        if (chart == null) {
    213            throw new IllegalArgumentException("Null 'chart' argument.");
    214        }
    215        BufferedImage chartImage = chart.createBufferedImage(width, height,
    216                BufferedImage.TYPE_INT_ARGB, info);
    217        ChartUtilities.writeBufferedImageAsPNG(out, chartImage, encodeAlpha,
    218                compression);
    219
    220    }
    221
    222    /**
    223     * Writes a scaled version of a chart to an output stream in PNG format.
    224     *
    225     * @param out  the output stream (<code>null</code> not permitted).
    226     * @param chart  the chart (<code>null</code> not permitted).
    227     * @param width  the unscaled chart width.
    228     * @param height  the unscaled chart height.
    229     * @param widthScaleFactor  the horizontal scale factor.
    230     * @param heightScaleFactor  the vertical scale factor.
    231     *
    232     * @throws IOException if there are any I/O problems.
    233     */
    234    public static void writeScaledChartAsPNG(OutputStream out,
    235            JFreeChart chart, int width, int height, int widthScaleFactor,
    236            int heightScaleFactor) throws IOException {
    237
    238        if (out == null) {
    239            throw new IllegalArgumentException("Null 'out' argument.");
    240        }
    241        if (chart == null) {
    242            throw new IllegalArgumentException("Null 'chart' argument.");
    243        }
    244
    245        double desiredWidth = width * widthScaleFactor;
    246        double desiredHeight = height * heightScaleFactor;
    247        double defaultWidth = width;
    248        double defaultHeight = height;
    249        boolean scale = false;
    250
    251        // get desired width and height from somewhere then...
    252        if ((widthScaleFactor != 1) || (heightScaleFactor != 1)) {
    253            scale = true;
    254        }
    255
    256        double scaleX = desiredWidth / defaultWidth;
    257        double scaleY = desiredHeight / defaultHeight;
    258
    259        BufferedImage image = new BufferedImage((int) desiredWidth,
    260                (int) desiredHeight, BufferedImage.TYPE_INT_ARGB);
    261        Graphics2D g2 = image.createGraphics();
    262
    263        if (scale) {
    264            AffineTransform saved = g2.getTransform();
    265            g2.transform(AffineTransform.getScaleInstance(scaleX, scaleY));
    266            chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth,
    267                    defaultHeight), null, null);
    268            g2.setTransform(saved);
    269            g2.dispose();
    270        }
    271        else {
    272            chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth,
    273                    defaultHeight), null, null);
    274        }
    275        out.write(encodeAsPNG(image));
    276
    277    }
    278
    279    /**
    280     * Saves a chart to the specified file in PNG format.
    281     *
    282     * @param file  the file name (<code>null</code> not permitted).
    283     * @param chart  the chart (<code>null</code> not permitted).
    284     * @param width  the image width.
    285     * @param height  the image height.
    286     *
    287     * @throws IOException if there are any I/O errors.
    288     */
    289    public static void saveChartAsPNG(File file, JFreeChart chart,
    290            int width, int height) throws IOException {
    291
    292        // defer argument checking...
    293        saveChartAsPNG(file, chart, width, height, null);
    294
    295    }
    296
    297    /**
    298     * Saves a chart to a file in PNG format.  This method allows you to pass
    299     * in a {@link ChartRenderingInfo} object, to collect information about the
    300     * chart dimensions/entities.  You will need this info if you want to
    301     * create an HTML image map.
    302     *
    303     * @param file  the file (<code>null</code> not permitted).
    304     * @param chart  the chart (<code>null</code> not permitted).
    305     * @param width  the image width.
    306     * @param height  the image height.
    307     * @param info  the chart rendering info (<code>null</code> permitted).
    308     *
    309     * @throws IOException if there are any I/O errors.
    310     */
    311    public static void saveChartAsPNG(File file, JFreeChart chart,
    312            int width, int height, ChartRenderingInfo info)
    313        throws IOException {
    314
    315        if (file == null) {
    316            throw new IllegalArgumentException("Null 'file' argument.");
    317        }
    318        OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    319        try {
    320            ChartUtilities.writeChartAsPNG(out, chart, width, height, info);
    321        }
    322        finally {
    323            out.close();
    324        }
    325    }
    326
    327    /**
    328     * Saves a chart to a file in PNG format.  This method allows you to pass
    329     * in a {@link ChartRenderingInfo} object, to collect information about the
    330     * chart dimensions/entities.  You will need this info if you want to
    331     * create an HTML image map.
    332     *
    333     * @param file  the file (<code>null</code> not permitted).
    334     * @param chart  the chart (<code>null</code> not permitted).
    335     * @param width  the image width.
    336     * @param height  the image height.
    337     * @param info  the chart rendering info (<code>null</code> permitted).
    338     * @param encodeAlpha  encode alpha?
    339     * @param compression  the PNG compression level (0-9).
    340     *
    341     * @throws IOException if there are any I/O errors.
    342     */
    343    public static void saveChartAsPNG(File file, JFreeChart chart,
    344           int width, int height, ChartRenderingInfo info, boolean encodeAlpha,
    345           int compression) throws IOException {
    346
    347        if (file == null) {
    348            throw new IllegalArgumentException("Null 'file' argument.");
    349        }
    350        if (chart == null) {
    351            throw new IllegalArgumentException("Null 'chart' argument.");
    352        }
    353
    354        OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    355        try {
    356            writeChartAsPNG(out, chart, width, height, info, encodeAlpha,
    357                    compression);
    358        }
    359        finally {
    360            out.close();
    361        }
    362
    363    }
    364
    365    /**
    366     * Writes a chart to an output stream in JPEG format.  Please note that
    367     * JPEG is a poor format for chart images, use PNG if possible.
    368     *
    369     * @param out  the output stream (<code>null</code> not permitted).
    370     * @param chart  the chart (<code>null</code> not permitted).
    371     * @param width  the image width.
    372     * @param height  the image height.
    373     *
    374     * @throws IOException if there are any I/O errors.
    375     */
    376    public static void writeChartAsJPEG(OutputStream out,
    377            JFreeChart chart, int width, int height) throws IOException {
    378
    379        // defer argument checking...
    380        writeChartAsJPEG(out, chart, width, height, null);
    381
    382    }
    383
    384    /**
    385     * Writes a chart to an output stream in JPEG format.  Please note that
    386     * JPEG is a poor format for chart images, use PNG if possible.
    387     *
    388     * @param out  the output stream (<code>null</code> not permitted).
    389     * @param quality  the quality setting.
    390     * @param chart  the chart (<code>null</code> not permitted).
    391     * @param width  the image width.
    392     * @param height  the image height.
    393     *
    394     * @throws IOException if there are any I/O errors.
    395     */
    396    public static void writeChartAsJPEG(OutputStream out, float quality,
    397            JFreeChart chart, int width, int height) throws IOException {
    398
    399        // defer argument checking...
    400        ChartUtilities.writeChartAsJPEG(out, quality, chart, width, height,
    401                null);
    402
    403    }
    404
    405    /**
    406     * Writes a chart to an output stream in JPEG format. This method allows
    407     * you to pass in a {@link ChartRenderingInfo} object, to collect
    408     * information about the chart dimensions/entities.  You will need this
    409     * info if you want to create an HTML image map.
    410     *
    411     * @param out  the output stream (<code>null</code> not permitted).
    412     * @param chart  the chart (<code>null</code> not permitted).
    413     * @param width  the image width.
    414     * @param height  the image height.
    415     * @param info  the chart rendering info (<code>null</code> permitted).
    416     *
    417     * @throws IOException if there are any I/O errors.
    418     */
    419    public static void writeChartAsJPEG(OutputStream out, JFreeChart chart,
    420            int width, int height, ChartRenderingInfo info)
    421            throws IOException {
    422
    423        if (chart == null) {
    424            throw new IllegalArgumentException("Null 'chart' argument.");
    425        }
    426        BufferedImage image = chart.createBufferedImage(width, height,
    427                BufferedImage.TYPE_INT_RGB, info);
    428        EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out);
    429
    430    }
    431
    432    /**
    433     * Writes a chart to an output stream in JPEG format.  This method allows
    434     * you to pass in a {@link ChartRenderingInfo} object, to collect
    435     * information about the chart dimensions/entities.  You will need this
    436     * info if you want to create an HTML image map.
    437     *
    438     * @param out  the output stream (<code>null</code> not permitted).
    439     * @param quality  the output quality (0.0f to 1.0f).
    440     * @param chart  the chart (<code>null</code> not permitted).
    441     * @param width  the image width.
    442     * @param height  the image height.
    443     * @param info  the chart rendering info (<code>null</code> permitted).
    444     *
    445     * @throws IOException if there are any I/O errors.
    446     */
    447    public static void writeChartAsJPEG(OutputStream out, float quality,
    448            JFreeChart chart, int width, int height, ChartRenderingInfo info)
    449            throws IOException {
    450
    451        if (chart == null) {
    452            throw new IllegalArgumentException("Null 'chart' argument.");
    453        }
    454        BufferedImage image = chart.createBufferedImage(width, height,
    455                BufferedImage.TYPE_INT_RGB, info);
    456        EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, quality);
    457
    458    }
    459
    460    /**
    461     * Saves a chart to a file in JPEG format.
    462     *
    463     * @param file  the file (<code>null</code> not permitted).
    464     * @param chart  the chart (<code>null</code> not permitted).
    465     * @param width  the image width.
    466     * @param height  the image height.
    467     *
    468     * @throws IOException if there are any I/O errors.
    469     */
    470    public static void saveChartAsJPEG(File file, JFreeChart chart,
    471            int width, int height) throws IOException {
    472
    473        // defer argument checking...
    474        saveChartAsJPEG(file, chart, width, height, null);
    475
    476    }
    477
    478    /**
    479     * Saves a chart to a file in JPEG format.
    480     *
    481     * @param file  the file (<code>null</code> not permitted).
    482     * @param quality  the JPEG quality setting.
    483     * @param chart  the chart (<code>null</code> not permitted).
    484     * @param width  the image width.
    485     * @param height  the image height.
    486     *
    487     * @throws IOException if there are any I/O errors.
    488     */
    489    public static void saveChartAsJPEG(File file, float quality,
    490            JFreeChart chart, int width, int height) throws IOException {
    491
    492        // defer argument checking...
    493        saveChartAsJPEG(file, quality, chart, width, height, null);
    494
    495    }
    496
    497    /**
    498     * Saves a chart to a file in JPEG format.  This method allows you to pass
    499     * in a {@link ChartRenderingInfo} object, to collect information about the
    500     * chart dimensions/entities.  You will need this info if you want to
    501     * create an HTML image map.
    502     *
    503     * @param file  the file name (<code>null</code> not permitted).
    504     * @param chart  the chart (<code>null</code> not permitted).
    505     * @param width  the image width.
    506     * @param height  the image height.
    507     * @param info  the chart rendering info (<code>null</code> permitted).
    508     *
    509     * @throws IOException if there are any I/O errors.
    510     */
    511    public static void saveChartAsJPEG(File file, JFreeChart chart,
    512            int width, int height, ChartRenderingInfo info) throws IOException {
    513
    514        if (file == null) {
    515            throw new IllegalArgumentException("Null 'file' argument.");
    516        }
    517        if (chart == null) {
    518            throw new IllegalArgumentException("Null 'chart' argument.");
    519        }
    520        OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    521        try {
    522            writeChartAsJPEG(out, chart, width, height, info);
    523        }
    524        finally {
    525            out.close();
    526        }
    527
    528    }
    529
    530    /**
    531     * Saves a chart to a file in JPEG format.  This method allows you to pass
    532     * in a {@link ChartRenderingInfo} object, to collect information about the
    533     * chart dimensions/entities.  You will need this info if you want to
    534     * create an HTML image map.
    535     *
    536     * @param file  the file name (<code>null</code> not permitted).
    537     * @param quality  the quality setting.
    538     * @param chart  the chart (<code>null</code> not permitted).
    539     * @param width  the image width.
    540     * @param height  the image height.
    541     * @param info  the chart rendering info (<code>null</code> permitted).
    542     *
    543     * @throws IOException if there are any I/O errors.
    544     */
    545    public static void saveChartAsJPEG(File file, float quality,
    546            JFreeChart chart, int width, int height,
    547            ChartRenderingInfo info) throws IOException {
    548
    549        if (file == null) {
    550            throw new IllegalArgumentException("Null 'file' argument.");
    551        }
    552        if (chart == null) {
    553            throw new IllegalArgumentException("Null 'chart' argument.");
    554        }
    555
    556        OutputStream out = new BufferedOutputStream(new FileOutputStream(
    557                file));
    558        try {
    559            writeChartAsJPEG(out, quality, chart, width, height, info);
    560        }
    561        finally {
    562            out.close();
    563        }
    564
    565    }
    566
    567    /**
    568     * Writes a {@link BufferedImage} to an output stream in JPEG format.
    569     *
    570     * @param out  the output stream (<code>null</code> not permitted).
    571     * @param image  the image (<code>null</code> not permitted).
    572     *
    573     * @throws IOException if there are any I/O errors.
    574     */
    575    public static void writeBufferedImageAsJPEG(OutputStream out,
    576            BufferedImage image) throws IOException {
    577
    578        // defer argument checking...
    579        writeBufferedImageAsJPEG(out, 0.75f, image);
    580
    581    }
    582
    583    /**
    584     * Writes a {@link BufferedImage} to an output stream in JPEG format.
    585     *
    586     * @param out  the output stream (<code>null</code> not permitted).
    587     * @param quality  the image quality (0.0f to 1.0f).
    588     * @param image  the image (<code>null</code> not permitted).
    589     *
    590     * @throws IOException if there are any I/O errors.
    591     */
    592    public static void writeBufferedImageAsJPEG(OutputStream out, float quality,
    593            BufferedImage image) throws IOException {
    594
    595        EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, quality);
    596
    597    }
    598
    599    /**
    600     * Writes a {@link BufferedImage} to an output stream in PNG format.
    601     *
    602     * @param out  the output stream (<code>null</code> not permitted).
    603     * @param image  the image (<code>null</code> not permitted).
    604     *
    605     * @throws IOException if there are any I/O errors.
    606     */
    607    public static void writeBufferedImageAsPNG(OutputStream out,
    608            BufferedImage image) throws IOException {
    609
    610        EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out);
    611
    612    }
    613
    614    /**
    615     * Writes a {@link BufferedImage} to an output stream in PNG format.
    616     *
    617     * @param out  the output stream (<code>null</code> not permitted).
    618     * @param image  the image (<code>null</code> not permitted).
    619     * @param encodeAlpha  encode alpha?
    620     * @param compression  the compression level (0-9).
    621     *
    622     * @throws IOException if there are any I/O errors.
    623     */
    624    public static void writeBufferedImageAsPNG(OutputStream out,
    625            BufferedImage image, boolean encodeAlpha, int compression)
    626            throws IOException {
    627
    628        EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out,
    629                compression, encodeAlpha);
    630    }
    631
    632    /**
    633     * Encodes a {@link BufferedImage} to PNG format.
    634     *
    635     * @param image  the image (<code>null</code> not permitted).
    636     *
    637     * @return A byte array in PNG format.
    638     *
    639     * @throws IOException if there is an I/O problem.
    640     */
    641    public static byte[] encodeAsPNG(BufferedImage image) throws IOException {
    642        return EncoderUtil.encode(image, ImageFormat.PNG);
    643    }
    644
    645    /**
    646     * Encodes a {@link BufferedImage} to PNG format.
    647     *
    648     * @param image  the image (<code>null</code> not permitted).
    649     * @param encodeAlpha  encode alpha?
    650     * @param compression  the PNG compression level (0-9).
    651     *
    652     * @return The byte array in PNG format.
    653     *
    654     * @throws IOException if there is an I/O problem.
    655     */
    656    public static byte[] encodeAsPNG(BufferedImage image, boolean encodeAlpha,
    657                                     int compression)
    658            throws IOException {
    659        return EncoderUtil.encode(image, ImageFormat.PNG, compression,
    660                encodeAlpha);
    661    }
    662
    663    /**
    664     * Writes an image map to an output stream.
    665     *
    666     * @param writer  the writer (<code>null</code> not permitted).
    667     * @param name  the map name (<code>null</code> not permitted).
    668     * @param info  the chart rendering info (<code>null</code> not permitted).
    669     * @param useOverLibForToolTips  whether to use OverLIB for tooltips
    670     *                               (http://www.bosrup.com/web/overlib/).
    671     *
    672     * @throws IOException if there are any I/O errors.
    673     */
    674    public static void writeImageMap(PrintWriter writer,
    675                                     String name,
    676                                     ChartRenderingInfo info,
    677                                     boolean useOverLibForToolTips)
    678        throws IOException {
    679
    680        ToolTipTagFragmentGenerator toolTipTagFragmentGenerator = null;
    681        if (useOverLibForToolTips) {
    682            toolTipTagFragmentGenerator
    683                    = new OverLIBToolTipTagFragmentGenerator();
    684        }
    685        else {
    686            toolTipTagFragmentGenerator
    687                    = new StandardToolTipTagFragmentGenerator();
    688        }
    689        ImageMapUtilities.writeImageMap(writer, name, info,
    690                toolTipTagFragmentGenerator,
    691                new StandardURLTagFragmentGenerator());
    692
    693    }
    694
    695    /**
    696     * Writes an image map to the specified writer.
    697     *
    698     * @param writer  the writer (<code>null</code> not permitted).
    699     * @param name  the map name (<code>null</code> not permitted).
    700     * @param info  the chart rendering info (<code>null</code> not permitted).
    701     * @param toolTipTagFragmentGenerator  a generator for the HTML fragment
    702     *     that will contain the tooltip text (<code>null</code> not permitted
    703     *     if <code>info</code> contains tooltip information).
    704     * @param urlTagFragmentGenerator  a generator for the HTML fragment that
    705     *     will contain the URL reference (<code>null</code> not permitted if
    706     *     <code>info</code> contains URLs).
    707     *
    708     * @throws IOException if there are any I/O errors.
    709     */
    710    public static void writeImageMap(PrintWriter writer, String name,
    711            ChartRenderingInfo info,
    712            ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
    713            URLTagFragmentGenerator urlTagFragmentGenerator)
    714            throws IOException {
    715
    716        writer.println(ImageMapUtilities.getImageMap(name, info,
    717                toolTipTagFragmentGenerator, urlTagFragmentGenerator));
    718    }
    719
    720    /**
    721     * Creates an HTML image map.  This method maps to
    722     * {@link ImageMapUtilities#getImageMap(String, ChartRenderingInfo,
    723     * ToolTipTagFragmentGenerator, URLTagFragmentGenerator)}, using default
    724     * generators.
    725     *
    726     * @param name  the map name (<code>null</code> not permitted).
    727     * @param info  the chart rendering info (<code>null</code> not permitted).
    728     *
    729     * @return The map tag.
    730     */
    731    public static String getImageMap(String name, ChartRenderingInfo info) {
    732        return ImageMapUtilities.getImageMap(name, info,
    733                new StandardToolTipTagFragmentGenerator(),
    734                new StandardURLTagFragmentGenerator());
    735    }
    736
    737    /**
    738     * Creates an HTML image map.  This method maps directly to
    739     * {@link ImageMapUtilities#getImageMap(String, ChartRenderingInfo,
    740     * ToolTipTagFragmentGenerator, URLTagFragmentGenerator)}.
    741     *
    742     * @param name  the map name (<code>null</code> not permitted).
    743     * @param info  the chart rendering info (<code>null</code> not permitted).
    744     * @param toolTipTagFragmentGenerator  a generator for the HTML fragment
    745     *     that will contain the tooltip text (<code>null</code> not permitted
    746     *     if <code>info</code> contains tooltip information).
    747     * @param urlTagFragmentGenerator  a generator for the HTML fragment that
    748     *     will contain the URL reference (<code>null</code> not permitted if
    749     *     <code>info</code> contains URLs).
    750     *
    751     * @return The map tag.
    752     */
    753    public static String getImageMap(String name, ChartRenderingInfo info,
    754            ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
    755            URLTagFragmentGenerator urlTagFragmentGenerator) {
    756
    757        return ImageMapUtilities.getImageMap(name, info,
    758                toolTipTagFragmentGenerator, urlTagFragmentGenerator);
    759
    760    }
    761
    762}