Source for java.awt.image.ReplicateScaleFilter

   1: /* ReplicateScaleFilter.java -- Java class for filtering images
   2:    Copyright (C) 1999 Free Software Foundation, Inc.
   3: 
   4: This file is part of GNU Classpath.
   5: 
   6: GNU Classpath is free software; you can redistribute it and/or modify
   7: it under the terms of the GNU General Public License as published by
   8: the Free Software Foundation; either version 2, or (at your option)
   9: any later version.
  10: 
  11: GNU Classpath is distributed in the hope that it will be useful, but
  12: WITHOUT ANY WARRANTY; without even the implied warranty of
  13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14: General Public License for more details.
  15: 
  16: You should have received a copy of the GNU General Public License
  17: along with GNU Classpath; see the file COPYING.  If not, write to the
  18: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19: 02110-1301 USA.
  20: 
  21: Linking this library statically or dynamically with other modules is
  22: making a combined work based on this library.  Thus, the terms and
  23: conditions of the GNU General Public License cover the whole
  24: combination.
  25: 
  26: As a special exception, the copyright holders of this library give you
  27: permission to link this library with independent modules to produce an
  28: executable, regardless of the license terms of these independent
  29: modules, and to copy and distribute the resulting executable under
  30: terms of your choice, provided that you also meet, for each linked
  31: independent module, the terms and conditions of the license of that
  32: module.  An independent module is a module which is not derived from
  33: or based on this library.  If you modify this library, you may extend
  34: this exception to your version of the library, but you are not
  35: obligated to do so.  If you do not wish to do so, delete this
  36: exception statement from your version. */
  37: 
  38: 
  39: package java.awt.image;
  40: 
  41: import java.util.Hashtable;
  42: 
  43: /**
  44:  * This filter should be used for fast scaling of images where the result
  45:  * does not need to ensure straight lines are still straight, etc.  The
  46:  * exact method is not defined by Sun but some sort of fast Box filter should
  47:  * probably be correct.
  48:  * <br>
  49:  * Currently this filter does nothing and needs to be implemented.
  50:  *
  51:  * @author C. Brian Jones (cbj@gnu.org) 
  52:  */
  53: public class ReplicateScaleFilter extends ImageFilter
  54: {
  55:     public ReplicateScaleFilter(int width, int height) {
  56:     destHeight = height;
  57:     destWidth = width;
  58:     }
  59: 
  60:     /**
  61:      * The height of the destination image.
  62:      */
  63:     protected int destHeight;
  64: 
  65:     /**
  66:      * The width of the destination image.
  67:      */
  68:     protected int destWidth;
  69: 
  70:     /**
  71:      * The height of the source image.
  72:      */
  73:     protected int srcHeight;
  74: 
  75:     /**
  76:      * The width of the source image.
  77:      */
  78:     protected int srcWidth;
  79: 
  80:     /**
  81:      *
  82:      */
  83:     protected int srcrows[];
  84: 
  85:     /**
  86:      *
  87:      */
  88:     protected int srccols[];
  89: 
  90:     /**
  91:      *
  92:      */
  93:     protected Object outpixbuf;
  94: 
  95:     /**
  96:      * An <code>ImageProducer</code> indicates the size of the image
  97:      * being produced using this method.  A filter can override this 
  98:      * method to intercept these calls from the producer in order to
  99:      * change either the width or the height before in turn calling
 100:      * the consumer's <code>setDimensions</code> method.
 101:      * 
 102:      * @param width the width of the image
 103:      * @param height the height of the image 
 104:      */
 105:     public void setDimensions(int width, int height)
 106:     {
 107:     srcWidth = width;
 108:     srcHeight = height;
 109: 
 110:     /* If either destHeight or destWidth is < 0, the image should
 111:        maintain its original aspect ratio.  When both are < 0,
 112:        just maintain the original width and height. */
 113:     if (destWidth < 0 && destHeight < 0)
 114:         {
 115:         destWidth = width;
 116:         destHeight = height;
 117:     }
 118:     else if (destWidth < 0)
 119:     {
 120:         destWidth = (int) (width * ((double) destHeight / srcHeight));
 121:     }
 122:     else if (destHeight < 0)
 123:     {
 124:         destHeight = (int) (height * ((double) destWidth / srcWidth));
 125:     }
 126: 
 127:     if (consumer != null)
 128:       consumer.setDimensions(destWidth, destHeight);
 129:     }
 130: 
 131:     /**
 132:      * An <code>ImageProducer</code> can set a list of properties
 133:      * associated with this image by using this method.
 134:      *
 135:      * @param props the list of properties associated with this image 
 136:      */
 137:     public void setProperties(Hashtable props)
 138:     {
 139:     props.put("filters", "ReplicateScaleFilter");
 140:     if (consumer != null)
 141:       consumer.setProperties(props);
 142:     }
 143: 
 144:     /**
 145:      * This function delivers a rectangle of pixels where any
 146:      * pixel(m,n) is stored in the array as a <code>byte</code> at
 147:      * index (n * scansize + m + offset).  
 148:      *
 149:      * @param x the x coordinate of the rectangle
 150:      * @param y the y coordinate of the rectangle
 151:      * @param w the width of the rectangle
 152:      * @param h the height of the rectangle
 153:      * @param model the <code>ColorModel</code> used to translate the pixels
 154:      * @param pixels the array of pixel values
 155:      * @param offset the index of the first pixels in the <code>pixels</code> array
 156:      * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
 157:      */
 158:     public void setPixels(int x, int y, int w, int h, 
 159:        ColorModel model, byte[] pixels, int offset, int scansize)
 160:     {
 161:     double rx = ((double) srcWidth) / destWidth;
 162:     double ry = ((double) srcHeight) / destHeight;
 163: 
 164:     int destScansize = (int) Math.round(scansize / rx);
 165: 
 166:     byte[] destPixels = replicatePixels(x, y, w, h,
 167:                                            model, pixels, offset, scansize,
 168:                                        rx, ry, destScansize);
 169: 
 170:     if (consumer != null)
 171:       consumer.setPixels((int) Math.floor(x/rx), (int) Math.floor(y/ry),
 172:                  (int) Math.ceil(w/rx), (int) Math.ceil(h/ry),
 173:                  model, destPixels, 0, destScansize);
 174:     }
 175: 
 176:     /**
 177:      * This function delivers a rectangle of pixels where any
 178:      * pixel(m,n) is stored in the array as an <code>int</code> at
 179:      * index (n * scansize + m + offset).  
 180:      *
 181:      * @param x the x coordinate of the rectangle
 182:      * @param y the y coordinate of the rectangle
 183:      * @param w the width of the rectangle
 184:      * @param h the height of the rectangle
 185:      * @param model the <code>ColorModel</code> used to translate the pixels
 186:      * @param pixels the array of pixel values
 187:      * @param offset the index of the first pixels in the <code>pixels</code> array
 188:      * @param scansize the width to use in extracting pixels from the <code>pixels</code> array
 189:      */
 190:     public void setPixels(int x, int y, int w, int h, 
 191:            ColorModel model, int[] pixels, int offset, int scansize)
 192:     {
 193:     double rx = ((double) srcWidth) / destWidth;
 194:     double ry = ((double) srcHeight) / destHeight;
 195: 
 196:     int destScansize = (int) Math.round(scansize / rx);
 197: 
 198:     int[] destPixels = replicatePixels(x, y, w, h,
 199:                                            model, pixels, offset, scansize,
 200:                                        rx, ry, destScansize);
 201: 
 202:     if (consumer != null)
 203:       consumer.setPixels((int) Math.floor(x/rx), (int) Math.floor(y/ry),
 204:                  (int) Math.ceil(w/rx), (int) Math.ceil(h/ry),
 205:                  model, destPixels, 0, destScansize);
 206:     }
 207: 
 208:     private byte[] replicatePixels(int srcx, int srcy, int srcw, int srch,
 209:                                    ColorModel model, byte[] srcPixels,
 210:                                    int srcOffset, int srcScansize,
 211:                                    double rx, double ry, int destScansize)
 212:     {
 213:     byte[] destPixels =
 214:       new byte[(int) Math.ceil(srcw/rx) * (int) Math.ceil(srch/ry)];
 215: 
 216:     int a, b;
 217:     for (int i = 0; i < destPixels.length; i++)
 218:     {
 219:         a = (int) ((int) ( ((double) i) / destScansize) * ry) * srcScansize;
 220:         b = (int) ((i % destScansize) * rx);
 221:         if ((a + b + srcOffset) < srcPixels.length)
 222:         destPixels[i] = srcPixels[a + b + srcOffset];
 223:     }
 224: 
 225:     return destPixels;
 226:     }
 227: 
 228:     private int[] replicatePixels(int srcx, int srcy, int srcw, int srch,
 229:                                   ColorModel model, int[] srcPixels,
 230:                                   int srcOffset, int srcScansize,
 231:                                   double rx, double ry, int destScansize)
 232:     {
 233:     int[] destPixels =
 234:       new int[(int) Math.ceil(srcw/rx) * (int) Math.ceil(srch/ry)];
 235: 
 236:     int a, b;
 237:     for (int i = 0; i < destPixels.length; i++)
 238:     {
 239:         a = (int) ((int) ( ((double) i) / destScansize) * ry) * srcScansize;
 240:         b = (int) ((i % destScansize) * rx);
 241:         if ((a + b + srcOffset) < srcPixels.length)
 242:         destPixels[i] = srcPixels[a + b + srcOffset];
 243:     }
 244: 
 245:     return destPixels;
 246:     }
 247: }