| 1 7 8 package java.awt.image; 9 10 import java.awt.color.ColorSpace ; 11 import java.awt.Transparency ; 12 13 92 public class DirectColorModel extends PackedColorModel { 93 private int red_mask; 94 private int green_mask; 95 private int blue_mask; 96 private int alpha_mask; 97 private int red_offset; 98 private int green_offset; 99 private int blue_offset; 100 private int alpha_offset; 101 private int red_scale; 102 private int green_scale; 103 private int blue_scale; 104 private int alpha_scale; 105 private boolean is_LinearRGB; 106 private int lRGBprecision; 107 private byte[] tosRGB8LUT; 108 private byte[] fromsRGB8LUT8; 109 private short[] fromsRGB8LUT16; 110 111 133 public DirectColorModel(int bits, 134 int rmask, int gmask, int bmask) { 135 this(bits, rmask, gmask, bmask, 0); 136 } 137 138 163 public DirectColorModel(int bits, int rmask, int gmask, 164 int bmask, int amask) { 165 super (ColorSpace.getInstance(ColorSpace.CS_sRGB), 166 bits, rmask, gmask, bmask, amask, false, 167 amask == 0 ? Transparency.OPAQUE : Transparency.TRANSLUCENT, 168 ColorModel.getDefaultTransferType(bits)); 169 setFields(); 170 } 171 172 213 public DirectColorModel(ColorSpace space, int bits, int rmask, 214 int gmask, int bmask, int amask, 215 boolean isAlphaPremultiplied, 216 int transferType) { 217 super (space, bits, rmask, gmask, bmask, amask, 218 isAlphaPremultiplied, 219 amask == 0 ? Transparency.OPAQUE : Transparency.TRANSLUCENT, 220 transferType); 221 if (ColorModel.isLinearRGBspace(colorSpace)) { 222 is_LinearRGB = true; 223 if (maxBits <= 8) { 224 lRGBprecision = 8; 225 tosRGB8LUT = ColorModel.getLinearRGB8TosRGB8LUT(); 226 fromsRGB8LUT8 = ColorModel.getsRGB8ToLinearRGB8LUT(); 227 } else { 228 lRGBprecision = 16; 229 tosRGB8LUT = ColorModel.getLinearRGB16TosRGB8LUT(); 230 fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT(); 231 } 232 } else if (!is_sRGB) { 233 for (int i = 0; i < 3; i++) { 234 if ((space.getMinValue(i) != 0.0f) || 237 (space.getMaxValue(i) != 1.0f)) { 238 throw new IllegalArgumentException ( 239 "Illegal min/max RGB component value"); 240 } 241 } 242 } 243 setFields(); 244 } 245 246 252 final public int getRedMask() { 253 return maskArray[0]; 254 } 255 256 262 final public int getGreenMask() { 263 return maskArray[1]; 264 } 265 266 272 final public int getBlueMask() { 273 return maskArray[2]; 274 } 275 276 282 final public int getAlphaMask() { 283 if (supportsAlpha) { 284 return maskArray[3]; 285 } else { 286 return 0; 287 } 288 } 289 290 291 296 private float[] getDefaultRGBComponents(int pixel) { 297 int components[] = getComponents(pixel, null, 0); 298 float norm[] = getNormalizedComponents(components, 0, null, 0); 299 return colorSpace.toRGB(norm); 301 } 302 303 304 private int getsRGBComponentFromsRGB(int pixel, int idx) { 305 int c = ((pixel & maskArray[idx]) >>> maskOffsets[idx]); 306 if (isAlphaPremultiplied) { 307 int a = ((pixel & maskArray[3]) >>> maskOffsets[3]); 308 c = (a == 0) ? 0 : 309 (int) (((c * scaleFactors[idx]) * 255.0f / 310 (a * scaleFactors[3])) + 0.5f); 311 } else if (scaleFactors[idx] != 1.0f) { 312 c = (int) ((c * scaleFactors[idx]) + 0.5f); 313 } 314 return c; 315 } 316 317 318 private int getsRGBComponentFromLinearRGB(int pixel, int idx) { 319 int c = ((pixel & maskArray[idx]) >>> maskOffsets[idx]); 320 if (isAlphaPremultiplied) { 321 float factor = (float) ((1 << lRGBprecision) - 1); 322 int a = ((pixel & maskArray[3]) >>> maskOffsets[3]); 323 c = (a == 0) ? 0 : 324 (int) (((c * scaleFactors[idx]) * factor / 325 (a * scaleFactors[3])) + 0.5f); 326 } else if (nBits[idx] != lRGBprecision) { 327 if (lRGBprecision == 16) { 328 c = (int) ((c * scaleFactors[idx] * 257.0f) + 0.5f); 329 } else { 330 c = (int) ((c * scaleFactors[idx]) + 0.5f); 331 } 332 } 333 return tosRGB8LUT[c] & 0xff; 335 } 336 337 338 351 final public int getRed(int pixel) { 352 if (is_sRGB) { 353 return getsRGBComponentFromsRGB(pixel, 0); 354 } else if (is_LinearRGB) { 355 return getsRGBComponentFromLinearRGB(pixel, 0); 356 } 357 float rgb[] = getDefaultRGBComponents(pixel); 358 return (int) (rgb[0] * 255.0f + 0.5f); 359 } 360 361 374 final public int getGreen(int pixel) { 375 if (is_sRGB) { 376 return getsRGBComponentFromsRGB(pixel, 1); 377 } else if (is_LinearRGB) { 378 return getsRGBComponentFromLinearRGB(pixel, 1); 379 } 380 float rgb[] = getDefaultRGBComponents(pixel); 381 return (int) (rgb[1] * 255.0f + 0.5f); 382 } 383 384 397 final public int getBlue(int pixel) { 398 if (is_sRGB) { 399 return getsRGBComponentFromsRGB(pixel, 2); 400 } else if (is_LinearRGB) { 401 return getsRGBComponentFromLinearRGB(pixel, 2); 402 } 403 float rgb[] = getDefaultRGBComponents(pixel); 404 return (int) (rgb[2] * 255.0f + 0.5f); 405 } 406 407 414 final public int getAlpha(int pixel) { 415 if (!supportsAlpha) return 255; 416 int a = ((pixel & maskArray[3]) >>> maskOffsets[3]); 417 if (scaleFactors[3] != 1.0f) { 418 a = (int)(a * scaleFactors[3] + 0.5f); 419 } 420 return a; 421 } 422 423 436 final public int getRGB(int pixel) { 437 if (is_sRGB || is_LinearRGB) { 438 return (getAlpha(pixel) << 24) 439 | (getRed(pixel) << 16) 440 | (getGreen(pixel) << 8) 441 | (getBlue(pixel) << 0); 442 } 443 float rgb[] = getDefaultRGBComponents(pixel); 444 return (getAlpha(pixel) << 24) 445 | (((int) (rgb[0] * 255.0f + 0.5f)) << 16) 446 | (((int) (rgb[1] * 255.0f + 0.5f)) << 8) 447 | (((int) (rgb[2] * 255.0f + 0.5f)) << 0); 448 } 449 450 481 public int getRed(Object inData) { 482 int pixel=0; 483 switch (transferType) { 484 case DataBuffer.TYPE_BYTE: 485 byte bdata[] = (byte[])inData; 486 pixel = bdata[0] & 0xff; 487 break; 488 case DataBuffer.TYPE_USHORT: 489 short sdata[] = (short[])inData; 490 pixel = sdata[0] & 0xffff; 491 break; 492 case DataBuffer.TYPE_INT: 493 int idata[] = (int[])inData; 494 pixel = idata[0]; 495 break; 496 default: 497 throw new UnsupportedOperationException ("This method has not been "+ 498 "implemented for transferType " + transferType); 499 } 500 return getRed(pixel); 501 } 502 503 504 534 public int getGreen(Object inData) { 535 int pixel=0; 536 switch (transferType) { 537 case DataBuffer.TYPE_BYTE: 538 byte bdata[] = (byte[])inData; 539 pixel = bdata[0] & 0xff; 540 break; 541 case DataBuffer.TYPE_USHORT: 542 short sdata[] = (short[])inData; 543 pixel = sdata[0] & 0xffff; 544 break; 545 case DataBuffer.TYPE_INT: 546 int idata[] = (int[])inData; 547 pixel = idata[0]; 548 break; 549 default: 550 throw new UnsupportedOperationException ("This method has not been "+ 551 "implemented for transferType " + transferType); 552 } 553 return getGreen(pixel); 554 } 555 556 557 587 public int getBlue(Object inData) { 588 int pixel=0; 589 switch (transferType) { 590 case DataBuffer.TYPE_BYTE: 591 byte bdata[] = (byte[])inData; 592 pixel = bdata[0] & 0xff; 593 break; 594 case DataBuffer.TYPE_USHORT: 595 short sdata[] = (short[])inData; 596 pixel = sdata[0] & 0xffff; 597 break; 598 case DataBuffer.TYPE_INT: 599 int idata[] = (int[])inData; 600 pixel = idata[0]; 601 break; 602 default: 603 throw new UnsupportedOperationException ("This method has not been "+ 604 "implemented for transferType " + transferType); 605 } 606 return getBlue(pixel); 607 } 608 609 637 public int getAlpha(Object inData) { 638 int pixel=0; 639 switch (transferType) { 640 case DataBuffer.TYPE_BYTE: 641 byte bdata[] = (byte[])inData; 642 pixel = bdata[0] & 0xff; 643 break; 644 case DataBuffer.TYPE_USHORT: 645 short sdata[] = (short[])inData; 646 pixel = sdata[0] & 0xffff; 647 break; 648 case DataBuffer.TYPE_INT: 649 int idata[] = (int[])inData; 650 pixel = idata[0]; 651 break; 652 default: 653 throw new UnsupportedOperationException ("This method has not been "+ 654 "implemented for transferType " + transferType); 655 } 656 return getAlpha(pixel); 657 } 658 659 685 public int getRGB(Object inData) { 686 int pixel=0; 687 switch (transferType) { 688 case DataBuffer.TYPE_BYTE: 689 byte bdata[] = (byte[])inData; 690 pixel = bdata[0] & 0xff; 691 break; 692 case DataBuffer.TYPE_USHORT: 693 short sdata[] = (short[])inData; 694 pixel = sdata[0] & 0xffff; 695 break; 696 case DataBuffer.TYPE_INT: 697 int idata[] = (int[])inData; 698 pixel = idata[0]; 699 break; 700 default: 701 throw new UnsupportedOperationException ("This method has not been "+ 702 "implemented for transferType " + transferType); 703 } 704 return getRGB(pixel); 705 } 706 707 741 public Object getDataElements(int rgb, Object pixel) { 742 int intpixel[] = null; 745 if (transferType == DataBuffer.TYPE_INT && 746 pixel != null) { 747 intpixel = (int[])pixel; 748 intpixel[0] = 0; 749 } else { 750 intpixel = new int[1]; 751 } 752 753 ColorModel defaultCM = ColorModel.getRGBdefault(); 754 if (this == defaultCM || equals(defaultCM)) { 755 intpixel[0] = rgb; 756 return intpixel; 757 } 758 759 int red, grn, blu, alp; 760 red = (rgb>>16) & 0xff; 761 grn = (rgb>>8) & 0xff; 762 blu = rgb & 0xff; 763 if (is_sRGB || is_LinearRGB) { 764 int precision; 765 float factor; 766 if (is_LinearRGB) { 767 if (lRGBprecision == 8) { 768 red = fromsRGB8LUT8[red] & 0xff; 769 grn = fromsRGB8LUT8[grn] & 0xff; 770 blu = fromsRGB8LUT8[blu] & 0xff; 771 precision = 8; 772 factor = 1.0f / 255.0f; 773 } else { 774 red = fromsRGB8LUT16[red] & 0xffff; 775 grn = fromsRGB8LUT16[grn] & 0xffff; 776 blu = fromsRGB8LUT16[blu] & 0xffff; 777 precision = 16; 778 factor = 1.0f / 65535.0f; 779 } 780 } else { 781 precision = 8; 782 factor = 1.0f / 255.0f; 783 } 784 if (supportsAlpha) { 785 alp = (rgb>>24) & 0xff; 786 if (isAlphaPremultiplied) { 787 factor *= (alp * (1.0f / 255.0f)); 788 precision = -1; } 790 if (nBits[3] != 8) { 791 alp = (int) 792 ((alp * (1.0f / 255.0f) * ((1<<nBits[3]) - 1)) + 0.5f); 793 if (alp > ((1<<nBits[3]) - 1)) { 794 alp = (1<<nBits[3]) - 1; 796 } 797 } 798 intpixel[0] = alp << maskOffsets[3]; 799 } 800 if (nBits[0] != precision) { 801 red = (int) ((red * factor * ((1<<nBits[0]) - 1)) + 0.5f); 802 } 803 if (nBits[1] != precision) { 804 grn = (int) ((grn * factor * ((1<<nBits[1]) - 1)) + 0.5f); 805 } 806 if (nBits[2] != precision) { 807 blu = (int) ((blu * factor * ((1<<nBits[2]) - 1)) + 0.5f); 808 } 809 } else { 810 float[] norm = new float[3]; 812 float factor = 1.0f / 255.0f; 813 norm[0] = red * factor; 814 norm[1] = grn * factor; 815 norm[2] = blu * factor; 816 norm = colorSpace.fromRGB(norm); 817 if (supportsAlpha) { 818 alp = (rgb>>24) & 0xff; 819 if (isAlphaPremultiplied) { 820 factor *= alp; 821 for (int i = 0; i < 3; i++) { 822 norm[i] *= factor; 823 } 824 } 825 if (nBits[3] != 8) { 826 alp = (int) 827 ((alp * (1.0f / 255.0f) * ((1<<nBits[3]) - 1)) + 0.5f); 828 if (alp > ((1<<nBits[3]) - 1)) { 829 alp = (1<<nBits[3]) - 1; 831 } 832 } 833 intpixel[0] = alp << maskOffsets[3]; 834 } 835 red = (int) ((norm[0] * ((1<<nBits[0]) - 1)) + 0.5f); 836 &n
|