1 /* Copyright (C) 2000, 2002, 2004 Free Software Foundation
3 This file is part of GNU Classpath.
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)
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.
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
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
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. */
38 package java
.awt
.image
;
40 import gnu
.java
.awt
.BitMaskExtent
;
42 import java
.awt
.Point
;
43 import java
.awt
.color
.ColorSpace
;
46 * @author Rolf W. Rasmussen (rolfwr@ii.uib.no)
48 public abstract class PackedColorModel
extends ColorModel
52 /* Package accessibility, the DirectColorModel needs this array */
55 public PackedColorModel(ColorSpace cspace
, int pixelBits
,
56 int[] colorMaskArray
, int alphaMask
,
57 boolean isAlphaPremultiplied
,
61 super(pixelBits
, calcBitsPerComponent(colorMaskArray
, alphaMask
),
62 cspace
, (alphaMask
!= 0), isAlphaPremultiplied
, transparency
,
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]");
71 private static int[] calcBitsPerComponent(int[] colorMaskArray
,
74 int numComponents
= colorMaskArray
.length
;
75 if (alphaMask
!= 0) numComponents
++;
77 int[] bitsPerComponent
= new int[numComponents
];
79 BitMaskExtent extent
= new BitMaskExtent();
80 for (int b
=0; b
<colorMaskArray
.length
; b
++)
82 extent
.setMask(colorMaskArray
[b
]);
83 bitsPerComponent
[b
] = extent
.bitWidth
;
87 extent
.setMask(alphaMask
);
88 bitsPerComponent
[numComponents
-1] = extent
.bitWidth
;
90 return bitsPerComponent
;
93 /** Initializes the masks. */
94 private void initMasks(int[] colorMaskArray
, int alphaMask
)
96 int numComponents
= colorMaskArray
.length
;
99 masks
= colorMaskArray
;
103 masks
= new int[numComponents
+1];
104 System
.arraycopy(colorMaskArray
, 0,
107 masks
[numComponents
++] = alphaMask
;
110 shifts
= new int[numComponents
];
112 // Bit field handling have been moved to a utility class
113 BitMaskExtent extent
= new BitMaskExtent();
114 for (int b
=0; b
<numComponents
; b
++)
116 extent
.setMask(masks
[b
]);
117 shifts
[b
] = extent
.leastSignificantBit
;
121 public PackedColorModel(ColorSpace cspace
, int pixelBits
,
122 int rmask
, int gmask
, int bmask
,
123 int amask
, boolean isAlphaPremultiplied
,
127 this(cspace
, pixelBits
, makeColorMaskArray(rmask
, gmask
, bmask
),
128 amask
, isAlphaPremultiplied
, transparency
, transferType
);
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
137 private static int[] makeColorMaskArray(int rmask
, int gmask
, int bmask
)
139 int[] colorMaskArray
= { rmask
, gmask
, bmask
};
140 return colorMaskArray
;
143 public final int getMask(int index
)
148 public final int[] getMasks()
153 public SampleModel
createCompatibleSampleModel(int w
, int h
)
155 return new SinglePixelPackedSampleModel(transferType
, w
, h
, masks
);
158 public boolean isCompatibleSampleModel(SampleModel sm
)
160 if (!super.isCompatibleSampleModel(sm
)) return false;
161 if (!(sm
instanceof SinglePixelPackedSampleModel
)) return false;
163 SinglePixelPackedSampleModel sppsm
=
164 (SinglePixelPackedSampleModel
) sm
;
165 return java
.util
.Arrays
.equals(sppsm
.getBitMasks(), masks
);
168 public WritableRaster
getAlphaRaster(WritableRaster raster
) {
169 if (!hasAlpha()) return null;
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
);
179 public boolean equals(Object obj
)
181 if (!super.equals(obj
)) return false;
182 if (!(obj
instanceof PackedColorModel
)) return false;
184 PackedColorModel other
= (PackedColorModel
) obj
;
186 return java
.util
.Arrays
.equals(masks
, other
.masks
);