KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > awt > image > ColorModel


1 /*
2  * @(#)ColorModel.java 1.80 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.awt.image;
9
10 import java.awt.Transparency JavaDoc;
11 import java.awt.color.ColorSpace JavaDoc;
12 import java.awt.color.ICC_ColorSpace JavaDoc;
13 import sun.awt.color.ICC_Transform;
14 import sun.awt.color.CMM;
15 import java.awt.Toolkit JavaDoc;
16 import java.util.Collections JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.WeakHashMap JavaDoc;
19
20 /**
21  * The <code>ColorModel</code> abstract class encapsulates the
22  * methods for translating a pixel value to color components
23  * (for example, red, green, and blue) and an alpha component.
24  * In order to render an image to the screen, a printer, or another
25  * image, pixel values must be converted to color and alpha components.
26  * As arguments to or return values from methods of this class,
27  * pixels are represented as 32-bit ints or as arrays of primitive types.
28  * The number, order, and interpretation of color components for a
29  * <code>ColorModel</code> is specified by its <code>ColorSpace</code>.
30  * A <code>ColorModel</code> used with pixel data that does not include
31  * alpha information treats all pixels as opaque, which is an alpha
32  * value of 1.0.
33  * <p>
34  * This <code>ColorModel</code> class supports two representations of
35  * pixel values. A pixel value can be a single 32-bit int or an
36  * array of primitive types. The Java(tm) Platform 1.0 and 1.1 APIs
37  * represented pixels as single <code>byte</code> or single
38  * <code>int</code> values. For purposes of the <code>ColorModel</code>
39  * class, pixel value arguments were passed as ints. The Java(tm) 2
40  * Platform API introduced additional classes for representing images.
41  * With {@link BufferedImage} or {@link RenderedImage}
42  * objects, based on {@link Raster} and {@link SampleModel} classes, pixel
43  * values might not be conveniently representable as a single int.
44  * Consequently, <code>ColorModel</code> now has methods that accept
45  * pixel values represented as arrays of primitive types. The primitive
46  * type used by a particular <code>ColorModel</code> object is called its
47  * transfer type.
48  * <p>
49  * <code>ColorModel</code> objects used with images for which pixel values
50  * are not conveniently representable as a single int throw an
51  * {@link IllegalArgumentException} when methods taking a single int pixel
52  * argument are called. Subclasses of <code>ColorModel</code> must
53  * specify the conditions under which this occurs. This does not
54  * occur with {@link DirectColorModel} or {@link IndexColorModel} objects.
55  * <p>
56  * Currently, the transfer types supported by the Java 2D(tm) API are
57  * DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, DataBuffer.TYPE_INT,
58  * DataBuffer.TYPE_SHORT, DataBuffer.TYPE_FLOAT, and DataBuffer.TYPE_DOUBLE.
59  * Most rendering operations will perform much faster when using ColorModels
60  * and images based on the first three of these types. In addition, some
61  * image filtering operations are not supported for ColorModels and
62  * images based on the latter three types.
63  * The transfer type for a particular <code>ColorModel</code> object is
64  * specified when the object is created, either explicitly or by default.
65  * All subclasses of <code>ColorModel</code> must specify what the
66  * possible transfer types are and how the number of elements in the
67  * primitive arrays representing pixels is determined.
68  * <p>
69  * For <code>BufferedImages</code>, the transfer type of its
70  * <code>Raster</code> and of the <code>Raster</code> object's
71  * <code>SampleModel</code> (available from the
72  * <code>getTransferType</code> methods of these classes) must match that
73  * of the <code>ColorModel</code>. The number of elements in an array
74  * representing a pixel for the <code>Raster</code> and
75  * <code>SampleModel</code> (available from the
76  * <code>getNumDataElements</code> methods of these classes) must match
77  * that of the <code>ColorModel</code>.
78  * <p>
79  * The algorithm used to convert from pixel values to color and alpha
80  * components varies by subclass. For example, there is not necessarily
81  * a one-to-one correspondence between samples obtained from the
82  * <code>SampleModel</code> of a <code>BufferedImage</code> object's
83  * <code>Raster</code> and color/alpha components. Even when
84  * there is such a correspondence, the number of bits in a sample is not
85  * necessarily the same as the number of bits in the corresponding color/alpha
86  * component. Each subclass must specify how the translation from
87  * pixel values to color/alpha components is done.
88  * <p>
89  * Methods in the <code>ColorModel</code> class use two different
90  * representations of color and alpha components - a normalized form
91  * and an unnormalized form. In the normalized form, each component is a
92  * <code>float</code> value between some minimum and maximum values. For
93  * the alpha component, the minimum is 0.0 and the maximum is 1.0. For
94  * color components the minimum and maximum values for each component can
95  * be obtained from the <code>ColorSpace</code> object. These values
96  * will often be 0.0 and 1.0 (e.g. normalized component values for the
97  * default sRGB color space range from 0.0 to 1.0), but some color spaces
98  * have component values with different upper and lower limits. These
99  * limits can be obtained using the <code>getMinValue</code> and
100  * <code>getMaxValue</code> methods of the <code>ColorSpace</code>
101  * class. Normalized color component values are not premultiplied.
102  * All <code>ColorModels</code> must support the normalized form.
103  * <p>
104  * In the unnormalized
105  * form, each component is an unsigned integral value between 0 and
106  * 2<sup>n</sup> - 1, where n is the number of significant bits for a
107  * particular component. If pixel values for a particular
108  * <code>ColorModel</code> represent color samples premultiplied by
109  * the alpha sample, unnormalized color component values are
110  * also premultiplied. The unnormalized form is used only with instances
111  * of <code>ColorModel</code> whose <code>ColorSpace</code> has minimum
112  * component values of 0.0 for all components and maximum values of
113  * 1.0 for all components.
114  * The unnormalized form for color and alpha components can be a convenient
115  * representation for <code>ColorModels</code> whose normalized component
116  * values all lie
117  * between 0.0 and 1.0. In such cases the integral value 0 maps to 0.0 and
118  * the value 2<sup>n</sup> - 1 maps to 1.0. In other cases, such as
119  * when the normalized component values can be either negative or positive,
120  * the unnormalized form is not convenient. Such <code>ColorModel</code>
121  * objects throw an {@link IllegalArgumentException} when methods involving
122  * an unnormalized argument are called. Subclasses of <code>ColorModel</code>
123  * must specify the conditions under which this occurs.
124  *
125  * @see IndexColorModel
126  * @see ComponentColorModel
127  * @see PackedColorModel
128  * @see DirectColorModel
129  * @see java.awt.Image
130  * @see BufferedImage
131  * @see RenderedImage
132  * @see java.awt.color.ColorSpace
133  * @see SampleModel
134  * @see Raster
135  * @see DataBuffer
136  * @version 10 Feb 1997
137  */

