001/*
002 * $RCSfile: BogusColorSpace.java,v $
003 *
004 * 
005 * Copyright (c) 2005 Sun Microsystems, Inc. All  Rights Reserved.
006 * 
007 * Redistribution and use in source and binary forms, with or without
008 * modification, are permitted provided that the following conditions
009 * are met: 
010 * 
011 * - Redistribution of source code must retain the above copyright 
012 *   notice, this  list of conditions and the following disclaimer.
013 * 
014 * - Redistribution in binary form must reproduce the above copyright
015 *   notice, this list of conditions and the following disclaimer in 
016 *   the documentation and/or other materials provided with the
017 *   distribution.
018 * 
019 * Neither the name of Sun Microsystems, Inc. or the names of 
020 * contributors may be used to endorse or promote products derived 
021 * from this software without specific prior written permission.
022 * 
023 * This software is provided "AS IS," without a warranty of any 
024 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 
025 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 
026 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
027 * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL 
028 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF 
029 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
030 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR 
031 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
032 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
033 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
034 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
035 * POSSIBILITY OF SUCH DAMAGES. 
036 * 
037 * You acknowledge that this software is not designed or intended for 
038 * use in the design, construction, operation or maintenance of any 
039 * nuclear facility. 
040 *
041 * $Revision: 1.1 $
042 * $Date: 2005/02/11 05:01:22 $
043 * $State: Exp $
044 */
045package com.github.jaiimageio.impl.common;
046
047import java.awt.color.ColorSpace;
048
049/**
050 * A dummy <code>ColorSpace</code> to enable <code>ColorModel</code>
051 * for image data which do not have an innate color representation.
052 */
053public class BogusColorSpace extends ColorSpace {
054    /**
055     * Return the type given the number of components.
056     *
057     * @param numComponents The number of components in the
058     * <code>ColorSpace</code>.
059     * @exception IllegalArgumentException if <code>numComponents</code>
060     * is less than 1.
061     */
062    private static int getType(int numComponents) {
063        if(numComponents < 1) {
064            throw new IllegalArgumentException("numComponents < 1!");
065        }
066
067        int type;
068        switch(numComponents) {
069        case 1:
070            type = ColorSpace.TYPE_GRAY;
071            break;
072        default:
073            // Based on the constant definitions TYPE_2CLR=12 through
074            // TYPE_FCLR=25. This will return unknown types for
075            // numComponents > 15.
076            type = numComponents + 10;
077        }
078
079        return type;
080    }
081
082    /**
083     * Constructs a bogus <code>ColorSpace</code>.
084     *
085     * @param numComponents The number of components in the
086     * <code>ColorSpace</code>.
087     * @exception IllegalArgumentException if <code>numComponents</code>
088     * is less than 1.
089     */
090    public BogusColorSpace(int numComponents) {
091        super(getType(numComponents), numComponents);
092    }
093
094    //
095    // The following methods simply copy the input array to the
096    // output array while otherwise attempting to adhere to the
097    // specified behavior of the methods vis-a-vis exceptions.
098    //
099
100    public float[] toRGB(float[] colorvalue) {
101        if(colorvalue.length < getNumComponents()) {
102            throw new ArrayIndexOutOfBoundsException
103                ("colorvalue.length < getNumComponents()");
104        }
105
106        float[] rgbvalue = new float[3];
107
108        System.arraycopy(colorvalue, 0, rgbvalue, 0,
109                         Math.min(3, getNumComponents()));
110
111        return colorvalue;
112    }
113
114    public float[] fromRGB(float[] rgbvalue) {
115        if(rgbvalue.length < 3) {
116            throw new ArrayIndexOutOfBoundsException
117                ("rgbvalue.length < 3");
118        }
119
120        float[] colorvalue = new float[getNumComponents()];
121
122        System.arraycopy(rgbvalue, 0, colorvalue, 0,
123                         Math.min(3, colorvalue.length));
124
125        return rgbvalue;
126    }
127
128    public float[] toCIEXYZ(float[] colorvalue) {
129        if(colorvalue.length < getNumComponents()) {
130            throw new ArrayIndexOutOfBoundsException
131                ("colorvalue.length < getNumComponents()");
132        }
133
134        float[] xyzvalue = new float[3];
135
136        System.arraycopy(colorvalue, 0, xyzvalue, 0,
137                         Math.min(3, getNumComponents()));
138
139        return colorvalue;
140    }
141
142    public float[] fromCIEXYZ(float[] xyzvalue) {
143        if(xyzvalue.length < 3) {
144            throw new ArrayIndexOutOfBoundsException
145                ("xyzvalue.length < 3");
146        }
147
148        float[] colorvalue = new float[getNumComponents()];
149
150        System.arraycopy(xyzvalue, 0, colorvalue, 0,
151                         Math.min(3, colorvalue.length));
152
153        return xyzvalue;
154    }
155}