1 /*
   2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 import java.awt.Color;
  24 import java.awt.Composite;
  25 import java.awt.CompositeContext;
  26 import java.awt.Graphics2D;
  27 import java.awt.RenderingHints;
  28 import java.awt.geom.AffineTransform;
  29 import java.awt.geom.GeneralPath;
  30 import java.awt.image.BufferedImage;
  31 import java.awt.image.ColorModel;
  32 import java.awt.image.Raster;
  33 import java.awt.image.WritableRaster;
  34 import java.awt.image.RasterFormatException;
  35 
  36 /**
  37  * @test
  38  * @bug 8048782
  39  * @summary Test program that demonstrates PiscesRendering bug in 
  40  * OpenJDK 1.7.0.60 (and probably in all other OpenJDK versions, too).
  41  */
  42 
  43 public class OpenJDKFillBug
  44 {
  45     /**
  46      * Test program that demonstrates a bug in OpenJDK 1.7.0.60 (and
  47      * probably in all other OpenJDK versions, too). To see the bug, simply run
  48      * the 'main' program with OpenJDK. The bug makes the 'g2d.fill'
  49      * method fail with the following exception:
  50      *
  51      * This bug is found in OpenJDK but also is present in OracleJDK 
  52      * if run with 
  53      * -Dsun.java2d.renderer=sun.java2d.pisces.PiscesRenderingEngine
  54      *
  55      * The bug is related to sun.java2d.pisces.PiscesCache constructor
  56      * that accepts '(int minx,int miny,int maxx,int maxy)' arguments:
  57      * the internal 'bboxX1' and 'bboxY1' are set to values one greater
  58      * than given maximum X and Y values. Those maximum values are then
  59      * later used in AAShapePipe' class 'renderTiles' method, where a
  60      * Y/X loop eventually calls 'GeneralCompositePipe' class
  61      * 'renderPathTile' method. In that method, the operation will
  62      * eventually call 'IntegerInterleavedRaster' class
  63      * 'createWritableChild' method with arguments:
  64      *
  65      * <UL>
  66      * <LI>x=800
  67      * <LI>y=0
  68      * <LI>width=2 (this value is too high: should be 1)
  69      * <LI>height=32
  70      * <LI>x0=0
  71      * <LI>y0=0
  72      * <LI>bandList[]=null
  73      * </UL>
  74      *
  75      * This calls for a sub-raster with bounds that fall outside the
  76      * original raster, and therefore the 'createWritableChild' method
  77      * correctly throws 'RasterFormatException'.
  78      *
  79      * The bug is closely related to the use of a custom Composite
  80      * implementation, which are quite rare. The application where this
  81      * bug was first detected implements a high-quality PDF rendering
  82      * engine that needs custom Composite operations to properly
  83      * implement PDF advanced color blending and masking operators.
  84      */
  85 
  86     public static void main(String args[])
  87     {
  88         BufferedImage bi = new BufferedImage(801,1202,
  89                                              BufferedImage.TYPE_INT_ARGB);
  90         Graphics2D g2d = bi.createGraphics();
  91         GeneralPath gp = new GeneralPath();
  92         AffineTransform m = new AffineTransform(2.483489907915543,
  93                                                 0.0,
  94                                                 0.0,
  95                                                 -2.4844977263331955,
  96                                                 0.0,
  97                                                 1202.0);
  98         Composite c = new CustomComposite();
  99 
 100         gp.moveTo(-4.511, -14.349);
 101         gp.lineTo(327.489, -14.349);
 102         gp.lineTo(327.489, 494.15);
 103         gp.lineTo(-4.511, 494.15);
 104         gp.closePath();
 105     
 106         g2d.setRenderingHint(RenderingHints.KEY_ALPHA_INTERPOLATION,
 107                              RenderingHints.VALUE_ALPHA_INTERPOLATION_QUALITY);
 108         g2d.setRenderingHint(RenderingHints.KEY_RENDERING,
 109                              RenderingHints.VALUE_RENDER_QUALITY);
 110         g2d.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING,
 111                              RenderingHints.VALUE_COLOR_RENDER_QUALITY);
 112         g2d.setRenderingHint(RenderingHints.KEY_TEXT_LCD_CONTRAST,
 113                              Integer.valueOf(140));
 114         g2d.setRenderingHint(RenderingHints.KEY_DITHERING,
 115                              RenderingHints.VALUE_DITHER_ENABLE);
 116         g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
 117                              RenderingHints.VALUE_TEXT_ANTIALIAS_DEFAULT);
 118         g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
 119                              RenderingHints.VALUE_ANTIALIAS_ON);
 120         g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL,
 121                              RenderingHints.VALUE_STROKE_NORMALIZE);
 122         g2d.setPaint(Color.red);
 123         g2d.setComposite(c);
 124         g2d.setTransform(m);
 125         try {
 126             g2d.fill(gp);
 127         } catch (RasterFormatException rfe) {
 128             System.out.println("Test failed"); 
 129             throw new RuntimeException("xmax/ymax rounding cause RasterFormatException: " + rfe);
 130         }
 131         g2d.dispose();
 132         System.out.println("Test passed");
 133     }
 134   
 135     // === CustomComposite ===
 136 
 137     /**
 138      * Dummy custom Composite implementation.
 139      */
 140 
 141     public static class CustomComposite implements Composite
 142     {
 143         @Override
 144         public CompositeContext createContext(ColorModel srcColorModel,
 145                                               ColorModel dstColorModel,
 146                                               RenderingHints hints)
 147         {
 148             return new CustomCompositeContext();
 149         }
 150   
 151         // === CustomCompositeContext ===
 152 
 153         /**
 154          * Dummy custom CompositeContext implementation.
 155          */
 156     
 157         public static class CustomCompositeContext implements CompositeContext
 158         {
 159 
 160             @Override
 161             public void dispose()
 162             {
 163                 // NOP
 164             }
 165 
 166             @Override
 167             public void compose(Raster src,Raster dstIn,WritableRaster dstOut)
 168             {
 169                 // NOP
 170             }
 171         }
 172     }
 173 }