1 7 8 17 18 package java.awt.color; 19 20 import sun.awt.color.ICC_Transform; 21 22 23 64 65 66 67 public class ICC_ColorSpace extends ColorSpace { 68 69 static final long serialVersionUID = 3455889114070431483L; 70 71 private ICC_Profile thisProfile; 72 private float[] minVal; 73 private float[] maxVal; 74 private float[] diffMinMax; 75 private float[] invDiffMinMax; 76 private boolean needScaleInit = true; 77 78 private transient ICC_Transform this2srgb; 80 private transient ICC_Transform srgb2this; 81 private transient ICC_Transform this2xyz; 82 private transient ICC_Transform xyz2this; 83 84 85 91 public ICC_ColorSpace (ICC_Profile profile) { 92 super (profile.getColorSpaceType(), profile.getNumComponents()); 93 94 int profileClass = profile.getProfileClass(); 95 96 97 if ((profileClass != ICC_Profile.CLASS_INPUT) && 98 (profileClass != ICC_Profile.CLASS_DISPLAY) && 99 (profileClass != ICC_Profile.CLASS_OUTPUT) && 100 (profileClass != ICC_Profile.CLASS_COLORSPACECONVERSION) && 101 (profileClass != ICC_Profile.CLASS_NAMEDCOLOR) ) { 102 throw new IllegalArgumentException ("Invalid profile type"); 103 } 104 105 thisProfile = profile; 106 setMinMax(); 107 } 108 109 113 public ICC_Profile getProfile() { 114 return thisProfile; 115 } 116 117 138 public float[] toRGB (float[] colorvalue) { 139 140 if (this2srgb == null) { 141 ICC_Transform[] transformList = new ICC_Transform [2]; 142 ICC_ColorSpace srgbCS = 143 (ICC_ColorSpace ) ColorSpace.getInstance (CS_sRGB); 144 transformList[0] = new ICC_Transform ( 145 thisProfile, ICC_Transform.Any, ICC_Transform.In); 146 transformList[1] = new ICC_Transform ( 147 srgbCS.getProfile(), ICC_Transform.Any, ICC_Transform.Out); 148 this2srgb = new ICC_Transform (transformList); 149 if (needScaleInit) { 150 setComponentScaling(); 151 } 152 } 153 154 int nc = this.getNumComponents(); 155 short tmp[] = new short[nc]; 156 for (int i = 0; i < nc; i++) { 157 tmp[i] = (short) 158 ((colorvalue[i] - minVal[i]) * invDiffMinMax[i] + 0.5f); 159 } 160 tmp = this2srgb.colorConvert(tmp, null); 161 float[] result = new float [3]; 162 for (int i = 0; i < 3; i++) { 163 result[i] = ((float) (tmp[i] & 0xffff)) / 65535.0f; 164 } 165 return result; 166 } 167 168 189 public float[] fromRGB(float[] rgbvalue) { 190 191 if (srgb2this == null) { 192 ICC_Transform[] transformList = new ICC_Transform [2]; 193 ICC_ColorSpace srgbCS = 194 (ICC_ColorSpace ) ColorSpace.getInstance (CS_sRGB); 195 transformList[0] = new ICC_Transform ( 196 srgbCS.getProfile(), ICC_Transform.Any, ICC_Transform.In); 197 transformList[1] = new ICC_Transform ( 198 thisProfile, ICC_Transform.Any, ICC_Transform.Out); 199 srgb2this = new ICC_Transform (transformList); 200 if (needScaleInit) { 201 setComponentScaling(); 202 } 203 } 204 205 short tmp[] = new short[3]; 206 for (int i = 0; i < 3; i++) { 207 tmp[i] = (short) ((rgbvalue[i] * 65535.0f) + 0.5f); 208 } 209 tmp = srgb2this.colorConvert(tmp, null); 210 int nc = this.getNumComponents(); 211 float[] result = new float [nc]; 212 for (int i = 0; i < nc; i++) { 213 result[i] = (((float) (tmp[i] & 0xffff)) / 65535.0f) * 214 diffMinMax[i] + minVal[i]; 215 } 216 return result; 217 } 218 219 220 323 public float[] toCIEXYZ(float[] colorvalue) { 324 325 if (this2xyz == null) { 326 ICC_Transform[] transformList = new ICC_Transform [2]; 327 ICC_ColorSpace xyzCS = 328 (ICC_ColorSpace ) ColorSpace.getInstance (CS_CIEXYZ); 329 try { 330 transformList[0] = new ICC_Transform (thisProfile, 331 ICC_Profile.icRelativeColorimetric, ICC_Transform.In); 332 } catch (CMMException e) { 333 transformList[0] = new ICC_Transform (thisProfile, 334 ICC_Transform.Any, ICC_Transform.In); 335 } 336 transformList[1] = new ICC_Transform (xyzCS.getProfile(), 337 ICC_Transform.Any, ICC_Transform.Out); 338 this2xyz = new ICC_Transform (transformList); 339 if (needScaleInit) { 340 setComponentScaling(); 341 } 342 } 343 344 int nc = this.getNumComponents(); 345 short tmp[] = new short[nc]; 346 for (int i = 0; i < nc; i++) { 347 tmp[i] = (short) 348 ((colorvalue[i] - minVal[i]) * invDiffMinMax[i] + 0.5f); 349 } 350 tmp = this2xyz.colorConvert(tmp, null); 351 float ALMOST_TWO = 1.0f + (32767.0f / 32768.0f); 352 float[] result = new float [3]; 354 for (int i = 0; i < 3; i++) { 355 result[i] = (((float) (tmp[i] & 0xffff)) / 65535.0f) * ALMOST_TWO; 356 } 357 return result; 358 } 359 360 361 466 public float[] fromCIEXYZ(float[] colorvalue) { 467 468 if (xyz2this == null) { 469 ICC_Transform[] transformList = new ICC_Transform [2]; 470 ICC_ColorSpace xyzCS = 471 (ICC_ColorSpace ) ColorSpace.getInstance (CS_CIEXYZ); 472 transformList[0] = new ICC_Transform (xyzCS.getProfile(), 473 ICC_Transform.Any, ICC_Transform.In); 474 try { 475 transformList[1] = new ICC_Transform (thisProfile, 476 ICC_Profile.icRelativeColorimetric, ICC_Transform.Out); 477 } catch (CMMException e) { 478 transformList[1] = new ICC_Transform (thisProfile, 479 ICC_Transform.Any, ICC_Transform.Out); 480 } 481 xyz2this = new ICC_Transform (transformList); 482 if (needScaleInit) { 483 setComponentScaling(); 484 } 485 } 486 487 short tmp[] = new short[3]; 488 float ALMOST_TWO = 1.0f + (32767.0f / 32768.0f); 489 float factor = 65535.0f / ALMOST_TWO; 490 for (int i = 0; i < 3; i++) { 492 tmp[i] = (short) ((colorvalue[i] * factor) + 0.5f); 493 } 494 tmp = xyz2this.colorConvert(tmp, null); 495 int nc = this.getNumComponents(); 496 float[] result = new float [nc]; 497 for (int i = 0; i < nc; i++) { 498 result[i] = (((float) (tmp[i] & 0xffff)) / 65535.0f) * 499 diffMinMax[i] + minVal[i]; 500 } 501 return result; 502 } 503 504 520 public float getMinValue(int component) { 521 if ((component < 0) || (component > this.getNumComponents() - 1)) { 522 throw new IllegalArgumentException ( 523 "Component index out of range: + component"); 524 } 525 return minVal[component]; 526 } 527 528 545 public float getMaxValue(int component) { 546 if ((component < 0) || (component > this.getNumComponents() - 1)) { 547 throw new IllegalArgumentException ( 548 "Component index out of range: + component"); 549 } 550 return maxVal[component]; 551 } 552 553 private void setMinMax() { 554 int nc = this.getNumComponents(); 555 int type = this.getType(); 556 minVal = new float[nc]; 557 maxVal = new float[nc]; 558 if (type == ColorSpace.TYPE_Lab) { 559 minVal[0] = 0.0f; maxVal[0] = 100.0f; 561 minVal[1] = -128.0f; maxVal[1] = 127.0f; 563 minVal[2] = -128.0f; maxVal[2] = 127.0f; 565 } else if (type == ColorSpace.TYPE_XYZ) { 566 minVal[0] = minVal[1] = minVal[2] = 0.0f; maxVal[0] = maxVal[1] = maxVal[2] = 1.0f + (32767.0f/ 32768.0f); 568 } else { 569 for (int i = 0; i < nc; i++) { 570 minVal[i] = 0.0f; 571 maxVal[i] = 1.0f; 572 } 573 } 574 } 575 576 private void setComponentScaling() { 577 int nc = this.getNumComponents(); 578 diffMinMax = new float[nc]; 579 invDiffMinMax = new float[nc]; 580 for (int i = 0; i < nc; i++) { 581 minVal[i] = this.getMinValue(i); maxVal[i] = this.getMaxValue(i); diffMinMax[i] = maxVal[i] - minVal[i]; 584 invDiffMinMax[i] = 65535.0f / diffMinMax[i]; 585 } 586 needScaleInit = false; 587 } 588 589 } 590 | Popular Tags |