summaryrefslogtreecommitdiffstats
path: root/libjava/classpath/gnu/java/awt/color/ColorLookUpTable.java
blob: 6b6418bc28496a301955dd7c472e5ca5374eb7e8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* ColorLookUpTable.java -- ICC v2 CLUT
   Copyright (C) 2004 Free Software Foundation

This file is part of GNU Classpath.

GNU Classpath is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

GNU Classpath is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.

You should have received a copy of the GNU General Public License
along with GNU Classpath; see the file COPYING.  If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301 USA.

Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.

As a special exception, the copyright holders of this library give you
permission to link this library with independent modules to produce an
executable, regardless of the license terms of these independent
modules, and to copy and distribute the resulting executable under
terms of your choice, provided that you also meet, for each linked
independent module, the terms and conditions of the license of that
module.  An independent module is a module which is not derived from
or based on this library.  If you modify this library, you may extend
this exception to your version of the library, but you are not
obligated to do so.  If you do not wish to do so, delete this
exception statement from your version. */

package gnu.java.awt.color;

import java.awt.color.ColorSpace;
import java.awt.color.ICC_Profile;
import java.nio.ByteBuffer;


/**
 * ColorLookUpTable handles color lookups through a color lookup table,
 * as defined in the ICC specification.
 * Both 'mft2' and 'mft1' (8 and 16-bit) type CLUTs are handled.
 *
 * This will have to be updated later for ICC 4.0.0
 *
 * @author Sven de Marothy
 */
public class ColorLookUpTable
{
  /**
   * CIE 1931 D50 white point (in Lab coordinates)
   */
  private static float[] D50 = { 0.96422f, 1.00f, 0.82521f };

  /**
   * Number of input/output channels
   */
  int nIn;

  /**
   * Number of input/output channels
   */
  int nOut;
  int nInTableEntries; // Number of input table entries
  int nOutTableEntries; // Number of output table entries
  int gridpoints; // Number of gridpoints
  int nClut; // This is nOut*(gridpoints**nIn)
  double[][] inTable; // 1D input table ([channel][table])
  short[][] outTable; // 1D input table ([channel][table])
  double[] clut; // The color lookup table
  float[][] inMatrix; // input matrix (XYZ only)
  boolean useMatrix; // Whether to use the matrix or not.
  int[] multiplier;
  int[] offsets; // Hypercube offsets 
  boolean inputLab; // Set if the CLUT input CS is Lab
  boolean outputLab; // Set if the CLUT output CS is Lab

  /**
   * Constructor
   * Requires a profile file to get the CLUT from and the tag of the
   * CLUT to create. (icSigXToYZTag where X,Y = [A | B], Z = [0,1,2])
   */
  public ColorLookUpTable(ICC_Profile profile, int tag)
  {
    useMatrix = false;

    switch (tag)
      {
      case ICC_Profile.icSigAToB0Tag:
      case ICC_Profile.icSigAToB1Tag:
      case ICC_Profile.icSigAToB2Tag:
	if (profile.getColorSpaceType() == ColorSpace.TYPE_XYZ)
	  useMatrix = true;
	inputLab = false;
	outputLab = (profile.getPCSType() == ColorSpace.TYPE_Lab);
	break;
      case ICC_Profile.icSigBToA0Tag:
      case ICC_Profile.icSigBToA1Tag:
      case ICC_Profile.icSigBToA2Tag:
	if (profile.getPCSType() == ColorSpace.TYPE_XYZ)
	  useMatrix = true;
	inputLab = (profile.getPCSType() == ColorSpace.TYPE_Lab);
	outputLab = false;
	break;
      default:
	throw new IllegalArgumentException("Not a clut-type tag.");
      }

    byte[] data = profile.getData(tag);
    if (data == null)
      throw new IllegalArgumentException("Unsuitable profile, does not contain a CLUT.");

    // check 'mft'
    if (data[0] != 0x6d || data[1] != 0x66 || data[2] != 0x74)
      throw new IllegalArgumentException("Unsuitable profile, invalid CLUT data.");

    if (data[3] == 0x32)
      readClut16(data);
    else if (data[3] == 0x31)
      readClut8(data);
    else
      throw new IllegalArgumentException("Unknown/invalid CLUT type.");
  }

