summaryrefslogtreecommitdiff
path: root/tools/babl-icc-dump.c
blob: b90590bba9d8e2b2f57bde10816eb46d58fa54e2 (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
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
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>

static int
file_get_contents (const char  *path,
                   char       **contents,
                   long        *length,
                   void        *error);

typedef struct {
  int16_t  integer;
  uint16_t fraction;
} s15f16_t;

typedef struct {
  int16_t  integer;
  uint16_t fraction;
} u8f8_t;

#define ICC_HEADER_LEN 128
#define TAG_COUNT_OFF  ICC_HEADER_LEN

static int 
load_u8 (const char *icc, 
         int         length, 
         int         offset)
{
/* all reading functions take both the char *pointer and the length of the
 * buffer, and all reads thus gets protected by this condition.
 */
  if (offset < 0 || offset > length)
    return 0;

  return *(uint8_t*) (&icc[offset]);
}

static int 
load_s8 (const char *icc, 
         int         length, 
         int         offset)
{
  if (offset < 0 || offset > length)
    return 0;

  return *(int8_t*) (&icc[offset]);
}

static int16_t 
load_u1f15 (const char *icc, 
            int         length, 
            int         offset)
{
  return load_u8 (icc, length, offset + 1) +
         (load_s8 (icc, length, offset + 0) << 8);
}

static uint16_t 
load_u16 (const char *icc, 
          int         length,  
          int         offset)
{
  return load_u8 (icc, length, offset + 1) +
         (load_u8 (icc, length, offset + 0) << 8);
}

static u8f8_t 
load_u8f8_ (const char *icc, 
            int         length, 
            int         offset)
{
  u8f8_t ret ={load_u8 (icc, length, offset),
               load_u8 (icc, length, offset + 1)};
  return ret;
}

static s15f16_t 
load_s15f16_ (const char *icc, 
              int         length, 
              int         offset)
{
  s15f16_t ret ={load_u1f15 (icc, length, offset),
                 load_u16 (icc, length, offset + 2)};
  return ret;
}

static double 
s15f16_to_d (s15f16_t fix)
{
  return fix.integer + fix.fraction / 65535.0;
}

static double 
u8f8_to_d (u8f8_t fix)
{
  return fix.integer + fix.fraction / 255.0;
}

static double 
load_s15f16 (const char *icc, 
             int         length, 
             int         offset)
{
  return s15f16_to_d (load_s15f16_ (icc, length, offset));
}

static double 
load_u8f8 (const char *icc, 
           int         length, 
           int         offset)
{
  return u8f8_to_d (load_u8f8_ (icc, length, offset));
}

static void 
print_u8f8 (u8f8_t fix)
{
  int i;
  uint32_t foo;
  foo = fix.fraction;
  fprintf (stdout, "%i.", fix.integer);
  for (i = 0; i < 18; i++)
  {
    foo *= 10;
    fprintf (stdout, "%i", (foo / 256) % 10);
    foo = foo & 0xff;
  }
}

static void 
print_s15f16 (s15f16_t fix)
{
  int i;
  uint32_t foo;
  foo = fix.fraction;
  if (fix.integer < 0)
  {
    if (fix.integer == -1)
      fprintf (stdout, "-");
    fprintf (stdout, "%i.", fix.integer + 1);
    foo = 65535-fix.fraction;
    for (i = 0; i < 18; i++)
    {
      foo *= 10;
      fprintf (stdout, "%i", (foo / 65536) % 10);
      foo = foo & 0xffff;
    }
  }
  else
  {
  fprintf (stdout, "%i.", fix.integer);
  for (i = 0; i < 18; i++)
  {
    foo *= 10;
    fprintf (stdout, "%i", (foo / 65536) % 10);
    foo = foo & 0xffff;
  }
  }
}

static uint32_t 
load_u32 (const char *icc, 
          int         length, 
          int         offset)
{
  return load_u8 (icc, length, offset + 3) +
         (load_u8 (icc, length, offset + 2) << 8) +
         (load_u8 (icc, length, offset + 1) << 16) +
         (load_u8 (icc, length, offset + 0) << 24);
}

static void 
load_sign (const char *icc, 
           int         length,
           int         offset, 
           char       *sign)
{
  sign[0]=load_u8(icc, length, offset);
  sign[1]=load_u8(icc, length, offset + 1);
  sign[2]=load_u8(icc, length, offset + 2);
  sign[3]=load_u8(icc, length, offset + 3);
  sign[4]=0;
}

/* looks up offset and length for a specific icc tag
 */
static int 
icc_tag (const char *icc, 
         int         length,
         const char *tag, 
         int        *offset, 
         int        *el_length)
{
  int tag_count = load_u32 (icc, length, TAG_COUNT_OFF);
  int t;

  for (t =  0; t < tag_count; t++)
  {
     char tag_signature[5];
     load_sign (icc, length, TAG_COUNT_OFF + 4 + 12 * t, tag_signature);
     if (!strcmp (tag_signature, tag))
     {
        if (offset)
          *offset = load_u32 (icc, length, TAG_COUNT_OFF + 4 + 12* t + 4);
        if (el_length)
          *el_length = load_u32 (icc, length, TAG_COUNT_OFF + 4 + 12* t + 4*2);
        return 1;
     }
  }
  return 0;
}

#if 0

#define ICC_HEADER_LEN 128
#define TAG_COUNT_OFF  ICC_HEADER_LEN

static int load_u8 (const char *icc, int offset)
{
  return *(uint8_t*) (&icc[offset]);
}

static int16_t load_u1Fixed15 (const char *icc, int offset)
{
  return load_u8 (icc, offset + 1) +
         (load_u8 (icc, offset + 0) << 8);
}

static uint16_t load_u16 (const char *icc, int offset)
{
  return load_u8 (icc, offset + 1) +
         (load_u8 (icc, offset + 0) << 8);
}

static double load_s15f16 (const char *icc, int offset)
{
  return load_u1Fixed15 (icc, offset) + load_u16 (icc, offset + 2) / 65535.0f;
}

static double load_u16f16 (const char *icc, int offset)
{
  return load_u16 (icc, offset) + load_u16 (icc, offset + 2) / 65535.0;
}

static uint32_t load_u32 (const char *icc, int offset)
{
  return load_u8 (icc, offset + 3) +
         (load_u8 (icc, offset + 2) << 8) +
         (load_u8 (icc, offset + 1) << 16) +
         (load_u8 (icc, offset + 0) << 24);
}

static float load_float32 (const char *icc, int offset)
{
  char buf[4]={load_u8 (icc, offset + 3),
         load_u8 (icc, offset + 2),
         load_u8 (icc, offset + 1),
         load_u8 (icc, offset + 0)};
  float *val = (float*)(&buf[0]);
  return *val;
}

static uint64_t load_uint64 (const char *icc, int offset)
{
  return ((uint64_t)load_u8 (icc, offset + 7) << (8*0)) +
         ((uint64_t)load_u8 (icc, offset + 6) << (8*1)) +
         ((uint64_t)load_u8 (icc, offset + 5) << (8*2)) +
         ((uint64_t)load_u8 (icc, offset + 4) << (8*3)) +
         ((uint64_t)load_u8 (icc, offset + 3) << (8*4)) +
         ((uint64_t)load_u8 (icc, offset + 2) << (8*5)) +
         ((uint64_t)load_u8 (icc, offset + 1) << (8*6)) +
         ((uint64_t)load_u8 (icc, offset + 0) << (8*7));
}

static void load_sign (const char *icc, int offset, char *sign)
{
  sign[0]=load_u8(icc, offset);
  sign[1]=load_u8(icc, offset + 1);
  sign[2]=load_u8(icc, offset + 2);
  sign[3]=load_u8(icc, offset + 3);
  sign[4]=0;
}

static int icc_tag (const char *icc, const char *tag, int *offset, int *length)
{
  int tag_count = load_u32 (icc, TAG_COUNT_OFF);
  int profile_size = load_u32 (icc, 0);
  int t;

  for (t =  0; t < tag_count; t++)
  {
     char tag_signature[5];
     load_sign (icc, TAG_COUNT_OFF + 4 + 12 * t, tag_signature);
     if (!strcmp (tag_signature, tag))
     {
        *offset = load_u32 (icc, TAG_COUNT_OFF + 4 + 12* t + 4);
        *length = load_u32 (icc, TAG_COUNT_OFF + 4 + 12* t + 4 * 2);
        /* avert potential for maliciousnes.. */
        if (*offset >= profile_size)
          {
            *offset = profile_size - 1;
          }
        if (*offset + *length >= profile_size)
          {
            *length = profile_size - *offset - 1;
          }
        return 1;
     }
  }
  return 0;
}
#endif

int exact = 0;

static int 
load_icc_from_memory (const char  *icc, 
                      long         length, 
                      char       **error)
{
  int  tag_count         = load_u32 (icc, length, TAG_COUNT_OFF);
  int  profile_size      = load_u32 (icc, length, 0);
  int  profile_version_major = load_u8 (icc, length, 8);
  int  profile_version_minor = load_u8 (icc, length, 9) >> 4;
  int  profile_version_micro = load_u8 (icc, length, 9) & 0xf;
  char profile_class[5];
  char color_space[5];
  char pcs_space[5];
  int rendering_intent = load_u32 (icc, length, 64);
  int t;
  // 64..67 rendering intent
  // 68..79 XYZ of D50

  load_sign (icc, length, 16, color_space);
  load_sign (icc, length, 20, pcs_space);
  load_sign (icc, length, 12, profile_class);

  if (strcmp (profile_class, "mntr"))
  {
    *error = "not a monitor-class profile";
    return -1;
  }
  if (strcmp (color_space, "RGB "))
  {
    *error = "not defining an RGB space";
    return -1;
  }
#if 0
  if (profile_version_major > 2)
  {
    *error = "only ICC v2 profiles supported";
    return -1;
  }
#endif
  {
     int offset, element_size;
     icc_tag (icc, length, "desc", &offset, &element_size);
     if (!strcmp (icc + offset, "mluc"))
     {
       fprintf (stdout, "desc: [babl-icc doesnt decode unicode]\n");
     }
     else
     if (!strcmp (icc + offset, "desc"))
       fprintf (stdout, "desc: %s\n", icc + offset + 12);
  }
  {
     int offset, element_size;
     icc_tag (icc, length, "cprt", &offset, &element_size);
     fprintf (stdout, "copyright: %s\n", icc + offset + 8);
  }

#if 1
  fprintf (stdout, "icc version: %i.%i.%i\n", profile_version_major, profile_version_minor, profile_version_micro);
  fprintf (stdout, "profile-size: %i\n", profile_size);
  fprintf (stdout, "profile-class: %s\n", profile_class);
  fprintf (stdout, "color-space: %s\n", color_space);
  fprintf (stdout, "rendering-intent: %i\n", rendering_intent);
  fprintf (stdout, "pcs-space: %s\n", pcs_space);
  fprintf (stdout, "length: %li\n", length);
#if 0
  fprintf (stdout, "tag-count: %i\n", tag_count);

  for (t =  0; t < tag_count; t++)
  {
     char tag_signature[5];
     int offset, element_size;
     load_sign (icc, length, TAG_COUNT_OFF + 4 + 12 * t, tag_signature);
     icc_tag (icc, length, tag_signature, &offset, &element_size);
     fprintf (stdout, "tag %i %s %i %i\n", t, tag_signature, offset, element_size);
  }
#endif
#endif
  fprintf (stdout, "tags: ");
  for (t =  0; t < tag_count; t++)
  {
     char tag_signature[5];
     int offset, element_size;
     load_sign (icc, length, TAG_COUNT_OFF + 4 + 12 * t, tag_signature);
     icc_tag (icc, length, tag_signature, &offset, &element_size);
     fprintf (stdout, "%s[%i@%i] ", tag_signature, element_size, offset);
  }
  fprintf (stdout, "\n");
  fprintf (stdout, "\n");


  {
     int offset, element_size;
     if (icc_tag (icc, length, "chrm", &offset, &element_size))
     {
     int channels = load_u16 (icc, length, offset + 8);
     int phosporant = load_u16 (icc, length, offset + 10);
     double redX   = load_s15f16 (icc, length, offset + 12);
     double redY   = load_s15f16 (icc, length, offset + 12 + 4);
     double greenX = load_s15f16 (icc, length, offset + 20);
     double greenY = load_s15f16 (icc, length, offset + 20 + 4);
     double blueX  = load_s15f16 (icc, length, offset + 28);
     double blueY  = load_s15f16 (icc, length, offset + 28 + 4);
     fprintf (stdout, "chromaticity:\n");
     fprintf (stdout, "  channels: %i\n", channels);
     fprintf (stdout, "  phosphorant: %i\n", phosporant);
     fprintf (stdout, "  CIE xy red:   %.6f %.6f\n", redX, redY);
     fprintf (stdout, "  CIE xy green: %.6f %.6f\n", greenX, greenY);
     fprintf (stdout, "  CIE xy blue:  %.6f %.6f\n", blueX, blueY);
     if (exact)
     {
        fprintf (stdout, "  exact:        ");
        print_s15f16 (load_s15f16_ (icc, length, offset + 12));
        fprintf (stdout, " ");
        print_s15f16 (load_s15f16_ (icc, length, offset + 12 + 4));
        fprintf (stdout, "\n");

        fprintf (stdout, "                ");
        print_s15f16 (load_s15f16_ (icc, length, offset + 20));
        fprintf (stdout, " ");
        print_s15f16 (load_s15f16_ (icc, length, offset + 20 + 4));
        fprintf (stdout, "\n");

        fprintf (stdout, "                ");
        print_s15f16 (load_s15f16_ (icc, length, offset + 28));
        fprintf (stdout, " ");
        print_s15f16 (load_s15f16_ (icc, length, offset + 28 + 4));
        fprintf (stdout, "\n");
     }

     }
  }

  {
     int offset, element_size;
     if (icc_tag (icc, length, "bkpt", &offset, &element_size))
     {
       double wX = load_s15f16 (icc, length, offset + 8);
       double wY = load_s15f16 (icc, length, offset + 8 + 4);
       double wZ = load_s15f16 (icc, length, offset + 8 + 4 * 2);
       fprintf (stdout,    "blackpoint CIE XYZ: %.6f %.6f %.6f\n", wX, wY, wZ);

       if (exact)
       {
          fprintf (stdout, "exact:              ");
          print_s15f16 (load_s15f16_ (icc, length, offset + 8));
          fprintf (stdout, " ");
          print_s15f16 (load_s15f16_ (icc, length, offset + 8 + 4));
          fprintf (stdout, " ");
          print_s15f16 (load_s15f16_ (icc, length, offset + 8 + 4 * 2));
          fprintf (stdout, "\n");
       }
     }

  }

  {
     int offset, element_size;
     if (icc_tag (icc, length, "wtpt", &offset, &element_size))
     {
       double wX = load_s15f16 (icc, length, offset + 8);
       double wY = load_s15f16 (icc, length, offset + 8 + 4);
       double wZ = load_s15f16 (icc, length, offset + 8 + 4 * 2);
       fprintf (stdout,    "whitepoint CIE XYZ: %.6f %.6f %.6f\n", wX, wY, wZ);

       if (exact)
       {
          fprintf (stdout, "exact:              ");
          print_s15f16 (load_s15f16_ (icc, length, offset + 8));
          fprintf (stdout, " ");
          print_s15f16 (load_s15f16_ (icc, length, offset + 8 + 4));
          fprintf (stdout, " ");
          print_s15f16 (load_s15f16_ (icc, length, offset + 8 + 4 * 2));
          fprintf (stdout, "\n");
       }

     }
  }


  {
     int offset, element_size;
     if (icc_tag (icc, length, "rXYZ", &offset, &element_size))
     {
       double wX = load_s15f16 (icc, length, offset + 8);
       double wY = load_s15f16 (icc, length, offset + 8 + 4);
       double wZ = load_s15f16 (icc, length, offset + 8 + 4 * 2);
       fprintf (stdout, "red        CIE XYZ: %.6f %.6f %.6f\n", wX, wY, wZ);
     }
     if (icc_tag (icc, length, "gXYZ", &offset, &element_size))
     {
       double wX = load_s15f16 (icc, length, offset + 8);
       double wY = load_s15f16 (icc, length, offset + 8 + 4);
       double wZ = load_s15f16 (icc, length, offset + 8 + 4 * 2);
       fprintf (stdout, "green      CIE XYZ: %.6f %.6f %.6f\n", wX, wY, wZ);
     }
     if (icc_tag (icc, length, "bXYZ", &offset, &element_size))
     {
       double wX = load_s15f16 (icc, length, offset + 8);
       double wY = load_s15f16 (icc, length, offset + 8 + 4);
       double wZ = load_s15f16 (icc, length, offset + 8 + 4 * 2);
       fprintf (stdout, "blue       CIE XYZ: %.6f %.6f %.6f\n", wX, wY, wZ);
     }
  }
  if(exact){
     int offset, element_size;
     if (icc_tag (icc, length, "rXYZ", &offset, &element_size))
     {
       fprintf (stdout, "exact:              ");
       print_s15f16 (load_s15f16_ (icc, length, offset + 8));
       fprintf (stdout, " ");
       print_s15f16 (load_s15f16_ (icc, length, offset + 8 + 4));
       fprintf (stdout, " ");
       print_s15f16 (load_s15f16_ (icc, length, offset + 8 + 4 * 2));
       fprintf (stdout, "\n");
     }
     if (icc_tag (icc, length, "gXYZ", &offset, &element_size))
     {
       fprintf (stdout, "                    ");
       print_s15f16 (load_s15f16_ (icc, length, offset + 8));
       fprintf (stdout, " ");
       print_s15f16 (load_s15f16_ (icc, length, offset + 8 + 4));
       fprintf (stdout, " ");
       print_s15f16 (load_s15f16_ (icc, length, offset + 8 + 4 * 2));
       fprintf (stdout, "\n");
     }
     if (icc_tag (icc, length, "bXYZ", &offset, &element_size))
     {
       fprintf (stdout, "                    ");
       print_s15f16 (load_s15f16_ (icc, length, offset + 8));
       fprintf (stdout, " ");
       print_s15f16 (load_s15f16_ (icc, length, offset + 8 + 4));
       fprintf (stdout, " ");
       print_s15f16 (load_s15f16_ (icc, length, offset + 8 + 4 * 2));
       fprintf (stdout, "\n");
     }
  }

  if (1) {
     int offset, element_size;
     if (icc_tag (icc, length, "rTRC", &offset, &element_size))
     {
       int count = load_u32 (icc, length, offset + 8);
       int i;
       if (!strcmp (icc + offset, "para"))
       {
         int function_type = load_u16 (icc, length, offset + 8);
         float g,a,b,c,d,e,f;
         switch (function_type)
         {
            case 0:
              g = load_s15f16 (icc, length, offset + 12 + 2 * 0);
              fprintf (stdout, "parametric TRC gamma type %.6f\n", g);
              if (exact)
              {
              fprintf (stdout, "    exact:");
              print_s15f16 (load_s15f16_ (icc, length, offset + 12 + 2 * 0));
              fprintf (stdout, "\n");
              }

              break;

            case 3:
              g = load_s15f16 (icc, length, offset + 12 + 2 * 0);
              a = load_s15f16 (icc, length, offset + 12 + 2 * 1);
              b = load_s15f16 (icc, length, offset + 12 + 2 * 2);
              c = load_s15f16 (icc, length, offset + 12 + 2 * 3);
              d = load_s15f16 (icc, length, offset + 12 + 2 * 4);
              e = load_s15f16 (icc, length, offset + 12 + 2 * 5);
              f = load_s15f16 (icc, length, offset + 12 + 2 * 6);
              fprintf (stdout, "parametric TRC sRGB type %.6f %.6f %.6f %.6f %.6f %.6f %.6f\n", g, a, b, c, d, e, f);
              if (exact)
              {
                int i;
                fprintf (stdout, "    exact:");
                for (i = 0; i < 7; i++)
                {
                  print_s15f16 (load_s15f16_ (icc, length, offset + 12 + 2 * i));
                  fprintf (stdout, " ");
                }
                fprintf (stdout, "\n");
              }
              break;
            default:
            fprintf (stdout, "unhandled parametric TRC type %i\n", function_type);
            break;
         }
       }
       else
       {
       fprintf (stdout, "rTRC count: %i  %s\n", count,  icc + offset);
       if (count == 0)
       {
         fprintf (stdout, "linear TRC\n");
       }
       else if (count == 1)
       {
         fprintf (stdout, "gamma TRC of: %.6f\n",
                load_u8f8 (icc,length, offset + 12));
         if (exact)
         {
            fprintf (stdout, " exact: ");
            print_u8f8 (load_u8f8_ (icc,length, offset + 12));
            fprintf (stdout, "\n");

         }
       }
       else for (i = 0; i < count && i < 10; i ++)
       {
         fprintf (stdout, "%i=%i ", i, load_u16 (icc, length, offset + 12 + i * 2));
         if (i % 7 == 0)
            fprintf (stdout, "\n");
       }
       }
     }
  }
  return 0;
}

static int 
load_icc (const char  *path, 
          char       **error)
{
  char *icc = NULL;
  long length = 0;
  int ret = 0;
  file_get_contents (path, &icc, &length, NULL);
  if (icc)
  {
    ret = load_icc_from_memory (icc, length, error);
    free (icc);
  }
  return ret;
}

static int
file_get_contents (const char  *path,
                   char       **contents,
                   long        *length,
                   void        *error)
{
  FILE *file;
  long  size;
  char *buffer;

  file = fopen (path,"rb");

  if (!file)
    return -1;

  fseek (file, 0, SEEK_END);
  *length = size = ftell (file);
  rewind (file);
  buffer = malloc(size + 8);

  if (!buffer)
    {
      fclose(file);
      return -1;
    }

  size -= fread (buffer, 1, size, file);
  if (size)
    {
      fclose (file);
      free (buffer);
      return -1;
    }
  fclose (file);
  *contents = buffer;
  return 0;
}

int 
main (int    argc, 
      char **argv)
{
  int i = 1;
  if (argc < 2)
  {
    fprintf (stdout, "usage: babl-icc-dump [options] <file1.icc [file2.icc ...]>\n");
    return -1;
  }

  if (argv[i] && (!strcmp (argv[i], "-e") ||
                  !strcmp (argv[i], "--exact")))
  {
    exact = 1;
    i++;
  }

  for (; argv[i]; i++)
  {
    char *error = NULL;
    fprintf (stdout, "\nfile: %s\n", argv[i]);
    load_icc (argv[i], &error);
    if (error)
    {
      fprintf (stdout, "icc-parse-problem: %s\n", error);
    }
  }

  return 0;
}