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