1. /*
  2. * @(#)ColorModel.java 1.60 01/11/29
  3. *
  4. * Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  5. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  6. */
  7. package java.awt.image;
  8. import java.awt.Transparency;
  9. import java.awt.color.ColorSpace;
  10. import java.awt.Toolkit;
  11. /**
  12. * The <code>ColorModel</code> abstract class encapsulates the
  13. * methods for translating a pixel value to color components
  14. * (for example, red, green, and blue) and an alpha component.
  15. * In order to render an image to the screen, a printer, or another
  16. * image, pixel values must be converted to color and alpha components.
  17. * As arguments to or return values from methods of this class,
  18. * pixels are represented as 32-bit ints or as arrays of primitive types.
  19. * The number, order, and interpretation of color components for a
  20. * <code>ColorModel</code> is specified by its <code>ColorSpace</code>.
  21. * A <code>ColorModel</code> used with pixel data that does not include
  22. * alpha information treats all pixels as opaque, which is an alpha
  23. * value of 1.0.
  24. * <p>
  25. * This <code>ColorModel</code> class supports two representations of
  26. * pixel values. A pixel value can be a single 32-bit int or an
  27. * array of primitive types. The Java(tm) Platform 1.0 and 1.1 APIs
  28. * represented pixels as single <code>byte</code> or single
  29. * <code>int</code> values. For purposes of the <code>ColorModel</code>
  30. * class, pixel value arguments were passed as ints. The Java(tm)
  31. * Platform 1.2 API introduced additional classes for representing images.
  32. * With {@link BufferedImage} or {@link RenderedImage}
  33. * objects, based on {@link Raster} and {@link SampleModel} classes, pixel
  34. * values might not be conveniently representable as a single int.
  35. * Consequently, <code>ColorModel</code> now has methods that accept
  36. * pixel values represented as arrays of primitive types. The primitive
  37. * type used by a particular <code>ColorModel</code> object is called its
  38. * transfer type.
  39. * <p>
  40. * <code>ColorModel</code> objects used with images for which pixel values
  41. * are not conveniently representable as a single int throw an
  42. * {@link IllegalArgumentException} when methods taking a single int pixel
  43. * argument are called. Subclasses of <code>ColorModel</code> must
  44. * specify the conditions under which this occurs. This does not
  45. * occur with {@link DirectColorModel} or {@link IndexColorModel} objects.
  46. * <p>
  47. * Currently, the transfer types supported by the Java(tm) 2D API are
  48. * DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT, and DataBuffer.TYPE_INT.
  49. * The transfer type for a particular <code>ColorModel</code> object is
  50. * specified when the object is created, either explicitly or by default.
  51. * All subclasses of <code>ColorModel</code> must specify what the
  52. * possible transfer types are and how the number of elements in the
  53. * primitive arrays representing pixels is determined.
  54. * <p>
  55. * For <code>BufferedImages</code>, the transfer type of its
  56. * <code>Raster</code> and of the <code>Raster</code> object's
  57. * <code>SampleModel</code> (available from the
  58. * <code>getTransferType</code> methods of these classes) must match that
  59. * of the <code>ColorModel</code>. The number of elements in an array
  60. * representing a pixel for the <code>Raster</code> and
  61. * <code>SampleModel</code> (available from the
  62. * <code>getNumDataElements</code> methods of these classes) must match
  63. * that of the <code>ColorModel</code>.
  64. * <p>
  65. * The algorithm used to convert from pixel values to color and alpha
  66. * components varies by subclass. For example, there is not necessarily
  67. * a one-to-one correspondence between samples obtained from the
  68. * <code>SampleModel</code> of a <code>BufferedImage</code> object's
  69. * <code>Raster</code> and color/alpha components. Even when
  70. * there is such a correspondence, the number of bits in a sample is not
  71. * necessarily the same as the number of bits in the corresponding color/alpha
  72. * component. Each subclass must specify how the translation from
  73. * pixel values to color/alpha components is done.
  74. * <p>
  75. * Methods in the <code>ColorModel</code> class use two different
  76. * representations of color and alpha components. In the unnormalized
  77. * form, each component is an unsigned integral value between 0 and
  78. * 2<sup>n</sup> - 1, where n is the number of significant bits for a
  79. * particular component. If pixel values for a particular
  80. * <code>ColorModel</code> represent color samples premultiplied by
  81. * the alpha sample, unnormalized color component values are
  82. * also premultiplied. In the normalized form, each component is a
  83. * <code>float</code> value between 0.0 and 1.0. Normalized color
  84. * component values are not premultiplied.
  85. *
  86. * @see IndexColorModel
  87. * @see ComponentColorModel
  88. * @see PackedColorModel
  89. * @see DirectColorModel
  90. * @see java.awt.Image
  91. * @see BufferedImage
  92. * @see RenderedImage
  93. * @see java.awt.color.ColorSpace
  94. * @see SampleModel
  95. * @see Raster
  96. * @see DataBuffer
  97. * @version 10 Feb 1997
  98. */
  99. public abstract class ColorModel implements Transparency{
  100. private long pData; // Placeholder for data for native functions
  101. /**
  102. * The total number of bits in the pixel.
  103. */
  104. protected int pixel_bits;
  105. int nBits[];
  106. int transparency = Transparency.TRANSLUCENT;
  107. boolean supportsAlpha = true;
  108. boolean isAlphaPremultiplied = false;
  109. int numComponents = -1;
  110. int numColorComponents = -1;
  111. ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
  112. int colorSpaceType = ColorSpace.TYPE_RGB;
  113. int maxBits;
  114. boolean is_sRGB = true;
  115. /**
  116. * Data type of the array used to represent pixel values.
  117. */
  118. protected int transferType;
  119. /**
  120. * This is copied from java.awt.Toolkit since we need the library
  121. * loaded in java.awt.image also:
  122. *
  123. * WARNING: This is a temporary workaround for a problem in the
  124. * way the AWT loads native libraries. A number of classes in the
  125. * AWT package have a native method, initIDs(), which initializes
  126. * the JNI field and method ids used in the native portion of
  127. * their implementation.
  128. *
  129. * Since the use and storage of these ids is done by the
  130. * implementation libraries, the implementation of these method is
  131. * provided by the particular AWT implementations
  132. * (for example, "Toolkit"s/Peer), such as Motif, Win32 or Tiny. The
  133. * problem is that this means that the native libraries must be
  134. * loaded by the java.* classes, which do not necessarily know the
  135. * names of the libraries to load. A better way of doing this
  136. * would be to provide a separate library which defines java.awt.*
  137. * initIDs, and exports the relevant symbols out to the
  138. * implementation libraries.
  139. *
  140. * For now, we know it's done by the implementation, and we assume
  141. * that the name of the library is "awt". -br.
  142. */
  143. private static boolean loaded = false;
  144. static void loadLibraries() {
  145. if (!loaded) {
  146. java.security.AccessController.doPrivileged(
  147. new sun.security.action.LoadLibraryAction("awt"));
  148. loaded = true;
  149. }
  150. }
  151. private static native void initIDs();
  152. static {
  153. /* ensure that the proper libraries are loaded */
  154. loadLibraries();
  155. initIDs();
  156. }
  157. private static ColorModel RGBdefault;
  158. /**
  159. * Returns a <code>DirectColorModel</code> that describes the default
  160. * format for integer RGB values used in many of the methods in the
  161. * AWT image interfaces for the convenience of the programmer.
  162. * The color space is the default {@link ColorSpace}, sRGB.
  163. * The format for the RGB values is an integer with 8 bits
  164. * each of alpha, red, green, and blue color components ordered
  165. * correspondingly from the most significant byte to the least
  166. * significant byte, as in: 0xAARRGGBB. Color components are
  167. * not premultiplied by the alpha component. This format does not
  168. * necessarily represent the native or the most efficient
  169. * <code>ColorModel</code> for a particular device or for all images.
  170. * It is merely used as a common color model format.
  171. * @return a <code>DirectColorModel</code>object describing default
  172. * RGB values.
  173. */
  174. public static ColorModel getRGBdefault() {
  175. if (RGBdefault == null) {
  176. RGBdefault = new DirectColorModel(32,
  177. 0x00ff0000, // Red
  178. 0x0000ff00, // Green
  179. 0x000000ff, // Blue
  180. 0xff000000 // Alpha
  181. );
  182. }
  183. return RGBdefault;
  184. }
  185. /**
  186. * Constructs a <code>ColorModel</code> that translates pixels of the
  187. * specified number of bits to color/alpha components. The color
  188. * space is the default RGB <code>ColorSpace</code>, which is sRGB.
  189. * Pixel values are assumed to include alpha information. If color
  190. * and alpha information are represented in the pixel value as
  191. * separate spatial bands, the color bands are assumed not to be
  192. * premultiplied with the alpha value. The transparency type is
  193. * java.awt.Transparency.TRANSLUCENT. The transfer type will be the
  194. * smallest of DataBuffer.TYPE_BYTE, DataBuffer.TYPE_USHORT,
  195. * or DataBuffer.TYPE_INT that can hold a single pixel
  196. * (or DataBuffer.TYPE_UNDEFINED if bits is greater
  197. * than 32). Since this constructor has no information about the
  198. * number of bits per color and alpha component, any subclass calling
  199. * this constructor should override any method that requires this
  200. * information.
  201. * @param bits the number of bits of a pixel
  202. * @exception <code>IllegalArgumentException</code> if the number
  203. * of bits in <code>bits</code> is less than 1
  204. */
  205. public ColorModel(int bits) {
  206. pixel_bits = bits;
  207. if (bits < 1) {
  208. throw new IllegalArgumentException("Number of bits must be > 0");
  209. }
  210. numComponents = 4;
  211. numColorComponents = 3;
  212. maxBits = bits;
  213. // REMIND: make sure transferType is set correctly
  214. transferType = ColorModel.getDefaultTransferType(bits);
  215. }
  216. /**
  217. * Constructs a <code>ColorModel</code> that translates pixel values
  218. * to color/alpha components. Color components will be in the
  219. * specified <code>ColorSpace</code>. <code>pixel_bits</code> is the
  220. * number of bits in the pixel values. The bits array
  221. * specifies the number of significant bits per color and alpha component.
  222. * Its length should be the number of components in the
  223. * <code>ColorSpace</code> if there is no alpha information in the
  224. * pixel values, or one more than this number if there is alpha
  225. * information. <code>hasAlpha</code> indicates whether or not alpha
  226. * information is present. The <code>boolean</code>
  227. * <code>isAlphaPremultiplied</code> specifies how to interpret pixel
  228. * values in which color and alpha information are represented as
  229. * separate spatial bands. If the <code>boolean</code>
  230. * is <code>true</code>, color samples are assumed to have been
  231. * multiplied by the alpha sample. The <code>transparency</code>
  232. * specifies what alpha values can be represented by this color model.
  233. * The transfer type is the type of primitive array used to represent
  234. * pixel values. Note that the bits array contains the number of
  235. * significant bits per color/alpha component after the translation
  236. * from pixel values. For example, for an
  237. * <code>IndexColorModel</code> with <code>pixel_bits</code> equal to
  238. * 16, the bits array might have four elements with each element set
  239. * to 8.
  240. * @param pixel_bits the number of bits in the pixel values
  241. * @param bits array that specifies the number of significant bits
  242. * per color and alpha component
  243. * @param cspace the specified <code>ColorSpace</code>
  244. * @param hasAlpha <code>true</code> if alpha information is present;
  245. * <code>false</code> otherwise
  246. * @param isAlphaPremultiplied <code>true</code> if color samples are
  247. * assumed to be premultiplied by the alpha samples;
  248. * <code>false</code> otherwise
  249. * @param transparency what alpha values can be represented by this
  250. * color model
  251. * @param transferType the type of the array used to represent pixel
  252. * values
  253. * @exception <code>IllegalArgumentException</code> if the length of
  254. * the bit array is less than the number of color or alpha
  255. * components in this <code>ColorModel</code>
  256. * @see java.awt.Transparency
  257. */
  258. protected ColorModel(int pixel_bits, int[] bits, ColorSpace cspace,
  259. boolean hasAlpha,
  260. boolean isAlphaPremultiplied,
  261. int transparency,
  262. int transferType) {
  263. colorSpace = cspace;
  264. colorSpaceType = cspace.getType();
  265. numColorComponents = cspace.getNumComponents();
  266. numComponents = numColorComponents + (hasAlpha ? 1 : 0);
  267. supportsAlpha = hasAlpha;
  268. if (bits.length < numComponents) {
  269. throw new IllegalArgumentException("Number of color/alpha "+
  270. "components should be "+
  271. numComponents+
  272. " but length of bits array is "+
  273. bits.length);
  274. }
  275. if (supportsAlpha == false) {
  276. this.isAlphaPremultiplied = false;
  277. this.transparency = Transparency.OPAQUE;
  278. }
  279. else {
  280. this.isAlphaPremultiplied = isAlphaPremultiplied;
  281. this.transparency = transparency;
  282. }
  283. nBits = (int[]) bits.clone();
  284. this.pixel_bits = pixel_bits;
  285. if (pixel_bits <= 0) {
  286. throw new IllegalArgumentException("Number of pixel bits must "+
  287. "be > 0");
  288. }
  289. // Check for bits < 0
  290. maxBits = 0;
  291. for (int i=0; i < bits.length; i++) {
  292. if (bits[i] <= 0) {
  293. throw new
  294. IllegalArgumentException("Number of bits must be > 0");
  295. }
  296. if (maxBits < bits[i]) {
  297. maxBits = bits[i];
  298. }
  299. }
  300. // Save this since we always need to check if it is the default CS
  301. if (cspace != ColorSpace.getInstance(ColorSpace.CS_sRGB)) {
  302. is_sRGB = false;
  303. }
  304. // Save the transfer type
  305. this.transferType = transferType;
  306. }
  307. /**
  308. * Returns whether or not alpha is supported in this
  309. * <code>ColorModel</code>.
  310. * @return <code>true</code> if alpha is supported in this
  311. * <code>ColorModel</code> <code>false</code> otherwise.
  312. */
  313. final public boolean hasAlpha() {
  314. return supportsAlpha;
  315. }
  316. /**
  317. * Returns whether or not the alpha has been premultiplied in the
  318. * pixel values to be translated by this <code>ColorModel</code>.
  319. * If the boolean is <code>true</code>, this <code>ColorModel</code>
  320. * is to be used to interpret pixel values in which color and alpha
  321. * information are represented as separate spatial bands, and color
  322. * samples are assumed to have been multiplied by the
  323. * alpha sample.
  324. * @return <code>true</code> if the alpha values are premultiplied
  325. * in the pixel values to be translated by this
  326. * <code>ColorModel</code> <code>false</code> otherwise.
  327. */
  328. final public boolean isAlphaPremultiplied() {
  329. return isAlphaPremultiplied;
  330. }
  331. /**
  332. * Returns the number of bits per pixel described by this
  333. * <code>ColorModel</code>.
  334. * @return the number of bits per pixel.
  335. */
  336. public int getPixelSize() {
  337. return pixel_bits;
  338. }
  339. /**
  340. * Returns the number of bits for the specified color/alpha component.
  341. * Color components are indexed in the order specified by the
  342. * <code>ColorSpace</code>. Typically, this order reflects the name
  343. * of the color space type. For example, for TYPE_RGB, index 0
  344. * corresponds to red, index 1 to green, and index 2
  345. * to blue. If this <code>ColorModel</code> supports alpha, the alpha
  346. * component corresponds to the index following the last color
  347. * component.
  348. * @param componentIdx the index of the color/alpha component
  349. * @return the number of bits for the color/alpha component at the
  350. * specified index.
  351. * @throws ArrayIndexOutOfBoundsException if <code>componentIdx</code>
  352. * is greater than the number of components or
  353. * less than zero
  354. */
  355. public int getComponentSize(int componentIdx) {
  356. // REMIND:
  357. if (nBits == null) {
  358. throw new NullPointerException("Number of bits array is null.");
  359. }
  360. return nBits[componentIdx];
  361. }
  362. /**
  363. * Returns an array of the number of bits per color/alpha component.
  364. * The array contains the color components in the order specified by the
  365. * <code>ColorSpace</code>, followed by the alpha component, if
  366. * present.
  367. * @return an array of the number of bits per color/alpha component
  368. */
  369. public int[] getComponentSize() {
  370. if (nBits != null) {
  371. return (int[]) nBits.clone();
  372. }
  373. return null;
  374. }
  375. /**
  376. * Returns the transparency. Returns either OPAQUE, BITMASK,
  377. * or TRANSLUCENT.
  378. * @return the transparency of this <code>ColorModel</code>.
  379. * @see Transparency#OPAQUE
  380. * @see Transparency#BITMASK
  381. * @see Transparency#TRANSLUCENT
  382. */
  383. public int getTransparency() {
  384. return transparency;
  385. }
  386. /**
  387. * Returns the number of components, including alpha, in this
  388. * <code>ColorModel</code>. This is equal to the number of color
  389. * components, optionally plus one, if there is an alpha component.
  390. * @return the number of components in this <code>ColorModel</code>
  391. */
  392. public int getNumComponents() {
  393. return numComponents;
  394. }
  395. /**
  396. * Returns the number of color components in this
  397. * <code>ColorModel</code>.
  398. * This is the number of components returned by
  399. * {@link ColorSpace#getNumComponents}.
  400. * @return the number of color components in this
  401. * <code>ColorModel</code>.
  402. * @see ColorSpace#getNumComponents
  403. */
  404. public int getNumColorComponents() {
  405. return numColorComponents;
  406. }
  407. /**
  408. * Returns the red color component for the specified pixel, scaled
  409. * from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
  410. * is done if necessary. The pixel value is specified as an int.
  411. * An <code>IllegalArgumentException</code> is thrown if pixel
  412. * values for this <code>ColorModel</code> are not conveniently
  413. * representable as a single int. The returned value is not a
  414. * pre-multiplied value. For example, if the
  415. * alpha is premultiplied, this method divides it out before returning
  416. * the value. If the alpha value is 0, the red value is 0.
  417. * @param pixel a specified pixel
  418. * @return the value of the red component of the specified pixel.
  419. */
  420. public abstract int getRed(int pixel);
  421. /**
  422. * Returns the green color component for the specified pixel, scaled
  423. * from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
  424. * is done if necessary. The pixel value is specified as an int.
  425. * An <code>IllegalArgumentException</code> is thrown if pixel
  426. * values for this <code>ColorModel</code> are not conveniently
  427. * representable as a single int. The returned value is a non
  428. * pre-multiplied value. For example, if the alpha is premultiplied,
  429. * this method divides it out before returning
  430. * the value. If the alpha value is 0, the green value is 0.
  431. * @param pixel the specified pixel
  432. * @return the value of the green component of the specified pixel.
  433. */
  434. public abstract int getGreen(int pixel);
  435. /**
  436. * Returns the blue color component for the specified pixel, scaled
  437. * from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
  438. * is done if necessary. The pixel value is specified as an int.
  439. * An <code>IllegalArgumentException</code> is thrown if pixel values
  440. * for this <code>ColorModel</code> are not conveniently representable
  441. * as a single int. The returned value is a non pre-multiplied
  442. * value, for example, if the alpha is premultiplied, this method
  443. * divides it out before returning the value. If the alpha value is
  444. * 0, the blue value is 0.
  445. * @param pixel the specified pixel
  446. * @return the value of the blue component of the specified pixel.
  447. */
  448. public abstract int getBlue(int pixel);
  449. /**
  450. * Returns the alpha component for the specified pixel, scaled
  451. * from 0 to 255. The pixel value is specified as an int.
  452. * An <code>IllegalArgumentException</code> is thrown if pixel
  453. * values for this <code>ColorModel</code> are not conveniently
  454. * representable as a single int.
  455. * @param pixel the specified pixel
  456. * @return the value of alpha component of the specified pixel.
  457. */
  458. public abstract int getAlpha(int pixel);
  459. /**
  460. * Returns the color/alpha components of the pixel in the default
  461. * RGB color model format. A color conversion is done if necessary.
  462. * The pixel value is specified as an int.
  463. * An <code>IllegalArgumentException</code> thrown if pixel values
  464. * for this <code>ColorModel</code> are not conveniently representable
  465. * as a single int. The returned value is in a non
  466. * pre-multiplied format. For example, if the alpha is premultiplied,
  467. * this method divides it out of the color components. If the alpha
  468. * value is 0, the color values are 0.
  469. * @param pixel the specified pixel
  470. * @return the RGB value of the color/alpha components of the
  471. * specified pixel.
  472. * @see ColorModel#getRGBdefault
  473. */
  474. public int getRGB(int pixel) {
  475. return (getAlpha(pixel) << 24)
  476. | (getRed(pixel) << 16)
  477. | (getGreen(pixel) << 8)
  478. | (getBlue(pixel) << 0);
  479. }
  480. /**
  481. * Returns the red color component for the specified pixel, scaled
  482. * from 0 to 255 in the default RGB <code>ColorSpace</code>, sRGB. A
  483. * color conversion is done if necessary. The pixel value is
  484. * specified by an array of data elements of type transferType passed
  485. * in as an object reference. The returned value is a non
  486. * pre-multiplied value. For example, if alpha is premultiplied,
  487. * this method divides it out before returning
  488. * the value. If the alpha value is 0, the red value is 0.
  489. * If <code>inData</code> is not a primitive array of type
  490. * transferType, a <code>ClassCastException</code> is thrown. An
  491. * <code>ArrayIndexOutOfBoundsException</code> is thrown if
  492. * <code>inData</code> is not large enough to hold a pixel value for
  493. * this <code>ColorModel</code>.
  494. * If this <code>transferType</code> is not supported, a
  495. * <code>UnsupportedOperationException</code> will be
  496. * thrown.
  497. * @param inData an array of pixel values
  498. * @return the value of the red component of the specified pixel.
  499. * @exception <code>ClassCastException</code> if <code>inData</code>
  500. * is not a primitive array of type <code>transferType</code>
  501. * @exception <code>ArrayIndexOutOfBoundsException</code> if
  502. * <code>inData</code> is not large enough to hold a pixel value
  503. * for this <code>ColorModel</code>
  504. * @exception <code>UnsupportedOperationException</code> if this
  505. * <code>tranferType</code> is not supported by this
  506. * <code>ColorModel</code>
  507. */
  508. public int getRed(Object inData) {
  509. int pixel=0,length=0;
  510. switch (transferType) {
  511. case DataBuffer.TYPE_BYTE:
  512. byte bdata[] = (byte[])inData;
  513. pixel = bdata[0] & 0xff;
  514. length = bdata.length;
  515. break;
  516. case DataBuffer.TYPE_USHORT:
  517. short sdata[] = (short[])inData;
  518. pixel = sdata[0];
  519. length = sdata.length;
  520. break;
  521. case DataBuffer.TYPE_INT:
  522. int idata[] = (int[])inData;
  523. pixel = idata[0];
  524. length = idata.length;
  525. break;
  526. default:
  527. throw new UnsupportedOperationException("This method has not been "+
  528. "implemented for transferType " + transferType);
  529. }
  530. if (length == 1) {
  531. return getRed(pixel);
  532. }
  533. else {
  534. throw new UnsupportedOperationException
  535. ("This method is not supported by this color model");
  536. }
  537. }
  538. /**
  539. * Returns the green color component for the specified pixel, scaled
  540. * from 0 to 255 in the default RGB <code>ColorSpace</code>, sRGB. A
  541. * color conversion is done if necessary. The pixel value is
  542. * specified by an array of data elements of type transferType passed
  543. * in as an object reference. The returned value will be a non
  544. * pre-multiplied value. For example, if the alpha is premultiplied,
  545. * this method divides it out before returning the value. If the
  546. * alpha value is 0, the green value is 0. If <code>inData</code> is
  547. * not a primitive array of type transferType, a
  548. * <code>ClassCastException</code> is thrown. An
  549. * <code>ArrayIndexOutOfBoundsException</code> is thrown if
  550. * <code>inData</code> is not large enough to hold a pixel value for
  551. * this <code>ColorModel</code>.
  552. * If this <code>transferType</code> is not supported, a
  553. * <code>UnsupportedOperationException</code> will be
  554. * thrown.
  555. * @param inData an array of pixel values
  556. * @return the value of the green component of the specified pixel.
  557. * @exception <code>ClassCastException</code> if <code>inData</code>
  558. * is not a primitive array of type <code>transferType</code>
  559. * @exception <code>ArrayIndexOutOfBoundsException</code> if
  560. * <code>inData</code> is not large enough to hold a pixel value
  561. * for this <code>ColorModel</code>
  562. * @exception <code>UnsupportedOperationException</code> if this
  563. * <code>tranferType</code> is not supported by this
  564. * <code>ColorModel</code>
  565. */
  566. public int getGreen(Object inData) {
  567. int pixel=0,length=0;
  568. switch (transferType) {
  569. case DataBuffer.TYPE_BYTE:
  570. byte bdata[] = (byte[])inData;
  571. pixel = bdata[0] & 0xff;
  572. length = bdata.length;
  573. break;
  574. case DataBuffer.TYPE_USHORT:
  575. short sdata[] = (short[])inData;
  576. pixel = sdata[0];
  577. length = sdata.length;
  578. break;
  579. case DataBuffer.TYPE_INT:
  580. int idata[] = (int[])inData;
  581. pixel = idata[0];
  582. length = idata.length;
  583. break;
  584. default:
  585. throw new UnsupportedOperationException("This method has not been "+
  586. "implemented for transferType " + transferType);
  587. }
  588. if (length == 1) {
  589. return getGreen(pixel);
  590. }
  591. else {
  592. throw new UnsupportedOperationException
  593. ("This method is not supported by this color model");
  594. }
  595. }
  596. /**
  597. * Returns the blue color component for the specified pixel, scaled
  598. * from 0 to 255 in the default RGB <code>ColorSpace</code>, sRGB. A
  599. * color conversion is done if necessary. The pixel value is
  600. * specified by an array of data elements of type transferType passed
  601. * in as an object reference. The returned value is a non
  602. * pre-multiplied value. For example, if the alpha is premultiplied,
  603. * this method divides it out before returning the value. If the
  604. * alpha value is 0, the blue value will be 0. If
  605. * <code>inData</code> is not a primitive array of type transferType,
  606. * a <code>ClassCastException</code> is thrown. An
  607. * <code>ArrayIndexOutOfBoundsException</code> is
  608. * thrown if <code>inData</code> is not large enough to hold a pixel
  609. * value for this <code>ColorModel</code>.
  610. * If this <code>transferType</code> is not supported, a
  611. * <code>UnsupportedOperationException</code> will be
  612. * thrown.
  613. * @param inData an array of pixel values
  614. * @return the value of the blue component of the specified pixel.
  615. * @exception <code>ClassCastException</code> if <code>inData</code>
  616. * is not a primitive array of type <code>transferType</code>
  617. * @exception <code>ArrayIndexOutOfBoundsException</code> if
  618. * <code>inData</code> is not large enough to hold a pixel value
  619. * for this <code>ColorModel</code>
  620. * @exception <code>UnsupportedOperationException</code> if this
  621. * <code>tranferType</code> is not supported by this
  622. * <code>ColorModel</code>
  623. */
  624. public int getBlue(Object inData) {
  625. int pixel=0,length=0;
  626. switch (transferType) {
  627. case DataBuffer.TYPE_BYTE:
  628. byte bdata[] = (byte[])inData;
  629. pixel = bdata[0] & 0xff;
  630. length = bdata.length;
  631. break;
  632. case DataBuffer.TYPE_USHORT:
  633. short sdata[] = (short[])inData;
  634. pixel = sdata[0];
  635. length = sdata.length;
  636. break;
  637. case DataBuffer.TYPE_INT:
  638. int idata[] = (int[])inData;
  639. pixel = idata[0];
  640. length = idata.length;
  641. break;
  642. default:
  643. throw new UnsupportedOperationException("This method has not been "+
  644. "implemented for transferType " + transferType);
  645. }
  646. if (length == 1) {
  647. return getBlue(pixel);
  648. }
  649. else {
  650. throw new UnsupportedOperationException
  651. ("This method is not supported by this color model");
  652. }
  653. }
  654. /**
  655. * Returns the alpha component for the specified pixel, scaled
  656. * from 0 to 255. The pixel value is specified by an array of data
  657. * elements of type transferType passed in as an object reference.
  658. * If inData is not a primitive array of type transferType, a
  659. * <code>ClassCastException</code> is thrown. An
  660. * <code>ArrayIndexOutOfBoundsException</code> is thrown if
  661. * <code>inData</code> is not large enough to hold a pixel value for
  662. * this <code>ColorModel</code>.
  663. * If this <code>transferType</code> is not supported, a
  664. * <code>UnsupportedOperationException</code> will be
  665. * thrown.
  666. * @param inData the specified pixel
  667. * @return the alpha component of the specified pixel, scaled from
  668. * 0 to 255.
  669. * @exception <code>ClassCastException</code> if <code>inData</code>
  670. * is not a primitive array of type <code>transferType</code>
  671. * @exception <code>ArrayIndexOutOfBoundsException</code> if
  672. * <code>inData</code> is not large enough to hold a pixel value
  673. * for this <code>ColorModel</code>
  674. * @exception <code>UnsupportedOperationException</code> if this
  675. * <code>tranferType</code> is not supported by this
  676. * <code>ColorModel</code>
  677. */
  678. public int getAlpha(Object inData) {
  679. int pixel=0,length=0;
  680. switch (transferType) {
  681. case DataBuffer.TYPE_BYTE:
  682. byte bdata[] = (byte[])inData;
  683. pixel = bdata[0] & 0xff;
  684. length = bdata.length;
  685. break;
  686. case DataBuffer.TYPE_USHORT:
  687. short sdata[] = (short[])inData;
  688. pixel = sdata[0];
  689. length = sdata.length;
  690. break;
  691. case DataBuffer.TYPE_INT:
  692. int idata[] = (int[])inData;
  693. pixel = idata[0];
  694. length = idata.length;
  695. break;
  696. default:
  697. throw new UnsupportedOperationException("This method has not been "+
  698. "implemented for transferType " + transferType);
  699. }
  700. if (length == 1) {
  701. return getAlpha(pixel);
  702. }
  703. else {
  704. throw new UnsupportedOperationException
  705. ("This method is not supported by this color model");
  706. }
  707. }
  708. /**
  709. * Returns the color/alpha components for the specified pixel in the
  710. * default RGB color model format. A color conversion is done if
  711. * necessary. The pixel value is specified by an array of data
  712. * elements of type transferType passed in as an object reference.
  713. * If inData is not a primitive array of type transferType, a
  714. * <code>ClassCastException</code> is thrown. An
  715. * <code>ArrayIndexOutOfBoundsException</code> is
  716. * thrown if <code>inData</code> is not large enough to hold a pixel
  717. * value for this <code>ColorModel</code>.
  718. * The returned value will be in a non pre-multiplied format, i.e. if
  719. * the alpha is premultiplied, this method will divide it out of the
  720. * color components (if the alpha value is 0, the color values will be 0).
  721. * @param inData the specified pixel
  722. * @return the color and alpha components of the specified pixel.
  723. * @see ColorModel#getRGBdefault
  724. */
  725. public int getRGB(Object inData) {
  726. return (getAlpha(inData) << 24)
  727. | (getRed(inData) << 16)
  728. | (getGreen(inData) << 8)
  729. | (getBlue(inData) << 0);
  730. }
  731. /**
  732. * Returns a data element array representation of a pixel in this
  733. * <code>ColorModel</code>, given an integer pixel representation in
  734. * the default RGB color model.
  735. * This array can then be passed to the
  736. * {@link WritableRaster#setDataElements} method of
  737. * a {@link WritableRaster} object. If the pixel variable is
  738. * <code>null</code>, a new array will be allocated. If
  739. * <code>pixel</code> is not
  740. * <code>null</code>, it must be a primitive array of type
  741. * <code>transferType</code> otherwise, a
  742. * <code>ClassCastException</code> is thrown. An
  743. * <code>ArrayIndexOutOfBoundsException</code> is thrown if
  744. * <code>pixel</code> is
  745. * not large enough to hold a pixel value for this
  746. * <code>ColorModel</code>. The pixel array is returned.
  747. * If this <code>transferType</code> is not supported, a
  748. * <code>UnsupportedOperationException</code> will be
  749. * thrown.
  750. * @param rgb the integer pixel representation in the default RGB
  751. * color model
  752. * @param pixel the specified pixel
  753. * @return an array representation of the specified pixel in this
  754. * <code>ColorModel</code>.
  755. * @exception <code>ClassCastException</code> if <code>pixel</code>
  756. * is not a primitive array of type <code>transferType</code>
  757. * @exception <code>ArrayIndexOutOfBoundsException</code> if
  758. * <code>pixel</code> is not large enough to hold a pixel value
  759. * for this <code>ColorModel</code>
  760. * @exception <code>UnsupportedOperationException</code> if this
  761. * method is not supported by this <code>ColorModel</code>
  762. * @see WritableRaster#setDataElements
  763. * @see SampleModel#setDataElements
  764. */
  765. public Object getDataElements(int rgb, Object pixel) {
  766. throw new UnsupportedOperationException
  767. ("This method is not supported by this color model.");
  768. }
  769. /**
  770. * Returns an array of unnormalized color/alpha components given a pixel
  771. * in this <code>ColorModel</code>. The pixel value is specified as
  772. * an <code>int</code>. An <code>IllegalArgumentException</code>
  773. * will be thrown if pixel values for this <code>ColorModel</code> are
  774. * not conveniently representable as a single <code>int</code>.
  775. * For example, this method can be used to retrieve the
  776. * components for a specific pixel value in a
  777. * <code>DirectColorModel</code>. If the components array is
  778. * <code>null</code>, a new array will be allocated. The
  779. * components array will be returned. Color/alpha components are
  780. * stored in the components array starting at <code>offset</code>
  781. * (even if the array is allocated by this method). An
  782. * <code>ArrayIndexOutOfBoundsException</code> is thrown if the
  783. * components array is not <code>null</code> and is not large
  784. * enough to hold all the color and alpha components (starting at offset).
  785. * @param pixel the specified pixel
  786. * @param components the array to receive the color and alpha
  787. * components of the specified pixel
  788. * @param offset the offset into the <code>components</code> array at
  789. * which to start storing the color and alpha components
  790. * @return an array containing the color and alpha components of the
  791. * specified pixel starting at the specified offset.
  792. * @exception <code>UnsupportedOperationException</code> if this
  793. * method is not supported by this <code>ColorModel</code>
  794. */
  795. public int[] getComponents(int pixel, int[] components, int offset) {
  796. throw new UnsupportedOperationException
  797. ("This method is not supported by this color model.");
  798. }
  799. /**
  800. * Returns an array of unnormalized color/alpha components given a pixel
  801. * in this <code>ColorModel</code>. The pixel value is specified by
  802. * an array of data elements of type transferType passed in as an
  803. * object reference. If <code>pixel</code> is not a primitive array
  804. * of type transferType, a <code>ClassCastException</code> is thrown.
  805. * An <code>ArrayIndexOutOfBoundsException</code> is
  806. * thrown if <code>pixel</code> is not large enough to hold a pixel
  807. * value for this <code>ColorModel</code>.
  808. * This method can be used to retrieve the components for a specific
  809. * pixel value in any <code>ColorModel</code>. If the components
  810. * array is <code>null</code>, a new array will be allocated. The
  811. * components array will be returned. Color/alpha components are
  812. * stored in the <code>components</code> array starting at
  813. * <code>offset</code> (even if the array is allocated by this
  814. * method). An <code>ArrayIndexOutOfBoundsException</code>
  815. * is thrown if the components array is not <code>null</code> and is
  816. * not large enough to hold all the color and alpha components
  817. * (starting at <code>offset</code>).
  818. * @param pixel the specified pixel
  819. * @param components an array that receives the color and alpha
  820. * components of the specified pixel
  821. * @param offset the index into the <code>components</code> array at
  822. * which to begin storing the color and alpha components of the
  823. * specified pixel
  824. * @return an array containing the color and alpha components of the
  825. * specified pixel starting at the specified offset.
  826. * @exception <code>UnsupportedOperationException</code> if this
  827. * method is not supported by this <code>ColorModel</code>
  828. */
  829. public int[] getComponents(Object pixel, int[] components, int offset) {
  830. throw new UnsupportedOperationException
  831. ("This method is not supported by this color model.");
  832. }
  833. /**
  834. * Returns an array of all of the color/alpha components in unnormalized
  835. * form, given a normalized component array. Unnormalized components
  836. * are unsigned integral values between 0 and 2<sup>n</sup> - 1, where
  837. * n is the number of bits for a particular component. Normalized
  838. * components are float values between 0.0 and 1.0. If the
  839. * <code>components</code> array is <code>null</code>, a new array
  840. * will be allocated. The <code>components</code> array will
  841. * be returned. Color/alpha components are stored in the
  842. * <code>components</code> array starting at <code>offset</code> (even
  843. * if the array is allocated by this method). An
  844. * <code>ArrayIndexOutOfBoundsException</code> is thrown if the
  845. * <code>components</code> array is not <code>null</code> and is not
  846. * large enough to hold all the color and alpha
  847. * components (starting at <code>offset</code>). An
  848. * <code>IllegalArgumentException</code> is thrown if the
  849. * <code>normComponents</code> array is not large enough to hold
  850. * all the color and alpha components starting at
  851. * <code>normOffset</code>.
  852. * @param normComponents an array containing normalized components
  853. * @param normOffset the offset into the <code>normComponents</code>
  854. * array at which to start retrieving normalized components
  855. * @param components an array that receives the components from
  856. * <code>normComponents</code>
  857. * @param offset the index into <code>components</code> at which to
  858. * begin storing normalized components from
  859. * <code>normComponents</code>
  860. * @return an array containing unnormalized color and alpha
  861. * components.
  862. * @exception <code>UnsupportedOperationException</code> if this
  863. * method is not supported by this <code>ColorModel</code>
  864. */
  865. public int[] getUnnormalizedComponents(float[] normComponents,
  866. int normOffset,
  867. int[] components, int offset) {
  868. // Make sure that someone isn't using a custom color model
  869. // that called the super(bits) constructor.
  870. if (colorSpace == null) {
  871. throw new UnsupportedOperationException("This method is not supported "+
  872. "by this color model.");
  873. }
  874. if (nBits == null) {
  875. throw new UnsupportedOperationException ("This method is not supported. "+
  876. "Unable to determine #bits per "+
  877. "component.");
  878. }
  879. if ((normComponents.length - normOffset) < numComponents) {
  880. throw new
  881. IllegalArgumentException(
  882. "Incorrect number of components. Expecting "+
  883. numComponents);
  884. }
  885. if (components == null) {
  886. components = new int[offset+numComponents];
  887. }
  888. if (supportsAlpha && isAlphaPremultiplied) {
  889. float normAlpha = normComponents[normOffset+numColorComponents];
  890. for (int i=0; i < numColorComponents; i++) {
  891. components[offset+i] = (int) (normComponents[normOffset+i]
  892. * ((1<<nBits[i]) - 1)
  893. * normAlpha + .5);
  894. }
  895. components[offset+numColorComponents] = (int)
  896. (normAlpha * (1<<nBits[numColorComponents] - 1) + .5);
  897. }
  898. else {
  899. for (int i=0; i < numComponents; i++) {
  900. components[offset+i] = (int) (normComponents[normOffset+i]
  901. * ((1<<nBits[i]) - 1));
  902. }
  903. }
  904. return components;
  905. }
  906. /**
  907. * Returns an array of all of the color/alpha components in normalized
  908. * form, given an unnormalized component array. Unnormalized components
  909. * are unsigned integral values between 0 and 2<sup>n</sup> - 1, where
  910. * n is the number of bits for a particular component. Normalized
  911. * components are float values between 0.0 and 1.0. If the
  912. * <code>normComponents</code> array is <code>null</code>, a new array
  913. * will be allocated. The <code>normComponents</code> array
  914. * will be returned. Color/alpha components are stored in the
  915. * <code>normComponents</code> array starting at
  916. * <code>normOffset</code> (even if the array is allocated by this
  917. * method). An <code>ArrayIndexOutOfBoundsException</code> is thrown
  918. * if the <code>normComponents</code> array is not <code>null</code>
  919. * and is not large enough to hold all the color and alpha components
  920. * (starting at <code>normOffset</code>). An
  921. * <code>IllegalArgumentException</code> is thrown if the
  922. * <code>components</code> array is not large enough to hold all the
  923. * color and alpha components starting at <code>offset</code>.
  924. * @param components an array containing unnormalized components
  925. * @param offset the offset into the <code>components</code> array at
  926. * which to start retrieving unnormalized components
  927. * @param normComponents an array that receives the components from
  928. * <code>components</code>
  929. * @param normOffset the index into <code>normComponents</code> at
  930. * which to begin storing unnormalized components from
  931. * <code>components</code>
  932. * @return an array containing normalized color and alpha
  933. * components.
  934. * @exception <code>UnsupportedOperationException</code> if this
  935. * method is not supported by this <code>ColorModel</code>
  936. */
  937. public float[] getNormalizedComponents(int[] components, int offset,
  938. float[] normComponents,
  939. int normOffset) {
  940. // Make sure that someone isn't using a custom color model
  941. // that called the super(bits) constructor.
  942. if (colorSpace == null) {
  943. throw new UnsupportedOperationException("This method is not supported by "+
  944. "this color model.");
  945. }
  946. if (nBits == null) {
  947. throw new UnsupportedOperationException ("This method is not supported. "+
  948. "Unable to determine #bits per "+
  949. "component.");
  950. }
  951. if ((components.length - offset) < numComponents) {
  952. throw new
  953. IllegalArgumentException(
  954. "Incorrect number of components. Expecting "+
  955. numComponents);
  956. }
  957. if (normComponents == null) {
  958. normComponents = new float[numComponents+normOffset];
  959. }
  960. if (supportsAlpha && isAlphaPremultiplied) {
  961. // Normalized coordinates are non premultiplied
  962. float normAlpha = (float)components[offset+numColorComponents];
  963. normAlpha /= ((1<<nBits[numColorComponents]) - 1.f);
  964. for (int i=0; i < numColorComponents; i++) {
  965. normComponents[normOffset+i] =
  966. ((float)components[offset+i]) /
  967. (normAlpha * ((1<<nBits[i]) - 1.f));
  968. }
  969. normComponents[normOffset+numColorComponents] = normAlpha;
  970. }
  971. else {
  972. for (int i=0; i < numComponents; i++) {
  973. normComponents[normOffset+i] =
  974. (float) components[offset+i] / ((1<<nBits[i]) - 1.f);
  975. }
  976. }
  977. return normComponents;
  978. }
  979. /**
  980. * Returns a pixel value represented as an <code>int</code> in this
  981. * <code>ColorModel</code>, given an array of unnormalized color/alpha
  982. * components. This method will throw an
  983. * <code>IllegalArgumentException</code> if pixel values for this
  984. * <code>ColorModel</code> are not conveniently representable as a
  985. * single <code>int</code>. An
  986. * <code>ArrayIndexOutOfBoundsException</code> is thrown if the
  987. * <code>components</code> array is not large enough to hold all the
  988. * color and alpha components (starting at <code>offset</code>).
  989. * @param components an array of unnormalized color and alpha
  990. * components
  991. * @param offset the index into <code>components</code> at which to
  992. * begin retrieving the color and alpha components
  993. * @return an <code>int</code> pixel value in this
  994. * <code>ColorModel</code> corresponding to the specified components.
  995. * @exception <code>IllegalArgumentException</code> if
  996. * pixel values for this <code>ColorModel</code> are not
  997. * conveniently representable as a single <code>int</code>
  998. * @exception <code>ArrayIndexOutOfBoundsException</code> if
  999. * the <code>components</code> array is not large enough to
  1000. * hold all of the color and alpha components starting at
  1001. * <code>offset</code>
  1002. * @exception <code>UnsupportedOperationException</code> if this
  1003. * method is not supported by this <code>ColorModel</code>
  1004. */
  1005. public int getDataElement(int[] components, int offset) {
  1006. throw new UnsupportedOperationException("This method is not supported "+
  1007. "by this color model.");
  1008. }
  1009. /**
  1010. * Returns a data element array representation of a pixel in this
  1011. * <code>ColorModel</code>, given an array of unnormalized color/alpha
  1012. * components. This array can then be passed to the
  1013. * <code>setDataElements</code> method of a <code>WritableRaster</code>
  1014. * object. An <code>ArrayIndexOutOfBoundsException</code> is thrown
  1015. * if the <code>components</code> array is not large enough to hold
  1016. * all the color and alpha components (starting at
  1017. * <code>offset</code>). If the <code>obj</code> variable is
  1018. * <code>null</code>, a new array will be allocated. If
  1019. * <code>obj</code> is not <code>null</code>, it must be a primitive
  1020. * array of type transferType; otherwise, a
  1021. * <code>ClassCastException</code> is thrown. An
  1022. * <code>ArrayIndexOutOfBoundsException</code> is thrown if
  1023. * <code>obj</code> is not large enough to hold a pixel value for this
  1024. * <code>ColorModel</code>.
  1025. * @param components an array of unnormalized color and alpha
  1026. * components
  1027. * @param offset the index into <code>components</code> at which to
  1028. * begin retrieving color and alpha components
  1029. * @param obj the <code>Object</code> representing an array of color
  1030. * and alpha components
  1031. * @return an <code>Object</code> representing an array of color and
  1032. * alpha components.
  1033. * @exception <code>ClassCastException</code> if <code>obj</code>
  1034. * is not a primitive array of type <code>transferType</code>
  1035. * @exception <code>ArrayIndexOutOfBoundsException</code> if
  1036. * <code>obj</code> is not large enough to hold a pixel value
  1037. * for this <code>ColorModel</code> or the <code>components</code>
  1038. * array is not large enough to hold all of the color and alpha
  1039. * components starting at <code>offset</code>
  1040. * @exception <code>UnsupportedOperationException</code> if this
  1041. * method is not supported by this <code>ColorModel</code>
  1042. * @see WritableRaster#setDataElements
  1043. * @see SampleModel#setDataElements
  1044. */
  1045. public Object getDataElements(int[] components, int offset, Object obj) {
  1046. throw new UnsupportedOperationException("This method has not been implemented "+
  1047. "for this color model.");
  1048. }
  1049. /**
  1050. * Tests if the specified <code>Object</code> is an instance of
  1051. * <code>ColorModel</code> and if it equals this
  1052. * <code>ColorModel</code>.
  1053. * @param obj the <code>Object</code> to test for equality
  1054. * @return <code>true</code> if the specified <code>Object</code>
  1055. * is an instance of <code>ColorModel</code> and equals this
  1056. * <code>ColorModel</code> <code>false</code> otherwise.
  1057. */
  1058. public boolean equals(Object obj) {
  1059. if (!(obj instanceof ColorModel)) {
  1060. return false;
  1061. }
  1062. ColorModel cm = (ColorModel) obj;
  1063. if (this == cm) {
  1064. return true;
  1065. }
  1066. if (supportsAlpha != cm.hasAlpha() ||
  1067. isAlphaPremultiplied != cm.isAlphaPremultiplied() ||
  1068. pixel_bits != cm.getPixelSize() ||
  1069. transparency != cm.getTransparency() ||
  1070. numComponents != cm.getNumComponents())
  1071. {
  1072. return false;
  1073. }
  1074. int i;
  1075. int[] nb = cm.getComponentSize();
  1076. for (i=0; i < numComponents; i++) {
  1077. if (nBits[i] != nb[i]) {
  1078. return false;
  1079. }
  1080. }
  1081. return true;
  1082. }
  1083. /**
  1084. * Returns the <code>ColorSpace</code> associated with this
  1085. * <code>ColorModel</code>.
  1086. * @return the <code>ColorSpace</code> of this
  1087. * <code>ColorModel</code>.
  1088. */
  1089. final public ColorSpace getColorSpace() {
  1090. return colorSpace;
  1091. }
  1092. /**
  1093. * Forces the raster data to match the state specified in the
  1094. * <code>isAlphaPremultiplied</code> variable, assuming the data is
  1095. * currently correctly described by this <code>ColorModel</code>. It
  1096. * may multiply or divide the color raster data by alpha, or do
  1097. * nothing if the data is in the correct state. If the data needs to
  1098. * be coerced, this method will also return an instance of this
  1099. * <code>ColorModel</code> with the <code>isAlphaPremultiplied</code>
  1100. * flag set appropriately. This method will throw a
  1101. * <code>UnsupportedOperationException</code> if it is not supported
  1102. * by this <code>ColorModel</code>.
  1103. * @param raster the <code>WritableRaster</code> data
  1104. * @param isAlphaPremultiplied <code>true</code> if the alpha is
  1105. * premultiplied; <code>false</code> otherwise
  1106. * @return a <code>ColorModel</code> object that represents the
  1107. * coerced data.
  1108. */
  1109. public ColorModel coerceData (WritableRaster raster,
  1110. boolean isAlphaPremultiplied) {
  1111. throw new UnsupportedOperationException
  1112. ("This method is not supported by this color model");
  1113. }
  1114. /**
  1115. * Returns <code>true</code> if <code>raster</code> is compatible
  1116. * with this <code>ColorModel</code> and <code>false</code> if it is
  1117. * not.
  1118. * @param raster the {@link Raster} object to test for compatibility
  1119. * @return <code>true</code> if <code>raster</code> is compatible
  1120. * with this <code>ColorModel</code>.
  1121. * @exception <code>UnsupportedOperationException</code> if this
  1122. * method has not been implemented for this
  1123. * <code>ColorModel</code>
  1124. */
  1125. public boolean isCompatibleRaster(Raster raster) {
  1126. throw new UnsupportedOperationException(
  1127. "This method has not been implemented for this ColorModel.");
  1128. }
  1129. /**
  1130. * Creates a <code>WritableRaster</code> with the specified width and
  1131. * height that has a data layout (<code>SampleModel</code>) compatible
  1132. * with this <code>ColorModel</code>.
  1133. * @param w the width to apply to the new <code>WritableRaster</code>
  1134. * @param h the height to apply to the new <code>WritableRaster</code>
  1135. * @return a <code>WritableRaster</code> object with the specified
  1136. * width and height.
  1137. * @exception <code>UnsupportedOperationException</code> if this
  1138. * method is not supported by this <code>ColorModel</code>
  1139. * @see WritableRaster
  1140. * @see SampleModel
  1141. */
  1142. public WritableRaster createCompatibleWritableRaster(int w, int h) {
  1143. throw new UnsupportedOperationException
  1144. ("This method is not supported by this color model");
  1145. }
  1146. /**
  1147. * Creates a <code>SampleModel</code> with the specified width and
  1148. * height that has a data layout compatible with this
  1149. * <code>ColorModel</code>.
  1150. * @param w the width to apply to the new <code>SampleModel</code>
  1151. * @param h the height to apply to the new <code>SampleModel</code>
  1152. * @return a <code>SampleModel</code> object with the specified
  1153. * width and height.
  1154. * @exception <code>UnsupportedOperationException</code> if this
  1155. * method is not supported by this <code>ColorModel</code>
  1156. * @see SampleModel
  1157. */
  1158. public SampleModel createCompatibleSampleModel(int w, int h) {
  1159. throw new UnsupportedOperationException
  1160. ("This method is not supported by this color model");
  1161. }
  1162. /** Checks if the <code>SampleModel</code> is compatible with this
  1163. * <code>ColorModel</code>.
  1164. * @param sm the specified <code>SampleModel</code>
  1165. * @return <code>true</code> if the specified <code>SampleModel</code>
  1166. * is compatible with this <code>ColorModel</code> <code>false</code>
  1167. * otherwise.
  1168. * @exception <code>UnsupportedOperationException</code> if this
  1169. * method is not supported by this <code>ColorModel</code>
  1170. * @see SampleModel
  1171. */
  1172. public boolean isCompatibleSampleModel(SampleModel sm) {
  1173. throw new UnsupportedOperationException
  1174. ("This method is not supported by this color model");
  1175. }
  1176. /**
  1177. * Disposes of system resources associated with this
  1178. * <code>ColorModel</code> once this <code>ColorModel</code> is no
  1179. * longer referenced.
  1180. */
  1181. public void finalize() {
  1182. }
  1183. /**
  1184. * Returns a <code>Raster</code> representing the alpha channel of an
  1185. * image, extracted from the input <code>Raster</code>, provided that
  1186. * pixel values of this <code>ColorModel</code> represent color and
  1187. * alpha information as separate spatial bands (e.g.
  1188. * {@link ComponentColorModel} and <code>DirectColorModel</code>).
  1189. * This method assumes that <code>Raster</code> objects associated
  1190. * with such a <code>ColorModel</code> store the alpha band, if
  1191. * present, as the last band of image data. Returns <code>null</code>
  1192. * if there is no separate spatial alpha channel associated with this
  1193. * <code>ColorModel</code>. If this is an
  1194. * <code>IndexColorModel</code> which has alpha in the lookup table,
  1195. * this method will return <code>null</code> since
  1196. * there is no spatially discrete alpha channel.
  1197. * This method will create a new <code>Raster</code> (but will share
  1198. * the data array).
  1199. * @param raster the specified <code>Raster</code>
  1200. * @return a <code>Raster</code> representing the alpha channel of
  1201. * an image, obtained from the specified <code>Raster</code>.
  1202. */
  1203. public WritableRaster getAlphaRaster(WritableRaster raster) {
  1204. return null;
  1205. }
  1206. /**
  1207. * Returns the <code>String</code> representation of the contents of
  1208. * this <code>ColorModel</code>object.
  1209. * @return a <code>String</code> representing the contents of this
  1210. * <code>ColorModel</code> object.
  1211. */
  1212. public String toString() {
  1213. return new String("ColorModel: #pixelBits = "+pixel_bits
  1214. + " numComponents = "+numComponents
  1215. + " color space = "+colorSpace
  1216. + " transparency = "+transparency
  1217. + " has alpha = "+supportsAlpha
  1218. + " isAlphaPre = "+isAlphaPremultiplied
  1219. );
  1220. }
  1221. static int getDefaultTransferType(int pixel_bits) {
  1222. if (pixel_bits <= 8) {
  1223. return DataBuffer.TYPE_BYTE;
  1224. } else if (pixel_bits <= 16) {
  1225. return DataBuffer.TYPE_USHORT;
  1226. } else if (pixel_bits <= 32) {
  1227. return DataBuffer.TYPE_INT;
  1228. } else {
  1229. return DataBuffer.TYPE_UNDEFINED;
  1230. }
  1231. }
  1232. }