Source for org.jfree.chart.ChartUtilities

   1: /* ===========================================================
   2:  * JFreeChart : a free chart library for the Java(tm) platform
   3:  * ===========================================================
   4:  *
   5:  * (C) Copyright 2000-2008, by Object Refinery Limited and Contributors.
   6:  *
   7:  * Project Info:  http://www.jfree.org/jfreechart/index.html
   8:  *
   9:  * This library is free software; you can redistribute it and/or modify it 
  10:  * under the terms of the GNU Lesser General Public License as published by 
  11:  * the Free Software Foundation; either version 2.1 of the License, or 
  12:  * (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but 
  15:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 
  16:  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
  17:  * License for more details.
  18:  *
  19:  * You should have received a copy of the GNU Lesser General Public
  20:  * License along with this library; if not, write to the Free Software
  21:  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, 
  22:  * USA.  
  23:  *
  24:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 
  25:  * in the United States and other countries.]
  26:  *
  27:  * -------------------
  28:  * ChartUtilities.java
  29:  * -------------------
  30:  * (C) Copyright 2001-2008, by Object Refinery Limited and Contributors.
  31:  *
  32:  * Original Author:  David Gilbert (for Object Refinery Limited);
  33:  * Contributor(s):   Wolfgang Irler;
  34:  *                   Richard Atkinson;
  35:  *                   Xavier Poinsard;
  36:  *
  37:  * Changes
  38:  * -------
  39:  * 11-Dec-2001 : Version 1.  The JPEG method comes from Wolfgang Irler's 
  40:  *               JFreeChartServletDemo class (DG);
  41:  * 23-Jan-2002 : Changed saveChartAsXXX() methods to pass IOExceptions back to 
  42:  *               caller (DG);
  43:  * 26-Jun-2002 : Added image map methods (DG);
  44:  * 05-Aug-2002 : Added writeBufferedImage methods
  45:  *               Modified writeImageMap method to support flexible image 
  46:  *               maps (RA);
  47:  * 26-Aug-2002 : Added saveChartAsJPEG and writeChartAsJPEG methods with info 
  48:  *               objects (RA);
  49:  * 05-Sep-2002 : Added writeImageMap() method to support OverLIB
  50:  *               - http://www.bosrup.com/web/overlib (RA);
  51:  * 26-Sep-2002 : Fixed errors reported by Checkstyle (DG);
  52:  * 17-Oct-2002 : Exposed JPEG quality setting and PNG compression level as 
  53:  *               parameters (DG);
  54:  * 25-Oct-2002 : Fixed writeChartAsJPEG() empty method bug (DG);
  55:  * 13-Mar-2003 : Updated writeImageMap method as suggested by Xavier Poinsard 
  56:  *               (see Feature Request 688079) (DG);
  57:  * 12-Aug-2003 : Added support for custom image maps using 
  58:  *               ToolTipTagFragmentGenerator and URLTagFragmentGenerator (RA);
  59:  * 02-Sep-2003 : Separated PNG encoding from writing chart to an 
  60:  *               OutputStream (RA);
  61:  * 04-Dec-2003 : Chart draw() method modified to include anchor point (DG);
  62:  * 20-Feb-2004 : Edited Javadocs and added argument checking (DG);
  63:  * 05-Apr-2004 : Fixed problem with buffered image type (DG);
  64:  * 01-Aug-2004 : Modified to use EncoderUtil for all image encoding (RA);
  65:  * 02-Aug-2004 : Delegated image map related functionality to ImageMapUtil (RA);
  66:  * 13-Jan-2005 : Renamed ImageMapUtil --> ImageMapUtilities, removed method
  67:  *               writeImageMap(PrintWriter, String, ChartRenderingInfo) which 
  68:  *               exists in ImageMapUtilities (DG);
  69:  * ------------- JFREECHART 1.0.x ---------------------------------------------
  70:  * 06-Feb-2006 : API doc update (DG);
  71:  * 19-Mar-2007 : Use try-finally to close output stream in saveChartAsXXX() 
  72:  *               methods (DG);
  73:  * 10-Jan-2008 : Fix bug 1868251 - don't create image with transparency when
  74:  *               saving to JPEG format (DG);
  75:  *
  76:  */
  77: 
  78: package org.jfree.chart;
  79: 
  80: import java.awt.Graphics2D;
  81: import java.awt.geom.AffineTransform;
  82: import java.awt.geom.Rectangle2D;
  83: import java.awt.image.BufferedImage;
  84: import java.io.BufferedOutputStream;
  85: import java.io.File;
  86: import java.io.FileOutputStream;
  87: import java.io.IOException;
  88: import java.io.OutputStream;
  89: import java.io.PrintWriter;
  90: 
  91: import org.jfree.chart.encoders.EncoderUtil;
  92: import org.jfree.chart.encoders.ImageFormat;
  93: import org.jfree.chart.imagemap.ImageMapUtilities;
  94: import org.jfree.chart.imagemap.OverLIBToolTipTagFragmentGenerator;
  95: import org.jfree.chart.imagemap.StandardToolTipTagFragmentGenerator;
  96: import org.jfree.chart.imagemap.StandardURLTagFragmentGenerator;
  97: import org.jfree.chart.imagemap.ToolTipTagFragmentGenerator;
  98: import org.jfree.chart.imagemap.URLTagFragmentGenerator;
  99: 
 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:  */
 107: public abstract class ChartUtilities {
 108: 
 109:     /**
 110:      * Writes a chart to an output stream in PNG format.
 111:      *
 112:      * @param out  the output stream (<code>null</code> not permitted).
 113:      * @param chart  the chart (<code>null</code> not permitted).
 114:      * @param width  the image width.
 115:      * @param height  the image height.
 116:      *
 117:      * @throws IOException if there are any I/O errors.
 118:      */
 119:     public static void writeChartAsPNG(OutputStream out, JFreeChart chart,
 120:             int width, int height) throws IOException {
 121: 
 122:         // defer argument checking...
 123:         writeChartAsPNG(out, chart, width, height, null);
 124: 
 125:     }
 126: 
 127:     /**
 128:      * Writes a chart to an output stream in PNG format.
 129:      *
 130:      * @param out  the output stream (<code>null</code> not permitted).
 131:      * @param chart  the chart (<code>null</code> not permitted).
 132:      * @param width  the image width.
 133:      * @param height  the image height.
 134:      * @param encodeAlpha  encode alpha?
 135:      * @param compression  the compression level (0-9).
 136:      *
 137:      * @throws IOException if there are any I/O errors.
 138:      */
 139:     public static void writeChartAsPNG(OutputStream out, JFreeChart chart,
 140:             int width, int height, boolean encodeAlpha, int compression) 
 141:             throws IOException {
 142: 
 143:         // defer argument checking...
 144:         ChartUtilities.writeChartAsPNG(out, chart, width, height, null, 
 145:                 encodeAlpha, compression);
 146: 
 147:     }
 148: 
 149:     /**
 150:      * Writes a chart to an output stream in PNG format.  This method allows 
 151:      * you to pass in a {@link ChartRenderingInfo} object, to collect 
 152:      * information about the chart dimensions/entities.  You will need this 
 153:      * info if you want to create an HTML image map.
 154:      *
 155:      * @param out  the output stream (<code>null</code> not permitted).
 156:      * @param chart  the chart (<code>null</code> not permitted).
 157:      * @param width  the image width.
 158:      * @param height  the image height.
 159:      * @param info  the chart rendering info (<code>null</code> permitted).
 160:      *
 161:      * @throws IOException if there are any I/O errors.
 162:      */
 163:     public static void writeChartAsPNG(OutputStream out, JFreeChart chart,
 164:             int width, int height,  ChartRenderingInfo info) 
 165:             throws IOException {
 166: 
 167:         if (chart == null) {
 168:             throw new IllegalArgumentException("Null 'chart' argument.");
 169:         }
 170:         BufferedImage bufferedImage 
 171:                 = chart.createBufferedImage(width, height, info);
 172:         EncoderUtil.writeBufferedImage(bufferedImage, ImageFormat.PNG, out);
 173:     }
 174: 
 175:     /**
 176:      * Writes a chart to an output stream in PNG format.  This method allows 
 177:      * you to pass in a {@link ChartRenderingInfo} object, to collect 
 178:      * information about the chart dimensions/entities.  You will need this 
 179:      * info if you want to create an HTML image map.
 180:      *
 181:      * @param out  the output stream (<code>null</code> not permitted).
 182:      * @param chart  the chart (<code>null</code> not permitted).
 183:      * @param width  the image width.
 184:      * @param height  the image height.
 185:      * @param info  carries back chart rendering info (<code>null</code> 
 186:      *              permitted).
 187:      * @param encodeAlpha  encode alpha?
 188:      * @param compression  the PNG compression level (0-9).
 189:      *
 190:      * @throws IOException if there are any I/O errors.
 191:      */
 192:     public static void writeChartAsPNG(OutputStream out, JFreeChart chart,
 193:             int width, int height, ChartRenderingInfo info,
 194:             boolean encodeAlpha, int compression) throws IOException {
 195: 
 196:         if (out == null) {
 197:             throw new IllegalArgumentException("Null 'out' argument.");
 198:         }
 199:         if (chart == null) {
 200:             throw new IllegalArgumentException("Null 'chart' argument.");
 201:         }
 202:         BufferedImage chartImage = chart.createBufferedImage(width, height, 
 203:                 BufferedImage.TYPE_INT_ARGB, info);
 204:         ChartUtilities.writeBufferedImageAsPNG(out, chartImage, encodeAlpha, 
 205:                 compression);
 206: 
 207:     }
 208: 
 209:     /**
 210:      * Writes a scaled version of a chart to an output stream in PNG format.
 211:      *
 212:      * @param out  the output stream (<code>null</code> not permitted).
 213:      * @param chart  the chart (<code>null</code> not permitted).
 214:      * @param width  the unscaled chart width.
 215:      * @param height  the unscaled chart height.
 216:      * @param widthScaleFactor  the horizontal scale factor.
 217:      * @param heightScaleFactor  the vertical scale factor.
 218:      *
 219:      * @throws IOException if there are any I/O problems.
 220:      */
 221:     public static void writeScaledChartAsPNG(OutputStream out,
 222:             JFreeChart chart, int width, int height, int widthScaleFactor,
 223:             int heightScaleFactor) throws IOException {
 224: 
 225:         if (out == null) {
 226:             throw new IllegalArgumentException("Null 'out' argument.");
 227:         }
 228:         if (chart == null) {
 229:             throw new IllegalArgumentException("Null 'chart' argument.");
 230:         }
 231: 
 232:         double desiredWidth = width * widthScaleFactor;
 233:         double desiredHeight = height * heightScaleFactor;
 234:         double defaultWidth = width;
 235:         double defaultHeight = height;
 236:         boolean scale = false;
 237: 
 238:         // get desired width and height from somewhere then...
 239:         if ((widthScaleFactor != 1) || (heightScaleFactor != 1)) {
 240:             scale = true;
 241:         }
 242: 
 243:         double scaleX = desiredWidth / defaultWidth;
 244:         double scaleY = desiredHeight / defaultHeight;
 245: 
 246:         BufferedImage image = new BufferedImage((int) desiredWidth, 
 247:                 (int) desiredHeight, BufferedImage.TYPE_INT_ARGB);
 248:         Graphics2D g2 = image.createGraphics();
 249: 
 250:         if (scale) {
 251:             AffineTransform saved = g2.getTransform();
 252:             g2.transform(AffineTransform.getScaleInstance(scaleX, scaleY));
 253:             chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, 
 254:                     defaultHeight), null, null);
 255:             g2.setTransform(saved);
 256:             g2.dispose();
 257:         }
 258:         else {
 259:             chart.draw(g2, new Rectangle2D.Double(0, 0, defaultWidth, 
 260:                     defaultHeight), null, null);
 261:         }
 262:         out.write(encodeAsPNG(image));
 263: 
 264:     }
 265: 
 266:     /**
 267:      * Saves a chart to the specified file in PNG format.
 268:      *
 269:      * @param file  the file name (<code>null</code> not permitted).
 270:      * @param chart  the chart (<code>null</code> not permitted).
 271:      * @param width  the image width.
 272:      * @param height  the image height.
 273:      *
 274:      * @throws IOException if there are any I/O errors.
 275:      */
 276:     public static void saveChartAsPNG(File file, JFreeChart chart,
 277:             int width, int height) throws IOException {
 278: 
 279:         // defer argument checking...
 280:         saveChartAsPNG(file, chart, width, height, null);
 281: 
 282:     }
 283: 
 284:     /**
 285:      * Saves a chart to a file in PNG format.  This method allows you to pass 
 286:      * in a {@link ChartRenderingInfo} object, to collect information about the 
 287:      * chart dimensions/entities.  You will need this info if you want to 
 288:      * create an HTML image map.
 289:      *
 290:      * @param file  the file (<code>null</code> not permitted).
 291:      * @param chart  the chart (<code>null</code> not permitted).
 292:      * @param width  the image width.
 293:      * @param height  the image height.
 294:      * @param info  the chart rendering info (<code>null</code> permitted).
 295:      *
 296:      * @throws IOException if there are any I/O errors.
 297:      */
 298:     public static void saveChartAsPNG(File file, JFreeChart chart,
 299:             int width, int height, ChartRenderingInfo info) 
 300:         throws IOException {
 301: 
 302:         if (file == null) {
 303:             throw new IllegalArgumentException("Null 'file' argument.");
 304:         }
 305:         OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
 306:         try {
 307:             ChartUtilities.writeChartAsPNG(out, chart, width, height, info);
 308:         }
 309:         finally {
 310:             out.close();
 311:         }
 312:     }
 313: 
 314:     /**
 315:      * Saves a chart to a file in PNG format.  This method allows you to pass 
 316:      * in a {@link ChartRenderingInfo} object, to collect information about the 
 317:      * chart dimensions/entities.  You will need this info if you want to 
 318:      * create an HTML image map.
 319:      *
 320:      * @param file  the file (<code>null</code> not permitted).
 321:      * @param chart  the chart (<code>null</code> not permitted).
 322:      * @param width  the image width.
 323:      * @param height  the image height.
 324:      * @param info  the chart rendering info (<code>null</code> permitted).
 325:      * @param encodeAlpha  encode alpha?
 326:      * @param compression  the PNG compression level (0-9).
 327:      *
 328:      * @throws IOException if there are any I/O errors.
 329:      */
 330:     public static void saveChartAsPNG(File file, JFreeChart chart,
 331:            int width, int height, ChartRenderingInfo info, boolean encodeAlpha,
 332:            int compression) throws IOException {
 333: 
 334:         if (file == null) {
 335:             throw new IllegalArgumentException("Null 'file' argument.");
 336:         }
 337:         if (chart == null) {
 338:             throw new IllegalArgumentException("Null 'chart' argument.");
 339:         }
 340: 
 341:         OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
 342:         try {
 343:             writeChartAsPNG(out, chart, width, height, info, encodeAlpha, 
 344:                     compression);
 345:         }
 346:         finally {
 347:             out.close();
 348:         }
 349: 
 350:     }
 351: 
 352:     /**
 353:      * Writes a chart to an output stream in JPEG format.  Please note that
 354:      * JPEG is a poor format for chart images, use PNG if possible.
 355:      * 
 356:      * @param out  the output stream (<code>null</code> not permitted).
 357:      * @param chart  the chart (<code>null</code> not permitted).
 358:      * @param width  the image width.
 359:      * @param height  the image height.
 360:      *
 361:      * @throws IOException if there are any I/O errors.
 362:      */
 363:     public static void writeChartAsJPEG(OutputStream out,
 364:             JFreeChart chart, int width, int height) throws IOException {
 365: 
 366:         // defer argument checking...
 367:         writeChartAsJPEG(out, chart, width, height, null);
 368: 
 369:     }
 370: 
 371:     /**
 372:      * Writes a chart to an output stream in JPEG format.  Please note that
 373:      * JPEG is a poor format for chart images, use PNG if possible.
 374:      *
 375:      * @param out  the output stream (<code>null</code> not permitted).
 376:      * @param quality  the quality setting.
 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, float quality,
 384:             JFreeChart chart, int width, int height) throws IOException {
 385: 
 386:         // defer argument checking...
 387:         ChartUtilities.writeChartAsJPEG(out, quality, chart, width, height, 
 388:                 null);
 389: 
 390:     }
 391: 
 392:     /**
 393:      * Writes a chart to an output stream in JPEG format. This method allows 
 394:      * you to pass in a {@link ChartRenderingInfo} object, to collect 
 395:      * information about the chart dimensions/entities.  You will need this 
 396:      * info if you want to create an HTML image map.
 397:      *
 398:      * @param out  the output stream (<code>null</code> not permitted).
 399:      * @param chart  the chart (<code>null</code> not permitted).
 400:      * @param width  the image width.
 401:      * @param height  the image height.
 402:      * @param info  the chart rendering info (<code>null</code> permitted).
 403:      *
 404:      * @throws IOException if there are any I/O errors.
 405:      */
 406:     public static void writeChartAsJPEG(OutputStream out, JFreeChart chart,
 407:             int width, int height, ChartRenderingInfo info) 
 408:             throws IOException {
 409: 
 410:         if (chart == null) {
 411:             throw new IllegalArgumentException("Null 'chart' argument.");
 412:         }
 413:         BufferedImage image = chart.createBufferedImage(width, height, 
 414:                 BufferedImage.TYPE_INT_RGB, 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, 
 442:                 BufferedImage.TYPE_INT_RGB, info);
 443:         EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, quality);
 444: 
 445:     }
 446: 
 447:     /**
 448:      * Saves a chart to a file in JPEG format.
 449:      *
 450:      * @param file  the file (<code>null</code> not permitted).
 451:      * @param chart  the chart (<code>null</code> not permitted).
 452:      * @param width  the image width.
 453:      * @param height  the image height.
 454:      *
 455:      * @throws IOException if there are any I/O errors.
 456:      */
 457:     public static void saveChartAsJPEG(File file, JFreeChart chart,
 458:             int width, int height) throws IOException {
 459: 
 460:         // defer argument checking...
 461:         saveChartAsJPEG(file, chart, width, height, null);
 462: 
 463:     }
 464: 
 465:     /**
 466:      * Saves a chart to a file in JPEG format.
 467:      *
 468:      * @param file  the file (<code>null</code> not permitted).
 469:      * @param quality  the JPEG quality setting.
 470:      * @param chart  the chart (<code>null</code> not permitted).
 471:      * @param width  the image width.
 472:      * @param height  the image height.
 473:      *
 474:      * @throws IOException if there are any I/O errors.
 475:      */
 476:     public static void saveChartAsJPEG(File file, float quality,
 477:             JFreeChart chart, int width, int height) throws IOException {
 478: 
 479:         // defer argument checking...
 480:         saveChartAsJPEG(file, quality, chart, width, height, null);
 481: 
 482:     }
 483: 
 484:     /**
 485:      * Saves a chart to a file in JPEG format.  This method allows you to pass 
 486:      * in a {@link ChartRenderingInfo} object, to collect information about the 
 487:      * chart dimensions/entities.  You will need this info if you want to 
 488:      * create an HTML image map.
 489:      *
 490:      * @param file  the file name (<code>null</code> not permitted).
 491:      * @param chart  the chart (<code>null</code> not permitted).
 492:      * @param width  the image width.
 493:      * @param height  the image height.
 494:      * @param info  the chart rendering info (<code>null</code> permitted).
 495:      *
 496:      * @throws IOException if there are any I/O errors.
 497:      */
 498:     public static void saveChartAsJPEG(File file, JFreeChart chart,
 499:             int width, int height, ChartRenderingInfo info) throws IOException {
 500: 
 501:         if (file == null) {
 502:             throw new IllegalArgumentException("Null 'file' argument.");
 503:         }
 504:         if (chart == null) {
 505:             throw new IllegalArgumentException("Null 'chart' argument.");
 506:         }
 507:         OutputStream out = new BufferedOutputStream(new FileOutputStream(file));
 508:         try {
 509:             writeChartAsJPEG(out, chart, width, height, info);
 510:         }
 511:         finally {
 512:             out.close();
 513:         }
 514: 
 515:     }
 516: 
 517:     /**
 518:      * Saves a chart to a file in JPEG format.  This method allows you to pass 
 519:      * in a {@link ChartRenderingInfo} object, to collect information about the 
 520:      * chart dimensions/entities.  You will need this info if you want to 
 521:      * create an HTML image map.
 522:      *
 523:      * @param file  the file name (<code>null</code> not permitted).
 524:      * @param quality  the quality setting.
 525:      * @param chart  the chart (<code>null</code> not permitted).
 526:      * @param width  the image width.
 527:      * @param height  the image height.
 528:      * @param info  the chart rendering info (<code>null</code> permitted).
 529:      *
 530:      * @throws IOException if there are any I/O errors.
 531:      */
 532:     public static void saveChartAsJPEG(File file, float quality,
 533:             JFreeChart chart, int width, int height,
 534:             ChartRenderingInfo info) throws IOException {
 535: 
 536:         if (file == null) {
 537:             throw new IllegalArgumentException("Null 'file' argument.");
 538:         }
 539:         if (chart == null) {
 540:             throw new IllegalArgumentException("Null 'chart' argument.");
 541:         }
 542:         
 543:         OutputStream out = new BufferedOutputStream(new FileOutputStream(
 544:                 file));
 545:         try {
 546:             writeChartAsJPEG(out, quality, chart, width, height, info);
 547:         }
 548:         finally {
 549:             out.close();
 550:         }
 551: 
 552:     }
 553: 
 554:     /**
 555:      * Writes a {@link BufferedImage} to an output stream in JPEG format.
 556:      *
 557:      * @param out  the output stream (<code>null</code> not permitted).
 558:      * @param image  the image (<code>null</code> not permitted).
 559:      *
 560:      * @throws IOException if there are any I/O errors.
 561:      */
 562:     public static void writeBufferedImageAsJPEG(OutputStream out, 
 563:             BufferedImage image) throws IOException {
 564: 
 565:         // defer argument checking...
 566:         writeBufferedImageAsJPEG(out, 0.75f, image);
 567: 
 568:     }
 569: 
 570:     /**
 571:      * Writes a {@link BufferedImage} to an output stream in JPEG format.
 572:      *
 573:      * @param out  the output stream (<code>null</code> not permitted).
 574:      * @param quality  the image quality (0.0f to 1.0f).
 575:      * @param image  the image (<code>null</code> not permitted).
 576:      *
 577:      * @throws IOException if there are any I/O errors.
 578:      */
 579:     public static void writeBufferedImageAsJPEG(OutputStream out, float quality,
 580:             BufferedImage image) throws IOException {
 581: 
 582:         EncoderUtil.writeBufferedImage(image, ImageFormat.JPEG, out, quality);
 583: 
 584:     }
 585: 
 586:     /**
 587:      * Writes a {@link BufferedImage} to an output stream in PNG format.
 588:      *
 589:      * @param out  the output stream (<code>null</code> not permitted).
 590:      * @param image  the image (<code>null</code> not permitted).
 591:      *
 592:      * @throws IOException if there are any I/O errors.
 593:      */
 594:     public static void writeBufferedImageAsPNG(OutputStream out, 
 595:             BufferedImage image) throws IOException {
 596: 
 597:         EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out);
 598: 
 599:     }
 600: 
 601:     /**
 602:      * Writes a {@link BufferedImage} to an output stream in PNG format.
 603:      *
 604:      * @param out  the output stream (<code>null</code> not permitted).
 605:      * @param image  the image (<code>null</code> not permitted).
 606:      * @param encodeAlpha  encode alpha?
 607:      * @param compression  the compression level (0-9).
 608:      *
 609:      * @throws IOException if there are any I/O errors.
 610:      */
 611:     public static void writeBufferedImageAsPNG(OutputStream out,
 612:             BufferedImage image, boolean encodeAlpha, int compression) 
 613:             throws IOException {
 614: 
 615:         EncoderUtil.writeBufferedImage(image, ImageFormat.PNG, out, 
 616:                 compression, encodeAlpha);
 617:     }
 618: 
 619:     /**
 620:      * Encodes a {@link BufferedImage} to PNG format.
 621:      *
 622:      * @param image  the image (<code>null</code> not permitted).
 623:      *
 624:      * @return A byte array in PNG format.
 625:      * 
 626:      * @throws IOException if there is an I/O problem.
 627:      */
 628:     public static byte[] encodeAsPNG(BufferedImage image) throws IOException {
 629:         return EncoderUtil.encode(image, ImageFormat.PNG);
 630:     }
 631: 
 632:     /**
 633:      * Encodes a {@link BufferedImage} to PNG format.
 634:      *
 635:      * @param image  the image (<code>null</code> not permitted).
 636:      * @param encodeAlpha  encode alpha?
 637:      * @param compression  the PNG compression level (0-9).
 638:      *
 639:      * @return The byte array in PNG format.
 640:      * 
 641:      * @throws IOException if there is an I/O problem.
 642:      */
 643:     public static byte[] encodeAsPNG(BufferedImage image, boolean encodeAlpha, 
 644:                                      int compression) 
 645:             throws IOException {
 646:         return EncoderUtil.encode(image, ImageFormat.PNG, compression, 
 647:                 encodeAlpha);
 648:     }
 649: 
 650:     /**
 651:      * Writes an image map to an output stream.
 652:      *
 653:      * @param writer  the writer (<code>null</code> not permitted).
 654:      * @param name  the map name (<code>null</code> not permitted).
 655:      * @param info  the chart rendering info (<code>null</code> not permitted).
 656:      * @param useOverLibForToolTips  whether to use OverLIB for tooltips
 657:      *                               (http://www.bosrup.com/web/overlib/).
 658:      *
 659:      * @throws IOException if there are any I/O errors.
 660:      */
 661:     public static void writeImageMap(PrintWriter writer,
 662:                                      String name,
 663:                                      ChartRenderingInfo info,
 664:                                      boolean useOverLibForToolTips) 
 665:         throws IOException {
 666: 
 667:         ToolTipTagFragmentGenerator toolTipTagFragmentGenerator = null;
 668:         if (useOverLibForToolTips) {
 669:             toolTipTagFragmentGenerator 
 670:                     = new OverLIBToolTipTagFragmentGenerator();
 671:         }
 672:         else {
 673:             toolTipTagFragmentGenerator 
 674:                     = new StandardToolTipTagFragmentGenerator();
 675:         }
 676:         ImageMapUtilities.writeImageMap(writer, name, info, 
 677:                 toolTipTagFragmentGenerator, 
 678:                 new StandardURLTagFragmentGenerator());
 679: 
 680:     }
 681: 
 682:     /**
 683:      * Writes an image map to the specified writer.
 684:      *
 685:      * @param writer  the writer (<code>null</code> not permitted).
 686:      * @param name  the map name (<code>null</code> not permitted).
 687:      * @param info  the chart rendering info (<code>null</code> not permitted).
 688:      * @param toolTipTagFragmentGenerator  a generator for the HTML fragment
 689:      *     that will contain the tooltip text (<code>null</code> not permitted 
 690:      *     if <code>info</code> contains tooltip information).
 691:      * @param urlTagFragmentGenerator  a generator for the HTML fragment that
 692:      *     will contain the URL reference (<code>null</code> not permitted if 
 693:      *     <code>info</code> contains URLs).
 694:      *
 695:      * @throws IOException if there are any I/O errors.
 696:      */
 697:     public static void writeImageMap(PrintWriter writer, String name, 
 698:             ChartRenderingInfo info, 
 699:             ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
 700:             URLTagFragmentGenerator urlTagFragmentGenerator) 
 701:             throws IOException {
 702: 
 703:         writer.println(ImageMapUtilities.getImageMap(name, info, 
 704:                 toolTipTagFragmentGenerator, urlTagFragmentGenerator));
 705:     }
 706: 
 707:     /**
 708:      * Creates an HTML image map.  This method maps to 
 709:      * {@link ImageMapUtilities#getImageMap(String, ChartRenderingInfo, 
 710:      * ToolTipTagFragmentGenerator, URLTagFragmentGenerator)}, using default 
 711:      * generators.
 712:      *
 713:      * @param name  the map name (<code>null</code> not permitted).
 714:      * @param info  the chart rendering info (<code>null</code> not permitted).
 715:      *
 716:      * @return The map tag.
 717:      */
 718:     public static String getImageMap(String name, ChartRenderingInfo info) {
 719:         return ImageMapUtilities.getImageMap(name, info,
 720:                 new StandardToolTipTagFragmentGenerator(),
 721:                 new StandardURLTagFragmentGenerator());
 722:     }
 723: 
 724:     /**
 725:      * Creates an HTML image map.  This method maps directly to
 726:      * {@link ImageMapUtilities#getImageMap(String, ChartRenderingInfo, 
 727:      * ToolTipTagFragmentGenerator, URLTagFragmentGenerator)}.
 728:      *
 729:      * @param name  the map name (<code>null</code> not permitted).
 730:      * @param info  the chart rendering info (<code>null</code> not permitted).
 731:      * @param toolTipTagFragmentGenerator  a generator for the HTML fragment
 732:      *     that will contain the tooltip text (<code>null</code> not permitted 
 733:      *     if <code>info</code> contains tooltip information).
 734:      * @param urlTagFragmentGenerator  a generator for the HTML fragment that
 735:      *     will contain the URL reference (<code>null</code> not permitted if 
 736:      *     <code>info</code> contains URLs).
 737:      *
 738:      * @return The map tag.
 739:      */
 740:     public static String getImageMap(String name, ChartRenderingInfo info,
 741:             ToolTipTagFragmentGenerator toolTipTagFragmentGenerator,
 742:             URLTagFragmentGenerator urlTagFragmentGenerator) {
 743: 
 744:         return ImageMapUtilities.getImageMap(name, info, 
 745:                 toolTipTagFragmentGenerator, urlTagFragmentGenerator);
 746:         
 747:     }
 748: 
 749: }