summaryrefslogtreecommitdiff
path: root/babl/babl-model.c
blob: a876cc2da7dce222852f5c50b58ee18717b3bf38 (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
/* babl - dynamically extendable universal pixel conversion 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 2 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, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "babl-internal.h"
#include <string.h>
#include <stdarg.h>
#include "babl-db.h"


static int 
each_babl_model_destroy (Babl *babl,
                         void         *data)
{
  babl_free (babl->model.from);
  babl_free (babl->model.to);
  babl_free (babl);
  return 0;  /* continue iterating */
}

static char buf[512]="";

static const char *
create_name (const char     *name,
             int             components,
             BablComponent **component)
{
  char *p = buf;
  if (name)
    return name;
  while (components--)
    {
      sprintf (p, (*component)->instance.name);
      p+=strlen ((*component)->instance.name);
      component++;
    }
  return buf;
}

static Babl *
model_new (const char     *name,
           int             id,
           int             components,
           BablComponent **component)
{
  Babl *babl;

  babl                   = babl_malloc (sizeof (BablModel) +
                                        sizeof (BablComponent*) * (components) +
                                        strlen (name) + 1);
  babl->model.component = ((void*)babl) + sizeof (BablModel);
  babl->instance.name   = ((void*)babl->model.component) + sizeof (BablComponent*) * (components);
  
  babl->class_type       = BABL_MODEL;
  babl->instance.id      = id;
  babl->model.components = components;
  strcpy (babl->instance.name, name);
  memcpy (babl->model.component, component, sizeof (BablComponent*)*components);

  babl->model.from         = NULL;
  babl->model.to           = NULL;
  return babl;
}

Babl *
babl_model_new (void *first_argument,
                ...)
{
  va_list        varg;
  Babl          *babl;
  int            id          = 0;
  int            components  = 0;
  const char    *arg         = first_argument;
  const char    *name        = NULL;
  BablComponent *component [BABL_MAX_COMPONENTS];

  va_start (varg, first_argument);
  
  while (1)
    {


      if (BABL_IS_BABL (arg))
        {
          Babl *babl = (Babl*)arg;

          switch (babl->class_type)
            {
              case BABL_COMPONENT:
                component [components] = (BablComponent*) babl;
                components++;

                if (components>=BABL_MAX_COMPONENTS)
                  {
                    babl_log ("maximum number of components (%i) exceeded for %s",
                      BABL_MAX_COMPONENTS, name);
                  }
                break;
              case BABL_MODEL:
                  babl_log ("submodels not handled yet");
                  break;
              case BABL_TYPE:
              case BABL_SAMPLING:
              case BABL_INSTANCE:
              case BABL_FORMAT:


              case BABL_CONVERSION:
              case BABL_CONVERSION_TYPE:
              case BABL_CONVERSION_TYPE_PLANAR:
              case BABL_CONVERSION_MODEL_PLANAR:
              case BABL_CONVERSION_FORMAT:
              case BABL_CONVERSION_FORMAT_PLANAR:
              case BABL_FISH:
              case BABL_FISH_REFERENCE:
              case BABL_IMAGE:
                babl_log ("%s unexpected", babl_class_name (babl->class_type));
                break;
              case BABL_SKY: /* shut up compiler */
                break;
            }
        }
      /* if we didn't point to a babl, we assume arguments to be strings */
      else if (!strcmp (arg, "id"))
        {
          id = va_arg (varg, int);
        }

      else if (!strcmp (arg, "name"))
        {
          name = va_arg (varg, char *);
        }
      
      else
        {
          babl_fatal ("unhandled argument '%s' for babl_model '%s'", arg, name);
        }

      arg = va_arg (varg, char *);
      if (!arg)
        break;
    }
    
  va_end   (varg);

  babl = model_new (create_name (name, components, component), id, components, component);
  
  { 
    Babl *ret = db_insert (babl);
    if (ret!=babl)
        babl_free (babl);
    return ret;
  }
}

BABL_CLASS_TEMPLATE (model)