aboutsummaryrefslogtreecommitdiffstats
path: root/src/cg-icc.c
blob: 6d18dd4e0624b13495bd932d4ffcbbe78082eebf (plain) (blame)
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/**
 * cg-tools -- Cooperative gamma-enabled tools
 * Copyright (C) 2016  Mattias Andrée (maandree@kth.se)
 * 
 * This program 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 3 of the License, or
 * (at your option) any later version.
 * 
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include "cg-base.h"

#include <libclut.h>

#include <stdio.h>
#include <stdlib.h>



/**
 * Magic number for dual-byte precision lookup table based profiles
 */
#define MLUT_TAG  0x6D4C5554L

/**
 * Magic number for gamma–brightness–contrast based profiles
 * and for variable precision lookup table profiles
 */
#define VCGT_TAG  0x76636774L



/**
 * The default filter priority for the program
 */
const int64_t default_priority = 0;

/**
 * The default class for the program
 */
char* const default_class = PKGNAME "::cg-icc::standard";



/**
 * The panhame of the selected ICC profile
 */
static const char* icc_filepath = NULL;



/**
 * Print usage information and exit
 */
void usage(void)
{
  fprintf(stderr,
	  "Usage: %s [-M method] [-S site] [-c crtc]... [-R rule] "
	  "(-x | [-p priority] [-d] [file])\n",
	  argv0);
  exit(1);
}


/**
 * Handle a command line option
 * 
 * @param   opt  The option, it is a NUL-terminate two-character
 *               string starting with either '-' or '+', if the
 *               argument is not recognised, call `usage`. This
 *               string will not be "-M", "-S", "-c", "-p", or "-R".
 * @param   arg  The argument associated with `opt`,
 *               `NULL` there is no next argument, if this
 *               parameter is `NULL` but needed, call `usage`
 * @return       0 if `arg` was not used,
 *               1 if `arg` was used,
 *               -1 on error
 */
int handle_opt(char* opt, char* arg)
{
  if (opt[0] == '-')
    switch (opt[1])
      {
      case 'd':
	if (dflag || xflag)
	  usage();
	dflag = 1;
	break;
      case 'x':
	if (xflag || dflag)
	  usage();
	xflag = 1;
	break;
      default:
	usage();
      }
  return 0;
}


/**
 * This function is called after the last
 * call to `handle_opt`
 * 
 * @param   argc    The number of unparsed arguments
 * @param   argv    `NULL` terminated list of unparsed arguments
 * @param   method  The argument associated with the "-M" option
 * @param   site    The argument associated with the "-S" option
 * @param   crtcs   The arguments associated with the "-c" options, `NULL`-terminated
 * @param   prio    The argument associated with the "-p" option
 * @param   rule    The argument associated with the "-R" option
 * @return          Zero on success, -1 on error
 */
int handle_args(int argc, char* argv[], char* method, char* site,
		char** crtcs, char* prio, char* rule)
{
  int free_fflag = 0, saved_errno;
  int q = xflag + dflag;
  q += (method != NULL) &&  !strcmp(method, "?");
  q += (prio   != NULL) &&  !strcmp(prio, "?");
  q += (rule   != NULL) && (!strcmp(rule, "?") || !strcmp(rule, "??"));
  for (; *crtcs; crtcs++)
    q += !strcmp(*crtcs, "?");
  if ((q > 1) || (xflag && ((argc > 0) || (prio != NULL))) || (argc > 1))
    usage();
  icc_filepath = *argv;
  return 0;
 fail:
  saved_errno = errno;
  if (free_fflag)
    free(fflag), fflag = NULL;
  errno = saved_errno;
  return cleanup(-1);
}


/**
 * Read an unsigned 64-bit integer
 * 
 * @param   content  The beginning of the encoded integer
 * @return           The integer, decoded
 */