  /**
   * Loads a 16-bit CLUT into our data structures
   */
  private void readClut16(byte[] data)
  {
    ByteBuffer buf = ByteBuffer.wrap(data);

    nIn = data[8] & (0xFF);
    nOut = data[9] & (0xFF);
    nInTableEntries = buf.getShort(48);
    nOutTableEntries = buf.getShort(50);
    gridpoints = data[10] & (0xFF);

    inMatrix = new float[3][3];
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
	inMatrix[i][j] = ((float) (buf.getInt(12 + (i * 3 + j) * 4))) / 65536.0f;

    inTable = new double[nIn][nInTableEntries];
    for (int channel = 0; channel < nIn; channel++)
      for (int i = 0; i < nInTableEntries; i++)
	inTable[channel][i] = (double) ((int) buf.getShort(52
	                                                   + (channel * nInTableEntries
	                                                   + i) * 2)
	                      & (0xFFFF)) / 65536.0;

    nClut = nOut;
    multiplier = new int[nIn];
    multiplier[nIn - 1] = nOut;
    for (int i = 0; i < nIn; i++)
      {
	nClut *= gridpoints;
	if (i > 0)
	  multiplier[nIn - i - 1] = multiplier[nIn - i] * gridpoints;
      }

    int clutOffset = 52 + nIn * nInTableEntries * 2;
    clut = new double[nClut];
    for (int i = 0; i < nClut; i++)
      clut[i] = (double) ((int) buf.getShort(clutOffset + i * 2) & (0xFFFF)) / 65536.0;

    outTable = new short[nOut][nOutTableEntries];
    for (int channel = 0; channel < nOut; channel++)
      for (int i = 0; i < nOutTableEntries; i++)
	outTable[channel][i] = buf.getShort(clutOffset
	                                    + (nClut
	                                    + channel * nOutTableEntries + i) * 2);

    // calculate the hypercube corner offsets
    offsets = new int[(1 << nIn)];
    offsets[0] = 0;
    for (int j = 0; j < nIn; j++)
      {
	int factor = 1 << j;
	for (int i = 0; i < factor; i++)
	  offsets[factor + i] = offsets[i] + multiplier[j];
      }
  }

  /**
   * Loads a 8-bit CLUT into our data structures.
   */
  private void readClut8(byte[] data)
  {
    ByteBuffer buf = ByteBuffer.wrap(data);

    nIn = (data[8] & (0xFF));
    nOut = (data[9] & (0xFF));
    nInTableEntries = 256; // always 256
    nOutTableEntries = 256; // always 256
    gridpoints = (data[10] & (0xFF));

    inMatrix = new float[3][3];
    for (int i = 0; i < 3; i++)
      for (int j = 0; j < 3; j++)
	inMatrix[i][j] = ((float) (buf.getInt(12 + (i * 3 + j) * 4))) / 65536.0f;

    inTable = new double[nIn][nInTableEntries];
    for (int channel = 0; channel < nIn; channel++)
      for (int i = 0; i < nInTableEntries; i++)
	inTable[channel][i] = (double) ((int) buf.get(48
	                                              + (channel * nInTableEntries
	                                              + i)) & (0xFF)) / 255.0;

    nClut = nOut;
    multiplier = new int[nIn];
    multiplier[nIn - 1] = nOut;
    for (int i = 0; i < nIn; i++)
      {
	nClut *= gridpoints;
	if (i > 0)
	  multiplier[nIn - i - 1] = multiplier[nIn - i] * gridpoints;
      }

    int clutOffset = 48 + nIn * nInTableEntries;
    clut = new double[nClut];
    for (int i = 0; i < nClut; i++)
      clut[i] = (double) ((int) buf.get(clutOffset + i) & (0xFF)) / 255.0;

    outTable = new short[nOut][nOutTableEntries];
    for (int channel = 0; channel < nOut; channel++)
      for (int i = 0; i < nOutTableEntries; i++)
	outTable[channel][i] = (short) (buf.get(clutOffset + nClut
	                                        + channel * nOutTableEntries
	                                        + i) * 257);

    // calculate the hypercube corner offsets
    offsets = new int[(1 << nIn)];
    offsets[0] = 0;
    for (int j = 0; j < nIn; j++)
      {
	int factor = 1 << j;
	for (int i = 0; i < factor; i++)
	  offsets[factor + i] = offsets[i] + multiplier[j];
      }
  }

