1:
36:
37:
38: package ;
39:
40: import ;
41:
42: import ;
43: import ;
44:
45:
48: public abstract class PackedColorModel extends ColorModel
49: {
50: private int masks[];
51:
52:
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:
94: private void initMasks(int[] colorMaskArray, int alphaMask)
95: {
96: int numComponents = colorMaskArray.length;
97: if (alphaMask == 0)
98: {
99: 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:
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:
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: }