< prev index next >

src/java.desktop/share/classes/java/awt/image/AreaAveragingScaleFilter.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2002, 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


  46  * will back off to a simple pixel replication behavior and utilize the
  47  * requestTopDownLeftRightResend() method to refilter the pixels in a
  48  * better way at the end.
  49  * <p>It is meant to be used in conjunction with a FilteredImageSource
  50  * object to produce scaled versions of existing images.  Due to
  51  * implementation dependencies, there may be differences in pixel values
  52  * of an image filtered on different platforms.
  53  *
  54  * @see FilteredImageSource
  55  * @see ReplicateScaleFilter
  56  * @see ImageFilter
  57  *
  58  * @author      Jim Graham
  59  */
  60 public class AreaAveragingScaleFilter extends ReplicateScaleFilter {
  61     private static final ColorModel rgbmodel = ColorModel.getRGBdefault();
  62     private static final int neededHints = (TOPDOWNLEFTRIGHT
  63                                             | COMPLETESCANLINES);
  64 
  65     private boolean passthrough;
  66     private float reds[], greens[], blues[], alphas[];
  67     private int savedy;
  68     private int savedyrem;
  69 
  70     /**
  71      * Constructs an AreaAveragingScaleFilter that scales the pixels from
  72      * its source Image as specified by the width and height parameters.
  73      * @param width the target width to scale the image
  74      * @param height the target height to scale the image
  75      */
  76     public AreaAveragingScaleFilter(int width, int height) {
  77         super(width, height);
  78     }
  79 
  80     /**
  81      * Detect if the data is being delivered with the necessary hints
  82      * to allow the averaging algorithm to do its work.
  83      * <p>
  84      * Note: This method is intended to be called by the
  85      * {@code ImageProducer} of the {@code Image} whose
  86      * pixels are being filtered.  Developers using


 191                 int amtx;
 192                 if (sxrem < dxrem) {
 193                     amtx = sxrem;
 194                 } else {
 195                     amtx = dxrem;
 196                 }
 197                 float mult = ((float) amtx) * amty;
 198                 alphas[dx] += mult * a;
 199                 reds[dx] += mult * r;
 200                 greens[dx] += mult * g;
 201                 blues[dx] += mult * b;
 202                 if ((sxrem -= amtx) == 0) {
 203                     sx++;
 204                 }
 205                 if ((dxrem -= amtx) == 0) {
 206                     dx++;
 207                     dxrem = srcWidth;
 208                 }
 209             }
 210             if ((dyrem -= amty) == 0) {
 211                 int outpix[] = calcRow();
 212                 do {
 213                     consumer.setPixels(0, dy, destWidth, 1,
 214                                        rgbmodel, outpix, 0, destWidth);
 215                     dy++;
 216                 } while ((syrem -= amty) >= amty && amty == srcHeight);
 217             } else {
 218                 syrem -= amty;
 219             }
 220             if (syrem == 0) {
 221                 syrem = destHeight;
 222                 sy++;
 223                 off += scansize;
 224             }
 225         }
 226         savedyrem = dyrem;
 227         savedy = dy;
 228     }
 229 
 230     /**
 231      * Combine the components for the delivered byte pixels into the
 232      * accumulation arrays and send on any averaged data for rows of
 233      * pixels that are complete.  If the correct hints were not
 234      * specified in the setHints call then relay the work to our
 235      * superclass which is capable of scaling pixels regardless of
 236      * the delivery hints.
 237      * <p>
 238      * Note: This method is intended to be called by the
 239      * {@code ImageProducer} of the {@code Image}
 240      * whose pixels are being filtered.  Developers using
 241      * this class to filter pixels from an image should avoid calling
 242      * this method directly since that operation could interfere
 243      * with the filtering operation.
 244      * @see ReplicateScaleFilter
 245      */
 246     public void setPixels(int x, int y, int w, int h,
 247                           ColorModel model, byte pixels[], int off,
 248                           int scansize) {
 249         if (passthrough) {
 250             super.setPixels(x, y, w, h, model, pixels, off, scansize);
 251         } else {
 252             accumPixels(x, y, w, h, model, pixels, off, scansize);
 253         }
 254     }
 255 
 256     /**
 257      * Combine the components for the delivered int pixels into the
 258      * accumulation arrays and send on any averaged data for rows of
 259      * pixels that are complete.  If the correct hints were not
 260      * specified in the setHints call then relay the work to our
 261      * superclass which is capable of scaling pixels regardless of
 262      * the delivery hints.
 263      * <p>
 264      * Note: This method is intended to be called by the
 265      * {@code ImageProducer} of the {@code Image}
 266      * whose pixels are being filtered.  Developers using
 267      * this class to filter pixels from an image should avoid calling
 268      * this method directly since that operation could interfere
 269      * with the filtering operation.
 270      * @see ReplicateScaleFilter
 271      */
 272     public void setPixels(int x, int y, int w, int h,
 273                           ColorModel model, int pixels[], int off,
 274                           int scansize) {
 275         if (passthrough) {
 276             super.setPixels(x, y, w, h, model, pixels, off, scansize);
 277         } else {
 278             accumPixels(x, y, w, h, model, pixels, off, scansize);
 279         }
 280     }
 281 }
   1 /*
   2  * Copyright (c) 1996, 2018, 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


  46  * will back off to a simple pixel replication behavior and utilize the
  47  * requestTopDownLeftRightResend() method to refilter the pixels in a
  48  * better way at the end.
  49  * <p>It is meant to be used in conjunction with a FilteredImageSource
  50  * object to produce scaled versions of existing images.  Due to
  51  * implementation dependencies, there may be differences in pixel values
  52  * of an image filtered on different platforms.
  53  *
  54  * @see FilteredImageSource
  55  * @see ReplicateScaleFilter
  56  * @see ImageFilter
  57  *
  58  * @author      Jim Graham
  59  */
  60 public class AreaAveragingScaleFilter extends ReplicateScaleFilter {
  61     private static final ColorModel rgbmodel = ColorModel.getRGBdefault();
  62     private static final int neededHints = (TOPDOWNLEFTRIGHT
  63                                             | COMPLETESCANLINES);
  64 
  65     private boolean passthrough;
  66     private float[] reds, greens, blues, alphas;
  67     private int savedy;
  68     private int savedyrem;
  69 
  70     /**
  71      * Constructs an AreaAveragingScaleFilter that scales the pixels from
  72      * its source Image as specified by the width and height parameters.
  73      * @param width the target width to scale the image
  74      * @param height the target height to scale the image
  75      */
  76     public AreaAveragingScaleFilter(int width, int height) {
  77         super(width, height);
  78     }
  79 
  80     /**
  81      * Detect if the data is being delivered with the necessary hints
  82      * to allow the averaging algorithm to do its work.
  83      * <p>
  84      * Note: This method is intended to be called by the
  85      * {@code ImageProducer} of the {@code Image} whose
  86      * pixels are being filtered.  Developers using


 191                 int amtx;
 192                 if (sxrem < dxrem) {
 193                     amtx = sxrem;
 194                 } else {
 195                     amtx = dxrem;
 196                 }
 197                 float mult = ((float) amtx) * amty;
 198                 alphas[dx] += mult * a;
 199                 reds[dx] += mult * r;
 200                 greens[dx] += mult * g;
 201                 blues[dx] += mult * b;
 202                 if ((sxrem -= amtx) == 0) {
 203                     sx++;
 204                 }
 205                 if ((dxrem -= amtx) == 0) {
 206                     dx++;
 207                     dxrem = srcWidth;
 208                 }
 209             }
 210             if ((dyrem -= amty) == 0) {
 211                 int[] outpix = calcRow();
 212                 do {
 213                     consumer.setPixels(0, dy, destWidth, 1,
 214                                        rgbmodel, outpix, 0, destWidth);
 215                     dy++;
 216                 } while ((syrem -= amty) >= amty && amty == srcHeight);
 217             } else {
 218                 syrem -= amty;
 219             }
 220             if (syrem == 0) {
 221                 syrem = destHeight;
 222                 sy++;
 223                 off += scansize;
 224             }
 225         }
 226         savedyrem = dyrem;
 227         savedy = dy;
 228     }
 229 
 230     /**
 231      * Combine the components for the delivered byte pixels into the
 232      * accumulation arrays and send on any averaged data for rows of
 233      * pixels that are complete.  If the correct hints were not
 234      * specified in the setHints call then relay the work to our
 235      * superclass which is capable of scaling pixels regardless of
 236      * the delivery hints.
 237      * <p>
 238      * Note: This method is intended to be called by the
 239      * {@code ImageProducer} of the {@code Image}
 240      * whose pixels are being filtered.  Developers using
 241      * this class to filter pixels from an image should avoid calling
 242      * this method directly since that operation could interfere
 243      * with the filtering operation.
 244      * @see ReplicateScaleFilter
 245      */
 246     public void setPixels(int x, int y, int w, int h,
 247                           ColorModel model, byte[] pixels, int off,
 248                           int scansize) {
 249         if (passthrough) {
 250             super.setPixels(x, y, w, h, model, pixels, off, scansize);
 251         } else {
 252             accumPixels(x, y, w, h, model, pixels, off, scansize);
 253         }
 254     }
 255 
 256     /**
 257      * Combine the components for the delivered int pixels into the
 258      * accumulation arrays and send on any averaged data for rows of
 259      * pixels that are complete.  If the correct hints were not
 260      * specified in the setHints call then relay the work to our
 261      * superclass which is capable of scaling pixels regardless of
 262      * the delivery hints.
 263      * <p>
 264      * Note: This method is intended to be called by the
 265      * {@code ImageProducer} of the {@code Image}
 266      * whose pixels are being filtered.  Developers using
 267      * this class to filter pixels from an image should avoid calling
 268      * this method directly since that operation could interfere
 269      * with the filtering operation.
 270      * @see ReplicateScaleFilter
 271      */
 272     public void setPixels(int x, int y, int w, int h,
 273                           ColorModel model, int[] pixels, int off,
 274                           int scansize) {
 275         if (passthrough) {
 276             super.setPixels(x, y, w, h, model, pixels, off, scansize);
 277         } else {
 278             accumPixels(x, y, w, h, model, pixels, off, scansize);
 279         }
 280     }
 281 }
< prev index next >