001 /* Copyright (C) 2000, 2002, 2004 Free Software Foundation
002
003 This file is part of GNU Classpath.
004
005 GNU Classpath is free software; you can redistribute it and/or modify
006 it under the terms of the GNU General Public License as published by
007 the Free Software Foundation; either version 2, or (at your option)
008 any later version.
009
010 GNU Classpath is distributed in the hope that it will be useful, but
011 WITHOUT ANY WARRANTY; without even the implied warranty of
012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013 General Public License for more details.
014
015 You should have received a copy of the GNU General Public License
016 along with GNU Classpath; see the file COPYING. If not, write to the
017 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
018 02110-1301 USA.
019
020 Linking this library statically or dynamically with other modules is
021 making a combined work based on this library. Thus, the terms and
022 conditions of the GNU General Public License cover the whole
023 combination.
024
025 As a special exception, the copyright holders of this library give you
026 permission to link this library with independent modules to produce an
027 executable, regardless of the license terms of these independent
028 modules, and to copy and distribute the resulting executable under
029 terms of your choice, provided that you also meet, for each linked
030 independent module, the terms and conditions of the license of that
031 module. An independent module is a module which is not derived from
032 or based on this library. If you modify this library, you may extend
033 this exception to your version of the library, but you are not
034 obligated to do so. If you do not wish to do so, delete this
035 exception statement from your version. */
036
037
038 package java.awt.image;
039
040 import gnu.java.awt.BitMaskExtent;
041
042 import java.awt.Point;
043 import java.awt.color.ColorSpace;
044
045 /**
046 * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
047 */
048 public abstract class PackedColorModel extends ColorModel
049 {
050 private int masks[];
051
052 /* Package accessibility, the DirectColorModel needs this array */
053 int shifts[];
054
055 public PackedColorModel(ColorSpace cspace, int pixelBits,
056 int[] colorMaskArray, int alphaMask,
057 boolean isAlphaPremultiplied,
058 int transparency,
059 int transferType)
060 {
061 super(pixelBits, calcBitsPerComponent(colorMaskArray, alphaMask),
062 cspace, (alphaMask != 0), isAlphaPremultiplied, transparency,
063 transferType);
064 initMasks(colorMaskArray, alphaMask);
065 if ((pixelBits<1) || (pixelBits>32)) {
066 throw new IllegalArgumentException("pixels per bits must be " +
067 "in the range [1, 32]");
068 }
069 }
070
071 private static int[] calcBitsPerComponent(int[] colorMaskArray,
072 int alphaMask)
073 {
074 int numComponents = colorMaskArray.length;
075 if (alphaMask != 0) numComponents++;
076
077 int[] bitsPerComponent = new int[numComponents];
078
079 BitMaskExtent extent = new BitMaskExtent();
080 for (int b=0; b<colorMaskArray.length; b++)
081 {
082 extent.setMask(colorMaskArray[b]);
083 bitsPerComponent[b] = extent.bitWidth;
084 }
085 if (alphaMask != 0)
086 {
087 extent.setMask(alphaMask);
088 bitsPerComponent[numComponents-1] = extent.bitWidth;
089 }
090 return bitsPerComponent;
091 }
092
093 /** Initializes the masks. */
094 private void initMasks(int[] colorMaskArray, int alphaMask)
095 {
096 int numComponents = colorMaskArray.length;
097 if (alphaMask == 0)
098 {
099 masks = colorMaskArray;
100 }
101 else
102 {
103 masks = new int[numComponents+1];
104 System.arraycopy(colorMaskArray, 0,
105 masks, 0,
106 numComponents);
107 masks[numComponents++] = alphaMask;
108 }
109
110 shifts = new int[numComponents];
111
112 // Bit field handling have been moved to a utility class
113 BitMaskExtent extent = new BitMaskExtent();
114 for (int b=0; b<numComponents; b++)
115 {
116 extent.setMask(masks[b]);
117 shifts[b] = extent.leastSignificantBit;
118 }
119 }
120
121 public PackedColorModel(ColorSpace cspace, int pixelBits,
122 int rmask, int gmask, int bmask,
123 int amask, boolean isAlphaPremultiplied,
124 int transparency,
125 int transferType)
126 {
127 this(cspace, pixelBits, makeColorMaskArray(rmask, gmask, bmask),
128 amask, isAlphaPremultiplied, transparency, transferType);
129 }
130
131 /* TODO: If there is a alpha mask, it is inefficient to create a
132 color mask array that will be discarded when the alpha mask is
133 appended. We should probably create a private constructor that
134 takes a complete array of masks (color+alpha) as an
135 argument. */
136
137 private static int[] makeColorMaskArray(int rmask, int gmask, int bmask)
138 {
139 int[] colorMaskArray = { rmask, gmask, bmask };
140 return colorMaskArray;
141 }
142
143 public final int getMask(int index)
144 {
145 return masks[index];
146 }
147
148 public final int[] getMasks()
149 {
150 return masks;
151 }
152
153 public SampleModel createCompatibleSampleModel(int w, int h)
154 {
155 return new SinglePixelPackedSampleModel(transferType, w, h, masks);
156 }
157
158 public boolean isCompatibleSampleModel(SampleModel sm)
159 {
160 if (!super.isCompatibleSampleModel(sm)) return false;
161 if (!(sm instanceof SinglePixelPackedSampleModel)) return false;
162
163 SinglePixelPackedSampleModel sppsm =
164 (SinglePixelPackedSampleModel) sm;
165 return java.util.Arrays.equals(sppsm.getBitMasks(), masks);
166 }
167
168 public WritableRaster getAlphaRaster(WritableRaster raster) {
169 if (!hasAlpha()) return null;
170
171 SampleModel sm = raster.getSampleModel();
172 int[] alphaBand = { sm.getNumBands() - 1 };
173 SampleModel alphaModel = sm.createSubsetSampleModel(alphaBand);
174 DataBuffer buffer = raster.getDataBuffer();
175 Point origin = new Point(0, 0);
176 return Raster.createWritableRaster(alphaModel, buffer, origin);
177 }
178
179 public boolean equals(Object obj)
180 {
181 if (!super.equals(obj)) return false;
182 if (!(obj instanceof PackedColorModel)) return false;
183
184 PackedColorModel other = (PackedColorModel) obj;
185
186 return java.util.Arrays.equals(masks, other.masks);
187 }
188 }