summaryrefslogtreecommitdiff
path: root/tools/babl-verify.c
blob: b76f707c8d3af764c5a33610d960d486966b900b (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
#include <stdio.h>
#include <stdlib.h>
#include "../config.h"
#include "babl/babl-internal.h"

//#define SPACE1 babl_space("ProPhoto")
//#define SPACE1 babl_space("Apple")
#define SPACE1 babl_space("sRGB")
//#define SPACE2 babl_space("Apple")

#ifdef _WIN32
/* On Windows setenv() does not exist, using _putenv_s() instead. The overwrite
 * arg is ignored (i.e. same as always 1).
 */
#define setenv(name,value,overwrite) _putenv_s(name, value)
#endif

int
file_get_contents (const char  *path,
                         char       **contents,
                         long        *length,
                         void        *error);
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;

  if (fseek (file, 0, SEEK_END) == -1 || (size = ftell (file)) == -1)
    {
      fclose (file);
      return -1;
    }
  if (length) *length = size;
  rewind (file);
  if ((size_t) size > SIZE_MAX - 8)
    {
      fclose (file);
      return -1;
    }
  buffer = calloc(size + 8, 1);

  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 final = 0;
  const Babl *fish;
  const Babl *SPACE2 = NULL;
  setenv ("BABL_INHIBIT_CACHE", "1", 1);

  if (argc < 3)
  {
    fprintf (stderr, "need two args, from and to babl-formats\n");
    return -1;
  }
  if (argc == 4)
    final = 1;

  if (!final)
  {
    putenv ("BABL_DEBUG_CONVERSIONS" "=" "1");
    putenv ("BABL_TOLERANCE"         "=" "0.2");
  }

  babl_init ();

//#define ICC_PATH "/tmp/my.icc"
//#define ICC_PATH "/usr/share/color/icc/colord/AppleRGB.icc"
//#define ICC_PATH "/tmp/ACEScg-elle-V2-labl.icc"
//#define ICC_PATH "/tmp/ACEScg-elle-V2-g10.icc"
//#define ICC_PATH "/tmp/ACEScg-elle-V4-g10.icc"
//#define ICC_PATH "/tmp/ACEScg-elle-V4-g22.icc"


  {
    //char *icc_data = NULL;
    //long     length = 0;
    //file_get_contents (ICC_PATH, &icc_data, &length, NULL);
    //SPACE2 = babl_space_from_icc (icc_data, length, BABL_ICC_INTENT_RELATIVE_COLORIMETRIC, NULL);
    SPACE2 = babl_space ("sRGB");
  }

  fish = babl_fish (babl_format_with_space(argv[1], SPACE1), babl_format_with_space (argv[2], SPACE2));
  if (!fish)
  {
    fprintf (stderr, "!!!! %s %s\n", argv[1], argv[2]);
    return -1;
  }

  if (final)
  switch (fish->class_type)
  {
    case BABL_FISH:
      fprintf (stderr, ">%s\n", babl_get_name (fish));
      break;
    case BABL_FISH_PATH:
      fprintf (stderr, "chosen %s to %s: steps: %i error: %.12f cost: %f\n", argv[1], argv[2], fish->fish_path.conversion_list->count, fish->fish.error, fish->fish_path.cost);
        for (int i = 0; i < fish->fish_path.conversion_list->count; i++)
          {
            fprintf (stderr, "\t%s (cost: %li)\n",
                      babl_get_name(fish->fish_path.conversion_list->items[i]  ), 
    fish->fish_path.conversion_list->items[i]->conversion.cost);
          }
      break;
  }


  babl_exit ();

  return 0;
}