static uint64_t icc_uint64(const char* restrict content)
{
  uint64_t rc;
  rc  = (uint64_t)(unsigned char)(content[0]), rc <<= 8;
  rc |= (uint64_t)(unsigned char)(content[1]), rc <<= 8;
  rc |= (uint64_t)(unsigned char)(content[2]), rc <<= 8;
  rc |= (uint64_t)(unsigned char)(content[3]), rc <<= 8;
  rc |= (uint64_t)(unsigned char)(content[4]), rc <<= 8;
  rc |= (uint64_t)(unsigned char)(content[5]), rc <<= 8;
  rc |= (uint64_t)(unsigned char)(content[6]), rc <<= 8;
  rc |= (uint64_t)(unsigned char)(content[7]);
  return rc;
}


/**
 * Read an unsigned 32-bit integer
 * 
 * @param   content  The beginning of the encoded integer
 * @return           The integer, decoded
 */
static uint32_t icc_uint32(const char* restrict content)
{
  uint32_t rc;
  rc  = (uint32_t)(unsigned char)(content[0]), rc <<= 8;
  rc |= (uint32_t)(unsigned char)(content[1]), rc <<= 8;
  rc |= (uint32_t)(unsigned char)(content[2]), rc <<= 8;
  rc |= (uint32_t)(unsigned char)(content[3]);
  return rc;
}


/**
 * Read an unsigned 16-bit integer
 * 
 * @param   content  The beginning of the encoded integer
 * @return           The integer, decoded
 */
static uint16_t icc_uint16(const char* restrict content)
{
  uint16_t rc;
  rc  = (uint16_t)(unsigned char)(content[0]), rc <<= 8;
  rc |= (uint16_t)(unsigned char)(content[1]);
  return rc;
}


/**
 * Read an unsigned 8-bit integer
 * 
 * @param   content  The beginning of the encoded integer
 * @return           The integer, decoded
 */
static uint16_t icc_uint8(const char* restrict content)
{
  return (uint8_t)(content[0])
}


/**
 * Read a floating-point value
 * 
 * @param   content  The beginning of the encoded value
 * @param   width    The number of bytes with which the value is encoded
 * @return           The value, decoded
 */
static double icc_double(const char* restrict content, size_t width)
{
  double ret = 0;
  size_t i;
  for (i = 0; i < width; i++)
    {
      ret /= 256;
      ret += (double)(unsigned char)(content[width - 1 - i]);
    }
  ret /= 255;
  return ret
}


/**
 * Parse an ICC profile
 * 
 * @param   content  The content of the ICC profile file
 * @param   n        The byte-size of `content`
 * @param   ramps    Output parameter for the filter stored in the ICC profile,
 *                   `.red_size`, `.green_size`, `.blue_size` should already be
 *                   set (these values can however be modified.)
 * @param   depth    Output parameter for ramps stop value type
 * @return           Zero on success, -1 on error, -2 if no usable data is
 *                   available in the profile.
 */
