001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2017, 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 * PaintAlpha.java 029 * --------------- 030 * (C) Copyright 2011-2016 by DaveLaw and Contributors. 031 * 032 * Original Author: DaveLaw (dave ATT davelaw D0TT de); 033 * Contributor(s): David Gilbert (for Object Refinery Limited); 034 * 035 * Changes 036 * ------- 037 * 09-Mar-2011 : Written (DaveLaw) 038 * 03-Jul-2012 : JDK 1.6 References made reflective for JDK 1.3 compatibility 039 * (DaveLaw); 040 * 16-Sep-2013 : Removed reflection since we are requiring JDK 1.6 now (DG) 041 * 042 */ 043 044package org.jfree.chart.util; 045 046import java.awt.Color; 047import java.awt.GradientPaint; 048import java.awt.LinearGradientPaint; 049import java.awt.Paint; 050import java.awt.RadialGradientPaint; 051import java.awt.TexturePaint; 052import java.awt.image.BufferedImage; 053import java.awt.image.IndexColorModel; 054import java.awt.image.WritableRaster; 055import java.util.Hashtable; 056 057/** 058 * This class contains static methods for the manipulation 059 * of objects of type {@code Paint} 060 * <p> 061 * The intention is to honour the alpha-channel in the process. 062 * {@code PaintAlpha} was originally conceived to improve the 063 * rendering of 3D Shapes with transparent colours and to allow 064 * invisible bars by making them completely transparent. 065 * <p> 066 * Previously {@link Color#darker()} was used for this, 067 * which always returns an opaque colour. 068 * <p> 069 * Additionally there are methods to control the behaviour and 070 * in particular a {@link PaintAlpha#cloneImage(BufferedImage) cloneImage(..)} 071 * method which is needed to darken objects of type {@link TexturePaint}. 072 * 073 * @author DaveLaw 074 * 075 * @since 1.0.15 076 */ 077public class PaintAlpha { 078 // TODO Revert to SVN revision 2469 in JFreeChart 1.0.16 079 // (MultipleGradientPaint's / JDK issues) 080 // TODO THEN: change visibility of ALL darker(...) Methods EXCEPT 081 // darker(Paint) to private! 082 083 /** 084 * Multiplier for the {@code darker} Methods.<br> 085 * (taken from {@link java.awt.Color}.FACTOR) 086 */ 087 private static final double FACTOR = 0.7; 088 089 private static boolean legacyAlpha = false; 090 091 /** 092 * Per default {@code PaintAlpha} will try to honour alpha-channel 093 * information. In the past this was not the case. 094 * If you wish legacy functionality for your application you can request 095 * this here. 096 * 097 * @param legacyAlpha boolean 098 * 099 * @return the previous setting 100 */ 101 public static boolean setLegacyAlpha(boolean legacyAlpha) { 102 boolean old = PaintAlpha.legacyAlpha; 103 PaintAlpha.legacyAlpha = legacyAlpha; 104 return old; 105 } 106 107 /** 108 * Create a new (if possible, darker) {@code Paint} of the same Type. 109 * If the Type is not supported, the original {@code Paint} is returned. 110 * <p> 111 * @param paint a {@code Paint} implementation 112 * (e.g. {@link Color}, {@link GradientPaint}, {@link TexturePaint},..) 113 * <p> 114 * @return a (usually new, see above) {@code Paint} 115 */ 116 public static Paint darker(Paint paint) { 117 118 if (paint instanceof Color) { 119 return darker((Color) paint); 120 } 121 if (legacyAlpha == true) { 122 /* 123 * Legacy? Just return the original Paint. 124 * (this corresponds EXACTLY to how Paints used to be darkened) 125 */ 126 return paint; 127 } 128 if (paint instanceof GradientPaint) { 129 return darker((GradientPaint) paint); 130 } 131 if (paint instanceof LinearGradientPaint) { 132 return darkerLinearGradientPaint((LinearGradientPaint) paint); 133 } 134 if (paint instanceof RadialGradientPaint) { 135 return darkerRadialGradientPaint((RadialGradientPaint) paint); 136 } 137 if (paint instanceof TexturePaint) { 138 try { 139 return darkerTexturePaint((TexturePaint) paint); 140 } 141 catch (Exception e) { 142 /* 143 * Lots can go wrong while fiddling with Images, Color Models 144 * & such! If anything at all goes awry, just return the original 145 * TexturePaint. (TexturePaint's are immutable anyway, so no harm 146 * done) 147 */ 148 return paint; 149 } 150 } 151 return paint; 152 } 153 154 /** 155 * Similar to {@link Color#darker()}. 156 * <p> 157 * The essential difference is that this method 158 * maintains the alpha-channel unchanged<br> 159 * 160 * @param paint a {@code Color} 161 * 162 * @return a darker version of the {@code Color} 163 */ 164 private static Color darker(Color paint) { 165 return new Color( 166 (int)(paint.getRed () * FACTOR), 167 (int)(paint.getGreen() * FACTOR), 168 (int)(paint.getBlue () * FACTOR), paint.getAlpha()); 169 } 170 171 /** 172 * Create a new {@code GradientPaint} with its colors darkened. 173 * 174 * @param paint the gradient paint ({@code null} not permitted). 175 * 176 * @return a darker version of the {@code GradientPaint} 177 */ 178 private static GradientPaint darker(GradientPaint paint) { 179 return new GradientPaint( 180 paint.getPoint1(), darker(paint.getColor1()), 181 paint.getPoint2(), darker(paint.getColor2()), 182 paint.isCyclic()); 183 } 184 185 /** 186 * Create a new Gradient with its colours darkened. 187 * 188 * @param paint a {@code LinearGradientPaint} 189 * 190 * @return a darker version of the {@code LinearGradientPaint} 191 */ 192 private static Paint darkerLinearGradientPaint(LinearGradientPaint paint) { 193 final Color[] paintColors = paint.getColors(); 194 for (int i = 0; i < paintColors.length; i++) { 195 paintColors[i] = darker(paintColors[i]); 196 } 197 return new LinearGradientPaint(paint.getStartPoint(), 198 paint.getEndPoint(), paint.getFractions(), paintColors, 199 paint.getCycleMethod(), paint.getColorSpace(), 200 paint.getTransform()); 201 } 202 203 /** 204 * Create a new Gradient with its colours darkened. 205 * 206 * @param paint a {@code RadialGradientPaint} 207 * 208 * @return a darker version of the {@code RadialGradientPaint} 209 */ 210 private static Paint darkerRadialGradientPaint(RadialGradientPaint paint) { 211 final Color[] paintColors = paint.getColors(); 212 for (int i = 0; i < paintColors.length; i++) { 213 paintColors[i] = darker(paintColors[i]); 214 } 215 return new RadialGradientPaint(paint.getCenterPoint(), 216 paint.getRadius(), paint.getFocusPoint(), 217 paint.getFractions(), paintColors, paint.getCycleMethod(), 218 paint.getColorSpace(), paint.getTransform()); 219 } 220 221 /** 222 * Create a new {@code TexturePaint} with its colors darkened. 223 * <p> 224 * This entails cloning the underlying {@code BufferedImage}, 225 * then darkening each color-pixel individually! 226 * 227 * @param paint a {@code TexturePaint} 228 * 229 * @return a darker version of the {@code TexturePaint} 230 */ 231 private static TexturePaint darkerTexturePaint(TexturePaint paint) { 232 /** 233 * Color Models with pre-multiplied Alpha tested OK without any 234 * special logic 235 * 236 * BufferedImage.TYPE_INT_ARGB_PRE: // Type 03: tested OK 2011.02.27 237 * BufferedImage.TYPE_4BYTE_ABGR_PRE: // Type 07: tested OK 2011.02.27 238 */ 239 if (paint.getImage().getColorModel().isAlphaPremultiplied()) { 240 /* Placeholder */ 241 } 242 243 BufferedImage img = cloneImage(paint.getImage()); 244 245 WritableRaster ras = img.copyData(null); 246 247 final int miX = ras.getMinX(); 248 final int miY = ras.getMinY(); 249 final int maY = ras.getMinY() + ras.getHeight(); 250 251 final int wid = ras.getWidth(); 252 253 /**/ int[] pix = new int[wid * img.getSampleModel().getNumBands()]; 254 /* (pix-buffer is large enough for all pixels of one row) */ 255 256 /** 257 * Indexed Color Models (sort of a Palette) CANNOT be simply 258 * multiplied (the pixel-value is just an index into the Palette). 259 * 260 * Fortunately, IndexColorModel.getComponents(..) resolves the colors. 261 * The resolved colors can then be multiplied by our FACTOR. 262 * IndexColorModel.getDataElement(..) then tries to map the computed 263 * color to the "nearest" in the Palette. 264 * 265 * It is quite possible that the "nearest" color is the ORIGINAL 266 * color! In the worst case, the returned Image will be identical to 267 * the original. 268 * 269 * Applies to following Image Types: 270 * 271 * BufferedImage.TYPE_BYTE_BINARY: // Type 12: tested OK 2011.02.27 272 * BufferedImage.TYPE_BYTE_INDEXED: // Type 13: tested OK 2011.02.27 273 */ 274 if (img.getColorModel() instanceof IndexColorModel) { 275 276 int[] nco = new int[4]; // RGB (+ optional Alpha which we leave 277 // unchanged) 278 279 for (int y = miY; y < maY; y++) { 280 281 pix = ras.getPixels(miX, y, wid, 1, pix); 282 283 for (int p = 0; p < pix.length; p++) { 284 nco = img.getColorModel().getComponents(pix[p], nco, 0); 285 nco[0] *= FACTOR; // Red 286 nco[1] *= FACTOR; // Green 287 nco[2] *= FACTOR; // Blue. Now map computed colour to 288 // nearest in Palette... 289 pix[p] = img.getColorModel().getDataElement(nco, 0); 290 } 291 /**/ ras.setPixels(miX, y, wid, 1, pix); 292 } 293 img.setData(ras); 294 295 return new TexturePaint(img, paint.getAnchorRect()); 296 } 297 298 /** 299 * For the other 2 Color Models, java.awt.image.ComponentColorModel and 300 * java.awt.image.DirectColorModel, the order of subpixels returned by 301 * ras.getPixels(..) was observed to correspond to the following... 302 */ 303 if (img.getSampleModel().getNumBands() == 4) { 304 /** 305 * The following Image Types have an Alpha-channel which we will 306 * leave unchanged: 307 * 308 * BufferedImage.TYPE_INT_ARGB: // Type 02: tested OK 2011.02.27 309 * BufferedImage.TYPE_4BYTE_ABGR: // Type 06: tested OK 2011.02.27 310 */ 311 for (int y = miY; y < maY; y++) { 312 313 pix = ras.getPixels(miX, y, wid, 1, pix); 314 315 for (int p = 0; p < pix.length;) { 316 pix[p] = (int)(pix[p++] * FACTOR); // Red 317 pix[p] = (int)(pix[p++] * FACTOR); // Green 318 pix[p] = (int)(pix[p++] * FACTOR); // Blue 319 /* Ignore alpha-channel -> */p++; 320 } 321 /**/ ras.setPixels(miX, y, wid, 1, pix); 322 } 323 img.setData(ras); 324 return new TexturePaint(img, paint.getAnchorRect()); 325 } else { 326 for (int y = miY; y < maY; y++) { 327 328 pix = ras.getPixels(miX, y, wid, 1, pix); 329 330 for (int p = 0; p < pix.length; p++) { 331 pix[p] = (int)(pix[p] * FACTOR); 332 } 333 /**/ ras.setPixels(miX, y, wid, 1, pix); 334 } 335 img.setData(ras); 336 return new TexturePaint(img, paint.getAnchorRect()); 337 /** 338 * Above, we multiplied every pixel by our FACTOR because the 339 * applicable Image Types consist only of color or grey channels: 340 * 341 * BufferedImage.TYPE_INT_RGB: // Type 01: tested OK 2011.02.27 342 * BufferedImage.TYPE_INT_BGR: // Type 04: tested OK 2011.02.27 343 * BufferedImage.TYPE_3BYTE_BGR: // Type 05: tested OK 2011.02.27 344 * BufferedImage.TYPE_BYTE_GRAY: // Type 10: tested OK 2011.02.27 345 * BufferedImage.TYPE_USHORT_GRAY: // Type 11: tested OK 2011.02.27 346 * BufferedImage.TYPE_USHORT_565_RGB: // Type 08: tested OK 2011.02.27 347 * BufferedImage.TYPE_USHORT_555_RGB: // Type 09: tested OK 2011.02.27 348 * 349 * Note: as ras.getPixels(..) returned colours in the order R, G, B, A (optional) 350 * for both TYPE_4BYTE_ABGR & TYPE_3BYTE_BGR, 351 * it is assumed that TYPE_INT_BGR will behave similarly. 352 */ 353 } 354 } 355 356 /** 357 * Clone a {@link BufferedImage}. 358 * <p> 359 * Note: when constructing the clone, the original Color Model Object is 360 * reused.<br> That keeps things simple and should not be a problem, as all 361 * known Color Models<br> 362 * ({@link java.awt.image.IndexColorModel IndexColorModel}, 363 * {@link java.awt.image.DirectColorModel DirectColorModel}, 364 * {@link java.awt.image.ComponentColorModel ComponentColorModel}) are 365 * immutable. 366 * 367 * @param image original BufferedImage to clone 368 * 369 * @return a new BufferedImage reusing the original's Color Model and 370 * containing a clone of its pixels 371 */ 372 public static BufferedImage cloneImage(BufferedImage image) { 373 374 WritableRaster rin = image.getRaster(); 375 WritableRaster ras = rin.createCompatibleWritableRaster(); 376 /**/ ras.setRect(rin); // <- this is the code that actually COPIES the pixels 377 378 /* 379 * Buffered Images may have properties, but NEVER disclose them! 380 * Nevertheless, just in case someone implements getPropertyNames() 381 * one day... 382 */ 383 Hashtable props = null; 384 String[] propNames = image.getPropertyNames(); 385 if (propNames != null) { // ALWAYS null 386 props = new Hashtable(); 387 for (int i = 0; i < propNames.length; i++) { 388 props.put(propNames[i], image.getProperty(propNames[i])); 389 } 390 } 391 return new BufferedImage(image.getColorModel(), ras, 392 image.isAlphaPremultiplied(), props); 393 } 394}