KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > imageio > plugins > common > BogusColorSpace


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

7
8 package com.sun.imageio.plugins.common;
9
10 import java.awt.color.ColorSpace JavaDoc;
11
12 /**
13  * A dummy <code>ColorSpace</code> to enable <code>ColorModel</code>
14  * for image data which do not have an innate color representation.
15  */

16 public class BogusColorSpace extends ColorSpace JavaDoc {
17     /**
18      * Return the type given the number of components.
19      *
20      * @param numComponents The number of components in the
21      * <code>ColorSpace</code>.
22      * @exception IllegalArgumentException if <code>numComponents</code>
23      * is less than 1.
24      */

25     private static int getType(int numComponents) {
26         if(numComponents < 1) {
27             throw new IllegalArgumentException JavaDoc("numComponents < 1!");
28         }
29
30         int type;
31         switch(numComponents) {
32         case 1:
33             type = ColorSpace.TYPE_GRAY;
34             break;
35         default:
36             // Based on the constant definitions TYPE_2CLR=12 through
37
// TYPE_FCLR=25. This will return unknown types for
38
// numComponents > 15.
39
type = numComponents + 10;
40         }
41
42         return type;
43     }
44
45     /**
46      * Constructs a bogus <code>ColorSpace</code>.
47      *
48      * @param numComponents The number of components in the
49      * <code>ColorSpace</code>.
50      * @exception IllegalArgumentException if <code>numComponents</code>
51      * is less than 1.
52      */

53     public BogusColorSpace(int numComponents) {
54         super(getType(numComponents), numComponents);
55     }
56
57     //
58
// The following methods simply copy the input array to the
59
// output array while otherwise attempting to adhere to the
60
// specified behavior of the methods vis-a-vis exceptions.
61
//
62

63     public float[] toRGB(float[] colorvalue) {
64         if(colorvalue.length < getNumComponents()) {
65             throw new ArrayIndexOutOfBoundsException JavaDoc
66                 ("colorvalue.length < getNumComponents()");
67         }
68
69         float[] rgbvalue = new float[3];
70
71         System.arraycopy(colorvalue, 0, rgbvalue, 0,
72                          Math.min(3, getNumComponents()));
73
74         return colorvalue;
75     }
76
77     public float[] fromRGB(float[] rgbvalue) {
78         if(rgbvalue.length < 3) {
79             throw new ArrayIndexOutOfBoundsException JavaDoc
80                 ("rgbvalue.length < 3");
81         }
82
83         float[] colorvalue = new float[getNumComponents()];
84
85         System.arraycopy(rgbvalue, 0, colorvalue, 0,
86                          Math.min(3, colorvalue.length));
87
88         return rgbvalue;
89     }
90
91     public float[] toCIEXYZ(float[] colorvalue) {
92         if(colorvalue.length < getNumComponents()) {
93             throw new ArrayIndexOutOfBoundsException JavaDoc
94                 ("colorvalue.length < getNumComponents()");
95         }
96
97         float[] xyzvalue = new float[3];
98
99         System.arraycopy(colorvalue, 0, xyzvalue, 0,
100                          Math.min(3, getNumComponents()));
101
102         return colorvalue;
103     }
104
105     public float[] fromCIEXYZ(float[] xyzvalue) {
106         if(xyzvalue.length < 3) {
107             throw new ArrayIndexOutOfBoundsException JavaDoc
108                 ("xyzvalue.length < 3");
109         }
110
111         float[] colorvalue = new float[getNumComponents()];
112
113         System.arraycopy(xyzvalue, 0, colorvalue, 0,
114                          Math.min(3, colorvalue.length));
115
116         return xyzvalue;
117     }
118 }
119
Popular Tags