1. /*
  2. * @(#)ComponentColorModel.java 1.66 03/01/23
  3. *
  4. * Copyright 2003 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. switch (transferType) {
  758. // Note: we do no clamping of the pixel data here - we
  759. // assume that the data is scaled properly
  760. case DataBuffer.TYPE_SHORT: {
  761. short sdata[] = (short[]) inData;
  762. float scalefactor = (float) ((1 << precision) - 1);
  763. if (needAlpha) {
  764. short s = sdata[numColorComponents];
  765. if (s != (short) 0) {
  766. return (int) ((((float) sdata[idx]) /
  767. ((float) s)) * scalefactor + 0.5f);
  768. } else {
  769. return 0;
  770. }
  771. } else {
  772. return (int) ((sdata[idx] / 32767.0f) * scalefactor + 0.5f);
  773. }
  774. }
  775. case DataBuffer.TYPE_FLOAT: {
  776. float fdata[] = (float[]) inData;
  777. float scalefactor = (float) ((1 << precision) - 1);
  778. if (needAlpha) {
  779. float f = fdata[numColorComponents];
  780. if (f != 0.0f) {
  781. return (int) (((fdata[idx] / f) * scalefactor) + 0.5f);
  782. } else {
  783. return 0;
  784. }
  785. } else {
  786. return (int) (fdata[idx] * scalefactor + 0.5f);
  787. }
  788. }
  789. case DataBuffer.TYPE_DOUBLE: {
  790. double ddata[] = (double[]) inData;
  791. double scalefactor = (double) ((1 << precision) - 1);
  792. if (needAlpha) {
  793. double d = ddata[numColorComponents];
  794. if (d != 0.0) {
  795. return (int) (((ddata[idx] / d) * scalefactor) + 0.5);
  796. } else {
  797. return 0;
  798. }
  799. } else {
  800. return (int) (ddata[idx] * scalefactor + 0.5);
  801. }
  802. }
  803. case DataBuffer.TYPE_BYTE:
  804. byte bdata[] = (byte[])inData;
  805. comp = bdata[idx] & 0xff;
  806. precision = 8;
  807. if (needAlpha) {
  808. alp = bdata[numColorComponents] & 0xff;
  809. }
  810. break;
  811. case DataBuffer.TYPE_USHORT:
  812. short usdata[] = (short[])inData;
  813. comp = usdata[idx]&0xffff;
  814. if (needAlpha) {
  815. alp = usdata[numColorComponents] & 0xffff;
  816. }
  817. break;
  818. case DataBuffer.TYPE_INT:
  819. int idata[] = (int[])inData;
  820. comp = idata[idx];
  821. if (needAlpha) {
  822. alp = idata[numColorComponents];
  823. }
  824. break;
  825. default:
  826. throw new
  827. UnsupportedOperationException("This method has not "+
  828. "been implemented for transferType " + transferType);
  829. }
  830. if (needAlpha) {
  831. if (alp != 0) {
  832. float scalefactor = (float) ((1 << precision) - 1);
  833. float fcomp = ((float) comp) / ((float) ((1<<nBits[idx]) - 1));
  834. float invalp = ((float) ((1<<nBits[numColorComponents]) - 1)) /
  835. ((float) alp);
  836. return (int) (fcomp * invalp * scalefactor + 0.5f);
  837. } else {
  838. return 0;
  839. }
  840. } else {
  841. if (nBits[idx] != precision) {
  842. float scalefactor = (float) ((1 << precision) - 1);
  843. float fcomp = ((float) comp) / ((float) ((1<<nBits[idx]) - 1));
  844. return (int) (fcomp * scalefactor + 0.5f);
  845. }
  846. return comp;
  847. }
  848. }
  849. private int getRGBComponent(Object inData, int idx) {
  850. if (needScaleInit) {
  851. initScale();
  852. }
  853. if (is_sRGB_stdScale) {
  854. return extractComponent(inData, idx, 8);
  855. } else if (is_LinearRGB_stdScale) {
  856. int lutidx = extractComponent(inData, idx, 16);
  857. return tosRGB8LUT[lutidx] & 0xff;
  858. } else if (is_ICCGray_stdScale) {
  859. int lutidx = extractComponent(inData, 0, 16);
  860. return tosRGB8LUT[lutidx] & 0xff;
  861. }
  862. // Not CS_sRGB, CS_LINEAR_RGB, or any TYPE_GRAY ICC_ColorSpace
  863. float[] norm = getNormalizedComponents(inData, null, 0);
  864. // Note that getNormalizedComponents returns non-premultiplied values
  865. float[] rgb = colorSpace.toRGB(norm);
  866. return (int) (rgb[idx] * 255.0f + 0.5f);
  867. }
  868. /**
  869. * Returns the red color component for the specified pixel, scaled
  870. * from 0 to 255 in the default RGB ColorSpace, sRGB. A color conversion
  871. * is done if necessary. The <CODE>pixel</CODE> value is specified by an array
  872. * of data elements of type <CODE>transferType</CODE> passed in as an object
  873. * reference. The returned value will be a non pre-multiplied value. If the
  874. * alpha is premultiplied, this method divides it out before returning
  875. * the value (if the alpha value is 0, the red value will be 0). Since
  876. * <code>ComponentColorModel</code> can be subclassed, subclasses
  877. * inherit the implementation of this method and if they don't override
  878. * it then they throw an exception if they use an unsupported
  879. * <code>transferType</code>.
  880. *
  881. * @param inData The pixel from which you want to get the red color component,
  882. * specified by an array of data elements of type <CODE>transferType</CODE>.
  883. *
  884. * @return The red color component for the specified pixel, as an int.
  885. *
  886. * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
  887. * of type <CODE>transferType</CODE>.
  888. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
  889. * large enough to hold a pixel value for this
  890. * <CODE>ColorModel</CODE>.
  891. * @throws UnsupportedOperationException If the transfer type of
  892. * this <CODE>ComponentColorModel</CODE>
  893. * is not one of the supported transfer types:
  894. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  895. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  896. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  897. */
  898. public int getRed(Object inData) {
  899. return getRGBComponent(inData, 0);
  900. }
  901. /**
  902. * Returns the green color component for the specified pixel, scaled
  903. * from 0 to 255 in the default RGB <CODE>ColorSpace</CODE>, sRGB.
  904. * A color conversion is done if necessary. The <CODE>pixel</CODE> value
  905. * is specified by an array of data elements of type <CODE>transferType</CODE>
  906. * passed in as an object reference. The returned value is a non pre-multiplied
  907. * value. If the alpha is premultiplied, this method divides it out before
  908. * returning the value (if the alpha value is 0, the green value will be 0).
  909. * Since <code>ComponentColorModel</code> can be subclassed,
  910. * subclasses inherit the implementation of this method and if they
  911. * don't override it then they throw an exception if they use an
  912. * unsupported <code>transferType</code>.
  913. *
  914. * @param inData The pixel from which you want to get the green color component,
  915. * specified by an array of data elements of type <CODE>transferType</CODE>.
  916. *
  917. * @return The green color component for the specified pixel, as an int.
  918. *
  919. * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
  920. * of type <CODE>transferType</CODE>.
  921. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
  922. * large enough to hold a pixel value for this
  923. * <CODE>ColorModel</CODE>.
  924. * @throws UnsupportedOperationException If the transfer type of
  925. * this <CODE>ComponentColorModel</CODE>
  926. * is not one of the supported transfer types:
  927. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  928. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  929. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  930. */
  931. public int getGreen(Object inData) {
  932. return getRGBComponent(inData, 1);
  933. }
  934. /**
  935. * Returns the blue color component for the specified pixel, scaled
  936. * from 0 to 255 in the default RGB <CODE>ColorSpace</CODE>, sRGB.
  937. * A color conversion is done if necessary. The <CODE>pixel</CODE> value is
  938. * specified by an array of data elements of type <CODE>transferType</CODE>
  939. * passed in as an object reference. The returned value is a non pre-multiplied
  940. * value. If the alpha is premultiplied, this method divides it out before
  941. * returning the value (if the alpha value is 0, the blue value will be 0).
  942. * Since <code>ComponentColorModel</code> can be subclassed,
  943. * subclasses inherit the implementation of this method and if they
  944. * don't override it then they throw an exception if they use an
  945. * unsupported <code>transferType</code>.
  946. *
  947. * @param inData The pixel from which you want to get the blue color component,
  948. * specified by an array of data elements of type <CODE>transferType</CODE>.
  949. *
  950. * @return The blue color component for the specified pixel, as an int.
  951. *
  952. * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
  953. * of type <CODE>transferType</CODE>.
  954. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
  955. * large enough to hold a pixel value for this
  956. * <CODE>ColorModel</CODE>.
  957. * @throws UnsupportedOperationException If the transfer type of
  958. * this <CODE>ComponentColorModel</CODE>
  959. * is not one of the supported transfer types:
  960. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  961. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  962. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  963. */
  964. public int getBlue(Object inData) {
  965. return getRGBComponent(inData, 2);
  966. }
  967. /**
  968. * Returns the alpha component for the specified pixel, scaled from
  969. * 0 to 255. The pixel value is specified by an array of data
  970. * elements of type <CODE>transferType</CODE> passed in as an
  971. * object reference. Since <code>ComponentColorModel</code> can be
  972. * subclassed, subclasses inherit the
  973. * implementation of this method and if they don't override it then
  974. * they throw an exception if they use an unsupported
  975. * <code>transferType</code>.
  976. *
  977. * @param inData The pixel from which you want to get the alpha component,
  978. * specified by an array of data elements of type <CODE>transferType</CODE>.
  979. *
  980. * @return The alpha component for the specified pixel, as an int.
  981. *
  982. * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
  983. * of type <CODE>transferType</CODE>.
  984. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
  985. * large enough to hold a pixel value for this
  986. * <CODE>ColorModel</CODE>.
  987. * @throws UnsupportedOperationException If the transfer type of
  988. * this <CODE>ComponentColorModel</CODE>
  989. * is not one of the supported transfer types:
  990. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  991. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  992. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  993. */
  994. public int getAlpha(Object inData) {
  995. if (supportsAlpha == false) {
  996. return 255;
  997. }
  998. int alpha = 0;
  999. int aIdx = numColorComponents;
  1000. switch (transferType) {
  1001. case DataBuffer.TYPE_SHORT:
  1002. short sdata[] = (short[])inData;
  1003. alpha = (int) ((sdata[aIdx] / 32767.0f) * 255.0f + 0.5f);
  1004. return alpha;
  1005. case DataBuffer.TYPE_FLOAT:
  1006. float fdata[] = (float[])inData;
  1007. alpha = (int) (fdata[aIdx] * 255.0f + 0.5f);
  1008. return alpha;
  1009. case DataBuffer.TYPE_DOUBLE:
  1010. double ddata[] = (double[])inData;
  1011. alpha = (int) (ddata[aIdx] * 255.0 + 0.5);
  1012. return alpha;
  1013. case DataBuffer.TYPE_BYTE:
  1014. byte bdata[] = (byte[])inData;
  1015. alpha = bdata[aIdx] & 0xff;
  1016. break;
  1017. case DataBuffer.TYPE_USHORT:
  1018. short usdata[] = (short[])inData;
  1019. alpha = usdata[aIdx]&0xffff;
  1020. break;
  1021. case DataBuffer.TYPE_INT:
  1022. int idata[] = (int[])inData;
  1023. alpha = idata[aIdx];
  1024. break;
  1025. default:
  1026. throw new
  1027. UnsupportedOperationException("This method has not "+
  1028. "been implemented for transferType " + transferType);
  1029. }
  1030. if (nBits[aIdx] == 8) {
  1031. return alpha;
  1032. } else {
  1033. return (int)
  1034. ((((float) alpha) / ((float) ((1 << nBits[aIdx]) - 1))) *
  1035. 255.0f + 0.5f);
  1036. }
  1037. }
  1038. /**
  1039. * Returns the color/alpha components for the specified pixel in the
  1040. * default RGB color model format. A color conversion is done if
  1041. * necessary. The pixel value is specified by an
  1042. * array of data elements of type <CODE>transferType</CODE> passed
  1043. * in as an object reference.
  1044. * The returned value is in a non pre-multiplied format. If
  1045. * the alpha is premultiplied, this method divides it out of the
  1046. * color components (if the alpha value is 0, the color values will be 0).
  1047. * Since <code>ComponentColorModel</code> can be subclassed,
  1048. * subclasses inherit the implementation of this method and if they
  1049. * don't override it then they throw an exception if they use an
  1050. * unsupported <code>transferType</code>.
  1051. *
  1052. * @param inData The pixel from which you want to get the color/alpha components,
  1053. * specified by an array of data elements of type <CODE>transferType</CODE>.
  1054. *
  1055. * @return The color/alpha components for the specified pixel, as an int.
  1056. *
  1057. * @throws ClassCastException If <CODE>inData</CODE> is not a primitive array
  1058. * of type <CODE>transferType</CODE>.
  1059. * @throws ArrayIndexOutOfBoundsException if <CODE>inData</CODE> is not
  1060. * large enough to hold a pixel value for this
  1061. * <CODE>ColorModel</CODE>.
  1062. * @throws UnsupportedOperationException If the transfer type of
  1063. * this <CODE>ComponentColorModel</CODE>
  1064. * is not one of the supported transfer types:
  1065. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  1066. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  1067. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  1068. * @see ColorModel#getRGBdefault
  1069. */
  1070. public int getRGB(Object inData) {
  1071. if (needScaleInit) {
  1072. initScale();
  1073. }
  1074. if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
  1075. return (getAlpha(inData) << 24)
  1076. | (getRed(inData) << 16)
  1077. | (getGreen(inData) << 8)
  1078. | (getBlue(inData));
  1079. } else if (colorSpaceType == ColorSpace.TYPE_GRAY) {
  1080. int gray = getRed(inData); // Red sRGB component should equal
  1081. // green and blue components
  1082. return (getAlpha(inData) << 24)
  1083. | (gray << 16)
  1084. | (gray << 8)
  1085. | gray;
  1086. }
  1087. float[] norm = getNormalizedComponents(inData, null, 0);
  1088. // Note that getNormalizedComponents returns non-premult values
  1089. float[] rgb = colorSpace.toRGB(norm);
  1090. return (getAlpha(inData) << 24)
  1091. | (((int) (rgb[0] * 255.0f + 0.5f)) << 16)
  1092. | (((int) (rgb[1] * 255.0f + 0.5f)) << 8)
  1093. | (((int) (rgb[2] * 255.0f + 0.5f)) << 0);
  1094. }
  1095. /**
  1096. * Returns a data element array representation of a pixel in this
  1097. * <CODE>ColorModel</CODE>, given an integer pixel representation
  1098. * in the default RGB color model.
  1099. * This array can then be passed to the <CODE>setDataElements</CODE>
  1100. * method of a <CODE>WritableRaster</CODE> object. If the
  1101. * <CODE>pixel</CODE>
  1102. * parameter is null, a new array is allocated. Since
  1103. * <code>ComponentColorModel</code> can be subclassed, subclasses
  1104. * inherit the implementation of this method and if they don't
  1105. * override it then
  1106. * they throw an exception if they use an unsupported
  1107. * <code>transferType</code>.
  1108. *
  1109. * @param rgb the integer representation of the pixel in the RGB
  1110. * color model
  1111. * @param pixel the specified pixel
  1112. * @return The data element array representation of a pixel
  1113. * in this <CODE>ColorModel</CODE>.
  1114. * @throws ClassCastException If <CODE>pixel</CODE> is not null and
  1115. * is not a primitive array of type <CODE>transferType</CODE>.
  1116. * @throws ArrayIndexOutOfBoundsException If <CODE>pixel</CODE> is
  1117. * not large enough to hold a pixel value for this
  1118. * <CODE>ColorModel</CODE>.
  1119. * @throws UnsupportedOperationException If the transfer type of
  1120. * this <CODE>ComponentColorModel</CODE>
  1121. * is not one of the supported transfer types:
  1122. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  1123. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  1124. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  1125. *
  1126. * @see WritableRaster#setDataElements
  1127. * @see SampleModel#setDataElements
  1128. */
  1129. public Object getDataElements(int rgb, Object pixel) {
  1130. // REMIND: Use rendering hints?
  1131. int red, grn, blu, alp;
  1132. red = (rgb>>16) & 0xff;
  1133. grn = (rgb>>8) & 0xff;
  1134. blu = rgb & 0xff;
  1135. if (needScaleInit) {
  1136. initScale();
  1137. }
  1138. if (signed) {
  1139. // Handle SHORT, FLOAT, & DOUBLE here
  1140. switch(transferType) {
  1141. case DataBuffer.TYPE_SHORT:
  1142. {
  1143. short sdata[];
  1144. if (pixel == null) {
  1145. sdata = new short[numComponents];
  1146. } else {
  1147. sdata = (short[])pixel;
  1148. }
  1149. float factor;
  1150. if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
  1151. factor = 32767.0f / 255.0f;
  1152. if (is_LinearRGB_stdScale) {
  1153. red = fromsRGB8LUT16[red] & 0xffff;
  1154. grn = fromsRGB8LUT16[grn] & 0xffff;
  1155. blu = fromsRGB8LUT16[blu] & 0xffff;
  1156. factor = 32767.0f / 65535.0f;
  1157. }
  1158. if (supportsAlpha) {
  1159. alp = (rgb>>24) & 0xff;
  1160. sdata[3] =
  1161. (short) (alp * (32767.0f / 255.0f) + 0.5f);
  1162. if (isAlphaPremultiplied) {
  1163. factor = alp * factor * (1.0f / 255.0f);
  1164. }
  1165. }
  1166. sdata[0] = (short) (red * factor + 0.5f);
  1167. sdata[1] = (short) (grn * factor + 0.5f);
  1168. sdata[2] = (short) (blu * factor + 0.5f);
  1169. } else if (is_LinearGray_stdScale) {
  1170. red = fromsRGB8LUT16[red] & 0xffff;
  1171. grn = fromsRGB8LUT16[grn] & 0xffff;
  1172. blu = fromsRGB8LUT16[blu] & 0xffff;
  1173. float gray = ((0.2125f * red) +
  1174. (0.7154f * grn) +
  1175. (0.0721f * blu)) / 65535.0f;
  1176. factor = 32767.0f;
  1177. if (supportsAlpha) {
  1178. alp = (rgb>>24) & 0xff;
  1179. sdata[1] =
  1180. (short) (alp * (32767.0f / 255.0f) + 0.5f);
  1181. if (isAlphaPremultiplied) {
  1182. factor = alp * factor * (1.0f / 255.0f);
  1183. }
  1184. }
  1185. sdata[0] = (short) (gray * factor + 0.5f);
  1186. } else if (is_ICCGray_stdScale) {
  1187. red = fromsRGB8LUT16[red] & 0xffff;
  1188. grn = fromsRGB8LUT16[grn] & 0xffff;
  1189. blu = fromsRGB8LUT16[blu] & 0xffff;
  1190. int gray = (int) ((0.2125f * red) +
  1191. (0.7154f * grn) +
  1192. (0.0721f * blu) + 0.5f);
  1193. gray = fromLinearGray16ToOtherGray16LUT[gray] & 0xffff;
  1194. factor = 32767.0f / 65535.0f;
  1195. if (supportsAlpha) {
  1196. alp = (rgb>>24) & 0xff;
  1197. sdata[1] =
  1198. (short) (alp * (32767.0f / 255.0f) + 0.5f);
  1199. if (isAlphaPremultiplied) {
  1200. factor = alp * factor * (1.0f / 255.0f);
  1201. }
  1202. }
  1203. sdata[0] = (short) (gray * factor + 0.5f);
  1204. } else {
  1205. factor = 1.0f / 255.0f;
  1206. float norm[] = new float[3];
  1207. norm[0] = red * factor;
  1208. norm[1] = grn * factor;
  1209. norm[2] = blu * factor;
  1210. norm = colorSpace.fromRGB(norm);
  1211. if (nonStdScale) {
  1212. for (int i = 0; i < numColorComponents; i++) {
  1213. norm[i] = (norm[i] - compOffset[i]) *
  1214. compScale[i];
  1215. // REMIND: need to analyze whether this
  1216. // clamping is necessary
  1217. if (norm[i] < 0.0f) {
  1218. norm[i] = 0.0f;
  1219. }
  1220. if (norm[i] > 1.0f) {
  1221. norm[i] = 1.0f;
  1222. }
  1223. }
  1224. }
  1225. factor = 32767.0f;
  1226. if (supportsAlpha) {
  1227. alp = (rgb>>24) & 0xff;
  1228. sdata[numColorComponents] =
  1229. (short) (alp * (32767.0f / 255.0f) + 0.5f);
  1230. if (isAlphaPremultiplied) {
  1231. factor *= alp * (1.0f / 255.0f);
  1232. }
  1233. }
  1234. for (int i = 0; i < numColorComponents; i++) {
  1235. sdata[i] = (short) (norm[i] * factor + 0.5f);
  1236. }
  1237. }
  1238. return sdata;
  1239. }
  1240. case DataBuffer.TYPE_FLOAT:
  1241. {
  1242. float fdata[];
  1243. if (pixel == null) {
  1244. fdata = new float[numComponents];
  1245. } else {
  1246. fdata = (float[])pixel;
  1247. }
  1248. float factor;
  1249. if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
  1250. if (is_LinearRGB_stdScale) {
  1251. red = fromsRGB8LUT16[red] & 0xffff;
  1252. grn = fromsRGB8LUT16[grn] & 0xffff;
  1253. blu = fromsRGB8LUT16[blu] & 0xffff;
  1254. factor = 1.0f / 65535.0f;
  1255. } else {
  1256. factor = 1.0f / 255.0f;
  1257. }
  1258. if (supportsAlpha) {
  1259. alp = (rgb>>24) & 0xff;
  1260. fdata[3] = alp * (1.0f / 255.0f);
  1261. if (isAlphaPremultiplied) {
  1262. factor *= fdata[3];
  1263. }
  1264. }
  1265. fdata[0] = red * factor;
  1266. fdata[1] = grn * factor;
  1267. fdata[2] = blu * factor;
  1268. } else if (is_LinearGray_stdScale) {
  1269. red = fromsRGB8LUT16[red] & 0xffff;
  1270. grn = fromsRGB8LUT16[grn] & 0xffff;
  1271. blu = fromsRGB8LUT16[blu] & 0xffff;
  1272. fdata[0] = ((0.2125f * red) +
  1273. (0.7154f * grn) +
  1274. (0.0721f * blu)) / 65535.0f;
  1275. if (supportsAlpha) {
  1276. alp = (rgb>>24) & 0xff;
  1277. fdata[1] = alp * (1.0f / 255.0f);
  1278. if (isAlphaPremultiplied) {
  1279. fdata[0] *= fdata[1];
  1280. }
  1281. }
  1282. } else if (is_ICCGray_stdScale) {
  1283. red = fromsRGB8LUT16[red] & 0xffff;
  1284. grn = fromsRGB8LUT16[grn] & 0xffff;
  1285. blu = fromsRGB8LUT16[blu] & 0xffff;
  1286. int gray = (int) ((0.2125f * red) +
  1287. (0.7154f * grn) +
  1288. (0.0721f * blu) + 0.5f);
  1289. fdata[0] = (fromLinearGray16ToOtherGray16LUT[gray] &
  1290. 0xffff) / 65535.0f;
  1291. if (supportsAlpha) {
  1292. alp = (rgb>>24) & 0xff;
  1293. fdata[1] = alp * (1.0f / 255.0f);
  1294. if (isAlphaPremultiplied) {
  1295. fdata[0] *= fdata[1];
  1296. }
  1297. }
  1298. } else {
  1299. float norm[] = new float[3];
  1300. factor = 1.0f / 255.0f;
  1301. norm[0] = red * factor;
  1302. norm[1] = grn * factor;
  1303. norm[2] = blu * factor;
  1304. norm = colorSpace.fromRGB(norm);
  1305. if (supportsAlpha) {
  1306. alp = (rgb>>24) & 0xff;
  1307. fdata[numColorComponents] = alp * factor;
  1308. if (isAlphaPremultiplied) {
  1309. factor *= alp;
  1310. for (int i = 0; i < numColorComponents; i++) {
  1311. norm[i] *= factor;
  1312. }
  1313. }
  1314. }
  1315. for (int i = 0; i < numColorComponents; i++) {
  1316. fdata[i] = norm[i];
  1317. }
  1318. }
  1319. return fdata;
  1320. }
  1321. case DataBuffer.TYPE_DOUBLE:
  1322. {
  1323. double ddata[];
  1324. if (pixel == null) {
  1325. ddata = new double[numComponents];
  1326. } else {
  1327. ddata = (double[])pixel;
  1328. }
  1329. if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
  1330. double factor;
  1331. if (is_LinearRGB_stdScale) {
  1332. red = fromsRGB8LUT16[red] & 0xffff;
  1333. grn = fromsRGB8LUT16[grn] & 0xffff;
  1334. blu = fromsRGB8LUT16[blu] & 0xffff;
  1335. factor = 1.0 / 65535.0;
  1336. } else {
  1337. factor = 1.0 / 255.0;
  1338. }
  1339. if (supportsAlpha) {
  1340. alp = (rgb>>24) & 0xff;
  1341. ddata[3] = alp * (1.0 / 255.0);
  1342. if (isAlphaPremultiplied) {
  1343. factor *= ddata[3];
  1344. }
  1345. }
  1346. ddata[0] = red * factor;
  1347. ddata[1] = grn * factor;
  1348. ddata[2] = blu * factor;
  1349. } else if (is_LinearGray_stdScale) {
  1350. red = fromsRGB8LUT16[red] & 0xffff;
  1351. grn = fromsRGB8LUT16[grn] & 0xffff;
  1352. blu = fromsRGB8LUT16[blu] & 0xffff;
  1353. ddata[0] = ((0.2125 * red) +
  1354. (0.7154 * grn) +
  1355. (0.0721 * blu)) / 65535.0;
  1356. if (supportsAlpha) {
  1357. alp = (rgb>>24) & 0xff;
  1358. ddata[1] = alp * (1.0 / 255.0);
  1359. if (isAlphaPremultiplied) {
  1360. ddata[0] *= ddata[1];
  1361. }
  1362. }
  1363. } else if (is_ICCGray_stdScale) {
  1364. red = fromsRGB8LUT16[red] & 0xffff;
  1365. grn = fromsRGB8LUT16[grn] & 0xffff;
  1366. blu = fromsRGB8LUT16[blu] & 0xffff;
  1367. int gray = (int) ((0.2125f * red) +
  1368. (0.7154f * grn) +
  1369. (0.0721f * blu) + 0.5f);
  1370. ddata[0] = (fromLinearGray16ToOtherGray16LUT[gray] &
  1371. 0xffff) / 65535.0;
  1372. if (supportsAlpha) {
  1373. alp = (rgb>>24) & 0xff;
  1374. ddata[1] = alp * (1.0 / 255.0);
  1375. if (isAlphaPremultiplied) {
  1376. ddata[0] *= ddata[1];
  1377. }
  1378. }
  1379. } else {
  1380. float factor = 1.0f / 255.0f;
  1381. float norm[] = new float[3];
  1382. norm[0] = red * factor;
  1383. norm[1] = grn * factor;
  1384. norm[2] = blu * factor;
  1385. norm = colorSpace.fromRGB(norm);
  1386. if (supportsAlpha) {
  1387. alp = (rgb>>24) & 0xff;
  1388. ddata[numColorComponents] = alp * (1.0 / 255.0);
  1389. if (isAlphaPremultiplied) {
  1390. factor *= alp;
  1391. for (int i = 0; i < numColorComponents; i++) {
  1392. norm[i] *= factor;
  1393. }
  1394. }
  1395. }
  1396. for (int i = 0; i < numColorComponents; i++) {
  1397. ddata[i] = norm[i];
  1398. }
  1399. }
  1400. return ddata;
  1401. }
  1402. }
  1403. }
  1404. // Handle BYTE, USHORT, & INT here
  1405. //REMIND: maybe more efficient not to use int array for
  1406. //DataBuffer.TYPE_USHORT and DataBuffer.TYPE_INT
  1407. int intpixel[];
  1408. if (transferType == DataBuffer.TYPE_INT &&
  1409. pixel != null) {
  1410. intpixel = (int[])pixel;
  1411. } else {
  1412. intpixel = new int[numComponents];
  1413. }
  1414. if (is_sRGB_stdScale || is_LinearRGB_stdScale) {
  1415. int precision;
  1416. float factor;
  1417. if (is_LinearRGB_stdScale) {
  1418. if (transferType == DataBuffer.TYPE_BYTE) {
  1419. red = fromsRGB8LUT8[red] & 0xff;
  1420. grn = fromsRGB8LUT8[grn] & 0xff;
  1421. blu = fromsRGB8LUT8[blu] & 0xff;
  1422. precision = 8;
  1423. factor = 1.0f / 255.0f;
  1424. } else {
  1425. red = fromsRGB8LUT16[red] & 0xffff;
  1426. grn = fromsRGB8LUT16[grn] & 0xffff;
  1427. blu = fromsRGB8LUT16[blu] & 0xffff;
  1428. precision = 16;
  1429. factor = 1.0f / 65535.0f;
  1430. }
  1431. } else {
  1432. precision = 8;
  1433. factor = 1.0f / 255.0f;
  1434. }
  1435. if (supportsAlpha) {
  1436. alp = (rgb>>24)&0xff;
  1437. if (nBits[3] == 8) {
  1438. intpixel[3] = alp;
  1439. }
  1440. else {
  1441. intpixel[3] = (int)
  1442. (alp * (1.0f / 255.0f) * ((1<<nBits[3]) - 1) + 0.5f);
  1443. }
  1444. if (isAlphaPremultiplied) {
  1445. factor *= (alp * (1.0f / 255.0f));
  1446. precision = -1; // force component calculations below
  1447. }
  1448. }
  1449. if (nBits[0] == precision) {
  1450. intpixel[0] = red;
  1451. }
  1452. else {
  1453. intpixel[0] = (int) (red * factor * ((1<<nBits[0]) - 1) + 0.5f);
  1454. }
  1455. if (nBits[1] == precision) {
  1456. intpixel[1] = (int)(grn);
  1457. }
  1458. else {
  1459. intpixel[1] = (int) (grn * factor * ((1<<nBits[1]) - 1) + 0.5f);
  1460. }
  1461. if (nBits[2] == precision) {
  1462. intpixel[2] = (int)(blu);
  1463. }
  1464. else {
  1465. intpixel[2] = (int) (blu * factor * ((1<<nBits[2]) - 1) + 0.5f);
  1466. }
  1467. } else if (is_LinearGray_stdScale) {
  1468. red = fromsRGB8LUT16[red] & 0xffff;
  1469. grn = fromsRGB8LUT16[grn] & 0xffff;
  1470. blu = fromsRGB8LUT16[blu] & 0xffff;
  1471. float gray = ((0.2125f * red) +
  1472. (0.7154f * grn) +
  1473. (0.0721f * blu)) / 65535.0f;
  1474. if (supportsAlpha) {
  1475. alp = (rgb>>24) & 0xff;
  1476. if (nBits[1] == 8) {
  1477. intpixel[1] = alp;
  1478. } else {
  1479. intpixel[1] = (int) (alp * (1.0f / 255.0f) *
  1480. ((1 << nBits[1]) - 1) + 0.5f);
  1481. }
  1482. if (isAlphaPremultiplied) {
  1483. gray *= (alp * (1.0f / 255.0f));
  1484. }
  1485. }
  1486. intpixel[0] = (int) (gray * ((1 << nBits[0]) - 1) + 0.5f);
  1487. } else if (is_ICCGray_stdScale) {
  1488. red = fromsRGB8LUT16[red] & 0xffff;
  1489. grn = fromsRGB8LUT16[grn] & 0xffff;
  1490. blu = fromsRGB8LUT16[blu] & 0xffff;
  1491. int gray16 = (int) ((0.2125f * red) +
  1492. (0.7154f * grn) +
  1493. (0.0721f * blu) + 0.5f);
  1494. float gray = (fromLinearGray16ToOtherGray16LUT[gray16] &
  1495. 0xffff) / 65535.0f;
  1496. if (supportsAlpha) {
  1497. alp = (rgb>>24) & 0xff;
  1498. if (nBits[1] == 8) {
  1499. intpixel[1] = alp;
  1500. } else {
  1501. intpixel[1] = (int) (alp * (1.0f / 255.0f) *
  1502. ((1 << nBits[1]) - 1) + 0.5f);
  1503. }
  1504. if (isAlphaPremultiplied) {
  1505. gray *= (alp * (1.0f / 255.0f));
  1506. }
  1507. }
  1508. intpixel[0] = (int) (gray * ((1 << nBits[0]) - 1) + 0.5f);
  1509. } else {
  1510. // Need to convert the color
  1511. float[] norm = new float[3];
  1512. float factor = 1.0f / 255.0f;
  1513. norm[0] = red * factor;
  1514. norm[1] = grn * factor;
  1515. norm[2] = blu * factor;
  1516. norm = colorSpace.fromRGB(norm);
  1517. if (nonStdScale) {
  1518. for (int i = 0; i < numColorComponents; i++) {
  1519. norm[i] = (norm[i] - compOffset[i]) *
  1520. compScale[i];
  1521. // REMIND: need to analyze whether this
  1522. // clamping is necessary
  1523. if (norm[i] < 0.0f) {
  1524. norm[i] = 0.0f;
  1525. }
  1526. if (norm[i] > 1.0f) {
  1527. norm[i] = 1.0f;
  1528. }
  1529. }
  1530. }
  1531. if (supportsAlpha) {
  1532. alp = (rgb>>24) & 0xff;
  1533. if (nBits[numColorComponents] == 8) {
  1534. intpixel[numColorComponents] = alp;
  1535. }
  1536. else {
  1537. intpixel[numColorComponents] =
  1538. (int) (alp * factor *
  1539. ((1<<nBits[numColorComponents]) - 1) + 0.5f);
  1540. }
  1541. if (isAlphaPremultiplied) {
  1542. factor *= alp;
  1543. for (int i = 0; i < numColorComponents; i++) {
  1544. norm[i] *= factor;
  1545. }
  1546. }
  1547. }
  1548. for (int i = 0; i < numColorComponents; i++) {
  1549. intpixel[i] = (int) (norm[i] * ((1<<nBits[i]) - 1) + 0.5f);
  1550. }
  1551. }
  1552. switch (transferType) {
  1553. case DataBuffer.TYPE_BYTE: {
  1554. byte bdata[];
  1555. if (pixel == null) {
  1556. bdata = new byte[numComponents];
  1557. } else {
  1558. bdata = (byte[])pixel;
  1559. }
  1560. for (int i = 0; i < numComponents; i++) {
  1561. bdata[i] = (byte)(0xff&intpixel[i]);
  1562. }
  1563. return bdata;
  1564. }
  1565. case DataBuffer.TYPE_USHORT:{
  1566. short sdata[];
  1567. if (pixel == null) {
  1568. sdata = new short[numComponents];
  1569. } else {
  1570. sdata = (short[])pixel;
  1571. }
  1572. for (int i = 0; i < numComponents; i++) {
  1573. sdata[i] = (short)(intpixel[i]&0xffff);
  1574. }
  1575. return sdata;
  1576. }
  1577. case DataBuffer.TYPE_INT:
  1578. if (maxBits > 23) {
  1579. // fix 4412670 - for components of 24 or more bits
  1580. // some calculations done above with float precision
  1581. // may lose enough precision that the integer result
  1582. // overflows nBits, so we need to clamp.
  1583. for (int i = 0; i < numComponents; i++) {
  1584. if (intpixel[i] > ((1<<nBits[i]) - 1)) {
  1585. intpixel[i] = (1<<nBits[i]) - 1;
  1586. }
  1587. }
  1588. }
  1589. return intpixel;
  1590. }
  1591. throw new IllegalArgumentException("This method has not been "+
  1592. "implemented for transferType " + transferType);
  1593. }
  1594. /** Returns an array of unnormalized color/alpha components given a pixel
  1595. * in this <CODE>ColorModel</CODE>.
  1596. * An IllegalArgumentException is thrown if the component value for this
  1597. * <CODE>ColorModel</CODE> is not conveniently representable in the
  1598. * unnormalized form. Color/alpha components are stored
  1599. * in the <CODE>components</CODE> array starting at <CODE>offset</CODE>
  1600. * (even if the array is allocated by this method).
  1601. *
  1602. * @param pixel The pixel value specified as an integer.
  1603. * @param components An integer array in which to store the unnormalized
  1604. * color/alpha components. If the <CODE>components</CODE> array is null,
  1605. * a new array is allocated.
  1606. * @param offset An offset into the <CODE>components</CODE> array.
  1607. *
  1608. * @return The components array.
  1609. *
  1610. * @throws IllegalArgumentException If there is more than one
  1611. * component in this <CODE>ColorModel</CODE>.
  1612. * @throws IllegalArgumentException If this
  1613. * <CODE>ColorModel</CODE> does not support the unnormalized form
  1614. * @throws ArrayIndexOutOfBoundsException If the <CODE>components</CODE>
  1615. * array is not null and is not large enough to hold all the color and
  1616. * alpha components (starting at offset).
  1617. */
  1618. public int[] getComponents(int pixel, int[] components, int offset) {
  1619. if (numComponents > 1) {
  1620. throw new
  1621. IllegalArgumentException("More than one component per pixel");
  1622. }
  1623. if (needScaleInit) {
  1624. initScale();
  1625. }
  1626. if (noUnnorm) {
  1627. throw new
  1628. IllegalArgumentException(
  1629. "This ColorModel does not support the unnormalized form");
  1630. }
  1631. if (components == null) {
  1632. components = new int[offset+1];
  1633. }
  1634. components[offset+0] = (pixel & ((1<<nBits[0]) - 1));
  1635. return components;
  1636. }
  1637. /**
  1638. * Returns an array of unnormalized color/alpha components given a pixel
  1639. * in this <CODE>ColorModel</CODE>. The pixel value is specified by an
  1640. * array of data elements of type <CODE>transferType</CODE> passed in as
  1641. * an object reference.
  1642. * An IllegalArgumentException is thrown if the component values for this
  1643. * <CODE>ColorModel</CODE> are not conveniently representable in the
  1644. * unnormalized form.
  1645. * Color/alpha components are stored in the <CODE>components</CODE> array
  1646. * starting at <CODE>offset</CODE> (even if the array is allocated by
  1647. * this method). Since <code>ComponentColorModel</code> can be
  1648. * subclassed, subclasses inherit the
  1649. * implementation of this method and if they don't override it then
  1650. * this method might throw an exception if they use an unsupported
  1651. * <code>transferType</code>.
  1652. *
  1653. * @param pixel A pixel value specified by an array of data elements of
  1654. * type <CODE>transferType</CODE>.
  1655. * @param components An integer array in which to store the unnormalized
  1656. * color/alpha components. If the <CODE>components</CODE> array is null,
  1657. * a new array is allocated.
  1658. * @param offset An offset into the <CODE>components</CODE> array.
  1659. *
  1660. * @return The <CODE>components</CODE> array.
  1661. *
  1662. * @throws IllegalArgumentException If this
  1663. * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
  1664. * @throws UnsupportedOperationException in some cases iff the
  1665. * transfer type of this <CODE>ComponentColorModel</CODE>
  1666. * is not one of the following transfer types:
  1667. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  1668. * or <CODE>DataBuffer.TYPE_INT</CODE>.
  1669. * @throws ClassCastException If <CODE>pixel</CODE> is not a primitive
  1670. * array of type <CODE>transferType</CODE>.
  1671. * @throws IllegalArgumentException If the <CODE>components</CODE> array is
  1672. * not null and is not large enough to hold all the color and alpha
  1673. * components (starting at offset), or if <CODE>pixel</CODE> is not large
  1674. * enough to hold a pixel value for this ColorModel.
  1675. */
  1676. public int[] getComponents(Object pixel, int[] components, int offset) {
  1677. int intpixel[];
  1678. if (needScaleInit) {
  1679. initScale();
  1680. }
  1681. if (noUnnorm) {
  1682. throw new
  1683. IllegalArgumentException(
  1684. "This ColorModel does not support the unnormalized form");
  1685. }
  1686. if (pixel instanceof int[]) {
  1687. intpixel = (int[])pixel;
  1688. } else {
  1689. intpixel = DataBuffer.toIntArray(pixel);
  1690. if (intpixel == null) {
  1691. throw new UnsupportedOperationException("This method has not been "+
  1692. "implemented for transferType " + transferType);
  1693. }
  1694. }
  1695. if (intpixel.length < numComponents) {
  1696. throw new IllegalArgumentException
  1697. ("Length of pixel array < number of components in model");
  1698. }
  1699. if (components == null) {
  1700. components = new int[offset+numComponents];
  1701. }
  1702. else if ((components.length-offset) < numComponents) {
  1703. throw new IllegalArgumentException
  1704. ("Length of components array < number of components in model");
  1705. }
  1706. System.arraycopy(intpixel, 0, components, offset, numComponents);
  1707. return components;
  1708. }
  1709. /**
  1710. * Returns an array of all of the color/alpha components in unnormalized
  1711. * form, given a normalized component array. Unnormalized components
  1712. * are unsigned integral values between 0 and 2<sup>n</sup> - 1, where
  1713. * n is the number of bits for a particular component. Normalized
  1714. * components are float values between a per component minimum and
  1715. * maximum specified by the <code>ColorSpace</code> object for this
  1716. * <code>ColorModel</code>. An <code>IllegalArgumentException</code>
  1717. * will be thrown if color component values for this
  1718. * <code>ColorModel</code> are not conveniently representable in the
  1719. * unnormalized form. If the
  1720. * <code>components</code> array is <code>null</code>, a new array
  1721. * will be allocated. The <code>components</code> array will
  1722. * be returned. Color/alpha components are stored in the
  1723. * <code>components</code> array starting at <code>offset</code> (even
  1724. * if the array is allocated by this method). An
  1725. * <code>ArrayIndexOutOfBoundsException</code> is thrown if the
  1726. * <code>components</code> array is not <code>null</code> and is not
  1727. * large enough to hold all the color and alpha
  1728. * components (starting at <code>offset</code>). An
  1729. * <code>IllegalArgumentException</code> is thrown if the
  1730. * <code>normComponents</code> array is not large enough to hold
  1731. * all the color and alpha components starting at
  1732. * <code>normOffset</code>.
  1733. * @param normComponents an array containing normalized components
  1734. * @param normOffset the offset into the <code>normComponents</code>
  1735. * array at which to start retrieving normalized components
  1736. * @param components an array that receives the components from
  1737. * <code>normComponents</code>
  1738. * @param offset the index into <code>components</code> at which to
  1739. * begin storing normalized components from
  1740. * <code>normComponents</code>
  1741. * @return an array containing unnormalized color and alpha
  1742. * components.
  1743. * @throws IllegalArgumentException If this
  1744. * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
  1745. * @throws IllegalArgumentException if the length of
  1746. * <code>normComponents</code> minus <code>normOffset</code>
  1747. * is less than <code>numComponents</code>
  1748. */
  1749. public int[] getUnnormalizedComponents(float[] normComponents,
  1750. int normOffset,
  1751. int[] components, int offset) {
  1752. if (needScaleInit) {
  1753. initScale();
  1754. }
  1755. if (noUnnorm) {
  1756. throw new
  1757. IllegalArgumentException(
  1758. "This ColorModel does not support the unnormalized form");
  1759. }
  1760. return super.getUnnormalizedComponents(normComponents, normOffset,
  1761. components, offset);
  1762. }
  1763. /**
  1764. * Returns an array of all of the color/alpha components in normalized
  1765. * form, given an unnormalized component array. Unnormalized components
  1766. * are unsigned integral values between 0 and 2<sup>n</sup> - 1, where
  1767. * n is the number of bits for a particular component. Normalized
  1768. * components are float values between a per component minimum and
  1769. * maximum specified by the <code>ColorSpace</code> object for this
  1770. * <code>ColorModel</code>. An <code>IllegalArgumentException</code>
  1771. * will be thrown if color component values for this
  1772. * <code>ColorModel</code> are not conveniently representable in the
  1773. * unnormalized form. If the
  1774. * <code>normComponents</code> array is <code>null</code>, a new array
  1775. * will be allocated. The <code>normComponents</code> array
  1776. * will be returned. Color/alpha components are stored in the
  1777. * <code>normComponents</code> array starting at
  1778. * <code>normOffset</code> (even if the array is allocated by this
  1779. * method). An <code>ArrayIndexOutOfBoundsException</code> is thrown
  1780. * if the <code>normComponents</code> array is not <code>null</code>
  1781. * and is not large enough to hold all the color and alpha components
  1782. * (starting at <code>normOffset</code>). An
  1783. * <code>IllegalArgumentException</code> is thrown if the
  1784. * <code>components</code> array is not large enough to hold all the
  1785. * color and alpha components starting at <code>offset</code>.
  1786. * @param components an array containing unnormalized components
  1787. * @param offset the offset into the <code>components</code> array at
  1788. * which to start retrieving unnormalized components
  1789. * @param normComponents an array that receives the normalized components
  1790. * @param normOffset the index into <code>normComponents</code> at
  1791. * which to begin storing normalized components
  1792. * @return an array containing normalized color and alpha
  1793. * components.
  1794. * @throws IllegalArgumentException If this
  1795. * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
  1796. */
  1797. public float[] getNormalizedComponents(int[] components, int offset,
  1798. float[] normComponents,
  1799. int normOffset) {
  1800. if (needScaleInit) {
  1801. initScale();
  1802. }
  1803. if (noUnnorm) {
  1804. throw new
  1805. IllegalArgumentException(
  1806. "This ColorModel does not support the unnormalized form");
  1807. }
  1808. return super.getNormalizedComponents(components, offset,
  1809. normComponents, normOffset);
  1810. }
  1811. /**
  1812. * Returns a pixel value represented as an int in this <CODE>ColorModel</CODE>,
  1813. * given an array of unnormalized color/alpha components.
  1814. *
  1815. * @param components An array of unnormalized color/alpha components.
  1816. * @param offset An offset into the <CODE>components</CODE> array.
  1817. *
  1818. * @return A pixel value represented as an int.
  1819. *
  1820. * @throws IllegalArgumentException If there is more than one component
  1821. * in this <CODE>ColorModel</CODE>.
  1822. * @throws IllegalArgumentException If this
  1823. * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
  1824. */
  1825. public int getDataElement(int[] components, int offset) {
  1826. if (needScaleInit) {
  1827. initScale();
  1828. }
  1829. if (numComponents == 1) {
  1830. if (noUnnorm) {
  1831. throw new
  1832. IllegalArgumentException(
  1833. "This ColorModel does not support the unnormalized form");
  1834. }
  1835. return components[offset+0];
  1836. }
  1837. throw new IllegalArgumentException("This model returns "+
  1838. numComponents+
  1839. " elements in the pixel array.");
  1840. }
  1841. /**
  1842. * Returns a data element array representation of a pixel in this
  1843. * <CODE>ColorModel</CODE>, given an array of unnormalized color/alpha
  1844. * components. This array can then be passed to the <CODE>setDataElements</CODE>
  1845. * method of a <CODE>WritableRaster</CODE> object.
  1846. *
  1847. * @param components An array of unnormalized color/alpha components.
  1848. * @param offset The integer offset into the <CODE>components</CODE> array.
  1849. * @param obj The object in which to store the data element array
  1850. * representation of the pixel. If <CODE>obj</CODE> variable is null,
  1851. * a new array is allocated. If <CODE>obj</CODE> is not null, it must
  1852. * be a primitive array of type <CODE>transferType</CODE>. An
  1853. * <CODE>ArrayIndexOutOfBoundsException</CODE> is thrown if
  1854. * <CODE>obj</CODE> is not large enough to hold a pixel value
  1855. * for this <CODE>ColorModel</CODE>. Since
  1856. * <code>ComponentColorModel</code> can be subclassed, subclasses
  1857. * inherit the implementation of this method and if they don't
  1858. * override it then they throw an exception if they use an
  1859. * unsupported <code>transferType</code>.
  1860. *
  1861. * @return The data element array representation of a pixel
  1862. * in this <CODE>ColorModel</CODE>.
  1863. *
  1864. * @throws IllegalArgumentException If the components array
  1865. * is not large enough to hold all the color and alpha components
  1866. * (starting at offset).
  1867. * @throws ClassCastException If <CODE>obj</CODE> is not null and is not a
  1868. * primitive array of type <CODE>transferType</CODE>.
  1869. * @throws ArrayIndexOutOfBoundsException If <CODE>obj</CODE> is not large
  1870. * enough to hold a pixel value for this <CODE>ColorModel</CODE>.
  1871. * @throws IllegalArgumentException If this
  1872. * <CODE>ComponentColorModel</CODE> does not support the unnormalized form
  1873. * @throws UnsupportedOperationException If the transfer type of
  1874. * this <CODE>ComponentColorModel</CODE>
  1875. * is not one of the following transfer types:
  1876. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  1877. * or <CODE>DataBuffer.TYPE_INT</CODE>.
  1878. *
  1879. * @see WritableRaster#setDataElements
  1880. * @see SampleModel#setDataElements
  1881. */
  1882. public Object getDataElements(int[] components, int offset, Object obj) {
  1883. if (needScaleInit) {
  1884. initScale();
  1885. }
  1886. if (noUnnorm) {
  1887. throw new
  1888. IllegalArgumentException(
  1889. "This ColorModel does not support the unnormalized form");
  1890. }
  1891. if ((components.length-offset) < numComponents) {
  1892. throw new IllegalArgumentException("Component array too small"+
  1893. " (should be "+numComponents);
  1894. }
  1895. switch(transferType) {
  1896. case DataBuffer.TYPE_INT:
  1897. {
  1898. int[] pixel;
  1899. if (obj == null) {
  1900. pixel = new int[numComponents];
  1901. }
  1902. else {
  1903. pixel = (int[]) obj;
  1904. }
  1905. System.arraycopy(components, offset, pixel, 0,
  1906. numComponents);
  1907. return pixel;
  1908. }
  1909. case DataBuffer.TYPE_BYTE:
  1910. {
  1911. byte[] pixel;
  1912. if (obj == null) {
  1913. pixel = new byte[numComponents];
  1914. }
  1915. else {
  1916. pixel = (byte[]) obj;
  1917. }
  1918. for (int i=0; i < numComponents; i++) {
  1919. pixel[i] = (byte) (components[offset+i]&0xff);
  1920. }
  1921. return pixel;
  1922. }
  1923. case DataBuffer.TYPE_USHORT:
  1924. {
  1925. short[] pixel;
  1926. if (obj == null) {
  1927. pixel = new short[numComponents];
  1928. }
  1929. else {
  1930. pixel = (short[]) obj;
  1931. }
  1932. for (int i=0; i < numComponents; i++) {
  1933. pixel[i] = (short) (components[offset+i]&0xffff);
  1934. }
  1935. return pixel;
  1936. }
  1937. default:
  1938. throw new UnsupportedOperationException("This method has not been "+
  1939. "implemented for transferType " +
  1940. transferType);
  1941. }
  1942. }
  1943. /**
  1944. * Returns a pixel value represented as an <code>int</code> in this
  1945. * <code>ColorModel</code>, given an array of normalized color/alpha
  1946. * components. This method will throw an
  1947. * <code>IllegalArgumentException</code> if pixel values for this
  1948. * <code>ColorModel</code> are not conveniently representable as a
  1949. * single <code>int</code>. An
  1950. * <code>ArrayIndexOutOfBoundsException</code> is thrown if the
  1951. * <code>normComponents</code> array is not large enough to hold all the
  1952. * color and alpha components (starting at <code>normOffset</code>).
  1953. * @param normComponents an array of normalized color and alpha
  1954. * components
  1955. * @param normOffset the index into <code>normComponents</code> at which to
  1956. * begin retrieving the color and alpha components
  1957. * @return an <code>int</code> pixel value in this
  1958. * <code>ColorModel</code> corresponding to the specified components.
  1959. * @throws IllegalArgumentException if
  1960. * pixel values for this <code>ColorModel</code> are not
  1961. * conveniently representable as a single <code>int</code>
  1962. * @throws ArrayIndexOutOfBoundsException if
  1963. * the <code>normComponents</code> array is not large enough to
  1964. * hold all of the color and alpha components starting at
  1965. * <code>normOffset</code>
  1966. * @since 1.4
  1967. */
  1968. public int getDataElement(float[] normComponents, int normOffset) {
  1969. if (numComponents > 1) {
  1970. throw new
  1971. IllegalArgumentException("More than one component per pixel");
  1972. }
  1973. if (signed) {
  1974. throw new
  1975. IllegalArgumentException("Component value is signed");
  1976. }
  1977. if (needScaleInit) {
  1978. initScale();
  1979. }
  1980. Object pixel = getDataElements(normComponents, normOffset, null);
  1981. switch (transferType) {
  1982. case DataBuffer.TYPE_BYTE:
  1983. {
  1984. byte bpixel[] = (byte[]) pixel;
  1985. return bpixel[0] & 0xff;
  1986. }
  1987. case DataBuffer.TYPE_USHORT:
  1988. {
  1989. short[] uspixel = (short[]) pixel;
  1990. return uspixel[0] & 0xffff;
  1991. }
  1992. case DataBuffer.TYPE_INT:
  1993. {
  1994. int[] ipixel = (int[]) pixel;
  1995. return ipixel[0];
  1996. }
  1997. default:
  1998. throw new UnsupportedOperationException("This method has not been "
  1999. + "implemented for transferType " + transferType);
  2000. }
  2001. }
  2002. /**
  2003. * Returns a data element array representation of a pixel in this
  2004. * <code>ColorModel</code>, given an array of normalized color/alpha
  2005. * components. This array can then be passed to the
  2006. * <code>setDataElements</code> method of a <code>WritableRaster</code>
  2007. * object. An <code>ArrayIndexOutOfBoundsException</code> is thrown
  2008. * if the <code>normComponents</code> array is not large enough to hold
  2009. * all the color and alpha components (starting at
  2010. * <code>normOffset</code>). If the <code>obj</code> variable is
  2011. * <code>null</code>, a new array will be allocated. If
  2012. * <code>obj</code> is not <code>null</code>, it must be a primitive
  2013. * array of type transferType; otherwise, a
  2014. * <code>ClassCastException</code> is thrown. An
  2015. * <code>ArrayIndexOutOfBoundsException</code> is thrown if
  2016. * <code>obj</code> is not large enough to hold a pixel value for this
  2017. * <code>ColorModel</code>.
  2018. * @param normComponents an array of normalized color and alpha
  2019. * components
  2020. * @param normOffset the index into <code>normComponents</code> at which to
  2021. * begin retrieving color and alpha components
  2022. * @param obj a primitive data array to hold the returned pixel
  2023. * @return an <code>Object</code> which is a primitive data array
  2024. * representation of a pixel
  2025. * @throws ClassCastException if <code>obj</code>
  2026. * is not a primitive array of type <code>transferType</code>
  2027. * @throws ArrayIndexOutOfBoundsException if
  2028. * <code>obj</code> is not large enough to hold a pixel value
  2029. * for this <code>ColorModel</code> or the <code>normComponents</code>
  2030. * array is not large enough to hold all of the color and alpha
  2031. * components starting at <code>normOffset</code>
  2032. * @see WritableRaster#setDataElements
  2033. * @see SampleModel#setDataElements
  2034. * @since 1.4
  2035. */
  2036. public Object getDataElements(float[] normComponents, int normOffset,
  2037. Object obj) {
  2038. boolean needAlpha = supportsAlpha && isAlphaPremultiplied;
  2039. float[] stdNormComponents;
  2040. if (needScaleInit) {
  2041. initScale();
  2042. }
  2043. if (nonStdScale) {
  2044. stdNormComponents = new float[numComponents];
  2045. for (int c = 0, nc = normOffset; c < numColorComponents;
  2046. c++, nc++) {
  2047. stdNormComponents[c] = (normComponents[nc] - compOffset[c]) *
  2048. compScale[c];
  2049. // REMIND: need to analyze whether this
  2050. // clamping is necessary
  2051. if (stdNormComponents[c] < 0.0f) {
  2052. stdNormComponents[c] = 0.0f;
  2053. }
  2054. if (stdNormComponents[c] > 1.0f) {
  2055. stdNormComponents[c] = 1.0f;
  2056. }
  2057. }
  2058. if (supportsAlpha) {
  2059. stdNormComponents[numColorComponents] =
  2060. normComponents[numColorComponents + normOffset];
  2061. }
  2062. normOffset = 0;
  2063. } else {
  2064. stdNormComponents = normComponents;
  2065. }
  2066. switch (transferType) {
  2067. case DataBuffer.TYPE_BYTE:
  2068. byte[] bpixel;
  2069. if (obj == null) {
  2070. bpixel = new byte[numComponents];
  2071. } else {
  2072. bpixel = (byte[]) obj;
  2073. }
  2074. if (needAlpha) {
  2075. float alpha =
  2076. stdNormComponents[numColorComponents + normOffset];
  2077. for (int c = 0, nc = normOffset; c < numColorComponents;
  2078. c++, nc++) {
  2079. bpixel[c] = (byte) ((stdNormComponents[nc] * alpha) *
  2080. ((float) ((1 << nBits[c]) - 1)) + 0.5f);
  2081. }
  2082. bpixel[numColorComponents] =
  2083. (byte) (alpha *
  2084. ((float) ((1 << nBits[numColorComponents]) - 1)) +
  2085. 0.5f);
  2086. } else {
  2087. for (int c = 0, nc = normOffset; c < numComponents;
  2088. c++, nc++) {
  2089. bpixel[c] = (byte) (stdNormComponents[nc] *
  2090. ((float) ((1 << nBits[c]) - 1)) + 0.5f);
  2091. }
  2092. }
  2093. return bpixel;
  2094. case DataBuffer.TYPE_USHORT:
  2095. short[] uspixel;
  2096. if (obj == null) {
  2097. uspixel = new short[numComponents];
  2098. } else {
  2099. uspixel = (short[]) obj;
  2100. }
  2101. if (needAlpha) {
  2102. float alpha =
  2103. stdNormComponents[numColorComponents + normOffset];
  2104. for (int c = 0, nc = normOffset; c < numColorComponents;
  2105. c++, nc++) {
  2106. uspixel[c] = (short) ((stdNormComponents[nc] * alpha) *
  2107. ((float) ((1 << nBits[c]) - 1)) +
  2108. 0.5f);
  2109. }
  2110. uspixel[numColorComponents] =
  2111. (short) (alpha *
  2112. ((float) ((1 << nBits[numColorComponents]) - 1)) +
  2113. 0.5f);
  2114. } else {
  2115. for (int c = 0, nc = normOffset; c < numComponents;
  2116. c++, nc++) {
  2117. uspixel[c] = (short) (stdNormComponents[nc] *
  2118. ((float) ((1 << nBits[c]) - 1)) +
  2119. 0.5f);
  2120. }
  2121. }
  2122. return uspixel;
  2123. case DataBuffer.TYPE_INT:
  2124. int[] ipixel;
  2125. if (obj == null) {
  2126. ipixel = new int[numComponents];
  2127. } else {
  2128. ipixel = (int[]) obj;
  2129. }
  2130. if (needAlpha) {
  2131. float alpha =
  2132. stdNormComponents[numColorComponents + normOffset];
  2133. for (int c = 0, nc = normOffset; c < numColorComponents;
  2134. c++, nc++) {
  2135. ipixel[c] = (int) ((stdNormComponents[nc] * alpha) *
  2136. ((float) ((1 << nBits[c]) - 1)) + 0.5f);
  2137. }
  2138. ipixel[numColorComponents] =
  2139. (int) (alpha *
  2140. ((float) ((1 << nBits[numColorComponents]) - 1)) +
  2141. 0.5f);
  2142. } else {
  2143. for (int c = 0, nc = normOffset; c < numComponents;
  2144. c++, nc++) {
  2145. ipixel[c] = (int) (stdNormComponents[nc] *
  2146. ((float) ((1 << nBits[c]) - 1)) + 0.5f);
  2147. }
  2148. }
  2149. return ipixel;
  2150. case DataBuffer.TYPE_SHORT:
  2151. short[] spixel;
  2152. if (obj == null) {
  2153. spixel = new short[numComponents];
  2154. } else {
  2155. spixel = (short[]) obj;
  2156. }
  2157. if (needAlpha) {
  2158. float alpha =
  2159. stdNormComponents[numColorComponents + normOffset];
  2160. for (int c = 0, nc = normOffset; c < numColorComponents;
  2161. c++, nc++) {
  2162. spixel[c] = (short)
  2163. (stdNormComponents[nc] * alpha * 32767.0f + 0.5f);
  2164. }
  2165. spixel[numColorComponents] = (short) (alpha * 32767.0f + 0.5f);
  2166. } else {
  2167. for (int c = 0, nc = normOffset; c < numComponents;
  2168. c++, nc++) {
  2169. spixel[c] = (short)
  2170. (stdNormComponents[nc] * 32767.0f + 0.5f);
  2171. }
  2172. }
  2173. return spixel;
  2174. case DataBuffer.TYPE_FLOAT:
  2175. float[] fpixel;
  2176. if (obj == null) {
  2177. fpixel = new float[numComponents];
  2178. } else {
  2179. fpixel = (float[]) obj;
  2180. }
  2181. if (needAlpha) {
  2182. float alpha = normComponents[numColorComponents + normOffset];
  2183. for (int c = 0, nc = normOffset; c < numColorComponents;
  2184. c++, nc++) {
  2185. fpixel[c] = normComponents[nc] * alpha;
  2186. }
  2187. fpixel[numColorComponents] = alpha;
  2188. } else {
  2189. for (int c = 0, nc = normOffset; c < numComponents;
  2190. c++, nc++) {
  2191. fpixel[c] = normComponents[nc];
  2192. }
  2193. }
  2194. return fpixel;
  2195. case DataBuffer.TYPE_DOUBLE:
  2196. double[] dpixel;
  2197. if (obj == null) {
  2198. dpixel = new double[numComponents];
  2199. } else {
  2200. dpixel = (double[]) obj;
  2201. }
  2202. if (needAlpha) {
  2203. double alpha =
  2204. (double) (normComponents[numColorComponents + normOffset]);
  2205. for (int c = 0, nc = normOffset; c < numColorComponents;
  2206. c++, nc++) {
  2207. dpixel[c] = normComponents[nc] * alpha;
  2208. }
  2209. dpixel[numColorComponents] = alpha;
  2210. } else {
  2211. for (int c = 0, nc = normOffset; c < numComponents;
  2212. c++, nc++) {
  2213. dpixel[c] = (double) normComponents[nc];
  2214. }
  2215. }
  2216. return dpixel;
  2217. default:
  2218. throw new UnsupportedOperationException("This method has not been "+
  2219. "implemented for transferType " +
  2220. transferType);
  2221. }
  2222. }
  2223. /**
  2224. * Returns an array of all of the color/alpha components in normalized
  2225. * form, given a pixel in this <code>ColorModel</code>. The pixel
  2226. * value is specified by an array of data elements of type transferType
  2227. * passed in as an object reference. If pixel is not a primitive array
  2228. * of type transferType, a <code>ClassCastException</code> is thrown.
  2229. * An <code>ArrayIndexOutOfBoundsException</code> is thrown if
  2230. * <code>pixel</code> is not large enough to hold a pixel value for this
  2231. * <code>ColorModel</code>.
  2232. * Normalized components are float values between a per component minimum
  2233. * and maximum specified by the <code>ColorSpace</code> object for this
  2234. * <code>ColorModel</code>. If the
  2235. * <code>normComponents</code> array is <code>null</code>, a new array
  2236. * will be allocated. The <code>normComponents</code> array
  2237. * will be returned. Color/alpha components are stored in the
  2238. * <code>normComponents</code> array starting at
  2239. * <code>normOffset</code> (even if the array is allocated by this
  2240. * method). An <code>ArrayIndexOutOfBoundsException</code> is thrown
  2241. * if the <code>normComponents</code> array is not <code>null</code>
  2242. * and is not large enough to hold all the color and alpha components
  2243. * (starting at <code>normOffset</code>).
  2244. * <p>
  2245. * This method must be overrridden by a subclass if that subclass
  2246. * is designed to translate pixel sample values to color component values
  2247. * in a non-default way. The default translations implemented by this
  2248. * class is described in the class comments. Any subclass implementing
  2249. * a non-default translation must follow the constraints on allowable
  2250. * translations defined there.
  2251. * @param pixel the specified pixel
  2252. * @param normComponents an array to receive the normalized components
  2253. * @param normOffset the offset into the <code>normComponents</code>
  2254. * array at which to start storing normalized components
  2255. * @return an array containing normalized color and alpha
  2256. * components.
  2257. * @throws ClassCastException if <code>pixel</code> is not a primitive
  2258. * array of type transferType
  2259. * @throws ArrayIndexOutOfBoundsException if
  2260. * <code>normComponents</code> is not large enough to hold all
  2261. * color and alpha components starting at <code>normOffset</code>
  2262. * @throws ArrayIndexOutOfBoundsException if
  2263. * <code>pixel</code> is not large enough to hold a pixel
  2264. * value for this <code>ColorModel</code>.
  2265. * @since 1.4
  2266. */
  2267. public float[] getNormalizedComponents(Object pixel,
  2268. float[] normComponents,
  2269. int normOffset) {
  2270. if (normComponents == null) {
  2271. normComponents = new float[numComponents+normOffset];
  2272. }
  2273. switch (transferType) {
  2274. case DataBuffer.TYPE_BYTE:
  2275. byte[] bpixel = (byte[]) pixel;
  2276. for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
  2277. normComponents[nc] = ((float) (bpixel[c] & 0xff)) /
  2278. ((float) ((1 << nBits[c]) - 1));
  2279. }
  2280. break;
  2281. case DataBuffer.TYPE_USHORT:
  2282. short[] uspixel = (short[]) pixel;
  2283. for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
  2284. normComponents[nc] = ((float) (uspixel[c] & 0xffff)) /
  2285. ((float) ((1 << nBits[c]) - 1));
  2286. }
  2287. break;
  2288. case DataBuffer.TYPE_INT:
  2289. int[] ipixel = (int[]) pixel;
  2290. for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
  2291. normComponents[nc] = ((float) ipixel[c]) /
  2292. ((float) ((1 << nBits[c]) - 1));
  2293. }
  2294. break;
  2295. case DataBuffer.TYPE_SHORT:
  2296. short[] spixel = (short[]) pixel;
  2297. for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
  2298. normComponents[nc] = ((float) spixel[c]) / 32767.0f;
  2299. }
  2300. break;
  2301. case DataBuffer.TYPE_FLOAT:
  2302. float[] fpixel = (float[]) pixel;
  2303. for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
  2304. normComponents[nc] = fpixel[c];
  2305. }
  2306. break;
  2307. case DataBuffer.TYPE_DOUBLE:
  2308. double[] dpixel = (double[]) pixel;
  2309. for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) {
  2310. normComponents[nc] = (float) dpixel[c];
  2311. }
  2312. break;
  2313. default:
  2314. throw new UnsupportedOperationException("This method has not been "+
  2315. "implemented for transferType " +
  2316. transferType);
  2317. }
  2318. if (supportsAlpha && isAlphaPremultiplied) {
  2319. float alpha = normComponents[numColorComponents + normOffset];
  2320. if (alpha != 0.0f) {
  2321. float invAlpha = 1.0f / alpha;
  2322. for (int c = normOffset; c < numColorComponents + normOffset;
  2323. c++) {
  2324. normComponents[c] *= invAlpha;
  2325. }
  2326. }
  2327. }
  2328. if (min != null) {
  2329. // Normally (i.e. when this class is not subclassed to override
  2330. // this method), the test (min != null) will be equivalent to
  2331. // the test (nonStdScale). However, there is an unlikely, but
  2332. // possible case, in which this method is overridden, nonStdScale
  2333. // is set true by initScale(), the subclass method for some
  2334. // reason calls this superclass method, but the min and
  2335. // diffMinMax arrays were never initialized by setupLUTs(). In
  2336. // that case, the right thing to do is follow the intended
  2337. // semantics of this method, and rescale the color components
  2338. // only if the ColorSpace min/max were detected to be other
  2339. // than 0.0/1.0 by setupLUTs(). Note that this implies the
  2340. // transferType is byte, ushort, int, or short - i.e. components
  2341. // derived from float and double pixel data are never rescaled.
  2342. for (int c = 0; c < numColorComponents; c++) {
  2343. normComponents[c + normOffset] = min[c] +
  2344. diffMinMax[c] * normComponents[c + normOffset];
  2345. }
  2346. }
  2347. return normComponents;
  2348. }
  2349. /**
  2350. * Forces the raster data to match the state specified in the
  2351. * <CODE>isAlphaPremultiplied</CODE> variable, assuming the data
  2352. * is currently correctly described by this <CODE>ColorModel</CODE>.
  2353. * It may multiply or divide the color raster data by alpha, or
  2354. * do nothing if the data is in the correct state. If the data needs
  2355. * to be coerced, this method also returns an instance of
  2356. * this <CODE>ColorModel</CODE> with
  2357. * the <CODE>isAlphaPremultiplied</CODE> flag set appropriately.
  2358. * Since <code>ColorModel</code> can be subclassed, subclasses inherit
  2359. * the implementation of this method and if they don't override it
  2360. * then they throw an exception if they use an unsupported
  2361. * <code>transferType</code>.
  2362. *
  2363. * @throws NullPointerException if <code>raster</code> is
  2364. * <code>null</code> and data coercion is required.
  2365. * @throws UnsupportedOperationException if the transfer type of
  2366. * this <CODE>ComponentColorModel</CODE>
  2367. * is not one of the supported transfer types:
  2368. * <CODE>DataBuffer.TYPE_BYTE</CODE>, <CODE>DataBuffer.TYPE_USHORT</CODE>,
  2369. * <CODE>DataBuffer.TYPE_INT</CODE>, <CODE>DataBuffer.TYPE_SHORT</CODE>,
  2370. * <CODE>DataBuffer.TYPE_FLOAT</CODE>, or <CODE>DataBuffer.TYPE_DOUBLE</CODE>.
  2371. */
  2372. public ColorModel coerceData (WritableRaster raster,
  2373. boolean isAlphaPremultiplied) {
  2374. if ((supportsAlpha == false) ||
  2375. (this.isAlphaPremultiplied == isAlphaPremultiplied))
  2376. {
  2377. // Nothing to do
  2378. return this;
  2379. }
  2380. int w = raster.getWidth();
  2381. int h = raster.getHeight();
  2382. int aIdx = raster.getNumBands() - 1;
  2383. float normAlpha;
  2384. int rminX = raster.getMinX();
  2385. int rY = raster.getMinY();
  2386. int rX;
  2387. if (isAlphaPremultiplied) {
  2388. switch (transferType) {
  2389. case DataBuffer.TYPE_BYTE: {
  2390. byte pixel[] = null;
  2391. byte zpixel[] = null;
  2392. float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
  2393. for (int y = 0; y < h; y++, rY++) {
  2394. rX = rminX;
  2395. for (int x = 0; x < w; x++, rX++) {
  2396. pixel = (byte[])raster.getDataElements(rX, rY,
  2397. pixel);
  2398. normAlpha = (pixel[aIdx] & 0xff) * alphaScale;
  2399. if (normAlpha != 0.0f) {
  2400. for (int c=0; c < aIdx; c++) {
  2401. pixel[c] = (byte)((pixel[c] & 0xff) *
  2402. normAlpha + 0.5f);
  2403. }
  2404. raster.setDataElements(rX, rY, pixel);
  2405. } else {
  2406. if (zpixel == null) {
  2407. zpixel = new byte[numComponents];
  2408. java.util.Arrays.fill(zpixel, (byte) 0);
  2409. }
  2410. raster.setDataElements(rX, rY, zpixel);
  2411. }
  2412. }
  2413. }
  2414. }
  2415. break;
  2416. case DataBuffer.TYPE_USHORT: {
  2417. short pixel[] = null;
  2418. short zpixel[] = null;
  2419. float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
  2420. for (int y = 0; y < h; y++, rY++) {
  2421. rX = rminX;
  2422. for (int x = 0; x < w; x++, rX++) {
  2423. pixel = (short[])raster.getDataElements(rX, rY,
  2424. pixel);
  2425. normAlpha = (pixel[aIdx] & 0xffff) * alphaScale;
  2426. if (normAlpha != 0.0f) {
  2427. for (int c=0; c < aIdx; c++) {
  2428. pixel[c] = (short)
  2429. ((pixel[c] & 0xffff) * normAlpha +
  2430. 0.5f);
  2431. }
  2432. raster.setDataElements(rX, rY, pixel);
  2433. } else {
  2434. if (zpixel == null) {
  2435. zpixel = new short[numComponents];
  2436. java.util.Arrays.fill(zpixel, (short) 0);
  2437. }
  2438. raster.setDataElements(rX, rY, zpixel);
  2439. }
  2440. }
  2441. }
  2442. }
  2443. break;
  2444. case DataBuffer.TYPE_INT: {
  2445. int pixel[] = null;
  2446. int zpixel[] = null;
  2447. float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
  2448. for (int y = 0; y < h; y++, rY++) {
  2449. rX = rminX;
  2450. for (int x = 0; x < w; x++, rX++) {
  2451. pixel = (int[])raster.getDataElements(rX, rY,
  2452. pixel);
  2453. normAlpha = pixel[aIdx] * alphaScale;
  2454. if (normAlpha != 0.0f) {
  2455. for (int c=0; c < aIdx; c++) {
  2456. pixel[c] = (int) (pixel[c] * normAlpha +
  2457. 0.5f);
  2458. }
  2459. raster.setDataElements(rX, rY, pixel);
  2460. } else {
  2461. if (zpixel == null) {
  2462. zpixel = new int[numComponents];
  2463. java.util.Arrays.fill(zpixel, 0);
  2464. }
  2465. raster.setDataElements(rX, rY, zpixel);
  2466. }
  2467. }
  2468. }
  2469. }
  2470. break;
  2471. case DataBuffer.TYPE_SHORT: {
  2472. short pixel[] = null;
  2473. short zpixel[] = null;
  2474. float alphaScale = 1.0f / 32767.0f;
  2475. for (int y = 0; y < h; y++, rY++) {
  2476. rX = rminX;
  2477. for (int x = 0; x < w; x++, rX++) {
  2478. pixel = (short[]) raster.getDataElements(rX, rY,
  2479. pixel);
  2480. normAlpha = pixel[aIdx] * alphaScale;
  2481. if (normAlpha != 0.0f) {
  2482. for (int c=0; c < aIdx; c++) {
  2483. pixel[c] = (short) (pixel[c] * normAlpha +
  2484. 0.5f);
  2485. }
  2486. raster.setDataElements(rX, rY, pixel);
  2487. } else {
  2488. if (zpixel == null) {
  2489. zpixel = new short[numComponents];
  2490. java.util.Arrays.fill(zpixel, (short) 0);
  2491. }
  2492. raster.setDataElements(rX, rY, zpixel);
  2493. }
  2494. }
  2495. }
  2496. }
  2497. break;
  2498. case DataBuffer.TYPE_FLOAT: {
  2499. float pixel[] = null;
  2500. float zpixel[] = null;
  2501. for (int y = 0; y < h; y++, rY++) {
  2502. rX = rminX;
  2503. for (int x = 0; x < w; x++, rX++) {
  2504. pixel = (float[]) raster.getDataElements(rX, rY,
  2505. pixel);
  2506. normAlpha = pixel[aIdx];
  2507. if (normAlpha != 0.0f) {
  2508. for (int c=0; c < aIdx; c++) {
  2509. pixel[c] *= normAlpha;
  2510. }
  2511. raster.setDataElements(rX, rY, pixel);
  2512. } else {
  2513. if (zpixel == null) {
  2514. zpixel = new float[numComponents];
  2515. java.util.Arrays.fill(zpixel, 0.0f);
  2516. }
  2517. raster.setDataElements(rX, rY, zpixel);
  2518. }
  2519. }
  2520. }
  2521. }
  2522. break;
  2523. case DataBuffer.TYPE_DOUBLE: {
  2524. double pixel[] = null;
  2525. double zpixel[] = null;
  2526. for (int y = 0; y < h; y++, rY++) {
  2527. rX = rminX;
  2528. for (int x = 0; x < w; x++, rX++) {
  2529. pixel = (double[]) raster.getDataElements(rX, rY,
  2530. pixel);
  2531. double dnormAlpha = pixel[aIdx];
  2532. if (dnormAlpha != 0.0) {
  2533. for (int c=0; c < aIdx; c++) {
  2534. pixel[c] *= dnormAlpha;
  2535. }
  2536. raster.setDataElements(rX, rY, pixel);
  2537. } else {
  2538. if (zpixel == null) {
  2539. zpixel = new double[numComponents];
  2540. java.util.Arrays.fill(zpixel, 0.0);
  2541. }
  2542. raster.setDataElements(rX, rY, zpixel);
  2543. }
  2544. }
  2545. }
  2546. }
  2547. break;
  2548. default:
  2549. throw new UnsupportedOperationException("This method has not been "+
  2550. "implemented for transferType " + transferType);
  2551. }
  2552. }
  2553. else {
  2554. // We are premultiplied and want to divide it out
  2555. switch (transferType) {
  2556. case DataBuffer.TYPE_BYTE: {
  2557. byte pixel[] = null;
  2558. float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
  2559. for (int y = 0; y < h; y++, rY++) {
  2560. rX = rminX;
  2561. for (int x = 0; x < w; x++, rX++) {
  2562. pixel = (byte[])raster.getDataElements(rX, rY,
  2563. pixel);
  2564. normAlpha = (pixel[aIdx] & 0xff) * alphaScale;
  2565. if (normAlpha != 0.0f) {
  2566. float invAlpha = 1.0f / normAlpha;
  2567. for (int c=0; c < aIdx; c++) {
  2568. pixel[c] = (byte)
  2569. ((pixel[c] & 0xff) * invAlpha + 0.5f);
  2570. }
  2571. raster.setDataElements(rX, rY, pixel);
  2572. }
  2573. }
  2574. }
  2575. }
  2576. break;
  2577. case DataBuffer.TYPE_USHORT: {
  2578. short pixel[] = null;
  2579. float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
  2580. for (int y = 0; y < h; y++, rY++) {
  2581. rX = rminX;
  2582. for (int x = 0; x < w; x++, rX++) {
  2583. pixel = (short[])raster.getDataElements(rX, rY,
  2584. pixel);
  2585. normAlpha = (pixel[aIdx] & 0xffff) * alphaScale;
  2586. if (normAlpha != 0.0f) {
  2587. float invAlpha = 1.0f / normAlpha;
  2588. for (int c=0; c < aIdx; c++) {
  2589. pixel[c] = (short)
  2590. ((pixel[c] & 0xffff) * invAlpha + 0.5f);
  2591. }
  2592. raster.setDataElements(rX, rY, pixel);
  2593. }
  2594. }
  2595. }
  2596. }
  2597. break;
  2598. case DataBuffer.TYPE_INT: {
  2599. int pixel[] = null;
  2600. float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1));
  2601. for (int y = 0; y < h; y++, rY++) {
  2602. rX = rminX;
  2603. for (int x = 0; x < w; x++, rX++) {
  2604. pixel = (int[])raster.getDataElements(rX, rY,
  2605. pixel);
  2606. normAlpha = pixel[aIdx] * alphaScale;
  2607. if (normAlpha != 0.0f) {
  2608. float invAlpha = 1.0f / normAlpha;
  2609. for (int c=0; c < aIdx; c++) {
  2610. pixel[c] = (int)
  2611. (pixel[c] * invAlpha + 0.5f);
  2612. }
  2613. raster.setDataElements(rX, rY, pixel);
  2614. }
  2615. }
  2616. }
  2617. }
  2618. break;
  2619. case DataBuffer.TYPE_SHORT: {
  2620. short pixel[] = null;
  2621. float alphaScale = 1.0f / 32767.0f;
  2622. for (int y = 0; y < h; y++, rY++) {
  2623. rX = rminX;
  2624. for (int x = 0; x < w; x++, rX++) {
  2625. pixel = (short[])raster.getDataElements(rX, rY,
  2626. pixel);
  2627. normAlpha = pixel[aIdx] * alphaScale;
  2628. if (normAlpha != 0.0f) {
  2629. float invAlpha = 1.0f / normAlpha;
  2630. for (int c=0; c < aIdx; c++) {
  2631. pixel[c] = (short)
  2632. (pixel[c] * invAlpha + 0.5f);
  2633. }
  2634. raster.setDataElements(rX, rY, pixel);
  2635. }
  2636. }
  2637. }
  2638. }
  2639. break;
  2640. case DataBuffer.TYPE_FLOAT: {
  2641. float pixel[] = null;
  2642. for (int y = 0; y < h; y++, rY++) {
  2643. rX = rminX;
  2644. for (int x = 0; x < w; x++, rX++) {
  2645. pixel = (float[])raster.getDataElements(rX, rY,
  2646. pixel);
  2647. normAlpha = pixel[aIdx];
  2648. if (normAlpha != 0.0f) {
  2649. float invAlpha = 1.0f / normAlpha;
  2650. for (int c=0; c < aIdx; c++) {
  2651. pixel[c] *= invAlpha;
  2652. }
  2653. raster.setDataElements(rX, rY, pixel);
  2654. }
  2655. }
  2656. }
  2657. }
  2658. break;
  2659. case DataBuffer.TYPE_DOUBLE: {
  2660. double pixel[] = null;
  2661. for (int y = 0; y < h; y++, rY++) {
  2662. rX = rminX;
  2663. for (int x = 0; x < w; x++, rX++) {
  2664. pixel = (double[])raster.getDataElements(rX, rY,
  2665. pixel);
  2666. double dnormAlpha = pixel[aIdx];
  2667. if (dnormAlpha != 0.0) {
  2668. double invAlpha = 1.0 / dnormAlpha;
  2669. for (int c=0; c < aIdx; c++) {
  2670. pixel[c] *= invAlpha;
  2671. }
  2672. raster.setDataElements(rX, rY, pixel);
  2673. }
  2674. }
  2675. }
  2676. }
  2677. break;
  2678. default:
  2679. throw new UnsupportedOperationException("This method has not been "+
  2680. "implemented for transferType " + transferType);
  2681. }
  2682. }
  2683. // Return a new color model
  2684. if (!signed) {
  2685. return new ComponentColorModel(colorSpace, nBits, supportsAlpha,
  2686. isAlphaPremultiplied, transparency,
  2687. transferType);
  2688. } else {
  2689. return new ComponentColorModel(colorSpace, supportsAlpha,
  2690. isAlphaPremultiplied, transparency,
  2691. transferType);
  2692. }
  2693. }
  2694. /**
  2695. * Returns true if <CODE>raster</CODE> is compatible with this
  2696. * <CODE>ColorModel</CODE> false if it is not.
  2697. *
  2698. * @param raster The <CODE>Raster</CODE> object to test for compatibility.
  2699. *
  2700. * @return <CODE>true</CODE> if <CODE>raster</CODE> is compatible with this
  2701. * <CODE>ColorModel</CODE>, <CODE>false</CODE> if it is not.
  2702. */
  2703. public boolean isCompatibleRaster(Raster raster) {
  2704. SampleModel sm = raster.getSampleModel();
  2705. if (sm instanceof ComponentSampleModel) {
  2706. if (sm.getNumBands() != getNumComponents()) {
  2707. return false;
  2708. }
  2709. for (int i=0; i<nBits.length; i++) {
  2710. if (sm.getSampleSize(i) < nBits[i]) {
  2711. return false;
  2712. }
  2713. }
  2714. return (raster.getTransferType() == transferType);
  2715. }
  2716. else {
  2717. return false;
  2718. }
  2719. }
  2720. /**
  2721. * Creates a <CODE>WritableRaster</CODE> with the specified width and height,
  2722. * that has a data layout (<CODE>SampleModel</CODE>) compatible with
  2723. * this <CODE>ColorModel</CODE>.
  2724. *
  2725. * @param w The width of the <CODE>WritableRaster</CODE> you want to create.
  2726. * @param h The height of the <CODE>WritableRaster</CODE> you want to create.
  2727. *
  2728. * @return A <CODE>WritableRaster</CODE> that is compatible with
  2729. * this <CODE>ColorModel</CODE>.
  2730. * @see WritableRaster
  2731. * @see SampleModel
  2732. */
  2733. public WritableRaster createCompatibleWritableRaster (int w, int h) {
  2734. int dataSize = w*h*numComponents;
  2735. WritableRaster raster = null;
  2736. switch (transferType) {
  2737. case DataBuffer.TYPE_BYTE:
  2738. case DataBuffer.TYPE_USHORT:
  2739. raster = Raster.createInterleavedRaster(transferType,
  2740. w, h,
  2741. numComponents, null);
  2742. break;
  2743. default:
  2744. SampleModel sm = createCompatibleSampleModel(w, h);
  2745. DataBuffer db = sm.createDataBuffer();
  2746. raster = Raster.createWritableRaster(sm, db, null);
  2747. }
  2748. return raster;
  2749. }
  2750. /**
  2751. * Creates a <CODE>SampleModel</CODE> with the specified width and height,
  2752. * that has a data layout compatible with this <CODE>ColorModel</CODE>.
  2753. *
  2754. * @param w The width of the <CODE>SampleModel</CODE> you want to create.
  2755. * @param h The height of the <CODE>SampleModel</CODE> you want to create.
  2756. *
  2757. * @return A <CODE>SampleModel</CODE> that is compatible with this
  2758. * <CODE>ColorModel</CODE>.
  2759. *
  2760. * @see SampleModel
  2761. */
  2762. public SampleModel createCompatibleSampleModel(int w, int h) {
  2763. int[] bandOffsets = new int[numComponents];
  2764. for (int i=0; i < numComponents; i++) {
  2765. bandOffsets[i] = i;
  2766. }
  2767. switch (transferType) {
  2768. case DataBuffer.TYPE_BYTE:
  2769. case DataBuffer.TYPE_USHORT:
  2770. return new PixelInterleavedSampleModel(transferType, w, h,
  2771. numComponents,
  2772. w*numComponents,
  2773. bandOffsets);
  2774. default:
  2775. return new ComponentSampleModel(transferType, w, h,
  2776. numComponents,
  2777. w*numComponents,
  2778. bandOffsets);
  2779. }
  2780. }
  2781. /**
  2782. * Checks whether or not the specified <CODE>SampleModel</CODE>
  2783. * is compatible with this <CODE>ColorModel</CODE>.
  2784. *
  2785. * @param sm The <CODE>SampleModel</CODE> to test for compatibility.
  2786. *
  2787. * @return <CODE>true</CODE> if the <CODE>SampleModel</CODE> is
  2788. * compatible with this <CODE>ColorModel</CODE>, <CODE>false</CODE>
  2789. * if it is not.
  2790. *
  2791. * @see SampleModel
  2792. */
  2793. public boolean isCompatibleSampleModel(SampleModel sm) {
  2794. if (!(sm instanceof ComponentSampleModel)) {
  2795. return false;
  2796. }
  2797. // Must have the same number of components
  2798. if (numComponents != sm.getNumBands()) {
  2799. return false;
  2800. }
  2801. if (sm.getTransferType() != transferType) {
  2802. return false;
  2803. }
  2804. return true;
  2805. }
  2806. /**
  2807. * Returns a <CODE>Raster</CODE> representing the alpha channel of an image,
  2808. * extracted from the input <CODE>Raster</CODE>.
  2809. * This method assumes that <CODE>Raster</CODE> objects associated with
  2810. * this <CODE>ColorModel</CODE> store the alpha band, if present, as
  2811. * the last band of image data. Returns null if there is no separate spatial
  2812. * alpha channel associated with this <CODE>ColorModel</CODE>.
  2813. * This method creates a new <CODE>Raster</CODE>, but will share the data
  2814. * array.
  2815. *
  2816. * @param raster The <CODE>WritableRaster</CODE> from which to extract the
  2817. * alpha channel.
  2818. *
  2819. * @return A <CODE>WritableRaster</CODE> containing the image's alpha channel.
  2820. *
  2821. */
  2822. public WritableRaster getAlphaRaster(WritableRaster raster) {
  2823. if (hasAlpha() == false) {
  2824. return null;
  2825. }
  2826. int x = raster.getMinX();
  2827. int y = raster.getMinY();
  2828. int[] band = new int[1];
  2829. band[0] = raster.getNumBands() - 1;
  2830. return raster.createWritableChild(x, y, raster.getWidth(),
  2831. raster.getHeight(), x, y,
  2832. band);
  2833. }
  2834. /**
  2835. * Compares this color model with another for equality.
  2836. *
  2837. * @param obj The object to compare with this color model.
  2838. * @return <CODE>true</CODE> if the color model objects are equal,
  2839. * <CODE>false</CODE> if they are not.
  2840. */
  2841. public boolean equals(Object obj) {
  2842. if (!super.equals(obj)) {
  2843. return false;
  2844. }
  2845. if (obj.getClass() != getClass()) {
  2846. return false;
  2847. }
  2848. return true;
  2849. }
  2850. }