| 1 7 8 package java.awt.image; 9 10 import java.awt.Transparency ; 11 import java.awt.color.ColorSpace ; 12 import java.awt.color.ICC_ColorSpace ; 13 import sun.awt.color.ICC_Transform; 14 import sun.awt.color.CMM; 15 import java.awt.Toolkit ; 16 import java.util.Collections ; 17 import java.util.Map ; 18 import java.util.WeakHashMap ; 19 20 138 public abstract class ColorModel implements Transparency { 139 private long pData; 141 144 protected int pixel_bits; 145 int nBits[]; 146 int transparency = Transparency.TRANSLUCENT; 147 boolean supportsAlpha = true; 148 boolean isAlphaPremultiplied = false; 149 int numComponents = -1; 150 int numColorComponents = -1; 151 ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB); 152 int colorSpaceType = ColorSpace.TYPE_RGB; 153 int maxBits; 154 boolean is_sRGB = true; 155 156 159 protected int transferType; 160 161 185 private static boolean loaded = false; 186 static void loadLibraries() { 187 if (!loaded) { 188 java.security.AccessController.doPrivileged( 189 new sun.security.action.LoadLibraryAction("awt")); 190 loaded = true; 191 } 192 } 193 private static native void initIDs(); 194 static { 195 196 loadLibraries(); 197 initIDs(); 198 } 199 private static ColorModel RGBdefault; 200 201 217 public static ColorModel getRGBdefault() { 218 if (RGBdefault == null) { 219 RGBdefault = new DirectColorModel (32, 220 0x00ff0000, 0x0000ff00, 0x000000ff, 0xff000000 ); 225 } 226 return RGBdefault; 227 } 228 229 249 public ColorModel(int bits) { 250 pixel_bits = bits; 251 if (bits < 1) { 252 throw new IllegalArgumentException ("Number of bits must be > 0"); 253 } 254 numComponents = 4; 255 numColorComponents = 3; 256 maxBits = bits; 257 transferType = ColorModel.getDefaultTransferType(bits); 259 } 260 261 307 protected ColorModel(int pixel_bits, int[] bits, ColorSpace cspace, 308 boolean hasAlpha, 309 boolean isAlphaPremultiplied, 310 int transparency, 311 int transferType) { 312 colorSpace = cspace; 313 colorSpaceType = cspace.getType(); 314 numColorComponents = cspace.getNumComponents(); 315 numComponents = numColorComponents + (hasAlpha ? 1 : 0); 316 supportsAlpha = hasAlpha; 317 if (bits.length < numComponents) { 318 throw new IllegalArgumentException ("Number of color/alpha "+ 319 "components should be "+ 320 numComponents+ 321 " but length of bits array is "+ 322 bits.length); 323 } 324 325 if (transparency < Transparency.OPAQUE || 327 transparency > Transparency.TRANSLUCENT) 328 { 329 throw new IllegalArgumentException ("Unknown transparency: "+ 330 transparency); 331 } 332 333 if (supportsAlpha == false) { 334 this.isAlphaPremultiplied = false; 335 this.transparency = Transparency.OPAQUE; 336 } 337 else { 338 this.isAlphaPremultiplied = isAlphaPremultiplied; 339 this.transparency = transparency; 340 } 341 342 nBits = (int[]) bits.clone(); 343 this.pixel_bits = pixel_bits; 344 if (pixel_bits <= 0) { 345 throw new IllegalArgumentException ("Number of pixel bits must "+ 346 "be > 0"); 347 } 348 maxBits = 0; 350 for (int i=0; i < bits.length; i++) { 351 if (bits[i] < 0) { 353 throw new 354 IllegalArgumentException ("Number of bits must be >= 0"); 355 } 356 if (maxBits < bits[i]) { 357 maxBits = bits[i]; 358 } 359 } 360 361 if (maxBits == 0) { 363 throw new IllegalArgumentException ("There must be at least "+ 364 "one component with > 0 "+ 365 "pixel bits."); 366 } 367 368 if (cspace != ColorSpace.getInstance(ColorSpace.CS_sRGB)) { 370 is_sRGB = false; 371 } 372 373 this.transferType = transferType; 375 } 376 377 383 final public boolean hasAlpha() { 384 return supportsAlpha; 385 } 386 387 399 final public boolean isAlphaPremultiplied() { 400 return isAlphaPremultiplied; 401 } 402 403 409 final public int getTransferType() { 410 return transferType; 411 } 412 413 418 public int getPixelSize() { 419 return pixel_bits; 420 } 421 422 440 public int getComponentSize(int componentIdx) { 441 if (nBits == null) { 443 throw new NullPointerException ("Number of bits array is null."); 444 } 445 446 return nBits[componentIdx]; 447 } 448 449 456 public int[] getComponentSize() { 457 if (nBits != null) { 458 return (int[]) nBits.clone(); 459 } 460 461 return null; 462 } 463 464 472 public int getTransparency() { 473 return transparency; 474 } 475 476 482 public int getNumComponents() { 483 return numComponents; 484 } 485 486 495 public int getNumColorComponents() { 496 return numColorComponents; 497 } 498 499 512 public abstract int getRed(int pixel); 513 514 527 public abstract int getGreen(int pixel); 528 529 542 public abstract int getBlue(int pixel); 543 544 553 public abstract int getAlpha(int pixel); 554 555 570 public int getRGB(int pixel) { 571 return (getAlpha(pixel) << 24) 572 | (getRed(pixel) << 16) 573 | (getGreen(pixel) << 8) 574 | (getBlue(pixel) << 0); 575 } 576 577 613 public int getRed(Object inData) { 614 int pixel=0,length=0; 615 switch (transferType) { 616 case DataBuffer.TYPE_BYTE: 617 byte bdata[] = (byte[])inData; 618 pixel = bdata[0] & 0xff; 619 length = bdata.length; 620 break; 621 case DataBuffer.TYPE_USHORT: 622 short sdata[] = (short[])inData; 623 pixel = sdata[0] & 0xffff; 624 length = sdata.length; 625 break; 626 case DataBuffer.TYPE_INT: 627 int idata[] = (int[])inData; 628 pixel = idata[0]; 629 length = idata.length; 630 break; 631 default: 632 throw new UnsupportedOperationException ("This method has not been "+ 633 "implemented for transferType " + transferType); 634 } 635 if (length == 1) { 636 return getRed(pixel); 637 } 638 else { 639 throw new UnsupportedOperationException  640 ("This method is not supported by this color model"); 641 } 642 } 643 644 680 public int getGreen(Object inData) { 681 int pixel=0,length=0; 682 switch (transferType) { 683 case DataBuffer.TYPE_BYTE: 684 byte bdata[] = (byte[])inData; 685 pixel = bdata[0] & 0xff; 686 length = bdata.length; 687 break; 688 case DataBuffer.TYPE_USHORT: 689 short sdata[] = (short[])inData; 690 pixel = sdata[0] & 0xffff; 691 length = sdata.length; 692 break; 693 case DataBuffer.TYPE_INT: 694 int idata[] = (int[])inData; 695 pixel = idata[0]; 696 length = idata.length; 697 break; 698 default: 699 throw new UnsupportedOperationException ("This method has not been "+ 700 "implemented for transferType " + transferType); 701 } 702 if (length == 1) { 703 return getGreen(pixel); 704 } 705 else { 706 throw new UnsupportedOperationException  707 ("This method is not supported by this color model"); 708 } 709 } 710 711 747 public int getBlue(Object inData) { 748 int pixel=0,length=0; 749 switch (transferType) { 750 case DataBuffer.TYPE_BYTE: 751 byte bdata[] = (byte[])inData; 752 pixel = bdata[0] & 0xff; 753 length = bdata.length; 754 break; 755 case DataBuffer.TYPE_USHORT: 756 short sdata[] = (short[])inData; 757 pixel = sdata[0] & 0xffff; 758 length = sdata.length; 759 break; 760 case DataBuffer.TYPE_INT: 761 int idata[] = (int[])inData; 762 pixel = idata[0]; 763 length = idata.length; 764 break; 765 default: 766 throw new UnsupportedOperationException ("This method has not been "+ 767 "implemented for transferType " + transferType); 768 } 769 if (length == 1) { 770 return getBlue(pixel); 771 } 772 else { 773 throw new UnsupportedOperationException  774 ("This method is not supported by this color model"); 775 } 776 } 777 778 810 public int getAlpha(Object inData) { 811 int pixel=0,length=0; 812 switch (transferType) { 813 case DataBuffer.TYPE_BYTE: 814 byte bdata[] = (byte[])inData; 815 pixel = bdata[0] & 0xff; 816 length = bdata.length; 817 break; 818 case DataBuffer.TYPE_USHORT: 819 short sdata[] = (short[])inData; 820 pixel = sdata[0] & 0xffff; 821 length = sdata.length; 822 break; 823 case DataBuffer.TYPE_INT: 824 int idata[] = (int[])inData; 825 pixel = idata[0]; 826 length = idata.length; 827 break; 828 default: 829 throw new UnsupportedOperationException ("This method has not been "+ 830 "implemented for transferType " + transferType); 831 } 832 if (length == 1) { 833 return getAlpha(pixel); 834 } 835 else { 836 throw new UnsupportedOperationException  837 ("This method is not supported by this color model"); 838 } 839 } 840 841 858 public int getRGB(Object inData) { 859 return (getAlpha(inData) << 24) 860 | (getRed(inData) << 16) 861 | (getGreen(inData) << 8) 862 | (getBlue(inData) << 0); 863 } 864 865 902 public Object getDataEle
|