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. package com.sun.imageio.plugins.common;
  8. import java.awt.color.ColorSpace;
  9. /**
  10. * A dummy <code>ColorSpace</code> to enable <code>ColorModel</code>
  11. * for image data which do not have an innate color representation.
  12. */
  13. public class BogusColorSpace extends ColorSpace {
  14. /**
  15. * Return the type given the number of components.
  16. *
  17. * @param numComponents The number of components in the
  18. * <code>ColorSpace</code>.
  19. * @exception IllegalArgumentException if <code>numComponents</code>
  20. * is less than 1.
  21. */
  22. private static int getType(int numComponents) {
  23. if(numComponents < 1) {
  24. throw new IllegalArgumentException("numComponents < 1!");
  25. }
  26. int type;
  27. switch(numComponents) {
  28. case 1:
  29. type = ColorSpace.TYPE_GRAY;
  30. break;
  31. default:
  32. // Based on the constant definitions TYPE_2CLR=12 through
  33. // TYPE_FCLR=25. This will return unknown types for
  34. // numComponents > 15.
  35. type = numComponents + 10;
  36. }
  37. return type;
  38. }
  39. /**
  40. * Constructs a bogus <code>ColorSpace</code>.
  41. *
  42. * @param numComponents The number of components in the
  43. * <code>ColorSpace</code>.
  44. * @exception IllegalArgumentException if <code>numComponents</code>
  45. * is less than 1.
  46. */
  47. public BogusColorSpace(int numComponents) {
  48. super(getType(numComponents), numComponents);
  49. }
  50. //
  51. // The following methods simply copy the input array to the
  52. // output array while otherwise attempting to adhere to the
  53. // specified behavior of the methods vis-a-vis exceptions.
  54. //
  55. public float[] toRGB(float[] colorvalue) {
  56. if(colorvalue.length < getNumComponents()) {
  57. throw new ArrayIndexOutOfBoundsException
  58. ("colorvalue.length < getNumComponents()");
  59. }
  60. float[] rgbvalue = new float[3];
  61. System.arraycopy(colorvalue, 0, rgbvalue, 0,
  62. Math.min(3, getNumComponents()));
  63. return colorvalue;
  64. }
  65. public float[] fromRGB(float[] rgbvalue) {
  66. if(rgbvalue.length < 3) {
  67. throw new ArrayIndexOutOfBoundsException
  68. ("rgbvalue.length < 3");
  69. }
  70. float[] colorvalue = new float[getNumComponents()];
  71. System.arraycopy(rgbvalue, 0, colorvalue, 0,
  72. Math.min(3, colorvalue.length));
  73. return rgbvalue;
  74. }
  75. public float[] toCIEXYZ(float[] colorvalue) {
  76. if(colorvalue.length < getNumComponents()) {
  77. throw new ArrayIndexOutOfBoundsException
  78. ("colorvalue.length < getNumComponents()");
  79. }
  80. float[] xyzvalue = new float[3];
  81. System.arraycopy(colorvalue, 0, xyzvalue, 0,
  82. Math.min(3, getNumComponents()));
  83. return colorvalue;
  84. }
  85. public float[] fromCIEXYZ(float[] xyzvalue) {
  86. if(xyzvalue.length < 3) {
  87. throw new ArrayIndexOutOfBoundsException
  88. ("xyzvalue.length < 3");
  89. }
  90. float[] colorvalue = new float[getNumComponents()];
  91. System.arraycopy(xyzvalue, 0, colorvalue, 0,
  92. Math.min(3, colorvalue.length));
  93. return xyzvalue;
  94. }
  95. }