static int parse_icc(const char* restrict content, size_t n, libcoopgamma_ramps_t* ramps,
		     libcoopgamma_depth_t* depth)
{
  uint32_t i_tag, n_tags;
  size_t i, ptr = 0, xptr;
  
  /* Skip header */
  if (n - ptr < 128)
    return -2;
  ptr += 128;
  
  /* Get the number of tags */
  if (n - ptr < 4)
    return -2;
  n_tags = icc_uint32(content + ptr), ptr += 4;
  
  for (i_tag = 0, xptr = ptr; i_tag < n_tags; i_tag++, ptr = xptr)
    {
      uint32_t tag_name, tag_offset, tag_size, gamma_type;
      
      /* Get profile encoding type, offset to the profile and the encoding size of its data */
      if (n - ptr < 12)
	return -2;
      tag_name   = icc_uint32(content + ptr), ptr += 4;
      tag_offset = icc_uint32(content + ptr), ptr += 4;
      tag_size   = icc_uint32(content + ptr), ptr += 4;
      xptr = ptr;
      
      /* Jump to the profile data */
      if (tag_offset > INT32_MAX - tag_size)
	return -2;
      if (tag_offset + tag_size > n)
	return -2;
      ptr = tag_offset;
      
      if (tag_name == MLUT_TAG)
	{
	  /* The profile is encododed as an dual-byte precision lookup table */
	  
	  /* Initialise ramps */
	  *depth = LIBCOOPGAMMA_UINT16;
	  ramps->u16.red_size   = 256;
	  ramps->u16.green_size = 256;
	  ramps->u16.blue_size  = 256;
	  if (libcoopgamma_ramps_initialise(&(ramps->u16)) < 0)
	    return -1;
	  
	  /* Get the lookup table */
	  if (n - ptr < 3 * 256 * 2)
	    continue;
	  for (i = 0; i < 256; i++)
	    ramps->u16.red[i]   = icc_uint16(content + ptr), ptr += 2;
	  for (i = 0; i < 256; i++)
	    ramps->u16.green[i] = icc_uint16(content + ptr), ptr += 2;
	  for (i = 0; i < 256; i++)
	    ramps->u16.blue[i]  = icc_uint16(content + ptr), ptr += 2;
	  
	  return 0;
	}
      else if (tag_name == VCGT_TAG)
	{
	  /* The profile is encoded as with gamma, brightness and contrast values
	   * or as a variable precision lookup table profile */
	  
	  /* VCGT profiles starts where their magic number */
	  if (n - ptr < 4)
	    continue;
	  tag_name = icc_uint32(content + ptr), ptr += 4;
	  if (tag_name == VCGT_TAG)
	    continue;
	  
	  /* Skip four bytes */
	  if (n - ptr < 4)
	    continue;
	  ptr += 4;
	  
	  /* Get the actual encoding type */
	  if (n - ptr < 4)
	    continue;
	  gamma_type = icc_uint32(content + ptr), ptr += 4;
	  
	  if (gamma_type == 0)
	    {
	      /* The profile is encoded as a variable precision lookup table */
	      uint16_t n_channels, n_entries, entry_size;
	      
	      /* Get metadata */
	      if (n - ptr < 3 * 4)
		continue;
	      n_channels = icc_uint32(content + ptr), ptr += 4;
	      n_entries  = icc_uint32(content + ptr), ptr += 4;
	      entry_size = icc_uint32(content + ptr), ptr += 4;
	      if (tag_size == 1584)
		n_channels = 3, n_entries = 256, entry_size = 2;
	      if (n_channels != 3)
		/* Assuming sRGB, can only be an correct assumption if there are exactly three channels */
		continue;
	      
	      /* Check data availability */
	      if (n_channels > SIZE_MAX / n_entries)
		continue;
	      if (entry_size > SIZE_MAX / (n_entries * n_channels))
		continue;
	      if (n - ptr < (size_t)n_channels * (size_t)n_entries * (size_t)entry_size)
		continue;
	      
	      /* Initialise ramps */
	      ramps->u8.red_size   = (size_t)n_entries;
	      ramps->u8.green_size = (size_t)n_entries;
	      ramps->u8.blue_size  = (size_t)n_entries;
	      switch (entry_size)
		{
		case 1:
		  *depth = LIBCOOPGAMMA_UINT8;
		  if (libcoopgamma_ramps_initialise(&(ramps->u8)) < 0)
		    return -1;
		  break;
		case 2:
		  *depth = LIBCOOPGAMMA_UINT16;
		  if (libcoopgamma_ramps_initialise(&(ramps->u16)) < 0)
		    return -1;
		  break;
		case 4:
		  *depth = LIBCOOPGAMMA_UINT32;
		  if (libcoopgamma_ramps_initialise(&(ramps->u32)) < 0)
		    return -1;
		  break;
		case 8:
		  *depth = LIBCOOPGAMMA_UINT64;
		  if (libcoopgamma_ramps_initialise(&(ramps->u64)) < 0)
		    return -1;
		  break;
		default:
		  *depth = LIBCOOPGAMMA_DOUBLE;
		  if (libcoopgamma_ramps_initialise(&(ramps->d)) < 0)
		    return -1;
		  break;
		}
	      
	      /* Get the lookup table */
	      switch (*depth)
		{
		case LIBCOOPGAMMA_UINT8:
		  for (i = 0; i < ramps->u8.red_size;   i++)
		    ramps->u8.red[i]   = icc_uint8(content + ptr), ptr += 1;
		  for (i = 0; i < ramps->u8.green_size; i++)
		    ramps->u8.green[i] = icc_uint8(content + ptr), ptr += 1;
		  for (i = 0; i < ramps->u8.blue_size;  i++)
		    ramps->u8.blue[i]  = icc_uint8(content + ptr), ptr += 1;
		  break;
		case LIBCOOPGAMMA_UINT16:
		  for (i = 0; i < ramps->u16.red_size;   i++)
		    ramps->u16.red[i]   = icc_uint16(content + ptr), ptr += 2;
		  for (i = 0; i < ramps->u16.green_size; i++)
		    ramps->u16.green[i] = icc_uint16(content + ptr), ptr += 2;
		  for (i = 0; i < ramps->u16.blue_size;  i++)
		    ramps->u16.blue[i]  = icc_uint16(content + ptr), ptr += 2;
		  break;
		case LIBCOOPGAMMA_UINT32:
		  for (i = 0; i < ramps->u32.red_size;   i++)
		    ramps->u32.red[i]   = icc_uint32(content + ptr), ptr += 4;
		  for (i = 0; i < ramps->u32.green_size; i++)
		    ramps->u32.green[i] = icc_uint32(content + ptr), ptr += 4;
		  for (i = 0; i < ramps->u32.blue_size;  i++)
		    ramps->u32.blue[i]  = icc_uint32(content + ptr), ptr += 4;
		  break;
		case LIBCOOPGAMMA_UINT64:
		  for (i = 0; i < ramps->u64.red_size;   i++)
		    ramps->u64.red[i]   = icc_uint64(content + ptr), ptr += 8;
		  for (i = 0; i < ramps->u64.green_size; i++)
		    ramps->u64.green[i] = icc_uint64(content + ptr), ptr += 8;
		  for (i = 0; i < ramps->u64.blue_size;  i++)
		    ramps->u64.blue[i]  = icc_uint64(content + ptr), ptr += 8;
		  break;
		default:
		  for (i = 0; i < ramps->d.red_size;   i++)
		    ramps->d.red[i]   = icc_double(content + ptr, entry_size), ptr += entry_size;
		  for (i = 0; i < ramps->d.green_size; i++)
		    ramps->d.green[i] = icc_double(content + ptr, entry_size), ptr += entry_size;
		  for (i = 0; i < ramps->d.blue_size;  i++)
		    ramps->d.blue[i]  = icc_double(content + ptr, entry_size), ptr += entry_size;
		  break;
		}
	      
	      return 0;
	    }
	  else if (gamma_type == 1)
	    {
	      /* The profile is encoded with gamma, brightness and contrast values */
	      double r_gamma, r_min, r_max, g_gamma, g_min, g_max, b_gamma, b_min, b_max;
	      
	      /* Get the gamma, brightness and contrast */
	      if (n - ptr < 9 * 4)
		continue;
	      r_gamma = (double)icc_uint32(content + ptr) / 65536L, ptr += 4;
	      r_min   = (double)icc_uint32(content + ptr) / 65536L, ptr += 4;
	      r_max   = (double)icc_uint32(content + ptr) / 65536L, ptr += 4;
	      g_gamma = (double)icc_uint32(content + ptr) / 65536L, ptr += 4;
	      g_min   = (double)icc_uint32(content + ptr) / 65536L, ptr += 4;
	      g_max   = (double)icc_uint32(content + ptr) / 65536L, ptr += 4;
	      b_gamma = (double)icc_uint32(content + ptr) / 65536L, ptr += 4;
	      b_min   = (double)icc_uint32(content + ptr) / 65536L, ptr += 4;
	      b_max   = (double)icc_uint32(content + ptr) / 65536L, ptr += 4;
	      
	      /* Initialise ramps */
	      *depth = LIBCOOPGAMMA_DOUBLE;
	      if (libcoopgamma_ramps_initialise(&(ramps->d)) < 0)
		return -1;
	      
	      /* Set ramps */
	      libclut_start_over(&(ramps->d), (double)1, double, 1, 1, 1);
	      libclut_gamma(&(ramps->d), (double)1, double, r_gamma, g_gamma, b_gamma);
	      libclut_rgb_limits(&(ramps->d), (double)1, double, r_min, r_max, g_min, g_max, b_min, b_max);
	      
	      return 0;
	    }
	}
    }
  
  return -2;
}