1 /*
   2  * Copyright (c) 2010, 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.  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 javafx.scene.effect;
  27 
  28 import javafx.beans.property.BooleanProperty;
  29 import javafx.beans.property.IntegerProperty;
  30 import javafx.beans.property.IntegerPropertyBase;
  31 import javafx.beans.property.SimpleBooleanProperty;
  32 
  33 import com.sun.javafx.util.Utils;
  34 
  35 
  36 /**
  37  * A buffer that contains floating point data, intended for use as a parameter
  38  * to effects such as {@link DisplacementMap}.
  39  * @since JavaFX 2.0
  40  */
  41 public class FloatMap {
  42     private com.sun.scenario.effect.FloatMap map;
  43     private float[] buf;
  44     private boolean mapBufferDirty = true;
  45 
  46     com.sun.scenario.effect.FloatMap getImpl() {
  47         return map;
  48     }
  49 
  50     private void updateBuffer() {
  51         if (getWidth() > 0 && getHeight() > 0) {
  52             int w = Utils.clampMax(getWidth(), 4096);
  53             int h = Utils.clampMax(getHeight(), 4096);
  54             int size = w * h * 4;
  55             buf = new float[size];
  56             mapBufferDirty = true;
  57         }
  58     }
  59 
  60     private void impl_update() {
  61         if (mapBufferDirty) {
  62             map = new com.sun.scenario.effect.FloatMap(
  63                     Utils.clamp(1, getWidth(), 4096),
  64                     Utils.clamp(1, getHeight(), 4096));
  65             mapBufferDirty = false;
  66         }
  67         map.put(buf);
  68     }
  69 
  70     void impl_sync() {
  71         if (impl_isEffectDirty()) {
  72             impl_update();
  73             impl_clearDirty();
  74         }
  75     }
  76     private BooleanProperty effectDirty;
  77 
  78 
  79     private void setEffectDirty(boolean value) {
  80         effectDirtyProperty().set(value);
  81     }
  82 
  83     final BooleanProperty effectDirtyProperty() {
  84         if (effectDirty == null) {
  85             effectDirty = new SimpleBooleanProperty(this, "effectDirty");
  86         }
  87         return effectDirty;
  88     }
  89 
  90     /**
  91      * @treatAsPrivate implementation detail
  92      * @deprecated This is an internal API that is not intended for use and will be removed in the next version
  93      */
  94     @Deprecated
  95     boolean impl_isEffectDirty() {
  96         return effectDirty == null ? false : effectDirty.get();
  97     }
  98 
  99     private void impl_markDirty() {
 100         setEffectDirty(true);
 101     }
 102 
 103     private void impl_clearDirty() {
 104         setEffectDirty(false);
 105     }
 106 
 107     /**
 108      * Creates a new instance of FloatMap with default parameters.
 109      */
 110     public FloatMap() {
 111         updateBuffer();
 112         impl_markDirty();
 113     }
 114 
 115     /**
 116      * Creates a new instance of FloatMap with the specified width and height.
 117      * @param width the width of the map, in pixels
 118      * @param height the height of the map, in pixels
 119      * @since JavaFX 2.1
 120      */
 121     public FloatMap(int width, int height) {
 122         setWidth(width);
 123         setHeight(height);
 124         updateBuffer();
 125         impl_markDirty();
 126     }
 127 
 128     /**
 129      * The width of the map, in pixels.
 130      * <pre>
 131      *       Min:    1
 132      *       Max: 4096
 133      *   Default:    1
 134      *  Identity:  n/a
 135      * </pre>
 136      * @defaultValue 1
 137      */
 138     private IntegerProperty width;
 139 
 140 
 141     public final void setWidth(int value) {
 142         widthProperty().set(value);
 143     }
 144 
 145     public final int getWidth() {
 146         return width == null ? 1 : width.get();
 147     }
 148 
 149     public final IntegerProperty widthProperty() {
 150         if (width == null) {
 151             width = new IntegerPropertyBase(1) {
 152 
 153                 @Override
 154                 public void invalidated() {
 155                     updateBuffer();
 156                     impl_markDirty();
 157                 }
 158 
 159                 @Override
 160                 public Object getBean() {
 161                     return FloatMap.this;
 162                 }
 163 
 164                 @Override
 165                 public String getName() {
 166                     return "width";
 167                 }
 168             };
 169         }
 170         return width;
 171     }
 172 
 173     /**
 174      * The height of the map, in pixels.
 175      * <pre>
 176      *       Min:    1
 177      *       Max: 4096
 178      *   Default:    1
 179      *  Identity:  n/a
 180      * </pre>
 181      * @defaultValue 1
 182      */
 183     private IntegerProperty height;
 184 
 185 
 186     public final void setHeight(int value) {
 187         heightProperty().set(value);
 188     }
 189 
 190     public final int getHeight() {
 191         return height == null ? 1 : height.get();
 192     }
 193 
 194     public final IntegerProperty heightProperty() {
 195         if (height == null) {
 196             height = new IntegerPropertyBase(1) {
 197 
 198                 @Override
 199                 public void invalidated() {
 200                     updateBuffer();
 201                     impl_markDirty();
 202                 }
 203 
 204                 @Override
 205                 public Object getBean() {
 206                     return FloatMap.this;
 207                 }
 208 
 209                 @Override
 210                 public String getName() {
 211                     return "height";
 212                 }
 213             };
 214         }
 215         return height;
 216     }
 217 
 218     /**
 219      * Sets the sample for a specific band at the given (x,y) location.
 220      *
 221      * @param x the x location
 222      * @param y the y location
 223      * @param band the band to set (must be 0, 1, 2, or 3)
 224      * @param s the sample value to set
 225      */
 226     public void setSample(int x, int y, int band, float s) {
 227         buf[((x+(y*getWidth()))*4) + band] = s;
 228         impl_markDirty();
 229     }
 230 
 231     /**
 232      * Sets the sample for the first band at the given (x,y) location.
 233      *
 234      * @param x the x location
 235      * @param y the y location
 236      * @param s0 the sample value to set for the first band
 237      */
 238     public void setSamples(int x, int y, float s0)
 239     {
 240         int index = (x+(y*getWidth()))*4;
 241         buf[index + 0] = s0;
 242         impl_markDirty();
 243     }
 244 
 245     /**
 246      * Sets the sample for the first two bands at the given (x,y) location.
 247      *
 248      * @param x the x location
 249      * @param y the y location
 250      * @param s0 the sample value to set for the first band
 251      * @param s1 the sample value to set for the second band
 252      */
 253     public void setSamples(int x, int y, float s0, float s1)
 254     {
 255         int index = (x+(y*getWidth()))*4;
 256         buf[index + 0] = s0;
 257         buf[index + 1] = s1;
 258         impl_markDirty();
 259     }
 260 
 261     /**
 262      * Sets the sample for the first three bands at the given (x,y) location.
 263      *
 264      * @param x the x location
 265      * @param y the y location
 266      * @param s0 the sample value to set for the first band
 267      * @param s1 the sample value to set for the second band
 268      * @param s2 the sample value to set for the third band
 269      */
 270     public void setSamples(int x, int y, float s0, float s1, float s2)
 271     {
 272         int index = (x+(y*getWidth()))*4;
 273         buf[index + 0] = s0;
 274         buf[index + 1] = s1;
 275         buf[index + 2] = s2;
 276         impl_markDirty();
 277     }
 278 
 279     /**
 280      * Sets the sample for each of the four bands at the given (x,y) location.
 281      *
 282      * @param x the x location
 283      * @param y the y location
 284      * @param s0 the sample value to set for the first band
 285      * @param s1 the sample value to set for the second band
 286      * @param s2 the sample value to set for the third band
 287      * @param s3 the sample value to set for the fourth band
 288      */
 289     public void setSamples(int x, int y,
 290                            float s0, float s1, float s2, float s3)
 291     {
 292         int index = (x+(y*getWidth()))*4;
 293         buf[index + 0] = s0;
 294         buf[index + 1] = s1;
 295         buf[index + 2] = s2;
 296         buf[index + 3] = s3;
 297         impl_markDirty();
 298     }
 299     
 300     /**
 301      * @treatAsPrivate implementation detail
 302      * @deprecated This is an internal API that is not intended for use and will be removed in the next version
 303      */
 304     @Deprecated
 305     public FloatMap impl_copy() {
 306         FloatMap dest = new FloatMap(this.getWidth(), this.getHeight());
 307         System.arraycopy(buf, 0, dest.buf, 0, buf.length);
 308         return dest;
 309     }
 310 }