1 /* ToneReproductionCurve.java -- Representation of an ICC 'curv' type TRC
2 Copyright (C) 2004 Free Software Foundation
4 This file is part of GNU Classpath.
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)
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
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
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. */
38 package gnu
.java
.awt
.color
;
42 * ToneReproductionCurve - TRCs are used to describe RGB
43 * and Grayscale profiles. The TRC is essentially the gamma
44 * function of the color space.
46 * For example, Apple RGB has a gamma of 1.8, most monitors are ~2.2,
47 * sRGB is 2.4 with a small linear part near 0.
48 * Linear spaces are of course 1.0.
49 * (The exact function is implemented in SrgbConverter)
51 * The ICC specification allows the TRC to be described as a single
52 * Gamma value, where the function is thus out = in**gamma.
53 * Alternatively, the gamma function may be represented by a lookup table
54 * of values, in which case linear interpolation is used.
56 * @author Sven de Marothy
58 public class ToneReproductionCurve
62 private float[] reverseTrc
;
65 * Constructs a TRC from a gamma values
67 public ToneReproductionCurve(float gamma
)
75 * Constructs a TRC from a set of float values
77 public ToneReproductionCurve(float[] trcValues
)
79 trc
= new float[trcValues
.length
];
80 System
.arraycopy(trcValues
, 0, trc
, 0, trcValues
.length
);
85 * Constructs a TRC from a set of short values normalized to
86 * the 0-65535 range (as in the ICC profile file).
87 * (Note the values are treated as unsigned)
89 public ToneReproductionCurve(short[] trcValues
)
91 trc
= new float[trcValues
.length
];
92 for (int i
= 0; i
< trcValues
.length
; i
++)
93 trc
[i
] = (float) ((int) trcValues
[i
] & (0xFFFF)) / 65535.0f
;
98 * Performs a TRC lookup
100 public float lookup(float in
)
108 return (float) Math
.exp(gamma
* Math
.log(in
));
112 double alpha
= in
* (trc
.length
- 1);
113 int index
= (int) Math
.floor(alpha
);
114 alpha
= alpha
- (double) index
;
115 if (index
>= trc
.length
- 1)
116 return trc
[trc
.length
- 1];
119 out
= (float) (trc
[index
] * (1.0 - alpha
) + trc
[index
+ 1] * alpha
);
125 * Performs an reverse lookup
127 public float reverseLookup(float in
)
135 return (float) Math
.exp((1.0 / gamma
) * Math
.log(in
));
139 double alpha
= in
* (reverseTrc
.length
- 1);
140 int index
= (int) Math
.floor(alpha
);
141 alpha
= alpha
- (double) index
;
142 if (index
>= reverseTrc
.length
- 1)
143 return reverseTrc
[reverseTrc
.length
- 1];
145 return reverseTrc
[0];
146 out
= (float) (reverseTrc
[index
] * (1.0 - alpha
)
147 + reverseTrc
[index
+ 1] * alpha
);
153 * Calculates a reverse-lookup table.
154 * We use a whopping 10,000 entries.. This is should be more than any
155 * real-life TRC table (typically around 256-1024) so we won't be losing
158 * This will of course generate completely invalid results if the curve
159 * is not monotonic and invertable. But what's the alternative?
161 public void setupReverseTrc()
163 reverseTrc
= new float[10000];
165 for (int i
= 0; i
< 10000; i
++)
167 float n
= ((float) i
) / 10000f
;
168 while (trc
[j
+ 1] < n
&& j
< trc
.length
- 2)
171 if (j
== trc
.length
- 2)
172 reverseTrc
[i
] = trc
[trc
.length
- 1];
174 reverseTrc
[i
] = (j
+ (n
- trc
[j
]) / (trc
[j
+ 1] - trc
[j
])) / ((float) trc
.length
);