1 7 8 package java.awt.image; 9 10 import java.awt.color.ColorSpace ; 11 import java.awt.color.ICC_ColorSpace ; 12 13 160 public class ComponentColorModel extends ColorModel { 161 162 168 private boolean signed; private boolean is_sRGB_stdScale; 171 private boolean is_LinearRGB_stdScale; 172 private boolean is_LinearGray_stdScale; 173 private boolean is_ICCGray_stdScale; 174 private byte[] tosRGB8LUT; 175 private byte[] fromsRGB8LUT8; 176 private short[] fromsRGB8LUT16; 177 private byte[] fromLinearGray16ToOtherGray8LUT; 178 private short[] fromLinearGray16ToOtherGray16LUT; 179 private boolean needScaleInit; 180 private boolean noUnnorm; 181 private boolean nonStdScale; 182 private float[] min; 183 private float[] diffMinMax; 184 private float[] compOffset; 185 private float[] compScale; 186 187 250 public ComponentColorModel (ColorSpace colorSpace, 251 int[] bits, 252 boolean hasAlpha, 253 boolean isAlphaPremultiplied, 254 int transparency, 255 int transferType) { 256 super (bitsHelper(transferType, colorSpace, hasAlpha), 257 bitsArrayHelper(bits, transferType, colorSpace, hasAlpha), 258 colorSpace, hasAlpha, isAlphaPremultiplied, transparency, 259 transferType); 260 switch(transferType) { 261 case DataBuffer.TYPE_BYTE: 262 case DataBuffer.TYPE_USHORT: 263 case DataBuffer.TYPE_INT: 264 signed = false; 265 needScaleInit = true; 266 break; 267 case DataBuffer.TYPE_SHORT: 268 signed = true; 269 needScaleInit = true; 270 break; 271 case DataBuffer.TYPE_FLOAT: 272 case DataBuffer.TYPE_DOUBLE: 273 signed = true; 274 needScaleInit = false; 275 noUnnorm = true; 276 nonStdScale = false; 277 break; 278 default: 279 throw new IllegalArgumentException ("This constructor is not "+ 280 "compatible with transferType " + transferType); 281 } 282 setupLUTs(); 283 } 284 285 329 public ComponentColorModel (ColorSpace colorSpace, 330 boolean hasAlpha, 331 boolean isAlphaPremultiplied, 332 int transparency, 333 int transferType) { 334 this(colorSpace, null, hasAlpha, isAlphaPremultiplied, 335 transparency, transferType); 336 } 337 338 private static int bitsHelper(int transferType, 339 ColorSpace colorSpace, 340 boolean hasAlpha) { 341 int numBits = DataBuffer.getDataTypeSize(transferType); 342 int numComponents = colorSpace.getNumComponents(); 343 if (hasAlpha) { 344 ++numComponents; 345 } 346 return numBits * numComponents; 347 } 348 349 private static int[] bitsArrayHelper(int[] origBits, 350 int transferType, 351 ColorSpace colorSpace, 352 boolean hasAlpha) { 353 switch(transferType) { 354 case DataBuffer.TYPE_BYTE: 355 case DataBuffer.TYPE_USHORT: 356 case DataBuffer.TYPE_INT: 357 if (origBits != null) { 358 return origBits; 359 } 360 break; 361 default: 362 break; 363 } 364 int numBits = DataBuffer.getDataTypeSize(transferType); 365 int numComponents = colorSpace.getNumComponents(); 366 if (hasAlpha) { 367 ++numComponents; 368 } 369 int[] bits = new int[numComponents]; 370 for (int i = 0; i < numComponents; i++) { 371 bits[i] = numBits; 372 } 373 return bits; 374 } 375 376 private void setupLUTs() { 377 if (is_sRGB) { 395 is_sRGB_stdScale = true; 396 nonStdScale = false; 397 } else if (ColorModel.isLinearRGBspace(colorSpace)) { 398 is_LinearRGB_stdScale = true; 402 nonStdScale = false; 403 if (transferType == DataBuffer.TYPE_BYTE) { 404 tosRGB8LUT = ColorModel.getLinearRGB8TosRGB8LUT(); 405 fromsRGB8LUT8 = ColorModel.getsRGB8ToLinearRGB8LUT(); 406 } else { 407 tosRGB8LUT = ColorModel.getLinearRGB16TosRGB8LUT(); 408 fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT(); 409 } 410 } else if ((colorSpaceType == ColorSpace.TYPE_GRAY) && 411 (colorSpace instanceof ICC_ColorSpace ) && 412 (colorSpace.getMinValue(0) == 0.0f) && 413 (colorSpace.getMaxValue(0) == 1.0f)) { 414 ICC_ColorSpace ics = (ICC_ColorSpace ) colorSpace; 418 is_ICCGray_stdScale = true; 419 nonStdScale = false; 420 fromsRGB8LUT16 = ColorModel.getsRGB8ToLinearRGB16LUT(); 421 if (ColorModel.isLinearGRAYspace(ics)) { 422 is_LinearGray_stdScale = true; 423 if (transferType == DataBuffer.TYPE_BYTE) { 424 tosRGB8LUT = ColorModel.getGray8TosRGB8LUT(ics); 425 } else { 426 tosRGB8LUT = ColorModel.getGray16TosRGB8LUT(ics); 427 } 428 } else { 429 if (transferType == DataBuffer.TYPE_BYTE) { 430 tosRGB8LUT = ColorModel.getGray8TosRGB8LUT(ics); 431 fromLinearGray16ToOtherGray8LUT = 432 ColorModel.getLinearGray16ToOtherGray8LUT(ics); 433 } else { 434 tosRGB8LUT = ColorModel.getGray16TosRGB8LUT(ics); 435 fromLinearGray16ToOtherGray16LUT = 436 ColorModel.getLinearGray16ToOtherGray16LUT(ics); 437 } 438 } 439 } else if (needScaleInit) { 440 nonStdScale = false; 445 for (int i = 0; i < numColorComponents; i++) { 446 if ((colorSpace.getMinValue(i) != 0.0f) || 447 (colorSpace.getMaxValue(i) != 1.0f)) { 448 nonStdScale = true; 449 break; 450 } 451 } 452 if (nonStdScale) { 453 min = new float[numColorComponents]; 454 diffMinMax = new float[numColorComponents]; 455 for (int i = 0; i < numColorComponents; i++) { 456 min[i] = colorSpace.getMinValue(i); 457 diffMinMax[i] = colorSpace.getMaxValue(i) - min[i]; 458 } 459 } 460 } 461 } 462 463 private void initScale() { 464 needScaleInit = false; if (nonStdScale || signed) { 485 noUnnorm = true; 494 } else { 495 noUnnorm = false; 496 } 497 float[] lowVal, highVal; 498 switch (transferType) { 499 case DataBuffer.TYPE_BYTE: 500 { 501 byte[] bpixel = new byte[numComponents]; 502 for (int i = 0; i < numColorComponents; i++) { 503 bpixel[i] = 0; 504 } 505 if (supportsAlpha) { 506 bpixel[numColorComponents] = 507 (byte) ((1 << nBits[numColorComponents]) - 1); 508 } 509 lowVal = getNormalizedComponents(bpixel, null, 0); 510 for (int i = 0; i < numColorComponents; i++) { 511 bpixel[i] = (byte) ((1 << nBits[i]) - 1); 512 } 513 highVal = getNormalizedComponents(bpixel, null, 0); 514 } 515 break; 516 case DataBuffer.TYPE_USHORT: 517 { 518 short[] uspixel = new short[numComponents]; 519 for (int i = 0; i < numColorComponents; i++) { 520 uspixel[i] = 0; 521 } 522 if (supportsAlpha) { 523 uspixel[numColorComponents] = 524 (short) ((1 << nBits[numColorComponents]) - 1); 525 } 526 lowVal = getNormalizedComponents(uspixel, null, 0); 527 for (int i = 0; i < numColorComponents; i++) { 528 uspixel[i] = (short) ((1 << nBits[i]) - 1); 529 } 530 highVal = getNormalizedComponents(uspixel, null, 0); 531 } 532 break; 533 case DataBuffer.TYPE_INT: 534 { 535 int[] ipixel = new int[numComponents]; 536 for (int i = 0; i < numColorComponents; i++) { 537 ipixel[i] = 0; 538 } 539 if (supportsAlpha) { 540 ipixel[numColorComponents] = 541 ((1 << nBits[numColorComponents]) - 1); 542 } 543 lowVal = getNormalizedComponents(ipixel, null, 0); 544 for (int i = 0; i < numColorComponents; i++) { 545 ipixel[i] = ((1 << nBits[i]) - 1); 546 } 547 highVal = getNormalizedComponents(ipixel, null, 0); 548 } 549 break; 550 case DataBuffer.TYPE_SHORT: 551 { 552 short[] spixel = new short[numComponents]; 553 for (int i = 0; i < numColorComponents; i++) { 554 spixel[i] = 0; 555 } 556 if (supportsAlpha) { 557 spixel[numColorComponents] = 32767; 558 } 559 lowVal = getNormalizedComponents(spixel, null, 0); 560 for (int i = 0; i < numColorComponents; i++) { 561 spixel[i] = 32767; 562 } 563 highVal = getNormalizedComponents(spixel, null, 0); 564 } 565 break; 566 default: 567 lowVal = highVal = null; break; 569 } 570 nonStdScale = false; 571 for (int i = 0; i < numColorComponents; i++) { 572 if ((lowVal[i] != 0.0f) || (highVal[i] != 1.0f)) { 573 nonStdScale = true; 574 break; 575 } 576 } 577 if (nonStdScale) { 578 noUnnorm = true; 579 is_sRGB_stdScale = false; 580 is_LinearRGB_stdScale = false; 581 is_LinearGray_stdScale = false; 582 is_ICCGray_stdScale = false; 583 compOffset = new float[numColorComponents]; 584 compScale = new float[numColorComponents]; 585 for (int i = 0; i < numColorComponents; i++) { 586 compOffset[i] = lowVal[i]; 587 compScale[i] = 1.0f / (highVal[i] - lowVal[i]); 588 } 589 } 590 } 591 592 private int getRGBComponent(int pixel, int idx) { 593 if (numComponents > 1) { 594 throw new 595 IllegalArgumentException ("More than one component per pixel"); 596 } 597 if (signed) { 598 throw new 599 IllegalArgumentException ("Component value is signed"); 600 } 601 if (needScaleInit) { 602 initScale(); 603 } 604 606 Object opixel = null; 608 switch (transferType) { 609 case DataBuffer.TYPE_BYTE: 610 { 611 byte[] bpixel = { (byte) pixel }; 612 opixel = bpixel; 613 } 614 break; 615 case DataBuffer.TYPE_USHORT: 616 { 617 short[] spixel = { (short) pixel }; 618 opixel = spixel; 619 } 620 break; 621 case DataBuffer.TYPE_INT: 622 { 623 int[] ipixel = { pixel }; 624 opixel = ipixel; 625 } 626 break; 627 } 628 float[] norm = getNormalizedComponents(opixel, null, 0); 629 float[] rgb = colorSpace.toRGB(norm); 630 631 return (int) (rgb[idx] * 255.0f + 0.5f); 632 } 633 634 652 public int getRed(int pixel) { 653 return getRGBComponent(pixel, 0); 654 } 655 656 674 public int getGreen(int pixel) { 675 return getRGBComponent(pixel, 1); 676 } 677 678 696 public int getBlue(int pixel) { 697 return getRGBComponent(pixel, 2); 698 } 699 700 713 public int getAlpha(int pixel) { 714 if (supportsAlpha == false) { 715 return 255; 716 } 717 if (numComponents > 1) { 718 throw new 719 IllegalArgumentException ("More than one component per pixel"); 720 } 721 if (signed) { 722 throw new 723 IllegalArgumentException ("Component value is signed"); 724 } 725 726 return (int) ((((float) pixel) / ((1<<nBits[0])-1)) * 255.0f + 0.5f); 727 } 728 729 745 public int getRGB(int pixel) { 746 if (numComponents > 1) { 747 throw new 748 IllegalArgumentException ("More than one component per pixel"); 749 } 750 if (signed) { 751 throw new 752 IllegalArgumentException ("Component value is signed"); 753 } 754 755 return (getAlpha(pixel) << 24) 756 | (getRed(pixel) << 16) 757 | (getGreen(pixel) << 8) 758 | (getBlue(pixel) << 0); 759 } 760 761 private int extractComponent(Object inData, int idx, int precision) { 762 768 777 boolean needAlpha = (supportsAlpha && isAlphaPremultiplied); 778 int alp = 0; 779 int comp; 780 int mask = (1 << nBits[idx]) - 1; 781 782 switch (transferType) { 783 case DataBuffer.TYPE_SHORT: { 786 short sdata[] = (short[]) inData; 787 float scalefactor = (float) ((1 << precision) - 1); 788 if (needAlpha) { 789 short s = sdata[numColorComponents]; 790 if (s != (short) 0) { 791 return (int) ((((float) sdata[idx]) / 792 ((float) s)) * scalefactor + 0.5f); 793 } else { 794 return 0; 795 } 796 } else { 797 return (int) ((sdata[idx] / 32767.0f) * scalefactor + 0.5f); 798 } 799 } 800 case DataBuffer.TYPE_FLOAT: { 801 float fdata[] = (float[]) inData; 802 float scalefactor = (float) ((1 << precision) - 1); 803 if (needAlpha) { 804 float f = fdata[numColorComponents]; 805 if (f != 0.0f) { 806 return (int) (((fdata[idx] / f) * scalefactor) + 0.5f); 807 } else { 808 return 0; 809 } 810 } else { 811 return (int) (fdata[idx] * scalefactor + 0.5f); 812 } 813 } 814 case DataBuffer.TYPE_DOUBLE: { 815 double ddata[] = (double[]) inData; 816 double scalefactor = (double) ((1 << precision) - 1); 817 if (needAlpha) { 818 double d = ddata[numColorComponents]; 819 if (d != 0.0) { 820 return (int) (((ddata[idx] / d) * scalefactor) + 0.5); 821 } else { 822 return 0; 823 } 824 } else { 825 return (int) (ddata[idx] * scalefactor + 0.5); 826 } 827 } 828 case DataBuffer.TYPE_BYTE: 829 byte bdata[] = (byte[])inData; 830 comp = bdata[idx] & mask; 831 precision = 8; 832 if (needAlpha) { 833 alp = bdata[numColorComponents] & mask; 834 } 835 break; 836 case DataBuffer.TYPE_USHORT: 837 short usdata[] = (short[])inData; 838 comp = usdata[idx] & mask; 839 if (needAlpha) { 840 alp = usdata[numColorComponents] & mask; 841 } 842 break; 843 case DataBuffer.TYPE_INT: 844 int idata[] = (int[])inData; 845 comp = idata[idx]; 846 if (needAlpha) { 847 alp = idata[numColorComponents]; 848 } 849 break; 850 default: 851 throw new 852 UnsupportedOperationException ("This method has not "+ 853 "been implemented for transferType " + transferType); 854 } 855 if (needAlpha) { 856 if (alp != 0) { 857 float scalefactor = (float) ((1 << precision) - 1); 858 float fcomp = ((float) comp) / ((float)mask); 859 float invalp = ((float) ((1<<nBits[numColorComponents]) - 1)) / 860 ((float) alp); 861 return (int) (fcomp * invalp * scalefactor + 0.5f); 862 } else { 863 return 0; 864 } 865 } else { 866 if (nBits[idx] != precision) { 867 float scalefactor = (float) ((1 << precision) - 1); 868 float fcomp = ((float) comp) / ((float)mask); 869 return (int) (fcomp * scalefactor + 0.5f); 870 } 871 return comp; 872 } 873 } 874 875 private int getRGBComponent(Object inData, int idx) { 876 if (needScaleInit) { 877 initScale(); 878 } 879 if (is_sRGB_stdScale) { 880 return extractComponent(inData, idx, 8); 881 } else if (is_LinearRGB_stdScale) { 882 int lutidx = extractComponent(inData, idx, 16); 883 return tosRGB8LUT[lutidx] & 0xff; 884 } else if (is_ICCGray_stdScale) { 885 int lutidx = extractComponent(inData, 0, 16); 886 return tosRGB8LUT[lutidx] & 0xff; 887 } 888 889 float[] norm = getNormalizedComponents(inData, null, 0); 891 float[] rgb = colorSpace.toRGB(norm); 893 return (int) (rgb[idx] * 255.0f + 0.5f); 894 } 895 896 926 public int getRed(Object inData) { 927 return getRGBComponent(inData, 0); 928 } 929 930 931 961 public int getGreen(Object inData) { 962 return getRGBComponent(inData, 1); 963 } 964 965 966 996 public int getBlue(Object inData) { 997 return getRGBComponent(inData, 2); 998 } 999 1000 1027 public int getAlpha(Object inData) { 1028 if (supportsAlpha == false) { 1029 return 255; 1030 } 1031 1032 int alpha = 0; 1033 int aIdx = numColorComponents; 1034 int mask = (1 << nBits[aIdx]) - 1; 1035 1036 switch (transferType) { 1037 case DataBuffer.TYPE_SHORT: 1038 short sdata[] = (short[])inData; 1039 alpha = (int) ((sdata[aIdx] / 32767.0f) * 255.0f + 0.5f); 1040 return alpha; 1041 case DataBuffer.TYPE_FLOAT: 1042 float fdata[] = (float[])inData; 1043 alpha = (int) (fdata[aIdx] * 255.0f + 0.5f); 1044 return alpha; 1045 case DataBuffer.TYPE_DOUBLE: 1046 double ddata[] = (double[])inData; 1047 alpha = (int) (ddata[aIdx] * 255.0 + 0.5); 1048 return alpha; 1049 case DataBuffer.TYPE_BYTE: 1050 byte bdata[] = (byte[])inData; 1051 alpha = bdata[aIdx] & mask; 1052 break; 1053 case DataBuffer.TYPE_USHORT: 1054 short usdata[] = (short[])inData; 1055 alpha = usdata[aIdx] & mask; 1056 break; 1057 case DataBuffer.TYPE_INT: 1058 int idata[] = (int[])inData; 1059 alpha = idata[aIdx]; 1060 break; 1061 default: 1062 throw new 1063 UnsupportedOperationException ("This method has not "+ 1064 "been implemented for transferType " + transferType); 1065 } 1066 1067 if (nBits[aIdx] == 8) { 1068 return alpha; 1069 } else { 1070 return (int) 1071 ((((float) alpha) / ((float) ((1 << nBits[aIdx]) - 1))) * 1072 255.0f + 0.5f); 1073 } 1074 } 1075 1076 1108 public int getRGB(Object inData) { 1109 if (needScaleInit) { 1110 initScale(); 1111 } 1112 if (is_sRGB_stdScale || is_LinearRGB_stdScale) { 1113 return (getAlpha(inData) << 24) 1114 | (getRed(inData) << 16) 1115 | (getGreen(inData) << 8) 1116 | (getBlue(inData)); 1117 } else if (colorSpaceType == ColorSpace.TYPE_GRAY) { 1118 int gray = getRed(inData); return (getAlpha(inData) << 24) 1121 | (gray << 16) 1122 | (gray << 8) 1123 | gray; 1124 } 1125 float[] norm = getNormalizedComponents(inData, null, 0); 1126 float[] rgb = colorSpace.toRGB(norm); 1128 return (getAlpha(inData) << 24) 1129 | (((int) (rgb[0] * 255.0f + 0.5f)) << 16) 1130 | (((int) (rgb[1] * 255.0f + 0.5f)) << 8) 1131 | (((int) (rgb[2] * 255.0f + 0.5f)) << 0); 1132 } 1133 1134 1168 public Object getDataElements(int rgb, Object pixel) { 1169 1171 int red, grn, blu, alp; 1172 red = (rgb>>16) & 0xff; 1173 grn = (rgb>>8) & 0xff; 1174 blu = rgb & 0xff; 1175 1176 if (needScaleInit) { 1177 initScale(); 1178 } 1179 if (signed) { 1180 1182 switch(transferType) { 1183 case DataBuffer.TYPE_SHORT: 1184 { 1185 short sdata[]; 1186 if (pixel == null) { 1187 sdata = new short[numComponents]; 1188 } else { 1189 sdata = (short[])pixel; 1190 } 1191 float factor; 1192 if (is_sRGB_stdScale || is_LinearRGB_stdScale) { 1193 factor = 32767.0f / 255.0f; 1194 if (is_LinearRGB_stdScale) { 1195 red = fromsRGB8LUT16[red] & 0xffff; 1196 grn = fromsRGB8LUT16[grn] & 0xffff; 1197 blu = fromsRGB8LUT16[blu] & 0xffff; 1198 factor = 32767.0f / 65535.0f; 1199 } 1200 if (supportsAlpha) { 1201 alp = (rgb>>24) & 0xff; 1202 sdata[3] = 1203 (short) (alp * (32767.0f / 255.0f) + 0.5f); 1204 if (isAlphaPremultiplied) { 1205 factor = alp * factor * (1.0f / 255.0f); 1206 } 1207 } 1208 sdata[0] = (short) (red * factor + 0.5f); 1209 sdata[1] = (short) (grn * factor + 0.5f); 1210 sdata[2] = (short) (blu * factor + 0.5f); 1211 } else if (is_LinearGray_stdScale) { 1212 red = fromsRGB8LUT16[red] & 0xffff; 1213 grn = fromsRGB8LUT16[grn] & 0xffff; 1214 blu = fromsRGB8LUT16[blu] & 0xffff; 1215 float gray = ((0.2125f * red) + 1216 (0.7154f * grn) + 1217 (0.0721f * blu)) / 65535.0f; 1218 factor = 32767.0f; 1219 if (supportsAlpha) { 1220 alp = (rgb>>24) & 0xff; 1221 sdata[1] = 1222 (short) (alp * (32767.0f / 255.0f) + 0.5f); 1223 if (isAlphaPremultiplied) { 1224 factor = alp * factor * (1.0f / 255.0f); 1225 } 1226 } 1227 sdata[0] = (short) (gray * factor + 0.5f); 1228 } else if (is_ICCGray_stdScale) { 1229 red = fromsRGB8LUT16[red] & 0xffff; 1230 grn = fromsRGB8LUT16[grn] & 0xffff; 1231 blu = fromsRGB8LUT16[blu] & 0xffff; 1232 int gray = (int) ((0.2125f * red) + 1233 (0.7154f * grn) + 1234 (0.0721f * blu) + 0.5f); 1235 gray = fromLinearGray16ToOtherGray16LUT[gray] & 0xffff; 1236 factor = 32767.0f / 65535.0f; 1237 if (supportsAlpha) { 1238 alp = (rgb>>24) & 0xff; 1239 sdata[1] = 1240 (short) (alp * (32767.0f / 255.0f) + 0.5f); 1241 if (isAlphaPremultiplied) { 1242 factor = alp * factor * (1.0f / 255.0f); 1243 } 1244 } 1245 sdata[0] = (short) (gray * factor + 0.5f); 1246 } else { 1247 factor = 1.0f / 255.0f; 1248 float norm[] = new float[3]; 1249 norm[0] = red * factor; 1250 norm[1] = grn * factor; 1251 norm[2] = blu * factor; 1252 norm = colorSpace.fromRGB(norm); 1253 if (nonStdScale) { 1254 for (int i = 0; i < numColorComponents; i++) { 1255 norm[i] = (norm[i] - compOffset[i]) * 1256 compScale[i]; 1257 if (norm[i] < 0.0f) { 1260 norm[i] = 0.0f; 1261 } 1262 if (norm[i] > 1.0f) { 1263 norm[i] = 1.0f; 1264 } 1265 } 1266 } 1267 factor = 32767.0f; 1268 if (supportsAlpha) { 1269 alp = (rgb>>24) & 0xff; 1270 sdata[numColorComponents] = 1271 (short) (alp * (32767.0f / 255.0f) + 0.5f); 1272 if (isAlphaPremultiplied) { 1273 factor *= alp * (1.0f / 255.0f); 1274 } 1275 } 1276 for (int i = 0; i < numColorComponents; i++) { 1277 sdata[i] = (short) (norm[i] * factor + 0.5f); 1278 } 1279 } 1280 return sdata; 1281 } 1282 case DataBuffer.TYPE_FLOAT: 1283 { 1284 float fdata[]; 1285 if (pixel == null) { 1286 fdata = new float[numComponents]; 1287 } else { 1288 fdata = (float[])pixel; 1289 } 1290 float factor; 1291 if (is_sRGB_stdScale || is_LinearRGB_stdScale) { 1292 if (is_LinearRGB_stdScale) { 1293 red = fromsRGB8LUT16[red] & 0xffff; 1294 grn = fromsRGB8LUT16[grn] & 0xffff; 1295 blu = fromsRGB8LUT16[blu] & 0xffff; 1296 factor = 1.0f / 65535.0f; 1297 } else { 1298 factor = 1.0f / 255.0f; 1299 } 1300 if (supportsAlpha) { 1301 alp = (rgb>>24) & 0xff; 1302 fdata[3] = alp * (1.0f / 255.0f); 1303 if (isAlphaPremultiplied) { 1304 factor *= fdata[3]; 1305 } 1306 } 1307 fdata[0] = red * factor; 1308 fdata[1] = grn * factor; 1309 fdata[2] = blu * factor; 1310 } else if (is_LinearGray_stdScale) { 1311 red = fromsRGB8LUT16[red] & 0xffff; 1312 grn = fromsRGB8LUT16[grn] & 0xffff; 1313 blu = fromsRGB8LUT16[blu] & 0xffff; 1314 fdata[0] = ((0.2125f * red) + 1315 (0.7154f * grn) + 1316 (0.0721f * blu)) / 65535.0f; 1317 if (supportsAlpha) { 1318 alp = (rgb>>24) & 0xff; 1319 fdata[1] = alp * (1.0f / 255.0f); 1320 if (isAlphaPremultiplied) { 1321 fdata[0] *= fdata[1]; 1322 } 1323 } 1324 } else if (is_ICCGray_stdScale) { 1325 red = fromsRGB8LUT16[red] & 0xffff; 1326 grn = fromsRGB8LUT16[grn] & 0xffff; 1327 blu = fromsRGB8LUT16[blu] & 0xffff; 1328 int gray = (int) ((0.2125f * red) + 1329 (0.7154f * grn) + 1330 (0.0721f * blu) + 0.5f); 1331 fdata[0] = (fromLinearGray16ToOtherGray16LUT[gray] & 1332 0xffff) / 65535.0f; 1333 if (supportsAlpha) { 1334 alp = (rgb>>24) & 0xff; 1335 fdata[1] = alp * (1.0f / 255.0f); 1336 if (isAlphaPremultiplied) { 1337 fdata[0] *= fdata[1]; 1338 } 1339 } 1340 } else { 1341 float norm[] = new float[3]; 1342 factor = 1.0f / 255.0f; 1343 norm[0] = red * factor; 1344 norm[1] = grn * factor; 1345 norm[2] = blu * factor; 1346 norm = colorSpace.fromRGB(norm); 1347 if (supportsAlpha) { 1348 alp = (rgb>>24) & 0xff; 1349 fdata[numColorComponents] = alp * factor; 1350 if (isAlphaPremultiplied) { 1351 factor *= alp; 1352 for (int i = 0; i < numColorComponents; i++) { 1353 norm[i] *= factor; 1354 } 1355 } 1356 } 1357 for (int i = 0; i < numColorComponents; i++) { 1358 fdata[i] = norm[i]; 1359 } 1360 } 1361 return fdata; 1362 } 1363 case DataBuffer.TYPE_DOUBLE: 1364 { 1365 double ddata[]; 1366 if (pixel == null) { 1367 ddata = new double[numComponents]; 1368 } else { 1369 ddata = (double[])pixel; 1370 } 1371 if (is_sRGB_stdScale || is_LinearRGB_stdScale) { 1372 double factor; 1373 if (is_LinearRGB_stdScale) { 1374 red = fromsRGB8LUT16[red] & 0xffff; 1375 grn = fromsRGB8LUT16[grn] & 0xffff; 1376 blu = fromsRGB8LUT16[blu] & 0xffff; 1377 factor = 1.0 / 65535.0; 1378 } else { 1379 factor = 1.0 / 255.0; 1380 } 1381 if (supportsAlpha) { 1382 alp = (rgb>>24) & 0xff; 1383 ddata[3] = alp * (1.0 / 255.0); 1384 if (isAlphaPremultiplied) { 1385 factor *= ddata[3]; 1386 } 1387 } 1388 ddata[0] = red * factor; 1389 ddata[1] = grn * factor; 1390 ddata[2] = blu * factor; 1391 } else if (is_LinearGray_stdScale) { 1392 red = fromsRGB8LUT16[red] & 0xffff; 1393 grn = fromsRGB8LUT16[grn] & 0xffff; 1394 blu = fromsRGB8LUT16[blu] & 0xffff; 1395 ddata[0] = ((0.2125 * red) + 1396 (0.7154 * grn) + 1397 (0.0721 * blu)) / 65535.0; 1398 if (supportsAlpha) { 1399 alp = (rgb>>24) & 0xff; 1400 ddata[1] = alp * (1.0 / 255.0); 1401 if (isAlphaPremultiplied) { 1402 ddata[0] *= ddata[1]; 1403 } 1404 } 1405 } else if (is_ICCGray_stdScale) { 1406 red = fromsRGB8LUT16[red] & 0xffff; 1407 grn = fromsRGB8LUT16[grn] & 0xffff; 1408 blu = fromsRGB8LUT16[blu] & 0xffff; 1409 int gray = (int) ((0.2125f * red) + 1410 (0.7154f * grn) + 1411 (0.0721f * blu) + 0.5f); 1412 ddata[0] = (fromLinearGray16ToOtherGray16LUT[gray] & 1413 0xffff) / 65535.0; 1414 if (supportsAlpha) { 1415 alp = (rgb>>24) & 0xff; 1416 ddata[1] = alp * (1.0 / 255.0); 1417 if (isAlphaPremultiplied) { 1418 ddata[0] *= ddata[1]; 1419 } 1420 } 1421 } else { 1422 float factor = 1.0f / 255.0f; 1423 float norm[] = new float[3]; 1424 norm[0] = red * factor; 1425 norm[1] = grn * factor; 1426 norm[2] = blu * factor; 1427 norm = colorSpace.fromRGB(norm); 1428 if (supportsAlpha) { 1429 alp = (rgb>>24) & 0xff; 1430 ddata[numColorComponents] = alp * (1.0 / 255.0); 1431 if (isAlphaPremultiplied) { 1432 factor *= alp; 1433 for (int i = 0; i < numColorComponents; i++) { 1434 norm[i] *= factor; 1435 } 1436 } 1437 } 1438 for (int i = 0; i < numColorComponents; i++) { 1439 ddata[i] = norm[i]; 1440 } 1441 } 1442 return ddata; 1443 } 1444 } 1445 } 1446 1447 int intpixel[]; 1451 if (transferType == DataBuffer.TYPE_INT && 1452 pixel != null) { 1453 intpixel = (int[])pixel; 1454 } else { 1455 intpixel = new int[numComponents]; 1456 } 1457 1458 if (is_sRGB_stdScale || is_LinearRGB_stdScale) { 1459 int precision; 1460 float factor; 1461 if (is_LinearRGB_stdScale) { 1462 if (transferType == DataBuffer.TYPE_BYTE) { 1463 red = fromsRGB8LUT8[red] & 0xff; 1464 grn = fromsRGB8LUT8[grn] & 0xff; 1465 blu = fromsRGB8LUT8[blu] & 0xff; 1466 precision = 8; 1467 factor = 1.0f / 255.0f; 1468 } else { 1469 red = fromsRGB8LUT16[red] & 0xffff; 1470 grn = fromsRGB8LUT16[grn] & 0xffff; 1471 blu = fromsRGB8LUT16[blu] & 0xffff; 1472 precision = 16; 1473 factor = 1.0f / 65535.0f; 1474 } 1475 } else { 1476 precision = 8; 1477 factor = 1.0f / 255.0f; 1478 } 1479 if (supportsAlpha) { 1480 alp = (rgb>>24)&0xff; 1481 if (nBits[3] == 8) { 1482 intpixel[3] = alp; 1483 } 1484 else { 1485 intpixel[3] = (int) 1486 (alp * (1.0f / 255.0f) * ((1<<nBits[3]) - 1) + 0.5f); 1487 } 1488 if (isAlphaPremultiplied) { 1489 factor *= (alp * (1.0f / 255.0f)); 1490 precision = -1; } 1492 } 1493 if (nBits[0] == precision) { 1494 intpixel[0] = red; 1495 } 1496 else { 1497 intpixel[0] = (int) (red * factor * ((1<<nBits[0]) - 1) + 0.5f); 1498 } 1499 if (nBits[1] == precision) { 1500 intpixel[1] = (int)(grn); 1501 } 1502 else { 1503 intpixel[1] = (int) (grn * factor * ((1<<nBits[1]) - 1) + 0.5f); 1504 } 1505 if (nBits[2] == precision) { 1506 intpixel[2] = (int)(blu); 1507 } 1508 else { 1509 intpixel[2] = (int) (blu * factor * ((1<<nBits[2]) - 1) + 0.5f); 1510 } 1511 } else if (is_LinearGray_stdScale) { 1512 red = fromsRGB8LUT16[red] & 0xffff; 1513 grn = fromsRGB8LUT16[grn] & 0xffff; 1514 blu = fromsRGB8LUT16[blu] & 0xffff; 1515 float gray = ((0.2125f * red) + 1516 (0.7154f * grn) + 1517 (0.0721f * blu)) / 65535.0f; 1518 if (supportsAlpha) { 1519 alp = (rgb>>24) & 0xff; 1520 if (nBits[1] == 8) { 1521 intpixel[1] = alp; 1522 } else { 1523 intpixel[1] = (int) (alp * (1.0f / 255.0f) * 1524 ((1 << nBits[1]) - 1) + 0.5f); 1525 } 1526 if (isAlphaPremultiplied) { 1527 gray *= (alp * (1.0f / 255.0f)); 1528 } 1529 } 1530 intpixel[0] = (int) (gray * ((1 << nBits[0]) - 1) + 0.5f); 1531 } else if (is_ICCGray_stdScale) { 1532 red = fromsRGB8LUT16[red] & 0xffff; 1533 grn = fromsRGB8LUT16[grn] & 0xffff; 1534 blu = fromsRGB8LUT16[blu] & 0xffff; 1535 int gray16 = (int) ((0.2125f * red) + 1536 (0.7154f * grn) + 1537 (0.0721f * blu) + 0.5f); 1538 float gray = (fromLinearGray16ToOtherGray16LUT[gray16] & 1539 0xffff) / 65535.0f; 1540 if (supportsAlpha) { 1541 alp = (rgb>>24) & 0xff; 1542 if (nBits[1] == 8) { 1543 intpixel[1] = alp; 1544 } else { 1545 intpixel[1] = (int) (alp * (1.0f / 255.0f) * 1546 ((1 << nBits[1]) - 1) + 0.5f); 1547 } 1548 if (isAlphaPremultiplied) { 1549 gray *= (alp * (1.0f / 255.0f)); 1550 } 1551 } 1552 intpixel[0] = (int) (gray * ((1 << nBits[0]) - 1) + 0.5f); 1553 } else { 1554 float[] norm = new float[3]; 1556 float factor = 1.0f / 255.0f; 1557 norm[0] = red * factor; 1558 norm[1] = grn * factor; 1559 norm[2] = blu * factor; 1560 norm = colorSpace.fromRGB(norm); 1561 if (nonStdScale) { 1562 for (int i = 0; i < numColorComponents; i++) { 1563 norm[i] = (norm[i] - compOffset[i]) * 1564 compScale[i]; 1565 if (norm[i] < 0.0f) { 1568 norm[i] = 0.0f; 1569 } 1570 if (norm[i] > 1.0f) { 1571 norm[i] = 1.0f; 1572 } 1573 } 1574 } 1575 if (supportsAlpha) { 1576 alp = (rgb>>24) & 0xff; 1577 if (nBits[numColorComponents] == 8) { 1578 intpixel[numColorComponents] = alp; 1579 } 1580 else { 1581 intpixel[numColorComponents] = 1582 (int) (alp * factor * 1583 ((1<<nBits[numColorComponents]) - 1) + 0.5f); 1584 } 1585 if (isAlphaPremultiplied) { 1586 factor *= alp; 1587 for (int i = 0; i < numColorComponents; i++) { 1588 norm[i] *= factor; 1589 } 1590 } 1591 } 1592 for (int i = 0; i < numColorComponents; i++) { 1593 intpixel[i] = (int) (norm[i] * ((1<<nBits[i]) - 1) + 0.5f); 1594 } 1595 } 1596 1597 switch (transferType) { 1598 case DataBuffer.TYPE_BYTE: { 1599 byte bdata[]; 1600 if (pixel == null) { 1601 bdata = new byte[numComponents]; 1602 } else { 1603 bdata = (byte[])pixel; 1604 } 1605 for (int i = 0; i < numComponents; i++) { 1606 bdata[i] = (byte)(0xff&intpixel[i]); 1607 } 1608 return bdata; 1609 } 1610 case DataBuffer.TYPE_USHORT:{ 1611 short sdata[]; 1612 if (pixel == null) { 1613 sdata = new short[numComponents]; 1614 } else { 1615 sdata = (short[])pixel; 1616 } 1617 for (int i = 0; i < numComponents; i++) { 1618 sdata[i] = (short)(intpixel[i]&0xffff); 1619 } 1620 return sdata; 1621 } 1622 case DataBuffer.TYPE_INT: 1623 if (maxBits > 23) { 1624 for (int i = 0; i < numComponents; i++) { 1629 if (intpixel[i] > ((1<<nBits[i]) - 1)) { 1630 intpixel[i] = (1<<nBits[i]) - 1; 1631 } 1632 } 1633 } 1634 return intpixel; 1635 } 1636 throw new IllegalArgumentException ("This method has not been "+ 1637 "implemented for transferType " + transferType); 1638 } 1639 1640 1664 public int[] getComponents(int pixel, int[] components, int offset) { 1665 if (numComponents > 1) { 1666 throw new 1667 IllegalArgumentException ("More than one component per pixel"); 1668 } 1669 if (needScaleInit) { 1670 initScale(); 1671 } 1672 if (noUnnorm) { 1673 throw new 1674 IllegalArgumentException ( 1675 "This ColorModel does not support the unnormalized form"); 1676 } 1677 if (components == null) { 1678 components = new int[offset+1]; 1679 } 1680 1681 components[offset+0] = (pixel & ((1<<nBits[0]) - 1)); 1682 return components; 1683 } 1684 1685 1724 public int[] getComponents(Object pixel, int[] components, int offset) { 1725 int intpixel[]; 1726 if (needScaleInit) { 1727 initScale(); 1728 } 1729 if (noUnnorm) { 1730 throw new 1731 IllegalArgumentException ( 1732 "This ColorModel does not support the unnormalized form"); 1733 } 1734 if (pixel instanceof int[]) { 1735 intpixel = (int[])pixel; 1736 } else { 1737 intpixel = DataBuffer.toIntArray(pixel); 1738 if (intpixel == null) { 1739 throw new UnsupportedOperationException ("This method has not been "+ 1740 "implemented for transferType " + transferType); 1741 } 1742 } 1743 if (intpixel.length < numComponents) { 1744 throw new IllegalArgumentException 1745 ("Length of pixel array < number of components in model"); 1746 } 1747 if (components == null) { 1748 components = new int[offset+numComponents]; 1749 } 1750 else if ((components.length-offset) < numComponents) { 1751 throw new IllegalArgumentException 1752 ("Length of components array < number of components in model"); 1753 } 1754 System.arraycopy(intpixel, 0, components, offset, numComponents); 1755 1756 return components; 1757 } 1758 1759 1799 public int[] getUnnormalizedComponents(float[] normComponents, 1800 int normOffset, 1801 int[] components, int offset) { 1802 if (needScaleInit) { 1803 initScale(); 1804 } 1805 if (noUnnorm) { 1806 throw new 1807 IllegalArgumentException ( 1808 "This ColorModel does not support the unnormalized form"); 1809 } 1810 return super.getUnnormalizedComponents(normComponents, normOffset, 1811 components, offset); 1812 } 1813 1814 1848 public float[] getNormalizedComponents(int[] components, int offset, 1849 float[] normComponents, 1850 int normOffset) { 1851 if (needScaleInit) { 1852 initScale(); 1853 } 1854 if (noUnnorm) { 1855 throw new 1856 IllegalArgumentException ( 1857 "This ColorModel does not support the unnormalized form"); 1858 } 1859 return super.getNormalizedComponents(components, offset, 1860 normComponents, normOffset); 1861 } 1862 1863 1877 public int getDataElement(int[] components, int offset) { 1878 if (needScaleInit) { 1879 initScale(); 1880 } 1881 if (numComponents == 1) { 1882 if (noUnnorm) { 1883 throw new 1884 IllegalArgumentException ( 1885 "This ColorModel does not support the unnormalized form"); 1886 } 1887 return components[offset+0]; 1888 } 1889 throw new IllegalArgumentException ("This model returns "+ 1890 numComponents+ 1891 " elements in the pixel array."); 1892 } 1893 1894 1935 public Object getDataElements(int[] components, int offset, Object obj) { 1936 if (needScaleInit) { 1937 initScale(); 1938 } 1939 if (noUnnorm) { 1940 throw new 1941 IllegalArgumentException ( 1942 "This ColorModel does not support the unnormalized form"); 1943 } 1944 if ((components.length-offset) < numComponents) { 1945 throw new IllegalArgumentException ("Component array too small"+ 1946 " (should be "+numComponents); 1947 } 1948 switch(transferType) { 1949 case DataBuffer.TYPE_INT: 1950 { 1951 int[] pixel; 1952 if (obj == null) { 1953 pixel = new int[numComponents]; 1954 } 1955 else { 1956 pixel = (int[]) obj; 1957 } 1958 System.arraycopy(components, offset, pixel, 0, 1959 numComponents); 1960 return pixel; 1961 } 1962 1963 case DataBuffer.TYPE_BYTE: 1964 { 1965 byte[] pixel; 1966 if (obj == null) { 1967 pixel = new byte[numComponents]; 1968 } 1969 else { 1970 pixel = (byte[]) obj; 1971 } 1972 for (int i=0; i < numComponents; i++) { 1973 pixel[i] = (byte) (components[offset+i]&0xff); 1974 } 1975 return pixel; 1976 } 1977 1978 case DataBuffer.TYPE_USHORT: 1979 { 1980 short[] pixel; 1981 if (obj == null) { 1982 pixel = new short[numComponents]; 1983 } 1984 else { 1985 pixel = (short[]) obj; 1986 } 1987 for (int i=0; i < numComponents; i++) { 1988 pixel[i] = (short) (components[offset+i]&0xffff); 1989 } 1990 return pixel; 1991 } 1992 1993 default: 1994 throw new UnsupportedOperationException ("This method has not been "+ 1995 "implemented for transferType " + 1996 transferType); 1997 } 1998 } 1999 2000 2025 public int getDataElement(float[] normComponents, int normOffset) { 2026 if (numComponents > 1) { 2027 throw new 2028 IllegalArgumentException ("More than one component per pixel"); 2029 } 2030 if (signed) { 2031 throw new 2032 IllegalArgumentException ("Component value is signed"); 2033 } 2034 if (needScaleInit) { 2035 initScale(); 2036 } 2037 Object pixel = getDataElements(normComponents, normOffset, null); 2038 switch (transferType) { 2039 case DataBuffer.TYPE_BYTE: 2040 { 2041 byte bpixel[] = (byte[]) pixel; 2042 return bpixel[0] & 0xff; 2043 } 2044 case DataBuffer.TYPE_USHORT: 2045 { 2046 short[] uspixel = (short[]) pixel; 2047 return uspixel[0] & 0xffff; 2048 } 2049 case DataBuffer.TYPE_INT: 2050 { 2051 int[] ipixel = (int[]) pixel; 2052 return ipixel[0]; 2053 } 2054 default: 2055 throw new UnsupportedOperationException ("This method has not been " 2056 + "implemented for transferType " + transferType); 2057 } 2058 } 2059 2060 2094 public Object getDataElements(float[] normComponents, int normOffset, 2095 Object obj) { 2096 boolean needAlpha = supportsAlpha && isAlphaPremultiplied; 2097 float[] stdNormComponents; 2098 if (needScaleInit) { 2099 initScale(); 2100 } 2101 if (nonStdScale) { 2102 stdNormComponents = new float[numComponents]; 2103 for (int c = 0, nc = normOffset; c < numColorComponents; 2104 c++, nc++) { 2105 stdNormComponents[c] = (normComponents[nc] - compOffset[c]) * 2106 compScale[c]; 2107 if (stdNormComponents[c] < 0.0f) { 2110 stdNormComponents[c] = 0.0f; 2111 } 2112 if (stdNormComponents[c] > 1.0f) { 2113 stdNormComponents[c] = 1.0f; 2114 } 2115 } 2116 if (supportsAlpha) { 2117 stdNormComponents[numColorComponents] = 2118 normComponents[numColorComponents + normOffset]; 2119 } 2120 normOffset = 0; 2121 } else { 2122 stdNormComponents = normComponents; 2123 } 2124 switch (transferType) { 2125 case DataBuffer.TYPE_BYTE: 2126 byte[] bpixel; 2127 if (obj == null) { 2128 bpixel = new byte[numComponents]; 2129 } else { 2130 bpixel = (byte[]) obj; 2131 } 2132 if (needAlpha) { 2133 float alpha = 2134 stdNormComponents[numColorComponents + normOffset]; 2135 for (int c = 0, nc = normOffset; c < numColorComponents; 2136 c++, nc++) { 2137 bpixel[c] = (byte) ((stdNormComponents[nc] * alpha) * 2138 ((float) ((1 << nBits[c]) - 1)) + 0.5f); 2139 } 2140 bpixel[numColorComponents] = 2141 (byte) (alpha * 2142 ((float) ((1 << nBits[numColorComponents]) - 1)) + 2143 0.5f); 2144 } else { 2145 for (int c = 0, nc = normOffset; c < numComponents; 2146 c++, nc++) { 2147 bpixel[c] = (byte) (stdNormComponents[nc] * 2148 ((float) ((1 << nBits[c]) - 1)) + 0.5f); 2149 } 2150 } 2151 return bpixel; 2152 case DataBuffer.TYPE_USHORT: 2153 short[] uspixel; 2154 if (obj == null) { 2155 uspixel = new short[numComponents]; 2156 } else { 2157 uspixel = (short[]) obj; 2158 } 2159 if (needAlpha) { 2160 float alpha = 2161 stdNormComponents[numColorComponents + normOffset]; 2162 for (int c = 0, nc = normOffset; c < numColorComponents; 2163 c++, nc++) { 2164 uspixel[c] = (short) ((stdNormComponents[nc] * alpha) * 2165 ((float) ((1 << nBits[c]) - 1)) + 2166 0.5f); 2167 } 2168 uspixel[numColorComponents] = 2169 (short) (alpha * 2170 ((float) ((1 << nBits[numColorComponents]) - 1)) + 2171 0.5f); 2172 } else { 2173 for (int c = 0, nc = normOffset; c < numComponents; 2174 c++, nc++) { 2175 uspixel[c] = (short) (stdNormComponents[nc] * 2176 ((float) ((1 << nBits[c]) - 1)) + 2177 0.5f); 2178 } 2179 } 2180 return uspixel; 2181 case DataBuffer.TYPE_INT: 2182 int[] ipixel; 2183 if (obj == null) { 2184 ipixel = new int[numComponents]; 2185 } else { 2186 ipixel = (int[]) obj; 2187 } 2188 if (needAlpha) { 2189 float alpha = 2190 stdNormComponents[numColorComponents + normOffset]; 2191 for (int c = 0, nc = normOffset; c < numColorComponents; 2192 c++, nc++) { 2193 ipixel[c] = (int) ((stdNormComponents[nc] * alpha) * 2194 ((float) ((1 << nBits[c]) - 1)) + 0.5f); 2195 } 2196 ipixel[numColorComponents] = 2197 (int) (alpha * 2198 ((float) ((1 << nBits[numColorComponents]) - 1)) + 2199 0.5f); 2200 } else { 2201 for (int c = 0, nc = normOffset; c < numComponents; 2202 c++, nc++) { 2203 ipixel[c] = (int) (stdNormComponents[nc] * 2204 ((float) ((1 << nBits[c]) - 1)) + 0.5f); 2205 } 2206 } 2207 return ipixel; 2208 case DataBuffer.TYPE_SHORT: 2209 short[] spixel; 2210 if (obj == null) { 2211 spixel = new short[numComponents]; 2212 } else { 2213 spixel = (short[]) obj; 2214 } 2215 if (needAlpha) { 2216 float alpha = 2217 stdNormComponents[numColorComponents + normOffset]; 2218 for (int c = 0, nc = normOffset; c < numColorComponents; 2219 c++, nc++) { 2220 spixel[c] = (short) 2221 (stdNormComponents[nc] * alpha * 32767.0f + 0.5f); 2222 } 2223 spixel[numColorComponents] = (short) (alpha * 32767.0f + 0.5f); 2224 } else { 2225 for (int c = 0, nc = normOffset; c < numComponents; 2226 c++, nc++) { 2227 spixel[c] = (short) 2228 (stdNormComponents[nc] * 32767.0f + 0.5f); 2229 } 2230 } 2231 return spixel; 2232 case DataBuffer.TYPE_FLOAT: 2233 float[] fpixel; 2234 if (obj == null) { 2235 fpixel = new float[numComponents]; 2236 } else { 2237 fpixel = (float[]) obj; 2238 } 2239 if (needAlpha) { 2240 float alpha = normComponents[numColorComponents + normOffset]; 2241 for (int c = 0, nc = normOffset; c < numColorComponents; 2242 c++, nc++) { 2243 fpixel[c] = normComponents[nc] * alpha; 2244 } 2245 fpixel[numColorComponents] = alpha; 2246 } else { 2247 for (int c = 0, nc = normOffset; c < numComponents; 2248 c++, nc++) { 2249 fpixel[c] = normComponents[nc]; 2250 } 2251 } 2252 return fpixel; 2253 case DataBuffer.TYPE_DOUBLE: 2254 double[] dpixel; 2255 if (obj == null) { 2256 dpixel = new double[numComponents]; 2257 } else { 2258 dpixel = (double[]) obj; 2259 } 2260 if (needAlpha) { 2261 double alpha = 2262 (double) (normComponents[numColorComponents + normOffset]); 2263 for (int c = 0, nc = normOffset; c < numColorComponents; 2264 c++, nc++) { 2265 dpixel[c] = normComponents[nc] * alpha; 2266 } 2267 dpixel[numColorComponents] = alpha; 2268 } else { 2269 for (int c = 0, nc = normOffset; c < numComponents; 2270 c++, nc++) { 2271 dpixel[c] = (double) normComponents[nc]; 2272 } 2273 } 2274 return dpixel; 2275 default: 2276 throw new UnsupportedOperationException ("This method has not been "+ 2277 "implemented for transferType " + 2278 transferType); 2279 } 2280 } 2281 2282 2326 public float[] getNormalizedComponents(Object pixel, 2327 float[] normComponents, 2328 int normOffset) { 2329 if (normComponents == null) { 2330 normComponents = new float[numComponents+normOffset]; 2331 } 2332 switch (transferType) { 2333 case DataBuffer.TYPE_BYTE: 2334 byte[] bpixel = (byte[]) pixel; 2335 for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) { 2336 normComponents[nc] = ((float) (bpixel[c] & 0xff)) / 2337 ((float) ((1 << nBits[c]) - 1)); 2338 } 2339 break; 2340 case DataBuffer.TYPE_USHORT: 2341 short[] uspixel = (short[]) pixel; 2342 for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) { 2343 normComponents[nc] = ((float) (uspixel[c] & 0xffff)) / 2344 ((float) ((1 << nBits[c]) - 1)); 2345 } 2346 break; 2347 case DataBuffer.TYPE_INT: 2348 int[] ipixel = (int[]) pixel; 2349 for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) { 2350 normComponents[nc] = ((float) ipixel[c]) / 2351 ((float) ((1 << nBits[c]) - 1)); 2352 } 2353 break; 2354 case DataBuffer.TYPE_SHORT: 2355 short[] spixel = (short[]) pixel; 2356 for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) { 2357 normComponents[nc] = ((float) spixel[c]) / 32767.0f; 2358 } 2359 break; 2360 case DataBuffer.TYPE_FLOAT: 2361 float[] fpixel = (float[]) pixel; 2362 for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) { 2363 normComponents[nc] = fpixel[c]; 2364 } 2365 break; 2366 case DataBuffer.TYPE_DOUBLE: 2367 double[] dpixel = (double[]) pixel; 2368 for (int c = 0, nc = normOffset; c < numComponents; c++, nc++) { 2369 normComponents[nc] = (float) dpixel[c]; 2370 } 2371 break; 2372 default: 2373 throw new UnsupportedOperationException ("This method has not been "+ 2374 "implemented for transferType " + 2375 transferType); 2376 } 2377 2378 if (supportsAlpha && isAlphaPremultiplied) { 2379 float alpha = normComponents[numColorComponents + normOffset]; 2380 if (alpha != 0.0f) { 2381 float invAlpha = 1.0f / alpha; 2382 for (int c = normOffset; c < numColorComponents + normOffset; 2383 c++) { 2384 normComponents[c] *= invAlpha; 2385 } 2386 } 2387 } 2388 if (min != null) { 2389 for (int c = 0; c < numColorComponents; c++) { 2403 normComponents[c + normOffset] = min[c] + 2404 diffMinMax[c] * normComponents[c + normOffset]; 2405 } 2406 } 2407 return normComponents; 2408 } 2409 2410 2433 public ColorModel coerceData (WritableRaster raster, 2434 boolean isAlphaPremultiplied) { 2435 if ((supportsAlpha == false) || 2436 (this.isAlphaPremultiplied == isAlphaPremultiplied)) 2437 { 2438 return this; 2440 } 2441 2442 int w = raster.getWidth(); 2443 int h = raster.getHeight(); 2444 int aIdx = raster.getNumBands() - 1; 2445 float normAlpha; 2446 int rminX = raster.getMinX(); 2447 int rY = raster.getMinY(); 2448 int rX; 2449 if (isAlphaPremultiplied) { 2450 switch (transferType) { 2451 case DataBuffer.TYPE_BYTE: { 2452 byte pixel[] = null; 2453 byte zpixel[] = null; 2454 float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1)); 2455 for (int y = 0; y < h; y++, rY++) { 2456 rX = rminX; 2457 for (int x = 0; x < w; x++, rX++) { 2458 pixel = (byte[])raster.getDataElements(rX, rY, 2459 pixel); 2460 normAlpha = (pixel[aIdx] & 0xff) * alphaScale; 2461 if (normAlpha != 0.0f) { 2462 for (int c=0; c < aIdx; c++) { 2463 pixel[c] = (byte)((pixel[c] & 0xff) * 2464 normAlpha + 0.5f); 2465 } 2466 raster.setDataElements(rX, rY, pixel); 2467 } else { 2468 if (zpixel == null) { 2469 zpixel = new byte[numComponents]; 2470 java.util.Arrays.fill(zpixel, (byte) 0); 2471 } 2472 raster.setDataElements(rX, rY, zpixel); 2473 } 2474 } 2475 } 2476 } 2477 break; 2478 case DataBuffer.TYPE_USHORT: { 2479 short pixel[] = null; 2480 short zpixel[] = null; 2481 float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1)); 2482 for (int y = 0; y < h; y++, rY++) { 2483 rX = rminX; 2484 for (int x = 0; x < w; x++, rX++) { 2485 pixel = (short[])raster.getDataElements(rX, rY, 2486 pixel); 2487 normAlpha = (pixel[aIdx] & 0xffff) * alphaScale; 2488 if (normAlpha != 0.0f) { 2489 for (int c=0; c < aIdx; c++) { 2490 pixel[c] = (short) 2491 ((pixel[c] & 0xffff) * normAlpha + 2492 0.5f); 2493 } 2494 raster.setDataElements(rX, rY, pixel); 2495 } else { 2496 if (zpixel == null) { 2497 zpixel = new short[numComponents]; 2498 java.util.Arrays.fill(zpixel, (short) 0); 2499 } 2500 raster.setDataElements(rX, rY, zpixel); 2501 } 2502 } 2503 } 2504 } 2505 break; 2506 case DataBuffer.TYPE_INT: { 2507 int pixel[] = null; 2508 int zpixel[] = null; 2509 float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1)); 2510 for (int y = 0; y < h; y++, rY++) { 2511 rX = rminX; 2512 for (int x = 0; x < w; x++, rX++) { 2513 pixel = (int[])raster.getDataElements(rX, rY, 2514 pixel); 2515 normAlpha = pixel[aIdx] * alphaScale; 2516 if (normAlpha != 0.0f) { 2517 for (int c=0; c < aIdx; c++) { 2518 pixel[c] = (int) (pixel[c] * normAlpha + 2519 0.5f); 2520 } 2521 raster.setDataElements(rX, rY, pixel); 2522 } else { 2523 if (zpixel == null) { 2524 zpixel = new int[numComponents]; 2525 java.util.Arrays.fill(zpixel, 0); 2526 } 2527 raster.setDataElements(rX, rY, zpixel); 2528 } 2529 } 2530 } 2531 } 2532 break; 2533 case DataBuffer.TYPE_SHORT: { 2534 short pixel[] = null; 2535 short zpixel[] = null; 2536 float alphaScale = 1.0f / 32767.0f; 2537 for (int y = 0; y < h; y++, rY++) { 2538 rX = rminX; 2539 for (int x = 0; x < w; x++, rX++) { 2540 pixel = (short[]) raster.getDataElements(rX, rY, 2541 pixel); 2542 normAlpha = pixel[aIdx] * alphaScale; 2543 if (normAlpha != 0.0f) { 2544 for (int c=0; c < aIdx; c++) { 2545 pixel[c] = (short) (pixel[c] * normAlpha + 2546 0.5f); 2547 } 2548 raster.setDataElements(rX, rY, pixel); 2549 } else { 2550 if (zpixel == null) { 2551 zpixel = new short[numComponents]; 2552 java.util.Arrays.fill(zpixel, (short) 0); 2553 } 2554 raster.setDataElements(rX, rY, zpixel); 2555 } 2556 } 2557 } 2558 } 2559 break; 2560 case DataBuffer.TYPE_FLOAT: { 2561 float pixel[] = null; 2562 float zpixel[] = null; 2563 for (int y = 0; y < h; y++, rY++) { 2564 rX = rminX; 2565 for (int x = 0; x < w; x++, rX++) { 2566 pixel = (float[]) raster.getDataElements(rX, rY, 2567 pixel); 2568 normAlpha = pixel[aIdx]; 2569 if (normAlpha != 0.0f) { 2570 for (int c=0; c < aIdx; c++) { 2571 pixel[c] *= normAlpha; 2572 } 2573 raster.setDataElements(rX, rY, pixel); 2574 } else { 2575 if (zpixel == null) { 2576 zpixel = new float[numComponents]; 2577 java.util.Arrays.fill(zpixel, 0.0f); 2578 } 2579 raster.setDataElements(rX, rY, zpixel); 2580 } 2581 } 2582 } 2583 } 2584 break; 2585 case DataBuffer.TYPE_DOUBLE: { 2586 double pixel[] = null; 2587 double zpixel[] = null; 2588 for (int y = 0; y < h; y++, rY++) { 2589 rX = rminX; 2590 for (int x = 0; x < w; x++, rX++) { 2591 pixel = (double[]) raster.getDataElements(rX, rY, 2592 pixel); 2593 double dnormAlpha = pixel[aIdx]; 2594 if (dnormAlpha != 0.0) { 2595 for (int c=0; c < aIdx; c++) { 2596 pixel[c] *= dnormAlpha; 2597 } 2598 raster.setDataElements(rX, rY, pixel); 2599 } else { 2600 if (zpixel == null) { 2601 zpixel = new double[numComponents]; 2602 java.util.Arrays.fill(zpixel, 0.0); 2603 } 2604 raster.setDataElements(rX, rY, zpixel); 2605 } 2606 } 2607 } 2608 } 2609 break; 2610 default: 2611 throw new UnsupportedOperationException ("This method has not been "+ 2612 "implemented for transferType " + transferType); 2613 } 2614 } 2615 else { 2616 switch (transferType) { 2618 case DataBuffer.TYPE_BYTE: { 2619 byte pixel[] = null; 2620 float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1)); 2621 for (int y = 0; y < h; y++, rY++) { 2622 rX = rminX; 2623 for (int x = 0; x < w; x++, rX++) { 2624 pixel = (byte[])raster.getDataElements(rX, rY, 2625 pixel); 2626 normAlpha = (pixel[aIdx] & 0xff) * alphaScale; 2627 if (normAlpha != 0.0f) { 2628 float invAlpha = 1.0f / normAlpha; 2629 for (int c=0; c < aIdx; c++) { 2630 pixel[c] = (byte) 2631 ((pixel[c] & 0xff) * invAlpha + 0.5f); 2632 } 2633 raster.setDataElements(rX, rY, pixel); 2634 } 2635 } 2636 } 2637 } 2638 break; 2639 case DataBuffer.TYPE_USHORT: { 2640 short pixel[] = null; 2641 float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1)); 2642 for (int y = 0; y < h; y++, rY++) { 2643 rX = rminX; 2644 for (int x = 0; x < w; x++, rX++) { 2645 pixel = (short[])raster.getDataElements(rX, rY, 2646 pixel); 2647 normAlpha = (pixel[aIdx] & 0xffff) * alphaScale; 2648 if (normAlpha != 0.0f) { 2649 float invAlpha = 1.0f / normAlpha; 2650 for (int c=0; c < aIdx; c++) { 2651 pixel[c] = (short) 2652 ((pixel[c] & 0xffff) * invAlpha + 0.5f); 2653 } 2654 raster.setDataElements(rX, rY, pixel); 2655 } 2656 } 2657 } 2658 } 2659 break; 2660 case DataBuffer.TYPE_INT: { 2661 int pixel[] = null; 2662 float alphaScale = 1.0f / ((float) ((1<<nBits[aIdx]) - 1)); 2663 for (int y = 0; y < h; y++, rY++) { 2664 rX = rminX; 2665 for (int x = 0; x < w; x++, rX++) { 2666 pixel = (int[])raster.getDataElements(rX, rY, 2667 pixel); 2668 normAlpha = pixel[aIdx] * alphaScale; 2669 if (normAlpha != 0.0f) { 2670 float invAlpha = 1.0f / normAlpha; 2671 for (int c=0; c < aIdx; c++) { 2672 pixel[c] = (int) 2673 (pixel[c] * invAlpha + 0.5f); 2674 } 2675 raster.setDataElements(rX, rY, pixel); 2676 } 2677 } 2678 } 2679 } 2680 break; 2681 case DataBuffer.TYPE_SHORT: { 2682 short pixel[] = null; 2683 float alphaScale = 1.0f / 32767.0f; 2684 for (int y = 0; y < h; y++, rY++) { 2685 rX = rminX; 2686 for (int x = 0; x < w; x++, rX++) { 2687 pixel = (short[])raster.getDataElements(rX, rY, 2688 pixel); 2689 normAlpha = pixel[aIdx] * alphaScale; 2690 if (normAlpha != 0.0f) { 2691 float invAlpha = 1.0f / normAlpha; 2692 for (int c=0; c < aIdx; c++) { 2693 pixel[c] = (short) 2694 (pixel[c] * invAlpha + 0.5f); 2695 } 2696 raster.setDataElements(rX, rY, pixel); 2697 } 2698 } 2699 } 2700 } 2701 break; 2702 case DataBuffer.TYPE_FLOAT: { 2703 float pixel[] = null; 2704 for (int y = 0; y < h; y++, rY++) { 2705 rX = rminX; 2706 for (int x = 0; x < w; x++, rX++) { 2707 pixel = (float[])raster.getDataElements(rX, rY, 2708 pixel); 2709 normAlpha = pixel[aIdx]; 2710 if (normAlpha != 0.0f) { 2711 float invAlpha = 1.0f / normAlpha; 2712 for (int c=0; c < aIdx; c++) { 2713 pixel[c] *= invAlpha; 2714 } 2715 raster.setDataElements(rX, rY, pixel); 2716 } 2717 } 2718 } 2719 } 2720 break; 2721 case DataBuffer.TYPE_DOUBLE: { 2722 double pixel[] = null; 2723 for (int y = 0; y < h; y++, rY++) { 2724 rX = rminX; 2725 for (int x = 0; x < w; x++, rX++) { 2726 pixel = (double[])raster.getDataElements(rX, rY, 2727 pixel); 2728 double dnormAlpha = pixel[aIdx]; 2729 if (dnormAlpha != 0.0) { 2730 double invAlpha = 1.0 / dnormAlpha; 2731 for (int c=0; c < aIdx; c++) { 2732 pixel[c] *= invAlpha; 2733 } 2734 raster.setDataElements(rX, rY, pixel); 2735 } 2736 } 2737 } 2738 } 2739 break; 2740 default: 2741 throw new UnsupportedOperationException ("This method has not been "+ 2742 "implemented for transferType " + transferType); 2743 } 2744 } 2745 2746 if (!signed) { 2748 return new ComponentColorModel (colorSpace, nBits, supportsAlpha, 2749 isAlphaPremultiplied, transparency, 2750 transferType); 2751 } else { 2752 return new ComponentColorModel (colorSpace, supportsAlpha, 2753 isAlphaPremultiplied, transparency, 2754 transferType); 2755 } 2756 2757 } 2758 2759 2768 public boolean isCompatibleRaster(Raster raster) { 2769 2770 SampleModel sm = raster.getSampleModel(); 2771 2772 if (sm instanceof ComponentSampleModel ) { 2773 if (sm.getNumBands() != getNumComponents()) { 2774 return false; 2775 } 2776 for (int i=0; i<nBits.length; i++) { 2777 if (sm.getSampleSize(i) < nBits[i]) { 2778 return false; 2779 } 2780 } 2781 return (raster.getTransferType() == transferType); 2782 } 2783 else { 2784 return false; 2785 } 2786 } 2787 2788 2801 public WritableRaster createCompatibleWritableRaster (int w, int h) { 2802 int dataSize = w*h*numComponents; 2803 WritableRaster raster = null; 2804 2805 switch (transferType) { 2806 case DataBuffer.TYPE_BYTE: 2807 case DataBuffer.TYPE_USHORT: 2808 raster = Raster.createInterleavedRaster(transferType, 2809 w, h, 2810 numComponents, null); 2811 break; 2812 default: 2813 SampleModel sm = createCompatibleSampleModel(w, h); 2814 DataBuffer db = sm.createDataBuffer(); 2815 raster = Raster.createWritableRaster(sm, db, null); 2816 } 2817 2818 return raster; 2819 } 2820 2821 2833 public SampleModel createCompatibleSampleModel(int w, int h) { 2834 int[] bandOffsets = new int[numComponents]; 2835 for (int i=0; i < numComponents; i++) { 2836 bandOffsets[i] = i; 2837 } 2838 switch (transferType) { 2839 case DataBuffer.TYPE_BYTE: 2840 case DataBuffer.TYPE_USHORT: 2841 return new PixelInterleavedSampleModel (transferType, w, h, 2842 numComponents, 2843 w*numComponents, 2844 bandOffsets); 2845 default: 2846 return new ComponentSampleModel (transferType, w, h, 2847 numComponents, 2848 w*numComponents, 2849 bandOffsets); 2850 } 2851 } 2852 2853 2865 public boolean isCompatibleSampleModel(SampleModel sm) { 2866 if (!(sm instanceof ComponentSampleModel )) { 2867 return false; 2868 } 2869 2870 if (numComponents != sm.getNumBands()) { 2872 return false; 2873 } 2874 2875 if (sm.getTransferType() != transferType) { 2876 return false; 2877 } 2878 2879 return true; 2880 } 2881 2882 2898 public WritableRaster getAlphaRaster(WritableRaster raster) { 2899 if (hasAlpha() == false) { 2900 return null; 2901 } 2902 2903 int x = raster.getMinX(); 2904 int y = raster.getMinY(); 2905 int[] band = new int[1]; 2906 band[0] = raster.getNumBands() - 1; 2907 return raster.createWritableChild(x, y, raster.getWidth(), 2908 raster.getHeight(), x, y, 2909 band); 2910 } 2911 2912 2919 public boolean equals(Object obj) { 2920 if (!super.equals(obj)) { 2921 return false; 2922 } 2923 2924 if (obj.getClass() != getClass()) { 2925 return false; 2926 } 2927 2928 return true; 2929 } 2930 2931} 2932 2933 | Popular Tags |