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., 59 Temple Place, Suite 330, 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.
39 package gnu.java.awt.color;
43 * ToneReproductionCurve - TRCs are used to describe RGB
44 * and Grayscale profiles. The TRC is essentially the gamma
45 * function of the color space.
47 * For example, Apple RGB has a gamma of 1.8, most monitors are ~2.2,
48 * sRGB is 2.4 with a small linear part near 0.
49 * Linear spaces are of course 1.0.
50 * (The exact function is implemented in SrgbConverter)
52 * The ICC specification allows the TRC to be described as a single
53 * Gamma value, where the function is thus out = in**gamma.
54 * Alternatively, the gamma function may be represented by a lookup table
55 * of values, in which case linear interpolation is used.
57 * @author Sven de Marothy
59 public class ToneReproductionCurve
63 private float[] reverseTrc;
66 * Constructs a TRC from a gamma values
68 public ToneReproductionCurve(float gamma)
76 * Constructs a TRC from a set of float values
78 public ToneReproductionCurve(float[] trcValues)
80 trc = new float[trcValues.length];
81 System.arraycopy(trcValues, 0, trc, 0, trcValues.length);
86 * Constructs a TRC from a set of short values normalized to
87 * the 0-65535 range (as in the ICC profile file).
88 * (Note the values are treated as unsigned)
90 public ToneReproductionCurve(short[] trcValues)
92 trc = new float[trcValues.length];
93 for (int i = 0; i < trcValues.length; i++)
94 trc[i] = (float) ((int) trcValues[i] & (0xFFFF)) / 65535.0f;
99 * Performs a TRC lookup
101 public float lookup(float in)
109 return (float) Math.exp(gamma * Math.log(in));
113 double alpha = in * (trc.length - 1);
114 int index = (int) Math.floor(alpha);
115 alpha = alpha - (double) index;
116 if (index >= trc.length - 1)
117 return trc[trc.length - 1];
120 out = (float) (trc[index] * (1.0 - alpha) + trc[index + 1] * alpha);
126 * Performs an reverse lookup
128 public float reverseLookup(float in)
136 return (float) Math.exp((1.0 / gamma) * Math.log(in));
140 double alpha = in * (reverseTrc.length - 1);
141 int index = (int) Math.floor(alpha);
142 alpha = alpha - (double) index;
143 if (index >= reverseTrc.length - 1)
144 return reverseTrc[reverseTrc.length - 1];
146 return reverseTrc[0];
147 out = (float) (reverseTrc[index] * (1.0 - alpha)
148 + reverseTrc[index + 1] * alpha);
154 * Calculates a reverse-lookup table.
155 * We use a whopping 10,000 entries.. This is should be more than any
156 * real-life TRC table (typically around 256-1024) so we won't be losing
159 * This will of course generate completely invalid results if the curve
160 * is not monotonic and invertable. But what's the alternative?
162 public void setupReverseTrc()
164 reverseTrc = new float[10000];
166 for (int i = 0; i < 10000; i++)
168 float n = ((float) i) / 10000f;
169 while (trc[j + 1] < n && j < trc.length - 2)
172 if (j == trc.length - 2)
173 reverseTrc[i] = trc[trc.length - 1];
175 reverseTrc[i] = (j + (n - trc[j]) / (trc[j + 1] - trc[j])) / ((float) trc.length);