10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.awt.image;
27
28 import java.awt.Transparency;
29 import java.awt.color.ColorSpace;
30
31 /**
32 * The {@code PackedColorModel} class is an abstract
33 * {@link ColorModel} class that works with pixel values which represent
34 * color and alpha information as separate samples and which pack all
35 * samples for a single pixel into a single int, short, or byte quantity.
36 * This class can be used with an arbitrary {@link ColorSpace}. The number of
37 * color samples in the pixel values must be the same as the number of color
38 * components in the {@code ColorSpace}. There can be a single alpha
39 * sample. The array length is always 1 for those methods that use a
40 * primitive array pixel representation of type {@code transferType}.
41 * The transfer types supported are DataBuffer.TYPE_BYTE,
42 * DataBuffer.TYPE_USHORT, and DataBuffer.TYPE_INT.
43 * Color and alpha samples are stored in the single element of the array
44 * in bits indicated by bit masks. Each bit mask must be contiguous and
45 * masks must not overlap. The same masks apply to the single int
46 * pixel representation used by other methods. The correspondence of
47 * masks and color/alpha samples is as follows:
48 * <ul>
49 * <li> Masks are identified by indices running from 0 through
71 * done according to the masks of the {@code ColorModel}.
72 * <p>
73 * A single {@code int} pixel representation is valid for all objects
74 * of this class since it is always possible to represent pixel values
75 * used with this class in a single {@code int}. Therefore, methods
76 * that use this representation do not throw an
77 * {@code IllegalArgumentException} due to an invalid pixel value.
78 * <p>
79 * A subclass of {@code PackedColorModel} is {@link DirectColorModel},
80 * which is similar to an X11 TrueColor visual.
81 *
82 * @see DirectColorModel
83 * @see SinglePixelPackedSampleModel
84 * @see BufferedImage
85 */
86
87 public abstract class PackedColorModel extends ColorModel {
88 int[] maskArray;
89 int[] maskOffsets;
90 float[] scaleFactors;
91
92 /**
93 * Constructs a {@code PackedColorModel} from a color mask array,
94 * which specifies which bits in an {@code int} pixel representation
95 * contain each of the color samples, and an alpha mask. Color
96 * components are in the specified {@code ColorSpace}. The length of
97 * {@code colorMaskArray} should be the number of components in
98 * the {@code ColorSpace}. All of the bits in each mask
99 * must be contiguous and fit in the specified number of least significant
100 * bits of an {@code int} pixel representation. If the
101 * {@code alphaMask} is 0, there is no alpha. If there is alpha,
102 * the {@code boolean isAlphaPremultiplied} specifies
103 * how to interpret color and alpha samples in pixel values. If the
104 * {@code boolean} is {@code true}, color samples are assumed
105 * to have been multiplied by the alpha sample. The transparency,
106 * {@code trans}, specifies what alpha values can be represented
107 * by this color model. The transfer type is the type of primitive
108 * array used to represent pixel values.
109 * @param space the specified {@code ColorSpace}
110 * @param bits the number of bits in the pixel values
368 * {@code WritableRaster}, but shares the data array.
369 * @param raster a {@code WritableRaster} containing an image
370 * @return a {@code WritableRaster} that represents the alpha
371 * channel of the image contained in {@code raster}.
372 */
373 public WritableRaster getAlphaRaster(WritableRaster raster) {
374 if (hasAlpha() == false) {
375 return null;
376 }
377
378 int x = raster.getMinX();
379 int y = raster.getMinY();
380 int[] band = new int[1];
381 band[0] = raster.getNumBands() - 1;
382 return raster.createWritableChild(x, y, raster.getWidth(),
383 raster.getHeight(), x, y,
384 band);
385 }
386
387 /**
388 * Tests if the specified {@code Object} is an instance
389 * of {@code PackedColorModel} and equals this
390 * {@code PackedColorModel}.
391 * @param obj the {@code Object} to test for equality
392 * @return {@code true} if the specified {@code Object}
393 * is an instance of {@code PackedColorModel} and equals this
394 * {@code PackedColorModel}; {@code false} otherwise.
395 */
396 public boolean equals(Object obj) {
397 if (!(obj instanceof PackedColorModel)) {
398 return false;
399 }
400
401 if (!super.equals(obj)) {
402 return false;
403 }
404
405 PackedColorModel cm = (PackedColorModel) obj;
406 int numC = cm.getNumComponents();
407 for(int i=0; i < numC; i++) {
408 if (maskArray[i] != cm.getMask(i)) {
409 return false;
410 }
411 }
412 return true;
413 }
414
415 private static final int[] createBitsArray(int[]colorMaskArray,
416 int alphaMask) {
417 int numColors = colorMaskArray.length;
418 int numAlpha = (alphaMask == 0 ? 0 : 1);
419 int[] arr = new int[numColors+numAlpha];
420 for (int i=0; i < numColors; i++) {
421 arr[i] = countBits(colorMaskArray[i]);
422 if (arr[i] < 0) {
423 throw new IllegalArgumentException("Noncontiguous color mask ("
424 + Integer.toHexString(colorMaskArray[i])+
425 "at index "+i);
426 }
427 }
428 if (alphaMask != 0) {
429 arr[numColors] = countBits(alphaMask);
430 if (arr[numColors] < 0) {
431 throw new IllegalArgumentException("Noncontiguous alpha mask ("
432 + Integer.toHexString(alphaMask));
|
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26 package java.awt.image;
27
28 import java.awt.Transparency;
29 import java.awt.color.ColorSpace;
30 import java.util.Arrays;
31
32 /**
33 * The {@code PackedColorModel} class is an abstract
34 * {@link ColorModel} class that works with pixel values which represent
35 * color and alpha information as separate samples and which pack all
36 * samples for a single pixel into a single int, short, or byte quantity.
37 * This class can be used with an arbitrary {@link ColorSpace}. The number of
38 * color samples in the pixel values must be the same as the number of color
39 * components in the {@code ColorSpace}. There can be a single alpha
40 * sample. The array length is always 1 for those methods that use a
41 * primitive array pixel representation of type {@code transferType}.
42 * The transfer types supported are DataBuffer.TYPE_BYTE,
43 * DataBuffer.TYPE_USHORT, and DataBuffer.TYPE_INT.
44 * Color and alpha samples are stored in the single element of the array
45 * in bits indicated by bit masks. Each bit mask must be contiguous and
46 * masks must not overlap. The same masks apply to the single int
47 * pixel representation used by other methods. The correspondence of
48 * masks and color/alpha samples is as follows:
49 * <ul>
50 * <li> Masks are identified by indices running from 0 through
72 * done according to the masks of the {@code ColorModel}.
73 * <p>
74 * A single {@code int} pixel representation is valid for all objects
75 * of this class since it is always possible to represent pixel values
76 * used with this class in a single {@code int}. Therefore, methods
77 * that use this representation do not throw an
78 * {@code IllegalArgumentException} due to an invalid pixel value.
79 * <p>
80 * A subclass of {@code PackedColorModel} is {@link DirectColorModel},
81 * which is similar to an X11 TrueColor visual.
82 *
83 * @see DirectColorModel
84 * @see SinglePixelPackedSampleModel
85 * @see BufferedImage
86 */
87
88 public abstract class PackedColorModel extends ColorModel {
89 int[] maskArray;
90 int[] maskOffsets;
91 float[] scaleFactors;
92 private volatile int hashCode;
93
94 /**
95 * Constructs a {@code PackedColorModel} from a color mask array,
96 * which specifies which bits in an {@code int} pixel representation
97 * contain each of the color samples, and an alpha mask. Color
98 * components are in the specified {@code ColorSpace}. The length of
99 * {@code colorMaskArray} should be the number of components in
100 * the {@code ColorSpace}. All of the bits in each mask
101 * must be contiguous and fit in the specified number of least significant
102 * bits of an {@code int} pixel representation. If the
103 * {@code alphaMask} is 0, there is no alpha. If there is alpha,
104 * the {@code boolean isAlphaPremultiplied} specifies
105 * how to interpret color and alpha samples in pixel values. If the
106 * {@code boolean} is {@code true}, color samples are assumed
107 * to have been multiplied by the alpha sample. The transparency,
108 * {@code trans}, specifies what alpha values can be represented
109 * by this color model. The transfer type is the type of primitive
110 * array used to represent pixel values.
111 * @param space the specified {@code ColorSpace}
112 * @param bits the number of bits in the pixel values
370 * {@code WritableRaster}, but shares the data array.
371 * @param raster a {@code WritableRaster} containing an image
372 * @return a {@code WritableRaster} that represents the alpha
373 * channel of the image contained in {@code raster}.
374 */
375 public WritableRaster getAlphaRaster(WritableRaster raster) {
376 if (hasAlpha() == false) {
377 return null;
378 }
379
380 int x = raster.getMinX();
381 int y = raster.getMinY();
382 int[] band = new int[1];
383 band[0] = raster.getNumBands() - 1;
384 return raster.createWritableChild(x, y, raster.getWidth(),
385 raster.getHeight(), x, y,
386 band);
387 }
388
389 /**
390 * Tests if the specified {@code Object} equals this
391 * {@code PackedColorModel}.
392 * In order to protect the symmetry property of
393 * {@code (a.equals(b) == b.equals(a))},
394 * the target object must be the same class(and not a subclass)
395 * as this object to evaluate as {equals}.
396 * @param obj the {@code Object} to test for equality
397 * @return {@code true} if the specified {@code Object}
398 * equals this {@code PackedColorModel}; {@code false} otherwise.
399 */
400 @Override
401 public boolean equals(Object obj) {
402 /*
403 * We verify the type of argument obj in super.equals() where we check
404 * for equality of class name.
405 */
406 if (!super.equals(obj)) {
407 return false;
408 }
409
410 PackedColorModel cm = (PackedColorModel) obj;
411 int numC = cm.getNumComponents();
412 for(int i=0; i < numC; i++) {
413 if (maskArray[i] != cm.getMask(i)) {
414 return false;
415 }
416 }
417 return true;
418 }
419
420 /**
421 * Returns the hash code for this PackedColorModel.
422 *
423 * @return a hash code for this PackedColorModel.
424 */
425 @Override
426 public int hashCode() {
427 int result = hashCode;
428 if (result == 0) {
429 result = super.hashCode();
430 result = 89 * result + Arrays.hashCode(this.maskArray);
431 hashCode = result;
432 }
433 return result;
434 }
435
436 private static final int[] createBitsArray(int[]colorMaskArray,
437 int alphaMask) {
438 int numColors = colorMaskArray.length;
439 int numAlpha = (alphaMask == 0 ? 0 : 1);
440 int[] arr = new int[numColors+numAlpha];
441 for (int i=0; i < numColors; i++) {
442 arr[i] = countBits(colorMaskArray[i]);
443 if (arr[i] < 0) {
444 throw new IllegalArgumentException("Noncontiguous color mask ("
445 + Integer.toHexString(colorMaskArray[i])+
446 "at index "+i);
447 }
448 }
449 if (alphaMask != 0) {
450 arr[numColors] = countBits(alphaMask);
451 if (arr[numColors] < 0) {
452 throw new IllegalArgumentException("Noncontiguous alpha mask ("
453 + Integer.toHexString(alphaMask));
|