001/* 002 * $RCSfile: TIFFCIELabColorConverter.java,v $ 003 * 004 * 005 * Copyright (c) 2005 Sun Microsystems, Inc. All Rights Reserved. 006 * 007 * Redistribution and use in source and binary forms, with or without 008 * modification, are permitted provided that the following conditions 009 * are met: 010 * 011 * - Redistribution of source code must retain the above copyright 012 * notice, this list of conditions and the following disclaimer. 013 * 014 * - Redistribution in binary form must reproduce the above copyright 015 * notice, this list of conditions and the following disclaimer in 016 * the documentation and/or other materials provided with the 017 * distribution. 018 * 019 * Neither the name of Sun Microsystems, Inc. or the names of 020 * contributors may be used to endorse or promote products derived 021 * from this software without specific prior written permission. 022 * 023 * This software is provided "AS IS," without a warranty of any 024 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND 025 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, 026 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY 027 * EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL 028 * NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF 029 * USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS 030 * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR 031 * ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, 032 * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND 033 * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR 034 * INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE 035 * POSSIBILITY OF SUCH DAMAGES. 036 * 037 * You acknowledge that this software is not designed or intended for 038 * use in the design, construction, operation or maintenance of any 039 * nuclear facility. 040 * 041 * $Revision: 1.1 $ 042 * $Date: 2005/02/11 05:01:44 $ 043 * $State: Exp $ 044 */ 045package com.github.jaiimageio.impl.plugins.tiff; 046 047import com.github.jaiimageio.plugins.tiff.TIFFColorConverter; 048 049/** 050 */ 051public class TIFFCIELabColorConverter extends TIFFColorConverter { 052 053 // XYX coordinate or reference white (CIE D65) 054 private static final float Xn = 95.047f; 055 private static final float Yn = 100.0f; 056 private static final float Zn = 108.883f; 057 058 private static final float THRESHOLD = (float)Math.pow(0.008856, 1.0/3.0); 059 060 public TIFFCIELabColorConverter() {} 061 062 063 private float clamp(float x) { 064 if (x < 0.0f) { 065 return 0.0f; 066 } else if (x > 100.0f) { 067 return 255.0f; 068 } else { 069 return x*(255.0f/100.0f); 070 } 071 } 072 073 private float clamp2(float x) { 074 if (x < 0.0f) { 075 return 0.0f; 076 } else if (x > 255.0f) { 077 return 255.0f; 078 } else { 079 return x; 080 } 081 } 082 083 public void fromRGB(float r, float g, float b, float[] result) { 084 float X = 0.412453f*r + 0.357580f*g + 0.180423f*b; 085 float Y = 0.212671f*r + 0.715160f*g + 0.072169f*b; 086 float Z = 0.019334f*r + 0.119193f*g + 0.950227f*b; 087 088 float YYn = Y/Yn; 089 float XXn = X/Xn; 090 float ZZn = Z/Zn; 091 092 if (YYn < 0.008856f) { 093 YYn = 7.787f*YYn + 16.0f/116.0f; 094 } else { 095 YYn = (float)Math.pow(YYn, 1.0/3.0); 096 } 097 098 if (XXn < 0.008856f) { 099 XXn = 7.787f*XXn + 16.0f/116.0f; 100 } else { 101 XXn = (float)Math.pow(XXn, 1.0/3.0); 102 } 103 104 if (ZZn < 0.008856f) { 105 ZZn = 7.787f*ZZn + 16.0f/116.0f; 106 } else { 107 ZZn = (float)Math.pow(ZZn, 1.0/3.0); 108 } 109 110 float LStar = 116.0f*YYn - 16.0f; 111 float aStar = 500.0f*(XXn - YYn); 112 float bStar = 200.0f*(YYn - ZZn); 113 114 LStar *= 255.0f/100.0f; 115 if (aStar < 0.0f) { 116 aStar += 256.0f; 117 } 118 if (bStar < 0.0f) { 119 bStar += 256.0f; 120 } 121 122 result[0] = clamp2(LStar); 123 result[1] = clamp2(aStar); 124 result[2] = clamp2(bStar); 125 } 126 127 public void toRGB(float x0, float x1, float x2, float[] rgb) { 128 float LStar = x0*100.0f/255.0f; 129 float aStar = (x1 > 128.0f) ? (x1 - 256.0f) : x1; 130 float bStar = (x2 > 128.0f) ? (x2 - 256.0f) : x2; 131 132 float YYn; // Y/Yn 133 float fY; // 'F' value for Y 134 135 if (LStar < 8.0f) { 136 YYn = LStar/903.3f; 137 fY = 7.787f*YYn + 16.0f/116.0f; 138 } else { 139 float YYn_cubeRoot = (LStar + 16.0f)/116.0f; 140 YYn = YYn_cubeRoot*YYn_cubeRoot*YYn_cubeRoot; 141 fY = (float)Math.pow(YYn, 1.0/3.0); 142 } 143 float Y = YYn*Yn; 144 145 float fX = fY + (aStar/500.0f); 146 float X; 147 if (fX <= THRESHOLD) { 148 X = Xn*(fX - 16.0f/116.0f)/7.787f; 149 } else { 150 X = Xn*fX*fX*fX; 151 } 152 153 float fZ = fY - bStar/200.0f; 154 float Z; 155 if (fZ <= THRESHOLD) { 156 Z = Zn*(fZ - 16.0f/116.0f)/7.787f; 157 } else { 158 Z = Zn*fZ*fZ*fZ; 159 } 160 161 float R = 3.240479f*X - 1.537150f*Y - 0.498535f*Z; 162 float G = -0.969256f*X + 1.875992f*Y + 0.041556f*Z; 163 float B = 0.055648f*X - 0.204043f*Y + 1.057311f*Z; 164 165 rgb[0] = clamp(R); 166 rgb[1] = clamp(G); 167 rgb[2] = clamp(B); 168 } 169}