  /**
   * Performs a lookup through the Color LookUp Table.
   * If the CLUT tag type is AtoB the conversion will be from the device
   * color space to the PCS, BtoA type goes in the opposite direction.
   *
   * For convenience, the PCS values for input or output will always be
   * CIE XYZ (D50), if the actual PCS is Lab, the values will be converted.
   *
   * N-dimensional linear interpolation is used.
   */
  float[] lookup(float[] in)
  {
    float[] in2 = new float[in.length];
    if (useMatrix)
      {
	for (int i = 0; i < 3; i++)
	  in2[i] = in[0] * inMatrix[i][0] + in[1] * inMatrix[i][1]
	           + in[2] * inMatrix[i][2];
      }
    else if (inputLab)
      in2 = XYZtoLab(in);
    else
      System.arraycopy(in, 0, in2, 0, in.length);

    // input table 
    for (int i = 0; i < nIn; i++)
      {
	int index = (int) Math.floor(in2[i] * (double) (nInTableEntries - 1)); // floor in

	// clip values.
	if (index >= nInTableEntries - 1)
	  in2[i] = (float) inTable[i][nInTableEntries - 1];
	else if (index < 0)
	  in2[i] = (float) inTable[i][0];
	else
	  {
	    // linear interpolation
	    double alpha = in2[i] * ((double) nInTableEntries - 1.0) - index;
	    in2[i] = (float) (inTable[i][index] * (1 - alpha)
	             + inTable[i][index + 1] * alpha);
	  }
      }

    // CLUT lookup
    double[] output2 = new double[nOut];
    double[] weights = new double[(1 << nIn)];
    double[] clutalpha = new double[nIn]; // interpolation values
    int offset = 0; // = gp
    for (int i = 0; i < nIn; i++)
      {
	int index = (int) Math.floor(in2[i] * ((double) gridpoints - 1.0));
	double alpha = in2[i] * ((double) gridpoints - 1.0) - (double) index;

	// clip values.
	if (index >= gridpoints - 1)
	  {
	    index = gridpoints - 1;
	    alpha = 1.0;
	  }
	else if (index < 0)
	  index = 0;
	clutalpha[i] = alpha;
	offset += index * multiplier[i];
      }

    // Calculate interpolation weights
    weights[0] = 1.0;
    for (int j = 0; j < nIn; j++)
      {
	int factor = 1 << j;
	for (int i = 0; i < factor; i++)
	  {
	    weights[factor + i] = weights[i] * clutalpha[j];
	    weights[i] *= (1.0 - clutalpha[j]);
	  }
      }

    for (int i = 0; i < nOut; i++)
      output2[i] = weights[0] * clut[offset + i];

    for (int i = 1; i < (1 << nIn); i++)
      {
	int offset2 = offset + offsets[i];
	for (int f = 0; f < nOut; f++)
	  output2[f] += weights[i] * clut[offset2 + f];
      }

    // output table 
    float[] output = new float[nOut];
    for (int i = 0; i < nOut; i++)
      {
	int index = (int) Math.floor(output2[i] * ((double) nOutTableEntries
	                             - 1.0));

	// clip values.
	if (index >= nOutTableEntries - 1)
	  output[i] = outTable[i][nOutTableEntries - 1];
	else if (index < 0)
	  output[i] = outTable[i][0];
	else
	  {
	    // linear interpolation
	    double a = output2[i] * ((double) nOutTableEntries - 1.0)
	               - (double) index;
	    output[i] = (float) ((double) ((int) outTable[i][index] & (0xFFFF)) * (1
	                - a)
	                + (double) ((int) outTable[i][index + 1] & (0xFFFF)) * a) / 65536f;
	  }
      }

    if (outputLab)
      return LabtoXYZ(output);
    return output;
  }

  /**
   * Converts CIE Lab coordinates to (D50) XYZ ones.
   */
  private float[] LabtoXYZ(float[] in)
  {
    // Convert from byte-packed format to a 
    // more convenient one (actual Lab values)
    // (See ICC spec for details)
    // factor is 100 * 65536 / 65280
    in[0] = (float) (100.392156862745 * in[0]);
    in[1] = (in[1] * 256.0f) - 128.0f;
    in[2] = (in[2] * 256.0f) - 128.0f;

    float[] out = new float[3];

    out[1] = (in[0] + 16.0f) / 116.0f;
    out[0] = in[1] / 500.0f + out[1];
    out[2] = out[1] - in[2] / 200.0f;

    for (int i = 0; i < 3; i++)
      {
	double exp = out[i] * out[i] * out[i];
	if (exp <= 0.008856)
	  out[i] = (out[i] - 16.0f / 116.0f) / 7.787f;
	else
	  out[i] = (float) exp;
	out[i] = D50[i] * out[i];
      }
    return out;
  }

  /**
   * Converts CIE XYZ coordinates to Lab ones.
   */
  private float[] XYZtoLab(float[] in)
  {
    float[] temp = new float[3];

    for (int i = 0; i < 3; i++)
      {
	temp[i] = in[i] / D50[i];

	if (temp[i] <= 0.008856f)
	  temp[i] = (7.7870689f * temp[i]) + (16f / 116.0f);
	else
	  temp[i] = (float) Math.exp((1.0 / 3.0) * Math.log(temp[i]));
      }

    float[] out = new float[3];
    out[0] = (116.0f * temp[1]) - 16f;
    out[1] = 500.0f * (temp[0] - temp[1]);
    out[2] = 200.0f * (temp[1] - temp[2]);

    // Normalize to packed format
    out[0] = (float) (out[0] / 100.392156862745);
    out[1] = (out[1] + 128f) / 256f;
    out[2] = (out[2] + 128f) / 256f;
    for (int i = 0; i < 3; i++)
      {
	if (out[i] < 0f)
	  out[i] = 0f;
	if (out[i] > 1f)
	  out[i] = 1f;
      }
    return out;
  }
}
OpenPOWER on IntegriCloud