138 public abstract class ColorModel implements Transparency JavaDoc{
139     private long pData; // Placeholder for data for native functions
140

141     /**
142      * The total number of bits in the pixel.
143      */

144     protected int pixel_bits;
145     int nBits[];
146     int transparency = Transparency.TRANSLUCENT;
147     boolean supportsAlpha = true;
148     boolean isAlphaPremultiplied = false;
149     int numComponents = -1;
150     int numColorComponents = -1;
151     ColorSpace JavaDoc colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
152     int colorSpaceType = ColorSpace.TYPE_RGB;
153     int maxBits;
154     boolean is_sRGB = true;
155
156     /**
157      * Data type of the array used to represent pixel values.
158      */

159     protected int transferType;
160
161     /**
162      * This is copied from java.awt.Toolkit since we need the library
163      * loaded in java.awt.image also:
164      *
165      * WARNING: This is a temporary workaround for a problem in the
166      * way the AWT loads native libraries. A number of classes in the
167      * AWT package have a native method, initIDs(), which initializes
168      * the JNI field and method ids used in the native portion of
169      * their implementation.
170      *
171      * Since the use and storage of these ids is done by the
172      * implementation libraries, the implementation of these method is
173      * provided by the particular AWT implementations (for example,
174      * "Toolkit"s/Peer), such as Motif, Microsoft Windows, or Tiny. The
175      * problem is that this means that the native libraries must be
176      * loaded by the java.* classes, which do not necessarily know the
177      * names of the libraries to load. A better way of doing this
178      * would be to provide a separate library which defines java.awt.*
179      * initIDs, and exports the relevant symbols out to the
180      * implementation libraries.
181      *
182      * For now, we know it's done by the implementation, and we assume
183      * that the name of the library is "awt". -br.
184      */

185     private static boolean loaded = false;
186     static void loadLibraries() {
187     if (!loaded) {
188         java.security.AccessController.doPrivileged(
189           new sun.security.action.LoadLibraryAction("awt"));
190         loaded = true;
191     }
192     }
193     private static native void initIDs();
194     static {
195     /* ensure that the proper libraries are loaded */
196         loadLibraries();
197     initIDs();
198     }
199     private static ColorModel JavaDoc RGBdefault;
200
201     /**
202      * Returns a <code>DirectColorModel</code> that describes the default
203      * format for integer RGB values used in many of the methods in the
204      * AWT image interfaces for the convenience of the programmer.
205      * The color space is the default {@link ColorSpace}, sRGB.
206      * The format for the RGB values is an integer with 8 bits
207      * each of alpha, red, green, and blue color components ordered
208      * correspondingly from the most significant byte to the least
209      * significant byte, as in: 0xAARRGGBB. Color components are
210      * not premultiplied by the alpha component. This format does not
211      * necessarily represent the native or the most efficient
212      * <code>ColorModel</code> for a particular device or for all images.
213      * It is merely used as a common color model format.
214      * @return a <code>DirectColorModel</code>object describing default
215      * RGB values.
216      */

217     public static ColorModel JavaDoc getRGBdefault() {
218     if (RGBdefault == null) {
219         RGBdefault = new DirectColorModel JavaDoc(32,
220                           0x00ff0000, // Red
221
0x0000ff00, // Green
222
0x000000ff, // Blue
223
0xff000000 // Alpha
224
);
225     }
226     return RGBdefault;
227     }
228
229     /**
230      * Constructs a <code>ColorModel</code> that translates pixels of the
231      * specified number of bits to color/alpha components. The color
232      * space is the default RGB <code>ColorSpace</code>, which is sRGB.
233      * Pixel values are assumed to include alpha information. If color
234      * and alpha information are represented in the pixel value as
235      * separate spatial bands, the color bands are assumed not to be
236      * premultiplied with the alpha value. The transparency type is
237      * java.awt.Transparency.TRANSLUCENT. The transfer type will be the
238      * smallest of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT,
239      * or DataBuffer.TYPE_INT that can hold a single pixel
240      * (or DataBuffer.TYPE_UNDEFINED if bits is greater
241      * than 32). Since this constructor has no information about the
242      * number of bits per color and alpha component, any subclass calling
243      * this constructor should override any method that requires this
244      * information.
245      * @param bits the number of bits of a pixel
246      * @throws IllegalArgumentException if the number
247      * of bits in <code>bits</code> is less than 1
248      */

249     public ColorModel(int bits) {
250     pixel_bits = bits;
251         if (bits < 1) {
252             throw new IllegalArgumentException JavaDoc("Number of bits must be > 0");
253         }
254         numComponents = 4;
255         numColorComponents = 3;
256         maxBits = bits;
257         // REMIND: make sure transferType is set correctly
258
transferType = ColorModel.getDefaultTransferType(bits);
259     }
260
261     /**
262      * Constructs a <code>ColorModel</code> that translates pixel values
263      * to color/alpha components. Color components will be in the
264      * specified <code>ColorSpace</code>. <code>pixel_bits</code> is the
265      * number of bits in the pixel values. The bits array
266      * specifies the number of significant bits per color and alpha component.
267      * Its length should be the number of components in the
268      * <code>ColorSpace</code> if there is no alpha information in the
269      * pixel values, or one more than this number if there is alpha
270      * information. <code>hasAlpha</code> indicates whether or not alpha
271      * information is present. The <code>boolean</code>
272      * <code>isAlphaPremultiplied</code> specifies how to interpret pixel
273      * values in which color and alpha information are represented as
274      * separate spatial bands. If the <code>boolean</code>
275      * is <code>true</code>, color samples are assumed to have been
276      * multiplied by the alpha sample. The <code>transparency</code>
277      * specifies what alpha values can be represented by this color model.
278      * The transfer type is the type of primitive array used to represent
279      * pixel values. Note that the bits array contains the number of
280      * significant bits per color/alpha component after the translation
281      * from pixel values. For example, for an
282      * <code>IndexColorModel</code> with <code>pixel_bits</code> equal to
283      * 16, the bits array might have four elements with each element set
284      * to 8.
285      * @param pixel_bits the number of bits in the pixel values
286      * @param bits array that specifies the number of significant bits
287      * per color and alpha component
288      * @param cspace the specified <code>ColorSpace</code>
289      * @param hasAlpha <code>true</code> if alpha information is present;
290      * <code>false</code> otherwise
291      * @param isAlphaPremultiplied <code>true</code> if color samples are
292      * assumed to be premultiplied by the alpha samples;
293      * <code>false</code> otherwise
294      * @param transparency what alpha values can be represented by this
295      * color model
296      * @param transferType the type of the array used to represent pixel
297      * values
298      * @throws IllegalArgumentException if the length of
299      * the bit array is less than the number of color or alpha
300      * components in this <code>ColorModel</code>, or if the
301      * transparency is not a valid value.
302      * @throws IllegalArgumentException if the sum of the number
303      * of bits in <code>bits</code> is less than 1 or if
304      * any of the elements in <code>bits</code> is less than 0.
305      * @see java.awt.Transparency
306      */

307     protected ColorModel(int pixel_bits, int[] bits, ColorSpace JavaDoc cspace,
308                          boolean hasAlpha,
309                          boolean isAlphaPremultiplied,
310                          int transparency,
311                          int transferType) {
312         colorSpace = cspace;
313         colorSpaceType = cspace.getType();
314         numColorComponents = cspace.getNumComponents();
315         numComponents = numColorComponents + (hasAlpha ? 1 : 0);
316         supportsAlpha = hasAlpha;
317         if (bits.length < numComponents) {
318             throw new IllegalArgumentException JavaDoc("Number of color/alpha "+
319                                                "components should be "+
320                                                numComponents+
321                                                " but length of bits array is "+
322                                                bits.length);
323         }
324
325         // 4186669
326
if (transparency < Transparency.OPAQUE ||
327             transparency > Transparency.TRANSLUCENT)
328         {
329             throw new IllegalArgumentException JavaDoc("Unknown transparency: "+
330                                                transparency);
331         }
332         
333         if (supportsAlpha == false) {
334             this.isAlphaPremultiplied = false;
335             this.transparency = Transparency.OPAQUE;
336         }
337         else {
338             this.isAlphaPremultiplied = isAlphaPremultiplied;
339             this.transparency = transparency;
340         }
341
342         nBits = (int[]) bits.clone();
343         this.pixel_bits = pixel_bits;
344         if (pixel_bits <= 0) {
345             throw new IllegalArgumentException JavaDoc("Number of pixel bits must "+
346                                                "be > 0");
347         }
348         // Check for bits < 0
349
maxBits = 0;
350         for (int i=0; i < bits.length; i++) {
351             // bug 4304697
352
if (bits[i] < 0) {
353                 throw new
354                     IllegalArgumentException JavaDoc("Number of bits must be >= 0");
355             }
356             if (maxBits < bits[i]) {
357                 maxBits = bits[i];
358             }
359         }
360
361         // Make sure that we don't have all 0-bit components
362
if (maxBits == 0) {
363             throw new IllegalArgumentException JavaDoc("There must be at least "+
364                                                "one component with > 0 "+
365                                               "pixel bits.");
366         }
367
368         // Save this since we always need to check if it is the default CS
369
if (cspace != ColorSpace.getInstance(ColorSpace.CS_sRGB)) {
370             is_sRGB = false;
371         }
372
373         // Save the transfer type
374
this.transferType = transferType;
375     }
376
377     /**
378      * Returns whether or not alpha is supported in this
379      * <code>ColorModel</code>.
380      * @return <code>true</code> if alpha is supported in this
381      * <code>ColorModel</code>; <code>false</code> otherwise.
382      */

383     final public boolean hasAlpha() {
384         return supportsAlpha;
385     }
386     
387     /**
388      * Returns whether or not the alpha has been premultiplied in the
389      * pixel values to be translated by this <code>ColorModel</code>.
390      * If the boolean is <code>true</code>, this <code>ColorModel</code>
391      * is to be used to interpret pixel values in which color and alpha
392      * information are represented as separate spatial bands, and color
393      * samples are assumed to have been multiplied by the
394      * alpha sample.
395      * @return <code>true</code> if the alpha values are premultiplied
396      * in the pixel values to be translated by this
397      * <code>ColorModel</code>; <code>false</code> otherwise.
398      */

399     final public boolean isAlphaPremultiplied() {
400         return isAlphaPremultiplied;
401     }
402
403     /**
404      * Returns the transfer type of this <code>ColorModel</code>.
405      * The transfer type is the type of primitive array used to represent
406      * pixel values as arrays.
407      * @return the transfer type.
408      */

409     final public int getTransferType() {
410         return transferType;
411     }
412     
413     /**
414      * Returns the number of bits per pixel described by this
415      * <code>ColorModel</code>.
416      * @return the number of bits per pixel.
417      */

418     public int getPixelSize() {
419     return pixel_bits;
420     }
421
422     /**
423      * Returns the number of bits for the specified color/alpha component.
424      * Color components are indexed in the order specified by the
425      * <code>ColorSpace</code>. Typically, this order reflects the name
426      * of the color space type. For example, for TYPE_RGB, index 0
427      * corresponds to red, index 1 to green, and index 2
428      * to blue. If this <code>ColorModel</code> supports alpha, the alpha
429      * component corresponds to the index following the last color
430      * component.
431      * @param componentIdx the index of the color/alpha component
432      * @return the number of bits for the color/alpha component at the
433      * specified index.
434      * @throws ArrayIndexOutOfBoundsException if <code>componentIdx</code>
435      * is greater than the number of components or
436      * less than zero
437      * @throws NullPointerException if the number of bits array is
438      * <code>null</code>
439      */

440     public int getComponentSize(int componentIdx) {
441         // REMIND:
442
if (nBits == null) {
443             throw new NullPointerException JavaDoc("Number of bits array is null.");
444         }
445         
446         return nBits[componentIdx];
447     }
448     
449     /**
450      * Returns an array of the number of bits per color/alpha component.
451      * The array contains the color components in the order specified by the
452      * <code>ColorSpace</code>, followed by the alpha component, if
453      * present.
454      * @return an array of the number of bits per color/alpha component
455      */

456     public int[] getComponentSize() {
457         if (nBits != null) {
458             return (int[]) nBits.clone();
459         }
460
461         return null;
462     }
463
464     /**
465      * Returns the transparency. Returns either OPAQUE, BITMASK,
466      * or TRANSLUCENT.
467      * @return the transparency of this <code>ColorModel</code>.
468      * @see Transparency#OPAQUE
469      * @see Transparency#BITMASK
470      * @see Transparency#TRANSLUCENT
471      */

472     public int getTransparency() {
473         return transparency;
474     }
475
476     /**
477      * Returns the number of components, including alpha, in this
478      * <code>ColorModel</code>. This is equal to the number of color
479      * components, optionally plus one, if there is an alpha component.
480      * @return the number of components in this <code>ColorModel</code>
481      */

482     public int getNumComponents() {
483         return numComponents;
484     }
485     
486     /**
487      * Returns the number of color components in this
488      * <code>ColorModel</code>.
489      * This is the number of components returned by
490      * {@link ColorSpace#getNumComponents}.
491      * @return the number of color components in this
492      * <code>ColorModel</code>.
493      * @see ColorSpace#getNumComponents
494      */

495     public int getNumColorComponents() {
496         return numColorComponents;
497     }
498
499     /**
500      * Returns the red color component for the specified pixel, scaled
501      * from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
502      * is done if necessary. The pixel value is specified as an int.
503      * An <code>IllegalArgumentException</code> is thrown if pixel
504      * values for this <code>ColorModel</code> are not conveniently
505      * representable as a single int. The returned value is not a
506      * pre-multiplied value. For example, if the
507      * alpha is premultiplied, this method divides it out before returning
508      * the value. If the alpha value is 0, the red value is 0.
509      * @param pixel a specified pixel
510      * @return the value of the red component of the specified pixel.
511      */

512     public abstract int getRed(int pixel);
513
514     /**
515      * Returns the green color component for the specified pixel, scaled
516      * from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
517      * is done if necessary. The pixel value is specified as an int.
518      * An <code>IllegalArgumentException</code> is thrown if pixel
519      * values for this <code>ColorModel</code> are not conveniently
520      * representable as a single int. The returned value is a non
521      * pre-multiplied value. For example, if the alpha is premultiplied,
522      * this method divides it out before returning
523      * the value. If the alpha value is 0, the green value is 0.
524      * @param pixel the specified pixel
525      * @return the value of the green component of the specified pixel.
526      */

527     public abstract int getGreen(int pixel);
528
529     /**
530      * Returns the blue color component for the specified pixel, scaled
531      * from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
532      * is done if necessary. The pixel value is specified as an int.
533      * An <code>IllegalArgumentException</code> is thrown if pixel values
534      * for this <code>ColorModel</code> are not conveniently representable
535      * as a single int. The returned value is a non pre-multiplied
536      * value, for example, if the alpha is premultiplied, this method
537      * divides it out before returning the value. If the alpha value is
538      * 0, the blue value is 0.
539      * @param pixel the specified pixel
540      * @return the value of the blue component of the specified pixel.
541      */

542     public abstract int getBlue(int pixel);
543
544     /**
545      * Returns the alpha component for the specified pixel, scaled
546      * from 0 to 255. The pixel value is specified as an int.
547      * An <code>IllegalArgumentException</code> is thrown if pixel
548      * values for this <code>ColorModel</code> are not conveniently
549      * representable as a single int.
550      * @param pixel the specified pixel
551      * @return the value of alpha component of the specified pixel.
552      */

553     public abstract int getAlpha(int pixel);
554
555     /**
556      * Returns the color/alpha components of the pixel in the default
557      * RGB color model format. A color conversion is done if necessary.
558      * The pixel value is specified as an int.
559      * An <code>IllegalArgumentException</code> thrown if pixel values
560      * for this <code>ColorModel</code> are not conveniently representable
561      * as a single int. The returned value is in a non
562      * pre-multiplied format. For example, if the alpha is premultiplied,
563      * this method divides it out of the color components. If the alpha
564      * value is 0, the color values are 0.
565      * @param pixel the specified pixel
566      * @return the RGB value of the color/alpha components of the
567      * specified pixel.
568      * @see ColorModel#getRGBdefault
569      */

570     public int getRGB(int pixel) {
571     return (getAlpha(pixel) << 24)
572         | (getRed(pixel) << 16)
573         | (getGreen(pixel) << 8)
574         | (getBlue(pixel) << 0);
575     }
576
577     /**
578      * Returns the red color component for the specified pixel, scaled
579      * from 0 to 255 in the default RGB <code>ColorSpace</code>, sRGB. A
580      * color conversion is done if necessary. The pixel value is
581      * specified by an array of data elements of type transferType passed
582      * in as an object reference. The returned value is a non
583      * pre-multiplied value. For example, if alpha is premultiplied,
584      * this method divides it out before returning
585      * the value. If the alpha value is 0, the red value is 0.
586      * If <code>inData</code> is not a primitive array of type
587      * transferType, a <code>ClassCastException</code> is thrown. An
588      * <code>ArrayIndexOutOfBoundsException</code> is thrown if
589      * <code>inData</code> is not large enough to hold a pixel value for
590      * this <code>ColorModel</code>.
591      * If this <code>transferType</code> is not supported, a
592      * <code>UnsupportedOperationException</code> will be
593      * thrown. Since
594      * <code>ColorModel</code> is an abstract class, any instance
595      * must be an instance of a subclass. Subclasses inherit the
596      * implementation of this method and if they don't override it, this
597      * method throws an exception if the subclass uses a
598      * <code>transferType</code> other than
599      * <code>DataBuffer.TYPE_BYTE</code>,
600      * <code>DataBuffer.TYPE_USHORT</code>, or
601      * <code>DataBuffer.TYPE_INT</code>.
602      * @param inData an array of pixel values
603      * @return the value of the red component of the specified pixel.
604      * @throws ClassCastException if <code>inData</code>
605      * is not a primitive array of type <code>transferType</code>
606      * @throws ArrayIndexOutOfBoundsException if
607      * <code>inData</code> is not large enough to hold a pixel value
608      * for this <code>ColorModel</code>
609      * @throws UnsupportedOperationException if this
610      * <code>tranferType</code> is not supported by this
611      * <code>ColorModel</code>
612      */

613     public int getRed(Object JavaDoc inData) {
614         int pixel=0,length=0;
615         switch (transferType) {
616             case DataBuffer.TYPE_BYTE:
617                byte bdata[] = (byte[])inData;
618                pixel = bdata[0] & 0xff;
619                length = bdata.length;
620             break;
621             case DataBuffer.TYPE_USHORT:
622                short sdata[] = (short[])inData;
623                pixel = sdata[0] & 0xffff;
624                length = sdata.length;
625             break;
626             case DataBuffer.TYPE_INT:
627                int idata[] = (int[])inData;
628                pixel = idata[0];
629                length = idata.length;
630             break;
631             default:
632                throw new UnsupportedOperationException JavaDoc("This method has not been "+
633                    "implemented for transferType " + transferType);
634         }
635         if (length == 1) {
636             return getRed(pixel);
637         }
638         else {
639             throw new UnsupportedOperationException JavaDoc
640                 ("This method is not supported by this color model");
641         }
642     }
643
644     /**
645      * Returns the green color component for the specified pixel, scaled
646      * from 0 to 255 in the default RGB <code>ColorSpace</code>, sRGB. A
647      * color conversion is done if necessary. The pixel value is
648      * specified by an array of data elements of type transferType passed
649      * in as an object reference. The returned value will be a non
650      * pre-multiplied value. For example, if the alpha is premultiplied,
651      * this method divides it out before returning the value. If the
652      * alpha value is 0, the green value is 0. If <code>inData</code> is
653      * not a primitive array of type transferType, a
654      * <code>ClassCastException</code> is thrown. An
655      * <code>ArrayIndexOutOfBoundsException</code> is thrown if
656      * <code>inData</code> is not large enough to hold a pixel value for
657      * this <code>ColorModel</code>.
658      * If this <code>transferType</code> is not supported, a
659      * <code>UnsupportedOperationException</code> will be
660      * thrown. Since
661      * <code>ColorModel</code> is an abstract class, any instance
662      * must be an instance of a subclass. Subclasses inherit the
663      * implementation of this method and if they don't override it, this
664      * method throws an exception if the subclass uses a
665      * <code>transferType</code> other than
666      * <code>DataBuffer.TYPE_BYTE</code>,
667      * <code>DataBuffer.TYPE_USHORT</code>, or
668      * <code>DataBuffer.TYPE_INT</code>.
669      * @param inData an array of pixel values
670      * @return the value of the green component of the specified pixel.
671      * @throws <code>ClassCastException</code> if <code>inData</code>
672      * is not a primitive array of type <code>transferType</code>
673      * @throws <code>ArrayIndexOutOfBoundsException</code> if
674      * <code>inData</code> is not large enough to hold a pixel value
675      * for this <code>ColorModel</code>
676      * @throws <code>UnsupportedOperationException</code> if this
677      * <code>tranferType</code> is not supported by this
678      * <code>ColorModel</code>
679      */

680     public int getGreen(Object JavaDoc inData) {
681         int pixel=0,length=0;
682         switch (transferType) {
683             case DataBuffer.TYPE_BYTE:
684                byte bdata[] = (byte[])inData;
685                pixel = bdata[0] & 0xff;
686                length = bdata.length;
687             break;
688             case DataBuffer.TYPE_USHORT:
689                short sdata[] = (short[])inData;
690                pixel = sdata[0] & 0xffff;
691                length = sdata.length;
692             break;
693             case DataBuffer.TYPE_INT:
694                int idata[] = (int[])inData;
695                pixel = idata[0];
696                length = idata.length;
697             break;
698             default:
699                throw new UnsupportedOperationException JavaDoc("This method has not been "+
700                    "implemented for transferType " + transferType);
701         }
702         if (length == 1) {
703             return getGreen(pixel);
704         }
705         else {
706             throw new UnsupportedOperationException JavaDoc
707                 ("This method is not supported by this color model");
708         }
709     }
710     
711     /**
712      * Returns the blue color component for the specified pixel, scaled
713      * from 0 to 255 in the default RGB <code>ColorSpace</code>, sRGB. A
714      * color conversion is done if necessary. The pixel value is
715      * specified by an array of data elements of type transferType passed
716      * in as an object reference. The returned value is a non
717      * pre-multiplied value. For example, if the alpha is premultiplied,
718      * this method divides it out before returning the value. If the
719      * alpha value is 0, the blue value will be 0. If
720      * <code>inData</code> is not a primitive array of type transferType,
721      * a <code>ClassCastException</code> is thrown. An
722      * <code>ArrayIndexOutOfBoundsException</code> is
723      * thrown if <code>inData</code> is not large enough to hold a pixel
724      * value for this <code>ColorModel</code>.
725      * If this <code>transferType</code> is not supported, a
726      * <code>UnsupportedOperationException</code> will be
727      * thrown. Since
728      * <code>ColorModel</code> is an abstract class, any instance
729      * must be an instance of a subclass. Subclasses inherit the
730      * implementation of this method and if they don't override it, this
731      * method throws an exception if the subclass uses a
732      * <code>transferType</code> other than
733      * <code>DataBuffer.TYPE_BYTE</code>,
734      * <code>DataBuffer.TYPE_USHORT</code>, or
735      * <code>DataBuffer.TYPE_INT</code>.
736      * @param inData an array of pixel values
737      * @return the value of the blue component of the specified pixel.
738      * @throws ClassCastException if <code>inData</code>
739      * is not a primitive array of type <code>transferType</code>
740      * @throws ArrayIndexOutOfBoundsException if
741      * <code>inData</code> is not large enough to hold a pixel value
742      * for this <code>ColorModel</code>
743      * @throws UnsupportedOperationException if this
744      * <code>tranferType</code> is not supported by this
745      * <code>ColorModel</code>
746      */

747     public int getBlue(Object JavaDoc inData) {
748         int pixel=0,length=0;
749         switch (transferType) {
750             case DataBuffer.TYPE_BYTE:
751                byte bdata[] = (byte[])inData;
752                pixel = bdata[0] & 0xff;
753                length = bdata.length;
754             break;
755             case DataBuffer.TYPE_USHORT:
756                short sdata[] = (short[])inData;
757                pixel = sdata[0] & 0xffff;
758                length = sdata.length;
759             break;
760             case DataBuffer.TYPE_INT:
761                int idata[] = (int[])inData;
762                pixel = idata[0];
763                length = idata.length;
764             break;
765             default:
766                throw new UnsupportedOperationException JavaDoc("This method has not been "+
767                    "implemented for transferType " + transferType);
768         }
769         if (length == 1) {
770             return getBlue(pixel);
771         }
772         else {
773             throw new UnsupportedOperationException JavaDoc
774                 ("This method is not supported by this color model");
775         }
776     }
777
778     /**
779      * Returns the alpha component for the specified pixel, scaled
780      * from 0 to 255. The pixel value is specified by an array of data
781      * elements of type transferType passed in as an object reference.
782      * If inData is not a primitive array of type transferType, a
783      * <code>ClassCastException</code> is thrown. An
784      * <code>ArrayIndexOutOfBoundsException</code> is thrown if
785      * <code>inData</code> is not large enough to hold a pixel value for
786      * this <code>ColorModel</code>.
787      * If this <code>transferType</code> is not supported, a
788      * <code>UnsupportedOperationException</code> will be
789      * thrown. Since
790      * <code>ColorModel</code> is an abstract class, any instance
791      * must be an instance of a subclass. Subclasses inherit the
792      * implementation of this method and if they don't override it, this
793      * method throws an exception if the subclass uses a
794      * <code>transferType</code> other than
795      * <code>DataBuffer.TYPE_BYTE</code>,
796      * <code>DataBuffer.TYPE_USHORT</code>, or
797      * <code>DataBuffer.TYPE_INT</code>.
798      * @param inData the specified pixel
799      * @return the alpha component of the specified pixel, scaled from
800      * 0 to 255.
801      * @throws ClassCastException if <code>inData</code>
802      * is not a primitive array of type <code>transferType</code>
803      * @throws ArrayIndexOutOfBoundsException if
804      * <code>inData</code> is not large enough to hold a pixel value
805      * for this <code>ColorModel</code>
806      * @throws UnsupportedOperationException if this
807      * <code>tranferType</code> is not supported by this
808      * <code>ColorModel</code>
809      */

810     public int getAlpha(Object JavaDoc inData) {
811         int pixel=0,length=0;
812         switch (transferType) {
813             case DataBuffer.TYPE_BYTE:
814                byte bdata[] = (byte[])inData;
815                pixel = bdata[0] & 0xff;
816                length = bdata.length;
817             break;
818             case DataBuffer.TYPE_USHORT:
819                short sdata[] = (short[])inData;
820                pixel = sdata[0] & 0xffff;
821                length = sdata.length;
822             break;
823             case DataBuffer.TYPE_INT:
824                int idata[] = (int[])inData;
825                pixel = idata[0];
826                length = idata.length;
827             break;
828             default:
829                throw new UnsupportedOperationException JavaDoc("This method has not been "+
830                    "implemented for transferType " + transferType);
831         }
832         if (length == 1) {
833             return getAlpha(pixel);
834         }
835         else {
836             throw new UnsupportedOperationException JavaDoc
837                 ("This method is not supported by this color model");
838         }
839     }
840
841     /**
842      * Returns the color/alpha components for the specified pixel in the
843      * default RGB color model format. A color conversion is done if
844      * necessary. The pixel value is specified by an array of data
845      * elements of type transferType passed in as an object reference.
846      * If inData is not a primitive array of type transferType, a
847      * <code>ClassCastException</code> is thrown. An
848      * <code>ArrayIndexOutOfBoundsException</code> is
849      * thrown if <code>inData</code> is not large enough to hold a pixel
850      * value for this <code>ColorModel</code>.
851      * The returned value will be in a non pre-multiplied format, i.e. if
852      * the alpha is premultiplied, this method will divide it out of the
853      * color components (if the alpha value is 0, the color values will be 0).
854      * @param inData the specified pixel
855      * @return the color and alpha components of the specified pixel.
856      * @see ColorModel#getRGBdefault
857      */

858     public int getRGB(Object JavaDoc inData) {
859         return (getAlpha(inData) << 24)
860             | (getRed(inData) << 16)
861             | (getGreen(inData) << 8)
862             | (getBlue(inData) << 0);
863     }
864
865     /**
866      * Returns a data element array representation of a pixel in this
867      * <code>ColorModel</code>, given an integer pixel representation in
868      * the default RGB color model.
869      * This array can then be passed to the
870      * {@link WritableRaster#setDataElements} method of
871      * a {@link WritableRaster} object. If the pixel variable is
872      * <code>null</code>, a new array will be allocated. If
873      * <code>pixel</code> is not
874      * <code>null</code>, it must be a primitive array of type
875      * <code>transferType</code>; otherwise, a
876      * <code>ClassCastException</code> is thrown. An
877      * <code>ArrayIndexOutOfBoundsException</code> is thrown if
878      * <code>pixel</code> is
879      * not large enough to hold a pixel value for this
880      * <code>ColorModel</code>. The pixel array is returned.
881      * If this <code>transferType</code> is not supported, a
882      * <code>UnsupportedOperationException</code> will be
883      * thrown. Since <code>ColorModel</code> is an abstract class,
884      * any instance is an instance of a subclass. Subclasses must
885      * override this method since the implementation in this abstract
886      * class throws an <code>UnsupportedOperationException</code>.
887      * @param rgb the integer pixel representation in the default RGB
888      * color model
889      * @param pixel the specified pixel
890      * @return an array representation of the specified pixel in this
891      * <code>ColorModel</code>.
892      * @throws ClassCastException if <code>pixel</code>
893      * is not a primitive array of type <code>transferType</code>
894      * @throws ArrayIndexOutOfBoundsException if
895      * <code>pixel</code> is not large enough to hold a pixel value
896      * for this <code>ColorModel</code>
897      * @throws UnsupportedOperationException if this
898      * method is not supported by this <code>ColorModel</code>
899      * @see WritableRaster#setDataElements
900      * @see SampleModel#setDataElements
901      */

902     public Object JavaDoc getDataEle