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