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 grn = (int) ((norm[1] * ((1<<nBits[1]) - 1)) + 0.5f); 837 blu = (int) ((norm[2] * ((1<<nBits[2]) - 1)) + 0.5f); 838 } 839 840 if (maxBits > 23) { 841 if (red > ((1<<nBits[0]) - 1)) { 846 red = (1<<nBits[0]) - 1; 847 } 848 if (grn > ((1<<nBits[1]) - 1)) { 849 grn = (1<<nBits[1]) - 1; 850 } 851 if (blu > ((1<<nBits[2]) - 1)) { 852 blu = (1<<nBits[2]) - 1; 853 } 854 } 855 856 intpixel[0] |= (red << maskOffsets[0]) | 857 (grn << maskOffsets[1]) | 858 (blu << maskOffsets[2]); 859 860 switch (transferType) { 861 case DataBuffer.TYPE_BYTE: { 862 byte bdata[]; 863 if (pixel == null) { 864 bdata = new byte[1]; 865 } else { 866 bdata = (byte[])pixel; 867 } 868 bdata[0] = (byte)(0xff&intpixel[0]); 869 return bdata; 870 } 871 case DataBuffer.TYPE_USHORT:{ 872 short sdata[]; 873 if (pixel == null) { 874 sdata = new short[1]; 875 } else { 876 sdata = (short[])pixel; 877 } 878 sdata[0] = (short)(intpixel[0]&0xffff); 879 return sdata; 880 } 881 case DataBuffer.TYPE_INT: 882 return intpixel; 883 } 884 throw new UnsupportedOperationException ("This method has not been "+ 885 "implemented for transferType " + transferType); 886 887 } 888 889 909 final public int[] getComponents(int pixel, int[] components, int offset) { 910 if (components == null) { 911 components = new int[offset+numComponents]; 912 } 913 914 for (int i=0; i < numComponents; i++) { 915 components[offset+i] = (pixel & maskArray[i]) >>> maskOffsets[i]; 916 } 917 918 return components; 919 } 920 921 960 final public int[] getComponents(Object pixel, int[] components, 961 int offset) { 962 int intpixel=0; 963 switch (transferType) { 964 case DataBuffer.TYPE_BYTE: 965 byte bdata[] = (byte[])pixel; 966 intpixel = bdata[0] & 0xff; 967 break; 968 case DataBuffer.TYPE_USHORT: 969 short sdata[] = (short[])pixel; 970 intpixel = sdata[0] & 0xffff; 971 break; 972 case DataBuffer.TYPE_INT: 973 int idata[] = (int[])pixel; 974 intpixel = idata[0]; 975 break; 976 default: 977 throw new UnsupportedOperationException ("This method has not been "+ 978 "implemented for transferType " + transferType); 979 } 980 return getComponents(intpixel, components, offset); 981 } 982 983 996 final public WritableRaster createCompatibleWritableRaster (int w, 997 int h) { 998 if ((w <= 0) || (h <= 0)) { 999 throw new IllegalArgumentException ("Width (" + w + ") and height (" + h + 1000 ") cannot be <= 0"); 1001 } 1002 int[] bandmasks; 1003 if (supportsAlpha) { 1004 bandmasks = new int[4]; 1005 bandmasks[3] = alpha_mask; 1006 } 1007 else { 1008 bandmasks = new int[3]; 1009 } 1010 bandmasks[0] = red_mask; 1011 bandmasks[1] = green_mask; 1012 bandmasks[2] = blue_mask; 1013 1014 if (pixel_bits > 16) { 1015 return Raster.createPackedRaster(DataBuffer.TYPE_INT, 1016 w,h,bandmasks,null); 1017 } 1018 else if (pixel_bits > 8) { 1019 return Raster.createPackedRaster(DataBuffer.TYPE_USHORT, 1020 w,h,bandmasks,null); 1021 } 1022 else { 1023 return Raster.createPackedRaster(DataBuffer.TYPE_BYTE, 1024 w,h,bandmasks,null); 1025 } 1026 } 1027 1028 1046 public int getDataElement(int[] components, int offset) { 1047 int pixel = 0; 1048 for (int i=0; i < numComponents; i++) { 1049 pixel |= ((components[offset+i]<<maskOffsets[i])&maskArray[i]); 1050 } 1051 return pixel; 1052 } 1053 1054 1096 public Object getDataElements(int[] components, int offset, Object obj) { 1097 int pixel = 0; 1098 for (int i=0; i < numComponents; i++) { 1099 pixel |= ((components[offset+i]<<maskOffsets[i])&maskArray[i]); 1100 } 1101 switch (transferType) { 1102 case DataBuffer.TYPE_BYTE: 1103 if (obj instanceof byte[]) { 1104 byte bdata[] = (byte[])obj; 1105 bdata[0] = (byte)(pixel&0xff); 1106 return bdata; 1107 } else { 1108 byte bdata[] = {(byte)(pixel&0xff)}; 1109 return bdata; 1110 } 1111 case DataBuffer.TYPE_USHORT: 1112 if (obj instanceof short[]) { 1113 short sdata[] = (short[])obj; 1114 sdata[0] = (short)(pixel&0xffff); 1115 return sdata; 1116 } else { 1117 short sdata[] = {(short)(pixel&0xffff)}; 1118 return sdata; 1119 } 1120 case DataBuffer.TYPE_INT: 1121 if (obj instanceof int[]) { 1122 int idata[] = (int[])obj; 1123 idata[0] = pixel; 1124 return idata; 1125 } else { 1126 int idata[] = {pixel}; 1127 return idata; 1128 } 1129 default: 1130 throw new ClassCastException ("This method has not been "+ 1131 "implemented for transferType " + transferType); 1132 } 1133 } 1134 1135 1159 final public ColorModel coerceData (WritableRaster raster, 1160 boolean isAlphaPremultiplied) 1161 { 1162 if (!supportsAlpha || 1163 this.isAlphaPremultiplied() == isAlphaPremultiplied) { 1164 return this; 1165 } 1166 1167 int w = raster.getWidth(); 1168 int h = raster.getHeight(); 1169 int aIdx = numColorComponents; 1170 float normAlpha; 1171 float alphaScale = 1.0f / ((float) ((1 << nBits[aIdx]) - 1)); 1172 1173 int rminX = raster.getMinX(); 1174 int rY = raster.getMinY(); 1175 int rX; 1176 int pixel[] = null; 1177 int zpixel[] = null; 1178 1179 if (isAlphaPremultiplied) { 1180 switch (transferType) { 1183 case DataBuffer.TYPE_BYTE: { 1184 for (int y = 0; y < h; y++, rY++) { 1185 rX = rminX; 1186 for (int x = 0; x < w; x++, rX++) { 1187 pixel = raster.getPixel(rX, rY, pixel); 1188 normAlpha = pixel[aIdx] * alphaScale; 1189 if (normAlpha != 0.f) { 1190 for (int c=0; c < aIdx; c++) { 1191 pixel[c] = (int) (pixel[c] * normAlpha + 1192 0.5f); 1193 } 1194 raster.setPixel(rX, rY, pixel); 1195 } else { 1196 if (zpixel == null) { 1197 zpixel = new int[numComponents]; 1198 java.util.Arrays.fill(zpixel, 0); 1199 } 1200 raster.setPixel(rX, rY, zpixel); 1201 } 1202 } 1203 } 1204 } 1205 break; 1206 case DataBuffer.TYPE_USHORT: { 1207 for (int y = 0; y < h; y++, rY++) { 1208 rX = rminX; 1209 for (int x = 0; x < w; x++, rX++) { 1210 pixel = raster.getPixel(rX, rY, pixel); 1211 normAlpha = pixel[aIdx] * alphaScale; 1212 if (normAlpha != 0.f) { 1213 for (int c=0; c < aIdx; c++) { 1214 pixel[c] = (int) (pixel[c] * normAlpha + 1215 0.5f); 1216 } 1217 raster.setPixel(rX, rY, pixel); 1218 } else { 1219 if (zpixel == null) { 1220 zpixel = new int[numComponents]; 1221 java.util.Arrays.fill(zpixel, 0); 1222 } 1223 raster.setPixel(rX, rY, zpixel); 1224 } 1225 } 1226 } 1227 } 1228 break; 1229 case DataBuffer.TYPE_INT: { 1230 for (int y = 0; y < h; y++, rY++) { 1231 rX = rminX; 1232 for (int x = 0; x < w; x++, rX++) { 1233 pixel = raster.getPixel(rX, rY, pixel); 1234 normAlpha = pixel[aIdx] * alphaScale; 1235 if (normAlpha != 0.f) { 1236 for (int c=0; c < aIdx; c++) { 1237 pixel[c] = (int) (pixel[c] * normAlpha + 1238 0.5f); 1239 } 1240 raster.setPixel(rX, rY, pixel); 1241 } else { 1242 if (zpixel == null) { 1243 zpixel = new int[numComponents]; 1244 java.util.Arrays.fill(zpixel, 0); 1245 } 1246 raster.setPixel(rX, rY, zpixel); 1247 } 1248 } 1249 } 1250 } 1251 break; 1252 default: 1253 throw new UnsupportedOperationException ("This method has not been "+ 1254 "implemented for transferType " + transferType); 1255 } 1256 } 1257 else { 1258 switch (transferType) { 1260 case DataBuffer.TYPE_BYTE: { 1261 for (int y = 0; y < h; y++, rY++) { 1262 rX = rminX; 1263 for (int x = 0; x < w; x++, rX++) { 1264 pixel = raster.getPixel(rX, rY, pixel); 1265 normAlpha = pixel[aIdx] * alphaScale; 1266 if (normAlpha != 0.0f) { 1267 float invAlpha = 1.0f / normAlpha; 1268 for (int c=0; c < aIdx; c++) { 1269 pixel[c] = (int) (pixel[c] * invAlpha + 1270 0.5f); 1271 } 1272 raster.setPixel(rX, rY, pixel); 1273 } 1274 } 1275 } 1276 } 1277 break; 1278 case DataBuffer.TYPE_USHORT: { 1279 for (int y = 0; y < h; y++, rY++) { 1280 rX = rminX; 1281 for (int x = 0; x < w; x++, rX++) { 1282 pixel = raster.getPixel(rX, rY, pixel); 1283 normAlpha = pixel[aIdx] * alphaScale; 1284 if (normAlpha != 0) { 1285 float invAlpha = 1.0f / normAlpha; 1286 for (int c=0; c < aIdx; c++) { 1287 pixel[c] = (int) (pixel[c] * invAlpha + 1288 0.5f); 1289 } 1290 raster.setPixel(rX, rY, pixel); 1291 } 1292 } 1293 } 1294 } 1295 break; 1296 case DataBuffer.TYPE_INT: { 1297 for (int y = 0; y < h; y++, rY++) { 1298 rX = rminX; 1299 for (int x = 0; x < w; x++, rX++) { 1300 pixel = raster.getPixel(rX, rY, pixel); 1301 normAlpha = pixel[aIdx] * alphaScale; 1302 if (normAlpha != 0) { 1303 float invAlpha = 1.0f / normAlpha; 1304 for (int c=0; c < aIdx; c++) { 1305 pixel[c] = (int) (pixel[c] * invAlpha + 1306 0.5f); 1307 } 1308 raster.setPixel(rX, rY, pixel); 1309 } 1310 } 1311 } 1312 } 1313 break; 1314 default: 1315 throw new UnsupportedOperationException ("This method has not been "+ 1316 "implemented for transferType " + transferType); 1317 } 1318 } 1319 1320 return new DirectColorModel (colorSpace, pixel_bits, maskArray[0], 1322 maskArray[1], maskArray[2], maskArray[3], 1323 isAlphaPremultiplied, 1324 transferType); 1325 1326 } 1327 1328 1336 public boolean isCompatibleRaster(Raster raster) { 1337 SampleModel sm = raster.getSampleModel(); 1338 SinglePixelPackedSampleModel spsm; 1339 if (sm instanceof SinglePixelPackedSampleModel ) { 1340 spsm = (SinglePixelPackedSampleModel ) sm; 1341 } 1342 else { 1343 return false; 1344 } 1345 if (spsm.getNumBands() != getNumComponents()) { 1346 return false; 1347 } 1348 1349 int[] bitMasks = spsm.getBitMasks(); 1350 for (int i=0; i<numComponents; i++) { 1351 if (bitMasks[i] != maskArray[i]) { 1352 return false; 1353 } 1354 } 1355 1356 return (raster.getTransferType() == transferType); 1357 } 1358 1359 private void setFields() { 1360 red_mask = maskArray[0]; 1363 red_offset = maskOffsets[0]; 1364 green_mask = maskArray[1]; 1365 green_offset = maskOffsets[1]; 1366 blue_mask = maskArray[2]; 1367 blue_offset = maskOffsets[2]; 1368 if (nBits[0] < 8) { 1369 red_scale = (1 << nBits[0]) - 1; 1370 } 1371 if (nBits[1] < 8) { 1372 green_scale = (1 << nBits[1]) - 1; 1373 } 1374 if (nBits[2] < 8) { 1375 blue_scale = (1 << nBits[2]) - 1; 1376 } 1377 if (supportsAlpha) { 1378 alpha_mask = maskArray[3]; 1379 alpha_offset = maskOffsets[3]; 1380 if (nBits[3] < 8) { 1381 alpha_scale = (1 << nBits[3]) - 1; 1382 } 1383 } 1384 } 1385 1386 1392 public String toString() { 1393 return new String ("DirectColorModel: rmask=" 1394 +Integer.toHexString(red_mask)+" gmask=" 1395 +Integer.toHexString(green_mask)+" bmask=" 1396 +Integer.toHexString(blue_mask)+" amask=" 1397 +Integer.toHexString(alpha_mask)); 1398 } 1399} 1400 1401 | Popular Tags |