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
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
377 }
378
379 int x = raster.getMinX();
380 int y = raster.getMinY();
381 int[] band = new int[1];
382 band[0] = raster.getNumBands() - 1;
383 return raster.createWritableChild(x, y, raster.getWidth(),
384 raster.getHeight(), x, y,
385 band);
386 }
387
388 /**
389 * Tests if the specified {@code Object} is an instance
390 * of {@code PackedColorModel} and equals this
391 * {@code PackedColorModel}.
392 * @param obj the {@code Object} to test for equality
393 * @return {@code true} if the specified {@code Object}
394 * is an instance of {@code PackedColorModel} and equals this
395 * {@code PackedColorModel}; {@code false} otherwise.
396 */
397 @Override
398 public boolean equals(Object obj) {
399
400 if (!(obj instanceof PackedColorModel)) {
401 return false;
402 }
403
404 PackedColorModel cm = (PackedColorModel) obj;
405 if (this == cm) {
406 return true;
407 }
408
409 if (!super.equals(obj)) {
410 return false;
411 }
412
413 int numC = cm.getNumComponents();
414 for(int i=0; i < numC; i++) {
415 if (maskArray[i] != cm.getMask(i)) {
416 return false;
417 }
418 }
419 return true;
420 }
421
422 /**
423 * Returns the hash code for this PackedColorModel.
424 *
425 * @return a hash code for this PackedColorModel.
426 */
427 @Override
428 public int hashCode() {
429 int hash = 3;
430 hash = 89 * hash + super.hashCode();
431 hash = 89 * hash + Arrays.hashCode(this.maskArray);
432 return hash;
433 }
434
435 private static final int[] createBitsArray(int[]colorMaskArray,
436 int alphaMask) {
437 int numColors = colorMaskArray.length;
438 int numAlpha = (alphaMask == 0 ? 0 : 1);
439 int[] arr = new int[numColors+numAlpha];
440 for (int i=0; i < numColors; i++) {
441 arr[i] = countBits(colorMaskArray[i]);
442 if (arr[i] < 0) {
443 throw new IllegalArgumentException("Noncontiguous color mask ("
444 + Integer.toHexString(colorMaskArray[i])+
445 "at index "+i);
446 }
447 }
448 if (alphaMask != 0) {
449 arr[numColors] = countBits(alphaMask);
450 if (arr[numColors] < 0) {
451 throw new IllegalArgumentException("Noncontiguous alpha mask ("
452 + Integer.toHexString(alphaMask));
|