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