1 /*
   2  * Copyright (c) 2008, 2013, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.scenario.effect;
  27 
  28 import com.sun.javafx.geom.BaseBounds;
  29 import com.sun.javafx.geom.DirtyRegionContainer;
  30 import com.sun.javafx.geom.DirtyRegionPool;
  31 import com.sun.javafx.geom.RectBounds;
  32 import com.sun.javafx.geom.Rectangle;
  33 import com.sun.javafx.geom.transform.BaseTransform;
  34 import com.sun.scenario.effect.impl.state.RenderState;
  35 
  36 /**
  37  * An effect that returns a mask that is the inverse of the input (i.e.,
  38  * opaque areas of the input become transparent and vice versa) with a
  39  * given offset and padding.
  40  */
  41 public class InvertMask extends CoreEffect {
  42 
  43     private int pad;
  44     private int xoff;
  45     private int yoff;
  46 
  47     /**
  48      * Constructs a new {@code InvertMask} effect with the default pad (10),
  49      * using the default input for source data.
  50      * This is a shorthand equivalent to:
  51      * <pre>
  52      *     new InvertMask(10, DefaultInput)
  53      * </pre>
  54      */
  55     public InvertMask() {
  56         this(10);
  57     }
  58 
  59     /**
  60      * Constructs a new {@code InvertMask} effect with the default pad (10),
  61      * using the given {@code Effect} as the input.
  62      * This is a shorthand equivalent to:
  63      * <pre>
  64      *     new InvertMask(10, input)
  65      * </pre>
  66      *
  67      * @param input the single input {@code Effect}
  68      */
  69     public InvertMask(Effect input) {
  70         this(10, input);
  71     }
  72 
  73     /**
  74      * Constructs a new {@code InvertMask} effect with the given pad value
  75      * using the default input for source data.
  76      *
  77      * @param pad the amount of padding on each side of the resulting image
  78      * @throws IllegalArgumentException if {@code pad} is negative
  79      */
  80     public InvertMask(int pad) {
  81         this(pad, DefaultInput);
  82     }
  83 
  84     /**
  85      * Constructs a new {@code InvertMask} effect with the given pad value
  86      * and effect input.
  87      *
  88      * @param pad the amount of padding on each side of the resulting image
  89      * @param input the single input {@code Effect}
  90      * @throws IllegalArgumentException if {@code pad} is negative
  91      */
  92     public InvertMask(int pad, Effect input) {
  93         super(input);
  94         setPad(pad);
  95         updatePeerKey("InvertMask");
  96     }
  97 
  98     /**
  99      * Returns the input for this {@code Effect}.
 100      *
 101      * @return the input for this {@code Effect}
 102      */
 103     public final Effect getInput() {
 104         return getInputs().get(0);
 105     }
 106 
 107     /**
 108      * Sets the input for this {@code Effect} to a specific
 109      * {@code Effect} or to the default input if {@code input} is
 110      * {@code null}.
 111      *
 112      * @param input the input for this {@code Effect}
 113      */
 114     public void setInput(Effect input) {
 115         setInput(0, input);
 116     }
 117 
 118     /**
 119      * Returns the amount of padding added to each side of the resulting
 120      * image, in pixels.
 121      *
 122      * @return the amount of padding, in pixels
 123      */
 124     public int getPad() {
 125         return pad;
 126     }
 127 
 128     /**
 129      * Sets the amount of padding added to each side of the resulting
 130      * image, in pixels.
 131      * <pre>
 132      *       Min: 0
 133      *       Max: Integer.MAX_VALUE
 134      *   Default: 0
 135      *  Identity: 0
 136      * </pre>
 137      *
 138      * @param pad the amount of padding, in pixels
 139      * @throws IllegalArgumentException if {@code pad} is negative
 140      */
 141     public void setPad(int pad) {
 142         if (pad < 0) {
 143             throw new IllegalArgumentException("Pad value must be non-negative");
 144         }
 145         int old = this.pad;
 146         this.pad = pad;
 147     }
 148 
 149     /**
 150      * Returns the offset in the x direction, in pixels.
 151      *
 152      * @return the offset in the x direction, in pixels.
 153      */
 154     public int getOffsetX() {
 155         return xoff;
 156     }
 157 
 158     /**
 159      * Sets the offset in the x direction, in pixels.
 160      * <pre>
 161      *       Min: Integer.MIN_VALUE
 162      *       Max: Integer.MAX_VALUE
 163      *   Default: 0
 164      *  Identity: 0
 165      * </pre>
 166      *
 167      * @param xoff the offset in the x direction, in pixels
 168      */
 169     public void setOffsetX(int xoff) {
 170         int old = this.xoff;
 171         this.xoff = xoff;
 172     }
 173 
 174     /**
 175      * Returns the offset in the y direction, in pixels.
 176      *
 177      * @return the offset in the y direction, in pixels.
 178      */
 179     public int getOffsetY() {
 180         return yoff;
 181     }
 182 
 183     /**
 184      * Sets the offset in the y direction, in pixels.
 185      * <pre>
 186      *       Min: Integer.MIN_VALUE
 187      *       Max: Integer.MAX_VALUE
 188      *   Default: 0
 189      *  Identity: 0
 190      * </pre>
 191      *
 192      * @param yoff the offset in the y direction, in pixels
 193      */
 194     public void setOffsetY(int yoff) {
 195         float old = this.yoff;
 196         this.yoff = yoff;
 197     }
 198 
 199     @Override
 200     public BaseBounds getBounds(BaseTransform transform, Effect defaultInput) {
 201         BaseBounds bounds = super.getBounds(BaseTransform.IDENTITY_TRANSFORM, defaultInput);
 202         BaseBounds ret = new RectBounds(bounds.getMinX(), bounds.getMinY(),
 203                 bounds.getMaxX(), bounds.getMaxY());
 204         ((RectBounds) ret).grow(pad, pad);
 205         if (!transform.isIdentity()) {
 206             ret = transformBounds(transform, ret);
 207         }
 208         return ret;
 209     }
 210 
 211     @Override
 212     public Rectangle getResultBounds(BaseTransform transform,
 213                                      Rectangle outputClip,
 214                                      ImageData... inputDatas)
 215     {
 216         Rectangle r = super.getResultBounds(transform, outputClip, inputDatas);
 217         Rectangle ret = new Rectangle(r);
 218         ret.grow(pad, pad);
 219         return ret;
 220     }
 221 
 222     @Override
 223     public RenderState getRenderState(FilterContext fctx,
 224                                       BaseTransform transform,
 225                                       Rectangle outputClip,
 226                                       Object renderHelper,
 227                                       Effect defaultInput)
 228     {
 229         return new RenderState() {
 230             @Override
 231             public EffectCoordinateSpace getEffectTransformSpace() {
 232                 return EffectCoordinateSpace.UserSpace;
 233             }
 234 
 235             @Override
 236             public BaseTransform getInputTransform(BaseTransform filterTransform) {
 237                 return BaseTransform.IDENTITY_TRANSFORM;
 238             }
 239 
 240             @Override
 241             public BaseTransform getResultTransform(BaseTransform filterTransform) {
 242                 return filterTransform;
 243             }
 244 
 245             @Override
 246             public Rectangle getInputClip(int i, Rectangle filterClip) {
 247                 // Typically the mask gets padded by synthetic opaque mask data
 248                 // that is computed from the lack of input pixels in the padded
 249                 // area.  But in the case where a clip has cut down on the
 250                 // amount of data we are generating then the padding for this
 251                 // particular (clipped) operation may not be synthetic, rather it
 252                 // may actually represent inversions of real input pixels.  Thus,
 253                 // the clip for the input needs to make sure it includes any
 254                 // valid input pixels that may appear not just in the output
 255                 // clip, but also in its padded fringe.
 256                 if (filterClip != null) {
 257                     if (pad != 0) {
 258                         filterClip = new Rectangle(filterClip);
 259                         filterClip.grow(pad, pad);
 260                     }
 261                 }
 262                 return filterClip;
 263             }
 264         };
 265     }
 266 
 267     @Override
 268     public boolean reducesOpaquePixels() {
 269         return true;
 270     }
 271 
 272     @Override
 273     public DirtyRegionContainer getDirtyRegions(Effect defaultInput, DirtyRegionPool regionPool) {
 274         Effect di = getDefaultedInput(0, defaultInput);
 275         DirtyRegionContainer drc = di.getDirtyRegions(defaultInput, regionPool);
 276 
 277         if (xoff != 0 || yoff != 0) {
 278             for (int i = 0; i < drc.size(); i++) {
 279                 drc.getDirtyRegion(i).translate(xoff, yoff, 0);
 280             }
 281         }
 282 
 283         return drc;
 284     }
 285 }