1:
37:
38:
39: package ;
40:
41: import ;
42:
43: import ;
44: import ;
45: import ;
46:
47:
52: public class DirectColorModel extends PackedColorModel
53: {
54:
66: public DirectColorModel(int pixelBits, int rmask, int gmask, int bmask)
67: {
68: this(ColorSpace.getInstance(ColorSpace.CS_sRGB), pixelBits,
69: rmask, gmask, bmask, 0,
70: false,
71: Buffers.smallestAppropriateTransferType(pixelBits)
72: );
73: }
74:
75:
88: public DirectColorModel(int pixelBits,
89: int rmask, int gmask, int bmask, int amask)
90: {
91: this(ColorSpace.getInstance(ColorSpace.CS_sRGB), pixelBits,
92: rmask, gmask, bmask, amask,
93: false,
94: Buffers.smallestAppropriateTransferType(pixelBits)
95: );
96: }
97:
98: public DirectColorModel(ColorSpace cspace, int pixelBits,
99: int rmask, int gmask, int bmask, int amask,
100: boolean isAlphaPremultiplied,
101: int transferType)
102: {
103: super(cspace, pixelBits,
104: rmask, gmask, bmask, amask, isAlphaPremultiplied,
105: ((amask == 0) ? Transparency.OPAQUE : Transparency.TRANSLUCENT),
106: transferType);
107: }
108:
109: public final int getRedMask()
110: {
111: return getMask(0);
112: }
113:
114: public final int getGreenMask()
115: {
116: return getMask(1);
117: }
118:
119: public final int getBlueMask()
120: {
121: return getMask(2);
122: }
123:
124: public final int getAlphaMask()
125: {
126: return hasAlpha() ? getMask(3) : 0;
127: }
128:
129:
133: public final int getRed(int pixel)
134: {
135: return extractAndNormalizeSample(pixel, 0);
136: }
137:
138:
142: public final int getGreen(int pixel)
143: {
144: return extractAndNormalizeSample(pixel, 1);
145: }
146:
147:
151: public final int getBlue(int pixel)
152: {
153: return extractAndNormalizeSample(pixel, 2);
154: }
155:
156:
160: public final int getAlpha(int pixel)
161: {
162: if (!hasAlpha())
163: return 255;
164: return extractAndScaleSample(pixel, 3);
165: }
166:
167: private int extractAndNormalizeSample(int pixel, int component)
168: {
169: int value = extractAndScaleSample(pixel, component);
170: if (hasAlpha() && isAlphaPremultiplied() && getAlpha(pixel) != 0)
171: value = value*255/getAlpha(pixel);
172: return value;
173: }
174:
175: private int extractAndScaleSample(int pixel, int component)
176: {
177: int field = pixel & getMask(component);
178: int to8BitShift =
179: 8 - shifts[component] - getComponentSize(component);
180: return (to8BitShift>0) ?
181: (field << to8BitShift) :
182: (field >>> (-to8BitShift));
183: }
184:
185:
192: public final int getRGB(int pixel)
193: {
194:
197: return super.getRGB(pixel);
198: }
199:
200: public int getRed(Object inData)
201: {
202: return getRed(getPixelFromArray(inData));
203: }
204:
205: public int getGreen(Object inData)
206: {
207: return getGreen(getPixelFromArray(inData));
208: }
209:
210: public int getBlue(Object inData)
211: {
212: return getBlue(getPixelFromArray(inData));
213: }
214:
215: public int getAlpha(Object inData)
216: {
217: return getAlpha(getPixelFromArray(inData));
218: }
219:
220: public int getRGB(Object inData)
221: {
222: return getRGB(getPixelFromArray(inData));
223: }
224:
225:
244: public Object getDataElements(int rgb, Object pixel)
245: {
246:
247:
248: int pixelValue = 0;
249: int a = 0;
250: if (hasAlpha()) {
251: a = (rgb >>> 24) & 0xff;
252: pixelValue = valueToField(a, 3, 8);
253: }
254:
255: if (hasAlpha() && isAlphaPremultiplied())
256: {
257: int r, g, b;
258:
267:
270:
271:
272: r = ((rgb >>> 8) & 0xff00)*a/255;
273: g = ((rgb >>> 0) & 0xff00)*a/255;
274: b = ((rgb << 8) & 0xff00)*a/255;
275: pixelValue |=
276: valueToField(r, 0, 16) |
277: valueToField(g, 1, 16) |
278: valueToField(b, 2, 16);
279: }
280: else
281: {
282: int r, g, b;
283:
284: r = (rgb >>> 16) & 0xff;
285: g = (rgb >>> 8) & 0xff;
286: b = (rgb >>> 0) & 0xff;
287:
288: pixelValue |=
289: valueToField(r, 0, 8) |
290: valueToField(g, 1, 8) |
291: valueToField(b, 2, 8);
292: }
293:
294:
296: DataBuffer buffer = Buffers.createBuffer(transferType, pixel, 1);
297: buffer.setElem(0, pixelValue);
298: return Buffers.getData(buffer);
299: }
300:
301:
308: private int valueToField(int val, int component, int highBit)
309: {
310: int toFieldShift =
311: getComponentSize(component) + shifts[component] - highBit;
312: int ret = (toFieldShift>0) ?
313: (val << toFieldShift) :
314: (val >>> (-toFieldShift));
315: return ret & getMask(component);
316: }
317:
318:
322: private int value16ToField(int val, int component)
323: {
324: int toFieldShift = getComponentSize(component) + shifts[component] - 16;
325: return (toFieldShift>0) ?
326: (val << toFieldShift) :
327: (val >>> (-toFieldShift));
328: }
329:
330:
335: public final int[] getComponents(int pixel, int[] components, int offset)
336: {
337: int numComponents = getNumComponents();
338: if (components == null) components = new int[offset + numComponents];
339:
340: for (int b=0; b<numComponents; b++)
341: components[offset++] = (pixel&getMask(b)) >>> shifts[b];
342:
343: return components;
344: }
345:
346: public final int[] getComponents(Object pixel, int[] components,
347: int offset)
348: {
349: return getComponents(getPixelFromArray(pixel), components, offset);
350: }
351:
352:
362: public final WritableRaster createCompatibleWritableRaster(int w, int h)
363: {
364:
365: if(w <= 0 || h <= 0)
366: throw new IllegalArgumentException("width (=" + w + ") and height (="
367: + h + ") must be > 0");
368:
369: SampleModel sm = createCompatibleSampleModel(w, h);
370: Point origin = new Point(0, 0);
371: return Raster.createWritableRaster(sm, origin);
372: }
373:
374: public int getDataElement(int[] components, int offset)
375: {
376: int numComponents = getNumComponents();
377: int pixelValue = 0;
378:
379: for (int c=0; c<numComponents; c++)
380: pixelValue |= (components[offset++] << shifts[c]) & getMask(c);
381:
382: return pixelValue;
383: }
384:
385: public Object getDataElements(int[] components, int offset, Object obj)
386: {
387:
389: int pixelValue = getDataElement(components, offset);
390:
391: DataBuffer buffer = Buffers.createBuffer(transferType, obj, 1);
392: buffer.setElem(0, pixelValue);
393: return Buffers.getData(buffer);
394: }
395:
396: public final ColorModel coerceData (WritableRaster raster,
397: boolean isAlphaPremultiplied)
398: {
399: if (this.isAlphaPremultiplied == isAlphaPremultiplied || !hasAlpha())
400: return this;
401:
402:
405: coerceDataWorker(raster, isAlphaPremultiplied);
406:
407: return new DirectColorModel(cspace, pixel_bits, getRedMask(),
408: getGreenMask(), getBlueMask(), getAlphaMask(),
409: isAlphaPremultiplied, transferType);
410: }
411:
412: public boolean isCompatibleRaster(Raster raster)
413: {
414:
417: return super.isCompatibleRaster(raster);
418: }
419:
420: String stringParam()
421: {
422: return super.stringParam() +
423: ", redMask=" + Integer.toHexString(getRedMask()) +
424: ", greenMask=" + Integer.toHexString(getGreenMask()) +
425: ", blueMask=" + Integer.toHexString(getBlueMask()) +
426: ", alphaMask=" + Integer.toHexString(getAlphaMask());
427: }
428:
429: public String toString()
430: {
431:
433: return super.toString();
434: }
435: }