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.Rectangle;
  29 import com.sun.javafx.geom.transform.BaseTransform;
  30 
  31 /**
  32  * A filter that produces a sepia tone effect, similar to antique photographs.
  33  */
  34 public class SepiaTone extends CoreEffect {
  35 
  36     private float level;
  37 
  38     /**
  39      * Constructs a new {@code SepiaTone} effect with the default
  40      * level value (1.0), using the default input for source data.
  41      * This is a shorthand equivalent to:
  42      * <pre>
  43      *     new SepiaTone(DefaultInput)
  44      * </pre>
  45      */
  46     public SepiaTone() {
  47         this(DefaultInput);
  48     }
  49 
  50     /**
  51      * Constructs a new {@code SepiaTone} effect with the default
  52      * level value (1.0).
  53      *
  54      * @param input the single input {@code Effect}
  55      */
  56     public SepiaTone(Effect input) {
  57         super(input);
  58         setLevel(1f);
  59         updatePeerKey("SepiaTone");
  60     }
  61 
  62     /**
  63      * Returns the input for this {@code Effect}.
  64      *
  65      * @return the input for this {@code Effect}
  66      */
  67     public final Effect getInput() {
  68         return getInputs().get(0);
  69     }
  70 
  71     /**
  72      * Sets the input for this {@code Effect} to a specific
  73      * {@code Effect} or to the default input if {@code input} is
  74      * {@code null}.
  75      *
  76      * @param input the input for this {@code Effect}
  77      */
  78     public void setInput(Effect input) {
  79         setInput(0, input);
  80     }
  81 
  82     /**
  83      * Returns the level value, which controls the intensity of the
  84      * sepia effect.
  85      *
  86      * @return the level value
  87      */
  88     public float getLevel() {
  89         return level;
  90     }
  91 
  92     /**
  93      * Sets the level value, which controls the intensity of the sepia effect.
  94      * <pre>
  95      *       Min: 0.0
  96      *       Max: 1.0
  97      *   Default: 1.0
  98      *  Identity: 0.0
  99      * </pre>
 100      *
 101      * @param level the level value
 102      * @throws IllegalArgumentException if {@code level} is outside
 103      * the allowable range
 104      */
 105     public void setLevel(float level) {
 106         if (level < 0f || level > 1f) {
 107             throw new IllegalArgumentException("Level must be in the range [0,1]");
 108         }
 109         float old = this.level;
 110         this.level = level;
 111     }
 112 
 113     @Override
 114     protected Rectangle getInputClip(int inputIndex,
 115                                      BaseTransform transform,
 116                                      Rectangle outputClip)
 117     {
 118         // Trivially, this effect simply modifies the colors of the pixels
 119         // of the input on a 1:1 basis so the input clip is the same as the
 120         // output clip.
 121         return outputClip;
 122     }
 123 
 124     @Override
 125     public boolean reducesOpaquePixels() {
 126         final Effect input = getInput();
 127         return input != null && input.reducesOpaquePixels();
 128     }
 129 }