summaryrefslogtreecommitdiff
path: root/babl/babl-fish.c
blob: ce22b6be037b03002af9c246379312c5f0832ab6 (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
/* babl - dynamically extendable universal pixel fish library.
 * Copyright (C) 2005, Øyvind Kolås.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, see
 * <https://www.gnu.org/licenses/>.
 */

#include "config.h"
#define NEEDS_BABL_DB
#include "babl-internal.h"
#include "babl-db.h"
#include <stddef.h>
#include <string.h>
#include <stdarg.h>

typedef struct _BablFindFish BablFindFish;

typedef struct _BablFindFish
{
  Babl       *fish_path;
  Babl       *fish_ref;
  Babl       *fish_fish;
  int        fishes;
  const Babl *source;
  const Babl *destination;
} _BablFishFish;


static int
match_conversion (Babl *conversion,
                  void *inout);

static int
find_fish_path (Babl *item,
                void *data);

static int
find_memcpy_fish (Babl *item,
                  void *data);

static int
find_fish_path (Babl *item,
                void *data)
{
  BablFindFish *ffish = (BablFindFish *) data;
  if ((item->fish.source == ffish->source) &&
      (item->fish.destination == ffish->destination))
    {
      if (item->instance.class_type == BABL_FISH_REFERENCE)
        {
          ffish->fish_ref = item;
          ffish->fishes++;
        }
      else if (item->instance.class_type == BABL_FISH_PATH)
        {
          ffish->fish_path = item;
          ffish->fishes++;
        }
      else if (item->instance.class_type == BABL_FISH)
        {
          ffish->fish_fish = item;
          ffish->fishes++;
        }
      if (ffish->fishes == 3)
        return 1;
    }

  return 0;
}

static int
find_memcpy_fish (Babl *item,
                  void *data)
{
  BablFindFish *ffish = (BablFindFish *) data;
  if ((item->fish.source == ffish->source) &&
      (item->fish.destination == ffish->destination))
    {
      if ((item->fish.source == item->fish.destination) &&
          (item->instance.class_type == BABL_FISH_REFERENCE))
        {
          ffish->fish_ref = item;
          return 1;
        }
    }

  return 0;
}

static int
match_conversion (Babl *conversion,
                  void *inout)
{
  void **data = inout;


  if ((Babl *) conversion->conversion.destination == (Babl *) *data)
    {
      *data = (void *) conversion;
      return 1;
    }

  return 0;
}



Babl *
babl_conversion_find (const void *source,
                      const void *destination)
{
  void *data = (void*)destination;

  if (BABL (source)->type.from_list)
    babl_list_each (BABL (source)->type.from_list, match_conversion, &data);
  if (data != (void*)destination) /* didn't change */
  {
    return data; /* found conversion */
  }
  data = NULL;

  if (BABL (source)->class_type == BABL_MODEL)
  {
     const Babl *srgb_source = BABL (source)->model.model ? BABL (source)->model.model:source;
     const Babl *srgb_destination = BABL (destination)->model.model ? BABL (destination)->model.model:destination;


     Babl *reference;

     if (srgb_source == source && srgb_destination == destination)
     {
        fprintf (stderr, "expected finding model conversion %s to %s", babl_get_name (source), babl_get_name (destination));
        return NULL;
     }

     reference = babl_conversion_find (srgb_source, srgb_destination);

  /* when conversions are sought between models, with non-sRGB chromaticities,
     we create the needed conversions from existing ones on the fly, and
     register them. The conversions registered by the models should pick up the
     RGB chromaticities, and TRC to use from the space on the model instead of
     hard-coding it.
   */

     switch (reference->instance.class_type)
     {
        case BABL_CONVERSION_LINEAR:
          return _conversion_new ("", 0, (void*)source, (void*)destination,
                            reference->conversion.function.linear,
                            NULL,
                            NULL,
                            reference->conversion.data, 1);
        case BABL_CONVERSION_PLANE:
          return _conversion_new ("", 0, source, destination,
                            NULL,
                            reference->conversion.function.plane,
                            NULL,
                            reference->conversion.data, 1);
        case BABL_CONVERSION_PLANAR:
          return _conversion_new ("", 0, source, destination,
                            NULL,
                            NULL,
                            reference->conversion.function.planar,
                            reference->conversion.data, 1);
     }
  }
  return NULL;
}

int
babl_fish_get_id (const Babl *source,
                  const Babl *destination)
{
  /* value of 'id' will be used as argument for hash function,
   * substraction serves as simple combination of
   * source/destination values. */
  int id = (((size_t)source * 93)) ^ ((size_t)destination);
  /* instances with id 0 won't be inserted into database */
  id *= ((((size_t)  (destination))) % 37);

  if (id == 0)
    id = 1;
  return id;
}

const Babl *
babl_fish (const void *source,
           const void *destination)
{
  const Babl *source_format      = NULL;
  const Babl *destination_format = NULL;

  babl_assert (source);
  babl_assert (destination);

  if (BABL_IS_BABL (source))
    source_format = source;

  if (!source_format)
    source_format = babl_format ((char *) source);

  if (!source_format)
    {
      babl_log ("args=(%p, %p) source format invalid", source, destination);
      return NULL;
    }

  if (BABL_IS_BABL (destination))
    destination_format = destination;

  if (!destination_format)
    destination_format = babl_format ((char *) destination);

  if (!destination_format)
    {
      babl_log ("args=(%p, %p) destination format invalid", source, destination);
      return NULL;
    }

  {
    int            hashval;
    BablHashTable *id_htable;
    BablFindFish   ffish = {(Babl *) NULL,
                            (Babl *) NULL,
                            (Babl *) NULL,
                            0,
                            (Babl *) NULL,
                            (Babl *) NULL};

    /* some vendor compilers can't compile non-constant elements of
     * compound struct initializers
     */
    ffish.source = source_format;
    ffish.destination = destination_format;

    id_htable = (babl_fish_db ())->id_hash;
    hashval = babl_hash_by_int (id_htable, babl_fish_get_id (source_format, destination_format));

    if (source_format == destination_format)
      {
        /* In the case of equal source and destination formats
         * we will search through the fish database for reference fish
         * to handle the memcpy */
        babl_hash_table_find (id_htable, hashval, find_memcpy_fish, (void *) &ffish);
      }
    else
      {
        /* In the case of different source and destination formats
         * we will search through the fish database for appropriate fish path
         * to handle the conversion. In the case that preexistent
         * fish path is found, we'll return it. In the case BABL_FISH
         * instance with the same source/destination is found, we'll
         * return reference fish.
         * In the case neither fish path nor BABL_FISH path are found,
         * we'll try to construct new fish path for requested
         * source/destination. In the case new fish path is found, we'll
         * return it, otherwise we'll create dummy BABL_FISH instance and
         * insert it into the fish database to indicate non-existent fish
         * path.
         */
        babl_hash_table_find (id_htable, hashval, find_fish_path, (void *) &ffish);
        if (ffish.fish_path)
          {
            /* we have found suitable fish path in the database */
            return ffish.fish_path;
          }

        babl_mutex_lock (babl_fish_mutex);
        /* do a second look in the database, in case another thread held the
           mutex and made the fish
         */
        if (!ffish.fish_fish)
        {
        babl_hash_table_find (id_htable, hashval, find_fish_path, (void *) &ffish);
        if (ffish.fish_path)
          {
            /* we have found suitable fish path in the database */
            babl_mutex_unlock (babl_fish_mutex);
            return ffish.fish_path;
          }
        }

        if (!ffish.fish_fish)
          {
            const Babl *src_space = (void*)source_format->format.space;
            const Babl *dst_space = (void*)destination_format->format.space;
            /* we haven't tried to search for suitable path yet */

            if (!babl_space_is_cmyk (src_space) &&
                !babl_space_is_cmyk (dst_space))
              {
                Babl *fish_path = babl_fish_path (source_format, destination_format);

                if (fish_path)
                  {
                    babl_mutex_unlock (babl_fish_mutex);
                    return fish_path;
                  }
#if 1
                else
                  {
                    /* there isn't a suitable path for requested formats,
                     * let's create a dummy BABL_FISH instance and insert
                     * it into the fish database to indicate that such path
                     * does not exist.
                     */
                    char *name = "X"; /* name does not matter */
                    Babl *fish = babl_calloc (1, sizeof (BablFish) + strlen (name) + 1);

                    fish->class_type                = BABL_FISH;
                    fish->instance.id               = babl_fish_get_id (source_format, destination_format);
                    fish->instance.name             = ((char *) fish) + sizeof (BablFish);
                    strcpy (fish->instance.name, name);
                    fish->fish.source               = source_format;
                    fish->fish.destination          = destination_format;
                    babl_db_insert (babl_fish_db (), fish);
                  }
#endif
                }
          }
        else if (ffish.fish_fish->fish.data)
          {
            /* the dummy fish was created by the cache, and we need to manually
             * show a "missing fast path" warning for it on the first lookup.
             */
#if 0
            _babl_fish_missing_fast_path_warning (ffish.fish_fish->fish.source,
                                                  ffish.fish_fish->fish.destination);
#endif

            ffish.fish_fish->fish.data = NULL;
          }
      }

    if (ffish.fish_ref)
      {
        /* we have already found suitable reference fish */
        babl_mutex_unlock (babl_fish_mutex);
        return ffish.fish_ref;
      }
    else
      {
        /* we have to create new reference fish */
        Babl *ret = babl_fish_reference (source_format, destination_format);
        babl_mutex_unlock (babl_fish_mutex);
        return ret;
      }
  }
}

BABL_CLASS_MINIMAL_IMPLEMENT (fish);