1 7 8 package javax.imageio; 9 10 import java.awt.Point ; 11 import java.awt.Transparency ; 12 import java.awt.image.BandedSampleModel ; 13 import java.awt.image.BufferedImage ; 14 import java.awt.image.ColorModel ; 15 import java.awt.color.ColorSpace ; 16 import java.awt.image.IndexColorModel ; 17 import java.awt.image.ComponentColorModel ; 18 import java.awt.image.DataBuffer ; 19 import java.awt.image.DirectColorModel ; 20 import java.awt.image.MultiPixelPackedSampleModel ; 21 import java.awt.image.PixelInterleavedSampleModel ; 22 import java.awt.image.SinglePixelPackedSampleModel ; 23 import java.awt.image.Raster ; 24 import java.awt.image.RenderedImage ; 25 import java.awt.image.SampleModel ; 26 import java.awt.image.WritableRaster ; 27 import java.util.Hashtable ; 28 29 36 public class ImageTypeSpecifier { 37 38 41 protected ColorModel colorModel; 42 43 46 protected SampleModel sampleModel; 47 48 52 private static ImageTypeSpecifier [] BISpecifier; 53 54 static { 56 ColorSpace sRGB = ColorSpace.getInstance(ColorSpace.CS_sRGB); 57 58 BISpecifier = 59 new ImageTypeSpecifier [BufferedImage.TYPE_BYTE_INDEXED + 1]; 60 61 BISpecifier[BufferedImage.TYPE_CUSTOM] = null; 62 63 BISpecifier[BufferedImage.TYPE_INT_RGB] = 64 createPacked(sRGB, 65 0x00ff0000, 66 0x0000ff00, 67 0x000000ff, 68 0x0, 69 DataBuffer.TYPE_INT, 70 false); 71 72 BISpecifier[BufferedImage.TYPE_INT_ARGB] = 73 createPacked(sRGB, 74 0x00ff0000, 75 0x0000ff00, 76 0x000000ff, 77 0xff000000, 78 DataBuffer.TYPE_INT, 79 false); 80 81 BISpecifier[BufferedImage.TYPE_INT_ARGB_PRE] = 82 createPacked(sRGB, 83 0x00ff0000, 84 0x0000ff00, 85 0x000000ff, 86 0xff000000, 87 DataBuffer.TYPE_INT, 88 true); 89 90 BISpecifier[BufferedImage.TYPE_INT_BGR] = 91 createPacked(sRGB, 92 0x000000ff, 93 0x0000ff00, 94 0x00ff0000, 95 0x0, 96 DataBuffer.TYPE_INT, 97 false); 98 99 int[] bOffsRGB = { 2, 1, 0 }; 100 BISpecifier[BufferedImage.TYPE_3BYTE_BGR] = 101 createInterleaved(sRGB, 102 bOffsRGB, 103 DataBuffer.TYPE_BYTE, 104 false, 105 false); 106 107 int[] bOffsABGR = { 3, 2, 1, 0 }; 108 BISpecifier[BufferedImage.TYPE_4BYTE_ABGR] = 109 createInterleaved(sRGB, 110 bOffsABGR, 111 DataBuffer.TYPE_BYTE, 112 true, 113 false); 114 115 BISpecifier[BufferedImage.TYPE_4BYTE_ABGR_PRE] = 116 createInterleaved(sRGB, 117 bOffsABGR, 118 DataBuffer.TYPE_BYTE, 119 true, 120 true); 121 122 BISpecifier[BufferedImage.TYPE_USHORT_565_RGB] = 123 createPacked(sRGB, 124 0xF800, 125 0x07E0, 126 0x001F, 127 0x0, 128 DataBuffer.TYPE_USHORT, 129 false); 130 131 BISpecifier[BufferedImage.TYPE_USHORT_555_RGB] = 132 createPacked(sRGB, 133 0x7C00, 134 0x03E0, 135 0x001F, 136 0x0, 137 DataBuffer.TYPE_USHORT, 138 false); 139 140 BISpecifier[BufferedImage.TYPE_BYTE_GRAY] = 141 createGrayscale(8, 142 DataBuffer.TYPE_BYTE, 143 false); 144 145 BISpecifier[BufferedImage.TYPE_USHORT_GRAY] = 146 createGrayscale(16, 147 DataBuffer.TYPE_USHORT, 148 false); 149 150 BISpecifier[BufferedImage.TYPE_BYTE_BINARY] = 151 createGrayscale(1, 152 DataBuffer.TYPE_BYTE, 153 false); 154 155 BufferedImage bi = 156 new BufferedImage (1, 1, BufferedImage.TYPE_BYTE_INDEXED); 157 IndexColorModel icm = (IndexColorModel )bi.getColorModel(); 158 int mapSize = icm.getMapSize(); 159 byte[] redLUT = new byte[mapSize]; 160 byte[] greenLUT = new byte[mapSize]; 161 byte[] blueLUT = new byte[mapSize]; 162 byte[] alphaLUT = new byte[mapSize]; 163 164 icm.getReds(redLUT); 165 icm.getGreens(greenLUT); 166 icm.getBlues(blueLUT); 167 icm.getAlphas(alphaLUT); 168 169 BISpecifier[BufferedImage.TYPE_BYTE_INDEXED] = 170 createIndexed(redLUT, greenLUT, blueLUT, alphaLUT, 171 8, 172 DataBuffer.TYPE_BYTE); 173 } 174 175 178 private ImageTypeSpecifier() {} 179 180 194 public ImageTypeSpecifier(ColorModel colorModel, SampleModel sampleModel) { 195 if (colorModel == null) { 196 throw new IllegalArgumentException ("colorModel == null!"); 197 } 198 if (sampleModel == null) { 199 throw new IllegalArgumentException ("sampleModel == null!"); 200 } 201 if (!colorModel.isCompatibleSampleModel(sampleModel)) { 202 throw new IllegalArgumentException 203 ("sampleModel is incompatible with colorModel!"); 204 } 205 this.colorModel = colorModel; 206 this.sampleModel = sampleModel; 207 } 208 209 222 public ImageTypeSpecifier(RenderedImage image) { 223 if (image == null) { 224 throw new IllegalArgumentException ("image == null!"); 225 } 226 colorModel = image.getColorModel(); 227 sampleModel = image.getSampleModel(); 228 } 229 230 232 static class Packed extends ImageTypeSpecifier { 233 ColorSpace colorSpace; 234 int redMask; 235 int greenMask; 236 int blueMask; 237 int alphaMask; 238 int transferType; 239 boolean isAlphaPremultiplied; 240 241 public Packed(ColorSpace colorSpace, 242 int redMask, 243 int greenMask, 244 int blueMask, 245 int alphaMask, int transferType, 247 boolean isAlphaPremultiplied) { 248 if (colorSpace == null) { 249 throw new IllegalArgumentException ("colorSpace == null!"); 250 } 251 if (colorSpace.getType() != ColorSpace.TYPE_RGB) { 252 throw new IllegalArgumentException 253 ("colorSpace is not of type TYPE_RGB!"); 254 } 255 if (transferType != DataBuffer.TYPE_BYTE && 256 transferType != DataBuffer.TYPE_USHORT && 257 transferType != DataBuffer.TYPE_INT) { 258 throw new IllegalArgumentException 259 ("Bad value for transferType!"); 260 } 261 if (redMask == 0 && greenMask == 0 && 262 blueMask == 0 && alphaMask == 0) { 263 throw new IllegalArgumentException 264 ("No mask has at least 1 bit set!"); 265 } 266 this.colorSpace = colorSpace; 267 this.redMask = redMask; 268 this.greenMask = greenMask; 269 this.blueMask = blueMask; 270 this.alphaMask = alphaMask; 271 this.transferType = transferType; 272 this.isAlphaPremultiplied = isAlphaPremultiplied; 273 274 int bits = 32; 275 this.colorModel = 276 new DirectColorModel (colorSpace, 277 bits, 278 redMask, greenMask, blueMask, 279 alphaMask, isAlphaPremultiplied, 280 transferType); 281 this.sampleModel = colorModel.createCompatibleSampleModel(1, 1); 282 } 283 } 284 285 319 public static ImageTypeSpecifier 320 createPacked(ColorSpace colorSpace, 321 int redMask, 322 int greenMask, 323 int blueMask, 324 int alphaMask, int transferType, 326 boolean isAlphaPremultiplied) { 327 return new ImageTypeSpecifier.Packed (colorSpace, 328 redMask, 329 greenMask, 330 blueMask, 331 alphaMask, transferType, 333 isAlphaPremultiplied); 334 } 335 336 static ColorModel createComponentCM(ColorSpace colorSpace, 337 int numBands, 338 int dataType, 339 boolean hasAlpha, 340 boolean isAlphaPremultiplied) { 341 int transparency = 342 hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE; 343 344 int[] numBits = new int[numBands]; 345 int bits = DataBuffer.getDataTypeSize(dataType); 346 347 for (int i = 0; i < numBands; i++) { 348 numBits[i] = bits; 349 } 350 351 return new ComponentColorModel (colorSpace, 352 numBits, 353 hasAlpha, 354 isAlphaPremultiplied, 355 transparency, 356 dataType); 357 } 358 359 361 static class Interleaved extends ImageTypeSpecifier { 362 ColorSpace colorSpace; 363 int[] bandOffsets; 364 int dataType; 365 boolean hasAlpha; 366 boolean isAlphaPremultiplied; 367 368 public Interleaved(ColorSpace colorSpace, 369 int[] bandOffsets, 370 int dataType, 371 boolean hasAlpha, 372 boolean isAlphaPremultiplied) { 373 if (colorSpace == null) { 374 throw new IllegalArgumentException ("colorSpace == null!"); 375 } 376 if (bandOffsets == null) { 377 throw new IllegalArgumentException ("bandOffsets == null!"); 378 } 379 int numBands = colorSpace.getNumComponents() + 380 (hasAlpha ? 1 : 0); 381 if (bandOffsets.length != numBands) { 382 throw new IllegalArgumentException 383 ("bandOffsets.length is wrong!"); 384 } 385 if (dataType != DataBuffer.TYPE_BYTE && 386 dataType != DataBuffer.TYPE_SHORT && 387 dataType != DataBuffer.TYPE_USHORT && 388 dataType != DataBuffer.TYPE_INT && 389 dataType != DataBuffer.TYPE_FLOAT && 390 dataType != DataBuffer.TYPE_DOUBLE) { 391 throw new IllegalArgumentException 392 ("Bad value for dataType!"); 393 } 394 this.colorSpace = colorSpace; 395 this.bandOffsets = (int[])bandOffsets.clone(); 396 this.dataType = dataType; 397 this.hasAlpha = hasAlpha; 398 this.isAlphaPremultiplied = isAlphaPremultiplied; 399 400 this.colorModel = 401 ImageTypeSpecifier.createComponentCM(colorSpace, 402 bandOffsets.length, 403 dataType, 404 hasAlpha, 405 isAlphaPremultiplied); 406 407 int minBandOffset = bandOffsets[0]; 408 int maxBandOffset = minBandOffset; 409 for (int i = 0; i < bandOffsets.length; i++) { 410 int offset = bandOffsets[i]; 411 minBandOffset = Math.min(offset, minBandOffset); 412 maxBandOffset = Math.max(offset, maxBandOffset); 413 } 414 int pixelStride = maxBandOffset - minBandOffset + 1; 415 416 int w = 1; 417 int h = 1; 418 this.sampleModel = 419 new PixelInterleavedSampleModel (dataType, 420 w, h, 421 pixelStride, 422 w*pixelStride, 423 bandOffsets); 424 } 425 426 public boolean equals(Object o) { 427 if ((o == null) || 428 !(o instanceof ImageTypeSpecifier.Interleaved )) { 429 return false; 430 } 431 432 ImageTypeSpecifier.Interleaved that = 433 (ImageTypeSpecifier.Interleaved )o; 434 435 if ((!(this.colorSpace.equals(that.colorSpace))) || 436 (this.dataType != that.dataType) || 437 (this.hasAlpha != that.hasAlpha) || 438 (this.isAlphaPremultiplied != that.isAlphaPremultiplied) || 439 (this.bandOffsets.length != that.bandOffsets.length)) { 440 return false; 441 } 442 443 for (int i = 0; i < bandOffsets.length; i++) { 444 if (this.bandOffsets[i] != that.bandOffsets[i]) { 445 return false; 446 } 447 } 448 449 return true; 450 } 451 452 public int hashCode() { 453 return (super.hashCode() + 454 (4 * bandOffsets.length) + 455 (25 * dataType) + 456 (hasAlpha ? 17 : 18)); 457 } 458 } 459 460 489 public static ImageTypeSpecifier 490 createInterleaved(ColorSpace colorSpace, 491 int[] bandOffsets, 492 int dataType, 493 boolean hasAlpha, 494 boolean isAlphaPremultiplied) { 495 return new ImageTypeSpecifier.Interleaved (colorSpace, 496 bandOffsets, 497 dataType, 498 hasAlpha, 499 isAlphaPremultiplied); 500 } 501 502 504 static class Banded extends ImageTypeSpecifier { 505 ColorSpace colorSpace; 506 int[] bankIndices; 507 int[] bandOffsets; 508 int dataType; 509 boolean hasAlpha; 510 boolean isAlphaPremultiplied; 511 512 public Banded(ColorSpace colorSpace, 513 int[] bankIndices, 514 int[] bandOffsets, 515 int dataType, 516 boolean hasAlpha, 517 boolean isAlphaPremultiplied) { 518 if (colorSpace == null) { 519 throw new IllegalArgumentException ("colorSpace == null!"); 520 } 521 if (bankIndices == null) { 522 throw new IllegalArgumentException ("bankIndices == null!"); 523 } 524 if (bandOffsets == null) { 525 throw new IllegalArgumentException ("bandOffsets == null!"); 526 } 527 if (bankIndices.length != bandOffsets.length) { 528 throw new IllegalArgumentException 529 ("bankIndices.length != bandOffsets.length!"); 530 } 531 if (dataType != DataBuffer.TYPE_BYTE && 532 dataType != DataBuffer.TYPE_SHORT && 533 dataType != DataBuffer.TYPE_USHORT && 534 dataType != DataBuffer.TYPE_INT && 535 dataType != DataBuffer.TYPE_FLOAT && 536 dataType != DataBuffer.TYPE_DOUBLE) { 537 throw new IllegalArgumentException 538 ("Bad value for dataType!"); 539 } 540 int numBands = colorSpace.getNumComponents() + 541 (hasAlpha ? 1 : 0); 542 if (bandOffsets.length != numBands) { 543 throw new IllegalArgumentException 544 ("bandOffsets.length is wrong!"); 545 } 546 547 this.colorSpace = colorSpace; 548 this.bankIndices = (int[])bankIndices.clone(); 549 this.bandOffsets = (int[])bandOffsets.clone(); 550 this.dataType = dataType; 551 this.hasAlpha = hasAlpha; 552 this.isAlphaPremultiplied = isAlphaPremultiplied; 553 554 this.colorModel = 555 ImageTypeSpecifier.createComponentCM(colorSpace, 556 bankIndices.length, 557 dataType, 558 hasAlpha, 559 isAlphaPremultiplied); 560 561 int w = 1; 562 int h = 1; 563 this.sampleModel = new BandedSampleModel (dataType, 564 w, h, 565 w, 566 bankIndices, 567 bandOffsets); 568 } 569 570 public boolean equals(Object o) { 571 if ((o == null) || 572 !(o instanceof ImageTypeSpecifier.Banded )) { 573 return false; 574 } 575 576 ImageTypeSpecifier.Banded that = 577 (ImageTypeSpecifier.Banded )o; 578 579 if ((!(this.colorSpace.equals(that.colorSpace))) || 580 (this.dataType != that.dataType) || 581 (this.hasAlpha != that.hasAlpha) || 582 (this.isAlphaPremultiplied != that.isAlphaPremultiplied) || 583 (this.bankIndices.length != that.bankIndices.length) || 584 (this.bandOffsets.length != that.bandOffsets.length)) { 585 return false; 586 } 587 588 for (int i = 0; i < bankIndices.length; i++) { 589 if (this.bankIndices[i] != that.bankIndices[i]) { 590 return false; 591 } 592 } 593 594 for (int i = 0; i < bandOffsets.length; i++) { 595 if (this.bandOffsets[i] != that.bandOffsets[i]) { 596 return false; 597 } 598 } 599 600 return true; 601 } 602 603 public int hashCode() { 604 return (super.hashCode() + 605 (3 * bandOffsets.length) + 606 (7 * bankIndices.length) + 607 (21 * dataType) + 608 (hasAlpha ? 19 : 29)); 609 } 610 } 611 612 647 public static ImageTypeSpecifier 648 createBanded(ColorSpace colorSpace, 649 int[] bankIndices, 650 int[] bandOffsets, 651 int dataType, 652 boolean hasAlpha, 653 boolean isAlphaPremultiplied) { 654 return new ImageTypeSpecifier.Banded (colorSpace, 655 bankIndices, 656 bandOffsets, 657 dataType, 658 hasAlpha, 659 isAlphaPremultiplied); 660 } 661 662 664 static class Grayscale extends ImageTypeSpecifier { 665 int bits; 666 int dataType; 667 boolean isSigned; 668 boolean hasAlpha; 669 boolean isAlphaPremultiplied; 670 671 public Grayscale(int bits, 672 int dataType, 673 boolean isSigned, 674 boolean hasAlpha, 675 boolean isAlphaPremultiplied) 676 { 677 if (bits != 1 && bits != 2 && bits != 4 && 678 bits != 8 && bits != 16) 679 { 680 throw new IllegalArgumentException ("Bad value for bits!"); 681 } 682 if (dataType != DataBuffer.TYPE_BYTE && 683 dataType != DataBuffer.TYPE_SHORT && 684 dataType != DataBuffer.TYPE_USHORT) 685 { 686 throw new IllegalArgumentException 687 ("Bad value for dataType!"); 688 } 689 if (bits > 8 && dataType == DataBuffer.TYPE_BYTE) { 690 throw new IllegalArgumentException 691 ("Too many bits for dataType!"); 692 } 693 694 this.bits = bits; 695 this.dataType = dataType; 696 this.isSigned = isSigned; 697 this.hasAlpha = hasAlpha; 698 this.isAlphaPremultiplied = isAlphaPremultiplied; 699 700 ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY); 701 702 if ((bits == 8 && dataType == DataBuffer.TYPE_BYTE) || 703 (bits == 16 && 704 (dataType == DataBuffer.TYPE_SHORT || 705 dataType == DataBuffer.TYPE_USHORT))) { 706 708 int numBands = hasAlpha ? 2 : 1; 709 int transparency = 710 hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE; 711 712 713 int[] nBits = new int[numBands]; 714 nBits[0] = bits; 715 if (numBands == 2) { 716 nBits[1] = bits; 717 } 718 this.colorModel = 719 new ComponentColorModel (colorSpace, 720 nBits, 721 hasAlpha, 722 isAlphaPremultiplied, 723 transparency, 724 dataType); 725 726 int[] bandOffsets = new int[numBands]; 727 bandOffsets[0] = 0; 728 if (numBands == 2) { 729 bandOffsets[1] = 1; 730 } 731 732 int w = 1; 733 int h = 1; 734 this.sampleModel = 735 new PixelInterleavedSampleModel (dataType, 736 w, h, 737 numBands, w*numBands, 738 bandOffsets); 739 } else { 740 int numEntries = 1 << bits; 741 byte[] arr = new byte[numEntries]; 742 for (int i = 0; i < numEntries; i++) { 743 arr[i] = (byte)(i*255/(numEntries - 1)); 744 } 745 this.colorModel = 746 new IndexColorModel (bits, numEntries, arr, arr, arr); 747 748 this.sampleModel = 749 new MultiPixelPackedSampleModel (dataType, 1, 1, bits); 750 } 751 } 752 } 753 754 777 public static ImageTypeSpecifier 778 createGrayscale(int bits, 779 int dataType, 780 boolean isSigned) { 781 return new ImageTypeSpecifier.Grayscale (bits, 782 dataType, 783 isSigned, 784 false, 785 false); 786 } 787 788 813 public static ImageTypeSpecifier 814 createGrayscale(int bits, 815 int dataType, 816 boolean isSigned, 817 boolean isAlphaPremultiplied) { 818 return new ImageTypeSpecifier.Grayscale (bits, 819 dataType, 820 isSigned, 821 true, 822 isAlphaPremultiplied); 823 } 824 825 827 static class Indexed extends ImageTypeSpecifier { 828 byte[] redLUT; 829 byte[] greenLUT; 830 byte[] blueLUT; 831 byte[] alphaLUT = null; 832 int bits; 833 int dataType; 834 835 public Indexed(byte[] redLUT, 836 byte[] greenLUT, 837 byte[] blueLUT, 838 byte[] alphaLUT, 839 int bits, 840 int dataType) { 841 if (redLUT == null || greenLUT == null || blueLUT == null) { 842 throw new IllegalArgumentException ("LUT is null!"); 843 } 844 if (bits != 1 && bits != 2 && bits != 4 && 845 bits != 8 && bits != 16) { 846 throw new IllegalArgumentException ("Bad value for bits!"); 847 } 848 if (dataType != DataBuffer.TYPE_BYTE && 849 dataType != DataBuffer.TYPE_SHORT && 850 dataType != DataBuffer.TYPE_USHORT && 851 dataType != DataBuffer.TYPE_INT) { 852 throw new IllegalArgumentException 853 ("Bad value for dataType!"); 854 } 855 if ((bits > 8 && dataType == DataBuffer.TYPE_BYTE) || 856 (bits > 16 && dataType != DataBuffer.TYPE_INT)) { 857 throw new IllegalArgumentException 858 ("Too many bits for dataType!"); 859 } 860 861 int len = 1 << bits; 862 if (redLUT.length != len || 863 greenLUT.length != len || 864 blueLUT.length != len || 865 (alphaLUT != null && alphaLUT.length != len)) { 866 throw new IllegalArgumentException ("LUT has improper length!"); 867 } 868 this.redLUT = (byte[])redLUT.clone(); 869 this.greenLUT = (byte[])greenLUT.clone(); 870 this.blueLUT = (byte[])blueLUT.clone(); 871 if (alphaLUT != null) { 872 this.alphaLUT = (byte[])alphaLUT.clone(); 873 } 874 this.bits = bits; 875 this.dataType = dataType; 876 877 if (alphaLUT == null) { 878 this.colorModel = new IndexColorModel (bits, 879 redLUT.length, 880 redLUT, 881 greenLUT, 882 blueLUT); 883 } else { 884 this.colorModel = new IndexColorModel (bits, 885 redLUT.length, 886 redLUT, 887 greenLUT, 888 blueLUT, 889 alphaLUT); 890 } 891 892 if ((bits == 8 && dataType == DataBuffer.TYPE_BYTE) || 893 (bits == 16 && 894 (dataType == DataBuffer.TYPE_SHORT || 895 dataType == DataBuffer.TYPE_USHORT))) { 896 int[] bandOffsets = { 0 }; 897 this.sampleModel = 898 new PixelInterleavedSampleModel (dataType, 899 1, 1, 1, 1, 900 bandOffsets); 901 } else { 902 this.sampleModel = 903 new MultiPixelPackedSampleModel (dataType, 1, 1, bits); 904 } 905 } 906 } 907 908 948 public static ImageTypeSpecifier 949 createIndexed(byte[] redLUT, 950 byte[] greenLUT, 951 byte[] blueLUT, 952 byte[] alphaLUT, 953 int bits, 954 int dataType) { 955 return new ImageTypeSpecifier.Indexed (redLUT, 956 greenLUT, 957 blueLUT, 958 alphaLUT, 959 bits, 960 dataType); 961 } 962 963 993 public static 994 ImageTypeSpecifier createFromBufferedImageType(int bufferedImageType) { 995 if (bufferedImageType >= BufferedImage.TYPE_INT_RGB && 996 bufferedImageType <= BufferedImage.TYPE_BYTE_INDEXED) { 997 return BISpecifier[bufferedImageType]; 998 } else if (bufferedImageType == BufferedImage.TYPE_CUSTOM) { 999 throw new IllegalArgumentException ("Cannot create from TYPE_CUSTOM!"); 1000 } else { 1001 throw new IllegalArgumentException ("Invalid BufferedImage type!"); 1002 } 1003 } 1004 1005 1018 public static 1019 ImageTypeSpecifier createFromRenderedImage(RenderedImage image) { 1020 if (image == null) { 1021 throw new IllegalArgumentException ("image == null!"); 1022 } 1023 1024 if (image instanceof BufferedImage ) { 1025 int bufferedImageType = ((BufferedImage )image).getType(); 1026 if (bufferedImageType != BufferedImage.TYPE_CUSTOM) { 1027 return BISpecifier[bufferedImageType]; 1028 } 1029 } 1030 1031 return new ImageTypeSpecifier (image); 1032 } 1033 1034 1057 public int getBufferedImageType() { 1058 BufferedImage bi = createBufferedImage(1, 1); 1059 return bi.getType(); 1060 } 1061 1062 1069 public int getNumComponents() { 1070 return colorModel.getNumComponents(); 1071 } 1072 1073 1080 public int getNumBands() { 1081 return sampleModel.getNumBands(); 1082 } 1083 1084 1095 public int getBitsPerBand(int band) { 1096 if (band < 0 | band >= getNumBands()) { 1097 throw new IllegalArgumentException ("band out of range!"); 1098 } 1099 return sampleModel.getSampleSize(band); 1100 } 1101 1102 1109 public SampleModel getSampleModel() { 1110 return sampleModel; 1111 } 1112 1113 1130 public SampleModel getSampleModel(int width, int height) { 1131 if ((long)width*height > Integer.MAX_VALUE) { 1132 throw new IllegalArgumentException 1133 ("width*height > Integer.MAX_VALUE!"); 1134 } 1135 return sampleModel.createCompatibleSampleModel(width, height); 1136 } 1137 1138 1143 public ColorModel getColorModel() { 1144 return colorModel; 1145 } 1146 1147 1166 public BufferedImage createBufferedImage(int width, int height) { 1167 try { 1168 SampleModel sampleModel = getSampleModel(width, height); 1169 WritableRaster raster = 1170 Raster.createWritableRaster(sampleModel, 1171 new Point (0, 0)); 1172 return new BufferedImage (colorModel, raster, 1173 colorModel.isAlphaPremultiplied(), 1174 new Hashtable ()); 1175 } catch (NegativeArraySizeException e) { 1176 throw new IllegalArgumentException 1178 ("Array size > Integer.MAX_VALUE!"); 1179 } 1180 } 1181 1182 1193 public boolean equals(Object o) { 1194 if ((o == null) || !(o instanceof ImageTypeSpecifier )) { 1195 return false; 1196 } 1197 1198 ImageTypeSpecifier that = (ImageTypeSpecifier )o; 1199 return (colorModel.equals(that.colorModel)) && 1200 (sampleModel.equals(that.sampleModel)); 1201 } 1202 1203 1208 public int hashCode() { 1209 return (9 * colorModel.hashCode()) + (14 * sampleModel.hashCode()); 1210 } 1211} 1212 | Popular Tags |