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 getDataElements(int rgb, Object pixel) { 903 throw new UnsupportedOperationException 904 ("This method is not supported by this color model."); 905 } 906 907 939 public int[] getComponents(int pixel, int[] components, int offset) { 940 throw new UnsupportedOperationException 941 ("This method is not supported by this color model."); 942 } 943 944 981 public int[] getComponents(Object pixel, int[] components, int offset) { 982 throw new UnsupportedOperationException 983 ("This method is not supported by this color model."); 984 } 985 986 1032 public int[] getUnnormalizedComponents(float[] normComponents, 1033 int normOffset, 1034 int[] components, int offset) { 1035 if (colorSpace == null) { 1038 throw new UnsupportedOperationException ("This method is not supported "+ 1039 "by this color model."); 1040 } 1041 1042 if (nBits == null) { 1043 throw new UnsupportedOperationException ("This method is not supported. "+ 1044 "Unable to determine #bits per "+ 1045 "component."); 1046 } 1047 if ((normComponents.length - normOffset) < numComponents) { 1048 throw new 1049 IllegalArgumentException ( 1050 "Incorrect number of components. Expecting "+ 1051 numComponents); 1052 } 1053 1054 if (components == null) { 1055 components = new int[offset+numComponents]; 1056 } 1057 1058 if (supportsAlpha && isAlphaPremultiplied) { 1059 float normAlpha = normComponents[normOffset+numColorComponents]; 1060 for (int i=0; i < numColorComponents; i++) { 1061 components[offset+i] = (int) (normComponents[normOffset+i] 1062 * ((1<<nBits[i]) - 1) 1063 * normAlpha + 0.5f); 1064 } 1065 components[offset+numColorComponents] = (int) 1066 (normAlpha * ((1<<nBits[numColorComponents]) - 1) + 0.5f); 1067 } 1068 else { 1069 for (int i=0; i < numComponents; i++) { 1070 components[offset+i] = (int) (normComponents[normOffset+i] 1071 * ((1<<nBits[i]) - 1) + 0.5f); 1072 } 1073 } 1074 1075 return components; 1076 } 1077 1078 1128 public float[] getNormalizedComponents(int[] components, int offset, 1129 float[] normComponents, 1130 int normOffset) { 1131 if (colorSpace == null) { 1134 throw new UnsupportedOperationException ("This method is not supported by "+ 1135 "this color model."); 1136 } 1137 if (nBits == null) { 1138 throw new UnsupportedOperationException ("This method is not supported. "+ 1139 "Unable to determine #bits per "+ 1140 "component."); 1141 } 1142 1143 if ((components.length - offset) < numComponents) { 1144 throw new 1145 IllegalArgumentException ( 1146 "Incorrect number of components. Expecting "+ 1147 numComponents); 1148 } 1149 1150 if (normComponents == null) { 1151 normComponents = new float[numComponents+normOffset]; 1152 } 1153 1154 if (supportsAlpha && isAlphaPremultiplied) { 1155 float normAlpha = (float)components[offset+numColorComponents]; 1157 normAlpha /= (float) ((1<<nBits[numColorComponents]) - 1); 1158 if (normAlpha != 0.0f) { 1159 for (int i=0; i < numColorComponents; i++) { 1160 normComponents[normOffset+i] = 1161 ((float) components[offset+i]) / 1162 (normAlpha * ((float) ((1<<nBits[i]) - 1))); 1163 } 1164 } else { 1165 for (int i=0; i < numColorComponents; i++) { 1166 normComponents[normOffset+i] = 0.0f; 1167 } 1168 } 1169 normComponents[normOffset+numColorComponents] = normAlpha; 1170 } 1171 else { 1172 for (int i=0; i < numComponents; i++) { 1173 normComponents[normOffset+i] = ((float) components[offset+i]) / 1174 ((float) ((1<<nBits[i]) - 1)); 1175 } 1176 } 1177 1178 return normComponents; 1179 } 1180 1181 1216 public int getDataElement(int[] components, int offset) { 1217 throw new UnsupportedOperationException ("This method is not supported "+ 1218 "by this color model."); 1219 } 1220 1221 1267 public Object getDataElements(int[] components, int offset, Object obj) { 1268 throw new UnsupportedOperationException ("This method has not been implemented "+ 1269 "for this color model."); 1270 } 1271 1272 1304 public int getDataElement(float[] normComponents, int normOffset) { 1305 int components[] = getUnnormalizedComponents(normComponents, 1306 normOffset, null, 0); 1307 return getDataElement(components, 0); 1308 } 1309 1310 1351 public Object getDataElements(float[] normComponents, int normOffset, 1352 Object obj) { 1353 int components[] = getUnnormalizedComponents(normComponents, 1354 normOffset, null, 0); 1355 return getDataElements(components, 0, obj); 1356 } 1357 1358 1411 public float[] getNormalizedComponents(Object pixel, 1412 float[] normComponents, 1413 int normOffset) { 1414 int components[] = getComponents(pixel, null, 0); 1415 return getNormalizedComponents(components, 0, 1416 normComponents, normOffset); 1417 } 1418 1419 1428 public boolean equals(Object obj) { 1429 if (!(obj instanceof ColorModel )) { 1430 return false; 1431 } 1432 ColorModel cm = (ColorModel ) obj; 1433 1434 if (this == cm) { 1435 return true; 1436 } 1437 if (supportsAlpha != cm.hasAlpha() || 1438 isAlphaPremultiplied != cm.isAlphaPremultiplied() || 1439 pixel_bits != cm.getPixelSize() || 1440 transparency != cm.getTransparency() || 1441 numComponents != cm.getNumComponents()) 1442 { 1443 return false; 1444 } 1445 1446 int[] nb = cm.getComponentSize(); 1447 1448 if ((nBits != null) && (nb != null)) { 1449 for (int i = 0; i < numComponents; i++) { 1450 if (nBits[i] != nb[i]) { 1451 return false; 1452 } 1453 } 1454 } else { 1455 return ((nBits == null) && (nb == null)); 1456 } 1457 1458 return true; 1459 } 1460 1461 1466 public int hashCode() { 1467 1468 int result = 0; 1469 1470 result = (supportsAlpha ? 2 : 3) + 1471 (isAlphaPremultiplied ? 4 : 5) + 1472 pixel_bits * 6 + 1473 transparency * 7 + 1474 numComponents * 8; 1475 1476 if (nBits != null) { 1477 for (int i = 0; i < numComponents; i++) { 1478 result = result + nBits[i] * (i + 9); 1479 } 1480 } 1481 1482 return result; 1483 } 1484 1485 1491 final public ColorSpace getColorSpace() { 1492 return colorSpace; 1493 } 1494 1495 1516 public ColorModel coerceData (WritableRaster raster, 1517 boolean isAlphaPremultiplied) { 1518 throw new UnsupportedOperationException 1519 ("This method is not supported by this color model"); 1520 } 1521 1522 1537 public boolean isCompatibleRaster(Raster raster) { 1538 throw new UnsupportedOperationException ( 1539 "This method has not been implemented for this ColorModel."); 1540 } 1541 1542 1559 public WritableRaster createCompatibleWritableRaster(int w, int h) { 1560 throw new UnsupportedOperationException 1561 ("This method is not supported by this color model"); 1562 } 1563 1564 1580 public SampleModel createCompatibleSampleModel(int w, int h) { 1581 throw new UnsupportedOperationException 1582 ("This method is not supported by this color model"); 1583 } 1584 1585 1599 public boolean isCompatibleSampleModel(SampleModel sm) { 1600 throw new UnsupportedOperationException 1601 ("This method is not supported by this color model"); 1602 } 1603 1604 1609 public void finalize() { 1610 } 1611 1612 1613 1638 public WritableRaster getAlphaRaster(WritableRaster raster) { 1639 return null; 1640 } 1641 1642 1648 public String toString() { 1649 return new String ("ColorModel: #pixelBits = "+pixel_bits 1650 + " numComponents = "+numComponents 1651 + " color space = "+colorSpace 1652 + " transparency = "+transparency 1653 + " has alpha = "+supportsAlpha 1654 + " isAlphaPre = "+isAlphaPremultiplied 1655 ); 1656 } 1657 1658 static int getDefaultTransferType(int pixel_bits) { 1659 if (pixel_bits <= 8) { 1660 return DataBuffer.TYPE_BYTE; 1661 } else if (pixel_bits <= 16) { 1662 return DataBuffer.TYPE_USHORT; 1663 } else if (pixel_bits <= 32) { 1664 return DataBuffer.TYPE_INT; 1665 } else { 1666 return DataBuffer.TYPE_UNDEFINED; 1667 } 1668 } 1669 1670 static byte[] l8Tos8 = null; static byte[] s8Tol8 = null; static byte[] l16Tos8 = null; static short[] s8Tol16 = null; 1675 static Map g8Tos8Map = null; static Map lg16Toog8Map = null; static Map g16Tos8Map = null; static Map lg16Toog16Map = null; 1681 static boolean isLinearRGBspace(ColorSpace cs) { 1682 return (cs == CMM.LINEAR_RGBspace); 1685 } 1686 1687 static boolean isLinearGRAYspace(ColorSpace cs) { 1688 return (cs == CMM.GRAYspace); 1691 } 1692 1693 static byte[] getLinearRGB8TosRGB8LUT() { 1694 if (l8Tos8 == null) { 1695 l8Tos8 = new byte[256]; 1696 float input, output; 1697 for (int i = 0; i <= 255; i++) { 1703 input = ((float) i) / 255.0f; 1704 if (input <= 0.0031308f) { 1705 output = input * 12.92f; 1706 } else { 1707 output = 1.055f * ((float) Math.pow(input, (1.0 / 2.4))) 1708 - 0.055f; 1709 } 1710 l8Tos8[i] = (byte) Math.round(output * 255.0f); 1711 } 1712 } 1713 return l8Tos8; 1714 } 1715 1716 static byte[] getsRGB8ToLinearRGB8LUT() { 1717 if (s8Tol8 == null) { 1718 s8Tol8 = new byte[256]; 1719 float input, output; 1720 for (int i = 0; i <= 255; i++) { 1722 input = ((float) i) / 255.0f; 1723 if (input <= 0.04045f) { 1724 output = input / 12.92f; 1725 } else { 1726 output = (float) Math.pow((input + 0.055f) / 1.055f, 2.4); 1727 } 1728 s8Tol8[i] = (byte) Math.round(output * 255.0f); 1729 } 1730 } 1731 return s8Tol8; 1732 } 1733 1734 static byte[] getLinearRGB16TosRGB8LUT() { 1735 if (l16Tos8 == null) { 1736 l16Tos8 = new byte[65536]; 1737 float input, output; 1738 for (int i = 0; i <= 65535; i++) { 1740 input = ((float) i) / 65535.0f; 1741 if (input <= 0.0031308f) { 1742 output = input * 12.92f; 1743 } else { 1744 output = 1.055f * ((float) Math.pow(input, (1.0 / 2.4))) 1745 - 0.055f; 1746 } 1747 l16Tos8[i] = (byte) Math.round(output * 255.0f); 1748 } 1749 } 1750 return l16Tos8; 1751 } 1752 1753 static short[] getsRGB8ToLinearRGB16LUT() { 1754 if (s8Tol16 == null) { 1755 s8Tol16 = new short[256]; 1756 float input, output; 1757 for (int i = 0; i <= 255; i++) { 1759 input = ((float) i) / 255.0f; 1760 if (input <= 0.04045f) { 1761 output = input / 12.92f; 1762 } else { 1763 output = (float) Math.pow((input + 0.055f) / 1.055f, 2.4); 1764 } 1765 s8Tol16[i] = (short) Math.round(output * 65535.0f); 1766 } 1767 } 1768 return s8Tol16; 1769 } 1770 1771 1778 static byte[] getGray8TosRGB8LUT(ICC_ColorSpace grayCS) { 1779 if (isLinearGRAYspace(grayCS)) { 1780 return getLinearRGB8TosRGB8LUT(); 1781 } 1782 if (g8Tos8Map != null) { 1783 byte[] g8Tos8LUT = (byte []) g8Tos8Map.get(grayCS); 1784 if (g8Tos8LUT != null) { 1785 return g8Tos8LUT; 1786 } 1787 } 1788 byte[] g8Tos8LUT = new byte[256]; 1789 for (int i = 0; i <= 255; i++) { 1790 g8Tos8LUT[i] = (byte) i; 1791 } 1792 ICC_Transform[] transformList = new ICC_Transform[2]; 1793 ICC_ColorSpace srgbCS = 1794 (ICC_ColorSpace ) ColorSpace.getInstance(ColorSpace.CS_sRGB); 1795 transformList[0] = new ICC_Transform ( 1796 grayCS.getProfile(), ICC_Transform.Any, ICC_Transform.In); 1797 transformList[1] = new ICC_Transform ( 1798 srgbCS.getProfile(), ICC_Transform.Any, ICC_Transform.Out); 1799 ICC_Transform t = new ICC_Transform(transformList); 1800 byte[] tmp = t.colorConvert(g8Tos8LUT, null); 1801 for (int i = 0, j= 2; i <= 255; i++, j += 3) { 1802 g8Tos8LUT[i] = tmp[j]; 1808 } 1809 if (g8Tos8Map == null) { 1810 g8Tos8Map = Collections.synchronizedMap(new WeakHashMap (2)); 1811 } 1812 g8Tos8Map.put(grayCS, g8Tos8LUT); 1813 return g8Tos8LUT; 1814 } 1815 1816 1821 static byte[] getLinearGray16ToOtherGray8LUT(ICC_ColorSpace grayCS) { 1822 if (lg16Toog8Map != null) { 1823 byte[] lg16Toog8LUT = (byte []) lg16Toog8Map.get(grayCS); 1824 if (lg16Toog8LUT != null) { 1825 return lg16Toog8LUT; 1826 } 1827 } 1828 short[] tmp = new short[65536]; 1829 for (int i = 0; i <= 65535; i++) { 1830 tmp[i] = (short) i; 1831 } 1832 ICC_Transform[] transformList = new ICC_Transform[2]; 1833 ICC_ColorSpace lgCS = 1834 (ICC_ColorSpace ) ColorSpace.getInstance(ColorSpace.CS_GRAY); 1835 transformList[0] = new ICC_Transform ( 1836 lgCS.getProfile(), ICC_Transform.Any, ICC_Transform.In); 1837 transformList[1] = new ICC_Transform ( 1838 grayCS.getProfile(), ICC_Transform.Any, ICC_Transform.Out); 1839 ICC_Transform t = new ICC_Transform(transformList); 1840 tmp = t.colorConvert(tmp, null); 1841 byte[] lg16Toog8LUT = new byte[65536]; 1842 for (int i = 0; i <= 65535; i++) { 1843 lg16Toog8LUT[i] = 1845 (byte) (((float) (tmp[i] & 0xffff)) * (1.0f /257.0f) + 0.5f); 1846 } 1847 if (lg16Toog8Map == null) { 1848 lg16Toog8Map = Collections.synchronizedMap(new WeakHashMap (2)); 1849 } 1850 lg16Toog8Map.put(grayCS, lg16Toog8LUT); 1851 return lg16Toog8LUT; 1852 } 1853 1854 1861 static byte[] getGray16TosRGB8LUT(ICC_ColorSpace grayCS) { 1862 if (isLinearGRAYspace(grayCS)) { 1863 return getLinearRGB16TosRGB8LUT(); 1864 } 1865 if (g16Tos8Map != null) { 1866 byte[] g16Tos8LUT = (byte []) g16Tos8Map.get(grayCS); 1867 if (g16Tos8LUT != null) { 1868 return g16Tos8LUT; 1869 } 1870 } 1871 short[] tmp = new short[65536]; 1872 for (int i = 0; i <= 65535; i++) { 1873 tmp[i] = (short) i; 1874 } 1875 ICC_Transform[] transformList = new ICC_Transform[2]; 1876 ICC_ColorSpace srgbCS = 1877 (ICC_ColorSpace ) ColorSpace.getInstance(ColorSpace.CS_sRGB); 1878 transformList[0] = new ICC_Transform ( 1879 grayCS.getProfile(), ICC_Transform.Any, ICC_Transform.In); 1880 transformList[1] = new ICC_Transform ( 1881 srgbCS.getProfile(), ICC_Transform.Any, ICC_Transform.Out); 1882 ICC_Transform t = new ICC_Transform(transformList); 1883 tmp = t.colorConvert(tmp, null); 1884 byte[] g16Tos8LUT = new byte[65536]; 1885 for (int i = 0, j= 2; i <= 65535; i++, j += 3) { 1886 1892 g16Tos8LUT[i] = 1894 (byte) (((float) (tmp[j] & 0xffff)) * (1.0f /257.0f) + 0.5f); 1895 } 1896 if (g16Tos8Map == null) { 1897 g16Tos8Map = Collections.synchronizedMap(new WeakHashMap (2)); 1898 } 1899 g16Tos8Map.put(grayCS, g16Tos8LUT); 1900 return g16Tos8LUT; 1901 } 1902 1903 1908 static short[] getLinearGray16ToOtherGray16LUT(ICC_ColorSpace grayCS) { 1909 if (lg16Toog16Map != null) { 1910 short[] lg16Toog16LUT = (short []) lg16Toog16Map.get(grayCS); 1911 if (lg16Toog16LUT != null) { 1912 return lg16Toog16LUT; 1913 } 1914 } 1915 short[] tmp = new short[65536]; 1916 for (int i = 0; i <= 65535; i++) { 1917 tmp[i] = (short) i; 1918 } 1919 ICC_Transform[] transformList = new ICC_Transform[2]; 1920 ICC_ColorSpace lgCS = 1921 (ICC_ColorSpace ) ColorSpace.getInstance(ColorSpace.CS_GRAY); 1922 transformList[0] = new ICC_Transform ( 1923 lgCS.getProfile(), ICC_Transform.Any, ICC_Transform.In); 1924 transformList[1] = new ICC_Transform ( 1925 grayCS.getProfile(), ICC_Transform.Any, ICC_Transform.Out); 1926 ICC_Transform t = new ICC_Transform(transformList); 1927 short[] lg16Toog16LUT = t.colorConvert(tmp, null); 1928 if (lg16Toog16Map == null) { 1929 lg16Toog16Map = Collections.synchronizedMap(new WeakHashMap (2)); 1930 } 1931 lg16Toog16Map.put(grayCS, lg16Toog16LUT); 1932 return lg16Toog16LUT; 1933 } 1934 1935} 1936 | Popular Tags |