1. /*
  2. * @(#)ComponentColorModel.java 1.68 03/12/19
  3. *
  4. * Copyright 2004 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.color.ColorSpace;
  9. import java.awt.color.ICC_ColorSpace;
  10. /**
  11. * A <CODE>ColorModel</CODE> class that works with pixel values that
  12. * represent color and alpha information as separate samples and that
  13. * store each sample in a separate data element. This class can be
  14. * used with an arbitrary <CODE>ColorSpace</CODE>. The number of
  15. * color samples in the pixel values must be same as the number of
  16. * color components in the <CODE>ColorSpace</CODE>. There may be a
  17. * single alpha sample.
  18. * <p>
  19. * For those methods that use
  20. * a primitive array pixel representation of type <CODE>transferType</CODE>,
  21. * the array length is the same as the number of color and alpha samples.
  22. * Color samples are stored first in the array followed by the alpha
  23. * sample, if present. The order of the color samples is specified
  24. * by the <CODE>ColorSpace</CODE>. Typically, this order reflects the
  25. * name of the color space type. For example, for <CODE>TYPE_RGB</CODE>,
  26. * index 0 corresponds to red, index 1 to green, and index 2 to blue.
  27. * <p>
  28. * The translation from pixel sample values to color/alpha components for
  29. * display or processing purposes is based on a one-to-one correspondence of
  30. * samples to components.
  31. * Depending on the transfer type used to create an instance of
  32. * <code>ComponentColorModel</code>, the pixel sample values
  33. * represented by that instance may be signed or unsigned and may
  34. * be of integral type or float or double (see below for details).
  35. * The translation from sample values to normalized color/alpha components
  36. * must follow certain rules. For float and double samples, the translation
  37. * is an identity, i.e. normalized component values are equal to the
  38. * corresponding sample values. For integral samples, the translation
  39. * should be only a simple scale and offset, where the scale and offset
  40. * constants may be different for each component. The result of
  41. * applying the scale and offset constants is a set of color/alpha
  42. * component values, which are guaranteed to fall within a certain
  43. * range. Typically, the range for a color component will be the range
  44. * defined by the <code>getMinValue</code> and <code>getMaxValue</code>
  45. * methods of the <code>ColorSpace</code> class. The range for an
  46. * alpha component should be 0.0 to 1.0.
  47. * <p>
  48. * Instances of <code>ComponentColorModel</code> created with transfer types
  49. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  50. * and <CODE>DataBuffer.TYPE_INT</CODE> have pixel sample values which
  51. * are treated as unsigned integral values.
  52. * The number of bits in a color or alpha sample of a pixel value might not
  53. * be the same as the number of bits for the corresponding color or alpha
  54. * sample passed to the
  55. * <code>ComponentColorModel(ColorSpace, int[], boolean, boolean, int, int)</code>
  56. * constructor. In
  57. * that case, this class assumes that the least significant n bits of a sample
  58. * value hold the component value, where n is the number of significant bits
  59. * for the component passed to the constructor. It also assumes that
  60. * any higher-order bits in a sample value are zero. Thus, sample values
  61. * range from 0 to 2<sup>n</sup> - 1. This class maps these sample values
  62. * to normalized color component values such that 0 maps to the value
  63. * obtained from the <code>ColorSpace's</code> <code>getMinValue</code>
  64. * method for each component and 2<sup>n</sup> - 1 maps to the value
  65. * obtained from <code>getMaxValue</code>. To create a
  66. * <code>ComponentColorModel</code> with a different color sample mapping
  67. * requires subclassing this class and overriding the
  68. * <code>getNormalizedComponents(Object, float[], int)</code> method.
  69. * The mapping for an alpha sample always maps 0 to 0.0 and
  70. * 2<sup>n</sup> - 1 to 1.0.
  71. * <p>
  72. * For instances with unsigned sample values,
  73. * the unnormalized color/alpha component representation is only
  74. * supported if two conditions hold. First, sample value value 0 must
  75. * map to normalized component value 0.0 and sample value 2<sup>n</sup> - 1
  76. * to 1.0. Second the min/max range of all color components of the
  77. * <code>ColorSpace</code> must be 0.0 to 1.0. In this case, the
  78. * component representation is the n least
  79. * significant bits of the corresponding sample. Thus each component is
  80. * an unsigned integral value between 0 and 2<sup>n</sup> - 1, where
  81. * n is the number of significant bits for a particular component.
  82. * If these conditions are not met, any method taking an unnormalized
  83. * component argument will throw an <code>IllegalArgumentException</code>.
  84. * <p>
  85. * Instances of <code>ComponentColorModel</code> created with transfer types
  86. * <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>, and
  87. * <CODE>DataBuffer.TYPE_DOUBLE</CODE> have pixel sample values which
  88. * are treated as signed short, float, or double values.
  89. * Such instances do not support the unnormalized color/alpha component
  90. * representation, so any methods taking such a representation as an argument
  91. * will throw an <code>IllegalArgumentException</code> when called on one
  92. * of these instances. The normalized component values of instances
  93. * of this class have a range which depends on the transfer
  94. * type as follows: for float samples, the full range of the float data
  95. * type; for double samples, the full range of the float data type
  96. * (resulting from casting double to float); for short samples,
  97. * from approximately -maxVal to +maxVal, where maxVal is the per
  98. * component maximum value for the <code>ColorSpace</code>
  99. * (-32767 maps to -maxVal, 0 maps to 0.0, and 32767 maps
  100. * to +maxVal). A subclass may override the scaling for short sample
  101. * values to normalized component values by overriding the
  102. * <code>getNormalizedComponents(Object, float[], int)</code> method.
  103. * For float and double samples, the normalized component values are
  104. * taken to be equal to the corresponding sample values, and subclasses
  105. * should not attempt to add any non-identity scaling for these transfer
  106. * types.
  107. * <p>
  108. * Instances of <code>ComponentColorModel</code> created with transfer types
  109. * <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>, and
  110. * <CODE>DataBuffer.TYPE_DOUBLE</CODE>
  111. * use all the bits of all sample values. Thus all color/alpha components
  112. * have 16 bits when using <CODE>DataBuffer.TYPE_SHORT</CODE>, 32 bits when
  113. * using <CODE>DataBuffer.TYPE_FLOAT</CODE>, and 64 bits when using
  114. * <CODE>DataBuffer.TYPE_DOUBLE</CODE>. When the
  115. * <code>ComponentColorModel(ColorSpace, int[], boolean, boolean, int, int)</code>
  116. * form of constructor is used with one of these transfer types, the
  117. * bits array argument is ignored.
  118. * <p>
  119. * It is possible to have color/alpha sample values
  120. * which cannot be reasonably interpreted as component values for rendering.
  121. * This can happen when <code>ComponentColorModel</code> is subclassed to
  122. * override the mapping of unsigned sample values to normalized color
  123. * component values or when signed sample values outside a certain range
  124. * are used. (As an example, specifying an alpha component as a signed
  125. * short value outside the range 0 to 32767, normalized range 0.0 to 1.0, can
  126. * lead to unexpected results.) It is the
  127. * responsibility of applications to appropriately scale pixel data before
  128. * rendering such that color components fall within the normalized range
  129. * of the <code>ColorSpace</code> (obtained using the <code>getMinValue</code>
  130. * and <code>getMaxValue</code> methods of the <code>ColorSpace</code> class)
  131. * and the alpha component is between 0.0 and 1.0. If color or alpha
  132. * component values fall outside these ranges, rendering results are
  133. * indeterminate.
  134. * <p>
  135. * Methods that use a single int pixel representation throw
  136. * an <CODE>IllegalArgumentException</CODE>, unless the number of components
  137. * for the <CODE>ComponentColorModel</CODE> is one and the component
  138. * value is unsigned -- in other words, a single color component using
  139. * a transfer type of <CODE>DataBuffer.TYPE_BYTE</CODE>,
  140. * <CODE>DataBuffer.TYPE_USHORT</CODE>, or <CODE>DataBuffer.TYPE_INT</CODE>
  141. * and no alpha.
  142. * <p>
  143. * A <CODE>ComponentColorModel</CODE> can be used in conjunction with a
  144. * <CODE>ComponentSampleModel</CODE>, a <CODE>BandedSampleModel</CODE>,
  145. * or a <CODE>PixelInterleavedSampleModel</CODE> to construct a
  146. * <CODE>BufferedImage</CODE>.
  147. *
  148. * @see ColorModel
  149. * @see ColorSpace
  150. * @see ComponentSampleModel
  151. * @see BandedSampleModel
  152. * @see PixelInterleavedSampleModel
  153. * @see BufferedImage
  154. *
  155. * @version 10 Feb 1997
  156. */
  157. public class ComponentColorModel extends ColorModel {
  158. /**
  159. * <code>signed</code> is <code>true</code> for <code>short</code>,
  160. * <code>float</code>, and <code>double</code> transfer types; it
  161. * is <code>false</code> for <code>byte</code>, <code>ushort</code>,
  162. * and <code>int</code> transfer types.
  163. */
  164. private boolean signed; // true for transfer types short, float, double
  165. // false for byte, ushort, int
  166. private boolean is_sRGB_stdScale;
  167. private boolean is_LinearRGB_stdScale;
  168. private boolean is_LinearGray_stdScale;
  169. private boolean is_ICCGray_stdScale;
  170. private byte[] tosRGB8LUT;
  171. private byte[] fromsRGB8LUT8;
  172. private short[] fromsRGB8LUT16;
  173. private byte[] fromLinearGray16ToOtherGray8LUT;
  174. private short[] fromLinearGray16ToOtherGray16LUT;
  175. private boolean needScaleInit;
  176. private boolean noUnnorm;
  177. private boolean nonStdScale;
  178. private float[] min;
  179. private float[] diffMinMax;
  180. private float[] compOffset;
  181. private float[] compScale;
  182. /**
  183. * Constructs a <CODE>ComponentColorModel</CODE> from the specified
  184. * parameters. Color components will be in the specified
  185. * <CODE>ColorSpace</CODE>. The supported transfer types are
  186. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  187. * <CODE>DataBuffer.TYPE_INT</CODE>,
  188. * <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>,
  189. * and <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  190. * If not null, the <CODE>bits</CODE> array specifies the
  191. * number of significant bits per color and alpha component and its
  192. * length should be at least the number of components in the
  193. * <CODE>ColorSpace</CODE> if there is no alpha
  194. * information in the pixel values, or one more than this number if
  195. * there is alpha information. When the <CODE>transferType</CODE> is
  196. * <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>,
  197. * or <CODE>DataBuffer.TYPE_DOUBLE</CODE> the <CODE>bits</CODE> array
  198. * argument is ignored. <CODE>hasAlpha</CODE> indicates whether alpha
  199. * information is present. If <CODE>hasAlpha</CODE> is true, then
  200. * the boolean <CODE>isAlphaPremultiplied</CODE>
  201. * specifies how to interpret color and alpha samples in pixel values.
  202. * If the boolean is true, color samples are assumed to have been
  203. * multiplied by the alpha sample. The <CODE>transparency</CODE>
  204. * specifies what alpha values can be represented by this color model.
  205. * The acceptable <code>transparency</code> values are
  206. * <CODE>OPAQUE</CODE>, <CODE>BITMASK</CODE> or <CODE>TRANSLUCENT</CODE>.
  207. * The <CODE>transferType</CODE> is the type of primitive array used
  208. * to represent pixel values.
  209. *
  210. * @param colorSpace The <CODE>ColorSpace</CODE> associated
  211. * with this color model.
  212. * @param bits The number of significant bits per component.
  213. * May be null, in which case all bits of all
  214. * component samples will be significant.
  215. * Ignored if transferType is one of
  216. * <CODE>DataBuffer.TYPE_SHORT</CODE>,
  217. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or
  218. * <CODE>DataBuffer.TYPE_DOUBLE</CODE>,
  219. * in which case all bits of all component
  220. * samples will be significant.
  221. * @param hasAlpha If true, this color model supports alpha.
  222. * @param isAlphaPremultiplied If true, alpha is premultiplied.
  223. * @param transparency Specifies what alpha values can be represented
  224. * by this color model.
  225. * @param transferType Specifies the type of primitive array used to
  226. * represent pixel values.
  227. *
  228. * @throws IllegalArgumentException If the <CODE>bits</CODE> array
  229. * argument is not null, its length is less than the number of
  230. * color and alpha components, and transferType is one of
  231. * <CODE>DataBuffer.TYPE_BYTE</CODE>,
  232. * <CODE>DataBuffer.TYPE_USHORT</CODE>, or
  233. * <CODE>DataBuffer.TYPE_INT</CODE>.
  234. * @throws IllegalArgumentException If transferType is not one of
  235. * <CODE>DataBuffer.TYPE_BYTE</CODE>,
  236. * <CODE>DataBuffer.TYPE_USHORT</CODE>,
  237. * <CODE>DataBuffer.TYPE_INT</CODE>,
  238. * <CODE>DataBuffer.TYPE_SHORT</CODE>,
  239. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or
  240. * <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  241. *
  242. * @see ColorSpace
  243. * @see java.awt.Transparency
  244. */
  245. public ComponentColorModel (ColorSpace colorSpace,
  246. int[] bits,
  247. boolean hasAlpha,
  248. boolean isAlphaPremultiplied,
  249. int transparency,
  250. int transferType) {
  251. super (bitsHelper(transferType, colorSpace, hasAlpha),
  252. bitsArrayHelper(bits, transferType, colorSpace, hasAlpha),
  253. colorSpace, hasAlpha, isAlphaPremultiplied, transparency,
  254. transferType);
  255. switch(transferType) {
  256. case DataBuffer.TYPE_BYTE:
  257. case DataBuffer.TYPE_USHORT:
  258. case DataBuffer.TYPE_INT:
  259. signed = false;
  260. needScaleInit = true;
  261. break;
  262. case DataBuffer.TYPE_SHORT:
  263. signed = true;
  264. needScaleInit = true;
  265. break;
  266. case DataBuffer.TYPE_FLOAT:
  267. case DataBuffer.TYPE_DOUBLE:
  268. signed = true;
  269. needScaleInit = false;
  270. noUnnorm = true;
  271. nonStdScale = false;
  272. break;
  273. default:
  274. throw new IllegalArgumentException("This constructor is not "+
  275. "compatible with transferType " + transferType);
  276. }
  277. setupLUTs();
  278. }
  279. /**
  280. * Constructs a <CODE>ComponentColorModel</CODE> from the specified
  281. * parameters. Color components will be in the specified
  282. * <CODE>ColorSpace</CODE>. The supported transfer types are
  283. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  284. * <CODE>DataBuffer.TYPE_INT</CODE>,
  285. * <CODE>DataBuffer.TYPE_SHORT</CODE>, <CODE>DataBuffer.TYPE_FLOAT</CODE>,
  286. * and <CODE>DataBuffer.TYPE_DOUBLE</CODE>. The number of significant
  287. * bits per color and alpha component will be 8, 16, 32, 16, 32, or 64,
  288. * respectively. The number of color components will be the
  289. * number of components in the <CODE>ColorSpace</CODE>. There will be
  290. * an alpha component if <CODE>hasAlpha</CODE> is <CODE>true</CODE>.
  291. * If <CODE>hasAlpha</CODE> is true, then
  292. * the boolean <CODE>isAlphaPremultiplied</CODE>
  293. * specifies how to interpret color and alpha samples in pixel values.
  294. * If the boolean is true, color samples are assumed to have been
  295. * multiplied by the alpha sample. The <CODE>transparency</CODE>
  296. * specifies what alpha values can be represented by this color model.
  297. * The acceptable <code>transparency</code> values are
  298. * <CODE>OPAQUE</CODE>, <CODE>BITMASK</CODE> or <CODE>TRANSLUCENT</CODE>.
  299. * The <CODE>transferType</CODE> is the type of primitive array used
  300. * to represent pixel values.
  301. *
  302. * @param colorSpace The <CODE>ColorSpace</CODE> associated
  303. * with this color model.
  304. * @param hasAlpha If true, this color model supports alpha.
  305. * @param isAlphaPremultiplied If true, alpha is premultiplied.
  306. * @param transparency Specifies what alpha values can be represented
  307. * by this color model.
  308. * @param transferType Specifies the type of primitive array used to
  309. * represent pixel values.
  310. *
  311. * @throws IllegalArgumentException If transferType is not one of
  312. * <CODE>DataBuffer.TYPE_BYTE</CODE>,
  313. * <CODE>DataBuffer.TYPE_USHORT</CODE>,
  314. * <CODE>DataBuffer.TYPE_INT</CODE>,
  315. * <CODE>DataBuffer.TYPE_SHORT</CODE>,
  316. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or
  317. * <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  318. *
  319. * @see ColorSpace
  320. * @see java.awt.Transparency
  321. * @since 1.4
  322. */
  323. public ComponentColorModel (ColorSpace colorSpace,
  324. boolean hasAlpha,
  325. boolean isAlphaPremultiplied,
  326. int transparency,
  327. int transferType) {
  328. this(colorSpace, null, hasAlpha, isAlphaPremultiplied,
  329. transparency, transferType);
  330. }
  331. private static int bitsHelper(int transferType,
  332. ColorSpace colorSpace,
  333. boolean hasAlpha) {
  334. int numBits = DataBuffer.getDataTypeSize(transferType);
  335. int numComponents = colorSpace.getNumComponents();
  336. if (hasAlpha) {
  337. ++numComponents;
  338. }
  339. return numBits * numComponents;
  340. }
  341. private static int[] bitsArrayHelper(int[] origBits,
  342. int transferType,
  343. ColorSpace colorSpace,
  344. boolean hasAlpha) {
  345. switch(transferType) {
  346. case DataBuffer.TYPE_BYTE:
  347. case DataBuffer.TYPE_USHORT:
  348. case DataBuffer.TYPE_INT:
  349. if (origBits != null) {
  350. return origBits;
  351. }
  352. break;
  353. default:
  354. break;
  355. }
  356. int numBits = DataBuffer.getDataTypeSize(transferType);
  357. int numComponents = colorSpace.getNumComponents();
  358. if (hasAlpha) {
  359. ++numComponents;
  360. }
  361. int[] bits = new int[numComponents];
  362. for (int i = 0; i < numComponents; i++) {
  363. bits[i] = numBits;
  364. }
  365. return bits;
  366. }
  367. private void setupLUTs() {
  368. // REMIND: there is potential to accelerate sRGB, LinearRGB,
  369. // LinearGray, ICCGray, and non-ICC Gray spaces with non-standard
  370. // scaling, if that becomes important
  371. //
  372. // NOTE: The is_xxx_stdScale and nonStdScale booleans are provisionally
  373. // set here when this method is called at construction time. These
  374. // variables may be set again when initScale is called later.
  375. // When setupLUTs returns, nonStdScale is true if (the transferType
  376. // is not float or double) AND (some minimum ColorSpace component
  377. // value is not 0.0 OR some maximum ColorSpace component value
  378. // is not 1.0). This is correct for the calls to
  379. // getNormalizedComponents(Object, float[], int) from initScale().
  380. // initScale() may change the value nonStdScale based on the
  381. // return value of getNormalizedComponents() - this will only
  382. // happen if getNormalizedComponents() has been overridden by a
  383. // subclass to make the mapping of min/max pixel sample values
  384. // something different from min/max color component values.
  385. if (is_sRGB) {
  386. is_sRGB_stdScale = true;
  387. nonStdScale = false;
  388. } else if (ColorModel.isLinearRGBspace(colorSpace)) {
  389. // Note that the built-in Linear RGB space has a normalized
  390. // range of 0.0 - 1.0 for each coordinate. Usage of these
  391. // LUTs makes that assumption.
  392. is_LinearRGB_stdScale = true;
  393. nonStdScale = false;
  394. if (transferType == DataBuffer.TYPE_BYTE) {
  395. tosRGB8LUT = ColorModel.getLinearRGB8TosRGB8LUT();
  396. fromsRGB8LUT8 = ColorModel.getsRGB8ToLinearRGB8LUT();
  397. } else {
  398. tosRGB8LUT = ColorModel.getLinearRGB16TosRGB8LUT();
  399. fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT();
  400. }
  401. } else if ((colorSpaceType == ColorSpace.TYPE_GRAY) &&
  402. (colorSpace instanceof ICC_ColorSpace) &&
  403. (colorSpace.getMinValue(0) == 0.0f) &&
  404. (colorSpace.getMaxValue(0) == 1.0f)) {
  405. // Note that a normalized range of 0.0 - 1.0 for the gray
  406. // component is required, because usage of these LUTs makes
  407. // that assumption.
  408. ICC_ColorSpace ics = (ICC_ColorSpace) colorSpace;
  409. is_ICCGray_stdScale = true;
  410. nonStdScale = false;
  411. fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT();
  412. if (ColorModel.isLinearGRAYspace(ics)) {
  413. is_LinearGray_stdScale = true;
  414. if (transferType == DataBuffer.TYPE_BYTE) {
  415. tosRGB8LUT = ColorModel.getGray8TosRGB8LUT(ics);
  416. } else {
  417. tosRGB8LUT = ColorModel.getGray16TosRGB8LUT(ics);
  418. }
  419. } else {
  420. if (transferType == DataBuffer.TYPE_BYTE) {
  421. tosRGB8LUT = ColorModel.getGray8TosRGB8LUT(ics);
  422. fromLinearGray16ToOtherGray8LUT =
  423. ColorModel.getLinearGray16ToOtherGray8LUT(ics);
  424. } else {
  425. tosRGB8LUT = ColorModel.getGray16TosRGB8LUT(ics);
  426. fromLinearGray16ToOtherGray16LUT =
  427. ColorModel.getLinearGray16ToOtherGray16LUT(ics);
  428. }
  429. }
  430. } else if (needScaleInit) {
  431. // if transferType is byte, ushort, int, or short and we
  432. // don't already know the ColorSpace has minVlaue == 0.0f and
  433. // maxValue == 1.0f for all components, we need to check that
  434. // now and setup the min[] and diffMinMax[] arrays if necessary.
  435. nonStdScale = false;
  436. for (int i = 0; i < numColorComponents; i++) {
  437. if ((colorSpace.getMinValue(i) != 0.0f) ||
  438. (colorSpace.getMaxValue(i) != 1.0f)) {
  439. nonStdScale = true;
  440. break;
  441. }
  442. }
  443. if (nonStdScale) {
  444. min = new float[numColorComponents];
  445. diffMinMax = new float[numColorComponents];
  446. for (int i = 0; i < numColorComponents; i++) {
  447. min[i] = colorSpace.getMinValue(i);
  448. diffMinMax[i] = colorSpace.getMaxValue(i) - min[i];
  449. }
  450. }
  451. }
  452. }
  453. private void initScale() {
  454. // This method is called the first time any method which uses
  455. // pixel sample value to color component value scaling information
  456. // is called if the transferType supports non-standard scaling
  457. // as defined above (byte, ushort, int, and short), unless the
  458. // method is getNormalizedComponents(Object, float[], int) (that
  459. // method must be overridden to use non-standard scaling). This
  460. // method also sets up the noUnnorm boolean variable for these
  461. // transferTypes. After this method is called, the nonStdScale
  462. // variable will be true if getNormalizedComponents() maps a
  463. // sample value of 0 to anything other than 0.0f OR maps a
  464. // sample value of 2^^n - 1 (2^^15 - 1 for short transferType)
  465. // to anything other than 1.0f. Note that this can be independent
  466. // of the colorSpace min/max component values, if the
  467. // getNormalizedComponents() method has been overridden for some
  468. // reason, e.g. to provide greater dynamic range in the sample
  469. // values than in the color component values. Unfortunately,
  470. // this method can't be called at construction time, since a
  471. // subclass may still have uninitialized state that would cause
  472. // getNormalizedComponents() to return an incorrect result.
  473. needScaleInit = false; // only needs to called once
  474. if (nonStdScale || signed) {
  475. // The unnormalized form is only supported for unsigned
  476. // transferTypes and when the ColorSpace min/max values
  477. // are 0.0/1.0. When this method is called nonStdScale is
  478. // true if the latter condition does not hold. In addition,
  479. // the unnormalized form requires that the full range of
  480. // the pixel sample values map to the full 0.0 - 1.0 range
  481. // of color component values. That condition is checked
  482. // later in this method.
  483. noUnnorm = true;
  484. } else {
  485. noUnnorm = false;
  486. }
  487. float[] lowVal, highVal;
  488. switch (transferType) {
  489. case DataBuffer.TYPE_BYTE:
  490. {
  491. byte[] bpixel = new byte[numComponents];
  492. for (int i = 0; i < numColorComponents; i++) {
  493. bpixel[i] = 0;
  494. }
  495. if (supportsAlpha) {
  496. bpixel[numColorComponents] =
  497. (byte) ((1 << nBits[numColorComponents]) - 1);
  498. }
  499. lowVal = getNormalizedComponents(bpixel, null, 0);
  500. for (int i = 0; i < numColorComponents; i++) {
  501. bpixel[i] = (byte) ((1 << nBits[i]) - 1);
  502. }
  503. highVal = getNormalizedComponents(bpixel, null, 0);
  504. }
  505. break;
  506. case DataBuffer.TYPE_USHORT:
  507. {
  508. short[] uspixel = new short[numComponents];
  509. for (int i = 0; i < numColorComponents; i++) {
  510. uspixel[i] = 0;
  511. }
  512. if (supportsAlpha) {
  513. uspixel[numColorComponents] =
  514. (short) ((1 << nBits[numColorComponents]) - 1);
  515. }
  516. lowVal = getNormalizedComponents(uspixel, null, 0);
  517. for (int i = 0; i < numColorComponents; i++) {
  518. uspixel[i] = (short) ((1 << nBits[i]) - 1);
  519. }
  520. highVal = getNormalizedComponents(uspixel, null, 0);
  521. }
  522. break;
  523. case DataBuffer.TYPE_INT:
  524. {
  525. int[] ipixel = new int[numComponents];
  526. for (int i = 0; i < numColorComponents; i++) {
  527. ipixel[i] = 0;
  528. }
  529. if (supportsAlpha) {
  530. ipixel[numColorComponents] =
  531. ((1 << nBits[numColorComponents]) - 1);
  532. }
  533. lowVal = getNormalizedComponents(ipixel, null, 0);
  534. for (int i = 0; i < numColorComponents; i++) {
  535. ipixel[i] = ((1 << nBits[i]) - 1);
  536. }
  537. highVal = getNormalizedComponents(ipixel, null, 0);
  538. }
  539. break;
  540. case DataBuffer.TYPE_SHORT:
  541. {
  542. short[] spixel = new short[numComponents];
  543. for (int i = 0; i < numColorComponents; i++) {
  544. spixel[i] = 0;
  545. }
  546. if (supportsAlpha) {
  547. spixel[numColorComponents] = 32767;
  548. }
  549. lowVal = getNormalizedComponents(spixel, null, 0);
  550. for (int i = 0; i < numColorComponents; i++) {
  551. spixel[i] = 32767;
  552. }
  553. highVal = getNormalizedComponents(spixel, null, 0);
  554. }
  555. break;
  556. default:
  557. lowVal = highVal = null; // to keep the compiler from complaining
  558. break;
  559. }
  560. nonStdScale = false;
  561. for (int i = 0; i < numColorComponents; i++) {
  562. if ((lowVal[i] != 0.0f) || (highVal[i] != 1.0f)) {
  563. nonStdScale = true;
  564. break;
  565. }
  566. }
  567. if (nonStdScale) {
  568. noUnnorm = true;
  569. is_sRGB_stdScale = false;
  570. is_LinearRGB_stdScale = false;
  571. is_LinearGray_stdScale = false;
  572. is_ICCGray_stdScale = false;
  573. compOffset = new float[numColorComponents];
  574. compScale = new float[numColorComponents];
  575. for (int i = 0; i < numColorComponents; i++) {
  576. compOffset[i] = lowVal[i];
  577. compScale[i] = 1.0f / (highVal[i] - lowVal[i]);
  578. }
  579. }
  580. }
  581. private int getRGBComponent(int pixel, int idx) {
  582. if (numComponents > 1) {
  583. throw new
  584. IllegalArgumentException("More than one component per pixel");
  585. }
  586. if (signed) {
  587. throw new
  588. IllegalArgumentException("Component value is signed");
  589. }
  590. if (needScaleInit) {
  591. initScale();
  592. }
  593. // Since there is only 1 component, there is no alpha
  594. // Normalize the pixel in order to convert it
  595. Object opixel = null;
  596. switch (transferType) {
  597. case DataBuffer.TYPE_BYTE:
  598. {
  599. byte[] bpixel = { (byte) pixel };
  600. opixel = bpixel;
  601. }
  602. break;
  603. case DataBuffer.TYPE_USHORT:
  604. {
  605. short[] spixel = { (short) pixel };
  606. opixel = spixel;
  607. }
  608. break;
  609. case DataBuffer.TYPE_INT:
  610. {
  611. int[] ipixel = { pixel };
  612. opixel = ipixel;
  613. }
  614. break;
  615. }
  616. float[] norm = getNormalizedComponents(opixel, null, 0);
  617. float[] rgb = colorSpace.toRGB(norm);
  618. return (int) (rgb[idx] * 255.0f + 0.5f);
  619. }
  620. /**
  621. * Returns the red color component for the specified pixel, scaled
  622. * from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
  623. * is done if necessary. The pixel value is specified as an int.
  624. * The returned value will be a non pre-multiplied value.
  625. * If the alpha is premultiplied, this method divides
  626. * it out before returning the value (if the alpha value is 0,
  627. * the red value will be 0).
  628. *
  629. * @param pixel The pixel from which you want to get the red color component.
  630. *
  631. * @return The red color component for the specified pixel, as an int.
  632. *
  633. * @throws IllegalArgumentException If there is more than
  634. * one component in this <CODE>ColorModel</CODE>.
  635. * @throws IllegalArgumentException If the component value for this
  636. * <CODE>ColorModel</CODE> is signed
  637. */
  638. public int getRed(int pixel) {
  639. return getRGBComponent(pixel, 0);
  640. }
  641. /**
  642. * Returns the green color component for the specified pixel, scaled
  643. * from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
  644. * is done if necessary. The pixel value is specified as an int.
  645. * The returned value will be a non
  646. * pre-multiplied value. If the alpha is premultiplied, this method
  647. * divides it out before returning the value (if the alpha value is 0,
  648. * the green value will be 0).
  649. *
  650. * @param pixel The pixel from which you want to get the green color component.
  651. *
  652. * @return The green color component for the specified pixel, as an int.
  653. *
  654. * @throws IllegalArgumentException If there is more than
  655. * one component in this <CODE>ColorModel</CODE>.
  656. * @throws IllegalArgumentException If the component value for this
  657. * <CODE>ColorModel</CODE> is signed
  658. */
  659. public int getGreen(int pixel) {
  660. return getRGBComponent(pixel, 1);
  661. }
  662. /**
  663. * Returns the blue color component for the specified pixel, scaled
  664. * from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
  665. * is done if necessary. The pixel value is specified as an int.
  666. * The returned value will be a non
  667. * pre-multiplied value. If the alpha is premultiplied, this method
  668. * divides it out before returning the value (if the alpha value is 0,
  669. * the blue value will be 0).
  670. *
  671. * @param pixel The pixel from which you want to get the blue color component.
  672. *
  673. * @return The blue color component for the specified pixel, as an int.
  674. *
  675. * @throws IllegalArgumentException If there is more than
  676. * one component in this <CODE>ColorModel</CODE>.
  677. * @throws IllegalArgumentException If the component value for this
  678. * <CODE>ColorModel</CODE> is signed
  679. */
  680. public int getBlue(int pixel) {
  681. return getRGBComponent(pixel, 2);
  682. }
  683. /**
  684. * Returns the alpha component for the specified pixel, scaled
  685. * from 0 to 255. The pixel value is specified as an int.
  686. *
  687. * @param pixel The pixel from which you want to get the alpha component.
  688. *
  689. * @return The alpha component for the specified pixel, as an int.
  690. *
  691. * @throws IllegalArgumentException If there is more than
  692. * one component in this <CODE>ColorModel</CODE>.
  693. * @throws IllegalArgumentException If the component value for this
  694. * <CODE>ColorModel</CODE> is signed
  695. */
  696. public int getAlpha(int pixel) {
  697. if (supportsAlpha == false) {
  698. return 255;
  699. }
  700. if (numComponents > 1) {
  701. throw new
  702. IllegalArgumentException("More than one component per pixel");
  703. }
  704. if (signed) {
  705. throw new
  706. IllegalArgumentException("Component value is signed");
  707. }
  708. return (int) ((((float) pixel) / ((1<<nBits[0])-1)) * 255.0f + 0.5f);
  709. }
  710. /**
  711. * Returns the color/alpha components of the pixel in the default
  712. * RGB color model format. A color conversion is done if necessary.
  713. * The returned value will be in a non pre-multiplied format. If
  714. * the alpha is premultiplied, this method divides it out of the
  715. * color components (if the alpha value is 0, the color values will be 0).
  716. *
  717. * @param pixel The pixel from which you want to get the color/alpha components.
  718. *
  719. * @return The color/alpha components for the specified pixel, as an int.
  720. *
  721. * @throws IllegalArgumentException If there is more than
  722. * one component in this <CODE>ColorModel</CODE>.
  723. * @throws IllegalArgumentException If the component value for this
  724. * <CODE>ColorModel</CODE> is signed
  725. */
  726. public int getRGB(int pixel) {
  727. if (numComponents > 1) {
  728. throw new
  729. IllegalArgumentException("More than one component per pixel");
  730. }
  731. if (signed) {
  732. throw new
  733. IllegalArgumentException("Component value is signed");
  734. }
  735. return (getAlpha(pixel) << 24)
  736. | (getRed(pixel) << 16)
  737. | (getGreen(pixel) << 8)
  738. | (getBlue(pixel) << 0);
  739. }
  740. private int extractComponent(Object inData, int idx, int precision) {
  741. // Extract component idx from inData. The precision argument
  742. // should be either 8 or 16. If it's 8, this method will return
  743. // an 8-bit value. If it's 16, this method will return a 16-bit
  744. // value for transferTypes other than TYPE_BYTE. For TYPE_BYTE,
  745. // an 8-bit value will be returned.
  746. // This method maps the input value corresponding to a
  747. // normalized ColorSpace component value of 0.0 to 0, and the
  748. // input value corresponding to a normalized ColorSpace
  749. // component value of 1.0 to 2^n - 1 (where n is 8 or 16), so
  750. // it is appropriate only for ColorSpaces with min/max component
  751. // values of 0.0/1.0. This will be true for sRGB, the built-in
  752. // Linear RGB and Linear Gray spaces, and any other ICC grayscale
  753. // spaces for which we have precomputed LUTs.
  754. boolean needAlpha = (supportsAlpha && isAlphaPremultiplied);
  755. int alp = 0;
  756. int comp;
  757. int mask = (1 << nBits[idx]) - 1;
  758. switch (transferType) {
  759. // Note: we do no clamping of the pixel data here - we
  760. // assume that the data is scaled properly
  761. case DataBuffer.TYPE_SHORT: {
  762. short sdata[] = (short[]) inData;
  763. float scalefactor = (float) ((1 << precision) - 1);
  764. if (needAlpha) {
  765. short s = sdata[numColorComponents];
  766. if (s != (short) 0) {
  767. return (int) ((((float) sdata[idx]) /
  768. ((float) s)) * scalefactor + 0.5f);
  769. } else {
  770. return 0;
  771. }
  772. } else {
  773. return (int) ((sdata[idx] / 32767.0f) * scalefactor + 0.5f);
  774. }
  775. }
  776. case DataBuffer.TYPE_FLOAT: {
  777. float fdata[] = (float[]) inData;
  778. float scalefactor = (float) ((1 << precision) - 1);
  779. if (needAlpha) {
  780. float f = fdata[numColorComponents];
  781. if (f != 0.0f) {
  782. return (int) (((fdata[idx] / f) * scalefactor) + 0.5f);
  783. } else {
  784. return 0;
  785. }
  786. } else {
  787. return (int) (fdata[idx] * scalefactor + 0.5f);
  788. }
  789. }
  790. case DataBuffer.TYPE_DOUBLE: {
  791. double ddata[] = (double[]) inData;
  792. double scalefactor = (double) ((1 << precision) - 1);
  793. if (needAlpha) {
  794. double d = ddata[numColorComponents];
  795. if (d != 0.0) {
  796. return (int) (((ddata[idx] / d) * scalefactor) + 0.5);
  797. } else {
  798. return 0;
  799. }
  800. } else {
  801. return (int) (ddata[idx] * scalefactor + 0.5);
  802. }
  803. }
  804. case DataBuffer.TYPE_BYTE:
  805. byte bdata[] = (byte[])inData;
  806. comp = bdata[idx] & mask;
  807. precision = 8;
  808. if (needAlpha) {
  809. alp = bdata[numColorComponents] & mask;
  810. }
  811. break;
  812. case DataBuffer.TYPE_USHORT:
  813. short usdata[] = (short[])inData;
  814. comp = usdata[idx] & mask;
  815. if (needAlpha) {
  816. alp = usdata[numColorComponents] & mask;
  817. }
  818. break;
  819. case DataBuffer.TYPE_INT:
  820. int idata[] = (int[])inData;
  821. comp = idata[idx];
  822. if (needAlpha) {
  823. alp = idata[numColorComponents];
  824. }
  825. break;
  826. default:
  827. throw new
  828. UnsupportedOperationException("This method has not "+
  829. "been implemented for transferType " + transferType);
  830. }
  831. if (needAlpha) {
  832. if (alp != 0) {
  833. float scalefactor = (float) ((1 << precision) - 1);
  834. float fcomp = ((float) comp) / ((float)mask);
  835. float invalp = ((float) ((1<<nBits[numColorComponents]) - 1)) /
  836. ((float) alp);
  837. return (int) (fcomp * invalp * scalefactor + 0.5f);
  838. } else {
  839. return 0;
  840. }
  841. } else {
  842. if (nBits[idx] != precision) {
  843. float scalefactor = (float) ((1 << precision) - 1);
  844. float fcomp = ((float) comp) / ((float)mask);
  845. return (int) (fcomp * scalefactor + 0.5f);
  846. }
  847. return comp;
  848. }
  849. }
  850. private int getRGBComponent(Object inData, int idx) {
  851. if (needScaleInit) {
  852. initScale();
  853. }
  854. if (is_sRGB_stdScale) {
  855. return extractComponent(inData, idx, 8);
  856. } else if (is_LinearRGB_stdScale) {
  857. int lutidx = extractComponent(inData, idx, 16);
  858. return tosRGB8LUT[lutidx] & 0xff;
  859. } else if (is_ICCGray_stdScale) {
  860. int lutidx = extractComponent(inData, 0, 16);
  861. return tosRGB8LUT[lutidx] & 0xff;
  862. }
  863. // Not CS_sRGB, CS_LINEAR_RGB, or any TYPE_GRAY ICC_ColorSpace
  864. float[] norm = getNormalizedComponents(inData, null, 0);
  865. // Note that getNormalizedComponents returns non-premultiplied values
  866. float[] rgb = colorSpace.toRGB(norm);
  867. return (int) (rgb[idx] * 255.0f + 0.5f);
  868. }
  869. /**
  870. * Returns the red color component for the specified pixel, scaled
  871. * from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
  872. * is done if necessary. The <CODE>pixel</CODE> value is specified by an array
  873. * of data elements of type <CODE>transferType</CODE> passed in as an object
  874. * reference. The returned value will be a non pre-multiplied value. If the
  875. * alpha is premultiplied, this method divides it out before returning
  876. * the value (if the alpha value is 0, the red value will be 0). Since
  877. * <code>ComponentColorModel</code> can be subclassed, subclasses
  878. * inherit the implementation of this method and if they don't override
  879. * it then they throw an exception if they use an unsupported
  880. * <code>transferType</code>.
  881. *
  882. * @param inData The pixel from which you want to get the red color component,
  883. * specified by an array of data elements of type <CODE>transferType</CODE>.
  884. *
  885. * @return The red color component for the specified pixel, as an int.
  886. *
  887. * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
  888. * of type <CODE>transferType</CODE>.
  889. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
  890. * large enough to hold a pixel value for this
  891. * <CODE>ColorModel</CODE>.
  892. * @throws UnsupportedOperationException If the transfer type of
  893. * this <CODE>ComponentColorModel</CODE>
  894. * is not one of the supported transfer types:
  895. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  896. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  897. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  898. */
  899. public int getRed(Object inData) {
  900. return getRGBComponent(inData, 0);
  901. }
  902. /**
  903. * Returns the green color component for the specified pixel, scaled
  904. * from 0 to 255 in the default RGB <CODE>ColorSpace</CODE>, sRGB.
  905. * A color conversion is done if necessary. The <CODE>pixel</CODE> value
  906. * is specified by an array of data elements of type <CODE>transferType</CODE>
  907. * passed in as an object reference. The returned value is a non pre-multiplied
  908. * value. If the alpha is premultiplied, this method divides it out before
  909. * returning the value (if the alpha value is 0, the green value will be 0).
  910. * Since <code>ComponentColorModel</code> can be subclassed,
  911. * subclasses inherit the implementation of this method and if they
  912. * don't override it then they throw an exception if they use an
  913. * unsupported <code>transferType</code>.
  914. *
  915. * @param inData The pixel from which you want to get the green color component,
  916. * specified by an array of data elements of type <CODE>transferType</CODE>.
  917. *
  918. * @return The green color component for the specified pixel, as an int.
  919. *
  920. * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
  921. * of type <CODE>transferType</CODE>.
  922. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
  923. * large enough to hold a pixel value for this
  924. * <CODE>ColorModel</CODE>.
  925. * @throws UnsupportedOperationException If the transfer type of
  926. * this <CODE>ComponentColorModel</CODE>
  927. * is not one of the supported transfer types:
  928. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  929. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  930. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  931. */
  932. public int getGreen(Object inData) {
  933. return getRGBComponent(inData, 1);
  934. }
  935. /**
  936. * Returns the blue color component for the specified pixel, scaled
  937. * from 0 to 255 in the default RGB <CODE>ColorSpace</CODE>, sRGB.
  938. * A color conversion is done if necessary. The <CODE>pixel</CODE> value is
  939. * specified by an array of data elements of type <CODE>transferType</CODE>
  940. * passed in as an object reference. The returned value is a non pre-multiplied
  941. * value. If the alpha is premultiplied, this method divides it out before
  942. * returning the value (if the alpha value is 0, the blue value will be 0).
  943. * Since <code>ComponentColorModel</code> can be subclassed,
  944. * subclasses inherit the implementation of this method and if they
  945. * don't override it then they throw an exception if they use an
  946. * unsupported <code>transferType</code>.
  947. *
  948. * @param inData The pixel from which you want to get the blue color component,
  949. * specified by an array of data elements of type <CODE>transferType</CODE>.
  950. *
  951. * @return The blue color component for the specified pixel, as an int.
  952. *
  953. * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
  954. * of type <CODE>transferType</CODE>.
  955. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
  956. * large enough to hold a pixel value for this
  957. * <CODE>ColorModel</CODE>.
  958. * @throws UnsupportedOperationException If the transfer type of
  959. * this <CODE>ComponentColorModel</CODE>
  960. * is not one of the supported transfer types:
  961. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  962. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  963. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  964. */
  965. public int getBlue(Object inData) {
  966. return getRGBComponent(inData, 2);
  967. }
  968. /**
  969. * Returns the alpha component for the specified pixel, scaled from
  970. * 0 to 255. The pixel value is specified by an array of data
  971. * elements of type <CODE>transferType</CODE> passed in as an
  972. * object reference. Since <code>ComponentColorModel</code> can be
  973. * subclassed, subclasses inherit the
  974. * implementation of this method and if they don't override it then
  975. * they throw an exception if they use an unsupported
  976. * <code>transferType</code>.
  977. *
  978. * @param inData The pixel from which you want to get the alpha component,
  979. * specified by an array of data elements of type <CODE>transferType</CODE>.
  980. *
  981. * @return The alpha component for the specified pixel, as an int.
  982. *
  983. * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
  984. * of type <CODE>transferType</CODE>.
  985. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
  986. * large enough to hold a pixel value for this
  987. * <CODE>ColorModel</CODE>.
  988. * @throws UnsupportedOperationException If the transfer type of
  989. * this <CODE>ComponentColorModel</CODE>
  990. * is not one of the supported transfer types:
  991. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  992. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  993. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  994. */
  995. public int getAlpha(Object inData) {
  996. if (supportsAlpha == false) {
  997. return 255;
  998. }
  999. int alpha = 0;
  1000. int aIdx = numColorComponents;
  1001. int mask = (1 << nBits[aIdx]) - 1;
  1002. switch (transferType) {
  1003. case DataBuffer.TYPE_SHORT:
  1004. short sdata[] = (short[])inData;
  1005. alpha = (int) ((sdata[aIdx] / 32767.0f) * 255.0f + 0.5f);
  1006. return alpha;
  1007. case DataBuffer.TYPE_FLOAT:
  1008. float fdata[] = (float[])inData;
  1009. alpha = (int) (fdata[aIdx] * 255.0f + 0.5f);
  1010. return alpha;
  1011. case DataBuffer.TYPE_DOUBLE:
  1012. double ddata[] = (double[])inData;
  1013. alpha = (int) (ddata[aIdx] * 255.0 + 0.5);
  1014. return alpha;
  1015. case DataBuffer.TYPE_BYTE:
  1016. byte bdata[] = (byte[])inData;
  1017. alpha = bdata[aIdx] & mask;
  1018. break;
  1019. case DataBuffer.TYPE_USHORT:
  1020. short usdata[] = (short[])inData;
  1021. alpha = usdata[aIdx] & mask;
  1022. break;
  1023. case DataBuffer.TYPE_INT:
  1024. int idata[] = (int[])inData;
  1025. alpha = idata[aIdx];
  1026. break;
  1027. default:
  1028. throw new
  1029. UnsupportedOperationException("This method has not "+
  1030. "been implemented for transferType " + transferType);
  1031. }
  1032. if (nBits[aIdx] == 8) {
  1033. return alpha;
  1034. } else {
  1035. return (int)
  1036. ((((float) alpha) / ((float) ((1 << nBits[aIdx]) - 1))) *
  1037. 255.0f + 0.5f);
  1038. }
  1039. }
  1040. /**
  1041. * Returns the color/alpha components for the specified pixel in the
  1042. * default RGB color model format. A color conversion is done if
  1043. * necessary. The pixel value is specified by an
  1044. * array of data elements of type <CODE>transferType</CODE> passed
  1045. * in as an object reference.
  1046. * The returned value is in a non pre-multiplied format. If
  1047. * the alpha is premultiplied, this method divides it out of the
  1048. * color components (if the alpha value is 0, the color values will be 0).
  1049. * Since <code>ComponentColorModel</code> can be subclassed,
  1050. * subclasses inherit the implementation of this method and if they
  1051. * don't override it then they throw an exception if they use an
  1052. * unsupported <code>transferType</code>.
  1053. *
  1054. * @param inData The pixel from which you want to get the color/alpha components,
  1055. * specified by an array of data elements of type <CODE>transferType</CODE>.
  1056. *
  1057. * @return The color/alpha components for the specified pixel, as an int.
  1058. *
  1059. * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
  1060. * of type <CODE>transferType</CODE>.
  1061. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
  1062. * large enough to hold a pixel value for this
  1063. * <CODE>ColorModel</CODE>.
  1064. * @throws UnsupportedOperationException If the transfer type of
  1065. * this <CODE>ComponentColorModel</CODE>
  1066. * is not one of the supported transfer types:
  1067. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  1068. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  1069. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  1070. * @see ColorModel#getRGBdefault
  1071. */
  1072. public int getRGB(Object inData) {
  1073. if (needScaleInit) {
  1074. initScale();
  1075. }
  1076. if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
  1077. return (getAlpha(inData) << 24)
  1078. | (getRed(inData) << 16)
  1079. | (getGreen(inData) << 8)
  1080. | (getBlue(inData));
  1081. } else if (colorSpaceType == ColorSpace.TYPE_GRAY) {
  1082. int gray = getRed(inData); // Red sRGB component should equal
  1083. // green and blue components
  1084. return (getAlpha(inData) << 24)
  1085. | (gray << 16)
  1086. | (gray << 8)
  1087. | gray;
  1088. }
  1089. float[] norm = getNormalizedComponents(inData, null, 0);
  1090. // Note that getNormalizedComponents returns non-premult values
  1091. float[] rgb = colorSpace.toRGB(norm);
  1092. return (getAlpha(inData) << 24)
  1093. | (((int) (rgb[0] * 255.0f + 0.5f)) << 16)
  1094. | (((int) (rgb[1] * 255.0f + 0.5f)) << 8)
  1095. | (((int) (rgb[2] * 255.0f + 0.5f)) << 0);
  1096. }
  1097. /**
  1098. * Returns a data element array representation of a pixel in this
  1099. * <CODE>ColorModel</CODE>, given an integer pixel representation
  1100. * in the default RGB color model.
  1101. * This array can then be passed to the <CODE>setDataElements</CODE>
  1102. * method of a <CODE>WritableRaster</CODE> object. If the
  1103. * <CODE>pixel</CODE>
  1104. * parameter is null, a new array is allocated. Since
  1105. * <code>ComponentColorModel</code> can be subclassed, subclasses
  1106. * inherit the implementation of this method and if they don't
  1107. * override it then
  1108. * they throw an exception if they use an unsupported
  1109. * <code>transferType</code>.
  1110. *
  1111. * @param rgb the integer representation of the pixel in the RGB
  1112. * color model
  1113. * @param pixel the specified pixel
  1114. * @return The data element array representation of a pixel
  1115. * in this <CODE>ColorModel</CODE>.
  1116. * @throws ClassCastException If <CODE>pixel</CODE> is not null and
  1117. * is not a primitive array of type <CODE>transferType</CODE>.
  1118. * @throws ArrayIndexOutOfBoundsException If <CODE>pixel</CODE> is
  1119. * not large enough to hold a pixel value for this
  1120. * <CODE>ColorModel</CODE>.
  1121. * @throws UnsupportedOperationException If the transfer type of
  1122. * this <CODE>ComponentColorModel</CODE>
  1123. * is not one of the supported transfer types:
  1124. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  1125. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  1126. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  1127. *
  1128. * @see WritableRaster#setDataElements
  1129. * @see SampleModel#setDataElements
  1130. */
  1131. public Object getDataElements(int rgb, Object pixel) {
  1132. // REMIND: Use rendering hints?
  1133. int red, grn, blu, alp;
  1134. red = (rgb>>16) & 0xff;
  1135. grn = (rgb>>8) & 0xff;
  1136. blu = rgb & 0xff;
  1137. if (needScaleInit) {
  1138. initScale();
  1139. }
  1140. if (signed) {
  1141. // Handle SHORT, FLOAT, & DOUBLE here
  1142. switch(transferType) {
  1143. case DataBuffer.TYPE_SHORT:
  1144. {
  1145. short sdata[];
  1146. if (pixel == null) {
  1147. sdata = new short[numComponents];
  1148. } else {
  1149. sdata = (short[])pixel;
  1150. }
  1151. float factor;
  1152. if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
  1153. factor = 32767.0f / 255.0f;
  1154. if (is_LinearRGB_stdScale) {
  1155. red = fromsRGB8LUT16[red] & 0xffff;
  1156. grn = fromsRGB8LUT16[grn] & 0xffff;
  1157. blu = fromsRGB8LUT16[blu] & 0xffff;
  1158. factor = 32767.0f / 65535.0f;
  1159. }
  1160. if (supportsAlpha) {
  1161. alp = (rgb>>24) & 0xff;
  1162. sdata[3] =
  1163. (short) (alp * (32767.0f / 255.0f) + 0.5f);
  1164. if (isAlphaPremultiplied) {
  1165. factor = alp * factor * (1.0f / 255.0f);
  1166. }
  1167. }
  1168. sdata[0] = (short) (red * factor + 0.5f);
  1169. sdata[1] = (short) (grn * factor + 0.5f);
  1170. sdata[2] = (short) (blu * factor + 0.5f);
  1171. } else if (is_LinearGray_stdScale) {
  1172. red = fromsRGB8LUT16[red] & 0xffff;
  1173. grn = fromsRGB8LUT16[grn] & 0xffff;
  1174. blu = fromsRGB8LUT16[blu] & 0xffff;
  1175. float gray = ((0.2125f * red) +
  1176. (0.7154f * grn) +
  1177. (0.0721f * blu)) / 65535.0f;
  1178. factor = 32767.0f;
  1179. if (supportsAlpha) {
  1180. alp = (rgb>>24) & 0xff;
  1181. sdata[1] =
  1182. (short) (alp * (32767.0f / 255.0f) + 0.5f);
  1183. if (isAlphaPremultiplied) {
  1184. factor = alp * factor * (1.0f / 255.0f);
  1185. }
  1186. }
  1187. sdata[0] = (short) (gray * factor + 0.5f);
  1188. } else if (is_ICCGray_stdScale) {
  1189. red = fromsRGB8LUT16[red] & 0xffff;
  1190. grn = fromsRGB8LUT16[grn] & 0xffff;
  1191. blu = fromsRGB8LUT16[blu] & 0xffff;
  1192. int gray = (int) ((0.2125f * red) +
  1193. (0.7154f * grn) +
  1194. (0.0721f * blu) + 0.5f);
  1195. gray = fromLinearGray16ToOtherGray16LUT[gray] & 0xffff;
  1196. factor = 32767.0f / 65535.0f;
  1197. if (supportsAlpha) {
  1198. alp = (rgb>>24) & 0xff;
  1199. sdata[1] =
  1200. (short) (alp * (32767.0f / 255.0f) + 0.5f);
  1201. if (isAlphaPremultiplied) {
  1202. factor = alp * factor * (1.0f / 255.0f);
  1203. }
  1204. }
  1205. sdata[0] = (short) (gray * factor + 0.5f);
  1206. } else {
  1207. factor = 1.0f / 255.0f;
  1208. float norm[] = new float[3];
  1209. norm[0] = red * factor;
  1210. norm[1] = grn * factor;
  1211. norm[2] = blu * factor;
  1212. norm = colorSpace.fromRGB(norm);
  1213. if (nonStdScale) {
  1214. for (int i = 0; i < numColorComponents; i++) {
  1215. norm[i] = (norm[i] - compOffset[i]) *
  1216. compScale[i];
  1217. // REMIND: need to analyze whether this
  1218. // clamping is necessary
  1219. if (norm[i] < 0.0f) {
  1220. norm[i] = 0.0f;
  1221. }
  1222. if (norm[i] > 1.0f) {
  1223. norm[i] = 1.0f;
  1224. }
  1225. }
  1226. }
  1227. factor = 32767.0f;
  1228. if (supportsAlpha) {
  1229. alp = (rgb>>24) & 0xff;
  1230. sdata[numColorComponents] =
  1231. (short) (alp * (32767.0f / 255.0f) + 0.5f);
  1232. if (isAlphaPremultiplied) {
  1233. factor *= alp * (1.0f / 255.0f);
  1234. }
  1235. }
  1236. for (int i = 0; i < numColorComponents; i++) {
  1237. sdata[i] = (short) (norm[i] * factor + 0.5f);
  1238. }
  1239. }
  1240. return sdata;
  1241. }
  1242. case DataBuffer.TYPE_FLOAT:
  1243. {
  1244. float fdata[];
  1245. if (pixel == null) {
  1246. fdata = new float[numComponents];
  1247. } else {
  1248. fdata = (float[])pixel;
  1249. }
  1250. float factor;
  1251. if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
  1252. if (is_LinearRGB_stdScale) {
  1253. red = fromsRGB8LUT16[red] & 0xffff;
  1254. grn = fromsRGB8LUT16[grn] & 0xffff;
  1255. blu = fromsRGB8LUT16[blu] & 0xffff;
  1256. factor = 1.0f / 65535.0f;
  1257. } else {
  1258. factor = 1.0f / 255.0f;
  1259. }
  1260. if (supportsAlpha) {
  1261. alp = (rgb>>24) & 0xff;
  1262. fdata[3] = alp * (1.0f / 255.0f);
  1263. if (isAlphaPremultiplied) {
  1264. factor *= fdata[3];
  1265. }
  1266. }
  1267. fdata[0] = red * factor;
  1268. fdata[1] = grn * factor;
  1269. fdata[2] = blu * factor;
  1270. } else if (is_LinearGray_stdScale) {
  1271. red = fromsRGB8LUT16[red] & 0xffff;
  1272. grn = fromsRGB8LUT16[grn] & 0xffff;
  1273. blu = fromsRGB8LUT16[blu] & 0xffff;
  1274. fdata[0] = ((0.2125f * red) +
  1275. (0.7154f * grn) +
  1276. (0.0721f * blu)) / 65535.0f;
  1277. if (supportsAlpha) {
  1278. alp = (rgb>>24) & 0xff;
  1279. fdata[1] = alp * (1.0f / 255.0f);
  1280. if (isAlphaPremultiplied) {
  1281. fdata[0] *= fdata[1];
  1282. }
  1283. }
  1284. } else if (is_ICCGray_stdScale) {
  1285. red = fromsRGB8LUT16[red] & 0xffff;
  1286. grn = fromsRGB8LUT16[grn] & 0xffff;
  1287. blu = fromsRGB8LUT16[blu] & 0xffff;
  1288. int gray = (int) ((0.2125f * red) +
  1289. (0.7154f * grn) +
  1290. (0.0721f * blu) + 0.5f);
  1291. fdata[0] = (fromLinearGray16ToOtherGray16LUT[gray] &
  1292. 0xffff) / 65535.0f;
  1293. if (supportsAlpha) {
  1294. alp = (rgb>>24) & 0xff;
  1295. fdata[1] = alp * (1.0f / 255.0f);
  1296. if (isAlphaPremultiplied) {
  1297. fdata[0] *= fdata[1];
  1298. }
  1299. }
  1300. } else {
  1301. float norm[] = new float[3];
  1302. factor = 1.0f / 255.0f;
  1303. norm[0] = red * factor;
  1304. norm[1] = grn * factor;
  1305. norm[2] = blu * factor;
  1306. norm = colorSpace.fromRGB(norm);
  1307. if (supportsAlpha) {
  1308. alp = (rgb>>24) & 0xff;
  1309. fdata[numColorComponents] = alp * factor;
  1310. if (isAlphaPremultiplied) {
  1311. factor *= alp;
  1312. for (int i = 0; i < numColorComponents; i++) {
  1313. norm[i] *= factor;
  1314. }
  1315. }
  1316. }
  1317. for (int i = 0; i < numColorComponents; i++) {
  1318. fdata[i] = norm[i];
  1319. }
  1320. }
  1321. return fdata;
  1322. }
  1323. case DataBuffer.TYPE_DOUBLE:
  1324. {
  1325. double ddata[];
  1326. if (pixel == null) {
  1327. ddata = new double[numComponents];
  1328. } else {
  1329. ddata = (double[])pixel;
  1330. }
  1331. if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
  1332. double factor;
  1333. if (is_LinearRGB_stdScale) {
  1334. red = fromsRGB8LUT16[red] & 0xffff;
  1335. grn = fromsRGB8LUT16[grn] & 0xffff;
  1336. blu = fromsRGB8LUT16[blu] & 0xffff;
  1337. factor = 1.0 / 65535.0;
  1338. } else {
  1339. factor = 1.0 / 255.0;
  1340. }
  1341. if (supportsAlpha) {
  1342. alp = (rgb>>24) & 0xff;
  1343. ddata[3] = alp * (1.0 / 255.0);
  1344. if (isAlphaPremultiplied) {
  1345. factor *= ddata[3];
  1346. }
  1347. }
  1348. ddata[0] = red * factor;
  1349. ddata[1] = grn * factor;
  1350. ddata[2] = blu * factor;
  1351. } else if (is_LinearGray_stdScale) {
  1352. red = fromsRGB8LUT16[red] & 0xffff;
  1353. grn = fromsRGB8LUT16[grn] & 0xffff;
  1354. blu = fromsRGB8LUT16[blu] & 0xffff;
  1355. ddata[0] = ((0.2125 * red) +
  1356. (0.7154 * grn) +
  1357. (0.0721 * blu)) / 65535.0;
  1358. if (supportsAlpha) {
  1359. alp = (rgb>>24) & 0xff;
  1360. ddata[1] = alp * (1.0 / 255.0);
  1361. if (isAlphaPremultiplied) {
  1362. ddata[0] *= ddata[1];
  1363. }
  1364. }
  1365. } else if (is_ICCGray_stdScale) {
  1366. red = fromsRGB8LUT16[red] & 0xffff;
  1367. grn = fromsRGB8LUT16[grn] & 0xffff;
  1368. blu = fromsRGB8LUT16[blu] & 0xffff;
  1369. int gray = (int) ((0.2125f * red) +
  1370. (0.7154f * grn) +
  1371. (0.0721f * blu) + 0.5f);
  1372. ddata[0] = (fromLinearGray16ToOtherGray16LUT[gray] &
  1373. 0xffff) / 65535.0;
  1374. if (supportsAlpha) {
  1375. alp = (rgb>>24) & 0xff;
  1376. ddata[1] = alp * (1.0 / 255.0);
  1377. if (isAlphaPremultiplied) {
  1378. ddata[0] *= ddata[1];
  1379. }
  1380. }
  1381. } else {
  1382. float factor = 1.0f / 255.0f;
  1383. float norm[] = new float[3];
  1384. norm[0] = red * factor;
  1385. norm[1] = grn * factor;
  1386. norm[2] = blu * factor;
  1387. norm = colorSpace.fromRGB(norm);
  1388. if (supportsAlpha) {
  1389. alp = (rgb>>24) & 0xff;
  1390. ddata[numColorComponents] = alp * (1.0 / 255.0);
  1391. if (isAlphaPremultiplied) {
  1392. factor *= alp;
  1393. for (int i = 0; i < numColorComponents; i++) {
  1394. norm[i] *= factor;
  1395. }
  1396. }
  1397. }
  1398. for (int i = 0; i < numColorComponents; i++) {
  1399. ddata[i] = norm[i];
  1400. }
  1401. }
  1402. return ddata;
  1403. }
  1404. }
  1405. }
  1406. // Handle BYTE, USHORT, & INT here
  1407. //REMIND: maybe more efficient not to use int array for
  1408. //DataBuffer.TYPE_USHORT and DataBuffer.TYPE_INT
  1409. int intpixel[];
  1410. if (transferType == DataBuffer.TYPE_INT &&
  1411. pixel != null) {
  1412. intpixel = (int[])pixel;
  1413. } else {
  1414. intpixel = new int[numComponents];
  1415. }
  1416. if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
  1417. int precision;
  1418. float factor;
  1419. if (is_LinearRGB_stdScale) {
  1420. if (transferType == DataBuffer.TYPE_BYTE) {
  1421. red = fromsRGB8LUT8[red] & 0xff;
  1422. grn = fromsRGB8LUT8[grn] & 0xff;
  1423. blu = fromsRGB8LUT8[blu] & 0xff;
  1424. precision = 8;
  1425. factor = 1.0f / 255.0f;
  1426. } else {
  1427. red = fromsRGB8LUT16[red] & 0xffff;
  1428. grn = fromsRGB8LUT16[grn] & 0xffff;
  1429. blu = fromsRGB8LUT16[blu] & 0xffff;
  1430. precision = 16;
  1431. factor = 1.0f / 65535.0f;
  1432. }
  1433. } else {
  1434. precision = 8;
  1435. factor = 1.0f / 255.0f;
  1436. }
  1437. if (supportsAlpha) {
  1438. alp = (rgb>>24)&0xff;
  1439. if (nBits[3] == 8) {
  1440. intpixel[3] = alp;
  1441. }
  1442. else {
  1443. intpixel[3] = (int)
  1444. (alp * (1.0f / 255.0f) * ((1<<nBits[3]) - 1) + 0.5f);
  1445. }
  1446. if (isAlphaPremultiplied) {
  1447. factor *= (alp * (1.0f / 255.0f));
  1448. precision = -1; // force component calculations below
  1449. }
  1450. }
  1451. if (nBits[0] == precision) {
  1452. intpixel[0] = red;
  1453. }
  1454. else {
  1455. intpixel[0] = (int) (red * factor * ((1<<nBits[0]) - 1) + 0.5f);
  1456. }
  1457. if (nBits[1] == precision) {
  1458. intpixel[1] = (int)(grn);
  1459. }
  1460. else {
  1461. intpixel[1] = (int) (grn * factor * ((1<<nBits[1]) - 1) + 0.5f);
  1462. }
  1463. if (nBits[2] == precision) {
  1464. intpixel[2] = (int)(blu);
  1465. }
  1466. else {
  1467. intpixel[2] = (int) (blu * factor * ((1<<nBits[2]) - 1) + 0.5f);
  1468. }
  1469. } else if (is_LinearGray_stdScale) {
  1470. red = fromsRGB8LUT16[red] & 0xffff;
  1471. grn = fromsRGB8LUT16[grn] & 0xffff;
  1472. blu = fromsRGB8LUT16[blu] & 0xffff;
  1473. float gray = ((0.2125f * red) +
  1474. (0.7154f * grn) +
  1475. (0.0721f * blu)) / 65535.0f;
  1476. if (supportsAlpha) {
  1477. alp = (rgb>>24) & 0xff;
  1478. if (nBits[1] == 8) {
  1479. intpixel[1] = alp;
  1480. } else {
  1481. intpixel[1] = (int) (alp * (1.0f / 255.0f) *
  1482. ((1 << nBits[1]) - 1) + 0.5f);
  1483. }
  1484. if (isAlphaPremultiplied) {
  1485. gray *= (alp * (1.0f / 255.0f));
  1486. }
  1487. }
  1488. intpixel[0] = (int) (gray * ((1 << nBits[0]) - 1) + 0.5f);
  1489. } else if (is_ICCGray_stdScale) {
  1490. red = fromsRGB8LUT16[red] & 0xffff;
  1491. grn = fromsRGB8LUT16[grn] & 0xffff;
  1492. blu = fromsRGB8LUT16[blu] & 0xffff;
  1493. int gray16 = (int) ((0.2125f * red) +
  1494. (0.7154f * grn) +
  1495. (0.0721f * blu) + 0.5f);
  1496. float gray = (fromLinearGray16ToOtherGray16LUT[gray16] &
  1497. 0xffff) / 65535.0f;
  1498. if (supportsAlpha) {
  1499. alp = (rgb>>24) & 0xff;
  1500. if (nBits[1] == 8) {
  1501. intpixel[1] = alp;
  1502. } else {
  1503. intpixel[1] = (int) (alp * (1.0f / 255.0f) *
  1504. ((1 << nBits[1]) - 1) + 0.5f);
  1505. }
  1506. if (isAlphaPremultiplied) {
  1507. gray *= (alp * (1.0f / 255.0f));
  1508. }
  1509. }
  1510. intpixel[0] = (int) (gray * ((1 << nBits[0]) - 1) + 0.5f);
  1511. } else {
  1512. // Need to convert the color
  1513. float[] norm = new float[3];
  1514. float factor = 1.0f / 255.0f;
  1515. norm[0] = red * factor;
  1516. norm[1] = grn * factor;
  1517. norm[2] = blu * factor;
  1518. norm = colorSpace.fromRGB(norm);
  1519. if (nonStdScale) {
  1520. for (int i = 0; i < numColorComponents; i++) {
  1521. norm[i] = (norm[i] - compOffset[i]) *
  1522. compScale[i];
  1523. // REMIND: need to analyze whether this
  1524. // clamping is necessary
  1525. if (norm[i] < 0.0f) {
  1526. norm[i] = 0.0f;
  1527. }
  1528. if (norm[i] > 1.0f) {
  1529. norm[i] = 1.0f;
  1530. }
  1531. }
  1532. }
  1533. if (supportsAlpha) {
  1534. alp = (rgb>>24) & 0xff;
  1535. if (nBits[numColorComponents] == 8) {
  1536. intpixel[numColorComponents] = alp;
  1537. }
  1538. else {
  1539. intpixel[numColorComponents] =
  1540. (int) (alp * factor *
  1541. ((1<<nBits[numColorComponents]) - 1) + 0.5f);
  1542. }
  1543. if (isAlphaPremultiplied) {
  1544. factor *= alp;
  1545. for (int i = 0; i < numColorComponents; i++) {
  1546. norm[i] *= factor;
  1547. }
  1548. }
  1549. }
  1550. for (int i = 0; i < numColorComponents; i++) {
  1551. intpixel[i] = (int) (norm[i] * ((1<<nBits[i]) - 1) + 0.5f);
  1552. }
  1553. }
  1554. switch (transferType) {
  1555. case DataBuffer.TYPE_BYTE: {
  1556. byte bdata[];
  1557. if (pixel == null) {
  1558. bdata = new byte[numComponents];
  1559. } else {
  1560. bdata = (byte[])pixel;
  1561. }
  1562. for (int i = 0; i < numComponents; i++) {
  1563. bdata[i] = (byte)(0xff&intpixel[i]);
  1564. }
  1565. return bdata;
  1566. }
  1567. case DataBuffer.TYPE_USHORT:{
  1568. short sdata[];
  1569. if (pixel == null) {
  1570. sdata = new short[numComponents];
  1571. } else {
  1572. sdata = (short[])pixel;
  1573. }
  1574. for (int i = 0; i < numComponents; i++) {
  1575. sdata[i] = (short)(intpixel[i]&0xffff);
  1576. }
  1577. return sdata;
  1578. }
  1579. case DataBuffer.TYPE_INT:
  1580. if (maxBits > 23) {
  1581. // fix 4412670 - for components of 24 or more bits
  1582. // some calculations done above with float precision
  1583. // may lose enough precision that the integer result
  1584. // overflows nBits, so we need to clamp.
  1585. for (int i = 0; i < numComponents; i++) {
  1586. if (intpixel[i] > ((1<<nBits[i]) - 1)) {
  1587. intpixel[i] = (1<<nBits[i]) - 1;
  1588. }
  1589. }
  1590. }
  1591. return intpixel;
  1592. }
  1593. throw new IllegalArgumentException("This method has not been "+
  1594. "implemented for transferType " + transferType);
  1595. }
  1596. /** Returns an array of unnormalized color/alpha components given a pixel
  1597. * in this <CODE>ColorModel</CODE>.
  1598. * An IllegalArgumentException is thrown if the component value for this
  1599. * <CODE>ColorModel</CODE> is not conveniently representable in the
  1600. * unnormalized form. Color/alpha components are stored
  1601. * in the <CODE>components</CODE> array starting at <CODE>offset</CODE>
  1602. * (even if the array is allocated by this method).
  1603. *
  1604. * @param pixel The pixel value specified as an integer.
  1605. * @param components An integer array in which to store the unnormalized
  1606. * color/alpha components. If the <CODE>components</CODE> array is null,
  1607. * a new array is allocated.
  1608. * @param offset An offset into the <CODE>components</CODE> array.
  1609. *
  1610. * @return The components array.
  1611. *
  1612. * @throws IllegalArgumentException If there is more than one
  1613. * component in this <CODE>ColorModel</CODE>.
  1614. * @throws IllegalArgumentException If this
  1615. * <CODE>ColorModel</CODE> does not support the unnormalized form
  1616. * @throws ArrayIndexOutOfBoundsException If the <CODE>components</CODE>
  1617. * array is not null and is not large enough to hold all the color and
  1618. * alpha components (starting at offset).
  1619. */
  1620. public int[] getComponents(int pixel, int[] components, int offset) {
  1621. if (numComponents > 1) {
  1622. throw new
  1623. IllegalArgumentException("More than one component per pixel");
  1624. }
  1625. if (needScaleInit) {
  1626. initScale();
  1627. }
  1628. if (noUnnorm) {
  1629. throw new
  1630. IllegalArgumentException(
  1631. "This ColorModel does not support the unnormalized form");
  1632. }
  1633. if (components == null) {
  1634. components = new int[offset+1];
  1635. }
  1636. components[offset+0] = (pixel & ((1<<nBits[0]) - 1));
  1637. return components;
  1638. }
  1639. /**
  1640. * Returns an array of unnormalized color/alpha components given a pixel
  1641. * in this <CODE>ColorModel</CODE>. The pixel value is specified by an
  1642. * array of data elements of type <CODE>transferType</CODE> passed in as
  1643. * an object reference.
  1644. * An IllegalArgumentException is thrown if the component values for this
  1645. * <CODE>ColorModel</CODE> are not conveniently representable in the
  1646. * unnormalized form.
  1647. * Color/alpha components are stored in the <CODE>components</CODE> array
  1648. * starting at <CODE>offset</CODE> (even if the array is allocated by
  1649. * this method). Since <code>ComponentColorModel</code> can be
  1650. * subclassed, subclasses inherit the
  1651. * implementation of this method and if they don't override it then
  1652. * this method might throw an exception if they use an unsupported
  1653. * <code>transferType</code>.
  1654. *
  1655. * @param pixel A pixel value specified by an array of data elements of
  1656. * type <CODE>transferType</CODE>.
  1657. * @param components An integer array in which to store the unnormalized
  1658. * color/alpha components. If the <CODE>components</CODE> array is null,
  1659. * a new array is allocated.
  1660. * @param offset An offset into the <CODE>components</CODE> array.
  1661. *
  1662. * @return The <CODE>components</CODE> array.
  1663. *
  1664. * @throws IllegalArgumentException If this
  1665. * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
  1666. * @throws UnsupportedOperationException in some cases iff the
  1667. * transfer type of this <CODE>ComponentColorModel</CODE>
  1668. * is not one of the following transfer types:
  1669. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  1670. * or <CODE>DataBuffer.TYPE_INT</CODE>.
  1671. * @throws ClassCastException If <CODE>pixel</CODE> is not a primitive
  1672. * array of type <CODE>transferType</CODE>.
  1673. * @throws IllegalArgumentException If the <CODE>components</CODE> array is
  1674. * not null and is not large enough to hold all the color and alpha
  1675. * components (starting at offset), or if <CODE>pixel</CODE> is not large
  1676. * enough to hold a pixel value for this ColorModel.
  1677. */
  1678. public int[] getComponents(Object pixel, int[] components, int offset) {
  1679. int intpixel[];
  1680. if (needScaleInit) {
  1681. initScale();
  1682. }
  1683. if (noUnnorm) {
  1684. throw new
  1685. IllegalArgumentException(
  1686. "This ColorModel does not support the unnormalized form");
  1687. }
  1688. if (pixel instanceof int[]) {
  1689. intpixel = (int[])pixel;
  1690. } else {
  1691. intpixel = DataBuffer.toIntArray(pixel);
  1692. if (intpixel == null) {
  1693. throw new UnsupportedOperationException("This method has not been "+
  1694. "implemented for transferType " + transferType);
  1695. }
  1696. }
  1697. if (intpixel.length < numComponents) {
  1698. throw new IllegalArgumentException
  1699. ("Length of pixel array < number of components in model");
  1700. }
  1701. if (components == null) {
  1702. components = new int[offset+numComponents];
  1703. }
  1704. else if ((components.length-offset) < numComponents) {
  1705. throw new IllegalArgumentException
  1706. ("Length of components array < number of components in model");
  1707. }
  1708. System.arraycopy(intpixel, 0, components, offset, numComponents);
  1709. return components;
  1710. }
  1711. /**
  1712. * Returns an array of all of the color/alpha components in unnormalized
  1713. * form, given a normalized component array. Unnormalized components
  1714. * are unsigned integral values between 0 and 2<sup>n</sup> - 1, where
  1715. * n is the number of bits for a particular component. Normalized
  1716. * components are float values between a per component minimum and
  1717. * maximum specified by the <code>ColorSpace</code> object for this
  1718. * <code>ColorModel</code>. An <code>IllegalArgumentException</code>
  1719. * will be thrown if color component values for this
  1720. * <code>ColorModel</code> are not conveniently representable in the
  1721. * unnormalized form. If the
  1722. * <code>components</code> array is <code>null</code>, a new array
  1723. * will be allocated. The <code>components</code> array will
  1724. * be returned. Color/alpha components are stored in the
  1725. * <code>components</code> array starting at <code>offset</code> (even
  1726. * if the array is allocated by this method). An
  1727. * <code>ArrayIndexOutOfBoundsException</code> is thrown if the
  1728. * <code>components</code> array is not <code>null</code> and is not
  1729. * large enough to hold all the color and alpha
  1730. * components (starting at <code>offset</code>). An
  1731. * <code>IllegalArgumentException</code> is thrown if the
  1732. * <code>normComponents</code> array is not large enough to hold
  1733. * all the color and alpha components starting at
  1734. * <code>normOffset</code>.
  1735. * @param normComponents an array containing normalized components
  1736. * @param normOffset the offset into the <code>normComponents</code>
  1737. * array at which to start retrieving normalized components
  1738. * @param components an array that receives the components from
  1739. * <code>normComponents</code>
  1740. * @param offset the index into <code>components</code> at which to
  1741. * begin storing normalized components from
  1742. * <code>normComponents</code>
  1743. * @return an array containing unnormalized color and alpha
  1744. * components.
  1745. * @throws IllegalArgumentException If this
  1746. * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
  1747. * @throws IllegalArgumentException if the length of
  1748. * <code>normComponents</code> minus <code>normOffset</code>
  1749. * is less than <code>numComponents</code>
  1750. */
  1751. public int[] getUnnormalizedComponents(float[] normComponents,
  1752. int normOffset,
  1753. int[] components, int offset) {
  1754. if (needScaleInit) {
  1755. initScale();
  1756. }
  1757. if (noUnnorm) {
  1758. throw new
  1759. IllegalArgumentException(
  1760. "This ColorModel does not support the unnormalized form");
  1761. }
  1762. return super.getUnnormalizedComponents(normComponents, normOffset,
  1763. components, offset);
  1764. }
  1765. /**
  1766. * Returns an array of all of the color/alpha components in normalized
  1767. * form, given an unnormalized component array. Unnormalized components
  1768. * are unsigned integral values between 0 and 2<sup>n</sup> - 1, where
  1769. * n is the number of bits for a particular component. Normalized
  1770. * components are float values between a per component minimum and
  1771. * maximum specified by the <code>ColorSpace</code> object for this
  1772. * <code>ColorModel</code>. An <code>IllegalArgumentException</code>
  1773. * will be thrown if color component values for this
  1774. * <code>ColorModel</code> are not conveniently representable in the
  1775. * unnormalized form. If the
  1776. * <code>normComponents</code> array is <code>null</code>, a new array
  1777. * will be allocated. The <code>normComponents</code> array
  1778. * will be returned. Color/alpha components are stored in the
  1779. * <code>normComponents</code> array starting at
  1780. * <code>normOffset</code> (even if the array is allocated by this
  1781. * method). An <code>ArrayIndexOutOfBoundsException</code> is thrown
  1782. * if the <code>normComponents</code> array is not <code>null</code>
  1783. * and is not large enough to hold all the color and alpha components
  1784. * (starting at <code>normOffset</code>). An
  1785. * <code>IllegalArgumentException</code> is thrown if the
  1786. * <code>components</code> array is not large enough to hold all the
  1787. * color and alpha components starting at <code>offset</code>.
  1788. * @param components an array containing unnormalized components
  1789. * @param offset the offset into the <code>components</code> array at
  1790. * which to start retrieving unnormalized components
  1791. * @param normComponents an array that receives the normalized components
  1792. * @param normOffset the index into <code>normComponents</code> at
  1793. * which to begin storing normalized components
  1794. * @return an array containing normalized color and alpha
  1795. * components.
  1796. * @throws IllegalArgumentException If this
  1797. * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
  1798. */
  1799. public float[] getNormalizedComponents(int[] components, int offset,
  1800. float[] normComponents,
  1801. int normOffset) {
  1802. if (needScaleInit) {
  1803. initScale();
  1804. }
  1805. if (noUnnorm) {
  1806. throw new
  1807. IllegalArgumentException(
  1808. "This ColorModel does not support the unnormalized form");
  1809. }
  1810. return super.getNormalizedComponents(components, offset,
  1811. normComponents, normOffset);
  1812. }
  1813. /**
  1814. * Returns a pixel value represented as an int in this <CODE>ColorModel</CODE>,
  1815. * given an array of unnormalized color/alpha components.
  1816. *
  1817. * @param components An array of unnormalized color/alpha components.
  1818. * @param offset An offset into the <CODE>components</CODE> array.
  1819. *
  1820. * @return A pixel value represented as an int.
  1821. *
  1822. * @throws IllegalArgumentException If there is more than one component
  1823. * in this <CODE>ColorModel</CODE>.
  1824. * @throws IllegalArgumentException If this
  1825. * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
  1826. */
  1827. public int getDataElement(int[] components, int offset) {
  1828. if (needScaleInit) {
  1829. initScale();
  1830. }
  1831. if (numComponents == 1) {
  1832. if (noUnnorm) {
  1833. throw new
  1834. IllegalArgumentException(
  1835. "This ColorModel does not support the unnormalized form");
  1836. }
  1837. return components[offset+0];
  1838. }
  1839. throw new IllegalArgumentException("This model returns "+
  1840. numComponents+
  1841. " elements in the pixel array.");
  1842. }
  1843. /**
  1844. * Returns a data element array representation of a pixel in this
  1845. * <CODE>ColorModel</CODE>, given an array of unnormalized color/alpha
  1846. * components. This array can then be passed to the <CODE>setDataElements</CODE>
  1847. * method of a <CODE>WritableRaster</CODE> object.
  1848. *
  1849. * @param components An array of unnormalized color/alpha components.
  1850. * @param offset The integer offset into the <CODE>components</CODE> array.
  1851. * @param obj The object in which to store the data element array
  1852. * representation of the pixel. If <CODE>obj</CODE> variable is null,
  1853. * a new array is allocated. If <CODE>obj</CODE> is not null, it must
  1854. * be a primitive array of type <CODE>transferType</CODE>. An
  1855. * <CODE>ArrayIndexOutOfBoundsException</CODE> is thrown if
  1856. * <CODE>obj</CODE> is not large enough to hold a pixel value
  1857. * for this <CODE>ColorModel</CODE>. Since
  1858. * <code>ComponentColorModel</code> can be subclassed, subclasses
  1859. * inherit the implementation of this method and if they don't
  1860. * override it then they throw an exception if they use an
  1861. * unsupported <code>transferType</code>.
  1862. *
  1863. * @return The data element array representation of a pixel
  1864. * in this <CODE>ColorModel</CODE>.
  1865. *
  1866. * @throws IllegalArgumentException If the components array
  1867. * is not large enough to hold all the color and alpha components
  1868. * (starting at offset).
  1869. * @throws ClassCastException If <CODE>obj</CODE> is not null and is not a
  1870. * primitive array of type <CODE>transferType</CODE>.
  1871. * @throws ArrayIndexOutOfBoundsException If <CODE>obj</CODE> is not large
  1872. * enough to hold a pixel value for this <CODE>ColorModel</CODE>.
  1873. * @throws IllegalArgumentException If this
  1874. * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
  1875. * @throws UnsupportedOperationException If the transfer type of
  1876. * this <CODE>ComponentColorModel</CODE>
  1877. * is not one of the following transfer types:
  1878. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  1879. * or <CODE>DataBuffer.TYPE_INT</CODE>.
  1880. *
  1881. * @see WritableRaster#setDataElements
  1882. * @see SampleModel#setDataElements
  1883. */
  1884. public Object getDataElements(int[] components, int offset, Object obj) {
  1885. if (needScaleInit) {
  1886. initScale();
  1887. }
  1888. if (noUnnorm) {
  1889. throw new
  1890. IllegalArgumentException(
  1891. "This ColorModel does not support the unnormalized form");
  1892. }
  1893. if ((components.length-offset) < numComponents) {
  1894. throw new IllegalArgumentException("Component array too small"+
  1895. " (should be "+numComponents);
  1896. }
  1897. switch(transferType) {
  1898. case DataBuffer.TYPE_INT:
  1899. {
  1900. int[] pixel;
  1901. if (obj == null) {
  1902. pixel = new int[numComponents];
  1903. }
  1904. else {
  1905. pixel = (int[]) obj;
  1906. }
  1907. System.arraycopy(components, offset, pixel, 0,
  1908. numComponents);
  1909. return pixel;
  1910. }
  1911. case DataBuffer.TYPE_BYTE:
  1912. {
  1913. byte[] pixel;
  1914. if (obj == null) {
  1915. pixel = new byte[numComponents];
  1916. }
  1917. else {
  1918. pixel = (byte[]) obj;
  1919. }
  1920. for (int i=0; i < numComponents; i++) {
  1921. pixel[i] = (byte) (components[offset+i]&0xff);
  1922. }
  1923. return pixel;
  1924. }
  1925. case DataBuffer.TYPE_USHORT:
  1926. {
  1927. short[] pixel;
  1928. if (obj == null) {
  1929. pixel = new short[numComponents];
  1930. }
  1931. else {
  1932. pixel = (short[]) obj;
  1933. }
  1934. for (int i=0; i < numComponents; i++) {
  1935. pixel[i] = (short) (components[offset+i]&0xffff);
  1936. }
  1937. return pixel;
  1938. }
  1939. default:
  1940. throw new UnsupportedOperationException("This method has not been "+
  1941. "implemented for transferType " +
  1942. transferType);
  1943. }
  1944. }
  1945. /**
  1946. * Returns a pixel value represented as an <code>int</code> in this
  1947. * <code>ColorModel</code>, given an array of normalized color/alpha
  1948. * components. This method will throw an
  1949. * <code>IllegalArgumentException</code> if pixel values for this
  1950. * <code>ColorModel</code> are not conveniently representable as a
  1951. * single <code>int</code>. An
  1952. * <code>ArrayIndexOutOfBoundsException</code> is thrown if the
  1953. * <code>normComponents</code> array is not large enough to hold all the
  1954. * color and alpha components (starting at <code>normOffset</code>).
  1955. * @param normComponents an array of normalized color and alpha
  1956. * components
  1957. * @param normOffset the index into <code>normComponents</code> at which to
  1958. * begin retrieving the color and alpha components
  1959. * @return an <code>int</code> pixel value in this
  1960. * <code>ColorModel</code> corresponding to the specified components.
  1961. * @throws IllegalArgumentException if
  1962. * pixel values for this <code>ColorModel</code> are not
  1963. * conveniently representable as a single <code>int</code>
  1964. * @throws ArrayIndexOutOfBoundsException if
  1965. * the <code>normComponents</code> array is not large enough to
  1966. * hold all of the color and alpha components starting at
  1967. * <code>normOffset</code>
  1968. * @since 1.4
  1969. */
  1970. public int getDataElement(float[] normComponents, int normOffset) {
  1971. if (numComponents > 1) {
  1972. throw new
  1973. IllegalArgumentException("More than one component per pixel");
  1974. }
  1975. if (signed) {
  1976. throw new
  1977. IllegalArgumentException("Component value is signed");
  1978. }
  1979. if (needScaleInit) {
  1980. initScale();
  1981. }
  1982. Object pixel = getDataElements(normComponents, normOffset, null);
  1983. switch (transferType) {
  1984. case DataBuffer.TYPE_BYTE:
  1985. {
  1986. byte bpixel[] = (byte[]) pixel;
  1987. return bpixel[0] & 0xff;
  1988. }
  1989. case DataBuffer.TYPE_USHORT:
  1990. {
  1991. short[] uspixel = (short[]) pixel;
  1992. return uspixel[0] & 0xffff;
  1993. }
  1994. case DataBuffer.TYPE_INT:
  1995. {
  1996. int[] ipixel = (int[]) pixel;
  1997. return ipixel[0];
  1998. }
  1999. default:
  2000. throw new UnsupportedOperationException("This method has not been "
  2001. + "implemented for transferType " + transferType);
  2002. }
  2003. }
  2004. /**
  2005. * Returns a data element array representation of a pixel in this
  2006. * <code>ColorModel</code>, given an array of normalized color/alpha
  2007. * components. This array can then be passed to the
  2008. * <code>setDataElements</code> method of a <code>WritableRaster</code>
  2009. * object. An <code>ArrayIndexOutOfBoundsException</code> is thrown
  2010. * if the <code>normComponents</code> array is not large enough to hold
  2011. * all the color and alpha components (starting at
  2012. * <code>normOffset</code>). If the <code>obj</code> variable is
  2013. * <code>null</code>, a new array will be allocated. If
  2014. * <code>obj</code> is not <code>null</code>, it must be a primitive
  2015. * array of type transferType; otherwise, a
  2016. * <code>ClassCastException</code> is thrown. An
  2017. * <code>ArrayIndexOutOfBoundsException</code> is thrown if
  2018. * <code>obj</code> is not large enough to hold a pixel value for this
  2019. * <code>ColorModel</code>.
  2020. * @param normComponents an array of normalized color and alpha
  2021. * components
  2022. * @param normOffset the index into <code>normComponents</code> at which to
  2023. * begin retrieving color and alpha components
  2024. * @param obj a primitive data array to hold the returned pixel
  2025. * @return an <code>Object</code> which is a primitive data array
  2026. * representation of a pixel
  2027. * @throws ClassCastException if <code>obj</code>
  2028. * is not a primitive array of type <code>transferType</code>
  2029. * @throws ArrayIndexOutOfBoundsException if
  2030. * <code>obj</code> is not large enough to hold a pixel value
  2031. * for this <code>ColorModel</code> or the <code>normComponents</code>
  2032. * array is not large enough to hold all of the color and alpha
  2033. * components starting at <code>normOffset</code>
  2034. * @see WritableRaster#setDataElements
  2035. * @see SampleModel#setDataElements
  2036. * @since 1.4
  2037. */
  2038. public Object getDataElements(float[] normComponents, int normOffset,
  2039. Object obj) {
  2040. boolean needAlpha = supportsAlpha && isAlphaPremultiplied;
  2041. float[] stdNormComponents;
  2042. if (needScaleInit) {
  2043. initScale();
  2044. }
  2045. if (nonStdScale) {
  2046. stdNormComponents = new float[numComponents];
  2047. for (int c = 0, nc = normOffset; c < numColorComponents;
  2048. c++, nc++) {
  2049. stdNormComponents[c] = (normComponents[nc] - compOffset[c]) *
  2050. compScale[c];
  2051. // REMIND: need to analyze whether this
  2052. // clamping is necessary
  2053. if (stdNormComponents[c] < 0.0f) {
  2054. stdNormComponents[c] = 0.0f;
  2055. }
  2056. if (stdNormComponents[c] > 1.0f) {
  2057. stdNormComponents[c] = 1.0f;
  2058. }
  2059. }
  2060. if (supportsAlpha) {
  2061. stdNormComponents[numColorComponents] =
  2062. normComponents[numColorComponents + normOffset];
  2063. }
  2064. normOffset = 0;
  2065. } else {
  2066. stdNormComponents = normComponents;
  2067. }
  2068. switch (transferType) {
  2069. case DataBuffer.TYPE_BYTE:
  2070. byte[] bpixel;
  2071. if (obj == null) {
  2072. bpixel = new byte[numComponents];
  2073. } else {
  2074. bpixel = (byte[]) obj;
  2075. }
  2076. if (needAlpha) {
  2077. float alpha =
  2078. stdNormComponents[numColorComponents + normOffset];
  2079. for (int c = 0, nc = normOffset; c < numColorComponents;
  2080. c++, nc++) {
  2081. bpixel[c] = (byte) ((stdNormComponents[nc] * alpha) *
  2082. ((float) ((1 << nBits[c]) - 1)) + 0.5f);
  2083. }
  2084. bpixel[numColorComponents] =
  2085. (byte) (alpha *
  2086. ((float) ((1 << nBits[numColorComponents]) - 1)) +
  2087. 0.5f);
  2088. } else {
  2089. for (int c = 0, nc = normOffset; c < numComponents;
  2090. c++, nc++) {
  2091. bpixel[c] = (byte) (stdNormComponents[nc] *
  2092. ((float) ((1 << nBits[c]) - 1)) + 0.5f);
  2093. }
  2094. }
  2095. return bpixel;
  2096. case DataBuffer.TYPE_USHORT:
  2097. short[] uspixel;
  2098. if (obj == null) {
  2099. uspixel = new short[numComponents];
  2100. } else {
  2101. uspixel = (short[]) obj;
  2102. }
  2103. if (needAlpha) {
  2104. float alpha =
  2105. stdNormComponents[numColorComponents + normOffset];
  2106. for (int c = 0, nc = normOffset; c < numColorComponents;
  2107. c++, nc++) {
  2108. uspixel[c] = (short) ((stdNormComponents[nc] * alpha) *
  2109. ((float) ((1 << nBits[c]) - 1)) +
  2110. 0.5f);
  2111. }
  2112. uspixel[numColorComponents] =
  2113. (short) (alpha *
  2114. ((float) ((1 << nBits[numColorComponents]) - 1)) +
  2115. 0.5f);
  2116. } else {
  2117. for (int c = 0, nc = normOffset; c < numComponents;
  2118. c++, nc++) {
  2119. uspixel[c] = (short) (stdNormComponents[nc] *
  2120. ((float) ((1 << nBits[c]) - 1)) +
  2121. 0.5f);
  2122. }
  2123. }
  2124. return uspixel;
  2125. case DataBuffer.TYPE_INT:
  2126. int[] ipixel;
  2127. if (obj == null) {
  2128. ipixel = new int[numComponents];
  2129. } else {
  2130. ipixel = (int[]) obj;
  2131. }
  2132. if (needAlpha) {
  2133. float alpha =
  2134. stdNormComponents[numColorComponents + normOffset];
  2135. for (int c = 0, nc = normOffset; c < numColorComponents;
  2136. c++, nc++) {
  2137. ipixel[c] = (int) ((stdNormComponents[nc] * alpha) *
  2138. ((float) ((1 << nBits[c]) - 1)) + 0.5f);
  2139. }
  2140. ipixel[numColorComponents] =
  2141. (int) (alpha *
  2142. ((float) ((1 << nBits[numColorComponents]) - 1)) +
  2143. 0.5f);
  2144. } else {
  2145. for (int c = 0, nc = normOffset; c < numComponents;
  2146. c++, nc++) {
  2147. ipixel[c] = (int) (stdNormComponents[nc] *
  2148. ((float) ((1 << nBits[c]) - 1)) + 0.5f);
  2149. }
  2150. }
  2151. return ipixel;
  2152. case DataBuffer.TYPE_SHORT:
  2153. short[] spixel;
  2154. if (obj == null) {
  2155. spixel = new short[numComponents];
  2156. } else {
  2157. spixel = (short[]) obj;
  2158. }
  2159. if (needAlpha) {
  2160. float alpha =
  2161. stdNormComponents[numColorComponents + normOffset];
  2162. for (int c = 0, nc = normOffset; c < numColorComponents;
  2163. c++, nc++) {
  2164. spixel[c] = (short)
  2165. (stdNormComponents[nc] * alpha * 32767.0f + 0.5f);
  2166. }
  2167. spixel[numColorComponents] = (short) (alpha * 32767.0f + 0.5f);
  2168. } else {
  2169. for (int c = 0, nc = normOffset; c < numComponents;
  2170. c++, nc++) {
  2171. spixel[c] = (short)
  2172. (stdNormComponents[nc] * 32767.0f + 0.5f);
  2173. }
  2174. }
  2175. return spixel;
  2176. case DataBuffer.TYPE_FLOAT:
  2177. float[] fpixel;
  2178. if (obj == null) {
  2179. fpixel = new float[numComponents];
  2180. } else {
  2181. fpixel = (float[]) obj;
  2182. }
  2183. if (needAlpha) {
  2184. float alpha = normComponents[numColorComponents + normOffset];
  2185. for (int c = 0, nc = normOffset; c < numColorComponents;
  2186. c++, nc++) {
  2187. fpixel[c] = normComponents[nc] * alpha;
  2188. }
  2189. fpixel[numColorComponents] = alpha;
  2190. } else {
  2191. for (int c = 0, nc = normOffset; c < numComponents;
  2192. c++, nc++) {
  2193. fpixel[c] = normComponents[nc];
  2194. }
  2195. }
  2196. return fpixel;
  2197. case DataBuffer.TYPE_DOUBLE:
  2198. double[] dpixel;
  2199. if (obj == null) {
  2200. dpixel = new double[numComponents];
  2201. } else {
  2202. dpixel = (double[]) obj;
  2203. }
  2204. if (needAlpha) {
  2205. double alpha =
  2206. (double) (normComponents[numColorComponents + normOffset]);
  2207. for (int c = 0, nc = normOffset; c < numColorComponents;
  2208. c++, nc++) {
  2209. dpixel[c] = normComponents[nc] * alpha;
  2210. }
  2211. dpixel[numColorComponents] = alpha;
  2212. } else {
  2213. for (int c = 0, nc = normOffset; c < numComponents;
  2214. c++, nc++) {
  2215. dpixel[c] = (double) normComponents[nc];
  2216. }
  2217. }
  2218. return dpixel;
  2219. default:
  2220. throw new UnsupportedOperationException("This method has not been "+
  2221. "implemented for transferType " +
  2222. transferType);
  2223. }
  2224. }
  2225. /**
  2226. * Returns an array of all of the color/alpha components in normalized
  2227. * form, given a pixel in this <code>ColorModel</code>. The pixel
  2228. * value is specified by an array of data elements of type transferType
  2229. * passed in as an object reference. If pixel is not a primitive array
  2230. * of type transferType, a <code>ClassCastException</code> is thrown.
  2231. * An <code>ArrayIndexOutOfBoundsException</code> is thrown if
  2232. * <code>pixel</code> is not large enough to hold a pixel value for this
  2233. * <code>ColorModel</code>.
  2234. * Normalized components are float values between a per component minimum
  2235. * and maximum specified by the <code>ColorSpace</code> object for this
  2236. * <code>ColorModel</code>. If the
  2237. * <code>normComponents</code> array is <code>null</code>, a new array
  2238. * will be allocated. The <code>normComponents</code> array
  2239. * will be returned. Color/alpha components are stored in the
  2240. * <code>normComponents</code> array starting at
  2241. * <code>normOffset</code> (even if the array is allocated by this
  2242. * method). An <code>ArrayIndexOutOfBoundsException</code> is thrown
  2243. * if the <code>normComponents</code> array is not <code>null</code>
  2244. * and is not large enough to hold all the color and alpha components
  2245. * (starting at <code>normOffset</code>).
  2246. * <p>
  2247. * This method must be overrridden by a subclass if that subclass
  2248. * is designed to translate pixel sample values to color component values
  2249. * in a non-default way. The default translations implemented by this
  2250. * class is described in the class comments. Any subclass implementing
  2251. * a non-default translation must follow the constraints on allowable
  2252. * translations defined there.
  2253. * @param pixel the specified pixel
  2254. * @param normComponents an array to receive the normalized components
  2255. * @param normOffset the offset into the <code>normComponents</code>
  2256. * array at which to start storing normalized components
  2257. * @return an array containing normalized color and alpha
  2258. * components.
  2259. * @throws ClassCastException if <code>pixel</code> is not a primitive
  2260. * array of type transferType
  2261. * @throws ArrayIndexOutOfBoundsException if
  2262. * <code>normComponents</code> is not large enough to hold all
  2263. * color and alpha components starting at <code>normOffset</code>
  2264. * @throws ArrayIndexOutOfBoundsException if
  2265. * <code>pixel</code> is not large enough to hold a pixel
  2266. * value for this <code>ColorModel</code>.
  2267. * @since 1.4
  2268. */
  2269. public float[] getNormalizedComponents(Object pixel,
  2270. float[] normComponents,
  2271. int normOffset) {
  2272. if (normComponents == null) {
  2273. normComponents = new float[numComponents+normOffset];
  2274. }
  2275. switch (transferType) {
  2276. case DataBuffer.TYPE_BYTE:
  2277. byte[] bpixel = (byte[]) pixel;
  2278. for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
  2279. normComponents[nc] = ((float) (bpixel[c] & 0xff)) /
  2280. ((float) ((1 << nBits[c]) - 1));
  2281. }
  2282. break;
  2283. case DataBuffer.TYPE_USHORT:
  2284. short[] uspixel = (short[]) pixel;
  2285. for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
  2286. normComponents[nc] = ((float) (uspixel[c] & 0xffff)) /
  2287. ((float) ((1 << nBits[c]) - 1));
  2288. }
  2289. break;
  2290. case DataBuffer.TYPE_INT:
  2291. int[] ipixel = (int[]) pixel;
  2292. for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
  2293. normComponents[nc] = ((float) ipixel[c]) /
  2294. ((float) ((1 << nBits[c]) - 1));
  2295. }
  2296. break;
  2297. case DataBuffer.TYPE_SHORT:
  2298. short[] spixel = (short[]) pixel;
  2299. for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
  2300. normComponents[nc] = ((float) spixel[c]) / 32767.0f;
  2301. }
  2302. break;
  2303. case DataBuffer.TYPE_FLOAT:
  2304. float[] fpixel = (float[]) pixel;
  2305. for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
  2306. normComponents[nc] = fpixel[c];
  2307. }
  2308. break;
  2309. case DataBuffer.TYPE_DOUBLE:
  2310. double[] dpixel = (double[]) pixel;
  2311. for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
  2312. normComponents[nc] = (float) dpixel[c];
  2313. }
  2314. break;
  2315. default:
  2316. throw new UnsupportedOperationException("This method has not been "+
  2317. "implemented for transferType " +
  2318. transferType);
  2319. }
  2320. if (supportsAlpha && isAlphaPremultiplied) {
  2321. float alpha = normComponents[numColorComponents + normOffset];
  2322. if (alpha != 0.0f) {
  2323. float invAlpha = 1.0f / alpha;
  2324. for (int c = normOffset; c < numColorComponents + normOffset;
  2325. c++) {
  2326. normComponents[c] *= invAlpha;
  2327. }
  2328. }
  2329. }
  2330. if (min != null) {
  2331. // Normally (i.e. when this class is not subclassed to override
  2332. // this method), the test (min != null) will be equivalent to
  2333. // the test (nonStdScale). However, there is an unlikely, but
  2334. // possible case, in which this method is overridden, nonStdScale
  2335. // is set true by initScale(), the subclass method for some
  2336. // reason calls this superclass method, but the min and
  2337. // diffMinMax arrays were never initialized by setupLUTs(). In
  2338. // that case, the right thing to do is follow the intended
  2339. // semantics of this method, and rescale the color components
  2340. // only if the ColorSpace min/max were detected to be other
  2341. // than 0.0/1.0 by setupLUTs(). Note that this implies the
  2342. // transferType is byte, ushort, int, or short - i.e. components
  2343. // derived from float and double pixel data are never rescaled.
  2344. for (int c = 0; c < numColorComponents; c++) {
  2345. normComponents[c + normOffset] = min[c] +
  2346. diffMinMax[c] * normComponents[c + normOffset];
  2347. }
  2348. }
  2349. return normComponents;
  2350. }
  2351. /**
  2352. * Forces the raster data to match the state specified in the
  2353. * <CODE>isAlphaPremultiplied</CODE> variable, assuming the data
  2354. * is currently correctly described by this <CODE>ColorModel</CODE>.
  2355. * It may multiply or divide the color raster data by alpha, or
  2356. * do nothing if the data is in the correct state. If the data needs
  2357. * to be coerced, this method also returns an instance of
  2358. * this <CODE>ColorModel</CODE> with
  2359. * the <CODE>isAlphaPremultiplied</CODE> flag set appropriately.
  2360. * Since <code>ColorModel</code> can be subclassed, subclasses inherit
  2361. * the implementation of this method and if they don't override it
  2362. * then they throw an exception if they use an unsupported
  2363. * <code>transferType</code>.
  2364. *
  2365. * @throws NullPointerException if <code>raster</code> is
  2366. * <code>null</code> and data coercion is required.
  2367. * @throws UnsupportedOperationException if the transfer type of
  2368. * this <CODE>ComponentColorModel</CODE>
  2369. * is not one of the supported transfer types:
  2370. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  2371. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  2372. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  2373. */
  2374. public ColorModel coerceData (WritableRaster raster,
  2375. boolean isAlphaPremultiplied) {
  2376. if ((supportsAlpha == false) ||
  2377. (this.isAlphaPremultiplied == isAlphaPremultiplied))
  2378. {
  2379. // Nothing to do
  2380. return this;
  2381. }
  2382. int w = raster.getWidth();
  2383. int h = raster.getHeight();
  2384. int aIdx = raster.getNumBands() - 1;
  2385. float normAlpha;
  2386. int rminX = raster.getMinX();
  2387. int rY = raster.getMinY();
  2388. int rX;
  2389. if (isAlphaPremultiplied) {
  2390. switch (transferType) {
  2391. case DataBuffer.TYPE_BYTE: {
  2392. byte pixel[] = null;
  2393. byte zpixel[] = null;
  2394. float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
  2395. for (int y = 0; y < h; y++, rY++) {
  2396. rX = rminX;
  2397. for (int x = 0; x < w; x++, rX++) {
  2398. pixel = (byte[])raster.getDataElements(rX, rY,
  2399. pixel);
  2400. normAlpha = (pixel[aIdx] & 0xff) * alphaScale;
  2401. if (normAlpha != 0.0f) {
  2402. for (int c=0; c < aIdx; c++) {
  2403. pixel[c] = (byte)((pixel[c] & 0xff) *
  2404. normAlpha + 0.5f);
  2405. }
  2406. raster.setDataElements(rX, rY, pixel);
  2407. } else {
  2408. if (zpixel == null) {
  2409. zpixel = new byte[numComponents];
  2410. java.util.Arrays.fill(zpixel, (byte) 0);
  2411. }
  2412. raster.setDataElements(rX, rY, zpixel);
  2413. }
  2414. }
  2415. }
  2416. }
  2417. break;
  2418. case DataBuffer.TYPE_USHORT: {
  2419. short pixel[] = null;
  2420. short zpixel[] = null;
  2421. float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
  2422. for (int y = 0; y < h; y++, rY++) {
  2423. rX = rminX;
  2424. for (int x = 0; x < w; x++, rX++) {
  2425. pixel = (short[])raster.getDataElements(rX, rY,
  2426. pixel);
  2427. normAlpha = (pixel[aIdx] & 0xffff) * alphaScale;
  2428. if (normAlpha != 0.0f) {
  2429. for (int c=0; c < aIdx; c++) {
  2430. pixel[c] = (short)
  2431. ((pixel[c] & 0xffff) * normAlpha +
  2432. 0.5f);
  2433. }
  2434. raster.setDataElements(rX, rY, pixel);
  2435. } else {
  2436. if (zpixel == null) {
  2437. zpixel = new short[numComponents];
  2438. java.util.Arrays.fill(zpixel, (short) 0);
  2439. }
  2440. raster.setDataElements(rX, rY, zpixel);
  2441. }
  2442. }
  2443. }
  2444. }
  2445. break;
  2446. case DataBuffer.TYPE_INT: {
  2447. int pixel[] = null;
  2448. int zpixel[] = null;
  2449. float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
  2450. for (int y = 0; y < h; y++, rY++) {
  2451. rX = rminX;
  2452. for (int x = 0; x < w; x++, rX++) {
  2453. pixel = (int[])raster.getDataElements(rX, rY,
  2454. pixel);
  2455. normAlpha = pixel[aIdx] * alphaScale;
  2456. if (normAlpha != 0.0f) {
  2457. for (int c=0; c < aIdx; c++) {
  2458. pixel[c] = (int) (pixel[c] * normAlpha +
  2459. 0.5f);
  2460. }
  2461. raster.setDataElements(rX, rY, pixel);
  2462. } else {
  2463. if (zpixel == null) {
  2464. zpixel = new int[numComponents];
  2465. java.util.Arrays.fill(zpixel, 0);
  2466. }
  2467. raster.setDataElements(rX, rY, zpixel);
  2468. }
  2469. }
  2470. }
  2471. }
  2472. break;
  2473. case DataBuffer.TYPE_SHORT: {
  2474. short pixel[] = null;
  2475. short zpixel[] = null;
  2476. float alphaScale = 1.0f / 32767.0f;
  2477. for (int y = 0; y < h; y++, rY++) {
  2478. rX = rminX;
  2479. for (int x = 0; x < w; x++, rX++) {
  2480. pixel = (short[]) raster.getDataElements(rX, rY,
  2481. pixel);
  2482. normAlpha = pixel[aIdx] * alphaScale;
  2483. if (normAlpha != 0.0f) {
  2484. for (int c=0; c < aIdx; c++) {
  2485. pixel[c] = (short) (pixel[c] * normAlpha +
  2486. 0.5f);
  2487. }
  2488. raster.setDataElements(rX, rY, pixel);
  2489. } else {
  2490. if (zpixel == null) {
  2491. zpixel = new short[numComponents];
  2492. java.util.Arrays.fill(zpixel, (short) 0);
  2493. }
  2494. raster.setDataElements(rX, rY, zpixel);
  2495. }
  2496. }
  2497. }
  2498. }
  2499. break;
  2500. case DataBuffer.TYPE_FLOAT: {
  2501. float pixel[] = null;
  2502. float zpixel[] = null;
  2503. for (int y = 0; y < h; y++, rY++) {
  2504. rX = rminX;
  2505. for (int x = 0; x < w; x++, rX++) {
  2506. pixel = (float[]) raster.getDataElements(rX, rY,
  2507. pixel);
  2508. normAlpha = pixel[aIdx];
  2509. if (normAlpha != 0.0f) {
  2510. for (int c=0; c < aIdx; c++) {
  2511. pixel[c] *= normAlpha;
  2512. }
  2513. raster.setDataElements(rX, rY, pixel);
  2514. } else {
  2515. if (zpixel == null) {
  2516. zpixel = new float[numComponents];
  2517. java.util.Arrays.fill(zpixel, 0.0f);
  2518. }
  2519. raster.setDataElements(rX, rY, zpixel);
  2520. }
  2521. }
  2522. }
  2523. }
  2524. break;
  2525. case DataBuffer.TYPE_DOUBLE: {
  2526. double pixel[] = null;
  2527. double zpixel[] = null;
  2528. for (int y = 0; y < h; y++, rY++) {
  2529. rX = rminX;
  2530. for (int x = 0; x < w; x++, rX++) {
  2531. pixel = (double[]) raster.getDataElements(rX, rY,
  2532. pixel);
  2533. double dnormAlpha = pixel[aIdx];
  2534. if (dnormAlpha != 0.0) {
  2535. for (int c=0; c < aIdx; c++) {
  2536. pixel[c] *= dnormAlpha;
  2537. }
  2538. raster.setDataElements(rX, rY, pixel);
  2539. } else {
  2540. if (zpixel == null) {
  2541. zpixel = new double[numComponents];
  2542. java.util.Arrays.fill(zpixel, 0.0);
  2543. }
  2544. raster.setDataElements(rX, rY, zpixel);
  2545. }
  2546. }
  2547. }
  2548. }
  2549. break;
  2550. default:
  2551. throw new UnsupportedOperationException("This method has not been "+
  2552. "implemented for transferType " + transferType);
  2553. }
  2554. }
  2555. else {
  2556. // We are premultiplied and want to divide it out
  2557. switch (transferType) {
  2558. case DataBuffer.TYPE_BYTE: {
  2559. byte pixel[] = null;
  2560. float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
  2561. for (int y = 0; y < h; y++, rY++) {
  2562. rX = rminX;
  2563. for (int x = 0; x < w; x++, rX++) {
  2564. pixel = (byte[])raster.getDataElements(rX, rY,
  2565. pixel);
  2566. normAlpha = (pixel[aIdx] & 0xff) * alphaScale;
  2567. if (normAlpha != 0.0f) {
  2568. float invAlpha = 1.0f / normAlpha;
  2569. for (int c=0; c < aIdx; c++) {
  2570. pixel[c] = (byte)
  2571. ((pixel[c] & 0xff) * invAlpha + 0.5f);
  2572. }
  2573. raster.setDataElements(rX, rY, pixel);
  2574. }
  2575. }
  2576. }
  2577. }
  2578. break;
  2579. case DataBuffer.TYPE_USHORT: {
  2580. short pixel[] = null;
  2581. float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
  2582. for (int y = 0; y < h; y++, rY++) {
  2583. rX = rminX;
  2584. for (int x = 0; x < w; x++, rX++) {
  2585. pixel = (short[])raster.getDataElements(rX, rY,
  2586. pixel);
  2587. normAlpha = (pixel[aIdx] & 0xffff) * alphaScale;
  2588. if (normAlpha != 0.0f) {
  2589. float invAlpha = 1.0f / normAlpha;
  2590. for (int c=0; c < aIdx; c++) {
  2591. pixel[c] = (short)
  2592. ((pixel[c] & 0xffff) * invAlpha + 0.5f);
  2593. }
  2594. raster.setDataElements(rX, rY, pixel);
  2595. }
  2596. }
  2597. }
  2598. }
  2599. break;
  2600. case DataBuffer.TYPE_INT: {
  2601. int pixel[] = null;
  2602. float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
  2603. for (int y = 0; y < h; y++, rY++) {
  2604. rX = rminX;
  2605. for (int x = 0; x < w; x++, rX++) {
  2606. pixel = (int[])raster.getDataElements(rX, rY,
  2607. pixel);
  2608. normAlpha = pixel[aIdx] * alphaScale;
  2609. if (normAlpha != 0.0f) {
  2610. float invAlpha = 1.0f / normAlpha;
  2611. for (int c=0; c < aIdx; c++) {
  2612. pixel[c] = (int)
  2613. (pixel[c] * invAlpha + 0.5f);
  2614. }
  2615. raster.setDataElements(rX, rY, pixel);
  2616. }
  2617. }
  2618. }
  2619. }
  2620. break;
  2621. case DataBuffer.TYPE_SHORT: {
  2622. short pixel[] = null;
  2623. float alphaScale = 1.0f / 32767.0f;
  2624. for (int y = 0; y < h; y++, rY++) {
  2625. rX = rminX;
  2626. for (int x = 0; x < w; x++, rX++) {
  2627. pixel = (short[])raster.getDataElements(rX, rY,
  2628. pixel);
  2629. normAlpha = pixel[aIdx] * alphaScale;
  2630. if (normAlpha != 0.0f) {
  2631. float invAlpha = 1.0f / normAlpha;
  2632. for (int c=0; c < aIdx; c++) {
  2633. pixel[c] = (short)
  2634. (pixel[c] * invAlpha + 0.5f);
  2635. }
  2636. raster.setDataElements(rX, rY, pixel);
  2637. }
  2638. }
  2639. }
  2640. }
  2641. break;
  2642. case DataBuffer.TYPE_FLOAT: {
  2643. float pixel[] = null;
  2644. for (int y = 0; y < h; y++, rY++) {
  2645. rX = rminX;
  2646. for (int x = 0; x < w; x++, rX++) {
  2647. pixel = (float[])raster.getDataElements(rX, rY,
  2648. pixel);
  2649. normAlpha = pixel[aIdx];
  2650. if (normAlpha != 0.0f) {
  2651. float invAlpha = 1.0f / normAlpha;
  2652. for (int c=0; c < aIdx; c++) {
  2653. pixel[c] *= invAlpha;
  2654. }
  2655. raster.setDataElements(rX, rY, pixel);
  2656. }
  2657. }
  2658. }
  2659. }
  2660. break;
  2661. case DataBuffer.TYPE_DOUBLE: {
  2662. double pixel[] = null;
  2663. for (int y = 0; y < h; y++, rY++) {
  2664. rX = rminX;
  2665. for (int x = 0; x < w; x++, rX++) {
  2666. pixel = (double[])raster.getDataElements(rX, rY,
  2667. pixel);
  2668. double dnormAlpha = pixel[aIdx];
  2669. if (dnormAlpha != 0.0) {
  2670. double invAlpha = 1.0 / dnormAlpha;
  2671. for (int c=0; c < aIdx; c++) {
  2672. pixel[c] *= invAlpha;
  2673. }
  2674. raster.setDataElements(rX, rY, pixel);
  2675. }
  2676. }
  2677. }
  2678. }
  2679. break;
  2680. default:
  2681. throw new UnsupportedOperationException("This method has not been "+
  2682. "implemented for transferType " + transferType);
  2683. }
  2684. }
  2685. // Return a new color model
  2686. if (!signed) {
  2687. return new ComponentColorModel(colorSpace, nBits, supportsAlpha,
  2688. isAlphaPremultiplied, transparency,
  2689. transferType);
  2690. } else {
  2691. return new ComponentColorModel(colorSpace, supportsAlpha,
  2692. isAlphaPremultiplied, transparency,
  2693. transferType);
  2694. }
  2695. }
  2696. /**
  2697. * Returns true if <CODE>raster</CODE> is compatible with this
  2698. * <CODE>ColorModel</CODE> false if it is not.
  2699. *
  2700. * @param raster The <CODE>Raster</CODE> object to test for compatibility.
  2701. *
  2702. * @return <CODE>true</CODE> if <CODE>raster</CODE> is compatible with this
  2703. * <CODE>ColorModel</CODE>, <CODE>false</CODE> if it is not.
  2704. */
  2705. public boolean isCompatibleRaster(Raster raster) {
  2706. SampleModel sm = raster.getSampleModel();
  2707. if (sm instanceof ComponentSampleModel) {
  2708. if (sm.getNumBands() != getNumComponents()) {
  2709. return false;
  2710. }
  2711. for (int i=0; i<nBits.length; i++) {
  2712. if (sm.getSampleSize(i) < nBits[i]) {
  2713. return false;
  2714. }
  2715. }
  2716. return (raster.getTransferType() == transferType);
  2717. }
  2718. else {
  2719. return false;
  2720. }
  2721. }
  2722. /**
  2723. * Creates a <CODE>WritableRaster</CODE> with the specified width and height,
  2724. * that has a data layout (<CODE>SampleModel</CODE>) compatible with
  2725. * this <CODE>ColorModel</CODE>.
  2726. *
  2727. * @param w The width of the <CODE>WritableRaster</CODE> you want to create.
  2728. * @param h The height of the <CODE>WritableRaster</CODE> you want to create.
  2729. *
  2730. * @return A <CODE>WritableRaster</CODE> that is compatible with
  2731. * this <CODE>ColorModel</CODE>.
  2732. * @see WritableRaster
  2733. * @see SampleModel
  2734. */
  2735. public WritableRaster createCompatibleWritableRaster (int w, int h) {
  2736. int dataSize = w*h*numComponents;
  2737. WritableRaster raster = null;
  2738. switch (transferType) {
  2739. case DataBuffer.TYPE_BYTE:
  2740. case DataBuffer.TYPE_USHORT:
  2741. raster = Raster.createInterleavedRaster(transferType,
  2742. w, h,
  2743. numComponents, null);
  2744. break;
  2745. default:
  2746. SampleModel sm = createCompatibleSampleModel(w, h);
  2747. DataBuffer db = sm.createDataBuffer();
  2748. raster = Raster.createWritableRaster(sm, db, null);
  2749. }
  2750. return raster;
  2751. }
  2752. /**
  2753. * Creates a <CODE>SampleModel</CODE> with the specified width and height,
  2754. * that has a data layout compatible with this <CODE>ColorModel</CODE>.
  2755. *
  2756. * @param w The width of the <CODE>SampleModel</CODE> you want to create.
  2757. * @param h The height of the <CODE>SampleModel</CODE> you want to create.
  2758. *
  2759. * @return A <CODE>SampleModel</CODE> that is compatible with this
  2760. * <CODE>ColorModel</CODE>.
  2761. *
  2762. * @see SampleModel
  2763. */
  2764. public SampleModel createCompatibleSampleModel(int w, int h) {
  2765. int[] bandOffsets = new int[numComponents];
  2766. for (int i=0; i < numComponents; i++) {
  2767. bandOffsets[i] = i;
  2768. }
  2769. switch (transferType) {
  2770. case DataBuffer.TYPE_BYTE:
  2771. case DataBuffer.TYPE_USHORT:
  2772. return new PixelInterleavedSampleModel(transferType, w, h,
  2773. numComponents,
  2774. w*numComponents,
  2775. bandOffsets);
  2776. default:
  2777. return new ComponentSampleModel(transferType, w, h,
  2778. numComponents,
  2779. w*numComponents,
  2780. bandOffsets);
  2781. }
  2782. }
  2783. /**
  2784. * Checks whether or not the specified <CODE>SampleModel</CODE>
  2785. * is compatible with this <CODE>ColorModel</CODE>.
  2786. *
  2787. * @param sm The <CODE>SampleModel</CODE> to test for compatibility.
  2788. *
  2789. * @return <CODE>true</CODE> if the <CODE>SampleModel</CODE> is
  2790. * compatible with this <CODE>ColorModel</CODE>, <CODE>false</CODE>
  2791. * if it is not.
  2792. *
  2793. * @see SampleModel
  2794. */
  2795. public boolean isCompatibleSampleModel(SampleModel sm) {
  2796. if (!(sm instanceof ComponentSampleModel)) {
  2797. return false;
  2798. }
  2799. // Must have the same number of components
  2800. if (numComponents != sm.getNumBands()) {
  2801. return false;
  2802. }
  2803. if (sm.getTransferType() != transferType) {
  2804. return false;
  2805. }
  2806. return true;
  2807. }
  2808. /**
  2809. * Returns a <CODE>Raster</CODE> representing the alpha channel of an image,
  2810. * extracted from the input <CODE>Raster</CODE>.
  2811. * This method assumes that <CODE>Raster</CODE> objects associated with
  2812. * this <CODE>ColorModel</CODE> store the alpha band, if present, as
  2813. * the last band of image data. Returns null if there is no separate spatial
  2814. * alpha channel associated with this <CODE>ColorModel</CODE>.
  2815. * This method creates a new <CODE>Raster</CODE>, but will share the data
  2816. * array.
  2817. *
  2818. * @param raster The <CODE>WritableRaster</CODE> from which to extract the
  2819. * alpha channel.
  2820. *
  2821. * @return A <CODE>WritableRaster</CODE> containing the image's alpha channel.
  2822. *
  2823. */
  2824. public WritableRaster getAlphaRaster(WritableRaster raster) {
  2825. if (hasAlpha() == false) {
  2826. return null;
  2827. }
  2828. int x = raster.getMinX();
  2829. int y = raster.getMinY();
  2830. int[] band = new int[1];
  2831. band[0] = raster.getNumBands() - 1;
  2832. return raster.createWritableChild(x, y, raster.getWidth(),
  2833. raster.getHeight(), x, y,
  2834. band);
  2835. }
  2836. /**
  2837. * Compares this color model with another for equality.
  2838. *
  2839. * @param obj The object to compare with this color model.
  2840. * @return <CODE>true</CODE> if the color model objects are equal,
  2841. * <CODE>false</CODE> if they are not.
  2842. */
  2843. public boolean equals(Object obj) {
  2844. if (!super.equals(obj)) {
  2845. return false;
  2846. }
  2847. if (obj.getClass() != getClass()) {
  2848. return false;
  2849. }
  2850. return true;
  2851. }
  2852. }