001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 *  image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause 
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 
006 * Copyright (c) 2006-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 
007 * Visit http://imagingbook.com for additional details.
008 *******************************************************************************/
009
010package imagingbook.pub.color.image;
011
012import java.awt.color.ColorSpace;
013
014/*
015 * This class implements a D65-based sRGBcolor space without performing
016 * chromatic adaptation between D50 and D65, as required by Java's profile 
017 * connection space. Everything is D65!
018 */
019
020public class sRgb65ColorSpace extends ColorSpace {
021
022        private static final long serialVersionUID = 1L;
023
024        public sRgb65ColorSpace() {
025                super(ColorSpace.TYPE_RGB, 3);
026        }
027
028        // XYZ (D65) -> sRGB
029        public float[] fromCIEXYZ(float[] xyz) {
030                final double X = xyz[0];
031                final double Y = xyz[1];
032                final double Z = xyz[2];
033                
034                // XYZ -> RGB (linear components)
035                final double r =  3.240479 * X + -1.537150 * Y + -0.498535 * Z;
036                final double g = -0.969256 * X +  1.875992 * Y +  0.041556 * Z;
037                final double b =  0.055648 * X + -0.204043 * Y +  1.057311 * Z;
038                // RGB -> sRGB (nonlinear components)
039                float rr = (float) sRgbUtil.gammaFwd(r);
040                float gg = (float) sRgbUtil.gammaFwd(g);
041                float bb = (float) sRgbUtil.gammaFwd(b);                        
042                return new float[] {rr,gg,bb} ;
043        }
044
045        public float[] fromRGB(float[] srgb) {
046                return srgb;
047        }
048
049        // sRGB -> XYZ (D65)
050        public float[] toCIEXYZ(float[] srgb) {
051                // get linear rgb components:
052                final double r = sRgbUtil.gammaInv(srgb[0]);
053                final double g = sRgbUtil.gammaInv(srgb[1]);
054                final double b = sRgbUtil.gammaInv(srgb[2]);
055                
056                // convert to XYZ (Poynton / ITU 709) 
057                final float x = (float) (0.412453 * r + 0.357580 * g + 0.180423 * b);
058                final float y = (float) (0.212671 * r + 0.715160 * g + 0.072169 * b);
059                final float z = (float) (0.019334 * r + 0.119193 * g + 0.950227 * b);
060                return new float[] {x, y, z};
061        }
062
063        @Override
064        public float[] toRGB(float[] srgb) {
065                return srgb;
066        }
067
068
069}