OSDN Git Service

b40c2f0292754214c67488a6bb0e7f41c834584c
[pf3gnuchains/gcc-fork.git] / libjava / gnu / java / awt / color / LinearRGBConverter.java
1 /* LinearRGBConverter.java -- conversion to a linear RGB color space
2    Copyright (C) 2004 Free Software Foundation
3
4    This file is part of GNU Classpath.
5
6    GNU Classpath is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2, or (at your option)
9    any later version.
10
11    GNU Classpath is distributed in the hope that it will be useful, but
12    WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14    General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with GNU Classpath; see the file COPYING.  If not, write to the
18    Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19    02111-1307 USA.
20
21    Linking this library statically or dynamically with other modules is
22    making a combined work based on this library.  Thus, the terms and
23    conditions of the GNU General Public License cover the whole
24    combination.
25
26    As a special exception, the copyright holders of this library give you
27    permission to link this library with independent modules to produce an
28    executable, regardless of the license terms of these independent
29    modules, and to copy and distribute the resulting executable under
30    terms of your choice, provided that you also meet, for each linked
31    independent module, the terms and conditions of the license of that
32    module.  An independent module is a module which is not derived from
33    or based on this library.  If you modify this library, you may extend
34    this exception to your version of the library, but you are not
35    obligated to do so.  If you do not wish to do so, delete this
36    exception statement from your version. */
37
38 package gnu.java.awt.color;
39
40
41 /**
42  * LinearRGBConverter - conversion routines for a linear sRGB colorspace
43  * sRGB is a standard for RGB colorspaces, adopted by the w3c.
44  *
45  * The specification is available at:
46  * http://www.w3.org/Graphics/Color/sRGB.html
47  *
48  * @author Sven de Marothy
49  */
50 public class LinearRGBConverter implements ColorSpaceConverter
51 {
52   /**
53    * linear RGB --> sRGB
54    * Use the inverse gamma curve
55    */
56   public float[] toRGB(float[] in)
57   {
58     float[] out = new float[3];
59     for (int i = 0; i < 3; i++)
60       {
61         float n = in[i];
62         if (n < 0)
63           n = 0f;
64         if (n > 1)
65           n = 1f;
66         if (n <= 0.00304f)
67           out[i] = in[0] * 12.92f;
68         else
69           out[i] = 1.055f * ((float) Math.exp((1 / 2.4) * Math.log(n)))
70                    - 0.055f;
71       }
72     return out;
73   }
74
75   /**
76    * sRGB --> linear RGB
77    * Use the gamma curve (gamma=2.4 in sRGB)
78    */
79   public float[] fromRGB(float[] in)
80   {
81     float[] out = new float[3];
82
83     // Convert non-linear RGB coordinates to linear ones,
84     //  numbers from the w3 spec.
85     for (int i = 0; i < 3; i++)
86       {
87         float n = in[i];
88         if (n < 0)
89           n = 0f;
90         if (n > 1)
91           n = 1f;
92         if (n <= 0.03928f)
93           out[i] = (float) (n / 12.92);
94         else
95           out[i] = (float) (Math.exp(2.4 * Math.log((n + 0.055) / 1.055)));
96       }
97     return out;
98   }
99
100   /**
101    * Linear RGB --> CIE XYZ (D50 relative)
102    * This is a simple matrix transform, the matrix (relative D65)
103    * is given in the sRGB spec. This has been combined with a
104    * linear Bradford transform for the D65-->D50 mapping, resulting
105    * in a single matrix which does the whole thing.
106    *
107    */
108   public float[] fromCIEXYZ(float[] in)
109   {
110     /*
111      * Note: The numbers which were used to calculate this only had four
112      * digits of accuracy. So don't be fooled by the number of digits here.
113      * If someone has more accurate source, feel free to update this.
114      */
115     float[] out = new float[3];
116     out[0] = (float) (3.13383065124221 * in[0] - 1.61711949411313 * in[1]
117              - 0.49071914111101 * in[2]);
118     out[1] = (float) (-0.97847026691142 * in[0] + 1.91597856031996 * in[1]
119              + 0.03340430640699 * in[2]);
120     out[2] = (float) (0.07203679486279 * in[0] - 0.22903073553113 * in[1]
121              + 1.40557835776234 * in[2]);
122     if (out[0] < 0)
123       out[0] = 0f;
124     if (out[1] < 0)
125       out[1] = 0f;
126     if (out[2] < 0)
127       out[2] = 0f;
128     if (out[0] > 1.0f)
129       out[0] = 1.0f;
130     if (out[1] > 1.0f)
131       out[1] = 1.0f;
132     if (out[2] > 1.0f)
133       out[2] = 1.0f;
134     return out;
135   }
136
137   /**
138    * Linear RGB --> CIE XYZ (D50 relative)
139    * Uses the inverse of the above matrix.
140    */
141   public float[] toCIEXYZ(float[] in)
142   {
143     float[] out = new float[3];
144     out[0] = (float) (0.43606375022190 * in[0] + 0.38514960146481 * in[1]
145              + 0.14308641888799 * in[2]);
146     out[1] = (float) (0.22245089403542 * in[0] + 0.71692584775182 * in[1]
147              + 0.06062451125578 * in[2]);
148     out[2] = (float) (0.01389851860679 * in[0] + 0.09707969011198 * in[1]
149              + 0.71399604572506 * in[2]);
150     return out;
151   }
152 }