summaryrefslogtreecommitdiff
path: root/babl/babl-type.c
blob: d08a4f32420e5762f734fd648d61b9f3400bbd20 (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
/* 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 <string.h>
#include <stdarg.h>

#include "babl-internal.h"
#include "babl-db.h"


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


static Babl *
type_new (const char  *name,
          int          id,
          int          bits)
{
  Babl *babl;

  babl_assert (bits != 0);
  babl_assert (bits % 8 == 0);
  
  babl                = babl_malloc (sizeof (BablType) + strlen (name) + 1);
  babl->instance.name = (void*) babl + sizeof (BablType);
  babl->class_type    = BABL_TYPE;
  babl->instance.id   = id;
  strcpy (babl->instance.name, name);
  babl->type.bits     = bits;
  babl->type.from     = NULL;
  babl->type.to       = NULL;

  return babl;
}

Babl *
babl_type_new (void *first_arg,
               ...)
{
  va_list  varg;
  Babl    *babl;
  int      id         = 0;
  int      is_integer = 0;
  int      bits       = 0;
  long     min        = 0;
  long     max        = 255;
  double   min_val    = 0.0;
  double   max_val    = 0.0;

  const char *arg=first_arg;

  va_start (varg, first_arg);
  
  while (1)
    {
      arg = va_arg (varg, char *);
      if (!arg)
        break;
     
      if (BABL_IS_BABL (arg))
        {
#ifdef BABL_LOG
          Babl *babl = (Babl*)arg;

          babl_log ("%s unexpected", babl_class_name (babl->class_type));
#endif
        }
      /* 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, "bits"))
        {
          bits = va_arg (varg, int);
          min = 0;
          max = 1<<bits;
        }
      else if (!strcmp (arg, "integer"))
        {
          is_integer = va_arg (varg, int);
        }
      else if (!strcmp (arg, "min"))
        {
          min = va_arg (varg, long);
        }
      else if (!strcmp (arg, "max"))
        {
          max = va_arg (varg, long);
        }
      else if (!strcmp (arg, "min_val"))
        {
          min_val = va_arg (varg, double);
        }
      else if (!strcmp (arg, "max_val"))
        {
          max_val = va_arg (varg, double);
        }
      
      else
        {
          babl_fatal ("unhandled argument '%s' for format '%s'", arg, first_arg);
        }
    }
    
  va_end   (varg);

  babl = type_new (first_arg, id, bits);

  { 
    Babl *ret = babl_db_insert (db, babl);
    if (ret!=babl)
        babl_free (babl);
    return ret;
  }
}

BABL_CLASS_TEMPLATE (type)