    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     * ChartUtilities.java
    029     * -------------------
    030     * (C) Copyright 2001-2005, 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.1 2005/10/25 16:50:20 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     *
    072     */
    073
    074    package org.jfree.chart;
    075
    076    import java.awt.Graphics2D;
    077    import java.awt.geom.AffineTransform;
    078    import java.awt.geom.Rectangle2D;
    079    import java.awt.image.BufferedImage;
    080    import java.io.BufferedOutputStream;
    081    import java.io.File;
    082    import java.io.FileOutputStream;
    083    import java.io.IOException;
    084    import java.io.OutputStream;
    085    import java.io.PrintWriter;
    086
    087    import org.jfree.chart.imagemap.ImageMapUtilities;
    088    import org.jfree.chart.imagemap.OverLIBToolTipTagFragmentGenerator;
    089    import org.jfree.chart.imagemap.StandardToolTipTagFragmentGenerator;
    090    import org.jfree.chart.imagemap.StandardURLTagFragmentGenerator;
    091    import org.jfree.chart.imagemap.ToolTipTagFragmentGenerator;
    092    import org.jfree.chart.imagemap.URLTagFragmentGenerator;
    093
    094    import org.jfree.chart.encoders.EncoderUtil;
    095    import org.jfree.chart.encoders.ImageFormat;
    096
    097    /**
    098     * A collection of utility methods for JFreeChart.  Includes methods for
    099     * converting charts to image formats (PNG and JPEG) plus creating simple HTML
    100     * image maps.
    101     */
    102    public abstract class ChartUtilities {
    103
    104        /**
    105         * Writes a chart to an output stream in PNG format.
    106         *
    107         * @param out  the output stream (<code>null</code> not permitted).
    108         * @param chart  the chart (<code>null</code> not permitted).
    109         * @param width  the image width.
    110         * @param height  the image height.
    111         *
    112         * @throws IOException if there are any I/O errors.
    113         */
    114        public static void writeChartAsPNG(OutputStream out,
    115                                           JFreeChart chart,
    116                                           int width,
    117                                           int height) throws IOException {
    118
    119            // defer argument checking...
    120            writeChartAsPNG(out, chart, width, height, null);
    121
    122        }
    123
    124        /**
    125         * Writes a chart to an output stream in PNG format.
    126         *
    127         * @param out  the output stream (<code>null</code> not permitted).
    128         * @param chart  the chart (<code>null</code> not permitted).
    129         * @param width  the image width.
    130         * @param height  the image height.
    131         * @param encodeAlpha  encode alpha?
    132         * @param compression  the compression level (0-9).
    133         *
    134         * @throws IOException if there are any I/O errors.
    135         */
    136        public static void writeChartAsPNG(OutputStream out,
    137                                           JFreeChart chart,
    138                                           int width,
    139                                           int height,
    140                                           boolean encodeAlpha,
    141                                           int compression) throws IOException {
    142
    143            // defer argument checking...
    144            ChartUtilities.writeChartAsPNG(
    145                out, chart, width, height, null, encodeAlpha, compression
    146            );
    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,
    165                                           JFreeChart chart,
    166                                           int width,
    167                                           int height,
    168                                           ChartRenderingInfo info)
    169            throws IOException {
    170
    171            if (chart == null) {
    172                throw new IllegalArgumentException("Null 'chart' argument.");
    173            }
    174            BufferedImage bufferedImage
    175                = chart.createBufferedImage(width, height, info);
    176            EncoderUtil.writeBufferedImage(bufferedImage, ImageFormat.PNG, out);
    177        }
    178
    179        /**
    180         * Writes a chart to an output stream in PNG format.  This method allows
    181         * you to pass in a {@link ChartRenderingInfo} object, to collect
    182         * information about the chart dimensions/entities.  You will need this
    183         * info if you want to create an HTML image map.
    184         *
    185         * @param out  the output stream (<code>null</code> not permitted).
    186         * @param chart  the chart (<code>null</code> not permitted).
    187         * @param width  the image width.
    188         * @param height  the image height.
    189         * @param info  carries back chart rendering info (<code>null</code>
    190         *              permitted).
    191         * @param encodeAlpha  encode alpha?
    192         * @param compression  the PNG compression level (0-9).
    193         *
    194         * @throws IOException if there are any I/O errors.
    195         */
    196        public static void writeChartAsPNG(OutputStream out,
    197                                           JFreeChart chart,
    198                                           int width, int height,
    199                                           ChartRenderingInfo info,
    200                                           boolean encodeAlpha,
    201                                           int compression) throws IOException {
    202
    203            if (out == null) {
    204                throw new IllegalArgumentException("Null 'out' argument.");
    205            }
    206            if (chart == null) {
    207                throw new IllegalArgumentException("Null 'chart' argument.");
    208            }
    209            BufferedImage chartImage = chart.createBufferedImage(
    210                width, height, BufferedImage.TYPE_INT_ARGB, info
    211            );
    212            ChartUtilities.writeBufferedImageAsPNG(
    213                out, chartImage, encodeAlpha, compression
    214            );
    215
    216        }
    217
    218        /**
    219         * Writes a scaled version of a chart to an output stream in PNG format.
    220         *
    221         * @param out  the output stream (<code>null</code> not permitted).
    222         * @param chart  the chart (<code>null</code> not permitted).
    223         * @param width  the unscaled chart width.
    224         * @param height  the unscaled chart height.
    225         * @param widthScaleFactor  the horizontal scale factor.
    226         * @param heightScaleFactor  the vertical scale factor.
    227         *
    228         * @throws IOException if there are any I/O problems.
    229         */
    230        public static void writeScaledChartAsPNG(OutputStream out,
    231                                                 JFreeChart chart,
    232                                                 int width,
    233                                                 int height,
    234                                                 int widthScaleFactor,
    235                                                 int heightScaleFactor)
    236            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(
    260                (int) desiredWidth, (int) desiredHeight, BufferedImage.TYPE_INT_ARGB
    261            );
    262            Graphics2D g2 = image.createGraphics();
    263
    264            if (scale) {
    265                AffineTransform saved = g2.getTransform();
    266                g2.transform(AffineTransform.getScaleInstance(scaleX, scaleY));
    267                chart.draw(
    268                    g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight),
    269                    null, null
    270                );
    271                g2.setTransform(saved);
    272                g2.dispose();
    273            }
    274            else {
    275                chart.draw(
    276                    g2, new Rectangle2D.Double(0, 0, defaultWidth, defaultHeight),
    277                    null, null
    278                );
    279            }
    280            out.write(encodeAsPNG(image));
    281
    282        }
    283
    284        /**
    285         * Saves a chart to the specified file in PNG format.
    286         *
    287         * @param file  the file name (<code>null</code> not permitted).
    288         * @param chart  the chart (<code>null</code> not permitted).
    289         * @param width  the image width.
    290         * @param height  the image height.
    291         *
    292         * @throws IOException if there are any I/O errors.
    293         */
    294        public static void saveChartAsPNG(File file,
    295                                          JFreeChart chart,
    296                                          int width,
    297                                          int height) throws IOException {
    298
    299            // defer argument checking...
    300            saveChartAsPNG(file, chart, width, height, null);
    301
    302        }
    303
    304        /**
    305         * Saves a chart to a file in PNG format.  This method allows you to pass
    306         * in a {@link ChartRenderingInfo} object, to collect information about the
    307         * chart dimensions/entities.  You will need this info if you want to
    308         * create an HTML image map.
    309         *
    310         * @param file  the file (<code>null</code> not permitted).
    311         * @param chart  the chart (<code>null</code> not permitted).
    312         * @param width  the image width.
    313         * @param height  the image height.
    314         * @param info  the chart rendering info (<code>null</code> permitted).
    315         *
    316         * @throws IOException if there are any I/O errors.
    317         */
    318        public static void saveChartAsPNG(File file,
    319                                          JFreeChart chart,
    320                                          int width,
    321                                          int height,
    322                                          ChartRenderingInfo info)
    323            throws IOException {
    324
    325            if (file == null) {
    326                throw new IllegalArgumentException("Null 'file' argument.");
    327            }
    328            OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    329            ChartUtilities.writeChartAsPNG(out, chart, width, height, info);
    330            out.close();
    331        }
    332
    333        /**
    334         * Saves a chart to a file in PNG format.  This method allows you to pass
    335         * in a {@link ChartRenderingInfo} object, to collect information about the
    336         * chart dimensions/entities.  You will need this info if you want to
    337         * create an HTML image map.
    338         *
    339         * @param file  the file (<code>null</code> not permitted).
    340         * @param chart  the chart (<code>null</code> not permitted).
    341         * @param width  the image width.
    342         * @param height  the image height.
    343         * @param info  the chart rendering info (<code>null</code> permitted).
    344         * @param encodeAlpha  encode alpha?
    345         * @param compression  the PNG compression level (0-9).
    346         *
    347         * @throws IOException if there are any I/O errors.
    348         */
    349        public static void saveChartAsPNG(File file,
    350                                          JFreeChart chart,
    351                                          int width,
    352                                          int height,
    353                                          ChartRenderingInfo info,
    354                                          boolean encodeAlpha,
    355                                          int compression) throws IOException {
    356
    357            if (file == null) {
    358                throw new IllegalArgumentException("Null 'file' argument.");
    359            }
    360            if (chart == null) {
    361                throw new IllegalArgumentException("Null 'chart' argument.");
    362            }
    363
    364            OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    365            writeChartAsPNG(
    366                out, chart, width, height, info, encodeAlpha, compression
    367            );
    368            out.close();
    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 chart  the chart (<code>null</code> not permitted).
    378         * @param width  the image width.
    379         * @param height  the image height.
    380         *
    381         * @throws IOException if there are any I/O errors.
    382         */
    383        public static void writeChartAsJPEG(OutputStream out,
    384                                            JFreeChart chart,
    385                                            int width,
    386                                            int height) throws IOException {
    387
    388            // defer argument checking...
    389            writeChartAsJPEG(out, chart, width, height, null);
    390
    391        }
    392
    393        /**
    394         * Writes a chart to an output stream in JPEG format.  Please note that
    395         * JPEG is a poor format for chart images, use PNG if possible.
    396         *
    397         * @param out  the output stream (<code>null</code> not permitted).
    398         * @param quality  the quality setting.
    399         * @param chart  the chart (<code>null</code> not permitted).
    400         * @param width  the image width.
    401         * @param height  the image height.
    402         *
    403         * @throws IOException if there are any I/O errors.
    404         */
    405        public static void writeChartAsJPEG(OutputStream out,
    406                                            float quality,
    407                                            JFreeChart chart,
    408                                            int width,
    409                                            int height) throws IOException {
    410
    411            // defer argument checking...
    412            ChartUtilities.writeChartAsJPEG(
    413                out, quality, chart, width, height, null
    414            );
    415
    416        }
    417
    418        /**
    419         * Writes a chart to an output stream in JPEG format. This method allows
    420         * you to pass in a {@link ChartRenderingInfo} object, to collect
    421         * information about the chart dimensions/entities.  You will need this
    422         * info if you want to create an HTML image map.
    423         *
    424         * @param out  the output stream (<code>null</code> not permitted).
    425         * @param chart  the chart (<code>null</code> not permitted).
    426         * @param width  the image width.
    427         * @param height  the image height.
    428         * @param info  the chart rendering info (<code>null</code> permitted).
    429         *
    430         * @throws IOException if there are any I/O errors.
    431         */
    432        public static void writeChartAsJPEG(OutputStream out,
    433                                            JFreeChart chart,
    434                                            int width,
    435                                            int height,
    436                                            ChartRenderingInfo info)
    437            throws IOException {
    438
    439            if (chart == null) {
    440                throw new IllegalArgumentException("Null 'chart' argument.");
    441            }
    442            BufferedImage image = chart.createBufferedImage(width, height, info);
    443            EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out);
    444
    445        }
    446
    447        /**
    448         * Writes a chart to an output stream in JPEG format.  This method allows
    449         * you to pass in a {@link ChartRenderingInfo} object, to collect
    450         * information about the chart dimensions/entities.  You will need this
    451         * info if you want to create an HTML image map.
    452         *
    453         * @param out  the output stream (<code>null</code> not permitted).
    454         * @param quality  the output quality (0.0f to 1.0f).
    455         * @param chart  the chart (<code>null</code> not permitted).
    456         * @param width  the image width.
    457         * @param height  the image height.
    458         * @param info  the chart rendering info (<code>null</code> permitted).
    459         *
    460         * @throws IOException if there are any I/O errors.
    461         */
    462        public static void writeChartAsJPEG(OutputStream out,
    463                                            float quality,
    464                                            JFreeChart chart,
    465                                            int width,
    466                                            int height,
    467                                            ChartRenderingInfo info)
    468            throws IOException {
    469
    470            if (chart == null) {
    471                throw new IllegalArgumentException("Null 'chart' argument.");
    472            }
    473            BufferedImage image = chart.createBufferedImage(width, height, info);
    474            EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, quality);
    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 chart  the chart (<code>null</code> not permitted).
    483         * @param width  the image width.
    484         * @param height  the image height.
    485         *
    486         * @throws IOException if there are any I/O errors.
    487         */
    488        public static void saveChartAsJPEG(File file,
    489                                           JFreeChart chart,
    490                                           int width,
    491                                           int height) throws IOException {
    492
    493            // defer argument checking...
    494            saveChartAsJPEG(file, chart, width, height, null);
    495
    496        }
    497
    498        /**
    499         * Saves a chart to a file in JPEG format.
    500         *
    501         * @param file  the file (<code>null</code> not permitted).
    502         * @param quality  the JPEG quality setting.
    503         * @param chart  the chart (<code>null</code> not permitted).
    504         * @param width  the image width.
    505         * @param height  the image height.
    506         *
    507         * @throws IOException if there are any I/O errors.
    508         */
    509        public static void saveChartAsJPEG(File file,
    510                                           float quality,
    511                                           JFreeChart chart,
    512                                           int width,
    513                                           int height) throws IOException {
    514
    515            // defer argument checking...
    516            saveChartAsJPEG(file, quality, chart, width, height, null);
    517
    518        }
    519
    520        /**
    521         * Saves a chart to a file in JPEG format.  This method allows you to pass
    522         * in a {@link ChartRenderingInfo} object, to collect information about the
    523         * chart dimensions/entities.  You will need this info if you want to
    524         * create an HTML image map.
    525         *
    526         * @param file  the file name (<code>null</code> not permitted).
    527         * @param chart  the chart (<code>null</code> not permitted).
    528         * @param width  the image width.
    529         * @param height  the image height.
    530         * @param info  the chart rendering info (<code>null</code> permitted).
    531         *
    532         * @throws IOException if there are any I/O errors.
    533         */
    534        public static void saveChartAsJPEG(File file,
    535                                           JFreeChart chart,
    536                                           int width,
    537                                           int height,
    538                                           ChartRenderingInfo info)
    539            throws IOException {
    540
    541            if (file == null) {
    542                throw new IllegalArgumentException("Null 'file' argument.");
    543            }
    544            if (chart == null) {
    545                throw new IllegalArgumentException("Null 'chart' argument.");
    546            }
    547            OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    548            writeChartAsJPEG(out, chart, width, height, info);
    549            out.close();
    550
    551        }
    552
    553        /**
    554         * Saves a chart to a file in JPEG format.  This method allows you to pass
    555         * in a {@link ChartRenderingInfo} object, to collect information about the
    556         * chart dimensions/entities.  You will need this info if you want to
    557         * create an HTML image map.
    558         *
    559         * @param file  the file name (<code>null</code> not permitted).
    560         * @param quality  the quality setting.
    561         * @param chart  the chart (<code>null</code> not permitted).
    562         * @param width  the image width.
    563         * @param height  the image height.
    564         * @param info  the chart rendering info (<code>null</code> permitted).
    565         *
    566         * @throws IOException if there are any I/O errors.
    567         */
    568        public static void saveChartAsJPEG(File file,
    569                                           float quality,
    570                                           JFreeChart chart,
    571                                           int width,
    572                                           int height,
    573                                           ChartRenderingInfo info)
    574            throws IOException {
    575
    576            if (file == null) {
    577                throw new IllegalArgumentException("Null 'file' argument.");
    578            }
    579            if (chart == null) {
    580                throw new IllegalArgumentException("Null 'chart' argument.");
    581            }
    582            OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
    583            writeChartAsJPEG(out, quality, chart, width, height, info);
    584            out.close();
    585
    586        }
    587
    588        /**
    589         * Writes a {@link BufferedImage} to an output stream in JPEG format.
    590         *
    591         * @param out  the output stream (<code>null</code> not permitted).
    592         * @param image  the image (<code>null</code> not permitted).
    593         *
    594         * @throws IOException if there are any I/O errors.
    595         */
    596        public static void writeBufferedImageAsJPEG(OutputStream out,
    597                                                    BufferedImage image)
    598            throws IOException {
    599
    600            // defer argument checking...
    601            writeBufferedImageAsJPEG(out, 0.75f, image);
    602
    603        }
    604
    605        /**
    606         * Writes a {@link BufferedImage} to an output stream in JPEG format.
    607         *
    608         * @param out  the output stream (<code>null</code> not permitted).
    609         * @param quality  the image quality (0.0f to 1.0f).
    610         * @param image  the image (<code>null</code> not permitted).
    611         *
    612         * @throws IOException if there are any I/O errors.
    613         */
    614        public static void writeBufferedImageAsJPEG(OutputStream out, float quality,
    615                                                    BufferedImage image)
    616            throws IOException {
    617
    618            EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, quality);
    619
    620        }
    621
    622        /**
    623         * Writes a {@link BufferedImage} to an output stream in PNG format.
    624         *
    625         * @param out  the output stream (<code>null</code> not permitted).
    626         * @param image  the image (<code>null</code> not permitted).
    627         *
    628         * @throws IOException if there are any I/O errors.
    629         */
    630        public static void writeBufferedImageAsPNG(OutputStream out,
    631                                                   BufferedImage image)
    632            throws IOException {
    633
    634            EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out);
    635
    636        }
    637
    638        /**
    639         * Writes a {@link BufferedImage} to an output stream in PNG format.
    640         *
    641         * @param out  the output stream (<code>null</code> not permitted).
    642         * @param image  the image (<code>null</code> not permitted).
    643         * @param encodeAlpha  encode alpha?
    644         * @param compression  the compression level (0-9).
    645         *
    646         * @throws IOException if there are any I/O errors.
    647         */
    648        public static void writeBufferedImageAsPNG(OutputStream out,
    649                                                   BufferedImage image,
    650                                                   boolean encodeAlpha,
    651                                                   int compression)
    652            throws IOException {
    653
    654            EncoderUtil.writeBufferedImage(
    655                image, ImageFormat.PNG, out, compression, encodeAlpha
    656            );
    657        }
    658
    659        /**
    660         * Encodes a {@link BufferedImage} to PNG format.
    661         *
    662         * @param image  the image (<code>null</code> not permitted).
    663         *
    664         * @return A byte array in PNG format.
    665         *
    666         * @throws IOException if there is an I/O problem.
    667         */
    668        public static byte[] encodeAsPNG(BufferedImage image) throws IOException {
    669            return EncoderUtil.encode(image, ImageFormat.PNG);
    670        }
    671
    672        /**
    673         * Encodes a {@link BufferedImage} to PNG format.
    674         *
    675         * @param image  the image (<code>null</code> not permitted).
    676         * @param encodeAlpha  encode alpha?
    677         * @param compression  the PNG compression level (0-9).
    678         *
    679         * @return The byte array in PNG format.
    680         *
    681         * @throws IOException if there is an I/O problem.
    682         */
    683        public static byte[] encodeAsPNG(BufferedImage image, boolean encodeAlpha,
    684                                         int compression)
    685            throws IOException {
    686            return EncoderUtil.encode(
    687                image, ImageFormat.PNG, compression, encodeAlpha
    688            );
    689        }
    690
    691        /**
    692         * Writes an image map to an output stream.
    693         *
    694         * @param writer  the writer (<code>null</code> not permitted).
    695         * @param name  the map name (<code>null</code> not permitted).
    696         * @param info  the chart rendering info (<code>null</code> not permitted).
    697         * @param useOverLibForToolTips  whether to use OverLIB for tooltips
    698         *                               (http://www.bosrup.com/web/overlib/).
    699         *
    700         * @throws IOException if there are any I/O errors.
    701         */
    702        public static void writeImageMap(PrintWriter writer,
    703                                         String name,
    704                                         ChartRenderingInfo info,
    705                                         boolean useOverLibForToolTips)
    706            throws IOException {
    707
    708            ToolTipTagFragmentGenerator toolTipTagFragmentGenerator = null;
    709            if (useOverLibForToolTips) {
    710                toolTipTagFragmentGenerator
    711                    = new OverLIBToolTipTagFragmentGenerator();
    712            }
    713            else {
    714                toolTipTagFragmentGenerator
    715                    = new StandardToolTipTagFragmentGenerator();
    716            }
    717            ImageMapUtilities.writeImageMap(
    718                writer, name, info, toolTipTagFragmentGenerator,
    719                new StandardURLTagFragmentGenerator()
    720            );
    721
    722        }
    723
    724        /**
    725         * Writes an image map to an output stream.
    726         *
    727         * @param writer  the writer (<code>null</code> not permitted).
    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  the tool tip generator.
    731         * @param urlTagFragmentGenerator  the url generator.
    732         *
    733         * @throws IOException if there are any I/O errors.
    734         */
    735        public static void writeImageMap(PrintWriter writer, String name,
    736                ChartRenderingInfo info,
    737                ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
    738                URLTagFragmentGenerator urlTagFragmentGenerator)
    739                throws IOException {
    740
    741            writer.println(
    742                ImageMapUtilities.getImageMap(
    743                    name, info, toolTipTagFragmentGenerator, urlTagFragmentGenerator
    744                )
    745            );
    746        }
    747
    748        /**
    749         * Creates an HTML image map.
    750         *
    751         * @param name  the map name (<code>null</code> not permitted).
    752         * @param info  the chart rendering info (<code>null</code> not permitted).
    753         *
    754         * @return The map tag.
    755         */
    756        public static String getImageMap(String name, ChartRenderingInfo info) {
    757            return ImageMapUtilities.getImageMap(
    758                name,
    759                info,
    760                new StandardToolTipTagFragmentGenerator(),
    761                new StandardURLTagFragmentGenerator()
    762            );
    763        }
    764
    765        /**
    766         * Creates an HTML image map.
    767         *
    768         * @param name  the map name (<code>null</code> not permitted).
    769         * @param info  the chart rendering info (<code>null</code> not permitted).
    770         * @param toolTipTagFragmentGenerator  the tool tip generator.
    771         * @param urlTagFragmentGenerator  the url generator.
    772         *
    773         * @return The map tag.
    774         */
    775        public static String getImageMap(String name,
    776                         ChartRenderingInfo info,
    777                         ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
    778                         URLTagFragmentGenerator urlTagFragmentGenerator) {
    779
    780            return ImageMapUtilities.getImageMap(
    781                name, info, toolTipTagFragmentGenerator, urlTagFragmentGenerator
    782            );
    783
    784        }
    785
    786    }