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 PackedColorModel cm = (PackedColorModel) obj;
401 if (this == cm) {
402 return true;
403 }
404
405 if (!super.equals(obj)) {
406 return false;
407 }
408
409 int numC = cm.getNumComponents();
410 for(int i=0; i < numC; i++) {
411 if (maskArray[i] != cm.getMask(i)) {
412 return false;
413 }
414 }
415 return true;
416 }
417
418 /**
419 * Returns the hash code for this PackedColorModel.
420 *
421 * @return a hash code for this PackedColorModel.
422 */
423 @Override
424 public int hashCode() {
425 int hash = 3;
426 hash = 89 * hash + super.hashCode();
427 hash = 89 * hash + Arrays.hashCode(this.maskArray);
428 return hash;
429 }
430
431 private static final int[] createBitsArray(int[]colorMaskArray,
432 int alphaMask) {
433 int numColors = colorMaskArray.length;
434 int numAlpha = (alphaMask == 0 ? 0 : 1);
435 int[] arr = new int[numColors+numAlpha];
436 for (int i=0; i < numColors; i++) {
437 arr[i] = countBits(colorMaskArray[i]);
438 if (arr[i] < 0) {
439 throw new IllegalArgumentException("Noncontiguous color mask ("
440 + Integer.toHexString(colorMaskArray[i])+
441 "at index "+i);
442 }
443 }
444 if (alphaMask != 0) {
445 arr[numColors] = countBits(alphaMask);
446 if (arr[numColors] < 0) {
447 throw new IllegalArgumentException("Noncontiguous alpha mask ("
448 + Integer.toHexString(alphaMask));
|