Source for java.awt.image.PackedColorModel

   1: /* Copyright (C) 2000, 2002, 2004  Free Software Foundation
   2: 
   3: This file is part of GNU Classpath.
   4: 
   5: GNU Classpath is free software; you can redistribute it and/or modify
   6: it under the terms of the GNU General Public License as published by
   7: the Free Software Foundation; either version 2, or (at your option)
   8: any later version.
   9: 
  10: GNU Classpath is distributed in the hope that it will be useful, but
  11: WITHOUT ANY WARRANTY; without even the implied warranty of
  12: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13: General Public License for more details.
  14: 
  15: You should have received a copy of the GNU General Public License
  16: along with GNU Classpath; see the file COPYING.  If not, write to the
  17: Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18: 02110-1301 USA.
  19: 
  20: Linking this library statically or dynamically with other modules is
  21: making a combined work based on this library.  Thus, the terms and
  22: conditions of the GNU General Public License cover the whole
  23: combination.
  24: 
  25: As a special exception, the copyright holders of this library give you
  26: permission to link this library with independent modules to produce an
  27: executable, regardless of the license terms of these independent
  28: modules, and to copy and distribute the resulting executable under
  29: terms of your choice, provided that you also meet, for each linked
  30: independent module, the terms and conditions of the license of that
  31: module.  An independent module is a module which is not derived from
  32: or based on this library.  If you modify this library, you may extend
  33: this exception to your version of the library, but you are not
  34: obligated to do so.  If you do not wish to do so, delete this
  35: exception statement from your version. */
  36: 
  37: 
  38: package java.awt.image;
  39: 
  40: import gnu.java.awt.BitMaskExtent;
  41: 
  42: import java.awt.Point;
  43: import java.awt.color.ColorSpace;
  44: 
  45: /**
  46:  * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
  47:  */
  48: public abstract class PackedColorModel extends ColorModel
  49: {
  50:   private int masks[];
  51:   
  52:   /* Package accessibility, the DirectColorModel needs this array */
  53:   int shifts[];
  54: 
  55:   public PackedColorModel(ColorSpace cspace, int pixelBits,
  56:               int[] colorMaskArray, int alphaMask,
  57:               boolean isAlphaPremultiplied,
  58:               int transparency,
  59:               int transferType)
  60:   {
  61:     super(pixelBits, calcBitsPerComponent(colorMaskArray, alphaMask),
  62:       cspace, (alphaMask != 0), isAlphaPremultiplied, transparency,
  63:       transferType);
  64:     initMasks(colorMaskArray, alphaMask);
  65:     if ((pixelBits<1) || (pixelBits>32)) {
  66:       throw new IllegalArgumentException("pixels per bits must be " +
  67:                      "in the range [1, 32]");
  68:     }
  69:   }
  70:     
  71:   private static int[] calcBitsPerComponent(int[] colorMaskArray,
  72:                         int alphaMask)
  73:   {
  74:     int numComponents = colorMaskArray.length;
  75:     if (alphaMask != 0) numComponents++;
  76:     
  77:     int[] bitsPerComponent = new int[numComponents];
  78:     
  79:     BitMaskExtent extent = new BitMaskExtent();
  80:     for (int b=0; b<colorMaskArray.length; b++)
  81:       {
  82:     extent.setMask(colorMaskArray[b]);
  83:     bitsPerComponent[b] = extent.bitWidth;
  84:       }
  85:     if (alphaMask != 0)
  86:       {
  87:     extent.setMask(alphaMask);
  88:     bitsPerComponent[numComponents-1] = extent.bitWidth;
  89:       }
  90:     return bitsPerComponent;
  91:   }
  92: 
  93:   /** Initializes the masks.
  94:    *
  95:    * @return an array containing the number of bits per color
  96:    * component.
  97:    */
  98:   private void initMasks(int[] colorMaskArray, int alphaMask)
  99:   {
 100:     int numComponents = colorMaskArray.length;
 101:     if (alphaMask == 0)
 102:       {
 103:     masks = colorMaskArray;
 104:       }
 105:     else
 106:       {
 107:     masks = new int[numComponents+1];
 108:     System.arraycopy(colorMaskArray, 0,
 109:              masks, 0,
 110:              numComponents);
 111:     masks[numComponents++] = alphaMask;
 112:       }
 113:     
 114:     shifts = new int[numComponents];
 115:     
 116:     // Bit field handling have been moved to a utility class
 117:     BitMaskExtent extent = new BitMaskExtent();
 118:     for (int b=0; b<numComponents; b++)
 119:       {
 120:     extent.setMask(masks[b]);
 121:     shifts[b] = extent.leastSignificantBit;
 122:       }
 123:   }
 124:     
 125:   public PackedColorModel(ColorSpace cspace, int pixelBits,
 126:               int rmask, int gmask, int bmask,
 127:               int amask, boolean isAlphaPremultiplied,
 128:               int transparency,
 129:               int transferType)
 130:   {
 131:     this(cspace, pixelBits, makeColorMaskArray(rmask, gmask, bmask),
 132:      amask, isAlphaPremultiplied, transparency, transferType);
 133:   }
 134:     
 135:   /* TODO: If there is a alpha mask, it is inefficient to create a
 136:      color mask array that will be discarded when the alpha mask is
 137:      appended. We should probably create a private constructor that
 138:      takes a complete array of masks (color+alpha) as an
 139:      argument. */
 140: 
 141:   private static int[] makeColorMaskArray(int rmask, int gmask, int bmask)
 142:   {
 143:     int[] colorMaskArray = { rmask, gmask, bmask };
 144:     return colorMaskArray;
 145:   }   
 146: 
 147:   public final int getMask(int index)
 148:   {
 149:     return masks[index];
 150:   }
 151:   
 152:   public final int[] getMasks()
 153:   {
 154:     return masks;
 155:   }
 156: 
 157:   public SampleModel createCompatibleSampleModel(int w, int h)
 158:   {
 159:     return new SinglePixelPackedSampleModel(transferType, w, h, masks);
 160:   }
 161:     
 162:   public boolean isCompatibleSampleModel(SampleModel sm)
 163:   {
 164:     if (!super.isCompatibleSampleModel(sm)) return false;
 165:     if (!(sm instanceof SinglePixelPackedSampleModel)) return false;
 166:     
 167:     SinglePixelPackedSampleModel sppsm =
 168:       (SinglePixelPackedSampleModel) sm;
 169:     return java.util.Arrays.equals(sppsm.getBitMasks(), masks);
 170:   }
 171: 
 172:   public WritableRaster getAlphaRaster(WritableRaster raster) {
 173:     if (!hasAlpha()) return null;
 174:     
 175:     SampleModel sm = raster.getSampleModel();
 176:     int[] alphaBand = { sm.getNumBands() - 1 };
 177:     SampleModel alphaModel = sm.createSubsetSampleModel(alphaBand);
 178:     DataBuffer buffer = raster.getDataBuffer();
 179:     Point origin = new Point(0, 0);
 180:     return Raster.createWritableRaster(alphaModel, buffer, origin);
 181:   }
 182:     
 183:   public boolean equals(Object obj)
 184:   {
 185:     if (!super.equals(obj)) return false;
 186:     if (!(obj instanceof PackedColorModel)) return false;
 187:     
 188:     PackedColorModel other = (PackedColorModel) obj;
 189:     
 190:     return java.util.Arrays.equals(masks, other.masks);
 191:   }
 192: }